from __future__ import absolute_import, division, print_function

import matplotlib.pyplot as plt

from sklearn.metrics import mean_squared_error

import pickle
import os
import numpy as np

# File for plotting figures in Figure 3
# Only run this file after generating all the experimental results

dataset_name_list = ["yacht","ENB_heating","ENB_cooling","airfoil_self_noise","concrete","winequality-red","winequality-white","CCPP","CASP","SuperConduct","slice_localization","MSD"]
title_name_list = ["yacht","ENB/h","ENB/c","airfoil","CCS","wine/r","wine/w","CCPP","protein","SC","CT","MSD"]
minibatch_size_list = [246,614,614,1202,824,1279,3918,7654,36584,17010,42800,463715]
NN_size_list = ["64+64","64+64","64+64","64+64","64+64","64+64","64+64","64+64","64+64","128+128","256+256","64+64+64+64"]
RUNS_list = [100,100,100,100,100,100,100,100,100,100,100,10]
alpha_value = 0.5

model_name = "SVGP"
M = 50

for k in range(len(dataset_name_list)):
    dataset_name = dataset_name_list[k]
    title_name = title_name_list[k]
    minibatch_size = minibatch_size_list[k]
    NN_size = NN_size_list[k]
    RUNS = RUNS_list[k]
    kernel_type = "RBF+RBF"
    optimizer_name = "LBFGSB"
    framework_variant = "GP_corrected"

    result_file_name = os.path.join(os.path.dirname(os.path.abspath(__file__)),'Results','MAE_NN_suffledData_{}_{}_{}_{}_M{}_minibatch{}_{}_{}run.pkl'.format(dataset_name, model_name, NN_size, kernel_type, M, minibatch_size, optimizer_name, RUNS))
    with open(result_file_name, 'rb') as result_file:
        MAE_original = pickle.load(result_file)

    result_file_name = os.path.join(os.path.dirname(os.path.abspath(__file__)),'Results','Storage_test_labels_suffledData_{}_{}_{}_{}_M{}_minibatch{}_{}_{}run.pkl'.format(dataset_name, model_name, NN_size, kernel_type, M, minibatch_size, optimizer_name, RUNS))
    with open(result_file_name, 'rb') as result_file:
        Storage_test_labels = pickle.load(result_file)

    result_file_name = os.path.join(os.path.dirname(os.path.abspath(__file__)),'Results','Storage_test_NN_predictions_suffledData_{}_{}_{}_{}_M{}_minibatch{}_{}_{}run.pkl'.format(dataset_name, model_name, NN_size, kernel_type, M, minibatch_size, optimizer_name, RUNS))
    with open(result_file_name, 'rb') as result_file:
        Storage_test_predictions = pickle.load(result_file)

    result_file_name = os.path.join(os.path.dirname(os.path.abspath(__file__)),'Results','MAE_{}_suffledData_{}_{}_{}_{}_M{}_minibatch{}_{}_{}run.pkl'.format(framework_variant, dataset_name, model_name, NN_size, kernel_type, M, minibatch_size, optimizer_name, RUNS))
    with open(result_file_name, 'rb') as result_file:
        MAE = pickle.load(result_file)

    result_file_name = os.path.join(os.path.dirname(os.path.abspath(__file__)),'Results','Storage_mean_{}_suffledData_{}_{}_{}_{}_M{}_minibatch{}_{}_{}run.pkl'.format(framework_variant, dataset_name, model_name, NN_size, kernel_type, M, minibatch_size, optimizer_name, RUNS))
    with open(result_file_name, 'rb') as result_file:
        Storage_mean = pickle.load(result_file)

    num_test_point = len(Storage_test_labels[0])
    print("num_test_point: {}".format(num_test_point))
    RMSE_NN = []
    for i in range(len(Storage_test_labels)):
        RMSE_NN.append(np.sqrt(mean_squared_error(Storage_test_labels[i], Storage_test_predictions[i])))

    RMSE_RIO = []
    for i in range(len(Storage_test_labels)):
        RMSE_RIO.append(np.sqrt(mean_squared_error(Storage_test_labels[i], Storage_test_predictions[i] + Storage_mean[i])))

    f = plt.figure()
    plt.title(title_name)
    plt.scatter(RMSE_NN, RMSE_RIO, label="RIO", alpha=alpha_value)

    kernel_type = "RBF"
    framework_variant = "GP_inputOnly"
    result_file_name = os.path.join(os.path.dirname(os.path.abspath(__file__)),'Results','Storage_mean_{}_suffledData_{}_{}_{}_{}_M{}_minibatch{}_{}_{}run.pkl'.format(framework_variant, dataset_name, model_name, NN_size, kernel_type, M, minibatch_size, optimizer_name, RUNS))
    with open(result_file_name, 'rb') as result_file:
        Storage_mean = pickle.load(result_file)

    RMSE = []
    for i in range(len(Storage_test_labels)):
        RMSE.append(np.sqrt(mean_squared_error(Storage_test_labels[i], Storage_mean[i])))
    if k!=10:
        plt.scatter(RMSE_NN, RMSE, label="SVGP", alpha=alpha_value)
    plt.xlabel('NN RMSE')
    plt.ylabel('RIO/SVGP RMSE')
    bottom, top = plt.ylim()
    left, right = plt.xlim()
    _ = plt.plot([-100, 100], [-100, 100])
    if k == 7:
        top = 10
        bottom = 3.6
    plt.ylim((bottom, top))
    plt.xlim((left, right))

    legend = plt.legend()
    plot_file_name = os.path.join(os.path.dirname(os.path.abspath(__file__)),'Plots','RMSE_comparison_suffledData_{}_{}_{}_{}_M{}_minibatch{}_{}_{}run.pdf'.format(dataset_name, model_name, NN_size, kernel_type, M, minibatch_size, optimizer_name, RUNS))
    f.savefig(plot_file_name, bbox_inches='tight')

plt.show()


"""Modules for parsing experiments configuration."""

import copy

import pandas as pd

import ruamel.yaml

class Experiments:
    """Parse and access experiments configuration.

    Parameters
    ----------
    experiments_config : str or dict
        Name of YAML file with experiment configuration, or dict
        read from such a file.
    viral_tags : list
        Valid viral tags, parsed from yaml file specified in data
        directory.
    barcoded_viral_genes : list
        Viral genes with barcode, parsed from viral genbank file.

    Attributes
    ----------
    config_dict : dict
        Direct result of parsing the map in `experiments_config_file`.
    experiments : list
        All experiments.
    transcriptomic_runs : list
        All transcriptomic runs.
    transcriptomics_df : pandas.DataFrame
        Data frame of all transcriptomics runs.
    pacbio_df : pandas.DataFrame
        Data frame of all pacbio runs.
    expts_with_pacbio : list
        Experiments that have PacBio viral sequencing.
    viral_barcodes_df : pandas.DataFrame
        Data frame with all viral barcode sequencing paths
    expts_with_progeny_barcodes: list
        Experiments that have progeny viral barcode data

    """

    def __init__(self, experiments_config, viral_tags, barcoded_viral_genes):
        """See main class docstring."""
        if isinstance(experiments_config, dict):
            self.config_dict = copy.deepcopy(experiments_config)
        else:
            yaml = ruamel.yaml.YAML(typ='safe')
            with open(experiments_config) as f:
                self.config_dict = yaml.load(f)
            assert isinstance(self.config_dict, dict)

        self.experiments = list(self.config_dict)
        self._expect_ncells = {}
        self._infection_threshold = {}

        # now get data for each experiment
        valid_keys = {'description',
                      'lab_notes',
                      'expect_ncells',
                      'infection_threshold',
                      'transcriptomics',
                      'viral_barcodes',
                      'pacbio_viral_sequencing'}
        transcriptomics_records = []
        viral_barcodes_records = []
        pacbio_records = []
        for expt, expt_d in self.config_dict.items():

            if not set(expt_d).issubset(valid_keys):
                raise ValueError(f"invalid entries for {expt}")

            if 'transcriptomics' in expt_d:
                for run, run_d in expt_d['transcriptomics'].items():
                    transcriptomics_records.append((
                            expt,
                            run,
                            f"{expt}_{run}",
                            run_d['index'],
                            run_d['bcl_folder'],
                            run_d['lane'],
                            run_d['index_sequencing'],
                            ))

            if 'pacbio_viral_sequencing' in expt_d:
                for run, subread_bam in (expt_d['pacbio_viral_sequencing']
                                         .items()):
                    pacbio_records.append((
                            expt,
                            run,
                            f"{expt}_{run}",
                            subread_bam,
                            ))

            if 'expect_ncells' in expt_d:
                self._expect_ncells[expt] = expt_d['expect_ncells']
                if not isinstance(self._expect_ncells[expt], int):
                    raise ValueError(f"`expect_ncells` not int for {expt}")
            else:
                raise KeyError(f"`expect_ncells` missing for {expt}")

            if 'infection_threshold' in expt_d:
                self._infection_threshold[expt] = expt_d['infection_threshold']
                if not isinstance(self._infection_threshold[expt], float):
                    raise ValueError(f"`infection_threshold` not int for {expt}")
            else:
                raise KeyError(f"`infection_threshold` missing for {expt}")

            if 'viral_barcodes' in expt_d:
                for source, source_d in expt_d['viral_barcodes'].items():
                    for tag, tag_d in source_d.items():
                        if tag not in viral_tags:
                            raise ValueError(f"invalid tag entry for {tag}")
                        for gene, gene_d in tag_d.items():
                            if gene not in barcoded_viral_genes:
                                raise ValueError(f"gene {gene} not barcoded")
                            for replicate, replicate_d in gene_d.items():
                                for run, fastq_path in replicate_d.items():
                                    viral_barcodes_records.append((
                                                                expt,
                                                                source,
                                                                tag,
                                                                gene,
                                                                replicate,
                                                                run,
                                                                fastq_path))

        self.transcriptomics_df = pd.DataFrame(transcriptomics_records,
                                               columns=['experiment',
                                                        'run_name',
                                                        'transcriptomic_run',
                                                        'index',
                                                        'bcl_folder',
                                                        'lane',
                                                        'index_sequencing',
                                                        ])
        self.transcriptomic_runs = (self.transcriptomics_df
                                    ['transcriptomic_run']
                                    .tolist()
                                    )
        assert (len(self.transcriptomic_runs) ==
                len(set(self.transcriptomic_runs)))

        self.pacbio_df = pd.DataFrame(pacbio_records,
                                      columns=['experiment',
                                               'run_name',
                                               'pacbio_run',
                                               'subread_bam',
                                               ])
        self.expts_with_pacbio = (self.pacbio_df
                                  ['experiment']
                                  .unique()
                                  .tolist()
                                  )
        assert len(self.pacbio_df) == self.pacbio_df['pacbio_run'].nunique()

        self.viral_barcodes_df = pd.DataFrame(viral_barcodes_records,
                                              columns=['experiment',
                                                       'source',
                                                       'tag',
                                                       'gene',
                                                       'replicate',
                                                       'run',
                                                       'fastq_path'])
        self.expts_with_progeny_barcodes = (self.viral_barcodes_df
                                            ['experiment']
                                            .unique()
                                            .tolist()
                                            )
        assert (len(self.expts_with_progeny_barcodes) ==
                len(set(self.expts_with_progeny_barcodes)))

    def transcriptomic_index(self, transcriptomic_run):
        """str: Illumina index for `transcriptomic_run`."""
        assert transcriptomic_run in self.transcriptomic_runs, (
                f"invalid `transcriptomic_run` {transcriptomic_run}")
        return str(self.transcriptomics_df
                   .set_index('transcriptomic_run')
                   .at[transcriptomic_run, 'index']
                   )

    def transcriptomic_bcl_folder(self, transcriptomic_run):
        """str: BCL folder for `transcriptomic_run`."""
        assert transcriptomic_run in self.transcriptomic_runs, (
                f"invalid `transcriptomic_run` {transcriptomic_run}")
        return str(self.transcriptomics_df
                   .set_index('transcriptomic_run')
                   .at[transcriptomic_run, 'bcl_folder']
                   )

    def transcriptomic_lane(self, transcriptomic_run):
        """str: Lane for `transcriptomic_run`."""
        assert transcriptomic_run in self.transcriptomic_runs, (
                f"invalid `transcriptomic_run` {transcriptomic_run}")
        return str(self.transcriptomics_df
                   .set_index('transcriptomic_run')
                   .at[transcriptomic_run, 'lane']
                   )

    def transcriptomic_index_sequencing(self, transcriptomic_run):
        """str: Index seq strategy for `transcriptomic_run`."""
        assert transcriptomic_run in self.transcriptomic_runs, (
                f"invalid `transcriptomic_run` {transcriptomic_run}")
        return str(self.transcriptomics_df
                   .set_index('transcriptomic_run')
                   .at[transcriptomic_run, 'index_sequencing']
                   )

    def expt_transcriptomic_runs(self, expt):
        """list: Transcriptomic runs for experiment `expt`."""
        assert expt in self.experiments, f"invalid `expt` {expt}"
        return (self.transcriptomics_df
                .query('experiment == @expt')
                ['transcriptomic_run']
                .tolist()
                )

    def expect_ncells(self, expt):
        """int: Expected number of cells for experiment `expt`."""
        return self._expect_ncells[expt]

    def infection_threshold(self, expt):
        """float: Fraction of UMIs from virus to be considered infected for experiment `expt`."""
        return self._infection_threshold[expt]

    def expt_viral_barcode_fastqs(self, expt):
        """pandas.DataFrame: FASTQs of viral barcodes

        This function takes an argument with the desired
        experiment, `expt`. It returns a pandas
        DataFrame with paths to the viral barcode FASTQ
        files for that experiment. Each FASTQ path is
        annotated with source, tag, gene, replicate, and run.
        """
        assert expt in self.experiments, f"invalid `expt` {expt}"
        return (self.viral_barcodes_df
                .query('experiment == @expt')
                )

    def expt_pacbio_runs(self, expt):
        """list: PacBio runs for experiment `expt`."""
        assert expt in self.experiments, f"invalid `expt` {expt}"
        return (self.pacbio_df
                .query('experiment == @expt')
                ['pacbio_run']
                .tolist()
                )

    def pacbio_subreads(self, pacbio_run):
        """str: Path to PacBio subreads BAM for `pacbio_run`."""
        assert pacbio_run in self.pacbio_df['pacbio_run'].tolist(), (
                    f"invalid `pacbio_run` {pacbio_run}")
        return str(self.pacbio_df
                   .set_index('pacbio_run')
                   .at[pacbio_run, 'subread_bam']
                   )


import os
import pandas as pd
import datetime
import numpy as np
import trackpy
import matplotlib.pyplot as plt
#from connect_the_dots.io import get_CZI_metadata
from scipy.signal import savgol_filter
trackpy.quiet()

##################################################################################
# PARAMETERS
##################################################################################
num_dates_to_average = 7 # the number of bead files to average over

# some parameters - need to put into configuration file eventually
verbose = False
bead_intensity_min = 100 # minimum volume in pixel counts
bead_intensity_max = 2000 # maximum volume in pixel counts
search_range = [3,4,4] # for linking beads across channels
min_len = 5 # minimum trajectory length for beads
memory = 0 # number of allowed missing frames for bead trajectories

sav_gol_window = 91 # filtering window length in X and Y
sav_gol_windowZ = 21 # filtering window length in Z
sav_gol_order = 1 # order of SG filter
step_size = 0.5 # step size in pixels for saving the chromatic shift

# superblocks -- need to get from configuration file
super_blocks = [('2020-08-27','2020-10-19'), # Colibri 7 installed
                ('2020-10-20','2020-11-10'), # Colibri 7 to 5 swap
                ('2020-11-11','2021-02-07'), # Definite focus issue fixed
                ('2021-02-08','2021-05-06'), # Piezo adjusted
                ('2021-05-07','2021-12-31'), # PSF check date
               ]

##################################################################################

# GET METADATA
# get pixel sizes and image dimensions from sample metadta
# data_info , sample_metadata = get_CZI_metadata(snakemake.input.sample_czi)
fx = 0.08507776712474871 #sample_metadata['OME']['Image']['Pixels']['PhysicalSizeX']
fy = 0.08507776712474871 #sample_metadata['OME']['Image']['Pixels']['PhysicalSizeY']
fz = 0.16 #sample_metadata['OME']['Image']['Pixels']['PhysicalSizeZ']
ux = 'um' #sample_metadata['OME']['Image']['Pixels']['PhysicalSizeXUnit']
uy = 'um' #sample_metadata['OME']['Image']['Pixels']['PhysicalSizeYUnit']
uz = 'um' #sample_metadata['OME']['Image']['Pixels']['PhysicalSizeZUnit']
(sizeX,sizeY,sizeZ,sizeT,num_channels) = (584, 584, 30, 365, 2) #data_info

traj = snakemake.input.traj

# get date from the sample movie
traj = traj.split('/')[-1]
try:
    YY = int(traj.split('_')[0])
    MM = int(traj.split('_')[1])
    DD = int(traj.split('_')[2])
except:
    YY = int(traj[0:4])
    MM = int(traj[5:7])
    DD = int(traj[8:10])
sample_date =  datetime.datetime(YY,MM,DD)

# get dates from the beads movies
beads_files = snakemake.input.bead_localizations

# get dates from the file name (assumes YY_MM_DD)
date_list = []
for f in beads_files:
    f = f.split('/')[-1]
    try:
        YY = int(f.split('_')[0])
        MM = int(f.split('_')[1])
        DD = int(f.split('_')[2])
        date = datetime.date(YY, MM, DD)
    except:
        try:
            YY = int(f[0:4])
            MM = int(f[5:7])
            DD = int(f[8:10])
            date = datetime.date(YY, MM, DD)
        except:
            date = ''
    date_list.append(date)

# create datetime object for the dates
df = pd.DataFrame({'date':date_list})
df['date'] = pd.to_datetime(df['date'])
df['beads_files'] = beads_files

# remove rows that have the NaT timestamp
df = df.loc[df.date.notnull()]

# get the nearest date
def get_nearest_date(bead_datetime_list, sample_datetime):
    return min(bead_datetime_list, key=lambda x: abs(x - sample_datetime))

def dates_to_average(idx,map_number_to_date,group_size=3,verbose=False):

    try:
        assert group_size%2 == 1
    except:
        print("group_size must be an odd number")
        return 0

    if group_size > len(map_number_to_date):
        group_size = len(map_number_to_date)

    if idx >= len(map_number_to_date):
        print(f"Date out of range for idx={idx}")
        return 0

    inds = np.array(sorted(map_number_to_date.keys()))
    lower = inds > idx+group_size//2
    upper = inds < idx-group_size//2

    # for non-edge cases
    if (idx >= group_size//2) and (idx < len(inds)-group_size//2):
        indices = [i for i, (l, u) in enumerate(zip(lower, upper)) if (l==False) and (u==False)]
        if verbose==True:
            print("Middle")

    elif (idx >= len(inds)-group_size//2):
        indices = [i for i in np.arange(len(inds)-group_size,len(inds))]
        if verbose==True:
            print('Lower')
    else:
        indices = [i for i in np.arange(0,group_size)]
        if verbose==True:
            print("Upper")

    dates = [map_number_to_date[inds[i]] for i in indices]
    return dates

# get the superblock - filter out the bead dates outside the given superblock
block_df = []
for start_date, end_date in super_blocks:
    YY = int(start_date.split('-')[0])
    MM = int(start_date.split('-')[1])
    DD = int(start_date.split('-')[2])
    start_datetime = datetime.datetime(YY,MM,DD)

    YY = int(end_date.split('-')[0])
    MM = int(end_date.split('-')[1])
    DD = int(end_date.split('-')[2])
    end_datetime = datetime.datetime(YY,MM,DD)

    # is this the correct date superblock?
    if start_datetime <= sample_date <= end_datetime:

        # create filtered dataframe with only dates within sample superblock
        block_df = df[(df['date'] >= start_date) & (df['date'] <= end_date)]
        break

# get the list of unique dates, and sort
sorted_unique_dates = sorted(set(block_df['date']))

# create mappings from date to a running index for grouping
map_date_to_number = {}
map_number_to_date = {}
for di, d in enumerate(sorted_unique_dates):
    map_date_to_number[d] = di
    map_number_to_date[di] = d

# get the bead date that is closest to the sample date
nearest_bead_date = get_nearest_date(block_df.date, sample_date)
nearest_bead_date_idx = map_date_to_number[nearest_bead_date]

bead_dates_group = dates_to_average(nearest_bead_date_idx,
                                    map_number_to_date,
                                    group_size=num_dates_to_average,
                                    verbose=False)

files_to_average = block_df[block_df['date'].isin(bead_dates_group)].beads_files

if verbose == True:
    print(f"Averaging over {len(files_to_average)} files")
if len(files_to_average)==0:
    if verbose == True:
        print(f"Skipping this date range -- no files found.")
    assert(len(files_to_average)>0)

## get the chromatic shifts for from the selected beads files
DX = []
DY = []
DZ = []
X1 = []
Y1 = []
Z1 = []
X0 = []
Y0 = []
Z0 = []
for file in files_to_average.values:

    # load pre-processed bead locations
    df_particles = pd.read_csv(file)

    # filter based on bead intensity
    df_particles = df_particles.query(f"dot_size_in_pixels>{bead_intensity_min}" +
                                      f"& dot_size_in_pixels<{bead_intensity_max}")

    colour_scheme = ['r','b','g','m','c']
    windowZYXdims = search_range

    # load csv files with segmentation positions ouputs
    df_particles['timestep'] = df_particles['frame']

    # get special positions
    c = df_particles['channel'].values
    t = df_particles['timestep'].values
    x = df_particles['x'].values
    y = df_particles['y'].values
    z = df_particles['z'].values

    # make new, reduced data frame
    df_short = pd.DataFrame({'channel': c, 'frame': t,'x': x, 'y': y, 'z': z})

    # do for each channel
    channels = set(df_short['channel'].values)

    # link trajectories across channels:
    # artificially shift the frames and create trajectories
    linked_df = df_short.copy()
    linked_df['frame'] = linked_df['frame']*len(channels)+linked_df['channel']
    try:
        linked_df = trackpy.link_df(linked_df, \
                    search_range=search_range, \
                    memory=memory,)
    except:
        continue
    # reverse shift the frame numbers
    linked_df['frame'] = (linked_df['frame']-linked_df['channel'])/len(channels)

    # get unique IDs for sets of linked dots
    particle_set = set(linked_df.particle)

    for p in particle_set:

        ch = 0
        x_vals0 = linked_df[(linked_df.particle==p) & (linked_df.channel==ch)].x.values
        y_vals0 = linked_df[(linked_df.particle==p) & (linked_df.channel==ch)].y.values
        z_vals0 = linked_df[(linked_df.particle==p) & (linked_df.channel==ch)].z.values
        t_vals0 = linked_df[(linked_df.particle==p) & (linked_df.channel==ch)].frame.values

        ch = 1
        x_vals1 = linked_df[(linked_df.particle==p) & (linked_df.channel==ch)].x.values
        y_vals1 = linked_df[(linked_df.particle==p) & (linked_df.channel==ch)].y.values
        z_vals1 = linked_df[(linked_df.particle==p) & (linked_df.channel==ch)].z.values
        t_vals1 = linked_df[(linked_df.particle==p) & (linked_df.channel==ch)].frame.values

        if (len(x_vals0) == len(x_vals1)) and (len(x_vals0) > min_len) and np.all(t_vals0==t_vals1):
            X0.extend(x_vals0)
            Y0.extend(y_vals0)
            Z0.extend(z_vals0)
            X1.extend(x_vals1)
            Y1.extend(y_vals1)
            Z1.extend(z_vals1)
            DX.extend( x_vals1-x_vals0)
            DY.extend( y_vals1-y_vals0)
            DZ.extend( z_vals1-z_vals0)

X0 = np.array(X0)
Y0 = np.array(Y0)
Z0 = np.array(Z0)
X1 = np.array(X1)
Y1 = np.array(Y1)
Z1 = np.array(Z1)
dx = np.array(DX)
dy = np.array(DY)
dz = np.array(DZ)

# add the trend lines
maxX = sizeX
maxY = sizeY
maxZ = sizeZ
minX = 0
minY = 0
minZ = 0

# do X  axis
xq = np.arange(minX,maxX,step_size)
xx = np.array(X0)
yy = np.array(dx)
ss = np.argsort(xx)
yq = np.interp(xq,xx[ss],yy[ss])
yq = savgol_filter(yq,sav_gol_window,sav_gol_order)
chromatic_shift_X = np.c_[xq,yq]

# do Y axis
xq = np.arange(minY,maxY,step_size)
xx = np.array(Y0)
yy = np.array(dy)
ss = np.argsort(xx)
yq = np.interp(xq,xx[ss],yy[ss])
yq = savgol_filter(yq,sav_gol_window,sav_gol_order)
chromatic_shift_Y = np.c_[xq,yq]

# do Z axis
xq = np.arange(minZ,maxZ,step_size)
xx = np.array(Z0)
yy = np.array(dz)
ss = np.argsort(xx)
yq = np.interp(xq,xx[ss],yy[ss])
yq = savgol_filter(yq,sav_gol_windowZ,sav_gol_order)
chromatic_shift_Z = np.c_[xq,yq]

if verbose == True:
    print("Saving chromatic shift")
# save the chromatic shift information
np.savez(snakemake.output.chromatic_shift,
         chromatic_shift_X=chromatic_shift_X,
         chromatic_shift_Y=chromatic_shift_Y,
         chromatic_shift_Z=chromatic_shift_Z,
         fx=fx,fy=fy,fz=fz,ux=ux,uy=uy,uz=uz)


from sklearn.metrics import accuracy_score
from joblib import load
import pandas as pd

def cape_handler(input_data):
    csv = input_data.decode("utf-8")
    csv = csv.replace("\\t", ",").replace("\\n", "\n")
    f = open("data.csv", "w")
    f.write(csv)
    f.close()

    data = pd.read_csv("data.csv")
    clf = load('model.joblib')
    y_pred = clf.predict(data)
    if y_pred == 0:
        return "This credit card transaction is legitimate"
    else:
        return "This credit card transaction is fraudulent"


import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import xlrd

# 颜色列表-2
color_List = ['yellow','red','green','purple','black']

# 分类函数
def classify(data,centers):
    length = centers.shape[0]  #查询有多少个中心点
    classes = [ [] for i in range(length) ]
    sumDis = 0

    for i in range(data.shape[0]) :
        per_data = data[i]
        diffMat = np.tile(per_data,(length,1))-centers  #复制并计算距离
        sqDiffMat = diffMat**2
        sqDisMat = sqDiffMat.sum(axis=1) #按行求和
        sortedIndex = sqDisMat.argsort() #对距离进行排序并得到相应指令
        classes[sortedIndex[0]].append(list(per_data))
        sumDis += sqDisMat[sortedIndex[0]]
    return classes,sumDis

# 中心点更新
def upCenters(classes):
    centers = []
    for i in range(len(classes)) :
        per_class = classes[i]
        per_class = np.array(per_class)
        center = per_class.sum(axis=0)/len(per_class) #按列求和求中心点
        centers.append(center)
    return np.array(centers)

# 聚类函数
def kmeans(data,centers,sumDis) :
    #聚类流程：聚类——修改中心点——再聚类——再修改中心点——两次聚类的总距离一样则戒指
    # 聚类
    classes,new_sumDis = classify(data, centers)
    if sumDis == new_sumDis :
        return
    # 修改中心
    new_centers = upCenters(classes)

    # 绘图
    fig = plt.figure()
    ax = fig.gca(projection="3d")
    for i in range(len(new_centers)):
        center = centers[i]
        ax.scatter(center[0], center[1], center[2], marker='x', c=color_List[i])

    for i in range(len(classes)):
        per_class = classes[i]
        for c in per_class:
            ax.scatter(c[0], c[1], c[2], c=color_List[i])

    plt.title('sumDis%f'%new_sumDis)
    plt.show()

    kmeans(data,new_centers,new_sumDis)

# 提取数组信息
def getdata(xls) :
    workbook = xlrd.open_workbook(xls)
    worksheet = workbook.sheet_by_index(0)
    nrows,ncols = worksheet.nrows,worksheet.ncols
    data = []

    for i in range(nrows) :
        temp = []
        for j in range(ncols):
            temp.append(worksheet.cell_value(i,j))
        data.append(temp)

    return np.array(data)  #转化成数组

if __name__ == '__main__':
    # 在相同文件夹下放一个data.xls的文件
    data = getdata('data.xls')
    # print(data)
    centers = data[:3]
    kmeans(data,centers,0)


from terra_sdk.client.lcd import LCDClient
from terra_sdk.key.mnemonic import MnemonicKey
import difflib
import itertools

####### UTILS #######

def check_seed(seed, address, blockchain='Terra'):

    if blockchain == 'Terra':
        chain = LCDClient("https://pisco-lcd.terra.dev/", "pisco-1")  # testnet

    key = MnemonicKey(seed)
    wallet = chain.wallet(key)
    possible_address = wallet.key.acc_address
    if possible_address == address:
        return True
    else:
        return False

####### EXACTLY ONE ERROR #######

# For each word in your seed phrase it looks for n closer words and test them
def one_error_close_words(seed, words, address, n = 10):
    seed_list = seed.split()
    for word in seed_list:
        matches = difflib.get_close_matches(word, words, n)
        for match in matches:
            possible_seed = seed.replace(word, match)
            if check_seed(possible_seed, address):
                return possible_seed
    return False

# This try all combination with all the words
def one_error_all_words(seed, words, address):
    seed_list = seed.split()
    for possible_word in words:
        for seed_word in seed_list:
            possible_seed = seed.replace(seed_word, possible_word)
            if check_seed(possible_seed, address):
              return possible_seed

    return False

####### UP TO 24 SMALL ERRORS OR WRONG WORDS POSITIONS #######

# For each word in your seed phrase it looks for n closer words, then test for all the combinations of these words
def multiple_errors(seed, words, address, n = 2):
    seed_list = seed.split()
    matches = []
    # Getting close words
    for word in seed_list:
        match = difflib.get_close_matches(word, words, n)
        matches.append(match)

    # Generating all combinations
    combinations = itertools.combinations(matches, 24)

    for possible_seed in combinations:
        if check_seed(possible_seed, address):
          return possible_seed

    # Generating all the products
    products = [p for p in itertools.product(*matches)]

    # Checking all possible seeds
    for product in products:
        possible_seed = " ".join(product)
        if check_seed(possible_seed, address):
          return possible_seed

    return False

def brute_force(seed, words, address):

    seed_phrase = one_error_close_words(seed, words, address)
    if seed_phrase != False:
        return seed_phrase

    seed_phrase = one_error_all_words(seed, words, address)

    if seed_phrase != False:
        return seed_phrase

    seed_phrase = multiple_errors(seed, words, address)

    if seed_phrase != False:
        return seed_phrase
    else:
        return "We are sorry, we can't recover this wallet :("


import math
import numpy as np
import cv2

def createKernel(kernelSize=9, sigma=4, theta=1.5):
    "create anisotropic filter kernel according to given parameters"
    assert kernelSize % 2 # must be odd size
    halfSize = kernelSize // 2

    kernel = np.zeros([kernelSize, kernelSize])
    sigmaX = sigma
    sigmaY = sigma * theta

    for i in range(kernelSize):
        for j in range(kernelSize):
            x = i - halfSize
            y = j - halfSize

            expTerm = np.exp(-x**2 / (2 * sigmaX) - y**2 / (2 * sigmaY))
            xTerm = (x**2 - sigmaX**2) / (2 * math.pi * sigmaX**5 * sigmaY)
            yTerm = (y**2 - sigmaY**2) / (2 * math.pi * sigmaY**5 * sigmaX)

            kernel[i, j] = (xTerm + yTerm) * expTerm

    kernel = kernel / np.sum(kernel)
    return kernel

def applySummFunctin(img):
    res = np.sum(img, axis = 0)    #  summ elements in columns
    return res

def normalize(img):
    (m, s) = cv2.meanStdDev(img)
    m = m[0][0]
    s = s[0][0]
    img = img - m
    img = img / s if s>0 else img
    return img

def smooth(x, window_len=11, window='hanning'):
    #     if x.ndim != 1:
    #         raise ValueError("smooth only accepts 1 dimension arrays.")
    # if x.size < window_len:
    #     raise ValueError("Input vector needs to be bigger than window size.")
    if window_len<3:
        return x
    if not window in ['flat', 'hanning', 'hamming', 'bartlett', 'blackman']:
        raise ValueError("Window is on of 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'")
    s = np.r_[x[window_len-1:0:-1],x,x[-2:-window_len-1:-1]]
    #print(len(s))
    if window == 'flat': #moving average
        w = np.ones(window_len,'d')
    else:
        w = eval('np.'+window+'(window_len)')

    y = np.convolve(w/w.sum(),s,mode='valid')
    return y

def get_line_coordinate(text, blanks):
    x1 = 0
    w = text.shape[1]
    y1 = 0
    lines = []
    for i, blank in enumerate(blanks):
        y2 = blank
        h  = y2-y1
        lines.append((x1, y1, w, h))
        y1 = blank
    return lines


"""PyDnet Model Freezer.
"""

import os
import tensorflow as tf
import coremltools as ct
from pathlib import Path
from typing import Any, Dict
from tensorflow.python.tools import freeze_graph
from tensorflow.compat.v1.graph_util import convert_variables_to_constants
from .pydnet import Pydnet

def freeze_pydnet(
    name: str,
    h: int,
    w: int,
    checkpoint: Path,
    output: Path
) -> Dict[str, Any]:
    """Freezes the PyDnet model.
    Args:
        name(str): The name of the architecture.
        h (int): The image height.
        w (int): The image wodth.
        checkpoint (pathlib.Paht): The path to the checkpoint file.
        output (pathlib.Paht): The path to the output directory.
    """
    with tf.Graph().as_default() as graph:
        placeholder = tf.placeholder(
            tf.float32,
            [1, h, w, 3],
            name="In",
        )
        network = Pydnet({
            "h": h,
            "w": w,
            "is_training": False,
        })
        network.forward(placeholder)
        save = tf.train.Saver()
        with tf.Session() as sess:
            save.restore(sess, str(checkpoint))
            # NOTE: This is a workaround to prevent using the `freeze_graph` function.
            # `freeze_graph` may cause an `IndexError.`
            graph_def = convert_variables_to_constants(
                sess,
                graph.as_graph_def(),
                [x.name.replace(":0", "") for x in network.output_nodes])
            tf.train.write_graph(
                graph_def,
                str(output),
                f"{name}.pb",
                as_text=False)
    return (output / f"{name}.pb").resolve()


from sanic_graphql import GraphQLView
from sanic import Sanic
from database import db_session, init_db
from schema import schema

app = Sanic(__name__)
app.debug = True

# This will be the query
default_query = """
{
    allEmployee {
        edges {
            node {
                id,
                name,
                department {
                    id,
                    name
                },
                role {
                    id,
                    name
                }
            }
        }
    }
}
""".strip()

# It's like HTTP routes but instead, it directs you to /graphql (Schema).
# graphiq = True will initiate the GUI
app.add_route(GraphQLView.as_view(schema=schema, graphiql=True), '/graphql')

# runs the app and database. init_db contains seed data.
if __name__ == '__main__':
    init_db()
    app.run()


# -*- coding: utf-8 -*-
import os, sys, atexit, subprocess, string, signal, glob
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.button import Button
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.dropdown import DropDown
from kivy.graphics import Color, Rectangle
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.scrollview import ScrollView
from kivy.utils import platform
from kivy import base
from kivy.uix.popup import Popup
import kivy.metrics
from mod_ecu_screen import *

import mod_globals

widgetglobal = None
choice_result = None
resizeFont = False
favouriteScreen = ecu_own_screen('FAV')
fmn = 2
bmn = 2.5

try:
    import webbrowser
except:
    pass

def InfoPopup(bas=None):
    fs = mod_globals.fontSize
    pop = MyPopup(content=MyLabel(text='LOADING', size_hint = (1, 1), font_size=fs*3, halign = 'center'))
    if not bas:
        base.runTouchApp(embedded=True)
    pop.open()
    base.EventLoop.idle()
    pop.dismiss()
    if not bas:
        base.stopTouchApp()

def MyPopup_close(title='', cont=True, l=True, op=True, cl=None):
    fs = mod_globals.fontSize
    layout = GridLayout(cols=1, padding=5, spacing=10, size_hint=(1, 1))
    if not l:
        t = 'CLOSE'
    else:
        t = get_message_by_id('16831')
    btn = MyButton(text=t, size_hint=(1, None), font_size=fs*2)
    layout.add_widget(cont)
    layout.add_widget(btn)
    pop = MyPopup(title=title, content=layout)
    if cl:
        btn.bind(on_press=lambda *args:exit())
    else:
        btn.bind(on_press=lambda *args:pop.dismiss())
    if op:
        pop.open()
    else:
        return pop

class MyTextInput(TextInput):
    def __init__(self, **kwargs):
        fs = mod_globals.fontSize
        font = None
        id = ''
        if 'id' in kwargs:
            self.id = kwargs['id']
            del kwargs ['id']
        super(MyTextInput, self).__init__(**kwargs)
        if 'size_hint' not in kwargs:
            self.size_hint = (1, None)
        if 'halign' not in kwargs:
            self.halign='center'
        if 'font_size' not in kwargs:
            self.font_size = fs
            font = True
        if 'height' not in kwargs:
            lines = len(self.text.split('\n'))
            simb = round((len(self.text) * self.font_size) / (Window.size[0] * self.size_hint[0]), 2)
            if lines < simb:
                if (lines * 2) > simb: lines = simb + lines/2
                else: lines = simb
            if 2 < lines < 3: lines = lines
            if lines < 2: lines = lines
            self.height = lines * self.font_size * 1.4
        if mod_globals.os == 'android':
            self.font_size = self.font_size * 0.9
        self.height = kivy.metrics.dp(self.height)
        self.font_size = kivy.metrics.dp(self.font_size)
        self.padding = str(self.font_size / self.height) + 'sp'

class MyPopup(Popup):
    close = ''
    def __init__(self, **kwargs):
        fs = mod_globals.fontSize
        super(MyPopup, self).__init__(**kwargs)
        if 'title' not in kwargs:
            self.title='INFO'
        if 'auto_dismiss' not in kwargs:
            self.auto_dismiss=True
        if 'title_size' not in kwargs:
            self.title_size=str(fs) + 'sp'
        if 'content' not in kwargs:
            self.content=MyLabel(text='LOADING', size_hint = (1, 1))
        if 'title_align' not in kwargs:
            self.title_align='center'
        if 'size_hint' not in kwargs:
            self.size_hint=(None, None)
            self.size=(Window.size[0]*0.95, Window.size[1]*0.95)

class MyButton(Button):
    def __init__(self, **kwargs):
        fs = mod_globals.fontSize
        id = ''
        if 'id' in kwargs:
            self.id = kwargs['id']
            del kwargs ['id']
        super(MyButton, self).__init__(**kwargs)
        self.bind(size=self.setter('text_size'))
        if 'halign' not in kwargs:
            self.halign = 'center'
        if 'size_hint' not in kwargs:
            self.size_hint = (1, None)
        if 'font_size' not in kwargs:
            self.font_size = fs
        if 'valign' not in kwargs:
            self.valign = 'middle'
        if 'height' not in kwargs:
            lines = len(self.text.split('\n'))
            simb = round((len(self.text) * self.font_size) / (Window.size[0] * self.size_hint[0]), 2)
            if lines < simb: lines = simb
            if lines < 2: lines = lines * 1.5
            self.height = lines * self.font_size * 1.5
        if mod_globals.os == 'android':
            self.font_size = self.font_size * 0.8
        self.height = kivy.metrics.dp(self.height)
        self.font_size = kivy.metrics.dp(self.font_size)

class MyGridLayout(GridLayout):
    def __init__(self, **kwargs):
        if 'spadding' in kwargs:
            del kwargs ['spadding']
        if 'bgcolor' in kwargs:
            self.bgcolor = kwargs['bgcolor']
            del kwargs ['bgcolor']
        else:
            self.bgcolor =(1, 0, 0, 1)
        super(MyGridLayout, self).__init__(**kwargs)

        if 'size_hint' not in kwargs:
            self.size_hint = (1, None)

    def on_size(self, *args):
        if not self.canvas:
            return
        self.canvas.before.clear()
        with self.canvas.before:
            Color(self.bgcolor[0], self.bgcolor[1], self.bgcolor[2], self.bgcolor[3])
            Rectangle(pos=(self.pos[0],self.pos[1]), size=(self.size[0], self.size[1]))

class MyLabel(Label):
    def __init__(self, **kwargs):
        fs = mod_globals.fontSize
        self.bgcolor = ''
        if 'bgcolor' in kwargs:
            self.bgcolor = kwargs['bgcolor']
            del kwargs['bgcolor']
        if 'multiline' in kwargs:
            del kwargs['multiline']
        super(MyLabel, self).__init__(**kwargs)
        self.bind(size=self.setter('text_size'))
        self.text = self.text.replace('\n\n', '\n')
        if 'halign' not in kwargs:
            self.halign = 'center'
        if 'valign' not in kwargs:
            self.valign = 'middle'
        if 'size_hint' not in kwargs:
            self.size_hint = (1, None)
        if 'font_size' not in kwargs:
            self.font_size = fs*0.8
        if 'height' not in kwargs:
            lines = len(self.text.split('\n'))
            simb = round((len(self.text) * self.font_size) / (Window.size[0] * self.size_hint[0]), 2)
            if lines < simb: lines = simb + lines / 1.5
            if lines <= 1.9: lines = 1.9
            if 1.9 < lines <= 3: lines = 3
            self.height = lines * self.font_size * 1.2
        self.height = kivy.metrics.dp(self.height)
        self.font_size = kivy.metrics.dp(self.font_size)

    def on_size(self, *args):
        if not self.canvas:
            return
        self.canvas.before.clear()
        if self.bgcolor:
            with self.canvas.before:
                Color(self.bgcolor[0], self.bgcolor[1], self.bgcolor[2], self.bgcolor[3])
                Rectangle(pos=self.pos, size=self.size)

class MyLabelGreen(ButtonBehavior, Label):
    global fs
    fs = mod_globals.fontSize
    def __init__(self, **kwargs):
        super(MyLabelGreen, self).__init__(**kwargs)
        self.bind(size=self.setter('text_size'))
        if 'halign' not in kwargs:
            self.halign = 'center'
        if 'valign' not in kwargs:
            self.valign = 'middle'
        if 'font_size' not in kwargs:
            self.font_size = fs*0.8
        if 'height' not in kwargs:
            lines = len(self.text.split('\n'))
            simb = ((len(self.text.strip()) * self.font_size) / (Window.size[0] * self.size_hint[0]))
            if lines < simb: lines = simb
            if 1.9 < lines <= 3: lines = 3
            if lines <= 1.9: lines = 1.9
            self.height = lines * self.font_size * 1.3
        self.height = kivy.metrics.dp(self.height)
        self.font_size = kivy.metrics.dp(self.font_size)

    def on_size(self, *args):
        self.canvas.before.clear()
        with self.canvas.before:
            Color(0, 1, 0, 0.25)
            Rectangle(pos=self.pos, size=self.size)

class MyLabelBlue(ButtonBehavior, Label):
    global fs
    fs = mod_globals.fontSize
    def __init__(self, mfs = None, **kwargs):
        if 'param_name' in kwargs:
            self.param_name = kwargs['param_name']
            del kwargs ['param_name']
        super(MyLabelBlue, self).__init__(**kwargs)
        self.bind(size=self.setter('text_size'))
        self.bind(text=self.on_text_changed)
        if 'halign' not in kwargs:
            self.halign = 'left'
        if 'font_size' not in kwargs:
            self.font_size = fs*0.8
        if 'valign' not in kwargs:
            self.valign = 'middle'
        if 'height' not in kwargs:
            lines = len(self.text.split('\n'))
            simb = ((len(self.text) * self.font_size) / (Window.size[0] * self.size_hint[0]))
            if lines < simb: lines = simb
            if lines <= 1.9: lines = 1.9
            if 1.9 < lines <= 3: lines = 3
            self.height = lines * self.font_size * 1.3
        self.height = kivy.metrics.dp(self.height)
        self.font_size = kivy.metrics.dp(self.font_size)
        self.clicked = False

    def on_size(self, widget, size):

        self.text_size = (size[0], None)
        self.texture_update()
        if self.size_hint_y is None and self.size_hint_x is not None:
            self.height = fs * fmn
        elif self.size_hint_x is None and self.size_hint_y is not None:
            self.width = self.texture_size[0]
        self.toNormal()
        for dr in favouriteScreen.datarefs:
            if dr.name == self.param_name:
                self.toAdd()
                self.clicked = True
                break

    def on_text_changed(self, widget, text):
        self.on_size(self, self.size)

    def toAdd(self, *args):
        self.canvas.before.clear()
        with self.canvas.before:
            Color(0.38, 0.55, 0.95, 0.5)
            Rectangle(pos=self.pos, size=self.size)

    def toNormal(self, *args):
        self.canvas.before.clear()
        with self.canvas.before:
            Color(0, 0, 1, 0.25)
            Rectangle(pos=self.pos, size=self.size)

    def on_press(self):
        if self.clicked:
            self.toNormal()
            self.clicked = False
        else:
            self.toAdd()
            self.clicked = True

class widgetChoiceLong(App):

    def __init__(self, list, question, header = ''):
        self.menu_entries = list
        self.header = header
        self.question = question
        self.choice_result = None
        super(widgetChoiceLong, self).__init__()
        Window.bind(on_keyboard=self.key_handler)

    def key_handler(self, window, keycode1, keycode2, text, modifiers):
        global choice_result
        global resizeFont
        if resizeFont:
            return True
        if keycode1 == 45 and mod_globals.fontSize > 10:
            mod_globals.fontSize = mod_globals.fontSize - 1
            resizeFont = True
            self.stop()
            return True
        if keycode1 == 61 and mod_globals.fontSize < 40:
            mod_globals.fontSize = mod_globals.fontSize + 1
            resizeFont = True
            self.stop()
            return True
        if keycode1 == 27:
            choice_result = ['<Up>', -1]
            self.stop()
            return True
        return False

    def choice_done(self, instance):
        global choice_result
        choice_result = [instance.txt, instance.ID]
        self.stop()
        InfoPopup()
        base.EventLoop.window.canvas.clear()

    def build(self):
        fs = mod_globals.fontSize
        layout = GridLayout(cols=1, padding=5, spacing=10, size_hint=(1.0, None))
        layout.bind(minimum_height=layout.setter('height'))
        if self.header:
            titlelabel = MyLabel(text=self.header)
            titlelabel.bind(size=titlelabel.setter('text_size'))
            titlelabel.texture_update()
            titlelabel.height = titlelabel.texture_size[1]
            layout.add_widget(titlelabel)
        question = MyLabel(text=self.question)
        layout.add_widget(question)
        i = 1
        for entry in self.menu_entries:
            btn = MyButton(text=' ' + (entry), size_hint=(1.0, None), halign='left', valign='middle', font_name='RobotoMono-Regular')
            btn.bind(size=btn.setter('text_size'))
            btn.txt = entry
            btn.ID = i
            btn.bind(on_press=self.choice_done)
            layout.add_widget(btn)
            i += 1

        root = ScrollView(size_hint=(1, 1), pos_hint={'center_x': 0.5,
         'center_y': 0.5}, do_scroll_x=False)
        root.add_widget(layout)
        return root

def packValues(requests, datas, requestName, iValues):
    r = requests[requestName]
    cmdPatt = r.SentBytes
    for sdi in r.SentDI.values():
        d = datas[sdi.Name]
        n = [v for v in iValues.keys() if (v.startswith(d.Name) and v.endswith(requestName))]
        if not len(n):
            n = [v for v in iValues.keys() if v.endswith(requestName)]
            if not len(n):
                n = [v for v in iValues.keys() if v.startswith(d.Name)]
                if not len(n):
                    continue

        try:
            value = iValues[n[0]]['value'].strip()
        except:
            value = iValues[d.Name].strip()
        value = getValueFromInput(d, value)
        littleEndian = True if sdi.Endian=="Little" else False
        sb = sdi.FirstByte - 1
        bits = d.BitsCount
        sbit = sdi.BitOffset
        bytes =(bits+sbit-1)//8 + 1
        if littleEndian:
            lshift = sbit
        else:
            lshift =((bytes+1)*8 -(bits+sbit))%8

        try:
            val = int(value,16)
        except:
            return 'ERROR: Wrong HEX value in parametr(%s) : "%s"' %(d.Name, value)
        val =(val&(2**bits-1))<<lshift
        value = hex(val)[2:]
        if value[-1:].upper()=='L':
            value = value[:-1]
        if len(value)%2:
            value = '0'+value
        if value.upper().startswith('0X'): value = value[2:]
        value = value.zfill(bytes*2).upper()
        if not all(c in string.hexdigits for c in value) and len(value)==bytes*2:
            return 'ERROR: Wrong value in parametr:%s(it should have %d bytes)' %(d.Name, d.BytesCount)
        base = cmdPatt[sb*2:(sb+bytes)*2]
        binbase = int(base,16)
        binvalue = int(value,16)
        mask =(2**bits-1)<<lshift

        binvalue = binbase ^(mask & binbase) | binvalue

        value = hex(binvalue)[2:].upper()
        if value[-1:].upper()=='L':
            value = value[:-1]
        value = value[-bytes*2:].zfill(bytes*2)
        cmdPatt = cmdPatt[0:sb*2] + value + cmdPatt[(sb+bytes)*2:]

    return cmdPatt

def getValueFromInput(d, value):
    if value == LANG.l_enter_here:
        return 'ERROR: Wrong value in parametr:%s(it should have %d bytes), be decimal or starts with 0x for hex' %(
                d.Name, d.BytesCount)
    if len(d.List.keys()) and ':' in value:
        value = value.split(':')[0]
    if d.Scaled:
        if ' ' in value:
            value = value.split(' ')[0]
        if value.upper().startswith('0X'):
            value = value[2:]
        else:
            if not all((c in string.digits or c == '.' or c == ',' or c == '-' or c == 'e' or c == 'E') for c in value):
                return 'ERROR: Wrong value in parametr:%s(it should have %d bytes), be decimal or starts with 0x for hex' %(
                d.Name, d.BytesCount)
            flv =(float(value) * float(d.DivideBy) - float(d.Offset)) / float(d.Step)
            value = hex(int(flv))

    if d.BytesASCII:
        hst = ''
        if len(value)<(d.BitsCount//8):
            value += ' '*(d.BitsCount//8 - len(value))
        for c in value:
            hst = hst + hex(ord(c))[2:].zfill(2)
        value = hst
    return value

def kivyChoiceLong(list, question, header = ''):
    global widgetglobal
    global resizeFont
    while 1:
        widgetglobal = widgetChoiceLong(list, question, header)
        widgetglobal.run()
        if not resizeFont:
            return choice_result
        resizeFont = False

def Choice(list, question):
    return kivyChoiceLong(list, question)

def ChoiceLong(list, question, header = ''):
    return kivyChoiceLong(list, question, header)

def ChoiceFromDict(dict, question, showId = True):
    d = {}
    c = 1
    exitNumber = 0
    for k in sorted(dict.keys()):
        s = dict[k]
        if k.lower() == '<up>' or k.lower() == '<exit>':
            exitNumber = c
            d['Q'] = k
        else:
            d[str(c)] = k
        c = c + 1

    while True:
        try:
            ch = input(question)
        except (KeyboardInterrupt, SystemExit):
            sys.exit()

        if ch == 'q':
            ch = 'Q'
        if ch in list(d.keys()):
            return [d[ch], ch]

def getValueFromInput(d, val):
    print(d)
    print(val)

def pyren_encodeS(inp):
    if mod_globals.os == 'android':
        return inp.encode('utf-8', errors='replace')
    else:
        return inp.encode(sys.stdout.encoding, errors='replace').decode('utf-8')

def pyren_encode(inp):
    return inp

def pyren_decode(inp):
    return inp

def pyren_decode_i(inp):
    if mod_globals.os == 'android':
        return inp.decode('utf-8', errors='ignore')
    else:
        return inp.decode(sys.stdout.encoding, errors='ignore')

def clearScreen():
    pass
    #sys.stdout.write(chr(27) + '[2J' + chr(27) + '[;H')

def get_message(ScmParam, msg):
    if msg in list(ScmParam.keys()):
        value = ScmParam[msg]
    else:
        value = msg
    if value.isdigit() and value in list(mod_globals.language_dict.keys()):
        value = (mod_globals.language_dict[value])
    return value

def get_message_by_id(id):
    if id.isdigit() and id in list(mod_globals.language_dict.keys()):
        value = (mod_globals.language_dict[id])
        return value

def hex_VIN_plus_CRC(VIN, plusCRC=True):
    VIN = VIN.upper()
    hexVIN = ''
    CRC = 65535
    for c in VIN:
        b = ord(c)
        hexVIN = hexVIN + hex(b)[2:].upper()
        for i in range(8):
            if (CRC ^ b) & 1:
                CRC = CRC >> 1
                CRC = CRC ^ 33800
                b = b >> 1
            else:
                CRC = CRC >> 1
                b = b >> 1

    CRC = CRC ^ 65535
    b1 = CRC >> 8 & 255
    b2 = CRC & 255
    CRC = (b2 << 8 | b1) & 65535
    sCRC = hex(CRC)[2:].upper()
    sCRC = '0'*(4 - len(sCRC)) + sCRC
    if plusCRC:
        return hexVIN + sCRC
    else:
        return hexVIN

def ASCIITOHEX(ATH):
    ATH = ATH.upper()
    hexATH = ''.join(('{:02x}'.format(ord(c)) for c in ATH))
    return hexATH

def StringToIntToHex(DEC):
    DEC = int(DEC)
    hDEC = hex(DEC)
    return hDEC[2:].zfill(2).upper()

def getVIN(de, elm, getFirst = False):
    m_vin = set([])
    for e in de:
        if mod_globals.opt_demo:
            loadDumpToELM(e['ecuname'], elm)
        else:
            if e['pin'].lower() == 'can':
                elm.init_can()
                elm.set_can_addr(e['dst'], e)
            else:
                elm.init_iso()
                elm.set_iso_addr(e['dst'], e)
            elm.start_session(e['startDiagReq'])
        if e['stdType'].lower() == 'uds':
            rsp = elm.request(req='22F190', positive='62', cache=False)[9:59]
        else:
            rsp = elm.request(req='2181', positive='61', cache=False)[6:56]
        try:
            vin = rsp.replace(' ', '').decode('HEX')
        except:
            continue

        if len(vin) == 17:
            m_vin.add(vin)
            if getFirst:
                return vin

    l_vin = m_vin
    if os.path.exists('savedVIN.txt'):
        with open(mod_globals.user_data_dir + 'savedVIN.txt') as vinfile:
            vinlines = vinfile.readlines()
            for l in vinlines:
                l = l.strip()
                if '#' in l:
                    continue
                if len(l) == 17:
                    l_vin.add(l.upper())

    if len(l_vin) == 0 and not getFirst:
        exit()
    if len(l_vin) < 2:
        try:
            ret = next(iter(l_vin))
        except:
            ret = ''

        return ret
    base.runTouchApp(embedded=True)
    pop.open()
    base.EventLoop.idle()
    pop.dismiss()
    base.stopTouchApp()
    choice = Choice(l_vin, 'Choose VIN : ')
    return choice[0]

def DBG(tag, s):
    if mod_globals.opt_debug and mod_globals.debug_file != None:
        mod_globals.debug_file.write('### ' + tag + '\n')
        mod_globals.debug_file.write('"' + s + '"\n')

def isHex(s):
    return all(c in string.hexdigits for c in s)

def kill_server():
    if mod_globals.doc_server_proc is None:
        pass
    else:
        os.kill(mod_globals.doc_server_proc.pid, signal.SIGTERM)

def show_doc(addr, id):
    if mod_globals.vin == '':
        return
    if mod_globals.doc_server_proc == None:
        mod_globals.doc_server_proc = subprocess.Popen(['python',
         '-m',
         'SimpleHTTPServer',
         '59152'])
        atexit.register(kill_server)
    if mod_globals.opt_sd:
        url = 'http://localhost:59152/doc/' + id[1:] + '.htm'
    else:
        url = 'http://localhost:59152/doc/' + mod_globals.vin + '.htm' + id
    webbrowser.open(url, new=0)


import os
from sched import scheduler
import cv2
from data.CHUV.unsupervised_register import UnsupervisedSliceRegistration
import torch
# from data.CHUV.CHUV import HeartDataset
import numpy as np

from tqdm import tqdm
from skimage import io
from scipy.ndimage import shift
from typing import Tuple
from utils.registration_utils import check_order, shift_2d_replace, reorder_data
from utils.utils_common import load_yaml
from data.CHUV.registration_models import AnalyticalContourRegistrator, OptimizationContourRegistrator, get_loss
from IPython import embed
# from model.registration_models import AnalyticalContourRegistrator, OptimizationContourRegistrator, get_loss

class Registration:
    def __init__(self,
                 configs: dict,
                 registration_type: str,
                 stacks: None,
                 shift_type: str,
                 save_path: str,
                 save_slices: Tuple[bool, bool],
                 plot_contours: bool) -> None:
        """
        Args:
            configs: Dictionary containing the configuration parameters.
            registration_type: type of registration to perform (analytical, anchor_optimization, free_optimization)
            mode: mode of data (train, test, val)
            shift_type: type of shift to use to sample shifted slices (interpolation, integer)
            save_path: path to save registered slices to
            save_slices: trigger to save slices
            plot_contours: trigger to plot contours
        Analytical Solution: The analytical solution is basically aligning the slices by finding the center of each
                             slice and then shifting the center of each slice to match the center of the first slice.
        Anchor Optimization: The anchor optimization consists of an optimization procedure that finds the best shifts
                             with respect to the first slice. The loss is computed by warping each contour to the first
                             contour.
        Free Optimization: The free optimization consists of an optimization procedure that finds the best shifts with
                           respect to every slice. The loss is computed by warping each contour to every other contour.
        """

        self.configs = configs
        self.stacks = stacks
        self.save_slices = save_slices
        self.plot_contours = plot_contours
        self.shift_type = shift_type
        self.registration_type = registration_type
        print(f"Doing {registration_type} registration")

    def registration(self):
        """
        Performs registration of slices with contours in dataset.
        """
        cls = 1
        print('new')

        for stack in tqdm(self.stacks):
            # Stack == patient
            # LV = stack.all_contours
            # outer contains the 2d/3d contours of the LV and the slices

            # all_centers_cls = stack.all_centers[cls]

            # centers = list(all_centers_cls.values())
            # slice_ids = list(all_centers_cls.keys())
            # num_contours = len(all_centers_cls.keys())
            # volume = stack.slices
            # for i in range(len(centers)):
            #     centers[i] = centers[i].cuda()

            # if self.registration_type == "unsupervised":
            #     registrator = UnsupervisedSliceRegistration(self.configs)
            #     shifts = registrator.register(volume=volume)
            # elif self.registration_type == "anchor_optimization" or "moving_anchor_optimization":
            #     # registrator = OptimizationContourRegistrator(registration_type=self.configs.CONTOUR_REGISTRATION.type,
            #     #                                                 num_contours=num_contours,
            #     #                                                 optimizer=torch.optim.Adam,
            #     #                                                 loss_fn=get_loss(self.configs.CONTOUR_REGISTRATION.loss_fn),
            #     #                                                 lr=self.configs.CONTOUR_REGISTRATION.lr,
            #     #                                                 num_iterations=5000)
            #     # registrator.cuda()
            #     # shifts = registrator.register(contours=centers,
            #     #                                 milestones=[10, 5000],
            #     #                                 verbose=True)

            #     shifts = []
            #     for c in centers[1:]:
            #         shifts += [centers[0]-c]
            #     shifts = torch.cat(shifts,dim=0)
            # else:
            #     print('Unsupported registration')

            # shifts_mapped = {}
            # for slice_id, shift in zip(slice_ids[1:], shifts):
            #     shifts_mapped[slice_id] = shift.cpu().detach()

            shifts_mapped = stack.shifts

            # embed()

            shifted_slices = []
            for c, all_contours_cls_ in stack.all_contours.items():
                all_centers_cls_ = stack.all_centers[c]
                for slice_id, contour_2d in all_contours_cls_.items():
                    if slice_id in shifts_mapped.keys():

                        contour_2d += shifts_mapped[slice_id]
                        all_centers_cls_[slice_id] += shifts_mapped[slice_id]
                        if slice_id not in shifted_slices:  # Each slice should only be shifted once.
                            stack.slices[slice_id].image = shift_2d_replace(data=stack.slices[slice_id].image.squeeze(),
                                                                                dx=round(shifts_mapped[slice_id][0, 0].detach().numpy().item()),
                                                                                dy=round(shifts_mapped[slice_id][0, 1].detach().numpy().item()))
                            shifted_slices += [slice_id]

            stack.compute_volumes(filled=True)
            # for c, all_contours_cls_ in stack.all_contours.items():
            #     surface_points =[]
            #     for slice_id, contour_2d in all_contours_cls_.items():
            #         contour_2d = contour_2d.clone()
            #         res_z_xy_ratio = stack.get_dicom_resolution()[0,2]/stack.get_dicom_resolution()[0,1]
            #         z = res_z_xy_ratio * slice_id * torch.ones(contour_2d.shape[0])[:, None]
            #         contour_3d = torch.cat([contour_2d, z], dim=1)
            #         surface_points += [contour_3d]
            #     surface_points = torch.cat(surface_points, dim=0)
            #     save_to_obj(f'/cvlabdata2/cvlab/datasets_udaranga/outputs/overlay4_{c}.obj', surface_points[None], None, None)

            # surface_points ={}
            # for cls, contours in stack.points3d.items():
            #     for slice_id, slice_contour in contours.items():
            #         (contour_2d, _) = slice_contour
            #         if slice_id > 0:
            #             contour_2d += shifts[slice_id-1][None]
            #         z = 4.535147392290249 * slice_id * torch.ones(contour_2d.shape[0])[:, None]
            #         contour_3d = torch.cat([contour_2d, z], dim=1)
            #         if cls not in surface_points.keys():
            #             surface_points[cls] = []
            #         surface_points[cls] += [contour_3d]
            # for k in surface_points.keys():
            #     surface_points[k] = torch.cat(surface_points[k], dim=0)
            #     save_to_obj(f'/cvlabdata2/cvlab/datasets_udaranga/outputs/overlay5_{k}.obj', surface_points[k][None], None, None)

            # for shift_index, slice_id in enumerate(slice_ids[1:]):
            #     if self.shift_type == "interpolation":
            #         stack.slices[slice_id].image = shift(input=stack.slices[slice_id].image.squeeze(),
            #                               shift=np.flip(
            #                                   np.transpose(shifts[shift_index].unsqueeze(0).detach().numpy())))
            #         for contour in stack.slices[slice_id].contours:
            #             contour.points += shifts[shift_index][None]

            #     elif self.shift_type == "integer":
            #         shifts = shifts.detach()
            #         stack.slices[slice_id].image = shift_2d_replace(data=stack.slices[slice_id].image.squeeze(),
            #                                          dx=round(shifts[shift_index][0].numpy().item()),
            #                                          dy=round(shifts[shift_index][1].numpy().item()))
            #         for contour in stack.slices[slice_id].contours:
            #             contour.points += shifts[shift_index][None].numpy()
            #     else:
            #         raise Exception("Specify correct shift type from {}.".format(["interpolation", "integer"]))

if __name__ == "__main__":
    configs = load_yaml("./configs/registration_config.yaml")
    R = Registration(configs=configs,
                     registration_type=configs.CONTOUR_REGISTRATION.type,
                     mode=configs.DATA.mode,
                     shift_type=configs.CONTOUR_REGISTRATION.shift_type,
                     save_path=configs.PATHS.save_path,
                     save_slices=(configs.PLOT_AND_SAVE.save_unaligned, configs.PLOT_AND_SAVE.save_aligned),
                     plot_contours=configs.PLOT_AND_SAVE.plot_contours)
    R.registration()


from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtWebEngineWidgets import *
import sys

class MainWindow(QMainWindow):
    homePage  = 'https://google.com'
    def __init__(self):
        super(MainWindow, self).__init__()
        self.showMaximized()
        self.browser = QWebEngineView()
        self.browser.layout()
        self.browser.setUrl(QUrl('https://google.com'))
        self.setCentralWidget(self.browser)

        # browser navbar
        navbar = QToolBar()
        self.addToolBar(navbar)

        back_btn = QAction('<<', self)
        back_btn.triggered.connect(self.browser.back)
        navbar.addAction(back_btn)

        prev_btn = QAction('>>', self)
        prev_btn.triggered.connect(self.browser.forward)
        navbar.addAction(prev_btn)

        home_btn = QAction('Home', self)
        home_btn.triggered.connect(self.navigate_home)
        navbar.addAction(home_btn)
    def navigate_home(self):
        self.browser.setUrl(QUrl(self.homePage))

app = QApplication(sys.argv)
QApplication.setApplicationName('Brosify')
window = MainWindow()

app.exec()


import math

PI = math.pi

def dobra(lista):
    return [x * 2 for x in lista]

def multiplca(lista):
    r = 1
    for i in lista:
        r *= i
    return r

# Se quiser apenas testar o módulo
# Execultando ele, ao invés de importar
if __name__ == "__main__":
    lista = range(1, 6)
    print(dobra(lista))
    print(multiplca(lista))

Aulas/Udemy/01 - Intermediário/86 - Criando o próprio módulo/modulo.py


from typing import TypedDict
from datetime import datetime
from dateutil.tz import tzlocal

__workgroup_response_example = {
    "WorkGroup": {
        "Name": "ade",
        "State": "ENABLED",
        "Configuration": {
            "ResultConfiguration": {
                "OutputLocation": "s3://cdktoolkit-stagingbucket-58l2kih455z7/",
                "EncryptionConfiguration": {"EncryptionOption": "SSE_S3", "KmsKey": ""},
            },
            "EnforceWorkGroupConfiguration": False,
            "PublishCloudWatchMetricsEnabled": True,
            "RequesterPaysEnabled": False,
            "EngineVersion": {
                "SelectedEngineVersion": "AUTO",
                "EffectiveEngineVersion": "Athena engine version 3",
            },
        },
        "CreationTime": datetime(
            2022, 2, 23, 11, 26, 10, 427000, tzinfo=tzlocal()
        ),
    },
    "ResponseMetadata": {
        "RequestId": "b0ef4c30-c74c-4b8b-a1d8-2282b5476868",
        "HTTPStatusCode": 200,
        "HTTPHeaders": {
            "date": "Fri, 21 Jul 2023 12:47:39 GMT",
            "content-type": "application/x-amz-json-1.1",
            "content-length": "569",
            "connection": "keep-alive",
            "x-amzn-requestid": "b0ef4c30-c74c-4b8b-a1d8-2282b5476868",
        },
        "RetryAttempts": 0,
    },
}

class ResultConfiguration(TypedDict):
    OutputLocation: str
    EncryptionConfiguration: dict[str, str]

class Configuration(TypedDict):
    ResultConfiguration: ResultConfiguration
    EnforceWorkGroupConfiguration: bool
    PublishCloudWatchMetricsEnabled: bool
    RequesterPaysEnabled: bool
    EngineVersion: dict[str, str]

class WorkGroup(TypedDict):
    Name: str
    State: str
    Configuration: Configuration
    CreationTime: datetime

HTTPHeaders = TypedDict(
    "HTTPHeaders",
    {
        "date": str,
        "content-type": str,
        "content-length": str,
        "connection": str,
        "z-amzn-requestid": str,
    },
)

class ResponseMetadata(TypedDict):
    RequestId: str
    HTTPStatusCode: int
    HTTPHeaders: HTTPHeaders
    RetryAttempts: int

GetWorkGroupResponse = TypedDict(
    "GetWorkGroupResponse",
    {
        "WorkGroup": WorkGroup,
        "ResponseMetadata": ResponseMetadata,
    },
)

__start_query_execution_example = {
    "QueryExecutionId": "819fe841-f63a-4578-9c3c-2a3bb1609fa4",
    "ResponseMetadata": {
        "RequestId": "1ef961cf-da8a-4290-8e32-86dc6d9d4a0b",
        "HTTPStatusCode": 200,
        "HTTPHeaders": {
            "date": "Fri, 21 Jul 2023 12:29:50 GMT",
            "content-type": "application/x-amz-json-1.1",
            "content-length": "59",
            "connection": "keep-alive",
            "x-amzn-requestid": "1ef961cf-da8a-4290-8e32-86dc6d9d4a0b",
        },
        "RetryAttempts": 0,
    },
}

StartQueryExecutionResponse = TypedDict(
    "StartQueryExecutionResponse",
    {
        "QueryExecutionId": str,
        "ResponseMetadata": ResponseMetadata,
    }
)

ColumnInfoItem = TypedDict(
    "ColumnInfoItem",
    {
        "CatalogName": str,
        "SchemaName": str,
        "TableName": str,
        "Name": str,
        "Label": str,
        "Type": str,
        "Precision": int,
        "Scale": int,
        "Nullable": str,
        "CaseSensitive": bool,
    }
)

ResultSetMetadata = TypedDict(
    "ResultSetMetadata",
    {
        "ColumnInfo": list[ColumnInfoItem]
    }
)

ResultSet = TypedDict(
    "ResultSet",
    {
        "Rows": list,
        "ResultSetMetadata": ResultSetMetadata,
    }
)

GetQueryResultsResponse = TypedDict(
    "GetQueryResultsResponse",
    {
        "UpdateCount": int,
        "ResultSet": ResultSet,
        "ResponseMetadata": ResponseMetadata,
    }
)


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def leafSimilar(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool:
        def solution(node, arr) -> list:
            if node.left == None and node.right == None:
                arr.append(node.val)

            if node.left != None:
                solution(node.left, arr)
            if node.right != None:
                solution(node.right, arr)
            return arr
        return solution(root1, []) == solution(root2, [])


from flask import Flask, request, jsonify
import sklearn
import numpy as np
import pickle

model = pickle.load(open('model.pkl', 'rb'))

app = Flask(__name__)

@app.route('/')
def index():
    return "Hello World"

@app.route('/predict', methods=['POST'])
def predict():

   fulfill_via = request.form.get('fulfill_via')
   shipment_mode = request.form.get('shipment_mode')
   product_group = request.form.get('product_group')
   sub_classification = request.form.get('sub_classification')
   line_item_quantity = request.form.get('line_item_quantity')
   pack_price = request.form.get('pack_price')
   unit_price = request.form.get('unit_price')
   weight = request.form.get('weight')
   dosage_numerical = request.form.get('dosage_numerical')

   input_query = np.array([[fulfill_via, shipment_mode,
                             product_group, sub_classification, line_item_quantity, pack_price,
                             unit_price, weight, dosage_numerical]])

   result = model.predict(input_query)[0]
   return jsonify({"Prediction": result})

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


import random # importando biblioteca para gerar um numero pseudo-aleatorio

def jogar():

	print("\n*********************************")
	print("Bem vindo ao jogo de Adivinhação!")
	print("*********************************\n")

	numero_secreto = random.randrange(1,101) # Gera numero secreto de 1 a 100

	print("Qual o nivel de dificuldade?")
	print("(1) Facil \n(2) Medio \n(3) Dificil\n") # Quanto maior a dificulade, menos rodadas o usuario tera

	nivel = int(input("Defina o nivel: "))	# a funcao input sempre gera uma entrada do tipo string
											# como vamos comparar com um número inteiro, precisamos
											# fazer a conversao para que o programa funcione

	total_de_rodadas = 0
	rodada_atual = 1
	pontos = 1000

	if(nivel == 1):
		total_de_rodadas = 20
	elif(nivel == 2):
		total_de_rodadas = 10
	elif(nivel == 3):
		total_de_rodadas = 5

	for rodada_atual in range(1, (total_de_rodadas + 1)):

		chute = int(input("Digite um número entre 1 e 100: "))	# mesmo caso de conversao da entrada do usuario
																# para um numero inteiro

		print("Rodada {} de {}".format(rodada_atual,str(total_de_rodadas)))

		rodada_atual = rodada_atual + 1

		acertou = (chute == numero_secreto)
		maior   = chute > numero_secreto
		menor   = chute < numero_secreto

		if(chute < 1 or chute > 100):
			rodada_atual = (rodada_atual - 1) # como o numero eh invalido, a rodada nao eh utilizada
			print("Digite um numero entre 1 e 100!")
			continue

		if(acertou):
			print("Voce acertou! O número secreto era: ", numero_secreto)
			print("Sua pontuacao: {} pontos".format(pontos))
			break # se acertou, entao o jogo acabou
		else:
			pontos_perdidos = abs(numero_secreto - chute)	# quanto maior for a distancia absoluta do chute do usuario
															# ao numero secreto, mais pontos o jogador ira  perder
			pontos -= pontos_perdidos
			if(maior):
				print("Voce errou! O chute foi maior do que o número secreto")
			elif(menor):
				print("Voce errou! O chute foi menor do que o número secreto")

	print("Fim do jogo!")

if(__name__ == "__main__"):
	jogar()


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

# http://t.yushu.im/v2/book/isbn/9787501524044
URL_ISBN = 'http://t.yushu.im/v2/book/isbn/{}'
URL_KEY = 'http://t.yushu.im/v2/book/search?q={}&count={}&start={}'
PER_PAGE = 15

# config默认配置为True，导致jsonify得到的数据在浏览器上无法显示中文
# 或用json.dumps(mydict, ensure_ascii = False)
# 对于安装了JSONView插件的谷歌浏览器不影响吗，会自动呈现合适的形式。
JSON_AS_ASCII = False
BEAN_GIVEN_BY_SYSTEM_PER_BOOK = 0.5
BOOK_NUMBER_IN_HOMEPAGE = 30


from random import randint

random_start = randint(1, 10)
random_step = randint(1, 5)
random_end = randint(20, 100)
random_list = [x for x in range(random_start, random_end, random_step)]

# collection = input('Введите числа: ')

def evens_and_odds(*collection):
    evens = []
    odds = []

    for number in collection:
        if number % 2 == 0:
            evens.append(str(number))
        else:
            odds.append(str(number))

    print(f'Список чисел: {", ".join(map (str, collection))}')
    if odds and evens:
        print()
        print(f'Четные числа: {", ".join(evens)}')
        print(f'Нечетные числа: {", ".join(odds)}')
    elif odds and not evens:
        print()
        print(f'Четных чисел нет!')
        print(f'Нечетные числа: {", ".join(odds)}')
    elif evens and not odds:
        print()
        print(f'Четные числа: {", ".join(evens)}')
        print(f'Нечетных чисел нет!')

evens_and_odds(4, 5, 4786, 23165798, 98763251465, 22057)


"""
A wrapper around some evelink classes

This module instantiates classes in evelink that has the `api` argument
as optional and provides an object for use.
"""

from __future__ import absolute_import

import itertools
from time import time
from xml.etree import ElementTree

import zope.interface
import zope.component
import evelink
from evelink.cache.sqlite import SqliteCache

from mtj.eve.tracker.interfaces import IAPIHelper, IEvelinkCache

class API(evelink.api.API):
    def __init__(self, *a, **kw):
        if len(a) < 2 and not 'cache' in kw:
            kw['cache'] = zope.component.queryUtility(IEvelinkCache)
        return super(API, self).__init__(*a, **kw)

class Corp(evelink.corp.Corp):

    def starbases(self):
        """
        Copy of the parent, except state is not converted to string,
        and the keys are identical to the api.
        """

        api_result = self.api.get('corp/StarbaseList')

        rowset = api_result.result.find('rowset')
        results = {}
        for row in rowset.findall('row'):
            a = row.attrib
            starbase = {
                'itemID': int(a['itemID']),
                'typeID': int(a['typeID']),
                'locationID': int(a['locationID']),
                'moonID': int(a['moonID']),
                'state': int(a['state']),
                'stateTimestamp': evelink.api.parse_ts(a['stateTimestamp']),
                'onlineTimestamp': evelink.api.parse_ts(a['onlineTimestamp']),
                'standingOwnerID': int(a['standingOwnerID']),
            }
            results[starbase['itemID']] = starbase

        return evelink.api.APIResult(results,
            api_result.timestamp, api_result.expires)

class UtilityAPICache(evelink.api.APICache):
    """
    Provide them with the ability to make use of the registered cache
    utility it is available.
    """

    def _get_cache(self):
        cache_util = zope.component.queryUtility(IEvelinkCache)
        if cache_util:
            return cache_util
        return super(UtilityAPICache, self)

    def get(self, *a, **kw):
        return self._get_cache().get(*a, **kw)

    def put(self, *a, **kw):
        return self._get_cache().put(*a, **kw)

@zope.interface.implementer(IEvelinkCache)
class EvelinkSqliteCache(SqliteCache):
    """
    For a zope component registered cache class extending from the
    default sqlite cache.
    """

    max_error_cache_duration = 3540  # an hour less one minute

    def put(self, key, value, duration):
        try:
            tree = ElementTree.fromstring(value)
        except ElementTree.ParseError:
            # Let this continue without issues.
            return super(EvelinkSqliteCache, self).put(key, value, duration)

        if tree.find('error') is not None:
            duration = min(duration, self.max_error_cache_duration)
        return super(EvelinkSqliteCache, self).put(key, value, duration)

@zope.interface.implementer(IAPIHelper)
class Helper(object):
    """
    API helper class

    Provides some methods that helps with providing data from the API to
    the pos tracker.
    """

    api_cache = None

    # control internal refresh, to prevent polling the cache and/or make
    # api call.
    refresh_limit = 300  # 5 minutes
    refresh_time = 0

    def __init__(self):
        cache = zope.component.queryUtility(
            IEvelinkCache, default=UtilityAPICache())
        api = API(cache=cache)
        self.map = evelink.map.Map(api=api)
        self.eve = evelink.eve.EVE(api=api)
        self.refresh()

    def refresh(self):
        """
        Clears the cached results within the helper

        The next call will obviously get from the API.
        """

        if self.refresh_time + self.refresh_limit > time():
            return

        self.refresh_time = time()
        self._alliances = None
        self._corporations = None
        self._sov = None

    @property
    def alliances(self):
        if not self._alliances:
            self._alliances = self.eve.alliances()
        return self._alliances.result

    @property
    def corporations(self):
        if not self._corporations:
            self._corporations = dict(itertools.chain(*[
                    [(j, i[0]) for j in i[1]['member_corps']]
                for i in self.alliances.iteritems()]))

        return self._corporations

    @property
    def sov(self):
        if not self._sov:
            self._sov, self._sov_timestamp = self.map.sov_by_system().result
        return self._sov


from keras.models import load_model
import numpy as np
import pandas as pd
from numpy.random import seed
import matplotlib.pyplot as plt
from sklearn.manifold import TSNE
import seaborn as sns
# 1. Density Plot
pred = pd.DataFrame(data = loaded_array, columns=predcolumns)

pred.insert(0,'SMILES',smiles)

pred.insert(1,"clusters",labels)

s=[]
i=0
index = []

for j in range(0,50):
  s = pred.loc[pred['clusters']==j,'SMILES']
  s = s.to_list()
  for s2 in s:
    i = i+1
  index.append(i)
  print(i)
  i = 0

###################################

plt.rcParams.update({'font.size': 22})

x = []
for i in range(1,51):
    x.append(i)

font = {'size'   : 22}
fig = plt.figure(figsize=(15,10))
ax = fig.add_axes([0,0,1,1])
langs = x
students = index
ax.bar(langs,students)
ax.set_ylabel('Number of molecules in each cluster\n', fontsize = 34)
ax.set_xlabel('\nNumber of clusters ', fontsize = 34)
ax.set_xticks([k for k in range(1,51,5)])
#ax.set_yticks( fontsize = 20)
ax.set_title('', fontsize = 14)

plt.show()

#######################################################################

# 2. t-SNE plot
tsne_comp = TSNE(n_components=2, perplexity=30,random_state=30, n_iter=1000).fit_transform(loaded_array)

tsne_df = pd.DataFrame(data = tsne_comp, columns =['t-SNE1','t-SNE2'])
tsne_df.head()

tsne_df = pd.concat([tsne_df,pd.DataFrame({'cluster':labels})], axis = 1)
tsne_df['cluster']+=1
tsne_df.head()

text = []
for i in range (1,51):
  text.append(str(i))
len(text)

plt.figure(figsize=(25,20))
sns.set(font_scale=3)
z = sns.color_palette("coolwarm", as_cmap=True)
ax = sns.scatterplot(x="t-SNE1", y="t-SNE2", hue = "cluster", data = tsne_df, palette=z)
#ax = sns.color_palette("mako", as_cmap=True)
x = tsne_df['t-SNE1']
y = tsne_df['t-SNE2']
for i in range(0, 50):

  plt.annotate(text[i], (x[i], y[i]+.2 ) ,size=22, color='black', weight='bold')
#plt.legend(bbox_to_anchor=(1.02, 1), loc='upper left', borderaxespad=0, ncol = 3 )

plt.show()


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import plotly.offline as py
import plotly.graph_objs as go
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn import metrics
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans

AREA_POPULATION = 'Area Population'
AVG_AREA_NUMBER_OF_BEDROOMS = 'Avg. Area Number of Bedrooms'
AVG_AREA_NUMBER_OF_ROOMS = 'Avg. Area Number of Rooms'
AVG__AREA_HOUSE_AGE = 'Avg. Area House Age'
AVG__AREA_INCOME = 'Avg. Area Income'
PRICE = 'Price'
USA_HOUSING_FILE_PATH = 'USA_Housing.csv'
COUNTRY_WISE_TEMPERATURE_FILE_PATH = 'climatechange/GlobalLandTemperaturesByCountry.csv'
GLOBAL_TEMPERATURES_CSV = "climatechange/GlobalTemperatures.csv"

def read_data(path):
    return pd.read_csv(path)

def shape_data(path):
    data = path.shape
    return data

def show_data(path):
    data_set = read_data(path)
    print(data_set)
    shaped_data_set = shape_data(data_set)
    described_data_set = data_set.describe(include="all")
    missing_values_data_set = data_set.isna().sum()
    replaced_missing_values_data_set = data_set.replace(np.nan, 0)
    dropped_data_set = replaced_missing_values_data_set.dropna()
    dropped_data_set.describe(include="all")
    dropped_data_set.head()
    dropped_data_set.info()
    dropped_data_set.describe()
    print(shaped_data_set)
    print(described_data_set)
    print(missing_values_data_set)
    print(replaced_missing_values_data_set)
    print(dropped_data_set)

def house_pricing_analysis_of_a_country(path):
    usa_housing_df = read_data(path)
    usa_housing_df.head()
    usa_housing_df.info()
    usa_housing_df.describe()
    sns.distplot(usa_housing_df[PRICE])
    sns.heatmap(usa_housing_df.corr(), annot=True, cmap='coolwarm')
    x = usa_housing_df[[AVG__AREA_INCOME, AVG__AREA_HOUSE_AGE, AVG_AREA_NUMBER_OF_ROOMS,
                        AVG_AREA_NUMBER_OF_BEDROOMS, AREA_POPULATION]]
    y = usa_housing_df[PRICE]
    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.4, random_state=101)
    linear_regression = LinearRegression()
    linear_regression.fit(x_train, y_train)
    print(linear_regression.intercept_)
    cdf = pd.DataFrame(linear_regression.coef_, x.columns, columns=['Coeff'])
    predictions = linear_regression.predict(x_test)
    plt.figure(figsize=(16, 8))
    plt.scatter(y_test, predictions)
    plt.show()
    plt.figure(figsize=(16, 8))

    sns.distplot((y_test - predictions), bins=50)
    plt.show()

    print('MAE:', metrics.mean_absolute_error(y_test, predictions))
    print('MSE:', metrics.mean_squared_error(y_test, predictions))
    print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test, predictions)))

    data = make_blobs(n_samples=200, n_features=2,
                      centers=4, cluster_std=1.8, random_state=101)
    plt.scatter(data[0][:, 0], data[0][:, 1], c=data[1], cmap='rainbow')

    kmeans = KMeans(n_clusters=4)
    kmeans.fit(data[0])
    kmeans.cluster_centers_
    kmeans.labels_
    f, (ax1, ax2) = plt.subplots(1, 2, sharey=True, figsize=(10, 6))
    ax1.set_title('K Means')
    ax1.scatter(data[0][:, 0], data[0][:, 1], c=kmeans.labels_, cmap='rainbow')
    ax2.set_title("Original")
    ax2.scatter(data[0][:, 0], data[0][:, 1], c=data[1], cmap='rainbow')

def global_warming_analysis(file_path):
    global_land_temp_country_wise = read_data(file_path)
    glbl_temp_cntry_clean = global_land_temp_country_wise[~global_land_temp_country_wise['Country'].isin(
        ['Denmark', 'Antarctica', 'France', 'Europe', 'Netherlands', 'United Kingdom', 'Africa', 'South America'])]
    glbl_temp_cntry_clean = glbl_temp_cntry_clean.replace(
        ['Denmark (Europe)', 'France (Europe)', 'Netherlands (Europe)', 'United Kingdom (Europe)'],
        ['Denmark', 'France', 'Netherlands', 'United Kingdom'])
    # Average temperature for each country
    countries = np.unique(glbl_temp_cntry_clean['Country'])
    mean_temp_list = []
    for country in countries:
        mean_temp_list.append(
            glbl_temp_cntry_clean[glbl_temp_cntry_clean['Country'] == country]['AverageTemperature'].mean())
    # Creating figure using 'plotly' library
    data = [dict(type='choropleth', locations=countries, z=mean_temp_list, locationmode='country names', text=countries,
                 marker=dict(line=dict(color='rgb(0,0,0)', width=1)),
                 colorbar=dict(autotick=True, tickprefix='', title='# Mean\nTemperature,\n°C'))]
    layout = dict(title='Mean land temperature in countries',
                  geo=dict(showframe=False, showocean=True, oceancolor='rgb(0,255,255)',
                           projection=dict(type='orthographic', rotation=dict(lon=60, lat=10), ),
                           lonaxis=dict(showgrid=True, gridcolor='rgb(102, 102, 102)'),
                           lataxis=dict(showgrid=True, gridcolor='rgb(102, 102, 102)')), )
    fig = dict(data=data, layout=layout)
    py.plot(fig, validate=False, filename='worldmap')
    # 'seaborn' as well as 'matplotlib' library is used to create horizontal bars for each countries
    mean_temp_bar_graph, countries_bar_graph = (list(x) for x in
                                                zip(*sorted(zip(mean_temp_list, countries), reverse=True)))
    sns.set(font_scale=0.9)
    f, ax = plt.subplots(figsize=(4.5, 50))
    colors_cw = sns.color_palette('coolwarm', len(countries))
    sns.barplot(mean_temp_bar_graph, countries_bar_graph, palette=colors_cw[::-1])
    ax.set(xlabel='Mean temperature', title='Mean land temperature in countries')

    glbl_temp = pd.read_csv(GLOBAL_TEMPERATURES_CSV)
    # Extract the year from a date
    years = np.unique(glbl_temp['dt'].apply(lambda x: x[:4]))
    mean_temp_wrld = []
    mean_temp_wrld_uncertainty = []
    for year in years:
        mean_temp_wrld.append(
            glbl_temp[glbl_temp['dt'].apply(lambda x: x[:4]) == year]['LandAverageTemperature'].mean())
        mean_temp_wrld_uncertainty.append(
            glbl_temp[glbl_temp['dt'].apply(lambda x: x[:4]) == year]['LandAverageTemperatureUncertainty'].mean())
    trace_0 = go.Scatter(x=years, y=np.array(mean_temp_wrld) + np.array(mean_temp_wrld_uncertainty), fill=None,
                         mode='lines', name='Uncertainty top', line=dict(color='rgb(0, 255, 255)', ))
    trace_1 = go.Scatter(x=years, y=np.array(mean_temp_wrld) - np.array(mean_temp_wrld_uncertainty), fill='tonexty',
                         mode='lines', name='Uncertainty bot', line=dict(color='rgb(0, 255, 255)', ))
    trace_2 = go.Scatter(x=years, y=mean_temp_wrld, name='Mean Temperature', line=dict(color='rgb(199, 121, 093)', ))
    data = [trace_0, trace_1, trace_2]
    layout = go.Layout(xaxis=dict(title='year'), yaxis=dict(title='Mean Temperature, °C'),
                       title='Mean land temperature in world', showlegend=False)
    fig = go.Figure(data=data, layout=layout)
    py.plot(fig)
    continents = ['China', 'United States', 'Niger', 'Greenland', 'Australia', 'Italy']
    mean_temp_year_country = [[0] * len(years[70:]) for i in range(len(continents))]
    j = 0
    for country in continents:
        all_temp_country = glbl_temp_cntry_clean[glbl_temp_cntry_clean['Country'] == country]
        i = 0
        for year in years[70:]:
            mean_temp_year_country[j][i] = all_temp_country[all_temp_country['dt'].apply(lambda x: x[:4]) == year][
                'AverageTemperature'].mean()
            i += 1
        j += 1
    traces = []
    colors = ['rgb(0, 255, 255)', 'rgb(255, 0, 255)', 'rgb(0, 0, 0)', 'rgb(255, 0, 0)', 'rgb(0, 255, 0)',
              'rgb(0, 0, 255)']
    for i in range(len(continents)): traces.append(
        go.Scatter(x=years[70:], y=mean_temp_year_country[i], mode='lines', name=continents[i],
                   line=dict(color=colors[i]), ))
    layout = go.Layout(xaxis=dict(title='year'), yaxis=dict(title='Mean Temperature, °C'),
                       title='Mean land temperature on the continents', )
    fig = go.Figure(data=traces, layout=layout)
    py.plot(fig)

if __name__ == "__main__":
    # printing and showing data description which is used for analysis further.
    # It will help in understanding the data better
    show_data(USA_HOUSING_FILE_PATH)
    # house pricing analysis using supervised and unsupervised learning
    house_pricing_analysis_of_a_country(USA_HOUSING_FILE_PATH)

    # printing and showing data description which is used for analysis further.
    # It will help in understanding the data better
    show_data(COUNTRY_WISE_TEMPERATURE_FILE_PATH)
    show_data(GLOBAL_TEMPERATURES_CSV)
    # global warming analysis country wise
    global_warming_analysis(COUNTRY_WISE_TEMPERATURE_FILE_PATH)