from utils.config import cfg
from tasks.aneurysm.datasets.base_dataset import BaseDataset
from tasks.aneurysm.datasets.data_utils import *
import random
import numpy as np
import torch
import nibabel as nib
import os
import math
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import scipy.io as scio
from utils.tools.util import progress_monitor
from torch.utils import data
import torch.nn.functional as F

from tasks.aneurysm.rotation_3d import getTransform, transform_point, transform_image

def FindConjunctionPoints(points, volume):
    '''
        在中心线上寻找交叉点
    '''

    pts = list(filter(lambda x: np.sum(volume[x[0]-1:x[0]+2,
                                         x[1]-1:x[1]+2,
                                         x[2]-1:x[2]+2])>=4, points))
    pts = filter(lambda x: np.sum(volume[x[0]-1:x[0]+2,
                                         x[1]-1:x[1]+2,
                                         x[2]-1:x[2]+2])<=6, pts)
    return np.array(list(pts))

def tensor_resize(image, shape, is_label=True):
    x = torch.from_numpy(image[np.newaxis, np.newaxis, ...])
    if is_label:
        y = F.interpolate(x.float(), shape, mode='nearest')
    else:
        y = F.interpolate(x.float(), shape, mode= 'trilinear', align_corners=True)
    y = y.numpy().astype(image.dtype)[0, 0]
    return y

class VesselPatchSampler(object):

    def __init__(self, subject, sample_num, stage='train'):
        self.use_cascade = cfg.TRAIN.DATA.USE_CASCADE if 'USE_CASCADE' in cfg.TRAIN.DATA.keys() else False
        self.use_rotation = cfg.TRAIN.DATA.USE_ROTATION if 'USE_ROTATION' in cfg.TRAIN.DATA.keys() else False
        self.use_skeleton = cfg.TRAIN.DATA.USE_SKELETON if 'USE_SKELETON' in cfg.TRAIN.DATA.keys() else False
        self.use_heatmap = cfg.TRAIN.DATA.USE_HEATMAP if 'USE_HEATMAP' in cfg.TRAIN.DATA.keys() else False

        self.random = False

        self.subject = subject
        if stage == 'train':
            # data_source = np.random.choice([0, 1], p=[0.25, 0.75])
            img_path = os.path.join(cfg.TRAIN.DATA.NII_FOLDER, '%s.nii.gz' % subject)
            # if data_source == 0:
            #     img_path = img_path.replace('vessel_largest', 'vessel_seg_largest')
            img_nii = nib.load(img_path)
        else:
            img_nii = nib.load(
                os.path.join(cfg.TEST.DATA.NII_FOLDER, '%s.nii.gz' % subject)
            )

        spacing = img_nii.header['pixdim'][1:4]
        image = img_nii.get_data()
        mask_path = os.path.join(cfg.TRAIN.DATA.BLOOD_FOLDER, '%s.nii.gz' % subject)
        if os.path.exists(mask_path):
            mask = nib.load(mask_path).get_data()
        else:
            print('{} not exists.'.format(mask_path))
            mask = np.zeros_like(image)

        ##############load skeleton and erase image ####################
        if stage == 'train':
            points = scio.loadmat(
                os.path.join(cfg.TRAIN.DATA.SKELETON_FOLDER, '%s.mat' % subject)
            )
            self.points = points['points']
            # if data_source == 0:
            #     image[image == 3] = 0
            #     image[image > 0] = 1
            # else:
            mask_erase = self.rand_erase(mask, self.points)
        else:
            self.points = []
            erase_path = os.path.join(cfg.TEST.DATA.ERASE_FOLDER, '%s.nii.gz' % subject)
            if not os.path.exists(erase_path):
                erase_path = os.path.join(cfg.TEST.DATA.ERASE_FOLDER, '%s_seg.nii.gz' % subject)
            erase_nii = nib.load(erase_path)
            mask_erase = erase_nii.get_data()
            mask_erase = mask_erase.astype('bool').astype('uint8')

        self.wl, self.ww = cfg.TRAIN.DATA.WL_WW
        image = set_window_wl_ww(image, self.wl, self.ww)
        image = image * 2.0 - 1.0
        mask = mask.astype('bool').astype('uint8')

        self.image = np.transpose(image, (2, 0, 1))
        self.mask = np.transpose(mask, (2, 0, 1))
        self.mask_erase = np.transpose(mask_erase, (2, 0, 1))
        self.spacing = np.roll(spacing, 1, axis=0)
        if len(self.points) > 0:
            self.points = np.roll(self.points, 1, axis=1)
            self.skeleton = np.zeros_like(self.mask)

        ############## load cascade #####################
        if self.use_cascade:
            feature_map = nib.load(
                os.path.join(cfg.TRAIN.DATA.CASCADE_FOLDER, '%s_seg.nii.gz' % subject)).get_data().astype('float32')
            self.feature_map = np.transpose(feature_map, (2, 0, 1))

        ##############load heatmap#####################
        if self.use_heatmap:
            heatmap = nib.load(os.path.join(cfg.TRAIN.DATA.NII_FOLDER, '../heatmap/%s.nii.gz' % subject)).get_data().astype('float32')
            self.heatmap = np.transpose(heatmap, (2, 0, 1))
        else:
            self.heatmap = np.zeros_like(self.mask)

        #########rotation################################
        if self.use_rotation and stage == 'train':
            R_x = (np.random.choice(2) * 2 - 1) * np.random.choice(15)
            R_y = (np.random.choice(2) * 2 - 1) * np.random.choice(15)
            R_z = (np.random.choice(2) * 2 - 1) * np.random.choice(15)
            tt = getTransform(self.mask.shape, R_x, R_y, R_z, (1.0, 1.0, 1.0))
            self.image = transform_image(tt, self.image, 'linear')
            self.mask = transform_image(tt, self.mask)
            self.mask_erase = transform_image(tt, self.mask_erase)
            self.heatmap = transform_image(tt, self.heatmap, 'linear')
            if self.use_cascade:
                self.feature_map = transform_image(tt, self.feature_map, 'linear')

            tt_inv = tt.GetInverse()
            self.points = list(map(lambda x: transform_point(tt_inv, np.array([x[0], x[1], x[2]],'float64')),self.points))
            self.points = list(map(lambda x: np.array([int(x[0]+0.5),int(x[1]+0.5),int(x[2]+0.5)], 'int32'),self.points))

        if self.use_skeleton and stage == 'train':
            #self.skeleton = np.zeros_like(self.mask)
            #for point in points:
            #    x, y, z = point
            #    self.skeleton[x-1:x+2, y-1:y+2, z-1:z+2] = 1
            #self.skeleton = self.skeleton * self.mask
            #self.skeleton[self.skeleton == 0] = 255
            self.skeleton = np.ones_like(self.mask) * 255
            for point in self.points:
                x, y, z = point
                self.skeleton[x-1:x+2, y-1:y+2, z-1:z+2] = self.mask[x-1:x+2, y-1:y+2, z-1:z+2]

        if stage == 'train':
            # if self.use_skeleton:
            #     self.skeleton[self.mask_ignore == 1] = 255
            self.num = sample_num
            self.coords = get_patch_coords(cfg.TRAIN.DATA.PATCH_SIZE, self.mask.shape, 2)
        else:
            self.coords = get_patch_coords(cfg.TEST.DATA.PATCH_SIZE, self.mask.shape, 2)
            self.num = len(self.coords)

        self.sample_idx = 0

    def __len__(self):
        return self.num

    def rand_erase(self, mask, points):
        '''
        erase the input mask to calculate loss
        :param mask: vessel mask
        :param points: skeleton
        :return: mask_erase
        '''
        mask_ske = np.zeros_like(mask)
        mask_ske[points[:, 0], points[:, 1], points[:, 2]] = 1
        np.random.shuffle(points)
        end_points = self.FindEndPoints(points, mask_ske.astype(bool))
        np.random.shuffle(end_points)

        radius_thres = 10
        erased_num_mid = 5
        erased_num_end = 0
        mask_erase = mask.copy()

        r = 12
        n_point = 0
        for point in points:
            x, y, z = point
            point_radius = self.cal_radius3d(mask, point)
            # skip large, main vessel
            sub_mask = mask_erase[x - r:x + r, y - r:y + r, z - r:z + r]
            if point_radius > radius_thres or (sub_mask == 1).sum() > 0:
                continue
            mask_erase[x - r:x + r, y - r:y + r, z - r:z + r] = (sub_mask > 0) * 4
            n_point += 1
            if n_point == erased_num_mid:
                break

        r = 6
        n_point = 0
        for point in end_points:
            x, y, z = point
            # skip main vessel
            sub_mask = mask_erase[x - r:x + r, y - r:y + r, z - r:z + r]
            if (sub_mask == 1).sum() > 0:
                continue
            # skip mid erase
            if (sub_mask == 4).sum() > 0:
                continue
            mask_erase[x - r:x + r, y - r:y + r, z - r:z + r] = (sub_mask > 0) * 5
            n_point += 1
            if n_point == erased_num_end:
                break

        mask_erase[mask_erase == 4] = 0
        mask_erase[mask_erase == 5] = 0
        mask_erase[mask_erase > 0] = 1
        return mask_erase

    def volume_size(self):
        return self.mask.shape

    def volume_mask(self):
        return self.mask

    def volume_heatmap(self):
        return self.heatmap

    def sample_random_coord(self):

        vx, vy, vz = self.mask.shape
        px, py, pz = cfg.TRAIN.DATA.PATCH_SIZE

        x = random.randint(0, vx - px)
        y = random.randint(0, vy - py)
        z = random.randint(0, vz - pz)

        return x, y, z

    def sample_validate(self):
        x, y, z = self.coords[self.sample_idx]
        self.sample_idx = (self.sample_idx + 1) % self.num

        px, py, pz = cfg.TEST.DATA.PATCH_SIZE
        img = self.image[x:x+px, y:y+py, z:z+pz]
        # img = set_window_wl_ww(img, self.wl, self.ww)
        # img = img * 2.0 - 1.0
        img = img.astype('float32')[np.newaxis, ...]
        msk = self.mask[x:x+px, y:y+py, z:z+pz]
        mask_erase = self.mask_erase[x:x + px, y:y + py, z:z + pz]
        mask_erase = mask_erase.astype('float32')[np.newaxis, ...]

        ddict = {
            'img': img,
            'gt': msk,
            'coord': np.array([[x, x+px],[y, y+py],[z, z+pz]], 'int32')
        }
        ddict['erase'] = mask_erase

        if self.use_heatmap:
            hp = self.heatmap[x:x+px, y:y+py, z:z+pz]
            ddict['hp'] = hp[np.newaxis, ...]

        if self.use_cascade:
            feature_map = self.feature_map[x:x + px, y:y + py, z:z + pz]
            feature_map = feature_map * 2.0 - 1.0
            feature_map = feature_map.astype('float32')[np.newaxis, ...]
            ddict['feature'] = feature_map

        return ddict

    def sample_train(self):

        self.sample_idx = (self.sample_idx + 1) % self.num

        vx, vy, vz = self.mask.shape
        px, py, pz = cfg.TRAIN.DATA.PATCH_SIZE

        if self.random:
            x, y, z = self.sample_random_coord()
        else:
            choice = random.randint(0, 3)
            if choice <= 1:
                while 1:
                    x, y, z = self.coords[random.randint(0, len(self.coords)-1)]
                    if self.mask[x:x+px, y:y+py, z:z+pz].sum() >= 100:
                        break

            elif choice == 2:
                x, y, z = self.coords[random.randint(0, len(self.coords)-1)] #random sample from coords
            else:
                x, y, z = self.sample_random_coord() #random sample

        #x = random.randint(0, vx - px)
        #y = random.randint(0, vy - py)
        #z = random.randint(0, vz - pz)

        #img = self.image[x:x+px, y:y+py, z:z+pz]
        #msk = self.mask[x:x+px, y:y+py, z:z+pz]
        #if msk.sum() < 10:
        #    return self.sample_train()
        flip_y = np.random.choice(2) * 2 - 1
        flip_z = np.random.choice(2) * 2 - 1
        img = self.image[x:x+px, y:y+py, z:z+pz]
        # img = set_window_wl_ww(img, self.wl, self.ww)
        # img = img * 2.0 - 1.0
        img = img.astype('float32')[np.newaxis, ...]
        img = img[..., ::flip_y, ::flip_z].copy()
        msk = self.mask[x:x+px, y:y+py, z:z+pz]
        msk = msk[..., ::flip_y, ::flip_z].copy()
        mask_erase = self.mask_erase[x:x + px, y:y + py, z:z + pz]
        mask_erase = mask_erase.astype('float32')[np.newaxis, ...]
        mask_erase = mask_erase[..., ::flip_y, ::flip_z].copy()

        ddict = {'img': img, 'gt': msk, 'subject': self.subject, 'coord': np.array([[x, x+px],[y, y+py],[z, z+pz]], 'int32')}
        ddict['erase'] = mask_erase
        if self.use_skeleton:
            skel = self.skeleton[x:x+px, y:y+py, z:z+pz]
            skel = skel[..., ::flip_y, ::flip_z].copy()
            ddict['skel'] = skel

        if self.use_heatmap:
            hp = self.heatmap[x:x+px, y:y+py, z:z+pz]
            hp = hp[..., ::flip_y, ::flip_z].copy()
            ddict['hp'] = hp[np.newaxis, ...]

        if self.use_cascade:
            feature_map = self.feature_map[x:x + px, y:y + py, z:z + pz]
            feature_map = feature_map * 2.0 - 1.0
            feature_map = feature_map.astype('float32')[np.newaxis, ...]
            ddict['feature'] = feature_map

        return ddict

    def cal_radius3d(self, mask, point):
        width, height, depth = mask.shape
        x, y, z = point

        # along x
        coord = x
        while (coord > 0 and mask[coord, y, z] > 0):
            coord -= 1
        x_min = coord
        coord = x
        while (coord < width and mask[coord, y, z] > 0):
            coord += 1
        x_max = coord

        # along y
        coord = y
        while (coord > 0 and mask[x, coord, z] > 0):
            coord -= 1
        y_min = coord
        coord = y
        while (coord < height and mask[x, coord, z] > 0):
            coord += 1
        y_max = coord

        # along z
        coord = z
        while (coord > 0 and mask[x, y, coord] > 0):
            coord -= 1
        z_min = coord
        coord = z
        while (coord < depth and mask[x, y, coord] > 0):
            coord += 1
        z_max = coord
        radius3d = min([x_max - x_min, y_max - y_min, z_max - z_min])
        return radius3d

    def FindEndPoints(self, points, volume):
        '''
            在中心线上寻找端点
        '''

        pts = filter(lambda x: np.sum(volume[x[0] - 1:x[0] + 2,
                                      x[1] - 1:x[1] + 2,
                                      x[2] - 1:x[2] + 2]) <= 2, points)
        return np.array(list(pts))

class VesselSubjectDS(BaseDataset):

    def __init__(self, para_dict, stage, VS=VesselPatchSampler):
        self.VS = VS
        super(VesselSubjectDS, self).__init__(para_dict, stage)

    def train_init(self):

        self.subjects = self.para_dict.get("subjects", None)
        self.sample_num_per_subject = self.para_dict.get("sample", 64)
        self.num = len(self.subjects) * self.sample_num_per_subject
        #self.subject = ''
        #self.data_sampler = None
        self.cache_subjects = []
        self.cache_samplers = []
        self.max_cache_num = self.para_dict.get("cache_num", 10)

    def train_load(self, index):

        subject = self.subjects[index // self.sample_num_per_subject]
        if subject not in self.cache_subjects:
            self.cache_subjects.append(subject)
            self.cache_samplers.append(
                self.VS(
                    subject,
                    self.sample_num_per_subject,
                    stage='train'
                )
            )
            if len(self.cache_subjects) > self.max_cache_num:
                self.cache_subjects.pop(0)
                self.cache_samplers.pop(0)

        return self.cache_samplers[self.cache_subjects.index(subject)].sample_train()

        #if self.subject != subject:
        #    self.subject = subject
        #    self.data_sampler = self.VS(subject, self.sample_num_per_subject)
        #return self.data_sampler.sample_train()

    def val_init(self):
        self.subject = self.para_dict.get("subject", None)
        self.data_sampler = self.VS(
            self.subject,
            -1,
            stage='validate'
        )
        self.num = len(self.data_sampler)

    def val_load(self, index):
        return self.data_sampler.sample_validate()

    def volume_size(self):
        return self.data_sampler.volume_size()

    def volume_mask(self):
        return self.data_sampler.volume_mask()

    def volume_heatmap(self):
        return self.data_sampler.volume_heatmap()

    def save(self, seg, res_pth, prob=False):
        img_nii = nib.load(os.path.join(cfg.TEST.DATA.NII_FOLDER, '%s.nii.gz' % self.subject))
        orign_size = tuple(np.roll(img_nii.header['dim'][1:4], 1, 0))
        if orign_size != seg.shape[-3:]:
            seg = tensor_resize(seg, orign_size, False if prob else True)

        # if img_nii.header['pixdim'][0] > 0:
        #     seg = seg[::-1, ...]
        seg = np.transpose(seg, (1, 2, 0))
        #seg = np.transpose(seg, (0, 2, 3, 1))
        seg = seg.astype('float32' if prob else 'uint8')
        affine = img_nii.affine
        seg_img = nib.Nifti1Image(seg, affine)
        nib.save(seg_img, res_pth)


#!/usr/local/bin/python3
""" Combine fasta files from multiple sequencing runs to make unpaired fasta files, one per individual
Author: Kelly Sovacool
Email: kellysovacool@uky.edu
Date: 29 Mar. 2018

Usage:
    ./combine_seq_runs.py <seq_runs_dir> <output_dir>

Options:
    -h --help   display this super helpful message
"""
import docopt
import os
import subprocess

def main(args):
    individual_ids = set()
    if not os.path.isdir(args['<output_dir>']):
        os.mkdir(args['<output_dir>'])
    for subdir in os.listdir(args['<seq_runs_dir>']):
        if os.path.isdir(args['<seq_runs_dir>'] + subdir):
            subdir = check_directory(subdir)
            for filename in os.listdir(args['<seq_runs_dir>'] + subdir):
                if os.path.isfile(args['<seq_runs_dir>'] + subdir + filename):
                    indiv_id = filename.split('_')[0]
                    individual_ids.add(indiv_id)
                    with open(args['<seq_runs_dir>'] + subdir + filename, 'r') as input_file:
                        with open(args['<output_dir>'] + indiv_id + '.tmp', 'a') as output_file:
                            for line in input_file:
                                output_file.write(line)
    for indiv_id in individual_ids:
        subprocess.call(['fastx_renamer', '-n', 'COUNT', '-i', args['<output_dir>'] + indiv_id + '.tmp', '-o', args['<output_dir>'] + indiv_id + '.fna'])
        os.remove(args['<output_dir>'] + indiv_id + '.tmp')

def check_directory(dir_name):
        if not dir_name.endswith('/'):
            dir_name = dir_name + '/'
        return dir_name

if __name__ == "__main__":
    arguments = docopt.docopt(__doc__)
    for arg in arguments:
        arguments[arg] = check_directory(arguments[arg])
    main(arguments)


import numpy as np
from numpy.lib.shape_base import apply_along_axis
import pandas as pd
from pandas import Series, DataFrame, Index
from functools import reduce
import warnings
warnings.filterwarnings("ignore")

# #Helsingin seudun väestöennusteohjelmisto, UUSI VERSIO 2020;
# #HUOM. VAIN Digiajan väestöennustejärjestelmä -Pääkaupunkiseudun mallityön projektiryhmän käyttöön, ei ulkopuolelle;
# #Seppo Laakso, Kaupunkitutkimus TA, versio 25.11.2020;
# #Tämän SAS-kooditiedoston nimi: Seuenn manual_V2020;
# Käännöstä Pythoniin työstänyt Miro Varilo

def main():
    # sasvertaus = pd.read_sas('hs_ve0_2019_2040.sas7bdat')
    # tulostaTaulukko(sasvertaus, 'SASvertaus.xlsx')

    # #TIEDOSTOT JA YEISPARAMETRIT;
    # #Lähtötiedoston nimi LIB-viiteineen, SAS-data;
    ltied_sas = 'Parametrit\\vaesto2019.sas7bdat'
    # #Tulostiedoston (Ennuste) nimi, SAS-data, ei käytössä
    etied = pd.read_sas('Parametrit\\hs_ve0_2019_2040.sas7bdat')
    # #Tulostiedoston (Ennuste) nimi polkuineen ja taulun nimi Excelissä;
    enn_exc = 'HS_Ve0_2019_2040.xls'
    enn_taul = 'Ve0'
    # #Lähtövuosi (=lähtötiedoston vuosiluku) ja ennustevuosi (=ennusteen viimeinen vuosi);
    lvuosi = 2019
    evuosi = 2040
    # #Syntyvien sp-jakauma: poikien osuus;
    Poikaos = 0.51

    # #PARAMETRITIEDOSTOT EXCEL;
    # #Erikseen Excel-tiedoston nimi ja taulun nimi;
    # #hedelmällisyys;
    hed_exc = 'Parametrit\\Hedelmallisyys.xls'
    # kuolemanvaara;
    kuo_exc = 'Parametrit\\Kuolemanvaara.xls'
    kuo_taul = 'Ve0'
    # HS sisäinen lähtömuutto;
    hsl_exc = 'Parametrit\\HSsis_lahto_2017_19.xls'
    hsl_taul = 'Ve0'
    # HS sisäinen tulomuutto;
    hst_exc = 'Parametrit\\HSsis_tulo_2017_19.xls'
    hst_taul = 'Ve0'
    # Työmarkkina lähtömuutto;
    tml_exc = 'Parametrit\\Tyomark_lahto_2017_19.xls'
    tml_taul = 'Ve0'
    # Työmarkkina tulomuutto;
    tmt_exc = 'Parametrit\\Tyomark_tulo_2017_19.xls'
    tmt_taul = 'Ve0'
    # Ulkomainen lähtömuutto;
    uml_exc = 'Parametrit\\Ulkom_lahto_2017_19.xls'
    uml_taul = 'Ve0'
    # Ulkomainen tulomuutto;
    umt_exc = 'Parametrit\\Ulkom_tulo_2017_19.xls'
    umt_taul = 'Ve0'

    # TÄSTÄ ETEENPÄIN KÄYTTÄJÄN EI TARVITSE MUUTTAA MITÄÄN (NORMAALITILANTEESSA);

    # VAIHE 2: LUETAAN PARAMETRITIEDOSTOT EXCELISTÄ

    # Lähtöväestö, esimerkkitiedoissa SAS-datasta, vaihda read_excel mikäli luet Excel-tiedostosta
    ltied = pd.read_sas(ltied_sas)
    print(ltied.head())

    # hedelmällisyys;
    hed = pd.read_excel(hed_exc)
    hed.sort_values(by=['alue', 'vuosi'])

    # kuolemanvaara;
    kuo = pd.read_excel(kuo_exc)
    kuo.sort_values(by=['alue', 'vuosi'])
    # HS sisäinen lähtömuutto;
    hsl = pd.read_excel(hsl_exc)
    hsl.sort_values(by=['alue', 'vuosi'])
    # HS sisäinen tulomuutto;
    hst = pd.read_excel(hst_exc)
    hst.sort_values(by=['alue', 'vuosi'])
    # Työmarkkina lähtömuutto;
    tml = pd.read_excel(tml_exc)
    tml.sort_values(by=['alue', 'vuosi'])
    # Työmarkkina tulomuutto;
    tmt = pd.read_excel(tmt_exc)
    tmt.sort_values(by=['alue', 'vuosi'])
    # Ulkomainen lähtömuutto;
    uml = pd.read_excel(uml_exc)
    uml.sort_values(by=['alue', 'vuosi'])
    # Ulkomainen tulomuutto;
    umt = pd.read_excel(umt_exc)
    umt.sort_values(by=['alue', 'vuosi'])

    print('Vaihe 1 ja 2 onnistuivat. Parametritiedostot luettu onnistuneesti. Aloitetaan ennusteen laskeminen (vie aikaa)...')
    # VAIHE 3: LUETAAN TIEDOSTOT ENNUSTEOHJELMAAN JA LASKETAAN ENNUSTEET;

    # Luetaan väestörakenteen lähtötiedosto ja poimitaan ennusteen lähtövuotta vastaava vuosi;

    vaesto = ltied.loc[ltied['vuosi'] == lvuosi]

    # Yhdistetään lähtötiedosto ja parametrit;
    ennuste = [vaesto, hed, kuo, hsl, hst, tml, tmt, uml, umt]
    ennuste = reduce(lambda left, right: pd.merge(left, right, on=['alue', 'vuosi'],
                                                how='outer'), ennuste)
    ennuste = ennuste.sort_values(by=['alue', 'vuosi'])
    ennuste = ennuste.drop(columns=['Kuvaus_x'])
    ennuste = ennuste.drop_duplicates()

    # print(ennuste.head())

    # print(ennuste['tmtyht'])

    # ENNUSTEEN LASKENTA;

    # data ennuste; set yhd0; by alue;
    # 	if &lvuosi<=vuosi<=&evuosi;			#poimitaan ennustejakson vuodet, ml. lähtövuosi;
    # Taulukot joissa väestö- ja parametrimuuttujat ryhmittäin;

    # miehet 31.12.vvvv
    indata = ['m' + str(x) for x in range(100)]
    mi = ennuste[indata]

    # naiset 31.12.vvvv
    indata = ['n' + str(x) for x in range(100)]
    ni = ennuste[indata]

    # apumuuttujat joihin viedään ed.v. väestö;
    # apumuuttujat miehet;
    apumi = mi

    # apumuuttujat naiset;
    apuni = ni

    # hedelmällisyydet;
    indata = ['h' + str(x) for x in range(15, 50)]
    hed = ennuste[indata]

    # # apumuuttujat hedelmällisyysikäiset naiset;
    indata = ['n' + str(x) for x in range(15, 50)]
    apuhn = ennuste[indata].copy()

    #  miesten kuolemanvaarat, 0-99-v;
    indata = ['mkv' + str(x) for x in range(100)]
    mkv = ennuste[indata]
    #  naisten kuolemanvaarat, 0-99-v;
    indata = ['nkv' + str(x) for x in range(100)]
    nkv = ennuste[indata]
    # miesten työm.muutto tulo;
    indata = ['mtt' + str(x) for x in range(100)]
    mtmt = ennuste[indata]
    # naisten työm.muutto tulo;
    indata = ['ntt' + str(x) for x in range(100)]
    ntmt = ennuste[indata]
    # miesten ulkom. muutto tulo;
    indata = ['mut' + str(x) for x in range(100)]
    mumt = ennuste[indata]
    # naisten ulkom. muutto tulo;
    indata = ['nut' + str(x) for x in range(100)]
    numt = ennuste[indata]
    # miesten HS sis. muutto tulo;
    indata = ['mst' + str(x) for x in range(100)]
    msmt = ennuste[indata]
    # naisten HS sis. umuutto tulo;
    indata = ['nst' + str(x) for x in range(100)]
    nsmt = ennuste[indata]
    # miesten työm.muutto lähtö;
    indata = ['mtl' + str(x) for x in range(100)]
    mtml = ennuste[indata]
    # naisten työm.muutto lähtö;
    indata = ['ntl' + str(x) for x in range(100)]
    ntml = ennuste[indata]
    # miesten ulkom. muutto lähtö;
    indata = ['mul' + str(x) for x in range(100)]
    muml = ennuste[indata]
    # naisten ulkom. muutto lähtö;
    indata = ['nul' + str(x) for x in range(100)]
    numl = ennuste[indata]
    # miesten HS sis. muutto lähtö;
    indata = ['msl' + str(x) for x in range(100)]
    msml = ennuste[indata]
    # naisten HA sis. muutto lähtö;
    indata = ['nsl' + str(x) for x in range(100)]
    nsml = ennuste[indata]

    mkvs = ennuste['mkvs']
    nkvs = ennuste['nkvs']

    tmtyht = ennuste['tmtyht']
    umtyht = ennuste['umtyht']
    smtyht = ennuste['smtyht']
    tmlyht = ennuste['tmlyht']
    umlyht = ennuste['umlyht']
    smlyht = ennuste['smlyht']
    iyht = ennuste['iyht']

    synt = 0

    # *ENNUSTEEN LASKENTA;

    # print(ennuste)
    ennusteAlue = 0
    firstAlue = True

    for x in range(0, len(ennuste.index)):

        if (ennusteAlue == ennuste['alue'].iloc[x]):
            firstAlue = False
        else:
            ennusteAlue = ennuste['alue'].iloc[x]
            firstAlue = True

    # 	*ikäryhmien ennusteet 1-98-vuotiaille;
        if (firstAlue == False):
            apumi = mi
            apuni = ni
            for i in range(0, 99):
                mi.iloc[x, i+1] = apumi.iloc[x-1, i]+tmtyht.iloc[x] * \
                    mtmt.iloc[x, i]+umtyht.iloc[x]*mumt.iloc[x, i] + \
                    smtyht.iloc[x]*msmt.iloc[x, i]-tmlyht.iloc[x] * \
                    mtml.iloc[x, i]-umlyht.iloc[x]*muml.iloc[x, i]-smlyht.iloc[x] * \
                    msml.iloc[x, i]-apumi.iloc[x-1, i]*mkv.iloc[x, i]/1000

                ni.iloc[x, i+1] = apuni.iloc[x-1, i]+tmtyht.iloc[x] * \
                    ntmt.iloc[x, i]+umtyht.iloc[x]*numt.iloc[x, i] + \
                    smtyht.iloc[x]*nsmt.iloc[x, i]-tmlyht.iloc[x] * \
                    ntml.iloc[x, i]-umlyht.iloc[x]*numl.iloc[x, i]-smlyht.iloc[x] * \
                    nsml.iloc[x, i]-apuni.iloc[x-1, i]*nkv.iloc[x, i]/1000

    # 	*ennuste 99+ -vuotiaille;
        if (firstAlue == False):
            mi.iloc[x, 99] = apumi.iloc[x-1, 98]+tmtyht.iloc[x] * \
                mtmt.iloc[x, 98]+umtyht.iloc[x]*mumt.iloc[x, 98] + \
                smtyht.iloc[x]*msmt.iloc[x, 98]-tmlyht.iloc[x] * \
                mtml.iloc[x, 98]-umlyht.iloc[x]*muml.iloc[x, 98]-smlyht.iloc[x] * \
                msml.iloc[x, 98]-apumi.iloc[x-1, 98]*mkv.iloc[x, 98]/1000+apumi.iloc[x-1, 99]+tmtyht.iloc[x] * \
                mtmt.iloc[x, 99]+umtyht.iloc[x]*mumt.iloc[x, 99] + \
                smtyht.iloc[x]*msmt.iloc[x, 99]-tmlyht.iloc[x] * \
                mtml.iloc[x, 99]-umlyht.iloc[x]*muml.iloc[x, 99]-smlyht.iloc[x] * \
                msml.iloc[x, 99]-apumi.iloc[x-1, 99]*mkv.iloc[x, 99]/1000

            ni.iloc[x, 99] = apuni.iloc[x-1, 98]+tmtyht.iloc[x] * \
                ntmt.iloc[x, 98]+umtyht.iloc[x]*numt.iloc[x, 98] + \
                smtyht.iloc[x]*nsmt.iloc[x, 98]-tmlyht.iloc[x] * \
                ntml.iloc[x, 98]-umlyht.iloc[x]*numl.iloc[x, 98]-smlyht.iloc[x] * \
                nsml.iloc[x, 98]-apuni.iloc[x-1, 98]*nkv.iloc[x, 98]/1000+apuni.iloc[x-1, 99]+tmtyht.iloc[x] * \
                ntmt.iloc[x, 99]+umtyht.iloc[x]*numt.iloc[x, 99] + \
                smtyht.iloc[x]*nsmt.iloc[x, 99]-tmlyht.iloc[x] * \
                ntml.iloc[x, 99]-umlyht.iloc[x]*numl.iloc[x, 99]-smlyht.iloc[x] * \
                nsml.iloc[x, 99]-apuni.iloc[x-1, 99]*nkv.iloc[x, 99]/1000

    # 	*syntyneet summa, huom. hedelmällisyydet 1/1000;
    # *hed.ik.naiset (ed.v.31.12) *hedelmällisyys, ikä 15-49;
        if(firstAlue == False):
            indata = ['n' + str(x) for x in range(15, 50)]
            apuhn = ni[indata].copy()
            synt = 0
            for i in range(0, 35):
                synt = synt + hed.iloc[x, i]*apuhn.iloc[x-1, i]/1000
    # 	*vuoden aikana syntyneet 0-vuotiaiksi 31.12.;
    # 	*Otetaan huomioon syntyvien kuolemanvaara syntymävuoden aikana;
        if(firstAlue == False):
            mi.iloc[x, 0] = Poikaos*synt*(1-mkvs.iloc[x]/1000)
            ni.iloc[x, 0] = (1-Poikaos)*synt*(1-nkvs.iloc[x]/1000)
    # 	*kuolleet summa, sisältää myös syntymävuonna kuolleet pojat ja tytöt, huom. kuolemanvaarat 1/1000;
    #  	  		kuol1  *Kuolleet ilman synt.v. kuolleita;
        if(firstAlue == False):
            kuol1 = apumi.iloc[x, i]*mkv.iloc[x, 0] / \
                1000+apuni.iloc[x, 0]*nkv.iloc[x, 0]/1000
    # 		kuol2  *Synt.vuonna kuolleet;
            kuol2 = Poikaos*synt*mkvs.iloc[x]/1000 + \
                (1-Poikaos)*synt*nkvs.iloc[x]/1000
    # 		 *Kuolleet yht.;
            kuol = kuol1+kuol2

    # 	*yhteenvetosummia;
        # if(firstAlue == False):
        summa = mi.iloc[x].sum() + ni.iloc[x].sum()
        iyht.iloc[x] = summa

    # 	*apumuuttujille arvot 31.12.vvvv;
        apumi = mi
        apuni = ni
        # Päivitys seuraavalle vuodelle
        ennuste.update(mi)
        ennuste.update(ni)
        ennuste.update(iyht)

    print('Vaihe 3 onnistui. Tulostetaan ennusteet tiedostoksi...')
    # *VAIHE 4: TULOSTETAAN ENNUSTE EXCEL-TAULUKSI

    # *Tulostetaan pysyvään tiedostoon parametrit ym. poistettuna;

    men = ['m' + str(x) for x in range(100)]
    women = ['n' + str(x) for x in range(100)]
    # var = ['iyht', 'myht', 'nyht', 'synt', 'kuol', 'tmtyht', 'umtyht', 'smtyht', 'tmlyht', 'umlyht', 'smlyht']
    var = ['alue', 'vuosi', 'iyht']
    var = var + men + women
    ennuste = ennuste[var]

    tiedostonNimi = enn_exc
    tulostus = ennuste
    tulostaTaulukko(tulostus, tiedostonNimi)

def tulostaTaulukko(tulostus, tiedostonNimi):
    try:
        writer = pd.ExcelWriter(tiedostonNimi)
        tulostus.to_excel(writer, 'output_sheet')
        writer.save()
        print('Ennuste onnistui. Tallennettu tiedostoon ' + tiedostonNimi)
    except PermissionError:
        print(
            'Tallentaminen epäonnistui! Ei oikeuksia tallentaa (tiedosto kenties jo auki).')
        user_input = input("Haluatko yrittää uudelleen? Y/N: ")
        if(user_input == 'Y'):
            tulostaTaulukko(tulostus, tiedostonNimi)

def tulostaTaulukkoCSV(tulostus, tiedostonNimi):
    try:
        # writer = pd.ExcelWriter(tiedostonNimi)
        tulostus.to_csv(tiedostonNimi)
        # writer.save()
        print('Ennuste onnistui. Tallennettu tiedostoon ' + tiedostonNimi)
    except PermissionError:
        print(
            'Tallentaminen epäonnistui! Ei oikeuksia tallentaa (tiedosto kenties jo auki).')
        user_input = input("Haluatko yrittää uudelleen? Y/N: ")
        if(user_input == 'Y'):
            tulostaTaulukko(tulostus, tiedostonNimi)

if __name__ == "__main__":
    main()

def indataf(indata, x, function=None):
    if indata == 1:
        if function:
            x = function(x)
        return x
    else:
        return np.NaN


from cmd import Cmd
from IPython.display import clear_output
from IlluminatiWindow import IlluminatiWindow
from IlluminatiEngine import IlluminatiEngine

class IlluminatiShell(Cmd):
    intro = 'Illuminati shell. Type help or ? to list commands.\n'
    prompt = 'Illuminati> '

    def print_puzzle(self, puzzle):
        clear_output(wait=True)
        i=1
        print("  ",end="")
        for col_num in range(1,len(puzzle[0])+1):
            print(f"{col_num:2d}",end="")
        print()
        for linha in puzzle:
            print(f"{i:2d}",end=" ")
            i+=1
            for simbolo in linha:
                print(simbolo,end=" ")
            print()

    def do_mr(self, arg):
        " -  comando mostrar que leva como parâmetro o nome de um ficheiro..: mr <nome_ficheiro> \n" \
        " -  opcionalmente pode colocar um segundo parametro com o tamanho da casa..: mr <nome_ficheiro> <tamanho>\n"
        lista_arg = arg.split()
        try:
            lista_arg = arg.split()
            eng.ler_tabuleiro_ficheiro(lista_arg[0])
            self.print_puzzle(eng.gettabuleiro())
            #self.do_ver(arg) #ou as linhas seguintes
            global janela  # pois pretendo atribuir um valor a um identificador global
            if janela is not None:
                del janela  # invoca o metodo destruidor de instancia __del__()
            janela = IlluminatiWindow(40, eng.getlinhas(), eng.getcolunas())
            janela.mostraJanela(eng.gettabuleiro())
        except Exception as e:
            print("Erro: ", e)

    def do_cr(self, arg):
        " -  comando carregar que leva como parâmetro o nome de um ficheiro..: cr <nome_ficheiro> \n"
        try:
            lista_arg = arg.split()
            eng.gravar_tabuleiro_ficheiro(lista_arg[0])
            print("Tabuleiro guardado no ficheiro: "+lista_arg[0])
        except:
            print("Formato inválido!")

    def do_gr(self, arg):
        " -  comando gravar que leva como parâmetro o nome de um ficheiro..: gr <nome_ficheiro> \n"
        try:
            lista_arg = arg.split()
            eng.gravar_tabuleiro_ficheiro(lista_arg[0])
            print("Tabuleiro guardado no ficheiro: "+lista_arg[0])
        except:
            print("Formato inválido!")

    def do_jg(self, arg):
        " - Colocação da lâmpada se possível nas coordenadas (linha, coluna) inseridas..: jg <coordenadas> \n" \
        " - Exemplo formato das coordenadas 'jg 1 2'"
        try:
            jogada = arg.split()
            lin = int(jogada[0]) - 1
            col = int(jogada[1]) - 1
            if eng.jg(lin, col):
                self.print_puzzle(eng.gettabuleiro())
                self.do_ver(arg)
                self.do_verify_victory(arg)
            else:
                print("Casa bloqueada!")
        except:
            print("Formato de coordenadas inválido!")

    def do_mc(self, arg):
        " - Marcação da casa nas coordenadas inseridas (linha, coluna) onde será impossível colocar uma lâmpada..: mc <coordenadas> \n" \
        " - Exemplo formato 'mc 1 2'"
        try:
            jogada = arg.split()
            lin = int(jogada[0]) - 1
            col = int(jogada[1]) - 1
            if eng.mc(lin, col):
                self.print_puzzle(eng.gettabuleiro())
                self.do_ver(arg)
            else:
                print("Casa bloqueada!")
        except:
            print("Formato de coordenadas inválido!")

    def do_est1(self, arg):
        " - Execução da estratégia 1..: est1\n" \
        " - Casa com um número n com exatamente n casas vizinhas, então essas mesmas terão lâmpadas"
        eng.estrategia1()
        self.print_puzzle(eng.gettabuleiro())
        self.do_ver(arg)
        self.do_verify_victory(arg)

    def do_est2(self, arg):
        " - Execução da estratégia 2..: est2\n" \
        " - Marcar como iluminadas todas as casas na mesma linha e coluna de uma lâmpada (que não estejam bloqueadas)"
        eng.estrategia2()
        self.print_puzzle(eng.gettabuleiro())
        self.do_ver(arg)
        self.do_verify_victory(arg)

    def do_est3(self, arg):
        " - Execução da estratégia 3..: est3\n" \
        " - Bloquear todas as casas em que se verifique impossível que esta contenha uma lâmpada"
        eng.estrategia3()
        self.print_puzzle(eng.gettabuleiro())
        self.do_ver(arg)
        self.do_verify_victory(arg)

    def do_est4(self, arg):
        " - Execução da estratégia 4..: est4\n" \
        " - Em casos de dois números numa diagonal (1 e 3) e haver certeza que as casas partilhadas não podem ter as duas iluminadas"
        eng.estrategia4()
        self.print_puzzle(eng.gettabuleiro())
        self.do_ver(arg)
        self.do_verify_victory(arg)

    def do_est5(self, arg):
        " - Execução da estratégia 5..: est5\n" \
        " - Verifica se uma casa não iluminada só pode ser iluminada se uma dada casa tiver uma lâmpada (devido a outras restrições)"
        eng.estrategia5()
        self.print_puzzle(eng.gettabuleiro())
        self.do_ver(arg)
        self.do_verify_victory(arg)

    def do_est6(self, arg):
        " - Execução da estratégia 6..: est6\n" \
        " - Caso as estratégias 1-5 não resolvam totalmente o puzzle, aplica-se a tentativa-erro para casas com apenas duas opções disponíveis para ficar iluminada"
        eng.estrategia6()
        self.print_puzzle(eng.gettabuleiro())
        self.do_ver(arg)
        self.do_verify_victory(arg)

    def do_est7(self, arg):
        " - Execução da estratégia 7 no puzzle..: est7\n" \
        " - Caso as estratégias 1-6 não resolvam totalmente o puzzle, aplica-se a tentativa-erro para casas com 3 ou mais opções disponíveis para ficar iluminada"
        eng.estrategia7()
        self.print_puzzle(eng.gettabuleiro())
        self.do_ver(arg)
        self.do_verify_victory(arg)

    def do_undo(self, arg):
        " - Retorna um passo atrás no tabuleiro..: undo"
        if eng.undo():
            self.print_puzzle(eng.gettabuleiro())
            self.do_ver(arg)
        else:
            print("Encontra-se no início do tabuleiro, não é possível retornar!")

    def do_rtstart(self, arg):
        " - Retorna o puzzle para o início..: rtstart"
        eng.returnStart()
        self.print_puzzle(eng.gettabuleiro())
        self.do_ver(arg)

    def do_resolve(self, arg):
        " - Resolve o puzzle automaticamente..: resolve"
        eng.resolve()
        self.print_puzzle(eng.gettabuleiro())
        self.do_ver(arg)
        self.do_verify_victory(arg)

    def do_verify_victory(self, arg):
        " - Verifica o puzzle construído no momento e passa uma mensagem de resposta..: verify_victory"
        iluminado = True
        lampadas = True
        if eng.verify_lights() is False: iluminado = False
        if eng.check_numbers() is False: lampadas = False
        if iluminado and lampadas: print("Puzzle resolvido corretamente!")
        elif iluminado is False and lampadas: print("Puzzle todo iluminado, mas lâmpadas colocadas incorretamente!")
        elif iluminado and lampadas is False: print("Lâmpadas corretamente colocadas, mas puzzle não se encontra totalmente iluminado!")
        else: print("Puzzle incorreto!")

    def do_ver(self, arg):
        " - Comando para visualizar o estado atual do puzzle em ambiente grafico caso seja válido: VER  \n"
        global janela  # pois pretendo atribuir um valor a um identificador global
        if janela is not None:
            del janela  # invoca o metodo destruidor de instancia __del__()
        janela = IlluminatiWindow(40, eng.getlinhas(), eng.getcolunas())
        janela.mostraJanela(eng.gettabuleiro())

    def do_sair(self, arg):
        "Sair do programa illuminati: sair"
        print('Obrigado por ter utilizado o illuminati, espero que tenha sido divertido!')
        global janela  # pois pretendo atribuir um valor a um identificador global
        if janela is not None:
            del janela  # invoca o metodo destruidor de instancia __del__()
        return True

if __name__ == '__main__':
    eng = IlluminatiEngine()
    janela = None
    sh = IlluminatiShell()
    sh.cmdloop()


from histo_loader import HistoDataset
from torch.utils import data
import torch
import torchvision
import matplotlib.pyplot as plt
import numpy as np
import time
import os
import pixel_net_eq256_kplus as pixel_net
import torch.nn.functional as F
from torch import optim
from utils.training_function import l1_loss,l2_loss,dice_loss_logit,AdjustLearningRate
#import torch.multiprocessing as mp
#mp.set_start_method('spawn')

iters=9999999
batch=16
init_lr=0.001
path_to_data_train='/media/ubmi/DATA2/vicar/cam_dataset/train/data'
path_to_data_valid='/media/ubmi/DATA2/vicar/cam_dataset/valid/data'
k0=16
k=8
gconv=0
lvl=1

save_dir='../results/s1_pixel_baseinfsampler'

try:
    os.makedirs(save_dir)
except:
    pass

#def worker_init_fn(worker_id):
#    np.random.seed(np.random.get_state()[1][0] + worker_id)

if __name__ == '__main__':

    loader = HistoDataset(split='train',path_to_data=path_to_data_train,level=lvl)
    trainloader= data.DataLoader(loader, batch_size=batch, num_workers=3, shuffle=True,drop_last=True,pin_memory=False,worker_init_fn=None)

    loader = HistoDataset(split='valid',path_to_data=path_to_data_valid,level=lvl)
    validloader= data.DataLoader(loader, batch_size=batch, num_workers=3, shuffle=False,drop_last=False,pin_memory=False)

    def inf_train_gen():
        while True:
#            np.random.seed()
            for img,mask,lbl in trainloader:
                yield img,mask,lbl

    gen = inf_train_gen()

    model=pixel_net.PixelNet(K=k0,kplus=k,input_size=3,gconv=gconv)
    model=model.cuda(0)

    optimizer = optim.Adam(model.parameters(),lr = init_lr ,betas= (0.9, 0.999),eps=1e-8,weight_decay=1e-8)

    it=0
    stop=0
    loss_train=[]
    acc_train=[]
    it_train=[]
    loss_valid=[]
    acc_valid=[]
    it_valid=[]
    losses=[]
    accs=[]
    while it<iters and stop==0:
        it=it+1
#        print(it)

        img,mask,lbl=next(gen)

        img=img.cuda(0)
        mask=mask.cuda(0)
        lbl=lbl.cuda(0)

        img.requires_grad=True
        mask.requires_grad=True

        model.train()

        output=model(img)

        loss = F.binary_cross_entropy_with_logits(output.squeeze(),lbl.squeeze())

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        clasif=torch.sigmoid(output)

        lbl=lbl.view(-1).detach().cpu().numpy()
        clasif=clasif.view(-1).detach().cpu().numpy()
        acc=np.sum(lbl==(clasif>0.5))/clasif.shape[0]

        loss=loss.detach().cpu().numpy()
        losses.append(loss)
        accs.append(acc)

        if it % 300 == 0:

            loss_train.append(np.mean(losses))
            acc_train.append(np.mean(accs))
            it_train.append(it)

            losses=[]
            accs=[]
            for itt,(img,mask,lbl) in enumerate(validloader):
                img=img.cuda(0)
                mask=mask.cuda(0)
                lbl=lbl.cuda(0)

                model.eval()

                output=model(img)

                loss = F.binary_cross_entropy_with_logits(output.squeeze(),lbl.squeeze())

                clasif=torch.sigmoid(output)

                lbl=lbl.view(-1).detach().cpu().numpy()
                clasif=clasif.view(-1).detach().cpu().numpy()
                acc=np.sum(lbl==(clasif>0.5))/clasif.shape[0]

                loss=loss.detach().cpu().numpy()
                losses.append(loss)
                accs.append(acc)

            loss_valid.append(np.mean(losses))
            acc_valid.append(np.mean(accs))
            it_valid.append(it)

            losses=[]
            accs=[]

            torch.save(model,save_dir + '/'+ str(acc_valid[-1])+ '__' + str(it).zfill(8)+'.pkl')

            np.savez(save_dir +'/training_log_train.npy', np.array(it_train),np.array(it_valid),np.array(loss_train),np.array(loss_valid))
            np.savez(save_dir +'/training_log_test.npy', np.array(it_train),np.array(it_valid),np.array(acc_train),np.array(acc_valid))

            for param_group in optimizer.param_groups:
                lr_act=param_group['lr']

            print(str(it) + ' train loss: ' + str(loss_train[-1]) +  '  train ACC: ' +str(acc_train[-1])
            + ' test loss: ' + str(loss_valid[-1]) +  '  test ACC: ' +str(acc_valid[-1]) + '  lr: ' +str(lr_act))

            plt.plot(it_train,loss_train)
            plt.plot(it_valid,loss_valid)
            plt.show()

            plt.plot(it_train,acc_train)
            plt.plot(it_valid,acc_valid)
            plt.show()


import sqlite3
from pprint import pprint
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
import shutil
from pathlib import Path
from time import perf_counter
from typing import Tuple
import threading
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import wait
import itertools
from multiprocessing import Pool
import time

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter
import mplfinance as mpf
import pandas as pd
import pandas_market_calendars as mcal
#import progressbar
import tqdm

import settings

PREDICT_WINDOW = 10
PLOT_LEN = 100
OUTPUT_DIR = Path(__file__).absolute().parent / 'plots2'
NUM_WORKERS = 8

def previous_n_trading_days(end_date: str, num_days: int):

    start_date = datetime.strptime(end_date, '%Y-%m-%d').date() - relativedelta(years=2)
    start_date = start_date.strftime('%Y-%m-%d')

    nyse = mcal.get_calendar('NYSE').schedule(start_date, end_date).iloc[-num_days:]
    nasdaq = mcal.get_calendar('NASDAQ').schedule(start_date, end_date).iloc[-num_days:]

    assert len(nyse) == num_days
    assert len(nasdaq) == num_days

    pd.testing.assert_frame_equal(nyse, nasdaq)

    return tuple(pd.to_datetime(day).strftime('%Y-%m-%d') for day in nyse.index.values)

#num_tasks = 0
#task_idx = itertools.count()
#bar = None

def run_tasks():

    with sqlite3.connect(settings.DATA_DIRECTORY / 'topk.sqlite3') as db:
        prediction_days = tuple(zip(*db.execute('SELECT DISTINCT date FROM predicted;')
                                    .fetchall()))[0][:-1]
    tasks = list()
    for day in prediction_days:

        with sqlite3.connect(settings.DATA_DIRECTORY / 'topk.sqlite3') as db:
            true_symbols = tuple(zip(*db.execute('''
SELECT DISTINCT symbol FROM true WHERE date=?;
''', (day,)).fetchall()))[0][:-1]
            predicted_symbols = tuple(zip(*db.execute('''
SELECT DISTINCT symbol FROM predicted WHERE date=?;
''', (day,)).fetchall()))[0][:-1]

        true_symbols = set(true_symbols) - {'VIX', 'SP500', '$VIX', '$SPX'}
        predicted_symbols = set(predicted_symbols) - {'VIX', 'SP500', '$VIX', '$SPX'}
        symbols = true_symbols.union(predicted_symbols)

        day_dir = OUTPUT_DIR / day
#        if day_dir.exists():
#            shutil.rmtree(day_dir)
#        day_dir.mkdir()

        for symbol in symbols:
            tasks.append((day, symbol, symbol in true_symbols, symbol in predicted_symbols))

#    num_tasks = len(tasks)

#    global bar
#    bar = progressbar.ProgressBar(maxval=num_tasks)
#    bar.start()
#    futures = list()
    with Pool(NUM_WORKERS) as pool:
        for _ in tqdm.tqdm(pool.imap_unordered(render_ohlcv,
                                               tasks), total=len(tasks)):
            pass
#        pool.map(render_ohlcv, task)

#    with ThreadPoolExecutor(max_workers=NUM_WORKERS) as pool:
#        for task in tasks:
#            futures.append(pool.submit(render_ohlcv, *task))

#    for task in futures:
#        task.result()

#    bar.finish()

def render_ohlcv(task):
    day, symbol, is_true, is_predicted = task
    trading_days = previous_n_trading_days(day, PLOT_LEN)

    with sqlite3.connect(settings.DATA_DIRECTORY / settings.QUANDL_DATABASE_NAME) as db:
        d, o, h, l, c, v = tuple(zip(*db.execute('''
SELECT date, adj_open, adj_high, adj_low, adj_close, adj_volume
FROM qdl_eod
WHERE symbol=? AND date >= ? AND date <= ?
ORDER BY date;''', (symbol, trading_days[0], day)).fetchall()))

    d = tuple(datetime.strptime(di, '%Y-%m-%d') for di in d)
    data = {'Open': o,
            'High': h,
            'Low': l,
            'Close': c,
            'Volume': v}
    df = pd.DataFrame(data, index=d)

    title = f'{symbol}'
    if is_true and is_predicted:
        title += f' - predicted & true'
        prefix = 'f-'
    elif is_true:
        title += f' - true'
        prefix = 't-'
    elif is_predicted:
        title += f' - predicted'
        prefix = 'p-'

#    fig, (ax1, ax2, ax3) = plt.subplots(3, 1, sharex=True)

    day_dir = OUTPUT_DIR / day
    plt.ioff()
    mpf.plot(df, type='candle', mav=[4, 12, 26], volume=True, style='yahoo',
             tight_layout=True,
             title=title,
             savefig={'fname': day_dir / f'{prefix}{symbol}_ohlcv_{day}.png',
                      'dpi': 200,
                      'pad_inches': 0.25,
                      'figsize': (600, 400)}
             )
    day_dir = OUTPUT_DIR / day

    plt.close()

def main():

    start_time = perf_counter()
    run_tasks()

    timer = perf_counter() - start_time
    print('Took {:2}:{:02} to run.'.format(int(timer // 60.0), int(timer % 60.0)))

if __name__ == '__main__':
    main()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# @Time：2020/6/2 13:57
# @Email: am1122@163.com
# @Author: 'Nemo'
import csv
from config import ConfigParser

def format_case(case):
    # {'module': '3. 订单管理（模块）',
    #  'preset': None,
    #  'priority': '3',
    #  'test_results': '添加已锁定用户，新增失败',
    #  'test_steps': '添加已锁定用户，新增失败',
    #  'test_title': '添加已锁定用户，新增失败'}

    module = '/' + case['module'].replace('（', '(').replace('）', ')')  # 将模块中的中文括号替换成英文括号
    row = [
        module,
        case['test_title'],
        case['preset'],
        case['test_steps'],
        case['test_results'],
        '',
        case['priority'],
        case['category'],
        case['stage']
    ]

    return row

def format_all_cases(all_map_cases):
    file_header = ["所属模块", "用例标题", "前置条件", "步骤", "预期", "关键词", "优先级", "用例类型", "适用阶段"]
    rows = [file_header]
    for map in all_map_cases:
        for case in map['test_cases']:
            rows.append(format_case(case))
    return rows

def case2zentao(zentao_file, xmind):

    with open(zentao_file, 'w', encoding=ConfigParser.get_config('encoding', 'sys'), newline='') as f:
        writer = csv.writer(f)
        writer.writerows(format_all_cases(xmind))

if __name__ == '__main__':
    from parsercases import ParserCases
    from pprint import pprint

    p = ParserCases('../test/test_zentao.xmind')
    pprint(p.all_map_case)

    case2zentao('../test/test.csv', p.all_map_case)


from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QMessageBox

def show_message_box():
    msg_box = QMessageBox()
    msg_box.setText("알림")
    msg_box.setInformativeText("이것은 메시지 박스입니다.")
    msg_box.setIcon(QMessageBox.Information)
    msg_box.setStandardButtons(QMessageBox.Ok)
    msg_box.setDefaultButton(QMessageBox.Ok)
    msg_box.exec_()

app = QApplication([])
window = QMainWindow()

button = QPushButton("메시지 박스 열기")
button.clicked.connect(show_message_box)
window.setCentralWidget(button)

window.show()
app.exec_()


from os import system
from pyfiglet import Figlet

system("cls")

titulo = Figlet(font='standard')
titulo = titulo.renderText('Aproximacion')

print(titulo)
objetivo = int(input('Escoge un numero: '))

# variable epsilon para determinar la precision
epsilon = 0.01
# variable paso para determinar el incremento
paso = epsilon**2
respuesta = 0.0

# while para determinar la aproximacion de la raiz cuadrada
while abs(respuesta**2 - objetivo) >= epsilon and respuesta <= objetivo:
    print(abs(respuesta**2 - objetivo), respuesta)
    respuesta += paso

# convierte respuesta**2 - objetivo a un numero entero con abs()
if abs(respuesta**2 - objetivo) >= epsilon:
    print(f'No se encontro la raiz cuadrada de {objetivo}')
else:
    print(f'La raiz cuadrada de {objetivo} es {respuesta}')


from django.core.management.base import BaseCommand
from quoter.models import Tag, Folder, Quote

class Command(BaseCommand):
    help = 'Remove all duplicates in tags (legacy issue)'

    def _detect_duplicates_in(self, tags):
        result = []  # What we will return
        handled = []  # Already handled tags so we don't deal with duplicates
        for tag in tags:
            if tag.name not in handled:
                duplicates = [t for t in tags if t.pk != tag.pk and
                              t.name == tag.name]
                if duplicates:
                    result.append((tag, duplicates))
                    handled.append(tag.name)
        return result

    def _update(self, original, duplicate):
        for q in Quote.objects.filter(tags__id__contains=duplicate.pk):
            # There is an improbable but potential case where two identical tags
            # were put on the same quote - then we just remove the duplicate
            new_tags = [t for t in q.tags.all() if t.pk != duplicate.pk]
            if original not in q.tags.all():
                # More likely situation where we replace the duplicate
                # by the original
                new_tags.append(original)
            q.tags = new_tags
            q.save()

    def handle(self, *args, **options):
        for folder in Folder.objects.all():
            all_tags = Tag.objects.filter(folder_id=folder.pk)
            duplicates = self._detect_duplicates_in(all_tags)
            for original, duplicate in duplicates:
                self.stdout.write('Handling duplicates of %s' % original)
                for dup in duplicate:
                    self._update(original, dup)
                    dup.delete()
                self.stdout.write('Handled duplicates of %s' % original)


from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='home'),
    path('login/', views.login, name='login'),
    path('sign_in/', views.sign_in, name='sign_in'),
    path('campaign/', views.campaign, name='campaign'),
    path('all_requests/', views.all_requests, name='all_requests'),
    path('all_details/', views.all_details, name='all_details'),
    path('new_campaign/', views.new_campaign, name='new_campaign'),
]


import jwt
from rest_framework.authentication import BaseAuthentication
from rest_framework import exceptions
from django.conf import settings

from user.models import User

class SafeJWTAuthentication(BaseAuthentication):
    """
        custom authentication class for DRF and JWT
       https://www.django-rest-framework.org/api-guide/authentication/#custom-authentication
    """

    def authenticate(self, request):
        access_token = request.headers.get('Authorization')

        if not access_token:
            return None
        try:
            payload = jwt.decode(
                access_token, settings.SECRET_KEY, algorithms=['HS256'])

        except jwt.ExpiredSignatureError:
            raise exceptions.AuthenticationFailed('access_token expired')
        except IndexError:
            raise exceptions.AuthenticationFailed('Token prefix missing')

        user = User.objects.filter(id=payload['user_id']).first()

        if user is None:
            raise exceptions.AuthenticationFailed('User not found')
        return user, None


from QPOEstimation.stabilisation import bar_lev
from QPOEstimation.smoothing import two_sided_exponential_smoothing

import numpy as np

import matplotlib.pyplot as plt
# import matplotlib
# matplotlib.use("Qt5Agg")

alpha = 0.04
sampling_frequency = 128
data = np.loadtxt(f"data/sgr1806_{sampling_frequency}Hz.dat")
print(data)
times = data[:, 0]
counts = data[:, 1]
ys = bar_lev(counts)
ys_smoothed = two_sided_exponential_smoothing(ys, alpha=alpha)
ys_residual = ys - ys_smoothed
plt.plot(times, ys)
plt.plot(times, ys_smoothed)
plt.show()

plt.plot(times, ys_residual)
plt.show()

smoothed_data = np.array([times, ys_smoothed]).T
residual_data = np.array([times, ys_residual]).T
np.savetxt(f"data/sgr1806_{sampling_frequency}Hz_exp_smoothed_alpha_{alpha}.dat", smoothed_data)

res_data = np.array([times, ys_residual]).T
np.savetxt(f"data/sgr1806_{sampling_frequency}Hz_exp_residual_alpha_{alpha}.dat", residual_data)

print(times)


import torch

import pytest

from torchsim.core import get_float
from torchsim.core.models.temporal_pooler.tp_output_projection import TPOutputProjection
from torchsim.core.utils.tensor_utils import same

class TestTPOutputProjection:
    @pytest.mark.parametrize('seq_length, seq_lookahead, expected_result', [
        (3, 1, [1, 2, 1]),
        (4, 0, [1, 2, 3, 4]),
        (4, 1, [1, 2, 3, 2]),
        (4, 2, [2, 3, 2, 1]),
        (4, 3, [4, 3, 2, 1]),
        (5, 0, [1, 2, 3, 4, 5]),
        (5, 1, [1, 2, 3, 4, 3]),
        (5, 2, [1, 2, 3, 2, 1]),
        (5, 3, [3, 4, 3, 2, 1]),
        (5, 4, [5, 4, 3, 2, 1])
    ])
    def test_generate_prob_scaling(self, seq_length, seq_lookahead, expected_result):
        scaling = TPOutputProjection._generate_prob_scaling(seq_length, seq_lookahead)
        assert expected_result == scaling

    @pytest.mark.parametrize('device', ['cpu'])
    @pytest.mark.parametrize('seqs, likelihoods, expected_output_projection', [
        (
                [
                    [[0, 1, 2],
                     [0, 1, 3]],
                    [[0, 1, 2],
                     [1, 3, 2]],
                ],
                [[1.0, 0.0], [0.5, 0.5]],
                [[0.25, 0.5, 0.25, 0.0], [0.125, 0.375, 0.250, 0.250]],
        ),
        (
                [
                    [[0, 1, 0]],
                    [[0, 1, 2]],
                    [[0, 1, 3]],
                    [[0, 2, 0]],
                    [[0, 2, 1]],
                ], [[1.], [1.], [1.], [1.], [1.]],
                [
                    [0.5, 0.5, 0, 0],
                    [0.25, 0.5, 0.25, 0.0],
                    [0.25, 0.5, 0.0, 0.25],
                    [0.5, 0, 0.5, 0],
                    [0.25, 0.25, 0.5, 0.0],
                ]
        )
    ])
    def test_compute_output_projection(self, device, seqs, likelihoods, expected_output_projection):
        seqs_tensor = torch.tensor(seqs, device=device, dtype=torch.int64)
        likelihoods_tensor = torch.tensor(likelihoods, device=device, dtype=get_float(device))
        expected_output_projection_tensor = torch.tensor(expected_output_projection, device=device,
                                                         dtype=get_float(device))
        flock_size, n_frequent_seqs, seq_length = seqs_tensor.shape
        n_cluster_centers = 4
        seq_lookahead = 1

        output_projection = TPOutputProjection(flock_size, n_frequent_seqs, n_cluster_centers, seq_length,
                                               seq_lookahead, device)

        projection_outputs = torch.zeros((flock_size, n_cluster_centers), device=device, dtype=get_float(device))

        output_projection.compute_output_projection(
            seqs_tensor,
            likelihoods_tensor,
            projection_outputs
        )

        assert same(expected_output_projection_tensor, projection_outputs)

    @pytest.mark.parametrize('device', ['cpu'])
    @pytest.mark.parametrize('seqs, expected_output_projection', [
        (
                [[
                    [0, 1, 0],
                    [0, 1, 2],
                    [0, 1, 3],
                    [0, 2, 0],
                    [0, 2, 1],
                ]],
                [[
                    [0.5, 0.5, 0, 0],
                    [0.25, 0.5, 0.25, 0.0],
                    [0.25, 0.5, 0.0, 0.25],
                    [0.5, 0, 0.5, 0],
                    [0.25, 0.25, 0.5, 0.0],
                ]]
        )
    ])
    def test_compute_output_projection_per_sequence(self, device, seqs, expected_output_projection):
        seqs_tensor = torch.tensor(seqs, device=device, dtype=torch.int64)
        expected_output_projection_tensor = torch.tensor(expected_output_projection, device=device,
                                                         dtype=get_float(device))
        flock_size, n_frequent_seqs, seq_length = seqs_tensor.shape
        n_cluster_centers = 4
        seq_lookahead = 1

        output_projection = TPOutputProjection(flock_size, n_frequent_seqs, n_cluster_centers, seq_length,
                                               seq_lookahead, device)

        projection_outputs = torch.zeros((flock_size, n_frequent_seqs, n_cluster_centers), device=device,
                                         dtype=get_float(device))

        output_projection.compute_output_projection_per_sequence(
            seqs_tensor,
            projection_outputs
        )

        assert same(expected_output_projection_tensor, projection_outputs)

    @pytest.mark.parametrize('device', ['cpu'])
    @pytest.mark.parametrize('item, sequences, expected_result', [
        ([[0.25, 0.5, 0.25, 0.0]],
         [[
             [0.5, 0.5, 0, 0],
             [0.25, 0.5, 0.25, 0.0],
             [0.25, 0.5, 0.0, 0.25],
             [0.5, 0, 0.5, 0],
             [0.25, 0.25, 0.5, 0.0]
         ]],
         [[1 - 0.125, 1, 1 - 0.125, 1 - 0.25, 1 - 0.125]]
         ),
        ([
             [0.0],
             [1.0]
         ],
         [
             [
                 [0.1],
                 [0.2],
                 [0.3],
             ],
             [
                 [0.4],
                 [0.5],
                 [0.6],
             ],
         ],
         [
             [0.9, 0.8, 0.7],
             [0.4, 0.5, 0.6],
         ]
        )
    ])
    def test_compute_similarity(self, device, item, sequences, expected_result):
        def t(data):
            return torch.tensor(data, device=device, dtype=get_float(device))

        result = TPOutputProjection.compute_similarity(t(item), t(sequences))
        assert same(t(expected_result), result, eps=1e-3)


from turtle import update
from telegram.ext.updater import Updater
from telegram.update import Update
from telegram.ext.callbackcontext import CallbackContext
from telegram.ext.commandhandler import CommandHandler
from telegram.ext.messagehandler import MessageHandler
from telegram.ext.filters import Filters
import os

TELE_TOKEN = "5264455127:AAHjR7B5ETqcC27iIsUhHMeYBQnffS-xogQ"
BOT_USER_NAME = "ChipTradingBot"
HEROKU_URL = "https://chip-trading-bot.herokuapp.com/"
PORT = int(os.environ.get('PORT','8443'))

watchlist = []

def start(update: Update, context: CallbackContext):
    update.message.reply_text("Hii, How are you doing? We are here to help you keep updated on your favourite stocks")
    update.message.reply_text("ðŸ¥³")
    update.message.reply_text("/help for list of commands")

def help(update: Update, context: CallbackContext):
	update.message.reply_text("""Available Commands :-
    /add_stock - Add a stock to your watchlist
    /motivate_me - get motivational quotes
    /how_do_i_look - get comments about yourself
    """)

"""
LIST OF COMMANDS
1. Add to watchlist
2. SET LIMIT ORDER UPDATE
3. Alerts to TRACK ORDER (every minute)
4. Change alert timings
"""

def add_stock():
    update.message.reply_text("What stock do you want to add to your watchlist?")
    watchlist.append(update.message.text)
    update.message.reply_text("Stock added to watchlist")
    update.message.reply_text("/watchlist to see your watchlist")

def watchlist():
    update.message.reply_text("Your watchlist is :-")
    update.message.reply_text(watchlist)

def userText(update: Update, context: CallbackContext):
    update.message.reply_text("What {}? Do you work bitch.".format(update.message.text))

def motivate_me(update: Update, context: CallbackContext):
	update.message.reply_text("Do it, or I will shove it up on your ass")

def how_do_look(update: Update, context: CallbackContext):
	update.message.reply_text("Ugly piece of shit, you will never get a girlfriend")

def unknown_text(update: Update, context: CallbackContext):
	update.message.reply_text("Sorry I can't recognize you , you said '%s'" % update.message.text)

def unknown(update: Update, context: CallbackContext):
	update.message.reply_text("Sorry '%s' is not a valid command" % update.message.text)

#updater.dispatcher.add_handler(MessageHandler(
	# Filters out unknown commands
#	Filters.command, unknown))

# Filters out unknown messages.

def main():
    updater = Updater(TELE_TOKEN, use_context=True)
    #getting the dispatchers to register handlers
    dp = updater.dispatcher

    #registering commands
    updater.dispatcher.add_handler(CommandHandler('start', start))
    updater.dispatcher.add_handler(CommandHandler('motivate_me', motivate_me))
    updater.dispatcher.add_handler(CommandHandler('help', help))
    updater.dispatcher.add_handler(CommandHandler('how_do_i_look', how_do_look))
    updater.dispatcher.add_handler(MessageHandler(Filters.text, userText))
    #registering Message Handler to reply to user messages
    #dp.add_handler(MessageHandler(Filters.text & ~Filters.command,userText))
    updater.dispatcher.add_handler(MessageHandler(Filters.text, userText))

    #starting the bot
    updater.start_webhook(listen = "0.0.0.0", port = int(PORT), url_path = TELE_TOKEN,
                        webhook_url = HEROKU_URL+TELE_TOKEN)
    updater.idle()

if __name__ == '__main__':
    main()


from ase.utils import StringIO
from ase.io import read

# Made from NWChem interface

def read_geom_orcainp(filename):
    """Method to read geometry from an ORCA input file."""
    f = filename
    if isinstance(filename, str):
        f = open(filename)
    lines = f.readlines()
    if type(filename) == str:
        f.close()

    # Find geometry region of input file.
    stopline = 0
    for index, line in enumerate(lines):
        if line[1:].startswith('xyz '):
            startline = index + 1
            stopline = -1
        elif (line.startswith('end') and stopline == -1):
            stopline = index
        elif (line.startswith('*') and stopline == -1):
            stopline = index
    # Format and send to read_xyz.
    xyz_text = '%i\n' % (stopline - startline)
    xyz_text += ' geometry\n'
    for line in lines[startline:stopline]:
        xyz_text += line
    atoms = read(StringIO(xyz_text), format='xyz')
    atoms.set_cell((0., 0., 0.))  # no unit cell defined

    return atoms

def write_orca(atoms, **params):
    """Function to write ORCA input file
    """
    charge = params['charge']
    mult = params['mult']
    label = params['label']

    if 'pcpot' in params.keys():
        pcpot = params['pcpot']
        pcstring = '% pointcharges \"' +\
                   label + '.pc\"\n\n'
        params['orcablocks'] += pcstring
        pcpot.write_mmcharges(label)

    with open(label + '.inp', 'w') as f:
        f.write("! engrad %s \n" % params['orcasimpleinput'])
        f.write("%s \n" % params['orcablocks'])

        f.write('*xyz')
        f.write(" %d" % charge)
        f.write(" %d \n" % mult)
        for atom in atoms:
            if atom.tag == 71:  # 71 is ascii G (Ghost)
                symbol = atom.symbol + ' : '
            else:
                symbol = atom.symbol + '   '
            f.write(symbol +
                    str(atom.position[0]) + ' ' +
                    str(atom.position[1]) + ' ' +
                    str(atom.position[2]) + '\n')
        f.write('*\n')


"""Migration

Revision ID: 8059985beed1
Revises: d86b14989681
Create Date: 2020-12-07 18:07:55.435065

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '8059985beed1'
down_revision = 'd86b14989681'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('comments', 'author')
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('comments', sa.Column('author', sa.VARCHAR(length=255), autoincrement=False, nullable=True))
    # ### end Alembic commands ###


import csv
from datetime import datetime

# 打开CSV文件并读取数据
with open('vrsn.csv', 'r') as file:
    reader = csv.reader(file)
    data = list(reader)

# 按照日期升序排序数据
data_sorted = sorted(data, key=lambda x: datetime.strptime(x[0], '%m/%d/%Y'))

# 迭代每一行数据并转换日期格式
for row in data_sorted:
    # 使用strptime函数解析原始日期字符串
    date = datetime.strptime(row[0], '%m/%d/%Y')
    # 使用strftime函数将日期格式化为新的格式
    row[0] = date.strftime('%Y-%m-%d')

# 将转换后的数据写回到CSV文件中
with open('vrsn_sorted_and_converted.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerows(data_sorted)


from math import floor

def calcuate_fuel(mass):
    # divided = mass / 3
    # floored = floor(divided)
    # subtracted = floored - 2
    # return subtracted
    print("Calculating fuel to carry {}".format(mass))

    return floor(mass / 3) - 2
    return mass // 3 - 2

def total_fuel(masses):
    totals = []
    for mass in masses:
        total = 0
        total += calcuate_fuel(mass)
        print("Now has a total of {}".format(total))

        print("Calculating fuel to carry {}".format(total))
        fuel_to_carry_fuel = calcuate_fuel(total)
        while fuel_to_carry_fuel > 0:
            print("Adding {} to the total.".format(fuel_to_carry_fuel))
            total += fuel_to_carry_fuel
            fuel_to_carry_fuel = calcuate_fuel(fuel_to_carry_fuel)

        totals.append(total)
    return sum(totals)

if __name__ == "__main__":

    import requests

    result = requests.get("https://adventofcode.com/2019/day/1/input", headers={"Cookie": "session=53616c7465645f5fa83960b1e99f2bfb3666e735109ba7aaa30e6e76b8f4fe458e74e9745c89fb3d8bc76b152ed1d446"})

    if result.status_code == 200:
        text = result.text
        split_lines = text.splitlines()

        # converted = []
        # for num in split_lines:
        #     converted.append(int(num))

        converted = list(map(int, split_lines))

        print(total_fuel(converted))

    else:
        print("Error.", result.status_code)


import torch

from .base import NeuralNetwork
from .layers.linear import BayesianLinear, FeedbackAlignLinear

class BayesianMultilayerPerceptron(NeuralNetwork):
    """
    Bayesian Neural Network with various feedback mechanisms
    """
    def __init__(self, dimensions, nonlinearity, stdv_init, radial):
        super().__init__(dimensions[0], dimensions[-1])
        self.layers = torch.nn.ModuleList(
            BayesianLinear(dim1, dim2, stdv_init, radial)
            for dim1, dim2 in zip(dimensions[:-1], dimensions[1:])
        )

        self.dimensions = dimensions
        self.nonlinearity = nonlinearity

    def forward(self, x):
        """
        Defines the computation performed at every call.

        Returns:
            y_pred: Unnormalized output (i.e. no logits or probabilities)
            state: dict containing hidden activations
        """
        x = x.view((x.size(0), self.input_dim))
        activation = []

        for l in self.layers[:-1]:
            x = l(x)
            x = self.nonlinearity(x)
            activation.append(x)

        # The output is unnormalized (i.e. no logits or probabilities)
        y_pred = self.layers[-1](x)

        return y_pred, {"activation": activation}

    def sample(self):
        for l in self.layers:
            l.sample()

    def reset_parameters(self):
        for l in self.layers:
            l.reset_parameters()

class MultilayerPerceptron(NeuralNetwork):
    """
    Multilayer Perceptron with optional feedback alignment.

    Args:
        dimensions: List of integers defining the network architecture
        nonlinearity: Function used as nonlinearity between layers
        feedback_alignment: Boolean specifying whether to use feedback alignment
    """
    def __init__(self, dimensions, nonlinearity, feedback_alignment=False):
        super().__init__(dimensions[0], dimensions[-1])

        if feedback_alignment:
            self.layers = torch.nn.ModuleList(
                FeedbackAlignLinear(dim1, dim2, bias=True)
                for dim1, dim2 in zip(dimensions[:-1], dimensions[1:])
            )
        else:
            self.layers = torch.nn.ModuleList(
                torch.nn.Linear(dim1, dim2, bias=True)
                for dim1, dim2 in zip(dimensions[:-1], dimensions[1:])
            )
        self.dimensions = dimensions
        self.nonlinearity = nonlinearity

    def forward(self, x):
        """
        Defines the computation performed at every call.

        Returns:
            y_pred: Unnormalized output (i.e. no logits or probabilities)
        """
        x = x.view((x.size(0), self.input_dim))
        activation = []

        for l in self.layers[:-1]:
            x = l(x)
            x = self.nonlinearity(x)
            activation.append(x)

        # The output is unnormalized (i.e. no logits or probabilities)
        y_pred = self.layers[-1](x)

        return y_pred, {"activation": activation}

    def reset_parameters(self):
        for l in self.layers:
            l.reset_parameters()


import requests
from helpers import GenericImages as GI
from helpers import cache
import concurrent.futures

#------------------------------------------------------------------------------------------------------

def getMatchIDs(api, shard, puuid):
    url = f"https://{shard}.api.riotgames.com/lol/match/v5/matches/by-puuid/{puuid}/ids"
    response = requests.get(url, params={'api_key':api, 'count':20})
    resp = response.json()
    return resp

#------------------------------------------------------------------------------------------------------

def getAllMatchDetails(api, shard, matchID):
    with concurrent.futures.ThreadPoolExecutor() as executor:
        result = executor.map(getMatchDetails, [api]*len(matchID), [shard]*len(matchID), matchID)
    return list(result)

def getMatchDetails(api, shard, id):
    url = f"https://{shard}.api.riotgames.com/lol/match/v5/matches/{id}"
    response = requests.get(url, params={'api_key':api})
    resp = response.json()
    return(resp)

#------------------------------------------------------------------------------------------------------

def getMatchInfo(data):
    match_info_list = []

    for key in data:
        match_info = {}
        match_info['QueueID'] = key['info']['queueId']
        match_info['QueueType'] = 'Other'

        if match_info['QueueID'] == 420:
            match_info['QueueType'] = 'Ranked Solo'
        elif match_info['QueueID'] == 440:
            match_info['QueueType'] = 'Ranked Flex'
        elif match_info['QueueID'] == 430:
            match_info['QueueType'] = 'Normal Blind'
        elif match_info['QueueID'] == 400:
            match_info['QueueType'] = 'Normal Draft'
        elif match_info['QueueID'] == 450:
            match_info['QueueType'] = 'ARAM'
        elif match_info['QueueID'] == 830:
            match_info['QueueType'] = 'Co-op [Intro]'
        elif match_info['QueueID'] == 840:
            match_info['QueueType'] = 'Co-op [Beginner]'
        elif match_info['QueueID'] == 850:
            match_info['QueueType'] = 'Co-op [Intermediate]'

        match_info['GameDuration'] = key['info']['gameDuration']
        match_info['GameTime'] = f"{int(key['info']['gameDuration']/60)}m {key['info']['gameDuration']%60}s"
        match_info_list.append(match_info)
        match_info = {}

    return match_info_list

def getMatchPlayersDetails(version, data):
    match_info = getMatchInfo(data)
    match_players_info = []

    for i in range(len(data)):
        players_info = []
        player_data = {}

        for player in data[i]['info']['participants']:
            # General
            player_data['PlayerName'] = player['summonerName']
            player_data['PlayerLevel'] = player['champLevel']
            player_data['Champion'] = player['championName']
            player_data['PlayerTeam'] = 'Blue' if player['teamId'] == 100 else 'Red'
            player_data['Win'] = player['win'] if match_info[i]['GameDuration'] > 240 else 'Remake'
            # KDA
            player_data['Kills'] = player['kills']
            player_data['Deaths'] = player['deaths']
            player_data['Assists'] = player['assists']
            player_data['KDA'] = "Perfect" if player_data['Deaths'] == 0 else round(((player['kills'] + player['assists']) / player_data['Deaths']), 2)
            # CS
            player_data['LaneMinionsKilled'] = player['totalMinionsKilled']
            player_data['NeutralMinionsKilled'] = player['neutralMinionsKilled']
            player_data['TotalMinionsKilled'] = player_data['NeutralMinionsKilled'] + player_data['LaneMinionsKilled']
            player_data['CSPerMinute'] = round(player_data['TotalMinionsKilled'] / ((match_info[i]['GameDuration'])/60), 2)
            # Vision
            player_data['VisionScore'] = player['visionScore']
            player_data['ControlWards'] = player['visionWardsBoughtInGame']
            player_data['WardsPlaced'] = player['wardsPlaced']
            player_data['WardsDestroyed'] = player['wardsKilled']
            # Damage
            player_data['DamageToChampions'] = player['totalDamageDealtToChampions']
            player_data['DamageTaken'] = player['totalDamageTaken']
            # Images
            player_data['ChampionImage'] = GI.getChampImage(version, player['championName'])
            player_data['Keystone'] = GI.getRuneImage(version, player["perks"]["styles"][0]["selections"][0]["perk"])
            player_data['SecondaryRune'] = GI.getRuneImage(version, player["perks"]["styles"][1]['style'])
            player_data['Item0'] = GI.getItemImage(version, player['item0'])
            player_data['Item1'] = GI.getItemImage(version, player['item1'])
            player_data['Item2'] = GI.getItemImage(version, player['item2'])
            player_data['Item3'] = GI.getItemImage(version, player['item3'])
            player_data['Item4'] = GI.getItemImage(version, player['item4'])
            player_data['Item5'] = GI.getItemImage(version, player['item5'])
            player_data['Item6'] = GI.getItemImage(version, player['item6'])
            player_data['Summoner1'] = GI.getSummonerSpellImage(version, player['summoner1Id'])
            player_data['Summoner2'] = GI.getSummonerSpellImage(version, player['summoner2Id'])

            players_info.append(player_data)
            player_data = {}

        match_players_info.append(players_info)

    return match_players_info

#------------------------------------------------------------------------------------------------------


from typing import Dict

import pytest
from pytest_mock import MockerFixture

from langchain_community.llms import GradientLLM

_MODEL_ID = "my_model_valid_id"
_GRADIENT_SECRET = "secret_valid_token_123456"
_GRADIENT_WORKSPACE_ID = "valid_workspace_12345"
_GRADIENT_BASE_URL = "https://api.gradient.ai/api"

class MockResponse:
    def __init__(self, json_data: Dict, status_code: int):
        self.json_data = json_data
        self.status_code = status_code

    def json(self) -> Dict:
        return self.json_data

def mocked_requests_post(url: str, headers: dict, json: dict) -> MockResponse:
    assert url.startswith(_GRADIENT_BASE_URL)
    assert _MODEL_ID in url
    assert json
    assert headers

    assert headers.get("authorization") == f"Bearer {_GRADIENT_SECRET}"
    assert headers.get("x-gradient-workspace-id") == f"{_GRADIENT_WORKSPACE_ID}"
    query = json.get("query")
    assert query and isinstance(query, str)
    output = "bar" if "foo" in query else "baz"

    return MockResponse(
        json_data={"generatedOutput": output},
        status_code=200,
    )

@pytest.mark.parametrize(
    "setup",
    [
        dict(
            gradient_api_url=_GRADIENT_BASE_URL,
            gradient_access_token=_GRADIENT_SECRET,
            gradient_workspace_id=_GRADIENT_WORKSPACE_ID,
            model=_MODEL_ID,
        ),
        dict(
            gradient_api_url=_GRADIENT_BASE_URL,
            gradient_access_token=_GRADIENT_SECRET,
            gradient_workspace_id=_GRADIENT_WORKSPACE_ID,
            model_id=_MODEL_ID,
        ),
    ],
)
def test_gradient_llm_sync(mocker: MockerFixture, setup: dict) -> None:
    mocker.patch("requests.post", side_effect=mocked_requests_post)

    llm = GradientLLM(**setup)
    assert llm.gradient_access_token == _GRADIENT_SECRET
    assert llm.gradient_api_url == _GRADIENT_BASE_URL
    assert llm.gradient_workspace_id == _GRADIENT_WORKSPACE_ID
    assert llm.model_id == _MODEL_ID

    response = llm("Say foo:")
    want = "bar"

    assert response == want

@pytest.mark.parametrize(
    "setup",
    [
        dict(
            gradient_api_url=_GRADIENT_BASE_URL,
            gradient_access_token=_GRADIENT_SECRET,
            gradient_workspace_id=_GRADIENT_WORKSPACE_ID,
            model=_MODEL_ID,
        )
    ],
)
def test_gradient_llm_sync_batch(mocker: MockerFixture, setup: dict) -> None:
    mocker.patch("requests.post", side_effect=mocked_requests_post)

    llm = GradientLLM(**setup)
    assert llm.gradient_access_token == _GRADIENT_SECRET
    assert llm.gradient_api_url == _GRADIENT_BASE_URL
    assert llm.gradient_workspace_id == _GRADIENT_WORKSPACE_ID
    assert llm.model_id == _MODEL_ID

    inputs = ["Say foo:", "Say baz:", "Say foo again"]
    response = llm._generate(inputs)

    want = ["bar", "baz", "bar"]
    assert len(response.generations) == len(inputs)
    for i, gen in enumerate(response.generations):
        assert gen[0].text == want[i]


## rbmgr [genaf]
##
## genaf rbmgr script will override both rhombus' rbmgr
## this script will be called by run.py
## all parameters has been set by run.py

import sys, transaction

from rhombus.lib.utils import cout, cerr, cexit
from rhombus.scripts.rbmgr import ( init_argparser as rhombus_init_argparser,
                                    do_rbmgr as rhombus_do_rbmgr,
                                    main as rhombus_main,
                                    get_dbhandler)
from rhombus.scripts import setup_settings
#from fatools.scripts.dbmgr import ( init_argparser as fatools_init_argparser,
#                                    do_dbmgr as fatools_do_dbmgr )

def init_argparser( parser = None ):

    if parser is None:
        import argparse
        p = argparse.ArgumentParser('[genaf] - rbmgr', conflict_handler='resolve')
    else:
        p = parser

    # update our argparser
    p = rhombus_init_argparser( p )

    # provide our entry here
    return p

def main(args):

    cerr('[genaf] rbmgr main()')

    rhombus_main( args )

    return

    settings = setup_settings( args )

    if any( (args.exportuserclass, args.exportgroup, args.exportenumkey) ):
        do_dbmgr( args, settings )

    elif not args.rollback and not args.test and (args.commit or args.initdb):
        with transaction.manager:
            do_dbmgr( args, settings )
            cerr('** COMMIT database **')

    else:
        cerr('** WARNING -- running without database COMMIT **')
        if not args.rollback and not args.test:
            keys = input('Do you want to continue [y/n]: ')
            if keys.lower()[0] != 'y':
                sys.exit(1)
        do_dbmgr( args, settings )

def do_dbmgr(args, settings, dbh=None):

    if dbh is None:
        dbh = get_dbhandler(settings, initial = args.initdb)
    print(dbh)

    if not rhombus_do_dbmgr(args, settings, dbh):
        cerr('ERR - command unrecognized!')


from fastai.vision import unet_learner, models, Learner

from fast_radiology.losses import generalized_dice_loss
from fast_radiology.models.unet import ClassicUNet
from artificial_contrast.const import (
    FREQS_LIMIT_WINDOWS,
    FREQS_NO_LIMIT_WINDOWS,
    SIMPLE_MULTIPLE_WINDOWS,
    SIMPLE_WINDOW_1CHANNEL_CLASSIC_UNET,
    SIMPLE_WINDOW_3CHANNEL_CLASSIC_UNET,
    SIMPLE_WINDOW_SMALL,
)

def get_learner(
    data, metrics=None, model_save_path='models', loss_func=None, pretrained=True, experiment_name=None
):
    if metrics is None:
        metrics = []
    if loss_func is None:
        loss_func = generalized_dice_loss

    if experiment_name is None or experiment_name in [
        FREQS_LIMIT_WINDOWS, FREQS_NO_LIMIT_WINDOWS, SIMPLE_MULTIPLE_WINDOWS, SIMPLE_WINDOW_SMALL
    ]:
        return unet_learner(
            data,
            models.resnet18,
            metrics=metrics,
            self_attention=True,
            loss_func=loss_func,
            path=model_save_path,
            pretrained=pretrained,
        )
    elif experiment_name in [
        SIMPLE_WINDOW_1CHANNEL_CLASSIC_UNET, SIMPLE_WINDOW_3CHANNEL_CLASSIC_UNET
    ]:
        if experiment_name == SIMPLE_WINDOW_1CHANNEL_CLASSIC_UNET:
            n_channels = 1
        elif experiment_name == SIMPLE_WINDOW_3CHANNEL_CLASSIC_UNET:
            n_channels = 3

        return Learner(
            data,
            ClassicUNet(n_channels=n_channels, n_classes=2),
            metrics=metrics,
            loss_func=loss_func,
            path=model_save_path,
        )
    else:
        raise RuntimeError('Incorrect experiment_name')


import os
import pickle

import numpy as np
import pandas as pd
from flask import Flask, jsonify, request

from patient_count import con_rec_dec

app = Flask(__name__)

current_path = os.getcwd()
pickle_path = os.path.join(current_path, "assets", "covid.pkl")
print(pickle_path)
classifier = pickle.load(open(pickle_path, "rb"))
count_data_path = os.path.join(current_path, "assets", "districts.csv")
counts = pd.read_csv(count_data_path)

@app.route("/api/covid_test", methods=["POST"])
def predict_risk():
    data = request.get_json()
    print(data)
    sex = data["sex"]
    pneumonia = data["pneumonia"]
    age = data["age"]
    pregnancy = data["pregnancy"]
    diabetes = data["diabetes"]
    copd = data["copd"]
    asthma = data["asthma"]
    hypertension = data["hypertension"]
    other_disease = data["other_disease"]
    cardiovascular = data["cardiovascular"]
    obesity = data["obesity"]
    renal_chronic = data["renal_chronic"]
    tobacco = data["tobacco"]
    contact_other_covid = data["contact_other_covid"]

    cols = [
        "sex",
        "pneumonia",
        "age",
        "pregnancy",
        "diabetes",
        "copd",
        "asthma",
        "hypertension",
        "other_disease",
        "cardiovascular",
        "obesity",
        "renal_chronic",
        "tobacco",
        "contact_other_covid",
    ]

    test = pd.DataFrame(
        [
            [
                sex,
                pneumonia,
                age,
                pregnancy,
                diabetes,
                copd,
                asthma,
                hypertension,
                other_disease,
                cardiovascular,
                obesity,
                renal_chronic,
                tobacco,
                contact_other_covid,
            ]
        ],
        columns=cols,
    )

    pred = classifier.predict_proba(test)
    result = pred[0][1]

    if result < 0.25:
        return jsonify("Low Risk")
    elif (result >= 0.25) and (result < 0.5):
        return jsonify("Medium Risk")

    elif (result >= 0.5) and (result < 0.75):
        return jsonify("High Risk")

    else:
        return jsonify("Vulnerable Risk")

@app.route("/api/patient_count", methods=["POST"])
def patient_count_dist():
    data = request.get_json()
    # print(data)
    date = str(data["date"])
    district = str(data["district"])
    try:
        confirmed, recovered, deceased = con_rec_dec(counts, date, district)
        patient_details = f"Confirmed_cases= {confirmed}, Recovered_cases={recovered}, Deceased_cases= {deceased}"
        return jsonify(patient_details)
    except:
        return jsonify("Date or District input Error")

if __name__ == "__main__":
    app.run(debug=True)


#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
@author: Florian Timm
@version: 2017.11.27
"""

import sqlite3
from vdFile import VdFile
from vdPoint import VdPoint

class VdSQLite(VdFile):

    """ class for writing data to sqlite database """

    def _open(self, filename):
        """
        opens a new db file
        :param filename: name of db file
        :type filename: str
        """
        filename = self._make_filename("db", filename)
        print(filename)
        self.__db = sqlite3.connect(filename)
        self.__cursor = self.__db.cursor()
        self.__cursor.execute("CREATE TABLE IF NOT EXISTS raw_data ("
                              "id INTEGER PRIMARY KEY AUTOINCREMENT,"
                              "time FLOAT,"
                              "azimuth FLOAT,"
                              "vertical FLOAT,"
                              "distance FLOAT,"
                              "reflection INTEGER)")
        self.__db.commit()

    def write(self):
        """ writes data to database """
        insert = []
        for p in self.writing_queue:
            insert.append((p.time, p.azimuth, p.vertical,
                           p.distance, p.reflection))

        self.__cursor.executemany("INSERT INTO raw_data ("
                                  "time, azimuth, vertical, "
                                  "distance, reflection) "
                                  "VALUES (?, ?, ?, ?, ?)", insert)
        self.__db.commit()
        self.clear_writing_queue()

    def close(self):
        """ close database """
        self.__db.close()


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

#Install Libraries
from bs4 import BeautifulSoup
import urllib2
import re
import csv
import itertools

#Add path to the file test.csv
writer=csv.writer(open('Path to crawled speaker data.csv','wb'))
x=[]
html = urllib2.urlopen("https://speakers.acm.org/")
soup = BeautifulSoup(html)
for h in soup.findAll("div", {"id": "topics-list"}):
  for a in h.findAll("a", {"href": True}):
    x.append("https://speakers.acm.org"+a["href"])

#https://speakers.acm.org/topics/applied-computing type page
for i in range(len(x)):
    html = urllib2.urlopen(x[i])
    soup = BeautifulSoup(html)
    for link in soup.findAll('a', attrs={'href': re.compile("\.\./speakers/")}):
        a=link.get('href')
        a=a.replace("..", "http://speakers.acm.org")
        a=(re.sub(r"\s+", "", a))
        a=a.encode('utf-8')
        a=a.replace("ö", "o")

        #Speaker page
        try:
            html = urllib2.urlopen(a)
        except urllib2.HTTPError as e:
            if e.getcode() == 404:
                continue
            raise

        #Using beautiful soup to crawl data
        soup = BeautifulSoup(html)
        for h in soup.findAll("li", {"class": "current"}):
            speaker_name=h.text
            speaker_name=speaker_name.encode('utf-8')
            writer.writerow([speaker_name])

        for h in soup.findAll("div", {"class": "speakers"}):
            for a in h.findAll("span"):
                b=a.text
                speaker_loc=re.sub(r"\s*Based in\s*", "", str(b))
                speaker_loc=re.sub(r"\s*$", "", str(speaker_loc))
                writer.writerow([speaker_loc])
                break


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/4/29 23:20
# @Author : Mr. Zhou Chuang
# @Site :
# @File : 接雨水.py
# @Software: PyCharm
from typing import List

class Solution:
    def trap(self, height: List[int]) -> int:
        # 1. 暴力法
        # 能接多少雨水与左右两侧最近的比它高的有关
        # if not height:
        #     return 0
        # n = len(height)
        # max_left = [0] * n
        # max_right = [0] * n
        # max_left[0] = height[0]
        # max_right[-1] = height[-1]
        #
        # for i in range(1, n):
        #     max_left[i] = max(height[i], max_left[i-1])  # max_left处的值要么为本身，要么为前一个max_left
        # for i in range(n - 1 - 1, -1, -1):
        #     max_right[i] = max(height[i], max_right[i+1])
        # res = 0
        # for i in range(n):
        #     res += min(max_left[i], max_right[i])-height[i]
        #
        # return res

        # 2. 双指针
        # if not height:
        #     return 0
        # left = 0
        # right = len(height) - 1
        # res = 0
        # # 记录左右两边最大值
        # left_max = height[left]
        # right_max = height[right]
        # while left < right:
        #     if height[left] < height[right]:
        #         if left_max > height[left]:
        #             res += left_max - height[left]
        #         else:
        #             left_max = height[left]  # 更新 left_max
        #         left += 1
        #     else:
        #         if right_max > height[right]:
        #             res += right_max - height[right]
        #         else:
        #             right_max = height[right]  # 更新right_max
        #         right -= 1
        # return res

        length = len(height)
        if length < 3: return 0
        res, idx = 0, 0
        stack = []
        while idx < length:
            while len(stack) > 0 and height[idx] > height[stack[-1]]:
                top = stack.pop()  # index of the last element in the stack
                if len(stack) == 0:
                    break
                h = min(height[stack[-1]], height[idx]) - height[top]
                dist = idx - stack[-1] - 1
                res += (dist * h)
            stack.append(idx)
            idx += 1
        return res

if __name__ == '__main__':
    Sol = Solution()
    ans = Sol.trap([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1])
    print(ans)

算法学习/栈、队列/接雨水.py


import json
import os

from PyQt5.QtWidgets import QMainWindow, QFileDialog, QMessageBox

from ComparatorUi import Ui_Comparator
from utils.Config import Config
from utils.comparator import compare

class CompareTool(QMainWindow, Ui_Comparator):

    def __init__(self, parent=None):
        super(CompareTool, self).__init__(parent=parent)
        self.setupUi(self)

        self.textEdit.textChanged.connect(self.compare)
        self.textEdit_2.textChanged.connect(self.compare)
        self.pushButton_2.clicked.connect(self.openDest)

    def openDest(self):
        try:
            config = Config.readConfig()
            lastOpenDir = config['lastOpenDir']
        except:
            lastOpenDir = './'
        file, type = QFileDialog.getOpenFileName(self, 'Open a JSon file', lastOpenDir)
        if file:
            dir = os.path.dirname(file)
            Config.writeConfig({'lastOpenDir': dir})
            try:
                f = open(file, 'r', encoding='utf-8')
                text = f.read()
                data = json.load(f)
                self.textEdit_2.setPlainText(text)
                f.close()
            except:
                QMessageBox.warning(self, 'Error', 'Not JSon')

    def setSource(self, text):
        self.textEdit.setPlainText(text)

    def compare(self):
        text1 = self.textEdit.toPlainText()
        text2 = self.textEdit_2.toPlainText()
        if not text1 or not text2:
            return

        data1 = json.loads(text1, encoding='utf-8')
        data2 = json.loads(text2, encoding='utf-8')
        isSame, err = compare(data1, data2)
        if isSame:
            self.textBrowser.setVisible(False)
            self.statusBar.showMessage('Same JSon')
        else:
            promote = 'Note：\n\tThis comparator only compares the keys and the type of values, ignors all the real value\n\n'
            self.textBrowser.setText(promote + err)
            self.textBrowser.setVisible(True)
            self.statusBar.showMessage('Not same JSon')