import os
import sys
# import time
# import configparser
import sqlite3
import hashlib
import logging
from optparse import OptionParser
from sync import scan_dir
from sync import parse_configs
from sync import db_create_tables
from sync import db_remove_deleted

logging.basicConfig(level=logging.INFO, format=' %(asctime)s - %(levelname)s - %(message)s')

# Global variable
config = {}
conn = None  # DB handle

# Global constant
CONFIG_FILE = 'data' + os.sep + 'sync.cfg'

# parms
parm = {'delete': False, 'scan': False}

class File(object):
    def __init__(self, file_name, file_md5, file_mtime, file_size, dir_name, root_dir, rel_path):
        self.file_name = file_name
        self.file_md5 = file_md5
        self.file_mtime = file_mtime
        self.file_size = file_size
        self.dir_name = dir_name
        self.root_dir = root_dir
        self.rel_path = rel_path

def parse_options():
    # Use optparse to get parms
    usage = "usage: %prog [options] scope_dir keep_dir"
    parser = OptionParser(usage=usage)
    parser.add_option("-d", "--delete", dest="delete", action="store_true", default=False,
                      help="Delete the duplicate files that are not in the keep directory.")
    parser.add_option("-s", "--scan", dest="scan", action="store_true", default=False,
                      help="Scan the scope directory. The default is to use the existing DB.")
    (options, args) = parser.parse_args()
    return options, args  # options: copy, rejects; args: source_dir

def db_get_name(dir_name):
    """
    Build a hash value from the concatenation of source_dir
    :param dir_name: valeur à hasher pour faire le db_name
    """
    m = hashlib.md5()
    m.update(dir_name.encode('utf-8'))
    db_name = m.hexdigest() + ".db"
    return db_name

def list_dup(db_h, root_dir):

    sel_md5 = \
        '''
        select file_md5, count(*)
          from file
         where file_md5 in (select file_md5 from file where dir_name = ?)
         group by file_md5
        having count(*) > 1
        '''
    sel_dup = \
        '''
        select dir_name, file_name, file_size, file_mtime
          from file
         where file_md5 = ?
       '''

    try:
        cur_md5 = db_h.cursor()
        cur_dup = db_h.cursor()
        for row_md5 in cur_md5.execute(sel_md5, [root_dir]):
            file_md5 = row_md5[0]
            logging.info("Possible duplicates: %s" % file_md5)
            for row_dup in cur_dup.execute(sel_dup, [file_md5]):
                dir_name = row_dup[0]
                file_name = row_dup[1]
                file_size = row_dup[2]
                file_mtime = row_dup[3]
                logging.info("    Fichier......: %s" % dir_name + os.sep + file_name)
                logging.info("        Size.....: %i" % file_size)
                logging.info("        MTime....: %s" % file_mtime)
            logging.info(" ")
    except sqlite3.Error as x:
        logging.error("SQL Error: \n" + str(x))

def delete_dup(db_h, keep_dir):
    sel_md5 = \
        '''
        select file_md5, count(*)
          from file
         where file_md5 in (select file_md5 from file where dir_name = ?)
         group by file_md5
        having count(*) > 1
        '''
    sel_dup = \
        '''
        select dir_name, file_name
          from file
         where file_md5 = ?
           and dir_name <> ?
        '''
    del_dup = \
        '''
        delete from file
         where file_md5 = ?
           and dir_name <> ?
       '''

    try:
        cur_md5 = db_h.cursor()
        cur_dup = db_h.cursor()
        for row_md5 in cur_md5.execute(sel_md5, [keep_dir]):
            file_md5 = row_md5[0]
            logging.info("Possible duplicates: %s" % file_md5)
            for row_dup in cur_dup.execute(sel_dup, [file_md5, keep_dir]):
                dir_name = row_dup[0]
                file_name = row_dup[1]
                logging.info("    Ce fichier sera effacé: %s" % dir_name + os.sep + file_name)
                if os.path.isfile(keep_dir + os.sep + file_name):
                    os.remove(dir_name + os.sep + file_name)
                else:
                    logging.error("    Ce fichier ne peut pas être effacé. " +
                                  " Il n'existe pas dans le répertoire à conserver.")
            logging.info(" ")
            cur_dup.execute(del_dup, [file_md5, keep_dir])

    except sqlite3.Error as x:
        logging.error("SQL Error: \n" + str(x))

def main():
    global parm, config, conn
    logging.info('Début du programme ' + sys.argv[0])

    # Get parameters and validate them
    (options, args) = parse_options()
    parm["delete"] = options.delete
    parm["scan"] = options.scan
    if len(args) < 2:
        logging.error("Ce programme a besoin de deux arguments, le dossier source et le dossier à protéger.")
        return 8
    source_dir = args[0]
    protec_dir = args[1]
    if source_dir.endswith(os.sep):
        source_dir = source_dir[0:-1]
    if protec_dir.endswith(os.sep):
        protec_dir = protec_dir[0:-1]

    logging.info("Paramètres:")
    logging.info("    Dossier scope................................: %s" % source_dir)
    logging.info("    Dossier à protéger...........................: %s" % protec_dir)
    if not os.path.isdir(source_dir):
        logging.error("Le dossier source n'existe pas.")
        return 8
    if not os.path.isdir(protec_dir):
        logging.error("Le dossier à protéger n'existe pas.")
        return 8

    if parm["delete"]:
        logging.info("    Option de suppression........................: Oui")
    else:
        logging.info("    Option de suppression........................: Non")
    if parm["scan"]:
        logging.info("    Option de population de la BD................: Oui")
    else:
        logging.info("    Option de population de la BD................: Non")
    logging.info(" ")

    # Read config file
    config = parse_configs()
    logging.info("Configurations:")
    logging.info("    Fichier de configuration.....................: " + CONFIG_FILE)
    for ext in config['accept_list']:
        logging.info("    Accepted extension...........................: %s" % ext)
    for ext in config['reject_list']:
        logging.info("    Rejected extension...........................: %s" % ext)

    db_name = db_get_name(source_dir)
    db_path = source_dir + os.sep + db_name
    logging.info("    Database file................................: " + db_name)
    logging.info(" ")

    conn = sqlite3.connect(db_path)
    db_create_tables(conn)            # Create the DB objects
    if parm['scan']:
        scan_dir(conn, source_dir)    # Create inventory of the files in the source directory structure
    # Pu sûr que c'est une bonne idée...
    # if parm['delete']:
    #    delete_dup(conn, protec_dir)
    #    db_remove_deleted(conn)       # Remove deleted files from db
    # else:
    list_dup(conn, source_dir)

    conn.commit()
    conn.close()

    logging.info('Fin du programme ' + sys.argv[0])
    return 0

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


import json
import logging
import random
import math

logging.basicConfig(level=logging.WARNING)

#logging.getLogger(__name__).setLevel(logging.INFO)
logger = logging.getLogger(__name__)
logger.setLevel(logging.ERROR)
#logger.setLevel(logging.WARNING)

def worth_goalie(p):
    worth = 0.0
    for pi in p:
        if 'worth' not in pi:
            continue
        if pi['pos'] in ['GK', 'Goalkeeper']:
            if pi['worth'] != None:
                worth = pi['worth']
            else:
                worth = 0.1
    return worth

def worth_defense(p):
    worth = 0.0
    for pi in p:
        if 'worth' not in pi:
            continue
        if pi['pos'] in ['CB', 'LB', 'RB']:
            if pi['worth'] != None:
                worth += pi['worth']
    return worth

def worth_midfield(p):
    worth = 0.0
    for pi in p:
        if 'worth' not in pi:
            continue
        if pi['pos'] in ['RM', 'LM', 'CM', 'AM', 'LW', 'RW', 'DM']:
            if pi['worth'] != None:
                worth += pi['worth']
    return worth

def worth_attack(p):
    worth = 0.0
    for pi in p:
        if 'worth' not in pi:
            continue
        if pi['pos'] in ['RF', 'LF', 'CF']:
            if pi['worth'] != None:
                worth += pi['worth']
    return worth

def worth_team(p):
    worth = 0
    for pi in p:
        if 'worth' not in pi:
            continue
        if pi['worth'] != None:
            worth += pi['worth']
    return worth

def predict(w, c1, c2, c3, c4, c5, c6):
    zone = 'midfield'
    possession = 'home'
    timer = 90

    score_home = 0
    score_away = 0

    norm1 = w['home']['g'] + w['home']['d'] + w['home']['m'] + w['home']['a']
    norm2 = w['away']['g'] + w['away']['d'] + w['away']['m'] + w['away']['a']
    norm = max(norm1, norm2)

    # home zone home goal
    hgk = w['home']['g']
    hgk /= norm
    # home zone home goal to zone home defense
    hgd = w['home']['d'] + c4 * w['home']['m']
    hgd /= norm
    # home zone home mid to zone away defense
    hmd = w['home']['a'] + w['home']['m'] + c5 * w['home']['d']
    hmd /= norm
    # home zone away defense to zone away goal
    hdg = w['home']['a'] + w['home']['m']
    hdg /= norm
    # home zone away goal
    hgs = w['home']['a'] + c6 * w['home']['m']
    hgs /= norm

    # away zone away goal
    agk = w['away']['g']
    agk /= norm
    # away zone away goal to zone away defense
    agd = w['away']['d'] + c4 * w['away']['m']
    agd /= norm
    # away zone away mid to zone home defense
    amd = w['away']['a'] + w['away']['m'] + c5 * w['away']['d']
    amd /= norm
    # away zone home defense to zone home goal
    adg = w['away']['a'] + w['away']['m']
    adg /= norm
    # away zone home goal
    ags = w['away']['a'] + c6 * w['away']['m']
    ags /= norm

    while timer > 0:
        timer -= 2
#        r1 = random.uniform(-0.5, 0.5)
#        r2 = random.uniform(-0.5, 0.5)
        r1 = random.gauss(0.6, 0.75)
        r2 = random.gauss(0.6, 0.75)

        if zone == 'midfield':
            if possession == 'home':
                m = r1 * hmd - r2 * amd
                logger.debug('h c1 {}'.format(m))
                if m > c1:
                    zone = 'defense away'
                else:
                    zone = 'midfield'
                    possession = 'away'
            elif possession == 'away':
                m = r1 * amd - r2 * hmd
                logger.debug('a c1 {}'.format(m))
                if m > c1:
                    zone = 'defense home'
                else:
                    zone = 'midfield'
                    possession = 'home'
        elif zone == 'defense away':
            if possession == 'home':
                m = r1 * hdg - r2 * adg
                logger.debug('h c2 {}'.format(m))
                if m > c2:
                    zone = 'goal away'
                else:
                    zone = 'midfield'
                    possession = 'away'
        elif zone == 'goal away':
            if possession == 'home':
                m = r1 * hgs - r2 * agk
                logger.debug('h c3 {}'.format(m))
                if m > c3:
                    score_home += 1
                zone = 'midfield'
                possession = 'away'
        elif zone == 'defense home':
            if possession == 'away':
                m = r1 * adg - r2 * hdg
                logger.debug('a c2 {}'.format(m))
                if m > c2:
                    zone = 'goal home'
                else:
                    zone = 'midfield'
                    possession = 'home'
        elif zone == 'goal home':
            if possession == 'away':
                m = r1 * ags - r2 * hgk
                logger.debug('a c3 {}'.format(m))
                if m > c3:
                    score_away += 1
                zone = 'midfield'
                possession = 'home'

    w['home']['pred score'] = score_home
    w['away']['pred score'] = score_away

    logger.info('Pred Score Home: {} Away: {}'.format(score_home, score_away))
    logger.info('Real Score Home: {} Away: {}'.format(w['home']['real score'], w['away']['real score']))

    return w

def train_model(data, c1, c2, c3, c4, c5, c6):
    quality = 0
    win = 0
    total = 0

    for ctr,d in enumerate(data):
        if ctr == 60:
            break

        w = {}
        for t in ['home', 'away']:
            w[t] = {}
            w[t]['g'] = worth_goalie(d[t]['start'])
            w[t]['d'] = worth_defense(d[t]['start'])
            w[t]['m'] = worth_midfield(d[t]['start'])
            w[t]['a']  = worth_attack(d[t]['start'])
            w[t]['name'] = d[t]['team']
            w[t]['real score'] = d[t]['score']
            w[t]['pred score'] = None

            s = 0
            if w[t]['g'] != None:
                s += w[t]['g']
            if w[t]['d'] != None:
                s += w[t]['d']
            if w[t]['m'] != None:
                s += w[t]['m']
            if w[t]['a'] != None:
                s += w[t]['a']
            logger.debug(d[t]['start'])
            if s < worth_team(d[t]['start']) - 0.001:
                logger.warning('Not all positions used for {}'.format(d[t]['team']))
                logger.info(s)
                logger.info(worth_team(d[t]['start']))
            else:
                logger.warning('{} {} {}'.format(d[t]['team'], 'Goalie', w[t]['g']))
                logger.warning('{} {} {}'.format(d[t]['team'], 'Defense', w[t]['d']))
                logger.warning('{} {} {}'.format(d[t]['team'], 'Midfield', w[t]['m']))
                logger.warning('{} {} {}'.format(d[t]['team'], 'Attack', w[t]['a']))

        sh = 0
        sa = 0
        rep = 100
        for i in range(rep):
            w = predict(w, c1, c2, c3, c4, c5, c6)
            sh += w['home']['pred score']
            sa += w['away']['pred score']

        w['home']['pred score'] = round(sh / rep, 2)
        w['away']['pred score'] = round(sa / rep, 2)
#        print('{} vs {}'.format(d['home']['team'], d['away']['team']))
#        print('Pred.', w['home']['pred score'], w['away']['pred score'])
#        print('Real.', w['home']['real score'], w['away']['real score'])

        quality += abs(w['home']['pred score'] - w['home']['real score']) + \
                abs(w['away']['pred score'] - w['away']['real score'])
        total += 1
        if (w['home']['real score'] > w['away']['real score']) \
                and (w['home']['pred score'] > w['away']['pred score']):
            win += 1
        if (w['home']['real score'] < w['away']['real score']) \
                and (w['home']['pred score'] < w['away']['pred score']):
            win += 1
        if (w['home']['real score'] == w['away']['real score']) \
                and (w['home']['pred score'] == w['away']['pred score']):
            win += 1

    return quality, win/total

def main():

    with open('qualifier_2.json', 'r') as infile:
        data = json.load(infile)

    c1 = 0.008
    c2 = 0.01
    c3 = 0.2
    c4 = 0.4
    c5 = 0.3
    c6 = 0.3

    last_fit = 1e99
    base_fit = 1e99

    while 1:
        c1 = random.uniform(0.0,0.1)
        c2 = random.uniform(0.0,0.1)
        c3 = random.uniform(0.0,0.4)
#        c4 = random.uniform(0.0,1.0)
#        c5 = random.uniform(0.0,1.0)
#        c6 = random.uniform(0.0,1.0)

        (last_fit, last_win) = train_model(data, c1, c2, c3, c4, c5, c6)

        if last_fit < base_fit:
            c1s = c1
            c2s = c2
            c3s = c3
            c4s = c4
            c5s = c5
            c6s = c6
            base_fit = last_fit
            print(last_fit, last_win, c1s, c2s, c3s, c4s, c5s, c6s)

if __name__ == '__main__':
    main()


path_name = 'Day1.txt'

'''
#Part 1
def get_number(lst):
    for i in lst:
        for j in lst:
                if i+j == 2020:
                    return i * j'''

#Part 2, high complexity
def get_number(lst):
    for i in lst:
        for j in lst:
            for k in lst:
                if i+j+k == 2020:
                    return i * j * k

# Reads from file, default is String so cast to int.
def read_file(path):
    f = open(path, 'r')
    l = f.readlines()
    lst = []
    for i in l:
        lst.append(int(i.rstrip()))
    #for the visuals
    print(get_number(lst))
    return get_number(lst)

read_file(path_name)


from fastapi import FastAPI
from pydantic import BaseModel
# input으로 List를 받기 위한 import.
from typing import List
from app.load_distance_matrix import load_distance_matrix
from app.floyd_warshall import floyd_warshall_initialize
from app.held_karp import held_karp
from app.two_opt import local_search_2_opt
from app.util import distance_calc
from app.make_distance_matrix import make_distance_matrix

from copy import deepcopy

import numpy as np

class Locations(BaseModel):
    locations: List[int]

# INF 상수는 연결되어있지 않음을 의미.
INF = 10**5

# distance matrix 초기화.
DISTANCE_MATRIX = make_distance_matrix(INF)

# 총 vertex 개수.
V = len(DISTANCE_MATRIX)

# DISTANCE_MATRIX 확인.
# print("DISTANCE_MATRIX")
# print(DISTANCE_MATRIX)

# distance matrix와 floyd-warshall을 이용하여,
# a -> b로의 최단 거리와, 이동 경로 저장.
# 모든 점으로부터 모든 점으로까지 최단거리
SHORTEST_DISTANCES, SHORTEST_PATHS = floyd_warshall_initialize(DISTANCE_MATRIX,INF)

# SHORTEST_DISTANCES, SHORTEST_PATHS 확인.
# print("SHORTEST_DISTANCES")
# print(SHORTEST_DISTANCES)
# print("SHORTEST_PATHS")
# print(SHORTEST_PATHS)

app = FastAPI()

@app.post("/shortest-path")
def find_shortest_path(locations: Locations):
    # 방문할 위치들 입력 확인.
    print("locations")
    print(locations.locations)

    # 시작점과 끝점을 추가해서 TSP를 적용할 list 만들기.
    visit_list = deepcopy(locations.locations)
    # 원래는 0번 카운터에서 시작했지만, 중간에서도 시작하기 위해 입력 리스트의 첫번째 원소를 시작위치로.
    start_location = locations.locations[0]
    # visit_list.insert(0,0)
    visit_list.append(V-1)

    # TSP 알고리즘을 적용하기 위해, 방문하는 위치들만으로 완전그래프(complete graph) 만들기.
    # complete matrix 만들기.
    l = len(visit_list)
    complete_graph = [[-1 for i in range(l)] for i in range(l)]
    for r in range(l):
        for c in range(l):
            complete_graph[r][c]=SHORTEST_DISTANCES[visit_list[r]][visit_list[c]]

    # 방문해야할 장소 개수가 15이하일 때는 exact algorithm인 held-karp algorithm 적용.
    #           "          15이상일 때는 heuristic algorithm인 2-opt algorithm 적용.
    # 최단거리, 최단경로 초기화.
    shortest_distance = -1
    temp_path = []
    if l <= 14:
        shortest_distance, temp_path = held_karp(complete_graph,INF)
    else:
        # 입력 형태를 맞추기 위해, distance matrix인 complete_graph를 numpy array로 변환해줌.
        complete_graph_np_array = np.array(complete_graph)

        # seed 초기화. seed[0]는 경로를, seed[1]은 거리를 의미.
        seed=[[],[]]

        # path 초기화는 숫자가 낮은 순서로 방문.
        seed[0]=[ i for i in range(len(complete_graph_np_array))]
        seed[1] = distance_calc(complete_graph_np_array,seed)

        temp_path,shortest_distance = local_search_2_opt(complete_graph_np_array,seed,-1,True)

    # 실제 경로로 변환해주기.
    # paths를 보고, 경로 알려주기.
    real_path = [start_location]
    path_len = len(temp_path)
    for i in range(path_len-1):
        real_path.extend(SHORTEST_PATHS[visit_list[temp_path[i]]][visit_list[temp_path[i+1]]][1:])

    # 디버깅.
    # 최단 경로
    print("real_path")
    print(real_path)

    # 최단 거리 출력해보기.
    print("shortest_distance")
    print(shortest_distance)

    return {"path":real_path}


import platform, os
from stack import stack

def menu(stack):
    clear()
    stackPrint(stack)
    print('1. push')
    print('2. pop')
    print('3. exit')
    x = int(input('select: '))
    return x

def stackPrint(stack):
    print('stack now:')
    stack.printStack()
    print('')

def clear():
    name = platform.system()
    os.system('clear' if name == 'Linux' else 'cls')


from flask import Flask
from flask import render_template
from flask import request
from flask import make_response
from flask import session

app = Flask(__name__)

app.secret_key = 'F12Zr47j\3yX R~X@H!jmM]Lwf/,?KT'

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/iniciarTest', methods=['POST'])
def iniciar():
    if request.method == 'POST':
        session['name'] = request.form['yourname']
        respuesta = make_response(render_template('pag1.html'))
        return respuesta

@app.route('/escogerCarrera', methods=['POST'])
def test1():
    carreras=[]
    pregunta1 = request.form['pregunta1']
    pregunta2 = request.form['pregunta2']
    pregunta3 = request.form['pregunta3']
    pregunta4 = request.form['pregunta4']
    pregunta5 = request.form['pregunta5']
    pregunta6 = request.form['pregunta6']
    pregunta7 = request.form['pregunta7']
    pregunta8 = request.form['pregunta8']
    pregunta9 = request.form['pregunta9']
    pregunta10 = request.form['pregunta10']
    pregunta11 = request.form['pregunta11']
    pregunta12 = request.form['pregunta12']
    pregunta13 = request.form['pregunta13']
    pregunta14 = request.form['pregunta14']
    pregunta15 = request.form['pregunta15']
    pregunta16 = request.form['pregunta16']
    pregunta17 = request.form['pregunta17']
    pregunta18 = request.form['pregunta18']
    pregunta19 = request.form['pregunta19']
    pregunta20 = request.form['pregunta20']
    pregunta21 = request.form['pregunta21']
    pregunta22 = request.form['pregunta22']
    pregunta23 = request.form['pregunta23']
    pregunta24 = request.form['pregunta24']
    pregunta25 = request.form['pregunta25']
    if pregunta1 == "si" and pregunta4 == "si" and pregunta18 == "si":
        carrera='Comunicacion y disenio'
        carreras.append(carrera)
    if pregunta2 == "si" and pregunta5 == "si" and pregunta14 == "si":
        carrera = 'Ciencias Administrativas'
        carreras.append(carrera)
    if pregunta7 == "si" and pregunta10 == "si" and pregunta15 == "si":
        carrera = 'Ingenieria y sistemas'
        carreras.append(carrera)
    if pregunta9 == "si" and pregunta11 == "si" and pregunta21 == "si":
        carrera = 'Ciencias de la salud'
        carreras.append(carrera)
    if pregunta6 == "si" and pregunta12 == "si" and pregunta16 == "si":
        carrera = 'Arquitectura'
        carreras.append(carrera)
    if pregunta13 == "si" and pregunta17 == "si" and pregunta20 == "si":
        carrera = 'Derecho y ciencias politicas'
        carreras.append(carrera)
    if pregunta3 == "si" and pregunta8 == "si" and pregunta19 == "si" and pregunta22 == 'si':
        carrera = 'Hotelería, Gastronomía y Turismo'
        carreras.append(carrera)
    if pregunta23 == "si" and pregunta24 == "si" and pregunta25 == "si":
        carrera = 'Logistica Maritma y portuaria'
        carreras.append(carrera)
    cantidad=len(carreras)

    respuesta = make_response(render_template('pag2.html', c=carreras, t=cantidad))
    return respuesta

@app.route('/SeleccionarFacultad', methods=['POST'])
def test2():
   facultad_final=request.form['pregunta']
   if facultad_final == 'Comunicacion y disenio':
       return render_template('pag3.html',facul=facultad_final)
   elif facultad_final == 'Ciencias Administrativas':
       return render_template()
   elif facultad_final == 'Ingenieria y sistemas':
       return render_template('sistemas.html', facul=facultad_final)
   elif facultad_final == 'Ciencias de la salud':
       return render_template()
   elif facultad_final == 'Arquitectura':
       return render_template()
   elif facultad_final == 'Derecho y ciencias politicas':
       return render_template()
   elif facultad_final == 'Hotelería, Gastronomía y Turismo':
       return render_template()
   elif facultad_final == 'Logistica Maritma y portuaria':
       return render_template()

@app.route('/ingenieria', methods=['POST'])
def ingenieria():
 if request.method == 'POST':
     carrera = []
     r1 = request.form['opcion1']
     r2 = request.form['opcion2']
     r3 = request.form['opcion3']
     r4 = request.form['opcion4']
     r5 = request.form['opcion5']
     r6 = request.form['opcion6']
     r7 = request.form['opcion7']
     if r1 == 'si':
         lic = 'Lic. en Ing. de Redes y Datos con énfasis en Sistemas Inalámbricos'
         carrera.append(lic)
         return render_template('resultados.html', l=lic)
     elif r2 == 'si':
         lic = 'Lic. en Ingeniería en Sistemas Computacionales '
         carrera.append(lic)
         return render_template('resultados.html', l=lic)
     elif r3 == 'si':
         lic = 'Lic. en Ingeniería en Electrónica y Comunicaciones '
         carrera.append(lic)
         return render_template('resultados.html', l=lic)
     elif r4 == 'si':
         lic = 'Lic. en Ingeniería en Industrial con énfasis en Gestión de Calidad'
         carrera.append(lic)
         return render_template('resultados.html', l=lic)
     elif r5 == 'si':
         lic = 'Lic. en Ingeniería en Industrial con énfasis en Gestión de Operaciones '
         carrera.append(lic)
         return render_template('resultados.html', l=lic)
     elif r6 == 'si':
         lic = 'Lic. en Ingeniería Industrial y de Sistemas '
         carrera.append(lic)
         return render_template('resultados.html', l=lic)
     elif r7 == 'si':
         lic = 'Lic. en Sistemas Comp. con énfasis en Desarrollo de Sistemas Avanzados de Redes y Software '
         carrera.append(lic)
         return render_template('resultados.html', l=lic)

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


import requests
import os

URL_BASE = "http://146.19.247.196"
token = str

def get_token_from_file() -> token:
    with open("TOKEN.txt", encoding="utf-8") as file:
        return file.read()

def get_or_create_token() -> token:
    if os.path.exists("TOKEN.txt"):
        return get_token_from_file()
    else:
        token = input("Введите ваш токен телеграм бота.\n")
        with open("TOKEN.txt", "w", encoding="utf-8") as file:
            file.write(token)
        return token

def check_access() -> bool:
    token = get_or_create_token()
    proj = "proj1"
    port = ":8000/"

    data = {'token_bot': token, 'project_name': proj}

    full_url = URL_BASE + port + "ask_access/"
    resp = requests.post(full_url, json=data)
    if resp.json()['status'] == "ok":
        return True
    else:
        return False

if __name__ == '__main__':
    has_access = check_access()


#! -*- coding:utf-8 -*-
"""
@author: chengbo
@software: PyCharm
@file: re_test.py
@time: 2022/8/11 15:08
"""

import re

text = "1p34.3 - pter microdeletion."

re_text = r"(\d+|[XY])[pq](ter)*(\+)?\d*(\.){0,1}\d*(\s*)(((-\s*>|-->|\-|\~|—|–|_|→|)|(\s*)(to)*(\s*))?(\(\d+\.*\d*\s*[-_–~]\s*\d+\.*\d*\)|)[pq]?(ter)*\d*){0,2}(\.){0,1}\d*(\s*)[pq]?\d*(\.){0,1}\d*(\(\d*(,\d*|)(,\d*|)(\s*)[_-](\s*)\d*(,\d*|)(,\d*|)\)(x\s*\d*|amp)|)"

print(re.search(re_text, text))
print(re.findall(re_text, text))

text = "\na\n"
print(text)
print(re.findall("\n", text))

import re

re_str_patt = "\\\\"
reObj = re.compile(re_str_patt)
str_test = "abc\\cd\\hh"
print(reObj.findall(str_test))
import re

re_str_patt = "\\\\"
reObj = re.compile(re_str_patt)
str_test = "abc\\cd\\hh"
print(reObj.findall(str_test))

text = "\\2345"
print(re.findall(r"\\|\d", text))


# import globals
from threading import Lock
from flask import Flask, render_template, session, request, copy_current_request_context
from flask_socketio import SocketIO, emit

# import our own code
from app_config import *
from networks import *
from models import *
from db import *
from freqs import *

# set up app
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, async_mode=async_mode)
thread = None
thread_lock = Lock()

# logger
def log(x):
		print(x)
		emit('status','(server) '+x)

id2M={}

@app.route('/')
def analyze_word():
		return render_template('word.html',DEFAULT_WORD_STR=DEFAULT_WORD_STR,W2V_MODELS=W2V_MODELS)

@app.route('/about')
def index():
	return render_template('about.html')

@app.route('/corpus_coha')
def corpus_coha():
	return render_template('corpus_coha.html')

@app.route('/manifestos')
def manifestos():
		return render_template('manifestos.html')

@app.route('/manifestos_feb22')
def manifestos_feb22():
		return render_template('manifestos_feb22.html')

def get_model(opts):
	global id2M

	Mid=opts['model_id']
	if Mid in id2M:
		model = id2M[Mid]
	else:
		model = id2M[Mid] = Embedding(id=Mid,fn=opts['model_fn'],periods=opts['model_periods'])
	return model

@socketio.on('mostsimnet')
def mostsimnet(opts):
	msg='mostsimnet'
	log('starting '+msg+'()')
	print('mostsimnet_opts: ',opts)

	log('getting model')
	model=get_model(opts)

	log('getting sim data')
	try:
		n_top = int(opts['n_top'])
	except ValueError:
		n_top = DEFAULT_N_TOP

	most_similar_data = model.get_most_similar(words=opts['words'],n_top=n_top,periods=opts['periods'],combine_periods=opts['combine_periods'])

	log('making network')
	networks_data = sims2net(most_similar_data,combine_periods=opts['combine_periods'])

	emit(msg+'_resp', {'data':networks_data})

@socketio.on('get_freqs')
def get_freqs(opts):
	msg='get_freqs'
	log('starting '+msg+'()')
	print(msg+'_opts: ',opts)

	words=deperiodize_l(opts['words'])
	log('getting freqs for %s' % ', '.join(words))

	#hdf_fn = W2V_MODELS[opts['model_id']]['fn_freqs']
	#log('using hdf data stored in: '+hdf_fn)
	model_opts = W2V_MODELS[opts['model_id']]

	DATA=[]
	for word in words:
		df_freqs = get_df_freqs(word, model_opts)
		print('get_freqs received:', df_freqs)
		if df_freqs is None: continue
		data_ld = df_freqs.to_dict('records')
		DATA+=[(word,data_ld)]

	emit(msg+'_resp', DATA)

if __name__ == '__main__':
	# app.run(debug=True)
	import sys
	print(sys.argv)
	port=1799
	if len(sys.argv)>1:
		port=int(sys.argv[-1])
	print(' * Starting server on port',port)
	socketio.run(app, debug=True, port=port,host='0.0.0.0')


'''
This script is the processing pipeline used to create the "automatic" datasets files shown in DGML.
 The automatic datasets are those that are not curated but created by this script.
 There are ___ steps in this pipeline:

   1. Get csv files' paths from dataset_path input parameter
   2. Filter csv paths according to a specific_ids text file
   3. Load DGF catalog to retrieve info about datasets
   4. For each dataset (csv) path, do:
        a. Return the csv dataframe and its metadata load_wrapper()
        b. Create an output folder with the id of the dataset as name
        c. Discard datasets not passing the first level constraints
        d. Generate pandas profiling. Create file {id}_pandas_profile.html
        e. Generate pandas profiling summary. Create file statistics_summary.csv
        f. Get data dictionary. Create file dict_data.csv
        g. Returns clean dataset before mljar.
        h. Discard empty datasets and datasets with less than 3 columns. Save
           its pandas profile report.
        i. For each target variable (column) in the dataset, do:
            1. Remove missing values in target variable column
            2. Run mljar process. Save output files in {dataset_id}/{automl_target-var}/
            3. Compute and store a score to this mljar run with this dataset and with this target var
            4. Add a new line to the open_data_ml_datasets.csv file with the info of this dataset
               (pandas profile + mljar profile for this run, etc)

Notes:
    1. We remove non-ascii characters within the categorical (nominal) values (it does not work if we don't do this)
    2. We remove the lines with missing values in the target variable

'''

import glob
import logging
from pathlib import Path
from typing import Union, Optional

import fire
from pandas.util import hash_pandas_object
from dotenv import load_dotenv
import fs
import pandas as pd
from csv_detective.explore_csv import routine
from fs.glob import GlobMatch
from supervised.model_framework import ModelFramework
from src.get_dataset import latest_catalog, info_from_catalog, load_dataset
from src.get_mljar import prepare_for_mljar, generate_mljar
from src.get_statistic_summary import generate_pandas_profiling, get_statistics_summary, get_data_dictionary
from app.apps.utils import slugify
import json

load_dotenv(".env")
logging.root.handlers = []
# noinspection PyArgumentList
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s [%(levelname)s] %(message)s",
    handlers=[
        logging.FileHandler("debug.log", mode="w"),
        logging.StreamHandler()
    ]
)

def get_specific_ids(specific_ids_path: Optional[Path] = None):
    """
    If there is a specific_ids_path file, take the ids within and process them
    exclusively.
    :param specific_ids_path:  The path of a text file with id per line
    :return: List of read ids from the specific_ids_path file
    """
    if specific_ids_path is None or not specific_ids_path.exists():
        return
    with open(specific_ids_path) as filo:
        specific_ids = [l.strip() for l in filo.readlines()]
    logging.info(f"We found specific ids. They are: {specific_ids}")
    return specific_ids

def create_folder(output_dir):
    if not output_dir.exists():
        output_dir.mkdir()

def get_mljar_info(output_dir, automl_report):
    """

    :param output_dir:
    :param automl_report:
    :return:
    """
    # 1. Move the leaderboard.csv file (automl summary) to the upper level
    automl_report.get_leaderboard().to_csv(output_dir.joinpath("leaderboard.csv"), index=False)

    # 2. Delete all models (bc they are heavy and we don't use them)
    model_file_paths = [Path(p) for p in glob.glob(output_dir.as_posix() + f"/**/learner_fold_*.*", recursive=True)]
    model_file_paths = [p for p in model_file_paths if
                        p.suffix not in [".csv", ".png", ".log", ".svg"]]
    for model_path in model_file_paths:
        model_path.unlink()

def fill_main_csv(id_, catalog, statistics_summary, output_dir=Path("../app/assets/datasets/"),
                  target_variable=None, task=None, score='', automl=None):
    """This function adds a new row in dgml_datasets.csv containing info of a chosen dataset."""
    main_csv_path = output_dir.joinpath('dgml_datasets.csv')
    new_row = {}
    dict_main_df = {'title': 'dataset.title', 'dgf_dataset_url': 'dataset.url',
                    'dgf_dataset_id': 'dataset.id', 'dgf_resource_url': 'url','description':'description'}
    for key, item in dict_main_df.items():
        new_row[key] = catalog[catalog['id'] == id_][item].values.item()
    new_row['nb_lines'] = statistics_summary['Number of lines'][0]
    new_row['nb_features'] = statistics_summary['Number of variables'][0]
    new_row['profile_url'] = f""
    if target_variable is None and task is None:
        new_row['automl_url'] = ''
        new_row['target_variable'] = ''
        new_row['task'] = ''
    else:
        new_row['automl_url'] = ''
        new_row['target_variable'] = target_variable
        if "classification" in task.lower():
            new_row["task"] = "Classification"
        else:
            new_row["task"] = task.capitalize()
    new_row["is_validated"] = False
    new_row["topic"] = "Undefined"
    new_row["dict_url"] = ""
    new_row['dgf_resource_id'] = id_
    new_row['openml_id'] = ""
    # add info about best model:
    if automl is None:
        new_row['best_model'] = ''
        new_row['metric_type'] = ''
        new_row['metric_value'] = ''
    else:
        if isinstance(automl._best_model, ModelFramework):
            new_row['best_model'] = automl._best_model.get_name()
            new_row['metric_type'] = automl._best_model.metric_name
            new_row['metric_value'] = automl._best_model.get_final_loss()
        else:
            new_row['best_model'] = automl._best_model.algorithm_short_name
            new_row['metric_type'] = automl._get_eval_metric()
            new_row['metric_value'] = automl._best_model.best_loss
    if main_csv_path.exists():
        main_df = pd.read_csv(main_csv_path)
        main_df = main_df.append(new_row, ignore_index=True)
    else:
        main_df = pd.DataFrame([new_row])
    # add score to main csv file:
    main_df.loc[
        (main_df['dgf_resource_id'] == id_) & (main_df['target_variable'] == target_variable), ['score']] = score
    main_df.to_csv(main_csv_path, index=False)
    return main_df

def check_constraints(data, parameters):
    """This function filters the datasets in the datasets folder according to the basic constraints defined in the config file
    (config/filters.json).
    In fact, only datasets that have the following characteristics will be included in DGML's analysis:
    * minimal  NUMBER OF LINES (param: min_lines)
    * maximal number of lines (param: max_lines)
    * minimal NUMBER OF COLUMNS (param: min_cols)
    * maximal number of columns (param: max_cols)
    * minimal LINES/COLUMNS RATIO ; ex. if 10: the dataset contains at least 10 times more
    lines than columns (param: lines_cols/ratio)
    * the presence of BOTH NUMERICAL AND CATEGORICAL VARIABLES ; if TRUE: the dataset must contain
    both categorical and numerical variables (param: num_and_cat)
    * the maximum percentage of MISSING VALUES (param:max_missing_values)
    """
    passed_constraints = False
    nb_lines = len(data)
    nb_columns = len(data.columns)
    check_categorical = data.select_dtypes(include='object').empty
    check_numerical = data.select_dtypes(include=['float64', 'int64']).empty
    for param in parameters:
        min_lines = float(param['min_lines'])
        max_lines = float(param['max_lines'])
        min_cols = float(param['min_cols'])
        max_cols = float(param['max_cols'])
        lines_cols_ratio = float(param['lines_cols/ratio'])
        max_missing = float(param['max_missing_values'])
    total_nan = data.isna().sum().sum() / (nb_lines * nb_columns)
    if (min_lines <= nb_lines <= max_lines) and (min_cols <= nb_columns <= max_cols) and (
            (nb_lines / nb_columns) >= lines_cols_ratio) and (
            check_categorical is False) and (check_numerical is False) and (total_nan <= max_missing):
        passed_constraints = True
    return passed_constraints

# TO DO: add these parameters to a config file
def load_dataset_wrapper(dataset_name: Union[Path, str]):
    csv_data = None
    if isinstance(dataset_name, Path):
        try:
            csv_data = routine(dataset_name.as_posix(), num_rows=200)
        except Exception as e:
            logging.exception(f"Dataset {dataset_name}: csv-detective analysis failed")
            raise e

        encoding = csv_data.get("encoding", "latin-1")
        separator = csv_data.get("separator", ",")
        dataset_df = pd.read_csv(dataset_name, sep=separator, encoding=encoding)
        dataset_df.rename(columns=slugify, inplace=True)
        id_data = dataset_name.stem
        # Delete file if it is a temp file (in /tmp)
        if "/tmp" in dataset_name.as_posix():
            dataset_name.unlink()

    else:
        catalog = latest_catalog()  # or fixed_catalog to use our catalog
        catalog_info = info_from_catalog(dataset_name, catalog)
        dataset_df = load_dataset(id=dataset_name, catalog_info=catalog_info)
        id_data = dataset_name

    return dataset_df, id_data, csv_data

def get_csv_paths(datasets_path: Path):
    def create_files_iterator(remote_globber: GlobMatch):
        """

        :param remote_globber:
        :return:
        """
        temp_path = Path("/tmp").joinpath(Path(remote_globber.path).stem.split("--")[1] + ".csv")
        with open(temp_path, "wb") as tmp:
            my_fs.download(remote_globber.path, tmp)
        return Path(tmp.name)

    """Return the paths of each dataset in the source CSVs folder, whether local or through a sftp connection"""
    # 1. If the dataset_path is local, just return the list of csv files
    if "sftp" not in datasets_path.as_posix():
        if not datasets_path.exists():
            raise FileNotFoundError(f"File {datasets_path} not found. Please choose another csv folder")
        csv_paths = [Path(p) for p in glob.glob(datasets_path.as_posix() + f"/*.csv", recursive=True)]
    else:
        # 2. Try to connect to this remote resource (only dealing with sftp)
        # noinspection PyBroadException
        try:
            my_fs = fs.open_fs(datasets_path)
            csv_paths = (create_files_iterator(path) for path in my_fs.glob("**/*.csv").__iter__())
        except Exception as e:
            logging.exception(f"Connecting to {datasets_path} did not work")
            raise e
    return csv_paths

def generate_score(statistics_summary, columns_to_drop, automl):
    """Returns a score of the evaluating the 'goodness' of a given dataset. Datasets with an higher score will be selected for the app.
    The score takes into account:
    * the overall percentage of missing values, extracted from the pandas df of statistics_summary.csv (30%)
    * the percentage of variables not retained when running mljar (warning columns and columns detected by csv detective) (40%)
    * the logloss (for classification) or rmse (for regression) value for the best model (30%), extracted leaderboard.csv """
    prop_missing = statistics_summary['Percentage of missing cells'] / 100
    prop_not_retained = len(columns_to_drop) / statistics_summary['Number of variables']
    best_metric = automl.get_leaderboard()['metric_value'].min()
    score = 1 / (0.3 * prop_missing + 0.4 * prop_not_retained + 0.3 * best_metric)
    return score

def read_parameters(parameters_file: Path):
    """This function reads the config file containing the parameters needed for filtering datasets."""
    if parameters_file.exists():
        with open(parameters_file) as fout:
            parameters = json.load(fout)
    else:
        raise FileNotFoundError(f"Config file {parameters_file.as_posix()} does not exist.")
    return parameters

def main(dataset_path: str,
         output_dir: str,
         specific_ids_path: str = None,
         automl_mode: str = "Explain"):
    """

    :param dataset_path: Folder with CSVs files
    :param output_dir: Folder were the output is saved
    :param specific_ids_path: Path to a text file with a file name per line. Only this files will be treated
    :param automl_mode: "perform" ou "explain". Perform maximizes the model performance. Explain maximizes the
                        explicability of the models.
    :return: None
    """

    seen_dataframes = set()
    output_dir = Path(output_dir)
    dataset_path = Path(dataset_path)
    dataset_paths = get_csv_paths(dataset_path)
    specific_ids = get_specific_ids(specific_ids_path)
    automl_mode = automl_mode
    catalog = latest_catalog()  # or fixed_catalog to use our catalog
    for ix, path in enumerate(dataset_paths):
        if specific_ids and dataset_path.stem not in specific_ids:
            logging.warning(f"We are only analysing specific ids. Id {path.stem} is not a specified id. Trying "
                            f"next...")
            continue
        try:
            data_df, id_data, csv_detective_data = load_dataset_wrapper(path)
            df_hash = hash_pandas_object(data_df).sum()
            if df_hash in seen_dataframes:
                logging.info(f"Dataset {id_data}: Same hash dataset already treated. Skipping dataset.")
                continue
            seen_dataframes.add(df_hash)
            logging.info(f"Treating Dataset {id_data} ({ix})")
            current_output_dir = output_dir.joinpath(id_data)
            logging.info(f"Dataset {id_data}: Successfully loaded dataset.")
            create_folder(current_output_dir)
            create_folder(current_output_dir / "our_experiments")
            parameters = read_parameters(Path('src/config/filters.json'))
            if not check_constraints(data_df, parameters=parameters):
                logging.warning(
                    f"The Dataset {id_data} did not pass the first-level constraints. It seems not adequate for Machine "
                    f"Learning")
                continue
            logging.info(f"Dataset {id_data}: passed the first-level constraints")
            profiling = generate_pandas_profiling(id_data, data_df, output_dir=current_output_dir, config_path=None)
            statistics_summary = get_statistics_summary(profiling, output_dir=current_output_dir)

            get_data_dictionary(profiling, output_dir=current_output_dir)
            logging.info(f"Dataset {id_data}: Successfully generated Pandas Profiling.")
            prep_data, columns_to_drop = prepare_for_mljar(data=data_df, profiling=profiling,
                                                           csv_data=csv_detective_data)
            logging.info(f"Dataset {id_data}: removed the following columns: {columns_to_drop}")
            logging.info(f"Dataset {id_data}: the following columns are left: {list(prep_data.columns)}")
            if prep_data is not None and len(prep_data.columns) < 3:
                logging.warning(f"Dataset {id_data}: We have less than 3 columns. "
                                f"We will only generate the pandas profiling")
                fill_main_csv(id_=id_data, catalog=catalog, output_dir=output_dir,
                              statistics_summary=statistics_summary, score='')
                continue
            for target_variable in prep_data.columns:
                try:
                    logging.info(f"Dataset {id_data}: Testing AutoML models with target var {target_variable}")
                    # drop nan lines
                    notna_data = prep_data[prep_data[target_variable].notna()]
                    mljar_output_dir = current_output_dir.joinpath(f"automl_{target_variable}")
                    automl = generate_mljar(data=notna_data, target_variable=target_variable,
                                            output_dir=mljar_output_dir, automl_mode=automl_mode)
                    get_mljar_info(output_dir=mljar_output_dir, automl_report=automl)
                    # plot_mljar_table(id)
                    logging.info(f"Dataset {id_data}: Successfully generated AutoML report.")
                    task = automl._get_ml_task()
                    score = generate_score(statistics_summary=statistics_summary, columns_to_drop=columns_to_drop,
                                           automl=automl)
                    logging.info(f"Dataset {id_data}: The score is: {score[0]}")
                    fill_main_csv(id_=id_data, catalog=catalog, statistics_summary=statistics_summary,
                                  output_dir=output_dir, target_variable=target_variable,
                                  task=task, score=score[0], automl=automl)
                    logging.info(f"Dataset {id_data}: Added info to main datasets csv.")
                except Exception:
                    logging.exception(f"Dataset {id_data}: Fatal error while testing var {target_variable}")
                    continue

        except Exception:
            logging.exception(f"Dataset {path}: Fatal error while treating file")

if __name__ == "__main__":
    fire.Fire(main)


from typing import List

from fastapi import APIRouter, BackgroundTasks, HTTPException
from starlette.responses import JSONResponse
from tortoise.exceptions import DoesNotExist

from ..schema import plant
from ..utils.tasks import check_if_job_exists, scrape_for_plant
from ..utils import search

router = APIRouter()

@router.get(
    "/plant",
    tags=["plant"],
    response_model=List[plant.PlantOut_Pydantic],
    responses={403: {"detail": "Not authorized"}},
)
async def get_plants():
    return await plant.get_plants()

@router.get(
    "/plant/{latin_name}",
    tags=["plant"],
    response_model=plant.PlantOut_Pydantic,
    responses={
        404: {"detail": "Object does not exist"},
        403: {"detail": "Not authorized"},
    },
)
async def get_plant(
    latin_name: str,
    background_tasks: BackgroundTasks,
):
    try:
        return await plant.get_plant(latin_name)
    except DoesNotExist:
        if not await check_if_job_exists(latin_name):
            background_tasks.add_task(scrape_for_plant, latin_name)
            return JSONResponse(
                status_code=404, content={"detail": "Object does not exist"}
            )
        else:
            return await plant.get_plant(latin_name)

@router.post(
    "/plant",
    tags=["plant"],
    response_model=plant.PlantOut_Pydantic,
    responses={403: {"detail": "Not authorized"}},
)
async def create_plant(plant_in: plant.PlantIn_Pydantic):
    return await plant.create_plant(plant_in)

@router.put(
    "/plant/{latin_name}",
    tags=["plant"],
    response_model=plant.PlantOut_Pydantic,
    responses={403: {"detail": "Not authorized"}},
)
async def update_plant(latin_name: str, plant_in: plant.Plant_Update_Pydantic):
    p = await plant.update_plant(latin_name, plant_in)
    search.update_or_add_plant(p)
    return p


import xbmc
import xbmcaddon
import xbmcgui
from contextlib import contextmanager

ADDON = xbmcaddon.Addon(id='script.screensaver.cocktail')
ADDON_NAME = ADDON.getAddonInfo('name')
ADDON_ICON = ADDON.getAddonInfo('icon')

def translate(text):
    return ADDON.getLocalizedString(text).encode('utf-8')

def log(message):
    try:
        xbmc.log("{}: {}".format(ADDON_NAME, message), level=xbmc.LOGDEBUG)
    except UnicodeEncodeError:
        xbmc.log("{}: {}".format(ADDON_NAME, message).encode(
            'utf-8', 'ignore'), xbmc.LOGDEBUG)

def notification(message_idx):
    xbmcgui.Dialog().notification(
        translate(32000),
        translate(message_idx),
        icon = ADDON_ICON,
        time = 1500
    )

@contextmanager
def busy_dialog():
    xbmc.executebuiltin('ActivateWindow(busydialognocancel)')
    try:
        yield
    finally:
        xbmc.executebuiltin('Dialog.Close(busydialognocancel)')


import rpyc
from english_words import *
from constCS import *

dns = rpyc.connect(DNS_HOST, port = DNS_PORT)

enc = dns.root.lookup(ENCRYPT_NAME)
dec = dns.root.lookup(DECRYPT_NAME)

if enc is None or dec is None:
	quit()

encrypter = rpyc.connect(enc[0], port = enc[1])
decrypter = rpyc.connect(dec[0], port = dec[1])

key = 15
for word in english_words_lower_set:
	encrypted = encrypter.root.encrypt(word, key)
	decrypted = decrypter.root.decrypt(encrypted, key)
	print(word, '->', encrypted, '->', decrypted)


#!/usr/bin/env python3

import os
import sys
import platform
import datetime

import cv2 as cv
import numpy as np
import scipy.io as sio

from PyQt5 import QtWidgets

"""
Copyright (C) 2019 Denis Polygalov,
Laboratory for Circuit and Behavioral Physiology,
RIKEN Center for Brain Science, Saitama, Japan.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, a copy is available at
http://www.fsf.org/
"""

"""
The SamTraker.py is a Semi-automatic mouse Tracker, - standalone
hybrid type (CLI + GUI) appliction for tracking single subject
in a recorded video stream. Output tracking data saved
as Matlab '*_tracking_data.mat' file.
"""

# TODO
# Add automatically generated timestamp to output mat file name?
# Add post-processing stage. Calculate distance traveled here?
# Add 'Subject ID' as user input? https://doc.qt.io/qt-5/qinputdialog.html
# Add pixel2cm constant input calculated based on first ROI selection?
# Allow user to terminate tracking WITH DATA SAVED during/after tracking failure handling?
# Save first accepted frame and first ROI drawn by user?

def process_video_file(s_fname_in, s_out_dir, s_out_fname_base, max_nframes=None):
    s_fname_out_mat = os.path.join(s_out_dir, s_out_fname_base + "_tracking_data.mat")
    if os.path.isfile(s_fname_out_mat):
        print("ERROR: output file already exist")
        sys.exit(-1)

    # the video source (movie) object
    oc_movie = CMuPaMovieCV((s_fname_in,))

    d_param = {}
    d_param['tracker_type'] = "KCF"
    d_param['aim_color'] = (60, 255, 255)
    d_param['aim_thickness'] = 2

    # extract video file parameters needed for detector
    d_param['frame_rate'] = oc_movie.df_info.loc[0, 'frame_rate']
    d_param['frames'] = oc_movie.df_info.loc[0, 'frames']

    i_frame_id = 0 # the frame counter
    t_ROI = None # current(!) ROI
    i_key_code = -1
    i_esc_key_code = 27
    oc_tracker = None # the video tracker instance
    na_tmp_img = None

    while(oc_movie.read_next_frame()):
        if not isinstance(na_tmp_img, np.ndarray):
            na_tmp_img = oc_movie.na_frame.copy()
        else:
            na_tmp_img[...] = oc_movie.na_frame[...]

        # yellow color frame-sized rectangle is used as indicator of first frame selection request
        cv.rectangle(na_tmp_img, (0,0), (na_tmp_img.shape[1], na_tmp_img.shape[0]), (0,255,255), 5)

        # Show current frame to user and ask for decision
        cv.imshow(s_out_fname_base, na_tmp_img)
        print("USER_INPUT_REQUEST: current frame #%i (a)ccept? (n)ext? (Esc)ape?" % i_frame_id)

        i_key_code = get_user_key_or_die(['a','n'])

        if i_key_code == ord('a'):
            break
        elif i_key_code == ord('n'):
            i_frame_id += 1
            continue
        else:
            raise ValueError("this should never happen")

    print("INFO: accepted frame #%i" % i_frame_id)

    b_is_frame_ready = True
    while b_is_frame_ready:
        if i_frame_id % 100 == 0: print("INFO: process frame #%i HINT: press 'r' to reset and assign new ROI" % (i_frame_id))

        if oc_tracker is None:
            d_param['initial_frame_number'] = i_frame_id
            print("USER_INPUT_REQUEST: select object to be tracked")
            d_param['initial_ROI'] = selectROI_or_die(s_out_fname_base, oc_movie.na_frame)
            print("INFO: selected ROI for the frame #%i: %s" % (i_frame_id, d_param['initial_ROI']))
            oc_tracker = CSingleSubjectTracker(
                oc_movie.na_frame.shape[0], # frame height
                oc_movie.na_frame.shape[1], # frame width
                oc_movie.na_frame.dtype,
                d_param
            )

        oc_tracker.process_frame(oc_movie.na_frame)
        cv.imshow(s_out_fname_base, oc_tracker.na_out)

        if not oc_tracker.is_tracking_succeed():
            print("WARNING: tracking failed at frame #%i" % i_frame_id)
            print("USER_INPUT_REQUEST: current frame #%i (a)ccept? (n)ext? (Esc)ape?" % i_frame_id)

            i_key_code = get_user_key_or_die(['a','n'])

            if i_key_code == ord('a'):
                print("USER_INPUT_REQUEST: accepted frame #%i. Select next ROI" % i_frame_id)
                t_ROI = selectROI_or_die(s_out_fname_base, oc_movie.na_frame)
                print("INFO: selected new ROI for the frame #%i: %s" % (i_frame_id, t_ROI))
                oc_tracker.restart_tracking(oc_movie.na_frame, t_ROI)
                b_is_frame_ready = oc_movie.read_next_frame() # WATCH OUT for number of calls
                i_frame_id += 1
                continue

            elif i_key_code == ord('n'):
                oc_tracker.skip_frame()
                b_is_frame_ready = oc_movie.read_next_frame() # WATCH OUT for number of calls
                i_frame_id += 1
                continue

            else: raise ValueError("this should never happen")

        # Wait for key press for 1ms
        i_key_code = cv.waitKey(1)
        i_key_code = i_key_code & 0xFF

        if i_key_code == ord('r'):
            print("USER_INPUT_REQUEST: ROI reset requested at frame #%i. Select new ROI" % i_frame_id)
            t_ROI = selectROI_or_die(s_out_fname_base, oc_movie.na_frame)
            print("INFO: selected new ROI for the frame #%i: %s" % (i_frame_id, t_ROI))
            oc_tracker.restart_tracking(oc_movie.na_frame, t_ROI)

        # Exit unconditionally if ESC key was pressed
        if i_key_code == i_esc_key_code: break

        b_is_frame_ready = oc_movie.read_next_frame() # WATCH OUT for number of calls
        i_frame_id += 1

        if max_nframes is not None and i_frame_id >= max_nframes: break
    #

    if i_key_code == i_esc_key_code:
        print("ERROR: interrupted by user. Tracking data is NOT saved!")
        sys.exit(-1)

    # seal output data into numpy arrays
    oc_tracker.finalize_tracking()
    # add extra parameters to be saved
    oc_tracker.d_STRACK['input_file_name'] = s_fname_in
    oc_tracker.d_STRACK['output_file_name'] = s_fname_out_mat
    oc_tracker.d_STRACK['hostname'] = platform.node()
    oc_tracker.d_STRACK['username'] = os.getlogin()
    oc_tracker.d_STRACK['datetime'] = str(datetime.datetime.now())
    # save output data into the mat file
    sio.savemat(s_fname_out_mat, oc_tracker.d_STRACK)
#

def get_user_key_or_die(l_key_choices):
    """
    Ask user to press one of keys listed in the l_key_choices
    or interrupt the whole script if 'q' or 'Esc' pressed.
    """
    while True:
        # Wait for key press indefinitely
        i_key_code = cv.waitKey(-1)
        i_key_code = i_key_code & 0xFF

        if i_key_code == 27 or i_key_code == ord('q'):
            print("ERROR: interrupted by user. No data saved.")
            sys.exit(-1)

        for _, ch in enumerate(l_key_choices):
            if i_key_code == ord(ch): return i_key_code

        print("ERROR: unknown key pressed")
        continue
    #
#

def selectROI_or_die(s_window_name, na_frame):
    """
    Ask user to draw the ROI on the frame.
    Providing acceptable ROI is obligatory for this script.
    """
    t_ROI = None
    while True:
        t_ROI = cv.selectROI(s_window_name, na_frame, False)
        if t_ROI[0] == 0 and all(i_elem == t_ROI[0] for i_elem in t_ROI):
            print("WARNING: selection was canceled or wrong")
        else:
            break
    if tuple is not type(t_ROI) or len(t_ROI) != 4:
        print("ERROR: something went seriously wrong. Exiting...")
        sys.exit(-1)
    return t_ROI
#

if __name__ == '__main__':
    s_base_dir, _ = os.path.split(os.getcwd())
    sys.path.append(s_base_dir)

    from mendouscopy.mupamovie import CMuPaMovieCV
    from mendouscopy.behavior import CSingleSubjectTracker

    i_max_nframes = None
    s_start_dir = os.path.expanduser('~')
    oc_app = QtWidgets.QApplication(sys.argv)
    s_file_filter = "Video Files(*.avi *.mp4 *.wmv *.mov *.mpg *.m4v *.mkv *.ts *.mjpg);; All Files(*.*)"

    while True:
        s_fname_in, _ = QtWidgets.QFileDialog.getOpenFileName(caption="Select input video file", directory=s_start_dir, filter=s_file_filter)

        if not os.path.isfile(s_fname_in):
            print("ERROR: input file is not accessible")
            sys.exit(-1)

        s_out_dir, s_fname = os.path.split(s_fname_in)
        if not os.path.isdir(s_out_dir):
            print("ERROR: output directory is not accessible")
            sys.exit(-1)

        s_out_fname_base, _ = os.path.splitext(s_fname)
        process_video_file(s_fname_in, s_out_dir, s_out_fname_base, max_nframes=i_max_nframes)
        cv.destroyAllWindows()

        oc_reply = QtWidgets.QMessageBox.question(
            None, 'Continue or Quit?',
            'Processing complete.\nDo you want to process another file?',
            QtWidgets.QMessageBox.Yes, QtWidgets.QMessageBox.No
        )

        if oc_reply == QtWidgets.QMessageBox.Yes:
            s_start_dir = s_out_dir
            continue
        else:
            break
    print()
#


from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
#from django.template import loader
from django.utils import timezone

from .models import Item, UserList
from .forms import NameForm
from .list_form import UserListForm

def index(request):
    item_list = Item.objects.all()
    produce_items = Item.objects.filter(item_type="produce")
    meat_items = Item.objects.filter(item_type="meat_and_fish")
    p_goods = Item.objects.filter(item_type="packaged_goods")
    bulk = Item.objects.filter(item_type="bulk_goods")
    item_types = Item.ITEM_TYPES
    item_types_hr = [x[1] for x in Item.ITEM_TYPES]
    item_status_hr = [x[1] for x in Item.ITEM_STATUS]
    context = {
        'item_list': item_list,
        'item_desc': item_types_hr,
        'bulk': bulk,
        'p_goods': p_goods,
        'meat_items': meat_items,
        'produce_items': produce_items,
        'status_types': item_status_hr,
        'item_types': item_types_hr
    }
    return render(request, 'grocery_list/index.html', context)

def list_display(request, list_id):
    mylist = UserList.objects.get(pk=list_id)
    return HttpResponse(mylist)

def detail(request, item_id):
    response = "Viewing details about %s."
    return HttpResponse(response % item_id)

def need(request):
    needed_item_list = Item.objects.exclude(status='good')
    context = {
        'needed_item_list': needed_item_list,
    }
    return render(request, 'grocery_list/get.html', context)

def get_list(request):
    if request.method == 'POST':
        form = UserListForm(request.POST)
        if form.is_valid():
            print(form.cleaned_data)
            new_list = form.save()
            return HttpResponseRedirect('/thanks/')
    else:
        form = UserListForm()

    return render(request, 'grocery_list/list.html', {'form': form})

def thanks(request):
    response = "Thanks for keeping things updated"
    return HttpResponse(response)


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

import numpy as np
import math
class LogisticRegression:

    def __init__(self, alpha = 0.01, regLambda=0.01, epsilon=0.0001, maxNumIters = 50):
        '''
        Initializes Parameters of the  Logistic Regression Model
        '''
        self.alpha = alpha
        self.regLambda = regLambda
        self.epsilon = epsilon
        self.maxNumIters = maxNumIters

    # def Product(self, weight, X):
    # 	# weight and X are all 1 dimension list
    # 	res=0
    # 	for i in range(len(X)):
    # 		res=res+weight[i]*X[i]
    # 	return res

    def calculateGradient(self, weight, X, Y, regLambda):# Assume X[0] is 1, which is illusory attribute for W[0]
        '''
        Computes the gradient of the objective function
        Arguments:

            X is a n-by-(d+1) numpy matrix
            Y is an n-by-1 dimensional numpy matrix
            weight is (d+1)-by-1 dimensional numpy matrix
            regLambda is the scalar regularization constant
        Returns:
            the gradient, an (d+1)-by-1 dimensional numpy matrix
        '''
        Gradient=np.zeros((weight.shape[0],1));# matrix, not list
        penalty=[regLambda*theta_j for theta_j in weight] #list not matrix
        penalty[0]=0 # do not to regularize the theta0 parameter
        list_x=np.zeros((X.shape[0],1))
        for i in range(X.shape[0]):
        		list_x[i]=np.dot(weight[:,0],X[i,:])
        list_x=self.sigmoid(list_x)
        for j in range(weight.shape[0]):
        	tmp=np.add(list_x,-Y)
        	Gradient[j]=np.dot(tmp[:,0],X[:,j])+penalty[j]

   #      for i in range(X.shape[0]):
   #      	#X[i][0]=1 # the illusory X[i][0] are set to be 1
   #      	tmp=1/(1+math.exp(-np.dot(weight[:,0],X[i,:])))-Y[i]
   #      	for j in range(X.shape[1]):
			# #self.Product([i for i in weight[:][0]],[i for i in X[i][:]])
   #      		Gradient[j]=Gradient[j]+tmp*X[i][j]
   #      for j in range(X.shape[1]):
   #      		Gradient[j]=Gradient[j]+penalty[j]
        return Gradient

    def sigmoid(self, Z):
        '''
        Computes the Sigmoid Function
        Arguments:
            A n-by-1 dimensional numpy matrix
        Returns:
            A n-by-1 dimensional numpy matrix

        '''
        Z=np.exp(-Z)
        for i in range(Z.shape[0]):
        	Z[i]=1/(1.0+Z[i])

        return Z

    def update_weight(self,X,Y,weight):
        '''
        Updates the weight vector.
        Arguments:
            X is a n-by-(d+1) numpy matrix
            Y is an n-by-1 dimensional numpy matrix
            weight is a d+1-by-1 dimensional numpy matrix
        Returns:
            updated weight vector : (d+1)-by-1 dimensional numpy matrix
        '''
        new_weight=np.zeros(weight.shape)
        gradient=self.calculateGradient(weight, X, Y, self.regLambda)
        new_weight=np.add(weight,-self.alpha*gradient)
        return new_weight

    def check_conv(self, weight, new_weight, epsilon):
        '''
        Convergence Based on Tolerance Values
        Arguments:
            weight is a (d+1)-by-1 dimensional numpy matrix
            new_weights is a (d+1)-by-1 dimensional numpy matrix
            epsilon is the Tolerance value we check against
        Return :
            True if the weights have converged, otherwise False

        '''
        gap=0
        gap_list=np.add(weight,-new_weight)
        gap=np.linalg.norm(gap_list)
        #print("gap is: %f\n"%gap)
        return gap <= epsilon

    def train(self,X,Y):
        '''
        Trains the model
        Arguments:
            X is a n-by-d numpy matrix
            Y is an n-by-1 dimensional numpy matrix
        Return:
            Updated Weights Vector: (d+1)-by-1 dimensional numpy matrix
        '''
        # Read Data
        n,d = X.shape
        #Add 1's column
        X = np.c_[np.ones((n,1)), X]
        self.weight = self.new_weight = np.zeros((d+1,1))
        # Calculate weight

        #self.new_weight=self.update_weight(X,Y,self.weight)#$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
        iters=0
        while iters<self.maxNumIters:
        	self.new_weight=self.update_weight(X,Y,self.weight)
        	if self.check_conv(self.weight,self.new_weight,self.epsilon):
        		print("break up")
        		break;
        	self.weight=self.new_weight
        	iters+=1
        	#print("%d\n" %iters)

        return self.new_weight

    def predict_label(self, X,weight):
        '''
        Used the model to predict values for each instance in X
        Arguments:
            X is a n-by-d numpy matrix
            weight is a d+1-by-1 dimensional matrix
        Returns:
            an n-by-1 dimensional matrix of the predictions 0 or 1
        '''
        #data
        n=X.shape[0]
        #Add 1's column
        X = np.c_[np.ones((n,1)), X]
        result=np.zeros((X.shape[0],1))
        tmp=0
        for i in range(X.shape[0]):
        	tmp=np.dot(X[i,:],weight[:,0])
        	if tmp<=0:
        		result[i]=0
        	else:
        		result[i]=1

        return result

    def calculateAccuracy (self, Y_predict, Y_test):
        '''
        Computes the Accuracy of the model
        Arguments:
            Y_predict is a n-by-1 dimensional matrix (Predicted Labels)
            Y_test is a n-by-1 dimensional matrix (True Labels )
        Returns:
            Scalar value for accuracy in the range of 0 - 100 %
        '''
        acc=0
        for i in range(Y_predict.shape[0]):
        	if Y_predict[i]==Y_test[i]:
        		acc+=1

        return (acc*100)/(0.0+Y_test.shape[0])


from __future__ import print_function
import os
from math import sqrt
import time

def cls():
    os.system('cls' if os.name=='nt' else 'clear')

answer_1 = ["1", "1."]
answer_2 = ["2", "2."]
answer_3 = ["3", "3."]
answer_4 = ["4", "4."]
answer_X = ["X", "x"]
answer_Y = ["Y", "y"]
answer_END = ["exit"]

required = ("\nIeveadi tikai skaitli 1 vai 2\n")
required_op = ("\nIeveadi tikai x, y vai exit\n")
required_fu = ("\nIeveadi tikai 1, 2, 3 vai 4\n")

a = 0.0
b = 0.0
c = 0.0
x0 = 0.0
y0 = 0.0
r = 0.0
k = 0.0
m = 0.0

class color:
   PURPLE = '\033[95m'
   CYAN = '\033[96m'
   DARKCYAN = '\033[36m'
   BLUE = '\033[94m'
   GREEN = '\033[92m'
   YELLOW = '\033[93m'
   RED = '\033[91m'
   BOLD = '\033[1m'
   UNDERLINE = '\033[4m'
   END = '\033[0m'

def sakums():
    cls()
    print(color.GREEN + """-------------------------------------------------------------------------
                            FUNKCIJU KALKULATORS
-------------------------------------------------------------------------""" + color.END)
    print(color.BOLD + """Izvēlies funkcijas veidu
    """ + color.END)
    print(color.PURPLE + "1. Lineāra funkcija (y=kx+m)" + color.END)
    print(color.CYAN + "\n2. Kvadrāt funkcija (y=ax^2+bx+c)" + color.END)
    izvele = input(color.GREEN + "\nIevadi attiecīgo skaitli:" + color.END)
    if izvele in answer_1:
        l_izvele()

    elif izvele in answer_2:
        k_izvele()

    else:
        cls()
        print(color.GREEN + required + color.END)
        time.sleep(2)
        cls()
        sakums()

def k_izvele():
    global a, b, c, x0, y0, r
    cls()
    try:
        print(color.CYAN + "------------Kvadrāt funkcija------------\n" + color.END)
        print("""* y = ax^2 + bx + c
      ^
  Šī vērtība
                """)
        a = float(input(color.BOLD + "\nIevadi funkcijas a vertību:" + color.END))
        cls()

        if a == 0:
            cls()
            print(color.GREEN + "a vērtība nevar būt 0 - tad tā vairs nav kvadrāt funkcija, bet lineāra funkcija" + color.END)
            time.sleep(4)
            cls()
            k_izvele()
    except:
        k_invalid()

    try:
        print(color.CYAN + "------------Kvadrāt funkcija------------\n" + color.END)
        print("""* y = ax^2 + bx + c
             ^
         Šī vērtība
            """)
        b = float(input(color.BOLD + "Ievadi funkcijas b vertību:" + color.END))
        cls()
    except:
        k_invalid()

    try:
        print(color.CYAN + "------------Kvadrāt funkcija------------\n" + color.END)
        print("""* y = ax^2 + bx + c
                  ^
             Šī vērtība
            """)
        c = float(input(color.BOLD + "Ievadi funkcijas c vertību:" + color.END))

    except:
        k_invalid()

    x0 = -b / (2*a)
    y0 = a*(x0**2) + b*x0 + c

    r = b**2 - 4*a*c

    k_apreikins()

def k_invalid():
    cls()
    print(color.GREEN + "Ievadi skaitlisku vērtību\n\nvai decimāl daļas raksti ar . nevis ," + color.END)
    time.sleep(3)
    cls()
    k_izvele()

def k_apreikins():
    cls()
    print(color.CYAN + "------------Kvadrāt funkcija------------\n" + color.END)
    print(color.BOLD + "Izvēlies ko vēlies apreiķināt" + color.END)
    print("\n1. Funkcijas virziens\n2. Krustpunktu ar y un x asi\n3. Funkcijas virsotne\n4. Definīcijas un vērtību apgabals\n")
    skaitlis_k = input(color.GREEN + "Ievadi attiecīgo skaitli:" + color.END)

    if skaitlis_k in answer_1:
        kvad_1()
    elif skaitlis_k in answer_2:
        kvad_2()
    elif skaitlis_k in answer_3:
        kvad_3()
    elif skaitlis_k in answer_4:
        kvad_4()
    else:
        cls()
        print(color.GREEN + required_fu + color.END)
        time.sleep(2)
        cls()
        k_apreikins()

def kvad_1():
    cls()
    if a > 0:
        cls()
        print(color.CYAN + "------------Funkcijas virziens------------\n" + color.END)
        print(color.BOLD +"Kvadrāt funkcijas virziens ir uz:" + color.END)
        print(color.GREEN + "AUGŠU\n" + color.END)
        time.sleep(0.5)
        opt_k()
    elif a < 0:
        cls()
        print(color.CYAN + "------------Funkcijas virziens------------\n" + color.END)
        print(color.BOLD +"Kvadrāt funkcijas virziens ir uz:" + color.END)
        print(color.GREEN + "LEJU\n" + color.END)
        time.sleep(0.5)
        opt_k()

def opt_k():
    op = input("Ievadi x ja vēlies turpināt veikt citus apreiķinus ar šo pašu funkciju\nIevadi y ja vēlies sākt no sākuma\nIevadi exit ja vēlies iziet no programas:").lower()
    if op in answer_X:
        k_apreikins()
    elif op in answer_Y:
        sakums()
    elif op in answer_END:
        exit()
    else:
        cls()
        print(color.GREEN + required_op + color.END)
        time.sleep(2)
        cls()
        opt_k()

def opt_l():
    op = input("Ievadi x ja vēlies turpināt veikt citus apreiķinus ar šo pašu funkciju\nIevadi y ja vēlies sākt no sākuma\nIevadi exit ja vēlies iziet no programas:").lower()
    if op in answer_X:
        l_apreikins()
    elif op in answer_Y:
        sakums()
    elif op in answer_END:
        exit()
    else:
        cls()
        print(color.GREEN + required_op + color.END)
        time.sleep(2)
        cls()
        opt_l()

def kvad_2():
    cls()
    print(color.CYAN + "------------Krustpunkts ar x un y asi------------\n" + color.END)

    if r > 0:
        x1 = (((-b) + sqrt(r))/(2*a))
        x2 = (((-b) - sqrt(r))/(2*a))
        print(color.BOLD +"2 x ass krustpunkti:" + color.END)
        print(color.GREEN + f"x1 = {x1} un x2 = {x2}\n"+ color.END)
        print(color.BOLD + "Krustpunkts ar y asi:" + color.END)
        print(color.GREEN + f"y = {c}\n" + color.END)
        time.sleep(0.5)
        opt_k()

    elif r == 0:
        x = (-b) / 2*a
        print(color.BOLD + "1 x ass krustpunkts: " + color.END)
        print(color.GREEN + f"x = {x}\n" + color.END)
        print(color.BOLD + "Krustpunkts ar y asi:" + color.END)
        print(color.GREEN + f"y = {c}\n" + color.END)
        time.sleep(0.5)
        opt_k()

    else:
        print(color.GREEN + "Nav x ass krustpunktu, diskriminants ir mazāks par 0\n" + color.END)
        print(color.BOLD + "Krustpunkts ar y asi:" + color.END)
        print(color.GREEN + f"y = {c}\n" + color.END)
        time.sleep(0.5)
        opt_k()

def kvad_3():
    cls()
    print(color.CYAN + "------------Funkcijas virsotne------------\n" + color.END)
    print(color.BOLD + "Kvadrāt funkcijas virsotnes kordinātes ir:" + color.END)
    print(color.GREEN + f"({x0};{y0})\n" + color.END)
    time.sleep(0.5)
    opt_k()

def kvad_4():
    cls()
    if a > 0:
        print(color.CYAN + "------------Definīcijas un vērtību apgabals------------\n" + color.END)
        print(color.GREEN + f"""D(f) = (-∞;+∞)\nE(f) = [{y0};+∞)\n""" + color.END)
        time.sleep(0.5)
        opt_k()
    elif a < 0:
        print(color.CYAN + "------------Definīcijas un vērtību apgabals------------\n" + color.END)
        print(color.GREEN + f"""D(f) = (-∞;+∞)\nE(f) = (-∞;{y0})\n""" + color.END)
        time.sleep(0.5)
        opt_k()

def l_izvele():
    global k, m
    cls()
    try:
        cls()
        print(color.PURPLE + "------------Lineār funkcija------------\n" + color.END)
        print("""* y = kx + m
      ^
 Šī vērtība
                    """)
        k = float(input(color.BOLD + "\nIevadi funkcijas k vertību:" + color.END))
    except:
        l_invalid()

    try:
        cls()
        print(color.PURPLE + "------------Lineār funkcija------------\n" + color.END)
        print("""* y = kx + m
           ^
       Šī vērtība
                    """)
        m = float(input(color.BOLD + "\nIevadi funkcijas m vertību:" + color.END))
    except:
        l_invalid()

    l_apreikins()

def l_invalid():
    cls()
    print(color.GREEN + "Ievadi skaitlisku vērtību\n\nvai decimāl daļas raksti ar . nevis ," + color.END)
    time.sleep(3)
    cls()
    l_izvele()

def l_invalid2():
    cls()
    print(color.GREEN + "Ievadi skaitlisku vērtību\n\nvai decimāl daļas raksti ar . nevis ," + color.END)
    time.sleep(3)
    cls()
    lin_4()

def l_apreikins():
    cls()
    print(color.PURPLE + "------------Lineār funkcija------------\n" + color.END)
    print(color.BOLD + "Izvēlies ko vēlies apreiķināt" + color.END)
    print("\n1. Funkcijas virziens\n2. Krustpunktu ar y un x asi\n3. Definīcijas un vērtību apgabals\n4. Divu lineāru funkciju krustpunkti\n")
    skaitlis_l = input(color.GREEN + "Ievadi attiecīgo skaitli:" + color.END)

    if skaitlis_l in answer_1:
        lin_1()
    elif skaitlis_l in answer_2:
        lin_2()
    elif skaitlis_l in answer_3:
        lin_3()
    elif skaitlis_l in answer_4:
        lin_4()
    else:
        cls()
        print(color.GREEN + required_fu + color.END)
        time.sleep(2)
        cls()
        l_apreikins()

def lin_1():
    cls()
    if k > 0:
        cls()
        print(color.PURPLE + "------------Funkcijas virziens------------\n" + color.END)
        print(color.BOLD +"Kvadrāt funkcijas virziens ir uz:" + color.END)
        print(color.GREEN + "AUGŠU\n" + color.END)
        time.sleep(0.5)
        opt_l()
    elif k < 0:
        cls()
        print(color.PURPLE + "------------Funkcijas virziens------------\n" + color.END)
        print(color.BOLD +"Kvadrāt funkcijas virziens ir uz:" + color.END)
        print(color.GREEN + "LEJU\n" + color.END)
        time.sleep(0.5)
        opt_l()
    elif k == 0:
        cls()
        print(color.PURPLE + "------------Funkcijas virziens------------\n" + color.END)
        print(color.BOLD +"Kvadrāt funkcijas virziens ir:" + color.END)
        print(color.GREEN + "KONSTANTA jeb nemainīga\n" + color.END)
        time.sleep(0.5)
        opt_l()

def lin_2():
    cls()
    print(color.PURPLE + "------------Krustpunkts ar x un y asi------------\n" + color.END)
    xl = m/(-k)
    yl = m
    print(color.BOLD +"x ass krustpunkts:" + color.END)
    print(color.GREEN + f"x = {xl}\n"+ color.END)
    print(color.BOLD + "y ass krustpunkts:" + color.END)
    print(color.GREEN + f"y = {yl}\n" + color.END)
    time.sleep(0.5)
    opt_l()

def lin_3():
    cls()
    print(color.PURPLE + "------------Definīcijas un vērtību apgabals------------\n" + color.END)
    print(color.GREEN + f"""D(f) = (-∞;+∞)\nE(f) = [-∞;+∞)\n""" + color.END)
    print(color.BOLD + "\n*** lineāras funkcijas definīcijas un vērtības apgabals ir visi reālie skaitļi\n" + color.END)
    time.sleep(0.5)
    opt_l()

def lin_4():
    cls()
    try:
        cls()
        print(color.PURPLE + "------------Divu lineāru funkciju krustpunkti------------\n" + color.END)
        print(color.PURPLE + "\nIevadi otrās lineārās funkcijas vērtības\n" + color.END)
        print("""* y = kx + m
      ^
  Šī vērtība
                    """)
        k2 = float(input(color.BOLD + "\nIevadi funkcijas k vertību:" + color.END))

    except:
        l_invalid2()

    try:

        cls()
        print(color.PURPLE + "------------Divu lineāru funkciju krustpunkti------------\n" + color.END)
        print(color.PURPLE + "\nIevadi otrās lineārās funkcijas vērtības\n" + color.END)
        print("""* y = kx + m
           ^
      Šī vērtība
                    """)
        m2 = float(input(color.BOLD + "\nIevadi funkcijas m vertību:" + color.END))

    except:
        l_invalid2()

    if k2 == k:

        cls()
        print(color.PURPLE + "------------Divu lineāru funkciju krustpunkti------------\n" + color.END)
        print(color.BOLD + "Funkcijas abas ir paralēlas:\n" + color.END)
        print(color.GREEN + "Nav krustpunktu (lineāras funkcijas nekrustojās)\n" + color.END)
        opt_l()

    xi = (m2-m)/(k-k2)
    yi = (k*xi+m)

    print(color.PURPLE + "------------Divu lineāru funkciju krustpunkti------------\n" + color.END)
    print(color.BOLD + "Krustpunkta kordenātes:" + color.END)
    print(color.GREEN + f"({xi};{yi})" + color.END)
    opt_l()

sakums()


#!/usr/bin/env python3

from sys import argv, platform
import os

DELIMITER = '\\' if platform == 'win32' else '/'

def __main__():
	folders = os.listdir(argv[1])
	for i in folders:
		i_fullpath = argv[1] + DELIMITER + i
		#print(i_fullpath)
		if os.path.isdir(i_fullpath):
			files = os.listdir(i_fullpath)
			for j in files:
				j_fullpath = i_fullpath + DELIMITER + j
				if os.path.isfile(j_fullpath):
					filename = i_fullpath + DELIMITER + j
					filenameNew = argv[1] + DELIMITER + j
					os.rename(filename, filenameNew)
					#print(filename, '->', filenameNew)
				else:
					print('Not a file. Skipping;')
		else:
			print('Not a folder. Skipping;')

__main__()


cave = []
input = []
with open("input3.txt") as f:
    input = f.read().split("\n")[:-1]
for line in input:
    cave.append([int(c) for c in line])

def flash(cave, r, c):
    # print(f"Flashing {r},{c}")
    r_max = len(cave)
    c_max = len(cave[r])

    def _bump(cave, r, c):
        if r >= 0 and r < len(cave) and c >= 0 and c < len(cave[r]):
            cave[r][c] += 1
            if cave[r][c] == 10:
                flash(cave, r, c)

    _bump(cave, r - 1, c - 1)
    _bump(cave, r - 1, c)
    _bump(cave, r - 1, c + 1)
    _bump(cave, r, c - 1)
    _bump(cave, r, c + 1)
    _bump(cave, r + 1, c - 1)
    _bump(cave, r + 1, c)
    _bump(cave, r + 1, c + 1)

def run(cave):
    # Increment by 1
    flashes = []
    for r in range(len(cave)):
        for c in range(len(cave[r])):
            cave[r][c] += 1
            if cave[r][c] == 10:
                flashes.append((r, c))

    # Check for flashes
    for coords in flashes:
        flash(cave, coords[0], coords[1])

    # Count actual flashes:
    flash_count = 0
    for r in range(len(cave)):
        for c in range(len(cave[r])):
            if cave[r][c] > 9:
                flash_count += 1
                cave[r][c] = 0

    return flash_count

def p_cave(cave):
    if is_synced(cave):
        print("Cave is SYNCED!")
    else:
        print("Cave is _not_ synced")
    for r in cave:
        print(r)

def is_synced(cave):
    for r in cave:
        for c in r:
            if c > 0:
                return False
    return True

# p_cave(cave)
# total_flashes = 0
# for i in range(195):
#     total_flashes += run(cave)
#     print("--")
#     p_cave(cave)
# print(total_flashes)

iters = 0
while not is_synced(cave):
    run(cave)
    iters += 1
print(iters)


import cv2
import numpy as np
import scipy.interpolate

def createCurveFunc(points):
    """Return a function derived from control points"""
    if points is None:
        return None
    numPoints = len(points)
    if numPoints < 2:
        return None

    xs,ys = zip(*points)

    if numPoints < 4:
        kind = 'linear' # 'quadratic is not implemented'

    else:
        kind = 'cubic'
    return scipy.interpolate.interp1d(xs,ys,kind,bounds_error=False)
    # bounds_error = false to permit extrapolation and interpolation

# print(createCurveFunc([(1,3),(3,4),(4,7)]))

def createLookupArray(func, length=256):
    """
    returns a lookup for whole-number inputs to a function
    the lookup values are clamped to [0,length-1]
    """
    if func is None:
        return None

    lookupArray = np.empty(length)
    i=0
    while i <length:
        func_i = func(i)
        lookupArray[i] = min(max(0,func_i),length-1)
        i+=1
    return lookupArray

def applyLookupArray(lookupArray, src,dest):
    """ map a source to destination using lookup"""
    if lookupArray is None:
        return
    dest[:] = lookupArray[src]

# combining two curve function into one function before creating a lookup array (optimization)

def createCompositefuntion(func0, func1):
    """ Returns a composite of two functions."""
    if func0 is None:
        return func1
    if func1 is None:
        return func0
    return lambda x: func0(func1(x))

# for image with multiple channel, instead of splitting and merging, flatten and make it 1-d and pass to create lookup array

def createFlatView(array):
    """ Returns 1-d view of an array of any dimension """
    flatView = array.view()
    flatView.shape = array.size # size will return the mxn value of the array
    return flatView

def isGray(image):
    """return true if the image has one channel per pixel"""
    return image.ndim<3

def widthHeightDividedBy(image, divisor):
    """returns and image's dimension divided by number"""
    h,w = image.shape[:2]
    # tup = []
    # # print(type(h))
    # # h = (h/divisor).astype(int)
    # # w = (w/divisor).astype(int)
    # for i in range(len(h)):
    #     tup.append(tuple((w[i],h[i])))

    # print(tup)
    # print(w)
    # print(type(w))
    # print(h)
    # concat = np.concatenate((w,h))
    return (int(w/divisor), int(h/divisor))


import logging as lg
from threading import Thread

from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
from thrift.transport import TSocket
from thrift.transport import TTransport

from ...thrift.worker.filesystem import LocalFilesystem
from .handler import LocalFileSystemHandler

_logger = lg.getLogger(__name__)

class LocalFileSystemService(object):
    def __init__(self, address="0.0.0.0", port=8989):
        self.bind_address = address
        self.bind_port = port

    def _run(self):
        # Create Service handler
        handler = LocalFileSystemHandler()

        server = TServer.TThreadedServer(
            LocalFilesystem.Processor(handler),
            TSocket.TServerSocket(self.bind_address, self.bind_port),
            TTransport.TBufferedTransportFactory(),
            TBinaryProtocol.TBinaryProtocolFactory(),
            daemon=True,
        )

        server.serve()

    def start(self):

        _logger.info("Stating Tanit local file system service.")
        self.daemon = Thread(target=self._run, args=())
        self.daemon.setDaemon(True)
        self.daemon.start()
        _logger.info(
            "Tanit local file system service started, listening  at %s:%s",
            self.bind_address,
            self.bind_port,
        )


#------------------------QFrame----------------------
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

class Main(QWidget):
    def __init__(self):
        super().__init__()

        self.frame = QFrame(self)
        self.frame.setFrameShape(QFrame.VLine)# -> draw vertical line
        self.frame.setFrameShape(QFrame.HLine)# -> draw horizontal line
        self.frame.setLineWidth(10)

        self.tbutton = QToolButton(self)
        self.tbutton.setGeometry(50,50,100,100)

        self.frame2 = QFrame(self.tbutton)# -> frame2 is resize for the self.button because his family is tbutton
        self.frame2.setFrameShape(QFrame.Box)
        self.frame2.setFrameShadow(QFrame.Raised)
        self.frame2.setLineWidth(10)
        self.frame2.setGeometry(0,0,100,100)

        self.tbutton.setToolTip("First row<br>Second row")#Work css codes

        self.resize(800,500)
        self.show()

app = QApplication([])
window = Main()
app.exec()


def calc_pi(n: int) -> float:
    numerator = 4.0
    denominator = 1.0
    operation = 1.0
    pi = 0.0
    for _ in range(n):
        pi += operation * (numerator / denominator)
        denominator += 2;
        operation *= -1
    return pi

print(calc_pi(1000000))


import numpy as np
from numpy import random, exp, log, ones, zeros, sqrt, array, arange
from treelib import Node, Tree
import pandas as pd
from itertools import chain, combinations
#%%

def f1(x):
    x1,x2 = x
    if np.isin(x2,['a','b']):
        return 8 if x1 <= 5 else 2
    else:
        if x1 <= 3: return 1
        elif x1 <= 7: return 5
        else: return 8

def genData(n=800,seed=None):
    if seed is not None:
        random.seed(seed)
    x1val = np.arange(1,11)
    x2val = ['a','b','c','d']
    x1 = random.choice(x1val,size=n)
    x2 = random.choice(x2val,size=n)
    y = array(list(map(f1,tuple(zip(x1,x2))))) + 2*random.normal(size=n)
    return pd.DataFrame({'y':y,'x1':x1,'x2':x2})

def powerset(s):
    "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
    #s = set(iterable)
    S = chain.from_iterable(combinations(s, r) for r in range(1,len(s)))
    return list(S)

def get_xvar(df):
    n = df.iloc[:,1:].apply(lambda s:len(set(s)))
    return arange(1,df.shape[1])[n>1]

def get_st(df):
    y = df['y']
    n = len(y)
    s = (n-1)*y.var()
    t = n*a/(n+a)*(y.mean()-mu)**2
    return s+t

def get_pSplit(alpha,beta,depth):
    p = alpha/(1+depth)**beta
    return p/p.sum()

def grow(tree):
    global j
    #random.seed(98)
    leaf = list(filter(lambda node:len(node.xvar)>0,tree.leaves()))
    b = len(leaf)
    if b<1: raise ValueError('cannot grow anymore')
    pNode = random.choice(leaf)
    pid = pNode.identifier
    x = random.choice(pNode.xvar)
    dtype = DataTypes[x]
    df = pNode.data
    xVal = set(df.iloc[:,x])
    if dtype == 'O':
        S = powerset(xVal)
        s = S[random.choice(range(len(S)))]
        idx = df.iloc[:,x].isin(s)
    elif (dtype == 'i') | (dtype == 'f'):
        S = sorted(xVal)[:-1] #leave the last element out
        s = random.choice(S)
        idx = (df.iloc[:,x] <= s)
    nXS = len(pNode.xvar)*len(S)
    childDF = [df.loc[idx],df.loc[~idx]]
    tree1 = Tree(tree=tree,deep=True)
    node1 = tree1.create_node(str(j),j,parent=pid,data=childDF[0])
    node2 = tree1.create_node(str(j+1),j+1,parent=pid,data=childDF[1])
    st1 = [get_st(df) for df in childDF]
    tree1.stSum = tree.stSum-pNode.st+sum(st1)

    p1 = a*(a+len(df))/(a+array([len(df) for df in childDF])).prod()
    p2 = (tree.stSum/tree1.stSum)**npv
    rLike = sqrt(p1)*p2
    w2 = len(set([node.bpointer for node in tree1.leaves()]))
    rTransit = pPrune/pGrow*b*nXS/w2
    d = tree.level(pid)+1
    rStruct = alpha*(1-alpha/(1+d)**beta)**2/(d**beta-alpha)/nXS
    r = rLike*rTransit*rStruct
    if random.uniform() < r:
        j+=2
        pNode = tree1.get_node(pid)
        pNode.tag = str(pid)+'-'+str(x)+'-'+str(s)
#        pNode.data = None
        nodes = [node1,node2]
        for i in [0,1]:
            df = childDF[i]
            nodes[i].xvar = get_xvar(df)
            nodes[i].st = st1[i]
            nodes[i].data = df
        return tree1
    else:
        return tree

#%%
df = genData()
df['x3'] = 22
s = df['y'].var()
mu = df['y'].mean()
n,p = df.shape

v = 12; lamda = 4
vlamda = v*lamda
ig1 = v/2; ig2 = lamda*ig1
a = 1/3
lna = log(a)
npv = (n+v)/2

alpha = 0.95
beta = 1
DataTypes = df.dtypes.map(lambda x:x.kind)
#%%
tree = Tree()
root = tree.create_node('0',0,data=df)
root.st = get_st(root.data)
root.xvar = get_xvar(df)
tree.stSum = root.st+vlamda
j = 1
#%%
#actionSelect
pGrow = 1
pPrune = 0.5
action = 'grow'
leaf = tree.leaves()
#depth = array([len(i) for i in tree.paths_to_leaves()])-1

for t in range(100):
    tree = grow(tree)
    tree.show()


import FWCore.ParameterSet.Config as cms

process = cms.Process( "ElectronIsolation" )
process.maxEvents = cms.untracked.PSet(
    input = cms.untracked.int32(100)
)

process.options = cms.untracked.PSet(wantSummary = cms.untracked.bool(True))
process.options.allowUnscheduled = cms.untracked.bool(False)

from RecoEgamma.EgammaIsolationAlgos.egmGedGsfElectronPFIsolation_cfi import *

process.load("CommonTools.ParticleFlow.pfNoPileUpIso_cff")
process.load("CommonTools.ParticleFlow.pfParticleSelection_cff")

process.pfNoPileUpCandidates = process.pfAllChargedHadrons.clone()
process.pfNoPileUpCandidates.pdgId.extend(process.pfAllNeutralHadronsAndPhotons.pdgId)

process.ElectronIsolation = cms.EDProducer("CITKPFIsolationSumProducer",
					    srcToIsolate = cms.InputTag("gedGsfElectrons"),
					    srcForIsolationCone = cms.InputTag('pfNoPileUpCandidates'),
					    isolationConeDefinitions = cms.VPSet(
									cms.PSet( isolationAlgo = cms.string('ElectronPFIsolationWithConeVeto'),
									coneSize = cms.double(0.3),
									VetoConeSizeEndcaps = cms.double(0.015),
									VetoConeSizeBarrel = cms.double(0.0),
									isolateAgainst = cms.string('h+'),
									miniAODVertexCodes = cms.vuint32(1,2,3) ),
									cms.PSet( isolationAlgo = cms.string('ElectronPFIsolationWithConeVeto'),
									coneSize = cms.double(0.3),
									VetoConeSizeEndcaps = cms.double(0.0),
									VetoConeSizeBarrel = cms.double(0.0),
									isolateAgainst = cms.string('h0'),
									miniAODVertexCodes = cms.vuint32(1,2,3) ),
									cms.PSet( isolationAlgo = cms.string('ElectronPFIsolationWithConeVeto'),
									coneSize = cms.double(0.3),
									VetoConeSizeEndcaps = cms.double(0.08),
									VetoConeSizeBarrel = cms.double(0.0),
									isolateAgainst = cms.string('gamma'),
									miniAODVertexCodes = cms.vuint32(2,3) )
								      )
					)
process.ntupler = cms.EDAnalyzer('ElectronNtupler_CITK',
				 pruned = cms.InputTag("genParticles"),
				 pileup = cms.InputTag("addPileupInfo"),
				 vertices = cms.InputTag("offlinePrimaryVertices"),
				 electrons = cms.InputTag("gedGsfElectrons"),
				 rho = cms.InputTag("fixedGridRhoFastjetAll"),
				 #CITK
				 ValueMaps_ChargedHadrons_src = cms.InputTag("ElectronIsolation", "h+-DR030-BarVeto000-EndVeto001"),
				 ValueMaps_NeutralHadrons_src = cms.InputTag("ElectronIsolation", "h0-DR030-BarVeto000-EndVeto000"),
				 ValueMaps_Photons_src = cms.InputTag("ElectronIsolation", "gamma-DR030-BarVeto000-EndVeto008"),

				)
process.particleFlowTmpPtrs = cms.EDProducer("PFCandidateFwdPtrProducer",
src = cms.InputTag('particleFlow')
)

process.electrons = cms.Path(process.particleFlowTmpPtrs + process.pfParticleSelectionSequence + process.pfNoPileUpCandidates + process.ElectronIsolation + process.ntupler)

process.source = cms.Source("PoolSource",
    secondaryFileNames = cms.untracked.vstring(),
    fileNames = cms.untracked.vstring('file:///afs/cern.ch/work/i/ishvetso/EgammaWork/test_samples/ttbar_AOD.root')

)

process.load("FWCore.MessageLogger.MessageLogger_cfi")
process.MessageLogger.cerr.FwkReport.reportEvery = 10

'''process.out = cms.OutputModule("PoolOutputModule",
 fileName = cms.untracked.string('patTuple.root'),
  outputCommands = cms.untracked.vstring('keep *')
)

process.outpath = cms.EndPath(process.out)'''

process.TFileService = cms.Service("TFileService",
                                 fileName = cms.string("tree.root")
                                  )


import numpy as np
import matplotlib.pyplot as plt
import glio
s = glio.GadgetSnapshot('snapshot_037')
s.load()

f = open('data_gas_xyz','w')
for i in range(0, len(s.pos[0][:])):
	f.write("%s %s %s\n" % (s.pos[0][i][0],s.pos[0][i][1],s.pos[0][i][2]))

f.close()

f2 = open('data_gas_xyz')
lines = f2.readlines()
f2.close()

gas_x = []
gas_y = []
gas_z = []

for line in lines:
	p = line.split()
	gas_x.append(float(p[0]))
	gas_y.append(float(p[1]))
	gas_z.append(float(p[2]))

gas_px = np.array(gas_x)
gas_py = np.array(gas_y)
gas_pz = np.array(gas_z)

#####################################
gas_px[:] = [x - -0.753 for x in gas_px]
gas_py[:] = [y - 0.796 for y in gas_py]
gas_pz[:] = [z - 1.000 for z in gas_pz]

plt.subplot(131)
plt.hist(gas_px, 1000)
plt.title('Gas x', fontsize=25)

plt.subplot(132)
plt.hist(gas_py, 1000)
plt.title('Gas y', fontsize=25)

plt.subplot(133)
plt.hist(gas_pz, 1000)
plt.title('Gas z', fontsize=25)

plt.show()

plt.plot(gas_px, gas_pz, '.', markersize=3,alpha=0.3)
plt.title('Gas x vs z', fontsize=30)
plt.show()