import torch
from sklearn.preprocessing import StandardScaler
from torch.utils.data import DataLoader, TensorDataset
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from pandas import DataFrame
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, \
    NavigationToolbar2Tk
import tkinter
from PIL import ImageTk, Image
from sklearn.preprocessing import LabelEncoder

def get_dataset(df: DataFrame):
    # We assume that the dataframe's last column are the target values
    # (or y values).
    label = LabelEncoder()

    for col in list(df.columns.values):
        if df[col].dtype == 'O':
            df[col] = label.fit_transform(df[col])

    x = df.iloc[:, :len(df.columns)-1].values
    y = df.iloc[:, len(df.columns)-1].values

    y = y.reshape(len(y), 1)

    x_train, x_val, y_train, y_val = train_test_split(x, y, test_size=0.2, random_state=50)
    # Scaling the data.
    scaler = StandardScaler()
    scaler.fit(x_train)
    x_train = scaler.transform(x_train)
    x_val = scaler.transform(x_val)

    x_train = torch.from_numpy(x_train).to(torch.float32)
    y_train = torch.from_numpy(y_train).to(torch.float32)

    x_val = torch.from_numpy(x_val).to(torch.float32)
    y_val = torch.from_numpy(y_val).to(torch.float32)

    train_dt = TensorDataset(x_train, y_train)
    train_loader = DataLoader(train_dt, batch_size=2048)

    val_dt = TensorDataset(x_val, y_val)
    val_loader = DataLoader(val_dt, batch_size=2048)

    return train_loader, val_loader

def train_loss_graphs(train_loss: list, val_loss: list, show=False):
    # Here the standalone graphs for the train and validation losses
    # are created. They are not shown on a different window by default
    # in case other graphs need to be created.
    train_loss_values = []
    for i in range(len(train_loss)):
        train_loss_values.append(train_loss[i].cpu().detach().numpy())

    val_loss_values = []
    for i in range(len(val_loss)):
        val_loss_values.append(val_loss[i].cpu().detach().numpy())

    fig, (ax1, ax2) = plt.subplots(1, 2)
    line1 = ax1.plot(range(len(train_loss_values)), train_loss_values)
    ax1.set_title('Train Loss')
    ax1.set_xlabel('Steps')

    line2 = ax2.plot(range(len(val_loss_values)), val_loss_values)
    ax2.set_title('Validation Loss')
    ax2.set_xlabel('Steps')

    fig.tight_layout()

    if show:
        put_em_on(fig)

    return line1, line2

def train_acc_scores(train_acc: list, val_acc: list, show=False):
    # Here the standalone graphs for the train and validation accuracy
    # are created. They are not shown on a different window by default
    # in case other graphs need to be created.
    train_acc_values = []
    for i in range(len(train_acc)):
        train_acc_values.append(train_acc[i].cpu().detach().numpy())

    val_acc_values = []
    for i in range(len(val_acc)):
        val_acc_values.append(val_acc[i].cpu().detach().numpy())

    fig, (ax1, ax2) = plt.subplots(1, 2)
    line1 = ax1.plot(range(len(train_acc_values)), train_acc_values)
    ax1.set_title('Train Accuracy')
    ax1.set_xlabel('Steps')

    line2 = ax2.plot(range(len(val_acc_values)), val_acc_values)
    ax2.set_title('Validation Accuracy')
    ax2.set_xlabel('Steps')

    fig.tight_layout()

    if show:
        put_em_on(fig)

    return line1, line2

def merge_graphs(loss_lines: tuple, acc_lines: tuple):
    # The merging of all the graphs into one figure.
    fig, axs = plt.subplots(2, 2)
    axs[0, 0].plot(loss_lines[0][0].get_data()[0], loss_lines[0][0].get_data()[1])
    axs[0, 1].plot(loss_lines[1][0].get_data()[0], loss_lines[1][0].get_data()[1])
    axs[1, 0].plot(acc_lines[0][0].get_data()[0], acc_lines[0][0].get_data()[1])
    axs[1, 1].plot(acc_lines[1][0].get_data()[0], acc_lines[1][0].get_data()[1])

    axs[0, 0].set_title('Training')
    axs[0, 0].set_ylabel('Train Loss')
    axs[0, 0].set_xlabel('Steps')

    axs[0, 1].set_title('Validation')
    axs[0, 1].set_ylabel('Validation Loss')
    axs[0, 1].set_xlabel('Steps')

    axs[1, 0].set_ylabel('Training Accuracy (%)')
    axs[1, 0].set_xlabel('Epochs')

    axs[1, 1].set_ylabel('Validation Accuracy(%)')
    axs[1, 1].set_xlabel('Epochs')

    axs[0, 0].grid()
    axs[0, 1].grid()
    axs[1, 0].grid()
    axs[1, 1].grid()
    fig.tight_layout()

    put_em_on(fig)

def put_em_on(fig: plt.Figure()):
    # PLacement of graphs to a separate tkinter window.
    window = tkinter.Toplevel()
    window.title('Loss Graphs')
    window.geometry('640x480')
    window['background'] = 'white'
    window.tk.call('wm', 'iconphoto', window._w,
                   ImageTk.PhotoImage(Image.open('icons8-neural-network-64.ico')))
    canvas = FigureCanvasTkAgg(fig, master=window)
    canvas.draw()
    canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
    toolbar = NavigationToolbar2Tk(canvas, window)
    toolbar.update()
    canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)


import random
from mpi4py import MPI

print(f"Random is at: {random.__file__}")
# init mpi env
comm = MPI.COMM_WORLD
# id/rank in mpi env
rank = comm.Get_rank()
# size of the mpi cluster
size = comm.Get_size()
# processor name
hostname = MPI.Get_processor_name()
print("Hello World ! main.py {}/{}@{}\n".format(rank, size, hostname))

INTERVAL = 1000

circle_points = 0
square_points = 0

# Total Random numbers generated= possible x
# values* possible y values
for i in range(INTERVAL**2):
    # Randomly generated x and y values from a
    # uniform distribution
    # Range of x and y values is -1 to 1
    rand_x = random.uniform(-1, 1)
    rand_y = random.uniform(-1, 1)
    # Distance between (x, y) from the origin
    origin_dist = rand_x**2 + rand_y**2
    # Checking if (x, y) lies inside the circle
    if origin_dist <= 1:
        circle_points += 1
    square_points += 1
    # Estimating value of pi,
    # pi= 4*(no. of points generated inside the
    # circle)/ (no. of points generated inside the square)
pi = 4 * circle_points / square_points
print("Final Estimation of Pi=", pi)
with open("/output/pi.out", "w") as f:
    f.write(f"Pi is: {pi}\n")
    f.write(random.__file__)
    f.write("\n\nHello World ! main.py {}/{}@{}\n".format(rank, size, hostname))


"""added image work relationship

Revision ID: 295ccda6e57c
Revises: cc0c6b8aa739
Create Date: 2023-09-29 22:19:31.757829

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '295ccda6e57c'
down_revision = 'cc0c6b8aa739'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('Image', schema=None) as batch_op:
        batch_op.add_column(sa.Column('work_id', sa.Integer(), nullable=True))
        batch_op.create_foreign_key(batch_op.f('fk_Image_work_id_work_orders'), 'work_orders', ['work_id'], ['id'])

    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('Image', schema=None) as batch_op:
        batch_op.drop_constraint(batch_op.f('fk_Image_work_id_work_orders'), type_='foreignkey')
        batch_op.drop_column('work_id')

    # ### end Alembic commands ###


##
# Convert grade points to letter grade and viceversa
#

# Create a dictionary with letter grades as keys
# and grade points as values
letter_points = {
    "A+" : 4.1,
    "A" : 4.0,
    "A-" : 3.7,
    "B+" : 3.3,
    "B" : 3.0,
    "B-" : 2.7,
    "C+" : 2.3,
    "C" : 2.0,
    "C-" : 1.7,
    "D+" : 1.3,
    "D" : 1.0,
    "F" : 0.0
}

# Read the first grade from the user
grade = input("Please, enter a grade point or letter grade: ")

# Until the user doesn't enter a blank line
while grade != "":
    try:
        # If the grade is a float value, convert it
        # to its corrisponding letter grade
        grade = float(grade)
        if grade > 4.0:
            print("A+")
        elif grade >= 3.8 and grade <= 4.0:
            print("A")
        elif grade >= 3.5 and grade < 3.8:
            print("A-")
        elif grade >= 3.2 and grade < 3.5:
            print("B+")
        elif grade >= 2.9 and grade < 3.2:
            print("B")
        elif grade >= 2.5 and grade < 2.9:
            print("B-")
        elif grade >= 2.2 and grade < 2.5:
            print("C+")
        elif grade >= 1.9 and grade < 2.2:
            print("C")
        elif grade >= 1.5 and grade < 1.9:
            print("C-")
        elif grade >= 1.2 and grade < 1.5:
            print("D+")
        elif grade >= 0.5 and grade < 1.2:
            print("D")
        elif grade >= 0.0 and grade < 0.5:
            print("F")
        # Display an error message if the float number is negative
        else:
            print("There's no negative scores")

    except:
        # If the grade is a letter, convert it
        # to its corresponding grade points
        try:
            grade = grade.upper()
            print(letter_points[grade])

        except:
            # Display an error message if the letter isn't in the dictionary
            print("The supplied input is invalid")

    # Ask to the following grade
    grade = input("Please, enter another grade point or letter grade: ")


# app.py

from flask import Flask, render_template, request
import pandas as pd
import matplotlib.pyplot as plt

app = Flask(__name__)

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

@app.route('/upload', methods=['GET', 'POST'])
def upload():
    if request.method == 'POST':
        file = request.files['file']
        if file:
            # Read the uploaded CSV file
            df = pd.read_csv(file)

            # Perform data processing and generate visualizations
            columns = df.columns
            x_column = columns[0]
            y_column = columns[1]

            plt.figure()
            plt.bar(df[x_column], df[y_column])
            plt.xlabel(x_column)
            plt.ylabel(y_column)
            plt.title('Data Visualization')
            plt.savefig('static/chart.png')
            plt.close()

            # Render the visualizations template
            with app.app_context():
                return render_template('visualizations.html')

    return render_template('upload.html')

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


"""Adds bills.last_payment column

Revision ID: 2d8f63c0c219
Revises:
Create Date: 2018-12-07 12:35:58.577693

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '2d8f63c0c219'
down_revision = None
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('bills', sa.Column('last_payment', sa.DateTime(), nullable=True))
    op.create_index(op.f('ix_bills_last_payment'), 'bills', ['last_payment'], unique=False)
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_bills_last_payment'), table_name='bills')
    op.drop_column('bills', 'last_payment')
    # ### end Alembic commands ###


"""
Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None
"""

class Solution:
    """
    @param: head: the List
    @param: k: rotate to the right k places
    @return: the list after rotation
    """

    def rotateRight(self, head, k):
        if not head or not head.next:
            return head
        fast, slow = head, head
        l = 0
        while fast:
            l += 1
            fast = fast.next
        k %= l
        fast = head
        while k:
            fast = fast.next
            k -= 1
        while fast.next:
            slow = slow.next
            fast = fast.next
        fast.next = head
        head = slow.next
        slow.next = None
        return head


a = True
b = False
x = ('bear', 'bunny', 'tree', 'sky', 'rain')
y = 'bear'

if a and b:
    print('expression is true')
else:
    print('expression is false')

if "bear" in x:
    print("yes in")

if "bear" and "tree" in x:
    print("yes and")

if "run" not in x:
    print("Not")


from flask import Flask,request,jsonify
from flask_cors import CORS
from werkzeug.utils import secure_filename
import os
from data import *
import chardet

app = Flask(__name__)  # Initialize Flask App
CORS(app)

FILES='FILES'
if not os.path.exists(FILES):
    os.makedirs(FILES)

app.config['FILES'] = FILES

@app.route('/upload', methods=['POST'])
def upload():
    try:
        if request.method == 'POST':
            if 'file' not in request.files:
                 return jsonify({"Error Status": "File does not exist"})
            else:
                file = request.files['file']
                if file.filename == '':
                    return jsonify({"Error Status": "File not selected"})
                if file:
                    filename = secure_filename(file.filename)
                    file_path = os.path.join(app.config['FILES'],filename)
                    file.save(file_path)

                    result=processing(file_path)
                    return jsonify(result)

    except Exception as e:
        return str(e)

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


# See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winprog/winprog/windows_api_reference.asp
# for information on Windows APIs.
STD_INPUT_HANDLE = -10
STD_OUTPUT_HANDLE= -11
STD_ERROR_HANDLE = -12

FOREGROUND_BLUE = 0x01 # text color contains blue.
FOREGROUND_GREEN= 0x02 # text color contains green.
FOREGROUND_RED  = 0x04 # text color contains red.
FOREGROUND_INTENSITY = 0x08 # text color is intensified.
BACKGROUND_BLUE = 0x10 # background color contains blue.
BACKGROUND_GREEN= 0x20 # background color contains green.
BACKGROUND_RED  = 0x40 # background color contains red.
BACKGROUND_INTENSITY = 0x80 # background color is intensified.

import ctypes

std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)

def set_color(color, handle=std_out_handle):
    """(color) -> BOOL

    Example: set_color(FOREGROUND_GREEN | FOREGROUND_INTENSITY)
    """
    bool = ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
    return bool
# set_color

# win_console.py


import logging
import re

from .idmatching.filtering import is_ignored_email
from .matcher import IdentityMatcher, FilteredIdentity
from ..services.dro.user_dto import User

EMAIL_ADDRESS_REGEX = r"^(?P<email>[^\s@]+@[^\s@.]+\.[^\s@]+)$"

logger = logging.getLogger(__name__)

class EmailIdentity(FilteredIdentity):
    """Class to stored EmailName filtered identities"""

    def __init__(self, canonical_name, user_id, email):
        super(EmailIdentity, self).__init__(canonical_name, user_id)
        self.email = email

    def to_dict(self):
        return {
                'canonical_name': self.canonical_name,
                'user_id': self.user_id,
                'email': self.email
               }

class EmailMatcher(IdentityMatcher):
    """
    Simple unique identities matcher.
    This matcher only produces a positive result when two identities
    from each unique identity share the same email address. When `strict`
    is set, the email must be well-formed. It also returns a positive
    match when the uuid on both unique identities is equal.
    :param blacklist: list of entries to ignore during the matching process
    :param sources: only match the identities from these sources
    :param strict: strict matching with well-formed email addresses
    """
    def __init__(self, blacklist=None, sources=None, strict=True):
        super(EmailMatcher, self).__init__(blacklist=blacklist,
                                           sources=sources,
                                           strict=strict)
        self.email_pattern = re.compile(EMAIL_ADDRESS_REGEX)

    def match(self, a, b):
        """Determine if two unique identities are the same.
        This method compares the email addresses of each identity to check
        if the given unique identities are the same. When the given unique
        identities are the same object or share the same UUID, this will
        also produce a positive match.
        Identities which their email addresses are in the blacklist will be
        ignored and the result of the comparison will be false.
        :param a: unique identity to match
        :param b: unique identity to match
        :returns: True when both unique identities are likely to be the same.
            Otherwise, returns False.
        :raises ValueError: when any of the given unique identities is not
            an instance of UniqueIdentity class
        """
        if not isinstance(a, User):
            raise ValueError("<a> is not an instance of User")
        if not isinstance(b, User):
            raise ValueError("<b> is not an instance of User")

        if a.id and b.id and a.id == b.id:
            return True

        filtered_a = self.filter(a)
        filtered_b = self.filter(b)

        for fa in filtered_a:
            for fb in filtered_b:
                if self.match_filtered_identities(fa, fb):
                    return True
        return False

    def match_filtered_identities(self, fa, fb):
        """Determine if two filtered identities are the same.
        The method compares the email addresses of each filtered identity
        to check if they are the same. When the given filtered identities
        are the same object or share the same UUID, this will also
        produce a positive match.
        Identities which their email addresses are in the blacklist will be
        ignored and the result of the comparison will be false.
        :param fa: filtered identity to match
        :param fb: filtered identity to match
        :returns: True when both filtered identities are likely to be the same.
            Otherwise, returns False.
        :raises ValueError: when any of the given filtered identities is not
            an instance of EmailIdentity class.
        """
        if not isinstance(fa, EmailIdentity):
            raise ValueError("<fa> is not an instance of EmailIdentity")
        if not isinstance(fb, EmailIdentity):
            raise ValueError("<fb> is not an instance of EmailIdentity")

        if fa.user_id and fb.user_id and fa.user_id == fb.user_id:
            return True

        if self._check_blacklist(fa):
            return False

        # Compare email addresses first
        if fa.email and fa.email == fb.email:
            return True

        return False

    def filter(self, u):
        """Filter the valid identities for this matcher.
        :param u: unique identity which stores the identities to filter
        :returns: a list of identities valid to work with this matcher.
        :raises ValueError: when the unique identity is not an instance
            of UniqueIdentity class
        """
        if not isinstance(u, User):
            raise ValueError("<u> is not an instance of User")

        filtered = []

        for author in u.identities:

            if self._check_blacklist(author):
                continue

            email = author.email.lower() if author.email else None

            if email:
                fid = EmailIdentity(author.canonical_name, author.user_id, email)
                filtered.append(fid)

        return filtered

    @staticmethod
    def matching_criteria():
        """List of keys used during the matching phase.
        returns: a list of keys
        """
        return ['email']

    def _check_blacklist(self, id_):
        if not id_.email:
            return False

        if self._check_value_in_blacklist(id_.email):
            return True

        if is_ignored_email(id_.email):
            return True

        return False

    def _check_email(self, email):
        if not email:
            return False

        checked = self.email_pattern.match(email) is not None

        return checked

    def _check_value_in_blacklist(self, value):
        return value and value.lower() in self.blacklist


# http://www.saltycrane.com/blog/2009/11/trying-out-retry-decorator-python/

from functools import wraps
import time
import sys

# hiding traceback
# sys.tracebacklimit = 0

_default_retry_interval = 10  # seconds
_default_retry_attempts = 10

def log(message):
    print(message)
    sys.stdout.flush()

def retry(interval=_default_retry_interval, attempts=_default_retry_attempts):
    def deco_retry(func):
        @wraps(func)
        def retry_wrapper(*args, **kwargs):
            attempt = 0
            while True:
                try:
                    return func(*args, **kwargs)
                except Exception as e:
                    attempt += 1
                    if attempt > attempts and attempts >= 0:
                        raise e
                    log("[ E ] {}".format(e))
                    if attempts >= 0:
                        log("[ I ] Retrying attempt {}/{} in {} seconds...".format(attempt, attempts, interval))
                    else:
                        log("[ I ] Retrying attempt {} in {} seconds...".format(attempt, interval))
                    time.sleep(interval)
        return retry_wrapper
    return deco_retry


# -*- coding: utf-8 -*-
import KBEngine, time
from KBEDebug import *
from strategy.trigger_strategy.TriggerStrategy import TriggerStrategy

class ImprisonStrategy(TriggerStrategy):
    """
    禁锢策略
    """
    def __init__(self):
        TriggerStrategy.__init__(self)

    def initializeStrategy(self, strategyData):
        super().initializeStrategy(strategyData)
        self.damage = strategyData["伤害"]

    def execute(self, trigger=None, otherEntity=None):
        if otherEntity.id == trigger.owner.id:
            return
        if not hasattr(otherEntity, "canDamage"):
            return
        if otherEntity.canReceiveSkill is True:
            if otherEntity.canMove is not None:
                otherEntity.moveToPointSample(otherEntity.position, 20)
                otherEntity.canMove = False
            if otherEntity.canCastSkill is not None:
                otherEntity.canCastSkill = False
            otherEntity.addSkillControlTimer(
                "ImprisonCancelTimer",
                3,
                0,
                "self.canMove = True\n" +
                "self.canCastSkill = True\n" +
                "DEBUG_MSG('ImprisonCancelTimer scriptString')",
                "onceOperation")


import nltk
from nltk import *
from nltk.corpus import treebank
from nltk import Nonterminal
from nltk.treetransforms import *
from nltk import treetransforms
from nltk.parse import pchart
from PYEVALB.scorer import Scorer
from PYEVALB import parser
import pickle
from CYK_PCFG import Parser
from pcfg import *
from pcfg import pcfg
from CYK_PCFG import *

def train_pcfg():
    pcfg()
    return

def test_pcfg(test_idx, grammar=None):
    if grammar == None:
    	print("No PCFG grammar found\n")
    	return
    p = Parser(grammar)
    goldtest = []
    for idx, x in enumerate(test_idx):
        for idx2, tree in enumerate(treebank.parsed_sents(x)):
            tree_words = list(map(lambda x: x.strip().replace('"', ''), tree.leaves()))
            print('Sentence:', tree_words)
            try:
                grammar.check_coverage(tree_words)
            except ValueError as v:

                new_words = set()
                words = v.args[0].split(':')[1].replace('"', '').replace("'", "")[:-1]
                for word in words.split(','):
                    new_words.add(word.strip())
                grammar = update_grammar(new_words, grammar)
                p = Parser(grammar)
            try:
                test_tree = p.parse(tree_words)
                if test_tree :
                    print(test_tree)
                    gold_test.append((str(tree), str(test_tree.pformat())))
                    evaluate([goldtest[-1]])
                else:
                    print('No tree')
            except ValueError as v:
                print('No tree. ', v)

    print('Got parse trees for: ', len(goldtest), 'sentences.')
    return goldtest

def evaluate(goldtest):
    scorer = Scorer()
    for gold, test in goldtest:
        goldtree = parser.create_from_bracket_string(gold)
        testtree = parser.create_from_bracket_string(test)
        result = scorer.score_trees(gold_tree, test_tree)
        print(result)

#%%
def test(sentences):
    for sentence in sentences:
        tokens = sentence.split()
        grammar = load_grammar()
        p = Parser(grammar)
        tree = p.parse(tokens)
        if tree is None:
            print('No Tree')
        else:
            print(tree)
    tree.pretty_print()

if __name__ == '__main__':
    treebank_download()
    try:
        with open('grammar.pkl', 'rb') as input_file:
            grammar = pickle.load(input_file)
    except FileNotFoundError as NF:
        train_idx = (len(treebank.fileids()) * 3 ) // 4
        test_idx = (len(treebank.fileids()) * 1 ) // 4
        grammar = pcfg(x_train)
    p = Parser(grammar)
    sent = treebank.parsed_sents('wsj_0180.mrg')[0].leaves()
    tree = p.parse(sent)
    print("Parsing for wsj_0180.mrg")
    print(tree)

    evaluate()


import unittest
from statistics import Statistics
from statistics import SortBy
from player import Player

class PlayerReaderStub:
    def get_players(self):
        return [
            Player("Semenko", "EDM", 4, 12),
            Player("Lemieux", "PIT", 45, 54),
            Player("Kurri",   "EDM", 37, 53),
            Player("Yzerman", "DET", 42, 56),
            Player("Gretzky", "EDM", 35, 89)
        ]

class TestStatistics(unittest.TestCase):
    def setUp(self):
        # annetaan Statistics-luokan oliolle "stub"-luokan olio
        self.statistics = Statistics(
            PlayerReaderStub()
        )

    def test_etsi_pelaaja(self):
        player = self.statistics.search("Kurri")
        self.assertEqual(player.team, "EDM")
        self.assertEqual(player.goals, 37)
        self.assertEqual(player.assists, 53)

    def test_etsi_pelaajat_tiimissa(self):
        players = self.statistics.team("EDM")
        self.assertEqual(players[0].name, "Semenko")
        self.assertEqual(players[1].name, "Kurri")
        self.assertEqual(players[2].name, "Gretzky")

    def test_paras_pelaaja(self):
        player = self.statistics.top(3)[0]
        self.assertEqual(player.name, "Gretzky")

    def test_etsi_pelaaja_ei_tulosta(self):
        self.assertEqual(self.statistics.search("Kucherov"), None)

    def test_paras_pelaaja_points(self):
        player1 = self.statistics.top(3, SortBy.POINTS)[0]
        player2 = self.statistics.top(3)[0]
        self.assertEqual(player1.name, player2.name)
        self.assertEqual(player1.name, "Gretzky")

    def test_paras_pelaaja_goals(self):
        player = self.statistics.top(3, SortBy.GOALS)[0]
        self.assertEqual(player.name, "Lemieux")

    def test_paras_pelaaja_assists(self):
        # myös paras pelaaja
        player1 = self.statistics.top(3, SortBy.ASSISTS)[0]
        self.assertEqual(player1.name, "Gretzky")

        # eri kuin toiseksi paras pelaaja pisteiden perusteella
        player2 = self.statistics.top(3, SortBy.ASSISTS)[1]
        self.assertEqual(player2.name, "Yzerman")


from copy import deepcopy
from django.contrib.admin import AdminSite
from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase, RequestFactory
from formative.admin import InlineFormativeBlobAdmin
from formative.forms import FormativeTypeForm
from formative.formsets import SortedInlineFormativeAdminFormSet
from formative.models import InlineFormativeBlob
from tests.testproject.testapp.admin import BookAdmin
from tests.testproject.testapp.models import Book

class MockUser(object):
    def has_perm(self, *args, **kwargs):
        return True

class TestAdd(TestCase):
    def setUp(self):
        self.admin = BookAdmin(Book, AdminSite())
        self.request = RequestFactory().get('/add/')

class TestChange(TestCase):
    def setUp(self):
        self.admin = BookAdmin(Book, AdminSite())
        self.book = Book(title='Gun Machine')
        self.book.save()
        InlineFormativeBlob(
            content_object=self.book, formative_type='simple', sortorder=0
        ).save()
        InlineFormativeBlob(
            content_object=self.book, formative_type='fancy', sortorder=0
        ).save()
        self.request = RequestFactory().get('/edit/')
        self.request.user = MockUser()
        response = self.admin.change_view(self.request, str(self.book.pk))
        self.formsets = response.context_data['inline_admin_formsets']
        self.forms = [form for form in self.formsets[0]]

    def test_formsets(self):
        self.assertIsInstance(
            self.formsets[0], SortedInlineFormativeAdminFormSet)

    def test_forms(self):
        self.assertEqual(len(self.forms), 4)

    def test_form_1_form(self):
        self.assertIsInstance(
            self.forms[0].form,
            InlineFormativeBlob.registry.get('simple').form)

    def test_form_2_form(self):
        self.assertIsInstance(
            self.forms[1].form,
            InlineFormativeBlob.registry.get('fancy').form)

    def test_form_1_type(self):
        self.assertEqual(
            self.forms[0].form.formative_type,
            InlineFormativeBlob.registry.get('simple'))

    def test_form_2_type(self):
        self.assertEqual(
            self.forms[1].form.formative_type,
            InlineFormativeBlob.registry.get('fancy'))

    def test_form_3_form(self):
        self.assertIsInstance(self.forms[2].form, FormativeTypeForm)

    def test_form_1_fieldsets(self):
        expected = deepcopy(
            InlineFormativeBlob.registry.get('simple').fieldsets)
        expected[0][1]['fields'] = ['sortorder'] + expected[0][1]['fields']
        self.assertEqual(
            self.forms[0].fieldsets, expected)

    def test_form_2_fieldsets(self):
        expected = deepcopy(
            InlineFormativeBlob.registry.get('fancy').fieldsets)
        expected[0][1]['fields'] = ['sortorder'] + expected[0][1]['fields']
        self.assertEqual(
            self.forms[1].fieldsets, expected)

class TestBadlyConfiguredAdmin(TestCase):
    def setUp(self):
        self.admin = InlineFormativeBlobAdmin(Book, AdminSite())

    def test_raises_improperly_configured(self):
        self.assertRaises(ImproperlyConfigured, self.admin.get_formative_model)


import cv2,os
import numpy as np
import PIL.Image
import PIL.ImageTk
import pandas as pd
import datetime, time

face_cascade = cv2.CascadeClassifier(r'C:\Users\Indra\Downloads\Kp\Project\cascades\data\haarcascade_frontalface_default.xml')
#face_cascade = cv2.CascadeClassifier(r'C:\Users\Indra\Downloads\Kp\Project\cascades\data\haarcascade_frontalface_alt2.xml')
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read(r'C:\Users\Indra\Downloads\Kp\Project\face_trainner\trainner.yml')
cap = cv2.VideoCapture(1) #menangkap object
#membaca file mahasiswa.csv
font=cv2.FONT_HERSHEY_SIMPLEX
df=pd.read_csv(r'C:\Users\Indra\Downloads\Kp\Project\Mahasiswa\mahasiswa.csv')
col_names = ['Id', 'Name', 'Date','Time']
attendance = pd.DataFrame(columns=col_names)

while(True):
    # Pengambilan bingkai(frame)
    ret, frame = cap.read()
    # Operasi pada frame datang ke sini
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5)
    for (x, y, w, h) in faces:
        #rectangle (empat persegi panjang)
        color = (255, 0, 0) #BGR
        stroke = 2
        end_color_x = x + w
        end_color_y = y + h
        cv2.rectangle(frame, (x,y), (end_color_x,end_color_y), color, stroke)
        #print(x,y,w,h)
        #color camera
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = frame[y:y+h, x:x+w]
        #incrementing sample number
        #sampleNum = sampleNum+1
        Id, conf = recognizer.predict(roi_gray)
        if (conf < 50):
            time_s = time.time()
            date = str(datetime.datetime.fromtimestamp(time_s).strftime('%Y-%m-%d'))
            timeStamp = datetime.datetime.fromtimestamp(time_s).strftime('%H:%M:%S')
            nama = df.loc[df['Id'] == Id]['Name'].values
            name_get = str(Id) + "_" + nama
            attendance.loc[len(attendance)] = [Id, nama, date, timeStamp]
        else:
            Id = 'Unknown'
            name_get = str(Id)
        if (conf > 75):
            noOfFile = len(os.listdir(r"C:\Users\Indra\Downloads\Kp\Project\ImagesUnknow"))+1
            cv2.imwrite("ImagesUnknown\Image " + str(noOfFile) + ".jpg", frame[y:y+h,x:x+w])
            print("images Unknown")
        cv2.putText(frame,str(name_get),(x,y),font ,1 ,(255,255,255), 2, cv2.LINE_AA)
    attendance=attendance.drop_duplicates(subset=['Id'], keep='first')
    # Tampilkan frame yang dihasilkan 100 milisecond
    cv2.imshow('frame', frame)
    if (cv2.waitKey(1) == ord('q')):
        break
# Setelah semuanya selesai, simpan ke dalam file csv
time_s = time.time()
date = str(datetime.datetime.fromtimestamp(time_s).strftime('%Y-%m-%d'))
timeStamp = datetime.datetime.fromtimestamp(time_s).strftime('%H:%M:%S')
Hour, Minute, Second = timeStamp.split(":")
fileName = r"C:\Users\Indra\Downloads\Kp\Project\attendance\attendance_" + date + "_" + Hour + "-" + Minute + "-" + Second + ".csv"
#fileName = r"C:\Users\Indra\Downloads\Kp\Project\attendance\attendance_" + date + ".csv"
attendance.to_csv(fileName, index=False)

# attendance=attendance.drop_duplicates(keep='first',subset=['Id'])
# fileName="attendance/in.json"
# attendance.to_json(fileName,orient="index")

cap.release() #menutup kembali kamera
cv2.destroyAllWindows()
print("Attendance tracked")


from __future__ import print_function
import os
import numpy as np
from pathlib import Path
import sys
import pandas as pd

sys.path.append(str(Path(__file__).resolve().parent.parent.resolve()))
import commentjson
import argparse
from tqdm import tqdm
from time import time

# ToDo: make more general!! especially paths.

def get_data(
    img_settings,
    dlc_file,
    save_arrays=False,
    chunksize=None,
    bp_to_keep=None,
    start_idx=0,
    nrows=None,
    filter_confidence_value=0.5
):

    # data_dir is e.g., Joao's folder with .json, folders per view, and a .csv dlc file
    # data_dir = Path(data_dir).resolve()  # assuming you run from preprocessing folder
    # filename = data_dir / "tank_dataset_5.h5"
    # f = h5py.File(filename, "r")
    # print("-------------DATASET INFO--------------")
    # print("keys: ", f.keys())
    # print("annotated: ", f["annotated"])
    # print("annotations: ", f["annotations"])
    # print("images: ", f["images"])
    # print("skeleton: ", f["skeleton"])
    # print("skeleton_names: ", f["skeleton_names"])
    # print("----------------------------------------")

    """
    img_settings = commentjson.load(
        open(str(img_settings_path), "r")
    )  # ToDo: the path in the json doesn't make sense
    """
    num_cameras = len(img_settings["height_lims"])

    # data_dir = Path(
    #     "/Users/Sunsmeister/Desktop/Research/Brain/MultiView/3D-Animal-Pose/data/Sawtell-data/fish_tracking"
    # ).resolve()
    print("Reading CSV...")
    if chunksize is None or nrows is not None:
        dlc_data = pd.read_csv(dlc_file, skiprows=range(1, start_idx), nrows=nrows)
    else:
        dlc_data = pd.read_csv(
            dlc_file, chunksize=chunksize, skiprows=range(1, start_idx)
        )
        dlc_data = pd.concat([i for i in tqdm(dlc_data)], ignore_index=True)

    worm_colnames = dlc_data.columns[dlc_data.columns.str.contains("worm")]
    dlc_data.columns = dlc_data.columns.str.replace("worm_right_", "worm_1_right_")
    dlc_data.columns = dlc_data.columns.str.replace("worm_front_", "worm_1_")
    dlc_data.columns = dlc_data.columns.str.replace("worm_right", "worm_")
    dlc_data.columns = dlc_data.columns.str.replace("worm_front", "worm_")
    worm_colnames = dlc_data.columns[dlc_data.columns.str.contains("worm")]

    # points are (num_frames, 3 * num_bodyparts)
    dlc_points = np.asarray(dlc_data)[:, 1:]
    # Find x,y columns
    columns = list(dlc_data.columns)[1:]
    x_points = []
    y_points = []
    confidences = []
    skeleton_names = []
    for i, name in enumerate(columns):
        # return a length-108 list whose entries are 1D np.arrays
        if name.endswith("x"):
            x_points.append(dlc_points[:, i])
            skeleton_names.append(name[:-2])
        elif name.endswith("y"):
            y_points.append(dlc_points[:, i])
        elif name.endswith("confidence"):
            confidences.append(dlc_points[:, i])
    x_points = np.asarray(x_points).transpose()[:, :, np.newaxis]
    y_points = np.asarray(y_points).transpose()[:, :, np.newaxis]
    confidences = np.asarray(confidences).transpose()

    pts_array = np.concatenate((x_points, y_points), axis=-1)

    # Make Nans if low confidence:
    if filter_confidence_value>0.:
        print("in here: {}".format(filter_confidence_value))
        pts_array[confidences < filter_confidence_value] = np.nan

    # Get number of bodyparts
    num_analyzed_body_parts = int(pts_array.shape[1] / num_cameras)

    skeleton_names = np.asarray(skeleton_names)
    reindexing = np.argsort(skeleton_names)

    skeleton_names = skeleton_names[reindexing]  # np.sort(skeleton_names)
    pts_array = pts_array[:, reindexing, :]

    # NOTE: the order should match the order in `image_settings.json`
    view_names = ["top", "main", "right"]
    assert len(view_names) == num_cameras

    # NOTE: Empty list keeps all bodyparts
    if bp_to_keep == None:
        bp_to_keep = []

    # Maps view_name to index that has a bp in that view
    multiview_name_to_idx = {}
    for view_name in view_names:
        multiview_name_to_idx[view_name] = []

    new_skeleton_names = []
    bodypart_names_without_view = []

    for idx, name in enumerate(
        skeleton_names
    ):  # was prev f["skeleton_names"] building on the labels data.
        name_without_view = "_".join(name.split("_")[:-1])
        if len(bp_to_keep) > 0:
            # NOTE: Handling worm specially since we want to turn the 5 keypoints into 1
            if name_without_view not in bp_to_keep and (
                "worm" not in name_without_view or "worm" not in bp_to_keep
            ):
                continue

        new_skeleton_names.append(name)

        if name_without_view not in bodypart_names_without_view:
            bodypart_names_without_view.append(name_without_view)

        for view_name in view_names:
            if view_name == name.split("_")[-1]:  # name.decode("UTF-8").split("_")[-1]:
                multiview_name_to_idx[view_name].append(idx)

    if len(bp_to_keep) > 0:
        num_analyzed_body_parts = int(len(new_skeleton_names) / num_cameras)

    # (views, frames, bp, 2)
    pts_array_2d_joints = np.empty(
        shape=(num_cameras, pts_array.shape[0], num_analyzed_body_parts, 2)
    )

    confidences_bp = np.empty(
        shape=(num_cameras, pts_array.shape[0], num_analyzed_body_parts)
    )

    # Here, the views are out of order
    for i, view_name in enumerate(view_names):
        # Select rows from indices
        view_indices = multiview_name_to_idx[view_name]
        view_points = pts_array[:, view_indices, :]
        view_points[:, :, 0] -= img_settings["width_lims"][i][0]
        view_points[:, :, 1] -= img_settings["height_lims"][i][0]
        pts_array_2d_joints[i, :, :, :] = view_points

        confidences_bp[i, :, :] = confidences[:, view_indices]

    # pts_array_2d_joints = pts_array_2d_joints[:, :, reindexing, :]

    # ----------------------------------------------
    # Replace worm with median
    if "worm" in bp_to_keep:
        print("REPLACING WORM WITH MEDIAN")
        worm_indices = []
        for i, bp in enumerate(bodypart_names_without_view):
            if "worm" in bp:
                worm_indices.append(i)

        worm_indices = np.asarray(worm_indices, dtype=np.int32)
        pts_array_2d_joints[:, :, worm_indices, :] = np.nanmedian(
            pts_array_2d_joints[:, :, worm_indices, :], axis=2
        )[:, :, np.newaxis, :]
        pts_array_2d_joints = np.delete(pts_array_2d_joints, worm_indices[1:], axis=2)

        confidences_bp[:, worm_indices, :] = np.nanmedian(
            confidences_bp[:, worm_indices, :], axis=1
        )[:, np.newaxis, :]
        confidences_bp = np.delete(confidences_bp, worm_indices[1:], axis=2)

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

    """
    points_path = data_dir / "2d_points_array.npy"
    conf_path = data_dir / "2d_confidences_array.npy"

    # Write points
    if save_arrays:
        np.save(points_path, pts_array_2d_joints)
        np.save(conf_path, confidences_bp)
    """

    # Replace worm name
    bp_names = []
    for i, bp in enumerate(bodypart_names_without_view):
        if "worm" in bp:
            if "worm" not in bp_names:
                bp_names.append("worm")
        else:
            bp_names.append(bp)

    return (
        pts_array_2d_joints,
        confidences_bp,
        img_settings,
        bp_names,
    )

def get_args():
    parser = argparse.ArgumentParser(description="Sawtell preprocessing")
    # dataset
    parser.add_argument(
        "--data_dir",
        type=str,
        default="/Users/Sunsmeister/Desktop/Research/Brain/MultiView/3D-Animal-Pose/data/Sawtell-data/20201102_Joao",
    )
    parser.add_argument(
        "--image_settings",
        type=str,
        default="/Users/Sunsmeister/Desktop/Research/Brain/MultiView/3D-Animal-Pose/data/Sawtell-data/20201102_Joao/image_settings.json",
    )
    parser.add_argument(
        "--dlc_file",
        type=str,
        default="/Volumes/sawtell-locker/C1/free/vids/20201102_Joao/concatenated_tracking.csv",
    )
    parser.add_argument("--save_arrays", action="store_true")
    parser.add_argument("--chunksize", type=int)

    return parser.parse_args()

if __name__ == "__main__":
    args = get_args()
    get_data(
        img_settings_path=args.image_settings,
        dlc_file=args.dlc_file,
        save_arrays=args.save_arrays,
        chunksize=args.chunksize,
    )


import sys
sys.stdin = open('5201. 컨테이너 운반.txt','r')
"""
트럭이 운반할 수 있는 무게의 컨테이너가 존재하면 해당 컨테이너를 옮기는 것이 최상
그렇지 않을 경우 내림차순으로 정렬한 컨테이너 중 자신보다 작은 값들 중 최대값
"""
T = int(input())
for tc in range(T):
    N, M = map(int,input().split())
    container = list(map(int,input().split()))
    container.sort(reverse=True)
    truck = list(map(int,input().split()))
    result = 0
    for t in truck:
        if t in container:
            result += t
            container.remove(t)
        else:
            for c in container:
                if t > c:
                    result += c
                    container.remove(c)
                    break
    print('#{} {}'.format(tc+1,result))


import openai
import os

private_key = os.environ.get('CHATGPT_KEY')
openai.api_key = private_key

model_engine = "text-davinci-003"
prompt = "Hello, how are you today?"

completion = openai.Completion.create(
    engine=model_engine,
    prompt=prompt,
    max_tokens=1024,
    n=1,
    stop=None,
    temperature=0.5
)

response = completion.choices[0].text
print(response)


import math
valorRad = int(input("Por favor ingrese el valor radian: "))
Contador = 0
Cs = 8     #Cifras significativas#
Es = float((0.5 * 10 ** -Cs) * 100) ##Error esperado##
print("El numero que digito es : ", Es)
Ea = float(100) ##Error aproximado##
def CalcEa(x, y):  ##Función para hallar el error aproximado##
    CEa = abs(((x - y) / x) * 100)
    return CEa
expoFacto = 0   ## Numero factorial##
AproxAc = 0     ## Aproximado actual      ##
AproxAn = 0     ## Aproximado anterior      ##
Signo = 1       ## Intercalar signo en la ecuación cos x     ##
while Ea > Es:
    Contador += 1
    AproxAc = AproxAc + Signo * (valorRad ** expoFacto) / math.factorial(expoFacto)
    Ea = CalcEa(AproxAc, AproxAn)
    AproxAn = AproxAc
    Signo = Signo * -1
    expoFacto += 2
print ("El resultado del valor actual es :",AproxAc)
print ("El resultado del error aproximado es :",Ea *100, "%")
print ("El numero total de iteraciones es de :",Contador)


"""common operations."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import math
from six.moves import xrange

import tensorflow as tf

import read_data

# Global constants describing the MNIST data set.
NUM_CLASSES = read_data.NUM_CLASSES
NUM_EXAMPLES_IN_TRAIN_SET = read_data.NUM_EXAMPLES_IN_TRAIN_SET

# for learning rate
BASE_LEARNING_RATE         = 0.01
NUM_EPOCHS_PER_DECAY       = 1
LEARNING_RATE_DECAY_FACTOR = 0.95

def _variable_on_cpu(name, shape, initializer):
  """Create a Variable stored on CPU memory.
  """
  with tf.device('/cpu:0'):
    var = tf.get_variable(name, shape, initializer=initializer)
  return var

def variable_with_weight_decay(name, shape, stddev=1e-2,
                               coefficient=1e-3, useMultiGPU=False):
  """Create a Variable with weight decay.
  """
  if useMultiGPU:
    var = _variable_on_cpu(name, shape,
               tf.truncated_normal_initializer(stddev=stddev))
  else:
    var = tf.Variable(tf.truncated_normal(shape, stddev=stddev), name=name)

  if coefficient is not None:
    weight_decay = tf.mul(tf.nn.l2_loss(var),
                          coefficient,
                          name='regularization_loss')
    tf.add_to_collection('losses', weight_decay)

  return var

def define_biases(name, shape, useMultiGPU=False):
  if useMultiGPU:
    biases = _variable_on_cpu(name, shape, tf.constant_initializer(0.0))
  else:
    biases = tf.Variable(tf.zeros(shape), name=name)

  return biases

def cross_entropy_loss(logits, labels, scheme=0):
  """Calculate cross entropy loss.
  """
  if scheme is 0:
    losses = tf.nn.sparse_softmax_cross_entropy_with_logits(logits, labels)

  if scheme is 1:
    labels_one_hot = tf.one_hot(labels, NUM_CLASSES, 1.0, 0.0)
    losses = tf.nn.softmax_cross_entropy_with_logits(logits, labels_one_hot)

  if scheme is 2:
    logits_log_softmax = -tf.nn.log_softmax(logits)
    labels_one_hot = tf.one_hot(labels, NUM_CLASSES, 1.0, 0.0)
    losses = tf.reduce_sum(labels * logits, reduction_indices=1)

  losses_mean = tf.reduce_mean(losses, name='cross_entropy_loss')
  tf.add_to_collection('losses', losses_mean)

  # total loss = cross entropy loss + regularization loss (L2 weight decay)
  return tf.add_n(tf.get_collection('losses'), name='total_loss')

def optimizer(batch_size, global_step):
  """Optimizer.
  """

  num_batches_per_epoch = NUM_EXAMPLES_IN_TRAIN_SET // batch_size
  decay_steps = num_batches_per_epoch * NUM_EPOCHS_PER_DECAY

  # Decay the learning rate exponentially based on the number of steps.
  learning_rate = tf.train.exponential_decay(
      BASE_LEARNING_RATE,
      global_step,
      decay_steps,
      LEARNING_RATE_DECAY_FACTOR,
      staircase=True)

  # Use momentum for the optimizer
  optimizer = tf.train.MomentumOptimizer(learning_rate, 0.9)

  return optimizer

def evaluate(logits, labels):
  """Calculate the accuracy of classifier.
  """
  #correct = tf.equal(tf.argmax(logits, 1), labels)
  #accuracy = tf.reduce_sum(tf.cast(correct, tf.float32))
  correct = tf.nn.in_top_k(logits, labels, 1)
  accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
  return accuracy


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Given a parallel corpus of sentence pairs: with one-to-one of target and source sentences,
produce the score, and optionally alignment for each pair.
"""

import sys
import argparse
import tempfile
import logging

import numpy

from data_iterator import TextIterator
from util import load_config
from alignment_util import combine_source_target_text_1to1
from compat import fill_options

from theano_util import (floatX, numpy_floatX, load_params, init_theano_params)
from nmt import (pred_probs, build_model, prepare_data)
from settings import ScorerSettings

from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams
import theano

def load_scorer(model, option, alignweights=None):

    # load model parameters and set theano shared variables
    param_list = numpy.load(model).files
    param_list = dict.fromkeys([key for key in param_list if not key.startswith('adam_')], 0)
    params = load_params(model, param_list)
    tparams = init_theano_params(params)

    trng, use_noise, \
        x, x_mask, y, y_mask, \
        opt_ret, \
        cost = \
        build_model(tparams, option)
    inps = [x, x_mask, y, y_mask]
    use_noise.set_value(0.)

    if alignweights:
        logging.debug("Save weight mode ON, alignment matrix will be saved.")
        outputs = [cost, opt_ret['dec_alphas']]
        f_log_probs = theano.function(inps, outputs)
    else:
        f_log_probs = theano.function(inps, cost)

    return f_log_probs

def rescore_model(source_file, target_file, output_file, scorer_settings, options):

    trng = RandomStreams(1234)

    def _score(pairs, alignweights=False):
        # sample given an input sequence and obtain scores
        scores = []
        alignments = []
        for i, model in enumerate(scorer_settings.models):
            f_log_probs = load_scorer(model, options[i], alignweights=alignweights)
            score, alignment = pred_probs(f_log_probs, prepare_data, options[i], pairs, normalization_alpha=scorer_settings.normalization_alpha, alignweights =alignweights)
            scores.append(score)
            alignments.append(alignment)

        return scores, alignments

    pairs = TextIterator(source_file.name,
                         target_file.name,
                         options[0]['dictionaries'][:-1],
                         options[0]['dictionaries'][-1],
                         n_words_source=options[0]['n_words_src'],
                         n_words_target=options[0]['n_words'],
                         batch_size=scorer_settings.b,
                         maxlen=float('inf'),
                         use_factor=(options[0]['factors'] > 1),
                         sort_by_length=False) #TODO: sorting by length could be more efficient, but we'd want to resort after

    scores, alignments = _score(pairs, scorer_settings.alignweights)

    source_file.seek(0)
    target_file.seek(0)
    source_lines = source_file.readlines()
    target_lines = target_file.readlines()

    for i, line in enumerate(target_lines):
        score_str = ' '.join(map(str,[s[i] for s in scores]))
        if scorer_settings.verbose:
            output_file.write('{0} '.format(line.strip()))
        output_file.write('{0}\n'.format(score_str))

    # optionally save attention weights
    if scorer_settings.alignweights:
        temp_name = output_file.name + ".json"
        with tempfile.NamedTemporaryFile(prefix=temp_name) as align_OUT:
            for line in alignments:
                if type(line)==list:
                    for l in line:
                        align_OUT.write(l + "\n")
                else:
                    align_OUT.write(line + "\n")
            # combining the actual source and target words.
            combine_source_target_text_1to1(source_file,
                                            target_file,
                                            output_file.name,
                                            align_OUT)

def main(source_file, target_file, output_file, scorer_settings):
    # load model model_options
    options = []
    for model in scorer_settings.models:
        options.append(load_config(model))
        fill_options(options[-1])
    rescore_model(source_file, target_file, output_file, scorer_settings, options)

if __name__ == "__main__":
    scorer_settings = ScorerSettings(from_console_arguments=True)
    source_file = scorer_settings.source
    target_file = scorer_settings.target
    output_file = scorer_settings.output
    level = logging.DEBUG if scorer_settings.verbose else logging.INFO
    logging.basicConfig(level=level, format='%(levelname)s: %(message)s')
    main(source_file, target_file, output_file, scorer_settings)


"""Tests for functions in the pqcoreutils.fileutils module."""
import datetime
import hashlib
import io
import os
import pathlib
import textwrap

import lxml.etree as et
import pytest

import pqcoreutils

from pqcoreutils import build_input_file_list
from pqcoreutils import read_lookup_file
from pqcoreutils import lazy_read_lookup_file
from pqcoreutils import parse_file_to_dict
from pqcoreutils import file_to_lookup
from pqcoreutils.fileutils import concatenate_singleton_files
from pqcoreutils.fileutils import parse_maybe_huge_xml

@pytest.fixture
def test_data_dir(tmpdir_factory):
    """Build a directory containing some files we can walk over to
    test the build_input_file_list function's behaviour."""
    rootdir = tmpdir_factory.mktemp("test_data")
    for i in range(3):
        subdir = rootdir.mkdir(f"test_subdir_{i}")
        for j in range(1, 11, 1):
            for sfx in ['xml', 'txt', 'log', 'bz2']:
                fn = subdir.join(f"test-file-{i}-{j}.{sfx}")
                fn.write("test file.")
    return rootdir

@pytest.fixture
def singleton_xml_files(tmpdir_factory):
    """Build a directory containing some single xml files we can use
    to test outputstreamhandler"""
    rootdir = tmpdir_factory.mktemp("test_xmls")
    xml = textwrap.dedent('''\
        <?xml version='1.0' encoding='utf-8'?>
        <root>
          <title>abc</title>
          <author>Someone</author>
        </root>
        ''')
    for i in range(3):
        fn = rootdir.join(f"{i}.xml")
        fn.write(xml)
    return rootdir

@pytest.fixture
def huge_tree_xml():
    return os.path.join(os.path.dirname(__file__), 'fixtures/huge_tree.xml')

@pytest.fixture
def cdata_xml():
    return os.path.join(os.path.dirname(__file__), 'fixtures/cdata.xml')

@pytest.fixture
def target_spec_file(tmpdir_factory):
    """Create a file containing file paths. It also contains a mix
    of Windows and UNIX line endings, just to be thorough."""
    fn = tmpdir_factory.mktemp("filespec").join("infiles.txt")
    fn.write("""# a comment\r\n/a/b/c.txt\r\n/a/b/d.txt\n/a/e/f.txt""")
    return fn

@pytest.fixture
def sample_flat_file(tmpdir_factory):
    fn = tmpdir_factory.mktemp("tests").join("sample_flat_file.txt")
    fn.write(textwrap.dedent(
        """\
        # Ignore this comment line
        line 1
        line 2
        line 3

        line 4
        """
        ))
    print(type(fn))
    print(isinstance(fn, os.PathLike))
    return fn

@pytest.fixture
def sample_lookup_file(tmpdir_factory):
    fn = tmpdir_factory.mktemp("tests").join("sample_lookup_file.txt")
    fn.write(textwrap.dedent(
        """\
        # Ignore this comment line

        key1|value1
        key2|value2_a;value2_b;value2_c
        key3|value3

        """
        ))
    return fn

@pytest.fixture
def multi_delimiters_lookup(tmpdir_factory):
    fn = tmpdir_factory.mktemp("tests").join("multi_delimiters_lookup.txt")
    fn.write(textwrap.dedent(
        """\
        # Comment

        key1|value1a|value1b
        key2|value2a|value2b
        """
        ))
    return fn

@pytest.fixture
def accumulate_dupe_keys_lookup(tmpdir_factory):
    fn = tmpdir_factory.mktemp("tests").join("accumulate_dks_lookup.txt")
    fn.write(textwrap.dedent(
        """\
        ; Comment

        key1|value1a
        key2|value2a
        key2|value2b
        """
        ))
    return fn

@pytest.fixture
def secondary_split_lookup(tmpdir_factory):
    fn = tmpdir_factory.mktemp("tests").join("2ary_split_lookup.txt")
    fn.write(textwrap.dedent(
        """\
        ; A comment

        key1|value1a;value1b;value1c
        key2|value2a
        key3|value3a;value3b
        key2|value2b;value2c
        """
        ))
    return fn

def test_build_file_list(test_data_dir, target_spec_file):
    """Run build_input_file_list with default params."""
    files = list(build_input_file_list(test_data_dir.strpath))
    assert len(files) == 120

    # Provide a file suffix pattern to limit filter the matches
    files = list(build_input_file_list(test_data_dir.strpath, pattern='*.xml'))
    assert len(files) == 30
    assert all([file.endswith('xml') for file in files])

    # Pass in two directories to scan, and check the output.
    dir1, dir2, dir3 = [
            os.path.join(test_data_dir.strpath, i) for i
            in sorted(os.listdir(test_data_dir.strpath))]
    files = list(build_input_file_list(
        [dir1, dir2], pattern='*.xml'))
    assert len(files) == 20
    assert all([file.endswith('xml') for file in files])

    # Pass in an explicit file name. Should just return it.
    filepath = os.path.join(
            dir1, sorted(os.listdir(dir1))[0])
    assert list(build_input_file_list(filepath)) == [filepath]

    # And the same again, but with the filename wrapped in a list.
    assert list(build_input_file_list([filepath])) == [filepath]

    # Check targets specified in a file. We want to accept
    # UNIX and Windows line endings, so people don't have to remember the
    # right one. Python's str.strip takes care of it for us.
    files = list(build_input_file_list(
        target_spec_file.strpath, from_file=True))
    assert files == ['/a/b/c.txt', '/a/b/d.txt', '/a/e/f.txt']

    # If we get more than one file and are told "from_file=True",
    # ignore all but the first and print a warning.
    files = list(
            build_input_file_list(
                [target_spec_file.strpath, dir1],
                from_file=True))
    assert len(files) == 3

def test_build_file_list_enumerated(test_data_dir, target_spec_file):
    '''The same as the previous test, just using the enumerated=True param to
    get an enumerated list.'''
    results = list(
        build_input_file_list(test_data_dir.strpath, enumerated=True))
    assert len(results) == 120

    assert isinstance(results[0], tuple)
    assert results[0][0] == 0
    assert results[1][0] == 1

    # Same thing, but set enumerated to an integer. Ensure counting starts at
    # this value
    results = list(
        build_input_file_list(test_data_dir.strpath, enumerated=2))

    assert results[0][0] == 2
    assert results[1][0] == 3

    # Check targets specified in a file. We want to accept
    # UNIX and Windows line endings, so people don't have to remember the
    # right one. Python's str.strip takes care of it for us.
    results = list(build_input_file_list(
        target_spec_file.strpath, from_file=True, enumerated=3))

    assert results[0] == (3, '/a/b/c.txt')
    assert results[1] == (4, '/a/b/d.txt')
    assert results[2] == (5, '/a/e/f.txt')
    assert len(results) == 3

def test_lazy_read_lookup_file(sample_flat_file):
    """The function lazy_read_lookup_file is an iterator generator."""
    results = lazy_read_lookup_file(sample_flat_file)
    assert hasattr(results, '__next__')

def test_read_lookup_file(sample_flat_file):
    """Check the file reader."""
    filename = sample_flat_file.strpath
    assert isinstance(filename, str)
    assert read_lookup_file(filename) == [
            'line 1', 'line 2', 'line 3', 'line 4']

def test_read_lookup_file_pathlike(sample_flat_file):
    """Check the file reader can handle an instance of
    os.PathLike."""
    assert isinstance(sample_flat_file, os.PathLike)
    assert read_lookup_file(sample_flat_file) == [
            'line 1', 'line 2', 'line 3', 'line 4']

def test_read_lookup_file_pathlib_path(sample_flat_file):
    """Check the file reader can handle an instance of
    pathlib.Path."""
    filename = pathlib.Path(sample_flat_file.strpath)
    assert isinstance(filename, pathlib.Path)
    assert read_lookup_file(filename) == [
            'line 1', 'line 2', 'line 3', 'line 4']

def test_read_lookup_file_with_string_io():
    """If passed a StringIO object, read it as though it were a file
    and behave as expected."""
    file_data = io.StringIO('''line 1\nline 2\nline 3\nline 4\n''')
    assert read_lookup_file(file_data) == [
        'line 1', 'line 2', 'line 3', 'line 4']

def test_read_lookup_file_with_bytes_io():
    """As above, but using a BytesIO object instead of StringIO"""
    file_data = io.BytesIO(b'''line 1\nline 2\nline 3\nline 4\n''')
    assert read_lookup_file(file_data) == [
        'line 1', 'line 2', 'line 3', 'line 4']

def test_read_lookup_file_fails_on_unknown_param_type():
    with pytest.raises(TypeError) as exc:
        read_lookup_file(123)
    assert "Unsupported type" in str(exc)

def test_read_lookup_file_includes_comments(sample_flat_file):
    data = read_lookup_file(
        sample_flat_file.strpath,
        transform=lambda l: l.strip(),
        strip_comments=False,
        comment_chars='#'
        )
    assert data[0].startswith('#')
    assert data[1:] == ['line 1', 'line 2', 'line 3', 'line 4']

def test_parse_file_to_dict(sample_lookup_file):
    """Make sure we get sensible values back when converting
    a lookup file to a dict"""
    data = parse_file_to_dict(sample_lookup_file.strpath)
    assert isinstance(data, dict)
    assert data['key1'] == 'value1'

def test_lookup_with_multiply_occurring_delimiter(multi_delimiters_lookup):
    """Sometimes, the delimiter will appear more than once on a line."""
    data = parse_file_to_dict(multi_delimiters_lookup.strpath)
    assert data['key1'] == 'value1a|value1b'

def test_accumulate_duplicated_keys(accumulate_dupe_keys_lookup):
    """Accumulate multiple values for a duplicated key"""
    data = parse_file_to_dict(
            accumulate_dupe_keys_lookup.strpath,
            comment_chars=';', accumulate_values=True)
    assert data['key1'] == ['value1a']
    assert data['key2'] == ['value2a', 'value2b']

def test_secondary_split_no_accumulation(secondary_split_lookup):
    """If the caller passes a value for secondary_delimiter, return a dict
    whose values are lists, generated from splitting the value on that
    secondary_delimiter. e.g. key1|value1a;value1b;value1c ->
    {'key1': ['value1a', 'value1b', 'value1c']}
    """
    data = parse_file_to_dict(
            secondary_split_lookup.strpath,
            comment_chars=';', delimiter='|',
            secondary_delimiter=';')
    assert data['key1'] == ['value1a', 'value1b', 'value1c']
    assert data['key2'] == ['value2b', 'value2c']
    assert data['key3'] == ['value3a', 'value3b']

def test_secondary_split_with_accumulation(secondary_split_lookup):
    """Now the same thing, but with accumulate_values=True as well"""
    data = parse_file_to_dict(
            secondary_split_lookup.strpath,
            comment_chars=';', delimiter='|',
            secondary_delimiter=';', accumulate_values=True)
    assert data['key2'] == ['value2a', 'value2b', 'value2c']

def test_file_to_lookup_causes_warning(sample_lookup_file):
    '''Make sure that a call to file_to_lookup results in a DeprecationWarning.
    It should also still return the expected results.'''
    with pytest.warns(DeprecationWarning):
        data = file_to_lookup(sample_lookup_file.strpath)

    assert isinstance(data, dict)
    assert data['key1'] == 'value1'

def test_concatenate_singleton_files(singleton_xml_files, tmpdir_factory):
    directory = singleton_xml_files.strpath
    output_dir = tmpdir_factory.mktemp("tempoutput")
    concatenate_singleton_files(
        directory, output_dir.strpath,
        product="test", pattern="*.xml")
    assert len(os.listdir(output_dir.strpath)) == 1
    # expected = [i.readlines() for i in os.listdir(directory)]
    expected = []
    for i in os.listdir(directory):
        for line in open(os.path.join(directory, i)).readlines():
            expected.append(line)
    outfilename = os.listdir(output_dir.strpath)[0]
    result = open(os.path.join(output_dir, outfilename)).readlines()
    assert result == expected

def test_concatenate_singleton_files_gets_product(
        singleton_xml_files, tmpdir_factory):
    directory = singleton_xml_files.strpath
    output_dir = tmpdir_factory.mktemp("tempoutput")
    with pytest.raises(TypeError):
        concatenate_singleton_files(
            directory, output_dir.strpath, pattern="*.xml")

def test_can_parse_huge_tree(huge_tree_xml):
    assert parse_maybe_huge_xml(huge_tree_xml)

def test_cdata_retained_from_parse_maybe_huge_xml(cdata_xml):
    tree = parse_maybe_huge_xml(cdata_xml)
    text = tree.xpath('.//text')[0]
    assert et.tostring(text) == b'<text><![CDATA[Some text]]></text>'

def test_concatenate_singleton_files_handles_input_record_numbers(
        singleton_xml_files, tmpdir_factory):
    output_dir = tmpdir_factory.mktemp("tempoutput")
    directory = singleton_xml_files.strpath
    date = datetime.date.today().strftime('%Y%m%d')
    concatenate_singleton_files(
        directory, output_dir.strpath,
        pattern="*.xml", product="product", records=2)
    assert os.path.isfile(f'{output_dir}/CH_SS_product_{date}_000.xml')
    assert os.path.isfile(f'{output_dir}/CH_SS_product_{date}_001.xml')

def test_file_size(sample_lookup_file, mocker):
    """Make sure we correctly call os.stat"""
    # We don't need to test the return value, since we can be
    # fairly sure os.stat's behaviour is fully tested in the
    # python test suite.
    mocker.patch('os.stat')
    pqcoreutils.fileutils.file_size(sample_lookup_file.strpath)
    assert os.stat.called_once_with(sample_lookup_file.strpath)

def test_file_checksum_generates_md5(sample_lookup_file):
    """Ensure we get back the expected value for the file's
    checksum."""
    # Note: Here we *do* want to test returns, since we have
    # logic that needs to be tested to do the right thing (i.e.
    # specifying the hash algorithm to use, and digest style we
    # want)
    d = hashlib.new('md5')
    with open(sample_lookup_file.strpath, 'rb') as f:
        while True:
            buf = f.read(65536)
            if not buf:
                break
            d.update(buf)

    md5 = d.hexdigest()
    assert md5 == pqcoreutils.fileutils.file_checksum(
        sample_lookup_file)

def test_file_checksum_generates_sha256(sample_lookup_file):
    """User requests the sha256 checksum"""
    d = hashlib.new('sha256')
    with open(sample_lookup_file.strpath, 'rb') as f:
        while True:
            buf = f.read(65536)
            if not buf:
                break
            d.update(buf)

    sha256 = d.hexdigest()
    assert sha256 == pqcoreutils.fileutils.file_checksum(
        sample_lookup_file.strpath, algorithm='sha256')

def test_file_checksum_returns_plain_digest(sample_lookup_file):
    """The user asked the plain sha256 digest"""
    d = hashlib.new('sha256')
    with open(sample_lookup_file.strpath, 'rb') as f:
        while True:
            buf = f.read(65536)
            if not buf:
                break
            d.update(buf)

    sha256_digest = d.digest()
    assert sha256_digest == pqcoreutils.fileutils.file_checksum(
        sample_lookup_file.strpath, algorithm='sha256',
        hexdigest=False)


import sys
sys.stdin = open('back2669.txt')
T = 4
#사각형 x1, y1, x2, y2 정보 한줄당 하나

rect = [list(map(int, input().split())) for i in range(T)]
# print(rect) # react[1~4] 숫자에 따라 각 사각형 x1~ y2 정보 담김
onesquare_area = 0
# 1.하나도 안 겹친 사각형들의 넓이 구하자
for i in range(len(rect)):
    onesquare_area += (rect[i][2] - rect[i][0]) * (rect[i][3] - rect[i][1])
# print(onesquare_area)

# 1.사각형 두개 중 비교해서 겹치는 것 받자
# 1-1. 우선 겹치는 조건 확인 하자
# 조건
# x=1 [0] x2 [2] y1[1] y2[3]
#1. x축은 a x1이 b x1보다 작고 a x2가 b x1보다 크다
#1-1. y축은 a y1이 b y1보다 작고 a y2 b y1보다 큼
#1-2. y축 a y1이 b y1보다 크고 b의 y2보다 작다.
#2. a x1이 b x1보다 크고 a x2는 b x2보다 작거나 같거나 크다.
#2-1. y축은 a y1이 b y1보다 작고 a y2 b y1보다 큼
#2-2. y축 a y1이 b y1보다 크고 b의 y2보다 작다.
twosquare=[]
# 위 조건을 겹친 박스안에서도 똑같이
# y축은
for i in range(T):
    for j in range(i+1, 4):
        if rect[i][0] < rect[j][2] and rect[i][2] > rect[j][0]:
            if rect[i][0] < rect[j][1] and rect[i][3] > rect[j][1]:
                twosquare.append([rect[i], rect[j]])
            if rect[j][1] < rect[i][1] < rect[j][3]:
                twosquare.append([rect[i], rect[j]])
        elif rect[i][2] < rect[j][0] and rect[i][0] > rect[j][2]:
            if rect[i][1] < rect[j][1] and rect[i][3] > rect[j][1]:
                twosquare.append([rect[i], rect[j]])
            if rect[j][1] < rect[i][1] < rect[j][3]:
                twosquare.append([rect[i], rect[j]])
### 이까지 twosquare 는 겹친 사각형 목록들

# 1-2 가져온 변수에서 넓이를 구하자
# 면적들 구하자
# x1은 큰값 , x2는 작은거 y1은  큰거 y2는 작은거
twosquare_area =[]
newbox=[]
for i in range(len(twosquare)):
    new_x1 = max(twosquare[i][0][0], twosquare[i][1][0])
    new_x2 = min(twosquare[i][0][2], twosquare[i][1][2])
    new_y1 = max(twosquare[i][0][1], twosquare[i][1][1])
    new_y2 = min(twosquare[i][0][3], twosquare[i][1][3])
    area = (new_x2 - new_x1) * (new_y2 - new_y1)
    twosquare_area.append(area)
# print(sums)
### 이까지 sums는 겹치는 사각형들의 넓이들
    newbox.append([new_x1, new_y1, new_x2, new_y2])
# print(newbox)
### newbox는 겹치는 세박스

# 2, 세개 중 비교해서 겹치는건 두개 중 비교랑도 똑같네
# 세개 겹치는 것도 위에서 구한 박스에서 처리하자
newboxinfo= []
for i in range(len(newbox)):
    x1 = newbox[0][0]
    if x1 < newbox[i][0]:
        x1 = newbox[i][0]
newboxinfo.append(x1)
for i in range(len(newbox)):
    y1 = newbox[0][1]
    if y1 < newbox[i][1]:
        y1 = newbox[i][1]
newboxinfo.append(y1)
for i in range(len(newbox)):
    x2 = newbox[0][2]
    if  x2 > newbox[i][2]:
        x2 = newbox[i][2]
newboxinfo.append(x2)
for i in range(len(newbox)):
    y2 = newbox[0][3]
    if y2 > newbox[i][3]:
        y2 = newbox[i][3]
newboxinfo.append(y2)

### 이까지 newboxinfo는 세개 겹친 박스의 좌표들

###여기서 부터는 세개 겹친 사각형 넓이
newboxarea = (newboxinfo[2] - newboxinfo[0]) * (newboxinfo[3] - newboxinfo[1])
# print(newboxarea)

# 결론적으로 4사각형 합에서 2개 겹친 사각형합 빼주고 세개 겹친 사각형을 1번 더해주면 됌
result = onesquare_area - sum(twosquare_area) + newboxarea
print(result)
# 2.1

# 위 조건을 겹친 박스안에서도 똑같이

# # 면적은 이차원 배열에서 그 안에 각 0, 1, 2, 3에 해당하는
# # 1차원 리스트 안에 해당 사각형의 (idx0-2) * abs(idx1-3)
# ### x1 0 y1 1 x2 2 y2 3
# # 1. 2차원 배열을 순회할 맨 겉의 for문
# sums = 0
# for i in range(4):
#     for j in range(1):
#         result = (abs(rects[i][j] - rects[i][j+2])) * (abs(rects[i][j+1] - rects[i][j+3]))
#         sums += result
# print(sums)
# # 아래에서 진행할 직사각형 넓이들을 담을 sum변수
# # 그 안에서 1차원 배열의 각 콜럼을 비교할 for문
# # 구하고 나서 sum에 추가
#
# # 세개 겹치는 녀석
# # for i in range(3):
# #     for j in range(4):
# #         rect[]
# #
#
# # 일단 두개 사각형이라도 해보자
#
#
# # 0이 가장 큰거 x1, 2가 가장 가장 큰거 y1, 3가장 작은거 x2 y2는 가장 큰거
# # 겹치는거 해결
# # 세개 겹치는거 -2빼고 둘이서 겹치는거 하나씩
#
# # x1, 리스트[i]두개중  x좌표 큰값 y좌표 큰값
# # x2는 리스트 두개 중 3인덱스가 작은걸로
# # y1은 리스트 두개중 1인덱스가 큰걸로
# # y2는 ''중 작은걸로
# 1,2 4,4   3,2 4,4까지는 모두 겹친거       1~4 2~4
# 2,3 5,7   0과1은 2,3 4,4까지      2~5  3~7
# 3,1 6,5   0과 2는 3,2 4,4      3~6  3~5
#           1과 2는 3,3 5,5까지
# #
# 7,3 8,6         7~8  3~6


import numpy as np
from scipy.special import erfc

def Qfunction(x):
    return 0.5 * erfc( x / np.sqrt(2) )

Pt_specified = 1e-6

# beta = 0.1
M = 2
Rb = 10e9 # data rate [b/s]
Tb = 1 / Rb # bit duration [s]
TS = Tb * np.log2(M)
beta = np.sqrt(3 * Pt_specified / (M**2 - 1) )
N0 = 1e-22 #[ W/Hz ]

print('Tb = ', Tb, 's')
print('TS = ', TS, 's')

LdB = -40 # channel loss
L = 10 ** (LdB/10)
print('Channel loss in linear scale = ', L)

Pt = (M ** 2 - 1) * beta ** 2.0 / 3
print('Transmitted power = ', Pt, 'W')

Pr = L * Pt
print('Received power = ', Pr, 'W')

Er = Pr * TS
print('Received energy in one symbol duration = ', Er, 'J')

Eb = Pr * Tb
print('Received energy in one bit duration = ', Eb, 'J')

SNRb = Eb / N0
print('Signal-to-noise per bit = ', SNRb)

SNRbdB = 10 * np.log10(SNRb)
print('Signal-to-noise per bit = ', SNRbdB)
q = np.sqrt( 6 * SNRb * np.log2(M) / (M**2 - 1) )
Pb = 2 * (M-1) / (M * np.log2(M) ) * Qfunction(q)

print('Error probability : %e' %Pb)

B = 1 / TS
print('Bandwidth : ', B, 'Hz')
print('Bandwidth : ', B/1e9, 'GHz')


from lxml import html
import requests
import re
import json
import time

def gethtmlpage(urlPagina):
    page = requests.get(urlPagina)
    tree = html.fromstring(page.content)
    return tree

def prepare(unformat_str):
    return unformat_str.strip().upper().replace(',','').replace('\n\r','').replace('\r\n','').replace('\n','').replace('\r','')

def prepareHostName(unformat_str):
    return unformat_str.strip().upper().replace('.','_').replace(':','_')

def prepareNumber(strnum):
    regex = '(\d+.\d+)(.*)'
    matches = re.search(regex, strnum )
    rn = matches.group(1).strip()
    un = matches.group(2).strip()
    val = 0
    if (un == 'B'):
        val = float(rn)*1024*1024
    if (un == 'MB'):
        val = float(rn)*1024
    if (un == 'GB'):
        val = float(rn)
    return int(val)

def translateStatus(status):
    if status == 'ALIVE':
        return 1
    return 0

def getmastertitle(st):
    tit = prepare(''.join(st))
    regex = 'SPARK MASTER AT SPARK:\/\/(.*)'
    matches = re.search(regex, tit )
    title = matches.group(1).strip()
    return title

def tratacores(st):
    regex = '(.*)TOTAL(.*)USED'
    matches = re.search(regex, st  )
    total = matches.group(1).strip()
    used =  matches.group(2).strip()
    return total,used

def tratadrivers(st):
    regex = '(.*)RUNNING(.*)COMPLETED'
    matches = re.search(regex, st  )
    rn = matches.group(1).strip()
    cp =  matches.group(2).strip()
    return rn,cp

def trataapps(st):
    regex = '([0-9]+).+([0-9]+)'
    matches = re.search(regex, st  )
    rn = matches.group(1).strip()
    cp =  matches.group(2).strip()
    return rn,cp

def trataworkercores(st):
    regex = '(.*)\((.*)USED\)'
    matches = re.search(regex, st  )
    total = matches.group(1).strip()
    used =  matches.group(2).strip()
    return total,used

def trataworkermemory(st):
    regex = '(.*)\((.*)USED\)'
    matches = re.search(regex, st  )
    total = matches.group(1).strip()
    used =  matches.group(2).strip()
    return total,used

def getmasterdata(page):
    #page = gethtmlpage(masterurl)
    itens = page.xpath('//div[@class="span12"][1]/ul/li')
    metrics_raw = {}
    #for item in itens[1:]:
    for item in itens:
        x = item.xpath('strong/text()')
        y = item.xpath('text()')
        t = prepare(''.join(y))
        m = x[0]
        if m == "REST URL:":
            metrics_raw[m] = t
        if m == "Alive Workers:":
            metrics_raw[m] = t
        if m == "Cores in use:":
            total,used = tratacores(t)
            metrics_raw['CORES_TOTAL'] = total
            metrics_raw['CORES_USED'] = used
        if m == "Memory in use:":
            total,used = tratacores(t)
            metrics_raw['MEMORY_TOTAL'] = total
            metrics_raw['MEMORY_USED'] = used
        if m == "Applications:":
            r,c=trataapps(t)
            metrics_raw['RUNNING_APPS'] = r
            metrics_raw['COMPLETED_APPS'] = c
        if m == "Drivers:":
            r,c = tratadrivers(t)
            metrics_raw['RUNNING_DRV'] = r
            metrics_raw['COMPLETED_DRV'] = c
        if m == "Status:":
            metrics_raw[m] = t
    t = page.xpath('//h3[@style="vertical-align: middle; display: inline-block;"]/text()')
    title = getmastertitle(t)
    metrics_raw['MASTER_HOST'] = title
    return metrics_raw

def getworkermaindata(page):
    #page = gethtmlpage(masterurl)
    itens = page.xpath('//div[@class="span12"][1]/ul/li')
    worker_metrics_raw = {}
    #for item in itens[1:]:
    for item in itens:
        x = item.xpath('strong/text()')
        y = item.xpath('text()')
        t = prepare(''.join(y))
        m = x[0].strip()
        if m == "Cores:":
            mem_tot,mem_used = trataworkercores(t)
            worker_metrics_raw['CORES_TOTAL'] = mem_tot
            worker_metrics_raw['CORES_USED'] = mem_used
        if m == "Memory:":
            mem_tot,mem_used = trataworkermemory(t)
            worker_metrics_raw['MEMORY_TOTAL'] = mem_tot
            worker_metrics_raw['MEMORY_USED'] = mem_used
        if m == "ID:":
            worker_metrics_raw['WORKER_ID'] = t
    return worker_metrics_raw

def getworkerslink(page):
    #itens = page.xpath('//tr/td/a')
    itens = page.xpath('/html/body/div/div[3]/div/table/tbody/tr/td/a')

    workers_links = []
    for item in itens:
        workers_links.append(item.get('href'))
    return workers_links

def getdatafromspark(url):
    page = gethtmlpage(url)
    metrics_raw = getmasterdata(page)
    workers = []
    workers_links = getworkerslink(page)
    for link in workers_links:
        p =  gethtmlpage(link)
        worker_data = getworkermaindata(p)
        workers.append(worker_data)
    metrics_raw['workers'] = workers
    return metrics_raw

def getmastermetricname(masterhost,compname,name):
    #return "Spark|"+masterhost+"|"+compname+":"+name
    return "Spark|"+masterhost+":"+name

def getworkermetricname(masterhost,worker,compname,name):
    return "Spark|"+masterhost+"|"+ worker+":"+name

def makemetric(type,masterhost,name,value):
    metric = {"type" : type, "name" : getmastermetricname(masterhost,'compname',name), "value" : value}
    return metric

def makeworkermetric(type,masterhost,workername,name,value):
    metric = {"type" : type, "name" : getworkermetricname(masterhost,workername,'compname',name), "value" : value}
    return metric

def getApmMetrics(spark_data):
    apm_metrics = {'metrics': ''}

    #master data
    master_alivew = makemetric('IntCounter',prepareHostName(spark_data['MASTER_HOST']),'Alive Workers',spark_data['Alive Workers:'])
    master_cputotal = makemetric('IntCounter',prepareHostName(spark_data['MASTER_HOST']),'Cores Total',spark_data['CORES_TOTAL'])
    master_cpuused = makemetric('IntCounter',prepareHostName(spark_data['MASTER_HOST']),'Cores Used',spark_data['CORES_USED'])
    master_memtotal = makemetric('IntCounter',prepareHostName(spark_data['MASTER_HOST']),'Memory Total',prepareNumber(spark_data['MEMORY_TOTAL']))
    master_memused = makemetric('IntCounter',prepareHostName(spark_data['MASTER_HOST']),'Memory Used',prepareNumber(spark_data['MEMORY_USED']))
    master_run_apps = makemetric('IntCounter',prepareHostName(spark_data['MASTER_HOST']),'Running Applications',spark_data['RUNNING_APPS'])
    master_run_drvs = makemetric('IntCounter',prepareHostName(spark_data['MASTER_HOST']),'Running Drivers',spark_data['RUNNING_DRV'])
    status = makemetric('IntCounter',prepareHostName(spark_data['MASTER_HOST']),'Status',translateStatus(spark_data['Status:']))
    m = [master_alivew,master_cputotal,master_cpuused,master_memtotal,master_memused,master_run_apps,master_run_drvs,status]

    #worker data
    workers_data = spark_data['workers']
    worker_metrics = []
    for wd in workers_data:
        m.append(makeworkermetric('IntCounter',prepareHostName(spark_data['MASTER_HOST']),wd['WORKER_ID'],'Memory Total',prepareNumber(spark_data['MEMORY_TOTAL'])))
        m.append(makeworkermetric('IntCounter',prepareHostName(spark_data['MASTER_HOST']),wd['WORKER_ID'],'Memory Used',prepareNumber(spark_data['MEMORY_USED'])))

    apm_metrics['metrics'] = m
    return apm_metrics

def sendToApm(apm_metrics):
    strmetrics = json.dumps(apm_metrics)
    headers = {
        'Content-Type': 'application/json',
    }
    r = requests.post(url = APM_EPA_URL, headers=headers, data = strmetrics)
    #print(r.status_code)
    return

def getConfig():
    with open('config.json', 'r') as f:
        config = json.load(f)
    return config

def collectData():
    spark_data_old = {}
    spark_data_final = {}
    print("-------------------------------------------")
    print("Collecting data from SPARK")
    print("Spark    :",SPARK_URL)
    print("APM_EPA  :",APM_EPA_URL)
    print("POLLING(S)  :",POLLING)
    print("-------------------------------------------")
    while True:
        try:
            spark_data = getdatafromspark(SPARK_URL)
            apm_metrics = getApmMetrics(spark_data)
            sendToApm(apm_metrics)
            time.sleep(POLLING)
        except Exception as e:
            print('Erro !', e)
            time.sleep(POLLING)
            break

configfile = getConfig()
SPARK_URL = configfile["spark_console_url"]
APM_EPA_URL = 'http://'+configfile["apm_epa_host"]+':'+configfile["apm_epa_port"]+'/apm/metricFeed'
POLLING = int(configfile["polling_seconds"])

collectData()


"""
KULLANICIDAN 0-100 ARASI BİR SAYI TUTMASI İSTENİYOR. PROGRAM BU SAYIYI BULMAYA ÇALIŞIYOR.
PROGRAMIN TAHMİN ETTİĞİ SAYI BÜYÜK İSE "B" KÜÇÜK İSE "K" DOĞRU İSE "D" KARAKTERLERİNİN GİRİLMESİ İSTENİYOR.
HATA GİRİŞ OLUR İSE TEKRAR SORULMAKTADIR. "D" DEĞERİ ALINDIĞINDA DA EKRANA TAHMİN SAYISI VE HATA SAYISI YAZILMAKTADIR.
"""
minn = 0 #iLK DEĞERLERİN VERİLMESİ
maxx = 100 #İLK DEĞERLERİN VERİLMESİ
tahmin = 0 #İLK DEĞERLERİN VERİLMESİ
hata = 0 #İLK DEĞERLERİN VERİLMESİ
while True: #SONSUZ DÖNDÜ OLUŞTURULDU VE SADECE "break" KOMUTU İLE DÖNGÜ SONLANDIRILACAK
    sayi = int(((maxx - minn)/2) + minn) #TAHMİN SAYİSİNİN ORTA SAYI ALINARAK OLUŞTURULMASI
    print("Tutulan sayi:", sayi) #TAHMİN EDİLEN SAYININ EKRANA YAZILMASI
    sonuc = input("Sayi (D)oğru mu, (B)üyük mü, (K)üçük mü?:") #TUTULAN SAYININ SORULMASI. TAHMİN BÜYÜK İSE B, KÜÇÜK İSE K GİRİLMELİDİR.
    tahmin = tahmin+1
    if sonuc == "D": # SAYI DOĞRU İSE BU KISIM ÇALIŞTIRILIYOR
        print("\nSonuç:", sayi)
        print("Tahmin sayisi:", tahmin-hata)
        print("Hatali giris sayisi:", hata)
        break #SAYI DOĞRU OLDUĞUNDAN WHİLE DÖNGÜSÜNDEN ÇIKILIYOR.
    elif sonuc == "B": # TAHMİN EDİLEN SAYI BÜYÜK İSE BU KISIM ÇALIŞTIRILIYOR
        maxx = sayi # TAHMNİN EDİKEN SAYI BÜYÜK OLDUĞUNDAN MAXX DEĞERE BU SAYI ATILIYOR VE YENİ DÖNGÜYE GEÇİLİYOR
    elif sonuc == "K": # TAHMİN EDİLEN SAYI KÜÇÜK İSE BU KISIM ÇALIŞTIRILIYOR
        minn = sayi # TAHMİN EDİLEN SAYI KÜÇÜK OLDUĞUNDAN MİNN SAYISINA BU SAYI ATILIYOR VE YENİ DÖNGÜYE GEÇİLİYOR.
    else:
        print("Hatalı değer girdiniz. Tekrar deneyiniz.")
        hata = hata+1


def unikati(s):
    c=[]
    for x in s:
        x=str(x)
        x=x.split()
        x=x[0]
        if len(x)<2:
            x = int(x)
        if x not in c:
            c.append(x)
    return c

def avtor(tvit):
    x = tvit.split()
    x = x[0]
    return x[:-1]

def vsi_avtorji(tviti):
    c = []
    for x in tviti:
        x = x.split()
        x = x[0]
        x = x[:-1]
        if x not in c:
            c.append(x)
    return c

def izloci_besedo(beseda):
    for x in beseda:
        if ord(x) >= 48 and ord(x) <= 57 or \
            ord(x) >= 65 and ord(x) <= 90 or \
            ord(x) >= 97 and ord(x) <= 122:
            break
        else:
            beseda = beseda[1:]

    for x in range(len(beseda)-1,0, -1):
        if ord(beseda[x]) >= 48 and ord(beseda[x]) <= 57 or \
                                ord(beseda[x]) >= 65 and ord(beseda[x]) <= 90 or \
                                ord(beseda[x]) >= 97 and ord(beseda[x]) <= 122:
            break
        else:
            beseda = beseda[:-1]

    return beseda

def se_zacne_z(tvit, c):
    t=[]
    tvit=tvit.split()
    for x in tvit:
        if x[0]==c:
            t+=(izloci_besedo(x),)
    return t

def zberi_se_zacne_z(tviti, c):
    t=[]
    for x in tviti:
        f=se_zacne_z(x,c)
        for s in f:
            if s not in t:
                t+=(s,)
    return t

def vse_afne(tviti):
    return zberi_se_zacne_z(tviti,"@")

def vsi_hashtagi(tviti):
    return (zberi_se_zacne_z(tviti, "#"))

def vse_osebe(tviti):
    o=[]
    for x in tviti:
        for y in range(0,len(x)):
            if x[y]==":":
                if x[:y] not in o:
                    o.append(x[:y])
    x=vse_afne(tviti)
    for z in x:
        if z not in o:
           o.append(z)

    o=sorted(o)
    return o

def custva(tviti, hashtagi):
    o=[]
    for x in tviti:
        for z in hashtagi:
            if "#"+z in x:
                for y in range(0, len(x)):
                    if x[y] == ":":
                        if x[:y] not in o:
                            o.append(x[:y])
    o = sorted(o)
    return o

def se_poznata(tviti, oseba1, oseba2):
    for x in tviti:
        if "#"+oseba1 not in x and "#"+oseba2 not in x:
            if oseba1 in x and oseba2 in x:
                return True
    else:
        return False

import unittest

class TestTviti(unittest.TestCase):
    tviti = [
        "sandra: Spet ta dež. #dougcajt",
        "berta: @sandra Delaj domačo za #programiranje1",
        "sandra: @berta Ne maram #programiranje1 #krneki",
        "ana: kdo so te @berta, @cilka, @dani? #krneki",
        "cilka: jst sm pa #luft",
        "benjamin: pogrešam ano #zalosten",
        "ema: @benjamin @ana #split? po dvopičju, za začetek?",
    ]

    def test_unikat(self):
        self.assertEqual(unikati([1, 2, 1, 1, 3, 2]), [1, 2, 3])
        self.assertEqual(unikati([1, 3, 2, 1, 1, 3, 2]), [1, 3, 2])
        self.assertEqual(unikati([1, 5, 4, 3, 2]), [1, 5, 4, 3, 2])
        self.assertEqual(unikati([1, 1, 1, 1, 1]), [1])
        self.assertEqual(unikati([1]), [1])
        self.assertEqual(unikati([]), [])
        self.assertEqual(unikati(["Ana", "Berta", "Cilka", "Berta"]), ["Ana", "Berta", "Cilka"])

    def test_avtor(self):
        self.assertEqual(avtor("janez: pred dvopičjem avtor, potem besedilo"), "janez")
        self.assertEqual(avtor("ana: malo krajse ime"), "ana")
        self.assertEqual(avtor("benjamin: pomembne so tri stvari: prva, druga in tretja"), "benjamin")

    def test_vsi_avtorji(self):
        self.assertEqual(vsi_avtorji(self.tviti), ["sandra", "berta", "ana", "cilka", "benjamin", "ema"])
        self.assertEqual(vsi_avtorji(self.tviti[:3]), ["sandra", "berta"])

    def test_izloci_besedo(self):
        self.assertEqual(izloci_besedo("@ana"), "ana")
        self.assertEqual(izloci_besedo("@@ana!!!"), "ana")
        self.assertEqual(izloci_besedo("ana"), "ana")
        self.assertEqual(izloci_besedo("!#$%\"=%/%()/Ben-jamin'"), "Ben-jamin")

    def test_vse_na_crko(self):
        self.assertEqual(se_zacne_z("Benjamin $je $skocil! Visoko!", "$"), ["je", "skocil"])
        self.assertEqual(se_zacne_z("Benjamin $je $skocil! #Visoko!", "$"), ["je", "skocil"])
        self.assertEqual(se_zacne_z("ana: kdo so te @berta, @cilka, @dani? #krneki", "@"), ["berta", "cilka", "dani"])

    def test_zberi_na_crko(self):
        self.assertEqual(zberi_se_zacne_z(self.tviti, "@"), ['sandra', 'berta', 'cilka', 'dani', 'benjamin', 'ana'])
        self.assertEqual(zberi_se_zacne_z(self.tviti, "#"), ['dougcajt', 'programiranje1', 'krneki', 'luft', 'zalosten', 'split'])

    def test_vse_afne(self):
        self.assertEqual(vse_afne(self.tviti), ['sandra', 'berta', 'cilka', 'dani', 'benjamin', 'ana'])

    def test_vsi_hashtagi(self):
        self.assertEqual(vsi_hashtagi(self.tviti), ['dougcajt', 'programiranje1', 'krneki', 'luft', 'zalosten', 'split'])

    def test_vse_osebe(self):
        self.assertEqual(vse_osebe(self.tviti), ['ana', 'benjamin', 'berta', 'cilka', 'dani', 'ema', 'sandra'])

class TestDodatna(unittest.TestCase):
    tviti = [
        "sandra: Spet ta dež. #dougcajt",
        "berta: @sandra Delaj domačo za #programiranje1",
        "sandra: @berta Ne maram #programiranje1 #krneki",
        "ana: kdo so te @berta, @cilka, @dani? #krneki",
        "cilka: jst sm pa #luft",
        "benjamin: pogrešam ano #zalosten",
        "ema: @benjamin @ana #split? po dvopičju, za začetek?",
    ]

    def test_custva(self):
        self.assertEqual(custva(self.tviti, ["dougcajt", "krneki"]), ["ana", "sandra"])
        self.assertEqual(custva(self.tviti, ["luft"]), ["cilka"])
        self.assertEqual(custva(self.tviti, ["meh"]), [])

    def test_se_poznata(self):
        self.assertTrue(se_poznata(self.tviti, "ana", "berta"))
        self.assertTrue(se_poznata(self.tviti, "ema", "ana"))
        self.assertFalse(se_poznata(self.tviti, "sandra", "ana"))
        self.assertFalse(se_poznata(self.tviti, "cilka", "luft"))
        self.assertFalse(se_poznata(self.tviti, "cilka", "balon"))

if __name__ == "__main__":
    unittest.main()


def saddle_points(matrix):
    if is_matrix_empty(matrix):
        return []
    if not has_matrix_correct_shape(matrix):
        raise ValueError("incorrect matrix shape")

    saddle_points = []
    for i, row in enumerate(matrix):
        for j, num in enumerate(row):
            if num == max(row) and num == min([row[j] for row in matrix]):
                saddle_points.append({"row": i+1, "column": j+1})
    return saddle_points

def has_matrix_correct_shape(matrix):
    shape = len(matrix[0])
    for row in matrix:
        if shape != len(row):
            return False
    return True

def is_matrix_empty(matrix):
    if matrix == []:
        return True
    return False


import os
import sys
import streamlit as st
from PIL import Image
import matplotlib.pyplot as plt
import torch
import torch.optim as optim
from torch import nn
from torch.utils.data import random_split, Dataset, DataLoader
import torchvision
from torchvision import datasets, transforms
import torchvision.transforms as tt

def metrics(preds, labels):
    acc = accuracy(preds, labels)
    prec = precision(preds, labels)
    rec = recall(preds, labels)
    return acc, prec, rec
class BaseModel(nn.Module):
    def training_step(self,batch):
        loss_func = torch.nn.BCELoss()
        images,labels = batch
        out = self(images).squeeze(1)
        labels = labels.to(torch.float32)
        loss = loss_func(torch.sigmoid(out),labels)
        acc, prec, rec = metrics(torch.sigmoid(out), labels)
        return loss, acc, prec, rec

    def validation_step(self,batch):
        images, labels = batch
        with torch.inference_mode():
            loss_func = torch.nn.BCELoss()
            out = self(images).squeeze(1)
            labels = labels.to(torch.float32)
            loss = loss_func(torch.sigmoid(out),labels)
        acc, rec, prec = metrics(torch.sigmoid(out), labels)
        return {"val_loss":loss.detach(),"val_acc":acc, "val_prec":prec, "val_rec":rec}

    def validation_epoch_end(self,outputs):
        batch_losses = [loss["val_loss"] for loss in outputs]
        loss = torch.stack(batch_losses).mean()
        batch_accuracy = [accuracy["val_acc"] for accuracy in outputs]
        acc = torch.stack(batch_accuracy).mean()
        batch_precision = [precision["val_prec"] for precision in outputs]
        prec = torch.stack(batch_precision).mean()
        batch_recall = [recall["val_rec"] for recall in outputs]
        rec = torch.stack(batch_recall).mean()
        f1 = 2 * (prec * rec) / (prec + rec)
        return {"val_loss":loss.item(),"val_acc":acc.item(), "val_prec": prec.item(), "val_rec": rec.item(), "val_f1": f1.item()}

    def epoch_end(self, epoch, result):
        print(f'Epoch [{epoch}], last_lr: {result["lrs"][-1]:.5f}, train_loss: {result["train_loss"]:.4f}, val_loss: {result["val_loss"]:.4f}, train_acc: {result["train_acc"]:.4f}, val_acc: {result["val_acc"]:.4f}')

class CustomModelConv(BaseModel):
    def __init__(self, num_classes):
        super().__init__()
        self.conv_first = nn.Conv2d(3, 12, 3, 2)
        self.model = torch.hub.load('pytorch/vision:v0.10.0', 'resnet34', pretrained=True)
        self.model.conv1 = nn.Conv2d(12, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
        self.model.fc = nn.Sequential(
          nn.Linear(512, 256),
          nn.Dropout(0.2),
          nn.ReLU(),
          nn.Linear(256, 64),
          nn.Dropout(0.3),
          nn.ReLU(),
          nn.Linear(64, num_classes)
        )

    def freeze_model(self):
        for param in self.model.parameters():
            param.requires_grad = False
    def unfreeze_for_train(self):
        for param in self.model.fc.parameters():
            param.requires_grad = True
        for param in self.conv_first.parameters():
            param.requires_grad = True
        for param in self.model.conv1.parameters():
            param.requires_grad = True
    def forward(self, xb):
        out = self.conv_first(xb)
        out = self.model(out)
        return out

sys.path.append("../")

root_dir = os.path.dirname(__file__)
model = CustomModelConv(1)
model.load_state_dict(torch.load('../models/best_res.pt'))
model.eval()

def main():
    st.header("Detection of blurred image demo")
    st.write("Upload your image for detecting:")

    uploaded_file = st.file_uploader("Choose an image...")

    if uploaded_file is not None:
        img = Image.open(uploaded_file)
        st.image(img, "your image")
        to_tensor = tt.ToTensor()
        img = to_tensor(img).unsqueeze(0)
        out = model(img)
        out = torch.sigmoid(out)
        if out > 0.5:
            st.text(f'The image is blurry with confidence of {out.item():.3f}')
        else:
            st.text(f'The image is not blurry with confidence of {1 - out.item():.3f}')

if __name__ == "__main__":
    main()


import os
import numpy as np
from montepython import io_mp
from montepython.likelihood_class import Likelihood
import scipy.constants as const
import scan_util_16 as util

class eBOSS_DR16_Lya_combined(Likelihood):

    # initialization routine

    def __init__(self, path, data, command_line):

        Likelihood.__init__(self, path, data, command_line)
        # needed arguments in order to get derived parameters
        # should be made into an if loop
        # self.need_cosmo_arguments(data, {'output': 'mPk'})
        # self.need_cosmo_arguments(data, {'P_k_max_h/Mpc': 20.})
        # self.need_cosmo_arguments(data, {'z_max_pk': 3.})

        # define array for values of z and data points
        self.z = np.array([self.zeff], 'float64')
        scan_locations = self.data_directory + '/' + self.cf_scan

        # number of data points
        self.num_points = np.shape(self.z)[0]

        #Make our interpolators
        self.chi2_interpolators = util.chi2_interpolators(scan_locations,self.transverse_fid,self.parallel_fid)

        # end of initialization

    # compute log likelihood
    def loglkl(self, cosmo1, cosmo2, data):

        chi2 = 0.

        for i in range(self.num_points):

            H  = cosmo2.Hubble(self.z[i]) * const.c / 1000.
            da = cosmo2.angular_distance(self.z[i])
            dm = da * (1 + self.z[i])
            rd = cosmo2.rs_drag() * self.rd_rescale

            transverse = dm / rd
            parallel = (const.c / 1000.) / (H * rd)
            chi2 += self.chi2_interpolators.get_Dchi2_from_distances(transverse,parallel)
        # return ln(L)
        lkl = - 0.5 * chi2

        return lkl


import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from PIL import Image
import torch
from torchvision import transforms

from src.model import UNet
from src.checkpoint import load_checkpoint

def visualize(transpose_channels=False, figsize=(30, 30), **images):
    """
    Helper function for data visualization
    PyTorch CHW tensor will be converted to HWC if `transpose_channels=True`
    """
    n_images = len(images)

    plt.figure(figsize=figsize)
    for idx, (key, image) in enumerate(images.items()):
        plt.subplot(1, n_images, idx + 1)
        plt.title(key.replace("_", " ").title(), fontsize=12)
        if transpose_channels:
            plt.imshow(np.transpose(image, (1, 2, 0)))
        else:
            plt.imshow(image)
        plt.axis("off")
    plt.show()

def get_input_and_target_images():
    file_ = pd.read_csv("./dataset.csv")

    input_column_names = ["images_vel_x", "images_vel_y"]
    output_column_name = "images_pressure"

    input_img_paths = [file_[column][0] for column in input_column_names]

    input_images = []
    for img_path in input_img_paths:
        input_img = Image.open(img_path).convert("RGB")
        input_images.append(input_img)

    # input_img = Image.open(file_["images_z_vort"][0]).convert("RGB")
    output_img = Image.open(file_[output_column_name][0]).convert("RGB")

    return input_images, output_img

def parse_input(input_images: np.ndarray, device: torch.device):
    data_transform = transforms.Compose(
        [
            transforms.Resize(256),
            transforms.ToTensor(),
        ]
    )

    for i, image in enumerate(input_images):
        input_images[i] = data_transform(image)
        input_images[i].unsqueeze_(0)
    input_images = torch.hstack(input_images)
    input_images = input_images.to(device)

    return input_images

def main():
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    model = UNet(in_channels=6)
    model.to(device=device)

    load_checkpoint(model=model, device=device)

    input_img, output_img = get_input_and_target_images()
    input_img = parse_input(input_img, device)

    model.eval()
    with torch.no_grad():
        prediction = model(input_img)

    prediction_viz = prediction.cpu().detach().numpy()
    prediction_viz = np.transpose(prediction_viz, (0, 2, 3, 1))[0, :, :, :]
    # prediction_viz_new = np.clip(prediction_viz * 255.0, 0, 255)
    # prediction_viz_new = np.round(prediction_viz_new).astype(np.uint8)

    input_img_viz = input_img.cpu().detach().numpy()
    input_img_viz = np.transpose(input_img_viz, (0, 2, 3, 1))[0, :, :, :]

    visualize(
        input=input_img_viz[:, :, 3:6], true=output_img, prediction=prediction_viz
    )

if __name__ == "__main__":
    main()


# # 함수 - function, method(메소드)

import time

# # 1부터 10까지 출력

# for i in range(1, 10+1):
#     print(i)

# # # 1부터 n까지 출력하는 함수
def get_num(n):
    for i in range(1, n+1):
        print(i)

get_num(20) # 출력

# # 1부터 n까지 합계를 구하는 함수
def get_sum(n):
    sum_v = 0
    for i in range(1, n+1):
        sum_v += i
    return sum_v

def get_sum2(n):
    sum_v = (n * (n+1)) // 2
    return sum_v

print(f'합계 : {get_sum(10)}')
print(f'합계 : {get_sum2(11)}')

# 계산 복잡도 - 곱셈, 덧셈, 나눗셈 - 3번 : 0(1)
# start = time.time()
# def get_sum2(n):
#     sum_v = (n * (n+1)) // 2
#     return sum_v

# print(f'합계 : {get_sum2(10000000)}')
# end = time.time()
# print(f'소요 시간 : {end-start} 초')


from PIL import Image
from typing import List
import numpy as np
import multiprocessing as mp
from time import sleep
import random

class ProcessManager :
    def __init__(self, pixels: np.ndarray, nbWorkers: int) -> None:
        self.__pixels = pixels
        self.__nbWorkers = nbWorkers
        self.__tabList = self.__decompose() # on découpe le tableau par la hauteur

    # décompose un tableau de m*n en plusieurs tableau de m * n/nb
    def __decompose (self) -> List[np.ndarray] :
        return np.split(self.__pixels, self.__nbWorkers)

    def runMultiproc(self) :
        process = [ ]
        for x in self.__tabList :
            p = mp.Process(target=self.createFile, args=(x,))
            process.append(p)

        [x.start() for x in process]
        # permet d'attendre la fin des processus enfants avant de retourner au parent
        [x.join() for x in process]

    def createFile(self, arr: np.ndarray) :
        print(f"{mp.current_process().name} has started")
        sleep(random.randrange(2,4))
        img = Image.new(mode="RGB", size=(arr.shape[1],arr.shape[0]))
        lst = list()
        for y in arr:
            for x in y :
                lst.append(x)
        img.putdata(lst)

        img.save(f"{mp.current_process().name}.bmp")

        print(f"{mp.current_process().name} s'est terminé")