from src import basetest
from src.pipelines.stages import gcedatatransformer

class GceDataTransformerTest(basetest.TestCase):

  def testAddNamePropertyToUrlProperty(self):
    # Set up properties with two values.
    properties = {'status': 'RUNNING',
                  'machineType': 'https://www.googleapis.com/compute/v1/'
                                 'projects/google.com:cloud-gce/zones/'
                                 'europe-west1-b/machineTypes/n1-standard-4'
                 }

    # Should add a name property to machineType
    gcedatatransformer.GceDataTransformer._AddNamePropertyToUrlProperty(
        properties, 'machineType')
    self.assertIn('machineTypeName', properties)
    self.assertEqual('n1-standard-4', properties.get('machineTypeName'))
    self.assertEqual(3, len(properties))

    # Should not add a name property to status
    gcedatatransformer.GceDataTransformer._AddNamePropertyToUrlProperty(
        properties, 'status')
    self.assertNotIn('statusName', properties)
    self.assertEqual(3, len(properties))

  def testTransformInstanceDataServiceAccount(self):
    # Convert the scopes from an array to a comma separated list.
    email_scope = 'https://www.googleapis.com/auth/userinfo.email'
    compute_scope = 'https://www.googleapis.com/auth/compute'
    storage_scope = 'https://www.googleapis.com/auth/devstorage.full_control'

    instance = {'status': 'RUNNING',
                'serviceAccounts': [
                    {'scopes': [email_scope, compute_scope, storage_scope],
                     'email': '987409142022@project.gserviceaccount.com'
                    }]
               }

    gcedatatransformer.GceDataTransformer._TransformInstanceData(instance)
    self.assertEqual('%s,%s,%s' % (email_scope, compute_scope, storage_scope),
                     instance['serviceAccounts'][0]['scopes'])

    # Convert the scopes from an array to a comma separated list
    # with multiple scopes
    instance = {'status': 'RUNNING',
                'serviceAccounts': [
                    {'scopes': [email_scope, compute_scope, storage_scope],
                     'email': '888888888888@project.gserviceaccount.com'
                    },
                    {'scopes': [compute_scope],
                     'email': '999999999999@project.gserviceaccount.com'
                    }]
               }
    gcedatatransformer.GceDataTransformer._TransformInstanceData(instance)
    self.assertEqual('%s,%s,%s' % (email_scope, compute_scope, storage_scope),
                     instance['serviceAccounts'][0]['scopes'])
    self.assertEqual(compute_scope, instance['serviceAccounts'][1]['scopes'])

  def testTransformInstanceDataTagItems(self):
    # Converts the items in tags from an array to a repeated field
    instance = {'status': 'RUNNING',
                'tags': {'items': ['cassandra'],
                         'fingerprint': '4ndtHne7-vE='}
               }
    gcedatatransformer.GceDataTransformer._TransformInstanceData(instance)
    self.assertListEqual([{'item': 'cassandra'}], instance['tags']['items'])
    # Test with multiple items
    instance = {'status': 'RUNNING',
                'tags': {'items': ['cassandra', 'mongoDb'],
                         'fingerprint': '4ndtHne7-vE='}
               }
    gcedatatransformer.GceDataTransformer._TransformInstanceData(instance)
    self.assertListEqual([{'item': 'cassandra'}, {'item': 'mongoDb'}],
                         instance['tags']['items'])

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


from simulator import Simulator
import argparse
import logging

logging.basicConfig(level=logging.DEBUG,
                    filename='new.log',
                    filemode='a',
                    format='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s'
                    )

# In the future, need to generate hardware first
parser = argparse.ArgumentParser(description="simulator_config")
parser.add_argument(
    '--arch', '-a', help='hardware architecture init file', default='xc7z020')
parser.add_argument(
    '--model', '-m', help='dnn model', default='resnet18')
args = parser.parse_args()

arch_str = args.arch
arch_file_str = "archs/" + arch_str + "_8b_arch.ini"
model_str = args.model
model_file_str = "models/" + model_str + ".csv"
result_file_str = "results/" + arch_str + '_' + model_str + ".csv"

hw_simulator = Simulator(arch_file_str)

eb_list_resnet18 = [2] * 21
eb_list_mobilenetv2 = [2] * 53
eb_list_vgg16 = [2] * 16
eb_list_vitbase = [2] * 5
eb_list_resnet50 = [2] * 54
# eb_list_mobilenetv2[31] = 2

eb_list_dict = {'resnet18': eb_list_resnet18,
                'mobilenetv2': eb_list_mobilenetv2,
                'vgg16': eb_list_vgg16,
                'vitbase': eb_list_vitbase,
                'resnet50': eb_list_resnet50}

if 'baseline' in arch_str:
    # print("baseline")
    total_latency, total_latency_ms = hw_simulator.generate_stats_csv_baseline([1]*54,
                                                                               model_file_str, result_file_str, False)
elif arch_str == 'xc7z020' and model_str == 'mobilenetv2':
    total_latency, total_latency_ms = hw_simulator.generate_stats_csv_opt_mobnet(eb_list_dict[model_str],
                                                                                model_file_str, result_file_str, False)
else:
    total_latency, total_latency_ms = hw_simulator.generate_stats_csv_opt(eb_list_dict[model_str],
                                                                          model_file_str, result_file_str, False)
# print("total latency cycle is: ", total_latency)
# print("total latency ms is: ", total_latency_ms)


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

# %%
dataset = pd.read_csv('Mall_Customers.csv')
X = dataset.iloc[:,[3,4]].values

# %%
#Using the dendrogram t find the optimal number of clusters
from scipy.cluster import hierarchy as sch
dendrogram = sch.dendrogram(sch.linkage(X,method='ward'))
plt.title('Dendrogram')
plt.xlabel('Customers')
plt.ylabel('Euclidean Distance')
plt.show()

# %%
#Training the Hierarchal Clustering model on the datatset

from sklearn.cluster import AgglomerativeClustering
hc = AgglomerativeClustering(n_clusters=3,affinity='euclidean',linkage='ward')
y_hc = hc.fit_predict(X)

# %%
print(y_hc)

# %%
#visualizing the cluster

plt.scatter(X[y_hc==0,0],X[y_hc==0,1],s=100,c='red',label='Cluster1')
plt.scatter(X[y_hc==1,0],X[y_hc==1,1],s=100,c='blue',label='Cluster2')
plt.scatter(X[y_hc==2,0],X[y_hc==2,1],s=100,c='green',label='Cluster3')
#plt.scatter(X[y_hc==3,0],X[y_hc==3,1],s=100,c='cyan',label='Cluster4')
#plt.scatter(X[y_hc==4,0],X[y_hc==4,1],s=100,c='magenta',label='Cluster5')
plt.legend()
plt.show()
# %%


#Smallest multiple
#Problem 5

#2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

#What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

#Answer:
#	232792560

# A timer to performance test the solutions
from timeit import default_timer as timer
import os.path as op

# Solution 1
def div20(nbr):
    c = 0
    while True:
        if nbr % 20 == 0:
            for x in range(19, 2, -1):
                if nbr % x != 0:
                    c = 0
                    break
                else:
                    c +=1
            if c == 17:
                return nbr
        nbr += 19
t11 = timer()
print(f"The answer to Project Euler, {op.basename(__file__)[:-3]} is: " + str(div20(19)))
t12 = timer()
print("Time lapsed for solution number 1: " + str((t12-t11)*10**6) + u'\u03BC' + "s")


import numpy as np
from stabletrees import BaseLineTree,AbuTree,AbuTree, BABUTree,StabilityRegularization
from matplotlib import pyplot as plt
from sklearn.metrics import mean_squared_error, mean_poisson_deviance
from sklearn.model_selection import train_test_split,GridSearchCV
import pandas as pd
from sklearn.tree import DecisionTreeRegressor
from sklearn.tree import plot_tree
from matplotlib import pyplot as plt
from sklearn.linear_model import PoissonRegressor
n = 5000
np.random.seed(0)
X = np.random.uniform(low=0,high=4,size=(n,1))

y = np.random.poisson(lam=X[:,0]**2+ np.sin(X[:,0]),size=n)

#y = np.random.normal(loc = X.ravel()**2,scale=1,size=  n)
X1,X2,y1,y2 = train_test_split(X,y,test_size=0.5,random_state=0)

criterion = "mse"

# lm = PoissonRegressor().fit(X,y)
# t2 =BABUTree(criterion=criterion, adaptive_complexity=True,min_samples_leaf=5).fit(X1,y1)
# t1 = BaseLineTree(criterion=criterion, adaptive_complexity=True,min_samples_leaf=5).fit(X1,y1)
# t3 = StabilityRegularization(criterion=criterion, adaptive_complexity=True,min_samples_leaf=5,gamma = 0.75).fit(X1,y1)

ind = np.argsort(X, axis=0).ravel()
# pred = t1.predict(X[ind,:])
# # t1.update(X,y)

# print(y.sum())
# print(y1.sum())
# # print(mean_poisson_deviance(y,t.predict(X)))

# # print(mean_poisson_deviance(y,t1.predict(X)))
# # print(pred- pred2)

# #t1.update(X2,y2)
# #pred3 =t1.predict(X)
# t1.update(X,y)

# t3.update(X,y)
# pred4 =t1.predict(X[ind,:])
# pred2 =t2.predict(X[ind,:])
# pred6 =t3.predict(X[ind,:])
# #print(pred)
# t = BaseLineTree(criterion=criterion, adaptive_complexity=True,min_samples_leaf=5).fit(X,y)
# pred5 =t.predict(X[ind,:])
# plt.scatter(X[:,0],y, s= 10)

# plt.scatter(X[ind,0],pred,c ="black",label= "base") #first
# #plt.scatter(X,pred3,c ="orange", s = 8) #t2
# plt.scatter(X[ind,0],pred2,c ="r",label= "BABU",alpha=0.5,s = 8) #combined
# plt.scatter(X[ind,0],pred6,c ="m",label= "SL",s = 8) #combined

# #plt.plot(X[ind,:],pred5,c ="y",label= "base retrained")

# plt.legend()
# plt.show()
# print(pred-pred2)

plot_params = {"ytick.color" : "black",
          "xtick.color" : "black",
          "axes.labelcolor" : "black",
          "axes.edgecolor" : "black",
         'figure.autolayout': True,
          "font.family" : "serif",
          'text.latex.preamble': r"\usepackage{amsmath}",
          "font.serif" : ["Computer Modern Serif"]}
#fig = plt.figure(dpi=500,figsize=(11,11/1.61803398875))
fig = plt.figure(dpi=500,figsize=(6.67*1.5*2/3, 4*1.5/2))
plt.rcParams.update(plot_params),

np.random.seed(0)
plt.subplot(1,2,1)
n = 100

np.random.seed(0)
X = np.random.uniform(low=0,high=4,size=(n,1))

y = np.random.poisson(lam=X[:,0],size=n)

#y = np.random.normal(loc = X.ravel()**2,scale=1,size=  n)
X1,X2,y1,y2 = train_test_split(X,y,test_size=0.5,random_state=0)

criterion = "mse"
t = BaseLineTree(criterion="poisson", adaptive_complexity=False,max_depth=3,min_samples_leaf=5).fit(X,y)
#parameters = {'max_depth':[None, 5, 10],"min_samples_leaf": [1,5], "ccp_alpha" : [0,0.01, 0.1]} # ,
parameters = {'max_depth':[3],"min_samples_leaf": [5]} # ,
clf = GridSearchCV(DecisionTreeRegressor(random_state=0), parameters).fit(X,y)
params = clf.best_params_
from matplotlib.lines import Line2D
legend_elements = [Line2D([0], [0], color='red', ls='-',lw=2, label = "second-order approximation"),
                   Line2D([0], [0], color='orange', ls='--',lw=2, label = "exact")]
plt.scatter(X,y, s= 2)
plt.xlabel(r"$\mathbf{x}$",fontsize=10)
plt.ylabel(r"$y$",fontsize=10)
plt.plot(np.sort(X,axis=0), t.predict(np.sort(X,axis=0)),c = "r", linewidth=3, label = "Adaptive Tree Complexity")
plt.plot(np.sort(X,axis=0), clf.predict(np.sort(X,axis=0)),c = "orange",linewidth=2,linestyle='dashed', label = "exact")
plt.legend(handles=legend_elements, loc='upper left',fontsize=8)

plt.subplot(1,2,2)
n = 1000

np.random.seed(0)
X = np.random.uniform(low=0,high=4,size=(n,1))

y = np.random.poisson(lam=X[:,0],size=n)
weigths = np.random.uniform(low=0,high=1,size=(n,))
t = BaseLineTree(criterion="poisson", adaptive_complexity=False,max_depth=3,min_samples_leaf=5).fit(X,y)
#parameters = {'max_depth':[None, 5, 10],"min_samples_leaf": [1,5], "ccp_alpha" : [0,0.01, 0.1]} # ,
parameters = {'max_depth':[3],"min_samples_leaf": [5]} # ,
clf = GridSearchCV(DecisionTreeRegressor(random_state=0), parameters).fit(X,y)
params = clf.best_params_
print(params)
from matplotlib.lines import Line2D
legend_elements = [Line2D([0], [0], color='red', ls='-',lw=2, label = "second-order approximation"),
                   Line2D([0], [0], color='orange', ls='--',lw=2, label = "exact")]
plt.scatter(X,y, s= 2)
plt.xlabel(r"$\mathbf{x}$",fontsize=10)
plt.ylabel(r"$y$",fontsize=10)
plt.plot(np.sort(X,axis=0), t.predict(np.sort(X,axis=0)),c = "r", linewidth=3, label = "second-order approximation")
plt.plot(np.sort(X,axis=0), clf.predict(np.sort(X,axis=0)),c = "orange",linewidth=2,linestyle='dashed', label = "exact")
plt.legend(handles=legend_elements, loc='upper left',fontsize=8)
plt.tight_layout()
plt.savefig(f"StableTrees_examples\plots\\adaptive_vs_hyperparameters_poisson.png",bbox_inches='tight')
plt.close()


"""
플루이드: 모든 노드에서 다른 모든 노드까지 가는데 최소 비용, O(V^3) = 거치는 노드 * 시작노드 * 끝노드
다익스트라: 한 노드 -> 다른 모든 노드, O(ElgV)
= 다익스트라는 시작점이 있는데 플루이드는 모든 노드가 시작점이 될 수 있다.

모든 경우를 체크하다보니 행렬형 (간선)비용 이중 배열을 만든다.

1. 아이디어
- 모든 점 -> 모든 점 : 플로이드
- 비용 배열 INF 초기화
- 간선의 비용 대입
- 거쳐서 비용 줄어들 경우, 갱신(for문 3번)

2. 시간복잡도 : 플로이드 : O(V^3) > 1e6가능

3. 변수
- 비용 배열, int[][]

"""

import sys
input = sys.stdin.readline
INF = sys.maxsize

n = int(input()) # 도시 개수
m = int(input()) # 버스 개수
rs = [[INF] * (n+1) for _ in range(n+1)] # 거리 배열

for i in range(1, n+1): rs[i][i] = 0 # 자기 자신으로 가는 것은 0으로 고정
for i in range(m):
    a,b,c, = map(int, input().split()) # 간선 처리 # a에서 b로 c비용으로 이동
    rs[a][b] = min(rs[a][b], c) # 하나에 여러 간선이 있을 수 있으므로 최소값만 넣어준다.

for k in range(1, n+1): # 거치는 값
    for j in range(1, n+1): # 시작
        for i in range(1, n+1): # 끝
            if rs[j][i] > rs[j][k] + rs[k][i]: # 시작에서 끝으로 가는게 > 거치는 거랑 끝으로 가는 것보다 크면
                rs[j][i] = rs[j][k] + rs[k][i] # 갱신 = 거치고 가는데도 그게 더 기존 길보다 작은 경우 그걸로 교체

for j in range(1, n+1):
    for i in range(1, n+1):
        if rs[j][i] == INF: print(0, end=' ')
        else: print(rs[j][i], end=' ')
    print()

"""
5
14
1 2 2
1 3 3
1 4 1
1 5 10
2 4 2
3 4 1
3 5 1
4 5 3
3 5 10
3 1 8
1 4 2
5 1 7
3 4 2
5 2 4
=0 2 3 1 4
12 0 15 2 5
8 5 0 1 1
10 7 13 0 3
7 4 10 6 0
"""

그래프탐색/플로이드.py


class Solution:
    def numDifferentIntegers(self, word: str) -> int:

        digit_str = ""
        for c in word:
            if not c.isdigit():
                digit_str += " "
            else:
                digit_str += c

        numbers = digit_str.split()

        numbers_set = set()

        for number in numbers:
            numbers_set.add(int(number))

        return len(numbers_set)


from pprint import pprint
# 1은 이동가능, 0은 이동 불가능
n,m=map(int,input().split())
miro=[list(map(int,input()))for _ in range(n)]
visited=[[0]*6 for _ in range(n)]
dx=[0,1,-1,0]
dy=[1,0,0,-1]
cnt=0

# miro(y,x)에서 출발해서 miro(N-1,M-1)위치로 이동할때 지나는 최소의 칸수
def dfs(y,x):

    if y==n and x==m:
        return 1

    print("방문y x:",y,x)
    print("cnt",cnt)

    for i in range(4):
        nx=x+dx[i]
        ny=y+dy[i]

        if nx<0 or ny<0 or ny>n-1 or nx>m-1:
            continue

        if miro[ny][nx]==1 and visited[ny][nx]==0:
            visited[ny][nx]=2
            dfs(ny,nx)
            visited[y][x]=1

            return 1

print(dfs(0,0))

for i in visited:
    print(i)


import numpy as np

def checkVec2d(a):
    '''
    preprocess 2d-vector specifications.
    accepted inputs are scalars and sequences of length two.
    '''
    result = np.atleast_2d(a)
    assert result.ndim == 2, "too many dimensions for 2d vector: %s" % (str(result.ndim))
    assert result.shape[1] <= 2, "length of vector must be <= 2"
    if result.shape[1] == 1:
        return np.repeat(result, 2, axis=1)
    return result


import tensorflow as tf
from tensorflow.contrib.rnn import *
from tensorflow.contrib.seq2seq.python.ops import attention_wrapper as wrapper, helper, basic_decoder, decoder, beam_search_decoder
from tensorflow.python.ops import init_ops, variable_scope
from tensorflow.python.layers.core import Dense
from tensorflow.python.util import nest
from tensorflow.contrib.seq2seq import LuongAttention, BahdanauAttention
import math
from models.model_utils import simple_embed_rnn, bidirectional_embed_rnn, single_rnn_cell, MultiHeadAttention

class AttentionModel(object):

    def __init__(self,
                 hparams,
                 mode,
                 src_vocab_table,
                 tgt_vocab_table,
                 reverse_target_vocab_table=None,
                 iterator=None,
                 dtype=tf.float32
                 ):
        self.num_encoder_symbols = hparams.vocab_size_source
        self.num_decoder_symbols = hparams.vocab_size_target
        self.num_units = hparams.num_units
        self.batch_size = tf.size(iterator.source_length)
        self.num_layers_encoder = hparams.num_layers_encoder
        self.num_layers_decoder = hparams.num_layers_decoder
        self.global_step = tf.Variable(0, trainable=False, name="global_step")
        self.dtype = dtype
        self.mode = mode
        self.hparams = hparams
        self.num_residual_layers = hparams.num_residual_layers
        self.iterator = iterator
        self.src_vocab_table = src_vocab_table
        if self.mode == tf.contrib.learn.ModeKeys.TRAIN:
            self.dropout = hparams.dropout
        else:
            self.beam_width = hparams.beam_width
            self.dropout = 0
            self.tgt_vocab_table = tgt_vocab_table
            self.reverse_target_vocab_table = reverse_target_vocab_table

        sqrt3 = math.sqrt(3)  # Uniform(-sqrt(3), sqrt(3)) has variance=1.
        self.initializer = init_ops.random_uniform_initializer(-sqrt3, sqrt3)

        with tf.variable_scope("nmt") as scope:
            # hidden layer
            with tf.name_scope("hidden"):
                encoder_outputs, encoder_state = self._build_encoder()
                output_ids, outputs = self._build_decoder(encoder_outputs, encoder_state)

            self.logits = output_ids
            self.predictions = outputs
            if self.mode != tf.contrib.learn.ModeKeys.INFER:
                logits = tf.argmax(self.logits, axis=-1, output_type=tf.int32, name='logits')
                max_time = logits.shape[-1].value
                mask = tf.sequence_mask(self.iterator.target_length, max_time, dtype=tf.int32)
                self.accuracy = tf.equal(tf.to_int32(logits * mask), tf.to_int32(self.iterator.target_out * mask))
                self.accuracy = tf.reduce_mean(tf.to_float(self.accuracy))

            if self.mode == tf.contrib.learn.ModeKeys.TRAIN:
                self.learning_rate = tf.Variable(float(hparams.learning_rate), trainable=False, dtype=dtype, name="learning_rate")
                self._add_loss()
                self._add_train_op()

                tf.summary.scalar("loss", self.loss)
                tf.summary.scalar("lr", self.learning_rate)
                tf.summary.scalar("accuracy", self.accuracy)
                self.summary_merge = tf.summary.merge_all()
            else:
                self.confidence = tf.reduce_mean(tf.reduce_max(tf.nn.softmax(tf.to_float(self.predictions), axis=-1), axis=-1), axis=-1)
                if hasattr(self.iterator, 'target_out'):
                    self.target = self.reverse_target_vocab_table.lookup(tf.to_int64(self.iterator.target_out))
                if hasattr(self.iterator, 'source_text'):
                    self.source_text = self.iterator.source_text
                self.predictions = self.reverse_target_vocab_table.lookup(tf.to_int64(self.logits))

    def _build_encoder(self):
        with tf.name_scope("seq_encoder"):
            # encoder_outputs = (batch_size, num_steps, hidden_size)
            # encoder_state = (batch_size, hidden_size) * num_layers
            if self.hparams.encoder_type == 'uni':
                encoder_outputs, encoder_state = simple_embed_rnn(inputs=self.iterator.source,
                                                                  batch_size=self.batch_size,
                                                                  num_units=self.num_units,
                                                                  num_layers=self.num_layers_encoder,
                                                                  num_residual_layers=self.num_residual_layers,
                                                                  num_classes=self.num_encoder_symbols,
                                                                  sequence_length=self.iterator.source_length,
                                                                  # initializer=self.initializer,
                                                                  dropout=self.dropout,
                                                                  unit_type=self.hparams.unit_type)
            elif self.hparams.encoder_type == 'bi':
                encoder_outputs, encoder_state = bidirectional_embed_rnn(inputs=self.iterator.source,
                                                                         num_units=self.num_units,
                                                                         num_layers=self.num_layers_encoder,
                                                                         num_residual_layers=self.num_residual_layers,
                                                                         num_classes=self.num_encoder_symbols,
                                                                         sequence_length=self.iterator.source_length,
                                                                         # initializer=self.initializer,
                                                                         dropout=self.dropout,
                                                                         unit_type=self.hparams.unit_type)
            else:
                raise ValueError("Unknown encoder_type %s" % self.hparams.encoder_type)

            if self.num_layers_encoder > 1:
                encoder_state = tuple([encoder_state for _ in range(self.num_layers_encoder)])
            return encoder_outputs, encoder_state

    def _build_decoder(self, encoder_outputs, encoder_state):
        with tf.name_scope("seq_decoder"):
            batch_size = self.batch_size
            # sequence_length = tf.fill([self.batch_size], self.num_steps)
            if self.mode == tf.contrib.learn.ModeKeys.TRAIN:
                sequence_length = self.iterator.target_length
            else:
                sequence_length = self.iterator.source_length
            if (self.mode != tf.contrib.learn.ModeKeys.TRAIN) and self.beam_width > 1:
                batch_size = batch_size * self.beam_width
                encoder_outputs = beam_search_decoder.tile_batch(
                    encoder_outputs, multiplier=self.beam_width)
                encoder_state = nest.map_structure(
                    lambda s: beam_search_decoder.tile_batch(s, self.beam_width), encoder_state)
                sequence_length = beam_search_decoder.tile_batch(
                    sequence_length, multiplier=self.beam_width)

            single_cell = single_rnn_cell(self.hparams.unit_type, self.num_units, self.dropout)
            decoder_cell = MultiRNNCell([single_cell for _ in range(self.num_layers_decoder)])
            decoder_cell = InputProjectionWrapper(decoder_cell, num_proj=self.num_units)
            attention_mechanism = create_attention_mechanism(self.hparams.attention_mechanism,
                                                             self.num_units,
                                                             memory=encoder_outputs,
                                                             source_sequence_length=sequence_length)
            decoder_cell = wrapper.AttentionWrapper(decoder_cell,
                                                    attention_mechanism,
                                                    attention_layer_size=self.num_units,
                                                    output_attention=True,
                                                    alignment_history=False)

            # AttentionWrapperState의 cell_state를 encoder의 state으로 설정한다.
            initial_state = decoder_cell.zero_state(batch_size=batch_size, dtype=tf.float32)
            embeddings_decoder = tf.get_variable("embedding_decoder",
                                                 [self.num_decoder_symbols, self.num_units],
                                                 initializer=self.initializer,
                                                 dtype=tf.float32)
            output_layer = Dense(units=self.num_decoder_symbols, use_bias=True, name="output_layer")

            if self.mode == tf.contrib.learn.ModeKeys.TRAIN:
                decoder_inputs = tf.nn.embedding_lookup(embeddings_decoder, self.iterator.target_in)
                decoder_helper = helper.TrainingHelper(decoder_inputs, sequence_length=sequence_length)

                dec = basic_decoder.BasicDecoder(decoder_cell,
                                                 decoder_helper,
                                                 initial_state,
                                                 output_layer=output_layer)
                final_outputs, final_state, _ = decoder.dynamic_decode(dec)
                output_ids = final_outputs.rnn_output
                outputs = final_outputs.sample_id
            else:

                def embedding_fn(inputs):
                    return tf.nn.embedding_lookup(embeddings_decoder, inputs)

                decoding_length_factor = 2.0
                max_encoder_length = tf.reduce_max(self.iterator.source_length)
                maximum_iterations = tf.to_int32(tf.round(
                    tf.to_float(max_encoder_length) * decoding_length_factor))

                tgt_sos_id = tf.cast(self.tgt_vocab_table.lookup(tf.constant(self.hparams.sos)), tf.int32)
                tgt_eos_id = tf.cast(self.tgt_vocab_table.lookup(tf.constant(self.hparams.eos)), tf.int32)
                start_tokens = tf.fill([self.batch_size], tgt_sos_id)
                end_token = tgt_eos_id

                if self.beam_width == 1:
                    decoder_helper = helper.GreedyEmbeddingHelper(embedding=embedding_fn,
                                                                  start_tokens=start_tokens,
                                                                  end_token=end_token)
                    dec = basic_decoder.BasicDecoder(decoder_cell,
                                                     decoder_helper,
                                                     initial_state,
                                                     output_layer=output_layer)
                else:
                    dec = beam_search_decoder.BeamSearchDecoder(cell=decoder_cell,
                                                                embedding=embedding_fn,
                                                                start_tokens=start_tokens,
                                                                end_token=end_token,
                                                                initial_state=initial_state,
                                                                output_layer=output_layer,
                                                                beam_width=self.beam_width)
                final_outputs, final_state, _ = decoder.dynamic_decode(dec,
                                                                       # swap_memory=True,
                                                                       maximum_iterations=maximum_iterations)
                if self.mode == tf.contrib.learn.ModeKeys.TRAIN or self.beam_width == 1:
                    output_ids = final_outputs.sample_id
                    outputs = final_outputs.rnn_output
                else:
                    output_ids = final_outputs.predicted_ids
                    outputs = final_outputs.beam_search_decoder_output.scores

            return output_ids, outputs

    def _add_loss(self):
        with tf.variable_scope("loss"):
            labels = self.iterator.target_out
            # sparse_softmax_cross_entropy_with_logits
            crossent = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels, logits=self.logits)

            max_time = labels.shape[1].value
            target_weights = tf.sequence_mask(self.iterator.target_length, max_time, dtype=tf.float32)
            self.loss = tf.reduce_sum(crossent * target_weights) / (tf.to_float(self.batch_size * tf.reduce_mean(self.iterator.target_length)))

    def _add_train_op(self):
        with tf.variable_scope("train_op"):
            if self.hparams.optimizer == 'adam':
                optimizer = tf.train.AdamOptimizer(self.learning_rate, name="optimizer")
            elif self.hparams.optimizer == 'sgd':
                optimizer = tf.train.GradientDescentOptimizer(self.learning_rate, name="optimizer")
            else:
                raise ValueError("Unknown optimizer %s" % self.hparams.optimizer)

            # train_op = optimizer.minimize(loss)
            tvars = tf.trainable_variables()
            grads, _ = tf.clip_by_global_norm(tf.gradients(self.loss, tvars), clip_norm=5.0)
            self.learning_rate = _learning_rate_decay(self.hparams.learning_rate,
                                                      self.global_step,
                                                      self.hparams.warmup_steps,
                                                      self.hparams.final_learning_rate)
            if self.mode == tf.contrib.learn.ModeKeys.TRAIN:
                self.train_step = optimizer.apply_gradients(zip(grads, tvars), global_step=self.global_step)

    def step(self, session):
        loss, global_step, learning_rate, accuracy, summary, _ = \
            session.run([self.loss,
                         self.global_step,
                         self.learning_rate,
                         self.accuracy,
                         self.summary_merge,
                         self.train_step])
        return loss, global_step, learning_rate, accuracy, summary

    def eval(self, session):
        predictions, source, target, source_text, confidence = session.run([self.predictions,
                                                                            self.iterator.source,
                                                                            self.target,
                                                                            self.source_text,
                                                                            self.confidence])
        return predictions, source, target, source_text, confidence

    def inference(self, session):
        predictions, confidence, source = session.run([self.predictions, self.confidence, self.iterator.source])

        return predictions, confidence, source

def _learning_rate_decay(init_lr, global_step, warmup_steps, final_learning_rate=0.00001):
    step = tf.cast(global_step + 1, dtype=tf.float32)
    learing_rate = init_lr * warmup_steps ** 0.5 * tf.minimum(step * warmup_steps ** -1.5, step ** -0.5)
    return tf.maximum(learing_rate, final_learning_rate)

def create_attention_mechanism(attention_option, num_units, memory,
                               source_sequence_length):
    """Create attention mechanism based on the attention_option."""
    if attention_option == "luong":
        attention_mechanism = LuongAttention(num_units, memory, memory_sequence_length=source_sequence_length)
    elif attention_option == "scaled_luong":
        attention_mechanism = LuongAttention(num_units, memory, memory_sequence_length=source_sequence_length, scale=True)
    elif attention_option == "bahdanau":
        attention_mechanism = BahdanauAttention(num_units, memory, memory_sequence_length=source_sequence_length)
    elif attention_option == "normed_bahdanau":
        attention_mechanism = BahdanauAttention(num_units, memory, memory_sequence_length=source_sequence_length, normalize=True)
    elif attention_option == "multi_head":
        attention_mechanism = MultiHeadAttention(num_units, memory, memory_sequence_length=source_sequence_length,
                                                 num_heads=4)
    else:
        raise ValueError("Unknown attention option %s" % attention_option)

    return attention_mechanism

def _create_attention_images_summary(final_context_state):
    """create attention image and attention summary."""
    attention_images = (final_context_state.alignment_history.stack())
    # Reshape to (batch, src_seq_len, tgt_seq_len,1)
    attention_images = tf.expand_dims(tf.transpose(attention_images, [1, 2, 0]), -1)
    # Scale to range [0, 255]
    attention_images *= 255
    attention_summary = tf.summary.image("attention_images", attention_images)
    return attention_summary


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/2/15 12:18
# @Author  : Baimohan/PH
# @Site    : https://github.com/BaiMoHan
# @File    : return_self.py
# @Software: PyCharm

class ReturnSelf:
    def grow(self):
        if hasattr(self,'age'):
            self.age += 1
        else:
            self.age = 1
        return self
    # 把self作为返回指可以让代码更简洁，但可能造成实际意义的模糊

if __name__ == '__main__':
    rs = ReturnSelf()
    # 可以连续调用同一个方法
    rs.grow().grow().grow()
    print('rs:age=', rs.age)
    help(hasattr)


import numpy as np
from tensorflow import keras
from keras.models import load_model
import spectrapepper as spep

from pudu import pudu, plots
from pudu import masks as msk
from pudu import perturbation as ptn

# Scale images to the [0, 1] range
x = spep.load('examples/data/for_1d_cnn_c3.txt')
x = np.expand_dims(x, 2)
y = [3, 3, 3] # these are all class 3

# convert class vectors to binary class matrices
y = keras.utils.to_categorical(y)

# Load the model and test it
model = load_model('examples/data/1d_cnn.h5')
score = model.evaluate(x, y, verbose=0)
print("Test loss:", score[0], "| Test accuracy:", score[1])
model.summary()

### PUDU ###
# Input should be 4d: (batch * rows * columns * depth)
# This is a classification problem, thus the output should be the
# probability by category: [prob_1, prob_2, ..., prob_c]
def cnn1d_prob(X):
    X = X[0,:,:,:]
    return model.predict(np.array(X), verbose=0)[0] # verbose 0 is important!

# Dimension standardization for parameters
y0 = np.argmax(y[0])
x0 = np.expand_dims(np.expand_dims(x[0], 0), 1)

# Build `pudu`, evaluate importance, and plot
imp = pudu.pudu(x0, y0, cnn1d_prob, model)

# First we check importance
imp.importance(window=150, perturbation=ptn.Positive(delta=0.1))
plots.plot(imp.x, imp.imp, axis=None, figsize=(10, 4), cmap='cool')

# Now we explore the unit re-activations observed in layer 4 (last conv. layer)
# We only consider the top 0.25% of the values as activated with `p=0.0025`.
# Negative values indicate that less units are being re-activated.
imp.reactivations(layer=4, slope=0, p=0.0025, window=150, perturbation=ptn.Positive(delta=0.1))
plots.plot(imp.x, imp.lac, axis=None, figsize=(10, 4), cmap='cool',
            title='Nº of unit activations in layer 0')

# There could be units that activate more frequently with changes in specific areas
# We can explore that too using the information obtained from 'activations'
# and generate a visual (and/or printed) report
plots.feature_report(imp.fac, plot=True, print_report=False, show_top=10)
plots.unit_report(imp.uac, plot=True, print_report=False, show_top=10)

# We can extract more information when using several data points and `relatable`.
# This will check, after applying `reactivations` to N images, what units activate
# with what feature changes the most. This can be very computer-intensive. Here
# it only tests for 3 samples, but you will need a larger N to get significant
# results. If you can use a GPU it is highly advised to do so.
x = x.reshape(3, 1, 14730, 1) # Change the shape of the array
y = [np.argmax(i) for i in y]

imp = pudu.pudu(x, y, cnn1d_prob, model) # we need to build another pudu
imp.relatable(layer=2, slope=0, p=0.0025, window=200, perturbation=ptn.Positive(delta=0.1))
idx, cnt, cor = imp.icc # this outputs the unit index, activation counts, and position of the feature
plots.relate_report(idx, cnt, cor, plot=True, show_top=20, font_size=10, rot_ticks=45, sort=True)


from math import sqrt

import numpy as np

from w2v import *
from bias import *

def identify_gender_subspace(w2v_model):
    B=vector_between_2_unit_words(w2v_model,"男子","女子")
    num=1
    with open("sex_team.txt", encoding='utf-8') as f:
        for i in f:
            if num>1:
                try:
                    a = i.split(" ")
                    for j in range(len(a)):
                        a[j]=a[j].replace("\n","")
                    B+=vector_between_2_unit_words(w2v_model,a[0],a[1])
                except:
                    print(a[0]+" "+a[1]+"不存在")
            else:
                num+=1
    B/=num
    return unit_word(B)
    # print(B)
    # print(num)

#硬去偏_中和
#把性别中性词中和消偏
def hard_debias_neutralize(w2v_model):
    B=identify_gender_subspace(w2v_model)#求性别子空间（差平均）
    # all_db=0
    # all_db_n=0
    num=0
    # sex=equalize(w2v_model)
    with open("no_sex_final.txt", encoding='utf-8') as f:
        with open("w2v_vector_neutralize.txt", "w", encoding='utf-8') as f_n:
        # for m in f:
        #     a=m.split("、")
        # a = delete_repeat(a)
        #
        # for word in a:
            for word in f:
                word=word.replace("\n","")
                try:
                    embedding_n=unit_word(w_vertical(w2v_model.wv[word],B))

                    f_n.write(word)
                    for i in embedding_n:
                        f_n.write(" ")
                        f_n.write(str(i))
                    f_n.write("\n")
                    # db_n = np.dot(embedding_n, np.array(gender)) / (np.linalg.norm(gender) * np.linalg.norm(embedding_n))
                    #
                    # db=np.dot(embedding_n,np.array(sex))/(np.linalg.norm(sex)*np.linalg.norm(embedding_n))
                    # print(word+":"+str(db))
                    # all_db+=abs(db)
                    # all_db_n+=abs(db_n)
                    num+=1
                    if num%10000==0:
                        print(num)
                except:
                    print(word)
    # all_db/=num
    # print("均衡:"+str(all_db))
    #
    # print("中和:"+str(all_db_n/num))

    # direct_bias(w2v_model,gender)

#硬去偏_均衡
#处理性别对
def equalize(w2v_model):
    B = identify_gender_subspace(w2v_model)
    with open("sex_team.txt", encoding='utf-8') as f:
        with open("w2v_vector_equalize.txt", "w", encoding='utf-8') as f_e:
            for team in f:
                words=team.replace("\n","").split(" ")
                a=unit_word(w2v_model.wv[words[0]])
                b=unit_word(w2v_model.wv[words[1]])

                u=(a+b)/2
                v=w_vertical(u,B)
                a_e=v+unit_word(B*cos(a,B)-B*cos(u,B))*sqrt(1-(np.linalg.norm(v))*(np.linalg.norm(v)))
                b_e=v+unit_word(B*cos(b,B)-B*cos(u,B))*sqrt(1-(np.linalg.norm(v))*(np.linalg.norm(v)))
                f_e.write(words[0])
                for i in a_e:
                    f_e.write(" ")
                    f_e.write(str(i))
                f_e.write("\n")
                f_e.write(words[1])
                for i in b_e:
                    f_e.write(" ")
                    f_e.write(str(i))
                f_e.write("\n")
    # print(np.linalg.norm(a_e))
    # print(np.linalg.norm(b_e))
    # return a_e-b_e

#硬去偏文件合成
#中和+均衡+其它
def merge_dehardbias_txt(w2v_model):
    sex_team=[]
    with open("w2v_vector_hard.txt","w", encoding='utf-8') as f:
        with open("w2v_vector_neutralize.txt", encoding='utf-8') as f1:
            for i in f1:
                f.write(i)
        with open("w2v_vector_equalize.txt", encoding='utf-8') as f2:
            for i in f2:
                f.write(i)
        with open("sex_final.txt",encoding='utf-8')as f3:
            #把性别对文件中的性别对变成一个列表
            with open("sex_team.txt",encoding='utf-8')as f4:
                for i in f4:
                    words=i.replace("\n","").split(" ")
                    sex_team.append(words[0])
                    sex_team.append(words[1])
            for i in f3:
                word=i.replace("\n","")
                if word not in sex_team:
                    f.write(word)
                    word_embedding=w2v_model.wv[word]
                    for j in word_embedding:
                        f.write(" ")
                        f.write(str(j))
                    f.write("\n")


from itertools import permutations

n = int(input())
numbers = list(map(int, input().split()))
res = []

for per in permutations(numbers, n):
    tmp = 0
    for i in range(n - 1):
        tmp += abs(per[i] - per[i + 1])
    res.append(tmp)

print(max(res))


import matplotlib.pyplot as plt
import numpy as np

node_r=2
node_s=3

my_xticks = []
timestamps=[] #start "12:32:00"

for i in range(32,60):
  for j in [0,5,10,15,20,25,30,35,40,45,50,55]:
   timestamps.append("{}:{}:{}".format(12,i,j))

for i in range(0,60):
  for j in [0,5,10,15,20,25,30,35,40,45,50,55]:
   timestamps.append("{}:{}:{}".format(13,i,j))
for i in range(0,42):
  for j in [0,5,10,15,20,25,30,35,40,45,50,55]:
   timestamps.append("{}:{}:{}".format(14,i,j))

#timestamps.append("{}:{}:{}".format(14,32,0))
timestamps=timestamps[0:1440]

filename= "./trace-freq2412-T1400-r6-a7-t1-i0.008-S64-N100-3/result-{}.txt".format(node_r)
received_power=[]

f = open(filename, 'r')

lines=f.readlines()

for i in range(0,2*720):
  tmp=lines[i*100:i*100+100]
  tmp2=[]
  for j in tmp:
   tmp2.append(float(j.split()[2].split(',')[0]))
  received_power.append(sum(tmp2)/len(tmp2))
b=range(1,len(received_power)+1)

filename= "./trace-freq2412-T1400-r6-a7-t1-i0.008-S64-N100-3/noise{}.txt".format(node_r)
received_noise=[]

f = open(filename, 'r')
lines=f.readlines()
for l in lines:
  received_noise.append(l.split()[1])
received_noise=received_noise[0:1440]
print(len(timestamps))
print(len(received_noise))
print(len(received_power))

plt.figure(1)
plt.subplot(211)
plt.plot(b,received_power , 'g',linewidth=2.0)
plt.xlabel('Time format H:MM:SS')
plt.title('Node {} => Node {} '.format(node_s,node_r))
plt.ylabel('Received power (dBm)')
for i in timestamps:
  my_xticks.append(i)
plt.xticks(b[0::120], my_xticks[0::120],fontsize= 8)

plt.subplot(212)
plt.plot(b,received_noise , 'r',linewidth=2.0)
plt.ylabel('Noise level (dBm)')
plt.xlabel('Time format H:MM:SS')
for i in timestamps:
  my_xticks.append(i)
plt.xticks(b[0::120], my_xticks[0::120],fontsize= 8)
#plt.savefig("figure3.png")
plt.show()


"""
implementation of the configuration

includes:
(1) all hyper-parameters
(2) controlling units

Please read the third arg of each flag.

"""

import tensorflow as tf
import os

def to_type(data):
    if data == 'True':
        return True
    if data == 'False':
        return False
    if data.find('.') >= 0:
        return float(data)
    return int(data)

class configs(object):
    def __init__(self):
        tf.app.flags.DEFINE_integer("batch_size", 16, "batch_size per gpu")
        tf.app.flags.DEFINE_string('raw_data_path',
                                   '/home/lsb/data_cleaned/pkl/',
                                   'where to load the raw data')
        tf.app.flags.DEFINE_string('dataset_name', 'totaltext', 'dataset_name')
        tf.app.flags.DEFINE_string('SynthPath',
                                   '/media/sda/eccv2018/data/pkl/result2',
                                   'where to load the raw synthtext data')
        tf.app.flags.DEFINE_string("epochs", '150,150',
                                   "the number of epochs that synthtext and the target dataset respectively run")
        tf.app.flags.DEFINE_string('dataset_size', '1255/300/526991',
                                   'the number of samples in: training set, total set of the target dataset, and synthtexxt')  # 833019
        # tf.app.flags.DEFINE_string('tfrecord_path', 'tfrecord', 'tfrecord_path') this has been abandoned
        # tf.app.flags.DEFINE_string('weights_path', '~/pre_trained', 'dir that contained the pretrained base net weights') this is in fact not in use.
        tf.app.flags.DEFINE_string("input_size", '512*512*3', "= image_size")
        tf.app.flags.DEFINE_string("Label_size", '512*512*5', "= size of the GT labels")
        tf.app.flags.DEFINE_string("padding", 'SAME', "what padding strategy is used in conv layers")
        tf.app.flags.DEFINE_string("pooling", 'max', "what pooling strategy is used in conv layers")
        tf.app.flags.DEFINE_string("basenet", 'vgg16', "which base net to use. 'vgg16', 'vgg19', 'resnet-18, 34, 50, 101, 152'")
        tf.app.flags.DEFINE_float("learning_rate", 0.001, "the initial learning rate")    # TODO: add a new param for decaying type/ratio
        tf.app.flags.DEFINE_string("save_path", 'model_backup/', "the dir where we save the model periodically")
        tf.app.flags.DEFINE_float("momentum", 0.9, "a param for optimizer (SGD)")
        tf.app.flags.DEFINE_string("log_path", '', "where to save the log file")
        tf.app.flags.DEFINE_string("restore_path", '', "if not blank, indicating the saved model to load")
        tf.app.flags.DEFINE_string("test_path", 'test_output', "saving the test output")
        tf.app.flags.DEFINE_string('record_path',
                                   '/home/lsb/runninglog',
                                   'the dir to hold all the record/log, etc.')
        tf.app.flags.DEFINE_string('code_name', 'debug', 'the code for the experiment, only used in the name of the log file')
        # tf.app.flags.DEFINE_boolean('data_aug', True, 'data_aug')                abandoned
        # tf.app.flags.DEFINE_boolean('data_aug_noise', True, 'data_aug_noise')    abandoned
        tf.app.flags.DEFINE_string("US_Params", '3 3 2 2 same ReLU', "params for the upsampling layers: (kernel_size1, kernel_size2, stride1, stride2, padding, acti)")
        tf.app.flags.DEFINE_string("predict_channels", '128 64 32 32', "number of channels in the prediction channels")
        tf.app.flags.DEFINE_string("upsampling", 'DeConv', "upsampling type")
        tf.app.flags.DEFINE_integer("Predict_stage", 4, "the number of stages in the prediction modules")
        tf.app.flags.DEFINE_string("optimizer", 'Adam', "name of the optimizer")  # currently only supports 'Adam', 'YF'
        tf.app.flags.DEFINE_string('gpu_list', '0,4', 'which gpus to be used')  # '0,1,2,3,4,...'
        # tf.app.flags.DEFINE_boolean('multi_gpu_switch', True, 'multi_gpu_switch') abandoned
        tf.app.flags.DEFINE_boolean('real_test', False, 'NowDeprecated//True: use the original size(resized to the nearest multiples of 32) and aspect ratio to test')
        tf.app.flags.DEFINE_boolean('evaluate_before_training', False, 'True: evaluate before training')
        tf.app.flags.DEFINE_boolean('print_on_screen', False, 'True: print all the log onto the screen')
        tf.app.flags.DEFINE_boolean('use_pre_generated_data', True, 'True: use offline data')
        tf.app.flags.DEFINE_boolean('test_mode', False, 'True: do reconstruction to calculate P/R/F every test time')
        tf.app.flags.DEFINE_boolean('data_generate_mode', False, 'True: only generate offline data, do not train')
        tf.app.flags.DEFINE_integer("threads", 10, "number of threads/processes in generating/loading data")
        tf.app.flags.DEFINE_integer('QueueLength', 8, 'the max length of the queue to hold the data from data-generating processes')
        tf.app.flags.DEFINE_string('pre_labelled_data_path', '/home/lsb/pre_labelled_data', 'the dir to save the offline data')
        tf.app.flags.DEFINE_string('decay_type', 'expo',
                                   'the type of decaying learning rate')  # see Models.py --> network_multi_gpu --> _learning_rate
        tf.app.flags.DEFINE_string('decay_params', '5000/0.8/True',
                                   'the params of decaying learning rate')
        tf.app.flags.DEFINE_float("moving_average_decay", 0.99, "moving_average_decay")  # 0.99
        tf.app.flags.DEFINE_string('model', 'Base',
                                   'which mode to use')  # see Model/__init__.py Model->__init__
        tf.app.flags.DEFINE_boolean('prediction_mode', False,
                                    'make prediction')
        tf.app.flags.DEFINE_boolean('NewEval', False,
                                    'use the revised eval protocal or not')
        tf.app.flags.DEFINE_string('input_path', '',
                                   '')
        tf.app.flags.DEFINE_string('output_path', '',
                                   '')
        tf.app.flags.DEFINE_string('suffix', 'jpg',
                                   '')

        self.FLAGS = tf.app.flags.FLAGS
        self._read()

    def _read(self):
        self.record_path = self.FLAGS.record_path
        self.SynthPath = self.FLAGS.SynthPath
        self.dataset_name = self.FLAGS.dataset_name
        self.raw_data_path = self.FLAGS.raw_data_path
        self.epochs = list(map(int, self.FLAGS.epochs.split(',')))
        self.epochs = {'Synth': self.epochs[0], 'train': self.epochs[1]}
        self.input_size = self.image_size = tuple(map(int, self.FLAGS.input_size.split('*')))
        self.Label_size = tuple(map(int, self.FLAGS.Label_size.split('*')))
        self.padding = self.FLAGS.padding
        self.basenet = self.FLAGS.basenet
        self.pooling = self.FLAGS.pooling
        self.learning_rate = self.FLAGS.learning_rate
        self.momentum = self.FLAGS.momentum
        self.dataset_size = list(map(int, self.FLAGS.dataset_size.split('/')))
        self.size = {'train': self.dataset_size[0], 'test': self.dataset_size[1], "Synth": self.dataset_size[2]}
        self.save_path = os.path.join(self.record_path, self.FLAGS.save_path)
        self.log_path = os.path.join(self.record_path, self.FLAGS.log_path)
        self.restore_path = self.FLAGS.restore_path
        self.summary_path_directory = os.path.join('/media/sda/eccv2018/Sum', self.record_path.split('/')[-1])
        self.test_path = os.path.join(self.record_path, self.FLAGS.test_path)
        self.code_name = self.FLAGS.code_name
        self.US_Params = self.FLAGS.US_Params
        self.upsampling = self.FLAGS.upsampling
        self.predict_channels = list(map(int, self.FLAGS.predict_channels.split()))
        self.optimizer = self.FLAGS.optimizer
        self.Predict_stage = self.FLAGS.Predict_stage
        self.gpu_list = self.FLAGS.gpu_list.split(',')
        self.real_test = self.FLAGS.real_test
        self.NewEval = self.FLAGS.NewEval
        self.print_on_screen = self.FLAGS.print_on_screen
        self.threads = self.FLAGS.threads
        self.evaluate_before_training = self.FLAGS.evaluate_before_training
        self.QueueLength = self.FLAGS.QueueLength
        self.pre_labelled_data_path = self.FLAGS.pre_labelled_data_path
        self.use_pre_generated_data = self.FLAGS.use_pre_generated_data
        self.test_mode = self.FLAGS.test_mode
        self.data_generate_mode = self.FLAGS.data_generate_mode
        self.decay_type = self.FLAGS.decay_type
        self.decay_params = list(map(to_type, self.FLAGS.decay_params.split('/')))
        self.moving_average_decay = self.FLAGS.moving_average_decay
        self.model = self.FLAGS.model
        self.prediction_mode = self.FLAGS.prediction_mode
        self.input_path = self.FLAGS.input_path
        self.output_path = self.FLAGS.output_path
        if self.prediction_mode and (not os.path.isdir(self.output_path)):
            os.mkdir(self.output_path)
        self.suffix = self.FLAGS.suffix
        if self.prediction_mode and ((not os.path.isdir(self.input_path))or(not os.path.isdir(self.output_path))):
            raise ValueError('You should assign a input/output path.')
        os.environ["CUDA_VISIBLE_DEVICES"] = self.FLAGS.gpu_list
        self.batch_size = self.FLAGS.batch_size * len(self.gpu_list)
        for path in [self.record_path, self.save_path, self.log_path, self.summary_path_directory,
                     self.test_path]:
            if not os.path.isdir(path):
                os.makedirs(path)


import os
from gettext import gettext as _

from .app_dirs_with_mkdir import AppDirsWithMkdir
from .exists_or_mkdirs import must_ensure_directory_exists

__all__ = ("must_ensure_appdirs_path",)

DEFAULT_APPDIRS_FILE_BASENAME_FMT = "{}"

def must_ensure_appdirs_path(
    file_basename,
    dir_dirname,
    appdirs_dir=None,
    basename_fmt=DEFAULT_APPDIRS_FILE_BASENAME_FMT,
):
    """Return the path to a file stored in a subdirectory of an AppDirs directory."""

    def _must_ensure_appdirs_path():
        app_dir_path = must_use_user_cache_dir_unless_appdirs_dir()
        full_path = expand_path_and_ensure_parents_exist(app_dir_path)
        must_verify_path_does_not_exist_or_is_a_file(full_path)
        return full_path

    def must_use_user_cache_dir_unless_appdirs_dir():
        # Returns the app_dir caller specified, or AppDirs.user_cache_dir.
        if appdirs_dir:
            return appdirs_dir

        return must_use_user_cache_dir()

    def must_use_user_cache_dir():
        # Repackages mkdir side effect exception if raises, and re-raises;
        # otherwise returns fallback app. dir. for path, `user_cache_dir`.
        try:
            return AppDirsWithMkdir().user_cache_dir
        except Exception as err:
            exceptional_path = AppDirsWithMkdir().safe.user_cache_dir
            msg = _(
                "{}: {} ‘{}’ {} “{}” ({})",
            ).format(
                _("ERROR"),
                _("Failed to create"),
                "user_cache_dir",
                _("path at"),
                exceptional_path,
                str(err),
            )
            raise Exception(msg)

    def expand_path_and_ensure_parents_exist(app_dir_path):
        subdir_path = os.path.join(app_dir_path, dir_dirname)
        # This must() raises, e.g., PermissionError, if os.makedirs fails.
        must_ensure_directory_exists(subdir_path)
        full_path = os.path.join(subdir_path, basename_fmt.format(file_basename))
        return full_path

    def must_verify_path_does_not_exist_or_is_a_file(full_path):
        if not os.path.exists(full_path) or os.path.isfile(full_path):
            return

        msg = _(
            "{}: {} ‘{}’ {} “{}”",
        ).format(
            _("ERROR"),
            _("Path exists for"),
            "user_cache_dir",
            _("but it‘s not a file"),
            full_path,
        )
        raise Exception(msg)

    return _must_ensure_appdirs_path()


# writing to csv files
# can use lists of dicts to write to csv
# writer - creates a write object for writing to csv
# writerow - method on writer to write a row to the CSV

from csv import writer, DictWriter, DictReader
# with open('CSV/cats.csv', 'w') as f:
#     csv_writer = writer(f)
#     csv_writer.writerow(["Name", "Age"])
#     csv_writer.writerow(["Blue","3"])
#     csv_writer.writerow(["Sam", "4"])

# writing to CSV files using dictionaries
with open('CSV/cats.csv', 'w') as f:
    headers = ["Name", "Breed", "Age"]
    csv_writer = DictWriter(f, fieldnames=headers)
    csv_writer.writeheader()
    csv_writer.writerow({
        "Name": "Garfield",
        "Breed": "Tabby",
        "Age": 10
    })

# using dictwriter and dictreader
# convert cm in csv to inches
def cm_to_in(cm):
    return float(cm) * 0.393701

with open('CSV/fighters.csv') as f:
    csv_reader = DictReader(f)
    fighters = list(csv_reader)

with open('CSV/inches_fighters.csv', 'w') as f:
    headers = ["Name", "Country", "Height"]
    csv_writer = DictWriter(f, fieldnames=headers)
    csv_writer.writeheader()
    for fighter in fighters:
        csv_writer.writerow({
            "Name": fighter["Name"],
            "Country":fighter["Country"],
            "Height": cm_to_in(fighter["Height (in cm)"])
        })


##
import nltk
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from nltk import pos_tag
from nltk.corpus import wordnet as wn
from nltk.metrics import jaccard_distance
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import AdaBoostRegressor, GradientBoostingRegressor
from sklearn.svm import SVR
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn import model_selection

from scipy.stats import pearsonr
from nltk.tag.perceptron import PerceptronTagger
from nltk import download
from nltk.corpus import treebank
from xgboost import XGBRegressor

import re
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
download('treebank')

stop_words = stopwords.words('english')
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)
wnl = WordNetLemmatizer()
filenames_train = ['MSRpar', 'MSRvid', 'SMTeuroparl']
filenames_test = ['MSRpar', 'MSRvid', 'SMTeuroparl',
                  'surprise.OnWN', 'surprise.SMTnews']
train_data = treebank.tagged_sents()

# Perceptron tagger
per = PerceptronTagger(load='false')
per.train(train_data)

file_prefix = lambda stage: stage + '/STS'

""" more orthodox and robust implementation """
def dice_coefficient(s1, s2, n=2):
    """dice coefficient 2nt/na + nb."""
    if isinstance((s1), list):
        a = ' '.join(s1)
    elif isinstance(s1, str):
        a = s1
    if isinstance((s2), list):
        b = ' '.join(s2)
    elif isinstance(s2, str):
        b = s2
    if not len(a) or not len(b): return 0.0
    if len(a) == 1:  a = a + u'.'
    if len(b) == 1:  b = b + u'.'

    a_bigram_list = []
    for i in range(len(a) - 1):
        a_bigram_list.append(a[i:i + n])
    b_bigram_list = []
    for i in range(len(b) - 1):
        b_bigram_list.append(b[i:i + n])

    a_bigrams = set(a_bigram_list)
    b_bigrams = set(b_bigram_list)
    overlap = len(a_bigrams & b_bigrams)
    dice_coeff = overlap * 2.0 / (len(a_bigrams) + len(b_bigrams))
    return dice_coeff

def getData(filenames, file_prefix, **removals):
    input_prefix = file_prefix + '.input.'
    gs_prefix = file_prefix + '.gs.'
    input = pd.DataFrame()
    for filename in filenames:
        sentences = pd.read_csv(input_prefix + filename + '.txt',
                                sep='\t', names=['sentence1', 'sentence2'],
                                quoting=3)
        golden_standards = pd.read_csv(gs_prefix + filename + '.txt',
                                       names=['golden_standard'])
        dfX = pd.concat([sentences, golden_standards], axis=1)
        input = pd.concat([input, dfX])

    input.reset_index(drop=True)
    # Remove punctuation (and numbers)
    input[['sentence1', 'sentence2']] = input[['sentence1', 'sentence2']].apply(lambda col: sentClean(col, **removals))
    # Tokenize (remove stop words)
    input[['sentence1', 'sentence2']] = input[['sentence1', 'sentence2']].apply(lambda col: sentTokenize(col, **removals))
    return input

def sentClean(sents, **removals):
    new_sent = []
    for sent in sents:
        if sent is np.NaN:
            pass#print(sent)
        if removals.get('numbers'):
            mod_sent = re.sub(r'[^A-Z a-z]', ' ', sent)
        else:
            mod_sent = re.sub(r'[^\w]', ' ', sent)  # |\b\w\b -> to take out single chars
        clean_sent = re.sub(r'[ ]+', ' ', mod_sent.strip())
        if len(clean_sent) == 0:
            clean_sent = sent
        new_sent.append(clean_sent)
    return new_sent

def sentTokenize(sents, **removals):
    new_sent = []
    for sent in sents:
        tokens = nltk.word_tokenize(sent)
        if removals.get('stop_words'):
            tokens = [token for token in tokens if token not in stop_words]
        new_sent.append(tokens)
    return new_sent

def lemmatize(p):
    if p[1][0] in {'N','V'}:
        return wnl.lemmatize(p[0].lower(), pos=p[1][0].lower())
    return p[0].lower()

def lemmas(sents):
    new_sent = []
    for sent in sents:
        #pairs = pos_tag(sent)
        pairs = per.tag(sent)
        new_sent.append([lemmatize(pair) for pair in pairs])
    return new_sent

def ngrams(sents, n, word=True):
    new_sent = []
    n_old = n
    for sent in sents:
        n = n_old
        if len(sent) < n:
            n_old = n
            n = len(sent)
        if word:
            grams_lst = [w for w in nltk.ngrams(sent, n)]
            new_sent.append(grams_lst)
        else:
            sent_joined = ' '.join(sent)
            grams_lst = [''.join(ch) for ch in nltk.ngrams(sent_joined, n)]
            new_sent.append(grams_lst)
    return new_sent

def mylesk(context_sentence, ambiguous_word, pos=None, synsets=None):
    context = set(context_sentence)
    # context = ' '.join(context_sentence)
    hyper = lambda s: s.hypernyms()
    if synsets is None:
        synsets = wn.synsets(ambiguous_word)

    if pos:
        synsets = [ss for ss in synsets if str(ss.pos()) == pos]

    if not synsets:
        return ambiguous_word

    max_sense = []
    for ss in synsets:
        ss_lst = list(ss.closure(hyper))
        ss_lst.append(ss)
        for ss_hyper in ss_lst:
            max_sense.append(
                (len(context.intersection(ss_hyper.definition().split())), ss_hyper, ss)
                # (self.dice_coefficient(context, ss_hyper.definition()), ss_hyper, ss)
            )
            for ex in ss_hyper.examples():
                if ex:
                    max_sense.append(
                        (len(context.intersection(ex.split())), ss_hyper, ss)
                        # (self.dice_coefficient(context, ex), ss_hyper, ss)
                    )
    _, hyper, sense = max(max_sense)
    return hyper.lemmas()[0].name()

def myWSD(pt_pair, context):
    '''
    Returns the synset if it's a noun, a verb, an adberv or an adjective or the lowered word
    '''
    wn_postag_map = {'N': 'n',
             'V': 'v',
             'J': 'a',
             'R': 'r'
            }

    pt = wn_postag_map.get(pt_pair[1][0])
    word = pt_pair[0]
    if pt:
        return mylesk(context, word, pt)
    else:
        return word

def lesk(sents):
    new_sent = []
    for sent in sents:
        pairs = pos_tag(sent)
        new_sent.append([myWSD(pair,sent) for pair in pairs])
    return new_sent

def modelFeatures(df_sents, files='', stage=''):
    Xlesk = df_sents.apply(lesk)

    Mdice_lem = df_sents.apply(lambda x: dice_coefficient(x['sentence1'], x['sentence2']), axis=1)

    Mjac_lem = df_sents.apply(lambda x: jaccard_distance(set(x['sentence1']), set(x['sentence2'])), axis=1)

    #Mlesk = Xlesk.apply(lambda x: jaccard_distance(set(x['sentence1']), set(x['sentence2'])), axis=1)
    Mlesk = Xlesk.apply(lambda x: dice_coefficient(x['sentence1'], x['sentence2']), axis=1)

    Xngram = df_sents.apply(lambda col: ngrams(col, 2))
    Mngram2 = Xngram.apply(lambda x: jaccard_distance(set(x['sentence1']), set(x['sentence2'])), axis=1)

    Xngram = df_sents.apply(lambda col: ngrams(col, 4))
    Mngram4 = Xngram.apply(lambda x: jaccard_distance(set(x['sentence1']), set(x['sentence2'])), axis=1)

    removals = {
        'stop_words': True,
        'numbers': True
    }
    df_XtrainSW = getData(files, file_prefix(stage), **removals)
    #labels_trn = df_Xtrain.iloc[:, -1]
    df_XtrainSW = df_XtrainSW.drop(df_XtrainSW.columns[len(df_XtrainSW.columns) - 1], axis=1)

    Xlem = df_XtrainSW.apply(lemmas)

    Xngram = Xlem.apply(lambda col: ngrams(col, 1))
    Mngram1_sw = Xngram.apply(lambda x: jaccard_distance(set(x['sentence1']), set(x['sentence2'])), axis=1)

    Xngram = Xlem.apply(lambda col: ngrams(col, 3))
    Mngram2_sw = Xngram.apply(lambda x: jaccard_distance(set(x['sentence1']), set(x['sentence2'])), axis=1)

    df = pd.concat([Mdice_lem, Mjac_lem, Mlesk, Mngram2, Mngram4, Mngram1_sw, Mngram2_sw], axis=1)
    #df = pd.concat([Mdice_lem, Mjac_lem, Mlesk, Mngram2, Mngram4], axis=1)
    return df

def fit_model2(X):
    bow = CountVectorizer(lowercase=False,
                          analyzer=lambda x: x)
    join_X = X['sentence1'] + X['sentence2']
    bow_Xtrn = bow.fit_transform(join_X)
    sents_tfidf = TfidfTransformer()
    tfidf_Xtrn = sents_tfidf.fit_transform(bow_Xtrn)
    return bow, sents_tfidf, tfidf_Xtrn
#input_prefix = lambda stage:  file_prefix(stage) + '.input.'
#gs_prefix = lambda stage: file_prefix(stage) + '.gs.'

removals = {
            'stop_words': False,
            'numbers': True
            }
# Tokenized raw train data
df_Xtrain = getData(filenames_train, file_prefix('train'), **removals)
#df_Xtrain.sample(frac=1)
labels_trn = df_Xtrain.iloc[:, -1]
df_Xtrain = df_Xtrain.drop(df_Xtrain.columns[len(df_Xtrain.columns) - 1], axis=1)

# Tokenized raw test data
df_Xtest = getData(filenames_test, file_prefix('test'), **removals)
labels_tst = df_Xtest.iloc[:, -1]
df_Xtest = df_Xtest.drop(df_Xtest.columns[len(df_Xtest.columns) - 1], axis=1)

'''
Xtrn, Xval, ytrn, yval = train_test_split(
                                        df_Xtrain[['sentence1', 'sentence2']],
                                        df_Xtrain['golden_standard'],
                                        test_size=0.4)
                                        #random_state=0)
'''
Xlem = df_Xtrain.apply(lemmas)
Xlem_tst = df_Xtest.apply(lemmas)
##
#print(Xlem.head())
#Xngram = Xlem.apply(lambda col: ngrams(col, n))

transformed_train = modelFeatures(Xlem, filenames_train, 'train')
#reg = LinearRegression().fit(transformed_train, labels_trn)
#reg_model = AdaBoostRegressor().fit(transformed_train, labels_trn)
reg_model = AdaBoostRegressor()
kfold = model_selection.KFold(n_splits=10)
results_model1 = model_selection.cross_val_predict(reg_model, transformed_train, labels_trn, cv=kfold)

transformed_test = modelFeatures(Xlem_tst, filenames_test, 'test')
pred_model = reg_model.fit(transformed_train, labels_trn).predict(transformed_test)
print(pearsonr(pred_model.T.tolist(), labels_tst.tolist())[0])

#bow = CountVectorizer(lowercase=False,
#                      analyzer=lambda x: x)
#join_Xlem = Xlem['sentence1'] + Xlem['sentence2']
#bow_Xtrn = bow.fit_transform(join_Xlem)
#sents_tfidf = TfidfTransformer()
#tfidf_Xtrn = sents_tfidf.fit_transform(bow_Xtrn)
#reg_bow = AdaBoostRegressor().fit(tfidf_Xtrn, labels_trn)

bow, sents_tfidf, tfidf_Xtrn = fit_model2(Xlem)
reg_bow = GradientBoostingRegressor()#.fit(tfidf_Xtrn, labels_trn)

results_model2 = model_selection.cross_val_predict(reg_bow, tfidf_Xtrn, labels_trn, cv=kfold)
join_Xlem_tst = Xlem_tst['sentence1'] + Xlem_tst['sentence2']
bow_Xtst = bow.transform(join_Xlem_tst)
tfidf_Xtst = sents_tfidf.transform(bow_Xtst)
pred_bow = reg_bow.fit(tfidf_Xtrn, labels_trn).predict(tfidf_Xtst)
print(pearsonr(pred_bow.T.tolist(), labels_tst.tolist())[0])
##
#pred_model_trn = reg_model.predict(transformed_train)

#pred_bow_trn = reg_bow.predict(tfidf_Xtrn)

# ensemble
reg_final = XGBRegressor().fit(np.concatenate([results_model1.reshape(-1,1), results_model2.reshape(-1,1)], axis=1), labels_trn)
pred_final = reg_final.predict(np.concatenate([pred_model.reshape(-1,1), pred_bow.reshape(-1,1)], axis=1))
print(pearsonr(pred_final.T.tolist(), labels_tst.tolist())[0])


import uvicorn
from fastapi import Depends, FastAPI, HTTPException
from sqlalchemy.engine import CursorResult
from sqlalchemy.ext.asyncio.engine import AsyncConnection
from sqlalchemy.schema import CreateTable

import domains
import tables
from conf.settings import settings
from dependencies import engine_begin, engine_connect

app = FastAPI()

@app.on_event("startup")
async def startup():
    print("on startup")

@app.on_event("shutdown")
async def shutdown():
    print("on shutdown")

@app.get("/init")
async def create_database(conn: AsyncConnection = Depends(engine_begin)):
    # tables.metadata.create_all(engien) - does not work asynchronously!
    # Inspection on an AsyncEngine is currently not supported.
    for table in tables.all:
        await conn.execute(CreateTable(table))
    return {"Database": "Initialized"}

@app.get("/")
async def read_all(conn: AsyncConnection = Depends(engine_connect)):
    cr: CursorResult = await conn.execute(tables.todos.select())
    # Use CursorResult if you need iterate data.
    """
    for row in cr:
        print(row)
    """
    # Use fetchall() if you need to return all
    return cr.fetchall()

@app.post("/")
async def create_todo(
    todo: domains.Todo, conn: AsyncConnection = Depends(engine_begin)
):
    await conn.execute(
        tables.todos.insert().values(
            title=todo.title,
            description=todo.description,
            priority=todo.priority,
            complete=todo.complete,
        )
    )
    return successful_response(201)

@app.get("/todo/{todo_id}")
async def read_todo(todo_id: int, conn: AsyncConnection = Depends(engine_connect)):
    todo_model = await conn.execute(
        tables.todos.select().where(tables.todos.c.id == todo_id)
    ).first()

    if todo_model:
        return todo_model

    raise http_exception()

@app.put("/{todo_id}")
async def update_todo(
    todo_id: int, todo: domains.Todo, conn: AsyncConnection = Depends(engine_begin)
):
    todo_model = await conn.execute(
        tables.todos.select().where(tables.todos.c.id == todo_id)
    ).first()

    if todo_model:
        await conn.execute(
            tables.todos.update()
            .where(tables.todos.c.id == todo_id)
            .values(
                title=todo.title,
                description=todo.description,
                priority=todo.priority,
                complete=todo.complete,
            )
        )
        return successful_response(200)

    raise http_exception()

@app.delete("/{todo_id}")
async def delete_todo(todo_id: int, conn: AsyncConnection = Depends(engine_begin)):
    todo_model = await conn.execute(
        tables.todos.select().where(tables.todos.c.id == todo_id)
    ).first()

    if todo_model:
        await conn.execute(tables.todos.delete().where(tables.todos.c.id == todo_id))

        return successful_response(200)

    raise http_exception()

def successful_response(status_code: int):
    return {"status": status_code, "transaction": "Successful"}

def http_exception():
    return HTTPException(status_code=404, detail="Todo not found")

if __name__ == "__main__":
    uvicorn.run(
        "main:app",
        host=settings["HOST"],
        port=settings["PORT"],
        debug=settings["DEBUG"],
        reload=settings["RELOAD"],
    )


from django.urls import include, path
from django.contrib.auth import views as auth_views

from . import views

urlpatterns = [
    path('', views.home, name="home"),
    path('dashboard', views.home),
    path('logout', views.logout),
    path('api/activities', views.seeActivities.as_view()),
    path('api/emotions', views.seeEmotions.as_view()),
    path('api/friends', views.seeFriends.as_view())
]


import datetime, pytz
from datetime import datetime
from django.db.models import Sum, OuterRef, Subquery, Func, Count, F

from rest_framework import status, filters
from rest_framework.permissions import IsAuthenticated
from rest_framework.views import APIView
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView

from books.models import Book
from helpers.csv_reader import CSVReader
from utils.permissions import CustomBasePermission

from .paginations import BuybackPagination
from .models import Buyback, BuybackOrder
from .serializers import BuybackOrderSerializer

class ListCreateBuybackAPIView(ListCreateAPIView):
    permission_classes = [CustomBasePermission]
    serializer_class = BuybackOrderSerializer
    queryset = BuybackOrder.objects.all()
    pagination_class = BuybackPagination
    filter_backends = [filters.OrderingFilter, filters.SearchFilter]
    ordering_fields = '__all__'
    ordering = ['-date']

    def paginate_queryset(self, queryset):
        if 'no_pagination' in self.request.query_params:
            return None
        else:
            return super().paginate_queryset(queryset)

    def create(self, request, *args, **kwargs):
        # Add User to BuybackOrder
        request.data['user'] = request.user.id

        serializer = BuybackOrderSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        saved_buyback_order = serializer.save()

        response_data = serializer.data
        response_data['id'] = saved_buyback_order.id
        return Response(response_data, status=status.HTTP_201_CREATED)

    def get_queryset(self):
        default_query_set = BuybackOrder.objects.all()

        # Create a subquery to aggregate the 'revenue' value for each buyback in BuybackOrder
        revenue_subquery = Buyback.objects.filter(buyback_order=OuterRef('id')).values_list(Func(
            'revenue',
            function='SUM',
        ),)

        default_query_set = default_query_set.annotate(total_revenue=Subquery(revenue_subquery))

        # Filter by quantity of books in BuybackOrder
        num_books_subquery = Buyback.objects.filter(buyback_order=OuterRef('id')).values_list(Func('quantity', function='SUM'),)

        default_query_set = default_query_set.annotate(num_books=Subquery(num_books_subquery))

        default_query_set = default_query_set.annotate(num_unique_books=Count('buybacks__book', distinct=True))

        default_query_set = default_query_set.annotate(vendor_name=F('vendor__name'))

        default_query_set = default_query_set.annotate(username=F('user__username'))

        # Filter by vendor
        vendor = self.request.GET.get('vendor')
        if vendor is not None:
            default_query_set = default_query_set.filter(vendor=vendor)

        # Filter by date
        start_date = self.request.GET.get('start')
        end_date = self.request.GET.get('end')
        if start_date is not None and end_date is not None:
            default_query_set = default_query_set.filter(date__range=(start_date, end_date))
        elif start_date is not None:
            default_query_set = default_query_set.filter(date__range=(start_date, datetime.datetime.now(pytz.timezone('US/Eastern'))))

        # Filter by book
        book = self.request.GET.get('book')
        if book is not None:
            default_query_set = default_query_set.filter(buybacks__book=book).distinct()

        # Filter by >= revenue
        buyback_revenue__gte = self.request.GET.get('buyback_revenue__gte')
        if buyback_revenue__gte is not None:
            default_query_set = default_query_set.filter(buybacks__revenue__gte=buyback_revenue__gte).distinct()

        # Filter by <= revenue
        buyback_revenue__lte = self.request.GET.get('buyback_revenue__lte')
        if buyback_revenue__lte is not None:
            default_query_set = default_query_set.filter(buybacks__revenue__lte=buyback_revenue__lte).distinct()

        # Filter by <= revenue
        buyback_revenue = self.request.GET.get('buyback_revenue')
        if buyback_revenue is not None:
            default_query_set = default_query_set.filter(buybacks__revenue=buyback_revenue).distinct()

        # Filter by >= quantity
        buyback_quantity__gte = self.request.GET.get('buyback_quantity__gte')
        if buyback_quantity__gte is not None:
            default_query_set = default_query_set.filter(buybacks__quantity__gte=buyback_quantity__gte).distinct()

        # Filter by <= quantity
        buyback_quantity__lte = self.request.GET.get('buyback_quantity__lte')
        if buyback_quantity__lte is not None:
            default_query_set = default_query_set.filter(buybacks__quantity__lte=buyback_quantity__lte).distinct()

        # Filter by == quantity
        buyback_quantity = self.request.GET.get('buyback_quantity')
        if buyback_quantity is not None:
            default_query_set = default_query_set.filter(buybacks__quantity=buyback_quantity).distinct()

        # Filter by >= unit_buyback_price
        buyback_unit_buyback_price__gte = self.request.GET.get('buyback_unit_buyback_price__gte')
        if buyback_unit_buyback_price__gte is not None:
            default_query_set = default_query_set.filter(buybacks__unit_buyback_price__gte=buyback_unit_buyback_price__gte).distinct()

        # Filter by <= unit_buyback_price
        buyback_unit_buyback_price__lte = self.request.GET.get('buyback_unit_buyback_price__lte')
        if buyback_unit_buyback_price__lte is not None:
            default_query_set = default_query_set.filter(buybacks__unit_buyback_price__lte=buyback_unit_buyback_price__lte).distinct()

        # Filter by == unit_buyback_price
        buyback_unit_buyback_price = self.request.GET.get('buyback_unit_buyback_price')
        if buyback_unit_buyback_price is not None:
            default_query_set = default_query_set.filter(buybacks__unit_buyback_price=buyback_unit_buyback_price).distinct()

        return default_query_set

class RetrieveUpdateDestroyBuybackAPIView(RetrieveUpdateDestroyAPIView):
    permission_classes = [CustomBasePermission]
    serializer_class = BuybackOrderSerializer
    lookup_field = 'id'
    pagination_class = BuybackPagination

    def get_queryset(self):
        return BuybackOrder.objects.filter(id=self.kwargs['id'])

    def retrieve(self, request, *args, **kwargs):
        invalid_id_response = self.verify_existance()
        if invalid_id_response:
            return invalid_id_response
        (buyback_order,) = self.get_queryset()
        serializer = self.get_serializer(buyback_order)
        return Response(serializer.data, status=status.HTTP_200_OK)

    def update(self, request, *args, **kwargs):
        invalid_id_response = self.verify_existance()
        if invalid_id_response:
            return invalid_id_response
        partial = kwargs.pop('partial', False)
        (buyback_order,) = self.get_queryset()
        serializer = self.get_serializer(buyback_order, data=request.data, partial=partial)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data, status=status.HTTP_200_OK)

    def verify_existance(self):
        if (len(self.get_queryset()) == 0):
            return Response({"id": "No buyback order with queried id."}, status=status.HTTP_400_BAD_REQUEST)
        return None

    def destroy(self, request, *args, **kwargs):
        buyback_book_quantities = Buyback.objects.filter(buyback_order=self.get_object().id).values('book').annotate(num_books=Sum('quantity')).values('book', 'num_books')
        for buyback_book_quantity in buyback_book_quantities:
            book_to_remove_buyback = Book.objects.filter(id=buyback_book_quantity['book']).get()
            book_to_remove_buyback.stock += buyback_book_quantity['num_books']
            book_to_remove_buyback.save()
        return super().destroy(request, *args, **kwargs)

class CSVBuybackAPIView(APIView):
    permission_classes = [CustomBasePermission]

    def post(self, request: Request):
        csv_reader = CSVReader("buybacks")
        return csv_reader.read_csv(request)


import errno
import os
import math
import subprocess

IMAGE_DIR = 'images'
FONT_DIR = 'fonts'
SPRITESHEET_DIR = 'spritesheets'
TILESET_DIR = 'tilesets'
TILEMAP_DIR = 'tilemaps'

TEXTURE_PACKER_PATH = '/usr/local/bin/TexturePacker'
TILED_PATH = '/Users/jbramlett/Documents/GitHub/tiled/build/bin/Tiled.app/Contents/MacOS/Tiled'
ASEPRITE_PATH = '/Applications/Aseprite.app/Contents/MacOS/aseprite'

# Attempt to create directory if it doesn't exist
def try_mkdir(path):
    try:
        os.makedirs(path)
    except OSError as err:
        if err.errno != errno.EEXIST:
            return False
    return True

def try_delete_file(path):
    print('Deleting file %s' % path)

    if not os.path.isfile(path):
        raise Exception('Invalid parameter')

    i = 0
    max_iterations = 10
    while True:
        try:
            os.remove(path)
            return
        except:
            print('Failed to delete file.  Attempt %d/%d' % (i + 1), max_iterations)
            time.sleep(1)
            i += 1
            if i >= max_iterations:
                raise Exception('Unable to delete file')

# Tiled exporting has the behavior to update the paths of all dependencies
# in the file based on the relative paths of the source and destination.
# To keep these unchanged, we invoke the call from the source directory,
# and manually move the file to it's proper destination.
def invoke_tiled_export(command, source_file, dest_file):
    cwd = os.getcwd()
    os.chdir(os.path.dirname(source_file))

    cmd = TILED_PATH
    cmd += ' ' + command
    cmd += ' ' + os.path.basename(source_file)
    cmd += ' ' + os.path.basename(dest_file)

    code = subprocess.call(cmd, shell=True)
    if code is not 0:
        raise Exception('Tiled export failed. code=%d' % code)

    os.rename(os.path.basename(dest_file), dest_file)
    os.chdir(cwd)

# Verify expected formats
def verify_properties(parent):
    if 'properties' in parent:
        for prop in parent['properties']:
            if 'name' not in prop:
                raise Exception('Invalid property format. missing "name" property')
            elif 'type' not in prop:
                raise Exception('Invalid property format. missing "type" property')
            elif 'value' not in prop:
                raise Exception('Invalid property format. missing "value" property')

def rotate_point(p, angle):
    sin = math.sin(math.radians(angle))
    cos = math.cos(math.radians(angle))
    px = float(p['x'])
    py = float(p['y'])

    return { 'x': cos * px - sin * py, 'y': sin * px + cos * py }

# Translates Tiled objects to a clear universal format, removes all unused values,
# and throws for any unsupported configurations
def translate_object(obj):
    if 'gid' in obj:
        obj['obj_type'] = 'sprite'

    elif 'ellipse' in obj:
        if obj['width'] != obj['height']:
            raise Exception('width/height mismatch: ellipse only supports circles')

        obj['obj_type'] = 'circle'
        obj['radius'] = float(obj['width']) * 0.5

        # ellipse position is the top-left corner of the surrounding AABB
        # convert it to the centroid
        obj['x'] = obj['x'] + obj['radius']
        obj['y'] = obj['y'] + obj['radius']

        obj.pop('ellipse')
        obj.pop('rotation')
        obj.pop('width')
        obj.pop('height')

    elif 'polygon' in obj:
        if len(obj['polygon']) > 8:
            raise Exception('polygon vertices exceed max supported')

        obj['obj_type'] = 'polygon'
        obj['coords'] = []
        for coord in obj['polygon']:
            obj['coords'].append(rotate_point(coord, obj['rotation']))

        obj.pop('polygon')
        obj.pop('rotation')
        obj.pop('width')
        obj.pop('height')

    elif 'polyline' in obj:
        raise Exception('polyline not supported')

    elif 'point' in obj:
        obj['obj_type'] = 'point'

        obj.pop('point')
        obj.pop('rotation')
        obj.pop('width')
        obj.pop('height')

    elif 'text' in obj:
        raise Exception('text not supported')

    else: # rectangle
        obj['obj_type'] = 'polygon'
        p1 = { 'x': 0, 'y': 0 }
        p2 = { 'x': obj['width'], 'y': 0 }
        p3 = { 'x': 0, 'y': obj['height'] }
        p4 = { 'x': obj['width'], 'y': obj['height'] }
        obj['coords'] = [ rotate_point(p1, obj['rotation']),
                          rotate_point(p2, obj['rotation']),
                          rotate_point(p3, obj['rotation']),
                          rotate_point(p4, obj['rotation']) ]

        obj.pop('rotation')
        obj.pop('width')
        obj.pop('height')

    verify_properties(obj)


import pygame as pg

from .tools import engine
from .settings import *

""" initialise the pygame """

pg.init()
pg.display.set_caption(GAME_CAPTION)
pg.display.set_mode(SCREEN_SIZE)

def run(root):
    game = engine.Engine(root)
    game.setup()
    game.mainloop()


class Meta():

    def __init__(self, cfg) -> None:
        self.meta = cfg.meta
        self.spatial_relation = cfg.spatial_relation
        self.temporal_relation = cfg.temporal_relation

        self.cur_sate = 0  # 时间上，当前进行的基元-基元-关系
        self.behaviour_state = False  # 该周期的操作规范标识符，周期结束时判断
        self.op_ing = False  # 操作状态标识符

    def reset(self):
        self.cur_sate = 0
        self.behaviour_state = False

    def update(self, meta_info):
        """
        meta_info: 字典{objInfo类}
        """
        if 'hand' in meta_info:
            # 手出现在操作区域内时，属于操作中，进行基元关系检测
            if not self.op_ing:
                self.reset()
            self.op_ing = True
            state_idx = self.temporal_relation[self.cur_sate]  # 当前操作的基元关系索引

            # 判断当前的操作基元关系是否出现
            state = False
            for idx in state_idx:
                meta_state = self.spatial_relation[idx]
                meta_state = meta_state.split('-')
                for meta1 in meta_info[meta_state[0]]:
                    for meta2 in meta_info[meta_state[1]]:
                        state |= self.check(meta1, meta2, meta_state[2])
            # 若当前基元关系已出现，则进行下一基元关系检测
            if state:
                self.cur_sate += 1
        else:
            # 手不在操作区域时，说明不在操作状态
            self.op_ing = False
            if self.cur_sate == len(self.temporal_relation):
                self.behaviour_state = True
            else:
                self.behaviour_state = False

    def check(self, meta1, meta2, spa_relation):
        """
        meta1, meta2: 两个基元类obfInfo
        spa_relation: 两基元之间的关系
        """

        if spa_relation == 'concate':
            relation = meta1.isConcate(meta2)
        elif spa_relation == 'depart':
            relation = meta1.isDepart(meta2)

        return relation


#!/usr/bin/env python3
# MRMS grib2 data fetching script for python-based HDWX
# Created 29 April 2022 by Sam Gardner <stgardner4@tamu.edu>

from os import path, remove, listdir
from pathlib import Path
import pandas as pd
from datetime import datetime as dt, timedelta
import json
import requests
import gzip
import shutil
import urllib

basePath = path.realpath(path.dirname(__file__))

def downloadFile(fileName):
    output = path.join(basePath, "input", fileName)
    print("Downloading "+fileName)
    urlToFetch = "https://mrms.ncep.noaa.gov/data/2D/ReflectivityAtLowestAltitude/"+fileName
    try:
        mrmsData = requests.get(urlToFetch)
        if mrmsData.status_code == 200:
            mrmsData = mrmsData.content
    except requests.exceptions.SSLError:
        import subprocess
        mrmsDataProc = subprocess.run(["curl", urlToFetch], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        mrmsData = mrmsDataProc.stdout
    with open(output, "wb") as fileWrite:
        fileWrite.write(mrmsData)
    with gzip.open(output, "rb") as f_in:
        with open(output.replace(".gz", ""), "wb") as f_out:
            shutil.copyfileobj(f_in, f_out)
    remove(output)

if __name__ == "__main__":
    Path(path.join(basePath, "input")).mkdir(parents=True, exist_ok=True)
    try:
        gribList = pd.read_html("https://mrms.ncep.noaa.gov/data/2D/ReflectivityAtLowestAltitude/")[0].dropna(how="any")
    except urllib.error.URLError:
        import subprocess
        from io import BytesIO
        gribListCurlProc = subprocess.run(["curl", "https://mrms.ncep.noaa.gov/data/2D/ReflectivityAtLowestAltitude/"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        gribList = pd.read_html(BytesIO(gribListCurlProc.stdout))[0].dropna(how="any")
    gribList = gribList[~gribList.Name.str.contains("latest") == True].reset_index()
    gribList["pyDateTimes"] = [dt.strptime(filename, "MRMS_ReflectivityAtLowestAltitude_00.50_%Y%m%d-%H%M%S.grib2.gz") for filename in gribList["Name"]]
    gribList = gribList.set_index(["pyDateTimes"])
    alreadyDownloaded = listdir(path.join(basePath, "input"))
    for mosaicTime in reversed(gribList.index):
        if mosaicTime > dt.utcnow() - timedelta(hours=1):
            runFile = path.join(basePath, "output", "metadata", "products", "3", mosaicTime.strftime("%Y%m%d%H00.json"))
            if path.exists(runFile):
                with open(runFile) as jsonLoad:
                    runData = json.load(jsonLoad)
                alreadyHavePlotted = False
                for productFrame in runData["productFrames"]:
                    if mosaicTime.strftime("%Y%m%d%H%M") == productFrame["valid"]:
                        alreadyHavePlotted = True
                if not alreadyHavePlotted:
                    fileToDownload = gribList[gribList.index == mosaicTime]["Name"][0]
                    if fileToDownload.replace(".gz", "") not in alreadyDownloaded:
                        downloadFile(fileToDownload)
            else:
                fileToDownload = gribList[gribList.index == mosaicTime]["Name"][0]
                if fileToDownload.replace(".gz", "") not in alreadyDownloaded:
                    downloadFile(fileToDownload)


#!/usr/bin/python3

import itertools
import subprocess

def readTaskFromInput():
    numberOfRows, numberOfColumns = [int(x) for x in input().split(" ")]
    grid = [ [int(x) for x in input().split(" ")] for i in range(numberOfRows)]
    return (numberOfRows, numberOfColumns, grid)

class VDC:
    """Variable decoder.
    """
    def __init__(self, rawInput):
        self.numberOfColumns, self.numberOfRows, self.grid = rawInput
        self.amountOfVerticalWalls = (self.numberOfColumns + 1) * self.numberOfRows
        self.amountOfHorizontalWalls = (self.numberOfRows + 1) * self.numberOfColumns
        self.amountOfWalls = self.amountOfHorizontalWalls + self.amountOfVerticalWalls
        self.maxTime = self.amountOfWalls

    def wallIsInRange(self, row, column, orientation):
        if row < 0 or column < 0: return False
        if row > self.numberOfRows or column > self.numberOfColumns: return False
        if orientation == 0 and row == self.numberOfRows: return False
        if orientation == 1 and column == self.numberOfColumns: return False
        return True

    def wall(self, row, column, orientation):
        """ `orientation` 0 - vertical, 1 - horizontal
        """
        assert(orientation in (0,1))
        assert(self.wallIsInRange(row, column, orientation))
        if orientation == 0: return 1 + column * self.numberOfRows + row
        if orientation == 1: return 1 + self.amountOfVerticalWalls + row * self.numberOfColumns + column

    def rcoFromWall(self, w):
        w -= 1
        assert(w < self.amountOfWalls)
        if w < self.amountOfVerticalWalls:
            orientation = 0
            column, row = w // self.numberOfRows, w % self.numberOfRows
            return (row, column, orientation)
        else:
            w = w - self.amountOfVerticalWalls
            orientation = 1
            row, column = w // self.numberOfColumns, w % self.numberOfColumns
            return (row, column, orientation)

    def reachable(self, row, column, orientation, time):
        assert(0 <= time <= self.maxTime)
        pref = self.amountOfWalls + 1
        return pref + time * self.amountOfWalls + self.wall(row, column, orientation)

    def cellWalls(self, row, column):
        return [self.wall(row, column, 0), self.wall(row, column, 1), self.wall(row+1, column, 1), self.wall(row, column+1, 0)]

    def nodeWalls(self, row, column):
        possibleWalls = [(row, column, 0), (row, column, 1), (row-1, column, 0), (row, column-1, 1)]
        res = [self.wall(*w) for w in possibleWalls if self.wallIsInRange(*w)]
        assert(len(res) >= 2)
        return res

    def wallNeighbours(self, row, column, orientation):
        firstNode = (row, column)
        secondNode = {0:(row+1, column), 1:(row, column+1)}[orientation]
        return list(set( self.nodeWalls(*firstNode) + self.nodeWalls(*secondNode) ))

    def atMostN(self, vs, n):
        # at least one from n+1 variables are false
        return [[-c for c in cs] for cs in itertools.combinations(vs, n+1)]

    def atLeastN(self, vs, n):
        # at most N - n variables are false
        # so at least one from N - n + 1 is true
        return [list(cs) for cs in itertools.combinations(vs, len(vs) - n + 1)]

    def exactlyN(self, vs, n):
        return self.atMostN(vs, n) + self.atLeastN(vs, n)

    def zeroOrTwo(self, vs):
        """Full CNF computation.
        """
        res = []
        for cs in itertools.product(range(2), repeat=len(vs)):
            if sum(cs) not in (0, 2):
                res.append([(-1)**c * v for (c, v) in zip(cs, vs)])
        return res

    def implies(self, A, B):
        """AND(A) -> OR(B) === OR(NOT Ai) OR OR(B)
        """
        return [[-x for x in A] + B]

def rawInputToCNF(rawInput):
    numberOfRows, numberOfColumns, grid = rawInput
    clauses = []

    def addClauses(cs):
        assert([] not in cs)
        nonlocal clauses
        for c in cs: clauses.append(c)

    def normalizeClauses():
        """Delete redundant clauses.
        """
        nonlocal clauses
        clauses = [list(clause) for clause in list(set([tuple(sorted(clause)) for clause in clauses]))]

    vdc = VDC(rawInput)

    # Number of walls
    for i in range(numberOfRows):
        for j in range(numberOfColumns):
            if grid[i][j] != -1:
                addClauses(vdc.exactlyN(vdc.cellWalls(i,j), grid[i][j]))

    # Connectivity
    for i in range(numberOfRows+1):
        for j in range(numberOfColumns+1):
            addClauses(vdc.zeroOrTwo(vdc.nodeWalls(i,j)))

    # One loop
    # everything except first wall is false at beginning
    # everything at end is equal to wall

    def findPositiveCell():
        for row in range(numberOfRows):
            for column in range(numberOfColumns):
                if grid[row][column] > 0:
                    return (row, column)
    positiveCell = findPositiveCell()
    rcoPossibleStarts = [vdc.rcoFromWall(w) for w in vdc.cellWalls(*positiveCell)]

    for i in range(len(rcoPossibleStarts)):
        # if previous walls are false and i-th is true, then its first wall
        prevWalls = rcoPossibleStarts[:i]
        addClauses(vdc.implies(
            [-vdc.wall(*x) for x in prevWalls] + [vdc.wall(*rcoPossibleStarts[i])],
            [vdc.reachable(*rcoPossibleStarts[i], time=0)]
        ))

        for otherWall in [x for x in rcoPossibleStarts if x != rcoPossibleStarts[i]]:
                addClauses(vdc.implies(
                    [-vdc.wall(*x) for x in prevWalls] + [vdc.wall(*rcoPossibleStarts[i])],
                    [-vdc.reachable(*otherWall, time=0)]
                ))
    for w in [vdc.rcoFromWall(w) for w in range(1, vdc.amountOfWalls + 1) if vdc.rcoFromWall(w) not in rcoPossibleStarts]:
        addClauses([[-vdc.reachable(*w, time=0)]])

    for w in [vdc.rcoFromWall(w) for w in range(1, vdc.amountOfWalls + 1)]:
        addClauses(vdc.implies(
            [vdc.reachable(*w, time=vdc.maxTime)],
            [vdc.wall(*w)]
        ))
        addClauses(vdc.implies(
            [vdc.wall(*w)],
            [vdc.reachable(*w, time=vdc.maxTime)]
        ))

    # inference rule:
    # if everything in neighbourhood at time t-1 is false, then I am false at time t
    # if not, then I am true at time t
    for t in range(1, vdc.maxTime+1):
        for w in [vdc.rcoFromWall(x) for x in range(1, vdc.amountOfWalls + 1)]:
            neighbourhood = [vdc.rcoFromWall(x) for x in vdc.wallNeighbours(*w)]
            # if w exitst
            addClauses(vdc.implies(
                [vdc.wall(*w)] + [-vdc.reachable(*x, time=t-1) for x in neighbourhood],
                [-vdc.reachable(*w, time=t)]
            ))
            for n in neighbourhood:
                addClauses(vdc.implies(
                    [vdc.wall(*w)] + [vdc.reachable(*n, time=t-1)],
                    [vdc.reachable(*w, time=t)]
                ))
            #if w doesn't exist
            addClauses(vdc.implies(
                [-vdc.wall(*w)],
                [-vdc.reachable(*w, time=t)]
            ))

    normalizeClauses()
    return clauses

def CNFtoPicosat(program):
    def maximumVariable(program):
        return max([
                max([abs(x) for x in clause]) for clause in program
                   ])

    header = "p cnf {0} {1}".format(maximumVariable(program),len(program))

    body = "\n".join(
                        [" ".join([str(x) for x in clause] + ["0"]) for clause in program]
                    )
    indata = header + "\n" + body + "\n"
    solver = subprocess.Popen(["picosat"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    solver.stdin.write(indata.encode("utf-8"))
    outdata, errdata = solver.communicate()
    return outdata.decode("utf-8")

def visualize(rawOutput, rawInput):
    numberOfRows, numberOfColumns, grid = rawInput
    vdc = VDC(rawInput)
    if "UNSATISFIABLE" in rawOutput:
        print("UNSATISFIABLE")
        #return

    activeWallss =  [ [ int(x) for x in  [x for x in line.split(" ")][1:] if int(x) > 0] for line in rawOutput.split("\n")[1:-1]]
    activeWalls = []
    for xs in activeWallss:
        for x in xs:
            activeWalls.append(x)

    for i in range(numberOfRows+1):
        print("+".join([""] + [{True:"-", False:" "}[vdc.wall(i,j,1) in activeWalls] for j in range(numberOfColumns)] + [""]))
        if i == numberOfRows: break
        print("".join([ {True:"|", False:" "}[vdc.wall(i,j,0) in activeWalls] + {True:str(grid[i][j]), False:" "}[grid[i][j] != -1] for j in range(numberOfColumns)]) +\
            {True:"|", False:" "}[vdc.wall(i,numberOfColumns,0) in activeWalls])

def visualizeDFS(rawOutput, rawInput):
    numberOfRows, numberOfColumns, grid = rawInput
    vdc = VDC(rawInput)
    if "UNSATISFIABLE" in rawOutput:
        print("UNSATISFIABLE")
        return

    activeVarss =  [ [ int(x) for x in  [x for x in line.split(" ")][1:] if int(x) > 0] for line in rawOutput.split("\n")[1:-1]]
    activeVars = []
    for xs in activeVarss:
        for x in xs:
            activeVars.append(x)

    activeWalls = [x for x in activeVars if x <= vdc.amountOfWalls]
    activeReachables = [x for x in activeVars if x > vdc.amountOfWalls]

    def isReached(row, column, orientation, time):
        nonlocal activeReachables
        return vdc.reachable(row, column, orientation, time) in activeReachables

    def rWall(row, column, orientation, time):
        return {(True, 0):"|", (False, 0):":", (True, 1):"-", (False, 1):"…"}[(isReached(row, column, orientation, time), orientation)]

    for t in range(0, vdc.maxTime//2):
        print ("time = " + str(t))
        for i in range(numberOfRows+1):
            print("+".join([""] + [{True:rWall(i, j, 1, t), False:" "}[vdc.wall(i,j,1) in activeWalls] for j in range(numberOfColumns)] + [""]))
            if i == numberOfRows: break
            print("".join([ {True:rWall(i,j,0,t), False:" "}[vdc.wall(i,j,0) in activeWalls] + {True:str(grid[i][j]), False:" "}[grid[i][j] != -1] for j in range(numberOfColumns)]) +\
                {True:rWall(i, numberOfColumns, 0, t), False:" "}[vdc.wall(i,numberOfColumns,0) in activeWalls])

def main():
    rawInput = readTaskFromInput()
    program = rawInputToCNF(rawInput)
    print("CNF length: ", len(program))
    rawOutput = CNFtoPicosat(program)
    visualize(rawOutput, rawInput)
    # visualizeDFS(rawOutput, rawInput)

if __name__ == "__main__": main()