from pathlib import Path
import subprocess
import matplotlib.pyplot as plt
from tqdm import tqdm
import cv2
from .detect import detect_in_frames
from .detection import load_detections
from .trajectories import TrajectoryBuilder, save_trajectories

def plot_trajectory_helper(trajectory, frames_path, output_file):
    start, _ = trajectory[0]
    middle, _ = trajectory[len(trajectory) // 2]
    end, _ = trajectory[-1]

    plt.figure(figsize=(15, 10))
    for ti, fi in enumerate([start, middle, end]):
        plt.subplot(1, 3, ti + 1)
        img = cv2.imread(str(frames_path / f"f{fi + 1:07d}.png"))
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        plt.imshow(img)
        trajectory.plot(max_idx=fi)
        plt.xticks([], [])
        plt.yticks([], [])
        plt.xlabel(f"Frame {fi}", fontsize=16)
    plt.savefig(output_file, bbox_inches="tight")
    plt.close()

def create_clip(project_path, clip_name, start, duration, num_workers=5):
    print("Extract clip")
    output_path = project_path / clip_name
    output_path.mkdir()
    cmd = [
        "ffmpeg",
        "-ss",
        start,
        "-i",
        str(project_path / "match.mp4"),
        "-t",
        f"00:00:{duration:02d}",
        "-c",
        "copy",
        str(output_path / "clip.mp4"),
    ]
    subprocess.check_call(cmd)
    print("Extract frames")
    frames_path = output_path / "frames"
    detections_path = output_path / "detections"
    frames_path.mkdir()
    detections_path.mkdir()
    cmd = [
        "ffmpeg",
        "-i",
        str(output_path / "clip.mp4"),
        str(frames_path / "f%07d.png"),
    ]
    subprocess.check_call(cmd)
    print("Detect players")
    num_frames = detect_in_frames(
        frames_path,
        detections_path,
        num_workers=num_workers,
    )
    print("Find player trajectories")
    trajectories_path = output_path / "trajectories"
    trajectories_path.mkdir()
    builder = TrajectoryBuilder()
    for fi in tqdm(range(num_frames)):
        detections = load_detections(detections_path / f"{fi + 1:07d}.det")
        builder.add_detections(fi, detections)
    print(f"Saving {len(builder.trajectories)} trajectories to {trajectories_path}")
    save_trajectories(builder.trajectories, trajectories_path / "trajectories")
    with (trajectories_path / "players.txt").open("w") as f:
        for ti, trajectory in enumerate(tqdm(builder.trajectories)):
            f.write(f"{ti}: player_name\n")
            plot_trajectory_helper(
                trajectory, frames_path, trajectories_path / f"{ti}.png"
            )
    print(f"Edit {trajectories_path}/players.txt and run the next step")

if __name__ == "__main__":
    from argparse import ArgumentParser

    parser = ArgumentParser(description="Create a clip for a project")
    parser.add_argument(
        "-s",
        "--start",
        type=str,
        default="00:00:00",
        help="Start time (format hh:mm:ss)",
    )
    parser.add_argument(
        "-d",
        "--duration",
        type=int,
        default=10,
        help="Duration in seconds (max 60; default: 10)",
    )
    parser.add_argument(
        "-W",
        "--num-workers",
        type=int,
        default=5,
        help="Number of workers for player detection",
    )
    parser.add_argument("-p", "--project", type=str, required=True, help="Project name")
    parser.add_argument("clip_name")
    args = parser.parse_args()
    project_path = Path(args.project)
    create_clip(
        project_path,
        args.clip_name,
        args.start,
        args.duration,
        num_workers=args.num_workers,
    )


""" The entry point to the converter methods.

This provides a method to convert from japanese number notation to numerical value,
and a method to convert from a string with Japanese number notations to the numerical string.
"""
from suji.accumulator import Acc
from suji.char import Char

def values(src):
    """ Convert from Japanese number notation to numerical value.
    The return value is a list of result objects.
    If the input string has no number notation, `values` returns a empty list.
    The result object has three keys: `val`, `beg`, and `end`:

    :val: the numerical value of the number notation.
    :beg: the start postion of the found number notation at the input string.
    :end: the end postion of the found number notation.

    :param src: a input string.
    :return: a list of the numerical value objects.
    """
    val = []
    acc = Acc()

    for i in range(0, len(src)):
        if Char.is_delimiter(src[i]):
            acc.index_increment(i)
            continue

        if Char.is_decimal_point(src[i]):
            acc.turn_to_decimal_state(i)
            continue

        cardinal = Char.get_cardinal(src[i])
        if cardinal is not None:
            if acc.inside:
                acc.attach_cardinal(i, cardinal)
            elif 1 < cardinal:
                acc.attach_cardinal(i, cardinal)
            continue

        number = Char.get_number(src[i])
        if number is not None:
            acc.attach_number(i, number)
            continue

        if not acc.inside:
            continue

        val.append(acc.get_value())
        acc = Acc()

    if acc.inside:
        val.append(acc.get_value())

    return val

def value(src):
    """ Convert from a string with Japanese number notations to the numerical string.
    The return value is a converted str.
    If the input string has no number notation, `value` returns the input string.

    :param src: a input string.
    :return: a converted str.
    """
    vals = values(src)
    if 0 == len(vals):
        return src

    start = 0
    s = ''
    for v in vals:
        s += src[start:v['beg']]
        s += str(v['val'])
        start = v['end']
    s += src[start:len(src)]
    return s

__all__ = ['values', 'value']


tab = []
apro = 0
repro = 0
alunos = int(input('Digite o número de alunos: '))
for i in range(alunos):
    aluno = []
    nome = input('Digite o nome do aluno:')
    n1 = float(input('Digite a primeira nota: '))
    n2 = float(input('Digite a segunda nota: '))
    med = (n1 + n2)/2
    aluno.append(nome)
    aluno.append(n1)
    aluno.append(n2)
    aluno.append(med)
    if med >= 6:
        apro += 1
    else:
        repro += 1
    tab.append(aluno)
print(tab)
print(f'A quantidade de alunos aprovados foi de {apro}, e a quantidade de alunos reprovados foi de {repro}')

quantidade de alunos que foram aprovador-reprovados.py


from app import db
from datetime import datetime as dt
import sqlalchemy

def create_tables():
    conn = db.connect()
    init_sql = open('init.sql')
    escaped_sql = sqlalchemy.text(init_sql.read())
    conn.execute(escaped_sql)
    conn.close()

def fetch_todo(user_id) -> dict:
    conn = db.connect()
    query_results = conn.execute(f"Select * from tasks WHERE user_id = '{user_id}' ORDER BY priority DESC;").fetchall()
    conn.close()

    todo_list = []

    for result in query_results:
        item = {
            "id": result[0],
            "task": result[1],
            "priority": round(result[2], 2),
            "status": result[3],
            "added_at": result[4],
            "difficulty": result[5],
            "deadline": result[6],
            "importance": result[7]
        }
        todo_list.append(item)

    return todo_list

def update_task(field, task_id: int, text) -> None:
    conn = db.connect()
    content = f"'{text}'" if isinstance(text, str) else int(text)

    query = f"Update tasks set {field} = {content} where id = {task_id};"
    conn.execute(query)
    conn.close()

def insert_new_task(user_id, text: str, difficulty, deadline, importance) ->  int:
    date = dt.today().strftime('%Y-%m-%d')
    priority = 100 - (round((int(difficulty) + (10-int(importance)) + int(deadline)) / 3, 2) * 10)

    conn = db.connect()
    query = "Insert Into tasks (task, priority, status, difficulty, deadline, importance, added_at, user_id)" + \
                    f"VALUES ('{text}', '{priority}', 'Todo', '{difficulty}', '{deadline}', '{importance}', '{date}', '{user_id}');"
    print(query)
    conn.execute(query)
    query_results = conn.execute("SELECT currval(pg_get_serial_sequence('tasks','id'));")
    query_results = [x for x in query_results]
    task_id = query_results[0][0]
    conn.close()

    return task_id

def remove_task_by_id(task_id: int) -> None:
    conn = db.connect()
    query = 'Delete From tasks where id={};'.format(task_id)
    conn.execute(query)
    conn.close()


from django.forms.models import model_to_dict
import requests
from celery import shared_task
import datetime
import time
from django.apps import apps
from .scraper import StockTickerScraper

def_ticker = 'AAPL'
def_service = 'finnhub'

@shared_task
def perform_scrape(ticker=def_ticker, service=def_service):
    client = StockTickerScraper(service=service)
    pc = client.scrape(ticker=ticker)
    print('task', ticker, pc)
    PriceLookupEvent = apps.get_model("mysite", "PriceLookupEvent")
    PriceLookupEvent.objects.create_event(ticker, pc, source=service)

@shared_task
def company_price_scrape_task(instance_id, service=def_service):
    Stock = apps.get_model("mysite", "Stock")
    obj = Stock.objects.get(id=instance_id)
    ticker = obj.ticker
    perform_scrape(ticker=ticker, service=service)

@shared_task
def company_granular_price_scrape_task(instance_id, service=def_service):
    Stock = apps.get_model("mysite", "Stock")
    obj = Stock.objects.get(id=instance_id)
    print("hi")
    ticker = obj.ticker
    perform_scrape(ticker=ticker, service=service)
    if obj.has_granular_scraping:
        now = datetime.datetime.now()
        expires = now + datetime.timedelta(seconds=65)
        for i in range(1, 60):  # 1 - 59
            perform_scrape.apply_async(kwargs={
                "ticker": ticker,
                "service": service
            }, countdown=i, expires=expires)

"""
def get_data(symbol, stocks):
    url = "https://finnhub.io/api/v1/quote?symbol={}&token=sandbox_c0t96iv48v6r4maen2gg"
    r = requests.get(url.format(symbol)).json()
    obj, created = Stocks.object.get_or_create(symbol=symbol)
    obj.name = symbol
    obj.pc = r['pc']
    obj.save()
    new_data = model_to_dict(obj)
    new_data.update({'state': state})
    stocks.append(new_data)

@shared_task
def get_stocks_data():
    for st in AllStocks:
        get_data(event.data[st]['name'], stocks)
    async_to_sync"""


#!/usr/bin/env python
# -*- coding: utf-8 -*-
try:
    from setuptools import setup
except ImportError:
    from distutils.core import setup

description = (
    "Simulation Based Inference using Energy Based Models"
)

dist = setup(
    name="sbi_ebm",
    version="0.0.1dev0",
    description=description,
    license="BSD 3-Clause License",
    packages=["sbi_ebm.sbibm.tasks", "sbi_ebm.sbibm"],
    classifiers=[
        "Development Status :: 4 - Beta",
        "License :: OSI Approved :: BSD License",
        "Operating System :: POSIX",
        "Programming Language :: Python :: 3.9",
        "Programming Language :: Python :: Implementation :: CPython",
    ],
    python_requires=">=3.9",
)


'''
Problem:
You are given a list of integers. Your task is to find the maximum difference between any two numbers in the list.

Write a Python function called max_difference(numbers) that takes a list of integers as input and returns the maximum difference between any two numbers in the list.
'''
def max_difference(numbers):
    sorted_numbers = sorted(numbers)
    difference = (sorted_numbers[-1]-sorted_numbers[0])
    return difference
numbers=[5, 2, 4 ,3, 6, 1]
result =(max_difference(numbers))
print(result)


import pickle, random
import matplotlib.pyplot as plt

### Hog game

# score needed to win
GOAL_SCORE = 100
# max number of dice the player can roll, ignore rolling 1, sides of the dice
K, START, SIDES = 10, 2, 6
INF = float("inf")
# Dynamic programming approach to compute the probability mass function.
dp = [0]*(6*K + 1)
for i in range(START, SIDES + 1):
    dp[i] += 1

pmf_matrix = [dp]
for i in range(K - 1):
    temp = [0]*(6*K + 1)
    for j in range(len(dp) - 6):
        for v in range(START, SIDES + 1):
            temp[j + v] += dp[j]
    dp = temp
    pmf_matrix.append(list(dp))

fig, ax = plt.subplots()

for k in range(1, 11):
    x = range(2*k, 6*k + 1)
    plt.hist(x, bins=4*k + 1, weights=pmf_matrix[k - 1][2*k: 6*k + 1], density=True, label=f"{k}")

ax.legend()
plt.title(f"Probability mass function of rolling $k$ dice")
plt.xlabel('Sum of rolls')
plt.ylabel('Probability')
plt.savefig(f"pmf.png")


"""Contains DatasetBuffer"""

from cmath import nan
import logging

import numpy as np
import pandas as pd

from PyDSS.common import DatasetPropertyType
from PyDSS.exceptions import InvalidConfiguration
from PyDSS.utils.utils import make_timestamps

KiB = 1024
MiB = KiB * KiB
GiB = MiB * MiB

# The optimal number of chunks to store in memory will vary widely.
# The h5py docs recommend keeping chunk byte sizes between 10 KiB - 1 MiB.
# It needs to be larger than the biggest possible row and also cover enough
# columns to compress duplicate values. Since we might store thousands of
# elements in one dataset, make it the max by default.
# Note that the downside to making this larger is that any read causes the
# entire chunk to be read.
DEFAULT_MAX_CHUNK_BYTES = 1 * MiB

logger = logging.getLogger(__name__)

class DatasetBuffer:
    """Provides a write buffer to an HDF dataset to increase performance.
    Users must call flush_data before the object goes out of scope to ensure
    that all data is flushed.

    """
    # TODO add support for context manager, though PyDSS wouldn't be able to
    # take advantage in its current implementation.

    def __init__(
            self, hdf_store, path, max_size, dtype, columns, scaleoffset=None,
            max_chunk_bytes=None, attributes=None, names=None,
            column_ranges_per_name=None, data=None
        ):
        if max_chunk_bytes is None:
            max_chunk_bytes = DEFAULT_MAX_CHUNK_BYTES
        self._buf_index = 0
        self._hdf_store = hdf_store
        self._max_size = max_size
        num_columns = len(columns)
        if data is None:
            self.chunk_count = self.compute_chunk_count(
                num_columns,
                max_size,
                dtype,
                max_chunk_bytes,
            )
            shape = (self._max_size, num_columns)
            chunks = (self.chunk_count, num_columns)
        else:
            self.chunk_count = None
            shape = None
            chunks = None

        self._dataset = self._hdf_store.create_dataset(
            name=path,
            shape=shape,
            data=data,
            chunks=chunks,
            dtype=dtype,
            compression="gzip",
            compression_opts=4,
            shuffle=True,
            # Does not preserve NaN, so don't use it.
            #scaleoffset=scaleoffset,
        )

        # Columns, names, and column_ranges_per_name can't be stored as
        # attributes because they can exceed the size limit. Store as datasets
        # instead.
        column_dataset_path = path + "Columns"
        column_dataset = self._hdf_store.create_dataset(
            name=column_dataset_path,
            data=np.array(columns, dtype="S"),
        )
        column_dataset.attrs["type"] = DatasetPropertyType.METADATA.value
        self._dataset.attrs["column_dataset_path"] = column_dataset_path

        if names is not None:
            name_dataset_path = path + "Names"
            name_dataset = self._hdf_store.create_dataset(
                name = name_dataset_path,
                data = np.array(names, dtype="S"),
            )
            name_dataset.attrs["type"] = DatasetPropertyType.METADATA.value
            self._dataset.attrs["name_dataset_path"] = name_dataset_path

        if column_ranges_per_name is not None:
            column_ranges_dataset_path = path + "ColumnRanges"
            column_ranges_dataset = self._hdf_store.create_dataset(
                name=column_ranges_dataset_path,
                data=column_ranges_per_name,
            )
            column_ranges_dataset.attrs["type"] = DatasetPropertyType.METADATA.value
            self._dataset.attrs["column_ranges_dataset_path"] = column_ranges_dataset_path

        self._dataset.attrs["length"] = 0
        self._dataset_index = 0
        self._buf = np.empty(chunks, dtype=dtype)

        if attributes is not None:
            for attr, val in attributes.items():
                self._dataset.attrs[attr] = val

        logger.debug("Created DatasetBuffer path=%s shape=%s chunks=%s",
                     path, shape, chunks)

    def __del__(self):
        assert self._buf_index == 0, \
            f"DatasetBuffer destructed with data in memory: {self._dataset.name}"

    def flush_data(self):
        """Flush the data in the temporary buffer to storage."""
        length = self._buf_index
        if length == 0:
            return

        new_index = self._dataset_index + length
        self._dataset[self._dataset_index:new_index] = self._buf[0:length]
        self._buf_index = 0
        self._dataset_index = new_index
        self._dataset.attrs["length"] = new_index
        self._dataset.flush()

    def max_num_bytes(self):
        """Return the maximum number of bytes the container could hold.

        Returns
        -------
        int

        """
        size_row = self._buf.size * self._buf.itemsize / len(self._buf)
        return size_row * self._max_size

    def write_value(self, value):
        """Write the value to the internal buffer, flushing when full."""

        self._buf[self._buf_index] = value
        self._buf_index += 1
        if self._buf_index == self.chunk_count:
            self.flush_data()

    def write_data(self, values):
        """Write the data to the dataset."""
        new_index = self._dataset_index + len(values)
        self._dataset[self._dataset_index:new_index] = values
        self._dataset_index = new_index
        self._dataset.attrs["length"] = new_index

    @staticmethod
    def compute_chunk_count(
            num_columns,
            max_size,
            dtype,
            max_chunk_bytes=DEFAULT_MAX_CHUNK_BYTES
        ):
        assert max_size > 0, f"max_size={max_size}"
        tmp = np.empty((1, num_columns), dtype=dtype)
        size_row = tmp.size * tmp.itemsize
        chunk_count = min(int(max_chunk_bytes / size_row), max_size)
        if chunk_count == 0:
            raise InvalidConfiguration(
                f"HDF Max Chunk Bytes is smaller than the size of a row. Please increase it. " \
                f"max_chunk_bytes={max_chunk_bytes} num_columns={num_columns} " \
                f"size_row={size_row}"
            )

        return chunk_count

    @staticmethod
    def get_column_ranges(dataset):
        """Return the column ranges per name for the dataset.

        Parameters
        ----------
        dataset : h5py.Dataset

        Returns
        -------
        list

        """
        column_ranges_dataset = dataset.file[dataset.attrs["column_ranges_dataset_path"]]
        return column_ranges_dataset[:]

    @staticmethod
    def get_columns(dataset):
        """Return the columns for the dataset.

        Parameters
        ----------
        dataset : h5py.Dataset

        Returns
        -------
        list

        """
        col_dataset = dataset.file[dataset.attrs["column_dataset_path"]]
        return [x.decode("utf8") for x in col_dataset[:]]

    @staticmethod
    def get_names(dataset):
        """Return the names for the dataset.

        Parameters
        ----------
        dataset : h5py.Dataset

        Returns
        -------
        list

        """
        name_dataset = dataset.file[dataset.attrs["name_dataset_path"]]
        return [x.decode("utf8") for x in name_dataset[:]]

    @staticmethod
    def to_dataframe(dataset, column_range=None):
        """Create a pandas DataFrame from a dataset created with this class.

        Parameters
        ----------
        dataset : h5py.Dataset
        column_range : None | list
            first element is column start, second element is length

        Returns
        -------
        pd.DataFrame

        """
        length = dataset.attrs["length"]
        columns = DatasetBuffer.get_columns(dataset)
        if column_range is None:
            return pd.DataFrame(dataset[:length], columns=columns)

        start = column_range[0]
        end = start + column_range[1]
        return pd.DataFrame(
            dataset[:length, start:end],
            columns=columns[start:end],
        )

    @staticmethod
    def to_datetime(dataset):
        """Create a pandas DatetimeIndex from a dataset.

        Parameters
        ----------
        dataset : h5py.Dataset

        Returns
        -------
        pd.DatetimeIndex

        """
        length = dataset.attrs["length"]
        return make_timestamps(dataset[:length])


# in binary search it only work if list is sorted

pos = 0

def Search(list, value):
    lower = 0
    upper = len(list) - 1

    while lower <= upper:
        mid = (lower+upper)//2

        if list[mid] == value:
            globals()['pos'] = mid + 1
            return True
        else:
            if list[mid] < value:
                lower = mid + 1
            else:
                upper = mid - 1
    return False

list = [1, 2, 3, 4, 5]
value = 10

if Search(list, value):
    print("Found at position", pos)
else:
    print("Not found")


print("Hello Word")
a = 10.36

import matplotlib.pyplot as plt

plt.plot((5,1),'x')

import numpy as np

X = np.arange(0,10)
Y = X + a

Y1 = a - X


from tqdm import tqdm
from typing import Dict, Optional
from lantern.metric import Metric

def ProgressBar(data_loader, name, metrics: Optional[Dict[str, Metric]] = None):
    """Simple progress bar with metrics"""
    if metrics is None:
        for item in tqdm(data_loader, desc=name, leave=False):
            yield item
    else:
        with tqdm(data_loader, desc=name, leave=False) as tqdm_:
            for item in tqdm_:
                yield item

                log_dict = dict()
                for metric_name, metric in metrics.items():
                    metric_value = metric.compute()
                    if isinstance(metric_value, dict):
                        log_dict.update(**metric_value)
                    else:
                        log_dict[metric_name] = metric_value
                tqdm_.set_postfix(log_dict)


# Follow the hints:

# use a list as your storage (just like we did in stack)
# put() should append elements to the beginning of the list,
# while get() should remove the elements from the list's end;
# define a new exception named QueueError (choose an exception to derive it from)
# and raise it when get() tries to operate on an empty list.

# class QueueError(IndexError):  # Choose base class for the new exception.
#     def __init__(self, message='QueueError: stack is empty'):
#         self.message = message
#         super().__init__(self.message)

# class Queue:
#     def __init__(self):
#         self.__stack = []

#     def put(self, elem):
#         self.__stack.insert(0, elem)

#     def get(self):
#         val = self.__stack[-1]
#         del self.__stack[-1]
#         return val

# que = Queue()
# que.put(1)
# que.put("dog")
# que.put(False)

# try:
#     for i in range(10):
#         print(que.get())
# except:
#     print('queue stack is empty')

class QueueError(IndexError):
    pass

class Queue:
    def __init__(self):
        self.__queue = []

    def put(self, elem):
        self.__queue.insert(0, elem)

    def get(self):
        if len(self.__queue) > 0:
            elem = self.__queue[-1]
            del self.__queue[-1]
            return elem
        else:
            raise QueueError

que = Queue()
que.put(1)
que.put("dog")
que.put(False)
que.put("Test")
que.put("ABCDEFG")

try:
    for i in range(100):
        print(que.get())
except:
    print('queue stack is empty')


import sys

N, C = map(int, input().split()) # N은 집의 개수, C는 공유기의 개수
array = [] # 집의 좌표 리스트

for _ in range(N):
    array.append(int(sys.stdin.readline().rstrip()))

array.sort()

start = 1 # 모든 집은 다른 좌표에 있으므로 최소 공유기 거리는 1
end = array[-1] - array[0] # 최대 공유기 거리는 맨 끝 집 - 맨 첫 집
answer = 0

while start <= end:
  mid = (start + end) // 2

  count = 1
  cur = array[0]

  for i in range(1, len(array)):
      if array[i] - cur >= mid:
          count += 1
          cur = array[i]

  if count >= C:
      answer = mid
      start = mid + 1
  else:
      end = mid - 1

print(answer)

# 이분탐색을 풀 때 min, max값은 출력시켜야할 값의 최소, 최대값을 가지고 풀어주면 될듯 ?

이진탐색/공유기 설치.py


#reading data

file = open("test.txt", "r") # opens file in read mode (w: write, a: append, r+: read and write)
if file.readable:
    for line in file.readlines(): # puts every line into array
        print(line)
file.close() # closes file

# appending data, be careful!

file = open("employees.txt", "a") # w would overwrite file
file.write("\n" + input("neuer Kollege: "))
file.close()


#!/usr/bin/env python3

from collections import defaultdict

def nucleo_stats(dna_seq):
    stats = defaultdict(int)
    for nuclio in dna_seq:
        stats[nuclio] += 1
    order_seq = ['A', 'C', 'G', 'T']
    return [stats[nuclio] for nuclio in order_seq]

def main():
    in_data = ''
    current_dir = pathlib.Path(__file__).parent
    with open(f'{current_dir}/input.txt') as fp:
        in_data = fp.read()
    print(' '.join(map(str, nucleo_stats(in_data))))

if __name__ == "__main__":
    main()


from docx import Document
from docx.shared import Pt

documentAnswerSheet = Document()
documentQuestionSheet = Document()
language1 = []
language2 = []

print('What should be the file name:')
fileName = input()

print('How many Vocabularies will there be:')
numVocab = input()

for x in range(int(numVocab)):
    print('Vocab in Language 1:')
    language1.append(input())
    print('Vocab in Language 2:')
    language2.append(input())

for x in range(int(numVocab)):
    run1 = documentAnswerSheet.add_paragraph().add_run(language1[x] + ' = ' + language2[x])
    run2 = documentQuestionSheet.add_paragraph().add_run(language1[x] + ' = ')
    run1.font.size = Pt(24)
    run2.font.size = Pt(24)

documentQuestionSheet.save(fileName + ' - Question Sheet.docx')
documentAnswerSheet.save(fileName + ' - Answer Sheet.docx')


# Databricks notebook source
# MAGIC %md
# MAGIC Union de valores clima, altitud, pendiente, ocurrencia de incendio, alto riesgo,  peligro y vulnerabilidad con datos de movilidad urbana

# COMMAND ----------

import pyspark.sql.functions as F

# COMMAND ----------

movilidadDF = spark.read.parquet('/mnt/IncendiosForestalesCAT/prep/mitma/movilidad/')
df = spark.read.parquet(f"/mnt/IncendiosForestalesCAT/prep/riesgo_incendios_catalunya_all")
estacionesMunicipioDF = spark.read.parquet(f"/mnt/IncendiosForestalesCAT/prep/meteocat/estaciones_cercanas_municipio/")

# COMMAND ----------

display(movilidadDF)

# COMMAND ----------

display(df)

# COMMAND ----------

display(estacionesMunicipioDF)

# COMMAND ----------

estacionesMunicipioDF = estacionesMunicipioDF.withColumn("codigo_municipio_m",F.expr("substring(codigo_municipio, 1, length(codigo_municipio)-1)"))

# COMMAND ----------

df.count()

# COMMAND ----------

movilidadDF = movilidadDF.alias("df1").join(estacionesMunicipioDF.alias("df2"), F.col("df1.cod_mun_ine")==F.col("df2.codigo_municipio_m"),"inner").select(["df1.fecha","df2.indicativo","df1.total_viajes","df1.total_viajes_km"])

# COMMAND ----------

movilidadDF.count()

# COMMAND ----------

df_all = df.alias("df1").join(movilidadDF.alias("df2"), (F.col("df1.indicativo")== F.col("df2.indicativo")) & (F.col("df1.fecha")== F.col("df2.fecha")), "left").select(["df1.*","df2.total_viajes","df2.total_viajes_km"])
df_all.count()

# COMMAND ----------

display(df_all)

# COMMAND ----------

df_all.coalesce(1).write.mode("overwrite").option("header",True).csv(f"/mnt/IncendiosForestalesCAT/prep/riesgo_incendios_catalunya_all_movilidad/")

2. TRANSFORMACION/8. All_mas_movilidad/8. Tranformacion_all_movilidad.py


"""Day 16 solution.

https://adventofcode.com/2018/day/16
"""

import logging as lg

import numpy as np
import _common

_logger = lg.getLogger(__name__)

class State:
    __slots__ = ("r1", "r2", "r3", "r4")

    def __init__(self, r1: int, r2: int, r3: int, r4: int):
        self.r1 = r1
        self.r2 = r2
        self.r3 = r3
        self.r4 = r4

    def __str__(self):
        return "({}, {}, {}, {})".format(self.r1, self.r2, self.r3, self.r4)

    def __repr__(self):
        return "{}({}, {}, {}, {})".format(
            type(self).__name__,
            repr(self.r1),
            repr(self.r2),
            repr(self.r3),
            repr(self.r4))

    def __getitem__(self, idx: int):
        if idx == 0:
            return self.r1
        elif idx == 1:
            return self.r2
        elif idx == 2:
            return self.r3
        elif idx == 3:
            return self.r4
        else:
            raise IndexError(idx)

    def __setitem__(self, idx: int, value: int):
        if idx == 0:
            self.r1 = value
        elif idx == 1:
            self.r2 = value
        elif idx == 2:
            self.r3 = value
        elif idx == 3:
            self.r4 = value
        else:
            raise IndexError(idx)

    def __eq__(self, other):
        if not isinstance(self, type(other)):
            return NotImplemented
        return (
            self.r1 == other.r1
            and self.r2 == other.r2
            and self.r3 == other.r3
            and self.r4 == other.r4)

    def copy(self):
        return type(self)(self.r1, self.r2, self.r3, self.r4)

class Instruction:
    __slots__ = ("opcode", "a", "b", "c")

    def __init__(self, opcode: int, a: int, b: int, c: int):
        self.opcode = opcode
        self.a = a
        self.b = b
        self.c = c

    @classmethod
    def from_str(cls, instruction_str: str):
        return cls(*map(int, instruction_str.strip().split()))

class Operations:
    def __init__(self):
        self.operations = {}
        self._finalised = False
        self.opnames = None

    def register_operation(self, fn):
        if self._finalised:
            raise RuntimeError("Operations set finalised")
        def wrapped(a: int, b: int, c: int, state: State):
            state[c] = fn(a, b, state)
        self.operations[fn.__name__] = wrapped
        return fn

    def apply(self, instruction: Instruction, state: State, opname: str = None):
        if opname is None:
            opname = self.opnames[instruction.opcode]
        self.operations[opname](instruction.a, instruction.b, instruction.c, state)

    def apply_all(self, instruction: Instruction, state: State):
        result_states = {}
        for opname in self.operations:
            result_state = state.copy()
            self.apply(instruction, result_state, opname=opname)
            result_states[opname] = result_state
        return result_states

    def finalise(self):
        self._finalised = True
        self.opnames = tuple(self.operations)

operations = Operations()

@operations.register_operation
def addr(a, b, state):
    return state[a] + state[b]

@operations.register_operation
def addi(a, b, state):
    return state[a] + b

@operations.register_operation
def mulr(a, b, state):
    return state[a] * state[b]

@operations.register_operation
def muli(a, b, state):
    return state[a] * b

@operations.register_operation
def banr(a, b, state):
    return state[a] & state[b]

@operations.register_operation
def bani(a, b, state):
    return state[a] & b

@operations.register_operation
def borr(a, b, state):
    return state[a] | state[b]

@operations.register_operation
def bori(a, b, state):
    return state[a] | b

@operations.register_operation
def setr(a, b, state):
    return state[a]

@operations.register_operation
def seti(a, b, state):
    return a

@operations.register_operation
def gtir(a, b, state):
    return 1 if a > state[b] else 0

@operations.register_operation
def gtri(a, b, state):
    return 1 if state[a] > b else 0

@operations.register_operation
def gtrr(a, b, state):
    return 1 if state[a] > state[b] else 0

@operations.register_operation
def eqir(a, b, state):
    return 1 if a == state[b] else 0

@operations.register_operation
def eqri(a, b, state):
    return 1 if state[a] == b else 0

@operations.register_operation
def eqrr(a, b, state):
    return 1 if state[a] == state[b] else 0

operations.finalise()

class BeforeNAfter:
    __slots__ = ("before_state", "after_state", "instruction", "_valid_ops_cache")
    _state_class = State
    _instruction_class = Instruction

    def __init__(self, before_state: State, after_state: State, instruction: Instruction = None):
        self.before_state = before_state
        self.after_state = after_state
        self.instruction = instruction
        self._valid_ops_cache = None

    @classmethod
    def from_lines(cls, lines: list):
        lines = [line for line in lines if line]
        assert lines[0].startswith("Before: ")
        assert lines[2].startswith("After: ")
        before_state = cls._state_class(*map(int, lines[0][9:19].split(", ")))
        after_state = cls._state_class(*map(int, lines[2][9:19].split(", ")))
        instruction = cls._instruction_class.from_str(lines[1])
        return cls(before_state, after_state, instruction=instruction)

    @property
    def valid_ops(self):
        if self._valid_ops_cache is None:
            result_states = operations.apply_all(self.instruction, self.before_state)
            valid_opnames = []
            for opname, result_state in result_states.items():
                if result_state == self.after_state:
                    valid_opnames.append(opname)
            self._valid_ops_cache = valid_opnames
        return self._valid_ops_cache

class Program:
    _instruction_class = Instruction
    _state_class = State

    def __init__(self, instructions: list, init_state: _state_class = None):
        self.instructions = instructions
        self.init_state = init_state or self._state_class(0, 0, 0, 0)
        self.state = self.init_state.copy()

    def run(self):
        for instruction in self.instructions:
            operations.apply(instruction, self.state)

    @classmethod
    def from_lines(cls, lines):
        instructions = [cls._instruction_class.from_str(line) for line in lines]
        return cls(instructions)

def get_validity_matrix(bnas: list):
    n_ops = len(operations.opnames)
    validity = np.ones((len(bnas), n_ops, n_ops), dtype=np.bool8)
    for j, bna in enumerate(bnas):
        validity[j, bna.instruction.opcode] = False  # 16 falses
        for valid_op in bna.valid_ops:
            validity[j, bna.instruction.opcode, operations.opnames.index(valid_op)] = True
    validity = np.all(validity, axis=0)
    return validity

def reduce_validity(validity: np.ndarray):
    n_ops = len(operations.opnames)
    while np.sum(validity) > n_ops:
        for j in range(n_ops):
            if np.sum(validity[j]) == 1:
                k = np.argwhere(validity[j])[0, 0]
                _logger.debug("Setting column {} to False except row {}".format(k, j))
                validity[:, k] = False
                validity[j, k] = True
        if not np.all(np.any(validity, axis=1)):
            return None
        for j in range(n_ops):
            if np.sum(validity[j]) == 1:
                continue
            for k in range(n_ops):
                if not validity[j, k]:
                    continue
                _logger.debug("Attempting reduction by setting row {} to {}".format(j, k))
                validity_temp = validity.copy()
                validity_temp[j] = False
                validity_temp[j, k] = True
                validity_reduced = reduce_validity(validity_temp)
                if validity_reduced is not None:
                    _logger.debug("Reduction by setting row {} to {} succeeded".format(j, k))
                    validity = validity_reduced
                    break
                _logger.debug("Reduction by setting row {} to {} failed".format(j, k))
            else:
                continue
            break
    return validity

def update_op_names(validity: np.ndarray):
    idxs = [np.argwhere(row)[0, 0] for row in validity]
    operations.opnames = tuple(operations.opnames[j] for j in idxs)
    _logger.debug("Opnames: {}".format(", ".join(operations.opnames)))

class Solution(_common.InputLinesSolution):  # TODO: unit-test, document
    def __init__(self):
        super().__init__()
        self.bnas = None

    def part_1(self):
        self.bnas = [BeforeNAfter.from_lines(self.items[j:j + 4]) for j in range(0, 3244, 4)]
        return len([bna for bna in self.bnas if len(bna.valid_ops) > 2])

    def part_2(self):
        validity = get_validity_matrix(self.bnas)
        _logger.debug("Initial validity matrix:\n{}".format(validity.astype(np.uint8)))

        validity = reduce_validity(validity.copy())
        assert np.array_equal(
            np.sum(validity, axis=1),
            np.ones((validity.shape[0],), dtype=np.uint8))
        assert np.array_equal(
            np.sum(validity, axis=0),
            np.ones((validity.shape[1],), dtype=np.uint8))
        _logger.debug("Reduced validity matrix:\n{}".format(validity.astype(np.uint8)))

        update_op_names(validity)

        program = Program.from_lines(self.items[3246:])
        program.run()
        _logger.debug("Final program state: {}".format(program.state))
        return program.state[0]

main = Solution.main
if __name__ == "__main__":  # pragma: no cover
    main()


def calc(*args):
    result = 0
    for arg in args:
        if type(arg) in (int, float):
            result += arg
    return result

# TypeError: calc() got an unexpected keyword argument 'a'
print(calc(a=1, b=2, c=3))


__author__ = 'abhijeet'

import re
import urlparse as up

class Canonicalizer(object):

    def __init__(self):
        # nothing here for now
        pass

    # function to canonicalize urls
    # INPUT:  url -> the current out link which will be canonicalized
    #         current_url -> the current url being crawled
    # OUTPUT: url -> canonicalized url
    '''
    1. Convert the scheme and host to lower case:
        HTTP://www.Example.com/SomeFile.html -> http://www.example.com/SomeFile.html
    2. Remove port 80 from http URLs, and port 443 from HTTPS URLs:
        http://www.example.com:80 -> http://www.example.com
    3. Make relative URLs absolute: if you crawl http://www.example.com/a/b.html and
        find the URL ../c.html -> http://www.example.com/c.html.
    4. Remove the fragment, which begins with #:
        http://www.example.com/a.html#anything -> http://www.example.com/a.html
    5. Remove duplicate slashes:
        http://www.example.com//a.html -> http://www.example.com/a.html'''
    @staticmethod
    def canonicalize(url, current_url):
        url = Canonicalizer.absolute_url(url, current_url)
        parsed_url = up.urlparse(url)
        new_scheme = parsed_url[0].lower()           # converting scheme to lowercase
        new_host_url = parsed_url[1].lower()         # converting netloc (host url) to lowercase
        port_list = re.findall(r':[0-9]+',new_host_url)
        for port_string in port_list:
            new_host_url = new_host_url.replace(port_string,"")

        new_path = parsed_url[2].replace("//","/") # replacing '//' with '/' in path
        new_params = ""                             # getting rid of params
        new_query = ""                              # getting rid of queries
        new_fragment = ""                           # getting rid of fragments
        new_parsed_url = (new_scheme, new_host_url, new_path, new_params, new_query, new_fragment)
        url = up.urlunparse(new_parsed_url)         # putting it back together
        return url

    # function to make partial urls absolute
    # INPUT:  url -> the current out link
    #         current_url -> the current url being crawled
    # OUTPUT: url -> absolute url
    # EXAMPLES:
    # absolute_url('/wiki/World_War_II','http://www.wikipedia.com') ->
    # 'http://www.wikipedia.com/wiki/World_War_II'
    @staticmethod
    def absolute_url(url, current_url):
        url = up.urljoin(current_url, url)
        return url


from django.db import models
from django.contrib.auth.models import AbstractUser
from django.core.validators import RegexValidator
from django.contrib.auth.models import User

from .model_base import CoordBaseModel

class Profile(CoordBaseModel):
    """Profile model.
    A profile holds a user's public data like biography, picture,
    and statistics.
    """

    user = models.ForeignKey(User, on_delete=models.CASCADE)
    date_joined = models.DateTimeField(auto_now_add=True)
    updated_on = models.DateTimeField(auto_now=True)
    semester = models.TextField(blank=True,null=True)
    picture = models.ImageField(
        'profile picture',
        upload_to='users/pictures/',
        blank=True,
        null=True
        )
    biography = models.TextField(max_length=500, blank=True)

    def __str__(self):
        """Return username."""
        return str(self.user)


from algo_profiler import Profiler

from linear_complexity import increment_by_one

def data_gen(list_length):
    numbers_list = [number for number in range(list_length)]
    return {"numbers_list": numbers_list}

profiler = Profiler()

profiler.run_time_analysis(
    func=increment_by_one,
    data_gen=data_gen,
    gen_min_arg=100,
    gen_max_arg=100000,
    gen_steps=10,
    iterations=1000,
    find_big_o=True
)


import pytest
import progressbar

def test_multi_progress_bar_out_of_range():
    widgets = [
        progressbar.MultiProgressBar('multivalues'),
    ]

    bar = progressbar.ProgressBar(widgets=widgets, max_value=10)
    with pytest.raises(ValueError):
        bar.update(multivalues=[123])

    with pytest.raises(ValueError):
        bar.update(multivalues=[-1])

def test_multi_progress_bar_fill_left():
    import examples
    return examples.multi_progress_bar_example(False)


#!/usr/bin/env python3
import sys
sys.path.append('.')
import pychains.chain
import imp
import taintedstr
if __name__ == "__main__":
    arg = sys.argv[1]
    times = int(sys.argv[2]) if len(sys.argv) > 2 else 1
    _mod = imp.load_source('mymod', arg)
    results = []
    for i in range(times):
        e = pychains.chain.Chain()
        (a, r) = e.exec_argument(_mod.main)
        print("Arg:", repr(a), flush=True)
        print("Eval:", repr(r), flush=True)
        taintedstr.reset_comparisons()


#!/usr/bin/env python
import sys
import numpy as np
from netCDF4 import Dataset

with Dataset(sys.argv[1]) as nc1, Dataset(sys.argv[2]) as nc2:
  # Check if the list of variables are the same
  if nc1.variables.keys()!=nc2.variables.keys():
    print("Variables are different")
    sys.exit(2)

  for varname in nc1.variables.keys():
    if varname == "time_iso":
      continue
    # First check if each variable has the same dimension
    if np.shape(nc1[varname][:])!=np.shape(nc2[varname][:]):
      print(varname,"dimension is different")
      sys.exit(2)
    # If dimension is the same, compare data
    else:
      diff = nc2[varname][:]-nc1[varname][:]

      if (np.abs(diff)).max() != 0:
        print(varname,"is different")
        sys.exit(2)


'''
CS5250 Assignment 4, Scheduling policies simulator
Sample skeleton program
Input file:
    input.txt
Output files:
    FCFS.txt
    RR.txt
    SRTF.txt
    SJF.txt
'''
import copy
import sys
from collections import deque

input_file = 'input.txt'

class Process:
    last_scheduled_time = 0

    def __init__(self, id, arrive_time, burst_time, priority=10000):
        self.id = id
        self.arrive_time = arrive_time
        self.burst_time = burst_time
        self.priority = priority  # enrich Process class to keep track of priority

    # for printing purpose
    def __repr__(self):
        return ('[id %d : arrival_time %d,  burst_time %d]'%(self.id, self.arrive_time, self.burst_time))

def FCFS_scheduling(process_list):
    # store the (switching time, proccess_id) pair
    schedule = []
    current_time = 0
    waiting_time = 0
    for process in process_list:
        if(current_time < process.arrive_time):
            current_time = process.arrive_time
        schedule.append((current_time,process.id))
        waiting_time = waiting_time + (current_time - process.arrive_time)
        current_time = current_time + process.burst_time
    average_waiting_time = waiting_time/float(len(process_list))
    return schedule, average_waiting_time

# Input: process_list, time_quantum (Positive Integer)
# Output_1 : Schedule list contains pairs of (time_stamp, proccess_id) indicating the time switching to that proccess_id
# Output_2 : Average Waiting Time
def RR_scheduling(process_list, time_quantum ):
    schedule = []
    num_of_process = len(process_list)
    processes = copy.deepcopy(process_list)
    ready_queue = deque()  # using double ended queue to simulate ready queue under RR scheduling algo
    process_expired_quantum = None  # add process expired its quantum to the very end even to break the tie
    current_time = 0
    waiting_time = 0
    while(1):
        completed = True

        tmp = []
        for process in processes:
            # check arrival time of process, append to ready_queue if it's less than current time
            if process.arrive_time <= current_time:
                ready_queue.append(process)
                tmp.append(process)
        if process_expired_quantum:
            ready_queue.append(process_expired_quantum)

        # remove arrived process from process list, prevent from re-adding
        for arrived_process in tmp:
            processes.remove(arrived_process)

        # there are processes to be execute but yet arrived
        if processes and not ready_queue:
            completed = False
            current_time += 1

        # waiting for process to arrive or complete executing all processes
        running_process = ready_queue.popleft() if ready_queue else None
        if running_process and running_process.burst_time > 0:
            schedule.append((current_time, running_process.id))
            completed = False
            if running_process.burst_time > time_quantum:
                current_time += time_quantum
                running_process.burst_time -= time_quantum
                process_expired_quantum = running_process  # append non-completed process at the end of the queue
            else:
                current_time += running_process.burst_time
                process_expired_quantum = None  # remove completed process from ready queue
                # use original process burst time which is not modified to calculate waiting time
                original_process = next(iter([process for process in process_list if process.id == running_process.id and process.arrive_time == running_process.arrive_time]))
                waiting_time += (current_time - running_process.arrive_time - original_process.burst_time)

        if completed:
            break
    return (schedule, waiting_time/float(num_of_process))

def SRTF_scheduling(process_list):
    ready_queue = deque()
    processes = copy.deepcopy(process_list)
    running_process = None
    schedule = []
    num_of_process = len(process_list)
    current_time = 0
    waiting_time = 0
    while(1):
        # in this task we don't need to handle processes arrive at the same time
        process = [process for process in processes if process.arrive_time == current_time]
        process = next(iter(process)) if process else None

        # waiting for new process to arrive
        if not process and not running_process:
            current_time += 1
        else:
            # initialize the running process as the 1st arrived process
            if not running_process:
                running_process = process
                # used  as flag to write to output file
                # write to output iff a new process start running, either newly arrived or shorter remaining time
                change_of_running_process = True

            # assumption:
            # current running process has the shortest burst time
            # preempt current running process only if the arrived process has smaller burst time than the remaining time of current process
            # if there is no new process comes in, complete current process in the ready queue
            if process and running_process.burst_time <= process.burst_time:
                if running_process.id != process.id:  # special handling for the first process
                    ready_queue.append(process)
                    change_of_running_process = False
            if process and running_process.burst_time > process.burst_time:
                preempted_process = running_process  # preempt running process, append to the end of the queue
                ready_queue.append(preempted_process)
                running_process = process
                change_of_running_process = True

            # write to output only if running process changed to another process
            if change_of_running_process:
                schedule.append((current_time, running_process.id))
            current_time += 1
            running_process.burst_time -= 1

        if running_process and running_process.burst_time == 0:
            # use original process burst time which is not modified to calculate waiting time
            original_process = next(iter([process for process in process_list if process.id == running_process.id and process.arrive_time == running_process.arrive_time]))
            waiting_time += (current_time - running_process.arrive_time - original_process.burst_time)
            processes.remove(running_process)  # remove process from list once completed
            # select the process with the shortest burst time to run
            if not ready_queue:
                running_process = None
            else:
                # sort in ascending burst time
                ready_queue = deque(sorted(ready_queue, key=lambda p: p.burst_time, reverse=False))
                running_process = ready_queue.popleft()
                change_of_running_process = True
        else:
            change_of_running_process = False

        completed = True if not running_process and not processes else False
        if completed:
            break

    return (schedule, waiting_time/float(num_of_process))

def SJF_scheduling(process_list, alpha):
    processes = copy.deepcopy(process_list)
    num_of_processes = len(processes)
    ready_queue = deque()
    schedule = []
    current_time = 0
    waiting_time = 0
    future_predict = {}
    while(1):
        tmp = []
        for process in processes:
            # add process with calculated priority to ready queue if arrived
            if process.arrive_time <= current_time:
                if process.id not in future_predict:
                    process.priority = 5  # use initial guess for new process
                    future_predict[process.id] = {"last_prediction": 5}
                else:
                    priority = alpha * future_predict[process.id]["last_actual_burst"] + (1 - alpha) * future_predict[process.id]["last_prediction"]
                    process.priority = priority
                    future_predict[process.id]["last_prediction"] = priority
                tmp.append(process)
                # append new process to ready queue and sort
                # ensure the left most process in the queue has the highest priority
                ready_queue.append(process)
                ready_queue = deque(sorted(ready_queue, key=lambda x: x.priority))

        for process in tmp:
            processes.remove(process)

        running_process = ready_queue.popleft() if ready_queue else None
        if running_process:
            # current running process has the shortest burst time based on prediction
            # other processes have to wait until it's completed
            # behave like FCFS here
            schedule.append((current_time, running_process.id))
            current_time += running_process.burst_time
            waiting_time += (current_time - running_process.arrive_time - running_process.burst_time)
            future_predict[running_process.id]["last_actual_burst"] = running_process.burst_time  # update param for next prediciton

        # waiting for new process to arrive
        if processes and not running_process:
            current_time += 1

        completed = True if not processes and not ready_queue else False
        if completed:
            break

    return (schedule, waiting_time/float(num_of_processes))

def read_input():
    result = []
    with open(input_file) as f:
        for line in f:
            array = line.split()
            if (len(array)!= 3):
                print ("wrong input format")
                exit()
            result.append(Process(int(array[0]),int(array[1]),int(array[2])))
    return result

def write_output(file_name, schedule, avg_waiting_time):
    with open(file_name,'w') as f:
        for item in schedule:
            f.write(str(item) + '\n')
        f.write('average waiting time %.2f \n'%(avg_waiting_time))

def main(argv):
    process_list = read_input()
    print ("printing input ----")
    for process in process_list:
        print (process)

    print ("simulating FCFS ----")
    FCFS_schedule, FCFS_avg_waiting_time =  FCFS_scheduling(process_list)
    write_output('FCFS.txt', FCFS_schedule, FCFS_avg_waiting_time )

    print ("simulating RR ----")
    RR_schedule, RR_avg_waiting_time =  RR_scheduling(process_list,time_quantum = 2)
    write_output('RR.txt', RR_schedule, RR_avg_waiting_time )

    print ("simulating SRTF ----")
    SRTF_schedule, SRTF_avg_waiting_time =  SRTF_scheduling(process_list)
    write_output('SRTF.txt', SRTF_schedule, SRTF_avg_waiting_time )

    print ("simulating SJF ----")
    SJF_schedule, SJF_avg_waiting_time =  SJF_scheduling(process_list, alpha = 0.2)
    write_output('SJF.txt', SJF_schedule, SJF_avg_waiting_time )

if __name__ == '__main__':
    main(sys.argv[1:])


import cadquery as cq
import streamlit as st
import os
import time
from uuid import uuid4

def stl_preview(color, render):
    # Load and embed the JavaScript file
    with open("js/three.min.js", "r") as js_file:
        three_js = js_file.read()

    with open("js/STLLoader.js", "r") as js_file:
        stl_loader = js_file.read()

    with open("js/OrbitControls.js", "r") as js_file:
        orbital_controls = js_file.read()

    with open("js/stl-viewer.js", "r") as js_file:
        stl_viewer_component = (
            js_file.read()
            .replace('{__REPLACE_COLOR__}',f'0x{color[1:]}')
            .replace('{__REPLACE_MATERIAL__}',render)
        )

    session_id = st.session_state['session_id']
    st.components.v1.html(
        r'
'+
        r''+
        r'<stl-viewer model="./app/static/model_' + str(session_id) + '.stl?cache='+str(time.time())+r'"></stl-viewer>'+
        r'
',
        height = 500
        )

def letter(let, angle, fontPath=""):
    """Extrude a letter, center it and rotate of the input angle"""
    wp = (cq.Workplane('XZ')
        .text(let, fontsize, extr, fontPath=fontPath, valign='bottom')
        )
    b_box = wp.combine().objects[0].BoundingBox()
    x_shift = -(b_box.xlen/2 + b_box.xmin )
    wp = (wp.translate([x_shift,extr/2,0])
        .rotate((0,0,0),(0,0,1),angle)
        )
    return wp

def dual_text(text1, text2, fontPath='', save='stl', b_h=2, b_pad=2, b_fil_per=0.8, space_per=0.3, export_name='file'):
    """Generate the dual letter illusion from the two text and save it"""
    space = fontsize*space_per # spece between letter
    res = cq.Assembly() # start assembly
    last_ymax = 0
    for ind, ab in enumerate(zip(text1, text2)):
        try:
            a = letter(ab[0], 45, fontPath=fontPath)
            b = letter(ab[1], 135, fontPath=fontPath,)
            a_inter_b = a & b
            b_box = a_inter_b.objects[0].BoundingBox()
            a_inter_b = a_inter_b.translate([0,-b_box.ymin,0])
            if ind:
                a_inter_b = a_inter_b.translate([0,last_ymax + space,0])
            last_ymax = a_inter_b.objects[0].BoundingBox().ymax
            res.add( a_inter_b) # add the intersection to the assebmly
        except:
            last_ymax += space*1.5

    b_box = res.toCompound().BoundingBox() # calculate the bounding box
    # add the base to the assembly
    res.add(cq.Workplane()
            .box(b_box.xlen+b_pad*2, b_box.ylen+b_pad*2, b_h, centered=(1,0,0))
            .translate([0, -b_pad, -b_h])
            .edges('|Z')
            .fillet(b_box.xlen/2*b_fil_per)
            )
    # convert the assemly toa shape and center it
    res = res.toCompound()
    res = res.translate([0, -b_box.ylen/2,0])
    # export the files
    cq.exporters.export(res, f'./app/static/model_{session_id}.stl')
    cq.exporters.export(res, f"{export_name}.{save}")

if __name__ == "__main__":
    # initialize session id
    if "session_id" not in st.session_state:
        st.session_state['session_id'] = uuid4()
    session_id = st.session_state['session_id']

    for file in os.listdir():
        if 'file' in file:
            try:
                os.remove(file)
            except:
                print(f'Cannot remove file {file}')

    st.title('TextTango: dual text illusion')
    st.write("Generate a custom dual letter illusion, a 3d ambigram! If you like the project put a like on [Printables](https://www.printables.com/it/model/520333-texttango-dual-letter-illusion) or [support me with a coffee](https://www.paypal.com/donate/?hosted_button_id=V4LJ3Z3B3KXRY)! On Printables you can find more info about the project.", unsafe_allow_html=True)
    st.write("The Web App supports all [Google Fonts](https://fonts.google.com/), check them out!", unsafe_allow_html=True)

    col1, col2, col3 = st.columns(3)
    # Input type
    with col1:
        text1 = st.text_input('First text', value="STOP")
    with col2:
        text2 = st.text_input('Second text', value="WORK")
    with col3:
        fontsize = st.number_input('Font size', min_value=1, max_value=None, value=20)
        extr = fontsize*2 # extrude letter

    if len(text1) != len(text2):
        st.warning("The two texts don't have the same length, letters in excess will be cut", icon="тЪая╕П")
    if not text1.isupper() or not text2.isupper():
        st.warning("Non-capital letters with different height lead to bad results", icon="тЪая╕П")

    col1, col2, col3 = st.columns(3)
    # Input type
    font_dir = os.listdir('fonts')
    with col1:
        font_name = st.selectbox('Select font', ['lato'] + sorted(font_dir))
    with col2:
        font_type_list = [f for f in os.listdir('fonts' + os.sep + font_name) if '.ttf' in f and '-' in f]
        # font with explicit type (-bold, -regular, ...)
        if font_type_list:
            font_start_name = font_type_list[0].split('-')[0]
            font_type_list_name = [f.split('-')[1].strip('.ttf') for f in font_type_list]
            font_type = st.selectbox('Font type', sorted(font_type_list_name))
            font_type_pathname = font_start_name + '-' + font_type + '.ttf'
            font_path = 'fonts' + os.sep + font_name + os.sep + font_type_pathname
        else: # font without explicit type
            font_type_list = [f for f in os.listdir('fonts' + os.sep + font_name) if '.ttf' in f]
            font_type = st.selectbox('Font type', sorted(font_type_list))
            font_path = 'fonts' + os.sep + font_name + os.sep + font_type
    with col3:
        space = st.slider('Letters space (%)', 0, 200, step=1, value=30) / 100
    # base parameters
    col1, col2, col3 = st.columns(3)
    with col1:
        b_h = st.slider('Base height', 0.0, fontsize/2, step=0.1, value=1.0)
    with col2:
        b_pad = st.slider('Base Padding', 0.0, fontsize/2, step=0.1, value=2.0)
    with col3:
        b_fil_per = st.slider('Base fillet (%)', 0, 100, step=1, value=80) / 100

    col1, _, _ = st.columns(3)
    with col1:
        out = st.selectbox('Output file type', ['stl', 'step'])

    if st.button('Render'):
        start = time.time()
        with st.spinner('Wait for it...'):
            dual_text(text1, text2, fontPath=font_path, save=out, b_h=b_h, b_pad=b_pad, b_fil_per=b_fil_per, space_per=space)
        end = time.time()
        if f'file.{out}' not in os.listdir():
            st.error('The program was not able to generate the mesh.', icon="ЁЯЪи")
        else:
            st.success(f'Rendered in {int(end-start)} seconds', icon="тЬЕ")
            with open(f'file.{out}', "rb") as file:
                btn = st.download_button(
                        label=f"Download {out}",
                        data=file,
                        file_name=f'TextTango_{text1}_{text2}.{out}',
                        mime=f"model/{out}"
                    )
            st.markdown("Post the make [on Printables](https://www.printables.com/it/model/520333-texttango-dual-letter-illusion) to support the project!", unsafe_allow_html=True)
            st.markdown("I am a student who enjoys 3D printing and programming. To support me with a coffee, just [click here!](https://www.paypal.com/donate/?hosted_button_id=V4LJ3Z3B3KXRY)", unsafe_allow_html=True)

            # stl preview
            stl_preview('#696969', "material")


# coding=utf-8

import sys
import numpy as np
import pandas
from sklearn.metrics import roc_auc_score, roc_curve

Ch_pred1 = str(sys.argv[1])
Ch_pred2 = str(sys.argv[2])
Labels = str(sys.argv[3])
weights = str(sys.argv[4])

# load dataset
dataframe_P1 = pandas.read_csv(Ch_pred1, header=None)
Prediction_1 = dataframe_P1.values

dataframe_P2 = pandas.read_csv(Ch_pred2, header=None)
Prediction_2 = dataframe_P2.values

dataframe_Y = pandas.read_csv(Labels, header=None)
Y = dataframe_Y.values

dataframe_weights = pandas.read_csv(weights, header=None)
weights_Y = dataframe_weights.values

print(Prediction_1[0])
print(Prediction_2[0])
print(Y[0])

Prediction1_AUC = np.copy(Prediction_1)
Prediction2_AUC = np.copy(Prediction_2)

for i in range(len(Prediction_1)):
	Prediction1_AUC[i]= Prediction_1[i]*2-1
	Prediction2_AUC[i]= Prediction_2[i]*2-1

c, r = Y.shape
Y_AUC = Y.reshape(c,)

# Statistiques
# Précision des classifieurs :

AUC1 = roc_auc_score(Y_AUC, Prediction1_AUC, average='macro', sample_weight=weights_Y)
print("L'AUC du modèle 1 est de : "+str(AUC1))

Vrais_positifs1 = 0.0
Vrais_negatifs1 = 0.0
total_positifs1 = 0.0

Vrais_positifs2 = 0.0
Vrais_negatifs2 = 0.0
total_positifs2 = 0.0

for i in range(len(Y)):
	if (Y[i] == 1):
		total_positifs1 += 1
		if (Prediction_1[i]>0.5):
			Vrais_positifs1 += 1
	if (Y[i] == 0 and Prediction_1[i] < 0.5):
		Vrais_negatifs1 += 1

Vrais_negatifs1 = Vrais_negatifs1 / (len(Y)-total_positifs1)
Vrais_positifs1 = Vrais_positifs1 / (total_positifs1)

print("Le premier classifieur prédit justement la nature de "+str(Vrais_positifs1)+" des elements de la classe+")
print("Le premier classifieur prédit justement la nature de "+str(Vrais_negatifs1)+" des elements de la classe- \n")

AUC2 = roc_auc_score(Y_AUC, Prediction2_AUC, average='macro', sample_weight=weights_Y)
print("L'AUC du modèle 2 est de : "+str(AUC2))

for i in range(len(Y)):
	if (Y[i] == 1):
		total_positifs2 += 1
		if (Prediction_2[i]>0.5):
			Vrais_positifs2 += 1
	if (Y[i] == 0 and Prediction_2[i] < 0.5):
		Vrais_negatifs2 += 1

Vrais_negatifs2 = Vrais_negatifs2 / (len(Y)-total_positifs2)
Vrais_positifs2 = Vrais_positifs2 / (total_positifs2)

print("Le premier classifieur prédit justement la nature de "+str(Vrais_positifs2)+" des elements de la classe+")
print("Le premier classifieur prédit justement la nature de "+str(Vrais_negatifs2)+" des elements de la classe- \n")

# Pourcentage de la base sur laquelle les classifieurs sont d'accords :
en_accords = 0.0

for i in range(len(Y)):
	if ((Prediction_1[i]<0.5 and Prediction_2[i]<0.5) or (Prediction_1[i]>=0.5 and Prediction_2[i]>=0.5)):
		en_accords += 1

en_accords /= len(Y)
en_desaccords = 1 - en_accords

print("Les deux classifieurs sont en accord sur "+str(en_accords)+" de la base. \n")

# Pourcentage de la base ou au moins l'un des classifieurs a raison :

un_a_raison = 0.0

for i in range(len(Y)):
	if (abs(Prediction_1[i]-Y[i])<0.5 or abs(Prediction_2[i]-Y[i])<0.5):
		un_a_raison += 1

un_a_raison /= len(Y)

print("Sur "+str(un_a_raison)+" de la base, au moins l'un des classifieurs à raison.\n")

# Pourcentage de la base sur lequel le classifieur 1 a raison, mais pas le classifieur 2

un_seul_a_raison = 0.0

for i in range(len(Y)):
	if (abs(Prediction_1[i]-Y[i])<0.5 and abs(Prediction_2[i]-Y[i])>0.5):
		un_seul_a_raison += 1

un_seul_a_raison /= (en_desaccords*len(Y))

print(str(un_seul_a_raison)+": Pourcentage des évenements douteux ou seul le classifieur 1 a raison.\n")

# 5 exemples ou seul le classifieur un a raison.
print("5 exemples ou seul le classifieur un a raison.")
k = 0
for i in range(len(Y)):
	if (abs(Prediction_1[i]-Y[i])<0.5 and abs(Prediction_2[i]-Y[i])>0.5):
		print("Classifieur 1 : "+str(Prediction_1[i])+"\nClassifieur 2 : "+str(Prediction_2[i])+"\n")
		k = k + 1
		if (k >= 5):
			break

# 5 exemples ou seul le classifieur deux a raison.
print("5 exemples ou seul le classifieur deux a raison.")
k = 0
for i in range(len(Y)):
	if (abs(Prediction_1[i]-Y[i])>0.5 and abs(Prediction_2[i]-Y[i])<0.5):
		print("Classifieur 1 : "+str(Prediction_1[i])+"\nClassifieur 2 : "+str(Prediction_2[i])+"\n")
		k = k + 1
		if (k >= 5):
			break

# Let's try to combine these two models

max_p1 = max(Prediction_1)
max_p2 = max(Prediction_2)

for i in range(len(Prediction_1)):
	Prediction_1[i]=Prediction_1[i]/max_p1
	Prediction_2[i]=Prediction_2[i]/max_p2

Prediction_3 = (Prediction_1+Prediction_2)/2

Prediction3_AUC = np.copy(Prediction_3)

np.savetxt("combi.csv", Prediction_3, delimiter=",")

for i in range(len(Prediction_3)):
	Prediction3_AUC[i]= Prediction_3[i]*2-1

AUC3 = roc_auc_score(Y_AUC, Prediction3_AUC, average='macro', sample_weight=weights_Y)
print("L'AUC de la combinaison est de : "+str(AUC3))

print("voulez vous conserver les prédictions du nouveau modèle ? (o/n)")
R_user = raw_input()
if (R_user == 'o'):
	print("Nom du fichier ?")
	nom = raw_input()
	np.savetxt(nom+'.csv', Prediction_3, delimiter=",")


from Agenda import Agenda
a1=Agenda()

while True:
    op=input('QUE DESEA HACER?\n1-AÑADIR CONTACTO\n2-LISTA CONTACTOS\n3-BUSCAR CONTACTO\n4-EDITAR CONTACTO\n5-CERRAR AGENDA\nOPCION: ')

    if op=='1':
        a1.añadirContactos()

    elif op=='2':
        a1.listaContactos()
    elif op=='3':
        a1.buscarContacto()
    elif op=='4':
        a1.editarContacto()
    elif op=='5':
        print('cerrando agenda....')
        break


# 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, see <http://www.gnu.org/licenses/>.

import coire

# Iarnrod Tuning options
Throttling = "True" # Limit the burden on the API, throttle the connections
PollIntervalMinutes = 5 # This is the status polling interval in minutes
StatusOutput = "small" # You can set this to small or large and it will change the output format
TFileName = '/tmp/iarn-i3-temp' # Where to store the timestamp file for poll throttling
SFileName = '/tmp/iarn-i3-stat' # Where to store the cache of the line status

# Parse the command line arguments
Line = coire.ParseArgs()

# Throttling
Run = coire.Throttle(PollIntervalMinutes,Throttling,TFileName)

# Gather the line status data
LineStatusData = coire.RetrieveTFLData(Line,Run,SFileName)

# Generate the status output and print
if (StatusOutput == "small") and (LineStatusData == "Good Service"):
    print ("TFL " + Line[0] + Line[1] + Line[2] + " OK")
elif StatusOutput == "small":
    print ("TFL " + Line[0] + Line[1] + Line[2] + " " + LineStatusData)
else:
    print ("TFL " + Line + " line has " + LineStatusData)


import os
import sys
import csv
import re
import requests
import getopt
import json
import operator
import numpy as np
import pandas as pd
import seaborn as sb
import matplotlib.pyplot as plt
from collections import Counter
from pandas import DataFrame as df
from matplotlib import rcParams
from matplotlib.lines import Line2D

# function retrieve GOnumber ('GO:0034219') from GOterm ('transmembrane transport')
# parameters: go_term (str)
# return go_number (str)
def get_GOterm_to_GOnumber(go_term):
    quickgo_url="https://www.ebi.ac.uk/QuickGO/services/ontology/go/search?query="\
                +go_term+\
                "&limit=25&page=1"
    req = requests.get(quickgo_url, headers={ "Accept" : "application/json"})
    try:
        page1 = req
    except requests.exceptions.ConnectionError:
        r.status_code = "Connection refused"

    if not req.ok:
        req.raise_for_status()
        sys.exit()
    json_data = json.loads(req.text)
    for jdata in json_data["results"]:
        go_number = str(jdata["id"])
    return go_number

# function: get a list of ancestors GO (['GO:1902494', 'GO:1902495',..., 'GO:0071944'])
#           given a GOnumber ('GO:0043190') using EMBL-EBI's QuickGO browser
# parameter: go_number (str)[a valid Gene Ontology ID, e.g. GO:0048527]
# return: all ancestors (list)
def get_ancestors(go_number):
	number=go_number[3:] # to remove "GO:" and use only the number
	quickgo_url='https://www.ebi.ac.uk/QuickGO/services/ontology/go/terms/GO%3A'\
	            +number+\
	            '/ancestors?relations=is_a%2Cpart_of%2Coccurs_in%2Cregulates'
	req = requests.get(quickgo_url, headers={ "Accept" : "application/json"})
	if not req.ok:
		req.raise_for_status()
		sys.exit()
	json_data = json.loads(req.text)

	go_ancestors = []
	for k_jdata,v_jdata in json_data.items():
		if k_jdata == "results":
			for v_jd in v_jdata:
				for k_dic,v_dic in v_jd.items():
					if k_dic == "ancestors":
						for go in v_dic:
							go_ancestors.append(go)
	return go_ancestors

# function: find the GOs in raw data (output of the program GO)
# parameter: raw string that contain GOs, example: 'F:GO:0003924; C:GO:0005840'
# return: list of GOnumbers
def findall_GOs(string):
    raw_string_gos = re.findall('GO:(\d+)',string)
    list_go_numbers =[]
    for go in raw_string_gos:
        go_find = 'GO:'+go
        list_go_numbers.append(go_find)
    return list_go_numbers

# function: find the term GOs in raw data (output of the program GO)
# input : string that contein GO terms. example.: 'P:transmembrane transport; P:deoxyribonucleoside monophosphate biosynthetic process; P:transport'
def findall_terms(string):
	raw_string_terms = string.split(";")
	list_terms =[]
	for i in raw_string_terms:
		term = re.findall(':\s*([^.]+|\S+)',i)
		for t in term:
			list_terms.append(t)
	return list_terms

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

txt_input = sys.argv[1]
if os.path.isfile(txt_input):
	if txt_input.endswith('txt'):
		print("--> working in ",txt_input)
		csv_output= txt_input[:-4]+'_out.csv'
		png_output= txt_input[:-4]+'_out.png'
	else:
		print('This file (',txt_input,') is not txt file! Review your file')
		sys.exit()
else:
	print('The file ',txt_input,'not exists')
	sys.exit()

#step_0 : Finding the columns with GOnumbers or GOterms
print('### FINDING THE COLUMNS WITH GOnumbers and/or GOterms ###')
# to identify the headers that contain the GOnumbers we will use regular expressions
# and they will be store in:
headers_GOs = []
# to indetify the header that contain the GOterm we will use this list
# and tehy will be store in:
posibles_headers = ['GO Names list','GO Names','Annotation GO Term','InterPro GO Term','InterPro GO Names']
headers_terms = []
with open(txt_input,'r') as my_data:
	#making dictionary with all raw data
	readrows = csv.DictReader(my_data,delimiter='\t',skipinitialspace=True)
	data = {name:[] for name in readrows.fieldnames}
	for row in readrows:
		for header in readrows.fieldnames:
			data[header].append(row[header])

	for head, rows in data.items():
		if len(head) != 0 and len(rows) != 0:
			for r in rows:
				regex_GO = re.findall('.*GO:.*',r)
				if regex_GO:
					headers_GOs.append(head)
				else:
					if head in posibles_headers:
						headers_terms.append(head)

headers_GOnumber_GO_term = [list(set(headers_GOs)),list(set(headers_terms))]
headers_GOnumber_GO_term = [l for l in headers_GOnumber_GO_term if l != []] # Removing the empty lists

number_col = []
if len(headers_GOnumber_GO_term) == 0:
	print("It was not possible to find any column with GOnumber or GOterms")
	sys.exit()
else:
	if len(headers_GOnumber_GO_term) == 2:
		columns_selected = headers_GOnumber_GO_term[0] #chose the GOnumber colums
		print('This file contain GO codes in:')
		key_list = list(data)
		keys_of_interest = headers_GOnumber_GO_term[0]
		for key in keys_of_interest:
			number_col.append(key_list.index(key))
		number_col.insert(0,'GO codes')
		for n in sorted(number_col[1:]):
			print("Column ",n)
	if len(headers_GOnumber_GO_term) == 1:
		columns_selected = headers_GOnumber_GO_term[0]
		print('This file contain GO terms in:')
		key_list = list(data)
		keys_of_interest = headers_GOnumber_GO_term[0]
		for key in keys_of_interest:
			number_col.append(key_list.index(key))
		number_col.insert(0,'GO terms')
		for n in  sorted(number_col[1:]):
			print("Column ",n)

key_list = list(data)
for n,i in enumerate(key_list):
	if i == 'SeqName':
		SeqName_column = n

#step_1: geting all GOs_IDs
#        or getting GO_IDs from GO_terms
print("#### MAKING A LIST WITH UNIQUES GOs OF THE ROW DATA ###")

list_GO = []
no_retrieve = []
no_InterPro = []
with open(txt_input,'r') as my_data:
    readrows = csv.reader(my_data,delimiter='\t')
    for rw in readrows:
    	if number_col[0] == 'GO codes':
    		column = sorted(number_col[1:])
	    	regex = re.findall('GO:',rw[column[0]])
	    	if regex:
	    		GOs_retrieve = findall_GOs(rw[column[0]])
	    		for i in GOs_retrieve:
	    			list_GO.append(i)
	    	else:
	    		if len(column) == 2:
		    		no_retrieve.append(rw[SeqName_column])
		    		GOs_retrieve = findall_GOs(rw[column[1]])
		    		if len(GOs_retrieve)!=0:
		    			for i in GOs_retrieve:
		    				list_GO.append(i)
		    		else:
		    			no_InterPro.append(rw[SeqName_column])

with open(txt_input,'r') as my_data:
    readrows = csv.reader(my_data,delimiter='\t')
    for rw in readrows:
    	if number_col[0] == 'GO terms':
    		column = sorted(number_col[1:])
    		list_term  = []
    		rows_string = str(rw[column[0]])
    		term_retrieve = findall_terms(rows_string)
    		if len(term_retrieve) != 0:
    			for tr in term_retrieve:
    				list_term.append(tr)
    		else:
    			if len(column) == 2:
    				no_retrieve.append(rw[SeqName_column])
    				rows_string = str(rw[column[1]])
    				term_retrieve = findall_terms(rows_string)
    				if len(term_retrieve) != 0:
    					for tr in term_retrieve:
    						list_term.append(tr)
    				else:
    					no_InterPro.append(rw[SeqName_column])

    		for my_term in list_term:
    			my_request = get_GOterm_to_GOnumber(my_term)
    			list_GO.append(my_request)

print("SeqName without principal GO IDs found:")
if len(no_retrieve[1:]) != 0:
	for n in no_retrieve[1:]:
		print(n)
else:
	print("0")
print("\nTrying to find in InterPro ...")
print("SeqName without InterPro GO IDs found:")

if len(no_InterPro[1:]) != 0:
	for n in no_InterPro[1:]:
		print(n)
else:
	print("0")

lista_GO_uniques_pre = set(list_GO)

#step_2: building a dic and buildind a list of GO ids
print("#### BUILDING A DICTIONARY 2ND LEVEL ###")
dict_2nd_GO_term = {}
data_from_dic = csv.reader(open('dictionary_2_level.csv'),delimiter=",")
for row in data_from_dic:
	if row[4] == "is_a":
		k,v = row[2],row[3]
		dict_2nd_GO_term[k]=v

my_dic_2nd_nivel = dict_2nd_GO_term#building_dic("dictionary_2_level.csv")
list_GOs_dic = []
for k,v in my_dic_2nd_nivel.items():
	list_GOs_dic.append(k)

print("#### GETING ANCESTOR TO EACH GO ###")
lista_GO_uniques_no_retrieve_ancestral = []
lista_GO_uniques =[]
for _GO in lista_GO_uniques_pre:
	if _GO in list_GOs_dic:
		lista_GO_uniques_no_retrieve_ancestral.append(_GO)
	else:
		lista_GO_uniques.append(_GO)

lista_term_uniques_no_retrieve_ancestral = []
for i in lista_GO_uniques_no_retrieve_ancestral:
	for k,v in my_dic_2nd_nivel.items():
		if i == k:
			lista_term_uniques_no_retrieve_ancestral.append(v)

print("GOs with existing 2nd-level ancestors:")
for n in lista_GO_uniques_no_retrieve_ancestral:
	print(n)

#step_3: geting ancestors and filtering with the before lis of the dic
my_GO_and_term_ancest_selected = {}
for my_GOs in lista_GO_uniques:
	list_all_ancestors = get_ancestors(my_GOs)
	list_selected_GO_ancestors = []
	for GO_ancest in list_all_ancestors:
		if GO_ancest in list_GOs_dic:
			list_selected_GO_ancestors.append(GO_ancest)

	#step 4: retrieving the term from the selected GO id
	list_selected_GOterm_ancestors = []
	for selected_GO_ancestor in list_selected_GO_ancestors:
		for k,v in my_dic_2nd_nivel.items():
			if selected_GO_ancestor == k:
				list_selected_GOterm_ancestors.append(v)
	my_GO_and_term_ancest_selected[my_GOs]=list_selected_GOterm_ancestors

#step_3: geting a list with all terms
all_terms_pre = []
for k,v in my_GO_and_term_ancest_selected.items():
    for i in v:
        all_terms_pre.append(i)
all_terms = all_terms_pre + lista_term_uniques_no_retrieve_ancestral

#step_4: preparing data calculate frecueny and percentage
data_from_dic = csv.reader(open('dictionary_2_level.csv'),delimiter=",")
dict_categ_2ndterm = {}
for row in data_from_dic:
    k,v = (row[0]+'-'+row[1]),row[3]
    if not k in dict_categ_2ndterm:
        dict_categ_2ndterm[k] = [v]
    else:
        dict_categ_2ndterm[k].append(v)

all_terms_freq = []
for terms,freq in Counter(all_terms).items():
    terms_freq = [terms,int(freq)]
    all_terms_freq.append(terms_freq)

total = []
for i in all_terms_freq:
    total.append(i[1])

list_results = []
for i in all_terms_freq:
    suma = sum(total)
    perc = round(i[1]*100/suma,1)
    for k,v in dict_categ_2ndterm.items():
        if i[0] in v:
            list_results.append([k,i[0],i[1],perc])
list_results = sorted(list_results, key=operator.itemgetter(0))

#step_5: writing in a tabulate file
print("#### WRITING THE RESULTS ###")
final_results = []
for r in list_results:
    col1 = r[0][2:].replace('_', ' ')
    col2 = r[1].replace('_', ' ')
    col3= int(r[2])
    col4= float(r[3])
    final_results.append([col1,col2,col3,col4])

with open(csv_output,"w") as my_out:
    writerow = csv.writer(my_out,delimiter='\t')
    writerow.writerows(final_results)

#step_6: making a graph
print("#### MAKING THE GRAPH ###")
category = []
term_2nd = []
frequenc = []
percenta = []
for finalr in final_results:
    category.append(finalr[0])
    term_2nd.append(finalr[1])
    frequenc.append(finalr[2])
    percenta.append(finalr[3])

sb.set_style('darkgrid')
plt.figure(figsize=(10,.2*len(term_2nd)))

pos = np.arange(len(term_2nd))+.5
color_categories = []
for n in category:
    if n == 'cellular component':
        color_categories.append("#3367e1")
    if n == 'molecular function':
        color_categories.append("#4e904a")
    if n == 'biological process':
        color_categories.append("#e95050")

plt.barh(pos,percenta,color=color_categories, height=0.7,align='center',alpha=0.8)#edgecolor='black'
plt.yticks(pos, term_2nd)
plt.rc('xtick',labelsize=10)
plt.rc('ytick',labelsize=8)
#plt.title("GO funtional analysis")
plt.xlabel('GO distribution by level (2) in percentage')
legend_elements = [Line2D([0], [0], color='#3367e1', lw=4, label='cellular component'),
                   Line2D([0], [0], color='#4e904a', lw=4, label='molecular function'),
                   Line2D([0], [0], color='#e95050', lw=4, label='biological process')]
plt.legend(handles=legend_elements, loc='best')
plt.savefig(png_output, dpi=100,bbox_inches = "tight")

print("#### FINALIZED ###","\nCheck the files: ",csv_output,' and ',png_output)


from dateutil.parser import parse
import xlrd
import os
import datetime
import glob
import pandas as pd
import numpy as np

def is_date(string, fuzzy=False):
    """
    Return whether the string can be interpreted as a date.
    :param string: str, string to check for date
    :param fuzzy: bool, ignore unknown tokens in string if True
    """
    try:
        parse(string, fuzzy=fuzzy)
        return True
    except ValueError:
        return False

def convert_string_to_date(date_string):
    for date_format in ('%Y-%m-%d %H:%M:%S', '%d-%m-%Y', '%d.%m.%Y', '%Y.%m.%d', '%d.%m.%y', '%d/%m/%Y', '%d/%m/%Y %H:%M:%S'):
        try:
            return datetime.datetime.strptime(date_string, date_format)
        except ValueError:
            pass
    raise ValueError('no valid date format found')

def convert_excel_date(excel_book, excel_date):
    ms_bbd_date_number = excel_date
    year, month, day, hour, minute, second = xlrd.xldate_as_tuple(ms_bbd_date_number, excel_book.datemode)
    py_date = datetime.datetime(year, month, day, hour, minute, second)
    return py_date

def main():
    # Change directory
    os.chdir(r"\\192.168.20.50\AlexServer\SD共有\ボタニーパレット\Siwan\StockFiles\working_place")
    #os.chdir('/home/siwanpark/ExcelData/plenus/')
    excel_files = glob.glob('*.xls*')
    print(excel_files)
    for excel_file in excel_files:
        generate_data_frame(excel_file)  #generate data frame
        os.chdir(r"\\192.168.20.50\AlexServer\SD共有\ボタニーパレット\Siwan\StockFiles\working_place")
        #os.chdir('/home/siwanpark/ExcelData/plenus/')

def generate_data_frame(file_path):
    loc = (file_path)
    wb = xlrd.open_workbook(loc)
    sheet = wb.sheet_by_index(0)

    file_name = file_path.split('.')[0]
    product_type = (file_path.split('.')[0]).split('_')[2]  # file_path = 'plenus_dry.xlsx'
    unit = 'ctn'

    stock_list = []
    stock_base_date = datetime.datetime.today().date()
    previous_code = sheet.cell(1, 0).value
    sf_code = previous_code.split('/')[0]
    sf_code = sf_code.strip()
    plenus_code = previous_code.split('/')[1]
    plenus_code = plenus_code.strip()
    product_name = sheet.cell(1, 1).value
    description = sheet.cell(1, 2).value

    i = 1
    while sheet.cell(i, 0).value != 'end':
        if sheet.cell(i, 0).value == previous_code or sheet.cell(i, 0).value == '':
            if sheet.cell(i, 9).value != '': # if stock data found
                bbd, qty = extract_stock_data(sheet.cell(i, 9).value)
                stock_data = create_stock_data(stock_base_date, product_type, sf_code, plenus_code, product_name, description, qty, unit, bbd)
                stock_list.append(stock_data)
        else:
            previous_code = sheet.cell(i, 0).value
            sf_code = previous_code.split('/')[0]
            sf_code = sf_code.strip()
            plenus_code = previous_code.split('/')[1]
            plenus_code = plenus_code.strip()
            product_name = sheet.cell(i, 1).value
            description = sheet.cell(i, 2).value
            if sheet.cell(i, 9).value != '': # if stock data found
                bbd, qty = extract_stock_data(sheet.cell(i, 9).value)
                stock_data = create_stock_data(stock_base_date, product_type, sf_code, plenus_code, product_name, description, qty, unit, bbd)
                stock_list.append(stock_data)

        i += 1

    df_usage = pd.DataFrame(stock_list)
    stock_file_name = file_name + '_processed_stock.xlsx'
    #os.chdir('/home/siwanpark/ExcelData/plenus')
    os.chdir(r"\\192.168.20.50\AlexServer\SD共有\ボタニーパレット\Siwan\StockFiles\working_place\uploading_files")
    df_usage.to_excel(stock_file_name)

def extract_stock_data(input_string):
    #print(input_string)
    input_list = input_string.split()
    stock_qty = input_list[0]
    temp_string = input_list[1]
    bbd = temp_string.split(':')[1]
    stock_bbd = datetime.datetime.strptime(bbd, "%d/%m/%Y")
    #print(type(stock_bbd))

    return stock_bbd, stock_qty

def create_stock_data(stock_base_date, product_type, sf_code, plenus_code, product_name, description, qty, unit, bbd):
    stock_data = {'stock_base_date': stock_base_date, 'product_type': product_type, 'sf_code': sf_code, 'plenus_code': plenus_code,
                  'product_name': product_name, 'description': description, 'qty': qty, 'unit': unit, 'bbd': bbd}
    return stock_data

if __name__ == "__main__":
    main()


class SnapshotUpdater:
	def __init__(self, sourceFolder, destinationFolder, startTime, fileHandler):
		self.sourceFolder = sourceFolder
		self.destinationFolder = destinationFolder
		self.startTime = startTime
		self.fh = fileHandler
		self.previousPeriod = None

	def process(self, period):
		if period.getNumOfSnapshots() > 0:
			periodFolder = self.getPeriodFolder(period)
			if not self.fh.ifFolderExists(periodFolder):
				self.fh.createFolder(periodFolder)
				lastUpdateTime = self.startTime
				if self.previousPeriod == None:
					self.copySource(periodFolder)
			else:
				lastUpdateTime = self.fh.getTimeStamp(periodFolder)
			if (self.startTime - lastUpdateTime) >= period.getPeriodInSeconds():
				self.shiftFolders(periodFolder, period.getNumOfSnapshots())
			self.previousPeriod = period

	def shiftFolders(self, periodFolder, numOfSnapshots):
		entryFolder = self.getEntryFolder(periodFolder, numOfSnapshots -1)
		if self.fh.ifFolderExists(entryFolder):
			self.fh.deleteFolder(entryFolder)
		for i in range(numOfSnapshots -1, 0, -1):
			entrySrc = self.getEntryFolder(periodFolder, i-1)
			entryDest = self.getEntryFolder(periodFolder, i)
			if self.fh.ifFolderExists(entrySrc):
				self.fh.moveFolder(entrySrc, entryDest)
		self.copySource(periodFolder)

	def getPeriodFolder(self, period):
		return self.fh.joinPaths(self.destinationFolder, period.getName())

	def getEntryFolder(self, periodFolder, entryNumber):
		return self.fh.joinPaths(periodFolder,str(entryNumber))

	def copySource(self, periodFolder):
		copySourceFolder = self.sourceFolder
		if self.previousPeriod != None:
			copySourcePeriodFolder = self.getPeriodFolder(self.previousPeriod)
			copySourceFolder = self.getEntryFolder(copySourcePeriodFolder, 0)
		self.fh.touchFolder(periodFolder, self.startTime)
		entryFolder = self.getEntryFolder(periodFolder, 0)
		self.fh.copyFolder(copySourceFolder, entryFolder)


def even_nos(n):
    i=1
    while n:
        yield 2*i
        i+=1
        n-=1

it = even_nos(10)
even_list = []      # Empty list
while True:
    try:
        even_list.append(next(it))
    except StopIteration:
        break

print(even_list)

'''
> A generator-function is defined like a normal function, but whenever it needs to generate a value,
    it does so with the yield keyword rather than return.

> If the body of a def contains yield, the function automatically becomes a generator function.

> Generator functions return a generator object.

> Generator objects are used either by calling the next method on the generator object or
    using the generator object in a “for-in” loop.
'''

'''
Difference between "return" and "yield" keyword :-
> When value gets returned by a return keyword, so along with "value" the "control" also gets returned and the function ends.
> When value gets returned by a yield keyword, "value" gets returned but the function does not ends, it gets paused and resumes
  from the same point.

'''


import os
import filecmp
import difflib
import numpy as np
from skmultiflow.data import RandomTreeGenerator
from skmultiflow.trees import HoeffdingTreeClassifier
from skmultiflow.evaluation import EvaluateHoldout

def test_evaluate_holdout_classifier(tmpdir, test_path):
    # Setup file stream
    stream = RandomTreeGenerator(tree_random_state=23, sample_random_state=12, n_classes=4, n_cat_features=2,
                                 n_num_features=5, n_categories_per_cat_feature=5, max_tree_depth=6, min_leaf_depth=3,
                                 fraction_leaves_per_level=0.15)

    # Setup learner
    nominal_attr_idx = [x for x in range(15, len(stream.feature_names))]
    learner = HoeffdingTreeClassifier(nominal_attributes=nominal_attr_idx)

    # Setup evaluator
    n_wait = 200
    max_samples = 1000
    metrics = ['accuracy', 'kappa', 'kappa_t']
    output_file = os.path.join(str(tmpdir), "holdout_summary.csv")
    evaluator = EvaluateHoldout(n_wait=n_wait,
                                max_samples=max_samples,
                                test_size=50,
                                metrics=metrics,
                                output_file=output_file)

    # Evaluate
    result = evaluator.evaluate(stream=stream, model=learner)
    result_learner = result[0]

    assert isinstance(result_learner, HoeffdingTreeClassifier)

    assert learner.model_measurements == result_learner.model_measurements

    expected_file = os.path.join(test_path, 'holdout_summary.csv')
    compare_files(output_file, expected_file)

    mean_performance, current_performance = evaluator.get_measurements(model_idx=0)

    expected_mean_accuracy = 0.344000
    assert np.isclose(mean_performance.accuracy_score(), expected_mean_accuracy)

    expected_mean_kappa = 0.135021
    assert np.isclose(mean_performance.kappa_score(), expected_mean_kappa)

    expected_mean_kappa_t = 0.180000
    assert np.isclose(mean_performance.kappa_t_score(), expected_mean_kappa_t)

    expected_current_accuracy = 0.360000
    assert np.isclose(current_performance.accuracy_score(), expected_current_accuracy)

    expected_current_kappa = 0.152542
    assert np.isclose(current_performance.kappa_score(), expected_current_kappa)

    expected_current_kappa_t = 0.200000
    assert np.isclose(current_performance.kappa_t_score(), expected_current_kappa_t)

    expected_info = "EvaluateHoldout(batch_size=1, dynamic_test_set=False, max_samples=1000,\n" \
                    "                max_time=inf, metrics=['accuracy', 'kappa', 'kappa_t'],\n" \
                    "                n_wait=200,\n" \
                    "                output_file='holdout_summary.csv',\n" \
                    "                restart_stream=True, show_plot=False, test_size=50)"
    assert evaluator.get_info() == expected_info

def compare_files(test, expected):
    lines_expected = open(expected).readlines()
    lines_test = open(test).readlines()

    print(''.join(difflib.ndiff(lines_test, lines_expected)))
    filecmp.clear_cache()
    assert filecmp.cmp(test, expected) is True


import dns.resolver
import dns.dnssec
from tldextract import extract

class DnssecChecker:

    @staticmethod
    def get_interface_list():
        return [
            "dnssec_domain",
            "dnssec_nameserver",
            "has_dnssec",
            "dnssec_is_valid",
            "dnssec_algorithm",
        ]

    def __init__(self, website):
        self.domain = None
        self.__get_domain__(website)
        self.nameserver = None
        self.ns_ip_address = None
        self.__dnskeys = None
        self.__dnssigs = None
        self.__algorithm_name = None
        self.__has_dnssec = False
        self.__dnssec_is_valid = False
        self.__resolver = None
        self.timeout = 20.0

    def __check__(self):
        self.__get_ns__()
        if self.__has_dnssec__():
            self.__has_dnssec = True
            self.__set_algorithm_name__()
            if self.__dnssec_is_valid__():
                self.__dnssec_is_valid = True
        return self

    def __get_domain__(self, domain_name_raw):
        _, td_location, tsu_location = extract(domain_name_raw)
        domain = f"{td_location}.{tsu_location}"
        self.domain = domain

    def __get_ns__(self):
        if self.domain is not None:
            try:
                self.nameserver = \
                    self.__get_resolver__().resolve(self.domain, dns.rdatatype.NS).rrset[0].to_text()
                self.ns_ip_address = \
                    self.__get_resolver__().resolve(self.nameserver, dns.rdatatype.A).rrset[0].to_text()
            except Exception as e:
                #print(e)
                pass

    def __set_algorithm_name__(self):
        if self.__dnskeys is not None:
            algos = set()
            for dns_record in self.__dnskeys:
                dns_key_text = str(dns_record)
                algorithm_code = dns_key_text.split(" ")[2]
                algos.add(dns.dnssec.algorithm_from_text(algorithm_code).name)

            self.__algorithm_name = '|'.join(algos)

    @staticmethod
    def __get_resolver__(nameserver='8.8.8.8'):
        resolver = dns.resolver.Resolver()
        resolver.nameservers = ([nameserver])
        resolver.lifetime = 15.0
        resolver.timeout = self.timeout
        resolver.use_edns(0, dns.flags.CD | dns.flags.DO | dns.flags.RD, 4096)
        return resolver

    def get_information(self):
        self.__check__()
        return {
            "dnssec_domain": self.domain,
            "dnssec_nameserver": self.nameserver,
            "has_dnssec": self.__has_dnssec,
            "dnssec_is_valid": self.__dnssec_is_valid,
            "dnssec_algorithm": self.__algorithm_name
        }

    def __has_dnssec__(self):
        if self.domain is not None:
            try:
                req = dns.message.make_query(self.domain, dns.rdatatype.DNSKEY, want_dnssec=True)

                self.__sec_response= dns.query.udp_with_fallback(req, self.ns_ip_address, timeout=self.timeout)
                self.__dnskeys, self.__dnssigs = self.__sec_response[0].answer

                if self.__dnskeys and self.__dnssigs:
                    return True
                else:
                    return False
            except Exception as e:
                #print(e)
                return False

    def __dnssec_is_valid__(self):
        try:
            q_name = dns.name.from_text(self.domain)
            dns.dnssec.validate(self.__dnskeys, self.__dnssigs,{q_name: self.__dnskeys})
            return True
        except Exception as e:
            #print(e)
            return False


import numpy as np
import cv2

def nothing(x):
	pass

cap = cv2.VideoCapture(1)

# cv2.namedWindow('BW')
# cv2.createTrackbar('Trackbar1', 'BW', 100, 255, nothing)

# cv2.namedWindow('Edge')
# cv2.createTrackbar('Trackbar2', 'Edge', 100, 255, nothing)
# cv2.createTrackbar('Trackbar3', 'Edge', 200, 255, nothing)

while cap.isOpened() :

	ret, frame = cap.read()

	if ret == True :
		cv2.imshow('one',frame)
		gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
		gray = cv2.flip(gray,1)
		cv2.imshow('two',gray)
		# thrs1 = cv2.getTrackbarPos('Trackbar1', 'BW')
		ret, balck_and_white= cv2.threshold(gray,100,255,cv2.THRESH_BINARY)
		balck_and_white = cv2.flip(balck_and_white,0)
		cv2.imshow('three',balck_and_white)

		# thrs2 = cv2.getTrackbarPos('Trackbar2', 'Edge')
		# thrs3 = cv2.getTrackbarPos('Trackbar3', 'Edge')
		gray = cv2.flip(gray,0)
		gray = cv2.flip(gray,1)
		edge = cv2.Canny(gray,80,80)
		cv2.imshow('four', edge)

		if cv2.waitKey(1) & 0xFF == ord('q'):
			break
	else :
		break

cap.release()
cv2.destroyAllWindows()


from pyplpath import *
from pypl import *
from math import *
import os.path

#variables

#define the dimesion of the grid on which the boat is located
grid_half_dimension = 50
grid_discretization_steps = grid_half_dimension * 2

#define the probabilistic variables associated to location
coordinate_type = plIntegerType(-grid_half_dimension,grid_half_dimension)
X=plVariable("X", coordinate_type)
Y=plVariable("Y", coordinate_type)

#define the probabilistic variables associated to range and bearing
bearing_type = plIntegerType(0,359)

#bearings
B1=plVariable("B1",bearing_type)
B2=plVariable("B2",bearing_type)
B3=plVariable("B3",bearing_type)

#Ancilary Variable "
V =plVariable("V",plLabelType(["true","false"]))

#define the function necessary to get bearing to
#each beacon knowing the position X and Y.

def radiantodegree(x):
    if x >= 0 :
        return 180 * x / pi
    else :
        return 360 + 180 * x / pi

def f_b_1(Output_,Input_):
    Output_[0]= radiantodegree(atan2(Input_[Y].to_float()-50.0,Input_[X].to_float()-50.0))

def f_b_2(Output_,Input_):
    Output_[0]= radiantodegree(atan2(Input_[Y].to_float(),Input_[X].to_float()-50.0))

def f_b_3(Output_,Input_):
    Output_[0]= radiantodegree(atan2(Input_[Y].to_float()-50.0,Input_[X].to_float()))

#define the function necessary to get the precision on the bearing
#knowing the anciliary clue

def g_b (Output_,Input_) :
    if Input_[V].to_float()== 0 :
        Output_[0]= 10
    else :
        Output_[0]= 30

#define the desciption
JointDistributionList=plComputableObjectList()

#building the specification
JointDistributionList.push_back(plUniform(X))
JointDistributionList.push_back(plUniform(Y))

#building soft evidence
#here assume no visibility
JointDistributionList.push_back(plProbTable(V,[0,1]))

#distributions related to bearing
JointDistributionList.push_back(plCndNormal(B1,X^Y^V, \
                                               plPythonExternalFunction(X^Y^V,f_b_1), \
                                               plPythonExternalFunction(X^Y^V,g_b)))
JointDistributionList.push_back(plCndNormal(B2,X^Y^V, \
                                               plPythonExternalFunction(X^Y^V,f_b_2), \
                                                plPythonExternalFunction(X^Y^V,g_b)))
JointDistributionList.push_back(plCndNormal(B3,X^Y^V, \
                                               plPythonExternalFunction(X^Y^V,f_b_3), \
                                               plPythonExternalFunction(X^Y^V,g_b)))
#define the description
localisation_model=plJointDistribution(X^Y^V^B1^B2^B3,JointDistributionList)

sensor_reading_values=plValues(B1^B2^B3)

# here we assume the boat to be at [0, 0, 0]
sensor_reading_values[B1]= 225
sensor_reading_values[B2]= 180
sensor_reading_values[B3]= 270

#estimating the location with low visibility
PXY_K_B1B2B3=localisation_model.ask(X^Y,B1^B2^B3)
PXY=PXY_K_B1B2B3.instantiate(sensor_reading_values)
PXY.plot(os.path.join(ExDir, 'chapter7', 'figures', 'loc00_with_ancillary_set_to_false'))

#estimating the location with High visibility
localisation_model.replace(V,plProbTable(V,[1,0]))
PXY_K_B1B2B3=localisation_model.ask(X^Y,B1^B2^B3)
PXY=PXY_K_B1B2B3.instantiate(sensor_reading_values)
PXY.plot(os.path.join(ExDir, 'chapter7', 'figures', 'loc00_with_ancillary_set_to_true'))

#estimating the location with low visibility
localisation_model.replace(V,plProbTable(V,[0.1,0.9]))
PXY_K_B1B2B3=localisation_model.ask(X^Y,B1^B2^B3)
PXY=PXY_K_B1B2B3.instantiate(sensor_reading_values)
PXY.plot(os.path.join(ExDir, 'chapter7', 'figures', 'loc00_with_soft_evidence'))

#estimating the location with approximate visibility
localisation_model.replace(V,plProbTable(V,[0.5,0.5]))
PXY_K_B1B2B3=localisation_model.ask(X^Y,B1^B2^B3)
PXY=PXY_K_B1B2B3.instantiate(sensor_reading_values)
PXY.plot(os.path.join(ExDir, 'chapter7', 'figures', 'loc00_with_soft_evidence_05'))