"""
flaskr.utils.db
~~~~~~~~~~~~~~~
Utilities for database operations.
"""
import sqlite3
from typing import List, Optional
from datetime import datetime, timezone

from flask import g
from flask import current_app
from flaskr.utils.node import Node

def convert_timestamp(t):
    return datetime.fromisoformat(t.decode()).replace(tzinfo=timezone.utc).timestamp()

# Register the converter
sqlite3.register_converter("timestamp", convert_timestamp)

def get_db():
    """Connect to the application's configured database. The connection
    is unique for each request and will be reused if this is called
    again.
    """
    if "db" not in g:
        g.db = sqlite3.connect(
            current_app.config["DATABASE"], detect_types=sqlite3.PARSE_DECLTYPES
        )
        g.db.row_factory = sqlite3.Row

    return g.db

def close_db(e=None):
    """If this request connected to the database, close the
    connection.
    """
    db = g.pop("db", None)
    if db is not None:
        db.close()

def init_db(app):
    """Clear existing data and create new tables."""
    with app.app_context():
        db = get_db()
        with app.open_resource("schema.sql") as f:
            db.executescript(f.read().decode("utf8"))

def init_app(app):
    """Register database functions with the Flask app. This is called by
    the application factory.
    """
    app.teardown_appcontext(close_db)

# modified from https://github.com/matthiask/django-tree-queries/blob/8863c5237f32585cc5ddc21041231155cb806149/tree_queries/compiler.py#L120
CTE = """WITH RECURSIVE __tree(tree_depth,
    tree_path,
    tree_ordering,
    tree_pk) AS (
    SELECT
        0 tree_depth,
        printf("%s ", id) tree_path,
        printf(" %020s ", id) tree_ordering,
        T.id tree_pk
    FROM comment T
    WHERE T.parent_id IS NULL
    UNION ALL
    SELECT
        __tree.tree_depth + 1,
        __tree.tree_path || printf("%s ", T.id),
        __tree.tree_ordering || printf("%020s ", T.id),
        T.id
    FROM comment T
    JOIN __tree ON T.parent_id = __tree.tree_pk
)
SELECT
    comment.id,
    comment.parent_id,
    comment.post_id,
    comment.body, comment.created,
    comment.author_id, user.username as author_name,
-- __tree.tree_depth
   __tree.tree_path
FROM __tree
JOIN comment ON comment.id=__tree.tree_pk
JOIN user ON user.id=comment.author_id
WHERE comment.post_id=?
-- AND instr(__tree.tree_path, '3') !=0
ORDER BY __tree.tree_ordering;
"""

def get_all_comments(post_id: int) -> List[Node]:
    root_nodes = Node.build_from_cte_rows(get_db().execute(CTE, (post_id, )))
    return list(root_nodes)

def put_in_child(parent: dict, child: dict):
    if 'children' in parent:
        parent['children'].append(child)
    else:
        parent['children'] = [child]

def get_post(post_id, with_comments=True) -> Optional[dict]:
    """Get a post and its author by id.

    :param post_id: id of post to get
    :param with_comments: if return with comments
    :return: the post with author information
    """
    post = (
        get_db()
        .execute(
            "SELECT p.id, title, body, created, author_id, username"
            " FROM post p JOIN user u ON p.author_id = u.id"
            " WHERE p.id = ?",
            (post_id,),
        )
        .fetchone()
    )

    if post is None:
        return None

    if with_comments:
        comments = get_all_comments(post_id=post['id'])
        return dict(post) | dict(comments=comments)
    else:
        return dict(post)

def get_all_posts():
    db = get_db()
    rst = []
    for post in db.execute(
        "SELECT p.id, title, body, created, author_id, username as author_name"
        " FROM post p JOIN user u ON p.author_id = u.id"
        " ORDER BY p.created DESC, p.id DESC"
    ):
        rst.append(dict(post) | dict(comments=get_all_comments(post["id"])))
    return rst


class Solution:
    def isSubsequence(self, s: str, t: str) -> bool:
        if len(s) > len(t):
            return False
        if len(s) == 0:
            return True

        index_s = 0
        for char_t in t:
            if char_t == s[index_s]:
                index_s += 1
                if index_s == len(s):
                    return True

        return False

print(Solution().isSubsequence("abcc", "ahbgdc"))


# python 3 has different package names
try: from urlparse import urlparse
except ImportError: from urllib.parse import urlparse

from collections import defaultdict

def _new_collection():
    """ Collection data type is
            {path: {method: (ResponseClass,) }}
        So e.g. a POST request to http://venmo.com/feed is stored as
        {'/feed': {'POST': (ResponseClass,)}}

        the ResponseClass will have had the the constructor partially applied
        with the specified stubbed data so after finding it we finish
        instantiatiing with the request we received and return it.
        Why? So the request attribute on the response is the request that
        was made, not just the matching criteria in the stub
    """
    return defaultdict(lambda: defaultdict(lambda: ()))

class RequestCollection(object):

    _requests = _new_collection()

    @classmethod
    def add(cls, request, response):
        parsed = urlparse(request.url)
        cls._requests[parsed.path][request.method] = \
                cls._requests[parsed.path][request.method] + (response,)

    @classmethod
    def find(cls, request):
        parsed = urlparse(request.url)
        responses = cls._requests[parsed.path][request.method]
        if len(responses) > 0:
            head = responses[0]
            cls._requests[parsed.path][request.method] = \
                    cls._requests[parsed.path][request.method][1:] + (head,)
        else:
            head = None
        return head

    @classmethod
    def remove(cls, request):
        parsed = urlparse(request.url)
        del cls._requests[parsed.path][request.method]

    @classmethod
    def reset(cls):
        cls._requests = _new_collection()


import re
from collections import defaultdict
from string import Template

from odoo import _
from odoo.exceptions import MissingError

DEFAULT_REFERENCE_SEPARATOR = ""
PLACE_HOLDER_4_MISSING_VALUE = "/"

class ReferenceMask(Template):
    pattern = r"""\[(?:
                    (?P<escaped>\[) |
                    (?P<named>[^\]]+?)\] |
                    (?P<braced>[^\]]+?)\] |
                    (?P<invalid>)
                    )"""

def extract_token(s):
    pattern = re.compile(r"\[([^\]]+?)\]")
    return set(pattern.findall(s))

def sanitize_reference_mask(product, mask):
    tokens = extract_token(mask)
    attribute_names = set()
    for line in product.attribute_line_ids:
        attribute_names.add(line.attribute_id.name)
    if not tokens.issubset(attribute_names):
        raise MissingError(
            _("Found unrecognized attribute name in " '"Partcode Template"')
        )

def get_rendered_default_code(product, mask):
    product_attrs = defaultdict(str)
    reference_mask = ReferenceMask(mask)
    for value in product.product_template_attribute_value_ids:
        if value.attribute_id.code:
            product_attrs[value.attribute_id.name] += value.attribute_id.code
        if value.product_attribute_value_id.code:
            product_attrs[
                value.attribute_id.name
            ] += value.product_attribute_value_id.code
    all_attrs = extract_token(mask)
    missing_attrs = all_attrs - set(product_attrs.keys())
    missing = dict.fromkeys(missing_attrs, PLACE_HOLDER_4_MISSING_VALUE)
    product_attrs.update(missing)
    default_code = reference_mask.safe_substitute(product_attrs)
    return default_code

def render_default_code(product, mask):
    sanitize_reference_mask(product, mask)
    product.default_code = get_rendered_default_code(product, mask)


import os

import numpy as np
import matplotlib.pyplot as plt

import cv2
import open3d as o3d

def mkdirs(path):
    try:
        os.makedirs(path)
    except:
        pass

class Saver(object):

    def __init__(self, save_dir):
        self.idx = 0
        self.save_dir = os.path.join(save_dir, "results")
        if not os.path.exists(self.save_dir):
            mkdirs(self.save_dir)

    def save_as_point_cloud(self, depth, rgb, path, mask=None):
        h, w = depth.shape
        Theta = np.arange(h).reshape(h, 1) * np.pi / h + np.pi / h / 2
        Theta = np.repeat(Theta, w, axis=1)
        Phi = np.arange(w).reshape(1, w) * 2 * np.pi / w + np.pi / w - np.pi
        Phi = -np.repeat(Phi, h, axis=0)

        X = depth * np.sin(Theta) * np.sin(Phi)
        Y = depth * np.cos(Theta)
        Z = depth * np.sin(Theta) * np.cos(Phi)

        if mask is None:
            X = X.flatten()
            Y = Y.flatten()
            Z = Z.flatten()
            R = rgb[:, :, 0].flatten()
            G = rgb[:, :, 1].flatten()
            B = rgb[:, :, 2].flatten()
        else:
            X = X[mask]
            Y = Y[mask]
            Z = Z[mask]
            R = rgb[:, :, 0][mask]
            G = rgb[:, :, 1][mask]
            B = rgb[:, :, 2][mask]

        XYZ = np.stack([X, Y, Z], axis=1)
        RGB = np.stack([R, G, B], axis=1)

        pcd = o3d.geometry.PointCloud()
        pcd.points = o3d.utility.Vector3dVector(XYZ)
        pcd.colors = o3d.utility.Vector3dVector(RGB)
        o3d.io.write_point_cloud(path, pcd)

    def save_samples(self, rgbs, gt_depths, pred_depths, depth_masks=None):
        """
        Saves samples
        """
        rgbs = rgbs.cpu().numpy().transpose(0, 2, 3, 1)
        depth_preds = pred_depths.cpu().numpy()
        gt_depths = gt_depths.cpu().numpy()
        if depth_masks is None:
            depth_masks = gt_depths != 0
        else:
            depth_masks = depth_masks.cpu().numpy()

        for i in range(rgbs.shape[0]):
            self.idx = self.idx+1
            mkdirs(os.path.join(self.save_dir, '%04d'%(self.idx)))

            cmap = plt.get_cmap("rainbow_r")

            depth_pred = cmap(depth_preds[i][0].astype(np.float32)/10)
            depth_pred = np.delete(depth_pred, 3, 2)
            path = os.path.join(self.save_dir, '%04d' % (self.idx) ,'_depth_pred.jpg')
            cv2.imwrite(path, (depth_pred * 255).astype(np.uint8))

            depth_gt = cmap(gt_depths[i][0].astype(np.float32)/10)
            depth_gt = np.delete(depth_gt, 3, 2)
            depth_gt[..., 0][~depth_masks[i][0]] = 0
            depth_gt[..., 1][~depth_masks[i][0]] = 0
            depth_gt[..., 2][~depth_masks[i][0]] = 0
            path = os.path.join(self.save_dir, '%04d' % (self.idx), '_depth_gt.jpg')
            cv2.imwrite(path, (depth_gt * 255).astype(np.uint8))

            path = os.path.join(self.save_dir, '%04d'%(self.idx) , '_pc_pred.ply')
            self.save_as_point_cloud(depth_preds[i][0], rgbs[i], path)

            path = os.path.join(self.save_dir, '%04d'%(self.idx) , '_pc_gt.ply')
            self.save_as_point_cloud(gt_depths[i][0], rgbs[i], path, depth_masks[i][0])

            rgb = (rgbs[i] * 255).astype(np.uint8)
            path = os.path.join(self.save_dir, '%04d'%(self.idx) , '_rgb.jpg')
            cv2.imwrite(path, rgb[:,:,::-1])


from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ("shop", "0005_auto_20150527_1127"),
    ]

    operations = [
        migrations.AlterField(
            model_name="order",
            name="key",
            field=models.CharField(max_length=40, db_index=True),
        ),
    ]


def sieve_of_primes(n):
# n = 20
	sev=[True for x in range(n+1)]
	sev[1]=False
	p=2
	while (p*p<=n):
		if sev[p] == True:
			for i in range(p*p,n+1,p):
				sev[i] = False
		p += 1
	return sev

hours,part=input().split()
hours = int(hours)
part = int(part)
div=hours//part
sev = sieve_of_primes(500)
# print(sev)
count=0
for x in range(1,div+1):
	flg=0
	for y in range(part):
		if not sev[x+y*div]:
			flg=1
			break
	if flg==0:
		count+=1
print(count)


from unittest import TestCase
import unittest
# from unittest.mock import patch, Mock
# import csv
# from flask import request, jsonify
import requests

# import sys
#
# sys.path.insert(0, '../../src')

class TestLoadDailyReports(TestCase):
    # def setUp(self):
    #     self.app = app.

    def test_load_data_success(self):
        f = open("tests/routes/01-01-2021.csv", "rb")
        file = f.read()
        url = 'https://covid-monitor-61.herokuapp.com/daily_reports/data'
        r = requests.post(url, data=file, headers={"Content-Type": "text/csv"})
        f.close()
        self.assertEqual(r.status_code, 200)

    def test_query_data_success(self):
        url = 'https://covid-monitor-61.herokuapp.com/daily_reports/cases'
        body = {"return_type": "json",
                "types": ["Confirmed", "Deaths", "Active"],
                "locations":
                    [
                        {"Country/Region": "Belgium"},
                        {"Country/Region": "Canada", "Province/State": "Ontario"},
                        {"Country/Region": "Australia",
                         "Province/State": "Queensland",
                         "Combined_Key": "Australian Capital Territory, Australia"}
                    ]
                }
        r = requests.post(url, json=body, headers={"Content-Type": "application/json"})
        print(r.json())
        self.assertEqual(r.status_code, 200)

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


#For randomly choice; we need import keyword and random module.
import random
customers = ['Jimmy', 'kim', 'John', 'Stacie']

#Choose one customer randomly and put the value as winner.
winner = random.choice(customers)

#Set a variable named flavor to the text vanilla.
flavor = 'vanilla'

#Printing Congratulations,
#Name of the winner which was chosen randomly as winner
#And you have won an ice cream sundae!
print('Congratulations ' + winner + ' you have won an ice cream sundae!')

#Set another variable named prompt to the text Would you like a cherry on top?
prompt = 'Would you like a cherry on top? '

#Set wants_cherry as input. User would be ask to give the input .
wants_cherry = input(prompt)

#Set a variable named order contains flavor (declared before as vanilla) and sundae.
order = flavor + ' sundae '

#Set up a condition that if user give input as yes,
#order variable will add with a cherry on top.
if (wants_cherry == 'yes'):
    order = order + ' with a cherry on top'

#Printing , One selected order(flavor and details) for winner’s (name) is coming right up.
print('One ' + order + ' for ' + winner +
' coming right up...')


from time import sleep
import time
import datetime
from datetime import timedelta
from time import sleep, strftime

motionTimeOutSeconds = 5
lastMotionTime = datetime.datetime.now()

def motionTimedOut():
    myNow = datetime.datetime.now()
    deltaTime = (myNow - lastMotionTime).total_seconds()
    if deltaTime > motionTimeOutSeconds:
        print('Motion timed out after {0} seconds'.format(deltaTime))
        return True
    return False

sleep(2)
# bTime = datetime.datetime.now()
# deltaT = (bTime-lastMotionTime).total_seconds()
# print(deltaT)
if motionTimedOut():
    print('Motion timeout test of 2 seconds failed!')
    exit(-1)
print('No timeout after 2 seconds.')
lastMotionTime = datetime.datetime.now()
sleep(6)
if motionTimedOut():
    print('Motion timeout test is working after 6 seconds')


#-*- coding: utf-8 -*-

from django.shortcuts import render, redirect
from blog.models import Mypost, MainPage
from blog.forms import CreateForms
# Create your views here.

def index(request):
    all_posts = Mypost.objects.all()
    maintext = MainPage.objects.all()
    # print('all_posts_all')
    # print(all_posts)
    context = {'maintext': maintext}
    return render(request, 'index.html', context)

def BlogList(request):
    all_posts = Mypost.objects.all()
    # print('all_posts_all')
    # print(all_posts)
    context = {'all_posts': all_posts}
    return render(request, 'blogs_list.html', context)

def PostDitail(request, pk):
    post_ditail = Mypost.objects.all().filter(pk=pk)

    print('post_ditail')
    print(post_ditail[0])
    return render(request, 'blog_ditail.html', {'post_ditail': post_ditail[0]})

def CreatePost(request):
    if request.method == 'POST':
        form = CreateForms(request.POST, request.FILES)
        if form.is_valid():
            create_post = form.save(commit=False)
            create_post.author = request.user
            create_post.save()
            return redirect('blog:ditail', pk=create_post.pk)
    else:
        form = CreateForms

    return render(request, 'create.html', {'form': form})

def EditPost(request, pk):
    # edit_post1 = get_object_or_404(Mypost, pk=pk)

    edit_post = Mypost.objects.filter(pk=pk).get()
    if request.method == 'POST':
        form = CreateForms(request.POST, request.FILES, instance=edit_post)
        # print('form')
        # print(form)

        if form.is_valid():
            edit = form.save(commit=False)
            # edit.author = request.user
            # print('edit.author')
            # print(edit.author)
            edit.save()
            return redirect('blog:ditail', pk=edit.pk)
    else:
        form = CreateForms(instance=edit_post)

    return render(request, 'edit.html', {'form': form})

def DelPost(request, pk):
    del_post = Mypost.objects.get(pk=pk)
    if request.method == 'POST':
        form = CreateForms(request.POST, instance=del_post)
        if form.is_valid():
            del_post = form.save(commit=False)
            del_post.delete()
            return redirect('blog:blogs_list')
    else:
        form = CreateForms(instance=del_post)
    return render(request, 'delete.html', {'form': form})

# def PagePostApi(request):
#
#     if request.method == 'POST':
#         form = CreateForms(request.POST)
#         if form.is_valid():
#             create_post = form.save(commit=False)
#             create_post.author = request.user
#             create_post.save()
#             return redirect('blog:ditail', pk=create_post.pk)
#     else:
#         form = CreateForms
#     return render(request, 'index.html', {'form': form} )

def ApiBlogjs(request):
    return render(request, 'apiblogjs.html', )

def ApiCreateblogjs(request):
    return render(request, 'apicreateblogjs.html',)

def ApiDetailblogjs(request, pk):
    post_ditail = Mypost.objects.all().filter(pk=pk)
    return render(request, 'apidetailblogjs.html',{'post_ditail': post_ditail[0]})

def ApiEditblogjs(request, pk):
    post_ditail = Mypost.objects.all().filter(pk=pk)
    return render(request, 'apieditblogjs.html',{'post_ditail': post_ditail[0]})

###   ResctJS   #####
def AllPostsReact(request):
    return render(request, 'react_post/allreact.html', )

def CreatePostReact(request):
    return render(request, 'react_post/createpostreact.html',)

def DetailPostReact(request, pk):
    post_ditail = Mypost.objects.all().filter(pk=pk)
    return render(request, 'react_post/detailreact.html',{'post_ditail': post_ditail[0]})

def EditPostReact(request, pk):
    post_ditail = Mypost.objects.all().filter(pk=pk)
    return render(request, 'react_post/editpostreact.html',{'post_ditail': post_ditail[0]})


import torch
import torch.nn as nn

class Decoder(nn.Module):
    def __init__(self):
        super(Decoder, self).__init__()

        self.reduce_dim_5 = nn.Conv2d(2048, 256, kernel_size=(1, 1), stride=1, padding=0)
        self.reduce_dim_4 = nn.Conv2d(1024, 256, kernel_size=(1, 1), stride=1, padding=0)
        self.reduce_dim_3 = nn.Conv2d(512, 256, kernel_size=(1, 1), stride=1, padding=0)
        self.reduce_dim_2 = nn.Conv2d(256, 256, kernel_size=(1, 1), stride=1, padding=0)

        self.double_conv_5 = self._make_dobule_conv(256, 128)
        self.double_conv_4 = self._make_dobule_conv(256, 128)
        self.double_conv_3 = self._make_dobule_conv(256, 128)
        self.double_conv_2 = self._make_dobule_conv(256, 128)

    def _up_add(self, x, y):
        (_, _, H, W) = y.size()
        x_up = nn.functional.upsample(x, size=(H, W), mode='nearest')

        return x_up + y

    def _make_dobule_conv(self, in_dims, out_dims):
        conv_layer = nn.Sequential(
            nn.Conv2d(in_dims, out_dims, kernel_size=(3, 3), stride=1, padding=1),
            nn.BatchNorm2d(out_dims),
            nn.ReLU(),
            nn.Conv2d(out_dims, out_dims, kernel_size=(3, 3), stride=1, padding=1),
            nn.BatchNorm2d(out_dims),
            nn.ReLU()
        )

        return conv_layer

    def forward(self, c2, c3, c4, c5):
        m5 = self.reduce_dim_5(c5)
        m4 = self._up_add(m5, self.reduce_dim_4(c4))
        m3 = self._up_add(m4, self.reduce_dim_3(c3))
        m2 = self._up_add(m3, self.reduce_dim_2(c2))

        m5 = self.double_conv_5(m5)
        m4 = self.double_conv_4(m4)
        m3 = self.double_conv_3(m3)
        m2 = self.double_conv_2(m2)

        return m5, m4, m3, m2


from distutils.core import setup
import sys
sys.path.insert(1, "lib")
from maxicom.strings import *

files = ["glade/*"]

setup(name = PACKAGE,
    version = VERSION,
    description = DESCRIPTION,
    author = AUTHOR,
    author_email = AUTHOR_EMAIL,
    url = URL,
    license = LICENSE,
    package_dir = {'': 'lib'},
    packages = ['maxicom'],
    package_data = {'maxicom' : files },
    scripts = ["maxicom"],
)


from setuptools import find_packages, setup

with open("./README.md") as fp:
    description = fp.read()

setup(
    name="pyC8",
    version="1.1.1",
    description="Python SDK for Macrometa Global Data Mesh",
    long_description=description,
    long_description_content_type="text/markdown",
    author="Macrometa",
    author_email="product@macrometa.com",
    url="https://www.macrometa.com",
    packages=find_packages(exclude=["tests"]),
    include_package_data=True,
    install_requires=["requests==2.25.1", "six", "websocket-client==0.57.0"],
    tests_require=["pytest", "mock", "flake8"],
    classifiers=[
        "Intended Audience :: Developers",
        "Intended Audience :: End Users/Desktop",
        "Intended Audience :: Information Technology",
        "Operating System :: MacOS",
        "Operating System :: Microsoft :: Windows",
        "Operating System :: POSIX",
        "Operating System :: Unix",
        "Programming Language :: Python",
        "Programming Language :: Python :: 3",
        "Topic :: Documentation :: Sphinx",
    ],
)


from data_test import DataGarbageCollector
from conftest import *

def test_function(setup_teardown):
    node_name = "k3d-p3"
    ip_address = get_ip(node_name)
    setup_sessions_metadata(node_name)
    setup_node_metadata(node_name)
    con = Connection(node_name='k3d-p3', ip=ip_address)
    session = 'marco'

    assert DataGarbageCollector.test_before == con.get(
        'session-offloading-manager?command=test-function&type=session&value=' + session)

    assert DataGarbageCollector.garbage_collector == con.get('session-offloading-manager-garbage-collector?command=garbage-collector&deletePolicy=forced&sessionId=' + session)

    assert DataGarbageCollector.test_after == con.get(
        'session-offloading-manager?command=test-function&type=session&value=' + session)


import pickle
import re
from pathlib import Path
from typing import List

from IPython.display import display

import os.path as op
from datetime import datetime

import pandas as pd
from tqdm.notebook import tqdm

from matplotlib import pyplot as plt

from sklearn.metrics import (
    accuracy_score,
    balanced_accuracy_score,
    f1_score,
    matthews_corrcoef,
    precision_score,
    recall_score,
)

from src.helpers.mylogger import get_handler

import logging

handler = get_handler()

log = logging.getLogger(__name__)
log.handlers[:] = []
log.addHandler(handler)
log.setLevel(logging.DEBUG)

class EntryNotFoundError(Exception):
    pass

class BidirectionalEntriesFoundError(Exception):
    pass

class MultipleEntryFoundError(Exception):
    pass

class ClassLabels:
    DISRUPTIVE = "Disruptive"
    NON_DISRUPTIVE = "Non-disruptive"
    NOT_AVAILABLE = "N/A"

def unzip_res_range(res_range):
    """
    Converts ranges in the form: [2-210] or [3-45,47A,47B,51-67] into lists of strings including all numbers in
    these ranges in order .
    Items are of type <str>.
    """
    res_ranges = res_range.strip()[1:-1].split(',')
    index_list = []
    for r in res_ranges:
        if re.match('.+-.+', r):
            a, b = r.split('-')
            index_list += [str(n) for n in range(int(a), int(b) + 1)]
        else:
            index_list.append(r)

    return index_list

# print(unzip_res_range("[95-96,98-100,102-103,122,262,266-267,270,273,294]"))

def get_mutation_position(mutation):
    return mutation[1:-1]

class CancerValidation:
    def __init__(self, interfaces_data_path):
        self.interfaces_data = self.load_data(interfaces_data_path)

    @staticmethod
    def load_data(data_path):
        interfaces_data = pd.read_csv(
            data_path, sep="\t", usecols=["P1", "P2", "P1_IRES", "P2_IRES"]
        )
        interfaces_data["P1_IRES"] = interfaces_data["P1_IRES"].apply(lambda x: unzip_res_range(x))
        interfaces_data["P2_IRES"] = interfaces_data["P2_IRES"].apply(lambda x: unzip_res_range(x))
        return interfaces_data

    def check(self, protein: str, mutation: str, interactor: str):
        # print(f"Checking ..\n"
        #       f"> PROTEIN: {protein} \n"
        #       f"> MUTATION: {mutation} \n"
        #       f"> INTERACTOR: {interactor}")
        try:
            data, res = self._get_entry(protein, interactor)
        except EntryNotFoundError:
            return ClassLabels.NOT_AVAILABLE

        mut_pos = get_mutation_position(mutation)
        if mut_pos in res:
            return ClassLabels.DISRUPTIVE
        else:
            return ClassLabels.NON_DISRUPTIVE

    @staticmethod
    def _handle_check_duplicated_entries(data, p_ires) -> List[int]:
        """
        Checks if all entries of given data duplicated. Each cell contains list in it.
        If all entries are duplicated entries, then we have no problem, just get the res.
        """
        # display(data)

        # In order for us to check if the entries are duplicated, we'll have to
        # convert list item in the cells to tuple. Otherwise, we get the following error:
        # TypeError: unhashable type: 'list'
        data_tuple = data[["P1_IRES", "P2_IRES"]].applymap(lambda x: tuple(x))
        data_tuple.duplicated(keep=False).all()

        # no problem, then.
        if p_ires == "P1_IRES":
            [p1] = data["P1"].unique()
            [p2] = data["P2"].unique()
        elif p_ires == "P2_IRES":
            [p2] = data["P1"].unique()
            [p1] = data["P2"].unique()
        else:
            raise ValueError(f"Illegal argument provided for parameter `p_ires`: {p_ires}")

        # check if all entries are duplicated
        if data_tuple.duplicated(keep=False).all():

            log.warning(
                f"Multiple entries but they were duplicated. PROTEIN: {p1}, INTERACTOR: {p2}"
            )
            [p_res] = data_tuple[p_ires].unique()
            p_res = list(p_res)

            return p_res

        else:
            log.error("MultipleEntryError with following data: ")
            display(data)
            p_res_list = data[p_ires].tolist()
            p_res = sorted(
                set([item for sublist in p_res_list for item in sublist])
            )
            log.error(F"Returned RES: {p_res}")

            return p_res
            # data.to_csv("ERROR_data.csv", index=False)
            # raise MultipleEntryFoundError

    def _get_entry(self, protein, interactor):
        a_b_interface_data = self.interfaces_data[
            (self.interfaces_data["P1"] == protein) &
            (self.interfaces_data["P2"] == interactor)
            ]

        b_a_interface_data = self.interfaces_data[
            (self.interfaces_data["P1"] == interactor) &
            (self.interfaces_data["P2"] == protein)
            ]

        # Both of them contains entry -- this is an unlikely situation, unless there is problem with the text file..
        if len(a_b_interface_data) != 0 and len(b_a_interface_data) != 0:
            raise BidirectionalEntriesFoundError

        # First data contains entry and the second one is empty
        elif len(a_b_interface_data) != 0 and len(b_a_interface_data) == 0:
            if len(a_b_interface_data) != 1:
                p1_res = self._handle_check_duplicated_entries(a_b_interface_data, "P1_IRES")

            else:
                [p1_res] = a_b_interface_data["P1_IRES"]

            return a_b_interface_data, p1_res

        # First data is empty and the second one contains entry
        elif len(a_b_interface_data) == 0 and len(b_a_interface_data) != 0:
            if len(b_a_interface_data) != 1:
                p2_res = self._handle_check_duplicated_entries(b_a_interface_data, "P2_IRES")

            else:
                [p2_res] = b_a_interface_data["P2_IRES"]

            return b_a_interface_data, p2_res

        # Both of them are empty
        else:
            raise EntryNotFoundError

    def validate(
            self,
            tcga_type: str,
            tcga_data: pd.DataFrame,
    ):
        tcga_data_validation = tcga_data.copy()
        validation_results = []
        for index, row in tqdm(
                tcga_data_validation.iterrows(),
                total=len(tcga_data_validation)
        ):
            protein = row["UniProt_ID"]
            mutation = row["Mutation"]
            interactor = row["Interactor_UniProt_ID"]
            valid_label = self.check(
                protein=protein, mutation=mutation, interactor=interactor
            )
            # print(f">> RESULT: {valid_label}")
            validation_results.append(valid_label)

        tcga_data_validation["Validation"] = validation_results
        tcga_data_validation["Validation"].value_counts().plot(
            kind="bar", title=f"{tcga_type} Validation Results"
        )
        plt.show()

        tcga_data_validation_processed = process_validation_data(tcga_data_validation)
        tcga_data_validation_processed["Validation"].value_counts().plot(
            kind="bar", title=f"{tcga_type} Validation Processed Results"
        )
        plt.show()

        metrics_data = get_scoring_metrics(tcga_data_validation_processed)
        num_entries = len(tcga_data_validation)
        counts = tcga_data_validation_processed["Validation"].value_counts().to_dict()
        num_disruptive = counts[0]
        num_non_disruptive = counts[1]
        metrics_data.insert(0, "TCGA", tcga_type)
        metrics_data.insert(1, "#_Entries", num_entries)
        metrics_data.insert(2, "#_Disruptive", num_disruptive)
        metrics_data.insert(3, "#_Non_disruptive", num_non_disruptive)

        return {
            "data_validation": tcga_data_validation,
            "data_validation_processed": tcga_data_validation_processed,
            "metrics_data": metrics_data,
        }

    @staticmethod
    def validate_single_class(
            tcga_type: str,
            output_already_calculated: dict,
            single_class: int,
    ):
        """
        Requires the positions to be already calculated.
        """

        tcga_data_validation = output_already_calculated["data_validation"]

        print(f"Using the class {single_class} only.")
        tcga_data_validation = tcga_data_validation[
            tcga_data_validation["Prediction"] == single_class
            ].copy()

        tcga_data_validation_processed = process_validation_data(tcga_data_validation)
        metrics_data = get_scoring_metrics(tcga_data_validation_processed)
        num_entries = len(tcga_data_validation)
        counts = tcga_data_validation_processed["Validation"].value_counts().to_dict()
        num_disruptive = counts[0]
        num_non_disruptive = counts[1]
        metrics_data.insert(0, "TCGA", tcga_type)
        metrics_data.insert(1, "#_Entries", num_entries)
        metrics_data.insert(2, "#_Disruptive", num_disruptive)
        metrics_data.insert(3, "#_Non_disruptive", num_non_disruptive)

        return {
            "data_validation": tcga_data_validation,
            "data_validation_processed": tcga_data_validation_processed,
            "metrics_data": metrics_data,
        }

    @staticmethod
    def extract_output_dict(name, dict_obj):
        folder_path = "outputs"
        Path(f"{folder_path}").mkdir(parents=True, exist_ok=True)
        current_date = datetime.now().strftime("%Y-%m-%d")
        file_name = f"{name}_{current_date}.pickle"
        file_path = op.join(folder_path, file_name)
        if op.exists(file_path):
            raise FileExistsError("File already exists")

        pickle.dump(dict_obj, open(file_path, "wb"))
        print("Object extracted successfully.")

    @staticmethod
    def load_output_dict(pickle_path):
        obj_loaded = pickle.load(open(pickle_path, "rb"))
        return obj_loaded

def test_entry_not_found(df, p, i):
    a_b = df[
        (df["P1"] == p) &
        (df["P2"] == i)
        ]

    b_a = df[
        (df["P1"] == i) &
        (df["P2"] == p)
        ]

    assert len(a_b) == len(b_a) == 0

def get_scoring_metrics(tcga_validation_data):
    y_true = tcga_validation_data["Validation"]
    y_pred = tcga_validation_data["Prediction"]
    metrics_data = pd.DataFrame(
        [
            accuracy_score(y_true, y_pred),
            balanced_accuracy_score(y_true, y_pred),
            f1_score(y_true, y_pred),
            precision_score(y_true, y_pred),
            recall_score(y_true, y_pred),
            matthews_corrcoef(y_true, y_pred),
        ],
        index=["ACCURACY", "BALANCED_ACCURACY", "F1", "PRECISION", "RECALL", "MATTHEWS_COR"]
    ).T

    return metrics_data

def process_validation_data(tcga_data: pd.DataFrame):
    """
    Process the validation data.
    1. Drop N/A entries
    2. Convert Labels as follows:
        DISRUPTIVE → 0
        NON_DISRUPTIVE → 1
    3. Convert its type to int.
    """

    tcga_processed = tcga_data[tcga_data["Validation"] != "N/A"].copy()
    tcga_processed["Validation"] = tcga_processed["Validation"].replace(
        {
            ClassLabels.DISRUPTIVE: 0,
            ClassLabels.NON_DISRUPTIVE: 1,
        }
    )
    tcga_processed = tcga_processed.astype({"Validation": "int"})

    return tcga_processed


import numpy as np

#this script contains all the necessary function required for training data using linear regression. Run gradient_Descent function and it will return the w vector, b and cost_history, which
#models your data. You can also apply the model to the new data for prediction.

def compute_pderivatives(listX, listY, listW, b):
    m = listX.shape[0]
    n = listX.shape[1]
    listPDerivatives = np.zeros(n)
    for i in range(n):
        pDerivative = 0
        for j in range(m):
            pDerivative += (np.dot(listW,listX[j]) + b - listY[j]) * (listX[j][i])
        listPDerivatives[i] = pDerivative

    pDerivativeb = 0
    for i in range(m):
        pDerivativeb += (np.dot(listW,listX[i]) + b - listY[i])
    pDerivativeb = pDerivativeb/m
    listPDerivatives = listPDerivatives/m

    return listPDerivatives, pDerivativeb

def costFunc(listX, listY, listW, b, lamb):
    cost1 = 0.0
    cost2 = 0.0
    m = listX.shape[0]
    for i in range(m):
        cost1 += ((np.dot(listX[i],listW) + b) - listY[i])**2
    cost1 = cost1/(2 *listX.shape[0])
    for i in range(listW.shape[0]):
        cost2 += (listW[i] ** 2)
    cost2 = (cost2 * lamb)/2*m
    return cost1+cost2

def gradient_Descent(listX, listY, listW, b, alpha, lamb):
    costHistory = np.zeros(100000)
    m = listX.shape[0]
    for i in range(100000):
        cost = costFunc(listX, listY, listW, b, lamb)
        costHistory[i] = cost
        listPDerivatives, pDerivativeB = compute_pderivatives(listX, listY, listW, b)
        listW = listW - alpha * (lamb/m) * listW - alpha * listPDerivatives
        b = b - alpha * pDerivativeB
    return listW,b,costHistory


from typing import *

class Solution:
    @staticmethod
    def gcd(a, b):
        while b:
            a, b = b, a%b
        return a

    def dfs(self, nums, opt, mask):
        # mask: 0 unused, 1 used
        n = len(nums)
        ans = 0
        if mask == (1 << n - 1):
            return ans
        if self.memo[mask] > 0:
            return self.memo[mask]
        for i in range(n):
            if mask & (1 << i) == 0:
                for j in range(i+1, n):
                    if mask & (1 << j) == 0:
                        ans = max(ans, opt * self.gcd(nums[i], nums[j]) + self.dfs(nums, opt+1, mask | (1<<i) | (1<<j)))
                        self.memo[mask] = ans
        return ans

    def maxScore(self, nums: List[int]) -> int:
        n = len(nums)
        self.memo = [0] * (1<<n)
        self.dfs(nums, 1, 0)
        return self.memo[0]

if __name__ == "__main__":
    s = Solution()

    nums = [3,4,6,8]
    assert s.maxScore(nums) == 11


from flask import Flask, request

app = Flask(__name__)

@app.route('/')
def home():
    return "TP Florian Marques"

@app.route('/means', methods=['GET'])
def meanOfList():
    list = request.args.getlist('int', type=int)

    if len(list) == 0:
        return "Given list is null"
    else:
        return "Mean of the list is : {}".format(sum(list)/len(list))


import multiprocessing
import time
import hashlib

memory = list(range(30_000_000))

def function(name):
    for i in range(10):
        print("Current:", name, i)
        time.sleep(1)

def slow_function(name):
    for i in range(10):
        print("Current:", name, i)
        for j in range(300_000):
            hashlib.md5(str(j).encode("utf-8")).hexdigest()

if __name__ == "__main__":
    for i in range(3):
        name = chr(97 + i)
        multiprocessing.Process(target=slow_function, args=(name, )).start()


n, nums = input().split(" ", 1)
n = int(n)
nums = list(map(int, nums.split(" ")))

# dynamic programming
def max_sum_increasing_subsequence(nums, n):
    maximum = 0

    dp = [0 for _ in range(n)]

    for i in range(n):
        dp[i] = nums[i]

    for i in range(n):
        for j in range(i):
            if nums[i] > nums[j] and dp[i] < dp[j] + nums[i]:
                dp[i] = dp[j] + nums[i]

    for i in range(n):
        if maximum < dp[i]:
            maximum = dp[i]

    return maximum

print(max_sum_increasing_subsequence(nums, n))


import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

def ex_deal(df_Int, df_ex):
    columns = ['顺序', '氮素', '频率', '刈割']
    df_Int = pd.concat([df_Int, pd.DataFrame(columns=columns)])
    for item in range(df_Int.shape[0]):
        for jtem in range(df_ex.shape[0]):
            if int(df_Int.iloc[item, 0]) == int(df_ex.iloc[jtem, 1]):
                df_Int.loc[item, '顺序'] = df_ex.iloc[jtem, 1]
                df_Int.loc[item, '氮素'] = df_ex.iloc[jtem, 2]
                df_Int.loc[item, '频率'] = df_ex.iloc[jtem, 3]
                df_Int.loc[item, '刈割'] = df_ex.iloc[jtem, 4]
    # df_Int.drop([0, 19], inplace=True)
    return df_Int

def MF(g, N="氮素",year=2010):
    # if year==2008:
    #     if N == "刈割":
    #         g=
    #     if N == "频率":
    #
    # else:
    if N == "刈割":
        if g == 0.0:
            g = "nm"
        else:
            g = "m"
    if N == "频率":
        if g == 2.0:
            g = "l"
        elif g == 0.0:
            g = "nan"
        else:
            g = "h"

    return g

def loop_chain_nan(year, gb, D, N="氮素"):

    # if year == 2008:
    #     g = MF(g_[0], N,year=2008)
    #     D["loop"][g], D["nan"][g], D["chain"][g] = 0, 0, 0
    #     for g_ in gb:
    #         print(g_[1][3])
    #         for item in g_[1][3]:
    #             if item == 0:  # 链
    #                 D["chain"][g] += 1
    #             elif item == -0.15:
    #                 D["nan"][g] += 1
    #                 print(2008,item)
    #             else:  # 环
    #                 D["loop"][g] += 1

    if year == 2009:
        for g_ in gb:
            g = MF(g_[0], N)
            # if g!=0.0:
            D["loop"][g],D["nan"][g], D["chain"][g] = 0, 0, 0
            for item in g_[1][3]:
                if item == 0:  # 链
                    D["chain"][g] += 1
                elif item == -0.15:
                    D["nan"][g] += 1
                else:  # 环
                    D["loop"][g] += 1
    elif year > 2009:
        for g_ in gb:
            g = MF(g_[0], N)
            for item in g_[1][3]:
                if item == 0:
                    D["chain"][g] += 1
                elif item == -0.15:
                    D["nan"][g] += 1
                else:
                    D["loop"][g] += 1
    return D

def main():
    path = "C:/Users/97899/Desktop/N/"
    df_ex = pd.read_excel(path + "实验处理_ex.xls")
    ind = np.linspace(2008, 2020, 13)
    D = {}
    D["loop"], D["chain"], D["nan"] = {}, {}, {}
    for year in ind:
        df_cir = pd.read_excel(path + "Network/circle20.xls", sheet_name=str(int(year)))
        df_cir = ex_deal(df_cir, df_ex)

        gb = df_cir.groupby("氮素")
        D = loop_chain_nan(year, gb, D)
        gm = df_cir.groupby("刈割")
        D = loop_chain_nan(year, gm, D, "刈割")
        gf = df_cir.groupby("频率")
        D = loop_chain_nan(year, gf, D, "频率")
    print(D)
    net_loop = []
    net_chain = []
    net_nan = []
    '''氮素'''
    for key in D["loop"].keys():
        sum_ = D["loop"][key] + D["chain"][key] + D["nan"][key]
        print(key,sum_)
        net_loop.append(D["loop"][key] / sum_)
        net_chain.append(D["chain"][key] / sum_)
        net_nan.append(D["nan"][key] / sum_)
    print("非竞争", len(net_nan), "链", len(net_chain), "环", len(net_loop))
    labels = ['N=0', 'N=1', 'N=2', 'N=3', 'N=5', 'N=10', 'N=15', 'N=20', 'N=50']
    width = 0.5  # the width of the bars: can also be len(x) sequence

    net = (np.array(net_loop) + np.array(net_chain)).tolist()
    print("竞争主导", len(net))
    plt.xticks(fontsize=15)
    plt.yticks(fontsize=15)
    ax1 = plt.subplot(212)
    print(net_loop[:9],net_chain[:9],net_nan[:9])
    ax1.bar(labels, net_loop[:9], width, label='ICN', color="darkcyan")
    ax1.bar(labels, net_chain[:9], width, bottom=net_loop[:9], label='TCN', color="turquoise")
    ax1.bar(labels, net_nan[:9], width, bottom=net[:9], label='SCS', color="yellow")
    ax1.set_ylabel('Ratio', fontdict={"size": 20})
    ax1.set_xlabel('N addition rater'"$(gNm^{-2}year^{-1})$", fontdict={"size": 15})

    width2 = 0.4
    '''刈割'''
    label_2 = ['No-Mowing', 'Mowing']
    ax2 = plt.subplot(221)
    ax2.bar(label_2, net_loop[9:11], width2, label='ICN', color="darkcyan")
    ax2.bar(label_2, net_chain[9:11], width2, bottom=net_loop[9:11], label='TCN', color="turquoise")
    ax2.bar(label_2, net_nan[9:11], width2, bottom=net[9:11], label='SCS', color="yellow")
    ax2.set_ylabel('Ratio', fontdict={"size": 20})
    ax2.set_xlabel('Mowing', fontdict={"size": 15})

    '''频率'''
    label_3 = ['Zero','Low (Twice)', 'High (Monthly)']
    ax3 = plt.subplot(222)  # 222
    ax3.bar(label_3, net_loop[11:14], width2, label='ICN', color="darkcyan")
    ax3.bar(label_3, net_chain[11:14], width2, bottom=net_loop[11:14], label='TCN', color="turquoise")
    ax3.bar(label_3, net_nan[11:14], width2, bottom=net[11:14], label='SCS', color="yellow")  # [11:13]
    # ax3.set_ylabel('Ratio', fontdict={"size": 15})
    ax3.set_xlabel('Frequency', fontdict={"size": 15})
    ax3.legend(ncol=1, bbox_to_anchor=(1.2, 1), fontsize=13)

    plt.show()
    plt.savefig(path+'Figure/bar_distribution.png')

main()


import time
from clusterbot import ClusterBot, activate_logger

# Print confirmation about sent Slack messages
activate_logger()
# Debug mode
# activate_logger('DEBUG')

# Send a message to the default user specified in you config files.
bot = ClusterBot()
message_id = bot.send("Starting example script.")
# Reply to the default user message from above (open a ne Thread).
bot.reply(message_id, "Waiting for 5s.")
# Wait 5 seconds.
time.sleep(5)
# Reply again.
bot.reply(message_id, "5s have passed.")

# Send a message to someone else (not default user)
message_id = bot.send(
    "Hi Denis. I started using ClusterBot :tada:", user_name="Denis Alevi"
)
# Reply to that message (message_id has to belong to a message exchanged with
# ``user_name``)
bot.reply(message_id, "And I ran the example script!", user_name="Denis Alevi")

# Upload a file to your slack chat
message_id = bot.upload(file_name="README.md", message="Upload of README.md")

# Update or append to a previously send message
message_id = bot.reply(message_id, "An answer to this file")
bot.update(message_id, "An updated answer to this figure, deleting the previous one.")
bot.append(message_id, "Another update, which is appended and keeps the previous one")

# Initialize and update a progress bar
bot.init_pbar(10, title="This is a progress bar with a title")
for i in range(10):
    message_new = bot.update_pbar()
    time.sleep(1)


import numpy as np
import pandas as pd
import scipy.spatial.distance as spd
import scipy.stats as sps
import sklearn.model_selection as skm
import sklearn.metrics as skmetrics
import matplotlib.pyplot as plt
import seaborn as sb
from hw1_modules import *

# read data from CSV to array
data = np.array(pd.read_csv("train.csv").values)

#separate values and labels into separate arrays
values = data[:,1:]
labels = data[:,0]
#convert labels array to vertical, 2d array of one column
labels = np.expand_dims(labels, axis=1)

#initialize confusion matrix
cf = np.ones((10,10), dtype=int)
#initialize cumulative accuracy
accuracy = 0
#set number of folds
number_folds = 3
#set k for k neighbors
k_neighbors = 3

#create kfold iterating object
kf = skm.KFold(n_splits=number_folds)
for train_idx, test_idx in kf.split(values, labels):
    print("Dividing data")
    #subset data using indexes generated by kfold object
    train_data = values[train_idx]
    test_data = values[test_idx]
    train_labels = labels[train_idx]
    test_lables = labels[test_idx]
    #run one iteration of testing with knn
    print("Testing data")
    predicted_labels = knn_predict_class(train_data, train_labels, test_data, k_neighbors)
    print("Accuracy for this run" + str(sum(predicted_labels == test_lables)/len(test_lables)))
    #cumulative accuracy
    accuracy += sum(predicted_labels == test_lables)/len(test_lables)
    #add this run's confusion values to cumulative confusion matrix
    cf = cf + skmetrics.confusion_matrix(test_lables, predicted_labels)

#calculate average accuracy from cumulative
accuracy = accuracy/number_folds
print(accuracy)

#Create and display plot for confusion matrix
ax = sb.heatmap(cf, annot=True, fmt="d")
ax.set(xlabel="Predicted Label", ylabel="True Label")
plt.show()


from django.conf.urls import url, include
from rest_framework import routers
from api import views

router = routers.DefaultRouter()
router.register(r'signup', views.ProfileViewSet)
router.register(r'add_animal', views.AddAnimalViewSet)

# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
    url(r'^', include(router.urls)),
    url(r'^login/', views.login),
]


import pandas as pd
import matplotlib.pyplot as plt

# Load the CSV file and set the type of the date column
reviews = pd.read_csv("branch_reviews.csv", index_col=0, parse_dates=["date"])

# Display a histogram of the number of reviews by date
reviews["date"].hist()
#plt.show()

# Create a new dataframe from reviews with the average rating for each date
reviews_by_date = reviews.groupby("date").mean()

# Plot a 30-day moving average rating per date
reviews_by_date[90:].rolling(30).mean().plot()
#plt.show()

# Print the number of reviews by rating
print(reviews["rating"].value_counts())

# Describe reviews
print(reviews.describe())


import xml.etree.ElementTree as ET
import pandas as pd
import numpy as np
import cv2 as cv

def draw_label(path):
    tree = ET.parse(path)
    img_out = np.zeros(shape=(1024, 1280))
    img_list_x = []
    img_list_y = []
    for elem in tree.iterfind('object'):
        mylist_x = []
        mylist_y = []
        # print(elem.tag, elem.attrib)
        for elem_1 in elem.iterfind('polygon/pt'):
            object_x = elem_1.find("x").text
            object_y = elem_1.find("y").text
            x = int(object_x)
            y = 1024 - int(object_y)
            if x < 0:
                x = 0
            if x > 1279:
                x = 1279
            if y < 0:
                y = 0
            if y > 1023:
                y = 1023
            mylist_x.append(x)
            mylist_y.append(y)
            img_list_x.append(x)
            img_list_y.append(y)
            img_out.itemset((y, x), 255)
        mylist = list(zip(mylist_x, mylist_y))
        pts = np.array(mylist, np.int32)
        cv.polylines(img_out, [pts], True, (255, 255, 255), 2)  # 画线
        cv.fillPoly(img_out, [pts], (255, 255, 255))  # 填充内部
    Alllist = list(zip(img_list_x, img_list_y))  # 统计标注点
    # cv.imwrite('./picture/label.png', img_out)
    return img_out

def getlabel(path):
    img1 = draw_label(path)
    list_out = np.zeros(shape=(1024, 1280))

    for i in range(img1.shape[0]):
        for j in range(img1.shape[1]):
            if img1[i, j] == 255:
                list_out[i, j] = 1
    return list_out


import sys
import os

def print_usage():
    print("Usage:")
    print("stringer.py 1 <file> <string_to_count>")
    print("            2 <file> <string_to_remove>")

if __name__ == "__main__":
    if len(sys.argv) < 2 or sys.argv[1] == "-h":
        print_usage()
        exit(1)

    if (sys.argv[1] == '1'):
        print(f"Counting '{sys.argv[3]}' in {os.path.join(os.getcwd(), sys.argv[2])}")

        occurrences = 0
        with open(sys.argv[2]) as input_file:
            for line in input_file:
                if line.find(sys.argv[3]) != -1:
                    occurrences += 1

        print(f"There are {occurrences} occurrences")


#!/usr/bin/env python3.8
"""
Given two rectangles, determine if they overlap. The rectangles are defined as a Dictionary, for example:
r1 = {

         # x and y coordinates of the bottom-left corner of the rectangle
         'x': 2 , 'y': 4,

         # Width and Height of rectangle
         'w':5,'h':12}
"""

import copy

def overlap(coord1, dim1, coord2, dim2):
	greater = max(coord1, coord2)

	smaller = min(coord1+dim1, coord2+dim2)
	if greater > smaller:
		return None, None
	return greater, smaller - greater

def rectOverlap(R_1, R_2):
	x_overlap, x_dim = overlap(R_1['x'], R_1['w'], R_2['x'], R_2['w'])
	y_overlap, y_dim = overlap(R_1['y'], R_1['h'], R_2['y'], R_2['h'])

	if not y_dim or not x_dim:
		if x_overlap and y_overlap:
			if x_dim or y_dim:
				print('side overlap at',x_overlap,x_dim,y_overlap,y_dim)
			else:
				print("point overlap at",x_overlap,y_overlap)
		else:
			print("no overlap")
		return

	print(x_overlap, x_dim, y_overlap, y_dim)

r1 = {'x': 2, 'y': 4, 'w':5, 'h':12}
r2 = {'x': 1, 'y': 5, 'w':7, 'h':14}
rectOverlap(r1, r2)

r1 = {'x': 5, 'y': 5, 'w':5, 'h':5}
r2 = {'x': 1, 'y': 5, 'w':4, 'h':14}
rectOverlap(r1, r2)

r1 = {'x': 5, 'y': 4, 'w':5, 'h':5}
r2 = {'x': 1, 'y': 1, 'w':4, 'h':3}
rectOverlap(r1, r2)

r1 = {'x': 5, 'y': 4, 'w':5, 'h':5}
r2 = {'x': 1, 'y': 1, 'w':4, 'h':2}
rectOverlap(r1, r2)


import core.modules
import core.modules.module_registry
from core.modules.vistrails_module import Module, ModuleError
import numpy
import scipy
import scipy.ndimage
from Array import *
from Matrix import *

class ArrayImaging(object):
    my_namespace = 'numpy|imaging'

class ExtractRGBAChannel(ArrayImaging, Module):
    """ Extract a single color channel from an array representing an
    RGBA type image.  This will return a 2D array with the single channel
    specified as the scalar elements """
    def compute(self):
        im = self.get_input("Image").get_array()
        chan = self.get_input("Channel")
        ar = im[:,:,chan]
        out = NDArray()
        out.set_array(ar)
        self.set_output("Output Array", out)

    @classmethod
    def register(cls, reg, basic):
        reg.add_module(cls, namespace=cls.my_namespace)
        reg.add_input_port(cls, "Image", (NDArray, 'Image Array'))
        reg.add_input_port(cls, "Channel", (basic.Integer, 'Channel'))
        reg.add_output_port(cls, "Output Array", (NDArray, 'Output Array'))

class GaussianGradientMagnitude(ArrayImaging, Module):
    """ Calculate the Gradient Magnitude of an input NDArray using gaussian derivatives.
    The standard-deviation of the Gaussian filter are given for each axis as a sequence
    or as a single number, in which case the filter will be isotropic. """
    def compute(self):
        im = self.get_input("Image")
        sigma = self.get_input_list("Sigmas")
        if len(sigma) <= 1:
            sigma = sigma[0]
        der = scipy.ndimage.gaussian_gradient_magnitude(im.get_array(), sigma)
        out = NDArray()
        out.set_array(der)
        self.set_output("Output Array", out)

    @classmethod
    def register(cls, reg, basic):
        reg.add_module(cls, namespace=cls.my_namespace)
        reg.add_input_port(cls, "Image", (NDArray, 'Image Array'))
        reg.add_input_port(cls, "Sigmas", (basic.Float, 'Standard Deviations'))
        reg.add_output_port(cls, "Output Array", (NDArray, 'Output Array'))

class JointHistogram(ArrayImaging, Module):
    """ Calculate the Joint Histogram of 2 inputs.  The inputs can be of arbitrary dimension,
    but must be equivalently sized. """
    def compute(self):
        in_x = self.get_input("Array One").get_array()
        in_y = self.get_input("Array Two").get_array()
        size_x = self.get_input("Bins X")
        size_y = self.get_input("Bins Y")

        take_log = True
        if self.has_input("Log10"):
            take_log = self.get_input("Log10")

        out_ar = numpy.zeros((size_x, size_y))
        min_x = in_x.min()
        max_x = in_x.max() - min_x
        min_y = in_y.min()
        max_y = in_y.max() - min_y

        in_x = in_x.flatten()
        in_y = in_y.flatten()

        for i in xrange(in_x.size):
            x_cor = int(((in_x[i] - min_x)/max_x) * (size_x - 1))
            y_cor = int(((in_y[i] - min_y)/max_y) * (size_y - 1))

            out_ar[x_cor,y_cor] += 1.0

        if take_log:
            out_ar = out_ar + 1.0
            out_ar = scipy.log(out_ar)
        out = NDArray()
        out_ar = out_ar.transpose()
        out_ar = out_ar[::-1]
        out.set_array(out_ar)
        self.set_output("Joint Histogram", out)

    @classmethod
    def register(cls, reg, basic):
        reg.add_module(cls, namespace=cls.my_namespace)
        reg.add_input_port(cls, "Array One", (NDArray, 'X Axis Input'))
        reg.add_input_port(cls, "Array Two", (NDArray, 'Y Axis Input'))
        reg.add_input_port(cls, "Log10", (basic.Boolean, 'Use Log of Histogram'), True)
        reg.add_input_port(cls, "Bins X", (basic.Integer, 'Number of X Bins'))
        reg.add_input_port(cls, "Bins Y", (basic.Integer, 'Number of Y Bins'))
        reg.add_output_port(cls, "Joint Histogram", (NDArray, 'Joint Histogram'))

class GaussianSmooth(ArrayImaging, Module):
    """ Smooth the Input array with a multi-dimensional gaussian kernel.
    The standard-deviation of the Gaussian filter are given for each axis as a sequence
    or as a single number, in which case the filter will be isotropic. """
    def compute(self):
        im = self.get_input("Input Array")
        sigma = self.get_input_list("Sigmas")
        if len(sigma) <= 1:
            sigma = sigma[0]
        der = scipy.ndimage.gaussian_filter(im.get_array(), sigma)
        out = NDArray()
        out.set_array(der)
        self.set_output("Output Array", out)

    @classmethod
    def register(cls, reg, basic):
        reg.add_module(cls, namespace=cls.my_namespace)
        reg.add_input_port(cls, "Input Array", (NDArray, 'Image Array'))
        reg.add_input_port(cls, "Sigmas", (basic.Float, 'Standard Deviations'))
        reg.add_output_port(cls, "Output Array", (NDArray, 'Output Array'))

class MedianFilter(ArrayImaging, Module):
    """ Smooth the Input array with a multi-dimensional median filter.  """
    def compute(self):
        im = self.get_input("Input Array")
        k_size = self.get_input("Size")
        der = scipy.ndimage.median_filter(im.get_array(), size=k_size)
        out = NDArray()
        out.set_array(der)
        self.set_output("Output Array", out)

    @classmethod
    def register(cls, reg, basic):
        reg.add_module(cls, namespace=cls.my_namespace)
        reg.add_input_port(cls, "Input Array", (NDArray, 'Image Array'))
        reg.add_input_port(cls, "Size", (basic.Integer, 'Kernel Size'))
        reg.add_output_port(cls, "Output Array", (NDArray, 'Output Array'))

class ImageDifference(ArrayImaging, Module):
    """ Calculate the difference between two input images. """
    def compute(self):
        im = self.get_input("Input 1")
        im2 = self.get_input("Input 2")

        da_ar = im.get_array() - im2.get_array()
        da_ar = numpy.abs(da_ar)

        out = NDArray()
        out.set_array(da_ar)
        self.set_output("Output", out)

    @classmethod
    def register(cls, reg, basic):
        reg.add_module(cls, namespace=cls.my_namespace)
        reg.add_input_port(cls, "Input 1", (NDArray, 'Image Array'))
        reg.add_input_port(cls, "Input 2", (NDArray, 'Image Array'))
        reg.add_output_port(cls, "Output", (NDArray, 'Output Array'))

class ImageNormalize(ArrayImaging, Module):
    """ Move the range of the image to [0,1] """
    def compute(self):
        im = self.get_input("Input")
        im_max = im.get_array().max()
        im_ar = im.get_array() / im_max

        out = NDArray()
        out.set_array(im_ar)
        self.set_output("Output", out)

    @classmethod
    def register(cls, reg, basic):
        reg.add_module(cls, namespace=cls.my_namespace)
        reg.add_input_port(cls, "Input", (NDArray, 'Image Array'))
        reg.add_output_port(cls, "Output", (NDArray, 'Output Array'))

class SobelGradientMagnitude(ArrayImaging, Module):
    """ Use n-dimensional sobel kernels to compute the gradient magnitude
    of an image """
    def compute(self):
        im = self.get_input("Input").get_array()
        mag = numpy.zeros(im.shape)
        for i in xrange(im.ndim):
            kern = scipy.ndimage.sobel(im, axis=i)
            mag += kern*kern

        out = NDArray()
        out.set_array(numpy.sqrt(mag))
        self.set_output("Output", out)

    @classmethod
    def register(cls, reg, basic):
        reg.add_module(cls, namespace=cls.my_namespace)
        reg.add_input_port(cls, "Input", (NDArray, 'Image Array'))
        reg.add_output_port(cls, "Output", (NDArray, 'Output Array'))


# -*- coding: utf-8 -*-
##############################################################################
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published
#    by the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see http://www.gnu.org/licenses/.
#
##############################################################################

from openerp import models, api

class ProductTemplate(models.Model):
    _inherit = 'product.template'

    @api.multi
    def open_related_partnerinfo(self):
        result = self._get_act_window_dict(
            'product_pricelist_partnerinfo.pricelist_partnerinfo_action')
        result['domain'] = "[('product_tmpl_id', 'in', " + str(self.ids) + ")]"
        return result

class ProductProduct(models.Model):
    _inherit = 'product.product'

    @api.multi
    def open_related_partnerinfo(self):
        self.ensure_one()
        result = self.product_tmpl_id._get_act_window_dict(
            'product_pricelist_partnerinfo.pricelist_partnerinfo_action')
        result['domain'] = ("[('product_tmpl_id', '=', " +
                            str(self.product_tmpl_id.id) + ")]")
        return result


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

import socket
import sys

BUFSIZE = 1024

def start_client(address):
    tcpCliSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    tcpCliSock.connect(address)

    while True:
        data = raw_input('> ')
        if not data:
            break
        tcpCliSock.send(data)
        data = tcpCliSock.recv(BUFSIZE)
        if not data:
            break
        print(data)

if __name__ == '__main__':
    host = '127.0.0.1'
    port = 1234

    if len(sys.argv) == 2:
        host = sys.argv[1]
    elif len(sys.argv) == 3:
        host = sys.argv[1]
        port = int(sys.argv[2])
    start_client((host, port))


x_house_height = float(input())
y_side_wall_lenght = float(input())
h_triangle_side_height = float(input())
window_side = 1.5
door_side_a = 1.2
door_side_b = 2
window_area = window_side * window_side
windows_total_area = window_area * 2
door_area = door_side_a * door_side_b
side_wall_area = x_house_height * y_side_wall_lenght
side_walls_area = side_wall_area * 2 - windows_total_area
front_wall_area = x_house_height * x_house_height
front_back_walls_area = front_wall_area * 2 - door_area
roof_side_wall_area = x_house_height * y_side_wall_lenght
roof_side_walls_area = roof_side_wall_area * 2
roof_front_side_area = x_house_height * h_triangle_side_height / 2
roof_front_back_sides_area = roof_front_side_area * 2
walls_total_area = side_walls_area + front_back_walls_area
roof_total_area = roof_side_walls_area + roof_front_back_sides_area
green_paint = walls_total_area / 3.4
red_paint = roof_total_area / 4.3
print(f'{green_paint:.2f}')
print(f'{red_paint:.2f}')


import torch
import torch.nn as nn

from GAWWN.tools.config import cfg
from GAWWN.tools.tools import replicate

class keyMulD(nn.Module):
    def __init__(self):
        super(keyMulD, self).__init__()
        self.ndf = cfg.GAN.NDF
        self.nt_d = cfg.TEXT.TXT_FEATURE_DIM
        self.keypoint_dim = cfg.KEYPOINT.DIM

        self.conv = nn.Sequential(
            nn.Conv2d(self.nt_d + self.ndf * 2, self.ndf * 2, 3, 1, 1),
            nn.BatchNorm2d(self.ndf * 2),
            nn.LeakyReLU(0.2, True)
        )

    def forward(self, imgGlobal, prep_txt_d, locs):
        prep_txt_d = replicate(prep_txt_d, 2, self.keypoint_dim)    # (bs, nt_d, 16)
        prep_txt_d = replicate(prep_txt_d, 3, self.keypoint_dim)    # (bs, nt_d, 16, 16)
        imgTextGlobal = torch.cat((imgGlobal, prep_txt_d), 1)       # (bs, nt_d + ndf * 2, 16, 16)
        imgTextGlobal = self.conv(imgTextGlobal)    # (bs, ndf * 2, 16, 16)

        # loc (bs, num_elt, keypoint_dim, keypoint_dim)
        locs = torch.sum(locs, 1)     # (bs, keypoint_dim, keypoint_dim)
        locs = torch.clamp(locs, 0, 1)
        locs = replicate(locs, 1, self.ndf * 2)

        x = imgTextGlobal * locs
        return x

class regionD(nn.Module):
    def __init__(self):
        super(regionD, self).__init__()
        self.ndf = cfg.GAN.NDF
        self.num_elt = cfg.KEYPOINT.NUM_ELT

        self.F_KeyMulD = keyMulD()
        self.conv = nn.Sequential(
            nn.Conv2d(self.ndf * 2 + self.num_elt, self.ndf * 2, 1),
            nn.BatchNorm2d(self.ndf * 2),
            nn.LeakyReLU(0.2, True),
            nn.Conv2d(self.ndf * 2, self.ndf, 2)
        )
        self.LReLU = nn.LeakyReLU(0.2, True)

    def forward(self, imgGlobal, prep_txt_d, locs):
        keyMul = self.F_KeyMulD(imgGlobal, prep_txt_d, locs)
        x = torch.cat((keyMul, locs), 1)        # (bs, ngf * 2 + num_elt, 16, 16)
        x = x.contiguous()
        x = self.conv(x)
        x = x.mean(3)
        x = x.mean(2)
        x = self.LReLU(x)
        return x

class globalD(nn.Module):
    def __init__(self):
        super(globalD, self).__init__()
        self.ndf = cfg.GAN.NDF
        self.nt_d = cfg.TEXT.TXT_FEATURE_DIM

        self.convGlobal = nn.Sequential(
            nn.Conv2d(self.ndf * 2, self.ndf * 4, 4, 2, 1),
            nn.BatchNorm2d(self.ndf * 4),
            nn.LeakyReLU(0.2, True),
            nn.Conv2d(self.ndf * 4, self.ndf * 8, 4, 2, 1),
            nn.BatchNorm2d(self.ndf * 8),
            nn.LeakyReLU(0.2, True)
        )

        self.conv = nn.Sequential(
            nn.Conv2d(self.ndf * 8 + self.nt_d, self.ndf * 4, 1),
            nn.BatchNorm2d(self.ndf * 4),
            nn.LeakyReLU(0.2, True),
            nn.Conv2d(self.ndf * 4, self.ndf, 4),
            nn.BatchNorm2d(self.ndf),
            nn.LeakyReLU(0.2, True)
        )

    def forward(self, imgGlobal, prep_txt_d):
        img = self.convGlobal(imgGlobal)      # (bs, ndf * 8, 4, 4)

        txtGlobal = replicate(prep_txt_d, 2, 4)      # (bs, nt_d, 4)
        txtGlobal = replicate(txtGlobal, 3, 4)      # (bs, nt_d, 4, 4)

        imgTxtGlobal = torch.cat((img, txtGlobal), 1)     # (bs, nt_d + ndf * 8, 4 ,4)
        imgTxtGlobal = imgTxtGlobal.contiguous()
        imgTxtGlobal = self.conv(imgTxtGlobal)  # (bs, ndf, 1, 1)
        imgTxtGlobal = imgTxtGlobal.view(-1, self.ndf)

        return imgTxtGlobal

class Dis(nn.Module):
    def __init__(self):
        super(Dis, self).__init__()
        self.ndf = cfg.GAN.NDF
        self.nt = cfg.TEXT.TXT_EMBEDDING_DIM
        self.nt_d = cfg.TEXT.TXT_FEATURE_DIM

        self.prep_txtD = nn.Sequential(
            nn.Linear(self.nt, self.nt_d),
            nn.LeakyReLU(0.2, True)
        )
        self.imgGlobalD = nn.Sequential(
            nn.Conv2d(3, self.ndf, 4, 2, 1),
            nn.LeakyReLU(0.2, True),
            nn.Conv2d(self.ndf, self.ndf, 4, 2, 1),
            nn.LeakyReLU(0.2, True),
            nn.Conv2d(self.ndf, self.ndf * 2, 4, 2, 1),
            nn.BatchNorm2d(self.ndf * 2),
            nn.LeakyReLU(0.2, True),
            nn.Conv2d(self.ndf * 2, self.ndf * 2, 3, 1, 1),
            nn.BatchNorm2d(self.ndf * 2),
            nn.LeakyReLU(0.2, True)
        )

        self.F_regionD = regionD()
        self.F_globalD = globalD()
        self.judge = nn.Sequential(
            nn.Linear(self.ndf * 2, self.ndf),
            nn.BatchNorm1d(self.ndf),
            nn.LeakyReLU(0.2, True),
            nn.Linear(self.ndf, 1),
            nn.Sigmoid()
        )

    def forward(self, img, txt, locs):
        prep_txt_d = self.prep_txtD(txt)
        image_Global = self.imgGlobalD(img)
        region_d = self.F_regionD(image_Global, prep_txt_d, locs)
        global_d = self.F_globalD(image_Global, prep_txt_d)
        x = torch.cat((region_d, global_d), 1)
        x = self.judge(x)
        return x


import json
from web3 import Web3
from decimal import Decimal
from router import*
import time
# add blockchain connection information

cronos_mainnet_rpc = "ws://rpc.vvs.finance/"
w3 = Web3(Web3.WebsocketProvider(cronos_mainnet_rpc, websocket_timeout= 6000))
ERC20ABI = json.load(open('./erc20_abi.abi'))

#getSelector("swapExactTokensForTokens(uint256,uint256,address[],address,uint256)")= 0x38ed1739
#getSelector("swapExactETHForTokens(uint256 amountOutMin, address[] path, address to, uint256 deadline)")= 0x7ff36ab5
#getSelector("swapExactTokensForETH(uint256,uint256,address[],address,uint256)")= 0x18cbafe5

mycontract = '0x109C48345e84459C658e79e806F6DdB236DbDD26'
# multilswap = Web3.toChecksumAddress(mycontract)
# multilswap_abi = json.loads()
# multilswap_contract = w3.eth.contract(address = multilswap, abi= multilswap_abi)

# amountIn = optimalAmount

def dataswap_encode(contract, amountIn, amountOut, path, mycontract):
	deadline = 1000
	dataswap = contract.encodeABI(fn_name="swapExactTokensForTokens", args=[amountIn,amountOut, path, mycontract,deadline])
	return dataswap

def dataswap(route,tokenIn, tokenOut, amountIn, mycontract):
	# route = trade['route']
	tos = []
	tos = [t['router:'] for t in route]
	data = []
	_tokenInapproveaddr = []
	n= 0
	for pair in route:

		if pair['router:'] == '0x145863Eb42Cf62847A6Ca784e6416C1682b1b2Ae':
			contract = VVS_ROUTER_CONTRACT
		elif pair['router:'] == '0x145677FC4d9b8F19B5D56d1820c48e0443049a30':
			contract = MMF_ROUTER_CONTRACT
		elif pair['router:'] == '0xcd7d16fB918511BF7269eC4f48d61D79Fb26f918':
			contract = CRONA_ROUTER_CONTRACT
		elif pair['router:'] == '0x5bFc95C3BbF50579bD57957cD074fa96a4d5fF9F':
			contract = CYBORG_ROUTER_CONTRACT

		if n == 0:
			amountIn = amountIn
			if pair['token0']['address'] == tokenIn['address']:
				tokenOut = pair['token1']
			else:
				tokenOut = pair['token0']
			path = [tokenIn['address'],tokenOut['address']]
			_tokenInapproveaddr.append(tokenIn['address'])
			amountOut_list = contract.functions.getAmountsOut(amountIn, path).call()
			amountOut= amountOut_list[1]
			print('amountout1:',amountOut)
			encode = dataswap_encode(contract, amountIn, amountOut, path, mycontract)
			data.append(encode)
			# approve =
			tokenIn = tokenOut
			amountIn = amountOut

		if n > 0:

			if pair['token0']['address'] == tokenIn['address']:
				tokenOut = pair['token1']
			else:
				tokenOut = pair['token0']
			path = [tokenIn['address'],tokenOut['address']]
			_tokenInapproveaddr.append(tokenIn['address'])
			amountOut_list = contract.functions.getAmountsOut(amountIn, path).call()
			amountOut= amountOut_list[1]
			print('amountout2:',amountOut)
			encode = dataswap_encode(contract, amountIn, amountOut, path, mycontract)
			data.append(encode)
			# approve =
			tokenIn = tokenOut
			amountIn = amountOut
		n+=1
	print('profit:', amountIn - 50*pow(10,18))
	return tos, data, _tokenInapproveaddr


from measurements.models import Location, Station, SourceType, Network
from django.contrib.gis.geos import Point
import requests
from bs4 import BeautifulSoup
from datetime import datetime, timedelta
import pandas as pd
import re

IOC = "http://www.ioc-sealevelmonitoring.org/station.php?code={}"

stations = (
        ('Trieste', 'TR22'),
        ('Venice', 'VE19'),
        ('Ancona', 'AN15'),
        ('S. Benedetto Del Tronto', 'SB36'),
        ('Stari Grad', 'stari'),
        ('Vela Luka', 'vela'),
        ('Sobra', 'sobr'),
        ('Otranto', 'OT15'),
        ('Kerkyra, Corfu', 'corf'),
        ('Crotone', 'CR08'),
        ('Le Castella', 'lcst'),
        ('Itea', 'itea'),
        ('Panormos', 'pano'),
        ('Aigio', 'aigi'),
        ('Katakolo', 'kata'),
        # ('Kyparissia', 'kypa'),
        )

ioc_source, created = SourceType.objects.get_or_create(code='ioc')
ioc_network, created = Network.objects.get_or_create(code='ioc')

# IOC stations
for label, code in stations:
    r = requests.get(IOC.format(code))
    # print(r.text)
    soup = BeautifulSoup(r.text)
    for elem in soup(text='Latitude   '):
        lat = float(elem.find_next('td').contents[0])
    for elem in soup(text='Longitude   '):
        lon = float(elem.find_next('td').contents[0])
    # print(lon, lat)
    l, created = Location.objects.get_or_create(label=label)
    l.geo = Point(lon, lat)
    l.save()
    # print(label, l)
    s, created = Station.objects.get_or_create(code=code,
                                               label=label,
                                               source=ioc_source,
                                               network=ioc_network,
                                               location=l)


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

'''
    sort.Sort
    Sort algorithm.
'''
class Sort(object):
    __CUTOFF = 5

    def _median3(arr, beg, end):
        mid = (beg + end) // 2

        if arr[beg] > arr[mid]:
            arr[beg], arr[mid] = arr[mid], arr[beg]

        if arr[mid] > arr[end]:
            arr[mid], arr[end] = arr[end], arr[mid]

        if arr[beg] > arr[mid]:
            arr[beg], arr[mid] = arr[mid], arr[beg]

        arr[mid], arr[end-1] = arr[end-1], arr[mid]

        return arr[end-1]

    def _qsort(arr, low, high, cmp_obj):
        if high - low < Sort.__CUTOFF:
            Sort._isort(arr, low, high, cmp_obj)
        else:
            pivot = Sort._median3(arr, low, high)
            lbeg = low + 1
            rend = high - 2

            while True:
                while arr[lbeg] < pivot:
                    lbeg += 1
                while arr[rend] > pivot:
                    rend -= 1

                if lbeg < rend:
                    arr[lbeg], arr[rend] = arr[rend], arr[lbeg]
                    lbeg += 1
                    rend -= 1
                else:
                    arr[lbeg], arr[high-1] = arr[high-1], arr[lbeg]
                    break

            Sort._qsort(arr, low, lbeg - 1, cmp_obj)
            Sort._qsort(arr, lbeg + 1, high, cmp_obj)

    def _isort(arr, low, high, cmp_obj):
        i = low + 1

        while i <= high:
            tmp = arr[i]
            j = i - 1

            while j >= low:
                if tmp < arr[j]:
                    arr[j+1] = arr[j]
                else:
                    break

                j -= 1

            arr[j+1] = tmp # bug: j+1 instead of j
            i += 1

    def _shsort(arr, low, high, cmp_obj):
        step = (high - low + 1) // 2

        while step > 0:
            i =  step

            while i <= high:
                tmp = arr[i]
                j = i - step

                while j >= low:
                    if tmp < arr[j]:
                        arr[j+step] = arr[j]
                    else:
                        break

                    j -= step

                arr[j+step] = tmp # bug: j+1 instead of j
                i += 1

            step //= 2

    def _perc_down(arr, pos, cnt, cmp_obj):
        '''Used for heap ajustment'''

        lchild = lambda x: 2 * x + 1
        tmp = arr[pos]
        p = pos

        while lchild(p) < cnt:
            lc = lchild(p)
            rc = lc + 1
            min_child = rc if rc < cnt and arr[rc] < arr[lc] else lc

            if tmp > arr[min_child]:
                arr[p] = arr[min_child]
                p = min_child
            else:
                break

        arr[p] = tmp

    def _swim_up(arr, pos, cmp_obj):
        '''Used for heap ajustment'''

        root = lambda x: x//2-1 if x%2 == 0 else x//2
        tmp = arr[pos]
        p = pos

        while root(p) >= 0:
            min_child = root(p)

            if tmp < arr[min_child]:
                arr[p] = arr[min_child]
                p = min_child
            else:
                break

        arr[p] = tmp

    def make_heap(arr, cmp_obj):
        cnt = len(arr)
        pos = cnt // 2 - 1

        # From the last node which has children,
        # to the first node, do perc_down
        while pos >= 0:
            Sort._perc_down(arr, pos, cnt, cmp_obj)
            pos -= 1

    def _merge(arr, beg, mid, end, cmp_obj):
        i = 0
        lbeg = beg
        rbeg = mid
        rend = end
        aux = [None] * (end - beg + 1)

        while lbeg < mid and rbeg < end + 1:
            if arr[lbeg] < arr[rbeg]:
                aux[i] = arr[lbeg]
                lbeg += 1
            else:
                aux[i] = arr[rbeg]
                rbeg += 1

            i += 1

        while lbeg < mid:
            aux[i] = arr[lbeg]
            i += 1
            lbeg += 1

        while rbeg < end + 1:
            aux[i] = arr[rbeg]
            i += 1
            rbeg += 1

        # copy sorted data back
        i = 0
        while beg <= end:
            arr[beg] = aux[i]
            beg += 1
            i += 1

    def _msort(arr, beg, end, cmp_obj):
        if end - beg + 1 < Sort.__CUTOFF:
            Sort._isort(arr, beg, end, cmp_obj)
        else:
            Sort._msort(arr, beg, (beg+end)//2, cmp_obj)
            Sort._msort(arr, (beg+end)//2+1, end, cmp_obj)
            Sort._merge(arr, beg, (beg+end)//2+1, end, cmp_obj)

    def is_sorted(arr, cmp_obj = None):
        ret = True
        i = 1

        while i < len(arr):
            if arr[i] < arr[i-1]:
                ret = False
                break
            i += 1

        return ret

    def qsort(arr, cmp_obj=None):
        Sort._qsort(arr, 0, len(arr)-1, cmp_obj)

    def isort(arr, cmp_obj=None):
        Sort._isort(arr, 0, len(arr)-1, cmp_obj)

    def shsort(arr, cmp_obj=None):
        Sort._shsort(arr, 0, len(arr)-1, cmp_obj)

    def msort(arr, cmp_obj=None):
        Sort._msort(arr, 0, len(arr)-1, cmp_obj)

    def hsort(arr, cmp_obj=None):
        Sort.make_heap(arr, cmp_obj=None)

        i = len(arr) - 1
        while i > 0:
            arr[0], arr[i] = arr[i], arr[0]
            Sort._perc_down(arr, 0, i, cmp_obj)
            i -= 1

if __name__ == '__main__':
    import random

    arr = random.sample(range(500), 10)
    arr = arr * 3
    print("before sort: %s" %arr)

    Sort.shsort(arr)
    print("after shsort: %s" %arr)
    assert Sort.is_sorted(arr)

    Sort.msort(arr)
    print("after msort: %s" %arr)
    assert Sort.is_sorted(arr)

    Sort.hsort(arr)
    print("after hsort: %s" %arr)

    Sort.qsort(arr)
    assert Sort.is_sorted(arr)

    Sort.qsort(arr)
    assert Sort.is_sorted(arr)

    print("after qsort: %s" %arr)


"""
Creation:
    Author: Martin Grunnill
    Date: 2022-11-01
Description: Getting prevelance data for world cup teams.

"""
import copy
import pandas as pd
import datetime

schedule_df = pd.read_csv('data_extraction/Fifa 2022 Group stages matches with venue capacity.csv')
covid_data = pd.read_csv('https://covid.ourworldindata.org/data/owid-covid-data.csv')
population_df = pd.read_csv('data_extraction/Population estimates world bank.csv',header=2, index_col='Country Name') # downloaded from https://data.worldbank.org/indicator/SP.POP.TOTL https://api.worldbank.org/v2/en/indicator/SP.POP.TOTL?downloadformat=csv

# need to change covid_data to datetime type
covid_data.date = pd.to_datetime(covid_data.date)

date_to = datetime.datetime(2022, 11, 18)
covid_data = covid_data[covid_data.date<=date_to]

#%%
# select data for only countries in the world cup
countries = set(schedule_df['Team A'].unique().tolist() +
                schedule_df['Team B'].unique().tolist())

# looking at the data set (https://covid.ourworldindata.org/data/owid-covid-data.csv) new cases smoothed
# for England and Wales is pooled under United Kingdom
proxies = copy.deepcopy(countries)
proxies.add('United Kingdom')
proxies.remove('England')
proxies.remove('Wales')
covid_data = covid_data[covid_data.location.isin(proxies)]
# sense check to make sure we have selected the right places
selected_proxies = covid_data.location.unique()
len(proxies)==len(selected_proxies)
#%% Selecting most recent available data for new_cases_smoothed
# remove missing data
covid_data.new_cases_smoothed = covid_data.new_cases_smoothed.replace({0:None})
covid_data = covid_data[pd.notnull(covid_data.new_cases_smoothed)]
prevelance_records = []
for country in countries:
    if country in ['England', 'Wales']:
        proxy = 'United Kingdom'
    else:
        proxy = country
    # select proxie
    location_data = covid_data[covid_data.location==proxy]
    # latest date for which we have information
    latest_date = location_data.date.max()
    latest_date_data = location_data[location_data.date==latest_date]
    cases_smoothed = latest_date_data.new_cases_smoothed.iloc[0]
    if proxy=='South Korea':
        population = population_df.loc['Korea, Rep.', '2021']
    elif proxy == 'Iran':
        population = population_df.loc['Iran, Islamic Rep.', '2021']
    else:
        population = population_df.loc[proxy,'2021']

    entry = {'country': country,
             'proxy': proxy,
             'date': latest_date,
             'case_prevalence': cases_smoothed/population,
             }

    prevelance_records.append(entry)

prevelance_df = pd.DataFrame(prevelance_records)
#%% Adding data on infection to detection ratio

# Getting data frame
# location of zip file downloaded from https://ghdx.healthdata.org/sites/default/files/record-attached-files/HME_COVID_19_IES_2019_2021_RATIOS.zip
zip_file = 'data_extraction/HME_COVID_19_IES_2019_2021_RATIOS.zip'
# read file
detection_raio_df = pd.read_csv(zip_file)
# Selecting detections/infections
detection_raio_df.measure_name.unique()
detection_raio_df = detection_raio_df[detection_raio_df.measure_name=='Cumulative infection-detection ratio']
# change date to date
detection_raio_df['date'] = pd.to_datetime(detection_raio_df['date'])
detection_raio_df = detection_raio_df[detection_raio_df.date==detection_raio_df.date.max()]
detection_raio_df.location_name = detection_raio_df.location_name.replace({'USA':'United States','UK':'United Kingdom'})

values_list = ['value_mean','value_lower','value_upper']
# Change percent to raw number
for column in values_list:
    detection_raio_df[column] = detection_raio_df[column]/100
detection_raio_df.metric_name = 'raw'
# invert values so they are now infections/detected cases.
for column in values_list:
    detection_raio_df[column] = detection_raio_df[column]**-1

to_merge = detection_raio_df[detection_raio_df.location_name.isin(proxies)]
to_merge = to_merge[['location_name']+values_list]

prevelance_df = prevelance_df.merge(to_merge, left_on='proxy', right_on='location_name')
prevelance_df.rename(columns={'value_lower': 'ratio_upper',
                              'value_mean': 'ratio_mean',
                              'value_upper': 'ratio_lower'},
                     inplace=True)

prevelance_df['infection_prevalence_lower'] = prevelance_df.case_prevalence*prevelance_df.ratio_lower
prevelance_df['infection_prevalence_mean'] = prevelance_df.case_prevalence*prevelance_df.ratio_mean
prevelance_df['infection_prevalence_upper'] = prevelance_df.case_prevalence*prevelance_df.ratio_upper
# host min and max
host_min = prevelance_df[prevelance_df.country=='Qatar']['infection_prevalence_lower'].tolist()[0]
host_max = prevelance_df[prevelance_df.country=='Qatar']['infection_prevalence_upper'].tolist()[0]

# Everybody elses min max
visior_min = prevelance_df[prevelance_df.country!='Qatar']['infection_prevalence_lower'].min()
visior_max = prevelance_df[prevelance_df.country!='Qatar']['infection_prevalence_upper'].max()


class Solution:
    def minCostClimbingStairs(self, cost) -> int:
        """
        dp[i]表示登上第i个阶梯所花费的体力值。
        登上楼顶所花费的体力值为0，
        所以我们要登上n层阶梯的阶梯顶部，则要求dp[n+1]
        """
        n = len(cost)
        if n < 1:
            return 0
        dp = [0] * (n+1)
        cost.append(0)
        dp[0] = cost[0]
        dp[1] = cost[1]
        for i in range(2, n+1):
            dp[i] = min(dp[i-1], dp[i-2]) + cost[i]

        return dp[n]

s = Solution()
print(s.minCostClimbingStairs([]))


'''02-05-2021 Baekjoon Algorithm
   단계별 문제 풀이 - 12단계
   언어 - Python'''

# 2751
# N개의 수가 주어졌을 때, 이를 오름차순으로 정렬하는 프로그램을 작성하시오.
'''
N = int(input())
num = []

for _ in range(0, N):
    num.append(int(input()))

num.sort()
for i in range(0, N):
    print(num[i])

#################################################################################
## 시간초과 -> PyPy로 아래 코드 제출시 해결 / 아래 코드와 무엇이 다른지 잘 모르겠음##
#################################################################################

N = int(input())
nums = []

for i in range(N):
    nums.append(int(input()))

nums = sorted(nums)
for i in range(N):
     print(nums[i])
'''

# 10989
# N개의 수가 주어졌을 때, 이를 오름차순으로 정렬하는 프로그램을 작성하시오.

N = int(input())
num = []

for _ in range(0, N):
    num.append(int(input()))

num = sorted(num)
for i in range(0, N):
    print(num[i])


#khai báo hàm long_words với 2 tham số là n và str
def long_words(n, str):
    #khai báo list rỗng
    word_len=[]
    #gán list mới bằng cách tách các từ trong str( mỗi chữ cách nhau bởi dấu cách sẽ biến thành một phần tử của list)
    txt=str.split(" ")
    for x in txt:
        #nếu số ký tự của chuỗi x trong list txt lớn hơn n thì thêm vào cuối list word_len chuỗi x
        if len(x)>n:
            word_len.append(x)
    # trả về gía trị của list word_len
    return word_len
# hàm long_words được truyền tham số n = 3 và str =  "The quick brown fox jumps over the lazy dog" và in
print(long_words(3, "The quick brown fox jumps over the lazy dog"))


import os
import sys

sys.path.append("..")
import taobaoTry.taobaoTryUtils
from task.logUtils import logUtils

class taobaoTryTask:

    def enum(**enums):
        return type('Enum', (), enums)
    taskType = enum(JingXuan=1, All=2)
    mTaskTypeFor = taskType.All
    taobaoTryTaskLockFile = ".." + os.path.sep + "lockFile" + os.path.sep + "taobaoTry.lock"
    def __init__(self,taskTypeFor=taskType.All):
        global mTaskTypeFor
        taobaoTryTask.mTaskTypeFor = taskTypeFor
        if(taskTypeFor==taobaoTryTask.taskType.JingXuan):
            taobaoTryTask.taobaoTryTaskLockFile = ".." + os.path.sep + "lockFile" + os.path.sep + "taobaoJingXuanTry.lock"
        else:
            taobaoTryTask.taobaoTryTaskLockFile = ".." + os.path.sep + "lockFile" + os.path.sep + "taobaoAllTry.lock"

        if (os.path.exists(taobaoTryTask.taobaoTryTaskLockFile)):
            if (taskTypeFor == taobaoTryTask.taskType.JingXuan):
                logUtils.info("精选类型文件已存在，即将退出")
            else:
                logUtils.info("所有类型文件已存在，即将退出")
            os._exit(0)
        else:
            # os.mknod('.lock')
            if (taskTypeFor == taobaoTryTask.taskType.JingXuan):
                logUtils.info("精选类型创建文件")
            else:
                logUtils.info("所有类型创建文件")
            open(taobaoTryTask.taobaoTryTaskLockFile, "w")
            if(taskTypeFor==taobaoTryTask.taskType.JingXuan):
                self.actionJingXuanTask()
            else:
                self.actionAllTask()

    def __del__(self):
        if (os.path.exists(taobaoTryTask.taobaoTryTaskLockFile)):
            os.remove(taobaoTryTask.taobaoTryTaskLockFile)
        if (taobaoTryTask.mTaskTypeFor == taobaoTryTask.taskType.JingXuan):
            logUtils.info("精选类型退出程序")
        else:
            logUtils.info("所有类型退出程序")

    def actionAllTask(self):
        logUtils.info("actionAllTask所有")
        taobaoTry.taobaoTryUtils.taobaoTryUtils().handlePcTryList(None, 0, 1)  # 第一个参数传入负数是精选，传0是所有的都采集

    def actionJingXuanTask(self):
        logUtils.info("actionAllTask精选")
        taobaoTry.taobaoTryUtils.taobaoTryUtils().handlePcTryList(None, -1, 1)  # 第一个参数传入负数是精选，传0是所有的都采集

# taobaoTryTask(taobaoTryTask.taskType.JingXuan).actionJingXuanTask()
taobaoTryTask(taobaoTryTask.taskType.JingXuan)


from panda3d.core import CollisionNode, CollisionTube, CollisionBox, AmbientLight, Vec4, DirectionalLight
from FreedomCampaignGame.comm_with_server import ClientLogObject
client_logger = ClientLogObject().client_logger

class GameMap():
    def __init__(self, render, load_model_fun):
        self.render = render
        # 加载环境模型。将加载models文件夹中的environment.egg文件，返回该模型的指针
        self.load_model = load_model_fun
        self.scene = self.load_model("Models/Environment/environment")
        # 重新绘制要 渲染的模型。
        self.scene.reparent_to(self.render)
        client_logger.info("开始生成地图障碍物...")
        self.set_map_solid()
        client_logger.info("加载环境光、定向灯光..")
        self.load_light_all()

    def load_light_all(self):
        # 添加环境光, AmbientLight对象是一个节点
        self.ambient_light = AmbientLight("ambient light")
        self.ambient_light.set_color(Vec4(0.2, 0.2, 0.2, 1))
        self.ambient_light_node_path = self.render.attach_new_node(self.ambient_light)
        # 环境灯光默认自动影响指示节点下方的所有节点，希望灯光影响所有节点场景，则需要在render(渲染)上指定灯光
        self.render.set_light(self.ambient_light_node_path)
        # 添加定向光，可指定方向
        self.directional_light = DirectionalLight("directional light")
        self.directional_light_node_path = self.render.attach_new_node(self.directional_light)
        # 一个正面对着你的人，H相当于它左转，P相当于它向左倒也就是顺时针转，R是往前向你这边扑倒旋转，光照射出的位置变动
        self.directional_light_node_path.setHpr(45, -45, 0)
        self.render.set_light(self.directional_light_node_path)
        # 应用着色器生成器，希望它影响的NodePath上调用“ setShaderAuto”即可。这里是将着色器生成器应用于“渲染”
        self.render.setShaderAuto()

    def set_box_solid(self, size=(0, 1, 1, 1), show=True):
        # size, 第一个0暂时不知道干嘛，官网文档也没说明，后面3分别是长宽高
        box_solid = CollisionBox(size[0], size[1], size[2], size[3])
        box_node = CollisionNode("box")
        box_node.add_solid(box_solid)
        box = self.render.attach_new_node(box_node)
        if show:
            box.show()
        return box

    def set_tube_solid(self, size=(0, 0, 0, 0, 0, 0, 0.4), show=True):
        # 管由其起点、终点和半径定义。这里定义的一个管子物体从（-8，0，0）到为（8，0，0），半径为0.4的圆形管道
        set_tube_solid = CollisionTube(size[0], size[1], size[2], size[3], size[4], size[5], size[6])
        wall_node = CollisionNode("wall")
        wall_node.add_solid(set_tube_solid)
        wall = self.render.attach_new_node(wall_node)
        if show:
            wall.show()
        return wall

    def set_map_solid(self):
        # 放一个大的管子
        # wall = self.set_tube_solid(size=(-2.0, 0, 0, 2.0, 0, 0, 0.2))
        # wall.setY(-3)
        # 用box设置楼梯
        box = self.set_box_solid(size=(0, 1, 1.5, 0.2))
        box.setX(-2)
        box.setZ(0.2)
        box = self.set_box_solid(size=(0, 1, 1.5, 0.4))
        box.setX(-3)
        box.setZ(0.4)
        box = self.set_box_solid(size=(0, 1, 1.5, 0.6))
        box.setX(-4)
        box.setZ(0.6)

        # 用box设置墙，这里是弄门这里的两面墙
        box = self.set_box_solid(size=(0, 3.65, 0.1, 1.5))
        box.setY(8.1)
        box.setX(-4.3)
        box.setZ(1.5)
        box = self.set_box_solid(size=(0, 3.65, 0.1, 1.5))
        box.setY(8.1)
        box.setX(4.3)
        box.setZ(1.5)
        # 弄门，门栏
        box = self.set_box_solid(size=(0, 0.65, 0.1, 0.25))
        box.setY(8.2)
        box.setZ(0.25)
        # 门顶部
        box = self.set_box_solid(size=(0, 0.65, 0.1, 0.2))
        box.setY(8.1)
        box.setZ(2.04)

        box = self.set_box_solid(size=(0, 8, 0.1, 1.5))
        box.setY(-8.1)
        box.setZ(1.5)
        box = self.set_box_solid(size=(0, 0.1, 8, 1.5))
        box.setX(8.1)
        box.setZ(1.5)
        box = self.set_box_solid(size=(0, 0.1, 8, 1.5))
        box.setX(-8.1)
        box.setZ(1.5)


'''
투포인터로 접근했다가 실패함
'''

class Solution:
    def maxSubArray(self, nums: List[int]) -> int:
        sum_num = 0
        answer = -999999999999999

        for i in range(len(nums)):
            sum_num += nums[i]
            answer = max(answer, sum_num)

            if sum_num < 0:
                sum_num = 0
        return answer


import glob
import math
import os
import sys
import random

import numpy as np
import pandas as pd
import tensorflow as tf
from tqdm import tqdm
from model.siamese.config import cfg

tqdm.pandas()

"""
Files have to be stored in a structure:

main_folder/
    1/
        0030.jpg
        1080.jpg
        ...
    2/
        2400.jpg
        ...
    14/
        8800.jpg
        ...

This structure is going to extract images for 3 classes [1,2,14].
"""

AUTOTUNE = tf.data.experimental.AUTOTUNE

class DataGenerator(tf.keras.utils.Sequence):
    def __init__(
        self,
        folder_path=cfg.TRAIN.DATA_PATH,
        file_ext="jpg",
        debug=False,
        training=True,
        exclude_aug=False,
        step_size=1
    ):
        """
        Args:
            folder_path: string ## Path to folder with video frames
            file_ext: string | List[str] (optional) looking for files with this extension
            debug: boolean (optional) should generator display any warnings?
        """
        self.images = None
        self.debug = debug
        self.data_path = folder_path
        self.batch_size = cfg.TRAIN.BATCH_SIZE
        self.shuffle = True
        self.training = training
        self.step_size = step_size

        if not os.path.isdir(folder_path):
            print(
                "Images folder path {} does not exist. Exiting...".format(folder_path)
            )
            sys.exit()

        images = []
        for class_dir in os.scandir(folder_path):
            if type(file_ext) is str:
                file_ext = [file_ext]

            files = []
            for ext in file_ext:
                pattern = '*'
                if exclude_aug:
                    pattern = '*_*'
                files.extend(glob.glob(f"{class_dir.path}/{pattern}.{ext}"))
            for i, file in enumerate(sorted(files)):
                images.append((file, class_dir.name))

        self.org_images = images[::self.step_size]
        batched = self.batch_images()

        self.images = pd.DataFrame(batched, columns=["path", "label"])
        print(
            f'Found {len(self.images)} files for {len(self.images["label"].unique())} unique classes'
        )

    def __len__(self):
        return math.ceil(len(self.images) / cfg.TRAIN.BATCH_SIZE)

    def add_dataset(self, dataset):
        """

        Args:
            dataset: List[path, label]

        Returns:

        """
        self.org_images = self.org_images + dataset
        batched = self.batch_images()

        self.images = pd.DataFrame(batched, columns=["path", "label"])
        print(
            f'Found {len(self.images)} files for {len(self.images["label"].unique())} unique classes'
        )

    def batch_images(self):
        images = self.org_images.copy()
        random.shuffle(images)
        images = pd.DataFrame(images, columns=["path", "label"])
        low_class_count = min(images["label"].value_counts())
        unique_classes = images["label"].unique()

        class_dfs = {}
        for class_id in unique_classes:
            class_dfs[str(class_id)] = (
                images[images["label"] == class_id]
                .sample(frac=1)
                .reset_index(drop=True)
            )

        batched = []
        for i in range(0, low_class_count - 1, 2):
            for class_id in unique_classes:
                rows = class_dfs[str(class_id)].loc[[i, i + 1], :]
                batched.append(rows.to_numpy())

        batched = np.array(batched)

        batched = batched.reshape(
            (batched.shape[0] * batched.shape[1], batched.shape[2])
        )
        return batched

    @staticmethod
    def process_image(image_path, to_input=False):
        """
        Args:
            image_path: string
            to_input: boolean - should image be wrapped into input shape (1, 224, 224, 3)

        Returns:
            ((cfg.NN.INPUT_SIZE, cfg.NN.INPUT_SIZE, 3), class)

        """
        image = tf.keras.preprocessing.image.load_img(
            image_path, target_size=(cfg.NN.INPUT_SIZE, cfg.NN.INPUT_SIZE)
        )
        image = tf.keras.preprocessing.image.img_to_array(image)
        image = np.expand_dims(image, axis=0)
        image - tf.keras.applications.mobilenet_v2.preprocess_input(image)

        if to_input:
            return image
        return image[0]

    @staticmethod
    def process_label(label):
        """
        Args:
            label: string

        Returns:
            int

        """
        return int(label)

    def get_dataset(self):
        """
        Returns:
            tf.Dataset
        """
        target = (
            self.images.pop("label")
            .progress_map(DataGenerator.process_label)
            .to_numpy()
        )
        images = (
            self.images.pop("path").progress_map(DataGenerator.process_image).to_numpy()
        )
        reshaped_images = np.concatenate(images).reshape(
            (
                images.shape[0],
                images[1].shape[0],
                images[1].shape[1],
                images[1].shape[2],
            )
        )
        ds = tf.data.Dataset.from_tensor_slices((reshaped_images, target))
        ds = ds.cache()
        ds = ds.batch(cfg.TRAIN.BATCH_SIZE)
        ds = ds.prefetch(buffer_size=cfg.TRAIN.BATCH_SIZE)
        return ds

    def on_epoch_end(self):
        if self.training:
            batched = self.batch_images()

            self.images = pd.DataFrame(batched, columns=["path", "label"])

    def __getitem__(self, item):
        images = self.images.loc[
            item * cfg.TRAIN.BATCH_SIZE : (item + 1) * cfg.TRAIN.BATCH_SIZE
        ]
        target = images.pop("label").map(DataGenerator.process_label).to_numpy()
        images = images.pop("path").map(DataGenerator.process_image).to_numpy()
        reshaped_images = np.concatenate(images).reshape(
            (
                images.shape[0],
                images[1].shape[0],
                images[1].shape[1],
                images[1].shape[2],
            )
        )

        return reshaped_images, target


import os
import random
import sys

import yaml
import numpy as np

with open("config.yml", 'r') as ymlfile:
    cfg = yaml.load(ymlfile, Loader=yaml.FullLoader)
    ymlfile.close()

if not cfg['use_gpu']:
    os.environ['CUDA_VISIBLE_DEVICES'] = '-1'

seed = cfg['seed']
os.environ['PYTHONHASHSEED'] = str(seed)
random.seed(seed)
np.random.seed(seed)

import tensorflow as tf
from tensorflow.keras.datasets.fashion_mnist import load_data
from tensorflow.keras.utils import to_categorical

tf.compat.v1.set_random_seed(seed)

tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
tf.compat.v1.disable_eager_execution()  # To solve the speed problem of TF2

# Deprecated in tf2
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
config.intra_op_parallelism_threads = 4
config.inter_op_parallelism_threads = 4
tf.compat.v1.keras.backend.set_session(tf.compat.v1.Session(graph=tf.compat.v1.get_default_graph(), config=config))

from utils.deepnetwork import DeepNetwork
from utils.tracker import Tracker

def fashion_mnist(params):
    tracker = Tracker(seed, 'fashion_mnist.h5')

    # Load dataset
    (x_train, y_train), (x_test, y_test) = load_data()

    # Preprocessing
    # Reshape data as dataset is grayscaled
    x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], x_train.shape[2], 1)
    x_test = x_test.reshape(x_test.shape[0], x_test.shape[1], x_test.shape[2], 1)

    # Convert labels into categorial
    n_classes = params['n_classes']
    y_train = to_categorical(y_train, n_classes)
    y_test = to_categorical(y_test, n_classes)

    # Normalize images values
    x_train = x_train.astype('float32')
    x_test = x_test.astype('float32')
    x_train /= 255
    x_test /= 255

    # Create model
    model = DeepNetwork.build((28, 28, 1), params)

    # Train model
    model.fit(x_train, y_train,
        batch_size=params['batch_size'],
        epochs=params['n_epochs'],
        validation_data=(x_test, y_test),
        shuffle=True)

    # Evaluate performance
    scores = model.evaluate(x_test, y_test, verbose=1)
    print('Test loss:', scores[0])
    print('Test accuracy:', scores[1])

    # Save model
    tracker.save_model(model)

if __name__ == "__main__":
    fashion_mnist(cfg['train'])