from tkinter.tix import Tree
from kivy.uix.floatlayout import FloatLayout
from kivy.lang import Builder
from kivy.properties import NumericProperty, ListProperty
from cls.ball import Ball
from functools import partial
from kivy.animation import Animation
from kivy.vector import Vector

Builder.load_string(
    """
<MainField>

    canvas.before:

        Color:
            rgba: root.border_color

        Rectangle: # left
            pos: self.pos
            size: root.border_width, self.height

        Rectangle: # top
            pos: self.x, self.top - root.border_width
            size: self.width, root.border_width

        Rectangle: # right
            pos: self.right - self.border_width, self.y
            size: root.border_width, self.height

        Rectangle: # bottom
            pos: self.pos
            size: self.width, root.border_width

""")

class MainField(FloatLayout):
    border_color = ListProperty([1, 0, 1, 1])
    border_width = NumericProperty("30dp")
    ball_size = NumericProperty("50dp")
    speed = NumericProperty("0.2") #seconds per 100px

    initial_pos = []
    last_pos = []
    ball = None
    dynamic_speed = None
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)

    def prepare_scene(self, touch):
        self.clear_widgets()
        ball_x = touch.x - self.ball_size/2
        ball_y = touch.y - self.ball_size/2
        self.ball = Ball(ball_size=self.ball_size, pos=[ball_x, ball_y])
        self.add_widget(self.ball)

    def on_touch_down(self, touch):
        if self.ball:
            Animation.cancel_all(self.ball)
        self.prepare_scene(touch)
        self.initial_pos = touch.pos

    def on_touch_up(self, touch):
        dx, dy = self.initial_pos
        x, y = touch.pos
        distance = Vector(self.initial_pos).distance(touch.pos)
        if distance==0:
            return True
        self.dynamic_speed = (100/distance)*self.speed
        collision_point = self.get_collision_point([dx, dy], [x, y])
        bounce_point = self.get_bounce_collision_point(collision_point[0], collision_point[1], [dx, dy])
        self.move_ball_anim(collision_point, bounce_point)

    def move_ball_anim(self, collision_point, bounce_point):
        wall = collision_point[1]
        x, y = collision_point[0]
        x_buffer = 0
        y_buffer = 0
        if wall=="t":
            y_buffer = -self.ball_size
        if wall=="r":
            x_buffer = -self.ball_size
        if wall=="b":
            y_buffer = 0
        if wall=="l":
            x_buffer = 0

        dis = Vector(collision_point[0]).distance(self.ball.pos)
        t = (dis/100)*self.dynamic_speed
        next_bounce_point = self.get_bounce_collision_point(bounce_point[0], bounce_point[1], collision_point[0])
        anim = Animation(pos = [x+x_buffer, y+y_buffer], d= t)
        anim.bind(on_complete=lambda *args: self.move_ball_anim(bounce_point, next_bounce_point))
        anim.start(self.ball)

    def get_collision_point(self, p1, p2):
        x, y = p2
        dx, dy = p1

        line = partial( self.two_line_intersection, [x, y], [dx, dy] )
        initial_dir= None
        if x>dx:
            if y>dy:
                initial_dir = "tr"
            else:
                initial_dir = "lr"
        else:
            if y>dy:
                initial_dir = "tl"
            else:
                initial_dir = "ll"

        field_cords = self.get_main_field_pos()
        collision_point = None
        collision_wall = None

        intersection_r = line(field_cords["tr"], field_cords["lr"])
        intersection_t = line(field_cords["tr"], field_cords["tl"])
        intersection_b = line(field_cords["ll"], field_cords["lr"])
        intersection_l = line(field_cords["ll"], field_cords["tl"])

        if initial_dir=="tr":
            if intersection_r[1] < field_cords["tr"][1]:
                collision_point = intersection_r
                collision_wall = "r"
            else:
                collision_point = intersection_t
                collision_wall = "t"

        if initial_dir=="lr":
            if intersection_r[1] > field_cords["lr"][1]:
                collision_point = intersection_r
                collision_wall = "r"
            else:
                collision_point = intersection_b
                collision_wall = "b"

        if initial_dir=="ll":
            if intersection_l[1] > field_cords["ll"][1]:
                collision_point = intersection_l
                collision_wall = "l"
            else:
                collision_point = intersection_b
                collision_wall = "b"

        if initial_dir=="tl":
            if intersection_l[1] < field_cords["tl"][1]:
                collision_point = intersection_l
                collision_wall = "l"
            else:
                collision_point = intersection_t
                collision_wall = "t"

        return [collision_point, collision_wall]

    def get_bounce_collision_point(self, collision_point, collision_wall, dpoint):
        dx, dy = dpoint
        second_point = []
        diff_x = collision_point[0] - dx
        diff_y = collision_point[1] - dy
        if collision_wall=="t" or collision_wall=="b":
            second_point = [collision_point[0] + diff_x, dy]
        elif collision_wall=="r" or collision_wall=="l":
            second_point = [dx, collision_point[1]+ diff_y]

        new_collision_point = self.get_collision_point(collision_point, second_point)
        return new_collision_point

    def get_main_field_pos(self):
        return {
            "ll": [self.x + self.border_width, self.y + self.border_width],
            "tl": [self.x + self.border_width, self.top - self.border_width],
            "tr": [self.right - self.border_width, self.top - self.border_width],
            "lr": [self.right - self.border_width, self.y + self.border_width],
        }

    def two_line_intersection(self, p0, p1, p2, p3):
        try:
            m1 = (p1[1] - p0[1])/(p1[0] - p0[0])
        except:
            m1 = 10000000000
        try:
            m2 = (p3[1] - p2[1]) / (p3[0] - p2[0])
        except:
            m2 = 10000000000
        try:
            x = (p2[1] + m1*p0[0] - p0[1] - m2*p2[0]) / (m1-m2)
            y = m1*x - m1*p0[0] + p0[1]
        except ZeroDivisionError:
            return(0,0)

        return (x, y)


# Runs the job

from lib.facebook.facebook import Facebookface
import datetime

now = datetime.datetime.now()
fb = Facebookface()

print("############ It is {}     ##########".format(now.strftime("%m/%d/%Y %H:%M")))
print("########### Begin Process ##########")
fb.send_message()
print("########### End Process ##########")


"""
Faça um vetor de tamanho 50 preenchido com o seguinte valor: (i + 5 * i) % (i + 1), sendo i a posição do elemento no vetor. Em seguida imprima o vetor na tela.

"""
vetor = []

for i in range(1, 51): # Gera um vetor de 50 posições, começando pelo 1 e terminando em 50, ignorando o 51.
    result = (i + 5 * i) % (i + 1) # Para cada item, substitua o i por sua posição dentro do vetor.
    vetor.append(result) # Adiciona cada resultado no vetor vazio 'Vetor'.

print(vetor)


from base import *

def reduce_mem_usage(df):
    """ iterate through all the columns of a dataframe and modify the data type
        to reduce memory usage.
    """
    start_mem = df.memory_usage().sum()
    print('Memory usage of dataframe is {:.2f} MB'.format(start_mem/(1024*1024)))
    numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
    for col in df.columns:
        col_type = df[col].dtype
        if col_type in numerics:
            c_min = df[col].min()
            c_max = df[col].max()
            if pd.isnull(c_min) or pd.isnull(c_max):
                continue
            if str(col_type)[:3] == 'int':
                if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:
                    df[col] = df[col].astype(np.int8)
                elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:
                    df[col] = df[col].astype(np.int16)
                elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:
                    df[col] = df[col].astype(np.int32)
                elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:
                    df[col] = df[col].astype(np.int64)
            else:
                if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:
                    df[col] = df[col].astype(np.float16)
                elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:
                    df[col] = df[col].astype(np.float32)
                else:
                    df[col] = df[col].astype(np.float64)

    end_mem = df.memory_usage().sum()
    print('Memory usage after optimization is: {:.2f} MB'.format(end_mem/(1024*1024)))
    print('Decreased by {:.1f}%'.format(100 * (start_mem - end_mem) / start_mem))
    return df

def multi_category_focal_loss2(gamma=2., alpha=.25):
    """
    focal loss for multi category of multi label problem
    适用于多分类或多标签问题的focal loss
    alpha控制真值y_true为1/0时的权重
        1的权重为alpha, 0的权重为1-alpha
    当你的模型欠拟合，学习存在困难时，可以尝试适用本函数作为loss
    当模型过于激进(无论何时总是倾向于预测出1),尝试将alpha调小
    当模型过于惰性(无论何时总是倾向于预测出0,或是某一个固定的常数,说明没有学到有效特征)
        尝试将alpha调大,鼓励模型进行预测出1。
    Usage:
     model.compile(loss=[multi_category_focal_loss2(alpha=0.25, gamma=2)], metrics=["accuracy"], optimizer=adam)
    """
    epsilon = 1.e-7
    gamma = float(gamma)
    alpha = tf.constant(alpha, dtype=tf.float32)

    def multi_category_focal_loss2_fixed(y_true, y_pred):
        y_true = tf.cast(y_true, tf.float32)
        y_pred = tf.clip_by_value(y_pred, epsilon, 1. - epsilon)

        alpha_t = y_true * alpha + (tf.ones_like(y_true) - y_true) * (1 - alpha)
        y_t = tf.multiply(y_true, y_pred) + tf.multiply(1 - y_true, 1 - y_pred)
        ce = -tf.math.log(y_t)
        weight = tf.pow(tf.subtract(1., y_t), gamma)
        fl = tf.multiply(tf.multiply(weight, ce), alpha_t)
        loss = tf.reduce_mean(fl)
        return loss

    return multi_category_focal_loss2_fixed


import h5py
import cv2
import os
import os.path as osp
import numpy as np
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-p', '--path', type=str, required=True, help="path to h5 result file")
parser.add_argument('-d', '--frm-dir', type=str, required=True, help="path to frame directory")
parser.add_argument('-i', '--idx', type=int, default=0, help="which key to choose")
parser.add_argument('--fps', type=int, default=30, help="frames per second")
parser.add_argument('--width', type=int, default=640, help="frame width")
parser.add_argument('--height', type=int, default=480, help="frame height")
parser.add_argument('--save-dir', type=str, default='log', help="directory to save")
parser.add_argument('--save-name', type=str, default='summary.mp4', help="video name to save (ends with .mp4)")
args = parser.parse_args()

def frm2video(frm_dir, summary, vid_writer):
    for idx, val in enumerate(summary):
        if val == 1:
            # here frame name starts with '000001.jpg'
            # change according to your need
            frm_name = str(idx+1).zfill(6) + '.jpg'
            frm_path = osp.join(frm_dir, frm_name)
            frm = cv2.imread(frm_path)
            frm = cv2.resize(frm, (args.width, args.height))
            vid_writer.write(frm)

if __name__ == '__main__':
    if not osp.exists(args.save_dir):
        os.mkdir(args.save_dir)
    vid_writer = cv2.VideoWriter(
        osp.join(args.save_dir, args.save_name),
        cv2.VideoWriter_fourcc(*'MP4V'),
        args.fps,
        (args.width, args.height),
    )
    h5_res = h5py.File(args.path, 'r')
    key = h5_res.keys()[args.idx]
    summary = h5_res[key]['machine_summary'][...]
    h5_res.close()
    frm2video(args.frm_dir, summary, vid_writer)
    vid_writer.release()


from os import listdir
from os.path import join, isfile

import torch
import collections
import pdb
import torch.utils.data
import csv
import json
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils
import math
import pandas as pd
import os
import numpy as np

DATA_DIR = "./data/"
OUTPUT_DIR = "./processed_data/"

def clean_rna_data():
    esc_rna = pd.read_csv(DATA_DIR + "GSE109262_ESC_RNA_counts.txt", sep='\t')
    cols = esc_rna.columns

    valid_genes = []
    for idx, row in esc_rna.iterrows():
        gene = row["ESC_ens_id"]
        cnt = 0
        for col in cols:
            if col != "ESC_ens_id":
                cnt += row[col]
        if cnt > 1000:
            valid_genes.append(gene)

    esc_rna_out = esc_rna.loc[esc_rna["ESC_ens_id"].isin(valid_genes)]
    print(len(valid_genes))
    print(esc_rna_out.shape[0])
    esc_rna_out.to_csv(DATA_DIR + "RNA_counts_cleaned.txt", sep='\t', index=False)

def load_rna_counts():
    esc_rna = pd.read_csv(DATA_DIR + "RNA_counts_cleaned.txt", sep='\t')
    esc_rna_counts = {}

    cols = esc_rna.columns

    for idx, row in esc_rna.iterrows():
        gene = row["ESC_ens_id"]
        if gene not in esc_rna_counts:
            esc_rna_counts[gene] = {}
        for col in cols:
            if col != "ESC_ens_id":
                esc_rna_counts[gene][col] = row[col]

    return esc_rna_counts

def get_expr_data():
    cnt_dict = {}
    esc_rna_counts = load_rna_counts()
    max_in_gene = {}
    expressed_count = {}
    for gene in esc_rna_counts:
        for col in esc_rna_counts[gene]:
            if col not in cnt_dict:
                cnt_dict[col] = {}
            cnt_dict[col][gene] = esc_rna_counts[gene][col]
            if gene not in max_in_gene:
                max_in_gene[gene] = 0
            max_in_gene[gene] = max(max_in_gene[gene], esc_rna_counts[gene][col])

    for col in cnt_dict:
        file = open(OUTPUT_DIR + "Cell_"+col+".expr.csv", "w+")
        data = sorted(cnt_dict[col].items())
        for gene, cnt in data:
            if cnt >  max_in_gene[gene] / 4:
                value = 1
                if col not in expressed_count:
                    expressed_count[col] = 0
                expressed_count[col] += 1
            else:
                value = 0
            file.write(gene + "," + str(value) + "\n")

    cnts = np.array([v for k, v in expressed_count.items()])

    print(cnts.mean())
    print(cnts.max())
    print(cnts.min())

    # see this!
    # get top k cells
    arr = (sorted(expressed_count, key=expressed_count.get, reverse=True)[:10])
    print(arr)

def analyze_expr_data():
    cnt_dict = {}
    esc_rna_counts = load_rna_counts()
    for gene in esc_rna_counts:
        for col in esc_rna_counts[gene]:
            cnt_dict[col] = 0
        break

    for gene in esc_rna_counts:
        for col in esc_rna_counts[gene]:
            cnt_dict[col] += esc_rna_counts[gene][col]

    cnts = np.array([v for k, v in cnt_dict.items()])

    print(cnt_dict)
    print(cnts.mean())
    print(cnts.max())
    print(cnts.min())

    # get top k cells
    arr = (sorted(cnt_dict, key=cnt_dict.get, reverse=True)[:10])
    print(arr)

    files = [f for f in listdir(DATA_DIR+"/GSE109262_TSV") if f.endswith(".tsv")]

    for k in arr:
        for file in files:
            if file.find(k) != -1:
                print(k, cnt_dict[k], file)
                break

def clean_tss_data():
    esc_rna_counts = load_rna_counts()

    tss_data = pd.read_csv(DATA_DIR + "TSS.tsv", sep='\t')
    tss_data = tss_data.sort_values(['Gene stable ID', 'Transcription start site (TSS)', 'Chromosome/scaffold name'])
    previous_gene = ""
    data = {"gene": [], "tss": [], "chrom": []}
    for idx, row in tss_data.iterrows():
        gene = row["Gene stable ID"]
        if gene == previous_gene:
            continue
        if gene not in esc_rna_counts:
            continue
        tss = int(row["Transcription start site (TSS)"])
        chrom = row["Chromosome/scaffold name"]
        previous_gene = gene
        if chrom not in [str(x) for x in range(0, 20)]:
            continue
        data["tss"].append(tss)
        data["gene"].append(gene)
        data["chrom"].append(chrom)

    df = pd.DataFrame.from_dict(data)
    df = df.sort_values(["gene", "tss", "chrom"])
    df.to_csv(DATA_DIR + "TSS_clean.csv", sep='\t')

# clean_rna_data()
# get_expr_data()
# clean_tss_data()
analyze_expr_data()
# clean_rna_data()


from .base_service import HC2BaseService
from .api_room import HC2APIRoom

class HC2RoomService(HC2BaseService):

    def __init__(self, args=None, logger=None):
        super(HC2RoomService,self).__init__(args,logger)

    def get_rooms(self):
        """return hc2 /api/rooms"""

        hc2_room_api = HC2APIRoom(self.args, self.logger)
        t_rooms = hc2_room_api.get(key=None)
        if t_rooms is None:
            self.logger.warning('hc2 rooms api for all rooms call fail')

        return t_rooms

if __name__ == '__main__':
    HC2RoomService.main()


class Solution(object):

    def getKth(self, a1, a2, k):
        if len(a1) > len(a2):
            a1, a2 = a2, a1
        if len(a1) == 0:
            return a2[k-1]
        if k == 1:
            return min(a1[0], a2[0])
        p1 = min(k/2, len(a1))
        p2 = k - p1
        if a1[p1-1] < a2[p2-1]:
            return self.getKth(a1[p1:], a2, p2)
        else:
            return self.getKth(a1, a2[p2:], p1)

    def findMedianSortedArrays(self, A, B):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: float
        """
        lenA = len(A); lenB = len(B)
        if (lenA + lenB) % 2 == 1:
            return self.getKth(A, B, (lenA + lenB)/2 + 1)
        else:
            return (self.getKth(A, B, (lenA + lenB)/2) + self.getKth(A, B, (lenA + lenB)/2 + 1)) * 0.5


import json
import re
import spacy

FILENAME = 'data/train.json' # Change this to be the JSON from the Recipe1M+ dataset
f = open(FILENAME)
recipes = json.load(f)
f.close()

print(len(recipes), "Recipes Loaded")

f = open('ingredient_map.json')
ing_map = json.load(f)
f.close()

print(len(ing_map), "mappings Loaded")

ing_dict = {}       # Track Reduced Ingredients and Frequency
# Counters
i = 0               # 10k Recipes Processed
j = 0               # 100k Recipes Processed
k = 0

# Set Up Spacy
# gpu = spacy.prefer_gpu()
# print('GPU:', gpu)
nlp = spacy.load("en_core_web_sm")
STOPWORDS = [   'teaspoon','teaspoons', 'tsp',
                'cup', 'cups', 'c.',
                'tablespoon', 'tablespoons', 'tbsp', 'tbs',
                'ounce', 'ounces',
                'quart', 'quarts',
                ' ',
                'lb', 'lbs', 'pound',
                'pinch', 'pinches',
                'finely', 'coarsely', 'thinly', 'to', 'taste',
                'can', 'cans', 'pkg', 'package', 'packages', 'jar',
                'sifted', 'melted', 'softened',
                'small', 'medium', 'large',
                'firmly', 'packed'
                # 'chopped', 'minced', 'sliced', 'diced'       #   *** Controversial ***
            ]

for recipe in recipes:
    ingredients = recipe['ingredients']
    for ingredient in ingredients:
        text = ingredient['text']
        text = re.sub(r'\([^)]*\)', '', text)           # Remove text within parenthesis
        doc = nlp(text)
        ing = []
        for token in doc:
            if token.pos_ not in ['NUM', 'PUNCT', 'CCONJ']:
                if token.lower_ not in STOPWORDS:
                    ing.append(token.lower_)
        # Reassemble Ingredient Name and Add to Dictionary
        ing = ' '.join(ing).strip()
        # If key in mapping, replace with reduced version
        if ing in ing_map.keys():
          ing = ing_map[ing]
        if ing in ing_dict:
            ing_dict[ing] += 1
        else:
            ing_dict[ing] = 1

    # Print Progress Status to Command Line
    i += 1
    if i > 10000:
        i = 0
        j += 1
        print("10k Recipes Processed")
        if j >= 10:
            j = 0
            k += 1
            print("100k Recipes Processed")
            out_json = json.dumps(dict(sorted(ing_dict.items(), key=lambda x: -x[1])[:999]))
            with open("ingredients_999_reduced_" + str(k) + "00k.json", "w") as outfile:
                outfile.write(out_json)

print("Done")
# print(ing_dict)
out_json = json.dumps(dict(sorted(ing_dict.items(), key=lambda x: -x[1])))
with open("ingredients_reduced_train.json", "w") as outfile:
    outfile.write(out_json)
out_json = json.dumps(dict(sorted(ing_dict.items(), key=lambda x: -x[1])[:999]))
with open("ingredients_999_reduced_train.json", "w") as outfile:
    outfile.write(out_json)


from flask import Flask, request
import subprocess

app = Flask(__name__)

@app.route('/execute-script', methods=['GET'])
def execute_script():
    try:
        print("IM DOIING IT")
        subprocess.call(['./create_next_cluster.sh'])
        print("created sub_process")
        return 'Script executed successfully'
    except Exception as e:
        print("I failed")
        return f'Failed to execute script: {str(e)}'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000)


import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from pathlib import Path
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('exp', type=int, help='Experiment number')

def main():
    args = parser.parse_args()
    exp_num = args.exp

    original_data_path = Path('./pyhard/sjc_internacao')
    coordinates_path = original_data_path / 'coordinates.csv'
    coordinates = pd.read_csv(coordinates_path)
    generated_data_path = Path(f'./pyhard/exp{exp_num}/')
    is_exp_path = generated_data_path / 'is_with_gen_points.csv'
    all_coords_path = generated_data_path / 'all_coordinates.csv'
    figpath = Path(f'./exp{exp_num}/figs')
    figpath.mkdir(parents=True, exist_ok=True)

    enc_points = pd.read_csv(generated_data_path / 'enc_points.csv')
    calc_coords = pd.read_csv(generated_data_path / 'calc_coordinates.csv')
    points = pd.read_csv(generated_data_path / 'points.csv')

    points = points.merge(calc_coords, left_index=True, right_index=True)
    enc_points = enc_points.merge(calc_coords, left_index=True, right_index=True)

    points['se_z1'] = (points['z_1_x'] - points['z_1_y']) ** 2
    points['se_z2'] = (points['z_2_x'] - points['z_2_y']) ** 2

    enc_points['se_z1'] = (enc_points['z_1_x'] - enc_points['z_1_y']) ** 2
    enc_points['se_z2'] = (enc_points['z_2_x'] - enc_points['z_2_y']) ** 2

    sns.set_theme()
    fig, axs = plt.subplots(1, 2, figsize=(16, 8))
    fig.suptitle('Scatterplot of calculated IS points from generated points')
    sns.scatterplot(points, x='z_1_x', y='z_1_y', c=points['se_z1'], ax=axs[0], palette='viridis')
    axs[0].set_xlabel('z_1 generated')
    axs[0].set_ylabel('z_1 calculated')
    axs[0].set_title('z_1')
    plt.colorbar(axs[0].collections[0], ax=axs[0], label='Squared Error')

    sns.scatterplot(points, x='z_2_x', y='z_2_y', c=points['se_z2'], ax=axs[1], palette='viridis')
    axs[1].set_xlabel('z_2 generated')
    axs[1].set_ylabel('z_2 calculated')
    axs[1].set_title('z_2')
    plt.colorbar(axs[1].collections[0], ax=axs[1], label='Squared Error')

    fig.savefig(figpath / f'scatterplot{exp_num}.png', format='png')
    fig.savefig(figpath / f'scatterplot{exp_num}.eps', format='eps')
    plt.clf()

    fig, axs = plt.subplots(1, 2, figsize=(16, 8))
    fig.suptitle('Scatterplot of calculated IS points from encoded points')
    sns.scatterplot(enc_points, x='z_1_x', y='z_1_y', c=enc_points['se_z1'], ax=axs[0], palette='viridis')
    axs[0].set_xlabel('z_1 encoded')
    axs[0].set_ylabel('z_1 calculated')
    axs[0].set_title('z_1')
    plt.colorbar(axs[0].collections[0], ax=axs[0], label='Squared Error')

    sns.scatterplot(enc_points, x='z_2_x', y='z_2_y', c=enc_points['se_z2'], ax=axs[1], palette='viridis')
    axs[1].set_xlabel('z_2 encoded')
    axs[1].set_ylabel('z_2 calculated')
    axs[1].set_title('z_2')
    plt.colorbar(axs[1].collections[0], ax=axs[1], label='Squared Error')

    fig.savefig(figpath / f'scatterplot_enc{exp_num}.png', format='png')
    fig.savefig(figpath / f'scatterplot_enc{exp_num}.eps', format='eps')
    plt.clf()

    is_exp = pd.read_csv(is_exp_path)
    all_coords = pd.read_csv(all_coords_path)

    fig = plt.gcf()
    fig.set_size_inches(12, 8)
    sns.scatterplot(is_exp, x='z_1', y='z_2', hue='Generated')
    plt.savefig(figpath / f'is_exp{exp_num}.png', format='png')
    plt.savefig(figpath / f'is_exp{exp_num}.eps', format='eps')
    plt.clf()

    fig = plt.gcf()
    fig.set_size_inches(12, 8)
    sns.scatterplot(all_coords, x='z_1', y='z_2', hue='Generated')
    plt.savefig(figpath / f'all_coords{exp_num}.png', format='png')
    plt.savefig(figpath / f'all_coords{exp_num}.eps', format='eps')
    plt.clf()

    fig = plt.gcf()
    fig.set_size_inches(12, 8)
    sns.scatterplot(coordinates, x='z_1', y='z_2')
    plt.savefig(figpath / f'coordinates{exp_num}.png', format='png')
    plt.savefig(figpath / f'coordinates{exp_num}.eps', format='eps')
    plt.clf()

if __name__ == '__main__':
    main()


import numpy as np
import sys
import math
import datetime
import os
import random
# import own modules #
sys.path.append('../public')
from my_modules import *
from constants  import *

class Oil:
    def __init__(self, history_data=None, neu=None, sigma=None, u=None, d=None, p=None):
        if history_data is None:
            self.monthly_history_data = load_history_data(MONTH,OIL_TYPE)
        else:
            self.monthly_history_data = history_data
        # initialize parameters
        if (neu is None or sigma is None or u is None or d is None or p is None):
            self.calc_params_from_history()
        else:
            self.neu, self.sigma, self.u, self.d, self.p = neu, sigma, u, d, p

    # calc neu and sigma from history data
    def calc_params_from_history(self):
        index   = 0
        delta_t = 1.0 / DELTA_T_DAY
        values  = np.array([])
        for date, oil_price in self.monthly_history_data:
            if index == 0:
                # initialize the price
                s_0 = oil_price
            else:
                s_t      = oil_price
                base_val = math.log(s_t / s_0)
                values   = np.append(values, base_val)
                # update the price
                s_0      = oil_price
            index += 1
        # substitute inf to nan in values
        values = inf_to_nan_in_array(values)
        self.neu    = np.nanmean(values)
        self.sigma  = np.nanstd(values)
        self.u      = np.exp(self.sigma * np.sqrt(delta_t))
        self.d      = np.exp(self.sigma * (-1) * np.sqrt(delta_t))
        self.p      = 0.5 + 0.5 * (self.neu / self.sigma) * np.sqrt(delta_t)
        return

    def calc_oilprice(self, current_oilprice):
        return self.u * current_oilprice if random.randint(0,1) < self.p else self.d * current_oilprice

    # generate predicted scenario
    def generate_scenario(self,predict_years=DEFAULT_PREDICT_YEARS,predict_pattern_number=DEFAULT_PREDICT_PATTERN_NUMBER):
        # default predict_years is 15 years [180 months]
        self.predict_years  = predict_years

        # predicted data type
        self.predicted_data = []
        for p_num in range(predict_pattern_number):
            self.predicted_data.append([])

        predict_months_num = int(self.predict_years * 12)

        # latest date from history_data
        latest_history_date_str, latest_oilprice = self.monthly_history_data[-1]
        latest_history_date                      = datetime.datetime.strptime(latest_history_date_str.decode('UTF-8'), '%Y/%m/%d')
        for pattern in range(predict_pattern_number):
            current_date  = latest_history_date
            current_oilprice = latest_oilprice
            for predict_month_num in range(predict_months_num):
                current_date        = add_month(current_date)
                current_date_str    = datetime.datetime.strftime(current_date, '%Y/%m/%d')
                for time in range(DELTA_T_DAY):
                    current_oilprice    = self.calc_oilprice(current_oilprice)
                    if current_oilprice > MAX_OIL_PRICE:
                        current_oilprice = current_oilprice/self.u
                self.predicted_data[pattern].append({'date':current_date_str,'price':current_oilprice})
        return


import os
import simplejson as json
from typing import List, Dict
from guthoms_helpers.base_types.Pose3D import Pose3D
from ropose_dataset_tools.DataClasses.Dataset.SensorBase import SensorBase
from ropose_dataset_tools.DataClasses.Dataset.FrameTypes import FrameTypes
from ropose_dataset_tools.DataClasses.Dataset.Dataset import Dataset
from ropose_dataset_tools.DataClasses.Dataset.YoloData import YoloData
from ropose_dataset_tools.DataClasses.Dataset.Image import Image
from ropose_dataset_tools.DataClasses.Dataset.DepthData import DepthData
from ropose_dataset_tools.DataClasses.Dataset.CameraInfo import CameraInfo
from ropose_dataset_tools.DataClasses.Dataset.Metadata import Metadata

import ropose_dataset_tools.config as config

class DataOrganizer:

    def __init__(self, dataPath: str) -> None:
        self.dataPath: str = dataPath
        self.worldTransformPath: str = "world_transforms"
        self.metadataPath: str = "metadata.json"
        self.dirList: List[str] = []
        self.datasets: List[type(Dataset)] = None

        self.datasets = self.LoadData(dataPath)
        pass

    def CheckData(self, dataDirs: [str]):

        tempDirs = dataDirs.copy()

        if not tempDirs.__contains__(self.worldTransformPath):
            raise Exception("Dataset does not contain the directory with world_transforms, this is not a valid RoPose "
                            "dataset!")

        if not os.path.isfile(os.path.join(self.dataPath, self.metadataPath)):
            raise Exception("Dataset does not contain the metadata.json file, this is not a valid RoPose "
                            "dataset!")

        tempDirs.remove(self.worldTransformPath)
        if not tempDirs.__len__() > 0:
            raise Exception("Dataset does not contain a directory with camera data, this is not a valid RoPose "
                            "dataset!")

        print("Dataset in " + self.dataPath + " is valid!")

    @staticmethod
    def GetDirs(dataPath: str) -> List[str]:
        dataDirs = []
        for dirName in os.listdir(dataPath):
            if os.path.isdir(os.path.join(dataPath, dirName)):
                dataDirs.append(dirName)

        return dataDirs

    @staticmethod
    def GetFiles(dataPath: str, endsWith: str) -> List[str]:
        files = []
        for file in os.listdir(dataPath):
            if file.endswith(endsWith):
                files.append(os.path.join(dataPath, file))

        return files

    def LoadData(self, dataPath: str) -> List[type(Dataset)]:
        print("Load raw datasets...")

        #get all dirs
        dataDirs = self.GetDirs(dataPath)

        self.CheckData(dataDirs)

        return self.LoadDataset(dataPath)

    def LoadMetadata(self, dataPath: str) -> type(Metadata):

        filePath = os.path.join(self.dataPath, self.metadataPath)

        rawData = self.LoadJson(filePath)

        metaData = Metadata(rawData["simulated"], rawData["greenscreened"], rawData["robotType"])

        return metaData

    def LoadDataset(self, dataPath: str) -> List[type(Dataset)]:
        datasets: List[List[Dataset]] = []

        metaData = self.LoadMetadata(dataPath)

        dataDirs = self.GetDirs(dataPath)

        worldTransforms = self.LoadTransforms(os.path.join(dataPath, self.worldTransformPath))
        #remobe the world transform path, rest should be cameras
        dataDirs.remove(self.worldTransformPath)

        #rest of dirs are sensor data directories
        sensorData: Dict[str, List[SensorBase]] = dict()

        singelDatasets = {}
        for sensorDir in dataDirs:
            sensorData[sensorDir] = self.LoadSensorData(dataPath, sensorDir, worldTransforms)
            singelDatasets[sensorDir] = []

        #fill final Datasets
        for i in range(0, worldTransforms.__len__()):
            for sensor in sensorData:
                dataset = Dataset()
                #if exception will happen, just ignore the dataset
                try:
                    dataset.worldTransforms = DataOrganizer.FilterTransforms(worldTransforms[i])
                    for frame in sensorData[sensor]:
                        singleData = sensorData[sensor][frame][i]
                        if singleData.frameType == FrameTypes.Image:
                            dataset.rgbFrame = singleData
                        elif singleData.frameType == FrameTypes.Depth:
                            dataset.depthFrame = singleData

                    dataset.metadata = metaData
                    dataset.yoloData = YoloData.FromRopose(dataset)
                    singelDatasets[sensor].append(dataset)
                except:
                    pass

        for sensor in sensorData:
            datasets.append(singelDatasets[sensor])

        return datasets

    @staticmethod
    def LoadSensorData(dataPath: str, cameraName: str, worldTransforms: List[Dict[str, type(Pose3D)]]) \
            -> Dict[str, List[type(SensorBase)]]:
        dataDir = os.path.join(dataPath, cameraName)
        dirs = DataOrganizer.GetDirs(dataDir)

        ret = dict()

        if dirs.__len__() == 0:
            raise Exception("Sensor directory is emtpy!. Seems like you try to load a not yet "
                            "supported RoPose datastructure!")

        if not dirs.__contains__("transforms"):
            raise Exception("Sensor does not contain a transform directory. Seems like you try to load a not yet "
                            "supported RoPose datastructure!")

        transforms = DataOrganizer.LoadTransforms(os.path.join(dataDir, "transforms"))

        #remove the transform dir because we don't need that anymore
        dirs.remove("transforms")

        for dir in dirs:
            frameType = DataOrganizer.PredictDirContent(os.path.join(dataDir, dir))
            if dir.__contains__("labels"):
                continue

            if frameType is FrameTypes.Image:
                frames = DataOrganizer.LoadImageData(os.path.join(dataDir, dir), cameraName,
                                                     worldTransforms=worldTransforms, transforms=transforms)
            elif frameType is FrameTypes.Depth:
                frames = DataOrganizer.LoadDepthData(os.path.join(dataDir, dir), cameraName,
                                                     worldTransforms=worldTransforms, transforms=transforms)
            else:
                raise Exception("Can't predict frame type. Seems like you try to load a not yet supported RoPose "
                                "datastructure!")

            ret[dir] = frames

        return ret

    @staticmethod
    def PredictDirContent(dataDir: str) -> type(FrameTypes):
        files = os.listdir(dataDir)

        for file in files:
            if file.endswith(".png") or file.endswith(".jpg"):
                return FrameTypes.Image
            elif file.endswith(".xml"):
                return FrameTypes.Depth
            else:
                return None

    @staticmethod
    def LoadCameraInfo(dataDir: str) -> type(CameraInfo):

        infoPath = os.path.join(dataDir, "CameraInfo.json")
        if not os.path.isfile(infoPath):
            raise Exception("Can't find CameraInfo.json. Seems like you try to load a bad RoPose datastructure!")

        dataRaw = DataOrganizer.LoadJson(infoPath)

        data = dict()

        for dataPart in dataRaw:
            if dataPart == "CameraName":
                data[dataPart] = dataRaw[dataPart]
            else:
                data[dataPart] = json.loads(dataRaw[dataPart])

        data["filePath"] = infoPath

        return CameraInfo.fromJson(data)

    @staticmethod
    def LoadImageData(dataDir: str, cameraName: str, worldTransforms:  List[Dict[str, type(Pose3D)]],
                      transforms: List[Dict[str, type(Pose3D)]] = None) -> List[type(Image)]:

        cameraInfo = DataOrganizer.LoadCameraInfo(dataDir)

        files = DataOrganizer.GetFiles(dataDir, ".png")
        if files.__len__() == 0:
            files = DataOrganizer.GetFiles(dataDir, ".jpg")

        imageData = [Image] * (files.__len__())

        for file in files:
            index = DataOrganizer.GetFrameIndex(file)
            imageData[index] = DataOrganizer.LoadSingleImageSet(file, cameraInfo,
                                                                worldTransforms[index][cameraName + "_link"],
                                                                transforms[index])

        return imageData

    @staticmethod
    def LoadSingleImageSet(dataPath: str, cameraInfo: type(CameraInfo), sensorPose: type(Pose3D),
                           transforms:  Dict[str, type(Pose3D)] = None) -> type(Image):
        try:
            transforms = DataOrganizer.FilterTransforms(transforms)
        except:
            return None

        return Image(dataPath, cameraInfo, sensorPose, transforms)

    @staticmethod
    def LoadDepthData(dataDir: str, cameraName: str, worldTransforms:  List[Dict[str, type(Pose3D)]],
                      transforms: List[Dict[str, type(Pose3D)]] = None) -> List[type(DepthData)]:

        cameraInfo = DataOrganizer.LoadCameraInfo(dataDir)

        files = DataOrganizer.GetFiles(dataDir, ".xml")
        depthData = [DepthData] * (files.__len__())

        for file in files:
            index = DataOrganizer.GetFrameIndex(file)
            depthData[index] = DataOrganizer.LoadSingleDepthSet(file, cameraInfo,
                                                                worldTransforms[index][cameraName + "_link"],
                                                                transforms[index])

        return depthData

    @staticmethod
    def LoadSingleDepthSet(dataPath: str, cameraInfo: type(CameraInfo), sensorPose: type(Pose3D),
                           transforms: List[type(Pose3D)] = None) -> type(DepthData):
        #sensorPose = transforms[cameraInfo.cameraName + "_link"]
        #transforms = DataOrganizer.FilterTransforms(transforms)
        return DepthData(sensorPose)

    @staticmethod
    def LoadTransforms(dataDir: str) -> List[Dict[str, type(Pose3D)]]:
        files = DataOrganizer.GetFiles(dataPath=dataDir, endsWith=".json")

        transforms = [Dict[str, type(Pose3D)]] * files.__len__()

        for file in files:
            index = DataOrganizer.GetDatasetIndex(file)
            jsonData = DataOrganizer.LoadJson(file)

            poses: Dict[str, type(Pose3D)] = dict()
            for data in jsonData:
                if data != "ros_time" and data != "redundant":
                    poses[data] = Pose3D.fromDict(jsonData[data])

            transforms[index] = poses

        return transforms

    @staticmethod
    def FilterTransforms(transforms: Dict[str, type(Pose3D)]) -> List[type(Pose3D)]:
        ret = []

        for joint in config.linkOrder:
            ret.append(transforms[joint])

        return ret

    @staticmethod
    def LoadJson(filePath: str) -> Dict:
        ret = dict()
        with open(filePath) as data_file:
            data = json.load(data_file)

            for d in data:
                    ret[d] = data[d]

        return ret

    @staticmethod
    def GetDatasetIndex(filename: str) -> int:
        return int(filename.split("/")[-1].split(".")[0].split("_")[-1])

    @staticmethod
    def GetFrameIndex(filename: str) -> int:
        return int(filename.split("/")[-1].split(".")[0])


# 3. Faça um programa, com uma função que necessite de três argumentos, e que forneça a soma desses três argumentos.

def calcular_soma(a, b, c):
    soma = a + b + c
    return soma

def main():
    # Solicita ao usuário três valores para calcular a soma
    a = float(input("Digite o primeiro valor: "))
    b = float(input("Digite o segundo valor: "))
    c = float(input("Digite o terceiro valor: "))

    # Chama a função calcular_soma passando os valores digitados como argumentos
    resultado = calcular_soma(a, b, c)

    # Exibe o resultado da soma
    print("A soma dos três valores é:", resultado)

# Chama a função principal
main()


class Solution:
    def removeKdigits(self, num: str, k: int) -> str:
        if k>=len(num):
            return '0'
        while k>0:
            for i in range(1,len(num)):
                if num[i-1]>num[i]:
                    num=num[:i-1]+num[i:]
                    break
                if i==len(num)-1:
                    num=num[:i]
            k-=1
            if num[0]=='0':
                if len(num)==1:
                    return '0'
                else:
                    num=num[1:]
        return num
#We only need  to check the charactor from left to right
#if the current number is larger than the number on it's right, we will remove that number


#!/usr/bin/env python
from lsm6ds0_msg_pb2 import lsm6ds0_msg
from sensor_msgs.msg import Imu
import serial
import struct
import rospy
import tf

def open_port():
    rospy.init_node('imu_publisher')
    port_name = "/dev/ttyACM0"
    portIsClosed = True

    while portIsClosed:
        try:
            serial_port = serial.Serial(port=port_name, baudrate=19200)
            portIsClosed = False
            rospy.loginfo('Port name = ' + port_name)
            return True, serial_port
        except:
            portIsClosed = True
            rospy.logerr('Cannot open a port. Port name could be wrong.')
            rospy.sleep(0.5)

def imu_publisher(serial_port):
    pub = rospy.Publisher('/Imu', Imu, queue_size=100)
    rospy.init_node('imu_publisher')

    msg_receive = lsm6ds0_msg()
    msg_send = Imu()
    br = tf.TransformBroadcaster()

    while not rospy.is_shutdown():
        sync_byte = serial_port.read(1)
        if sync_byte == '\xff':
            try:
                size_byte = struct.unpack("B", serial_port.read(1))[0]
                s = serial_port.read(size_byte)
                msg_receive.ParseFromString(s)
                rospy.loginfo(msg_receive)
                try:
                    br.sendTransform((0,0,0), (msg_receive.q0, msg_receive.q1, msg_receive.q2, msg_receive.q3),
                                     rospy.Time.now(),'imu','base_link')
                except:
                    rospy.logerr('Cannot send the tf msg')
                    pass

            except:
                rospy.logerr('Cannot read data from uC')
                pass

        msg_send.linear_acceleration.x = msg_receive.acc_x
        msg_send.linear_acceleration.y = msg_receive.acc_y
        msg_send.linear_acceleration.z = msg_receive.acc_z

        msg_send.angular_velocity.x = msg_receive.gyro_x
        msg_send.angular_velocity.y = msg_receive.gyro_y
        msg_send.angular_velocity.z = msg_receive.gyro_z

        msg_send.orientation.x = msg_receive.q0
        msg_send.orientation.y = msg_receive.q1
        msg_send.orientation.z = msg_receive.q2
        msg_send.orientation.w = msg_receive.q3

        msg_send.orientation_covariance = [0,0,0,0,0,0,0,0,0]
        msg_send.angular_velocity_covariance = [0,0,0,0,0,0,0,0,0]
        msg_send.linear_acceleration_covariance = [0,0,0,0,0,0,0,0,0]

        pub.publish(msg_send)

    if serial_port.is_open == True:
        serial_port.close()

if __name__ == '__main__':
    try:
        status, serial_port = open_port()
        if status == True:
            imu_publisher(serial_port)
        else:
            rospy.logerr('Cannot publish message from IMU')
    except rospy.ROSInterruptException:
        pass


#!/usr/bin/env python
#

from importlib import import_module
import logging
import os
import sys

from saml2 import NAMESPACE
from saml2 import ExtensionElement
from saml2 import SAMLError
from saml2 import extension_elements_to_elements
from saml2 import saml
from saml2.s_utils import do_ava
from saml2.s_utils import factory
from saml2.saml import NAME_FORMAT_UNSPECIFIED
from saml2.saml import NAMEID_FORMAT_PERSISTENT

logger = logging.getLogger(__name__)

class UnknownNameFormat(SAMLError):
    pass

class ConverterError(SAMLError):
    pass

def load_maps(dirspec):
    """load the attribute maps

    :param dirspec: a directory specification
    :return: a dictionary with the name of the map as key and the
        map as value. The map itself is a dictionary with two keys:
        "to" and "fro". The values for those keys are the actual mapping.
    """
    mapd = {}
    if dirspec not in sys.path:
        sys.path.insert(0, dirspec)

    for fil in os.listdir(dirspec):
        if fil.endswith(".py"):
            mod = import_module(fil[:-3])
            for item in _find_maps_in_module(mod):
                mapd[item["identifier"]] = item

    return mapd

def ac_factory(path=""):
    """Attribute Converter factory

    :param path: The path to a directory where the attribute maps are expected
        to reside.
    :return: A list of AttributeConverter instances
    """
    acs = []

    if path:
        if path not in sys.path:
            sys.path.insert(0, path)

        for fil in sorted(os.listdir(path)):
            if fil.endswith(".py"):
                mod = import_module(fil[:-3])
                acs.extend(_attribute_map_module_to_acs(mod))
    else:
        from saml2 import attributemaps

        for typ in attributemaps.__all__:
            mod = import_module(f".{typ}", "saml2.attributemaps")
            acs.extend(_attribute_map_module_to_acs(mod))

    return acs

def _attribute_map_module_to_acs(module):
    """Scan an attribute map module and return any attribute maps defined

    :param: module: the python map module
    :type: types.ModuleType
    :return: a generator yielding AttributeConverter defintions
    :rtype: typing.Iterable[AttributeConverter]
    """
    for item in _find_maps_in_module(module):
        atco = AttributeConverter(item["identifier"])
        atco.from_dict(item)
        yield atco

def _find_maps_in_module(module):
    """Find attribute map dictionaries in a map file

    :param: module: the python map module
    :type: types.ModuleType
    :return: a generator yielding dict objects which have the right shape
    :rtype: typing.Iterable[dict]
    """
    for key, item in module.__dict__.items():
        if key.startswith("__"):
            continue
        if isinstance(item, dict) and "identifier" in item and ("to" in item or "fro" in item):
            yield item

def to_local(acs, statement, allow_unknown_attributes=False):
    """Replaces the attribute names in a attribute value assertion with the
    equivalent name from a local name format.

    :param acs: List of Attribute Converters
    :param statement: The Attribute Statement
    :param allow_unknown_attributes: If unknown attributes are allowed
    :return: A key,values dictionary
    """
    return list_to_local(acs, statement.attribute, allow_unknown_attributes)

def list_to_local(acs, attrlist, allow_unknown_attributes=False):
    """Replaces the attribute names in a attribute value assertion with the
    equivalent name from a local name format.

    :param acs: List of Attribute Converters
    :param attrlist: List of Attributes
    :param allow_unknown_attributes: If unknown attributes are allowed
    :return: A key,values dictionary
    """
    if not acs:
        acs = [AttributeConverter()]
        acsd = {"": acs}
    else:
        acsd = {a.name_format: a for a in acs}

    ava = {}
    for attr in attrlist:
        try:
            _func = acsd[attr.name_format].ava_from
        except KeyError:
            if attr.name_format == NAME_FORMAT_UNSPECIFIED or allow_unknown_attributes:
                _func = acs[0].lcd_ava_from
            else:
                logger.info("Unsupported attribute name format: %s", attr.name_format)
                continue

        try:
            key, val = _func(attr)
        except KeyError:
            if allow_unknown_attributes:
                key, val = acs[0].lcd_ava_from(attr)
            else:
                logger.info("Unknown attribute name: %s", attr)
                continue
        except AttributeError:
            continue

        try:
            ava[key].extend(val)
        except KeyError:
            ava[key] = val

    return ava

def from_local(acs, ava, name_format):
    for aconv in acs:
        # print(ac.format, name_format)
        if aconv.name_format == name_format:
            # print("Found a name_form converter")
            return aconv.to_(ava)

    return None

def from_local_name(acs, attr, name_format):
    """
    :param acs: List of AttributeConverter instances
    :param attr: attribute name as string
    :param name_format: Which name-format it should be translated to
    :return: An Attribute instance
    """
    for aconv in acs:
        # print(ac.format, name_format)
        if aconv.name_format == name_format:
            # print("Found a name_form converter")
            return aconv.to_format(attr)
    return attr

def to_local_name(acs, attr):
    """
    :param acs: List of AttributeConverter instances
    :param attr: an Attribute instance
    :return: The local attribute name
    """
    for aconv in acs:
        lattr = aconv.from_format(attr)
        if lattr:
            return lattr

    return attr.friendly_name

def get_local_name(acs, attr, name_format):
    for aconv in acs:
        # print(ac.format, name_format)
        if aconv.name_format == name_format:
            return aconv._fro.get(attr)

def d_to_local_name(acs, attr):
    """
    :param acs: List of AttributeConverter instances
    :param attr: an Attribute dictionary
    :return: The local attribute name
    """
    for aconv in acs:
        lattr = aconv.d_from_format(attr)
        if lattr:
            return lattr

    # if everything else fails this might be good enough
    try:
        return attr["friendly_name"]
    except KeyError:
        raise ConverterError(f"Could not find local name for {attr}")

class AttributeConverter:
    """Converts from an attribute statement to a key,value dictionary and
    vice-versa"""

    def __init__(self, name_format=""):
        self.name_format = name_format
        self._to = None
        self._fro = None

    def adjust(self):
        """If one of the transformations is not defined it is expected to
        be the mirror image of the other.
        """

        if self._fro is None and self._to is not None:
            self._fro = {value.lower(): key for key, value in self._to.items()}
        if self._to is None and self._fro is not None:
            self._to = {value.lower(): key for key, value in self._fro.items()}

    def from_dict(self, mapdict):
        """Import the attribute map from  a dictionary

        :param mapdict: The dictionary
        """

        self.name_format = mapdict["identifier"]
        try:
            self._fro = {k.lower(): v for k, v in mapdict["fro"].items()}
        except KeyError:
            pass
        try:
            self._to = {k.lower(): v for k, v in mapdict["to"].items()}
        except KeyError:
            pass

        if self._fro is None and self._to is None:
            raise ConverterError("Missing specifications")

        if self._fro is None or self._to is None:
            self.adjust()

    def lcd_ava_from(self, attribute):
        """
        If nothing else works, this should

        :param attribute: an Attribute instance
        :return:
        """
        name = attribute.name.strip()
        values = [(value.text or "").strip() for value in attribute.attribute_value]
        return name, values

    def fail_safe_fro(self, statement):
        """In case there is not formats defined or if the name format is
        undefined

        :param statement: AttributeStatement instance
        :return: A dictionary with names and values
        """
        result = {}
        for attribute in statement.attribute:
            if attribute.name_format and attribute.name_format != NAME_FORMAT_UNSPECIFIED:
                continue
            try:
                name = attribute.friendly_name.strip()
            except AttributeError:
                name = attribute.name.strip()

            result[name] = []
            for value in attribute.attribute_value:
                if not value.text:
                    result[name].append("")
                else:
                    result[name].append(value.text.strip())
        return result

    def ava_from(self, attribute, allow_unknown=False):
        try:
            attr = self._fro[attribute.name.strip().lower()]
        except AttributeError:
            attr = attribute.friendly_name.strip().lower()
        except KeyError:
            if allow_unknown:
                try:
                    attr = attribute.name.strip().lower()
                except AttributeError:
                    attr = attribute.friendly_name.strip().lower()
            else:
                raise

        val = []
        for value in attribute.attribute_value:
            if value.extension_elements:
                ext = extension_elements_to_elements(value.extension_elements, [saml])
                for ex in ext:
                    if attr == "eduPersonTargetedID" and ex.text:
                        val.append(ex.text.strip())
                    else:
                        cval = {}
                        for key, (name, typ, mul) in ex.c_attributes.items():
                            exv = getattr(ex, name)
                            if exv:
                                cval[name] = exv
                        if ex.text:
                            cval["value"] = ex.text.strip()
                        val.append({ex.c_tag: cval})
            elif not value.text:
                val.append("")
            else:
                val.append(value.text.strip())

        return attr, val

    def fro(self, statement):
        """Get the attributes and the attribute values.

        :param statement: The AttributeStatement.
        :return: A dictionary containing attributes and values
        """

        if not self.name_format:
            return self.fail_safe_fro(statement)

        result = {}
        for attribute in statement.attribute:
            if attribute.name_format and self.name_format and attribute.name_format != self.name_format:
                continue

            try:
                (key, val) = self.ava_from(attribute)
            except (KeyError, AttributeError):
                pass
            else:
                result[key] = val

        return result

    def to_format(self, attr):
        """Creates an Attribute instance with name, name_format and
        friendly_name

        :param attr: The local name of the attribute
        :return: An Attribute instance
        """
        try:
            _attr = self._to[attr]
        except KeyError:
            try:
                _attr = self._to[attr.lower()]
            except KeyError:
                _attr = ""

        if _attr:
            return factory(saml.Attribute, name=_attr, name_format=self.name_format, friendly_name=attr)
        else:
            return factory(saml.Attribute, name=attr)

    def from_format(self, attr):
        """Find out the local name of an attribute

        :param attr: An saml.Attribute instance
        :return: The local attribute name or "" if no mapping could be made
        """
        if attr.name_format:
            if self.name_format == attr.name_format:
                try:
                    return self._fro[attr.name.lower()]
                except KeyError:
                    pass
        else:  # don't know the name format so try all I have
            try:
                return self._fro[attr.name.lower()]
            except KeyError:
                pass

        return ""

    def d_from_format(self, attr):
        """Find out the local name of an attribute

        :param attr: An Attribute dictionary
        :return: The local attribute name or "" if no mapping could be made
        """
        if attr["name_format"]:
            if self.name_format == attr["name_format"]:
                try:
                    return self._fro[attr["name"].lower()]
                except KeyError:
                    pass
        else:  # don't know the name format so try all I have
            try:
                return self._fro[attr["name"].lower()]
            except KeyError:
                pass

        return ""

    def to_(self, attrvals):
        """Create a list of Attribute instances.

        :param attrvals: A dictionary of attributes and values
        :return: A list of Attribute instances
        """
        attributes = []
        for key, value in attrvals.items():
            name = self._to.get(key.lower())
            if name:
                if name == "urn:oid:1.3.6.1.4.1.5923.1.1.1.10":
                    # special case for eduPersonTargetedID
                    attr_value = self.to_eptid_value(value)
                else:
                    attr_value = do_ava(value)
                attributes.append(
                    factory(
                        saml.Attribute,
                        name=name,
                        name_format=self.name_format,
                        friendly_name=key,
                        attribute_value=attr_value,
                    )
                )
            else:
                attributes.append(factory(saml.Attribute, name=key, attribute_value=do_ava(value)))

        return attributes

    def to_eptid_value(self, values):
        """
        Create AttributeValue instances of NameID from the given values.

        Special handling for the "eptid" attribute
        Name=urn:oid:1.3.6.1.4.1.5923.1.1.1.10
        FriendlyName=eduPersonTargetedID

        values is a list of items of type str or dict. When an item is a
        dictionary it has the keys: "NameQualifier", "SPNameQualifier", and
        "text".

        Returns a list of AttributeValue instances of NameID elements.
        """

        if type(values) is not list:
            values = [values]

        def _create_nameid_ext_el(value):
            text = value["text"] if isinstance(value, dict) else value
            attributes = (
                {
                    "Format": NAMEID_FORMAT_PERSISTENT,
                    "NameQualifier": value["NameQualifier"],
                    "SPNameQualifier": value["SPNameQualifier"],
                }
                if isinstance(value, dict)
                else {"Format": NAMEID_FORMAT_PERSISTENT}
            )
            element = ExtensionElement("NameID", NAMESPACE, attributes=attributes, text=text)
            return element

        attribute_values = [saml.AttributeValue(extension_elements=[_create_nameid_ext_el(v)]) for v in values]
        return attribute_values

class AttributeConverterNOOP(AttributeConverter):
    """Does a NOOP conversion, that is no conversion is made"""

    def __init__(self, name_format=""):
        AttributeConverter.__init__(self, name_format)

    def to_(self, attrvals):
        """Create a list of Attribute instances.

        :param attrvals: A dictionary of attributes and values
        :return: A list of Attribute instances
        """
        attributes = []
        for key, value in attrvals.items():
            key = key.lower()
            attributes.append(
                factory(saml.Attribute, name=key, name_format=self.name_format, attribute_value=do_ava(value))
            )

        return attributes


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

# 列表生成式: List Comprehensions

# 生成[1x1, 2x2, 3x3, ...]
L1 = [x * x for x in range(1, 11)]

print(L1)

# if筛选
L2 = [x * x for x in range(1, 11) if 0 == x % 2]

print(L2)

# 两层循环
L3 = [ m + n for m in 'ABC' for n in 'XYZ']

print(L3)

# @ 列出当前目录下所有文件和目录名:
import os   # 导入os模块
L4 = [d for d in os.listdir('.')]

print(L4)

# 两个变量
d = {'T': 'A', 'a': 'B', 'y': 'B', 'l': 'C'}
L5 = [ key + ':' + val for key, val in d.items()]

print(L5)

# 将所有大写转为小写
L = ['Hello', 'World', 'IBM', 'Apple']
L6 = [s.lower() for s in L]

print(L6)

# 数据类型不一样
L_2 = ['Hello', 'World', 18, 'Apple', None]

L7 = [ s.lower() for s in L_2 if isinstance(s, str)]

print(L7)


from random import randint

def create_test_case(rows, cols, height):
    res = [f"{cols} {rows}"]
    for r in range(rows):
        row = []
        for c in range(cols):
            row.append(str(randint(0, height)))
        res.append(" ".join(row))
    return "\n".join(res)

with open("/tmp/terraces.in", "w") as f:
    rows = 500
    cols = 500
    height = randint(1, 999)
    tc = create_test_case(rows, cols, height)
    f.write(tc)
    print(tc)


import numpy as np
from numpy.linalg import inv
from astropy.io import ascii
import json
import os
import time

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

#Define functions

def files_from_directory(directory):

    '''Takes the files from an input directory
       and returns the file names as strings
       in a list'''

    for filename in os.listdir(directory):
        if filename.endswith('.dat'):
            files=[filename for filename in os.listdir(directory)]
    return files

def read_ascii_file(filepath):
    data = ascii.read(filepath)
    try:
        return np.array(data['JD']), np.array(data['g_transit_mag']),np.array(data['mag_error'])
    except ValueError:
        print('Incompatible Field. Table must have columns containing'
        'JD, g_transit_mag, mag_error')

def generate_params(filepath):

    """Extracts necessary params from ASCII table"""

    _jd, _mag, _mag_err = read_ascii_file(filepath)
    _jd = _jd-2450000
    _nobs = len(_jd)
    _weights = 1/(_mag_err**2)
    _jd_min = np.min(_jd)
    _jd = _jd - _jd_min
    return _nobs, _weights, _jd, _mag, _jd_min, _mag_err

def generate_arrays(n_obs,n_points):
    """Generates the arrays used by the chi_squared function """
    _chi = np.zeros(n_points,dtype=np.int64)
    _per = np.zeros(n_points,dtype=np.int64)
    _func = np.zeros(n_obs,dtype=np.int64)

    return _chi, _per, _func

def extract_source_id(filepath, delim = '/'):
    filename = filepath.split(delim)[-1]
    return filename.split('.')[0]

def write_to_json(dictionary,filepath):

    """Dumps the results of a curve fit to a JSON file"""

    folder = r"C:\Users\cuand\ESAC Internship\OHIR Sample\OHIR_sources_json"
    source = extract_source_id(filepath)
    new_file = f"{folder}\{source}.json"

    with open(new_file, "w") as outfile:
        json.dump(dictionary, outfile, indent=4)

def list_to_file(_list, _directory):
    return [f"{_directory}/{i}.dat" for i in _list]

def read_list_from_file(file):
    data = np.loadtxt(file,dtype='str')
    return data[:,0], [float(i) for i in data[:,1]]

#------------------------------------------
# Chi squared function

def run_chi_squared(filepath,period=None):

    TWO_PI = 2*np.pi

    if period is not None:
        P_MIN=period-99
        P_MAX=period+99
        DELTA_P = int(P_MAX-P_MIN)
        print("Short Run")

    #else:
    P_MIN = 100
    P_MAX = 2500
    DELTA_P = P_MAX - P_MIN
    PERIODS = np.arange(P_MIN,P_MAX)
    print("Long Run")

    nobs, weights, jd, mag, jd_min, mag_err = generate_params(filepath)

    chi, per, func = generate_arrays(nobs, DELTA_P)
    chi_min = 10000000.

    for i in PERIODS:

        chiminperiod = 10000000.

        for j in np.arange(1,100, dtype=np.float64):
            f=j/100.
            jd_int=np.int32((jd/per[i])+f)
            jdr=((jd/per[i])+f)-jd_int

            for k in np.arange(10,90, dtype=np.float64):
                fact=0.01*k
                for m in np.arange(nobs):
                    if jdr[m] < fact:
                        func[m]=jdr[m]/(2*fact)

                    else:
                        func[m]=((jdr[m]-1)/(2*(1-fact)))+1

                cc=np.sum(((np.cos(TWO_PI*func))**2)*weights)
                co=np.sum((np.cos(TWO_PI*func))*weights)
                err=np.sum(weights)
                com=np.sum(np.cos(TWO_PI*func)*mag*weights)
                m = np.sum(mag*weights)

                    #coefficients

                det = cc*err - co*co
                b=(com*err-co*m)/det
                a=(cc*m-com*co)/det

                    #fit error

                chi_phase = np.sum((weights*((mag-a-b*np.cos(TWO_PI*func))**2))/(nobs-2))

                if chi_phase <= chiminperiod:
                    chi[i]=chi_phase
                    chiminperiod=chi_phase

                if chi_phase <= chi_min:
                    chi_min = chi_phase
                    ffactor=fact
                    error_ffactor=0.01
                    period = per[i]
                    error_period=(period**2)/(2*(np.max(jd)-np.min(jd)))
                    phase=f
                    error_phase=.01
                    matrix=np.array([[cc,co],[co,err]])
                    matrix_inv=inv(matrix)
                    ampli=b*2
                    if ampli <= 0:
                        ampli=-ampli
                        ffactor=1-ffactor
                        phase=phase+ffactor

                    if phase > 1.:
                        phase=phase-1

                    error_ampli=2*np.sqrt(matrix_inv[0,0])
                    magmed=a
                    error_magmed=np.sqrt(matrix_inv[1,1])
                    #print(ffactor)

    jd = jd+jd_min

    results = {
        'time': jd.tolist(),
        'jd_min':jd_min,
        'chi': chi.tolist(),
        'mag':(mag.tolist(), mag_err.tolist()),
        'period':per.tolist(),
        'final_period':(period, error_period),
        'phase':(phase, error_phase),
        'amplitude':(ampli, error_ampli),
        'ffactor':(ffactor,error_ffactor),
        'mag_med': (magmed, error_magmed)
    }

    return results

#-----------------------------
#Testing the script

#DIR = <Insert path of ascii files here>

def asymmetric_fit(filepath, period=None):
    #print(period)
    data = run_chi_squared(filepath, period)
    write_to_json(data, filepath)
    return data

if __name__=='__main__':
    #print('Start script...')
    file_list = files_from_directory(DIR)
    for file in file_list:
        asymmetric_fit(file)


import tensorflow as tf

from labels_model.training.settings import (
    BUSINESS_RULES_DEFINITION,
    LABEL_DEPENDENCIES,
    TRAINING_LABELS,
    TRANSACTION_LABELS,
    N_TRAINING_LABELS,
    N_TRANSACTION_LABELS,
    GENERAL_SIMILARITY_THRESHOLD,
)

class BusinessRulesTransformer(tf.keras.layers.Layer):
    """
    Layer which applies business rules and modifies model similarities accordingly
    Note that the business logic is defined by the parameter BUSINESS_RULES_DEFINITION
    """

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.trainable = False

    def call(self, inputs, **kwargs):

        # extract similarities
        # >>> it will be modified on the fly while applying business rules
        postprocessed_similarities = inputs["similarities"]

        # generate [True] values mask with the shape of [n_rows, n_labels]
        # >>> by generating all_trues we make sure that we repeat the condition mask for every row
        all_trues = tf.cast(
            tf.clip_by_value(postprocessed_similarities, 1.0, 1.0), tf.dtypes.bool
        )

        # extract labels that have a business rule defined
        labels = BUSINESS_RULES_DEFINITION.keys()
        # iterate through all labels
        for label in labels:
            # extract label index mask
            label_index_mask = all_trues & tf.equal(TRAINING_LABELS, label)

            # iterate through the rules defined for given label
            for rule in BUSINESS_RULES_DEFINITION[label]:
                input_feature = inputs[rule["feature_name"]]

                # create a mask for the input based on the feature value & tensorflow operator defined in the rule
                tf_comparison_operator = rule["tf_comparison_operator"]

                input_feature_value_mask = all_trues & tf_comparison_operator(
                    tf.reshape(input_feature, (-1, 1)), rule["feature_value"]
                )

                # generate similarities with the value defined for the rule
                rule_similarity_value = tf.clip_by_value(
                    postprocessed_similarities,
                    rule["similarity_value"],
                    rule["similarity_value"],
                )

                # overwrite similarities with the value defined in the rule for given label
                postprocessed_similarities = tf.where(
                    tf.logical_and(label_index_mask, input_feature_value_mask),
                    rule_similarity_value,
                    postprocessed_similarities,
                )

        return postprocessed_similarities

    def compute_output_shape(self, input_shape):
        return input_shape[0], N_TRAINING_LABELS

class EliminateLabelDependencies(tf.keras.layers.Layer):
    """
    Eliminate the dependencies between labels. Within the conflicting labels, we leave the max predicted
    similarity unchanged and set -1 for the remaining conflicting labels.
    """

    def call(self, similarities):
        # generate [True] values mask with the shape of [n_rows, n_labels]
        # >>> by generating all_trues we make sure that we repeat the condition mask for every row
        all_trues = tf.cast(tf.clip_by_value(similarities, 1.0, 1.0), tf.bool)

        # iterate through conflicting labels
        for label_conflict in LABEL_DEPENDENCIES:
            # extract indices for conflicting labels
            conflicting_labels_indices = [
                TRAINING_LABELS.index(label) for label in label_conflict
            ]

            # extract similarities for conflicting labels based on their indices
            conflicting_labels_similarities = tf.gather(
                similarities, conflicting_labels_indices, batch_dims=-1
            )

            # extract best predicted similarity for conflicting labels
            conflicting_labels_best_predicted_similarity = tf.reshape(
                tf.reduce_max(conflicting_labels_similarities, axis=1), (-1, 1)
            )

            # extract mask for best predicted similarity within conflicting labels
            conflicting_labels_best_predicted_similarity_mask = tf.equal(
                similarities, conflicting_labels_best_predicted_similarity
            )

            # define mask for non conflicting labels
            not_conflicting_labels_index_flag = [
                True if label not in label_conflict else False
                for label in TRAINING_LABELS
            ]
            non_conflicting_labels_mask = all_trues & not_conflicting_labels_index_flag

            # leave similarities unchanged if it's the best prediction within the conflicting labels or it's about non conflicting labels;
            #   otherwise set the similarities to -1
            similarities = tf.where(
                tf.logical_or(
                    conflicting_labels_best_predicted_similarity_mask,
                    non_conflicting_labels_mask,
                ),
                similarities,
                tf.clip_by_value(similarities, -1.0, -1.0),
            )

        return similarities

    def compute_output_shape(self, input_shape):
        return input_shape[0], N_TRAINING_LABELS

class PredictionsIndicatorAboveThreshold(tf.keras.layers.Layer):
    """
    Return the matrix with 1. if the label similarity is above threshold and 0. otherwise
    The output shape would be (n_transactions, n_transaction_labels) including the label for "Unknown"
    """

    def __init__(self, **kwargs):
        super(PredictionsIndicatorAboveThreshold, self).__init__(**kwargs)

        # define list with indices for transaction labels to extract the similarities we are interested in
        self.transaction_labels_indices = [
            TRAINING_LABELS.index(label)
            for label in TRAINING_LABELS
            if label in TRANSACTION_LABELS
        ]

    def call(self, similarities):
        # extract similarities for transaction labels
        similarities_for_transaction_labels = tf.gather(
            similarities, self.transaction_labels_indices, batch_dims=-1
        )

        # create boolean where the similarity is equal or greater than the threshold
        postprocessed_similarities_above_threshold_mask = tf.greater_equal(
            similarities_for_transaction_labels, GENERAL_SIMILARITY_THRESHOLD
        )

        # convert boolean to float
        # > compiled Keras model objects always are attached to a loss function and the loss function must have floats (or similar) as inputs.
        postprocessed_similarities_above_threshold_float = tf.cast(
            postprocessed_similarities_above_threshold_mask, tf.dtypes.float32
        )

        return postprocessed_similarities_above_threshold_float

    def compute_output_shape(self, input_shape):
        return input_shape[0], N_TRANSACTION_LABELS


from cryptography.fernet import Fernet
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC

# Define encryption and decryption functions
def encrypt_message(message, key):
    salt = os.urandom(16)
    kdf = PBKDF2HMAC(
        algorithm=hashes.SHA256(),
        length=32,
        salt=salt,
        iterations=100000,
    )
    derived_key = base64.urlsafe_b64encode(kdf.derive(key))
    cipher = Cipher(algorithms.AES(derived_key), modes.CTR())
    encryptor = cipher.encryptor()
    ciphertext = encryptor.update(message) + encryptor.finalize()
    return (ciphertext, salt)

def decrypt_message(ciphertext, salt, key):
    kdf = PBKDF2HMAC(
        algorithm=hashes.SHA256(),
        length=32,
        salt=salt,
        iterations=100000,
    )
    derived_key = base64.urlsafe_b64encode(kdf.derive(key))
    cipher = Cipher(algorithms.AES(derived_key), modes.CTR())
    decryptor = cipher.decryptor()
    message = decryptor.update(ciphertext) + decryptor.finalize()
    return message

# Generate encryption key
key = os.urandom(32)

# Encrypt message
message = b"my secret message"
ciphertext, salt = encrypt_message(message, key)

# Decrypt message
decrypted_message = decrypt_message(ciphertext, salt, key)

print("Original message:", message)
print("Encrypted message:", ciphertext)
print("Decrypted message:", decrypted_message)


import sys
def find_set(v,set_list):
    for index,v_set in enumerate(set_list):
        if v in v_set:
            v_index = index
            break
    return v_index #u와 v가 각각 몇번 째 집합에 위치해 있는지 반환

n = int(input())

edges = []
for i in range(n):
    for j,w in enumerate(map(int,sys.stdin.readline().split())):
        if w > 0:
            edges.append((i,j,w))

set_list = [set([i]) for i in range(n)]
edges.sort(key = lambda weight: weight[2])
res = 0

for u,v,w in edges:
    u_index = find_set(u,set_list); v_index = find_set(v,set_list)
    if u_index != v_index:
        set_list[u_index] = set_list[u_index].union(set_list[v_index]) #합집합으로 값을 덮어 씌움
        set_list = set_list[:v_index] + set_list[v_index + 1:]
        res += w

print(res)


import csv
from app.extensions import db
from sqlalchemy import func
from app.database import Position

def read_csv_file(file):
    output = []
    with open(file) as csv_file:
        csv_reader = csv.reader(csv_file)
        for line in csv_reader:
            if not line:
                continue
            line = trim(line)
            output.append(
                {
                    'name': line[0],
                    'rating': line[1],
                    'position': {
                        'id': get_position_id(line[2]),
                    },
                    'age': line[3],
                    'starter': 1 if line[4] == 'True' else 0,
                }
            )
    return output

def get_position_id(pos):
    position = db.session.query(Position).filter(func.lower(Position.name) == func.lower(pos)).first()
    if position is None:
        raise ValueError
    return position.id

def trim(line):
    trimmed_line = []
    for field in line:
        trimmed_line.append(field.strip())
    return trimmed_line


#! /usr/bin/env python2.7
from __future__ import division, print_function
import sys
import argparse
import re
import gzip
"""
axt2HapAlignedFa.py
Created by Nathan Schaefer on 09/04/14 at 14:40

Converts an AXT formatted alignment to a haploid FASTA file in the coordinates
of the reference genome. Ignores gaps in the reference sequence and replaces
all gaps in the query sequence with ambiguous "N" bases.

"""

def parse_args():
    """Set up and parse command-line arguments."""
    parser = argparse.ArgumentParser(description=__doc__)
    return parser.parse_args()

def read_axt(filename):
    if filename[-3:] == ".gz":
        fp = gzip.open(filename, "r")
    else:
        fp = open(filename, "r")

    readingAlignment = False
    nextAlnNumber = 0

    refStart = -1
    refEnd = -1
    chrom = None

    refSeqBlock = None
    qSeqBlock = None

    for line in fp:
        line = line.rstrip()
        if len(line) == 0 or line[0] == "#":
            continue
        elif not readingAlignment and "{}".format(nextAlnNumber) == line.split()[0]:
            data = line.split()
            alnNumber = int(data[0])
            if alnNumber == nextAlnNumber:
                nextAlnNumber += 1
                readingAlignment = True
                refStart = int(data[2])
                refEnd = int(data[3])
                chrom = data[1]
            else:
                print("ERROR: alignment block indices do not match: {} {}".format(alnNumber, nextAlnNumber), file=sys.stderr)
                print(line, file=sys.stderr)
                exit(1)
        else:
            # Read sequences
            if refSeqBlock is None:
                refSeqBlock = line
            elif qSeqBlock is None:
                qSeqBlock = line

                # Get ready to return data

                # Transform gaps in query seq into "N"
                #qSeqBlock = qSeqBlock.replace('-', 'N')

                # Remove gaps in reference seq from query seq
                refGaps = re.finditer('\-+', refSeqBlock)

                qseqFixed = ''
                qseqIndex = 0

                for refGap in refGaps:
                    #gaplen = len(refGap.group(0))
                    gapStart = refGap.start(0)
                    gapEnd = refGap.end(0)
                    for i in range(qseqIndex, gapStart):
                        qseqFixed += qSeqBlock[i]
                    qseqIndex = gapEnd

                for i in range(qseqIndex, len(qSeqBlock)):
                    qseqFixed += qSeqBlock[i]

                # Yield data
                yield(chrom, refStart, refEnd, qseqFixed)

                # Reset data
                readingAlignment = False
                refSeqBlock = None
                qSeqBlock = None
    fp.close()

def main(args):
    """Main method"""
    # Arguments: input AXT files
    for axtfilename in args[1:]:
        lastRefIndex = 1
        lastChromName = None
        needLineBreak = False
        for chromName, refStart, refEnd, qSeq in read_axt(axtfilename):
            if chromName != lastChromName:
                if needLineBreak:
                    print("\n", end="")
                print(">{}".format(chromName))
                lastChromName = chromName

            # Add any sequence since the end of the previous sequence
            if (refStart - lastRefIndex) > 0:
                fillerSeq = "-" * (refStart - lastRefIndex)
                print(fillerSeq, end="")

            lastRefIndex = refEnd + 1

            # Print this sequence block
            print(qSeq.upper(), end="")

            needLineBreak = True

if __name__ == "__main__" :
    sys.exit(main(sys.argv))


import pandas as pd
#------------------------------------------------------------------------------

# Набор данных печень (вроде прикольный)
#583 записи,11 столбцов
#https://www.kaggle.com/uciml/indian-liver-patient-records

df1 = pd.read_csv('indian_liver_patient.csv')
"""
Набор данных для оценки болезни печени.

[Столбцы:
 'Age' - Возраст пациента,
 'Gender' - Пол пациента,
 'Total_Bilirubin' - Общий билирубин в мг / дл,
 'Direct_Bilirubin' - Прямой билирубин в мг / дл,
 'Alkaline_Phosphotase' - Щелочная фосфотаза  в МЕ / л,
 'Alamine_Aminotransferase' - Аламин аминотрансфераза АЛТ в МЕ / л,
 'Aspartate_Aminotransferase' - Аспартатаминотрансфераза АСТ в МЕ / л,
 'Total_Protiens' - Всего протиенов (общий белок наверное) Общее количество белков г / дл,
 'Albumin' - Альбумин в г / дл
 'Albumin_and_Globulin_Ratio - Соотношение альбумина и глобулинаю
 'Dataset' - поле, используемое для разделения данных на два набора (пациент с заболеванием печени или без заболеваний 1- да, 2- нет)

Этот набор данных содержит 416 историй болезни пациентов с заболеваниями печени и 167 историй болезни пациентов без печени,
собранных на северо-востоке штата Андхра-Прадеш, Индия.
Столбец «Набор данных» представляет собой метку класса, используемую для разделения групп
на пациентов с печенью (заболевание печени) или нет (без заболевания).
Этот набор данных содержит 441 историю болезни мужчин и 142 истории болезни женщин.

Любой пациент, возраст которого превышает 89 лет, указывается как возраст «90».

"""
#------------------------------------------------------------------------------

# Набор данных про грыжи тоже вроде прикольный
#310 записей,7 столбцов
#https://www.kaggle.com/uciml/biomechanical-features-of-orthopedic-patients
df2=pd.read_csv('column_2C_weka.csv')
df3=pd.read_csv('column_3C_weka.csv')

"""Данные сгруппированы по двум различным, но связанным задачам классификации.
столбец 3C weka.csv (файл с тремя метками классов)
Первая задача состоит в том, чтобы отнести пациентов к одной из трех категорий:
    нормальные (100 пациентов),
    грыжа диска (60 пациентов) или
    спондилолистез (150 пациентов).

столбец 2C weka.csv (файл с двумя метками классов)

Для второй задачи категории грыжи диска и спондилолистеза были объединены в единую категорию,
обозначенную как «аномальная». Таким образом, вторая задача состоит в том,
чтобы отнести пациентов к одной из двух категорий: нормальные (100 пациентов) или аномальные (210 пациентов).
Каждый пациент представлен в наборе данных шестью биомеханическими атрибутами,
производными от формы и ориентации таза и поясничного отдела позвоночника
(каждый из них представляет собой столбец):

Столбцы:

['pelvic_incidence' - заболеваемость тазом,
 'pelvic_tilt numeric' - наклон таза,
 'lumbar_lordosis_angle' - угол поясничного лордоза,
 'sacral_slope' - крестцовый склон,
 'pelvic_radius' - радиус таза,
 'degree_spondylolisthesis' - степень спондилолистеза
,
 'class']

"""
#------------------------------------------------------------------------------
# Набор данных про сердечко heart_failure_clinical_records_dataset 299 записей 13 столбцов
""" может предсказать выживаемость пациентов с сердечной недостаточностью только по креатинину сыворотки
# и фракции выброса.
#https://www.kaggle.com/andrewmvd/heart-failure-clinical-data - куча каких-то кодов есть, примеров и тд
"""

df4 = pd.read_csv('heart_failure_clinical_records_dataset.csv')

"""Столбцы:
 'age' - возраст,
 'anaemia' - Уменьшение эритроцитов или гемоглобина (логическое значение),
 'creatinine_phosphokinase' - Уровень фермента КФК в крови (мкг / л),
 'diabetes' - есть диабет или нет (1- есть/0 - нет),
 'ejection_fraction'  - Процент крови, покидающей сердце при каждом сокращении (в процентах),
 'high_blood_pressure' - Если у пациента гипертония (логическое значение),
 'platelets' - Тромбоциты в крови (килотромбоциты / мл),
 'serum_creatinine' - сыворотка_креатинин, то есть Уровень сывороточного креатинина в крови (мг / дл),
 'serum_sodium' - Уровень сывороточного натрия в крови (мг-экв / л),
 'sex' - пол (1- М,0-Ж),
 'smoking' - курит или нет,
 'time' - время, срок наблюдения в днях,
 'DEATH_EVENT - если пациент умер в течение периода наблюдения (логическое значение)]"""

#------------------------------------------------------------------------------
# Набор данных про сердечко heart 303 записи, 14 столбцов - не особо нравится
"""
# Куча кода и исследований, по сути одинаковые датасеты:
#https://www.kaggle.com/ronitf/heart-disease-uci
#https://www.kaggle.com/nareshbhat/health-care-data-set-on-heart-attack-possibility
"""

df5 = pd.read_csv('heart.csv')
"""
Столбцы:
['age'      - возраст,
 'sex'      - пол, 0 - ж, 1-М.
 'cp'       - chest pain type тип боли в груди (4 значения),
 'trestbps' - resting blood pressure - артериальное давление в покое
 'chol',    - serum cholestoral in mg/dl - холесторальный уровень сыворотки в мг / дл
 'fbs'      -fasting blood sugar > 120 mg/dl - уровень сахара в крови натощак> 120 мг / дл, логическое значение 1/0
 'restecg'  -resting electrocardiographic results-  результаты электрокардиографии в покое (значения 0,1 , 2),
 'thalach'  -maximum heart rate achieved - достигнута максимальная частота сердечных сокращений,
 'exang'    -exercise induced angina- стенокардия, вызванная физической нагрузкой (0/1),
 'oldpeak'  - oldpeak =ST depression induced by exercise relative to rest -
             депрессия ST, вызванная физической нагрузкой относительно покоя,
 'slope'    - the slope of the peak exercise ST segment - наклон пикового сегмента ST при нагрузке,
 'ca'       - number of major vessels (0-3) colored by flourosopy- количество крупных сосудов (0-3), окрашенных флурозопией,
 'thal'     - thal: 0 = нормально; 1 = исправленный дефект; 2 = обратимый дефект (заполнено с ошибкой, надо разбираться),
 'target'   - цель: 0 = меньше вероятность сердечного приступа 1 = больше вероятность сердечного приступа]

Поле «цель» относится к наличию сердечного заболевания у пациента.
Это целое число: 0 = нет / меньше шансов сердечного приступа и 1 = больше шансов сердечного приступа.

"""

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

# Набор данных рак груди breast-cancer-wisconsin
# 699 записей, 10 столбцов
"""
Этот набор данных (исходная база данных рака молочной железы штата Висконсин)
взят из репозитория машинного обучения UCI.
Этот набор данных предназначен для прогнозирования того, являются ли раковые клетки
доброкачественными или злокачественными.
# https://www.kaggle.com/ninjacoding/breast-cancer-wisconsin-benign-or-malignant
# https://www.kaggle.com/roustekbio/breast-cancer-csv
"""
df6=pd.read_csv('breast-cancer-wisconsin.csv')
df66=pd.read_csv('breastCancer.csv')

"""
Столбцы:
['id',
 'clump_thickness' - толщина комка 1-10,
 'size_uniformity' - Однородность размера клеток: 1 - 10,
 'shape_uniformity' - Однородность формы клеток: 1 - 10,
 'marginal_adhesion' - Маргинальная адгезия: 1 - 10,
 'epithelial_size' - Размер одиночных эпителиальных клеток: 1 - 10,
 'bare_nucleoli' - голые ядра: 1-10,
 'bland_chromatin' - Блочный хроматин: 1-10,
 'normal_nucleoli' - Нормальные ядрышки: 1-10,
 'mitoses' - Митозов: 1-10,
 'class' - 2 для доброкачественных и 4 для злокачественных]
'Clump Thickness',
 'Uniformity of Cell Size',
 'Uniformity of Cell Shape',
 'Marginal Adhesion',
 ' Single Epithelial Cell Size',
 'Bare Nuclei',
 'Bland Chromatin',
 'Normal Nucleoli',
 'Mitoses',
 'class']
Распределение классов:
Доброкачественные: 458 (65,5%)
Злокачественные: 241 (34,5%)

"""

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

# Набор про рак data 569 записей,33 столбца
# https://www.kaggle.com/uciml/breast-cancer-wisconsin-data
"""
Набор по раку груди, Характеристики вычисляются на основе оцифрованного изображения
тонкоигольного аспирата (FNA) новообразования груди.
Они описывают характеристики ядер клеток, представленных на изображении.

"""
df7 = pd.read_csv('data1.csv')

"""
Информация об атрибутах:
1)'id' - Идентификационный номер
2) 'diagnosis' - Диагноз (M = злокачественный, B = доброкачественный)

Для каждого ядра клетки вычисляются десять характеристик с действительным знаком:
a) 'radius_mean' - радиус (среднее расстояние от центра до точек по периметру)
b)  'texture_mean' - текстура (стандартное отклонение значений шкалы серого)
c) 'perimeter_mean - периметр
d) 'area_se'  - площадь
e) 'smoothness_se' - гладкость (локальное изменение длины радиуса)
f) 'compactness_se'- компактность (периметр ^ 2 / площадь - 1,0)
ж) 'concavity_se' - вогнутость (степень вогнутости участков контура)
з) 'concave points_se' - точки вогнутости (количество вогнутых участков контура)
и) 'symmetry_se' - симметрия к
) 'fractal_dimension_se' - фрактальная размерность («приближение береговой линии» - 1)

Среднее значение, стандартная ошибка и «худшее» или наибольшее (среднее из трех
наибольших значений) этих характеристик были вычислены для каждого изображения, в
результате чего было получено 30 функций. Например, поле 3 - это средний радиус, поле
13 - радиус SE, поле 23 - худший радиус.
'radius_worst',
 'texture_worst',
 'perimeter_worst',
 'area_worst',
 'smoothness_worst',
 'compactness_worst',
 'concavity_worst',
 'concave points_worst',
 'symmetry_worst',
 'fractal_dimension_worst'
Все значения функций перекодированы с использованием четырех значащих цифр.

Отсутствующие значения атрибутов: нет

Распределение по классам: 357 доброкачественных, 212 злокачественных"""