import os
import shlex
import subprocess
import numpy as np
import pandas as pd
from SentiCR.SentiCR.SentiCR import SentiCR

def clean_data(df):
    df = df.copy()

    # fill all rows with corresponding discussion link
    df[df['discussion_link'] == ""] = np.NaN
    df['discussion_link'] = df['discussion_link'].fillna(method='ffill')

    # keep only analyzed comments
    df = df[df['comment_ok'].notnull()]

    # identify contributors' comments
    df['contributor_comment'] = df['removed'].str.contains("contributor's comment")

    # save dataframe
    df_complete = df.copy()

    # remove all rows where change_ok is different than 'yes' or 'no'
    df = df[(df['change_ok'] == 'yes') | (df['change_ok'] == 'no')]

    # concatenate all answers to a discussion, as well as the last answer in the discussion, in a column
    answers = []
    last_answers = []
    last_answer_is_from_contributor = []
    for index, row in df.iterrows():
        row_answers = df_complete[(df_complete['discussion_link'] == row['discussion_link']) &
                                  (df_complete['filename'] == row['filename']) &
                                  (df_complete['commented_line'] == row['commented_line']) &
                                  (df_complete['comment'] != row['comment'])]

        if not row_answers.empty:
            last_answers.append(row_answers['comment'].iloc[-1])
            last_answer_is_from_contributor.append(row_answers['contributor_comment'].iloc[-1])
        else:
            last_answers.append(np.NaN)
            last_answer_is_from_contributor.append(np.NaN)
        answers.append(' '.join(row_answers['comment']))

    df['answers'] = answers
    df['last_answer'] = last_answers
    df['last_answer_is_from_contributor'] = last_answer_is_from_contributor

    # keep only one instance per discussion
    df = df.drop_duplicates(subset=['discussion_link', 'filename', 'method_signature', 'commented_line'], keep='first')

    # discard rows without answers
    df = df[df['answers'].str.len() > 0]

    return df

def merge_and_clean_data(df, df_complete):
    # fill all rows with corresponding discussion link
    df[df['discussion_link'] == ""] = np.NaN
    df['discussion_link'] = df['discussion_link'].fillna(method='ffill')

    # keep only analyzed comments
    df = df[df['comment_ok'].notnull()]

    # remove all rows where change_ok is different than 'yes' or 'no'
    df = df[(df['change_ok'] == 'yes') | (df['change_ok'] == 'no')]

    # add original commented line to dataframe
    for index, row in df.iterrows():
        match = df_complete[(df_complete['url'] == row['discussion_link']) &
                            (df_complete['filename'] == row['filename']) &
                            (df_complete['message'] == row['comment'])]

        if len(match) > 0:
            df.loc[index, 'commented_line'] = match['original_line'].iloc[0]

    # concatenate all answers to a discussion, as well as the last answer in the discussion, in a column
    answers = []
    last_answers = []
    last_answer_is_from_contributor = []
    for _, row in df.iterrows():
        row_answers = df_complete[(df_complete['url'] == row['discussion_link']) &
                                  (df_complete['filename'] == row['filename']) &
                                  (df_complete['original_line'] == row['commented_line']) &
                                  (df_complete['message'] != row['comment'])]

        # sort by creation date
        row_answers = row_answers.sort_values(by='created_at')

        if not row_answers.empty:
            last_answers.append(row_answers['message'].iloc[-1])
            last_answer_is_from_contributor.append(row_answers['owner_id'].iloc[-1] == row_answers['user_id'].iloc[-1])
        else:
            last_answers.append(np.NaN)
            last_answer_is_from_contributor.append(np.NaN)
        answers.append(' '.join(row_answers['message']))

    df['answers'] = answers
    df['last_answer'] = last_answers
    df['last_answer_is_from_contributor'] = last_answer_is_from_contributor

    # keep only one instance per discussion
    df = df.drop_duplicates(subset=['discussion_link', 'filename', 'method_signature', 'commented_line'], keep='first')

    # discard rows without answers
    df = df[df['answers'].str.len() > 0]

    return df

def extract_polarity(df, strategy, sa_tool='sentistrength'):
    # extract text to analyze depending on strategy
    df[strategy] = df[strategy].replace(r'[\n\t\r]', ' ', regex=True).replace(r'\"', '', regex=True)

    # run sentiment analysis
    if sa_tool == 'sentistrength':
        df.to_csv(f'{strategy}.tsv', sep='\t', columns=[strategy], index=True, header=False)
        sentiment_analysis_process = subprocess.Popen(shlex.split('java uk/ac/wlv/sentistrength/SentiStrength '
                                                                  f'sentidata {os.getcwd()}/SentiStrength-SE/ConfigFiles/ '
                                                                  f'input ../{strategy}.tsv '
                                                                  'annotateCol 2 overwrite '
                                                                  'trinary'),
                                                      cwd="SentiStrength-SE/")
        sentiment_analysis_process.communicate()

        # read results
        polarities = pd.read_csv(f'{strategy}.tsv', sep='\t',
                                 names=['original_index', 'text', 'polarity_sentistrength'])
        os.remove(f'{strategy}.tsv')
        return df.merge(polarities, left_index=True, right_on='original_index')
    else:
        sentiment_analyzer = SentiCR()
        df[f'polarity_senticr'] = df[strategy].apply(lambda x: sentiment_analyzer.get_sentiment_polarity(x)[0])
        return df

def build_oracle():
    df_all = clean_data(pd.read_csv('manual_analysis_all.csv'))
    df_filtered = merge_and_clean_data(pd.read_csv('manual_analysis_filtered.csv'),
                                       pd.read_csv('manual_analysis_filtered_complete.csv'))

    # add polarity to dataframe (separately because indexes overlap)
    df_all = extract_polarity(df_all, 'last_answer')
    df_filtered = extract_polarity(df_filtered, 'last_answer')
    df_all = extract_polarity(df_all, 'last_answer', 'senticr')
    df_filtered = extract_polarity(df_filtered, 'last_answer', 'senticr')

    # remove unnecessary columns
    df_all = df_all.drop(['original_index', 'removed', 'commented_file'], axis=1)
    df_filtered = df_filtered.drop(['can be identified as accepted'], axis=1)

    # combine the two dataframes
    df = pd.concat([df_all, df_filtered])

    df.to_csv('oracle.csv', index=False)

    return df

if __name__ == '__main__':
    build_oracle()


## Első feladat
for i in range(1,10):
    print(1/i)

## Második feladat
hatvany=int(input("Kérem a hatvány alapot:"))
kitevo=int(input("Kérem a hatvány kitevőt:"))
hatvanyertek=(hatvany**kitevo)
print(hatvanyertek)

## Harmadik feladat
while True:
    szam=int(input("Kérek egy pozitív számot: "))
    if szam<=0:
        print("Nem tudsz olvasni?? Pozitív szám!")
    else:
        print("Ügyes vagy!")
        break

## Negyedik feladat
a=int(input("Kérem az első számot: "))
b=int(input("Kérek a második számot: "))

if a>b:
    print("A két szám közötti távolság: "+str(-(b-a)))
else:
    print("A két szám közötti távolság: "+str(-(a-b)))

## Negyedik feladat


# string: ordered, ____, text representation

# init
from timeit import default_timer as timer
movie_name = "The murder on the orient express"

# single quote
fav_quote = 'That\'s what she said'
# print(fav_quote)

# double quote
fav_quote = "That's what she said"
# print(fav_quote)

quote = "Where should I go? \
To the left where nothing is right \
or to the right where nothing is left."
# print(quote)

# triple quote

quote = """Where should I go?
To the left where nothing is right
or to the right where nothing is left."""
# print(quote)

# indexing
movie_name = "The murder on the orient express"

# reverse string
# print(movie_name[::-1])

# slicing
# print(movie_name[4:10])
# print(movie_name[11:17])
# print(movie_name[-4])
# print(movie_name[9:3:-1])

# concate
greetings = "Good morning"
student_name = "John"

greetings = greetings + " " + student_name
# print(greetings)

# interation
# for char in student_name:
#     if char == 'o':
#         print("o in name")
#     else:
#         print("o not in name")

# print(char)

# check
# if 'o' in student_name:
#     print('o in name')
# else:
#     print('o not in name')

# white space
hello = "     hello     "
# print(hello.strip())

# upper lower
# print(hello.upper())
upper_hello = "HELLO"
# print(upper_hello.lower())

# startswith
akshit_email = "akshit.mithaiwala@techbulls.in"
aditya_name = "techbullsaditya"
anushka_name = "techbullsanushka"

# print(anushka_name.startswith("techbulls"))

# domain_name = [".com", ".in"]

# print(akshit_email.endswith(".com"))

# find
akshit_name = "akshit"
# print(akshit_name.find('s'))

# SHIT
s_index = akshit_name.find('s')
shit_name = akshit_name[s_index:]
upper_shit = shit_name.upper()
# print(upper_shit)
# print(akshit_name[akshit_name.find('s'):].upper())

# count
series_name = "The Woman in the House Across the Street from the Girl in the Window"
# print(series_name.count('the'))

# replace
# series_name = series_name.replace('the', 'anything')
# print(series_name)

# split
series_name_list = series_name.split(" ")
# print(series_name_list)

# join
series_name_join_with_comma = ",".join(series_name_list)
# print(series_name_join_with_comma)

# print(series_name.replace(" ", ","))

number_of_a = ['a'] * 100

# print(number_of_a)

# 01
start = timer()
a_join = "".join(number_of_a)
end = timer()

print(end-start)

# 02
start = timer()
a_join_using_loop = ""
for char_a in number_of_a:
    a_join_using_loop += char_a
end = timer()

print(end-start)

# print(a_join)
# print(a_join_using_loop)

# % .format() f-string

name = "akshit"
greetings = "Good morning"
student_roll_no = 123

print(f"{greetings}, this is my name: {name} roll no: {student_roll_no}")

# print("{} yello {}".format(greetings, name))

# print(greetings + " " + name + " " + str(student_roll_no))
# print(1+student_roll_no)


import argparse
import cv2

# ArgParse é usado para captar argumentos passados na chamada do .py no CMD
ap = argparse.ArgumentParser()
# Aqui definimos a label do argumento esperado
ap.add_argument("-i", "--image", required=True,
                help= "Path to the image")
# Criamos um dicionário que receberá os valores dos argumentos
# As chaves do dicionário serão as labels criadas no na definição do argumento
args = vars(ap.parse_args())
# A função vars() retorna os valores correspondente ao atributo __dict__ do objeto

# Aqui lemos a imagem que é acessada através do caminho no disco passado como argumento.
# Acessamos o valor em args usando como chave do dicionário args o mesmo valor que a definição do argumento
image = cv2.imread(args["image"])
print("width: {} pixels".format(image.shape[1]))
print("height: {} pixels".format(image.shape[0]))
print("channels: {}".format(image.shape[2]))
print("Matrix shape: {}".format(image.shape))

cv2.imshow("Image", image)
cv2.waitKey(0)

cv2.imwrite("newimage.jpg", image)


'''  Урок 2. Парсинг HTML. BeautifulSoup, MongoDB

Необходимо собрать информацию о вакансиях на вводимую должность (используем input) с сайтов Superjob(необязательно) и HH(обязательно). Приложение должно анализировать несколько страниц сайта (также вводим через input).
Получившийся список должен содержать в себе минимум:

    Наименование вакансии.
    Предлагаемую зарплату (отдельно минимальную и максимальную).
    Ссылку на саму вакансию.
    Сайт, откуда собрана вакансия.

По желанию можно добавить ещё параметры вакансии (например, работодателя и расположение). Структура должна быть одинаковая для вакансий с обоих сайтов. Общий результат можно вывести с помощью dataFrame через pandas.

'''
from bs4 import BeautifulSoup as bs
import requests
import json

class HHscraper:
    def __init__(self, start_url, headers, params):
        self.start_url = start_url
        self.start_headers = headers
        self.start_params = params
        self.info_vacance = []

    def get_html_string(self, url, headers='', params=''):
        try:
            response = requests.get(url, headers=headers, params=params)
            if response.ok:
                return response.text

        except Exception as e:
            sleep(1)
            print(e)
            return None

    @staticmethod
    def get_dom(html_string):
        return bs(html_string, "html.parser")

    def run(self):
        next_butten_hh = ''
        while  next_butten_hh != None:
            if next_butten_hh == '':
                html_string = self.get_html_string(self.start_url + '/search/vacancy', self.start_headers, self.start_params)
            else:
                html_string = self.get_html_string(next_butten_hh)

            soup = HHscraper.get_dom(html_string)
            vacance_list = soup.findAll('div', attrs={'class': 'vacancy-serp-item'})
            self.get_info_from_element(vacance_list)
            try:
                next_butten_hh = self.start_url + soup.find('a', attrs={'data-qa': 'pager-next'}).attrs["href"]
            except Exception as e:
                next_butten_hh = None

    def get_info_from_element(self, vacance_list):

        for vacance in vacance_list:
            vacance_data = {}
            vacance_name = vacance.find('a', {'class': 'bloko-link'}).getText()
            vacance_city = vacance.find('div', {'data-qa': 'vacancy-serp__vacancy-address'}).getText()
            vacance_link = vacance.find('a', {'class': 'bloko-link'}).attrs["href"]
            vacance_data['имя вакансии'] = vacance_name
            vacance_data['город'] = vacance_city
            vacance_data['ссылка на вакансию'] = vacance_link
            vacance_data['источник'] = self.start_url
            self.get_salary(vacance_data, vacance)
            self.info_vacance.append(vacance_data)

    def get_salary(self, vacance_data, vacance):
        try:
            vacance_salary = vacance.find('span', {'data-qa': 'vacancy-serp__vacancy-compensation'}).getText()
            vacance_salary = vacance_salary.replace('\u202f', '').split()
            if '–' in vacance_salary:
                vacance_data['мин зарплата'] = float(vacance_salary[0])
                vacance_data['макс зарплата'] = float(vacance_salary[2])
                vacance_data['валюта'] = vacance_salary[-1]
            elif 'от' in vacance_salary:
                vacance_data['мин зарплата'] = float(vacance_salary[1])
                vacance_data['валюта'] = vacance_salary[-1]
            elif 'до' in vacance_salary:
                vacance_data['макс зарплата'] = float(vacance_salary[1])
                vacance_data['валюта'] = vacance_salary[-1]

        except Exception as e:
            vacance_data['зарплата'] = None

    def save_info_vacance(self):
        with open("vacancy_hh.json", 'w', encoding="utf-8") as file:
            json.dump(self.info_vacance, file, indent=2, ensure_ascii=False)

class SJscraper:
    def __init__(self, start_url, headers, params):
        self.start_url = start_url
        self.start_headers = headers
        self.start_params = params
        self.info_sj_vacance = []

    def get_html_string(self, url, headers='', params=''):
        try:
            response = requests.get(url, headers=headers, params=params)
            if response.ok:
                return response.text

        except Exception as e:
            sleep(1)
            print(e)
            return None

    @staticmethod
    def get_dom(html_string):
        return bs(html_string, "html.parser")

    def run(self):
        next_butten_sj = ''
        while next_butten_sj != None:
            if next_butten_sj == '':
                html_string = self.get_html_string(self.start_url + "vacancy/search/", self.start_headers,
                                                   self.start_params)
            else:
                html_string = self.get_html_string(next_butten_sj)

            soup = SJscraper.get_dom(html_string)
            vacance_list = soup.findAll('div', attrs={'class': 'Fo44F QiY08 LvoDO'})
            self.get_info_from_element(vacance_list)
            try:
                next_butten_sj = main_link_sj + soup.find('a', attrs={'class': 'f-test-button-dalshe'}).attrs["href"]

            except Exception as e:
                next_butten_sj = None

    def get_info_from_element(self, vacance_list):
        for vacancy in vacance_list:
            vacancy_sj_data = {}
            vacancy_sj_name = vacancy.find('a', {'class': 'icMQ_'}).getText()
            # vacance_sj_city = vacancy.find('span', {'class': 'f-test-text-company-item-location _2LcRC _1_rZy dXrZh Ml4Nx'}).getText()
            vacancy_sj_link = main_link_sj + vacancy.find('a', {'class': 'icMQ_'}).attrs["href"]
            vacancy_sj_data['имя вакансии'] = vacancy_sj_name
            # vacance_sj_city['город'] = vacance_sj_city
            vacancy_sj_data['ссылка на вакансию'] = vacancy_sj_link
            vacancy_sj_data['источник'] = self.start_url
            self.get_salary(vacancy_sj_data, vacancy)
            self.info_sj_vacance.append(vacancy_sj_data)

    def get_salary(self, vacancy_sj_data, vacancy):
        try:
            vacancy_sj_salary = vacancy.find("span",
                                             {'class': "_1OuF_ _1qw9T f-test-text-company-item-salary"}).getText()
            if '—' in vacancy_sj_salary:
                sal = vacancy_sj_salary.replace('\xa0', ' ').split()
                if sal[0].isdigit() and sal[1].isdigit():
                    mim_sal = sal[0] + sal[1]
                    vacancy_sj_data['мин зарплата'] = float(mim_sal)
                else:
                    vacancy_sj_data['мин зарплата'] = float(sal[0])
                if sal[-3].isdigit() and sal[-2].isdigit():
                    max_sal = sal[-3] + sal[-2]
                    vacancy_sj_data['макс зарплата'] = float(max_sal)
                else:
                    vacancy_sj_data['макс зарплата'] = float(sal[-3])
                vacancy_sj_data['валюта'] = sal[-1]
            elif 'По' in vacancy_sj_salary:
                vacancy_sj_data['зарплата'] = "По договоренности"
                vacancy_sj_data['валюта'] = None
            elif 'от' in vacancy_sj_salary:
                sal = vacancy_sj_salary.replace('\xa0', ' ').split()
                if sal[1].isdigit() and sal[2].isdigit():
                    mim_sal = sal[1] + sal[2]
                    vacancy_sj_data['мин зарплата'] = float(mim_sal)
                else:
                    vacancy_sj_data['мин зарплата'] = float(sal[1])
                vacancy_sj_data['валюта'] = sal[-1]
            elif 'до' in vacancy_sj_salary:
                sal = vacancy_sj_salary.replace('\xa0', ' ').split()
                if sal[1].isdigit() and sal[2].isdigit():
                    max_sal = sal[1] + sal[2]
                    vacancy_sj_data['макс зарплата'] = float(max_sal)
                else:
                    vacancy_sj_data['макс зарплата'] = float(sal[1])
                vacancy_sj_data['валюта'] = sal[-1]
            else:
                sal = vacancy_sj_salary.replace('\xa0', ' ').split()
                if sal[0].isdigit() and sal[1].isdigit():
                    user_sal = sal[0] + sal[1]
                    vacancy_sj_data['макс зарплата'] = float(user_sal)
        except:
            vacancy_sj_data['зарплата'] = None

    def save_info_vacance(self):
        with open("vacancy_sj.json", 'w', encoding="utf-8") as file:
            json.dump(self.info_sj_vacance, file, indent=2, ensure_ascii=False)

if __name__ == '__main__':
    user_find = input('Введите вакансию: ')
    #user_find = 'python'

    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36"}

    main_link_hh = "https://hh.ru"
    params_main_hh = {"area": "1",
                      "fromSearchLine": "true",
                      "st": "searchVacancy",
                      "text": user_find,
                      "page": "0"}

    scraper_hh = HHscraper(main_link_hh, headers, params_main_hh)
    scraper_hh.run()
    scraper_hh.save_info_vacance()

    main_link_sj = "https://www.superjob.ru/"
    params_sj = {"keywords": user_find,
                 "geo[t][0]": "4"}
    scraper_sj = SJscraper(main_link_sj, headers, params_sj)
    scraper_sj.run()
    scraper_sj.save_info_vacance()


n=int(input())
arr=list(map(int,input().split()))
sof=0
sos=0
for i in range(n):
  if(i<n//2):
      sof+=arr[i]
  else:
      sos+=arr[i]
print(abs(sof-sos))


def is_finish(x, y):
    return x == 4
l = list(range(4))
cnt = 0
a = set(range(10))
assert(len(a & set(l)) == 4)
print(*l)
cnt += 1
X, Y = map(int, input().split())
if is_finish(X, Y):
    exit(0)

for i in range(4):
    not_in = a - set(l)
    for n in not_in:
        tmpl = l[:]
        tmpl[i] = n
        assert(len(a & set(tmpl)) == 4)
        print(*tmpl)
        cnt += 1
        assert(cnt <= 100)
        tmpx, tmpy = map(int, input().split())
        if is_finish(tmpx, tmpy):
            exit(0)
        if tmpx > X:
            l = tmpl[:]
            X, Y = tmpx, tmpy
            break
        elif tmpx < X:
            break
    else:
        for j in range(i+1, 4):
            tmpl = l[:]
            tmpl[i], tmpl[j] = tmpl[j], tmpl[i]
            assert(len(a & set(tmpl)) == 4)
            print(*tmpl)
            cnt += 1
            assert(cnt <= 100)

            tmpx, tmpy = map(int, input().split())
            if is_finish(tmpx, tmpy):
                exit(0)
            if tmpx > X:
                l = tmpl[:]
                X, Y = tmpx, tmpy
                break
        else:
            assert(0)


import os
import argparse
import re
import textwrap

default_mpi_function_list = [
    "int MPI_Init(int *argc, char ***argv)",
    "int MPI_Finalize(void)",
    "int MPI_Comm_rank(MPI_Comm comm, int *rank)",
    "int MPI_Comm_size(MPI_Comm comm, int *size)",
    "int MPI_Send(const void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm)",
    "int MPI_Recv(void *buf, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm comm, MPI_Status *status)"
]

def extract_between(text, sub1, sub2, nth=1):
    """
    extract a substring from text between two given substrings
    sub1 (nth occurrence) and sub2 (nth occurrence)
    arguments are case sensitive
    """
    # prevent sub2 from being ignored if it's not there
    if sub2 not in text.split(sub1, nth)[-1]:
        return None
    return text.split(sub1, nth)[-1].split(sub2, nth)[0]

def get_args_list(args_name, args_type, args_post):
    d = {}
    d["pargs"] = ""
    d["args"] = ""

    for idy,function_name in enumerate(args_name):
        d["pargs"] += args_type[idy]
        d["pargs"] += " "
        d["pargs"] += args_name[idy]
        d["pargs"] += args_post[idy]
        d["pargs"] += ", "
        d["args"] += args_name[idy]
        d["args"] += ", "
    if(len((d["pargs"])) > 0):
        if(d["pargs"][-2] == ','):
            d["pargs"] = d["pargs"][:-2]
        if(d["args"][-2] == ','):
            d["args"] = d["args"][:-2]

    return d

def get_ret_list(rtype):
    d = {}

    dec_ret_val = ""
    get_ret_val = ""
    ret_ret_val = "return"
    if(rtype != "void"):
        dec_ret_val += rtype + " val = ("+rtype+") 0;"
        get_ret_val += "val = "
        ret_ret_val += " val"
    ret_ret_val += ";"

    d["dec"] = dec_ret_val
    d["get"] = get_ret_val
    d["ret"] = ret_ret_val
    return d

def parse_mpi_functions(mpi_functions_list):
    d={}
    d["name"] = []
    d["type"] = []
    d["args"] = {}
    d["args"]["type"] = []
    d["args"]["name"] = []
    d["args"]["post"] = []

    for function in mpi_functions_list:
        d["name"] += [function.split()[1].split('(')[0]]
        d["type"] += [function.split()[0]]
        args_list = extract_between(function, '(', ')')
        name_list = []
        type_list = []
        post_list = []
        tmp = ""
        for mpi_args in args_list.split(','):
            mpi_arg = mpi_args.split()
            if(len(mpi_arg) > 1):
                tmp_idx = mpi_arg[-1].strip('*').find("[")
                if(tmp_idx < 0):
                    tmp_idx = len(mpi_arg[-1].strip('*'))
                name_list += [mpi_arg[-1].strip('*')[0:tmp_idx]]
                tmp = mpi_arg[0]
                if(tmp == "const"):
                    tmp += " " + mpi_arg[1]
                for idx in range(0,mpi_args.count('*')):
                    tmp += ' *'
                type_list += [tmp]
                if("[" in mpi_arg[-1]):
                    post_list += ["[]"]
                else:
                    post_list += [""]
        d["args"]["name"] += [name_list]
        d["args"]["type"] += [type_list]
        d["args"]["post"] += [post_list]
    return d

def get_mpi_proto_list(d):
    l = []
    for idx,function in enumerate(d["name"]):
        proto = d["type"][idx]+" "+d["name"][idx]+"("
        for idy,function_name in enumerate(d["args"]["name"][idx]):
            proto += d["args"]["type"][idx][idy]
            proto += " "
            proto += d["args"]["name"][idx][idy]
            proto += d["args"]["post"][idx][idy]
            proto += ", "
        if(proto[-2] == ','):
            proto = proto[:-2]
        proto += ")"
        l += [proto]
    return l

def print_selfie_h_header():
    s  = ""
    s += '''#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <cstring>
#include <execinfo.h>
#include <dlfcn.h>
#include <cstdarg>
#include <fenv.h>

#pragma STDC FENV_ACCESS ON

typedef void (*function_type)(...);

'''
    return s

def print_selfie_h_footer():
    s  = ""
    s += '''
}
'''
    return s

def print_selfie_h_n_mpi(d, plugin_name):
    s  = '''
/// \\brief Total number of {1} functions
#define N_{1}_FUNCTIONS {0}

'''.format(str(len(d["name"])), plugin_name.upper())
    return s

def print_selfie_h_get_name(d,plugin_name):

    s  = ""
    s +='''/// \\brief Return a string containing name of functions
/// \\param[in] i Index
/// \\return   Return a string containing name of functions
///
char *selfie_get_{0}_function_name(int i)
{{
   char const *{0}_functions_name[] = {{
'''.format(plugin_name)
    for name in d["name"]:
        s += '''      "{0}",\n'''.format(name)
    for name in d["name"]:
        s += '''      "P{0}",\n'''.format(name)
    s += '''      NULL
   }};
   return strdup({0}_functions_name[i]);
}};
'''.format(plugin_name)
    return s

def print_selfie_h_builtin_function(idx, name, symbol, rtype, plugin_name):
    d_ret = get_ret_list(rtype)
    s  = '''
#ifdef __SELFIE_MPI_BUILTIN__
/// \\brief {1}
///
/// \\param ...
/// \\return {3}
///
/// \details
///

{3} {1}(...)
{{
    double f_start = 0.0;
    function_type selfie_function = NULL;
    int ap_except = 0;

    selfie_function = selfie_{4}_pointer_functions[{0}];

    if(selfie_function == NULL)
    {{
       selfie_function = (function_type) dlsym(RTLD_NEXT,"{2}");
    }}

    selfie_{4}_global_data[{0}].function_count++;
    f_start = selfie_mysecond();
    ap_except = fedisableexcept(FE_INVALID);
    void* ret = __builtin_apply(selfie_function,
                                      __builtin_apply_args(), 1024);
    feclearexcept(FE_INVALID);
    feenableexcept(ap_except);
    selfie_{4}_global_data[{0}].function_time += selfie_mysecond() - f_start;
    __builtin_return(ret);

}};
#endif
'''.format(idx, name, symbol, rtype, plugin_name)
    return s

def print_selfie_h_functions(d,plugin_name):

    s  = ""

    for idx,name in enumerate(d["name"]):
        s += print_selfie_h_builtin_function(idx, name, name,
                                             d["type"][idx], plugin_name)
        s += print_selfie_h_builtin_function(idx,
                                             "P"+name, name, d["type"][idx],
                                             plugin_name)
    return s

def print_selfie_h_global_array(d,plugin_name):
    s =  '''
/// \\brief Array of pointers of functions
function_type selfie_{1}_orig_pointer_functions[{0}] = {{NULL}};

/// \\brief Array of pointers of functions
function_type *selfie_{1}_pointer_functions = selfie_{1}_orig_pointer_functions;

'''.format(len(d["name"]),plugin_name)
    return s

def print_selfie_h(d,pname):

    s  = ""
    s += print_selfie_h_header()
    s += print_selfie_h_n_mpi(d, pname)
    s += print_selfie_h_get_name(d, pname)
    s += print_selfie_h_global_array(d, pname)
    s += "\nextern \"C\" {\n\n"
    s += print_selfie_h_functions(d, pname)
    s += print_selfie_h_footer()
    return s

def read_inputfile(inputfile):
    function_list = []
    with open(inputfile,"r") as fdi:
        for line in fdi:
            if (len(line) > 1):
                function_list += [line[:-1]]
    return function_list

def main():
    parser = argparse.ArgumentParser(
        description="Generate list of MPI functions")
    parser.add_argument("-p","--proto",action="store_true",
                        default=False,
                        help="Print list of MPI functions prototypes")
    parser.add_argument("-i","--input",action="store",
                        default=None,
                        help="File containing MPI functions list")
    parser.add_argument("-n","--name",action="store",
                        default="mpi",
                        help="Name of plugin")
    parser.add_argument("-o","--output",action="store",
                        default=None,
                        help="File where to print "+
                        "result (If None, print to stdout)")

    args = parser.parse_args()
    print("")
    print(parser.description)
    print("")

    header = True

    # Print proto or not
    if(args.proto == True):
        header = False

    # Input file
    if(args.input != None):
        mpi_function_list = read_inputfile(args.input)
    else:
        mpi_function_list = default_mpi_function_list

    # Output file
    if(args.output != None):
        outfile = args.output
    else:
        outfile = None

    pname = args.name

    # Parse functions
    d = parse_mpi_functions(mpi_function_list)

    # Print prototypes
    if(header == False):
        if(outfile == None):
            for proto_name in get_mpi_proto_list(d):
                print(proto_name)
        else:
            with open(outfile,"w") as fd:
                for proto_name in get_mpi_proto_list(d):
                    fd.write(proto_name)
            print("File "+outfile+" written")
    # Print header
    else:
        if(outfile == None):
            print(print_selfie_h(d,pname))
        else:
            with open(outfile,"w") as fd:
                fd.write(print_selfie_h(d,pname))
            print("File "+outfile+" written")

if __name__ == "__main__": main()


import matplotlib.pyplot as plt
import random
import numpy as np
from IPython.display import display, clear_output
import time

def head_home(x, y):
    """
    Head home down and to the left.

    Parameters
    ----------

    x : float
        Horizontal coordinate.

    y : float
        Vertical coordinate.

    Returns
    -------
    x : float
        Updated horizontal coordinate.

    y : float
        Updated vertical coordinate.

    """

    pick = np.zeros(x + y)
    pick[0:x] = 1
    if (np.random.choice(pick) == 1):
        x -= 1
    else:
        y -= 1
    if (x < 0):
        x = 0
    if (y < 0):
        y = 0
    return x, y

def search_for_food(x, y, smell):
    """
    Search for food by following the smell.

    Parameters
    ----------

    x : float
        Horizontal coordinate.

    y : float
        Vertical coordinate.

    smell : numpy.ndarray
        2D array of smells

    Returns
    -------
    x : float
        Updated horizontal coordinate.

    y : float
        Updated vertical coordinate.

    """
    directions = ['up', 'left', 'down', 'right']

    x_dim = smell.shape[0]
    y_dim = smell.shape[1]

    # First check to see if there is food up and to the right.
    g = []  # follow gradient
    m = []

    if (x + 1 < x_dim):
        if (smell[x + 1, y] > 0):
            m.append(smell[x + 1, y])
            g.append('right')
    if (y + 1 < y_dim):
        if (smell[x, y + 1] > 0):
            m.append(smell[x, y + 1])
            g.append('up')
    if (g != []):
        grad = g[m.index(max(m))]
        # print("Following smell", grad)
    else:
        # else just pick a random direction.
        grad = random.choice(directions)
        # print("Choosing ",grad)

    # move the ant
    if (grad == 'up'):
        y = y + 1
    elif (grad == 'right'):
        x = x + 1
    elif (grad == 'down'):
        y = y - 1
    elif (grad == 'left'):
        x = x - 1
    else:
        print(grad)
        print("ERROR!!!!!!!!!!!!")

    # make sure we don't go off the gird.
    if (x < 0):
        x = 0
    if (y < 0):
        y = 0
    if (x > x_dim - 1):
        x = x_dim - 1
    if (y > y_dim - 1):
        y = y_dim - 1
    return x, y

def run(num_ants=100, x_dim=70, y_dim=30):
    """
    Run the simulation

    Parameters
    ----------
    num_ants : int
        Initial number of ants to simulate. Dafualt =100

    x_dim : int
        Horizontal dimension of the board. Default = 70

    y_dim : int
        Vertical  dimension of the board. Default = 30

    """
    smell = np.zeros((x_dim, y_dim))
    food = np.zeros((x_dim, y_dim))

    # place food
    food[45:50, 25:30] = 10
    food[45:50, 25:30] = 10
    food[65:70, 0:5] = 10

    x_loc = np.random.randint(0, x_dim, size=(num_ants, 1))
    y_loc = np.random.randint(0, y_dim, size=(num_ants, 1))
    ant_loc = np.concatenate((x_loc, y_loc), axis=1)

    has_food = np.zeros((num_ants, 1))

    fig, ax = plt.subplots(figsize=(10, 5))

    # Main simulation loop
    for i in range(0, 100):

        # Loop over ants
        for a in range(0, num_ants):
            x = ant_loc[a, 0]
            y = ant_loc[a, 1]

            if (x == 0 and y == 0):
                has_food[a] = 0

            if has_food[a] > 0:
                x, y = head_home(x, y)
                smell[x, y] = smell[x, y] + 100

            else:
                x, y = search_for_food(x, y, smell)

            if food[x, y] > 0:
                food[x, y] -= 1
                has_food[a] = 1

            ant_loc[a, 0] = x
            ant_loc[a, 1] = y

        smell = smell - 1
        smell[smell < 0] = 0

        # plot world
        plt.imshow(food.T, origin='lower', aspect='equal', cmap="magma")
        for a in range(0, num_ants):
            color = 'r'
            if (has_food[a] > 0):
                color = 'g'
            plt.scatter(ant_loc[a, 0], ant_loc[a, 1], color=color)

        # Animaiton part (dosn't change)
        clear_output(wait=True)  # Clear output for dynamic display
        display(fig)  # Reset display
        fig.clear()  # Prevent overlapping and layered plots
        time.sleep(0.0001)  # Sleep for a fraction of a second to allow animation to catch up


from django.views.generic.base import TemplateView
from albums.forms import FileForm
from albums.models import Album, File
from core.decorators import view_decorator
from core.views import ResourceView

class AlbumPage(TemplateView):
    template_name = "albums/main.html"

def expose(view):
    view.expose = True
    return view

@view_decorator(expose)
class AlbumView(ResourceView):
    model = Album

@view_decorator(expose)
class FileView(ResourceView):
    create_form = FileForm
    model = File


from flask import request

def validate_id(id):

    # if not found in params
    if (id is None):
        raise TypeError("Request params (id) not found")

    # if description params is empty
    if not id:
        raise ValueError("id is empty")

    # if not integer
    if not isinstance(id, int):
        raise TypeError("id is not integer")

def validate_latitude(latitude):

    # if not found in params
    if (latitude is None):
        raise TypeError("Request params (latitude) not found")

    # if not float
    if not isinstance(latitude, float):
        raise TypeError("latitude is not float")

def validate_longtitude(longtitude):

    # if not found in params
    if (longtitude is None):
        raise TypeError("Request params (longtitude) not found")

    # if not float
    if not isinstance(longtitude, float):
        raise TypeError("longtitude is not float")

def point_read_contract(request):

    id = request.args.get('id', type=int)

    validate_id(id)

    return {
        'id': int(id)
    }

def point_create_contract(request):

    latitude = request.args.get('latitude', type=float)
    longtitude = request.args.get('longtitude', type=float)

    validate_latitude(latitude)
    validate_longtitude(longtitude)

    return {
        'latitude': float(latitude),
        'longtitude': float(longtitude)
    }


import argparse
import asyncio
import csv
import functools
import gc
import hashlib
import http.client
import importlib
import io
import math
import platform
import re
import socket
import statistics
import sys
import textwrap
import time
import urllib.parse
from typing import Callable, Awaitable, Tuple, Iterable, Optional

_Method = Callable[[str], bytes]
_AMethod = Callable[[str], Awaitable[bytes]]
METHODS = {}
CHECKSUMS = {
    10**6 + 128: 'fa82243e0db587af04504f5d3229ff7227f574f8f938edaad8be8e168bc2bc87',
    10**7 + 128: '128ceaac08362426bb7271ed6202d11c6830587a415bd7868359725c22d2fe88',
    10**9 + 128: 'd699e2c306b897609be6222315366b25137778e18f8634c75b006cef50647978'
}

def method(name: str, requires: Iterable[str] = ()) -> Callable[[_Method], _Method]:
    def decorate(func: _Method) -> _Method:
        for mod in requires:
            try:
                importlib.import_module(mod)
            except ImportError:
                return func
        METHODS[name] = func
        return func

    return decorate

def run_async(func: _AMethod) -> _Method:
    @functools.wraps(func)
    def wrapper(url: str) -> bytes:
        loop = asyncio.new_event_loop()
        try:
            asyncio.set_event_loop(loop)
            return loop.run_until_complete(func(url))
        finally:
            loop.run_until_complete(loop.shutdown_asyncgens())
            loop.close()

    return wrapper

@method('httpclient')
def load_httpclient(url: str) -> bytes:
    parts = urllib.parse.urlparse(url)
    conn = http.client.HTTPConnection(parts.netloc)
    conn.request('GET', parts.path)
    resp = conn.getresponse()
    return resp.read(resp.length)     # type: ignore

@method('httpclient-na')
def load_httpclient_na(url: str) -> bytes:
    parts = urllib.parse.urlparse(url)
    conn = http.client.HTTPConnection(parts.netloc)
    conn.request('GET', parts.path)
    resp = conn.getresponse()
    return resp.read()

@method('requests', ['requests'])
def load_requests(url: str) -> bytes:
    import requests
    return requests.get(url).content

@method('requests-c1M', ['requests'])
def load_requests_c1M(url: str) -> bytes:
    import requests
    old_chunk = requests.models.CONTENT_CHUNK_SIZE
    try:
        requests.models.CONTENT_CHUNK_SIZE = 1024 * 1024
        return requests.get(url).content
    finally:
        requests.models.CONTENT_CHUNK_SIZE = old_chunk

@method('requests-stream', ['requests'])
def load_requests_stream(url: str) -> bytes:
    import requests
    with requests.get(url, stream=True) as resp:
        return resp.raw.read()

@method('requests-stream-fp-read', ['requests'])
def load_requests_stream_fp_read(url: str) -> bytes:
    import requests
    with requests.get(url, stream=True) as resp:
        return resp.raw._fp.read()

@method('requests-np', ['requests', 'numpy'])
def load_requests_np(url: str) -> bytes:
    import requests
    import numpy as np
    with requests.get(url, stream=True) as resp:
        data = np.empty(int(resp.headers['Content-length']), np.uint8)
        resp.raw.readinto(memoryview(data))
    return data

@method('requests-np-fp', ['requests', 'numpy'])
def load_requests_np(url: str) -> bytes:
    import requests
    import numpy as np
    with requests.get(url, stream=True) as resp:
        data = np.empty(int(resp.headers['Content-length']), np.uint8)
        resp.raw._fp.readinto(memoryview(data))
    return data

@method('urllib3', ['urllib3'])
def load_urllib3(url: str) -> bytes:
    import urllib3
    return urllib3.PoolManager().request('GET', url).data

@method('tornado', ['tornado'])
@run_async
async def load_tornado(url: str) -> bytes:
    import tornado.simple_httpclient
    client = tornado.simple_httpclient.SimpleAsyncHTTPClient(max_body_size=10**10)
    response = await client.fetch(url)
    return response.body

@method('aiohttp', ['aiohttp'])
@run_async
async def load_aiohttp(url: str) -> bytes:
    import aiohttp
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as resp:
            return await resp.read()

@method('httpx', ['httpx'])
def load_httpx(url: str) -> bytes:
    import httpx
    return httpx.get(url).content

@method('httpx-async', ['httpx'])
@run_async
async def load_httpx_async(url: str) -> bytes:
    import httpx
    async with httpx.AsyncClient() as client:
        r = await client.get(url)
        return r.content

def prepare_socket(url: str) -> Tuple[io.BufferedIOBase, int]:
    parts = urllib.parse.urlparse(url)
    address = (parts.hostname, parts.port)
    sock = socket.socket()
    sock.connect(address)
    req_header = textwrap.dedent(f'''\
        GET {parts.path} HTTP/1.1
        Host: {parts.hostname}:{parts.port}
        User-Agent: python
        Connection: close
        Accept: */*

        ''').replace('\n', '\r\n').encode('ascii')
    fh = sock.makefile('rwb')
    fh.write(req_header)
    fh.flush()
    content_length: Optional[int] = None
    while True:
        line = fh.readline()
        if line == b'\r\n':
            if content_length is None:
                raise RuntimeError('Did not receive Content-Length header')
            return fh, content_length        # type: ignore
        else:
            text = line.decode('latin-1').rstrip().lower()
            if text.startswith('content-length: '):
                content_length = int(text.split(' ')[1])

@method('socket-read')
def load_socket_read(url: str) -> bytes:
    fh, content_length = prepare_socket(url)
    return fh.read(content_length)

@method('socket-readinto')
def load_socket_readinto(url: str) -> bytes:
    fh, content_length = prepare_socket(url)
    raw = bytearray(content_length)
    n = fh.readinto(raw)
    assert n == content_length
    return memoryview(raw)[:n]

def validate(data: bytes):
    size = len(data)
    try:
        checksum = CHECKSUMS[size]
    except KeyError:
        print('No checksum found')
    else:
        actual_checksum = hashlib.sha256(data).hexdigest()
        if actual_checksum != checksum:
            print(f'Checksum mismatch ({actual_checksum} != {checksum})')

def measure_method(method: str, args: argparse.Namespace) -> None:
    # Warmup pass
    METHODS[method](args.url)
    rates = []
    size = 0
    for i in range(args.passes):
        gc.collect()
        start = time.monotonic()
        data = METHODS[method](args.url)
        stop = time.monotonic()
        elapsed = stop - start
        rates.append(len(data) / elapsed)
        if i == 0:
            validate(data)
            size = len(data)
        del data
    mean = statistics.mean(rates)
    std = statistics.stdev(rates) / math.sqrt(args.passes - 1)
    return mean, std, size

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--passes', type=int, default=5)
    parser.add_argument('--csv', action='store_true')
    parser.add_argument('method')
    parser.add_argument('url')
    args = parser.parse_args()
    if args.method not in METHODS and args.method != 'all':
        parser.error('Method must be "all" or one of {}'.format(set(METHODS.keys())))

    if args.csv:
        writer = csv.DictWriter(sys.stdout, ['Python', 'Method', 'Size', 'mean', 'std'])
        writer.writeheader()
        match = re.search(r'PyPy \S+', sys.version)
        if match:
            version = match.group(0)
        else:
            version = platform.python_version()

    if args.method == 'all':
        methods = METHODS
    else:
        methods = [args.method]
    for method in methods:
        mean, std, size = measure_method(method, args)
        if args.csv:
            writer.writerow(
                {
                    'Python': version,
                    'Method': method,
                    'Size': size,
                    'mean': mean,
                    'std': std
                }
            )
        else:
            print('{}: {:.1f} ± {:.1f} MB/s'.format(method, mean / 1e6, std / 1e6))

if __name__ == '__main__':
    main()


import math
def f(a:float, b:float, c:float) -> float:
  if a==0:
    raise Exception("a no puede ser cero")
  if b*b< 4*a*c:
    raise Exception("Esos valores dan un resultado complejo")
  try:
    d=(-b + math.sqrt(b*b - 4*a*c))/(2*a)
  except:
    print("Hay un error")
  return d
a=0
b=3
c=-3
print(f(a,b,c))


import numpy as np
import argparse
import imutils
import cv2

ap = argparse.ArgumentParser()
ap.add_argument("-i","--image",required = True, help="Path of Image File")
args = vars(ap.parse_args())

#image = cv2.imread("image.png")
print("Path: ", args["image"])
image = cv2.imread(args["image"])

# find all the 'black' shapes in the image
upper = np.array([15,15,15])
lower = np.array([0,0,0])
shapeMask = cv2.inRange(image,lower,upper)

# find the contours in the mask
cnts = cv2.findContours(shapeMask.copy(), cv2.RETR_EXTERNAL,
	cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
print("Found {} black shapes".format(len(cnts)))
cv2.imshow("Mask", shapeMask)
# loop over the contours
for c in cnts:
	# draw the contour and show it
	cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
	cv2.imshow("Image", image)
	cv2.waitKey(0)


from pymongo import MongoClient
import pprint
from urllib.request import urlopen
from bs4 import BeautifulSoup

class Data_extraction_creation:

    def __init__(self):
        self.source=""
        self.search=""
        self.search_length=0

    def getting_source(self):
        #client=MongoClient("mongodb://127.0.0.1:27017")
        #database=client['testing']

        self.file_name=input("Enter the name of the text file to read the source code :\n")
        self.file_name = self.file_name + ".txt"
        self.file_open=open(self.file_name, 'r')
        self.file2=self.file_open.read()
        self.file=BeautifulSoup(self.file2)
        print(self.file + "\n\n")
        self.search="small text-uber-white"
        search_length=len(self.search)
        c=0
        for i in range(0, len((self.file))-search_length): # for total counting part
            substr = self.file[i:i+search_length]
            if self.search == substr:
                c = c + 1
                if c == 3: # got the total time of the day
                    self.time_total = self.file[i+search_length+2: i+search_length+12]

                if c==4: # got the total distance of the day
                    self.distance_total = self.file[i+search_length+2:i+search_length+7] + " km"

                if c==5: # got the total cash collection
                    self.cash_collection_total = self.file[i+search_length+2:i+search_length+10]

                if c==6: # got the total earnings
                    self.earnings_total = self.file[i+search_length+2: i+search_length+10]
                    break

        #print(self.time_total + self.distance_total + self.cash_collection_total + self.earnings_total)

        self.search='
' # first day
        search_length=len(self.search)
        c=0

        day=""
        #collection=database[day]

        day_last_left=0
        for i in range(0, len((self.file))-search_length): # counting individual trip of that day.
            substr = self.file[i:i+search_length]

            if self.search == substr:
                trip_number=-1
                pos=i
                pos_span_ending=0
                ending_span=""
                for oo in range(1, 1000):
                    ss=self.file[pos + oo: pos+oo+7]
                    if ss=="":
                        pos_span_ending=pos+oo
                c = c + 1 # day count
                day = self.file[i+search_length+1:pos_span_ending+1]
                s_trip_start=''
                s_trip_time=''
                s_trip_distance='<span class="trip-list__distance layout__item one-quarter"'
                s_trip_earning='<span class="soft-tiny--left"'
                span_endings=''
                s_trip_start_l=len(s_trip_start)
                s_trip_time_l=len(s_trip_time)
                s_trip_distance_l=len(s_trip_distance)
                s_trip_earning_l=len(s_trip_earning)
                e_trip_start=0
                e_trip_time=0
                e_trip_distance=0
                e_trip_earning=0
                check=2
                trip_number = trip_number + 1

                # trip time
                for r in range(e_trip_time, len(self.file)- s_trip_time_l):
                    t = self.file[ e_trip_time + r : e_trip_time + r + s_trip_time_l ]
                    check=2

                    if t == s_trip_time:

                        start = r + s_trip_time_l +1
                        for m in range(1,100): # trip time findings
                            now=self.file[r+m: r+m+7]
                            if now==span_endings:
                                e_trip_time=r+m+7
                                self.trip_time=self.file[start : e_trip_time + 1 ]
                                check=0
                                break
                        if trip_number==0:
                            continue

                        if check==0:
                            check=2
                            break

                # trip start time
                for r in range(e_trip_start, len(self.file)- s_trip_start_l):
                    t = self.file[ e_trip_start + r : e_trip_start + r + s_trip_start_l ]
                    check=2
                    if t == s_trip_start:
                        start = r + s_trip_start_l +1
                        for m in range(1,100): # trip time findings
                            now=self.file[r+m: r+m+7]
                            if now==span_endings:
                                e_trip_start=r+m+7
                                self.trip_start=self.file[start : e_trip_start + 1 ]
                                check=0
                                break
                        if trip_number==0:
                            continue
                        if check==0:
                            check=2
                            break

                #trip distance
                for r in range(e_trip_distance, len(self.file)- s_trip_distance_l):
                    t = self.file[ e_trip_distance + r : e_trip_distance + r + s_trip_distance_l ]
                    check=2
                    if t== s_trip_distance:
                        start = r + s_trip_distance_l +1
                        for m in range(1,100): # trip time findings
                            now=self.file[r+m: r+m+7]
                            if now==span_endings:
                                e_trip_distance=r+m+7
                                self.trip_distance=self.file[start : e_trip_distance + 1 ]
                                check=0
                                break
                        if trip_number==0:
                            continue
                        if check==0:
                            check=2
                            break

                # trip earnings
                for r in range(e_trip_earning, len(self.file)- s_trip_earning_l):
                    t = self.file[ e_trip_earning + r : e_trip_earning + r + s_trip_earning_l ]
                    check=2
                    if t==s_trip_earning:
                        start = r + s_trip_earning_l +1
                        for m in range(1,100): # trip time findings
                            now=self.file[r+m: r+m+7]
                            if now==span_endings:
                                e_trip_earning=r+m+7
                                self.trip_earning=self.file[start : e_trip_earning + 1 ]
                                check=0
                                break
                        if trip_number==0:
                            continue
                        if check==0:
                            check=2
                            break

                # completed trips calcultaion for one trip.
                print("Day "+day)
                print("Trip number "+str(trip_number))
                print("Trip starting "+self.trip_start)
                print("Trip time "+self.trip_time)
                print("Trip distance "+self.trip_distance)
                print("Trip earnings "+self.trip_earning)

object= Data_extraction_creation()
object.getting_source()


#!/bin/python3

import os
import sys

#
# Complete the xorMatrix function below.
#

#define  GET_BIT(x, bit)        (((x)>>(bit)) & 1ULL)

def xorMatrix(m, first_row):
    m = m - 1
    for j in range(63, -1, -1):
        if((m>>j) & 1 == 1):
            intialRow = first_row.copy()

            for i in range(0, len(first_row)):
                first_row[i] ^= intialRow[(i + (1<<j)) % len(first_row)];
    return first_row

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    nm = input().split()

    n = int(nm[0])

    m = int(nm[1])

    first_row = list(map(int, input().rstrip().split()))

    last_row = xorMatrix(m, first_row)

    fptr.write(' '.join(map(str, last_row)))
    fptr.write('\n')

    fptr.close()


import copy
import tempfile
import yaml
import re
import os

import constellation.vault as vault
from constellation.util import ImageReference

def read_yaml(filename):
    with open(filename, "r") as f:
        dat = yaml.load(f, Loader=yaml.SafeLoader)
    dat = parse_env_vars(dat)
    return dat

def config_build(path, data, extra=None, options=None):
    data = copy.deepcopy(data)
    if extra:
        data_extra = read_yaml("{}/{}.yml".format(path, extra))
        config_check_additional(data_extra)
        combine(data, data_extra)
    if options:
        if isinstance(options, list):
            options = collapse(options)
        config_check_additional(options)
        combine(data, options)
    return data

# Utility function for centralising control over pulling information
# out of the configuration.
def config_value(data, path, data_type, is_optional, default=None):
    if type(path) is str:
        path = [path]
    for i, p in enumerate(path):
        try:
            data = data[p]
            if data is None:
                raise KeyError()
        except KeyError as e:
            if is_optional:
                return default
            e.args = (":".join(path[:(i + 1)]),)
            raise e

    expected = {"string": str,
                "integer": int,
                "boolean": bool,
                "dict": dict,
                "list": list}
    if type(data) is not expected[data_type]:
        raise ValueError("Expected {} for {}".format(
            data_type, ":".join(path)))
    return data

# TODO: This can be made better with respect to optional values (e.g.,
# if url is present other keys are required).
def config_vault(data, path):
    url = config_string(data, path + ["addr"], True)
    auth_method = config_string(data, path + ["auth", "method"], True)
    auth_args = config_dict(data, path + ["auth", "args"], True)
    return vault.vault_config(url, auth_method, auth_args)

def config_string(data, path, is_optional=False, default=None):
    return config_value(data, path, "string", is_optional, default)

def config_integer(data, path, is_optional=False, default=None):
    return config_value(data, path, "integer", is_optional, default)

def config_boolean(data, path, is_optional=False, default=None):
    return config_value(data, path, "boolean", is_optional, default)

def config_dict(data, path, is_optional=False, default=None):
    return config_value(data, path, "dict", is_optional, default)

def config_dict_strict(data, path, keys, is_optional=False, default=None):
    d = config_dict(data, path, is_optional)
    if not d:
        return default
    if set(keys) != set(d.keys()):
        raise ValueError("Expected keys {} for {}".format(
            ", ".join(keys), ":".join(path)))
    for k, v in d.items():
        if type(v) is not str:
            raise ValueError("Expected a string for {}".format(
                ":".join(path + [k])))
    return d

def config_list(data, path, is_optional=False, default=None):
    return config_value(data, path, "list", is_optional, default)

def config_enum(data, path, values, is_optional=False, default=None):
    value = config_string(data, path, is_optional, default)
    if value not in values:
        raise ValueError("Expected one of [{}] for {}".format(
            ", ".join(values), ":".join(path)))
    return value

def config_image_reference(dat, path, name="name"):
    if type(path) is str:
        path = [path]
    repo = config_string(dat, path + ["repo"])
    name = config_string(dat, path + [name])
    tag = config_string(dat, path + ["tag"])
    return ImageReference(repo, name, tag)

def config_check_additional(options):
    if "container_prefix" in options:
        raise Exception("'container_prefix' may not be modified")

def combine(base, extra):
    """Combine exactly two dictionaries recursively, modifying the first
argument in place with the contets of the second"""
    for k, v in extra.items():
        if k in base and type(base[k]) is dict and v is not None:
            combine(base[k], v)
        else:
            base[k] = v

def collapse(options):
    """Combine a list of dictionaries recursively, combining from left to
right so that later dictionaries override values in earlier ones"""
    ret = {}
    for o in options:
        combine(ret, o)
    return ret

def parse_env_vars(data):
    if isinstance(data, (dict, list)):
        for k, v in (data.items() if isinstance(data, dict)
                     else enumerate(data)):
            if isinstance(v, (dict, list)):
                data[k] = parse_env_vars(v)
            if isinstance(v, str) and re.search("^\\$[0-9A-Z_]+$", v):
                data[k] = get_envvar(v[1:])
    return data

def get_envvar(name):
    try:
        return os.environ[name]
    except KeyError:
        raise KeyError("Did not find env var '{}'".format(
            name))


from secuenciales.colaprioridad import *
from secuenciales.pila import Pila
import copy

class nodoGrafo:
    def __init__(self, nodo_padre, torreA, torreB, torreC):
        self.torreA = torreA
        self.torreB = torreB
        self.torreC = torreC
        self.padre = nodo_padre
        self.nivel = self.calcularNivelNodo()
        self.funcion_heuristica = self.CalcularFuncionHeuristica()

    def calcularNivelNodo(self):
        if self.padre is not None:
            return self.padre.nivel + 1
        else:
            return 0

    def calcularFuncionHeuristicaTorre(self, torre, idTorre):
        iteracion = 0
        aux_disco = 0
        valor_heuristico_torre = 0
        if len(torre) > 0:
            for disco in torre:
                if iteracion == 0:
                    aux_disco = disco
                    iteracion += 1
                elif aux_disco < disco:
                    if idTorre == "A": valor_heuristico_torre += 1
                    if idTorre == "B": valor_heuristico_torre += 15
                    if idTorre == "C": valor_heuristico_torre += 10
                    aux_disco = disco
                elif aux_disco > disco:
                    valor_heuristico_torre -= 1000

            if idTorre == "A": valor_heuristico_torre += 1
            if idTorre == "B": valor_heuristico_torre += 15
            if idTorre == "C": valor_heuristico_torre += 10

        return valor_heuristico_torre

    def CalcularFuncionHeuristica(self):
        valor_heuristico = 0

        valor_heuristico += self.calcularFuncionHeuristicaTorre(self.torreA, "A")
        valor_heuristico += self.calcularFuncionHeuristicaTorre(self.torreB, "B")
        valor_heuristico += self.calcularFuncionHeuristicaTorre(self.torreC, "C")

        if len(self.torreC) > 0:
            actual = 0
            for disco in self.torreC:
                actual = disco
            if actual == 4:
                valor_heuristico += 15
            if actual == 3:
                valor_heuristico += 10
            if actual == 2:
                valor_heuristico += 5
            if actual == 1:
                valor_heuristico += 1

        if self.nivel > 15:
            valor_heuristico -= 15

        return valor_heuristico - self.nivel

    def convertirEnLista(self, torre):
        auxTorre = copy.deepcopy(torre)
        lista = []
        if len(auxTorre) > 0:
            for disco in auxTorre:
                lista.append(disco)
                auxTorre.desapilar()
        return lista

    def generarEstadosSucesores(self):
        lista_sucesores = []
        if not (self.torreA.cima() is None):
            lista_sucesores += self.generarSucesores("A")
        if not (self.torreB.cima() is None):
            lista_sucesores += self.generarSucesores("B")
        if not (self.torreC.cima() is None):
            lista_sucesores += self.generarSucesores("C")
        return lista_sucesores

    def generarSucesores(self, idTorre):
        if idTorre == "A":
            lista_sucesoresA = []
            copia_estado1 = copy.deepcopy(self)
            copia_estado2 = copy.deepcopy(self)
            copia_estado1.torreB.apilar(copia_estado1.torreA.cima())
            copia_estado1.torreA.desapilar()
            self.recalcularParametros(copia_estado1)
            lista_sucesoresA.append(copia_estado1)
            copia_estado2.torreC.apilar(copia_estado2.torreA.cima())
            copia_estado2.torreA.desapilar()
            self.recalcularParametros(copia_estado2)
            lista_sucesoresA.append(copia_estado2)
            return lista_sucesoresA
        if idTorre == "B":
            lista_sucesoresB = []
            copia_estado1 = copy.deepcopy(self)
            copia_estado2 = copy.deepcopy(self)
            copia_estado1.torreA.apilar(copia_estado1.torreB.cima())
            copia_estado1.torreB.desapilar()
            self.recalcularParametros(copia_estado1)
            lista_sucesoresB.append(copia_estado1)
            copia_estado2.torreC.apilar(copia_estado2.torreB.cima())
            copia_estado2.torreB.desapilar()
            self.recalcularParametros(copia_estado2)
            lista_sucesoresB.append(copia_estado2)
            return lista_sucesoresB
        if idTorre == "C":
            lista_sucesoresC = []
            copia_estado1 = copy.deepcopy(self)
            copia_estado2 = copy.deepcopy(self)
            copia_estado1.torreB.apilar(copia_estado1.torreC.cima())
            copia_estado1.torreC.desapilar()
            self.recalcularParametros(copia_estado1)
            lista_sucesoresC.append(copia_estado1)
            copia_estado2.torreA.apilar(copia_estado2.torreC.cima())
            copia_estado2.torreC.desapilar()
            self.recalcularParametros(copia_estado2)
            lista_sucesoresC.append(copia_estado2)
            return lista_sucesoresC

    def recalcularParametros(self, estado):
        estado.padre = self
        estado.nivel = estado.calcularNivelNodo()
        estado.funcion_heuristica = estado.CalcularFuncionHeuristica()

    def __eq__(self, other) -> bool:
        if self.convertirEnLista(self.torreA) == self.convertirEnLista(other.torreA) and self.convertirEnLista(self.torreB) == self.convertirEnLista(other.torreB) and self.convertirEnLista(self.torreC) == self.convertirEnLista(other.torreC):
            return True
        return False

    def __str__(self) -> str:
        estado = ""
        for disco in reversed(self.convertirEnLista(self.torreA)):
            estado += str(disco) + " "
        estado += "\n"
        for disco in reversed(self.convertirEnLista(self.torreB)):
            estado += str(disco) + " "
        estado += "\n"
        for disco in reversed(self.convertirEnLista(self.torreC)):
            estado += str(disco) + " "
        estado += "\n"
        estado += "FH = " + str(self.funcion_heuristica)
        return estado

    def backtracking(self, list_solucion):
        list_solucion.append(self)
        if self.padre is not None:
            return self.padre.backtracking(list_solucion)
        else:
            return 0

def inAbiertos(abiertos, estado):
    flag = False
    for abierto in abiertos:
        if abierto.dato.__eq__(estado):
            flag = True
            break
    return flag

if __name__ == "__main__":
    abiertos = Colaprioridad()
    cerrados = []

    torreA = Pila()
    torreA.apilar(4)
    torreA.apilar(3)
    torreA.apilar(2)
    torreA.apilar(1)
    torreB = Pila()
    torreC = Pila()

    torreAo = Pila()
    torreBo = Pila()
    torreCo = Pila()
    torreCo.apilar(4)
    torreCo.apilar(3)
    torreCo.apilar(2)
    torreCo.apilar(1)

    estado_inicial = nodoGrafo(None, torreA, torreB, torreC)
    estado_objetivo = nodoGrafo(None, torreAo, torreBo, torreCo)
    abiertos.encolar(estado_inicial, estado_inicial.funcion_heuristica)
    estado_actual = estado_inicial
    iteraciones = 0
    while not (estado_actual.__eq__(estado_objetivo)) and len(abiertos) > 0:
        estado_actual = abiertos.desencolar().dato
        # print(estado_actual)
        # print("=======================")
        sucesores = estado_actual.generarEstadosSucesores()
        for estado in sucesores:
            if not inAbiertos(abiertos, estado) and estado not in cerrados:
                abiertos.encolar(estado, estado.funcion_heuristica)
        cerrados.append(estado_actual)
        iteraciones += 1
    if estado_actual.__eq__(estado_objetivo):
        print("Exito")
        print("Nivel : " + str(estado_actual.nivel))
        print("Iteraciones : " + str(iteraciones))
        list_solucion = []
        estado_actual.backtracking(list_solucion)
        for solucion in reversed(list_solucion):
            print("=======================")
            print(solucion)
    else:
        print("No se pudo encontrar una solucion")


from __future__ import annotations

import logging
from dataclasses import dataclass

from pants.backend.python.subsystems.twine import TwineSubsystem
from pants.backend.python.target_types import PythonDistribution
from pants.backend.python.util_rules.pex import PexRequest, VenvPex, VenvPexProcess
from pants.core.goals.publish import (
    PublishFieldSet,
    PublishOutputData,
    PublishPackages,
    PublishProcesses,
    PublishRequest,
)
from pants.core.util_rules.config_files import ConfigFiles, ConfigFilesRequest
from pants.engine.env_vars import EnvironmentVars, EnvironmentVarsRequest
from pants.engine.fs import CreateDigest, Digest, MergeDigests, Snapshot
from pants.engine.process import InteractiveProcess, Process
from pants.engine.rules import Get, MultiGet, collect_rules, rule
from pants.engine.target import BoolField, StringSequenceField
from pants.option.global_options import GlobalOptions
from pants.util.strutil import help_text

logger = logging.getLogger(__name__)

class PythonRepositoriesField(StringSequenceField):
    alias = "repositories"
    help = help_text(
        """
        List of URL addresses or Twine repository aliases where to publish the Python package.

        Twine is used for publishing Python packages, so the address to any kind of repository
        that Twine supports may be used here.

        Aliases are prefixed with `@` to refer to a config section in your Twine configuration,
        such as a `.pypirc` file. Use `@pypi` to upload to the public PyPi repository, which is
        the default when using Twine directly.
        """
    )

    # Twine uploads to 'pypi' by default, but we don't set default to ["@pypi"] here to make it
    # explicit in the BUILD file when a package is meant for public distribution.

class SkipTwineUploadField(BoolField):
    alias = "skip_twine"
    default = False
    help = "If true, don't publish this target's packages using Twine."

class PublishPythonPackageRequest(PublishRequest):
    pass

@dataclass(frozen=True)
class PublishPythonPackageFieldSet(PublishFieldSet):
    publish_request_type = PublishPythonPackageRequest
    required_fields = (PythonRepositoriesField,)

    repositories: PythonRepositoriesField
    skip_twine: SkipTwineUploadField

    def get_output_data(self) -> PublishOutputData:
        return PublishOutputData(
            {
                "publisher": "twine",
                **super().get_output_data(),
            }
        )

    # I'd rather opt out early here, so we don't build unnecessarily, however the error feedback is
    # misleading and not very helpful in that case.
    #
    # @classmethod
    # def opt_out(cls, tgt: Target) -> bool:
    #     return not tgt[PythonRepositoriesField].value

def twine_upload_args(
    twine_subsystem: TwineSubsystem,
    config_files: ConfigFiles,
    repo: str,
    dists: tuple[str, ...],
    ca_cert: Snapshot | None,
) -> tuple[str, ...]:
    args = ["upload", "--non-interactive"]

    if ca_cert and ca_cert.files:
        args.append(f"--cert={ca_cert.files[0]}")

    if config_files.snapshot.files:
        args.append(f"--config-file={config_files.snapshot.files[0]}")

    args.extend(twine_subsystem.args)

    if repo.startswith("@"):
        # Named repository from the config file.
        args.append(f"--repository={repo[1:]}")
    else:
        args.append(f"--repository-url={repo}")

    args.extend(dists)
    return tuple(args)

def twine_env_suffix(repo: str) -> str:
    return f"_{repo[1:]}".replace("-", "_").upper() if repo.startswith("@") else ""

def twine_env_request(repo: str) -> EnvironmentVarsRequest:
    suffix = twine_env_suffix(repo)
    env_vars = [
        "TWINE_USERNAME",
        "TWINE_PASSWORD",
        "TWINE_REPOSITORY_URL",
    ]
    req = EnvironmentVarsRequest(env_vars + [f"{var}{suffix}" for var in env_vars])
    return req

def twine_env(env: EnvironmentVars, repo: str) -> EnvironmentVars:
    suffix = twine_env_suffix(repo)
    return EnvironmentVars(
        {key.rsplit(suffix, maxsplit=1)[0] if suffix else key: value for key, value in env.items()}
    )

@rule
async def twine_upload(
    request: PublishPythonPackageRequest,
    twine_subsystem: TwineSubsystem,
    global_options: GlobalOptions,
) -> PublishProcesses:
    dists = tuple(
        artifact.relpath
        for pkg in request.packages
        for artifact in pkg.artifacts
        if artifact.relpath
    )

    if twine_subsystem.skip or not dists:
        return PublishProcesses()

    # Too verbose to provide feedback as to why some packages were skipped?
    skip = None
    if request.field_set.skip_twine.value:
        skip = f"(by `{request.field_set.skip_twine.alias}` on {request.field_set.address})"
    elif not request.field_set.repositories.value:
        # I'd rather have used the opt_out mechanism on the field set, but that gives no hint as to
        # why the target was not applicable..
        skip = f"(no `{request.field_set.repositories.alias}` specified for {request.field_set.address})"

    if skip:
        return PublishProcesses(
            [
                PublishPackages(
                    names=dists,
                    description=skip,
                ),
            ]
        )

    twine_pex, packages_digest, config_files = await MultiGet(
        Get(VenvPex, PexRequest, twine_subsystem.to_pex_request()),
        Get(Digest, MergeDigests(pkg.digest for pkg in request.packages)),
        Get(ConfigFiles, ConfigFilesRequest, twine_subsystem.config_request()),
    )

    ca_cert_request = twine_subsystem.ca_certs_digest_request(global_options.ca_certs_path)
    ca_cert = await Get(Snapshot, CreateDigest, ca_cert_request) if ca_cert_request else None
    ca_cert_digest = (ca_cert.digest,) if ca_cert else ()

    input_digest = await Get(
        Digest, MergeDigests((packages_digest, config_files.snapshot.digest, *ca_cert_digest))
    )
    pex_proc_requests = []
    twine_envs = await MultiGet(
        Get(EnvironmentVars, EnvironmentVarsRequest, twine_env_request(repo))
        for repo in request.field_set.repositories.value
    )

    for repo, env in zip(request.field_set.repositories.value, twine_envs):
        pex_proc_requests.append(
            VenvPexProcess(
                twine_pex,
                argv=twine_upload_args(twine_subsystem, config_files, repo, dists, ca_cert),
                input_digest=input_digest,
                extra_env=twine_env(env, repo),
                description=repo,
            )
        )

    processes = await MultiGet(
        Get(Process, VenvPexProcess, request) for request in pex_proc_requests
    )

    return PublishProcesses(
        PublishPackages(
            names=dists,
            process=InteractiveProcess.from_process(process),
            description=process.description,
            data=PublishOutputData({"repository": process.description}),
        )
        for process in processes
    )

def rules():
    return (
        *collect_rules(),
        *PublishPythonPackageFieldSet.rules(),
        PythonDistribution.register_plugin_field(PythonRepositoriesField),
        PythonDistribution.register_plugin_field(SkipTwineUploadField),
    )


#!/usr/bin/python3
'''Post the compositions in a given directory filtered or not by a basename
   now one ehr per composition
'''
import json
import logging
import requests
from url_normalize import url_normalize
import sys
import argparse
import os
from typing import Any,Callable
import re
from json_tools import diff
import collections
import uuid

def compare(firstjson:json,secondjson:json)->None:
	'''
	compare the given jsons
	'''
	one=flatten(firstjson)
	two=flatten(secondjson)
	return json.dumps((diff(one,two)),indent=4)

def change_naming(myjson:json)->json:
	'''change naming convention on the json'''
	return change_dict_naming_convention(myjson,convertcase)

def flatten(d:dict, parent_key:str='', sep:str='_')->dict:
	items = []
	for k, v in d.items():
		new_key = parent_key + sep + k if parent_key else k
		if isinstance(v, collections.abc.MutableMapping):
				items.extend(flatten(v, new_key, sep=sep).items())
		else:
				items.append((new_key, v))
	return dict(items)

def change_dict_naming_convention(d:Any, convert_function:Callable[[str],str])->dict:
	"""
	Convert a nested dictionary from one convention to another.
	Args:
		d (dict): dictionary (nested or not) to be converted.
		convert_function (func): function that takes the string in one convention and returns it in the other one.
	Returns:
			Dictionary with the new keys.
	"""
	if not isinstance(d,dict):
			return d
	new = {}
	for k, v in d.items():
		new_v = v
		if isinstance(v, dict):
				new_v = change_dict_naming_convention(v, convert_function)
		elif isinstance(v, list):
				new_v = list()
				for x in v:
						new_v.append(change_dict_naming_convention(x, convert_function))
		new[convert_function(k)] = new_v
	return new

def convertcase(name:str)->str:
	s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
	return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()

def analyze_comparison(comparison_results:list)->int:
	ndifferences=0
	for l in comparison_results:
		if "add" in l:
			if("_uid" in l['add']): #ignore if it is _uid
				continue
			else:
				ndifferences+=1
				logging.debug(f"difference add:{l['add']} value={l['value']}")
		elif "remove" in l:
			ndifferences+=1
			logging.debug(f"difference remove:{l['remove']} value={l['value']}")

		elif "replace" in l:
			if(l['replace'].endswith("time")):
				if(l['value'][:18]==l['prev'][:18]):
					continue
				ndifferences+=1
				logging.debug(f"difference replace:{l['replace']} value={l['value']} prev={l['prev']}")
			elif(l['value'].startswith('P') and l['value'].endswith('D')):
				continue
			else:
				ndifferences+=1
				logging.debug(f"difference replace:{l['replace']} value={l['value']} prev={l['prev']}")
	return ndifferences

def create_ehr(client,EHR_SERVER_BASE_URL, auth,patientid):
	logging.debug('----POST EHR----')
	body1='''
	{
	  "_type" : "EHR_STATUS",
	  "name" : {
	    "_type" : "DV_TEXT",
	    "value" : "EHR Status"
	  },
	  "subject" : {
	    "_type" : "PARTY_SELF",
	    "external_ref" : {
	      "_type" : "PARTY_REF",
	      "namespace" : "BBMRI",
	      "type" : "PERSON",
	      "id" : {
	        "_type" : "GENERIC_ID",
	'''
	body2=f'	"value" : "{patientid}",'
	body3='''
	        "scheme" : "BBMRI"
	      }
	    }
	  },
	  "archetype_node_id" : "openEHR-EHR-EHR_STATUS.generic.v1",
	  "is_modifiable" : true,
	  "is_queryable" : true
	}
	'''
	body=body1+body2+body3
	logging.debug(f'body={body}')
#	sys.exit(0)
	ehrs = client.post(EHR_SERVER_BASE_URL + 'ehr', \
	                   params={},headers={'Authorization':auth,'Content-Type':'application/JSON','Accept': 'application/json','Prefer': 'return={representation|minimal}'},\
	                   data=body)

	print(f'create ehr status_code={ehrs.status_code}')
	logging.info(f'create ehr: status_code={ehrs.status_code}')
	logging.debug(f'ehr url={ehrs.url}')
	logging.debug(f'ehrs.headers={ehrs.headers}')
	logging.debug(f'ehrs.text={ehrs.text}')
	logging.debug(f'ehrs.json={ehrs.json}')

	if(ehrs.status_code==409 and 'Specified party has already an EHR set' in json.loads(ehrs.text)['message']):
		#get ehr summary by subject_id , subject_namespace
		payload = {'subject_id':patientid,'subject_namespace':'BBMRI'}
		ehrs = client.get(EHR_SERVER_BASE_URL + 'ehr',  params=payload,headers={'Authorization':auth,'Content-Type':'application/JSON','Accept': 'application/json'})
		print('ehr already existent')
		logging.info('ehr already existent')
		logging.debug('----GET EHR----')
		print(f'get ehr: status_code={ehrs.status_code}')
		logging.info(f'get ehr: status_code={ehrs.status_code}')
		logging.debug(f'ehr url={ehrs.url}')
		logging.debug(f'ehr.headers={ehrs.headers}')
		logging.debug(f'ehr.text={ehrs.text}')
		logging.debug(f'ehr.json={ehrs.json}')

		ehrid=json.loads(ehrs.text)["ehr_id"]["value"]
		print(f'Patient {patientid}: retrieved ehrid={ehrid}')
		logging.info(f'Patient {patientid}: retrieved ehrid={ehrid}')
		return ehrid

#	print(f'ehrheaders={ehrs.headers}')
	urlehrstring = ehrs.headers['Location']
	ehridstring = "{"+urlehrstring.split("v1/ehr/",2)[2]
	ehrid=uuid.UUID(ehridstring)
	print(f'Patient {patientid}: ehrid={str(ehrid)}')
	logging.info(f'Patient {patientid}: ehrid={str(ehrid)}')
	return ehrid

def main():
	print('COMPOSITIONS UPLOADER')
	parser = argparse.ArgumentParser()
	parser.add_argument('--loglevel',help='the logging level:DEBUG,INFO,WARNING,ERROR or CRITICAL',default='WARNING')
	parser.add_argument('--inputdir',help='dir containing the compositions',default='RESULTS')
	parser.add_argument('--basename',help='basename to filter compositions')
	parser.add_argument('--templatename',help='template to use when posting',default='crc_cohort')
	parser.add_argument('--check',action='store_true', help='check the missing leafs for leafs that should be there but are not')
	args=parser.parse_args()

	loglevel=getattr(logging, args.loglevel.upper(),logging.WARNING)
	if not isinstance(loglevel, int):
		raise ValueError('Invalid log level: %s' % loglevel)
	logging.basicConfig(filename='./CompositionUploader.log',filemode='w',level=loglevel)

	inputdir=args.inputdir
	print(f'inputdir given: {inputdir}')
	logging.info(f'inputdir given: {inputdir}')

	if not os.path.exists(inputdir):
		print(f'directory {inputdir} does not exist')
		logging.error(f'directory {inputdir} does not exist')
		sys.exit(1)

	basename=args.basename

	if(basename):
		logging.info(f'basename given: {basename}')
		print(f'basename given: {basename}')

	check=False
	if args.check:
		check=True
		print ('Check is set to true')
		logging.info('Check is set to true')

	#get the list of files
	filelist=[]
	if basename:
		for file in os.listdir(inputdir):
			if file.startswith(basename) and file.endswith(".json"):
				logging.debug(f'file added {os.path.join(inputdir, file)}')
				filelist.append(file)
	else:
		for file in os.listdir(inputdir):
			if file.endswith(".json"):
				logging.debug(f'file added {os.path.join(inputdir, file)}')
				filelist.append(file)

#	Now sort the list
	filelist.sort(key=lambda a: int(a.split('_')[1]))
	for i,f in enumerate(filelist):
		logging.info(f'file {i+1} = {f}')

# 	Initialize the connection to ehrbase
	EHR_SERVER_BASE_URL = 'http://localhost:8080/ehrbase/rest/openehr/v1/'
	EHR_SERVER_BASE_URL_FLAT = 'http://localhost:8080/ehrbase/rest/ecis/v1/composition/'
	client = requests.Session()
	client.auth = ('ehrbase-user','SuperSecretPassword')
	auth="Basic ZWhyYmFzZS11c2VyOlN1cGVyU2VjcmV0UGFzc3dvcmQ="

	nfiles=len(filelist)
	print(f'{nfiles} to insert')
	logging.info(f'{nfiles} to insert')

	#check if the template is already in the db
	templatename=args.templatename

	myurl=url_normalize(EHR_SERVER_BASE_URL  + 'definition/template/adl1.4')
	response = client.get(myurl,params={'format': 'JSON'},headers={'Authorization':auth,'Content-Type':'application/JSON'})
	templates=[a["template_id"] for a in json.loads(response.text)]
	if(templatename not in templates):
		print(f'Missing template {templatename}')
		logging.error(f'Missing template {templatename}')
		sys.exit(1)

#   loop over files and upload the compositions
	myurl=url_normalize(EHR_SERVER_BASE_URL_FLAT)
	compinserted=0
	compok=0
	for i,file in enumerate(filelist):
		print(f'********FILE {i+1}/{nfiles}  {file}********')
		logging.info(f'********FILE {i+1}/{nfiles}  {file}********')
		filename=os.path.join(inputdir, file)
		with open(filename) as json_file:
			compositionjson = json.load(json_file)
		patientid='Patient'+compositionjson[templatename.lower()+'/context/case_identification/patient_pseudonym']
		print(f'Patientid={patientid}')
		logging.info(f'Patientid={patientid}')
#		create ehr
		ehrid=create_ehr(client,EHR_SERVER_BASE_URL, auth,patientid)
# 		post composition
		compositionjson=json.dumps(compositionjson)
		response = client.post(myurl,
					   params={'ehrId':str(ehrid),'templateId':templatename,'format':'FLAT'}, \
					   headers={'Authorization':auth,'Content-Type':'application/json','Prefer':'return=representation'}, \
					   data=compositionjson  \
					  )
		if(response.status_code != 200 and response.status_code != 201):
			print(f"Couldn't post the composition. Error={response.status_code}")
			print(f'response.text {response.text}')
			logging.info(f"Couldn't post the composition. Error={response.status_code}")
			logging.info(f'response.headers {response.headers}')
			logging.info(f'response.text {response.text}')
		else:
			compinserted+=1
			print(f'Composition inserted')
			compositionUid=json.loads(response.text)["compositionUid"]
			print(f'compositionUid={compositionUid}')
			logging.info(f'compositionUid={compositionUid}')
			if(check):
				print(f'checking...')
				logging.info(f'checking...')
				#get composition created and compare with the one posted
				myurlu=url_normalize(EHR_SERVER_BASE_URL_FLAT+compositionUid)
				response = client.get(myurlu, \
					   params={'ehrId':str(ehrid),'templateId':templatename,'format':'FLAT'}, \
					   headers={'Authorization':auth,'Content-Type':'application/json'}, \
					  )
				if(response.status_code != 200 and response.status_code != 201):
					print(f"Couldn't retrieve the composition. Error{response.status_code}")
					logging.info(f"Couldn't retrieve the composition. Error{response.status_code}")
					logging.info(f'response.headers {response.headers}')
					logging.info(f'response.text {response.text}')
				else:
					origjson=json.loads(compositionjson)
					retrievedjson=json.loads(response.text)["composition"]
					origchanged=change_naming(origjson)
					retrchanged=change_naming(retrievedjson)

					comparison_results=compare(origchanged,retrchanged)
					ndiff=analyze_comparison(comparison_results)
					if(ndiff>0):
						print('original and retrieved json differ')
						logging.info('original and retrieved json differ')
						logging.debug(f'comparison_results:')
						logging.debug(comparison_results)
					else:
						print('original and retrieved json do not differ')
						logging.info('original and retrieved json do not differ')
						compok+=1

	print(f'{compinserted}/{nfiles} compositions inserted successfully')
	logging.info(f'{compinserted}/{nfiles} compositions inserted successfully')
	print(f'{nfiles-compinserted}/{nfiles} compositions with errors')
	if(check):
		print(f'{compok}/{compinserted} checked successfully')
		logging.info(f'{compok}/{compinserted} checked successfully')
		print(f'{compinserted-compok}/{compinserted} checked unsuccessfully')
		logging.info(f'{compinserted-compok}/{compinserted} checked unsuccessfully')

if __name__ == '__main__':
	main()


"""
IMU 6-DOF

    Acceleration
 - imu_accel_x
 - imu_accel_y
 - imu_accel_z

    Angular speed
 - imu_gyro_x
 - imu_gyro_y
 - imu_gyro_z

"""

import numpy as np
from numpy.linalg import inv
from scipy.spatial.transform import Rotation as rot
"""
X: states:
    - pitch
    - roll
    - yaw (not used)
    - bias angular rate pitch
    - bias angular rate roll
    - bias angular rate yaw

Note: In order to compute yaw, an additional sensor like a magnetometer is required.

u:    inputs
    - Euler angles
"""

class INS_filter:

    def __init__(self, data):
        dt = 1e-2

        self.X = np.zeros([6,1]) # error in Euler angles, gyro biases
        self.X[0] = -np.arctan2(data["imu_accel_y"], np.sqrt(data["imu_accel_y"]**2+data["imu_accel_z"]**2))
        self.X[1] = np.arctan2(data["imu_accel_x"], np.sqrt(data["imu_accel_x"]**2+data["imu_accel_z"]**2))

        self.Cnb = rot.from_euler("xyz", self.X[0:3].transpose()).as_matrix()[0]

        self.P = np.identity(6)

        # Process model
        self.F = np.identity(6)
        self.F[0:3,3:6] = -dt*self.Cnb

        # Control action model
        self.B = np.zeros([6,3])
        self.B[0:3, 0:3] = np.identity(3)*dt

        # Observation matrix
        self.H = np.zeros([3,6])
        self.H[0:3, 0:3] = np.identity(3)

        # Process noise matrix
        self.gyro_psd = 3.5e-4
        self.gyro_bias_psd = 1e-7

        self.Q = np.zeros([6,6])
        self.updateQ(dt)

        # Sensor noise matrix (accel)
        self.R = np.zeros([3,3])
        self.R[0][0] = 5
        self.R[1][1] = 5
        self.R[2][2] = 5

    def updateQ(self, dt):
        self.Q[0:3, 0:3] = np.identity(3)*self.gyro_psd*dt
        self.Q[3:6, 3:6] = np.identity(3) * self.gyro_bias_psd * dt

    def predict(self, w, dt): # w is the angular rate vector
        self.Cnb = rot.from_euler("xyz", self.X[0:3].transpose()).as_matrix()[0]
        u = w.transpose()

        self.updateQ(dt)

        #update dt
        self.F[0:3,3:6] = -dt*self.Cnb
        self.B[0:3, 0:3] = dt*self.Cnb

        # build pseudo control var u
        self.X = self.F@self.X + self.B@u
        self.P = self.F@self.P@self.F.transpose() + self.Q

    def updateAttitude(self, a_bib):

        z = self.getEulerAnglesFromAccel(a_bib.transpose())
        y = z - self.H@self.X
        S = self.H@self.P@self.H.transpose() + self.R
        K = (self.P@self.H.transpose())@inv(S)
        self.X = self.X+K@y

        I = np.identity(6)
        self.P = (I-K@self.H)@self.P

    def getEulerAnglesFromAccel(self, a_bib):
        eul_nb = np.zeros([3,1])
        eul_nb[0] = -np.arctan2(a_bib[1], np.sqrt(a_bib[1]**2+a_bib[2]**2))
        eul_nb[1] = np.arctan2(a_bib[0], np.sqrt(a_bib[0]**2+a_bib[2]**2))

        return eul_nb

    def get_states(self):
        return {"roll": np.asscalar(self.X[0])*180/np.pi,
                "pitch": np.asscalar(self.X[1])*180/np.pi,
                "yaw": np.asscalar(self.X[2])*180/np.pi,
                "gyro_bias_roll": np.asscalar(self.X[3])*180/np.pi,
                "gyro_bias_pitch": np.asscalar(self.X[4])*180/np.pi}

def run_filter_simulation(df):
    import time

    start = time.time()

    init = False
    kf_ins_res = {"roll": [], "pitch":[], "yaw":[], "gyro_bias_roll":[], "gyro_bias_pitch":[]}
    last_time = 0
    for index, row in df.iterrows():
        if not init:
            ins_kf = INS_filter(row)
            init = True
            last_time = row["time"] - 1e-2

        # Note: in a real-time system, the prediction step should run at each iteration
        # This hack is only used here for simulation purposes
        if row["imu_new_data"]:
            dt = row["time"] - last_time
            ins_kf.predict(np.matrix([row["imu_gyro_x"], row["imu_gyro_y"], row["imu_gyro_z"]]), dt)
            last_time = row["time"]

        if row["imu_new_data"]:
            ins_kf.updateAttitude(np.matrix([row["imu_accel_x"], row["imu_accel_y"], row["imu_accel_z"]]))

        res = ins_kf.get_states()
        kf_ins_res["roll"].append(res["roll"])
        kf_ins_res["pitch"].append(res["pitch"])
        kf_ins_res["yaw"].append(res["yaw"])
        kf_ins_res["gyro_bias_roll"].append(res["gyro_bias_roll"])
        kf_ins_res["gyro_bias_pitch"].append(res["gyro_bias_pitch"])

    end = time.time()
    print(f"Execution time: {end - start} s")

    import matplotlib.pyplot as plt
    f, ax = plt.subplots(4, 1)

    ax[0].set_title("Roll")
    ax[0].plot(df["time"], kf_ins_res["roll"], label="roll")

    ax[1].set_title("Pitch")
    ax[1].plot(df["time"], kf_ins_res["pitch"], label="pitch")

    ax[2].set_title("Gyro bias roll")
    ax[2].plot(df["time"], kf_ins_res["gyro_bias_roll"], label="gyro_bias_roll")

    ax[3].set_title("Gyro bias pitch")
    ax[3].plot(df["time"], kf_ins_res["gyro_bias_pitch"], label="gyro_bias_pitch")

    plt.subplots_adjust(hspace=0.4)
    f.canvas.set_window_title('Kalman Filter INS')
    f.suptitle("Kalman Filter INS")
    # f.legend()
    plt.show()

if __name__ == "__main__":
    import pandas as pd
    data = pd.read_csv("gns_ins_data2.csv")
    run_filter_simulation(data)


import os
import pytest
import pathlib
import numpy as np
import pandas as pd
from math import isclose

from cytominer_eval.operations import mp_value

from cytominer_eval.utils.mpvalue_utils import (
    calculate_mp_value,
    calculate_mahalanobis,
)

# Load CRISPR dataset
example_file = "SQ00014610_normalized_feature_select.csv.gz"
example_file = pathlib.Path(
    "{file}/../../example_data/gene/{eg}".format(
        file=os.path.dirname(__file__), eg=example_file
    )
)

df = pd.read_csv(example_file)

meta_features = [
    x for x in df.columns if (x.startswith("Metadata_") or x.startswith("Image_"))
]
features = df.drop(meta_features, axis="columns").columns.tolist()

control_perts = ["Luc-2", "LacZ-2", "LacZ-3"]
replicate_id = "Metadata_pert_name"

def test_calculate_mahalanobis():
    sub_df = df[(df.Metadata_WellRow == "A") & (df.Metadata_pert_name == "EMPTY")][
        features
    ]
    control_df = df[df[replicate_id].isin(control_perts)][features]

    maha = calculate_mahalanobis(pert_df=sub_df, control_df=control_df)

    assert isinstance(maha, float)
    # The following value is empirically determined
    # and not theoretically justified but avoids unwanted
    # changes in the implementation of the Mahalanobis distance
    assert isclose(maha, 3.62523778789, abs_tol=1e-09)

    maha = calculate_mahalanobis(pert_df=control_df, control_df=control_df)

    # Distance to itself should be approximately zero
    assert isclose(maha, 0, abs_tol=1e-05)

def test_calculate_mp_value():
    # The mp-values are empirical p-values
    # so they range from 0 to 1, with low values
    # showing a difference to the control condition.

    sub_df = df[(df.Metadata_WellRow == "A") & (df.Metadata_pert_name == "EMPTY")][
        features
    ]
    control_df = df[df[replicate_id].isin(control_perts)][features]

    # Avoid fluctuations in permutations
    np.random.seed(2020)
    result = calculate_mp_value(pert_df=sub_df, control_df=control_df)

    assert isinstance(result, float)
    assert result > 0
    assert result < 1

    # Distance to itself should be approximately zero
    # So mp-value should be 1
    result = calculate_mp_value(
        pert_df=control_df, control_df=control_df, params={"nb_permutations": 2000}
    )

    assert isclose(result, 1, abs_tol=1e-02)

    with pytest.raises(AssertionError) as ae:
        result = calculate_mp_value(
            pert_df=control_df, control_df=control_df, params={"not_a_parameter": 2000}
        )
    assert "Unknown parameters provided. Only" in str(ae.value)

def test_mp_value():
    result = mp_value(
        df=df,
        control_perts=control_perts,
        replicate_id=replicate_id,
        features=features,
    )

    assert "mp_value" in result.columns
    assert all(result.mp_value <= 1)
    assert all(result.mp_value >= 0)
    assert len(np.unique(df[replicate_id])) == len(result)

    with pytest.raises(AssertionError) as ae:
        result = mp_value(
            df=df,
            control_perts=control_perts,
            replicate_id=replicate_id,
            features=features,
            params={"not_a_parameter": 2000},
        )
    assert "Unknown parameters provided. Only" in str(ae.value)