import os

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
from common.utils import compute_std_of_mean

SAVE_ROOT = '../../figs_sigcomm22'

plt.style.use('seaborn-deep')
plt.rcParams['font.family'] = 'Arial'
# plt.rcParams['font.size'] = 42
# plt.rcParams['axes.labelsize'] = 42
# plt.rcParams['legend.fontsize'] = 42
# plt.rcParams['figure.figsize'] = (11, 9)
plt.rcParams['svg.fonttype'] = 'none'

HATCHES = ['/', '\\', 'x', 'o', '.', 'O', '-', '*', '+']
WIDTH = 0.3

bbr_reward, bbr_tput, bbr_tail_lat, bbr_loss = 192.81, 32.94, 368.93, 0.03
copa_reward, copa_tput, copa_tail_lat, copa_loss = 183.89, 25.70, 265.02, 0.01
cubic_reward, cubic_tput, cubic_tail_lat, cubic_loss = -19.16, 33.99, 802.69, 0.02
vivace_reward, vivace_tput, vivace_tail_lat, vivace_loss = -547.01, 21.71, 947.25, 0.13
vivace_latency_reward, vivace_latency_tput, vivace_latency_tail_lat, vivace_latency_loss = -548.64, 21.84, 1010.43, 0.13
vivace_loss_reward, vivace_loss_tput, vivace_loss_tail_lat, vivace_loss_loss = -825.15, 28.89, 1125.94, 0.26

genet_reward = 223.88
genet_reward_err = 8.05
genet_tput, genet_tail_lat, genet_loss = 31.77, 183.75, 0.02
udr1_reward = 136.81
udr1_reward_err = 23.61
udr1_tput, udr1_tail_lat, udr1_loss = 23.16, 204.23, 0.03
udr2_reward = 158.48
udr2_reward_err = 17.71
udr2_tput, udr2_tail_lat, udr2_loss = 23.09, 185.58, 0.02
udr3_reward = 159.34
udr3_reward_err = 22.83
udr3_tput, udr3_tail_lat, udr3_loss = 22.72, 179.06, 0.02
real_reward = 191.61
real_reward_err = 3.88      # 26.39   250.47  0.02
cl1_reward = 143.86
cl1_reward_err = 7.64      # 22.53   206.07  0.02
cl2_reward = 177.97
cl2_reward_err = 4.55      # 23.17   204.86  0.01

udr3_real_5percent_ethernet_rewards = [177.2, 209.8, 95.2]
udr3_real_10percent_ethernet_rewards = [139, 175, 173]
udr3_real_20percent_ethernet_rewards = [133, 125, 151]
udr3_real_50percent_ethernet_rewards = [162, 124, 78]

column_wid = 0.7
capsize_wid = 8
eline_wid = 2

def generalization_test_ethernet():
    plt.rcParams['font.size'] = 36
    plt.rcParams['axes.labelsize'] = 36
    plt.rcParams['axes.titlesize'] = 36
    plt.rcParams['legend.fontsize'] = 36
    fig, ax = plt.subplots(figsize=(9, 5))
    # plt.bar([1, 2], [bbr_reward, cubic_reward], hatch=HATCHES[:2])
    bars = ax.bar([1, 2, 3, 4],
           [udr1_reward, udr2_reward, udr3_reward, real_reward],
           yerr=[udr1_reward_err, udr2_reward_err, udr3_reward_err,
                 real_reward_err], color='C0', width=column_wid,
           error_kw=dict( lw=eline_wid, capsize=capsize_wid))
    # bars = ax.bar([1, 2, 3, 4],
    #         [udr1_reward, udr2_reward, udr3_reward, real_reward],
    #         color=None, edgecolor='white')
    for bar, pat in zip(bars, HATCHES):
        bar.set_hatch(pat)

    ax.bar([5], [genet_reward], yerr=[genet_reward_err], capsize=8, width=column_wid,
           color='C2', error_kw=dict( lw=eline_wid, capsize=capsize_wid))
    # plt.title('Ethernet')
    ax.set_xticks([1, 2, 3, 4, 5])
    ax.set_xticklabels(['RL1', 'RL2', 'RL3', 'RL-real', 'Genet'], rotation=20)
    ax.set_ylabel('Test reward')
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.tick_params(
    axis='x',          # changes apply to the x-axis
    which='both',      # both major and minor ticks are affected
    bottom=False,      # ticks along the bottom edge are off
    top=False)         # ticks along the top edge are off
    # plt.tight_layout()
    svg_file = os.path.join(SAVE_ROOT, 'evaluation_generalization_test_ethernet.svg')
    pdf_file = os.path.join(SAVE_ROOT, 'evaluation_generalization_test_ethernet.pdf')
    fig.savefig(svg_file,  bbox_inches='tight')
    os.system("inkscape {} --export-pdf={}".format(svg_file, pdf_file))
    os.system("pdfcrop --margins 1 {} {}".format(pdf_file, pdf_file))

def asymptotic_real():
    plt.rcParams['font.size'] = 34
    plt.rcParams['axes.labelsize'] = 34
    plt.rcParams['axes.titlesize'] = 34
    plt.rcParams['legend.fontsize'] = 34
    fig, ax = plt.subplots(figsize=(9.5, 5))
    bbr_reward = 192.81 #32.94   368.93  0.03
    udr_real_synthetic_reward =  171.16 #    23.67   194.00  0.02
    udr_real_synthetic_reward_err =  24.22
    genet_real_synthetic_reward = 239.39   # 30.93   208.04  0.02
    genet_real_synthetic_reward_err = 7.34
    cubic_reward = 97.16 #  33.99   802.69  0.02

    # plt.bar([1, 2], [bbr_reward, cubic_reward])
    # plt.bar([3.5, 4.5,],
    ax.bar([1, 2.2, 3.4, 4.6, 5.8],
            [udr_real_synthetic_reward, # 1%
             np.mean(udr3_real_10percent_ethernet_rewards),
             np.mean(udr3_real_20percent_ethernet_rewards),
             np.mean(udr3_real_50percent_ethernet_rewards),
             real_reward],
            yerr=[udr_real_synthetic_reward_err,
              compute_std_of_mean(udr3_real_10percent_ethernet_rewards),
              compute_std_of_mean(udr3_real_20percent_ethernet_rewards),
              compute_std_of_mean(udr3_real_50percent_ethernet_rewards),

                  real_reward_err], hatch=HATCHES[:5], capsize=8)

    ax.bar([7.3], [genet_real_synthetic_reward],
           yerr=[genet_real_synthetic_reward_err], capsize=8, color='C2')

    ax.set_xticks([1, 2.2, 3.4, 4.6, 5.8, 7.3])
    ax.set_xticklabels(['5%', '10%',
                        '20%', '50%',
                        '100%', 'Genet\n(synthetic+real)'],)
                        # rotation=30, ha='right', rotation_mode='anchor')
    ax.annotate("RL (synthetic + real)", (0.9, 209))
    ax.set_ylabel('Test reward')
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.tick_params(
    axis='x',          # changes apply to the x-axis
    which='both',      # both major and minor ticks are affected
    bottom=False,      # ticks along the bottom edge are off
    top=False)         # ticks along the top edge are off
    # plt.tight_layout()

    svg_file = os.path.join(SAVE_ROOT, 'evaluation_asymptotic_real_new.svg')
    pdf_file = os.path.join(SAVE_ROOT, 'evaluation_asymptotic_real_new.pdf')
    fig.savefig(svg_file,  bbox_inches='tight')
    os.system("inkscape {} --export-pdf={}".format(svg_file, pdf_file))
    os.system("pdfcrop --margins 1 {} {}".format(pdf_file, pdf_file))

def cc_scatter():
    plt.rcParams['font.size'] = 32
    plt.rcParams['axes.labelsize'] = 32
    plt.rcParams['axes.titlesize'] = 32
    plt.rcParams['legend.fontsize'] = 32
    fig, ax = plt.subplots(figsize=(9, 5))
    msize = 200
    ax.scatter([bbr_tail_lat], [bbr_tput], marker='d', s=msize, color='C0',
               label='BBR')
    ax.annotate('BBR', (bbr_tail_lat+70, bbr_tput-2))
    ax.scatter([copa_tail_lat], [copa_tput], marker='>', s=msize, color='C1',
               label='Copa')
    ax.annotate('Copa', (copa_tail_lat+110, copa_tput+0.8))
    ax.scatter([cubic_tail_lat], [cubic_tput], marker='v',
               s=msize, color='darkorange', label='Cubic')
    ax.annotate('Cubic', (cubic_tail_lat+80, cubic_tput - 2))
    ax.scatter([vivace_latency_tail_lat], [vivace_latency_tput], marker='^',
               s=msize, color='C3', label='Vivace')
    ax.annotate('Vivace', (vivace_latency_tail_lat, vivace_latency_tput))
    ax.scatter([udr1_tail_lat], [udr1_tput], marker='<', s=msize, color='C4',
               label='RL1')
    ax.annotate('RL1', (udr1_tail_lat+115, udr1_tput))
    ax.scatter([udr2_tail_lat], [udr2_tput], marker='p', s=msize, color='C5',
               label='RL2')
    ax.annotate('RL2', (udr2_tail_lat+20, udr2_tput+0.5))
    ax.scatter([udr3_tail_lat], [udr3_tput], marker='s', s=msize, color='indigo', label='RL3')
    ax.annotate('RL3', (udr3_tail_lat+120, udr3_tput-1.2))
    ax.scatter([genet_tail_lat], [genet_tput], s=msize, color='C2',
               label='Genet')
    ax.annotate('Genet', (genet_tail_lat+80, genet_tput-1.5))
    ax.set_ylabel('Throughput (Mbps)')
    ax.set_xlabel('90th percentile latency (ms)')
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.invert_xaxis()
    # fig.legend(bbox_to_anchor=(0, 1.02, 1, 0.14), ncol=4, loc="upper center",
    #            borderaxespad=0, borderpad=0.2, columnspacing=0.01,
    #            handletextpad=0.001)

    svg_file = os.path.join(SAVE_ROOT, 'evaluation_cc_scatter.svg')
    pdf_file = os.path.join(SAVE_ROOT, 'evaluation_cc_scatter.pdf')
    fig.savefig(svg_file,  bbox_inches='tight')
    os.system("inkscape {} --export-pdf={}".format(svg_file, pdf_file))
    os.system("pdfcrop --margins 1 {} {}".format(pdf_file, pdf_file))

if __name__ == '__main__':
    # generalization_test_ethernet()
    asymptotic_real()
    # cc_scatter()


from collections import deque
def DFS(n,graph,q):
    visited_DFS[q]=1
    print(q+1, end=' ')
    for i in range(0,n):
        if(visited_DFS[i]==0) and (graph[q][i]==1):
            DFS(n,graph,i)
def BFS(n,graph,q):
    queue=deque()
    queue.append(q)
    visited_BFS[q]=1
    while queue:
        tmp=queue.popleft()
        print(tmp+1, end=' ')
        for i in range(0,n):
            if(visited_BFS[i]==0) and (graph[tmp][i]==1):
                queue.append(i)
                visited_BFS[i]=1
n=4
q=0
graph=[[0]*(n+1) for _ in range(n+1)]
visited_DFS=[0]*n
visited_BFS=[0]*n
computers=[[1,2],[1,3],[1,4],[2,4],[3,4]]
for i in computers:
    graph[i[0]-1][i[1]-1]=graph[i[1]-1][i[0]-1]=1
print(graph)
DFS(n,graph,q)
print()
BFS(n,graph,q)


# coding: utf-8

# In[ ]:

from scipy import sparse

train_tfidf=sparse.load_npz("./npy/data1_tfidf_train_x.npz")
train_count_fea=sparse.load_npz("./npy/data1_count_fea_train_x.npz")
train_x=sparse.hstack((train_tfidf,train_count_fea))

# In[ ]:

test_tfidf=sparse.load_npz("./npy/data1_tfidf_test_x.npz")
test_count_fea=sparse.load_npz("./npy/data1_count_fea_test_x.npz")
test_x=sparse.hstack((test_tfidf,test_count_fea))

# In[ ]:

import numpy as np
train_y=np.load("./npy/data1_train_y.npy")

# In[ ]:

from sklearn.model_selection import  StratifiedKFold
import lightgbm as lgb
from sklearn.metrics import  log_loss
nfold=5

kf=StratifiedKFold(nfold)
train_x=train_x.tocsr()
test_x=test_x.tocsr()

params = {
    'max_depth':-1,
    'boosting_type':'gbdt',
    'num_leaves': 31,
    'objective': 'multiclass',
    'min_data_in_leaf': 200,
    'learning_rate': 0.02,
    'max_bin':255,
    'feature_fraction': 0.8,
    'bagging_fraction': 0.7,
    'bagging_freq': 1,
    'metric': 'multi_logloss',
    'num_class':6,
    'num_threads': -1,
    'is_unbalance':True,
}
import numpy as np
oof_train=np.zeros((train_x.shape[0],6))
oof_test=np.zeros((test_x.shape[0],6))
oof_test_skf=np.zeros((nfold,test_x.shape[0],6))
logloss_csv=[]

from sklearn.linear_model import LogisticRegression

for i,(train,test) in  enumerate(kf.split(train_x,train_y)):
    print("*******第 %d 折********"%i)

    x_tr=train_x[train]
    x_te=train_x[test]
    y_tr=train_y[train]
    y_te=train_y[test]

    lr=LogisticRegression()
    lr.fit(x_tr,y_tr)
    eva_pre=lr.predict_proba(x_te)
    oof_train[test]=eva_pre
    logloss=log_loss(y_te,eva_pre)
    print("valid logloss %s"%logloss)
    logloss_csv.append(logloss)
    pre=lr.predict_proba(test_x)
    oof_test_skf[i,:]=pre

print("mean loss  %f"%np.mean(logloss_csv))

# In[ ]:

oof_test=oof_test_skf.mean(axis=0)
score=np.mean(logloss_csv)
print(score)

# In[ ]:

import numpy as np
def check_ans(result):
    new_result=[]
    for i in result:
        m=i.sum()
        for j in range(0,6):
            i[j]=i[j]/m
        if abs(i.sum()-1)>=1e-6:
            print(i.sum()-1)
        new_result.append(i)
    return new_result

# In[ ]:

import numpy as np

res=check_ans(oof_test)

# In[ ]:

#
test_prob=pd.DataFrame(oof_test)
test_prob.columns=['prob0','prob1','prob2','prob3','prob4','prob5']
test_prob['file_id']=test_data['file_id']

# In[ ]:

test_prob=test_prob[['file_id','prob0','prob1','prob2','prob3','prob4','prob5']]
test_prob.head(20)

# In[ ]:

test_prob.to_csv("lgb_submit_%f.csv"%np.mean(logloss_csv),index=False)

# In[ ]:

test_prob.info()

# In[ ]:

#保存train_prob
import pandas as pd
train_num=1
train_prob=pd.DataFrame(oof_train)
train_prob.columns=['class_%s'%i for i  in   range(0,6)]
test_prob=pd.DataFrame(oof_test)
test_prob.columns=['class_%s'%i for i in range(0,6)]
train_prob.to_csv("./stacking_csv/train%d_lr_train_prob_cv5_%f.csv"%(train_num,score),index=False)
test_prob.to_csv("./stacking_csv/train%d_lr_test_prob_cv5_%f.csv"%(train_num,score),index=False)

# In[ ]:

np.mean(logloss_csv)

# In[ ]:

from sklearn.metrics import  classification_report
y_pr=[np.argmax(i) for i in eva_pre]
print(classification_report(y_te,y_pr))


import datetime

from django.shortcuts import redirect

from core.tasks import send_email
from core.models import User, Product, ProductPackage, \
    ClientPackages, Transaction, Notification

def generate_buyer_token(buyer):
    """
    GET BUYER ID
    GET THE DATEANDTIME
    GENERATE TOKEN BY ADDING THE BUYER ID + DATEANDTIME
    """
    return f"{buyer.id}{datetime.datetime.now().isoformat().replace('-', '').replace(':', '').replace('.', '')}"

def complete_product_purchase(data):
    """
    COMPLETE PRODUCT PURCHASE:
    GET PRODUCT PACKAGE
    GET PRODUCT BUYER
    GET SELLER
    CREATE CLIENT PACKAGE[SO USER CAN VIEW PRODUCT IN DASHBOARD]
    IF API PRODUCT:
        GENERATE TOKEN
        APPEND THE NO OF REUQESTS
    TRANSACTION LOG TO SUBRACT AMOUNT FROM BUYER
    TRANSACTION LOG TO ADD AMOUNT TO SELLER
    """
    product_package = ProductPackage.objects.get(id=data['product_package_id'])
    buyer = User.objects.get(id=data['uid'])
    seller = product_package.service.owner

    if product_package.service.product_type == 'A':
        cp = ClientPackages.objects.create(
            package=product_package,
            user=buyer,
            token=generate_buyer_token(buyer),
            amount_paid=product_package.price
        )
        cp.normal_requests_left = cp.normal_requests_left + product_package.normal_requests
        cp.save()
    else:
        ClientPackages.objects.create(
            package=product_package,
            user=buyer,
            token="LIFETIME-TOKEN",
            amount_paid=product_package.price,
        )
    # SUBRACT AMOUNT FROM BUYER
    Transaction.objects.create(
        coins=product_package.price,
        user=buyer,
        content='Product Purchase',
        type=1
    )
    send_email.delay(
        buyer.email,
        'Product Purchase Successful',
        f'Product Purchased successfully.\n{product_package.price}$ is subracted from your wallet.'
    )
    # ADD AMOUNT TO SELLER
    Transaction.objects.create(
        coins=product_package.price,
        user=seller,
        content=f'{buyer.username} purchased your product.',
        type=0
    )
    # SERVICE FEE SUBRACTING
    service_fee = product_package.price // 10
    Transaction.objects.create(
        coins=service_fee,
        user=seller,
        content=f'[SERVICE FEE] {buyer.username} purchased your product.',
        type=1
    )
    # ADD SERVICE FEE TO ADMIN ACCOUNT
    Transaction.objects.create(
        coins=service_fee,
        user=User.objects.get(username='admin'),
        content=f'{buyer.username} purchased your product.',
        type=0
    )
    send_email.delay(
        seller.email,
        'Product Purchase',
        f'{buyer.username} purchased your product.\n{product_package.price}$ is added to your wallet.'
    )


import pygame
from City import City
from pygame.locals import KEYDOWN, QUIT, MOUSEBUTTONDOWN, K_RETURN, K_ESCAPE
import sys

# # globals
screen_x = 500
screen_y = 500

city_color = [10, 10, 200]  # blue
city_radius = 3

font_color = [255, 255, 255]  # white

def getCitiesPoints():
    cities = []
    draw(cities)

    collecting = True

    while collecting:
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit(0)
            elif event.type == KEYDOWN and event.key == K_RETURN:
                collecting = False
            elif event.type == MOUSEBUTTONDOWN:
                pos = pygame.mouse.get_pos()
                city = City(pos[0], pos[1])
                cities.append(city)
                draw(cities)

    return cities

def draw(cities):
    screen.fill(0)
    for city in cities:
        pygame.draw.circle(screen, city_color, city.getPos(), city_radius)
    text = font.render("Nombre: %i" % len(cities), True, font_color)
    textRect = text.get_rect()
    screen.blit(text, textRect)
    pygame.display.flip()

def drawPath(cities, length):
    screen.fill(0)
    pygame.draw.lines(screen, city_color, True, cities)
    text = font.render("Meilleur chemin actuel %s" % length, True, font_color)
    textRect = text.get_rect()
    screen.blit(text, textRect)
    pygame.display.flip()

def showGUI():
    global window, screen, font
    pygame.init()
    window = pygame.display.set_mode((screen_x, screen_y))
    pygame.display.set_caption('Exemple')
    screen = pygame.display.get_surface()
    font = pygame.font.Font(None, 30)


import unittest
import os
import logging

from datetime import date

from decimal import Decimal

from ibrokersflex import parse_flex_accounts, parse_flex_positions, parse_flex_flows

class LoadFlexResultsTestCase(unittest.TestCase):
    def setUp(self):
        self.tree = None
        example_file_path = os.path.abspath(os.sep.join(['tests-data', 'example-result.xml']))
        logging.info('loading example result file: {}'.format(example_file_path))
        with open(example_file_path, 'r') as result_content:
            lines = result_content.readlines()
            content = ''.join(lines)
            self.content = content

    def test_load_accounts(self):
        accounts = parse_flex_accounts(self.content)
        self.assertEqual(accounts['U1812119']['nav_change'], 93262)
        self.assertEqual(accounts['U1812946']['account_alias'], 'Vol 946')
        self.assertEqual(accounts['U1812119']['cash'], 1520018)
        self.assertEqual(accounts['U1812946']['cash'], 1515189)
        total_cash = 0.
        total_nav = 0.
        for account_code in accounts:
            total_cash += accounts[account_code]['cash']
            total_nav += accounts[account_code]['nav_end']

        self.assertAlmostEqual(total_cash, 8245576, places=0)
        self.assertAlmostEqual(total_nav, 12176892, places=0)

    def test_load_flows(self):
        flows = parse_flex_flows(self.content)
        self.assertTrue(date(2017, 8, 7) in flows.index)
        self.assertTrue(date(2017, 8, 6) not in flows.index)
        result = flows.loc[date(2017, 8, 7)]
        expected = {
            'U1812119': Decimal('-650000'),
            'U2036485': Decimal('250000'),
        }
        self.assertDictEqual(result.to_dict(), expected)

    def test_load_positions(self):
        positions = parse_flex_positions(self.content)
        self.assertEqual(positions['U1760542']['132757576']['description'], 'EUR 18SEP17')
        self.assertEqual(positions['U1812038']['215313814']['description'], 'BAX 17DEC18')
        self.assertEqual(positions['U1812038']['119750055']['currency'], 'USD')
        self.assertEqual(positions['U1812038']['259910541']['assetCategory'], 'FUT')

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(name)s:%(levelname)s:%(message)s')
    unittest.main()


from theBrain import *
from datetime import datetime
import shutil

SENSE_BUFFER = [np.zeros([TRUNC_BACKPROP_LENGTH, SENSOR_DATA_DIM]),
                    np.zeros([TRUNC_BACKPROP_LENGTH, OUTPUT_DIM])]

def truth_values(batch_size):
    # this is supposed to be high when I am here
    hour = datetime.now().hour/NUM_HOURS
    detection = int(sense.IAM_HERE_PIN.read())
    angle = 0

    return [[hour, detection, angle]]*batch_size

def create_buffer():
    global SENSE_BUFFER
    print("Creating sense buffer... please wait")
    for i in range(TRUNC_BACKPROP_LENGTH):
        updateBuffer()
        time.sleep(0.5)
    print("Buffer created.")

def updateBuffer():
    global SENSE_BUFFER
    truth = np.array(truth_values(1))
    sensed_data = np.array([sense.allSensors_read()])

    SENSE_BUFFER[0] = np.concatenate([SENSE_BUFFER[0][1:,:], sensed_data], axis=0)
    SENSE_BUFFER[1] = np.concatenate([SENSE_BUFFER[1][1:,:], truth], axis=0)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    # creating new summary writers and deleting old summary logs
    print("Preparing to log and serve the training summaries to tensorboard...")
    shutil.rmtree(log_dir, ignore_errors=True)
    train_writer, test_writer = getSummaryWriters(sess)

    # at the beginning previous time step data is set to zero
    cycles = 200000
    testStep = 500
    saveStep = 1000
    log_step = 5
    create_buffer()
    _cur_state = np.zeros([SENSOR_DATA_DIM, STATE_SIZE], dtype=np.float32)
    # print(type(_cur_state))
    for i in range(cycles):
        truth_val = truth_values(1)
        sensed_data = [sense.allSensors_read()]
        _, hour_pred, detection_pred, head_angle, _cur_state,loss_val,summary = sess.run([
            optim_t,
            hour_t,
            detection_t,
            motor_t,
            cur_state,
            loss_tensor,
            merged
            ],
            feed_dict={
                input_series: SENSE_BUFFER[0],
                truth_series: SENSE_BUFFER[1],
                init_state: _cur_state
            })

        updateBuffer()

        if i % log_step == 0: train_writer.add_summary(summary, i)
        if i % saveStep == 0: saveModel(sess, model_save_path[0])

        # print(hour_pred, detection_pred, head_angle, truth_val[0][:2], out[0].tolist())
        print('Time: %s, Detection: %s, Angle: %s'%(hour_pred,detection_pred,head_angle))
        print('Loss: %s'%loss_val)

        detection_LED.write(detection_pred)
        head.write(min(max(0,head_angle),180))
        time.sleep(0.5)


from __future__ import with_statement
from fabric.api import *
from fabric.contrib.console import confirm

env.hosts = ['my_server']

def test():
    with settings(warn_only=True):
        result = local('./manage.py test my_app', capture=True)
    if result.failed and not confirm("Tests failed. Continue anyway?"):
        abort("Aborting at user request.")

def commit():
    local("git add -p && git commit")

def push():
    local("git push")

def prepare_deploy():
    test()
    commit()
    push()

def deploy():
    code_dir = '/srv/django/myproject'
    with settings(warn_only=True):
        if run("test -d %s" % code_dir).failed:
            run("git clone user@vcshost:/path/to/repo/.git %s" % code_dir)
    with cd(code_dir):
        run("git pull")
        run("touch app.wsgi")

"""
This fabfile makes use of a large portion of Fabric’s feature set:

1. defining fabfile tasks and running them with fab;
2. calling local shell commands with local;
3. modifying env vars with settings;
4. handling command failures, prompting the user, and manually aborting;
5. and defining host lists and run-ning remote commands.
However, there’s still a lot more we haven’t covered here! Please make sure you follow the various “see also” links, and check out the documentation table of contents on the main index page.
"""


import time
import pytest
from aeon.measurement import Measurement
from aeon.errors import InvalidMeasurementState

def test_cant_start_measurement_twice():
    m = Measurement("name", "group")
    m.start()
    with pytest.raises(InvalidMeasurementState):
        m.start()

def test_cant_stop_measurement_before_starting_it():
    m = Measurement("name", "group")
    with pytest.raises(InvalidMeasurementState):
        m.stop()

def test_cant_stop_measurement_twice():
    m = Measurement("name", "group")
    m.start()
    m.stop()
    with pytest.raises(InvalidMeasurementState):
        m.stop()

def test_starting_measurement_increases_number_of_calls():
    m = Measurement("name", "group")
    assert m.calls == 0
    m.start()
    assert m.calls == 1

def test_measurement_measures_something():
    m = Measurement("name", "group")

    m.start()
    time.sleep(1e-3)
    m.stop()
    elapsed = m.total_runtime
    assert elapsed > 0

    m.start()
    time.sleep(1e-3)
    m.stop()
    elapsed_again = m.total_runtime
    assert elapsed_again > elapsed

@pytest.mark.fixed
def test_measurement_has_name_and_group():
    m = Measurement("name", "group")
    assert m.name == "name"
    assert m.group == "group"


from django.urls import path
from . import views
from django.conf.urls import url

urlpatterns = [
path('', views.index, name='index'),
path('series', views.series, name='series'),
path('free_episodes', views.free_episodes, name='free_episodes'),
path('gethbo', views.gethbo, name='gethbo'),
path('blog', views.post_list, name='post_list'),

	# path('main', views.main, name='main'),

]


from clients.python import TaskService_v1
from clients.python.protos.sylklabs.Todos.v1.Todos_pb2 import TaskId
import grpc

client = TaskService_v1(
    client_opt={
        'host': 'localhost',
        'port': 51010
    }
)

try:
    task = client.GetTask(TaskId(id='1'))
    print(task)
# Custom handling of exceptions
except grpc.RpcError as rpc_error:
    if rpc_error.code() == grpc.StatusCode.NOT_FOUND:
        print(f"Received RPC error: code={rpc_error.code()} message={rpc_error.details()}")
    else:
        print(f"Received unknown RPC error: code={rpc_error.code()} message={rpc_error.details()}")


"""new_eac_schema_payment
"""
from alembic import op
import sqlalchemy as sa

# pre/post deployment: pre
# revision identifiers, used by Alembic.
revision = "a7accb2d29db"
down_revision = "801ba453e407"
branch_labels = None
depends_on = None

def upgrade() -> None:
    op.add_column("payment", sa.Column("collectiveBookingId", sa.BigInteger(), nullable=True))
    op.alter_column("payment", "bookingId", existing_type=sa.BIGINT(), nullable=True)
    op.create_index(op.f("ix_payment_collectiveBookingId"), "payment", ["collectiveBookingId"], unique=False)
    op.create_foreign_key(
        "payment_collective_booking_fk", "payment", "collective_booking", ["collectiveBookingId"], ["id"]
    )

def downgrade() -> None:
    op.drop_constraint("payment_collective_booking_fk", "payment", type_="foreignkey")
    op.drop_index(op.f("ix_payment_collectiveBookingId"), table_name="payment")
    op.alter_column("payment", "bookingId", existing_type=sa.BIGINT(), nullable=False)
    op.drop_column("payment", "collectiveBookingId")


import cv2
import numpy as np
import time

t0 = time.time()

cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    if ret:
        t1 = time.time() - t0
        t1_str = str(round(t1, 2))
        frame = cv2.flip(frame, 1)
        text = f"Amir Madankan {t1_str}"
        cv2.putText(frame, text, (100, 100), cv2.FONT_HERSHEY_SIMPLEX,
                    1, (0, 0, 255), 1)
        frame_LD = 255 - frame
        frame_RD = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        n = frame_RD.shape[0]
        m = frame_RD.shape[1]
        frame_RD = frame_RD.reshape((n, m, 1))
        frame_RD = np.concatenate([frame_RD, frame_RD, frame_RD], 2)
        frame_RU = frame.copy()
        frame_RU[:, :, 2] = 255
        frame_new1 = np.concatenate((frame, frame_RU), axis=1)
        frame_new2 = np.concatenate((frame_LD, frame_RD), axis=1)
        frame_new = np.concatenate((frame_new1, frame_new2), axis=0)
        cv2.imshow("webcam", frame_new)
        q = cv2.waitKey(1)
        if q == ord("q"):
            break

cv2.destroyAllWindows()
cap.release()


import pygame
from cell import *

class Button:
    def __init__(self, WIN):
        self.FONT = pygame.font.Font('font/8-BIT WONDER.ttf', 20)
        self.WIN = WIN

    def draw_buttons(self):
        start_text = self.FONT.render("Start", 1, LIGHT_GRAY)
        reset_text = self.FONT.render("Reset",1, LIGHT_GRAY)
        pause_text = self.FONT.render("Pause", 1, LIGHT_GRAY)
        self.WIN.blit(start_text, (153, 105))
        self.WIN.blit(pause_text, (353, 105))
        self.WIN.blit(reset_text, (553, 105))

    def button_hover(self,mouse):
        # start button()
        if mouse[0] >= 150 and mouse[0] <= 250 and mouse[1] >= 100 and mouse[1]<= 140:
            pygame.draw.rect(self.WIN, DARK_GREEN, [150, 100, 100, 40])
        else:
            pygame.draw.rect(self.WIN, GREEN, [150, 100, 100, 40])
        # pause button
        if mouse[0] >= 350 and mouse[0] <= 450 and mouse[1] >= 100 and mouse[1]<= 140:
            pygame.draw.rect(self.WIN, DARK_BLUE, [350, 100, 100, 40])
        else:
            pygame.draw.rect(self.WIN, BLUE, [350, 100, 100, 40])
        # stop button
        if mouse[0] >= 550 and mouse[0] <= 650 and mouse[1] >= 100 and mouse[1]<= 140:
            pygame.draw.rect(self.WIN, DARK_RED, [550, 100, 100, 40])
        else:
            pygame.draw.rect(self.WIN, RED, [550, 100, 100, 40])


import requests
import json
import os
import time,datetime

def updateJsonFile( path, data ):
    with open(path, 'w', encoding='utf-8') as f:
        json.dump(data, f, ensure_ascii=False, indent=4)

# Actual temperature
waterData = {}
url = 'https://data.bs.ch/api/v2/catalog/datasets/100046/records?order_by=startzeitpunkt%20DESC&limit=2&pretty=false&timezone=UTC'
try:
    resp = requests.get(url=url)
    d = resp.json()
except requests.exceptions.RequestException as e:
    raise SystemExit(e)

if d['records'][0]['record']['fields']['rus_w_o_s3_te'] is not None:
    waterData['actualValue'] = d['records'][0]['record']['fields']['rus_w_o_s3_te']
    waterData['lastUpdate'] = d['records'][0]['record']['fields']['startzeitpunkt']
else:
    print('newest temp is null we take next older one')
    if d['records'][1]['record']['fields']['rus_w_o_s3_te'] is not None:
        waterData['actualValue'] = d['records'][1]['record']['fields']['rus_w_o_s3_te']
        waterData['lastUpdate'] = d['records'][1]['record']['fields']['startzeitpunkt']
    else:
        print('no valid temp')
        waterData['actualValue'] = 0
        waterData['lastUpdate'] = None

# Weekly data
waterData['chart'] = {}
waterData['chart']['week'] = []
url = 'https://data.bs.ch/api/v2/catalog/datasets/100046/records?select=avg(rus_w_o_s3_te)%20as%20temp&where=startzeitpunkt%3E%3Dnow(days%3D-7)&group_by=range(startzeitpunkt%2C%2012%20hour)%20as%20time&limit=900&pretty=false&timezone=UTC'
try:
    resp = requests.get(url=url)
    d = resp.json()
except requests.exceptions.RequestException as e:
    raise SystemExit(e)

oldT = ''
for e in d['records']:
    if  e['record']['fields']['temp'] is not None:
        waterData['chart']['week'].append(e['record']['fields']['temp'])
        oldT = e['record']['fields']['temp']
    else:
        waterData['chart']['week'].append(oldT | 0)

# Monthly data
waterData['chart']['month'] = []
url = 'https://data.bs.ch/api/v2/catalog/datasets/100046/records?select=avg(rus_w_o_s3_te)%20as%20temp&where=startzeitpunkt%3E%3Dnow(days%3D-30)&group_by=range(startzeitpunkt,2days)%20as%20time&limit=900&pretty=false&timezone=UTC'
try:
    resp = requests.get(url=url)
    d = resp.json()
except requests.exceptions.RequestException as e:
    raise SystemExit(e)

oldT = ''
for e in d['records']:
    if e['record']['fields']['temp'] is not None:
        waterData['chart']['month'].append(e['record']['fields']['temp'])
        oldT = e['record']['fields']['temp']
    else:
        waterData['chart']['month'].append(oldT | 0)

updateJsonFile( 'data/data/waterData.json', waterData)

# Air temperature
airData = {}
url = 'https://data.bs.ch/api/v2/catalog/datasets/100009/records?select=meta_airtemp%20as%20temp&where=name_original="0340A010"&limit=1&pretty=false&timezone=UTC&order_by=record_timestamp%20DESC'
try:
    resp = requests.get(url=url)
    d = resp.json()
except requests.exceptions.RequestException as e:
    raise SystemExit(e)

try:
    d['records'][0]['record']['fields']['temp']
except NameError:
    print('air temp not defined')
    try:
        d['records'][1]['record']['fields']['rus_w_o_s3_te']
    except NameError:
        print('last air temp not defined')
        airData['actualValue'] = 0
        airData['lastUpdate'] = None
    else:
        airData['actualValue'] = d['records'][1]['record']['fields']['temp']
        airData['lastUpdate'] = d['records'][1]['record']['timestamp']
else:
    airData['actualValue'] = d['records'][0]['record']['fields']['temp']
    airData['lastUpdate'] = d['records'][0]['record']['timestamp']

# Weekly data
airData['chart'] = {}
airData['chart']['week'] = []
url = 'https://data.bs.ch/api/v2/catalog/datasets/100009/records?select=avg(meta_airtemp)%20as%20temp&where=name_original="0340A010"%20and%20record_timestamp%3E%3Dnow(days%3D-7)&group_by=range(record_timestamp%2C%206%20hour)%20as%20time&limit=900&pretty=false&timezone=UTC'
try:
    resp = requests.get(url=url)
    d = resp.json()
except requests.exceptions.RequestException as e:
    raise SystemExit(e)

oldT = ''
for e in d['records']:
    try:
        e['record']['fields']['temp']
    except NameError:
        airData['chart']['week'].append(oldT | 0)
    else:
        airData['chart']['week'].append(e['record']['fields']['temp'])
        oldT = e['record']['fields']['temp']

# Monthly data
airData['chart']['month'] = []
url = 'https://data.bs.ch/api/v2/catalog/datasets/100009/records?select=avg(meta_airtemp)%20as%20temp&where=name_original="0340A010"%20and%20record_timestamp%3E%3Dnow(days%3D-30)&group_by=range(record_timestamp,2days)%20as%20time&limit=900&pretty=false&timezone=UTC'
try:
    resp = requests.get(url=url)
    d = resp.json()
except requests.exceptions.RequestException as e:
    raise SystemExit(e)

oldT = ''
for e in d['records']:
    try:
        e['record']['fields']['temp']
    except NameError:
        airData['chart']['month'].append(oldT | 0)
    else:
        airData['chart']['month'].append(e['record']['fields']['temp'])
        oldT = e['record']['fields']['temp']

updateJsonFile( 'data/data/airData.json', airData)

# Water level
levelData = {}
url = 'https://data.bs.ch/api/records/1.0/analyze?dataset=100089&y.pegel.func=AVG&y.pegel.expr=pegel&precision=year&x=timestamp&sort=-x&exclude.pegel=0'
try:
    resp = requests.get(url=url)
    d = resp.json()
except requests.exceptions.RequestException as e:
    raise SystemExit(e)
lastAvg = d[1]['pegel']

url = 'https://data.bs.ch/api/v2/catalog/datasets/100089/records?select=pegel&limit=1&pretty=false&timezone=UTC&order_by=record_timestamp%20DESC'
try:
    resp = requests.get(url=url)
    d = resp.json()
except requests.exceptions.RequestException as e:
    raise SystemExit(e)
try:
    d['records'][0]['record']['fields']['pegel']
except NameError:
    print('level not defined')
    levelData['actualValue'] = 0
    levelData['lastUpdate'] = None
else:
    levelData['actualValue'] = d['records'][0]['record']['fields']['pegel'] - lastAvg
    levelData['lastUpdate'] = d['records'][0]['record']['timestamp']

# Weekly data
levelData['chart'] = {}
levelData['chart']['week'] = []
url = 'https://data.bs.ch/api/v2/catalog/datasets/100089/records?select=avg(pegel)%20as%20pegel&where=record_timestamp%3E%3Dnow(days%3D-7)&group_by=range(record_timestamp%2C%206%20hour)%20as%20time&limit=900&pretty=false&timezone=UTC'
try:
    resp = requests.get(url=url)
    d = resp.json()
except requests.exceptions.RequestException as e:
    raise SystemExit(e)

oldT = ''
for e in d['records']:
    try:
        e['record']['fields']['pegel']
    except NameError:
        levelData['chart']['week'].append(oldT | 0)
    else:
        levelData['chart']['week'].append(e['record']['fields']['pegel'])
        oldT = e['record']['fields']['pegel']

# Monthly data
levelData['chart']['month'] = []
url = 'https://data.bs.ch/api/v2/catalog/datasets/100089/records?select=avg(pegel)%20as%20pegel&where=record_timestamp%3E%3Dnow(days%3D-30)&group_by=range(record_timestamp,2days)%20as%20time&limit=900&pretty=false&timezone=UTC'
try:
    resp = requests.get(url=url)
    d = resp.json()
except requests.exceptions.RequestException as e:
    raise SystemExit(e)

oldT = ''
for e in d['records']:
    try:
        e['record']['fields']['pegel']
    except NameError:
        levelData['chart']['month'].append(oldT | 0)
    else:
        levelData['chart']['month'].append(e['record']['fields']['pegel'])
        oldT = e['record']['fields']['pegel']

updateJsonFile( 'data/data/levelData.json', levelData)

# Quality data
qualityData = {}

qualityData['quality'] = 2

# Global radiation
# Weekly data
globalRadiationData = {}
globalRadiationData['measure'] = 'globalRadiation'
globalRadiationData['chart'] = {}
globalRadiationData['chart']['week'] = []
url = 'https://data.bs.ch/api/v2/catalog/datasets/100254/records?select=gre000d0%20as%20globalRadiation&limit=7&pretty=false&timezone=UTC&order_by=date%20DESC'
try:
    resp = requests.get(url=url)
    d = resp.json()
except requests.exceptions.RequestException as e:
    raise SystemExit(e)

oldD = ''
for e in d['records']:
    try:
        e['record']['fields']['globalRadiation']
    except NameError:
        globalRadiationData['chart']['week'].append(oldD | 0)
    else:
        globalRadiationData['chart']['week'].append(e['record']['fields']['globalRadiation'])
        oldD = e['record']['fields']['globalRadiation']

qualityData['lastUpdate'] = d['records'][0]['record']['timestamp']

globalRadiationLast72h = globalRadiationData['chart']['week'][0] + globalRadiationData['chart']['week'][1] + globalRadiationData['chart']['week'][2]
globalRadiationLast48h = globalRadiationData['chart']['week'][0] + globalRadiationData['chart']['week'][1]

# Monthly data
globalRadiationData['chart']['month'] = []
url = 'https://data.bs.ch/api/v2/catalog/datasets/100254/records?select=avg(gre000d0)%20as%20globalRadiation&order_by=date%20DESC&group_by=range(date,2days)%20as%20date&limit=15&pretty=false&timezone=UTC'
try:
    resp = requests.get(url=url)
    d = resp.json()
except requests.exceptions.RequestException as e:
    raise SystemExit(e)

oldD = ''
for e in d['records']:
    try:
        e['record']['fields']['globalRadiation']
    except NameError:
        globalRadiationData['chart']['month'].append(oldD | 0)
    else:
        globalRadiationData['chart']['month'].append(e['record']['fields']['globalRadiation'])
        oldD = e['record']['fields']['globalRadiation']

qualityData['data'] = []
qualityData['data'].append(globalRadiationData)

# Rain
# Weekly data
rainData = {}
rainData['measure'] = 'rain'
rainData['chart'] = {}
rainData['chart']['week'] = []
url = "https://data.bs.ch/api/v2/catalog/datasets/100009/records?select=max(meta_rain24h_sum)%20as%20rain&where=name_original=\"034001AF\"&limit=7&pretty=false&timezone=UTC&order_by=date%20DESC&group_by=date_format(dates_max_date, 'yyyyMMdd') as date"
try:
    resp = requests.get(url=url)
    d = resp.json()
except requests.exceptions.RequestException as e:
    raise SystemExit(e)

oldD = ''
for e in d['records']:
    try:
        e['record']['fields']['rain']
    except NameError:
        rainData['chart']['week'].append(oldD | 0)
    else:
        rainData['chart']['week'].append(e['record']['fields']['rain'])
        oldD = e['record']['fields']['rain']

maxRainLast72h = rainData['chart']['week'][0]
maxRainLast48h = maxRainLast72h
if rainData['chart']['week'][1] > maxRainLast72h:
    maxRainLast72h = rainData['chart']['week'][1]
if rainData['chart']['week'][2] > maxRainLast72h:
    maxRainLast72h = rainData['chart']['week'][2]
if rainData['chart']['week'][1] > maxRainLast48h:
    maxRainLast48h = rainData['chart']['week'][1]
if rainData['chart']['week'][2] > maxRainLast48h:
    maxRainLast48h = rainData['chart']['week'][2]

# Monthly data
rainData['chart']['month'] = []
url = 'https://data.bs.ch/api/v2/catalog/datasets/100009/records?select=avg(meta_rain24h_sum)%20as%20rain&where=name_original=\"034001AF\"&limit=15&pretty=false&timezone=UTC&order_by=date%20DESC&group_by=range(dates_max_date,2days) as date'
try:
    resp = requests.get(url=url)
    d = resp.json()
except requests.exceptions.RequestException as e:
    raise SystemExit(e)

oldD = ''
for e in d['records']:
    try:
        e['record']['fields']['rain']
    except NameError:
        rainData['chart']['month'].append(oldD | 0)
    else:
        rainData['chart']['month'].append(e['record']['fields']['rain'])
        oldD = e['record']['fields']['rain']

qualityData['data'].append(rainData)

# Quality value
dailyRainTreshold = 2
dailyRadiationTreshold = 170

print(f'dailyRainTreshold: {dailyRainTreshold}')
print(f'dailyRadiationTreshold: {dailyRadiationTreshold}')
print(f'maxRainLast72h: {maxRainLast72h}')
print(f'maxRainLast48h: {maxRainLast48h}')
print(f'globalRadiationLast72h: {globalRadiationLast72h}')
print(f'globalRadiationLast48h: {globalRadiationLast48h}')

if maxRainLast72h < dailyRainTreshold and globalRadiationLast72h > ( dailyRadiationTreshold * 3 ):
    qualityData['quality'] = 3
elif maxRainLast48h < dailyRainTreshold and globalRadiationLast48h > ( dailyRadiationTreshold * 2 ):
    qualityData['quality'] = 2
else:
    qualityData['quality'] = 1

updateJsonFile( 'data/data/qualityData.json', qualityData)


import tensorflow as tf
from tensorflow.keras.models import load_model
import pickle
import numpy as np
import argparse
import datetime
import glob
import time
import sys
import os
import re

import seaborn as sns
from matplotlib import pyplot as plt
from sklearn.metrics import (roc_auc_score, roc_curve,
  classification_report, confusion_matrix)

from milk.utilities import data_utils
from milk.utilities import model_utils
from milk.utilities import training_utils
from milk import MilkEncode, MilkPredict

# with open('../dataset/case_dict_obfuscated.pkl', 'rb') as f:
#with open('../dataset/cases_md5.pkl', 'rb') as f:
with open('case_dict_obfuscated.pkl', 'rb') as f:
  case_dict = pickle.load(f)

from milk.encoder_config import deep_args as encoder_args

def case_label_fn(data_path):
  case = os.path.splitext(os.path.basename(data_path))[0]
  y_ = case_dict[case]
  # print(data_path, y_)
  return y_

def read_test_list(test_file):
  test_list = []
  with open(test_file, 'r') as f:
    for l in f:
      test_list.append(l.replace('\n', ''))

  return test_list

def auc_curve(ytrue, yhat, savepath=None):
  plt.figure(figsize=(2,2), dpi=300)
  ytrue_c = ytrue[:,1]
  yhat_c = yhat[:,1]
  auc_c = roc_auc_score(y_true=ytrue_c, y_score=yhat_c)
  fpr, tpr, _ = roc_curve(y_true=ytrue_c, y_score=yhat_c)

  plt.plot(fpr, tpr,
      label='M1 AUC={:3.3f}'.format(auc_c))

  plt.legend(loc='lower right', frameon=True)
  plt.ylabel('Sensitivity')
  plt.xlabel('1 - specificity')

  if savepath is None:
    plt.show()
  else:
    plt.savefig(savepath, bbox_inches='tight')

""" Return a pretty string """
def pprint_cm(ytrue, yhat, labels=['0', '1']):
  n_classes = len(labels)
  cm = confusion_matrix(y_true=ytrue, y_pred=yhat)
  s = '\nConfusion Matrix:\n'
  s += '\t{}\t{}'.format(*labels)
  for k in range(n_classes):
    s += '\n'
    s += '{}'.format(labels[k])
    for j in range(n_classes):
      s += '\t{: 3d}'.format(cm[k, j])

  s += '\n'
  return s

def test_eval(ytrue, yhat, savepath=None):
  ytrue = ytrue[:,1]
  yhat_c = np.argmax(yhat, axis=-1)
  yhat = yhat[:,1]
  auc_c = 'AUC = {:3.5f}\n'.format(roc_auc_score(y_true=ytrue, y_score=yhat))
  cr = classification_report(y_true=ytrue, y_pred=yhat_c) + '\n'
  cm = pprint_cm(ytrue, yhat_c) + '\n'
  if savepath is not None:
    with open(savepath, 'w+') as f:
        f.write(auc_c)
        f.write(cr)
        f.write(cm)
  else:
    print(auc_c)
    print(cr)
    print(cm)

def run_sample(case_x, encode_model, predict_model, mcdropout=False,
             batch_size=64):
  if mcdropout:
    yhats = []
    for _ in range(25):
      zs = encode_model.predict(case_x, batch_size=batch_size)
      yhat = predict_model.predict_on_batch(zs)
      yhats.append(yhat)

    yhats = np.stack(yhats, axis=0)
    yhat = np.mean(yhats, axis=0)
  else:
    zs = encode_model.predict(case_x, batch_size=batch_size)
    yhat = predict_model.predict_on_batch(zs)

  return yhat

def main(args):
  transform_fn = data_utils.make_transform_fn(args.x_size, args.y_size,
                                              args.crop_size, args.scale)

  snapshot = 'save/{}.h5'.format(args.timestamp)
  # trained_model = load_model(snapshot)

  if args.mcdropout:
    encoder_args['mcdropout'] = True

  print('Model initializing')
  encode_model = MilkEncode(input_shape=(args.crop_size, args.crop_size, 3),
                            encoder_args=encoder_args, deep_classifier=args.deep_classifier)
  encode_shape = list(encode_model.output.shape)
  x_pl = tf.placeholder(shape=(None, args.input_dim, args.input_dim, 3), dtype=tf.float32)
  z_op = encode_model(x_pl)

  input_shape = z_op.shape[-1]
  predict_model = MilkPredict(input_shape=[input_shape], mode=args.mil)

  print('loading weights from {}'.format(snapshot))
  encode_model.load_weights(snapshot, by_name=True)
  predict_model.load_weights(snapshot, by_name=True)

  # models = model_utils.make_inference_functions(encode_model,
  #                                               predict_model,
  #                                               trained_model,
  #                                               attention_model=None)
  # encode_model, predict_model = models

  test_list = os.path.join(args.testdir, '{}.txt'.format(args.timestamp))
  test_list = read_test_list(test_list)

  ytrues = []
  yhats = []
  print('MC Dropout: {}'.format(args.mcdropout))
  for _ in range(args.n_repeat):
    for test_case in test_list:
      case_x, case_y = data_utils.load(
          data_path = test_case,
          transform_fn=transform_fn,
          case_label_fn=case_label_fn,
          all_tiles=True,
          )
      case_x = np.squeeze(case_x, axis=0)
      print('Running case x: ', case_x.shape)

      yhat = run_sample(case_x, encode_model, predict_model,
                        mcdropout=args.mcdropout,
                        batch_size=args.batch_size)
      ytrues.append(case_y)
      yhats.append(yhat)
      print(test_case, case_y, case_x.shape, yhat)

  ytrue = np.concatenate(ytrues, axis=0)
  yhat = np.concatenate(yhats, axis=0)

  ytrue_max = np.argmax(ytrue, axis=-1)
  yhat_max = np.argmax(yhat, axis=-1)
  accuracy = (ytrue_max == yhat_max).mean()
  print('Accuracy: {:3.3f}'.format(accuracy))

  if args.odir is not None:
    save_img = os.path.join(args.odir, '{}.png'.format(args.timestamp))
    save_metrics = os.path.join(args.odir, '{}.txt'.format(args.timestamp))
    save_yhat = os.path.join(args.odir, '{}.npy'.format(args.timestamp))
    save_ytrue = os.path.join(args.odir, '{}_ytrue.npy'.format(args.timestamp))
    np.save(save_yhat, yhat)
    np.save(save_ytrue, ytrue)
  else:
    save_img = None
    save_metrics = None
    save_yhat = None

  auc_curve(ytrue, yhat, savepath=save_img)
  test_eval(ytrue, yhat, savepath=save_metrics)

if __name__ == '__main__':
  # n_repeat controls re-sampling from the case
  # mcdropout is a flag for doing mcdropout to approximate posterior probability
  parser = argparse.ArgumentParser()
  parser.add_argument('--timestamp', default=None, type=str)
  parser.add_argument('--n_repeat', default=1, type=int)
  parser.add_argument('--mcdropout', default=False, action='store_true')
  parser.add_argument('--testdir', default='test_lists', type=str)
  parser.add_argument('--odir', default=None, type=str)

  parser.add_argument('--x_size', default=128, type=int)
  parser.add_argument('--y_size', default=128, type=int)
  parser.add_argument('--crop_size', default=96, type=int)
  parser.add_argument('--scale', default=1.0, type=float)
  parser.add_argument('--batch_size', default=64, type=int)

  parser.add_argument('--mil', default='attention', type=str)

  args = parser.parse_args()
  assert args.timestamp is not None
  main(args)


import sys
from collections import deque

read = sys.stdin.readline

n, m = map(int, read().split())
graph = [read().split() for _ in range(n)]
dist = [[0] * m for _ in range(n)]
res = 0

q = deque()
for i in range(n):
    for j in range(m):
        if graph[i][j] == '1':
            q.append((i, j))

while q:
    x, y = q.popleft()
    for i in range(-1, 2):
        for j in range(-1, 2):
            if i == j == 0: continue
            nx, ny = x + i, y + j
            if 0 <= nx < n and 0 <= ny < m and \
                graph[nx][ny] == '0' and dist[nx][ny] == 0:
                dist[nx][ny] = dist[x][y] + 1
                q.append((nx, ny))

for l in dist:
    res = max(res, max(l))
print(res)


#!/usr/bin/python3.2

import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.post()

    def post(self):
        raise tornado.web.HTTPError(403)

application = tornado.web.Application([
    (r"/", MainHandler),
])

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import scanpy.api as sc
import scanpy.external as sce
import os
import louvain
# import phate
import seaborn as sns
import anndata as ad
import time
import random
import tensorflow as tf
import statsmodels.api as sm
from sklearn.model_selection import train_test_split

from matplotlib import cm, colors

from palettable.scientific.sequential import Bilbao_20
from palettable.cartocolors.qualitative import Vivid_3
from palettable.cartocolors.qualitative import Vivid_4
from palettable.cartocolors.qualitative import Bold_4
from palettable.cartocolors.qualitative import Bold_6
from palettable.cartocolors.qualitative import Vivid_10
from palettable.scientific.diverging import Vik_20
from palettable.scientific.diverging import Berlin_10

random.seed(42)
np.random.seed(42)
tf.set_random_seed(42)

from model import SAUCIE
from loader import Loader

sc.settings.verbosity = 4  # verbosity: errors (0), warnings (1), info (2), hints (3)
sc.settings.set_figure_params(dpi=100, dpi_save=400)  # low dpi (dots per inch) yields small inline figures
sc.logging.print_versions()
sc.settings.figdir='./paper.figs/fig5/'

print(os.getcwd())
os.chdir("/home/UTHSCSA/iskra/CytOF_Datasets/10.08-10.10-Debarcoded/GG")

mapping = ['CD45', 'CD11b', 'FolR2', 'IAIE', 'TNFa', 'CD140a',
       'Vimentin', 'CD90', 'Ly6C', 'Ly6AE', 'Postn', 'IdU',
       'IL6', 'CD9', 'VEGF', 'CD146', 'CD200', 'Notch3',
       'aSMA', 'CD31', 'VEGFR2', 'ActCaspase3', 'P53', 'MTCO1',
       'HSP60', 'Ki67', 'pRb', 'CyclinB1', 'P21', 'bCatTotal',
       'bCatActive', 'pHistoneH3', 'Sample', 'Batch']

cytof_marks = ['CD45', 'CD11b', 'FolR2', 'IAIE', 'TNFa', 'CD140a',
       'Vimentin', 'CD90', 'Ly6C', 'Ly6AE', 'Postn', 'IdU',
       'IL6', 'CD9', 'VEGF', 'CD146', 'CD200', 'Notch3',
       'aSMA', 'CD31', 'VEGFR2', 'ActCaspase3', 'P53', 'MTCO1',
       'HSP60', 'Ki67', 'pRb', 'CyclinB1', 'P21', 'bCatTotal',
       'bCatActive', 'pHistoneH3']
expr3 = pd.read_csv("10.08.2019.gg.in.vitro.b4.csv", sep=',')

expr3_adata = ad.AnnData(expr3[mapping[:-2]])
expr3_adata.obs['Sample'] = expr3['Sample'].values
expr3_adata.obs['Batch'] = 1

vitro_adata1 = sc.pp.subsample(expr3_adata[expr3_adata.obs['Sample'] == np.unique(expr3_adata.obs['Sample'])[0]],
                              n_obs=11250,
                              random_state=42,
                              copy=True)

vitro_adata2 = sc.pp.subsample(expr3_adata[expr3_adata.obs['Sample'] == np.unique(expr3_adata.obs['Sample'])[1]],
                              n_obs=11250,
                              random_state=42,
                              copy=True)

vitro_adata3 = sc.pp.subsample(expr3_adata[expr3_adata.obs['Sample'] == np.unique(expr3_adata.obs['Sample'])[2]],
                              n_obs=11250,
                              random_state=42,
                              copy=True)

vitro_adata4 = sc.pp.subsample(expr3_adata[expr3_adata.obs['Sample'] == np.unique(expr3_adata.obs['Sample'])[3]],
                              n_obs=11250,
                              random_state=42,
                              copy=True)

expr3_adata = ad.AnnData.concatenate(vitro_adata1,
                                     vitro_adata2,
                                     vitro_adata3,
                                     vitro_adata4)

expr3_adata.var_names_make_unique()
sc.pp.filter_cells(expr3_adata, min_genes=1)

expr3_adata.obs['n_counts'] = expr3_adata.X.sum(axis=1)
sc.pl.violin(expr3_adata, ['n_counts', 'n_genes'],
             jitter=0.4, multi_panel=True, stripplot=False,
              save='vitro.pre.qc.plots.png'
             )

sc.pp.neighbors(expr3_adata, n_neighbors=30, random_state=42, method='umap')
sc.tl.louvain(expr3_adata, resolution=1, random_state=42)

sc.tl.paga(expr3_adata, groups='louvain')
sc.pl.paga(expr3_adata, plot=True, random_state=42)
sc.tl.umap(expr3_adata, init_pos='paga', random_state=42)

sc.pl.umap(expr3_adata, color='louvain', ncols=3, legend_loc='on data', save='.unsupervised.louvain.png')
sc.pl.umap(expr3_adata, color=['CD31', 'CD140a', 'CD45'], ncols=3, save='.key_markers.png')

sc.pl.dotplot(expr3_adata, groupby='louvain',
              var_names=cytof_marks, dendrogram=True,
              standard_scale='var', save='.invitro.louvain.png')

sum(expr3_adata.obs['louvain'].isin(['10', '13', '14', '15']))/45000 #Endo-like
sum(expr3_adata.obs['louvain'].isin(['0', '1', '9']))/45000 #Leuko
sum(expr3_adata.obs['louvain'].isin(['2', '3', '4', '5', '6', '7', '8', '11', '12']))/45000 #Fibro

major_clust = {'0':  'Leukocytes 30.5%',
               '1':  'Leukocytes 30.5%',
               '9':  'Leukocytes 30.5%',

               '10': 'Endothelial-like Cells 5.6%',
               '13': 'Endothelial-like Cells 5.6%',
               '14': 'Endothelial-like Cells 5.6%',
               '15': 'Endothelial-like Cells 5.6%',

               '2': 'Fibroblasts 63.9%',
               '3': 'Fibroblasts 63.9%',
               '4': 'Fibroblasts 63.9%',
               '5': 'Fibroblasts 63.9%',
               '6': 'Fibroblasts 63.9%',
               '7': 'Fibroblasts 63.9%',
               '8': 'Fibroblasts 63.9%',
               '11': 'Fibroblasts 63.9%',
               '12': 'Fibroblasts 63.9%',
              }

expr3_adata.obs['Major Clusters'] = expr3_adata.obs['louvain'].map(major_clust)

major_clust = {'0':  'FolR2 Hi Leukocytes 13.9%',
               '1':  'FolR2 Low Leukocytes 10.8%',
               '9':  'Replicating Leukocytes 5.78%',

               '10': 'CD140a Hi Endothelial-like Cells 3.04%',
               '13': 'ActCaspase3 Hi Endothelial-like Cells 1.56%',
               '14': 'CD90 Hi Endothelial-like Cells 0.511%',
               '15': 'Replicating Endothelial-like Cells 0.482%',

               '2': 'Ly6AE Low Fibroblasts 19.3%',
               '3': 'Ly6AE Hi Fibroblasts 37.9%',
               '4': 'Ly6AE Hi Fibroblasts 37.9%',
               '5': 'Ly6AE Hi Fibroblasts 37.9%',
               '6': 'Postn Hi Fibroblasts 7.98%',
               '7': 'Ly6AE Low Fibroblasts 19.3%',
               '8': 'Marker Low Fibroblasts 7.10%',
               '11': 'Ly6AE Low Fibroblasts 19.3%',
               '12': 'Replicating Fibroblasts 1.83%',
              }

expr3_adata.obs['Subclusters'] = expr3_adata.obs['louvain'].map(major_clust)
expr3_adata.obs['Condition'] = [i[:2] for i in np.asarray(expr3_adata.obs['Sample'])]

sc.pl.umap(expr3_adata, color='louvain', ncols=3, legend_loc='on data', save='.invitro.louvain.png')
sc.pl.umap(expr3_adata, color='Major Clusters', ncols=3, save='.invitro.mcs.png')
# sc.pl.paga(expr3_adata, plot=True, random_state=42, color='Major Clusters', save='.invitro.paga.mcs.png')
sc.pl.umap(expr3_adata, color='Subclusters', ncols=3, save='.invitro.subclusters.png')
sc.pl.dotplot(expr3_adata, groupby='Subclusters', var_names=cytof_marks, dendrogram=True, standard_scale='var', save='.invitro.subclusters.png')

expr3_adata.obs['Subclusters'] = [i[:-6] for i in expr3_adata.obs['Subclusters']]

prop_freq = pd.DataFrame(index=np.unique(expr3_adata.obs['Condition']), columns=np.unique(expr3_adata.obs['Subclusters']))

for i in np.unique(expr3_adata.obs['Condition']):
    for j in np.unique(expr3_adata.obs['Subclusters']):
        prop_freq[j].loc[i] = sum(expr3_adata[expr3_adata.obs['Condition']==i].obs['Subclusters']==j)/11250

prop_freq = prop_freq.T

cg = sns.clustermap(data=prop_freq.astype('float64'),
               cmap=Bilbao_20.mpl_colormap,
               figsize=(15, 15),
               method='ward',
               row_cluster=True,
               col_cluster=True,
               # annot=prop_freq.T.astype,
               # vmin=-1.0,
               # vmax=1.0,
               cbar_kws={"ticks":[0, 0.25, 0.5, 0.75, 1], "label":"Frequency of Cells"})

plt.setp(cg.ax_heatmap.xaxis.get_majorticklabels(), rotation=35)
plt.setp(cg.ax_heatmap.yaxis.get_majorticklabels(), rotation=0)

cg.fig.subplots_adjust(right=.75, top=.90, bottom=.15)
#
# cg.ax_row_dendrogram.set_visible(False)
# dendro_box = cg.ax_row_dendrogram.get_position()
# dendro_box.x0 = 0.205
# dendro_box.x1 = 0.225
#cg.cax.set_position(dendro_box)
cg.cax.yaxis.set_ticks_position("left")
cg.cax.yaxis.set_label_position("left")

for a in cg.ax_row_dendrogram.collections:
    a.set_linewidth(2)

for a in cg.ax_col_dendrogram.collections:
    a.set_linewidth(2)

plt.savefig('./paper.figs/fig5/in.vitro.freq.png', dpi=400)
plt.close()

prop_freq = prop_freq.loc[prop_freq.index[cg.dendrogram_row.reordered_ind]]
prop_freq = prop_freq[prop_freq.columns[cg.dendrogram_col.reordered_ind]]

cg = sns.clustermap(data=prop_freq.astype('float64'),
               cmap=Bilbao_20.mpl_colormap,
               figsize=(15, 15),
               method='ward',
               row_cluster=True,
               col_cluster=True,
               annot=prop_freq.round(3),
               # vmin=-1.0,
               # vmax=1.0,
               cbar_kws={"ticks":[0, 0.25, 0.5, 0.75, 1], "label":"Frequency of Cells"})

plt.setp(cg.ax_heatmap.xaxis.get_majorticklabels(), rotation=35)
plt.setp(cg.ax_heatmap.yaxis.get_majorticklabels(), rotation=0)

cg.fig.subplots_adjust(right=.75, top=.90, bottom=.15)
#
# cg.ax_row_dendrogram.set_visible(False)
# dendro_box = cg.ax_row_dendrogram.get_position()
# dendro_box.x0 = 0.205
# dendro_box.x1 = 0.225
#cg.cax.set_position(dendro_box)
cg.cax.yaxis.set_ticks_position("left")
cg.cax.yaxis.set_label_position("left")

for a in cg.ax_row_dendrogram.collections:
    a.set_linewidth(2)

for a in cg.ax_col_dendrogram.collections:
    a.set_linewidth(2)

plt.savefig('./paper.figs/fig5/in.vitro.freq.png', dpi=400)
plt.close()


from PyQt5.QtCore import QThread, pyqtSignal
import threading
import time
from program import MainProgram

# Model: pengkoneksi UI dengan program aslinya
class Model:
    def __init__(self):
        self.size = 4
        self.bombs = 0
        self.bombLoc = []
        self.program = None
        self.updater = None
        self.programThread = None
        self.printToUi = None

    def startAI(self):
        self.program = MainProgram()
        self.program.ukuran = self.size
        self.program.jumlah_bom = self.bombs
        self.program.bombLoc = self.bombLoc
        self.program.printToUI = self.printToUi
        self.programThread = threading.Thread(
            target=self.program.main
        )
        self.programThread.start()
        self.updater = checkFromProgram(self)

class checkFromProgram(QThread):
    progress = pyqtSignal(list)

    def __init__(self, model):
        super().__init__()
        self.model = model

    def run(self):
        print("run")
        while self.model.program.is_finished == 0:
            time.sleep(0.1)
        print("setup finished")
        while not self.model.program.is_finished == 2:
            time.sleep(0.2)
            self.progress.emit(self.model.program.updatePapanBasedOnFacts())


import json
import logging
from decimal import Decimal
from typing import Dict, List, Optional, Type, Union

from enforce_typing import enforce_types
from web3.datastructures import AttributeDict

from ocean_lib.config import Config
from ocean_lib.data_provider.data_service_provider import DataServiceProvider
from ocean_lib.models.bpool import BPool
from ocean_lib.models.dispenser import Dispenser
from ocean_lib.models.erc20_token import ERC20Token
from ocean_lib.models.erc721_factory import ERC721FactoryContract
from ocean_lib.models.erc721_nft import ERC721NFT
from ocean_lib.models.factory_router import FactoryRouter
from ocean_lib.models.fixed_rate_exchange import FixedRateExchange
from ocean_lib.ocean.ocean_assets import OceanAssets
from ocean_lib.ocean.ocean_compute import OceanCompute
from ocean_lib.ocean.util import get_address_of_type, get_ocean_token_address, get_web3
from ocean_lib.web3_internal.constants import ZERO_ADDRESS
from ocean_lib.web3_internal.currency import DECIMALS_18
from ocean_lib.web3_internal.currency import to_wei as _to_wei
from ocean_lib.web3_internal.utils import split_signature
from ocean_lib.web3_internal.wallet import Wallet

logger = logging.getLogger("ocean")

class Ocean:
    """The Ocean class is the entry point into Ocean Protocol."""

    @enforce_types
    def __init__(
        self, config: Union[Dict, Config], data_provider: Optional[Type] = None
    ) -> None:
        """Initialize Ocean class.

        Usage: Make a new Ocean instance

        `ocean = Ocean({...})`

        This class provides the main top-level functions in ocean protocol:
        1. Publish assets metadata and associated services
            - Each asset is assigned a unique DID and a DID Document (DDO)
            - The DDO contains the asset's services including the metadata
            - The DID is registered on-chain with a URL of the metadata store
              to retrieve the DDO from

            `asset = ocean.assets.create(metadata, publisher_wallet)`

        2. Discover/Search assets via the current configured metadata store (Aquarius)

            - Usage:
            `assets_list = ocean.assets.search('search text')`

        An instance of Ocean is parameterized by a `Config` instance.

        :param config: `Config` instance
        :param data_provider: `DataServiceProvider` instance
        """
        if isinstance(config, dict):
            # fallback to metadataStoreUri
            cache_key = (
                "metadataCacheUri"
                if ("metadataCacheUri" in config)
                else "metadataStoreUri"
            )
            metadata_cache_uri = config.get(
                cache_key, config.get("metadata_cache_uri", "http://172.15.0.5:5000")
            )
            config_dict = {
                "eth-network": {"network": config.get("network", "")},
                "resources": {
                    "metadata_cache_uri": metadata_cache_uri,
                    "provider.url": config.get("providerUri", "http://172.15.0.4:8030"),
                },
            }
            config = Config(options_dict=config_dict)
        self.config = config
        self.web3 = get_web3(self.config.network_url)

        if not data_provider:
            data_provider = DataServiceProvider

        self.assets = OceanAssets(self.config, self.web3, data_provider)
        self.compute = OceanCompute(self.config, data_provider)

        logger.debug("Ocean instance initialized: ")

    @property
    @enforce_types
    def OCEAN_address(self) -> str:
        return get_ocean_token_address(self.config.address_file, web3=self.web3)

    @enforce_types
    def to_wei(
        self, amount_in_ether: Union[Decimal, str, int], decimals: int = DECIMALS_18
    ):
        return _to_wei(amount_in_ether=amount_in_ether, decimals=decimals)

    @enforce_types
    def create_erc721_nft(
        self,
        name: str,
        symbol: str,
        from_wallet: Wallet,
        token_uri: Optional[str] = "https://oceanprotocol.com/nft/",
        template_index: Optional[int] = 1,
        additional_erc20_deployer: Optional[str] = None,
        additional_metadata_updater: Optional[str] = None,
    ) -> ERC721NFT:
        """
        This method deploys a ERC721 token contract on the blockchain.
        Usage:
        ```python
            config = Config('config.ini')
            ocean = Ocean(config)
            wallet = Wallet(
                ocean.web3,
                private_key=private_key,
                block_confirmations=config.block_confirmations,
                transaction_timeout=config.transaction_timeout,
            )
            erc721_nft = ocean.create_erc721_nft("Dataset name", "dtsymbol", from_wallet=wallet)
        ```
        :param name: ERC721 token name, str
        :param symbol: ERC721 token symbol, str
        :param from_wallet: wallet instance, wallet
        :param template_index: Template type of the token, int
        :param additional_erc20_deployer: Address of another ERC20 deployer, str
        :param token_uri: URL for ERC721 token, str

        :return: `ERC721NFT` instance
        """

        if not additional_erc20_deployer:
            additional_erc20_deployer = ZERO_ADDRESS

        if not additional_metadata_updater:
            additional_metadata_updater = ZERO_ADDRESS

        nft_factory = self.get_nft_factory()
        tx_id = nft_factory.deploy_erc721_contract(
            name=name,
            symbol=symbol,
            template_index=template_index,
            additional_metadata_updater=additional_metadata_updater,
            additional_erc20_deployer=additional_erc20_deployer,
            token_uri=token_uri,
            from_wallet=from_wallet,
        )

        address = nft_factory.get_token_address(tx_id)
        assert address, "new NFT token has no address"
        token = ERC721NFT(self.web3, address)
        return token

    @enforce_types
    def get_nft_token(self, token_address: str) -> ERC721NFT:
        """
        :param token_address: Token contract address, str
        :return: `ERC721NFT` instance
        """

        return ERC721NFT(self.web3, token_address)

    @enforce_types
    def get_datatoken(self, token_address: str) -> ERC20Token:
        """
        :param token_address: Token contract address, str
        :return: `ERC20Token` instance
        """

        return ERC20Token(self.web3, token_address)

    @enforce_types
    def get_nft_factory(self, nft_factory_address: str = "") -> ERC721FactoryContract:
        """
        :param nft_factory_address: contract address, str

        :return: `ERC721FactoryContract` instance
        """
        if not nft_factory_address:
            nft_factory_address = get_address_of_type(
                self.config, ERC721FactoryContract.CONTRACT_NAME
            )

        return ERC721FactoryContract(self.web3, nft_factory_address)

    @enforce_types
    def get_user_orders(
        self, address: str, datatoken: Optional[str] = None
    ) -> List[AttributeDict]:
        """
        :return: List of orders `[Order]`
        """
        dt = ERC20Token(self.web3, datatoken)
        _orders = []
        for log in dt.get_start_order_logs(
            address, from_all_tokens=not bool(datatoken)
        ):
            a = dict(log.args.items())
            a["amount"] = int(log.args.amount)
            a["address"] = log.address
            a["transactionHash"] = log.transactionHash
            a = AttributeDict(a.items())

            _orders.append(a)

        return _orders

    @property
    @enforce_types
    def dispenser(self):
        return Dispenser(self.web3, get_address_of_type(self.config, "Dispenser"))

    @property
    @enforce_types
    def fixed_rate_exchange(self):
        return FixedRateExchange(
            self.web3, get_address_of_type(self.config, "FixedPrice")
        )

    @enforce_types
    def create_fixed_rate(
        self,
        erc20_token: ERC20Token,
        base_token: ERC20Token,
        amount: int,
        from_wallet: Wallet,
    ) -> bytes:
        fixed_price_address = get_address_of_type(self.config, "FixedPrice")
        erc20_token.approve(fixed_price_address, amount, from_wallet)

        tx = erc20_token.create_fixed_rate(
            fixed_price_address=fixed_price_address,
            base_token_address=base_token.address,
            owner=from_wallet.address,
            publish_market_swap_fee_collector=from_wallet.address,
            allowed_swapper=ZERO_ADDRESS,
            base_token_decimals=base_token.decimals(),
            datatoken_decimals=erc20_token.decimals(),
            fixed_rate=self.to_wei(1),
            publish_market_swap_fee_amount=int(1e15),
            with_mint=0,
            from_wallet=from_wallet,
        )
        tx_receipt = self.web3.eth.wait_for_transaction_receipt(tx)
        fixed_rate_event = erc20_token.get_event_log(
            ERC721FactoryContract.EVENT_NEW_FIXED_RATE,
            tx_receipt.blockNumber,
            self.web3.eth.block_number,
            None,
        )
        exchange_id = fixed_rate_event[0].args.exchangeId

        return exchange_id

    @property
    @enforce_types
    def factory_router(self):
        return FactoryRouter(self.web3, get_address_of_type(self.config, "Router"))

    @enforce_types
    def create_pool(
        self,
        erc20_token: ERC20Token,
        base_token: ERC20Token,
        rate: int,
        vesting_amount: int,
        vesting_blocks: int,
        base_token_amount: int,
        lp_swap_fee_amount: int,
        publish_market_swap_fee_amount: int,
        from_wallet: Wallet,
    ) -> BPool:
        base_token.approve(
            get_address_of_type(self.config, "Router"), self.to_wei("2000"), from_wallet
        )

        tx = erc20_token.deploy_pool(
            rate=rate,
            base_token_decimals=base_token.decimals(),
            vesting_amount=vesting_amount,
            vesting_blocks=vesting_blocks,
            base_token_amount=base_token_amount,
            lp_swap_fee_amount=lp_swap_fee_amount,
            publish_market_swap_fee_amount=publish_market_swap_fee_amount,
            ss_contract=get_address_of_type(self.config, "Staking"),
            base_token_address=base_token.address,
            base_token_sender=from_wallet.address,
            publisher_address=from_wallet.address,
            publish_market_swap_fee_collector=get_address_of_type(
                self.config, "OPFCommunityFeeCollector"
            ),
            pool_template_address=get_address_of_type(self.config, "poolTemplate"),
            from_wallet=from_wallet,
        )
        tx_receipt = self.web3.eth.wait_for_transaction_receipt(tx)
        pool_event = self.factory_router.get_event_log(
            ERC721FactoryContract.EVENT_NEW_POOL,
            tx_receipt.blockNumber,
            self.web3.eth.block_number,
            None,
        )

        bpool_address = pool_event[0].args.poolAddress
        bpool = BPool(self.web3, bpool_address)

        return bpool

    @enforce_types
    def build_compute_provider_fees(
        self,
        provider_data: Union[str, dict],
        provider_fee_address: str,
        provider_fee_token: str,
        provider_fee_amount: int,
        valid_until: int,
    ) -> tuple:
        if isinstance(provider_data, dict):
            provider_data = json.dumps(provider_data, separators=(",", ":"))

        message = self.web3.solidityKeccak(
            ["bytes", "address", "address", "uint256", "uint256"],
            [
                self.web3.toHex(self.web3.toBytes(text=provider_data)),
                provider_fee_address,
                provider_fee_token,
                provider_fee_amount,
                valid_until,
            ],
        )
        signed = self.web3.eth.sign(provider_fee_address, data=message)
        signature = split_signature(signed)

        return (
            signature.v,
            signature.r,
            signature.s,
            self.web3.toHex(self.web3.toBytes(text=provider_data)),
        )


from typing import Type, TypeVar, List, Optional
from sqlalchemy import and_, select
from sqlalchemy.ext.asyncio import AsyncSession

from core.database import Base

ModelType = TypeVar('ModelType', bound=Base)

class BaseRepository:
    '''Base Data Repository'''

    def __init__(self, model: Type[ModelType], session: AsyncSession):
        self.model_class = model
        self.session = session

    async def create(self, **attributes) -> ModelType:
        '''
        Create a new model instance.
        :param attributes: the data for creating object
        :return: The created object.
        '''

        model = self.model_class(**attributes)
        self.session.add(model)
        await self.session.commit()
        return model

    async def update(self, model: ModelType, **update_data) -> ModelType:
        """
        Updates the given model instance with the given data.

        :param model: The model instance to update.
        :param update_data: A dictionary containing the new data to update
        the model with.
        :return: The updated model instance.
        """

        for key, value in update_data.items():
            setattr(model, key, value)

        self.session.add(model)
        await self.session.commit()
        return model

    async def delete(self, model: ModelType) -> None:
        """
        Deletes the model instance.

        :param model: The model to delete.
        :return: None
        """
        await self.session.delete(model)
        await self.session.commit()

    async def get_all(self, limit: int = 20, **kwargs) -> List[ModelType]:
        """
        Retrieves all model instances that match the specified criteria.

        :param limit: The maximum number of results to return.
        :param kwargs: Filter criteria.
        :return: A list of model instances, up to the specified limit.
        """
        query = await self._get_base_query(limit=limit, **kwargs)
        result = await self.session.execute(query)
        return result.all()

    async def get_one(self, **kwargs) -> Optional[ModelType]:
        """
        Retrieves the first model instance that matches the specified criteria.

        :param limit: The maximum number of results to return.
        :param kwargs: Filter criteria.
        :return: The model instance that matches the specified criteria,
        or None if not found.
        """
        query = await self._get_base_query(limit=1, **kwargs)
        result = await self.session.execute(query)
        model_id = result.scalars().first()
        if not model_id:
            return

        model = await self.session.get(self.model_class, model_id)
        return model

    async def _get_base_query(self, limit: int = None, **kwargs):
        """
        Constructs a base query object based on the specified filter criteria
        and limit.

        :param limit: The maximum number of results to return.
        :param kwargs: Filter criteria.
        :return: A base query object.
        """
        table = self.model_class.__table__
        query = select(table)
        filters = [
            table.columns[field] == value for field, value in kwargs.items()
        ]
        if filters:
            query = query.where(and_(*filters))
        if limit is not None:
            query = query.limit(limit)
        return query


import json
import pytest

from botocore.errorfactory import ClientError
from datetime import datetime
from datetime import timedelta

from .conftest import ObjectUsers
from .util import random_bucket_name

def test_governance_lock(users: ObjectUsers):

    # Prepare two users and a shared bucket
    owner = users.create()
    guest = users.create()

    bucket_name = random_bucket_name()

    # Object locking only works if we enable it during creation. There's no
    # way to do it afterwards.
    owner.s3_client.create_bucket(
        Bucket=bucket_name,
        ObjectLockEnabledForBucket=True
    )

    # Additionally we need to configure the default retention.
    owner.s3_client.put_object_lock_configuration(
        Bucket=bucket_name,
        ObjectLockConfiguration={
            'ObjectLockEnabled': 'Enabled',
            'Rule': {
                'DefaultRetention': {
                    'Mode': 'GOVERNANCE',
                    'Years': 10,
                }
            }
        }
    )

    # This might be Ceph-specific. Here we disable two things for everyone:
    #
    # - The ability to override the governance retention.
    # - The ability to delete object versions.
    #
    # A guest user is added with a limited set of actions.
    #
    owner.s3_client.put_bucket_policy(
        Bucket=bucket_name,
        Policy=json.dumps({
            'Version': '2012-10-17',
            'Statement': [
                {
                  "Effect": "Deny",
                  "Principal": {
                    "AWS": [
                      "*"
                    ]
                  },
                  "Action": [
                        "s3:BypassGovernanceRetention",
                        "s3:DeleteObjectVersion",
                  ],
                  "Resource": "*"
                },
                {
                    'Effect': 'Allow',
                    'Principal': {
                        'AWS': f'arn:aws:iam:::user/{guest.id}',
                    },
                    'Action': [
                        's3:PutObject',
                        's3:PutObjectAcl',
                        's3:GetObject',
                        's3:GetObjectAcl',
                        's3:DeleteObject',
                    ],
                    'Resource': [
                        '*',
                    ]
                }
            ]
        })
    )

    # Upload a test key
    owner.s3_client.put_object(
        Bucket=bucket_name,
        Body='0xdeadbeef',
        Key='test-key'
    )

    # Ensure the lock mode has been set
    data = owner.s3_client.head_object(
        Bucket=bucket_name,
        Key='test-key'
    )

    assert data['ObjectLockMode'] == 'GOVERNANCE'
    assert data['ObjectLockRetainUntilDate'].replace(tzinfo=None) \
        >= (datetime.utcnow() + timedelta(days=360*10))

    # Make sure the additional user can access s3
    obj = guest.s3_client.head_object(
        Bucket=bucket_name,
        Key='test-key',
    )

    # Governance retention does not mean we cannot delete an object...
    guest.s3_client.delete_object(
        Bucket=bucket_name,
        Key='test-key',
    )

    # ...we just can't delete a version, even if we try to force it.
    with pytest.raises(ClientError):
        guest.s3_client.delete_object(
            Bucket=bucket_name,
            Key='test-key',
            VersionId=obj['VersionId'],
            BypassGovernanceRetention=True,
        )

    # The same goes for the owner (interestingly, this seems to be idempotent)
    owner.s3_client.delete_object(
        Bucket=bucket_name,
        Key='test-key',
        BypassGovernanceRetention=True,
    )

    # But even the owner cannot bypass the retention
    with pytest.raises(ClientError):
        owner.s3_client.delete_object(
            Bucket=bucket_name,
            Key='test-key',
            VersionId=obj['VersionId'],
            BypassGovernanceRetention=True,
        )


from flask import Flask, render_template,request,redirect
from models.transaction import Transaction
from models.user import User
from models.tag import Tag
from models.item import Item
from models.merchant import Merchant
from flask import Blueprint

import repositories.transaction_repository as transaciton_repository
import repositories.user_repository as user_repository
import repositories.merchant_repository as merchant_repository
import repositories.tag_repository as tag_repository
import repositories.item_repository as item_repository

transactions_blueprint = Blueprint("transaction", __name__)

# GET
@transactions_blueprint.route("/users/<id>/transactions/new")
def transaction(id):
    user = user_repository.select(id)
    merchants = merchant_repository.select_all()
    items = item_repository.select_all()
    tags = tag_repository.select_all()
    return render_template("/users/transactions/new.html", user=user, merchants=merchants, items=items, tags=tags)

# CREATE
@transactions_blueprint.route("/users/<id>/transactions",methods=["POST"])
def create_user(id):
    name = request.form["item"]
    cost = request.form["cost"]
    tag = request.form["tag"]
    merchant = request.form["merchant"]
    dt_string = request.form["time"]
    tag_object = tag_repository.select(tag)
    merchant_object = merchant_repository.select(merchant)
    item = Item(name,cost,tag_object,merchant_object)
    item_repository.save(item)
    # item saved: working
    user_object = user_repository.select(id)

    transaction = Transaction(merchant_object,user_object,item,cost, dt_string)
    transaciton_repository.save(transaction)
    # reduce user money
    updated_user_cash = int(user_object.money) - int(cost)
    updated_user = User(user_object.name,updated_user_cash,user_object.daily_allowance,id)
    user_repository.update(updated_user)

    updated_merchant_cash = int(merchant_object.money_received) + int(cost)
    updated_merchant = Merchant(merchant_object.name,updated_merchant_cash,merchant_object.id)
    merchant_repository.update(updated_merchant)
    # reduce user money:working

    # update tag
    updated_tag_used = int(tag_object.times_used) + 1
    updated_tag = Tag(tag_object.name,updated_tag_used,tag_object.id)
    tag_repository.update(updated_tag)

    return redirect("/users/"+id)


import json
import networkx as nx
import matplotlib.pyplot as plt

# Load dataset
with open('USAir97v2.json') as file:
    dataset = json.load(file)

# Create empty graph
G = nx.Graph()

# Add nodes (airports) to the graph with importance factor attribute
for airport in dataset['nodes']:
    airport_code = airport['id']
    importance_factor = sum(edge['value'] for edge in dataset['links'] if edge['source'] == airport_code or edge['target'] == airport_code)
    G.add_node(airport_code, importance=importance_factor)

# Add edges (connections between airports) to the graph with weight and color attributes
for edge in dataset['links']:
    source = edge['source']
    target = edge['target']
    weight = edge['value']
    color = weight / max(edge['value'] for edge in dataset['links'])
    G.add_edge(source, target, weight=weight, color=color)

# Calculate positions using force layout
pos = nx.spring_layout(G)

# Draw the graph
fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(1, 1, 1)
ax.set_facecolor('None')
nx.draw_networkx_nodes(G, pos, node_size=[d['importance'] * 100 for n, d in G.nodes(data=True)], node_color='turquoise', ax=ax)
nx.draw_networkx_edges(G, pos, edge_color=[(d['color']*0.7, d['color']*0.7, d['color']*0.7) for u, v, d in G.edges(data=True)], width=[d['weight'] / 200 for u, v, d in G.edges(data=True)], ax=ax)
nx.draw_networkx_labels(G, pos, labels={node: node for node in G.nodes()}, font_size=8, ax=ax)

# Create legend
plt.scatter([], [], s=100, color='turquoise', label='Node Size: Importance Factor')
plt.scatter([], [], s=100, color='black', label='Edge Width: Weight')
plt.scatter([], [], s=100, color='red', label='Edge Color: Frequency')
plt.legend()

# Save plot with transparent background
plt.savefig('airports.png', bbox_inches='tight', transparent=True)

# Show the plot
plt.show()


from collections import defaultdict
from typing import Union
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.palettes import Blues

import numpy as np
import pandas as pd

from dara_data_interactivity.definitions import CATEGORICAL_FEATURES, GREEN, RED

def _categorical_bar_plot(data: pd.DataFrame, feature: str, individual: Union[None, dict] = None):
    """
    Plots a horizontal bar plot of the value counts of a categorical variable

    :param data: DataFrame hosting the data in question
    :param feature: the feature in which the distribution of will be plotted
    :param individual: optional individual datapoint to be highlighted in the distribution plot
    :return: the Bokeh figure to be plotted by the Bokeh extension
    """
    value_counts = data[feature].value_counts()
    hist_data = pd.DataFrame({
        feature: [*value_counts.index],
        'count': value_counts.values
    })
    if individual is not None:
        # highlighting individual in coral
        color_dict = defaultdict(lambda: 'steelblue')
        color_dict[individual[feature]] = 'coral'
        hist_data['color'] = hist_data[feature].map(color_dict)

        title = f'{feature} Distribution (Whole Dataset)'
    else:
        if feature == 'Income Bracket':
            # color coding Income Bracket in blue gradient as in the table
            hist_data['color'] = hist_data[feature].map({
                'Below Q1': Blues[4][0],
                'Above Q1': Blues[4][1],
                'Above Q2': Blues[4][2],
                'Above Q3': Blues[4][3]
            })
        else:
            # color coding Y/N features in green/red as in the table
            hist_data['color'] = hist_data[feature].map({'Y': GREEN, 'N': RED})

        title = f'{feature} Distribution (Selected Individuals)'

    p = figure(title=title, toolbar_location=None, sizing_mode='stretch_both', y_range=hist_data[feature],
               tools='hover', tooltips='@{%s}: @count' % feature)
    p.hbar(y=feature, right='count', left=0, source=ColumnDataSource(hist_data),
           line_color='white', height=0.8, color='color')

    return p

def _continuous_histogram(data: pd.DataFrame, feature: str, individual: Union[None, dict] = None):
    """
    Plots a histogram of a continuous variable

    :param data: DataFrame hosting the data in question
    :param feature: the feature in which the distribution of will be plotted
    :param individual: optional individual datapoint to be highlighted in the distribution plot
    :return: the Bokeh figure to be plotted by the Bokeh extension
    """

    hist, edges = np.histogram(data[feature].dropna(), bins=10)
    hist_data = pd.DataFrame({
        feature: hist,
        'left': edges[:-1],
        'right': edges[1:]
    })

    if individual is not None:
        # highlighting individual in coral
        colors = []
        for _, row in hist_data.iterrows():
            if individual[feature] > row['left'] and individual[feature] < row['right']:
                colors.append('coral')
            else:
                colors.append('steelblue')
        hist_data['color'] = colors

        title = f'{feature} Distribution (Whole Dataset)'
    else:
        hist_data['color'] = ['steelblue'] * len(hist)

        title = f'{feature} Distribution (Selected Individuals)'

    p = figure(title=title, toolbar_location=None, sizing_mode='stretch_both',
               tools='hover', tooltips=[(feature, '@{%s}{0.00}' % feature)])
    p.quad(bottom=0, top=feature, left='left', right='right', source=ColumnDataSource(hist_data), color='color')

    p.yaxis.formatter.use_scientific = False
    p.xaxis.formatter.use_scientific = False

    return p

def plot_distribution(data: pd.DataFrame, feature: str, individual: Union[None, dict] = None):
    """
    Plots the correct type of distribution plot based on whether the feature is categorical or continuous

    :param data: DataFrame hosting the data in question
    :param feature: the feature in which the distribution of will be plotted
    :param individual: optional individual datapoint to be highlighted in the distribution plot
    :return: the Bokeh figure to be plotted by the Bokeh extension
    """
    if feature in CATEGORICAL_FEATURES:
        return _categorical_bar_plot(data, feature, individual)
    else:
        return _continuous_histogram(data, feature, individual)


import sys
import math
from fractions import Fraction
from typing import Dict, List

class Vector:
    def __init__(self, x: Fraction, y: Fraction):
        self.x = x
        self.y = y

    @staticmethod
    def from_str(s: str):
        x, y = s.split(',')
        return Vector(Fraction(x), Fraction(y))

    def __repr__(self):
        return "Vector(" + str(self.x) + ', ' + str(self.y) + ")"

    def __str__(self):
        return str(self.x) + ',' + str(self.y)

    def __eq__(self, other):
        return self.x == other.x and self.y == other.y

    def __lt__(self, other):
        return self.x < other.x or (self.x == other.x and self.y < other.y)

    def __hash__(self):
        return (self.x, self.y).__hash__()

class Segment:
    def __init__(self, p0: Vector, p1: Vector):
        self.p0 = p0
        self.p1 = p1

    def __repr__(self):
        return "Segment(" + repr(self.p0) + ', ' + repr(self.p1) + ")"

class Polygon:
    def __init__(self, points: List[Vector]):
        self.points = points
        self.segments = [Segment(p0, p1) for p0, p1 in zip(points, points[1:] + points[:1])]

    def __repr__(self):
        return "Polygon(" + repr(self.points) + ")"

class Problem:
    def __init__(self):
        #: :type: List[Polygon]
        self.polygons = []
        #: :type: List[Segment]
        self.segments = []

    def load(self, filename):
        with open(filename) as f:
            num_polygons = int(f.readline())
            for _ in range(num_polygons):
                num_vertices = int(f.readline())
                self.polygons.append(Polygon([Vector.from_str(f.readline()) for _ in range(num_vertices)]))
            num_segments = int(f.readline())
            for _ in range(num_segments):
                p0, p1 = (Vector.from_str(x) for x in f.readline().split(' '))
                self.segments.append(Segment(p0, p1))

def intersect(s: Segment, t: Segment):
    sx = s.p1.x - s.p0.x
    sy = s.p1.y - s.p0.y
    tx = t.p1.x - t.p0.x
    ty = t.p1.y - t.p0.y
    cross = sx * ty - sy * tx
    if cross == Fraction(0):
        return None
    ux = t.p0.x - s.p0.x
    uy = t.p0.y - s.p0.y
    a = (ux * ty - uy * tx) / cross
    b = (ux * sy - uy * sx) / cross
    if Fraction(0) < a < Fraction(1) and Fraction(0) < b < Fraction(1):
        return Vector(a * sx + s.p0.x, a * sy + s.p0.y)
    return None

def position(s: Segment, p: Vector):
    x0, y0 = s.p0.x, s.p0.y
    x1, y1 = s.p1.x, s.p1.y
    x2, y2 = p.x, p.y
    sx = x0 - x2
    tx = x1 - x2
    sy = y0 - y2
    ty = y1 - y2
    cross = sx * ty - sy * tx
    if cross != Fraction(0):
        return None
    dot = sx * tx + sy * ty
    if dot > Fraction(0):
        return None
    if x0 != x1:
        return (x2 - x0) / (x1 - x0)
    else:
        return (y2 - y0) / (y1 - y0)

class Vertices:
    def __init__(self, p: Problem):
        points = set()
        for x in p.polygons:
            for y in x.points:
                points.add(y)
        for x in p.segments:
            points.add(x.p0)
            points.add(x.p1)
            for y in p.segments:
                p2 = intersect(x, y)
                if p2 is not None:
                    points.add(p2)
        #: :type: List[Vector]
        self.v_list = sorted(points)
        #: :type: Dict[Vector, int]
        self.v_dict = {x: i for i, x in enumerate(self.v_list)}

class Graph:
    def __init__(self, p: Problem, v: Vertices):
        self.p = p
        self.v = v
        self.G = {i: set() for i in range(len(v.v_list))}
        self.e_polygon = set()
        self.area_idx_dict = {}
        self.areas = []
        self.painted = {}

    def add_segment(self, s: Segment, is_polygon: bool):
        a = []
        for k, v in enumerate(self.v.v_list):
            p = position(s, v)
            if p is not None:
                a.append((p, k))
        b = [x[1] for x in sorted(a)]
        for s, t in zip(b, b[1:]):
            self.G[s].add(t)
            self.G[t].add(s)
            if is_polygon:
                self.e_polygon.add((s, t))

    def visit(self, p, q, area_idx):
        self.area_idx_dict[(p, q)] = area_idx
        if q in self.areas[area_idx]:
            return
        self.areas[area_idx].append(q)
        # todo 有理数化
        min_c = 2
        max_c = -2
        selected_r = None
        for r in self.G[q]:
            if p != r:
                vp = self.v.v_list[p]
                vq = self.v.v_list[q]
                vr = self.v.v_list[r]
                sx = vq.x - vp.x
                sy = vq.y - vp.y
                tx = vr.x - vq.x
                ty = vr.y - vq.y
                crs = sx * ty - sy * tx
                c = (sx * tx + sy * ty) / math.sqrt(sx * sx + sy * sy) / math.sqrt(tx * tx + ty * ty)
                if crs >= Fraction(0):
                    if c < min_c:
                        min_c = c
                        selected_r = r
                elif min_c == 2:
                    if c > max_c:
                        max_c = c
                        selected_r = r
        self.visit(q, selected_r, area_idx)

    def paint(self, area_idx, is_inside):
        if area_idx in self.painted:
            assert self.painted[area_idx] == is_inside
            return
        self.painted[area_idx] = is_inside
        area = self.areas[area_idx]
        for s, t in zip(area, area[1:] + area[:1]):
            if (s, t) not in self.e_polygon and (t, s) not in self.e_polygon:
                next_area_idx = self.area_idx_dict[(t, s)]
                self.paint(next_area_idx, is_inside)

    def build_area(self):
        for x in self.p.polygons:
            for y in x.segments:
                self.add_segment(y, True)
        for x in self.p.segments:
            self.add_segment(x, False)

        for i in range(len(self.G)):
            for j in sorted(self.G[i]):
                if (i, j) not in self.area_idx_dict:
                    area_idx = len(self.areas)
                    self.areas.append([i])
                    self.visit(i, j, area_idx)

        for s, t in self.e_polygon:
            area_idx_i = self.area_idx_dict[(s, t)]
            area_idx_o = self.area_idx_dict[(t, s)]
            self.paint(area_idx_i, True)
            self.paint(area_idx_o, False)

    def print(self):
        print(len(self.v.v_list))
        for v in self.v.v_list:
            print(str(v))

        res = []
        for idx, area in enumerate(self.areas):
            if self.painted[idx]:
                res.append(' '.join(str(x) for x in area))
        print(len(res))
        for x in res:
            print(x)

def main():
    problem = Problem()
    problem.load(sys.argv[1])
    vertices = Vertices(problem)
    graph = Graph(problem, vertices)
    graph.build_area()
    graph.print()

main()