import random
import os
import json
import librosa
from tqdm import tqdm
from glob import glob
from collections import defaultdict

from utils.util import has_existed
from preprocessors import GOLDEN_TEST_SAMPLES

def get_test_songs():
    golden_samples = GOLDEN_TEST_SAMPLES["opensinger"]
    # every item is a tuple (singer, song)
    golden_songs = [s.split("_")[:3] for s in golden_samples]
    # singer_song, eg: Female1#Almost_lover_Amateur
    return golden_songs

def opensinger_statistics(data_dir):
    singers = []
    songs = []
    singer2songs = defaultdict(lambda: defaultdict(list))

    gender_infos = glob(data_dir + "/*")

    for gender_info in gender_infos:
        gender_info_split = gender_info.split("/")[-1][:-3]

        singer_and_song_infos = glob(gender_info + "/*")

        for singer_and_song_info in singer_and_song_infos:
            singer_and_song_info_split = singer_and_song_info.split("/")[-1].split("_")
            singer_id, song = (
                singer_and_song_info_split[0],
                singer_and_song_info_split[1],
            )
            singer = gender_info_split + "_" + singer_id
            singers.append(singer)
            songs.append(song)

            utts = glob(singer_and_song_info + "/*.wav")

            for utt in utts:
                uid = utt.split("/")[-1].split("_")[-1].split(".")[0]
                singer2songs[singer][song].append(uid)

    unique_singers = list(set(singers))
    unique_songs = list(set(songs))
    unique_singers.sort()
    unique_songs.sort()

    print(
        "opensinger: {} singers, {} songs ({} unique songs)".format(
            len(unique_singers), len(songs), len(unique_songs)
        )
    )
    print("Singers: \n{}".format("\t".join(unique_singers)))
    return singer2songs, unique_singers

def main(output_path, dataset_path):
    print("-" * 10)
    print("Preparing test samples for opensinger...\n")

    save_dir = os.path.join(output_path, "opensinger")
    os.makedirs(save_dir, exist_ok=True)
    train_output_file = os.path.join(save_dir, "train.json")
    test_output_file = os.path.join(save_dir, "test.json")
    singer_dict_file = os.path.join(save_dir, "singers.json")
    utt2singer_file = os.path.join(save_dir, "utt2singer")
    if (
        has_existed(train_output_file)
        and has_existed(test_output_file)
        and has_existed(singer_dict_file)
        and has_existed(utt2singer_file)
    ):
        return
    utt2singer = open(utt2singer_file, "w")

    # Load
    opensinger_path = dataset_path

    singer2songs, unique_singers = opensinger_statistics(opensinger_path)
    test_songs = get_test_songs()

    # We select songs of standard samples as test songs
    train = []
    test = []

    train_index_count = 0
    test_index_count = 0

    train_total_duration = 0
    test_total_duration = 0

    for i, (singer, songs) in enumerate(singer2songs.items()):
        song_names = list(songs.keys())

        for chosen_song in tqdm(
            song_names, desc="Singer {}/{}".format(i, len(singer2songs))
        ):
            for chosen_uid in songs[chosen_song]:
                res = {
                    "Dataset": "opensinger",
                    "Singer": singer,
                    "Song": chosen_song,
                    "Uid": "{}_{}_{}".format(singer, chosen_song, chosen_uid),
                }
                res["Path"] = "{}Raw/{}_{}/{}_{}_{}.wav".format(
                    singer.split("_")[0],
                    singer.split("_")[1],
                    chosen_song,
                    singer.split("_")[1],
                    chosen_song,
                    chosen_uid,
                )
                res["Path"] = os.path.join(opensinger_path, res["Path"])
                assert os.path.exists(res["Path"])

                duration = librosa.get_duration(filename=res["Path"])
                res["Duration"] = duration

                if duration > 30:
                    print(
                        "Wav file: {}, the duration = {:.2f}s > 30s, which has been abandoned.".format(
                            res["Path"], duration
                        )
                    )
                    continue

                if (
                    [singer.split("_")[0], singer.split("_")[1], chosen_song]
                ) in test_songs:
                    res["index"] = test_index_count
                    test_total_duration += duration
                    test.append(res)
                    test_index_count += 1
                else:
                    res["index"] = train_index_count
                    train_total_duration += duration
                    train.append(res)
                    train_index_count += 1

                utt2singer.write("{}\t{}\n".format(res["Uid"], res["Singer"]))

    print("#Train = {}, #Test = {}".format(len(train), len(test)))
    print(
        "#Train hours= {}, #Test hours= {}".format(
            train_total_duration / 3600, test_total_duration / 3600
        )
    )

    # Save train.json and test.json
    with open(train_output_file, "w") as f:
        json.dump(train, f, indent=4, ensure_ascii=False)
    with open(test_output_file, "w") as f:
        json.dump(test, f, indent=4, ensure_ascii=False)

    # Save singers.json
    singer_lut = {name: i for i, name in enumerate(unique_singers)}
    with open(singer_dict_file, "w") as f:
        json.dump(singer_lut, f, indent=4, ensure_ascii=False)


#!bin/python3

def get_max_len_valid_substring(s):
    st = [-1]
    result = 0
    for i, n_i in enumerate(s):
        if n_i == '(':
            st.append(i)
        else:
            st.pop()
            if not st:
                st.append(i)
            else:
                curr_len = i - st[-1]
                if result < curr_len:
                    result = curr_len
    return result

num_t = int(input())
for i in range(num_t):
    s = input()
    print(get_max_len_valid_substring(s))


import os
import functools
from time import perf_counter

import torch
from torch.utils.tensorboard import SummaryWriter
import transformers

class AverageMeter(object):
    """Computes and stores the average and current value"""

    def __init__(self, name, fmt=':f'):
        self.name = name
        self.fmt = fmt
        self.reset()

    def reset(self):
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def update(self, val, n=1):
        self.val = val
        self.sum += val * n
        self.count += n
        self.avg = self.sum / self.count

    def __str__(self):
        fmtstr = '{name} {val' + self.fmt + '} ({avg' + self.fmt + '})'
        return fmtstr.format(**self.__dict__)

def compute_metrics(metrics: dict, preds: torch.Tensor, labels: torch.Tensor):
    results = {}
    for metric_name, metric in metrics.items():
        results[metric_name] = metric(preds, labels).item()

    return results

def reset_trackers(trackers: dict):
    for metric in trackers.values():
        metric.reset()

def update_trackers(trackers: dict, metrics_results: dict):
    for metric_name, metric in trackers.items():
        metric.update(metrics_results[metric_name])

def get_trackers_avg(trackers: dict):
    avg_results = {}
    for metric_name, metric in trackers.items():
        avg_results[metric_name] = metric.avg

    return avg_results

def write_to_tensorboard(writer: SummaryWriter, logs: dict, mode: str, step: int):
    for metric_name, value in logs.items():
        writer.add_scalar(f'{mode}/{metric_name}', value, step)

def maybe_save_checkpoint(model, path, epoch_num, save_freq):
    """
    Save a checkpoint
    Args:
        model: a nn.Module instance
        path: path to save checkpoint to
        epoch_num: current epoch number
        save_freq: save frequency based on epoch number

    """
    if not os.path.exists(path):
        os.makedirs(path)
    path = os.path.join(path, f'{epoch_num}.pt')
    if epoch_num % save_freq == 0:
        checkpoint = {'model': model.state_dict(),
                      'epoch': epoch_num}
        torch.save(checkpoint, path)
        print(f'Saved checkpoint to `{path}`')

def get_lm_module_name(model):
    """
    Return the name of the LM part e.g, distilbert, bert, roberta since we don't know which language model is used
    when loading model using AutoModel
    """
    modules = list(model.named_modules())[1:]  # ignore the main module
    for name, m in modules:
        if issubclass(m.__class__, transformers.PreTrainedModel):
            return name

def time_it(method):
    @functools.wraps(method)
    def inner(self, *args, **kwargs):
        start = perf_counter()
        out = method(self, *args, **kwargs)
        if type(out) != tuple:
            out = out,
        end = perf_counter()
        return tuple([*out, end - start])

    return inner

def clean_text(text, invalid_chars):
    text_ = text.split()
    text_ = [word for word in text_ if not word.isascii()]
    cleaned = ''
    valid = True
    for i, word in enumerate(text_):
        for x in invalid_chars:
            if x in word:
                valid = False
                break
        if valid:
            cleaned += f'{word} '
    return cleaned.strip()


import os, re, subprocess

def mkdir(Dir):

    nSlash = Dir.count('/')
    iSlash = 1*(Dir.startswith('/'))

    if (nSlash == 0):
        if not os.path.exists(Dir):
            subprocess.call('mkdir %s' % d, shell=True)
        return

    for i in range(iSlash+1, Dir.count('/')+1):
        idxs   = [x.start() for x in re.finditer('/', Dir)]
        Subdir = Dir[0:idxs[i-1]]
        if not os.path.exists(Subdir):
            subprocess.call('mkdir %s' % Subdir, shell=True)
    if not os.path.exists(Dir):
        subprocess.call('mkdir %s' % Dir, shell=True)


#!/usr/env python
import string
import re

import numpy as np
import pandas as pd
from rantanplan import get_scansion
from rantanplan.structures import STRUCTURES
from sklearn.metrics import classification_report
from sklearn.preprocessing import LabelEncoder
from sklearn.tree import DecisionTreeClassifier, export_text

def export_py_code(tree, feature_names, max_depth=100, spacing=4):
    if spacing < 2:
        raise ValueError('spacing must be > 1')
    # Clean up feature names (for correctness)
    nums = string.digits
    alnums = string.ascii_letters + nums
    clean = lambda s: ''.join(c if c in alnums else '_' for c in s)
    features = [clean(x) for x in feature_names]
    features = ['_'+x if x[0] in nums else x for x in features if x]
    if len(set(features)) != len(feature_names):
        raise ValueError('invalid feature names')
    # First: export tree to text
    res = export_text(tree, feature_names=features,
                        max_depth=max_depth,
                        decimals=6,
                        spacing=spacing-1)
    # Second: generate Python code from the text
    skip, dash = ' '*spacing, '-'*(spacing-1)
    code = 'def decision_tree({}):\n'.format(', '.join(features))
    for line in repr(tree).split('\n'):
        code += skip + "# " + line + '\n'
    for line in res.split('\n'):
        line = line.rstrip().replace('|',' ')
        if '<' in line or '>' in line:
            line, val = line.rsplit(maxsplit=1)
            line = line.replace(' ' + dash, 'if')
            line = '{} {:g}:'.format(line, float(val))
        else:
            line = line.replace(' {} class:'.format(dash), 'return')
        code += skip + line + '\n'
    return code

def pad(l, size, element=0):
    return l + [0] * (size - len(l))

def features(scansion, size=100):
    pattern = [s.get("rhyme", 0) for s in scansion]
    min_lengths = [
        s["rhythm"].get("length_range", {}).get("min_length", s["rhythm"]["length"])
        for s in scansion
    ]
    max_lengths = [
        s["rhythm"].get("length_range", {}).get("max_length", s["rhythm"]["length"])
        for s in scansion
    ]
    rhyme_types = [s.get("rhyme_type") for s in scansion]
    if "assonant" in rhyme_types:
        rhyme_type = "assonant"
    elif "consonant" in rhyme_types:
        rhyme_type = "consonant"
    else:
        rhyme_type = "unrhymed"
    structure = scansion[0].get("structure", "unknown")
    return (
        [structure, rhyme_type]
        + pad(pattern, size, element="")
        + pad(min_lengths, size) + pad(max_lengths, size)
    )

def features_regex(scansion, size=100):
    pattern = "".join([s.get("rhyme", "") for s in scansion])
    rhyme_types = [s.get("rhyme_type") for s in scansion]
    if "assonant" in rhyme_types:
        rhyme_type = "assonant"
    elif "consonant" in rhyme_types:
        rhyme_type = "consonant"
    else:
        rhyme_type = "unrhymed"
    length_ranges = [
        range(
            line["rhythm"].get("length_range", {}).get("min_length", line["rhythm"]["length"]),
            line["rhythm"].get("length_range", {}).get("max_length", line["rhythm"]["length"]) + 1)
        for line in scansion]
    regex_features = []
    lengths_features = []
    for _, name, regex, lengths_func in STRUCTURES:
        if callable(regex):
            regex_features.append(int(regex(pattern)))
        else:
            regex_features.append(
                int(bool(re.compile(regex, re.VERBOSE).fullmatch(pattern)))
            )
        lengths_features.append(int(lengths_func(length_ranges)))
    structure = scansion[0].get("structure", "unknown")
    return [structure, rhyme_type] + regex_features + lengths_features

def main(use_regex=False):
    stanzas = pd.read_csv(
        open("evaluation-final.tsv"),
        sep="\t", names=["text", "label"], header=0
    )
    stanzas.label = stanzas.label.apply(
        lambda x: "unknown" if x == "None" else x
    )
    if not use_regex:
        feats = stanzas.text.apply(
            lambda x: features(get_scansion(x, rhyme_analysis=True))
        )
        feats_names = (
            ["rhyme_type"]
            + [f"rhyming_pattern_verse_{i}" for i in range(1, 101)]
            + [f"min_length_verse_{i}" for i in range(1, 101)]
            + [f"max_length_verse_{i}" for i in range(1, 101)]
        )
    else:
        feats = stanzas.text.apply(
            lambda x: features_regex(get_scansion(x, rhyme_analysis=True))
        )
        feats_names = (
            ["rhyme_type"]
            + [f"{name}_regex" for _, name, *_ in STRUCTURES]
            + [f"{name}_lengths" for _, name, *_ in STRUCTURES]
        )

    feats_df = pd.DataFrame(
        np.vstack(feats),
        columns=["predicted"] + feats_names)
    stanzas_feats = pd.concat([stanzas, feats_df], axis=1, ignore_index=True)
    stanzas_feats.columns = stanzas.columns.tolist() + feats_df.columns.tolist()
    stanzas_feats.to_csv("stanzas_feats.csv", header=False)
    train = stanzas_feats[stanzas_feats.label == stanzas_feats.predicted]
    test = stanzas_feats[stanzas_feats.label != stanzas_feats.predicted]

    X_train = train.drop(["text", "label", "predicted"], axis=1)
    y_train = train["label"]

    encoder_columns = ["rhyme_type"]
    if not use_regex:
        encoder_columns += [f"rhyming_pattern_verse_{i}" for i in range(1, 101)]

    encoders = {col: LabelEncoder().fit(stanzas_feats[col])
                for col in encoder_columns}
    for column in encoder_columns:
        X_train[column] = encoders[column].transform(X_train[column])

    clf = DecisionTreeClassifier().fit(X_train, y_train)

    X_test = test.drop(["text", "label", "predicted"], axis=1)
    y_test = test["label"]
    for column in encoder_columns:
        X_test[column] = encoders[column].transform(X_test[column])

    print(export_text(clf, feature_names=feats_names, max_depth=1e3))
    [print(k, e.classes_) for k, e in encoders.items()]
    print(clf.score(X_train, y_train))
    print(classification_report(y_test, clf.predict(X_test)))

if __name__ == "__main__":
    main()


#Please copy the completed function from above into this active code window. Now write a function called get_movie_rating.
#It takes an OMDB dictionary result for one movie and extracts the Rotten Tomatoes rating as an integer.
#For example, if given the OMDB dictionary for “Black Panther”, it would return 97. If there is no Rotten Tomatoes rating, return 0.

# some invocations that we use in the automated tests; uncomment these if you are getting errors and want better error messages
# get_movie_rating(get_movie_data("Deadpool 2"))
import requests_with_caching
import json

def get_movie_data(title):
    endpoint = 'http://www.omdbapi.com/'
    param = {}
    param['t'] = title
    param['r'] = 'json'
    this_page_cache = requests_with_caching.get(endpoint, params=param)
    return json.loads(this_page_cache.text)
print(get_movie_data("Black Panther")['Ratings'][1])
def get_movie_rating(dic):
    ranking = dic['Ratings']
    for dic_item in ranking:
        if dic_item['Source'] == 'Rotten Tomatoes':
            return int(dic_item['Value'][:-1])
    return 0

get_movie_rating(get_movie_data("Deadpool 2"))

Python 3 Programming/Course 3- Data Collection and Processing with Python/Assessment 3 - Project OMDB and TasteDive Mashup/Assessment 3.5.py


class title_class:
    def __init__(self,name:str,parent) -> None:
        self.name = name
        self.parent = parent
        self.child_display = []
    def action(self,function:str,new_parent):
        if function == "test1" and function in [i.name for i in self.parent.child_display]:
            self.child_display=[
                title_class(name="teir2_1",parent=new_parent),
                title_class(name="teir2_2",parent=new_parent),
                title_class(name="back",parent=new_parent),
            ]
            text_display(self)
        elif function == "teir2_1" and function in [i.name for i in self.parent.child_display]:
            self.child_display=[
                title_class(name="teir3_1",parent=new_parent),
                title_class(name="teir3_2",parent=new_parent),
                title_class(name="back",parent=new_parent),
            ]
            text_display(self)
        elif function == "teir3_1" and function in [i.name for i in self.parent.child_display]:
            self.child_display=[
                title_class(name="teir4_1",parent=new_parent),
                title_class(name="teir4_2",parent=new_parent),
                title_class(name="back",parent=new_parent),
            ]
            text_display(self)
        elif function == "teir3_2" and function in [i.name for i in self.parent.child_display]:
            self.child_display=[
                title_class(name="teir4_3",parent=new_parent),
                title_class(name="teir4_4",parent=new_parent),
                title_class(name="back",parent=new_parent),
            ]
            text_display(self)
        elif function == "teir2_2" and function in [i.name for i in self.parent.child_display]:
            self.child_display=[
                title_class(name="teir3_3",parent=new_parent),
                title_class(name="teir3_4",parent=new_parent),
                title_class(name="back",parent=new_parent),
            ]
            text_display(self)
        elif function == "back" and function in [i.name for i in self.parent.child_display]:
            back_display(self.parent)
        elif function == "test2":
            print("test2")
        elif function == "close":
            print("test3")

    #def select(self):
    #    if self.name:
    #        print("you are in combact what will you do?")
    #        for st in self.child_display:
    #            pass
#if is_player, entity.title = combact then entity.title.select
#menu, start and create character= select stats and levels you add to yourself
#run function and then reprint and run again. the input as a selector and break
    #def create_combact_text():
    #    self.child_display
    #def function1
    #def function2

    #def action()

title = title_class(name="title",parent=None)
title.child_display = [title_class(name="test1",parent=title),title_class(name="test2",parent=title),title_class(name="back",parent=title)]
def text_display(title_input:title_class):
    for i in title_input.child_display:
        print(f"{i.name}")

    select = input("Type Here:")
    #exceptation handling
    i.action(function=select,new_parent=i)
    #enum class then pass in enum values

def back_display(title_input:title_class):
    #because your going off child display you need to parent twice to find the parent of this branch
    text_display(title_input.parent)

text_display(title)

#activate certain text with other functions go into text then end on a battle function and by the ned of the battle function go back into the text
def title_list(title_object):
    for n,i in enumerate(title_object.child_display):
        print(f"({n}){i}")

#you can pass a function in as a input

#round counter method, changes every object to run a round pass method to boost objects

        #print list
        #   then if input == name then function again.
        #
        #if name = __ and name in parent.child_list_names then execute that function:
        #
        #    function that changes the selected title's children titles and then displays them

        #combat menu, options during combat.
        #inventory
        #attack
        #run (sometimes)

        #two modes of combat

        #either one takes the move up.


num = input("Digite um número inteiro: ")
tam=len(num)
num=int(num)
i=1

digigual=False

while i<tam and not digigual:
    dig1=num%10
    num=num//10
    dig2=num%10
    i=i+1
    if dig1==dig2:
        digigual=True

if digigual:
    print("sim")
else:
    print("não")


import requests
from bs4 import BeautifulSoup
from PIL import Image
from io import BytesIO
donatelink = 'https://act.biologicaldiversity.org/onlineactions/mLBxIVrBY0-PF8_oJWJbEw2?utm_expid=.QGpwm5uyRCWKJOr0upwtRQ.2&utm_referrer=https%3A%2F%2Fwww.biologicaldiversity.org%2F'
home = requests.get('https://www.biologicaldiversity.org/species/')
soup = BeautifulSoup(home.text, 'lxml')
mainlinks = soup.find(class_ = 'col cbd_content_main').find_all('a')
speclist = [spec.text for spec in mainlinks]
refs = ['https://www.biologicaldiversity.org/species/' + link['href'] for link in mainlinks]

def userprogram():

    for a, b in enumerate(speclist, 1):
        print("{}. {}".format(a, b))
    i = int(input('Type a number 1-7 to explore a subject: '))
    page = requests.get(refs[i-1])
    soup2 = BeautifulSoup(page.text, 'lxml')
    species_img_url = soup2.find(class_='banner').find('img')['src']
    species_img_link = 'https://www.biologicaldiversity.org/' + species_img_url
    species_pic = requests.get(species_img_link)
    species_img = Image.open(BytesIO(species_pic.content))
    species_img.show()
    speciesinfo = [paragraph.text for paragraph in soup2.find(class_= 'col cbd_content_main').find_all('p')]
    print(speclist[i-1])
    for info in speciesinfo:
        if len(info) > 5:
            print(info)
            print("-"*20)
        else:
            pass
    ii = input('Would you like a list of {} that you could help to save? (type y/n) '.format(speclist[i-1]))
    if ii.lower() == 'y':
        species_img.close()
        creature_page = soup2.find(class_='col-md-4 cbd_content_sidebar').find_all('a')
        creature_links = ['https://www.biologicaldiversity.org/species/' + speclist[i-1].lower() + '/' + creature['href'] for creature in creature_page if len(creature.text) > 2]
        creature_list = [creature.text for creature in creature_page if len(creature.text) > 2]
        for a, b in enumerate(creature_list, 1):
            print('{}. {}'.format(a, b.title()))
        print('Type a number, 1-{} and press enter to view'.format(len(creature_list)))
        iii = int(input(''))
        creature_site = requests.get(creature_links[iii-1])
        creature_soup = BeautifulSoup(creature_site.text, 'lxml')
        creature_summary = creature_soup.find(class_='col-md-6 cbd_content_left').find_all('p')
        creature_img_url = creature_soup.find(class_='banner').find('img')['src']
        creature_img_link = 'https://www.biologicaldiversity.org/' + creature_img_url
        r = requests.get(creature_img_link)
        species_img = Image.open(BytesIO(r.content))
        species_img.show(title='creature_list[iii-1]')
        print('-'*20)
        print('Here\'s a pic of the {}'.format(creature_list[iii-1]))
        print('-'*20)

        for sec in creature_summary:
            print(sec.text)
            print('-'*20)
        print('The {} needs your help!  Click this link to donate: '.format(creature_list[iii-1]))
        print(donatelink)

userprogram()


import FWCore.ParameterSet.Config as cms
import copy

from CMGTools.H2TauTau.eventContent.common_cff import common, commonDebug

diTauSpecific = [
    ]

diTauSpecificDebug = copy.deepcopy( diTauSpecific)
diTauSpecificDebug.extend( [
    ] )

diTau = copy.deepcopy( common )
diTau.extend(diTauSpecific)

diTauDebug = copy.deepcopy( commonDebug )
diTauDebug.extend( diTauSpecificDebug )


from cs50 import get_string
from cs50 import get_int

total = 0
shipping_cost = 0
international = ""

# Only accepts yes or no answer, single letter or full word
# Will ask again if invalid response
# Capitalizaton doesn't matter because response will be forced into lowercase not matter what
while (international.lower() not in ["y", "yes", "n", "no"]):
    # Get user input
    international = get_string("Is this being shipped international? ")

# International fee if applicable
if international in ["y", "yes"]:
    shipping_cost += 15
    print("An international fee has been applied")
    print("--------------------")

# Get total from user
while (total <= 0):
    total = get_int("What is the total of purchase? ")

# Shipping calculation
if total <= 30:
    shipping_cost += 20
elif total <= 50:
    shipping_cost += 10
else:
    shipping_cost += 0

# Print calculation
print(f"Total cost: ${total}")
print(f"Shipping cost: ${shipping_cost}")
print("--------------------")
print(f"Grand total: ${total+shipping_cost}")

exit()


# 視圖 - 直接面對使用者的使用者介面設計
from flask import Flask, request, jsonify, render_template

from app import app, db
from models import Record

@app.route("/")
def index():
    return render_template('index.html', title='To-Do List')

@app.route("/record", methods=['POST'])
def add_record():
    req_data = request.form
    todo = req_data['name']
    date = req_data['cost']
    record = Record(todo=todo, date=date)
    db.session.add(record)
    db.session.commit()
    return 'Create Succeeded', 200

@app.route("/record", methods=['GET'])
def get_records():
    records = Record.query.all()
    records_data = [
        {
            'id': record.id,
            'name': record.todo,
            'cost': record.date
        }
        for record in records
    ]
    return jsonify(records_data), 200

@app.route('/record/<int:record_id>', methods=['GET'])
def get_record(record_id):
    record = Record.query.filter_by(id=record_id).first()
    record_data = {
        'id': record.id,
        'name': record.todo,
        'cost': record.date
    }
    return jsonify(record_data), 200

@app.route('/record/<int:record_id>', methods=['PUT'])
def update_record(record_id):
    req_data = request.form
    record = Record.query.filter_by(id=record_id).first()
    record.todo = req_data['name']
    record.date = req_data['cost']
    db.session.add(record)
    db.session.commit()
    return 'Update Succeeded', 200

@app.route("/record/<int:record_id>", methods=["DELETE"])
def delete_record(record_id):
    record = Record.query.filter_by(id=record_id).first()
    db.session.delete(record)
    db.session.commit()
    return 'Delete Succeeded', 200


import cv2 as cv
import os
from imutils import paths
import shutil

def listPosImagem():

    # Entra na pasta contendo as imagens positivas
    imagemPath = list(paths.list_images('Imagens/comMascara'))
    numero = 1

    # Cria uma pasta chamada 'pos'
    if not os.path.exists('pos'):
        os.makedirs('pos')

    # Percorre as imagens da pasta e cria uma imagem formatada em escala cinza
    for foto in imagemPath:

        foto.replace(foto, f"pos/{numero}.png")
        shutil.copy(foto, foto.replace(foto, f"pos/{numero}.png"))
        img = cv.imread(f"pos/{numero}.png", cv.IMREAD_GRAYSCALE)
        resized_image = cv.resize(img, (100, 100))
        cv.imwrite(f"pos/{numero}.png", resized_image)

        print(foto.replace(foto, f"pos/{str(numero)}.png"))

        numero += 1

# Chama a função
listPosImagem()


import numpy as np
import pandas as pd
import seaborn as sns
from matplotlib.axes import Axes
from matplotlib import pyplot as plt

def plot_kde(data, xlabel, ylabel, title, clip):
	sns.set_style("darkgrid")
	plot: Axes = sns.kdeplot(data=data,
	                         shade=True,
	                         kernel="gau",
	                         clip=clip,
	                         legend=False,
	                         color=sns.xkcd_rgb["dark red"])
	plot.set_title(title, fontdict={"size": 14})
	plot.set_xlabel(xlabel)
	plot.set_ylabel(ylabel)

df: pd.DataFrame = pd.read_csv("final_dns_dataset.csv", usecols=[5,9,10,11,12,13])
df.rename({"authoritative_state": "state", "authoritative_latitude": "lat", "authoritative_longitude": "lng"}, inplace=True, axis=1)
df = df.groupby(["lat", "lng"], as_index=False).agg({"state": "first", "rtt": ["median", "mean", "std"], "rtt_normalized": ["median", "mean", "std"]})

df = df["rtt_normalized"]["median"]
quantiles = df.quantile([0.025, 0.975]).to_numpy()
plot_kde(data=df,
         xlabel="Normalized RTT median per location (ms)",
         ylabel="Density",
         title="Distribution of DNS Normalized RTT Median by Location",
         clip=quantiles)
plt.show()


# Code from Chapter 8 of Machine Learning: An Algorithmic Perspective
# by Stephen Marsland (http://seat.massey.ac.nz/personal/s.r.marsland/MLBook.html)

# You are free to use, change, or redistribute the code in any way you wish for
# non-commercial purposes, but please maintain the name of the original author.
# This code comes with no warranty of any kind.

# Stephen Marsland, 2008

# Plots of three 2D Gaussians

from pylab import *
from numpy import *

x = arange(-5,5,0.01)
s = 1
mu = 0
y = 1/(sqrt(2*pi)*s) * exp(-0.5*(x-mu)**2/s**2)
plot(x,y,'k')

close('all')
mu = array([2,-3])
s = array([1,1])
#s = array([0.5,2])
x = random.normal(mu,scale=s,size = (500,2))
plot(x[:,0],x[:,1],'ko')
#axis(array([0,3,-8,4]))
axis('equal')

theta = arange(0,2.1*pi,pi/20)

plot(mu[0]+2*cos(theta),mu[1]+2*sin(theta),'k-')
plot(mu[0]+3*cos(theta),mu[1]+3*sin(theta),'k-')

figure()

mu = array([2,-3])
s = array([0.5,2])
x = random.normal(mu,scale=s,size = (500,2))
phi = 2*pi/3
plot(x[:,0]*cos(phi)+x[:,1]*sin(phi),x[:,0]*(-sin(phi)) + x[:,1]*cos(phi),'ko')
axis('equal')

theta = arange(0,2.1*pi,pi/20)
plot((mu[0]+3*s[0]*cos(theta))*cos(phi)+(mu[1]+3*s[1]*sin(theta))*sin(phi), (mu[0]+3*s[0]*cos(theta))*sin(-phi)+(mu[1]+3*s[1]*sin(theta))*cos(phi), 'k-')

figure()
mu = array([2,-3])
s = array([0.5,2])
x = random.normal(mu,scale=s,size = (500,2))
plot(x[:,0],x[:,1],'ko')
axis('equal')

theta = arange(0,2.1*pi,pi/20)
plot(mu[0]+3*s[0]*cos(theta),mu[1]+3*s[1]*sin(theta), 'k-')

show()


#!/usr/bin/env python

from pyspark.sql import SparkSession

spark = SparkSession \
    .builder \
    .appName("Python Spark SQL basic example") \
    .config("spark.some.config.option", "some-value") \
    .getOrCreate()

# Load the Parquet file
on_time_dataframe = spark.read.parquet('data/on_time_performance.parquet')

# Use SQL to look at the total flights by month across 2015
on_time_dataframe.registerTempTable("on_time_dataframe")
total_flights_by_month = spark.sql(
"""Select Month, Year, Count(*) as total_flights
from on_time_dataframe
group by Year, Month
order by Year, Month"""
)

# This map/asDict trick makes the rows print a little prettier,
# It is optional
flights_chart_data = total_flights_by_month.rdd.map(lambda row: row.asDict())
print(flights_chart_data.collect())

# Save chart to MongoDB
import pymongo_spark
pymongo_spark.activate()
flights_chart_data.saveToMongoDB('mongodb://localhost:27017/agile_data_science.flights_by_month')

'''
[root@localhost workspace]# python total_flights.py
Setting default log level to "WARN".
To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).
17/11/07 14:35:03 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
17/11/07 14:35:03 WARN SparkConf:
SPARK_CLASSPATH was detected (set to '/root/workspace/lib/snappy-java-1.1.2.6.jar').
This is deprecated in Spark 1.0+.

Please instead use:
 - ./spark-submit with --driver-class-path to augment the driver classpath
 - spark.executor.extraClassPath to augment the executor classpath

17/11/07 14:35:03 WARN SparkConf: Setting 'spark.executor.extraClassPath' to '/root/workspace/lib/snappy-java-1.1.2.6.jar' as a work-around.
17/11/07 14:35:03 WARN SparkConf: Setting 'spark.driver.extraClassPath' to '/root/workspace/lib/snappy-java-1.1.2.6.jar' as a work-around.
17/11/07 14:35:03 WARN Utils: Your hostname, localhost.localdomain resolves to a loopback address: 127.0.0.1; using 192.168.1.103 instead (on interface eth0)
17/11/07 14:35:03 WARN Utils: Set SPARK_LOCAL_IP if you need to bind to another address
17/11/07 14:35:06 WARN Utils: Service 'SparkUI' could not bind on port 4040. Attempting port 4041.
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
[{'Year': '2015', 'Month': '1', 'total_flights': 469968}, {'Year': '2015', 'Month': '10', 'total_flights': 486165},
{'Year': '2015', 'Month': '11', 'total_flights': 467972}, {'Year': '2015', 'Month': '12', 'total_flights': 479230},
{'Year': '2015', 'Month': '2', 'total_flights': 429191}, {'Year': '2015', 'Month': '3', 'total_flights': 504312},
{'Year': '2015', 'Month': '4', 'total_flights': 485151}, {'Year': '2015', 'Month': '5', 'total_flights': 496993},
{'Year': '2015', 'Month': '6', 'total_flights': 503897}, {'Year': '2015', 'Month': '7', 'total_flights': 520718},
{'Year': '2015', 'Month': '8', 'total_flights': 510536}, {'Year': '2015', 'Month': '9', 'total_flights': 464946}]
'''

'''
[root@localhost workspace]# mongo agile_data_science
MongoDB shell version v3.4.2
connecting to: mongodb://127.0.0.1:27017/agile_data_science
MongoDB server version: 3.4.2
Server has startup warnings:
2017-08-09T15:01:54.581-0700 I STORAGE  [initandlisten]
2017-08-09T15:01:54.581-0700 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2017-08-09T15:01:54.581-0700 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2017-08-09T15:01:55.840-0700 I CONTROL  [initandlisten]
2017-08-09T15:01:55.840-0700 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-08-09T15:01:55.840-0700 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-08-09T15:01:55.840-0700 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-08-09T15:01:55.840-0700 I CONTROL  [initandlisten]
2017-08-09T15:01:55.843-0700 I CONTROL  [initandlisten]
2017-08-09T15:01:55.843-0700 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-08-09T15:01:55.843-0700 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-08-09T15:01:55.844-0700 I CONTROL  [initandlisten]
2017-08-09T15:01:55.844-0700 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-08-09T15:01:55.844-0700 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-08-09T15:01:55.844-0700 I CONTROL  [initandlisten]
> db.flights_by_month.find().sort({"Year": 1, "Month": 1})
{ "_id" : ObjectId("599a09f14e9540530def1276"), "total_flights" : 469968, "Month" : "1", "Year" : "2015" }
{ "_id" : ObjectId("599a0aaf4e954054637f7559"), "total_flights" : 469968, "Month" : "1", "Year" : "2015" }
{ "_id" : ObjectId("5a0235464e954017f9eff004"), "total_flights" : 469968, "Month" : "1", "Year" : "2015" }
{ "_id" : ObjectId("599a09f34e9540530def1279"), "total_flights" : 486165, "Month" : "10", "Year" : "2015" }
{ "_id" : ObjectId("599a0ab04e954054637f755c"), "total_flights" : 486165, "Month" : "10", "Year" : "2015" }
{ "_id" : ObjectId("5a0235464e954017f9eff007"), "total_flights" : 486165, "Month" : "10", "Year" : "2015" }
{ "_id" : ObjectId("599a09f44e9540530def127c"), "total_flights" : 467972, "Month" : "11", "Year" : "2015" }
{ "_id" : ObjectId("599a0ab04e954054637f755f"), "total_flights" : 467972, "Month" : "11", "Year" : "2015" }
{ "_id" : ObjectId("5a0235464e954017f9eff00a"), "total_flights" : 467972, "Month" : "11", "Year" : "2015" }
{ "_id" : ObjectId("599a09f44e9540530def127f"), "total_flights" : 479230, "Month" : "12", "Year" : "2015" }
{ "_id" : ObjectId("599a0ab04e954054637f7562"), "total_flights" : 479230, "Month" : "12", "Year" : "2015" }
{ "_id" : ObjectId("5a0235464e954017f9eff00d"), "total_flights" : 479230, "Month" : "12", "Year" : "2015" }
{ "_id" : ObjectId("599a09f44e9540530def1282"), "total_flights" : 429191, "Month" : "2", "Year" : "2015" }
{ "_id" : ObjectId("599a0ab04e954054637f7565"), "total_flights" : 429191, "Month" : "2", "Year" : "2015" }
{ "_id" : ObjectId("5a0235464e954017f9eff010"), "total_flights" : 429191, "Month" : "2", "Year" : "2015" }
{ "_id" : ObjectId("599a09f44e9540530def1285"), "total_flights" : 504312, "Month" : "3", "Year" : "2015" }
{ "_id" : ObjectId("599a0ab04e954054637f7568"), "total_flights" : 504312, "Month" : "3", "Year" : "2015" }
{ "_id" : ObjectId("5a0235474e954017f9eff013"), "total_flights" : 504312, "Month" : "3", "Year" : "2015" }
{ "_id" : ObjectId("599a09f44e9540530def1288"), "total_flights" : 485151, "Month" : "4", "Year" : "2015" }
{ "_id" : ObjectId("599a0ab14e954054637f756b"), "total_flights" : 485151, "Month" : "4", "Year" : "2015" }
Type "it" for more
'''


from flask_restplus import Namespace

from api.common.resource import EntityCollectionResource, EntityResource
from api.environment.model import environmentModel, EnvironmentSchema

api = Namespace('environments', description='Ações relacionadas a ambientes')
api.models[environmentModel.name] = environmentModel

@api.route('')
class EnvironmentsResource(EntityCollectionResource):
    schema = EnvironmentSchema

    @api.expect(environmentModel)
    def post(self):
        return super().post()

@api.route('/<string:id>')
@api.param('id', 'Environment identifier')
class EnvironmentResource(EntityResource):
    schema = EnvironmentSchema


from django.forms import ModelForm,widgets
from .models import Project,Review
from django import forms

class ProjectForm(ModelForm):
    class Meta:
        model = Project
        fields = ['title','description','featured_image','vote_total','vote_ratio','tags']

        widgets ={
            'tags':forms.CheckboxSelectMultiple(),
        }

    def __init__(self,*args ,**kwargs):
        super(ProjectForm,self).__init__(*args,**kwargs)

        for name ,filed in self.fields.items():
            filed.widget.attrs.update({'class':'input'})

        # self.fields['title'].widget.attrs.update(
        #     {'class':'input'}
        # )
        #
        # self.fields['description'].widget.attrs.update({
        #     'class':'input'
        # })

class ReviewForm(ModelForm):
    class Meta:
        model = Review
        fields = ['value','body']
        labels = {
            'value':'Place Your Vote',
            'body':'Add a comment for this project'
        }

    def __init__(self,*args,**kwargs):
        super(ReviewForm,self).__init__(*args,**kwargs)

        for name,filed in self.fields.items():
            filed.widget.attrs.update({'class':'input'})


from django.shortcuts import render

def django(request):
    """A view that renders a django template."""
    return render(
        request,
        'superadvanced/django.html',
        {'title': 'This is a Django template #superadvanced'})

def jinja(request):
    """A view that renders a jinja template."""
    return render(
        request,
        'superadvanced/jinja.html',
        {'title': 'This is a Jinja2 template #superadvanced'})


class Solution(object):
    def heightChecker(self, heights):
        """
        :type heights: List[int]
        :rtype: int
        """
        a = sorted(heights)
        count = 0
        for idx, element in enumerate(a):
            if element!=heights[idx]:
                count+=1
        return(count)


from dataclasses import dataclass
from unittest import mock

import pytest

from genie.backends.database.backend import DatabaseBackend
from genie.backends.database.models import BaseModel

@dataclass
class TestModel(BaseModel):
    table_name = 'Test'
    name: str

class TestBaseModel:

    @pytest.fixture
    def model(self):
        return TestModel(name='test')

    def test_connection_returns_instance(self, model):
        assert isinstance(model.connection(), DatabaseBackend)

    def test_to_dict_calls_asdict(self, model):
        with mock.patch(
            'genie.backends.database.models.asdict'
        ) as mock_asdict:
            model.to_dict()

        mock_asdict.assert_called_with(model)

    def test_get_calls_connection(self, model):
        with mock.patch(
            'genie.backends.database.backend.DatabaseBackend.get'
        ) as connection:
            TestModel.get(a='a', b='b')

        connection.assert_called_with(a='a', b='b')

    def test_delete_calls_connection(self, model):
        with mock.patch(
            'genie.backends.database.backend.DatabaseBackend.delete'
        ) as connection:
            TestModel.delete(a='a')

        connection.assert_called_with(a='a')

    def test_save_calls_connection(self, model):
        with mock.patch(
            'genie.backends.database.backend.DatabaseBackend.save'
        ) as connection:
            model.save()

        connection.assert_called_with(**model.to_dict())


import math
import torch

from torch import nn

class DEformerCSDI(nn.Module):
    def __init__(
        self,
        n_feats,
        idx_embed_dim,
        mlp_layers,
        nhead,
        dim_feedforward,
        num_layers,
        dropout,
    ):
        super().__init__()
        initrange = 0.1

        self.idx_embedding = nn.Embedding(n_feats, idx_embed_dim)

        mlps = {}
        for which_mlp in ["obs", "unobs"]:
            mlp = nn.Sequential()
            in_feats = idx_embed_dim + 2 if which_mlp == "obs" else idx_embed_dim + 1
            for (layer_idx, out_feats) in enumerate(mlp_layers):
                mlp.add_module(f"layer{layer_idx}", nn.Linear(in_feats, out_feats))
                if layer_idx < len(mlp_layers) - 1:
                    mlp.add_module(f"relu{layer_idx}", nn.ReLU())

                in_feats = out_feats

            mlps[which_mlp] = mlp

        self.mlps = nn.ModuleDict(mlps)

        d_model = mlp_layers[-1]
        self.d_model = d_model
        encoder_layer = nn.TransformerEncoderLayer(
            d_model, nhead, dim_feedforward, dropout
        )
        self.transformer = nn.TransformerEncoder(encoder_layer, num_layers)

        self.final_layer = nn.Linear(d_model, 1)
        self.final_layer.weight.data.uniform_(-initrange, initrange)
        self.final_layer.bias.data.zero_()

    def forward(self, tensors):
        device = list(self.final_layer.parameters())[0].device

        obs_vals = tensors["obs_vals"].unsqueeze(1).to(device)
        obs_times = tensors["obs_times"].unsqueeze(1).to(device)
        obs_feat_idx_embeds = self.idx_embedding(tensors["obs_feat_idxs"].to(device))
        obs_inputs = torch.cat([obs_vals, obs_times, obs_feat_idx_embeds], dim=1)
        obs_inputs = self.mlps["obs"](obs_inputs) * math.sqrt(self.d_model)

        unobs_times = tensors["unobs_times"].unsqueeze(1).to(device)
        unobs_feat_idx_embeds = self.idx_embedding(
            tensors["unobs_feat_idxs"].to(device)
        )
        unobs_inputs = torch.cat([unobs_times, unobs_feat_idx_embeds], dim=1)
        unobs_inputs = self.mlps["unobs"](unobs_inputs) * math.sqrt(self.d_model)

        all_inputs = torch.cat([obs_inputs, unobs_inputs]).unsqueeze(0).permute(1, 0, 2)

        outputs = self.transformer(all_inputs).permute(1, 0, 2).squeeze(0)
        n_obs = len(obs_inputs)
        preds = self.final_layer(outputs[n_obs:])

        return preds


from sklearn import linear_model
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(1)

# 第一步 生成数据
X = np.random.rand(20, 1) * 10 # sklearn线性模型只接受二维数据
y = 3 * X + (5 + np.random.randn(20, 1))

# 第二步 定义模型
model = linear_model.LinearRegression()

# 第三步 拟合数据
model.fit(X, y)

# 第四步 预测（以下两种方法都可以）
# y_pred = k * X + b
y_pred = model.predict(X)

# 第四步 展示拟合结果
b = model.intercept_.item()
k = model.coef_.item()
print('截距为：', b)  #截距
print('斜率为：', k)  #线性模型的系数

plt.figure(figsize = (12,12))
plt.scatter(X.reshape(-1), y.reshape(-1))
plt.plot(X.reshape(-1), y_pred.reshape(-1), 'r-', lw=5)
plt.tick_params(labelsize=20)
plt.xlabel('X', fontsize = 25)
plt.ylabel('y', fontsize = 25)
plt.xlim(0, 10)
plt.ylim(8, 35)
plt.title('Sklearn result of linear regression', fontsize = 30)
plt.show()


# This file contains the code for the implementation of the Hill Climbing.
# This file is written by Chongwu Guo.
import numpy as np
import math
import time

import matplotlib.pyplot as plt

def readData(filename):
    filename = 'DATA/'+ filename
    f = open(filename,'r')
    line = f.readline()
    line = f.readline()
    line = f.readline().split()
    dimension = int( line[1])
    cities = []

    citycoors = [[0 for x in range(2)] for y in range(dimension)]
    distMatrix = [[0 for x in range(dimension)] for y in range(dimension)]
    f.readline()
    f.readline()
    for i in range(0,dimension):
        line = f.readline().split()
        citycoors[i][0] = int(float(line[1]))
        citycoors[i][1] = int(float(line[2]))
        cities.append(int(line[0])-1)

    for i in range(dimension):
        for j in range(dimension):
            distMatrix[i][j] = math.sqrt( (citycoors[i][0] - citycoors[j][0]) **2 + (citycoors[i][1] - citycoors[j][1])**2 )

    return cities,distMatrix

def createCityPermutation(cities,seed):

    np.random.seed(seed)
    citiesPerm = np.random.permutation(cities)
    citiesPerm = np.append(citiesPerm, citiesPerm[0])
    return citiesPerm

def totalCost(citiesPerm,distMatrix):
    cost = 0
    for i in range(len(citiesPerm)-1):
        cost += distMatrix[citiesPerm[i]][citiesPerm[i+1]]
    return cost

def Hill_Climbing(filename,cutoff,randseed):
    randseed = int (randseed)
    #cutoff = int (cutoff)
    cities,distMatrix = readData(filename)
    citiesPerm = createCityPermutation(cities,randseed)

    bestCost = totalCost(citiesPerm,distMatrix)
    best_time_score=[[0,int(bestCost)]]

    start = time.time()
    elapsed = 0
    while(elapsed < cutoff):
        while True:
            minchange = 0
            for i in range(0,len(cities)-2):
                for j in range(i+2,len(cities)):
                    change = (distMatrix[citiesPerm[i]][citiesPerm[j]]
                              + distMatrix[citiesPerm[i+1]][citiesPerm[j+1]]
                              - distMatrix[citiesPerm[i]][citiesPerm[i+1]]
                              - distMatrix[citiesPerm[j]][citiesPerm[j+1]]
                              )
                    if(minchange > change ):
                        minchange = change

                        min_i = i
                        min_j = j
            citiesPerm = twoOptSwap(citiesPerm,min_i,min_j)

            bestCost += minchange
            endTime = time.time()
            elapsed = endTime - start
            #print elapsed
            best_time_score.append([  endTime - start,int(bestCost)   ])
            if minchange >= -0.1:
                citiesPerm = rearrange(citiesPerm)
                return int(bestCost), citiesPerm, best_time_score
    if(elapsed >= cutoff):

        return int(bestCost), citiesPerm, best_time_score

def rearrange(citiesPerm):
###################### rearrange the result so it starts with 0  ##############
    citiesPermX = citiesPerm[0:-1]

    for w in range(len(citiesPermX)):
        if citiesPermX[w] == 0:
            zeroIndex = w
            break

    firstHalf = citiesPermX[zeroIndex:]
    secondHalf = citiesPermX[0:zeroIndex]
    permStartWithZero = np.append(firstHalf, secondHalf)
    permStartWithZero = np.append(permStartWithZero,0)
    return permStartWithZero

def twoOptSwap(citiesPerm,i,j):

    newCitiesPerm = citiesPerm[:]
    newCitiesPerm[i+1:j+1] = citiesPerm[j:i:-1]
    return newCitiesPerm


from collections import defaultdict

class InvertedIndex:
    def __init__(self):
        self.index = defaultdict(list)
        self.document_metadata = {}

    def add_document(self, document_id, content, metadata=None):
        tokens = content.lower().split()  # Convert to lowercase for case-insensitive search
        for token in tokens:
            self.index[token].append(document_id)
        if metadata:
            self.document_metadata[document_id] = metadata

    def search_documents(self, keywords):
        keyword_set = set(keywords)
        matching_documents = defaultdict(int)
        for keyword in keyword_set:
            if keyword in self.index:
                for document_id in self.index[keyword]:
                    matching_documents[document_id] += 1
        sorted_results = sorted(matching_documents.items(), key=lambda x: x[1], reverse=True)
        return [result[0] for result in sorted_results]

    def remove_document(self, document_id):
        for postings in self.index.values():
            if document_id in postings:
                postings.remove(document_id)
                if not postings:
                    del postings
        if document_id in self.document_metadata:
            del self.document_metadata[document_id]

index = InvertedIndex()

index.add_document(1, "Apple Inc. announces new product lineup.", {"date": "2023-06-15", "source": "PR Newswire"})
index.add_document(2, "Google reports strong financial results for Q2.",
                   {"date": "2023-06-16", "source": "Business Insider"})
index.add_document(3, "Amazon launches new subscription service.", {"date": "2023-06-14", "source": "Reuters"})
index.add_document(4, "Microsoft introduces new cloud computing solution.",
                   {"date": "2023-06-17", "source": "TechCrunch"})
index.add_document(5, "Tesla unveils new electric vehicle model.", {"date": "2023-06-17", "source": "CNN"})
index.add_document(6, "Facebook announces expansion into virtual reality market.",
                   {"date": "2023-06-16", "source": "The Verge"})
index.add_document(7, "Samsung releases latest smartphone with advanced features.",
                   {"date": "2023-06-15", "source": "Gizmodo"})
index.add_document(8, "Netflix reports record-breaking subscriber growth.", {"date": "2023-06-18", "source": "Variety"})
index.add_document(9, "IBM introduces new AI-powered analytics platform.", {"date": "2023-06-18", "source": "Forbes"})
index.add_document(10, "General Electric announces major renewable energy project.",
                   {"date": "2023-06-19", "source": "CNBC"})
index.add_document(11, "Toyota unveils all-electric SUV with impressive range.",
                   {"date": "2023-06-19", "source": "The Guardian"})
index.add_document(12, "Intel launches revolutionary processor for high-performance computing.",
                   {"date": "2023-06-20", "source": "PCMag"})
index.add_document(13, "SpaceX successfully launches satellite into orbit.",
                   {"date": "2023-06-20", "source": "Space.com"})
index.add_document(14, "Coca-Cola introduces new line of healthy beverages.",
                   {"date": "2023-06-21", "source": "Bloomberg"})
index.add_document(15, "Adobe releases latest version of creative software suite.",
                   {"date": "2023-06-21", "source": "Engadget"})
index.add_document(16, "Walmart announces expansion into online grocery delivery.",
                   {"date": "2023-06-22", "source": "TechRadar"})
index.add_document(17, "Volkswagen unveils electric vehicle charging infrastructure plan.",
                   {"date": "2023-06-22", "source": "Reuters"})
index.add_document(18, "Twitter introduces new feature to combat misinformation.",
                   {"date": "2023-06-23", "source": "The New York Times"})
index.add_document(19, "JPMorgan Chase reports strong quarterly earnings.",
                   {"date": "2023-06-23", "source": "Financial Times"})
index.add_document(20, "Boeing showcases next-generation aircraft with advanced technologies.",
                   {"date": "2023-06-24", "source": "Aviation Week"})


from libgravatar import Gravatar
from sqlalchemy.orm import Session

from src.database.models import User
from src.schemas import UserModel

async def get_user_by_email(email: str, db: Session) -> User | None:
    """
    Get user by e-mail.

    :param email: User's e-mail.
    :type email: str
    :param db: Database session.
    :type db: Session
    :return: Return the found user, or None if it does not exist
    :rtype: User | None
    """
    return db.query(User).filter(User.email == email).first()

async def create_user(body: UserModel, db: Session) -> User:
    """
    Create user.

    :param body: User body.
    :type body: UserModel
    :param db: Database session.
    :type db: Session
    :return: Created user.
    :rtype: User
    """
    avatar = None
    try:
        g = Gravatar(body.email)
        avatar = g.get_image()
    except Exception as e:
        print(e)
    new_user = User(**body.dict(), avatar=avatar)
    db.add(new_user)
    db.commit()
    db.refresh(new_user)
    return new_user

async def update_token(user: User, token: str | None, db: Session) -> None:
    """
    Update user token

    :param user: Current user.
    :type user: User
    :param token: User's token.
    :type token: str
    :param db: Database session.
    :type db: Session
    :return: Not return result.
    """
    user.refresh_token = token
    db.commit()

async def confirmed_email(email: str, db: Session) -> None:
    """
    Set user's e-mail confirmed.

    :param email: User's e-mail
    :type email: str
    :param db: Database session.
    :type db: Session
    :return: Not return result.
    """
    user = await get_user_by_email(email, db)
    user.confirmed = True
    db.commit()

async def update_avatar(email, url: str, db: Session) -> User:
    """
    Update user's avatar

    :param email: User's e-mail.
    :type email: str
    :param url: User avatar link.
    :type url: str
    :param db: Database session.
    :type db: Session
    :return: Updated user.
    :rtype: User

    """
    user = await get_user_by_email(email, db)
    user.avatar = url
    db.commit()
    return user


from scipy.spatial import distance_matrix as _distance_matrix
import numpy as np
import pandas as pd

import sys
from os import listdir
from os.path import isfile
from os.path import basename
import time
from tqdm import tqdm
import git

from ioutils import parse_input
from ioutils import parse_solutions
from graph_utils import dijkstra
from graph_utils import floyd_warshall
from graph_utils import floyd_warshall_py
from graph_utils import NO_PATH_INDICATOR
from graph_utils import NO_EDGE_INDICATOR
from plot_utils import plot_two_solutions

from wrapc.wrapc import normal_paths_calulation_c
from wrapc.wrapc import floyd_warshall_c
from wrapc.wrapc import get_solution_cost_c

class ProblemInstance:
    def __init__(self, n, p, alpha, delta, ksi, node_coordinates, demand, distances=None, name=None, optimal_cost=None):
        self.n = n
        self.p = p
        self.alpha = alpha
        self.delta = delta
        self.ksi = ksi
        self.node_coordinates = node_coordinates
        self.demand = demand
        if distances is not None:
            self.distances = distances
        else:
            self.distances = get_distance_matrix(self.node_coordinates)
        self.optimal_cost = optimal_cost
        self.name = name

    def __str__(self):
        if self.name is None:
            return f"{self.n}.{self.p}"
        else:
            return self.name

    def __lt__(self, obj):
        return (self.n, self.p) < (obj.n, obj.p)

class Solution:
    def __init__(self, hubs, problem, use_c=True, cost=None):
        self.hubs = hubs
        self.problem = problem
        self.use_c = use_c
        if cost is not None:
            self.cost = cost
        else:
            self.cost = self._get_cost()
        # We'll caluclate the neighbourhood only when it's needed for the first time
        self.neighbourhood = None

    def __str__(self):
        return f"Solution(hubs={self.hubs})"

    def _get_cost(self):
        if self.use_c:
            return get_solution_cost_c(self.hubs, self.problem)
        else:
            return get_solution_cost_fw(self.hubs, self.problem, False)

    def get_neighbourhood(self, get_neighbourhood):
        if self.neighbourhood == None:
            self.neighbourhood = get_neighbourhood(self.hubs, self.problem.n)
        return self.neighbourhood

def get_nodes(n):
    return list(range(n))

def get_distance_matrix(nodes_coordinates):
    return _distance_matrix(nodes_coordinates, nodes_coordinates)

def get_discount_matrix(n, hubs, alpha, delta, ksi):
    discounts = np.ones((n,n))
    nodes = get_nodes(n)
    for A in nodes:
        for B in nodes:
            if (A in hubs) and (B in hubs):
                discounts[A, B] = alpha
            if (A not in hubs) and (B in hubs):
                discounts[A, B] = ksi
            if (A in hubs) and (B not in hubs):
                discounts[A, B] = delta
    return discounts

def get_total_cost(flow_matrix, distances, discounts):
    return sum(sum(flow_matrix*distances*discounts))

def get_flow_from_paths(n, paths, demand):
    flow = np.zeros((n,n))
    for path in paths:
        orig, dest = path[0], path[-1]
        for prev, cur in zip(path, path[1:]):
            flow[prev, cur] += demand[orig, dest]
    return flow

def get_flow_from_hubs(hubs, n, alpha, delta, ksi, distances, demand):
    discounts = get_discount_matrix(n, hubs, alpha, delta, ksi)
    paths = allocate_paths(n, hubs, distances, discounts)
    return get_flow_from_paths(n, paths, demand)

def _get_valid_cost_graph(n, hubs, distances, discounts):
    nodes = get_nodes(n)
    cost_graph = distances*discounts
    for A in nodes:
        for B in nodes:
            # connection between non-hub nodes is not allowed
            if A != B and (A not in hubs) and (B not in hubs):
                cost_graph[A, B] = NO_EDGE_INDICATOR
    return cost_graph

def allocate_paths(n, hubs, distances, discounts):
    nodes = get_nodes(n)
    # prepare graph
    cost_graph = _get_valid_cost_graph(n, hubs, distances, discounts)
    # calulate paths
    paths = []
    for A in nodes:
        _, paths_from_A = dijkstra(cost_graph, A)
        for B in nodes:
            if A != B:
                pathAB = paths_from_A[B]
                paths.append(pathAB)
    # by problem definition demand from a node to itself can be non-zero value
    # in that case, given demand should make a circuit through a hub and return to the node
    # although illogical at first glance, it could make sense in some setups
    #
    # for example in post offices problem, if a non-hub node represents all residents of a city
    # a resident have to go to a post office (hub) to leave a mail,
    # in order for it to be delivered to another resident of the same city
    more_paths = _orig_equals_dest_paths(nodes, hubs, cost_graph)
    return paths + more_paths

def get_solution_cost(hubs, problem):
    discounts = get_discount_matrix(problem.n, hubs, problem.alpha, problem.delta, problem.ksi)
    paths = allocate_paths(problem.n, hubs, problem.distances, discounts)
    flow = get_flow_from_paths(problem.n, paths, problem.demand)
    return get_total_cost(flow, problem.distances, discounts)

def _prepare(hubs, problem):
    nodes = get_nodes(problem.n)
    discounts = get_discount_matrix(problem.n, hubs, problem.alpha, problem.delta, problem.ksi)
    # prepare graph
    cost_graph = _get_valid_cost_graph(problem.n, hubs, problem.distances, discounts)
    # calculate cost
    return nodes, discounts, cost_graph

def _normal_paths_calulation(n, demand, cost_graph, predecessors):
    tmp_total_cost = 0
    for orig in range(n):
        for dest in range(n):
            left = predecessors[orig, dest]
            right = dest
            while True:
                if left == NO_PATH_INDICATOR:
                    break
                tmp_total_cost += demand[orig, dest] * cost_graph[left, right]
                if left == orig:
                    break
                right = left
                left = predecessors[orig, left]
    return tmp_total_cost

def _peculiar_paths_calulations(n, problem, hubs, cost_graph, discounts):
    tmp_total_cost = 0
    for node in range(n):
        if node in hubs:
            continue
        closest_hub = _closest(node, hubs, cost_graph)
        # adding cost of [node -> closest_hub -> node] path
        tmp_total_cost += problem.demand[node, node] * problem.distances[node, closest_hub] * discounts[node, closest_hub] + \
                      problem.demand[node, node] * problem.distances[closest_hub, node] * discounts[closest_hub, node]
    return tmp_total_cost

def get_solution_cost_fw(hubs, problem, use_c=True):
    total_cost = 0
    nodes, discounts, cost_graph = _prepare(hubs, problem)
    # calculate cost
    predecessors = floyd_warshall_c(cost_graph, hubs, problem.n, problem.p)

    if use_c:
        total_cost += normal_paths_calulation_c(problem.n, problem.demand, cost_graph, predecessors)
    else:
        total_cost += _normal_paths_calulation(problem.n, problem.demand, cost_graph, predecessors)

    # origin equals destination paths
    total_cost += _peculiar_paths_calulations(problem.n, problem, hubs, cost_graph, discounts)

    return total_cost

def get_swap_neighbourhood(hubs, n):
    neighbourhood = []
    non_hubs = [node for node in range(n) if node not in hubs]
    for hub in hubs:
        for non_hub in non_hubs:
            # swap the hub with the non-hub
            neighbourhood.append([h for h in hubs if h != hub] + [non_hub])
    return neighbourhood

def get_swap2_neighbourhood(hubs, n):
    neighbourhood = []
    non_hubs = [node for node in range(n) if node not in hubs]
    for hub1 in hubs:
        for hub2 in hubs:
            if hub1 == hub2:
                continue
            for non_hub1 in non_hubs:
                for non_hub2 in non_hubs:
                    if non_hub1 == non_hub2:
                        continue
                    # swap the hub with the non-hub
                    neighbourhood.append([h for h in hubs if h not in [hub1, hub2]] + [non_hub1, non_hub2])
    return neighbourhood

def get_swap3_neighbourhood(hubs, n):
    neighbourhood = []
    non_hubs = [node for node in range(n) if node not in hubs]
    for hub1 in hubs:
        for hub2 in hubs:
            for hub3 in hubs:
                if hub1 == hub2 or hub1 == hub3 or hub2 == hub3:
                    continue
                for non_hub1 in non_hubs:
                    for non_hub2 in non_hubs:
                        for non_hub3 in non_hubs:
                            if non_hub1 == non_hub2 or non_hub1 == non_hub2 or non_hub2 == non_hub3:
                                continue
                            # swap the hub with the non-hub
                            neighbourhood.append([h for h in hubs if h not in [hub1, hub2, hub3]] + [non_hub1, non_hub2, non_hub3])
    return neighbourhood

def bitmap(n, list):
    return [1 if i in list else 0 for i in range(n)]

def plot_comparison_with_optimal(input_directory, solutions_file, dataset, get_solution, num_of_problems):
    '''
    Parameters:
    input_directory (str): directory with input files
    solution_file (str): file with solutions
    dataset (str): dataset label
    get_solution (method): method that returns a solution (as a list of hubs)
    '''
    solutions = parse_solutions(solutions_file)
    problems = list(solutions.keys())[:num_of_problems]
    for n, p in problems:
        # parse the instance n, p
        filepath = input_directory + f'{n}.{p}'
        if not isfile(filepath):
            continue
        n, p, alpha, delta, ksi, nodes_coordinates, demand = parse_input(filepath, dataset)
        distances = get_distance_matrix(nodes_coordinates)

        optimal_hubs = solutions[n,p]['hubs']
        if optimal_hubs is None:
            continue
        optimal_discounts = get_discount_matrix(n, optimal_hubs, alpha, delta, ksi)
        optimal_paths = allocate_paths(n, optimal_hubs, distances, optimal_discounts)

        initial_hubs = get_solution(n, p, distances, nodes_coordinates)
        initial_discounts = get_discount_matrix(n, initial_hubs, alpha, delta, ksi)
        initial_paths = allocate_paths(n, initial_hubs, distances, initial_discounts)
        plot_two_solutions(nodes_coordinates,
                        bitmap(n, optimal_hubs),
                        bitmap(n, initial_hubs),
                        get_flow_from_paths(n, optimal_paths, demand),
                        get_flow_from_paths(n, initial_paths, demand),
                        title1=f"Optimal solution for n={n}, p={p}",
                        title2=f"Initial solution for n={n}, p={p}",
                        verbose=2)

def get_comparison_table(list_of_methods,
                         method_names,
                         dataset,
                         test_data_directory,
                         solutions_file=None,
                         from_problem=0,
                         to_problems=45,
                         initializations=[None],
                         display_solution=False,
                         display_deviation=True,
                         display_time=True,
                         display_nan=True):
    problems = []
    solutions = {}
    if solutions_file != None:
        solutions = parse_solutions(solutions_file)
    files = listdir(test_data_directory)
    for file in files:
        nodes = None
        distances = None
        if dataset == 'AP':
            n, p, alpha, delta, ksi, nodes, demand = parse_input(test_data_directory + file, dataset)
        elif dataset == 'CAB':
            n, p, alpha, delta, ksi, distances, demand = parse_input(test_data_directory + file, dataset)
        if (n,p) in solutions:
            optimal_cost = solutions[(n,p)]['objective']
        else:
            # todo fix this hack
            optimal_cost = None
        if dataset == 'AP':
            problem_name = None
        elif dataset == 'CAB':
            problem_name = basename(file)
        problems.append(ProblemInstance(n, p, alpha, delta, ksi, node_coordinates=nodes, distances=distances, demand=demand, optimal_cost=optimal_cost, name=problem_name))
    problems = sorted(problems)[from_problem:to_problems]

    if display_solution:
        columns = ['optimal solution']
    else:
        columns = []
    for i in range(len(list_of_methods)):
        method = list_of_methods[i]
        method_name = method_names[i]
        for initialization in initializations:
            if initialization != None and method_name != "cplex":
                if display_solution:
                    columns.append(method_name + " " + initialization.__name__ + " - solution")
                if display_deviation:
                    columns.append(method_name + " " + initialization.__name__ + " - deviation (%)")
                if display_time:
                    columns.append(method_name + " " + initialization.__name__ + " - time (s)")
            else:
                if display_solution:
                    columns.append(method_name + " - solution")
                if display_deviation:
                    columns.append(method_name + " - deviation (%)")
                if display_time:
                    columns.append(method_name + " - time (s)")

    data = {}
    for problem in tqdm(problems):
        if not display_nan and problem.optimal_cost == None:
            continue
        if display_solution:
            data[str(problem)] = [problem.optimal_cost]
        else:
            data[str(problem)] = []
        for method in list_of_methods:
            for initialization in initializations:
                if initialization != None and method.__name__ != "cplex":
                    initial_solution = Solution(initialization(problem.n, problem.p, problem.distances, problem.node_coordinates), problem)
                    starttime = time.time()
                    solution = method(problem, initial_solution=initial_solution)
                    endtime = time.time()
                else:
                    starttime = time.time()
                    solution = method(problem)
                    endtime = time.time()
                solution.cost = solution.cost/1000
                if display_solution:
                    data[str(problem)].append(round(solution.cost, 2))
                if problem.optimal_cost == None:
                    deviation = None
                else:
                    deviation = 100 * abs(problem.optimal_cost - solution.cost) / problem.optimal_cost
                    deviation = round(deviation, 2)

                if display_deviation:
                    data[str(problem)].append(deviation)
                if display_time:
                    data[str(problem)].append(round(endtime-starttime, 2))
    df = pd.DataFrame.from_dict(data, orient='index', columns=columns)
    df.loc['mean'] = df.mean()
    return df

def get_latest_commit_id():
    repo = git.Repo(search_parent_directories=True)
    return repo.head.object.hexsha

def _orig_equals_dest_paths(nodes, hubs, cost_graph):
    peculiar_paths = []
    for node in nodes:
        if node in hubs:
            continue
        closest_hub = _closest(node, hubs, cost_graph)
        peculiar_paths.append([node, closest_hub, node])
    return peculiar_paths

def _closest(node, hubs, cost_graph):
    hub_dist = {hub: cost_graph[node, hub] for hub in hubs}
    return min(hub_dist, key=hub_dist.get)

NEIGHBOURHOOD_TYPES = [get_swap_neighbourhood, get_swap2_neighbourhood, get_swap3_neighbourhood]


import collections

class Solution:
    def totalFruit(self, tree: List[int]):
        l, nums, res = 0, collections.Counter(), 0
        for r in range(len(tree)):
            nums[tree[r]] += 1
            while len(nums) > 2:
                nums[tree[l]] -= 1
                if not nums[tree[l]]:
                    nums.pop(tree[l])
                l += 1
            res = max(res, r - l + 1)
        return res

trees = [[1, 2, 1], [0, 1, 2, 2], [1, 2, 3, 2, 2]]

for i in trees:
    print(Solution().totalFruit(i))


import cv2 as cv
import numpy as np

print(np.__version__)

train_data = np.load('training_data.npy')

for data in train_data:
    img = data[0]
    key_press = data[1]
    cv.imshow('test', img)
    print(key_press)
    if cv.waitKey(25) & 0xFF == ord('q'):
        cv.destroyAllWindows()
        break


import sys

sys.stdin = open("input.txt", "r")

T = 10
for t in range(1, T + 1):
    N, num = map(str, input().split())
    N = int(N)
    arr = [num[i] for i in range(N)]

    while True:
        for i in range(1, len(arr)):
            if arr[i-1] == arr[i]:
                arr.pop(i-1)
                arr.pop(i-1)
                break
        else:
            break

    result = ''
    for i in arr:
        result += i
    print('#%d' %t, result)


#The logic to give minimum time output for each case.
def getMinumFlips(pancakeString):
	count = 0
	old_string = pancakeString

	k = old_string.find("-")
	while k != -1:
		firtChar = old_string[0]
		if firtChar == '+':
			j = old_string.find("-")
		if firtChar == '-':
			j = old_string.find("+")

		if j == -1:
			lString = old_string
			j = len(old_string)
		else:
			lString = old_string[:j]

		lString = lString.replace("-", "1")
		lString = lString.replace("+", "0")
		lString = lString.replace("0", "-")
		lString = lString.replace("1", "+")
		lString = lString[::-1]

		if (j == len(old_string)):
			old_string = lString
		else:
			old_string = lString + old_string[j:]

		count = count+1
		k = old_string.rfind("-")

	return count

#actual logic to read data and print it by calling getOutputString function
outputList = []

with open ('C:/Users/rpant/Desktop/Python_Programs/RevengeOfPancakes/input.txt') as data :
	totalNumberOfCases = data.readline()
	for index in range (int(totalNumberOfCases)):
		testCaseStr = data.readline().strip()
		outputList.append(getMinumFlips(testCaseStr))

with open ('C:/Users/rpant/Desktop/Python_Programs/RevengeOfPancakes/output.txt', "w") as os :
	for index in range(len(outputList)):
		print('Case #' + str(index+1) + ': ' + str(outputList[index]), file=os)


""" Converts DataTables JSON format to URCVT """

import json
import sys
from rcvformats.conversions.ut_without_transfers import UTWithoutTransfersConverter

class InvalidDataTableInput(Exception):
    """ Error messages caught here, that may not have been caught in JS """

# pylint: disable=too-few-public-methods
class ReadDataTableJSON():
    """ A class to read DataTable-serialized data """

    def __init__(self, jsonData):
        self.jsonData = jsonData
        self.numRounds = None

    def _parse_config(self):
        """ Parses the config from the JSON data passed from upload.html """
        config = {}
        config['contest'] = self.jsonData['configElectionTitle']
        config['date'] = self.jsonData['configElectionDate']
        config['threshold'] = self.jsonData['configThreshold']

        if config['contest'] == "":
            raise InvalidDataTableInput("Must give this election a title")

        if config['date'] == "":
            raise InvalidDataTableInput("Must give this election a date")

        if config['threshold'] == "":
            raise InvalidDataTableInput("Threshold cannot be empty")

        return config

    def _fill_rest_of_rounds_after_elected(
            self,
            results,
            candidateName,
            electedOnRound,
            finalVoteTotal):
        assert electedOnRound < self.numRounds

        # All future rounds keep the same vote total
        for futureRoundNum in range(electedOnRound + 1, self.numRounds):
            results[futureRoundNum]['tally'][candidateName] = finalVoteTotal

    def _handle_candidate(self, results, candidateData, candidateName):
        """
        Fills in the data for one candidate across all rounds
        Returns true if was just elected
        """
        # Iterate over each row for this candidate
        wasElectedLastRound = False
        for roundNum, roundData in enumerate(candidateData):
            # Gather data from table
            numVotes = roundData['# Votes']
            status = roundData['Status']

            try:
                numVotes = float(numVotes)
            except (TypeError, ValueError) as exc:
                raise InvalidDataTableInput(f"On Round {roundNum+1}, \"{candidateName}\" " +
                                            "has an invalid number of votes") from exc

            if numVotes < 0:
                raise InvalidDataTableInput("All vote counts must be positive")

            # Already elected last round - fill out remaining rounds, then break
            if wasElectedLastRound:
                if numVotes is None or numVotes == 0:
                    # No surplus transfer: use the last-round's value
                    finalVoteTotal = results[roundNum - 1]['tally'][candidateName]
                else:
                    # Has surplus transfer
                    finalVoteTotal = float(numVotes)

                self._fill_rest_of_rounds_after_elected(
                    results, candidateName, roundNum - 1, finalVoteTotal)
                break

            # The normal case: just append the tally
            results[roundNum]['tally'][candidateName] = numVotes

            # TallyResults only exist for eliminated and elected candidates
            if status == 'Eliminated':
                results[roundNum]['tallyResults'].append({'eliminated': candidateName})
                break
            if status == 'Elected':
                results[roundNum]['tallyResults'].append({'elected': candidateName})
                wasElectedLastRound = True
            else:
                assert status == 'Active'

    def _parse_results(self):
        """ Parses the results from the dataEntry serialization """
        if 'dataEntry' not in self.jsonData:
            raise InvalidDataTableInput("Data Tables isn't even shown. This should never happen.")
        dataTableData = json.loads(self.jsonData['dataEntry'])

        self.numRounds = len(dataTableData['data'][0])
        results = [{"round": i + 1, "tally": {}, "tallyResults": []} for i in range(self.numRounds)]

        names = dataTableData['rowNames']
        if len(set(names)) != len(names):
            raise InvalidDataTableInput("All candidate names must be unique")
        if any(n == "" or n is None for n in names):
            raise InvalidDataTableInput("All candidates must have names")

        # Iterate over the data, which goes candidate-by-candidate (whereas URCVT data
        # goes round-by-round), so we will transpose it
        for candidateNum, candidateData in enumerate(dataTableData['data']):
            candidateName = names[candidateNum]
            self._handle_candidate(results, candidateData, candidateName)

        return results

    def convert_to_urcvt(self):
        """
        Worker function: converts all data passed from upload.html, including the config
        (from RCVis' fields) and the results (from the datatables' serialization)
        """
        config = self._parse_config()
        results = self._parse_results()
        withoutTransfers = {'config': config, 'results': results}

        converter = UTWithoutTransfersConverter()
        try:
            withTransfers = converter.convert_to_ut(withoutTransfers)
        except Exception as exc:
            # Try to find a reason
            totalVotesLastRound = sys.maxsize
            for roundNum, result in enumerate(results):
                totalVotesThisRound = sum(t for t in result['tally'].values())
                if totalVotesThisRound > totalVotesLastRound:
                    raise InvalidDataTableInput(
                        f"Round {roundNum+1} has {totalVotesThisRound} votes, which is more " +
                        f"than the previous round, which only has {totalVotesLastRound}. " +
                        "This should not be possible.") from exc
                totalVotesLastRound = totalVotesThisRound

            # No reason found
            raise InvalidDataTableInput("Could not add transfers") from exc

        return withTransfers


# -*- coding: utf-8 -*-
"""
Created on Wed Mar  9 11:50:48 2022

@author: iant

CHECK LOIC VS GODDARD SPECTRAL CALIBRATION AND FIND WHICH ONE MATCHES DATA THE BEST
"""
import re
import numpy as np
import matplotlib.pyplot as plt
import h5py
import os
import pandas as pd

from matplotlib.backends.backend_pdf import PdfPages

# from scipy.optimize import curve_fit
# from scipy.optimize import OptimizeWarning

from tools.spectra.molecular_spectrum_so import get_molecular_hr
from tools.spectra.baseline_als import baseline_als
# from tools.spectra.fit_gaussian_absorption import fit_gaussian_absorption

from tools.plotting.colours import get_colours

from tools.general.get_minima_maxima import get_local_minima
from tools.general.get_nearest_index import get_nearest_index

from tools.file.hdf5_functions import make_filelist
from tools.file.paths import paths

from tools.file.write_log import write_log

order_dict = {
    119:{"molecule":"CO2", "smin":1.0e-30, "n_lines":20},
    121:{"molecule":"CO2", "smin":1.0e-30, "n_lines":20},
    134:{"molecule":"H2O", "smin":1.0e-28, "n_lines":9},
    136:{"molecule":"H2O", "smin":1.0e-28, "n_lines":7},
    149:{"molecule":"CO2", "smin":1.0e-28, "n_lines":17},
    165:{"molecule":"CO2", "smin":1.0e-24, "n_lines":20},
    168:{"molecule":"H2O", "smin":1.0e-33, "n_lines":5},
    169:{"molecule":"H2O", "smin":1.0e-33, "n_lines":6},
    186:{"molecule":"CO", "smin":1.0e-33, "n_lines":8},
    189:{"molecule":"CO", "smin":1.0e-33, "n_lines":9},
    190:{"molecule":"CO", "smin":1.0e-33, "n_lines":10},
    191:{"molecule":"CO", "smin":1.0e-33, "n_lines":10},

}

chosen_bin = 3
chosen_y_mean = 0.5

temperature_grid_spacing = 0.1

PLOT_SPECTRA = False

def gd21_waven(order, inter_temp, px_in):
    """spectral calibration Goddard Oct 21"""
    cfpixel = [1.75128E-08, 5.55953E-04, 2.24734E+01]             # Blaze free-spectral-range (FSR) [cm-1 from pixel]
    ncoeff  = [-2.44383699e-07, -2.30708836e-05, -1.90001923e-04] # Relative frequency shift coefficients [shift/frequency from Celsius]

    xdat  = np.polyval(cfpixel, px_in) * order
    xdat += xdat  *np.polyval(ncoeff, inter_temp)
    return xdat

def lt21_waven(order, inter_temp, px_in):
    """spectral calibration Loic Feb 22"""

    cfpixel = [3.32e-8, 5.480e-4, 22.4701]

    p0_nu = -0.8276 * inter_temp #px/°C * T(interpolated)
    px_temp = px_in + p0_nu
    xdat  = np.polyval(cfpixel, px_temp) * order

    return xdat

# px = np.arange(n_px)
# plt.figure()
# for order in range(119, 195, 10):
#     for inter_temp in range(-10, 10, 2):
#         nu_p_gd = gd21_waven(order, inter_temp, px)
#         nu_p_lt = lt21_waven(order, inter_temp, px)

#         delta = nu_p_gd - nu_p_lt
#         plt.plot(delta, label="%i %0.1fC" %(order, inter_temp))
#         plt.legend()
# stop()

# def fit_gaussian_absorption(x_in, y_in, error=False, hr_num=500):
#     """fit inverted gaussian to absorption band.
#     Normalise continuum to 1 first"""

#     def func(x, a, b, c, d):
#         return 1.0 - a * np.exp(-((x - b)/c)**2.0) + d

#     x_mean = np.mean(x_in)
#     x_centred = x_in - x_mean
#     try:
#         popt, pcov = curve_fit(func, x_centred, y_in, p0=[0.1, 0.02, 0.25, 0.0])
#     except OptimizeWarning :
#         if error:
#             return [], [], [], []
#         else:
#             return [], [], []
#     except RuntimeError:
#         if error:
#             return [], [], [], []
#         else:
#             return [], [], []

#     if error:
#         y_fit = func(x_centred, *popt)
#         chi_squared = np.sum(((y_in - y_fit) / y_fit)**2) #divide by yfit to normalise large and small absorption bands

#     x_hr = np.linspace(x_in[0], x_in[-1], num=hr_num)
#     y_hr = func(x_hr - x_mean, *popt)

#     min_index = (np.abs(y_hr - np.min(y_hr))).argmin()

#     x_min_position = x_hr[min_index]

#     if error:
#         return x_hr, y_hr, x_min_position, chi_squared
#     else:
#         return x_hr, y_hr, x_min_position

def fit_polynomial(x_in, y_in, deg=2, error=False, hr_num=100):

    x_mean = np.mean(x_in)
    x_centred = x_in - x_mean
    polyfit = np.polyfit(x_centred, y_in, deg)

    y_fit = np.polyval(polyfit, x_centred)

    if error:
        chi_squared = np.sum(((y_in - y_fit) / y_fit)**2) #divide by yfit to normalise large and small absorption bands

    x_hr = np.linspace(x_in[0], x_in[-1], num=hr_num)
    y_hr = np.polyval(polyfit, x_hr - x_mean)

    min_index = (np.abs(y_hr - np.min(y_hr))).argmin()

    x_min_position = x_hr[min_index]

    if error:
        return x_hr, y_hr, x_min_position, chi_squared
    else:
        return x_hr, y_hr, x_min_position

from matplotlib.colors import LinearSegmentedColormap

redgreyblue = LinearSegmentedColormap.from_list('redgreyblue', (
    (0.000, (0.000, 0.325, 0.984)),
    (0.480, (0.620, 0.627, 0.863)),
    (0.500, (0.596, 0.588, 0.588)),
    (0.520, (0.863, 0.620, 0.624)),
    (1.000, (1.000, 0.000, 0.047))))

def make_spectral_comparison_log(order_dict, chosen_bin, chosen_y_mean):

    write_log("Filename\tOrder\tTemperature\tAbsorption nu\tAbsorption pixel\tGD nu\tLT nu\tGD delta nu\tLT delta nu\tFitted depth\tFitted max")

    for order, order_info in order_dict.items():

        molecule = order_info["molecule"]
        smin = order_info["smin"]
        n_lines = order_info["n_lines"]

        n_px = 320.0 #consider whole detector
        file_level = "hdf5_level_1p0a"

        # regex = re.compile("20(18|21)04.._......_.*_SO_A_[IE]_%i" %order)
        # regex = re.compile("20(18|19|20|21)04.._......_.*_SO_A_[IE]_%i" %order)
        # regex = re.compile("20(18|19|20|21)0[4567].._......_.*_SO_A_[IE]_%i" %order)
        regex = re.compile("20(18|19|20|21|22)...._......_.*_SO_A_[IE]_%i" %order)

        hdf5_files, hdf5_filenames, _ = make_filelist(regex, file_level, open_files=False, silent=True)

        colours = get_colours(len(hdf5_filenames))

        if PLOT_SPECTRA:
            fig, (ax1,ax2) = plt.subplots(nrows=2, ncols=1, figsize=(18,10), sharex=True, constrained_layout=True)
            fig.suptitle("Order %i" %order)

        for file_index, (hdf5_file_path, hdf5_filename) in enumerate(zip(hdf5_files, hdf5_filenames)):

            hdf5_file = h5py.File(hdf5_file_path, "r")

            colour = colours[file_index]

            y_all = hdf5_file["Science/Y"][:, :]
            y_mean = np.mean(y_all, axis=1)

            if "InterpolatedTemperature" not in hdf5_file["Channel"].keys():
                print("%s: Error temperatures not in file" %hdf5_filename)
                continue

            temperatures = hdf5_file["Channel/InterpolatedTemperature"][...]

            bins = hdf5_file["Science/Bins"][:, 0]
            unique_bins = sorted(list(set(bins)))

            bin_indices = np.where(bins == unique_bins[chosen_bin])[0]

            y_bin = y_all[bin_indices, :]
            y_mean_bin = y_mean[bin_indices]

            index = get_nearest_index(chosen_y_mean, y_mean_bin)

            y = y_bin[index, :]
            t = temperatures[index]

            y_cont = baseline_als(y)
            y_cr = y / y_cont

            px = np.arange(n_px)

            #for plotting use only Loic's code
            # x_gd = gd21_waven(order, t, px)
            x_lt = lt21_waven(order, t, px)

            if PLOT_SPECTRA:
                # ax1.plot(x_gd, y_cr, color=colour, linestyle="--", label="%s GD" %hdf5_filename)
                ax1.plot(x_lt, y_cr, color=colour, linestyle="-", label="%s LT" %hdf5_filename)

            #do simulation only for first file
            if file_index == 0:
                nu_hr = gd21_waven(order, t, np.arange(-10.0, n_px + 10.0, 0.001))
                mol_hr = 1.0 - get_molecular_hr(molecule, nu_hr, Smin=smin)
                if PLOT_SPECTRA:
                    ax2.plot(nu_hr, mol_hr)

                abs_ix_hr = get_local_minima(mol_hr) #get hitran absorption minima indices
                abs_nu_hrs = nu_hr[abs_ix_hr] #get absorption nu
                abs_y_hrs = mol_hr[abs_ix_hr] #get absorption depth

                #N strongest lines
                abs_y_cutoff = sorted(abs_y_hrs)[n_lines] #select only the n strongest lines

                if PLOT_SPECTRA:
                    for abs_index, (abs_nu_hr, abs_y_hr) in enumerate(zip(abs_nu_hrs, abs_y_hrs)):
                        if abs_y_hr < abs_y_cutoff:
                            ax2.text(abs_nu_hr, abs_y_hr, abs_nu_hr)

            #loop through hr absorptions
            for abs_index, (abs_nu_hr, abs_y_hr) in enumerate(zip(abs_nu_hrs, abs_y_hrs)):
                if abs_y_hr < abs_y_cutoff:
                    # print("Checking line at %0.3f" %abs_nu_hr)

                    error = False

                    """use Loic's cal to find approx pixel range of absorption line"""
                    #find local minima close to the simulated lines
                    mol_gd_index = get_nearest_index(abs_nu_hr, x_lt)

                    #define micro window a few pixels to either side of expected pixel
                    local_indices_to_check = np.arange(mol_gd_index - 3, mol_gd_index + 4, 1)

                    #avoid lines at edge of detector
                    if min(local_indices_to_check) > 10 and max(local_indices_to_check) < n_px - 8:

                        #find local minima in pixels close to expected location of absorption line
                        local_minimum_index = get_local_minima(y_cr[local_indices_to_check]) + local_indices_to_check[0]

                        if len(local_minimum_index) == 1:
                            #if just one local minima found in spectrum micro window, centre indices on that
                            local_minimum_indices = np.arange(local_minimum_index - 1, local_minimum_index + 2, 1) #just take 1 point +- minimum

                            #find pixel number minimum
                            # x_hr, y_hr, px_min_position, chisq = fit_gaussian_absorption(px[local_minimum_indices], y_cr[local_minimum_indices], error=True)
                            x_hr, y_hr, px_min_position = fit_polynomial(px[local_minimum_indices], y_cr[local_minimum_indices], error=False)

                            if len(x_hr) > 0: #if no fitting error

                                # print("Single line found in data at %0.3f, chisq %0.3f" %(px_min_position, chisq*1.0e6))
                                abs_depth = np.min(y_hr)
                                abs_max = np.max(y_hr)

                                if abs_index == 0:
                                    label = "Gaussian fit to absorption bands"
                                else:
                                    label = ""

                                """Goddard"""
                                #convert pixel minimum and x_hr fit wavenumbers
                                nu_min_position_gd = gd21_waven(order, t, px_min_position)
                                delta_nu_gd = nu_min_position_gd - abs_nu_hr

                                """Loic"""
                                #convert pixel minimum and x_hr fit wavenumbers
                                nu_min_position_lt = lt21_waven(order, t, px_min_position)
                                delta_nu_lt = nu_min_position_lt - abs_nu_hr

                                if np.abs(delta_nu_gd) > 0.3 or np.abs(delta_nu_lt) > 0.3:
                                    print("Ignoring %0.1f line, too far from expected value" %abs_nu_hr)
                                    error = True

                                else:
                                    #plot
                                    if PLOT_SPECTRA:
                                        # x_hr_nu_gd = gd21_waven(order, t, x_hr)
                                        x_hr_nu_lt = lt21_waven(order, t, x_hr)
                                        # ax1.axvline(x=nu_min_position_gd, color=colour)
                                        # ax1.plot(x_hr_nu_gd, y_hr, color=colour, linestyle=":", label=label)
                                        ax1.axvline(x=nu_min_position_lt, color=colour)
                                        ax1.plot(x_hr_nu_lt, y_hr, color=colour, linestyle=":", label=label)

                            else:
                                # print("%s: fitting failed" %(hdf5_filename))
                                error = True
                        else:
                            # print("%s: %i lines found" %(hdf5_filename, len(local_minimum_index)))
                            error = True

                    else:
                        # print("%s: line too close to detector edge" %(hdf5_filename))
                        error = True

                    if not error:
                        text = "%s\t%i\t%0.3f\t%0.3f\t%0.3f\t%0.3f\t%0.3f\t%0.3f\t%0.3f\t%0.3f\t%0.3f" \
                            %(hdf5_filename, order, t, abs_nu_hr, px_min_position, nu_min_position_gd, nu_min_position_lt, \
                              delta_nu_gd, delta_nu_lt, \
                              abs_depth, abs_max)
                        write_log(text)

            hdf5_file.close()

            # abs_mean_gd = np.mean(np.abs(delta_nus_gd))
            # abs_mean_lt = np.mean(np.abs(delta_nus_lt))

            # # text = "%s\t%0.3f\t%0.3f" %(hdf5_filename, abs_mean_gd, abs_mean_lt)

            # print(text)
            # write_log(text)

        # ax1.legend()
        if PLOT_SPECTRA:
            plt.savefig("order_%i.png" %order)

# make_spectral_comparison_log(order_dict, chosen_bin, chosen_y_mean)

df = pd.read_csv(os.path.join(paths["BASE_DIRECTORY"], "reference_files", "so_spectral_cal_absorption_lines.txt"), sep="\t")

temperature_range = np.arange(np.floor(min(df["Temperature"])), np.ceil(max(df["Temperature"])) + temperature_grid_spacing, temperature_grid_spacing)
px_range = np.arange(320)

df["temp_bins"] = pd.cut(df["Temperature"], temperature_range, labels=False)
df["pixel_bins"] = pd.cut(df["Absorption pixel"], px_range, labels=False)

df["GD-LT"] = np.abs(df["GD delta nu"]) - np.abs(df["LT delta nu"])

with PdfPages("so_spectral_calibration_comparison.pdf") as pdf: #open pdf

    """plot all orders"""
    # grid = np.zeros((len(temperature_range), len(px_range))) + np.nan
    grid_list = [[[] for _ in range(len(px_range))] for _ in range(len(temperature_range))]

    for i, v in enumerate(df["GD-LT"]):
        # grid[df["temp_bins"][i], df["pixel_bins"][i]] = v
        # if 0.8 < df["Fitted max"][i] < 1.1:
        #     if not ((df["GD delta nu"][i] < 0.02) & (df["GD delta nu"][i] > -0.02)):
        #         if not ((df["LT delta nu"][i] < 0.02) & (df["LT delta nu"][i] > -0.02)):
                    grid_list[int(df["temp_bins"][i])][int(df["pixel_bins"][i])].append(v)

    grid_median = np.zeros((len(temperature_range), len(px_range))) + np.nan

    grid_counts = np.zeros((len(temperature_range), len(px_range)))
    for i in range(len(px_range)):
        for j in range(len(temperature_range)):
            if len(grid_list[j][i]) > 0:
                grid_median[j, i] = np.median(grid_list[j][i])
            grid_counts[j, i] = len(grid_list[j][i])

    plt.figure(figsize=(10, 5), constrained_layout=True)
    im = plt.pcolormesh(px_range, temperature_range, grid_median, cmap=redgreyblue, vmin=-0.1, vmax=0.1)
    plt.xlabel("Pixel Number")
    plt.ylabel("Interpolated Temperature")
    cbar = plt.colorbar(im)
    cbar.set_label("abs(delta GD)-abs(delta LT)\n(median if more than 1 value per grid point)", rotation=270, labelpad=20)
    plt.title("Comparing spectral calibration methods: Loic vs Goddard")

    pdf.savefig()
    plt.close()

    """plot each order"""
    for order in order_dict.keys():

        # grid = np.zeros((len(temperature_range), len(px_range))) + np.nan
        grid_list = [[[] for _ in range(len(px_range))] for _ in range(len(temperature_range))]

        for i in df.index[df["Order"] == order]:
            # grid[df["temp_bins"][i], df["pixel_bins"][i]] = v
            # if 0.8 < df["Fitted max"][i] < 1.1:
            #     if not ((df["GD delta nu"][i] < 0.02) & (df["GD delta nu"][i] > -0.02)):
            #         if not ((df["LT delta nu"][i] < 0.02) & (df["LT delta nu"][i] > -0.02)):
                        grid_list[int(df["temp_bins"][i])][int(df["pixel_bins"][i])].append(df["GD-LT"][i])

        grid_median = np.zeros((len(temperature_range), len(px_range))) + np.nan

        grid_counts = np.zeros((len(temperature_range), len(px_range)))
        for i in range(len(px_range)):
            for j in range(len(temperature_range)):
                if len(grid_list[j][i]) > 0:
                    grid_median[j, i] = np.median(grid_list[j][i])
                grid_counts[j, i] = len(grid_list[j][i])

        plt.figure(figsize=(10, 5), constrained_layout=True)
        im = plt.pcolormesh(px_range, temperature_range, grid_median, cmap=redgreyblue, vmin=-0.1, vmax=0.1)
        plt.xlabel("Pixel Number")
        plt.ylabel("Interpolated Temperature")
        cbar = plt.colorbar(im)
        cbar.set_label("abs(delta GD)-abs(delta LT)\n(median if more than 1 value per grid point)", rotation=270, labelpad=20)
        plt.title("Comparing spectral calibration methods: Loic vs Goddard Order %i" %order)

        pdf.savefig()
        plt.close()

# plt.figure(constrained_layout=True)
# im = plt.pcolormesh(px_range, temperature_range, grid_counts, cmap="binary")
# plt.xlabel("Pixel Number")
# plt.ylabel("Interpolated Temperature")
# cbar = plt.colorbar(im)
# cbar.set_label("Number of values per grid point", rotation=270, labelpad=20)
# plt.title("Comparing spectral calibration methods: Loic vs Goddard\nNumber of values per temperature-pixel bin")

# plt.figure(constrained_layout=True)
# plt.plot(np.sum(grid_counts, axis=1), temperature_range)
# plt.xlabel("Number of observations at this temperature")
# plt.ylabel("Interpolated Temperature")
# plt.title("Comparing spectral calibration methods: Loic vs Goddard\nNumber of values per temperature-pixel bin")

# from scipy.interpolate import griddata
# points = np.asfarray([df["Absorption pixel"], df["Temperature"]]).T
# values = df["GD-LT"]
# grid_x, grid_y = np.mgrid[0:320:1, np.floor(min(df["Temperature"])):np.ceil(max(df["Temperature"])):0.001]
# grid2 = griddata(points, values, (grid_x, grid_y), method='linear')

# plt.figure(constrained_layout=True)
# im2 = plt.imshow(grid2.T, aspect="auto", cmap="winter", \
#                  extent=(min(df["Absorption pixel"]), max(df["Absorption pixel"]), min(df["Temperature"]), max(df["Temperature"])), \
#                  vmin=-0.1, vmax=0.1, interpolation=None)
# cbar2 = plt.colorbar(im2)
# cbar2.set_label("abs(delta GD)-abs(delta LT)", rotation=270, labelpad=20)


from turtle import *
from random import choice, randint
from duze_cyfry import dajCyfre

BOK = 10

ht()
colormode(255)

def kwadrat(x,y,bok, c1,c2):
   color(c1,c2)
   pu()
   goto(x * bok-100, y * bok-100)
   pd()

   begin_fill()
   for i in range(4):
     fd(bok)
     rt(90)
   end_fill()

def losujKolor():
   #return (randint(0,255), randint(0,255), randint(0,255))
   return choice(['pink', 'orange', 'red', 'green', 'cyan', 'yellow', 'red', 'magenta'])

tracer(5,0)

T = []

for i in range(37):
  T.append(['white'] * 37)

for i in range(222):
	c = randint(0,9)
	dc = dajCyfre(c)[::-1]
	x = randint(0,30)
	y = randint(0,30)
	k = losujKolor()
	konflikt = False

	for dy,wiersz in enumerate(dc):
		for dx,znak in enumerate(wiersz):
			if znak == '#' and T[y+dy][x+dx] != 'white':
				konflikt = True
	for i in range(-1,6):
		if(T[y+i][x-1] == k or T[y+1][x+len(dc[1])] == k):
			konflikt = True
	for i in range(len(dc[0])):
		if(T[y-1][x+i]==k or T[y+6][x+i] == k):
			konflikt = True

	if not konflikt:
		for dy,wiersz in enumerate(dc):
			for dx,znak in enumerate(wiersz):
				if znak == '#':
					kwadrat(x+dx,y+dy, BOK, 'black', k)
					T[y+dy][x+dx] = k

update()

raw_input()