import argparse
import logging

def main(pretrained_graph_path, dataset_path):
    from model import FacialRecognition
    from dataloader import load_inception_graph

    load_inception_graph(pretrained_graph_path)

    model = FacialRecognition(dataset_path, 'test_set.csv')
    model.train()

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('graph_path', nargs=1)
    parser.add_argument('dataset_path', nargs=1)
    parser.add_argument('--v', action='store_true')
    parser.add_argument('--vv', action='store_true')
    args = parser.parse_args()

    if args.vv:
        print('Super Verbose Mode enabled')
        logging.getLogger().setLevel(logging.DEBUG)
    elif args.v:
        print('Verbose Mode enabled')
        logging.getLogger().setLevel(logging.INFO)

    main(args.graph_path[0], args.dataset_path[0])


import pygame
import assets

clock = pygame.time.Clock()
win = pygame.display.set_mode((1365, 768))

#=================ROW 1====================

button1 = assets.Button(0, 0, 452, 253, (12, 12, 12), "", (255, 255, 255))
button2 = assets.Button(455, 0, 452, 253, (12, 12, 12), "", (255, 255, 255))
button3 = assets.Button(910, 0, 452, 253, (12, 12, 12), "", (255, 255, 255))

#=================ROW 2======================

button4 = assets.Button(0, 256, 452, 253, (12, 12, 12), "", (255, 255, 255))
button5 = assets.Button(455, 256, 452, 253, (12, 12, 12), "", (255, 255, 255))
button6 = assets.Button(910, 256, 452, 253, (12, 12, 12), "", (255, 255, 255))

#=================ROW 3=======================

button7 = assets.Button(0, 512, 452, 253, (12, 12, 12), "", (255, 255, 255))
button8 = assets.Button(455, 512, 452, 253, (12, 12, 12), "", (255, 255, 255))
button9 = assets.Button(910, 512, 452, 253, (12, 12, 12), "", (255, 255, 255))
buttons = [button1, button2, button3, button4, button5, button6, button7, button8, button9]

click = True
def messagebox(text):
    button_font = pygame.font.SysFont("Impact", 22)
    text_surface = button_font.render(text, 1, (45, 167, 235))
    win.blit(text_surface, (1365 /
                            2 - text_surface.get_width()/2, 768/2 - text_surface.get_height()/2))

def anyone_won():
    '''WIN CHECK FOR TIC TACK TOE'''
    if(button1.text == "X" and button2.text == "X" and button3.text == "X" or
         button4.text == "X" and button5.text == "X" and button6.text == "X" or
         button7.text == "X" and button8.text == "X" and button9.text == "X" or
         button3.text == "X" and button6.text == "X" and button9.text == "X" or
         button5.text == "X" and button2.text == "X" and button8.text == "X" or
         button1.text == "X" and button4.text == "X" and button7.text == "X" or
         button1.text == "X" and button5.text == "X" and button9.text == "X" or
         button3.text == "X" and button5.text == "X" and button7.text == "X" ):
        win.fill((0, 0, 0))
        messagebox("X has won a game")
        return

    if(button1.text == "O" and button2.text == "O" and button3.text == "O" or
         button4.text == "O" and button5.text == "O" and button6.text == "O" or
         button7.text == "O" and button8.text == "O" and button9.text == "O" or
         button3.text == "O" and button6.text == "O" and button9.text == "O" or
         button5.text == "O" and button2.text == "O" and button8.text == "O" or
         button1.text == "O" and button4.text == "O" and button7.text == "O" or
         button1.text == "O" and button5.text == "O" and button9.text == "O" or
         button3.text == "O" and button5.text == "O" and button7.text == "O" ):
        win.fill((0, 0, 0))
        messagebox("O has won a game")
        return
    i = 0
    for x in buttons:
        if x == "":
            continue
        i += 1

    if i == 9:

        return"Tie"

def whoose_turn():
    '''
    TURN DECIDER
    '''
    global click
    if click == True:
        click = False
    elif click == False:
        click = True

run = True
while run:
    clock.tick(100)
    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            run = False

    win.fill((45, 167, 235))

    left, middle, right = pygame.mouse.get_pressed()

    if left:
        pos = pygame.mouse.get_pos()

        for i in buttons:
            x = i.clicked(pos)

            if x:

                if click and i.text == "":
                    i.text = "O"

                elif i.text == "" and click == False:
                    i.text = "X"

                click = not click

    for i in buttons:
        i.draw(win)

    messagebox(anyone_won())
    pygame.display.flip()


import pytest
from mixer.backend.django import mixer
from apps.edemocracia.models import EdemocraciaGA
from apps.edemocracia.tasks import (get_ga_edemocracia_daily,
                                    get_ga_edemocracia_monthly,
                                    get_ga_edemocracia_yearly,)
from django.db import IntegrityError

class TestGAEdemocracia:

    @pytest.mark.django_db
    def test_edemocracia_ga_create(self):
        mixer.blend(EdemocraciaGA)
        assert EdemocraciaGA.objects.count() == 1

    @pytest.mark.django_db
    def test_edemocracia_ga_integrity_error(self):
        content = mixer.blend(EdemocraciaGA)
        with pytest.raises(IntegrityError) as excinfo:
            mixer.blend(EdemocraciaGA,
                        period=content.period,
                        start_date=content.start_date)
        assert 'duplicate key value violates unique constraint' in str(
            excinfo.value)

    @pytest.mark.django_db
    def test_monthly_get_ga_data(self):
        json_data = {"date": "00000000", "users": 10, "newUsers": 10,
                     "sessions": 10, "pageViews": 10}

        mixer.cycle(5).blend(EdemocraciaGA, period='daily', data=json_data,
                             start_date=mixer.sequence('2020-10-1{0}'),
                             end_date=mixer.sequence('2020-10-1{0}'))

        get_ga_edemocracia_monthly.apply(args=(['2020-10-01']))

        monthly_data = EdemocraciaGA.objects.filter(period='monthly').first()

        assert monthly_data.data['users'] == 50
        assert monthly_data.data['newUsers'] == 50
        assert monthly_data.data['sessions'] == 50
        assert monthly_data.data['pageViews'] == 50

    @pytest.mark.django_db
    def test_yearly_get_ga_data(self):
        json_data = {"users": 10, "newUsers": 10,
                     "sessions": 10, "pageViews": 10}
        start_dates = ['2019-01-01', '2019-02-01', '2019-03-01']
        end_dates = ['2019-01-31', '2019-02-28', '2019-03-31']
        for i in range(3):
            mixer.blend(EdemocraciaGA, period='monthly', data=json_data,
                        start_date=start_dates[i], end_date=end_dates[i])

        get_ga_edemocracia_yearly.apply(args=(['2019-01-01']))

        monthly_data = EdemocraciaGA.objects.filter(period='yearly').first()

        assert monthly_data.data['users'] == 30
        assert monthly_data.data['newUsers'] == 30
        assert monthly_data.data['sessions'] == 30
        assert monthly_data.data['pageViews'] == 30

    @pytest.mark.django_db
    def test_get_ga_edemocracia_daily(self, mocker):
        ga_data = ['20201208', '647', '446', '830', '1692']
        mocker.patch(
            'apps.edemocracia.tasks.get_analytics_data',
            return_value=[ga_data])
        get_ga_edemocracia_daily.apply()
        data = {
            "date": ga_data[0],
            "users": ga_data[1],
            "newUsers": ga_data[2],
            "sessions": ga_data[3],
            "pageViews": ga_data[4],
        }

        adiencias_ga = EdemocraciaGA.objects.first()

        assert EdemocraciaGA.objects.count() > 0
        assert adiencias_ga.data == data


from block import Block
from hashlib import sha256
from collections import deque

class Blockchain():
    # Set the parameters for the blockchain
    def __init__(self, block_size, genesis_block_secret):
        self.block_size         = block_size
        self.genesis_block_hash = sha256(genesis_block_secret.encode('utf-8')).hexdigest()

        # Create the blockchain and the first block on it
        self.blockchain = deque()
        self.blockchain.append(Block(0, self.block_size, self.genesis_block_hash))

    # Function to return the starting hash for the blockchain
    def get_genesis_block_hash(self):
        return self.genesis_block_hash

    # Function to create a vote transaction on the blockchain
    # We first try to perform the transaction on the current block,
    # if it fails being full, we create a new block on the blockchain append
    # add the new transaction to the newly created block.
    def new_vote(self, vote_for):
        # We will try to add the new transaction on the last block on the blockchain
        # If the current_block is already full, then we create a new block and add a transaction to it
        current_block = self.blockchain[-1]
        if len(current_block.block) >= self.block_size:
            self.blockchain.append(Block(len(self.blockchain), self.block_size, current_block.get_ending_hash()))
            current_block = self.blockchain[-1]

        # We are now sure that the current_block does have space for a new transaction
        transaction_status = current_block.new_vote(vote_for)

    # Function to summarise the blockchain
    def summary(self, only_hashes = True):
        for block in self.blockchain:
            print("\nBlock: " + str(block.block_id))
            t = 0
            for transaction in block.block:
                print("   " + str(t) + ": " + transaction['hash'])
                if not only_hashes:
                    print("      vote_for: " + str(transaction['vote_for']) + "   timestamp: " + str(transaction['timestamp']))
                t += 1


import pytest

from demo_app import create_app
from demo_app import db as _db

from demo_app.blog.models import Author, Category, Entry

@pytest.fixture(scope='session')
def app():
    app = create_app('testing')
    app_context = app.app_context()
    app_context.push()

    yield app

    app_context.pop()

@pytest.fixture(scope='session')
def app_client(app):
    client = app.test_client()

    return client

@pytest.fixture(scope='module')
def db():
    _db.create_all()

    yield _db

    _db.drop_all()

@pytest.fixture(scope='function')
def session(db):
    session = db.create_scoped_session()

    db.session = session

    yield  session

    session.remove()

@pytest.fixture()
def create_authors(session):
    mike = Author(name='Mike', description="Hi, I'm Mike Doe", email='mike@example.com')
    jane = Author(name='Jane', description="Hi, I'm Jane Doe", email='jane@example.com')
    session.add_all([mike, jane])
    session.commit()

@pytest.fixture()
def create_categories(session):
    python = Category(name='Python')
    javascript = Category(name='Javascript')
    session.add_all([python, javascript])
    session.commit()

@pytest.fixture()
def create_entries(session, create_authors, create_categories):
    mike = Author.query.filter_by(name='Mike').first()

    javascript = Category.query.filter_by(name='Javascript').first()
    python = Category.query.filter_by(name='Python').first()

    entry1 = Entry(title='Hello World', body='This is my first entry', \
                   author=mike)
    entry1.en_ca.append(javascript)
    entry1.en_ca.append(python)
    session.add(entry1)
    session.commit()


from datetime import datetime
from typing import Any, Dict, List

import jsonlines
from tinydb import TinyDB, where

from higgins import const

class DateTimeSerializer():
    OBJ_CLASS = datetime  # The class this serializer handles

    def encode(self, obj):
        return obj.strftime('%Y-%m-%dT%H:%M:%S')

    def decode(self, s):
        return datetime.strptime(s, '%Y-%m-%dT%H:%M:%S')

def load_database(db_path: str = const.TINY_DB_PATH) -> TinyDB:
    return TinyDB(db_path)

def truncate(table_name: str, db: TinyDB) -> None:
    table = db.table(table_name)
    table.truncate()

def insert(table_name: str, records: List[Dict], db: TinyDB) -> None:
    table = db.table(table_name)
    table.insert_multiple(records)

def query(table_name: str, field_name: str, field_value: Any, db: TinyDB) -> List[Dict]:
    table = db.table(table_name)
    records = table.search(where(field_name) == field_value)
    return records

def export_openai_jsonl(table_name: str, field_name: str, db: TinyDB, export_path: str):
    # Export in the openai format needed for search: https://beta.openai.com/docs/guides/search
    table = db.table(table_name)
    with jsonlines.open(export_path, 'w') as writer:
        for record in table:
            writer.write({"text": record[field_name], "metadata": ""})

def load_jsonl(jsonl_path: str, table_name: str, db: TinyDB):
    table = db.table(table_name)
    with jsonlines.open(jsonl_path) as reader:
        for record in reader:
            table.insert(record)

if __name__ == "__main__":
    db = load_database()
    print(db)
    table = db.table("episodes")
    print(table)
    chat_text = 'Brendan: Hello. Higgins: How can I help you?'
    insert(
        table_name="episodes",
        records=[
            {'context': {'active_window': 'Google Chrome', 'running_applications': []}, 'chat_text': chat_text, 'start_time': '2021-09-03T11:54:54'},
            {'context': {'active_window': 'App Store', 'running_applications': []}, 'chat_text': chat_text, 'start_time': '2021-09-03T11:54:54'}
        ],
        db=db
    )
    print(table.all())
    rows = query(table_name="episodes", field_name="chat_text", field_value=chat_text, db=db)
    print(rows)
    export_path = "data/episode_openai.jsonl"
    export_openai_jsonl(
        table_name="episodes",
        field_name="chat_text",
        db=db,
        export_path=export_path
    )

    from higgins.utils import jsonl_utils

    records = jsonl_utils.open_jsonl(export_path)
    print(records)

    truncate("episodes", db)


from tensorflow.keras.preprocessing import image as imageprep
import os
import numpy as np
from PIL import Image
import json
import requests
from io import BytesIO

def image_to_np_array(img_path, image_size):
    img = imageprep.load_img(img_path, target_size=(image_size, image_size))
    img = imageprep.img_to_array(img)
    return img

def to_np_array(img, image_size):
    img = img.resize((image_size, image_size))
    img = imageprep.img_to_array(img)
    if (img.shape[2] == 4):
        img = img[..., :3]
    return img

def file_to_np_array(file, image_size):
    img = Image.open(file)
    img = img.resize((image_size, image_size))
    img = imageprep.img_to_array(img)
    if (img.shape[2] == 4):
        img = img[..., :3]
    return img

def url_to_np_array(url, image_size):
    if url.endswith(('.png', '.jpg', '.jpeg')):
        response = requests.get(url)
        img = file_to_np_array(BytesIO(response.content), image_size)
        return img
    else:
        return None

def mkdir(dir_path):
    os.makedirs(dir_path, exist_ok=True)

def image_array_from_dir(dir_path, image_size, valid_file_types):
    image_paths = os.listdir(dir_path)
    image_paths = [os.path.join(dir_path, file_) for file_ in image_paths if file_.split(
        ".")[1] in valid_file_types]

    image_links = [file_.split("build/")[1] for file_ in image_paths]
    image_holder = []
    for img_path in image_paths:
        img_path = os.path.join(dir_path, img_path)
        image_holder.append(image_to_np_array(img_path, image_size))
    return np.asarray(image_holder), image_links

def load_json_file(file_path):
    with open(file_path, 'r') as f:
        data = json.load(f)
        return data

def save_json_file(file_path, data):
    with open(file_path, 'w') as f:
        json.dump(data, f)


from .code_ast import ASTFile
from .goto import Goto
from .util import bf_move

class CodeLinker:
    def __init__(self, code: ASTFile):
        self.code: ASTFile = code

    def process(self) -> str:
        code, declarations = self.code.process()
        pos = 0
        data = ""
        for i in code:
            if isinstance(i, str):
                data += i
                continue
            if isinstance(i, Goto):
                var = i.var.get_var(declarations).pos
                data += bf_move(var - pos)
                pos = var
                continue

            raise Exception

        return data


from aljoadmin.models import Comment
from django import forms

class CommentForm(forms.ModelForm):
	content = forms.CharField(
		widget=forms.Textarea(attrs={'style':'width:100%; height:80px;'}),
		label=''
	)
	class Meta:
		model = Comment
		fields = ('content',)


#!/usr/bin/env python3

import requests
import json
import sys
from collections import OrderedDict

def get_versions():
    url = 'https://api.github.com/repos/jenkinsci/swamp-plugin/releases'
    versions = set()
    response = requests.get(url)
    if response.status_code == 200:
        response = response.json()

        for rp in response:
            if 'tag_name' in rp.keys() and rp['tag_name'].startswith('swamp'):
                versions.add(rp['tag_name'].partition('swamp-')[-1])

    return versions

def get_stats():

    versions = get_versions()

    if isinstance(versions, str):
        versions = versions.split()

    stats = OrderedDict()

    for version in versions:
        data = {"type": "file",
                "repoKey": "releases",
                "path": "org/continuousassurance/swamp/jenkins/swamp/{version}/swamp-{version}.hpi".format(version=version)}

        response = requests.post('https://repo.jenkins-ci.org/ui/artifactgeneral', json=data)

        if response.status_code == 200:
            info = json.loads(response.text)
            stats['swamp-jenkins-plugin-{version}'.format(version=version)] = info['info']['downloaded']
        else:
            print(response, file=sys.stderr)

    return stats

if __name__ == '__main__':
    print(get_stats())


# -*- coding: utf-8 -*-
"""
Created on Thu Jan 27 15:26:23 2022

@author: lidon
"""

# -*- coding: utf-8 -*-
"""
Created on Fri Aug 13 14:57:16 2021

@author: a
"""

import numpy as np
import scipy.stats
import math

# Markov chain class
class Markov:
    # state: states of a Markov Chain
    # transition: transition matrix
    # pi: original distribution
    def __init__(self,state,transition,pi=None):
        self.state=state
        self.transition=transition
        if pi:
            self.pi=pi
        # if pi not specified, start from a uniform distribution
        else:
            self.pi=np.array([1 for i in range(0,len(state))])
            self.pi=self.pi/len(self.state)

    # start: the state that starts
    # length: number of the path length
    def sample(self,length):
        start=np.random.choice(self.state,1,p=self.pi)[0]
        path=[start]
        for i in range(0,length-1):
            index=np.where(self.state==start)[0][0]
            start=np.random.choice(self.state,1,p=self.transition[index,:])[0]
            path.append(start)
        path=np.array(path)
        return path

# hidden markov model class
# this class is capable of generating simulated paths with missing observations
class HMM(Markov):
    # h_state, o_state: a list of hidden state and observable state
    # trans_prob, obs_prob: transition matrix
    # obs_prob: matrix that transform hidden state to obs state
    # pi: initial distribution
    def __init__(self,h_state,o_state,trans_prob,obs_prob,pi):
        self.h_state=h_state
        self.state=h_state
        self.o_state=o_state
        self.transition=trans_prob
        self.obs_prob=obs_prob
        self.pi=pi

    # sample the observable path
    def sample_obs(self,hidden_path):
        obs=[]
        for i in range(0,len(hidden_path)):
            index=np.where(self.state==hidden_path[i])[0][0]
            new_obs=np.random.choice(self.o_state,1,p=self.obs_prob[index,:])[0]
            obs.append(new_obs)
        obs=np.array(obs)
        return obs

    # return the index of a hidden variable in the hidden_state list
    def hidden_index(self, h_var):
        index=np.where(self.h_state==h_var)[0][0]
        return index

    # return the index of an observed variable in the observe state list
    def obs_index(self,o_var):
        index=np.where(self.o_state==o_var)[0][0]
        return index

    # generate size sequences, each of length length
    # return observation path and hidden path
    def generate_seq(self,size,length):
        hidden_data=[]
        observe_data=[]
        for i in range(0,size):
            h=self.sample(length)
            o=self.sample_obs(h)
            hidden_data.append(h)
            observe_data.append(o)
        hidden_data=np.array(hidden_data)
        observe_data=np.array(observe_data)
        return hidden_data,observe_data

    # generate a sequences with missing observations
    def generate_partial_seq(self,size,length,p=0.3):
        hidden_data=[]
        observe_data=[]
        for i in range(0,size):
            h=self.sample(length)
            o=self.sample_obs(h)
            hidden_data.append(h)
            observe_data.append(o)

        for i in range(0,len(observe_data)):
            for j in range(0,len(observe_data[0])):
                if np.random.binomial(1,p):
                    observe_data[i][j]=None
            # if a whole sequence is missing, delete it
            if sum(observe_data[i]==None)==len(observe_data[i]):
                observe_data[i]=observe_data[i-1]

        hidden_data=np.array(hidden_data)
        observe_data=np.array(observe_data)
        return hidden_data,observe_data

'''
# HMM construction
transition=np.array(
        [[0.6,0.2,0.1,0.05,0.05],[0.05,0.6,0.2,0.1,0.05],[0.05,0.05,0.6,0.2,0.1],[0.05,0.05,0.1,0.6,0.2],
         [0.05,0.05,0.1,0.2,0.6]]
        )

state=np.array(['A','B','C','D','E'])
hidden_state=state
obs_state=np.array(['Blue','Red','Green','Purple','Grey'])

obs_prob=np.array([[0.5,0.3,0.05,0.05,0.1],[0.1,0.5,0.3,0.05,0.05],[0.05,0.1,0.5,0.3,0.05],
                   [0.05,0.05,0.1,0.5,0.3],[0.3,0.05,0.05,0.1,0.5]
        ])

pi=[0.5,0.2,0.2,0.1,0]

MC=HMM(hidden_state,obs_state,transition,obs_prob,pi)
'''


import pygame
import pytest

from scoreboard import Scoreboard
from settings import Settings
from game_stats import GameStats

@pytest.fixture
def scoreboard():
    """ 创建一个新的 Scoreboard 实例 """
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
    stats = GameStats(ai_settings)
    return Scoreboard(ai_settings, screen, stats)

def test_prep_score(scoreboard):
    """ 测试 prep_score 方法 """
    scoreboard.prep_score()
    assert isinstance(scoreboard.score_iamge, pygame.Surface)
    #assert scoreboard.score_iamge.get_rect().top == 12

def test_prep_high_score(scoreboard):
    """ 测试 prep_high_score 方法 """
    scoreboard.prep_high_score()
    assert isinstance(scoreboard.high_score_iamge, pygame.Surface)
    #assert scoreboard.high_score_iamge.get_rect().centerx == scoreboard.screen_rect.centerx

def test_prep_level(scoreboard):
    """ 测试 prep_level 方法 """
    scoreboard.prep_level()
    assert isinstance(scoreboard.level_image, pygame.Surface)

def test_prep_ships(scoreboard):
    """ 测试 prep_ships 方法 """
    scoreboard.prep_ships()
    assert len(scoreboard.ships.sprites()) == scoreboard.stats.ships_left

def test_show_score(scoreboard):
    """ 测试 show_score 方法 """
    scoreboard.show_score()
    assert isinstance(scoreboard.score_iamge, pygame.Surface)
    assert isinstance(scoreboard.high_score_iamge, pygame.Surface)
    assert isinstance(scoreboard.level_image, pygame.Surface)
    assert isinstance(scoreboard.ships, pygame.sprite.Group)
    #assert scoreboard.score_iamge.get_rect().top == 12
    #assert scoreboard.high_score_iamge.get_rect().centerx == scoreboard.screen_rect.centerx
    #assert scoreboard.level_image.get_rect().top == scoreboard.score_rect.bottom + 10


import logging
import collections
import html

import gw2buildutil

from . import util as gw2util

logger = logging.getLogger(__name__)
PAGE_ID = 'build'
PAGE_ID_PREFIX = 'builds/'
PAGE_TITLE_PREFIX = 'Guild Wars 2 build: '

def build (gw2site):
    textbody_renderer = gw2buildutil.textbody.Renderer(
        gw2buildutil.textbody.RenderFormat.RST_HTML, {'heading level': 3})

    with gw2buildutil.api.storage.FileStorage() as api_storage:
        for build in gw2site.builds.values():
            logger.info(f'render {build.metadata}')
            dest_page_id = PAGE_ID_PREFIX + gw2util.get_build_id(build)
            page_title = PAGE_TITLE_PREFIX + str(build.metadata)

            texts = {}
            if build.intro.description is not None:
                texts['desc'] = textbody_renderer.render(
                    build.intro.description, build.metadata, api_storage)
            if build.notes is not None:
                texts['notes'] = textbody_renderer.render(
                    build.notes, build.metadata, api_storage)
            if build.usage is not None:
                texts['usage'] = textbody_renderer.render(
                    build.usage, build.metadata, api_storage)

            gw2site.render_page_template(PAGE_ID, page_title, {
                'build': build,
                'texts': texts,
            }, dest_page_id=dest_page_id)


import qrcode

data = "Winson is the goat no cappa"
img = qrcode.make(data)

qr = qrcode.QRCode(version = 1, box_size = 10, border = 5)
qr.add_data(data)
qr.make(fit=True)
img = qr.make_image(fill_color = 'red', back_color = 'white')
img.save('C:/Users/wilko/Desktop/python12projects/qrcode/qrcode.png')


import logging

from django.core.exceptions import ValidationError
from django import forms
from django.utils.translation import gettext as _

from custody.models import MultiSigAddress
from coldstoragetransfers.helpers.btc import BTCHelper

class MultiSigAddressForm(forms.ModelForm):
    class Meta:
        model = MultiSigAddress
        exclude = ['address', 'redeem_script']

    def clean(self):
        #{'currency': <Currency: ETH>, 'user_addresses': <QuerySet [<UserAddress: BTC_tipu>, <UserAddress: ETH_tipu>]>, 'minimum_signatures': 2}
        this_currency = self.cleaned_data['currency'].symbol
        errors = {}

        for a in self.cleaned_data['user_addresses']:
            if a.currency.symbol != this_currency:
                errors['user_addresses'] = _("Child addresses' currency must match selected currency.")

        if self.cleaned_data['minimum_signatures'] > len(self.cleaned_data['user_addresses']):
            errors['minimum_signatures'] = _("This amount can't be less than the number of child addresses.")

        if errors:
            raise ValidationError(errors)

        super().clean()

        """
        doesn't work on save_model in the MultisigAddressAdmin due to:
        <MultiSigAddress: BTC_None>" needs to have a value for field "id" before this many-to-many relationship can be used.
        because new models don't exist in the db, relationship can't be accessed. must happen here.
        """

        raw_public_keys = [ua.address for ua in self.cleaned_data['user_addresses']]

        # predictable ordering is required for multisig creation
        raw_public_keys.sort()

        #should only happen once !
        if not self.instance.pk:
            create_payload = BTCHelper().add_multisig_address(self.cleaned_data['minimum_signatures'], raw_public_keys)
            self.instance.address = create_payload['address']
            self.instance.redeem_script = create_payload['redeemScript']


import messageUtils as mu
import threading

class Listener(threading.Thread):
	def __init__(self, socketO, caller, connection=None):
		threading.Thread.__init__(self)
		self.caller = caller # Client or Server object
		self.connection = connection # Connection object or None if Client is calling this
		if self.connection==None: # caller is a Client
			self.check = self.caller
		else: # caller is a Server
			self.check = self.connection
		self.socketO = socketO # socket object
		print("[DEBUG] listener set up")

	def run(self):
		while self.check.connectionOn:
			received = self.socketO.recv(1024)
			if len(received)!=0:
				self.caller.receivedMsg(self.connection, received)
			received=""
		return

class Sender(threading.Thread):
	def __init__(self, caller):
		threading.Thread.__init__(self)
		self.caller = caller # Client or Server object
		print("[DEBUG] sender set up")

	def run(self):
		while self.caller.connectionOn:
			msg = raw_input()
			self.caller.sendMessage(msg)
		return


# -*- coding: utf-8 -*-
import os
import sys
import datetime
import struct
import wave

def argumentsparser():
    usage = "Usage: python {} inputfile.kamata_programs".format(__file__)
    arguments = sys.argv
    if len(arguments) == 1 or len(arguments) > 2:
        return usage
    arguments.pop(0)
    if not arguments[0].endswith('.kamata_programs') or arguments[0].startswith('-'):
        return usage

if __name__ == '__main__' :
    if argumentsparser() is None :
        filesize = os.path.getsize(sys.argv[0])
        readpoint = 96
        filenumber = 1
        now = datetime.datetime.now()
        dirname = "{0:%y%m%d%H%M%S}".format(now)
        fin = open(sys.argv[0], mode="rb")
        if readpoint < filesize :
            os.makedirs(dirname, exist_ok=True)

        print("inputfile size =", filesize)

        while readpoint < filesize :
            fin.seek(readpoint)
            filename = "{0:03d}".format(filenumber)
            data = struct.unpack("B", fin.read(1))
            i = 0
            while not data[0] == 0 and i < 12 :
                filename += chr(data[0])
                data = struct.unpack("B", fin.read(1))
                i += 1
            filename += ".wav"
            readpoint += 616

            fout = wave.Wave_write(dirname + "/" + filename)
            fout.setparams((
                1,                 # mono
                1,                 # 8 bits = 1 byte
                48000,             # sampling bitrate
                32,                # samples
                "NONE",            # not compressed
                "not compressed"   # not compressed
                ))

            for i in range(32):
                fin.seek(readpoint)
                valuekey = fin.read(4)
                bitvalue = round(struct.unpack('<f', valuekey)[0] * 255)
                fout.writeframesraw(struct.pack("B", bitvalue))
                print("readpoint =", readpoint, " , ", bitvalue)
                readpoint += 12
            print("-----------------")

            fout.close()
            filenumber += 1
            readpoint += 4

        fin.close()
        print(filenumber - 1, "wave files are created in the", dirname, "folder successfully.")
        print("The format is monoral, 8-bit, 48kHz and 32 samples. Files are expected to be readable for an ELZ_1 synthesizer.")

    else:
        print(argumentsparser())


import torch
from torch import optim, nn
import os
from tqdm.auto import tqdm
from model import *
from data import *
from torch.cuda.amp import autocast, GradScaler
from validate_and_test import *

def load_checkpointed_model_params(model, optimizer, resume_checkpoint):
    checkpoint = torch.load(resume_checkpoint)
    model.load_state_dict(checkpoint['model_state_dict'])
    optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
    start_epoch = checkpoint['epoch']
    # Things we are keeping track of
    epoch_numbers = checkpoint['epoch_numbers']
    training_losses = checkpoint['training_losses']
    validation_losses = checkpoint['validation_losses']
    training_accuracy = checkpoint['training_accuracy']
    validation_accuracy = checkpoint['validation_accuracy']
    print(f"Model checkpoint {resume_checkpoint} loaded! Will resume the epochs from number #{start_epoch}")
    return model, optimizer, start_epoch, epoch_numbers, training_losses, training_accuracy, validation_losses, validation_accuracy

def save_model_checkpoint(experiment, model, optimizer, params, epoch, epoch_numbers, training_losses,
                          validation_losses, training_accuracy, validation_accuracy):
    # set the model to train mode so that in case there was a validation before it doesnt impact the saved weights (as we have dropouts!)
    model.train()

    # create the directory if it doesn't exist
    model_save_directory = os.path.join(params["save_dir"], experiment)
    os.makedirs(model_save_directory, exist_ok=True)

    # Checkpoint the model at the end of each epoch
    checkpoint_path = os.path.join(params["save_dir"], experiment, f'model_epoch_{epoch + 1}.pt')
    torch.save({
        'model_state_dict': model.state_dict(),
        'optimizer_state_dict': optimizer.state_dict(),
        'epoch': epoch + 1,
        'epoch_numbers': epoch_numbers,
        'training_losses': training_losses,
        'validation_losses': validation_losses,
        'training_accuracy': training_accuracy,
        'validation_accuracy': validation_accuracy,
    }, checkpoint_path)
    print(f"Save checkpointed the model at the path {checkpoint_path}")

def train_model(model, train_loader, val_loader, num_epochs, params, experiment, epoch_saver_count=5,
                resume_checkpoint=None):
    # Device
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    torch.cuda.empty_cache()

    # Things we are keeping track of
    start_epoch = 0
    epoch_numbers = []
    training_losses = []
    validation_losses = []
    training_accuracy = []
    validation_accuracy = []

    # Adam optimizer
    optimizer = optim.Adam(model.parameters(), lr=params['learning_rate'], weight_decay=params['weight_decay'])

    # loss
    criterion = nn.CrossEntropyLoss()

    # load checkpoint
    if resume_checkpoint:
        model, optimizer, start_epoch, epoch_numbers, training_losses, training_accuracy, validation_losses, validation_accuracy = load_checkpointed_model_params(
            model,
            optimizer,
            resume_checkpoint
        )

    # Set up one-cycle learning rate scheduler
    sched = torch.optim.lr_scheduler.OneCycleLR(
        optimizer, params['learning_rate'],
        epochs=num_epochs,
        steps_per_epoch=len(train_loader)
    )

    # Custom progress bar for total epochs with color and displaying average epoch loss
    total_progress_bar = tqdm(total=num_epochs, desc=f"Total Epochs", position=0,
                              bar_format="{desc}: {percentage}% |{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_fmt}]",
                              dynamic_ncols=True, ncols=100, colour='red')

    # training loop
    for epoch in range(start_epoch, start_epoch + num_epochs):
        #set to train mode
        model.train()

        epoch_training_loss = 0.0
        train_correct_predictions = 0
        total_samples = 0

        # Custom progress bar for each epoch with color
        epoch_progress_bar = tqdm(total=len(train_loader), desc=f"Epoch {epoch + 1}/{start_epoch + num_epochs}",
                                  position=1, leave=False, dynamic_ncols=True, ncols=100, colour='green')

        for batch_idx, data in enumerate(train_loader):
            # get the data and outputs
            images, labels = data
            images = images.to(device)
            labels = labels.to(device)

            output_logits = model(images)
            loss = criterion(output_logits, labels)
            loss.backward()

            # Gradient clipping
            nn.utils.clip_grad_value_(model.parameters(), params['grad_clip'])

            optimizer.step()
            optimizer.zero_grad()

            # print(f"Curr LR -> {optimizer.param_groups[0]['lr']}")

            # scheduler update
            sched.step()

            epoch_training_loss += loss.item()

            # batch stats
            # Compute training accuracy for this batch
            output_probs = nn.Softmax(dim=1)(output_logits)
            predicted = torch.argmax(output_probs, 1)
            batch_correct_predictions = (predicted == labels).sum().item()
            batch_size = labels.size(0)

            train_correct_predictions += batch_correct_predictions
            total_samples += batch_size  # batch size basically

            # Update the epoch progress bar (overwrite in place)
            epoch_progress_bar.set_postfix({
                "loss": loss.item(),
                "batch_acc": batch_correct_predictions / batch_size
            })
            epoch_progress_bar.update(1)

        # Close the epoch progress bar
        epoch_progress_bar.close()

        # Calculate average loss for the epoch
        avg_training_loss_for_epoch = epoch_training_loss / len(train_loader)

        # Calculate training accuracy for the epoch
        avg_training_accuracy = train_correct_predictions / total_samples

        # Validation loop
        avg_val_accuracy, avg_val_loss_for_epoch = perform_validation(criterion, device, model, val_loader)

        # Store values
        training_accuracy.append(avg_training_accuracy)
        training_losses.append(avg_training_loss_for_epoch)
        validation_accuracy.append(avg_val_accuracy)
        validation_losses.append(avg_val_loss_for_epoch)
        epoch_numbers.append(epoch + 1)

        # Update the total progress bar
        total_progress_bar.set_postfix(
            {
                "loss": avg_training_loss_for_epoch,
                "train_acc": avg_training_accuracy,
                "val_loss": avg_val_loss_for_epoch,
                "val_acc": avg_val_accuracy,
            }

        )

        # Close the tqdm bat
        total_progress_bar.update(1)

        # Print state
        print(
            f'Epoch {epoch + 1}: train_loss: {avg_training_loss_for_epoch} | train_accuracy: {avg_training_accuracy} | val_loss: {avg_val_loss_for_epoch} | val_accuracy: {avg_val_accuracy} '
        )

        # Save model checkpoint periodically
        need_to_save_model_checkpoint = (epoch + 1) % epoch_saver_count == 0
        if need_to_save_model_checkpoint:
            print(f"Going to save model @ Epoch:{epoch + 1}")
            save_model_checkpoint(
                experiment,
                model,
                optimizer,
                params,
                epoch,
                epoch_numbers,
                training_losses,
                validation_losses,
                training_accuracy,
                validation_accuracy
            )

    # Close the total progress bar
    total_progress_bar.close()

    # Return things needed for plotting
    return epoch_numbers, training_losses, training_accuracy, validation_losses, validation_accuracy

# %%
# if __name__ == '__main__':
#     params = {
#         'batch_size': 32,
#         'learning_rate': 0.0045,
#         'save_dir': 'model_ckpts'
#     }
#     train_data_loader = create_train_data_loader(32)
#     test_data_loader, validation_data_loader = create_test_and_validation_data_loader(32)
#
#     full_experiment = "Full Data"
#     # Check if GPU is available, otherwise use CPU
#     device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
#     full_cifar_model = CIFARClassifier()
#     full_cifar_model.to(device)
#
#     train_model(
#         full_cifar_model,
#         train_data_loader,
#         validation_data_loader,
#         2,
#         params,
#         full_experiment,
#         epoch_saver_count=1,
#         resume_checkpoint=None
#     )

Assignment 2 (Autoencoders & CNNs)/Question-5_CIFAR10/train.py


from django.shortcuts import get_object_or_404
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.views import View

from main.models import *
from main.forms import *

from cart.forms import CartAddProductForm

def base_context(request):
    context = dict()
    context['user'] = request.user
    context["site_name"] = "Sushiman"  # Строка перед | в title страницы
    context["page_name"] = "Главная"  # Строка после |
    context["page_header"] = ""  # Название страницы в display-3 стиле
    return context

# Начальная страница
def index(request):
    c = base_context(request)
    c["page_header"] = "Меню"
    c["categories"] = Category.objects.all()
    return render(request, 'pages/index.html', c)

# Вьюха для просмотра товаров по категориям меню с фильтрацией
def view_category(request, category_slug):
    c = base_context(request)
    category = get_object_or_404(Category, slug=category_slug)

    c["page_name"] = category.name
    c["page_header"] = category.name
    c['products'] = Product.objects.filter(category=category)
    c['form'] = CartAddProductForm()

    return render(request, 'pages/category.html', c)

def adresses(request):
    c = base_context(request)
    c["page_header"] = "Адреса"
    c["page_name"] = "Адреса"
    return render(request, 'pages/adresses.html', c)


# %%
from datasets import load_dataset
from transformers import AutoTokenizer, BertForSequenceClassification, TrainingArguments, Trainer
from transformers import pipeline

# %%
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
model = BertForSequenceClassification.from_pretrained("distilbert-base-uncased").cuda()

# %%
dataset = load_dataset("NgThVinh/dsc_model")
dataset.with_format("torch")
dataset

# %%
# dataset.push_to_hub('NgThVinh/dsc_model')

# %%
dataset['train'][:5]

# %%
dataset['train'].features

# %%
dataset['train'][0]

# %%
# max_length = 0
# for sen in dataset['train']['document']:
#     length = len(tokenizer.tokenize(sen))
#     max_length = max(length, max_length)
# max_length

# %%
def create_input_sentence(document, claim):
    return f"Given claim-document pair where claim: \"{claim}\", document: \"{document}\". Classify the claim to which class it belongs. If the claim contains information about the document, its label will be SUPPORTED, otherwise, its label will be REFUTED. In case the information of the claim cannot be verified based on the given document, its label will be NEI"

# %%
print(create_input_sentence(dataset['train'][100]['document'], dataset['train'][100]['claim']))

# %%
def preprocess_function(examples):
    inputs = tokenizer.encode_plus(
        create_input_sentence(examples["claim"], examples["document"]),
        truncation=True,
        padding="max_length",
        return_tensors='pt'
    )
    label = tokenizer.encode_plus(
        examples["label"],
        truncation=True,
        padding="max_length",
        return_tensors='pt'
    )

    examples["input_ids"] = inputs['input_ids'][0]
    examples["attention_mask"] = inputs['attention_mask'][0]

    examples['labels'] = label['input_ids'][0]

    return examples

# %%
print(preprocess_function(dataset['train'][100]))

# %%
train_dataset = dataset["train"].map(preprocess_function, remove_columns=dataset["train"].column_names)
test_dataset = dataset["test"].map(preprocess_function, remove_columns=dataset["test"].column_names)

# %%
# from transformers import DefaultDataCollator

# data_collator = DefaultDataCollator()

# %%
training_args = TrainingArguments(
    output_dir="dsc_model",
    evaluation_strategy="epoch",
    learning_rate=2e-5,
    # per_device_train_batch_size=16,
    # per_device_eval_batch_size=16,
    num_train_epochs=3,
    weight_decay=0.01,
    push_to_hub=True,
)

trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=train_dataset,
    eval_dataset=test_dataset,
    tokenizer=tokenizer,
    # data_collator=data_collator,
)

trainer.train()


import argparse
import pprint
import sys

from designspaceProblems import DesignSpaceChecker

def main(args=None):
    parser = argparse.ArgumentParser(
        description='Check designspace data.')
    parser.add_argument(
        'input_ds',
        metavar='PATH',
        help='path to designspace file',
        type=argparse.FileType())
    options = parser.parse_args(args)

    dc = DesignSpaceChecker(options.input_ds.name)
    dc.checkEverything()

    pprint.pprint(dc.problems)

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


"""
Graph implementation
    class GraphMatrix - adjacency matrix
"""
from collections import deque

class GraphMatrix:
    """
    Graph implementation using an adjacency matrix
    [
     [ ]
     [ ]
     [ ]
    ]
    """

    def __init__(self, size: int):
        """ Inits Graph class with optional graph_matrix """
        self.graph_matrix = []
        for _ in range(size):
            self.graph_matrix.append([0 for _ in range(size)])
        self.size = size

    def add_edge(self, edge: list):
        """ add an edge between two vertices """
        if not self.has_edge(edge):
            v1, v2 = edge
            self.graph_matrix[v1][v2] = 1
            self.graph_matrix[v2][v1] = 1
            return True
        return False

    def delete_edge(self, edge: list):
        """ removes the edge between two given vertices - if it exists """
        if self.has_edge(edge):
            v1, v2 = edge
            self.graph_matrix[v1][v2] = 0
            self.graph_matrix[v2][v1] = 0
            return True
        return False

    def has_edge(self, vertices: list):
        """ checks if there's an edge between two given vertices """
        if len(vertices) == 2:
            v1, v2 = vertices
            if self.graph_matrix[v1][v2] and self.graph_matrix[v2][v1]:
                return True
        return False

    def bfs_traversal(self, node):
        """ traverses graph level-order -  O(v^2) """
        self._bfs(node)

    def _bfs(self, node):
        """ helper method to BFS graph """
        visited = [node]
        queue = deque()
        queue.append(node)

        while queue:
            node = queue.popleft()
            print(node)

            for i in range(len(self.graph_matrix)):
                temp = self.graph_matrix[node][i]
                if temp != 0 and i not in visited:
                    queue.append(i)
                    visited.append(i)

    def dfs_traversal(self, node):
        """ traverses graph in a dfs style - O(v^2) """
        visited = set()
        self._dfs(visited, node)

    def _dfs(self, visited, node):
        """ helper method to recursively DFS graph """
        if node not in visited:
            print(node)
            visited.add(node)

            for i in range(len(self.graph_matrix[node])):
                if self.graph_matrix[node][i] != 0:
                    self._dfs(visited,i)

if __name__ == '__main__':
    # 0 -- 1
    # |
    # |
    # 2 -- 3 -- 4
    # |  /
    # | /
    # 5
    g = GraphMatrix(6)

    # add
    print('Adding edges...')
    g.add_edge([0,1])
    g.add_edge([0,2])
    g.add_edge([2,3])
    g.add_edge([2,5])
    g.add_edge([3,4])
    g.add_edge([3,5])
    # show
    print('***Has edge?***')
    print( g.has_edge([5,3]) )

    #delete edge
    print('***Delete edge: 5,3:***')
    print( g.delete_edge([5,3]) )

    # # show
    print('***Has edge?***')
    print( g.has_edge([5,3]) )

    # # traversal
    print('*** DFS ***')
    g.dfs_traversal(0)

    print('*** BFS ***')
    g.bfs_traversal(0)


import apache_beam as beam

with beam.Pipeline() as pipeline:
  batches_with_keys = (
      pipeline
      | 'Create produce' >> beam.Create([
          ('spring', '🍓'),
          ('spring', '🥕'),
          ('spring', '🍆'),
          ('spring', '🍅'),
          ('summer', '🥕'),
          ('summer', '🍅'),
          ('summer', '🌽'),
          ('fall', '🥕'),
          ('fall', '🍅'),
          ('winter', '🍆'),
      ])
      | 'Group into batches' >> beam.GroupIntoBatches(3)
      | beam.Map(print))


"""Initializes and checks the environment needed to run pytype."""

import logging
import sys
from typing import List

from pytype.imports import typeshed
from pytype.platform_utils import path_utils
from pytype.tools import runner

def check_pytype_or_die():
  if not runner.can_run("pytype", "-h"):
    logging.critical(
        "Cannot run pytype. Check that it is installed and in your path")
    sys.exit(1)

def check_python_version(exe: List[str], required):
  """Check if exe is a python executable with the required version."""
  try:
    # python --version outputs to stderr for earlier versions
    _, out, err = runner.BinaryRun(exe + ["--version"]).communicate()  # pylint: disable=unpacking-non-sequence
    version = out or err
    version = version.decode("utf-8")
    if version.startswith(f"Python {required}"):
      return True, None
    else:
      return False, version.rstrip()
  except OSError:
    return False, None

def check_python_exe_or_die(required) -> List[str]:
  """Check if a python executable with the required version is in path."""
  error = []
  if sys.platform == "win32":
    possible_exes = (["py", f"-{required}"], ["py3"], ["py"])
  else:
    possible_exes = ([f"python{required}"], ["python3"], ["python"])
  for exe in possible_exes:
    valid, out = check_python_version(exe, required)
    if valid:
      return exe
    elif out:
      error.append(out)
  logging.critical(
      "Could not find a valid python%s interpreter in path (found %s)",
      required, ", ".join(sorted(set(error))))
  sys.exit(1)

def initialize_typeshed_or_die():
  """Initialize a Typeshed object or die.

  Returns:
    An instance of Typeshed()
  """
  try:
    return typeshed.Typeshed()
  except OSError as e:
    logging.critical(str(e))
    sys.exit(1)

def compute_pythonpath(filenames):
  """Compute a list of dependency paths."""
  paths = set()
  for f in filenames:
    containing_dir = path_utils.dirname(f)
    if path_utils.exists(path_utils.join(containing_dir, "__init__.py")):
      # If the file's containing directory has an __init__.py, we assume that
      # the file is in a (sub)package. Add the containing directory of the
      # top-level package so that 'from package import module' works.
      package_parent = path_utils.dirname(containing_dir)
      while path_utils.exists(path_utils.join(package_parent, "__init__.py")):
        package_parent = path_utils.dirname(package_parent)
      p = package_parent
    else:
      # Otherwise, the file is a standalone script. Add its containing directory
      # to the path so that 'import module_in_same_directory' works.
      p = containing_dir
    paths.add(p)
  # Reverse sorting the paths guarantees that child directories always appear
  # before their parents. To see why this property is necessary, consider the
  # following file structure:
  #   foo/
  #     bar1.py
  #     bar2.py  # import bar1
  #     baz/
  #       qux1.py
  #       qux2.py  # import qux1
  # If the path were [foo/, foo/baz/], then foo/ would be used as the base of
  # the module names in both directories, yielding bar1 (good) and baz.qux1
  # (bad). With the order reversed, we get bar1 and qux1 as expected.
  return sorted(paths, reverse=True)


import numpy as np
import cv2
import NeuralNetwork
import json
import os
import matplotlib.pyplot as plt

#defining the initial parameters and the learning rate
batch_size = 10
nn_hdim = 2048
learning_rate = 0.1
f1 = "relu"
f2 = "sigmoid"
threshold = 0.0001
sd_init = 0.01
sd_init_w2 = sd_init

def make_json(W1, W2, b1, b2, id1, id2, activation1, activation2, nn_h_dim, path_to_save):
    """
    make json file with trained parameters.
    W1: numpy arrays of shape (1024, nn_h_dim)
    W2: numpy arrays of shape (nn_h_dim, 1)
    b1: numpy arrays of shape (1, nn_h_dim)
    b2: numpy arrays of shape (1, 1)
    nn_hdim - 2048
    id1: id1 - str '204214928'
    id2: id2 - str '308407907'
    activation1:  'ReLU'
    activation2:  'sigmoid'
    """
    trained_dict = {'weights': (W1.tolist(), W2.tolist()),
                    'biases': (b1.tolist(), b2.tolist()),
                    'nn_hdim': nn_h_dim,
                    'activation_1': activation1,
                    'activation_2': activation2,
                    'IDs': (id1, id2)}
    file_path = os.path.join(path_to_save, 'trained_dict_{}_{}'.format(
        trained_dict.get('IDs')[0], trained_dict.get('IDs')[1])
                             )
    with open(file_path, 'w') as f:
        json.dump(trained_dict, f, indent=4)

def load_image(prefix, number, data_vec, label_vec, is_training):
    if is_training:
        path = "data\\training\\"
    else:
        path = "data\\validation\\"

    path = path + prefix + number + ".png"
    image = cv2.imread(path, flags=cv2.IMREAD_GRAYSCALE)
    data_vec.append(image.flatten() / 255.0)
    if prefix == "pos_":
        label_vec.append(1)
    else:
        label_vec.append(0)

def load_data(train_data, val_data, train_label, val_label):
    # load train data
    for i in range(256):
        load_image("neg_", str(i), train_data, train_label, True)
        load_image("pos_", str(i), train_data, train_label, True)
    for i in range(256, 334):
        load_image("neg_", str(i), val_data, val_label, False)
        load_image("pos_", str(i), val_data, val_label, False)
    return np.asarray(train_data), np.asarray(val_data), np.asarray(train_label), np.asarray(val_label),

def main():
    convergence_flag = False
    previous_loss = np.inf
    counter = 0
    accuracy_per_training_epoch = 0
    loss_per_training_epoch = 0
    train_data = []
    val_data = []
    train_label = []
    val_label = []
    epoch_training_loss = []
    epoch_validation_loss= []
    epoch_training_accuracy = []
    epoch_validation_accuracy = []
    train_data, val_data, train_label, val_label = load_data(train_data, val_data, train_label, val_label)
    my_net = NeuralNetwork.NeuralNetwork(learning_rate, f1, f2, sd_init, sd_init_w2)
    epoc = 0
    my_net.forward_pass(val_data, val_label)
    my_net.calculate_accuracy(val_label)
    print("Inintial validation loss: ", my_net.loss, "Inintial accuracy: ", my_net.accuracy)
    while not convergence_flag:
        batch_count = 0
        shuffler = np.random.permutation(len(train_label))
        train_label = train_label[shuffler]
        train_data = train_data[shuffler]
        if (not epoc % 10) and (epoc != 0):
            my_net.learning_rate = my_net.learning_rate / 2
        for i in range(0, len(train_label), batch_size):
            batch = train_data[i:batch_size + i, :]
            batch_labels = train_label[i:batch_size + i]
            my_net.forward_pass(batch, batch_labels)
            my_net.calculate_accuracy(batch_labels)
            accuracy_per_training_epoch += my_net.accuracy
            loss_per_training_epoch += my_net.loss
            # print("epoc:", epoc, "batch:", batch_count, "loss:", my_net.loss, "accuracy:",
            #         my_net.accuracy, "prediction:", my_net.a2, np.round(my_net.a2).squeeze(), "real labels:", batch_labels)
            my_net.backward_pass(batch_labels)
            my_net.compute_gradient(batch)
            batch_count += 1
        accuracy_per_training_epoch = accuracy_per_training_epoch/(len(train_label)/batch_size)
        loss_per_training_epoch = loss_per_training_epoch/(len(train_label)/batch_size)
        epoch_training_accuracy.append(accuracy_per_training_epoch)
        epoch_training_loss.append(loss_per_training_epoch)
        accuracy_per_training_epoch = 0
        loss_per_training_epoch = 0
        my_net.forward_pass(val_data, val_label)
        my_net.calculate_accuracy(val_label)
        if (my_net.loss - previous_loss) <= threshold:
            counter += 1
        else:
            counter = 0
        if epoc > 100:
            convergence_flag = (counter >= 3)

        print("Validation loss: ", my_net.loss, "Accuracy:", my_net.accuracy, "learning rate:", my_net.learning_rate)
        previous_loss = my_net.loss
        epoch_validation_accuracy.append(my_net.accuracy)
        epoch_validation_loss.append(my_net.loss)
        epoc += 1

## plotting section-----------------------------------------------------------------------------------------------

    trained_dict = {
        'weights': (my_net.W1, my_net.W2),
        'biases': (my_net.b1, my_net.b2),
        'nn_hdim': 2048,
        'activation_1': 'relu',
        'activation_2': 'sigmoid',
        'IDs': (204214928, 308407907)
        }
    json_path = ''
    make_json(my_net.W1,my_net.W2,my_net.b1,my_net.b2,'204214928','308407907','relu','sigmoid',nn_hdim, json_path)
    plt.subplot(2, 1, 1)
    plt.plot(range(epoc), epoch_training_loss)
    plt.plot(range(epoc), epoch_validation_loss)
    plt.scatter(epoc, epoch_training_loss[epoc-1], marker='o')
    plt.scatter(epoc, epoch_validation_loss[epoc-1], marker='o')
    x = [epoc, epoc]
    n = [round(epoch_training_loss[epoc-1], 2), round(epoch_validation_loss[epoc-1], 2)]
    for i, txt in enumerate(n):
        plt.annotate(txt, (x[i], n[i]))
    plt.legend(["training", "validation"])
    plt.title('loss and accuracy as function of epoc number')
    plt.ylabel('loss [au]')
    plt.subplot(2, 1, 2)
    plt.plot(range(epoc), epoch_training_accuracy)
    plt.plot(range(epoc), epoch_validation_accuracy)
    plt.scatter(epoc, epoch_training_accuracy[epoc-1], marker='o')
    plt.scatter(epoc, epoch_validation_accuracy[epoc-1], marker='o')
    y = [epoc, epoc]
    s = [round(epoch_training_accuracy[epoc-1], 2), round(epoch_validation_accuracy[epoc-1], 2)]
    for i, txt in enumerate(s):
        plt.annotate(txt, (y[i], s[i]))
    plt.legend(["training", "validation"])
    plt.xlabel('epoc number')
    plt.ylabel('accuracy [%]')
    plt.show()

if __name__ == "__main__":
    main()


from math import sqrt, cos, sin, pi
import numpy as np
import pyvista as pv

# Affine rotation ####
#' Matrix of the affine rotation around an axis
#' @param theta angle of rotation in radians
#' @param P1,P2 the two points defining the axis of rotation
def AffineRotationMatrix(theta, P1, P2):
    T = np.vstack(
        (
            np.hstack((np.eye(3), -P1.reshape(3,1))),
            np.array([0, 0, 0, 1])
        )
    )
    invT = np.vstack(
        (
            np.hstack((np.eye(3), P1.reshape(3,1))),
            np.array([0, 0, 0, 1])
        )
    )
    a, b, c = (P2 - P1) / np.linalg.norm(P2 - P1)
    d = sqrt(b*b + c*c)
    if d > 0:
        Rx = np.array([
            [1, 0, 0, 0],
            [0, c/d, -b/d, 0],
            [0, b/d, c/d, 0],
            [0, 0, 0, 1]
        ])
        invRx = np.array([
            [1, 0, 0, 0],
            [0, c/d, b/d, 0],
            [0, -b/d, c/d, 0],
            [0, 0, 0, 1]
        ])
    else:
        Rx = invRx = np.eye(4)
    Ry = np.array([
        [d, 0, -a, 0],
        [0, 1, 0, 0],
        [a, 0, d, 0],
        [0, 0, 0, 1]
    ])
    invRy = np.array([
        [d, 0, a, 0],
        [0, 1, 0, 0],
        [-a, 0, d, 0],
        [0, 0, 0, 1]
    ])
    Rz = np.array([
        [cos(theta), -sin(theta), 0, 0],
        [sin(theta), cos(theta), 0, 0],
        [0, 0, 1, 0],
        [0, 0, 0, 1]
    ])
    return invT @ invRx @ invRy @ Rz @ Ry @ Rx @ T

O = np.array([0.0, 0.0, 0.0])
A = np.array([0.0, 10.0, 0.0])
Rot = AffineRotationMatrix(3*pi/4, O, A)

def f(x, y, z, a, b):
    return ((
        (x * x + y * y + 1) * (a * x * x + b * y * y)
        + z * z * (b * x * x + a * y * y)
        - 2 * (a - b) * x * y * z
        - a * b * (x * x + y * y)
    ) ** 2
    - 4 * (x * x + y * y) * (a * x * x + b * y * y - x * y * z * (a - b)) ** 2)

def inversion(omega, M):
    Omega0 = np.array([omega, 0.0, 0.0])
    OmegaM = M - Omega0;
    k = np.dot(OmegaM, OmegaM)
    return Omega0 + OmegaM / k

def params(alpha, gamma, mu):
    beta = sqrt(alpha*alpha - gamma*gamma)
    theta = beta * sqrt(mu * mu - gamma*gamma)
    omega = (alpha * mu + theta) / gamma
    ratio = (
        (mu - gamma) * ((alpha - gamma) * (mu + gamma) + theta)
        / ((alpha + gamma) * (mu - gamma) + theta) / (alpha - gamma)
    )
    R = (
        1/ratio * gamma * gamma / ((alpha - gamma) * (mu - gamma) + theta)
        * (mu - gamma) / ((alpha + gamma) * (mu - gamma) + theta)
    )
    omegaT = (
        omega - (beta * beta * (omega - gamma))
        / ((alpha - gamma) * (mu + omega) - beta * beta)
        / ((alpha + gamma) * (omega - gamma) + beta * beta)
    )
    return (omega, omegaT, ratio, R)

alpha = 0.97
gamma = 0.32
mu = 0.56
omega, omegaT, ratio, R = params(alpha, gamma, mu)
OmegaT = np.array([omegaT, 0.0, 0.0])
a = ratio*ratio
b = 0.06

# generate data grid for computing the values
X, Y, Z = np.mgrid[(-1.3):1.3:350j, (-1.6):1.6:350j, (-0.6):0.6:350j]
# create a structured grid
grid = pv.StructuredGrid(X, Y, Z)
# compute and assign the values
values = f(X, Y, Z, a, b)
grid.point_data["values"] = values.ravel(order="F")
# compute the isosurface f(x, y, z) = 0
isosurf = grid.contour(isosurfaces=[0])
# convert to a PolyData mesh
mesh = isosurf.extract_geometry()
# rotate mesh
mesh.transform(Rot)
# transform
points = R * mesh.points
points = np.apply_along_axis(lambda M: inversion(omega, M + OmegaT), 1, points)
newmesh = pv.PolyData(points, mesh.faces)
newmesh["dist"] = np.linalg.norm(mesh.points, axis=1)

pltr = pv.Plotter(window_size=[512, 512])
pltr.set_focus(newmesh.center)
pltr.set_position(newmesh.center - np.array([0.0, 0.0, 7.0]))
pltr.add_background_image("SpaceBackground.png")
pltr.add_mesh(
    newmesh, smooth_shading=True, cmap="turbo", specular=25,
    show_scalar_bar=False
)
pltr.show()


#!/usr/local/bin/python3.7

#############
 # Imports #
#############

import globalvars
import modules.conf as conf
import modules.misc as misc
import modules.platform as platform
import modules.special as special
import modules.subst as subst
import configparser
import os
import shutil
import subprocess

###############
 # Functions #
###############

def print_info():
    """Print info about operating system and target triple."""
    print("\nInformation summary:\n--------------------------------------")
    print("OSNAME: " + globalvars.OSNAME)
    print("OSVERSION: " + globalvars.OSVERSION)
    print("OSRELEASE: " + globalvars.OSRELEASE)
    print("OSMAJOR: " + globalvars.OSMAJOR)
    print("OSARCH: " + globalvars.OSARCH)
    print("STDARCH: " + globalvars.STDARCH)
    print("TARGET_TRIPLE: " + globalvars.TGT_TRIPLE)
    print("--------------------------------------\n")
    print(str(len(packages_present)) + " packages present:")
    for p in packages_present:
        print(p + ' ', end='')
    print("\n" + str(len(packages_missing)) + " packages missing:")
    for p in packages_missing:
        print(p + ' ', end='')

def ensure_distfile(mode, package):
    """Ensure that the compressed or uncompressed ("mode") distfile for a package is present."""
    if mode == "compressed":
        distdir = globalvars.SUBSTITUTION_MAP['rbuild_dist_comp_dir']
        filename = misc.get_filename('distfiles', package)
        hashtype = "md5"
    elif mode == "uncompressed":
        distdir = globalvars.SUBSTITUTION_MAP['rbuild_dist_uncomp_dir']
        filename = os.path.basename(misc.get_tarball_uri(package))
        hashtype = "umd5"
    else:
        misc.die("Invalid ensure_distfile mode \"" + mode + "\"! Aborting...")
    absolute_path = distdir + '/' + filename

    if not os.path.isfile(absolute_path):
        if mode == "compressed":
            misc.fetch_file(conf.get_config_value('distfiles', package), distdir, filename)
        else:
            misc.decompress_file(globalvars.SUBSTITUTION_MAP['rbuild_dist_comp_dir'], misc.get_filename('distfiles', package), distdir)

    checksum = misc.get_distfile_checksum(hashtype, package)
    misc.verbose_output("Checksum for \"" + package + "\": Comparing for " + mode + " distfile... ")
    if checksum == None:
        misc.verbose_output("skipping (not available)\n")
    else:
        if misc.get_file_hash(absolute_path) == checksum:
            misc.verbose_output("ok (matches)\n")
        else:
            if mode == "compressed":
                misc.verbose_output("Mismatch! Fetching again...\n")
                misc.remove_file_or_dir(absolute_path)
                misc.fetch_file(conf.get_config_value('distfiles', package), globalvars.SUBSTITUTION_MAP['rbuild_dist_comp_dir'], filename)
                misc.verbose_output("Comparing checksums once more... ")
                if misc.get_file_hash(absolute_path) == checksum:
                    misc.verbose_output("ok (matches)\n")
                else:
                    misc.die("Mismatch again! Bailing out...")
            else:
                misc.verbose_output("Mismatch! Extracting again...\n")
                misc.die("Extract again!")

def ensure_extrafiles_present(package):
    """Ensure that the extra files for a package are present."""
    extradir = globalvars.SUBSTITUTION_MAP['rbuild_extra_dir'] + '/' + package
    extrafiles = conf.get_config_value('extrafiles', package).split(", ")
    md5s = None
    if package + "_md5" in conf.config['extrafiles']:
        md5s = conf.get_config_value('extrafiles', package + "_md5").split(", ")
    misc.verbose_output("Extra files: Ensuring directory \"" + extradir + "\" exists... ")
    if not os.path.isdir(extradir):
        try:
            os.makedirs(extradir)
        except OSError as e:
            misc.die("\nPatches error: Could not create directory \"" + extradir + "\"! Exiting.")
    misc.verbose_output("ok\n")

    i = 0
    for f in extrafiles:
        filename = os.path.basename(f)
        absolute_path = extradir + '/' + filename
        if not os.path.isfile(absolute_path):
            misc.fetch_file(f, extradir, filename)
        misc.verbose_output("Comparing checksums for extra file " + str(i) + "... ")
        if md5s == None:
            misc.verbose_output("skipping (not available)\n")
        else:
            if misc.get_file_hash(absolute_path) == md5s[i]:
                misc.verbose_output("ok (matches)\n")
            else:
                misc.verbose_output("Mismatch! Fetching again...\n")
                misc.remove_file_or_dir(absolute_path)
                misc.fetch_file(f, extradir, filename)
                misc.verbose_output("Comparing checksums once more... ")
                if misc.get_file_hash(absolute_path) == md5s[i]:
                    misc.verbose_output("ok (matches)\n")
                else:
                    misc.die("Mismatch again! Bailing out...")
        i = i + 1

def ensure_clean_wrkdir(package):
    """Ensure that a fresh work directory is present for the package to build."""
    wrkdir = misc.get_wrkdir(package)
    if os.path.exists(wrkdir):
        print("Old workdir found. Deleting... ", end='', flush=True)
        misc.remove_file_or_dir(wrkdir)
        print("ok")

    if package in conf.config['distfiles']:
        ensure_distfile("compressed", package)
        ensure_distfile("uncompressed", package)
        misc.extract_tarball(package)

    if package in conf.config['extrafiles']:
        if not os.path.exists(wrkdir):
            try:
                os.makedirs(wrkdir)
            except OSError as e:
                misc.die("\nFilesystem error: Could not create directory \"" + directory + "\"! Exiting.")

        if package in conf.config['extrafiles']:
            ensure_extrafiles_present(package)
            extradir = globalvars.SUBSTITUTION_MAP['rbuild_extra_dir'] + '/' + package
            extrafiles = conf.get_config_value('extrafiles', package).split(", ")
            for f in extrafiles:
                absolute_path = extradir + '/' + os.path.basename(f)
                try:
                    shutil.copy(absolute_path, wrkdir)
                except IOError as e:
                    misc.die("\nFilesystem error: Could not copy \"" + absolute_path + "\" to \"" + wrkdir + "\"! Exiting.")

def ensure_patchfiles_present(package):
    """Check if patches required to build the package are present, try to fetch them otherwise."""
    patches = conf.get_config_value('patches', package).split(", ")
    md5s = None
    if package + "_md5" in conf.config['patches']:
        md5s = conf.get_config_value('patches', package + "_md5").split(", ")
    patchdir = globalvars.SUBSTITUTION_MAP['rbuild_patches_dir'] + '/' + package
    misc.verbose_output("Patches: Ensuring directory \"" + patchdir + "\" exists... ")
    if not os.path.isdir(patchdir):
        try:
            os.makedirs(patchdir)
        except OSError as e:
            misc.die("\nPatches error: Could not create directory \"" + patchdir + "\"! Exiting.")
    misc.verbose_output("ok\n")
    i = 0

    for uri in patches:
        filename = os.path.basename(uri)
        absolute_path = patchdir + '/' + filename
        if not os.path.isfile(absolute_path):
            misc.fetch_file(uri, patchdir, filename)
        misc.verbose_output("Comparing checksums for patch " + str(i) + "... ")
        if md5s == None:
            misc.verbose_output("skipping (not available)\n")
        else:
            if misc.get_file_hash(absolute_path) == md5s[i]:
                misc.verbose_output("ok (matches)\n")
            else:
                misc.verbose_output("Mismatch! Fetching again...\n")
                misc.remove_file_or_dir(absolute_path)
                misc.fetch_file(uri, patchdir, filename)
                misc.verbose_output("Comparing checksums once more... ")
                if misc.get_file_hash(absolute_path) == md5s[i]:
                    misc.verbose_output("ok (matches)\n")
                else:
                    misc.die("Mismatch again! Bailing out...")
        i = i + 1

def build_package(phase, package):
    """Configure, make or install (phase) a program (package)."""
    if phase == "configure":
        activity = "Configuring"
        env = phase
    elif phase == "make":
        activity = "Building"
        env = phase
    elif phase == "install":
        activity = "Installing"
        env = "make"
    else:
        misc.die("\nError: Unknown build phase \"" + phase + "\"! Exiting.")
    env = misc.prepare_env(env, package)
    print(activity + " \"" + package + "\"... ", end='', flush=True)
    wrkdir = misc.get_wrkdir(package)
    for cmd in conf.get_config_value(phase + "_cmds", package).split(', '):
        r = misc.do_shell_cmd(cmd, wrkdir, env)
        if r != 0:
            misc.die("\nError: " + activity + " failed for package \"" + package + "\"! Exiting.")
    print("ok")

def ensure_missing_packages():
    """Build and install missing packages."""
    print()
    for p in packages_missing:
        ensure_clean_wrkdir(p)
        if p == "uname":
            special.prepare_uname_source()
        if p in conf.config['patches']:
            ensure_patchfiles_present(p)
            if p == "bmake":
                special.prepare_bmake_patch()
            misc.patch_source(p)
        if p in conf.config['configure_cmds']:
            build_package('configure', p)
        if p in conf.config['make_cmds']:
            build_package('make', p)
        build_package('install', p)

##########
 # Main #
##########

conf.assert_conf_file_present()
conf.config = configparser.ConfigParser()
conf.config.read(globalvars.CONFNAME)
conf.assert_config_valid()

globalvars.OSNAME = platform.get_osname()
if not globalvars.OSNAME in globalvars.OPERATING_SYSTEMS_SUPPORTED:
    misc.die("Unsupported OS: \"" + globalvars.OSNAME + "\"!")
globalvars.OSRELEASE = platform.get_os_release()
globalvars.OSVERSION = platform.get_os_version()
globalvars.OSMAJOR = platform.get_os_major()
globalvars.OSARCH = platform.get_os_arch()
globalvars.STDARCH = platform.get_stdarch()
globalvars.TGT_TRIPLE = platform.assemble_triple()
print("System: Set for " + globalvars.TGT_TRIPLE + ".")

subst.populate_substitution_map()
misc.assert_external_binaries_available()
misc.ensure_fs_hierarchy('rjail')
misc.ensure_fs_hierarchy('rbuild')
print("Filesystem: Hierarchy is in place.")
packages_present, packages_missing = misc.detect_packages()
print_info()

if len(packages_missing) > 0:
    a = 0
    while (a != "N" and a != "Y"):
        a = input("\n\nBuild missing packages now? (Y/N) ").upper()
    if (a == "N"):
        exit(0)
ensure_missing_packages()
print("All done!")


from datetime import datetime
class DateBuilder:

    def __init__(self, raw_date: str):
        self.raw_date = raw_date

    def get_month(self):
        months = {}
        for i, m in enumerate(["january", "febuary", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"]):
            months[m] = i + 1
        return months[self.raw_date.split(" ")[0].strip().lower()]
    def get_day(self):
        return int(self.raw_date.split(",", 1)[0].split(" ", 1)[1].split(" - ")[0].strip())
    def get_year(self):
        try:
            return int(self.raw_date.split(",")[1].strip())
        except:
            return datetime.now().year
    def get_time(self):
        # 0 - 23
        def convert(time):
            if "pm" in time:
                hr, min = time.split("pm")[0].split(":")
                min = float(min)/100.0
                time = float(hr)
                if time < 12.0:
                    time += 12.0
                time += min
            else:
                hr, min = time.split("am")[0].split(":")
                min = float(min)/100.0
                time = float(hr)
                if time == 12.0:
                    time -= time
                time += min
            return time
        temp = self.raw_date.split(",")[-1].split(" to ")
        if len(temp) == 1:
            return convert(temp[0].strip()), convert(temp[0].strip())
        start, end = temp
        return convert(start.strip()), convert(end.strip())

    def less_than_equal(self, date):
        return self.get_year() <= date.get_year() and (self.get_month() < date.get_month() or (self.get_month() == date.get_month() and (self.get_day() < date.get_day() or (self.get_day() == date.get_day() and self.get_time()[-1] <= date.get_time()[-1]))))

    def makeDate(self):
        start, end = self.get_time()
        return (self.get_year(), self.get_month(), self.get_day(), start, end)

    def displayDate(self):
        o = ""
        for value in self.makeDate():
            o += str(value) + " "
        return o.strip()


import os
import copy
import sys
import glog
import tifffile
try: from .tools import uity
except: from tools import uity
import numpy as np
from absl import flags, app
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
import controller.processing

class TissueCut(object):
    def __init__(self, gpu="-1", num_threads=0):
        """
        :param img_type:ssdna，rna
        :param model_path:path of weights
        """
        self._WIN_SIZE = None
        self._model_path = None
        self.net_cfg()
        self._gpu = gpu
        self._model = None
        self._num_threads = num_threads
        self._init_model()

    def net_cfg(self, cfg='weights.json'):
        cfg = os.path.join(os.path.dirname(os.path.abspath(__file__)), cfg)
        import json
        with open(cfg, 'r') as fd:
            dct = json.load(fd)
        self._WIN_SIZE = dct['tissue']['input']
        self._model_path = dct['tissue']['weights_path']
        if not os.path.exists(self._model_path):
            glog.error('Not found weights file in {}.'.format(self._model_path))
        else: glog.info(f"Start load weights from {self._model_path}")

    def _init_model(self):
        from net.onnx_net import cl_onnx_net
        self._model = cl_onnx_net(self._model_path, self._gpu, self._num_threads)

    def f_predict(self, img):
        """
        :param img:CHANGE
        :return: Model input image, mask
        """
        img = np.squeeze(img)
        src_shape = img.shape[:2]

        img = controller.processing.f_tissue_preprocess(img, self._WIN_SIZE)
        pred = self._model.f_predict(copy.deepcopy(img))
        pred = controller.processing.f_post_process(pred)
        pred = uity.f_resize(pred, src_shape)
        return img, pred

def tissue_cut(input: str, output: str, gpu: str=-1, num_threads: int=0):
    if input is None or output is None:
        print("please check your parameters")
        return

    img = tifffile.imread(input)
    sg = TissueCut(gpu=gpu, num_threads=int(num_threads))
    img, pred = sg.f_predict(img)
    glog.info(f"Predict finish, start write.")
    tifffile.imwrite(output, pred, compression="zlib", compressionargs={"level": 8})
    glog.info(f"Work Finished.")

def main(argv):
    tissue_cut(input=FLAGS.input,
               output=FLAGS.output,
               gpu=FLAGS.gpu,
               num_threads=FLAGS.num_threads)

if __name__ == '__main__':
    FLAGS = flags.FLAGS
    flags.DEFINE_string('input', '', 'the input img path')
    flags.DEFINE_string('output', '', 'the output file')
    flags.DEFINE_string('gpu', '-1', 'output path')
    flags.DEFINE_integer('num_threads', 0, 'num threads.', lower_bound=0)
    app.run(main)


from itertools import combinations
import sys
input = sys.stdin.readline

def solution(orders, course):
    answer = {}
    for n in course:
        food = {}
        for i in orders:
            combi = list(combinations(sorted(i), n))
            for i2 in combi:
                try:
                    food[''.join(i2)] += 1
                except:
                    food[''.join(i2)] = 1
        food = dict(filter(lambda x:x[1] == max(food.values()), food.items()))
        for idx in food.keys():
            answer[idx] = max(food.values())
    answer = dict(filter(lambda x:x[1] >= 2, answer.items()))
    answer = [ i[0] for i in sorted(answer.items(), key=lambda x : (x[0],x[1]),reverse=False)]
    return answer


from django.shortcuts import render

# Create your views here.

from django.views.decorators.csrf import csrf_exempt
from rest_framework.parsers import JSONParser
from django.http.response import JsonResponse
from djangoapi.models import Department,Employee
from djangoapi.serializers import DepartmentSerializer,EmployeeSerializer

@csrf_exempt
def departmentApi(request,id=0):
    if request.method=='GET':
        departments=Department.objects.all()
        departments_serializer=DepartmentSerializer(departments,many=True)
        return JsonResponse(departments_serializer.data,safe=False)
    elif request.method=='POST':
        department_data=JSONParser().parse(request)
        departments_serializers=DepartmentSerializer(data=department_data)
        if departments_serializers.is_valid():
            departments_serializer.save()
            return JsonResponse("Added Sucessfully",safe=False)
        return JsonResponse("Failed to Add",safe=False)

    elif request.method=='PUT':
        department_data=JSONParser().parse(request)
        department=Department.objects.get(DepartmentId=department_data['DepartmentId'])
        departments_serializers=DepartmentSerializer(department,data=department_data)
        if departments_serializers.is_valid():
            departments_serializer.save()
            return JsonResponse("Added Sucessfully",safe=False)
        return JsonResponse("Failed to Add",safe=False)

    elif request.method=='DELETE':
        departments=Department.objects.GET(DepartmentId=id)
        department.delete()
        return JsonResponse("Deleted Sucessfully",safe=False)


import re

stroke_dic = dict()
with open('data/stoke.dat', encoding='utf-8') as f:
    data = f.readlines()
    for string in data:
        temp = string.split("|")
        temp[2] = temp[2].replace("\n", "")
        stroke_dic[temp[1]] = int(temp[2])

split_dic = dict()
with open('data/chaizi-ft.dat', encoding='utf-8') as f:
    data = f.readlines()
    for string in data:
        temp = re.split("\s", string)
        if len(temp) < 2:
            continue
        split_list = list()
        for index in range(1, len(temp) - 1):
            split_list.append(temp[index])
        split_dic[temp[0]] = split_list

def get_stroke_number(word):
    total = 0
    for i in word:
        if "一" in i:
            total += 1
        elif "二" in i:
            total += 2
        elif "三" in i:
            total += 3
        elif "四" in i:
            total += 4
        elif "五" in i:
            total += 5
        elif "六" in i:
            total += 6
        elif "七" in i:
            total += 7
        elif "八" in i:
            total += 8
        elif "九" in i:
            total += 9
        elif "十" in i:
            total += 10
        else:
            total += stroke_dic[i]
    return get_final_number(word, total)

# 检查特殊部首笔画
def get_final_number(word, number):
    for i in word:
        if i in split_dic:
            splits = split_dic[i]
            if "氵" in splits:
                # 水
                number += 1
            if "扌" in splits:
                # 手
                number += 1
            if splits[0] == "月":
                # 肉
                number += 2
            if "艹" in splits:
                # 艸
                number += 3
            if "辶" in splits:
                # 辵
                number += 4
            if splits[0] == "阜":
                # 左阝 阜
                number += 6
            if "邑" in splits and "阝" in splits:
                # 右阝 邑
                number += 5
            if splits[0] == "玉":
                # 王字旁 玉
                number += 1
            if splits[0] == "示":
                # 礻 示
                number += 1
            if splits[0] == "衣":
                # 衤 衣
                number += 1
            if splits[0] == "衣":
                # 犭 犬
                number += 1
            if splits[0] == "心":
                # 忄 心
                number += 1
    return number


import covasim as cv
import pandas as pd
import sciris as sc
import pylab as pl
import numpy as np
from matplotlib import ticker
import datetime as dt
import matplotlib.patches as patches
import seaborn as sns
import matplotlib as mpl
from matplotlib.colors import LogNorm

# Filepaths
resultsfolder = 'sweeps'
sensfolder = 'sweepssens'
figsfolder = 'figs'
process = False

# Parameter levels
T = sc.tic()
tlevels = [0.067, 0.1, 0.15, 0.19]
vlevels = np.arange(0, 5) / 4
mlevels = np.arange(0, 4) / 4
nt, nv, nm = len(tlevels), len(vlevels), len(mlevels)

# Fonts and sizes for all figures
font_size = 26
font_family = 'Proxima Nova'
pl.rcParams['font.size'] = font_size
pl.rcParams['font.family'] = font_family

################################################################################################
# Do processing if required
################################################################################################
if process:
    for thisfig in [resultsfolder,sensfolder]:
        results = {'cum_infections': {}, 'r_eff': {}, 'new_infections':{}, 'cum_quarantined':{}}
        for future_test_prob in tlevels:
            for name in ['cum_infections', 'r_eff', 'new_infections','cum_quarantined']: results[name][future_test_prob] = {}
            for venue_trace_prob in vlevels:
                for name in ['cum_infections', 'r_eff', 'new_infections','cum_quarantined']: results[name][future_test_prob][venue_trace_prob] = []
                for mask_uptake in mlevels:
                    print(f'mask_uptake: {mask_uptake}, venue_trace_prob: {venue_trace_prob}, future_test_prob: {future_test_prob}')
                    msim = sc.loadobj(f'{thisfig}/nsw_tracingsweeps_T{int(future_test_prob * 100)}_M{int(mask_uptake * 100)}_V{int(venue_trace_prob * 100)}.obj')
                    results['cum_quarantined'][future_test_prob][venue_trace_prob].append(msim.results['cum_quarantined'].values[-1]-msim.results['cum_quarantined'].values[244])
                    results['cum_infections'][future_test_prob][venue_trace_prob].append(msim.results['cum_infections'].values[-1]-msim.results['cum_infections'].values[244])
                    results['r_eff'][future_test_prob][venue_trace_prob].append(msim.results['r_eff'].values[-1])
                    results['new_infections'][future_test_prob][venue_trace_prob].append(msim.results['new_infections'].values)
        sc.saveobj(f'{thisfig}/nsw_sweep_results.obj', results)
#else:
#    results = sc.loadobj(f'{resultsfolder}/nsw_sweep_results.obj')

################################################################################################################
# Figure 2 and S2: grids of new infections
################################################################################################################

for thisfig in [resultsfolder, sensfolder]:

    # Fonts and sizes
    fig = pl.figure(figsize=(24,16))
    results = sc.loadobj(f'{thisfig}/nsw_sweep_results.obj')

    # Subplot sizes
    xgapl = 0.05
    xgapm = 0.017
    xgapr = 0.05
    ygapb = 0.05
    ygapm = 0.017
    ygapt = 0.05
    nrows = nt
    ncols = nv
    dx = (1-(ncols-1)*xgapm-xgapl-xgapr)/ncols
    dy = (1-(nrows-1)*ygapm-ygapb-ygapt)/nrows
    nplots = nrows*ncols
    ax = {}
    colors = pl.cm.GnBu(np.array([0.4,0.6,0.8,1.]))
    labels = ['0% masks', '25% masks', '50% masks', '75% masks']

    epsx = 0.003
    epsy = 0.008
    llpad = 0.01
    rlpad = 0.005

    if thisfig==resultsfolder:
        pl.figtext(xgapl+dx*nv+xgapm*(nv-1)+rlpad, ygapb+(ygapm+dy)*0+epsy, '      90% testing      ', rotation=90, fontsize=30, fontweight='bold', bbox={'edgecolor':'none', 'facecolor':'silver', 'alpha':0.5, 'pad':4})
        pl.figtext(xgapl+dx*nv+xgapm*(nv-1)+rlpad, ygapb+(ygapm+dy)*1+epsy, '      80% testing      ', rotation=90, fontsize=30, fontweight='bold', bbox={'edgecolor':'none', 'facecolor':'silver', 'alpha':0.5, 'pad':4})
        pl.figtext(xgapl+dx*nv+xgapm*(nv-1)+rlpad, ygapb+(ygapm+dy)*2+epsy, '      65% testing      ', rotation=90, fontsize=30, fontweight='bold', bbox={'edgecolor':'none', 'facecolor':'silver', 'alpha':0.5, 'pad':4})
        pl.figtext(xgapl+dx*nv+xgapm*(nv-1)+rlpad, ygapb+(ygapm+dy)*3+epsy, '      50% testing      ', rotation=90, fontsize=30, fontweight='bold', bbox={'edgecolor':'none', 'facecolor':'silver', 'alpha':0.5, 'pad':4})

    elif thisfig==sensfolder:
        pl.figtext(xgapl+dx*nv+xgapm*(nv-1)+rlpad, ygapb+(ygapm+dy)*0+epsy, '  90% symp. testing \n 60% contact testing ', rotation=90, fontsize=26, fontweight='bold', bbox={'edgecolor':'none', 'facecolor':'silver', 'alpha':0.5, 'pad':4})
        pl.figtext(xgapl+dx*nv+xgapm*(nv-1)+rlpad, ygapb+(ygapm+dy)*1+epsy, '  80% symp. testing \n 50% contact testing ', rotation=90, fontsize=26, fontweight='bold', bbox={'edgecolor':'none', 'facecolor':'silver', 'alpha':0.5, 'pad':4})
        pl.figtext(xgapl+dx*nv+xgapm*(nv-1)+rlpad, ygapb+(ygapm+dy)*2+epsy, '  65% symp. testing \n 40% contact testing ', rotation=90, fontsize=26, fontweight='bold', bbox={'edgecolor':'none', 'facecolor':'silver', 'alpha':0.5, 'pad':4})
        pl.figtext(xgapl+dx*nv+xgapm*(nv-1)+rlpad, ygapb+(ygapm+dy)*3+epsy, '  50% symp. testing \n 30% contact testing ', rotation=90, fontsize=26, fontweight='bold', bbox={'edgecolor':'none', 'facecolor':'silver', 'alpha':0.5, 'pad':4})

    pl.figtext(xgapl+(dx+xgapm)*0+epsx, ygapb+dy*nm+ygapm*(nm-1)+llpad, '        0% tracing           ', fontsize=30, fontweight='bold', bbox={'edgecolor':'none', 'facecolor':'silver', 'alpha':0.5, 'pad':4})
    pl.figtext(xgapl+(dx+xgapm)*1+epsx, ygapb+dy*nm+ygapm*(nm-1)+llpad, '        25% tracing          ', fontsize=30, fontweight='bold', bbox={'edgecolor':'none', 'facecolor':'silver', 'alpha':0.5, 'pad':4})
    pl.figtext(xgapl+(dx+xgapm)*2+epsx, ygapb+dy*nm+ygapm*(nm-1)+llpad, '        50% tracing         ', fontsize=30, fontweight='bold', bbox={'edgecolor':'none', 'facecolor':'silver', 'alpha':0.5, 'pad':4})
    pl.figtext(xgapl+(dx+xgapm)*3+epsx, ygapb+dy*nm+ygapm*(nm-1)+llpad, '        75% tracing          ', fontsize=30, fontweight='bold', bbox={'edgecolor':'none', 'facecolor':'silver', 'alpha':0.5, 'pad':4})
    pl.figtext(xgapl+(dx+xgapm)*4+epsx, ygapb+dy*nm+ygapm*(nm-1)+llpad, '        100% tracing        ', fontsize=30, fontweight='bold', bbox={'edgecolor':'none', 'facecolor':'silver', 'alpha':0.5, 'pad':4})

    # Extract plot values
    def plinf(pn, what='new_infections'):
        # Get series for this plot number
        t = list(results['new_infections'].keys())[(nplots-1-pn)//nv]
        v = list(results['new_infections'][t].keys())[pn%nv]
        if what =='new_infections':
            #return np.array(([results['new_infections'][t][v][mm][214:] for mm in range(nm)]))
            return np.array([[results['new_infections'][t][v][mm][200+i:214+i].sum() / 14 for i in range(306-214)] for mm in range(nm)])
        elif what == 'cum_infections':
            return results['cum_infections'][t][v]

    @ticker.FuncFormatter
    def date_formatter(x, pos):
        return (cv.date('2020-09-30') + dt.timedelta(days=x)).strftime('%d-%b')

    for pn in range(nplots):
        ax[pn] = pl.axes([xgapl+(dx+xgapm)*(pn%ncols), ygapb+(ygapm+dy)*(pn//ncols), dx, dy])
        data = plinf(pn)
        for mi,mval in enumerate(mlevels):
            ax[pn].plot(range(len(data[mi,:])), data[mi,:], '-', lw=4, c=colors[mi], label=labels[mi], alpha=1.0)
            val = sc.sigfig(plinf(pn, what='cum_infections')[mi],3) if plinf(pn, what='cum_infections')[mi]<100 else sc.sigfig(plinf(pn, what='cum_infections')[mi],2)
            ax[pn].text(0.1, 180-mi*15, val.rjust(6), fontsize=20, family='monospace', color=colors[mi])

        ax[pn].set_ylim(0, 200)
        ax[pn].xaxis.set_major_formatter(date_formatter)

        if pn==4: pl.legend(loc='upper right', frameon=False, fontsize=20)
        if pn not in [0,5,10,15]:
            ax[pn].set_yticklabels([])
        else:
            ax[pn].set_ylabel('New infections')
        if pn not in range(nv):
            ax[pn].set_xticklabels([])
        else:
            xmin, xmax = ax[pn].get_xlim()
            ax[pn].set_xticks(pl.arange(xmin+5, xmax, 40))

    if thisfig==resultsfolder: figname = figsfolder+'/fig2_grid.png'
    elif thisfig==sensfolder: figname = figsfolder+'/figS2_grid.png'

    cv.savefig(figname, dpi=100)

#d = {'testing': [0.067]*nv*nm+[0.1]*nv*nm+[0.15]*nv*nm+[0.19]*nv*nm, 'tracing': [0.0]*nm+[0.25]*nm+[0.5]*nm+[0.75]*nm+[1.0]*nm+[0.0]*nm+[0.25]*nm+[0.5]*nm+[0.75]*nm+[1.0]*nm+[0.0]*nm+[0.25]*nm+[0.5]*nm+[0.75]*nm+[1.0]*nm+[0.0]*nm+[0.25]*nm+[0.5]*nm+[0.75]*nm+[1.0]*nm, 'masks': [0.0,0.25,0.5,0.75]*nt*nv}
#d['val'] = []
#for t in tlevels:
#    for v in vlevels:
#        d['val'].extend(sc.sigfig(results['cum_infections'][t][v],3))
#import pandas as pd
#df = pd.DataFrame(d)
#df.to_excel('sweepresults.xlsx')

################################################################################################################
# Figure 3: bar plot of cumulative infections
################################################################################################################
mainres = sc.loadobj(f'{resultsfolder}/nsw_sweep_results.obj')
sensres = sc.loadobj(f'{sensfolder}/nsw_sweep_results.obj')

# Subplot sizes
xgapl = 0.07
xgapm = 0.02
xgapr = 0.02
ygapb = 0.1
ygapm = 0.02
ygapt = 0.08
nrows = 1
ncols = 2
dx = (1-(ncols-1)*xgapm-xgapl-xgapr)/ncols
dy = (1-(nrows-1)*ygapm-ygapb-ygapt)/nrows
nplots = nrows*ncols
ax = {}
colors = pl.cm.GnBu(np.array([0.4,0.6,0.8,1.]))
mlabels = ['0% masks', '25% masks', '50% masks', '75% masks']
tlabels = ['50%', '65%', '80%', '90%']

fig = pl.figure(figsize=(24,8*nrows))

x = np.arange(len(tlabels))
width = 0.2  # the width of the bars

# Extract data
datatoplot = {}

datatoplot[0] = np.array([[mainres['cum_infections'][t][1.0][mi] for t in tlevels] for mi in range(nm)])
datatoplot[1] = np.array([[sensres['cum_infections'][t][1.0][mi] for t in tlevels] for mi in range(nm)])
#datatoplot[2] = np.array([[mainres['cum_quarantined'][t][1.0][mi] for t in tlevels] for mi in range(nm)])
#datatoplot[3] = np.array([[sensres['cum_quarantined'][t][1.0][mi] for t in tlevels] for mi in range(nm)])

# Headings
pl.figtext(xgapl+0.001, ygapb+dy*nrows+ygapm*(nrows-1)+0.01, '     Asymptomatic testing equal to symptomatic testing         ',
           fontsize=30, fontweight='bold', bbox={'edgecolor':'none', 'facecolor':'silver', 'alpha':0.5, 'pad':4})
pl.figtext(xgapl+xgapm+dx+0.001, ygapb+dy*nrows+ygapm*(nrows-1)+0.01, '     Asymptomatic testing lower than symptomatic testing      ',
           fontsize=30, fontweight='bold', bbox={'edgecolor':'none', 'facecolor':'silver', 'alpha':0.5, 'pad':4})

# Make plots
for pn in range(nplots):
    ax[pn] = pl.axes([xgapl+(dx+xgapm)*(pn%ncols), ygapb+(ygapm+dy)*(pn//ncols), dx, dy])
    data = datatoplot[pn]
    for mi,mval in enumerate(mlevels):
        ax[pn].bar(x+width*(mval*4-1.5), data[mi,:], width, color=colors[mi], label=mlabels[mi], alpha=1.0)

    ax[pn].set_xticks(x)
    ax[pn].set_xticklabels(tlabels)
    if pn <2:
        ax[pn].set_ylim(0, 20e3)
        ax[pn].set_xlabel('Symptomatic testing rate')
    else:
        ax[pn].set_ylim(0, 250e3)
    sc.boxoff()

    if pn in [0,2]:
        ax[pn].set_ylabel('Cumulative infections')
    if pn==1:
        pl.legend(loc='upper right', frameon=False, fontsize=20)
        ax[pn].set_yticklabels([])

cv.savefig(f'{figsfolder}/fig3_bars.png', dpi=100)

################################################################################################################
# Figure X: trade-off heatmaps
################################################################################################################

# Subplot sizes
xgapl = 0.07
xgapm = 0.02
xgapr = 0.02
ygapb = 0.3
ygapm = 0.02
ygapt = 0.08
nrows = 1
ncols = nv
dx = (1-(ncols-1)*xgapm-xgapl-xgapr)/ncols
dy = (1-(nrows-1)*ygapm-ygapb-ygapt)/nrows
nplots = nrows*ncols
ax = {}

# Create figure
fig = pl.figure(figsize=(24,10))

colors = pl.cm.GnBu(np.array([0.4,0.6,0.8,1.]))
mlabels = ['0% masks', '25% masks', '50% masks', '75% masks']
tlabels = ['50%', '65%', '80%', '90%']
vlabels = ['0% tracing', '25% tracing', '50% tracing', '75% tracing', '100% tracing']

M, T = np.meshgrid(mlevels, tlevels)
mt = M.reshape(nt*nm,)
tt = T.reshape(nt*nm,)
cmin, cmax = 0., 5.
lev_exp = np.arange(0., 5.1, 0.1)
levs = np.power(10, lev_exp)

# Load objects
for pn,vl in enumerate(vlevels):

    # Load in scenario multisims
    zi1 = np.array([mainres['cum_infections'][ti][vl] for ti in tlevels])
    z = zi1.reshape(nt*nm,)

    # Set axis and plot
    ax[pn] = pl.axes([xgapl+(dx+xgapm)*(pn%ncols), ygapb+(ygapm+dy)*(pn//ncols), dx, dy])
    im = ax[pn].imshow(zi1, cmap='Oranges')

    # Annotate
    for i in range(nm):
        for j in range(nt):
            c = sc.sigfig(zi1[j, i],3)
            ax[pn].text(i, j, str(c), va='center', ha='center')

    # Axis and plot labelling
    if pn == 0:
        ax[pn].set_ylabel('Symptomatic testing rate', fontsize=24, labelpad=20)
    ax[pn].set_xlabel('Mask uptake')
    ax[pn].set_title(vlabels[pn])

    ax[pn*100] = pl.axes([xgapl+(dx+xgapm)*(pn%ncols), 0.05, dx, 0.1])
    cbar = pl.colorbar(im, cax=ax[pn*100])
cv.savefig(f'{figsfolder}/figX_heatmaps.png', dpi=100)

sc.toc(T)


from django.shortcuts import render
from django.http import HttpResponseRedirect, HttpResponse
from myapp.forms import MyModelForm
from myapp.models import MyModel

def form_request(request, url, template):
    if request.method == 'POST':
        form = MyModelForm(request.POST)
        if form.is_valid():
            name = form.cleaned_data['name']
            request.session['name'] = name
            mm = MyModel.objects.create(name=name)
            mm.save()
            return HttpResponseRedirect(url) # Redirect after POST
    else:
        form = MyModelForm()
    args = {}
    objs = MyModel.objects.all()
    if objs:
        args['last_item'] = MyModel.objects.all().order_by('pk').reverse()[0]
    args['form'] = form
    return render(request, template, args)

def main(request):
    return form_request(request, '/add/', 'main.html')

def form_1(request):
    return form_request(request, '/1/', 'form_1.html')

def form_2(request):
    return form_request(request, '/2/', 'form_2.html')

def form_add(request):
    args = {}
    name = request.session['name']
    args['name'] = name

    return render(request, 'add.html', args)


import re

NAME=r'(?P<NAME>[a-zA-Z_][a-zA-Z_0-9])'
NUM=r'(?P<NUM>\d+)'
PLUS=r'(?P<PLUS>\+)'
TIMES=r'(?P<TIMES>\*)'
EQ=r'(?P<EQ>=)'
WS=r'(?P<WS>\s+)'

master_pat=re.compile('|'.join([NAME,NUM,PLUS,TIMES,EQ,WS]))

if __name__=="__main__":
    scanner=master_pat.scanner('foo=42')
    scanner.matcher()