import numpy as np
import math
import random
import sys

def loadInputFile(fileName):
    data = []
    ## prva linija u data su headeri
    with open(fileName, 'r') as f:
        for line in f:
            splitano = line.split(',')
            if (len(data)>0):
                for i in range(len(splitano)):
                    splitano[i] = float(splitano[i])
            data.append(splitano)
    return data

def obradi(data, dimensions, functions, primjerak_populacije):
    # propagiraj sve testove iz data do kraja
    # vrati pogresku
    rezultati = propagirajUnaprijed(dimensions, primjerak_populacije.matricaTezina, primjerak_populacije.matricaOsjetljivosti, data, functions)
    pogreska = izracunajPogresku(data, rezultati)
    return pogreska

class Primjerak_Populacije:
    ## matricaTezina oblikovana na nacin da je tezina iz cvora 1 iz prvog sloja do cvora 3 iz drugog sloja
    ## = matricaTezina[0][0][2]

    ## matricaOsjetljivosti oblikovana nacin da je osjetljivost cvora 2 u drugom sloju = matricaOsjetljivosti[1][1]
    def __init__(self, dimensions=None):
        if(dimensions == None):
            self.matricaTezina = None
            self.matricaOsjetljivosti = None
        else:
            self.matricaTezina = kreirajRandomMatricuTezine(dimensions)
            self.matricaOsjetljivosti = kreirajRandomMatricuOsjetljivosti(dimensions)

def propagirajUnaprijed(dimensions, matricaTezina, matricaOsjetljivosti, data, funkcije):
    ## propagira ulaz testova kroz neuronsku mrezu do izlaza
    ## vraca vrijednosti izlaza za svaki test
    ## koristen algoritam s predavanja s mnozenjem matrica
    koncaniRezultati = []
    for u in range(1, len(data)):
        rezultati = []
        for i in range(len(dimensions)-1):
            tezine = np.array(matricaTezina[i]).transpose()
            if(i == 0):
                podaci = np.array(data[u][:-1]).transpose()
            else:
                podaci = rezultati[i-1]
            osjetljivosti = np.array(matricaOsjetljivosti[i+1]).transpose()
            rezultat = np.add(np.matmul(tezine, podaci), osjetljivosti)
            if(i<len(funkcije)):
                for j in range(len(rezultat)):
                    ## primjenimo funkciju
                    ## s = sigmoida
                    if(funkcije[i] == "s"):
                        rezultat[j] = 1/(1+math.pow(math.e, -rezultat[j]))
            rezultati.append(rezultat)
        koncaniRezultati.append(rezultati[-1][0])
    return koncaniRezultati

def izracunajPogresku(data, rezultati):
    ## usporeduje nase dobivene rezultate sa zadnjim stupcem iz data (tocni rezultati)
    ## vraca srednju sumu razlike kvadrata
    tocniRezultati = []
    for i in range(1, len(data)):
        tocniRezultati.append(data[i][-1])
    zbrojRazlikeKvadrata = 0
    for i in range(len(rezultati)):
        zbrojRazlikeKvadrata += math.pow(tocniRezultati[i]-rezultati[i],2)
    toReturn = (1/len(rezultati))*zbrojRazlikeKvadrata
    return toReturn

def kreirajRandomMatricuTezine(dimensions):
    ## matrica oblikovana na nacin da je tezina iz cvora 1 iz prvog sloja do cvora 3 iz drugog sloja
    ## = matricaTezina[0][0][2]
    ulaznaDimenzija = dimensions[0]
    srednjeDimenzije = dimensions[1:-1]
    izlaznaDimenzija = dimensions[-1]
    matricaTezina = []
    tezinaSloj = []
    for i in range (ulaznaDimenzija):
        ## broj tezina za svaki neuron s lijeve strane je broj neurona s desne strane
        tezinaSloj.append(np.random.normal(0, 0.01, srednjeDimenzije[0]))
    matricaTezina.append(tezinaSloj)
    for i in range (len(srednjeDimenzije)):
        tezinaSloj = []
        if(i == len(srednjeDimenzije)-1):
            for j in range(srednjeDimenzije[i]):
                ## broj tezina za svaki neuron s lijeve strane je broj neurona na izlazu (1)
                tezinaSloj.append(np.random.normal(0, 0.01, izlaznaDimenzija))
        else:
            for j in range(srednjeDimenzije[i]):
                ## broj tezina za svaki neuron s lijeve strane je broj neurona s desne strane
                tezinaSloj.append(np.random.normal(0, 0.01, srednjeDimenzije[i+1]))
        matricaTezina.append(tezinaSloj)
    return matricaTezina

def kreirajRandomMatricuOsjetljivosti(dimensions):
    ## matricaOsjetljivosti oblikovana nacin da je osjetljivost cvora 2 u drugom sloju = matricaOsjetljivosti[1][1]
    ulaznaDimenzija = dimensions[0]
    srednjeDimenzije = dimensions[1:-1]
    izlaznaDimenzija = dimensions[-1]
    matricaOsjetljivosti = []

    matricaOsjetljivosti.append([0] * ulaznaDimenzija) ## osjetljivost ulaznih je 0
    for i in range(len(srednjeDimenzije)):
        ## broj "osjetljivosti" je velicina dimenzije
        matricaOsjetljivosti.append(np.random.normal(0, 0.01, srednjeDimenzije[i]))
    matricaOsjetljivosti.append(np.random.normal(0, 0.01, izlaznaDimenzija))

    return matricaOsjetljivosti

def genetskiAlgoritam(data, dimensions, functions, velPop, elitizam, vjer_mutacija, skala_mutacije, broj_iteracija):
    ## pracen algoritam s predavanja
    ## prvo generira nasumicnu populaciju
    ## zatim prema ruletu selektira parove iz generacije te ih kriza
    ## krizanje se radi uzimanjem aritmeticke sredine
    ## nakon krizanja mutira, svaki kromosom ima vjer_mutacija vrijednosti da mutira
    ## mutira se tako da se kromosomu pribroji vrijednost iz normalne razdiobe sa srednjom devijacijom skala_mutacije
    ## radi se broj_iteracija iteracija
    populacija = []
    rezultati = []
    sumaRezultata = 0
    ## generiramo nasumicnu populaciju
    for i in range(velPop):
        noviPrimjerak = Primjerak_Populacije(dimensions) ## ovo generira nasumican primjerak
        populacija.append(noviPrimjerak)
        rezultat = 1/obradi(data, dimensions, functions, noviPrimjerak)
        sumaRezultata += rezultat
        rezultati.append(rezultat)

    for i in range(broj_iteracija):
        if(i>0 and i%2000==0):
            print("[Train error @" + str(i) + "]: " + "{:.6f}".format(1/max(rezultati))) ##svakih 2000 iteracija ispisuje
        nova_populacija = []

        ## uzima elitizam najboljih jedinki i odmah ih doda u novu populaciju
        if(elitizam == 1):
            nova_populacija.append(populacija[rezultati.index(max(rezultati))])
        else:
            sortirani = sorted(rezultati, reverse=True)
            for j in range(elitizam):
                nova_populacija.append(populacija[rezultati.index(sortirani[j])])

        while len(nova_populacija) < velPop:
            zaKrizanje1, zaKrizanje2 = nadjiKandidateZaKrizanje(populacija, rezultati, sumaRezultata) ## metodom ruleta
            krizani1, krizani2 = krizaj(zaKrizanje1, zaKrizanje2), krizaj(zaKrizanje1, zaKrizanje2) ## aritmeticka sredina
            mutiraj(krizani1, vjer_mutacija, skala_mutacije) ## random
            mutiraj(krizani2, vjer_mutacija, skala_mutacije)
            nova_populacija.append(krizani1)
            nova_populacija.append(krizani2)

        populacija = nova_populacija
        rezultati = []
        sumaRezultata = 0
        ## evaluiramo novu populaciju
        for i in range(velPop):
            rezultat = 1/obradi(data, dimensions, functions, populacija[i])
            sumaRezultata += rezultat
            rezultati.append(rezultat)

    maxRez = max(rezultati)
    print("[Train error @" + str(broj_iteracija) + "]: " + "{:.6f}".format(1/maxRez))
    pobjednik = populacija[rezultati.index(maxRez)]
    return pobjednik

def mutiraj(primjerak_populacije, vjer_mutacija, skala_mutacije):
    ## dodji do kromosoma, "baci kockicu", dodaj mu random vrijednosti
    for i in range(len(primjerak_populacije.matricaTezina)):
        for j in range(len(primjerak_populacije.matricaTezina[i])):
            ## probamo mutirati
                randomBroj = random.random()
                if(randomBroj < vjer_mutacija):
                    ## mutiramo
                    primjerak_populacije.matricaTezina[i][j] += np.random.normal(0, skala_mutacije, len(primjerak_populacije.matricaTezina[i][j]))
    for i in range(len(primjerak_populacije.matricaOsjetljivosti)):
        ## probamo mutirati
            randomBroj = random.random()
            if(randomBroj < vjer_mutacija):
                ## mutiramo
                primjerak_populacije.matricaOsjetljivosti[i] += np.random.normal(0, skala_mutacije, len(primjerak_populacije.matricaOsjetljivosti[i]))
    return

def nadjiKandidateZaKrizanje(populacija, rezultati, sumaRezultata):
    ## sto je primjerak populacije imao bolji rezultat, to je veca vjerojatnost da ga izabere
    randomBroj = random.random()
    toReturn1, toReturn2 = None, None
    ## iteriramo, smanjujemo randomBroj za velicinu rezultata, kad se smanji ispod nule izaberemo taj
    for i in range(len(rezultati)):
        vjerojatnost_krizanja = (rezultati[i]/sumaRezultata)
        randomBroj -= vjerojatnost_krizanja
        if(randomBroj<=0):
            toReturn1 = populacija[i]
            break

    ## isto kao i gore jos jedamput
    randomBroj = random.random()
    for i in range(len(rezultati)):
        vjerojatnost_krizanja = (rezultati[i]/sumaRezultata)
        randomBroj -= vjerojatnost_krizanja
        if(randomBroj<=0):
            toReturn2 = populacija[i]
            break
    return toReturn1, toReturn2

def krizaj(primjerakPopulacije1, primjerakPopulacije2):
    ## zbrojimo matrice i podijelimo ih s 2
    novaMatricaTezina = []
    for i in range(len(primjerakPopulacije1.matricaTezina)):
        ## ovaj for je potreban jer inace nisu svi array-evi iste velicine pa se numpy zali
        novaMatricaTezina.append(np.add(primjerakPopulacije1.matricaTezina[i], primjerakPopulacije2.matricaTezina[i])/2)

    novaMatricaOsjetljivosti = []
    for i in range(len(primjerakPopulacije1.matricaOsjetljivosti)):
        novaMatricaOsjetljivosti.append(np.add(primjerakPopulacije1.matricaOsjetljivosti[i], primjerakPopulacije2.matricaOsjetljivosti[i])/2)

    noviPrimjerak = Primjerak_Populacije(None)
    noviPrimjerak.matricaTezina = novaMatricaTezina
    noviPrimjerak.matricaOsjetljivosti = novaMatricaOsjetljivosti
    return noviPrimjerak

args = sys.argv[1:]

trainFileName = ""
testFileName = ""
arhitektura = ""
popsize = 0
elitism = 0
vjer_mutacije = 0
skala_mutacije = 0
broj_iteracija = 0
for i in range(len(args)):
    if(args[i] == "--train"):
        trainFileName = args[i+1]
        i+=1
        continue
    if(args[i] == "--test"):
        testFileName = args[i+1]
        i+=1
        continue
    if(args[i] == "--nn"):
        arhitektura = args[i+1]
        i+=1
        continue
    if(args[i] == "--popsize"):
        popsize = int(args[i+1])
        i+=1
        continue
    if(args[i] == "--elitism"):
        elitism = int(args[i+1])
        i+=1
        continue
    if(args[i] == "--p"):
        vjer_mutacije = float(args[i+1])
        i+=1
        continue
    if(args[i] == "--K"):
        skala_mutacije = float(args[i+1])
        i+=1
        continue
    if(args[i] == "--iter"):
        broj_iteracija = int(args[i+1])
        i+=1
        continue

data = loadInputFile(trainFileName)

headers = data[0]
data = data[1:]
ulaznaDimenzija = len(headers)-1
izlaznaDimenzija = 1
dimensions = [ulaznaDimenzija]
functions = []
broj = 0
for i in range(0,len(arhitektura)):
    if(arhitektura[i]>='0' and arhitektura[i]<='9'):
        broj*=10
        broj+=int(arhitektura[i])
    else:
        dimensions.append(broj)
        functions.append(arhitektura[i])
        broj=0
dimensions.append(izlaznaDimenzija)

pobjednik = genetskiAlgoritam(data, dimensions, functions, popsize, elitism, vjer_mutacije, skala_mutacije, broj_iteracija)

testData = loadInputFile(testFileName)
data = data[1:]

testGreska = obradi(testData, dimensions, functions, pobjednik)
print("[Test error]: {:.6f}".format(testGreska))


import os
from bs4 import BeautifulSoup
from pprint import pprint
import requests

class ZillowScrape():
    def __init__(self):
        self.zillow_response = requests.get(url=os.environ.get("ZILLOW_CLONE_LINK"))
        self.zillow_response_text = self.zillow_response.text

        self.soup = BeautifulSoup(self.zillow_response_text, "html.parser")

    def get_links(self):
        list_of_links = self.soup.select('.property-card-link')
        links = [href.get('href') for href in list_of_links]

        pprint(links)
        return links

    def get_prices(self):
        list_of_prices = self.soup.select('.PropertyCardWrapper__StyledPriceLine')
        prices = [int(price.getText().split('/')[0].split('+')[0].split('$')[1].replace(",", "").replace('+', "")) for price in list_of_prices]
        pprint(prices)
        return prices

    def get_address(self):
        list_of_addresses = self.soup.select('address')
        addresses = [address.getText().replace('\n', "").strip() for address in list_of_addresses]
        pprint(addresses)
        return addresses

    def build_data(self):
        links = self.get_links()
        prices = self.get_prices()
        addresses = self.get_address()

        data = {}

        for _ in range(len(links)):
            data[_] = {
                "Link": links[_],
                "Price": prices[_],
                'Address': addresses[_]
            }

        pprint(data)
        return data

zillow = ZillowScrape()

zillow.build_data()


from flask import Flask, request, render_template
import call_list
import call_list_update
import call_list_management
import call_list_delete
import os

app = Flask(__name__)

@app.route('/')
def hello():
  return '''<!DOCTYPE HTML>

    <title>Flask app</title>

GS Neotek

    리스트 갱신

    캠페인 시작

    리스트 갱신 및 캠페인 시작

    주기 생성

    리스트 삭제

'''

@app.route('/call_list_update')
def update():
  return call_list_update.run()

@app.route('/crontab')
def crontab():
  return '''<!DOCTYPE HTML>

    <title>Flask app</title>

GS Neotek

현재 주기는 평일 15시입니다. 계속하시겠습니까?

    계속하기
    삭제하기

'''

@app.route('/crontab/complete')
def complete():
  os.system('(crontab -l 2>/dev/null; echo "0 15 * * 1-5 /home/vagrant/call_list_update.py") | crontab -')
  return '''<!DOCTYPE HTML>

    <title>Flask app</title>

GS Neotek

Complete!!

'''

@app.route('/crontab/delete')
def delete():
  os.system('crontab -r')
  return '''<!DOCTYPE HTML>

    <title>Flask app</title>

GS Neotek

Complete!!

'''

@app.route('/campaign')
def campaign():

  return call_list_management.run()

@app.route('/all')
def all():

  return call_list.run()

@app.route('/delete')
def user_del():

  return render_template('delete.html')

@app.route('/delete/complete', methods=['GET'])
def user_del_comp():
  tmp = request.args.get('delete')

  return call_list_delete.run(tmp)

if __name__ == '__main__':
  app.run()


import os,time
import win32com.client

class Utility:
    email_outlook = win32com.client.Dispatch("Outlook.Application")

    @staticmethod
    def find_latest_file(path):
        files = (fle for rt, _, f in os.walk(path) for fle in f if time.time() - os.stat(os.path.join(rt, fle)).st_mtime < 120)
        return list(files)

    @staticmethod
    def send_message(to, subject, body):
        mail = outlook.CreateItem(0)
        mail.Recipients.Add(to)
        mail.Subject = subject
        mail.Body = body
        mail.Send()

    @staticmethod
    def send_message_attach(to, body, subject, attach_files):
        mail = Utility.email_outlook.CreateItem(0)
        mail.Recipients.Add(to)
        mail.Subject = subject
        mail.Body = body
        for attach in attach_files:
            mail.Attachments.Add(attach)
        #mail.Send()
        mail.Display(True)

    @staticmethod
    def send_email(config):
        to_email, body, subject = config.email_data
        file_list = Utility.find_latest_file(config.get_show_output_path)
        file_path_list = []
        for file_path in file_list:
            file_path_list.append('{}/{}'.format(config.get_show_output_path,file_path))
        Utility.send_message_attach(to_email, body, subject, file_path_list)

if __name__ == "__main__":
    Utility.find_latest_file('C:/Users/imran.mohammed/PycharmProjects/Automation/configfiles/showoutput')


from classes.config import Config
from .notification import Notification
from jinja2 import Template
from pytz import timezone
import socket
import logging
import boto3
import hashlib
from lib import HTML

class AWSInstanceStatusEvent(object):

    def __init__(self, instance_id, event):
        self._config = Config().get_config()
        self._instance_id = instance_id
        self._event = event
        self.set_digest()

    def is_complete(self):
        if "Completed" in self._event['Description']: self._completed = True
        else: self._completed = False
        return self._completed

    def set_event_saserver_details(self, servers):
        logging.info('START: set_event_saserver_details')
        server_details = servers.get_saserver_details()
        if ( self._instance_id in server_details.keys() ):
            self._server = server_details[self._instance_id]
            logging.info('AWSInstanceStatusEvent: set_event_saserver_details: Found server: ' + str(self._server))
        else:
            self._server = None
        logging.info('AWSInstanceStatusEvent: set_event_saserver_details: Has server: ' + str(self.has_server()))

    def has_server(self):
        return True if ( self._server ) else False

    def set_digest(self):
        self._digest = hashlib.sha224(
            self._instance_id.encode('utf-8')
            + self._event['Code'].encode('utf-8')
        ).hexdigest()
        logging.info('AWSInstanceStatusEvent: digest: ' + str(self._digest))

    def get_digest(self):
        return self._digest

    def set_is_new(self, digests):
        last_run_digests = digests.get_last_run_digests()
        if ( self._digest in last_run_digests ):
            self._new_event = False
        else:
            self._new_event = True
        logging.info('AWSInstanceStatusEvent: Is new: ' + str(self._new_event))

    def is_new(self):
        return self._new_event

    def get_owners(self):
        if ( self._server is None ):
            return []
        else:
            return self._server['owners']

    def get_owners_str(self):
        if ( self._server is None ):
            return 'N/A'
        else:
            return ', '.join(self.get_owners())

    def get_owners_email_addrs(self):
        if ( self._server is None ):
            return ['paul.leddy@jpl.nasa.gov']
        else:
            return map(lambda x: x + '@jpl.nasa.gov', self._server['owners'])

    def get_owners_email_addrs_str(self):
        return ', '.join(self.get_owners_email_addrs())

    def get_cog_admin(self):
        if ( self._server is None ):
            return 'N/A'
        else:
            return self._server['cog_admin']

    def get_html_message_to_owners(self):
        return self.get_html_template()

    def get_text_message_to_owners(self):
        return 'You got mail: ' + self._instance_id + '. STOP.'

    def get_not_before_date_str(self):
        return self._event['NotBefore'].astimezone(timezone('US/Pacific')).strftime("%Y-%m-%d %H:%M %Z") if ( 'NotBefore' in self._event ) else 'N/A'

    def get_not_after_date_str(self):
        return self._event['NotAfter'].astimezone(timezone('US/Pacific')).strftime("%Y-%m-%d %H:%M %Z") if ( 'NotAfter' in self._event ) else 'N/A'

    def set_name(self, name):
        self._name = name

    def get_vname(self):
        if ( self._server is None ):
            return self._instance_id
        if ( self._server['hostname'] is not None and self._server['domain'] is not None ):
            return self._server['hostname'] + '.' + self._server['domain']
        elif ( ( self._server['hostname'] is None or self._server['domain'] is None) and self._server['device_name'] is not None ):
            return self._server['device_name']
        elif ( self._server['aws_acct_type'] is 'GOV' and self._server['ip_address'] is not None):
             return self._server['ip_address']
        elif ( self._server['device_name'] is None and self._server['aws_acct_type'] is 'EC2' and self._server['ip_address'] is not None ):
            return self._server['ip_address']
        elif ( self._name is not None ):
            return self._name
        else:
            return self._instance_id

    def send_notification_to_owners(self):
        subject='Instance Status Event Notification: ' + self.get_vname()
        email_to = self._config['general']['sa_recipients'] if ( self._config['general']['POC'] ) else self.get_owners_email_addrs()
        email_attrs = {
            'email_type': self._config['general']['email_type'],
            'email_to': email_to,
            'email_from': self._config['general']['email_from'],
            'subject': subject,
            'html': self.get_html_message_to_owners(),
            'text': self.get_text_message_to_owners()
        }
        Notification('email', email_attrs)

    def get_html_template(self):
        template_raw = '''

DEV PHASE

To: {{ owners_email_addrs_str }}

Hello,

Your Amazon VM, {{ vname }}, is scheduled for maintenance by Amazon.  An outage will be incurred during the maintenance window below.  Please reply to this email if you would like to resolve the maintenance at a time before the scheduled maintenance window.  Resolving the maintenance requirement ahead of time will involve powering down your VM, and powering it back on, or merely a reboot, depending on the maintenance type.

Maintenance Details:

VM: {{ vname }}

Instance: {{ instance_id }}

Description: {{ maintenance_desc }}

Maintenance window start time: {{ not_before }}

Maintenance window end time: {{ not_after }}

Owners: {{ owners_str }}

Thank you,

            Infrastructure SA Team

            jplis-sa@jpl.nasa.gov

            '''

        template = Template(p_template_raw)
        html = template.render(
            owners_email_addrs_str = self.get_owners_email_addrs_str(),
            vname = self.get_vname(),
            instance_id = self._instance_id,
            not_before = self.get_not_before_date_str(),
            not_after = self.get_not_after_date_str(),
            maintenance_desc = self._event['Description'],
            owners_str = self.get_owners_str(),
        )

        return html

    def get_sa_report_row(self):
        return [self._name, self.get_cog_admin(), self._instance_id, self._event['Description'], self.get_not_before_date_str(), self.get_not_after_date_str(), self.get_owners_str()]

# END


import cv2
import numpy as np
from shapely.geometry import LineString, Point

import mmocr.utils as utils
from .box_utils import sort_vertex

def box_jitter(points_x, points_y, jitter_ratio_x=0.5, jitter_ratio_y=0.1):
    """Jitter on the coordinates of bounding box.

    Args:
        points_x (list[float | int]): List of y for four vertices.
        points_y (list[float | int]): List of x for four vertices.
        jitter_ratio_x (float): Horizontal jitter ratio relative to the height.
        jitter_ratio_y (float): Vertical jitter ratio relative to the height.
    """
    assert len(points_x) == 4
    assert len(points_y) == 4
    assert isinstance(jitter_ratio_x, float)
    assert isinstance(jitter_ratio_y, float)
    assert 0 <= jitter_ratio_x < 1
    assert 0 <= jitter_ratio_y < 1

    points = [Point(points_x[i], points_y[i]) for i in range(4)]
    line_list = [
        LineString([points[i], points[i + 1 if i < 3 else 0]])
        for i in range(4)
    ]

    tmp_h = max(line_list[1].length, line_list[3].length)

    for i in range(4):
        jitter_pixel_x = (np.random.rand() - 0.5) * 2 * jitter_ratio_x * tmp_h
        jitter_pixel_y = (np.random.rand() - 0.5) * 2 * jitter_ratio_y * tmp_h
        points_x[i] += jitter_pixel_x
        points_y[i] += jitter_pixel_y

def warp_img(src_img,
             box,
             jitter_flag=False,
             jitter_ratio_x=0.5,
             jitter_ratio_y=0.1):
    """Crop box area from image using opencv warpPerspective w/o box jitter.

    Args:
        src_img (np.array): Image before cropping.
        box (list[float | int]): Coordinates of quadrangle.
    """
    assert utils.is_type_list(box, float) or utils.is_type_list(box, int)
    assert len(box) == 8

    h, w = src_img.shape[:2]
    points_x = [min(max(x, 0), w) for x in box[0:8:2]]
    points_y = [min(max(y, 0), h) for y in box[1:9:2]]

    points_x, points_y = sort_vertex(points_x, points_y)

    if jitter_flag:
        box_jitter(
            points_x,
            points_y,
            jitter_ratio_x=jitter_ratio_x,
            jitter_ratio_y=jitter_ratio_y)

    points = [Point(points_x[i], points_y[i]) for i in range(4)]
    edges = [
        LineString([points[i], points[i + 1 if i < 3 else 0]])
        for i in range(4)
    ]

    pts1 = np.float32([[points[i].x, points[i].y] for i in range(4)])
    box_width = max(edges[0].length, edges[2].length)
    box_height = max(edges[1].length, edges[3].length)

    pts2 = np.float32([[0, 0], [box_width, 0], [box_width, box_height],
                       [0, box_height]])
    M = cv2.getPerspectiveTransform(pts1, pts2)
    dst_img = cv2.warpPerspective(src_img, M,
                                  (int(box_width), int(box_height)))

    return dst_img

def crop_img(src_img, box):
    """Crop box area to rectangle.

    Args:
        src_img (np.array): Image before crop.
        box (list[float | int]): Points of quadrangle.
    """
    assert utils.is_type_list(box, float) or utils.is_type_list(box, int)
    assert len(box) == 8

    h, w = src_img.shape[:2]
    points_x = [min(max(x, 0), w) for x in box[0:8:2]]
    points_y = [min(max(y, 0), h) for y in box[1:9:2]]

    left = int(min(points_x))
    top = int(min(points_y))
    right = int(max(points_x))
    bottom = int(max(points_y))

    dst_img = src_img[top:bottom, left:right]

    return dst_img


# MPCI TD06
# groupe: CHIBOUT CLAUDE
# Lien git: https://github.com/uvsq22005256/generation_terrain

# Explication du programme:
# Alors nous avons ici un programme qui nous permet de générer des terrains de manière 'aléatoire'
# Et d'y positionner un personnage que nous allons pouvoir
# faire bouger à l'aide des flêches directionnelles du clavier.
# Le personnage ne peux pas apparaitre sur de l'eau et ne peut pas y aller.
# De sauvegarder ces terrains et de les charger à notre guise.
# Les case d'eau sont représenté en bleu et les case de terre en gris
#
# clique sur le terrain créer le personnage si le clique n'est pas sur une case d'eau
# Bouton 'Génerer map': génère un nouveau terrain
# Bouton 'Supprimer map': Supprime le terrain de l'écran
# (Attention ça supprimera la map de l'écran mais pas de vos fichier il faudra le faire manuellement,
#  s'il provient d'une sauvegarde)
# Bouton 'Paramètres': Ouvre la fenetre de modification des paramètre de génération de terrain
# Ceci vas nous ouvrir une fenêtre qui nous permet de modifier les paramètre de génération du terrain
# Les paramètre en question:
# (
# Taille: pour la longueur des côtés du terrain en blocs
# p: la probabilitée qu'une case soit de l'eau à l'origine
# n: le nombre de répétition de l'automate
# k: L'ordre du voisinage de Moore
# T: Le comparateur au voisinage de moore ( si la valeur du voisinage est supérieure ou égale à T,
# alors une case reste ou est convertie en eau)
# )
# On clique sur 'Valider' une fois les paramètres entré sinon il ne sont pas prit en compte.
# Bouton 'Annuler dernier déplacement': Pour annuler le dernier déplacement du personnage lorqu'il y en a un
# Bouton 'Sauvegarder map': Sauvegarde la map dans un fichier au format csv sous le nom qu'on lui donne
# Bouton 'Charger map': Charge un map à l'aide du fichier csv choisit générer par le programme au préalable

# Les imports
import tkinter as tk
import random
from math import floor
from tkinter.filedialog import asksaveasfilename
import csv

# CONSTANTES
block = ["Eau", "Terre"]
block_color = ["Blue", "gray"]
taille_case = 10

# Variables
length = 50
p = 0.5
n = 4
T = 5
k = 1
perso = None
perso_coord = [0, 0]
persoAlive = False
perso_hist_pos = [] #contient: '[v0, v1],..' avec v0 qui prend (0,1)<=>(x,y) et v1 qui prend (0,1)<=>(-,+)

save_grille = []

def createPerso(pos):
    """
    On créer le personnage et affectons les valeurs des variables du personnage
    Evidemment ceci n'est possible que s'il n'éxiste aucun personnage sur le terrain
    """
    global persoAlive, perso, perso_coord
    if persoAlive: return
    pos = [floor(pos.x/taille_case), floor(pos.y/taille_case)]
    c = canvas.itemcget(items[pos[1]][pos[0]], "fill")
    print(c)
    if c == block_color[0]:
        print("Aie")
        return
    perso = canvas.create_rectangle(taille_case*pos[0],
                            taille_case*pos[1],
                            taille_case*pos[0]+taille_case,
                            taille_case*pos[1]+taille_case,
                            fill="red")

    perso_coord = pos
    persoAlive = True

def deletePerso():
    """
    Suppressions du perso, reset des variables du perso
    """
    global persoAlive, perso, perso_hist_pos
    if not persoAlive: return
    canvas.delete(perso)
    perso_coord = [0, 0]
    perso = None
    persoAlive = False
    perso_hist_pos = []

def movePerso(event):
    """
    Traitement de la demande de déplacement du perso
    """
    global persoAlive, perso, perso_coord
    if not persoAlive: return
    if event.keysym == "Up" and perso_coord[1] != 0:
        try:
            c = canvas.itemcget(items[perso_coord[1]-1][perso_coord[0]], "fill")
            if c == block_color[1]:
                perso_coord[1] -= 1
                canvas.move(perso, 0, -taille_case)
                perso_hist_pos.append([1, 0])
        except:
            pass
    elif event.keysym == "Down":
        try:
            c = canvas.itemcget(items[perso_coord[1]+1][perso_coord[0]], "fill")
            if c == block_color[1]:
                perso_coord[1] += 1
                canvas.move(perso, 0, taille_case)
                perso_hist_pos.append([1, 1])
        except:
            pass
    elif event.keysym == "Left" and perso_coord[0] != 0:
        try:
            c = canvas.itemcget(items[perso_coord[1]][perso_coord[0]-1], "fill")
            if c == block_color[1]:
                perso_coord[0] -= 1
                canvas.move(perso, -taille_case, 0)
                perso_hist_pos.append([0, 0])
        except:
            pass
    elif event.keysym == "Right":
        try:
            c = canvas.itemcget(items[perso_coord[1]][perso_coord[0]+1], "fill")
            if c == block_color[1]:
                perso_coord[0] += 1
                canvas.move(perso, taille_case, 0)
                perso_hist_pos.append([0, 1])
        except:
            pass

def undoPersoPosition():
    """
    Annulation du dernier déplacement du perso
    """
    global perso_coord
    if not persoAlive: return
    try:
        dep = perso_hist_pos[-1]
    except:
        return
    if dep[0] == 0: # si x
        if dep[1] == 0: # si -
            perso_coord[0] += 1
            canvas.move(perso, taille_case, 0)
        else:# si +
            perso_coord[0] -= 1
            canvas.move(perso, -taille_case, 0)

    else: # si y
        if dep[1] == 0: # si -
            perso_coord[1] += 1
            canvas.move(perso, 0, taille_case)
        else:# si +
            perso_coord[1] -= 1
            canvas.move(perso, 0, -taille_case)
    perso_hist_pos.pop(-1)

def createGrille():
    """
    création de la grille de base contenant de manière aléatoire des 0 et des 1
    """
    # 0 -- Eau et 1 -- Terre
    grille = [
    [
        0 if random.random() < p else 1 for i in range(length)
        ] for i in range(length)
    ]
    return showGrille(runAutomate(grille))

def showGrille(grille):
    """
    Affiche la grille sur l'écrans
    """
    grille_items = [
        [
            canvas.create_rectangle(x*taille_case, y*taille_case, x*taille_case+taille_case,
                                    y*taille_case+taille_case,
                                    fill=block_color[grille[y][x]])
            for x in range(length)
        ] for y in range(length)
    ]
    canvas.config(width=(length * taille_case), height=(length * taille_case))

    return grille_items

def runAutomate(grille):
    """
    Renvoie le résultat de l'application de l'automate n fois sur la grille donnée
    """
    global save_grille
    #On répete l'automate n fois
    for etape in range(n):
        newgrille = [
            [
                0 for v in range(length)
            ] for i in range(length)
        ]
        grille_voisin = getVoisinageGrille(grille)
        for y, row in enumerate(grille):
            for x, col in enumerate(row):
                if grille_voisin[y][x] >= T:
                    newgrille[y][x] = 0
                else:
                    newgrille[y][x] = 1
        grille = newgrille
    save_grille = grille
    return grille

def getVoisinageGrille(grille):
    """
        Fonction qui nous permet d'obtenir le tableau du nombre
        de voisins de chaque case.
        return un tableau à deux dimension de la taille de la grille
    """
    voisinage_grille = [
        [
            0 for i in range(length)
        ] for i in range(length)
    ]
    for y, row in enumerate(grille):
        for x, col in enumerate(row):
            # 0,0,0
            # 0,x,0
            # 0,0,0
            for i in range(k*2+1):
                for v in range(k*2+1):
                    try:
                        if grille[y-k+i][x-k+v] == 0 and not (y-k+i == y and x-k+v == x):
                            voisinage_grille[y][x] += 1
                    except:
                        pass
                # try:
                    # if grille[y-1+i][x] == 0 and y-1+i != y:
                        # voisinage_grille[y][x] += 1
                # except:
                    # pass
                # try:
                    # if grille[y-1+i][x+1] == 0 and not y-1+i == y:
                        # voisinage_grille[y][x] += 1
                # except:
                    # pass
    #print(voisinage_grille)
    return voisinage_grille

def deleteGrille():
    """
    On supprime la grille
    """
    global items
    deletePerso()
    for row in items:
        for item in row:
            canvas.delete(item)
    items = []

def genNewMap():
    """
    génération d'un nouveau terrain
    """
    global items
    deleteGrille()
    deletePerso()
    items = createGrille()

def openParamWindow():
    """
    Ouvre la fenetre de modification des paramètre de la génération de terrain
    """

    def set_param(box, val):
        box.delete(0, len(box.get()))
        box.insert(0, val)

    def validate_information():
        global p, k, n, T, length
        p = float(p_spin.get())
        n = int(n_spin.get())
        T = int(T_spin.get())
        k = int(k_spin.get())
        length = int(length_spin.get())
        param_window.destroy()

    global p, k, n, T, length
    param_window = tk.Toplevel(root)
    param_window.lift()
    p_spin = tk.Spinbox(param_window, from_=0, to=1, increment=0.1)
    p_spin.grid(row=1, column=1, padx=(0, 20))
    set_param(p_spin, p)
    p_label = tk.Label(param_window, text="p:")
    p_label.grid(row=1, column=0)
    n_spin = tk.Spinbox(param_window, from_=0, to=9999, increment=1)
    n_spin.grid(row=2, column=1, padx=(0, 20))
    set_param(n_spin, n)
    n_label = tk.Label(param_window, text="n:")
    n_label.grid(row=2, column=0)
    k_spin = tk.Spinbox(param_window, from_=0, to=9999, increment=1)
    k_spin.grid(row=3, column=1, padx=(0, 20))
    set_param(k_spin, k)
    k_label = tk.Label(param_window, text="k:")
    k_label.grid(row=3, column=0)
    T_spin = tk.Spinbox(param_window, from_=0, to=9999, increment=1)
    T_spin.grid(row=4, column=1, padx=(0, 20))
    set_param(T_spin, T)
    T_label = tk.Label(param_window, text="T:")
    T_label.grid(row=4, column=0)
    length_spin = tk.Spinbox(param_window, from_=0, to=9999, increment=1)
    length_spin.grid(row=0, column=1, pady=(20,0), padx=(0, 20))
    set_param(length_spin, length)
    length_label = tk.Label(param_window, text="Taille:")
    length_label.grid(row=0, column=0, pady=(20,0), padx=(20, 0))
    valider = tk.Button(param_window, text="Valider", command=validate_information)
    valider.grid(row=5, column=0, columnspan=2, pady=(0, 20))
    param_window.mainloop()

# Fonctions liées à la sauvegarde.

def saveMap():
    """Fonction qui nous permet de sauvegarder la map actuelle dans un fichier
    csv nommé par l'utilisateur"""
    save_path = tk.filedialog.asksaveasfilename()
    if save_path == "":
        raise Exception("Pas de nom donné")

    # On créer et ouvre en écriture un fichier csv
    with open(save_path+".csv", "w", newline='') as f:
        writer = csv.writer(f)

        # on enregistre les paramètres de la map
        map_param = [length, p, n, T, k]
        writer.writerow(map_param)

        #On enregistre les paramètres du joueur
        player_param = [persoAlive, perso_coord, perso_hist_pos]
        writer.writerow(player_param)

        # on enregistre à l'intérrieur du fichier csv la grille
        for row in save_grille:
            writer.writerow(row)

def loadMap():
    """Fonction qui nous permet de charger une map depuis un fichier
    csv générer par le programme lors d'une sauvegarde"""
    global items, save_grille, length, p, n, T, k, persoAlive, perso_coord, perso_hist_pos, perso
    save_path = tk.filedialog.askopenfilename()
    with open(save_path, "r") as f:
        reader = list(csv.reader(f))

        # On supprime la grille actuelle
        deleteGrille()

        # On récupère les paramètres de la map
        map_param = [int(i) if float(i).is_integer() else float(i) for i in reader[0]]
        length, p, n, T, k = map_param

        # On récupère les paramètres du joueur
        player_param = reader[1]
        if player_param[0] == "True":
            player_param[0] = True
        else:
            player_param[0] = False
        player_param[1] = [int(x) for x in player_param[1][1:-1].split(", ")]
        if player_param[2] != '[]':
            player_param[2] = [
                [
                    int(b) for b in x[1:].split(", ")
                ] for x in player_param[2][1:-2].split("], ")
            ]
        else:
            player_param[2] = []
        persoAlive, perso_coord, perso_hist_pos = player_param[0], player_param[1], player_param[2]

        # On récupère la grille
        save_grille = [
            [
                int(x) for x in row
            ] for row in reader[2:]
        ]

    # On dessine la grille chargé
    items = showGrille(save_grille)
    # On affiche le personnage
    if persoAlive:
        perso = canvas.create_rectangle(taille_case*perso_coord[0],
                                        taille_case*perso_coord[1],
                                        taille_case*perso_coord[0]+taille_case,
                                        taille_case*perso_coord[1]+taille_case,
                                        fill="red")

root = tk.Tk()
root.resizable(width=False, height=True)
canvas = tk.Canvas(root, width=(length * taille_case),
                   height=(length * taille_case), bg="Black")
canvas.grid(row=0, column=1, rowspan=7)
canvas.bind("<Button-1>", createPerso)
canvas.bind("<1>", canvas.focus_set())
canvas.bind('<KeyPress>', movePerso)
items = createGrille()
param_button = tk.Button(text="Paramètres", command=openParamWindow)
param_button.grid(row=2, column=0)
gen_button = tk.Button(text="Supprimer map", command=deleteGrille)
gen_button.grid(row=1, column=0)
gen_button = tk.Button(text="Générer map", command=genNewMap)
gen_button.grid(row=0, column=0)

delete_perso_button = tk.Button(text="Supprimer le perso", command = deletePerso)
delete_perso_button.grid(row = 3, column = 0)
undo_perso_position_button = tk.Button(text="Annuler dernier déplacement", command = undoPersoPosition)
undo_perso_position_button.grid(row = 4, column = 0)

save_button = tk.Button(text="Sauvegarder map", command=saveMap)
save_button.grid(row=5, column=0)
load_button = tk.Button(text="Charger map", command=loadMap)
load_button.grid(row=6, column=0)

root.mainloop()


# -*- coding: utf-8 -*-

import time
import random

import json

import ipfshttpclient
from eternity_backend_server.blueprints.ipfs.aes import PrpCrypt

def upload_data(data):
    ip = data.get("ip")
    address = data.get("address")
    detail = data.get("detail")
    detail_dic = json.loads(detail)
    timestamp = time.time()
    need_upload_msg = {
        "timestamp": timestamp,
        "ip": ip,
        "detail": detail_dic,
        "address": address,
        "checkcode": True
    }
    upload_msg_str = json.dumps(need_upload_msg)
    client = ipfshttpclient.connect()

    res = client.add_str(upload_msg_str)
    return res

def check_code(ipfshash):
    data = get_data_by_ipfsHash(ipfshash)
    if data is None:
        raise ValueError("IPFS data is Null, Please enter the correct ipfshash.")
    checkcode = data.get("checkcode")
    if checkcode==True:
        return checkcode
    else:
        return False

def get_data_by_ipfsHash(ipfshash):
    client = ipfshttpclient.connect()
    data_bytes = client.cat(ipfshash)
    data_str = str(data_bytes, encoding="utf-8")
    data_dict = json.loads(data_str)
    return data_dict

def upload_encode_aes_file(key1, key2, content):
    client = ipfshttpclient.connect()
    pc = PrpCrypt(key1, key2)
    redata = pc.encrypt(content)
    res = client.add_str(redata)
    # res 存储的是ipfs hash地址

    return res

def download_decode_aes_file(key1, key2, ipfshash):
    client = ipfshttpclient.connect()
    bytes_encrypt_content = client.cat(ipfshash)
    encrypt_content = str(bytes_encrypt_content, "utf-8")
    pc = PrpCrypt(key1, key2)
    content = pc.decrypt(encrypt_content)
    # 返回解密之后的存储内容
    return content

def download_decode_aes_py_file(key1, key2, ipfshash):
    client = ipfshttpclient.connect()
    bytes_encrypt_content = client.cat(ipfshash)
    encrypt_content = str(bytes_encrypt_content, "utf-8")
    filename, aes_content = encrypt_content.split("_")
    pc = PrpCrypt(key1, key2)
    content = pc.decrypt(aes_content)
    result = {filename:content}
    return result


import numpy as np
from PIL import Image
from matplotlib import image, patches
import os
import skimage as sk
import skimage.io as io
import skimage.filters
import math
import matplotlib.pyplot as plt
from skimage.transform import resize, rescale, downscale_local_mean
from skimage.morphology import closing
from skimage.morphology import disk
from skimage.color import hsv2rgb, rgb2hsv
import pickle
import time
import datetime
#import yagmail

from skimage.filters import threshold_niblack
from astropy.convolution import convolve
from astropy.convolution import Gaussian2DKernel

from tensorflow.keras.models import model_from_json

def save_var(var, file_name):
    '''
    Saves any type of variable with the given filename(can be a path)
    '''
    out_file = open(file_name,'wb')
    pickle.dump(var,out_file)
    out_file.close()

def read_var(file_name):
    infile = open(file_name,'rb')
    var = pickle.load(infile)
    infile.close()
    return var

def read_image(filename):
    img = io.imread(filename)
    return img

def rgb2bool(rgb):
    r, g, b = rgb[:,:,0], rgb[:,:,1], rgb[:,:,2]
    bool_img = (r + g + b) > 0
    return bool_img

def gray2bool(gray):
    bool_img = gray > 0
    return bool_img

def rgb2gray(rgb):

    r, g, b = rgb[:,:,0], rgb[:,:,1], rgb[:,:,2]
    gray = 0.2989 * r + 0.5870 * g + 0.1140 * b

    return gray

def rgb2green(rgb):
    r, g, b = rgb[:,:,0], rgb[:,:,1], rgb[:,:,2]
    #green=3*g / (r + g + b + 0.000001)
    total = (r + g + b + 0.000001)
    green = g
    result = 3*g/total
    print(total[500:550, 512])
    print(green[500:550, 512])
    print(result[500:550, 512])
    smallest = total.min(axis=0).min(axis=0)
    biggest = total.max(axis=0).max(axis=0)
    mean = np.mean(result, axis=(0, 1))
    print(smallest)
    print(biggest)
    print(mean)
    return result

def rgb2yellow(rgb):
    r, g, b = rgb[:,:,0], rgb[:,:,1], rgb[:,:,2]
    yellow=(g + r) / 2
    return yellow

def load_data(dir_name = 'DR_dataset', preprocessing = False):
    '''
    Load images from the given directory. "DR_dataset" as default.
    Returns image list
    '''
    imgs = []
    for filename in os.listdir(dir_name):
        if os.path.isfile(dir_name + '/' + filename):
            img=io.imread(dir_name + '/' + filename)
            if(preprocessing == True):
                preProcessing(img)
            imgs.append(img)
    return imgs

def addWeighted(src1, alpha, src2, beta, gamma, colored=True):
    if colored:
        gamma_im = sk.img_as_float(Image.new("RGB", (src1.shape[1],src1.shape[0]), (gamma,gamma,gamma)))
    else:
        gamma_im = sk.img_as_float(Image.new("L", (src1.shape[1],src1.shape[0]), gamma))

    dst = sk.img_as_float((src1*alpha) + (src2*beta) + gamma_im)
    return dst

def bens_processing(img):
    image=sk.img_as_float(img)
    gaus_img = skimage.filters.gaussian(image, sigma=image.shape[0]/32, truncate=6)
    result = addWeighted(image, 4, gaus_img, -4, 128, colored=(img.ndim==3))
    #print('result min: ', np.amin(result), 'result max: ', np.amax(result))
    #rescaled_img = sk.exposure.rescale_intensity(result, in_range=(0.5,1.5), out_range=(0,1))
    rescaled_img = sk.exposure.rescale_intensity(result, in_range=(0,1), out_range=(0,1))
    return rescaled_img

def delete_vessels(image, vessels):
    '''
    Takes gray image as input and binary image of vessels. Mask blood vessel regions with
    closing version of the original gray level image.
    Returns image with no vessels
    '''
    i_vessels = vessels < 0.5
    closed = closing(image, disk(15))
    hop = closed * vessels
    top = image * i_vessels
    no_vessel = hop + top
    return no_vessel

def colorize_binary_img(bin_img):
    red = [255,0,0]
    green = [0,255,0]
    blue = [0,0,255]
    yellow = [255,255,0]
    orange = [255,120,0]
    purple = [255,0,255]
    pink = [255,120,120]
    white = [255,255,255]
    #color_list = [red, green, blue, yellow, white, orange, purple, pink]
    color_list = [blue, red, green, white, purple, orange, pink]

    colored_img = np.zeros([bin_img.shape[0],bin_img.shape[1],3], dtype = np.uint8)
    for ch in range (bin_img.shape[2]):
        for i in range(3):  #Because rgb 3 channel output
            colour = color_list[ch]
            colour_ch = color_list[ch][i]
            colored_img[:,:,i] = colored_img[:,:,i] + bin_img[:,:,ch]*color_list[ch][i]

    return colored_img

def show_images(images: list, titles: list="Untitled    ", colorScale='gray', rows = 0, columns = 0) -> None:
    n: int = len(images)
    if rows == 0:
        rows=int(math.sqrt(n))
    if columns == 0:
        columns=(n//rows)
    f = plt.figure()
    for i in range(n):
        # Debug, plot figure
        f.add_subplot(rows, columns, i + 1)
        plt.imshow(images[i], cmap=colorScale)
        plt.title(titles[i])
    plt.show(block=True)

def save_images(images: list, titles: list="Untitled    ", colorScale='gray', rows = 0, columns = 0, save_dir="out/", name="image.png") -> None:
    n: int = len(images)
    if rows == 0:
        rows=int(math.sqrt(n))
    if columns == 0:
        columns=(n//rows)
    f = plt.figure(figsize=[19.2, 14.4])
    for i in range(n):
        # Debug, plot figure
        f.add_subplot(rows, columns, i + 1)
        plt.imshow(images[i], cmap=colorScale)
        plt.title(titles[i])
    plt.savefig(save_dir + name)
    plt.show(block=False)
    plt.close()

def idrid_to_5channel(ch0_file, ch1_file, ch2_file, ch3_file):
    ch0= io.imread(ch0_file)
    try:
      ch1= io.imread(ch1_file)
    except:
      print("ch1 image cannot read:")
      print(ch1_file)
      ch1=np.zeros([ch0.shape[0], ch0.shape[1], 3], dtype=bool)
    try:
      ch2= io.imread(ch2_file)
    except:
      print("ch2 image cannot read:")
      print(ch2_file)
      ch2=np.zeros([ch0.shape[0], ch0.shape[1], 3], dtype=bool)
    ch3=io.imread(ch3_file)

    arr_5ch = np.zeros([ch0.shape[0], ch0.shape[1], 5], dtype=bool)
    arr_5ch[:,:,0] = rgb2bool(ch0)
    arr_5ch[:,:,1] = rgb2bool(ch1)
    arr_5ch[:,:,2] = rgb2bool(ch2)
    arr_5ch[:,:,3] = gray2bool(ch3) & (np.invert(rgb2bool(ch0) | rgb2bool(ch1) |rgb2bool(ch2)))
    arr_5ch[:,:,4] = np.invert(arr_5ch[:,:,0] | arr_5ch[:,:,1] |arr_5ch[:,:,2])
    return arr_5ch

def idrid_to_4channel(ch0_file, ch1_file, ch2_file):
    ch0= io.imread(ch0_file)
    try:
      ch1= io.imread(ch1_file)
    except:
      print("ch1 image cannot read:")
      print(ch1_file)
      ch1=np.zeros([ch0.shape[0], ch0.shape[1], 3], dtype=bool)
    try:
      ch2= io.imread(ch2_file)
    except:
      print("ch2 image cannot read:")
      print(ch2_file)
      ch2=np.zeros([ch0.shape[0], ch0.shape[1], 3], dtype=bool)

    arr_4ch = np.zeros([ch0.shape[0], ch0.shape[1], 4], dtype=bool)
    arr_4ch[:,:,0] = rgb2bool(ch0)
    arr_4ch[:,:,1] = rgb2bool(ch1)
    arr_4ch[:,:,2] = rgb2bool(ch2)
    arr_4ch[:,:,3] = np.invert(arr_4ch[:,:,0] | arr_4ch[:,:,1] | arr_4ch[:,:,2])
    return arr_4ch

def idrid_to_3channel(ch0_file, ch1_file, ch2_file):
    ch0= io.imread(ch0_file)
    try:
      ch1= io.imread(ch1_file)
    except:
      print("ch1 image cannot read:")
      print(ch1_file)
      ch1=np.zeros([ch0.shape[0], ch0.shape[1], 3], dtype=bool)
    try:
      ch2= io.imread(ch2_file)
    except:
      print("ch2 image cannot read:")
      print(ch2_file)
      ch2=np.zeros([ch0.shape[0], ch0.shape[1], 3], dtype=bool)

    arr_4ch = np.zeros([ch0.shape[0], ch0.shape[1], 3], dtype=bool)
    arr_4ch[:,:,0] = (rgb2bool(ch0) | rgb2bool(ch1))
    arr_4ch[:,:,1] = rgb2bool(ch2)
    arr_4ch[:,:,2] = np.invert(arr_4ch[:,:,0] | arr_4ch[:,:,1])
    return arr_4ch

def create_4ch_dataset(path0, path1, path2, path_out, ind = 1):
    entries = sorted(os.listdir(path0))
    i=ind
    for entry in entries:
        print(entry)
        index = str(i).zfill(2)
        ch0_file = path0 + "IDRiD_" + index + "_MA.tif"
        ch1_file = path1 + "IDRiD_" + index + "_HE.tif"
        ch2_file = path2 + "IDRiD_" + index + "_EX.tif"

        arr_4ch = idrid_to_4channel(ch0_file, ch1_file, ch2_file)
        save_var(arr_4ch, path_out + "IDRiD_" + index + "_4CH")
        i=i+1

def create_5ch_dataset(path0, path1, path2, path3, path_out, ind = 1):
    entries = sorted(os.listdir(path0))
    i=ind
    for entry in entries:
        print(entry)
        index = str(i).zfill(2)
        ch0_file = path0 + "IDRiD_" + index + "_MA.png"
        ch1_file = path1 + "IDRiD_" + index + "_HE.png"
        ch2_file = path2 + "IDRiD_" + index + "_EX.png"
        ch3_file = path3 + "IDRiD_" + index + "_BV.png"

        arr_4ch = idrid_to_5channel(ch0_file, ch1_file, ch2_file, ch3_file)
        save_var(arr_4ch, path_out + "IDRiD_" + index + "_5CH")
        i=i+1

def patch_4ch_dataset(path0, path1, path2, path_out):
    entries = sorted(os.listdir(path0))
    rows = []
    for entry in entries:
        name, img_formatt = entry.split("MA")
        inp_name = name + img_formatt
        ch0_name = name + "MA" + img_formatt
        ch1_name = name + "HE" + img_formatt
        ch2_name = name + "EX" + img_formatt
        ch4_name = name + "4CH"
        ch0_file = path0 + ch0_name
        ch1_file = path1 + ch1_name
        ch2_file = path2 + ch2_name

        arr_4ch = idrid_to_4channel(ch0_file, ch1_file, ch2_file)
        save_var(arr_4ch, path_out + ch4_name)
        #TODO:Check if background works correct
        background = (arr_4ch[:,:,3]==1).all()   #background
        row = [inp_name, ch0_name, ch1_name, ch2_name, ch4_name, background]
        rows.append(row)
    return rows

def patch_5ch_dataset(path0, path1, path2, path3, path_out):
    entries = sorted(os.listdir(path0))
    rows = []
    for entry in entries:
        name, img_formatt = entry.split("MA")
        inp_name = name + img_formatt
        ch0_name = name + "MA" + img_formatt
        ch1_name = name + "HE" + img_formatt
        ch2_name = name + "EX" + img_formatt
        ch3_name = name + "BV" + img_formatt
        ch4_name = name + "4CH"
        ch0_file = path0 + ch0_name
        ch1_file = path1 + ch1_name
        ch2_file = path2 + ch2_name
        ch3_file = path3 + ch3_name

        #arr_4ch = idrid_to_4channel(ch0_file, ch1_file, ch2_file)
        arr_4ch = idrid_to_4channel(ch0_file, ch1_file, ch2_file, ch3_file)
        save_var(arr_4ch, path_out + ch4_name)
        #TODO:Check if background works correct
        background = (arr_4ch[:,:,3]==1).all()   #background
        row = [inp_name, ch0_name, ch1_name, ch2_name, ch3_name, ch4_name, background]
        rows.append(row)
    return rows

def patch_6ch_dataset(path0, path1, path2, path3, path_out):
    entries = sorted(os.listdir(path0))
    rows = []
    for entry in entries:
        name, img_formatt = entry.split("MA")
        inp_name = name + img_formatt
        ch0_name = name + "MA" + img_formatt
        ch1_name = name + "HE" + img_formatt
        ch2_name = name + "EX" + img_formatt
        ch3_name = name + "BV" + img_formatt
        ch4_name = name + "4CH"
        ch0_file = path0 + ch0_name
        ch1_file = path1 + ch1_name
        ch2_file = path2 + ch2_name
        ch3_file = path3 + ch3_name

        arr_5ch = idrid_to_5channel(ch0_file, ch1_file, ch2_file, ch3_file)
        save_var(arr_5ch, path_out + ch4_name)
        #TODO:Check if background works correct
        background = (arr_5ch[:,:,4]==1).all()   #background
        row = [inp_name, ch0_name, ch1_name, ch2_name, ch3_name, ch4_name, background]
        rows.append(row)
    return rows

def create_1024_dataset(srcFileName, destFileName):
    for filename in os.listdir(srcFileName):
        if os.path.isfile(srcFileName + '/' + filename):
            print(filename)
            img=Image.open(srcFileName + '/' + filename)
            old_size = img.size
            if((old_size[0]<1024) & (old_size[1]<1024)):
                ratio=float(old_size[0])/float(old_size[1])
                if(old_size[0]<old_size[1]):
                    img = img.resize((int(1024*ratio),1024), Image.LANCZOS)
                else:
                    img = img.resize((1024,int(1024.0/ratio)), Image.LANCZOS)
            old_size = img.size
            new_size = (1024, 1024)
            new_im = Image.new("RGB", new_size)             #this is already black!
            new_im.paste(img, (int((new_size[0]-old_size[0])/2),
                                int((new_size[1]-old_size[1])/2)))
            new_im.save(destFileName + '/' + filename)

def adems_processing(img):
    image=sk.img_as_float(img)
    small_img = resize(img, (img.shape[0]/8,img.shape[1]/8))

    gaus_img = skimage.filters.gaussian(small_img, sigma=small_img.shape[0]/32, truncate=6)

    diff2mean = small_img - gaus_img
    distance2mean = np.sqrt(np.multiply(diff2mean[:,:,0],diff2mean[:,:,0]) + np.multiply(diff2mean[:,:,1],diff2mean[:,:,1]) + np.multiply(diff2mean[:,:,2],diff2mean[:,:,2]))
    treshold = 0.05

    small_img0 = small_img[:,:,0]
    small_img1 = small_img[:,:,1]
    small_img2 = small_img[:,:,2]
    small_img0[distance2mean > treshold] = np.nan
    small_img1[distance2mean > treshold] = np.nan
    small_img2[distance2mean > treshold] = np.nan
    small_img[:,:,0] = small_img0
    small_img[:,:,1] = small_img1
    small_img[:,:,2] = small_img2

    kernel = Gaussian2DKernel(x_stddev=int(small_img.shape[0]/32))

    astropy_conv = np.ones_like(small_img)
    astropy_conv[:,:,0] = convolve(small_img[:,:,0], kernel)
    astropy_conv[:,:,1] = convolve(small_img[:,:,1], kernel)
    astropy_conv[:,:,2] = convolve(small_img[:,:,2], kernel)

    astropy_conv_big = resize(astropy_conv, (img.shape[0],img.shape[1]))
    result = addWeighted(image, 4, astropy_conv_big, -4, 128, colored=True)
    rescaled_img = sk.exposure.rescale_intensity(result, in_range=(0,1), out_range=(0,1))
    return rescaled_img

def get_eyeshape(img):
    '''
    Gets rgb image. Return eyeshape mask
    '''
    gray=rgb2gray(img)
    #binary=gray>0.0588
    binary=gray>14.994
    return binary

class feat_train_config:

    def __init__(self,  name= "default_config",
                        MODEL_NAME = 'fcn',
                        IMG_SIZE = 512,
                        CLASS_NUM = 3,
                        BATCH_SIZE = 8,
                        _1ST_STEP_EPOCH = 8,
                        _2ND_STEP_EPOCH = 64,
                        LOSS_WEIGHT = 0.5
    ):

        self.name = name
        self.MODEL_NAME = MODEL_NAME
        self.IMG_SIZE = IMG_SIZE
        self.CLASS_NUM = CLASS_NUM
        self.BATCH_SIZE = BATCH_SIZE
        self._1ST_STEP_EPOCH = _1ST_STEP_EPOCH
        self._2ND_STEP_EPOCH = _2ND_STEP_EPOCH
        self.LOSS_WEIGHT = LOSS_WEIGHT
        self.log_dir = ""

    def save(self, save_dir = ""):
        self.log_dir = save_dir
        save_var(self, save_dir + self.name)
        write_to_log(self.log_dir, "\n\nConfiguration name: ")
        write_to_log(self.log_dir, self.name)
        write_to_log(self.log_dir, "\nMODEL_NAME: ")
        write_to_log(self.log_dir, self.MODEL_NAME)
        write_to_log(self.log_dir, "\nIMG_SIZE: ")
        write_to_log(self.log_dir, str(self.IMG_SIZE))
        write_to_log(self.log_dir, "\nCLASS_NUM: ")
        write_to_log(self.log_dir, str(self.CLASS_NUM))
        write_to_log(self.log_dir, "\nBATCH_SIZE: ")
        write_to_log(self.log_dir, str(self.BATCH_SIZE))
        write_to_log(self.log_dir, "\n_1ST_STEP_EPOCH: ")
        write_to_log(self.log_dir, str(self._1ST_STEP_EPOCH))
        write_to_log(self.log_dir, "\n_2ND_STEP_EPOCH: ")
        write_to_log(self.log_dir, str(self._2ND_STEP_EPOCH))
        write_to_log(self.log_dir, "\nLOSS_WEIGHT: ")
        write_to_log(self.log_dir, str(self.LOSS_WEIGHT))

def save_model(model, name):
    model_json = model.to_json()
    with open(name+'.json', 'w') as json_file:
        json_file.write(model_json)

    model.save_weights(name+'.h5')
    print("Saved model to disk")

def load_model(name):
    json_file = open(name+'.json', 'r')
    loaded_model_json = json_file.read()
    json_file.close()
    loaded_model = model_from_json(loaded_model_json)
    # load weights into new model
    loaded_model.load_weights(name+'.h5')
    print("Loaded model from disk")
    return loaded_model

def make_subfolder(dirname,parent_path):
    path = os.path.join(parent_path, dirname)
    os.mkdir(path)
    print("Directory '%s' created" %dirname)
    return path + '/'

def make_log_dir(parent_path = ""):
    current_date = datetime.datetime.now()
    dirname = current_date.strftime("%Y_%B_%d-%H_%M_%S")
    path = make_subfolder(dirname,parent_path)
    return path

def write_to_log(log_dir ="", log_entry = ""):
    with open(log_dir + "/log.txt", "a") as file:
        file.write(log_entry)

def send_as_mail(log_dir):
    log = log_dir + '/log.txt'
    #metrics = log_dir + '/metrics.png'
    loss_and_acc = log_dir + '/loss_and_acc.png'
    contents = [ "Merhaba Adem,\n\tSen yokken bir süredir feature extraction üzerine çalışıyordum. Şöyle bir şeyler elde ettim, ekteki dosyalara bakabilir misin?",
    log, loss_and_acc#, metrics
    ]
    with yagmail.SMTP('viventedevelopment', 'yeniparrola2.1') as yag:
        yag.send(to = 'ademgunesen+viventedev@gmail.com', subject = 'Feature Extraction Train Sonuçları', contents = contents)

'''
#Create dataset with lesions MA, HE,  EX
#Train
path0='/home/vivente/Desktop/TEZ/Dataset/A. Segmentation/2. All Segmentation Groundtruths/a. Training Set/1. Microaneurysms/'
path1='/home/vivente/Desktop/TEZ/Dataset/A. Segmentation/2. All Segmentation Groundtruths/a. Training Set/2. Haemorrhages/'
path2='/home/vivente/Desktop/TEZ/Dataset/A. Segmentation/2. All Segmentation Groundtruths/a. Training Set/3. Hard Exudates/'
path_out='/home/vivente/Desktop/TEZ/Dataset/A. Segmentation/2. All Segmentation Groundtruths/a. Training Set/0. All/'
#create_4ch_dataset(path0, path1, path2, path_out)
#Test
path0='/home/vivente/Desktop/TEZ/Dataset/A. Segmentation/2. All Segmentation Groundtruths/b. Testing Set/1. Microaneurysms/'
path1='/home/vivente/Desktop/TEZ/Dataset/A. Segmentation/2. All Segmentation Groundtruths/b. Testing Set/2. Haemorrhages/'
path2='/home/vivente/Desktop/TEZ/Dataset/A. Segmentation/2. All Segmentation Groundtruths/b. Testing Set/3. Hard Exudates/'
path_out='/home/vivente/Desktop/TEZ/Dataset/A. Segmentation/2. All Segmentation Groundtruths/b. Testing Set/0. All/'
create_4ch_dataset(path0, path1, path2, path_out, ind=55)
'''
'''
#Create dataset with lesions and natural structures MA, HE,  EX, OD, BV
#Train
path0='/home/vivente/Desktop/TEZ/Dataset/A. Segmentation/2. All Segmentation Groundtruths/a. Training Set/1. Microaneurysms/'
path1='/home/vivente/Desktop/TEZ/Dataset/A. Segmentation/2. All Segmentation Groundtruths/a. Training Set/2. Haemorrhages/'
path2='/home/vivente/Desktop/TEZ/Dataset/A. Segmentation/2. All Segmentation Groundtruths/a. Training Set/3. Hard Exudates/'
path3='/home/vivente/Desktop/TEZ/Dataset/A. Segmentation/2. All Segmentation Groundtruths/a. Training Set/6. Blood Vessel/'
path_out='/home/vivente/Desktop/TEZ/Dataset/A. Segmentation/2. All Segmentation Groundtruths/a. Training Set/0. All/'
create_5ch_dataset(path0, path1, path2, path3, path_out, ind = 55)
#Test
path0='/home/vivente/Desktop/TEZ/Dataset/A. Segmentation/2. All Segmentation Groundtruths/b. Testing Set/1. Microaneurysms/'
path1='/home/vivente/Desktop/TEZ/Dataset/A. Segmentation/2. All Segmentation Groundtruths/b. Testing Set/2. Haemorrhages/'
path2='/home/vivente/Desktop/TEZ/Dataset/A. Segmentation/2. All Segmentation Groundtruths/b. Testing Set/3. Hard Exudates/'
path3='/home/vivente/Desktop/TEZ/Dataset/A. Segmentation/2. All Segmentation Groundtruths/b. Testing Set/6. Blood Vessel/'
path_out='/home/vivente/Desktop/TEZ/Dataset/A. Segmentation/2. All Segmentation Groundtruths/b. Testing Set/0. All/'
create_5ch_dataset(path0, path1, path2, path3, path_out, ind = 55)
'''


import click
from pathlib import Path

@click.command()
@click.option('--bed', '-b', type=Path, required=True,
              help="input bed file")
@click.option('--toml', '-t', type=Path, required=True,
              help="output toml file")
def main(bed, toml):
    with open(toml, 'w') as out:
        out.write('[[jobs]]\n')
        out.write('name = "change_here"\n')
        out.write('time = [0, 6000]\n')
        out.write('ch = [1, 512]\n')
        out.write('bc = "all"\n')
        out.write('target = [\n')

        with open(bed, 'r') as f:
            for line in f:
                content = line.rstrip().split('\t')
                chrom, start, end = content[0], int(content[1]), int(content[2])
                out.write(f'\t{{region = ["{chrom}", "{start}", "{end}"], action = "stop_receiving"}},\n')

        out.write(f'\t{{region = "multi", action = "unblock"}},\n')
        out.write(f'\t{{region = "unmapped", action = "unblock"}},\n')
        out.write(f'\t{{region = "miss", action = "unblock"}},\n')
        out.write("]\n")

if __name__ == "__main__":
    main()


from selenium import webdriver
from time import sleep

# Iniciando o Navegador.
driver = webdriver.Chrome('C:/Users/Jhony/Downloads/Animes/chromedriver.exe')
pagina = 'https://www.meuanime.com/baixar?file=1857'
count = 0
status = "notok"
open('anime.txt', 'a')
while count != 3:
    # Procurando o link para download.
    driver.get(pagina)

    episodio = driver.find_element_by_xpath('/html/body/div[5]/div/div[1]/p')
    episodio = episodio.text
    episodio = episodio.replace('Voçê esta baixando - ','')
    print(episodio)

    download = driver.find_element_by_xpath('//*[@id="conteudo404"]/div/a')
    download = download.get_attribute('href')
    print(download)

    proximo = driver.find_element_by_xpath('//*[@id="postados404"]/div[16]/a[1]')
    proximo = proximo.get_attribute('href')
    print(proximo)

    with open('anime.txt', 'a') as texte :
        print(f'{episodio} adicionado com sucesso')
        print(episodio, '\n',
              download, '\n', file=texte)

    pagina = proximo
    count += 1
    print()
    sleep(1)

driver.close()


import unittest
import requests
import calendar

host = 'http://45.76.114.158/api/'
version = 'v2.01'
baseurl = '{host}?StatisticsArea={statsArea}&State={states}&Category={category}&{params}'

def formUrl(statsArea='Retail', category='Total', states='AUS', startDate = None, endDate = None):
    params = ''
    if startDate != None:
        params += 'startDate={}'.format(startDate)
    else:
        params += 'startDate={}'.format('1900-01-01')
    params += '&'
    if endDate != None:
        params += 'endDate={}'.format(endDate)
    else:
        params += 'endDate={}'.format('2100-01-01')
    import sys
    sys.stderr.write(baseurl.format(host=host, version=version, statsArea=statsArea, category=category, states=states, params=params))
    return baseurl.format(host=host, version=version, statsArea=statsArea, category=category, states=states, params=params)

def is_ok(response):
    return response.ok and response.json()['header']['status'] == 'success'

def get_error(response):
    return response.json()['data']['code']

def get_data(response, name):
    return response.json()['data'][name]

CATEGORIES = [
    'Total',
    'Food',
    'HouseholdGood',
    'ClothingFootwareAndPersonalAccessory',
    'DepartmentStores',
    'CafesResturantsAndTakeawayFood',
    'Other'
]
NUM_CATEGORIES = len(CATEGORIES)

STATES = ['AUS', 'NSW', 'WA',
    'SA', 'ACT', 'VIC', 'TAS', 'QLD', 'NT']
NUM_STATES = len(STATES)

COMMODITIES = [
    'Total', 'FoodAndLiveAnimals',
    'BeveragesAndTobacco',
    'CrudMaterialAndInedible',
    'MineralFuelLubricentAndRelatedMaterial',
    'AnimalAndVegitableOilFatAndWaxes',
    'ChemicalsAndRelatedProducts',
    'ManufacturedGoods',
    'MachineryAndTransportEquipments',
    'OtherManufacturedArticles',
    'Unclassified'
]
NUM_COMMODITIES = len(COMMODITIES)

def isMonthEnd(d):
    (y,m,d) = map(int, d.split('-'))
    return d == calendar.monthrange(y,m)[1]


'''
    Mooc 的虚基类：用于派生所有Mooc子类
'''

import os
from abc import ABC, abstractmethod
from Mooc.Mooc_Config import *
from Mooc.Mooc_Download import *
from Mooc.Mooc_Request import *
import m3u8
from urllib.parse import urljoin
from shutil import rmtree
__all__ = [
    "Mooc_Base"
]

class Mooc_Base(ABC):
    def __init__(self):
        self.__mode = None
        self.__cid = None
        self.__title = None
        self.__infos = None
        self.__rootDir = None

    @property
    def mode(self):
        '''下载模式: 用于选择性下载'''
        return self.__mode

    @mode.setter
    def mode(self, mode):
        self.__mode = mode

    @property
    def cid(self):
        '''课程的 ID'''
        return self.__cid

    @cid.setter
    def cid(self, cid):
        self.__cid = cid

    @property
    def title(self):
        '''课程的标题'''
        return self.__title

    @title.setter
    def title(self, title):
        self.__title = title

    @property
    def infos(self):
        '''解析后的课程信息'''
        return self.__infos

    @property
    def rootDir(self):
        return self.__rootDir

    @rootDir.setter
    def rootDir(self, rootDir):
        self.__rootDir = rootDir

    @infos.setter
    def infos(self, infos):
        self.__infos = infos

    @abstractmethod
    def _get_cid(self):
        pass

    @abstractmethod
    def _get_title(self):
        pass

    @abstractmethod
    def _get_infos(self):
        pass

    @abstractmethod
    def _download(self):
        pass

    @abstractmethod
    def set_mode(self):
        pass

    @abstractmethod
    def prepare(self, url):
        pass

    @abstractmethod
    def download(self):
        pass

    @classmethod
    def download_video(cls, video_url, video_name, video_dir, video_size=None):
        '''下载 MP4 视频文件'''
        succeed = True
        if not cls.judge_file_existed(video_dir, video_name, '.mp4'):
            if 'm3u8' in video_url:
                cls.download_m3u8_video(
                    video_url, video_name, video_dir, video_size=video_size)
                print(
                    "\r  |-{}  [mp4]下载完成".format(cls.align(video_name, LENGTH)))
            else:
                try:
                    header = request_head(video_url)
                    size = float(header['Content-Length']) / (1024*1024)
                    print(
                        "  |-{}  [mp4] 大小: {:.2f}M".format(cls.align(video_name, LENGTH), size))
                    aria2_download_file(
                        video_url, video_name+'.mp4', video_dir)
                except DownloadFailed:
                    print(
                        "  |-{}  [mp4] 资源无法下载！".format(cls.align(video_name, LENGTH)))
                    succeed = False
        else:
            print(
                "  |-{}  [mp4] 已经成功下载！".format(cls.align(video_name, LENGTH)))
        return succeed

    @classmethod
    def download_m3u8_video(cls, video_url, video_name, video_dir, video_size=None):
        tmpdir = os.path.join(video_dir, 'tmp')
        tsURLsFile = 'ts.txt'

        succeed = True

        if not (os.path.exists(tmpdir)):
            os.mkdir(tmpdir)
        os.chdir(tmpdir)

        m3u8Str = request_get(video_url)
        m3u8_obj = m3u8.loads(m3u8Str)

        with open(tsURLsFile, 'w') as f:
            for url in m3u8_obj.files:
                # m3u8文件获取各个ts流的真正下载地址，它实际是m3u8文件的url更改路径得到的
                # 另外，由于实际下载下来的文件名称可能和url中的名称不一致，所以需要强制指定
                # 下载文件名称
                f.write(urljoin(video_url, url) + '\n' +
                        '    out=' + url + '\n')
            f.close()

        try:
            print("  |-{}  [mp4] 大小: {:.2f}M".format(cls.align(video_name,
                                                               LENGTH), video_size / (1024*1024)))
            aria2_download_m3u8_video(
                video_name, video_dir, tsURLsFile, video_size)
            # 一定要记得先跳出tmp目录再删除，否则无法删除！！！
            os.chdir(video_dir)
            rmtree(tmpdir)
        except DownloadFailed:
            print(
                "  |-{}  [mp4] 资源无法下载！".format(cls.align(video_name, LENGTH)))
            succeed = False
        return succeed

    @classmethod
    def download_pdf(cls, pdf_url, pdf_name, pdf_dir):
        '''下载 PDF '''
        succeed = True
        if not cls.judge_file_existed(pdf_dir, pdf_name, '.pdf'):
            try:
                aria2_download_file(pdf_url, pdf_name+'.pdf', pdf_dir)
                print("  |-{}  (pdf) 已经成功下载！".format(cls.align(pdf_name, LENGTH)))
            except DownloadFailed:
                print("  |-{}  (pdf) 资源无法下载！".format(cls.align(pdf_name, LENGTH)))
                succeed = False
        else:
            print("  |-{}  (pdf) 已经成功下载！".format(cls.align(pdf_name, LENGTH)))
        return succeed

    @classmethod
    def download_sub(cls, sub_url, sub_name, sub_dir):
        '''下载字幕'''
        succeed = True
        if not cls.judge_file_existed(sub_dir, sub_name, '.srt'):
            try:
                aria2_download_file(sub_url, sub_name+'.srt', sub_dir)
            except DownloadFailed:
                succeed = False
        return succeed

    @staticmethod
    def judge_file_existed(dirname, filename, fmt):
        '''
        judge_file_existed(dirname, filename, fmt)
        判断在 dirname 目录下是否存在已下载成功的格式为 fmt 且文件名为 filename 的文件
        '''
        filepath = os.path.join(dirname, filename)
        exist1 = os.path.exists(filepath+fmt)
        exist2 = os.path.exists(filepath+fmt+'.aria2')
        return exist1 and not exist2

    @staticmethod
    def align(string, width):  # 对齐汉字字符窜，同时截断多余输出
        '''
        align(string, width) 根据width宽度居中对齐字符窜 string，主要用于汉字居中
        '''
        res = ""
        size = 0
        for ch in string:
            if (size+3 > width):
                break
            size += 1 if ord(ch) <= 127 else 2
            res += ch
        res += (width-size)*' '
        return res


import scipy.io as spio
import h5py
import numpy as np
import os
import sys
sys.path.insert(0,os.getcwd()+'/Modules/')
import MJD
data_path=os.getcwd()+'/Data22D/'

option=0
Window_size=20
num_joints=7

if option ==0:
	save_path=os.getcwd()+'/MJDdata0/'
	filelist=os.listdir(data_path)
	filelist.sort()
	j=1
	for file in filelist:
		data=np.genfromtxt(data_path+file,delimiter=',')
		print(data.shape)
		R,B=MJD.mjd(data,Window_size,option)
		new_data=[]
		for Rs, Bs, dat in zip(R,B,data.T):
			Rs=np.reshape(Rs,(Window_size*num_joints,))
			Bs=np.reshape(Bs,(Window_size*num_joints,))
			new_dat=list(Rs)+list(Bs)
			new_dat=new_dat+list(dat)
			new_data.append(new_dat)
		new_data=np.asarray(new_data).T
		print(new_data.shape)
		np.savetxt(save_path+'/part'+str(j)+'(mjd_option0).csv', new_data, delimiter=",")
		j+=1


# spec1
# print('上手')

# spec5
with open("dictionary-data.txt") as dictionary_file:
    dictionary = dictionary_file.read()
    word_list = dictionary.split("\n")
    id_by_word = dict()
    for id in range(len(word_list)):
        id_by_word[id] = word_list[id]

    input_id = input("単語IDを入力してください: ")
    if input_id.isdecimal():
        input_id = int(input_id)
        print("{}: {}".format(input_id, id_by_word[input_id]))
    else:
        for term in id_by_word.items():
            print("{}: {}".format(term[0], term[1]))

    dictionary_file.close()


#Python file to fetch specific jobs, cvs, categories in command line
# Example usage:
# python3 -u fetch.py 0 542 "description" > output.txt
from pathlib import Path
import sys
import json
import os
def process(jeysan, property_name):
    if property_name == "all":
        print(json.dumps(jeysan))
        os._exit(0)
    else:
        try:
            print(jeysan[property_name])
            os._exit(0)
        except:
            print("invalid property name")
            os._exit(1)

def main():
    if len(sys.argv) != 4:
        print('Usage: python3 fetch.py type id json_property_name')
        os._exit(1)
    job_type = sys.argv[1] #0 for job, 1 for cv, 2 for category
    resource_id = sys.argv[2]
    property_name = sys.argv[3] #can also be 'all'
    try:
        id_string, fullpath = "", ""
        if(job_type == '0'):
            id_string = "jobId"
            fullpath = 'alljobs4rdd/alljobs.jsonl'
        elif(job_type == '1'):
            id_string = "cvid"
            fullpath = 'allcvs4rdd/allcvs.jsonl'
        elif(job_type == '2'):
            id_string = "id"
            fullpath = 'allcategories4rdd/allcategories.jsonl'
        else:
            print('invalid job type')
            os._exit(1)
        contents = Path(fullpath).read_text().strip()
        line_arr = contents.split('\n')
        for line in line_arr:
            try:
                jeysan = json.loads(line)
                if jeysan[id_string] == int(resource_id):
                    process(jeysan, property_name)
            except ValueError:
                print('ValueError')
                continue
    except BaseException as e:
        print('exceptionda')
        print(str(e))
        os._exit(1)

if __name__ == '__main__':
    main()