from threading import Lock
from Common import *

class Model:

    def __init__(self,ravensProblem):

        self._figA = self.createFigure(ravensProblem.figures["A"])
        self._figB = self.createFigure(ravensProblem.figures["B"])
        self._figC = self.createFigure(ravensProblem.figures["C"])

        self._answers = []
        for i in range(1, 7, 1):
            self._answers.append(self.createFigure(ravensProblem.figures[str(i)]))

        self._objectsA = [ EmptyObject() ]
        self._objectsB = [ EmptyObject() ]
        self._objectsC = [ EmptyObject() ]
        self._objectsA += self._figA.getObjects()
        self._objectsB += self._figB.getObjects()
        self._objectsC += self._figC.getObjects()

        self._sizeA = len(self._objectsA)
        self._sizeB = len(self._objectsB)
        self._sizeC = len(self._objectsC)

        self._model = [[[Block() for k in range(len(self._objectsC))] for j in range(len(self._objectsB))] for i in range(len(self._objectsA))]

        idxA = 0
        for objectA in self._objectsA:
            idxB = 0
            for objectB in self._objectsB:
                idxC = 0
                for objectC in self._objectsC:
                    if not(idxA == 0 and idxB == 0 and idxC == 0):
                        block = Block(objectA, objectB, objectC, idxA, idxB, idxC, self._sizeA, self._sizeB, self._sizeC)
                        self._model[idxA][idxB][idxC] = block

                    idxC += 1

                idxB += 1

            idxA += 1

    def solve(self):
        blocks = []

        for i in range(self._sizeA):
            for j in range(self._sizeB):
                for k in range(self._sizeC):
                    block = self._model[i][j][k]

                    if block != {} and (i != 0 or j != 0 or k != 0):
                        blocks.append(block)

        # Order them based on score
        blocks = self.sortObjects(blocks)

        # Generator content
        generatedBlocks = []

        for block in blocks:
            if len(generatedBlocks) == 0:
                generatedBlocks.append(block)
            else:
                found = False

                for generatedBlock in generatedBlocks:
                    if (block.getIndexA() == generatedBlock.getIndexA() and block.getIndexA() != 0) or (block.getIndexB() == generatedBlock.getIndexB() and block.getIndexB() != 0) or (block.getIndexC() == generatedBlock.getIndexC() and block.getIndexC() != 0):
                        found = True

                if not found:
                    generatedBlocks.append(block)

        generatedObjects = []

        for generatedBlock in generatedBlocks:
            for generatedObject in generatedBlock.getGenerated():
                if type(generatedObject) is not EmptyObject:
                    generatedObjects.append(generatedObject)

        for answer in self._answers:
            answerObjects = answer.getObjects()
            if len(answerObjects) == len(generatedObjects):
                allMatch = True
                for generatedObject in generatedObjects:
                    if not self.checkGeneratedObject(generatedObject, answerObjects):
                        allMatch = False

                if allMatch:
                    return int(answer.getName())

        # Couldn't find a solution!
        return -1

    def sortObjects(self, blocks):
        for i in range(len(blocks)):
            for j in range(len(blocks) - i - 1):
                if self.shouldSwap(blocks[j], blocks[j + 1]):
                    temp = blocks[j]
                    blocks[j] = blocks[j + 1]
                    blocks[j + 1] = temp

        return blocks

    def shouldSwap(self, blockA, blockB):
        if blockA.getTotalScore() < blockB.getTotalScore():
            return True
        if blockA.getTotalScore() > blockB.getTotalScore():
            return True
        if blockA.getDownScore() < blockB.getDownScore() and blockA.getDownScore() < blockB.getRightScore():
            return True
        if blockA.getRightScore() < blockB.getDownScore() and blockA.getRightScore() < blockB.getRightScore():
            return True
        if blockA.getDownScore() > blockB.getDownScore() and blockA.getDownScore() > blockB.getRightScore():
            return False
        if blockA.getRightScore() > blockB.getDownScore() and blockA.getRightScore() > blockB.getRightScore():
            return False

        return False

    def checkGeneratedObject(self, generatedObject, answerObjects):
        for answerObject in answerObjects:
            transformations = Block().getTransformations(generatedObject, answerObject)
            score = Block().getScore(transformations, False, False)

            if score == 0:
                return True

        return False

    def createFigure(self, ravensFigure):
        lock = Lock()
        lock.acquire()

        self._createdObjects = []
        self._createdAttributes = []

        name = ravensFigure.name
        self._createdObjects = []
        for object in ravensFigure.objects:
            o = self.createObject(ravensFigure.objects[object])
            self._createdObjects.append(o)

        lock.release()

        return Figure(name, self._createdObjects)

    def createObject(self, ravensObject):
        lock = Lock()
        lock.acquire()

        name = ravensObject.name
        attributes = ravensObject.attributes

        self._createdAttributes = []
        for attr in ravensObject.attributes:
            if any(x == attr for x in self._createdAttributes):
                pass
            else:
                self._createdAttributes.append(Attribute(attr, attributes[attr]))

        lock.release()

        return Object(name, self._createdAttributes)


from django.conf.urls import url, include
from rest_framework import routers
from backend.views import CustomObtainAuthToken, CounselorViewSet

router = routers.DefaultRouter()
router.register(r'counselor', CounselorViewSet)

# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
    url(r'^', include(router.urls)),
    url(
        r'^api-auth/',
        include('rest_framework.urls', namespace='rest_framework')
    ),
    url(
        r'^get_auth_token/$',
        CustomObtainAuthToken.as_view(),
        name='get_auth_token'
    ),
]


from xai.brain.wordbase.adjectives._catholic import _CATHOLIC

#calss header
class _CATHOLICS(_CATHOLIC, ):
	def __init__(self,):
		_CATHOLIC.__init__(self)
		self.name = "CATHOLICS"
		self.specie = 'adjectives'
		self.basic = "catholic"
		self.jsondata = {}


from django.shortcuts import render
import requests

def index(request):
    area = request.GET.get('area')
    area = str(area)
    baserUrl = 'https://api.weatherapi.com/v1/current.json?key=e7a6fabfe4ae403280593925210208&q={}&aqi=yes'.format(area)
    response = requests.get(baserUrl).json()

    city_weather = {
        'City': response['location']['region'],
        'Time': response['location']['localtime'],
        'Celcius': response['current']['temp_c'],
        'Fahrenheit': response['current']['temp_f'],
        'Condition': response['current']['condition']['text'],
        'Icon': response['current']['condition']['icon'],
        'WindSpeed': response['current']['wind_mph'],
        'WindDirection': response['current']['wind_dir'],
        'Humidity': response['current']['humidity'],
    }

    return render(request, 'index.html', {'city_weather': city_weather})


#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name:sent_tokenize
   Author:admin
   date:2018/4/11
-------------------------------------------------
   Change Activity:2018/4/11:
-------------------------------------------------
"""
from stanfordcorenlp import StanfordCoreNLP

def word_tokenize(sents, nlp):
	tokenses = []
	for sent in sents:
		tokenses.append(nlp.word_tokenize(sent))
	return tokenses

if __name__ == '__main__':
	input = "../../data/sents/test.txt"
	output = "../../data/tokens/test.txt"

	nlp = StanfordCoreNLP("c:/stanford-corenlp-full-2018-02-27")

	# print(nltk.sent_tokenize("Textron_Systems_Canada_Inc._@ORG"))
	# print(nlp.word_tokenize("Textron_Systems_Canada_Inc._@ORG"))

	sents = []
	with open(input, 'r', encoding='utf-8') as f:
		sents = f.readlines()

	tokenses = []
	for sent in sents:
		tokenses.append(nlp.word_tokenize(sent))

	with open(output, 'w', encoding='utf-8') as f:
		for tokens in tokenses:
			f.write(' '.join(tokens))
			f.write("\n")
	nlp.close()


import tensorflow as tf
import config_keras
import config
from keras.utils import to_categorical
# Deep learning
# Keras-related imports
from keras.models import Sequential
from keras.layers import Lambda, Dense, Dropout, Activation, Flatten, LSTM
from keras.layers import Convolution1D, MaxPooling2D, Convolution2D
from keras import backend as K
# K.set_image_dim_ordering('th')
from keras.callbacks import ModelCheckpoint, RemoteMonitor, EarlyStopping
from keras.models import load_model
from keras.layers import Conv2D, MaxPooling2D
from keras.regularizers import l2
import os

def train_model(X_train, y_train):

	y_train = tf.keras.utils.to_categorical(y_train, 2)

	################################ CONVOLUTIONAL NEURAL NETWORK ################################
	## NN parameters
	class_weight = {0: 1.,
	                1: 1.,
	                }
	input_shape = (1, X_train.shape[2], X_train.shape[-1])

	# BNN parameters
	dropout=config_keras.dropout  # change to 0.05
	# Regularise
	tau = config_keras.tau
	lengthscale = config_keras.lengthscale
	reg = lengthscale**2 * (1 - dropout) / (2. * len(X_train) * tau)

	W_regularizer=l2(reg)  # regularisation used in layers

	model = Sequential()
	n_dense = 128
	nb_classes = 2
	# number of convolutional filters
	nb_conv_filters = 32
	# num_hidden = 236
	nb_conv_filters_2 = 64
	convout1 = Activation('relu')
	convout2 = Activation('relu')

	model.add(Conv2D(nb_conv_filters, kernel_size = (3,3),
	     activation = 'relu', padding = 'valid', strides = 1,
	     input_shape = input_shape))
	model.add(MaxPooling2D(pool_size=(2, 2)))
	model.add(Lambda(lambda x: K.dropout(x,level=dropout)))

	model.add(Conv2D(nb_conv_filters_2, kernel_size = (3,3),
	     activation = 'relu', padding = 'valid'))
	model.add(MaxPooling2D(pool_size=(2, 2)))
	model.add(Lambda(lambda x: K.dropout(x,level=dropout)))

	model.add(Conv2D(nb_conv_filters_2, kernel_size = (3,3),
	     activation = 'relu', padding = 'valid'))
	# model.add(MaxPooling2D(pool_size=(2, 2)))
	model.add(Lambda(lambda x: K.dropout(x,level=dropout)))

	# # model.add(Dropout(0.2))
	model.add(Conv2D(nb_conv_filters_2, kernel_size = (3,3),
	     activation = 'relu', padding = 'valid'))
	model.add(Lambda(lambda x: K.dropout(x,level=dropout)))

	# model.add(Conv2D(nb_conv_filters_2, kernel_size = (3,3),
	#      activation = 'relu', padding = 'valid'))
	# model.add(Lambda(lambda x: K.dropout(x,level=dropout)))

	model.add(Flatten())
	# # Shared between MLP and CNN:
	model.add(Dense(n_dense, activation='relu'))
	model.add(Lambda(lambda x: K.dropout(x,level=dropout)))

	model.add(Dense(nb_classes, activation='softmax',W_regularizer=l2(reg)))
	model.compile(loss='categorical_crossentropy',
	                optimizer='adadelta',
	                metrics=['accuracy'])

    # if checkpoint_name is not None:
    # 	os.path.join(os.path.pardir, 'models', 'keras', checkpoint_name)

	model_name = 'Win_' + str(config.win_size) + '_Stride_' + str(config.step_size) + '_BNN.h5'
	checkpoint_filepath = os.path.join(os.path.pardir, 'models/keras', model_name) # Need to makedir there too.
	model_checkpoint_callback = ModelCheckpoint(
	    filepath=checkpoint_filepath,
	    save_weights_only=False,
	    monitor='val_accuracy',
	    mode='max',
	    save_best_only=True)

	model.fit(x=X_train, y=y_train, batch_size=config_keras.batch_size, epochs=config_keras.epochs, verbose=1,  validation_split=config_keras.validation_split,
          validation_data=None,
          shuffle=True, class_weight=class_weight, sample_weight=None, initial_epoch=0,
          steps_per_epoch=None, validation_steps=None, callbacks=[model_checkpoint_callback])

	# print('Saving model to:', os.path.join(os.path.pardir, 'models', 'keras', checkpoint_name))

	return model

def evaluate_model(model, X_test, y_test, n_samples):
	all_y_pred = []
	for n in range(n_samples):
		all_y_pred.append(model.predict(X_test))
	return all_y_pred

def load_model(filepath):
	model = tf.keras.models.load_model(filepath, custom_objects={"dropout": config_keras.dropout})
	return model


from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
app = QApplication(sys.argv)

tabs = QTabWidget()
tab1 = QWidget()
tab2 = QWidget()
tabs.addTab(tab1 , "window")
tabs.addTab(tab2 , "test")
vbox = QVBoxLayout(tabs)
button1 = QPushButton("Start")
button2 = QPushButton("End")
vbox.addWidget(button1)
vbox.addWidget(button2)
tab1.setLayout(vbox)

tabs.show()
app.exec_()


import serial
import numpy as np
import time

class ArduinoInterface(object):
    """docstring for ResistanceValues"""

    def __init__(self):
        # self.ser = serial.Serial('/dev/cu.usbmodem14201', 9600) #Ryan's Port
        time.sleep(.5)
        self.serR = serial.Serial('/COM8', 9600)  # Sufiyaan's Port
        self.serL = serial.Serial('/COM9', 9600)  # Sufiyaan's Port

    def get_resistance_values(self):
        self.serL.write(b"send")
        self.serR.write(b"send")

        line = self.serL.readline()

        resistanceValues = np.zeros(10)

        for i in range(0, 10):
            # print(i)
            if i < 5:
                line = self.serL.readline()
            else:
                line = self.serR.readline()
            resistanceValues[i] = float(line.replace(b"\r\n", b""))

        return resistanceValues


import numpy as np

class LocalAlignment:
    def __init__(self, string1, string2, gap_penalty, matrix):
        """
        :param string1: first string to be aligned, string
        :param string2: second string to be aligned, string
        :param gap_penalty: gap penalty, integer
        :param matrix: substitution matrix containing scores for amino acid
                       matches and mismatches, dict

        Attention! string1 is used to index columns, string2 is used to index rows
        """
        self.string1 = string1
        self.string2 = string2
        self.gap_penalty = gap_penalty
        self.substitution_matrix = matrix
        self.score_matrix = np.zeros((len(string2) + 1, len(string1) + 1), dtype=np.int)
        self.max_row = 0
        self.max_column = 0
        self.alignment = ('', '')
        self.align()

    def align(self):
        """
        Align given strings using the Smith-Waterman algorithm.
        NB: score matrix and the substitution matrix are different matrices!
        """
        self.compute_score_matrix()
        self.compute_alignment()

    def compute_score_matrix(self):
        """
        Initialize the score matrix.
        Fill the first row and the first column with zeroes,
        then calculate the top score for each empty cell starting
        from top left.
        """
        max_value = 0

        for row in range(1, len(self.string2) + 1):
            for column in range(1, len(self.string1) + 1):
                match = (
                    self.score_matrix[row - 1, column - 1]
                    + self.substitution_matrix[self.string1[column - 1]][self.string2[row - 1]]
                )
                string1_insertion = (
                    self.score_matrix[row - 1, column] + self.gap_penalty
                )
                string2_insertion = (
                    self.score_matrix[row, column - 1] + self.gap_penalty
                )
                new_value = max(
                    match, string1_insertion, string2_insertion, 0
                )
                if new_value > max_value:
                    max_value = new_value
                    self.max_row = row
                    self.max_column = column
                self.score_matrix[row, column] = new_value

    def compute_alignment(self):
        string1_aligned = []
        string1_aligned_indices = []
        string2_aligned = []
        string2_aligned_indices = []
        row = self.max_row
        column = self.max_column
        while self.score_matrix[row, column] > 0:
            if (row > 0 and
                    column > 0 and
                    self.score_matrix[row, column] == self.score_matrix[row - 1, column - 1]
                    + self.substitution_matrix[self.string1[column - 1]][self.string2[row - 1]]):
                string1_aligned.append(self.string1[column - 1])
                string1_aligned_indices.append(column - 1)
                string2_aligned.append(self.string2[row - 1])
                string2_aligned_indices.append(row - 1)
                row -= 1
                column -= 1
            elif (row > 0 and
                    self.score_matrix[row, column] == self.score_matrix[row - 1, column]
                    + self.gap_penalty):
                # insertion into string1
                string1_aligned.append('-')
                string2_aligned.append(self.string2[row - 1])
                string2_aligned_indices.append(row - 1)
                row -= 1
            else:
                # insertion into string2
                string1_aligned.append(self.string1[column - 1])
                string1_aligned_indices.append(column - 1)
                string2_aligned.append('-')
                column -= 1
        string1_aligned.reverse()
        string2_aligned.reverse()
        self.alignment = (''.join(string1_aligned), ''.join(string2_aligned))
        self.string1_aligned_indices = string1_aligned_indices
        self.string2_aligned_indices = string2_aligned_indices

    def has_alignment(self):
        """
        :return: True if a local alignment has been found, False otherwise
        """
        return self.alignment != ('', '')

    def get_alignment(self):
        """
        :return: list of alignments, where each alignment is represented
                 as a tuple of aligned strings
        """
        return self.alignment

    def is_residue_aligned(self, string_number, residue_index):
        """
        :param string_number: number of the string (1 for string1, 2 for string2) to check
        :param residue_index: index of the residue to check
        :return: True if the residue with a given index in a given string has been alined
                 False otherwise
        """
        if string_number == 1:
            return residue_index in self.string1_aligned_indices
        if string_number == 2:
            return residue_index in self.string2_aligned_indices
        return False


#calss header
class _KAMIKAZE():
	def __init__(self,):
		self.name = "KAMIKAZE"
		self.definitions = [u'A kamikaze attack is a sudden violent attack on an enemy, especially one in which the person or people attacking know that they will be killed: ', u'being willing to take risks and not worrying about safety: ']

		self.parents = []
		self.childen = []
		self.properties = []
		self.jsondata = {}

		self.specie = 'adjectives'

	def run(self, obj1, obj2):
		self.jsondata[obj2] = {}
		self.jsondata[obj2]['properties'] = self.name.lower()
		return self.jsondata


def isYearLeap(year):
    if(year > 1582):
        if(year % 4 != 0):
            return False
        elif(year % 100 != 0):
            return True
        elif(year % 400 != 0):
            return False
        else:
            return True

def daysInMonth(year, month):
    if(month == 2):
        if(isYearLeap(year)):
            return 29
        else:
            return 28
    elif(month <= 7):
        if(month%2 == 0):
            return 30
    elif(month > 7):
        if(month%2 != 0):
            return 30
    return 31

def dayOfYear(year, month, day):
    x = 0
    for i in range(12):
        if(month-1 != i):
            x += daysInMonth(year, i+1)
        else:
            x += day
            return x

print(dayOfYear(1999, 12, 31))


# filtering_ver2.py

import numpy as np
from scipy.signal import butter, filtfilt
from matplotlib import pyplot
import tkinter as Tk

root = Tk.Tk()
root.title('Filter GUI, version 2')

N = 500
n = np.arange(0, N)
x = np.sin(5 * np.pi * n/N) + 0.4 * np.random.randn(N)

pyplot.ion()           # Turn on interactive mode so plot gets updated
my_fig = pyplot.figure()
my_plot = my_fig.add_subplot(1, 1, 1)
my_plot.plot(n, x, label = 'Noisy data', marker = '.', markersize = 5, linestyle = 'None', color = 'gray')
[my_line] = my_plot.plot(n, x, label = 'Filtered data', linewidth = 2, color = 'black')
my_plot.legend()
my_plot.set_xlim(0, N)
my_plot.set_ylim(-3, 3)

cutoff_freq = Tk.DoubleVar()			# Define Tk variable
cutoff_freq.set(0.25)  					# Initilize

# Update plot when slider is moved
def updatePlot(event):
  [b, a] = butter(2, 2*cutoff_freq.get())
  y = filtfilt(b, a, x)
  my_line.set_ydata(y)
  my_plot.set_title('Cut-off frequency = %f' % cutoff_freq.get() )
  my_fig.canvas.draw()

def my_quit():
  global CONTINUE
  CONTINUE = False
  print('Good bye')
  root.quit()     # stops mainloop
  root.destroy()  # This prevents an error on Windows

# Define slider
S1 = Tk.Scale(root,
  length = 200, orient = Tk.HORIZONTAL,
  from_ = 0.01, to = 0.49,
  resolution = 0.005,
  command = updatePlot,
  variable = cutoff_freq, label = 'Cut-off frequency')

B1 = Tk.Button(root, text = 'Quit', command = my_quit)

# Place widgets in the GUI window
S1.pack()
B1.pack()

updatePlot(None)

CONTINUE = True
while CONTINUE:
  root.update()

Module 7/demo 19 - tkinter _plots and audio_/demo 19 - tkinter (plots and audio)/filtering_ver2.py


from threading import Thread
import time
import logging

logging.basicConfig(lebel=logging.DEBUG,format='[%(levelname)s (%(threadName)-s) %(message)s]')
class Hilo2(Thread) :
    def __init__(self,nameThread,idPersona,data):
        Thread.__init__(self,name=nameThread,target=Hilo2.run)
        # Thread.__init__(self,name=nameThread,target=__guardar,args=(self,idPersona,data))
        self.__nameThread=nameThread
        self.__idPersona=idPersona
        self.__data=data
    def run(self):
        self.__guardar(self.__idPersona,self.__data)

    def __guardar(self,idPersona,data):
        logging.debug("Guardando para el id "+str(idPersona)+" la data "+str(data))
        time.sleep(3)
        return


# URL.py lists all the site urls and different paths and name for each

from django.urls import path
from . import views

# post/<int:pk>/ specifies a URL pattern
#	post/ = URL should begin with the word post followed by a /.
#	<int:pk> = expects an integer value and transfer it to a view as a variable called pk.
#	/ – then we need a / again before finishing the URL.

urlpatterns = [
# ---- POST URLs SECTION ---
	path('',views.post_list,name='post_list'),
	path('post/<int:pk>/',views.post_detail,name="post_detail"),
	path('post/new/',views.post_new,name='post_new'),
	path('post/<int:pk>/edit/',views.post_edit,name="post_edit"),
	path('drafts/',views.post_draft_list,name="post_draft_list"),
	path('post/<pk>/publish/',views.post_publish,name="post_publish"),
	path('post/<pk>/remove/',views.post_remove,name='post_remove'),
# ---- COMMENT URLs SECTION ---
	path('post/<int:pk>/comment',views.add_comment_to_post,name='add_comment_to_post'),
	path('comment/<int:pk>/approve/', views.comment_approve, name='comment_approve'),
	path('comment/<int:pk>/remove/', views.comment_remove, name='comment_remove'),
]


import os
import json
import tensorflow as tf

from PIL import Image

from luminoth.utils.dataset import (
    read_xml, read_image, to_int64, to_string, to_bytes
)
from .dataset import DatasetTool, InvalidDataDirectory

WNIDS_FILE = 'imagenet_wnids.json'

def adjust_bbox(xmin, ymin, xmax, ymax, old_width, old_height,
                new_width, new_height):
    # TODO: consider reusing luminoth.utils.image.adjust_bboxes instead of
    # this, but note it uses tensorflow, and using tf and np here may introduce
    # too many problems.
    xmin = (xmin / old_width) * new_width
    ymin = (ymin / old_height) * new_height
    xmax = (xmax / old_width) * new_width
    ymax = (ymax / old_height) * new_height

    return xmin, ymin, xmax, ymax

class ImageNet(DatasetTool):
    def __init__(self, data_dir):
        super(ImageNet, self).__init__()
        self._data_dir = data_dir
        self._imagesets_path = os.path.join(self._data_dir, 'ImageSets', 'DET')
        self._images_path = os.path.join(self._data_dir, 'Data', 'DET',)
        self._annotations_path = os.path.join(
            self._data_dir, 'Annotations', 'DET'
        )
        self.is_valid()

    def is_valid(self):
        if not tf.gfile.Exists(self._data_dir):
            raise InvalidDataDirectory(
                '"{}" does not exist.'.format(self._data_dir)
            )

        if not tf.gfile.Exists(self._imagesets_path):
            raise InvalidDataDirectory('ImageSets path is missing')

        if not tf.gfile.Exists(self._images_path):
            raise InvalidDataDirectory('Images path is missing')

        if not tf.gfile.Exists(self._annotations_path):
            raise InvalidDataDirectory('Annotations path is missing')

    def read_classes(self):
        jsonpath = os.path.join(
            os.path.dirname(os.path.realpath(__file__)),
            'data',
            WNIDS_FILE
        )
        with tf.gfile.GFile(jsonpath) as wnidsjson:
            self._wnids = json.load(wnidsjson)
            return list(self._wnids.values())

    def get_split_path(self, split):
        if split not in self.VALID_SPLITS:
            raise ValueError
        split_path = os.path.join(self._imagesets_path, '{}.txt'.format(split))
        return split_path

    def get_image_path(self, image_id):
        return os.path.join(self._images_path, '{}.JPEG'.format(image_id))

    def load_split(self, split='train'):
        split_path = self.get_split_path(split)

        if not tf.gfile.Exists(split_path):
            raise ValueError('"{}" not found'.format(split))

        with tf.gfile.GFile(split_path) as f:
            for line in f:
                # The images in 'extra' directories don't have annotations.
                if 'extra' in line:
                    continue
                filename = line.split()[0]
                filename = os.path.join(split, filename)
                yield filename.strip()

    def get_split_size(self, split):
        # TODO: More efficient way to do this.
        total_records = 0
        for line in self.load_split(split):
            total_records += 1

        return total_records

    def get_image_annotation(self, image_id):
        return os.path.join(self._annotations_path, '{}.xml'.format(image_id))

    def image_to_example(self, classes, image_id):
        annotation_path = self.get_image_annotation(image_id)
        image_path = self.get_image_path(image_id)

        # Read both the image and the annotation into memory.
        annotation = read_xml(annotation_path)
        image = read_image(image_path)

        # TODO: consider alternatives to using Pillow here.
        image_pil = Image.open(image_path)
        width = image_pil.width
        height = image_pil.height
        image_pil.close()

        obj_vals = {
            'label': [],
            'xmin': [],
            'ymin': [],
            'xmax': [],
            'ymax': [],
        }

        objects = annotation.get('object')
        if objects is None:
            # If there's no bounding boxes, we don't want it
            return
        for b in annotation['object']:
            try:
                label_id = classes.index(self._wnids[b['name']])
            except ValueError:
                continue

            (xmin, ymin, xmax, ymax) = adjust_bbox(
                xmin=int(b['bndbox']['xmin']), ymin=int(b['bndbox']['ymin']),
                xmax=int(b['bndbox']['xmax']), ymax=int(b['bndbox']['ymax']),
                old_width=int(annotation['size']['width']),
                old_height=int(annotation['size']['height']),
                new_width=width, new_height=height
            )
            obj_vals['label'].append(to_int64(label_id))
            obj_vals['xmin'].append(to_int64(xmin))
            obj_vals['ymin'].append(to_int64(ymin))
            obj_vals['xmax'].append(to_int64(xmax))
            obj_vals['ymax'].append(to_int64(ymax))

        if len(obj_vals['label']) == 0:
            # No bounding box matches the available classes.
            return

        object_feature_lists = {
            'label': tf.train.FeatureList(feature=obj_vals['label']),
            'xmin': tf.train.FeatureList(feature=obj_vals['xmin']),
            'ymin': tf.train.FeatureList(feature=obj_vals['ymin']),
            'xmax': tf.train.FeatureList(feature=obj_vals['xmax']),
            'ymax': tf.train.FeatureList(feature=obj_vals['ymax']),
        }

        object_features = tf.train.FeatureLists(
            feature_list=object_feature_lists
        )

        sample = {
            'width': to_int64(width),
            'height': to_int64(height),
            'depth': to_int64(3),
            'filename': to_string(annotation['filename']),
            'image_raw': to_bytes(image),
        }

        # Now build an `Example` protobuf object and save with the writer.
        context = tf.train.Features(feature=sample)
        example = tf.train.SequenceExample(
            feature_lists=object_features, context=context
        )

        return example


from enum import Enum
import logging

class SoundDeviceManager:
    def __init__(self, config, player, ttclient):
        self.output_device_index = config['output_device']
        self.input_device_index = config['input_device']
        self.player = player
        self.ttclient = ttclient
        self.output_devices = self.player.get_output_devices()
        self.input_devices = self.ttclient.get_input_devices()

    def initialize(self):
        logging.debug('Initializing sound devices')
        self.player.set_output_device(self.output_devices[self.output_device_index].id)
        self.ttclient.set_input_device(self.input_devices[self.input_device_index].id)
        logging.debug('Sound devices initialized')

class SoundDevice:
    def __init__(self, name, id, type):
        self.name = name
        self.id = id
        self.type = type

class SoundDeviceType(Enum):
    Output = 0
    Input = 1


# -*- encoding: utf-8 -*-
from __future__ import (unicode_literals, absolute_import,
                        division, print_function)
import json
from collections import namedtuple
from jsonschema.validators import Draft4Validator
import jsonschema.exceptions
from .schema import OPARL

class ValidationError(namedtuple('ValidationError', ['message', 'section'])):

    def __new__(cls, message, section=None):
        return super(ValidationError, cls).__new__(cls, message, section)

class OParl(object):

    def __init__(self, string=None, response=None):
        if string is response is None:
            raise ValueError('Specify either a JSON string or a response.')
        self.string = string
        self.response = response

    @staticmethod
    def _import_from_string(path):
        path_parts = path.split(':')
        if len(path_parts) != 2:
            raise ImportError('path must be in the form of '
                              'pkg.module.submodule:attribute')
        module = __import__(path_parts[0], fromlist=path_parts[1])
        return getattr(module, path_parts[1])

    @staticmethod
    def _build_object_type(object_type):
        if object_type.startswith('oparl:'):
            return object_type
        parts = object_type.split('_')
        return 'oparl:%s' % ''.join([part.capitalize() for part in parts])

    @classmethod
    def _get_validator(cls, object_type):
        object_type = cls._build_object_type(object_type)
        if object_type not in OPARL:
            return None
        return Draft4Validator(OPARL[object_type])

    @staticmethod
    def _validate_type(data):
        type_check = {
            'type': 'object',
            'properties': {
                '@type': {
                    'type': 'string',
                    'enum': OPARL.keys()  # check if we have a schema for @type
                }
            },
            'required': ['@type']
        }
        Draft4Validator(type_check).validate(data)
        return data['@type']

    @classmethod
    def _validate_schema(cls, obj_type, data):
        validator = cls._get_validator(obj_type)
        return validator.iter_errors(data)

    @classmethod
    def _validate_custom(cls, schema, data):
        if 'oparl:validate' in schema:
            for test in schema['oparl:validate']:
                func = cls._import_from_string(test['method'])
                if not func(data):
                    yield ValidationError(section=test['section'],
                                          message=test['message'])

    def _validate_response_success(self):
        return self.response.status_code in range(200, 400)  # O(1) in Py 3

    def validate(self):
        if self.response:
            for name in dir(self):
                if name.startswith('_validate_response_'):
                    if not getattr(self, name)():
                        yield ValidationError('URL invalid')
                        return
                    else:
                        self.string = self.response.text

        if not self.string:
            yield ValidationError('No data for validation')
            return

        try:
            data = json.loads(self.string)
        except ValueError as excp:
            yield ValidationError('JSON error: %s' % excp)
            return

        try:
            obj_type = self._validate_type(data)

            # simple pass all errors to the caller
            for error in self._validate_schema(obj_type, data):
                yield error
            for error in self._validate_custom(OPARL[obj_type], data):
                yield error

        except (jsonschema.exceptions.ValidationError,
                jsonschema.exceptions.SchemaError) as excp:
            # _validate_type may raise an exception, we translate it
            # here into en apropriate ValidationError and pass it to
            # the caller (if the type does not validate, it does not
            # make sense to continue validation
            if len(excp.path) > 0:
                yield ValidationError('"{}": {}'.format(''.join(excp.path),
                                                        excp.message))
            else:
                yield ValidationError(excp.message)


import copy
import time

import torch
from torchmetrics.functional import accuracy
from utils.meter import AverageMeter, ProgressMeter

def train(model, dataloaders, criterion, optimizer, logger, device, cfgs, exp_tracker_framework='tensorboard', exp_tracker=None):
    """a simple train and evaluate script modified from https://pytorch.org/tutorials/beginner/finetuning_torchvision_models_tutorial.html.

        Args:
            model (nn.Module): model to be trained.
            dataloaders (dict): should be a dict in the format of {'train': DataLoader, 'val': DataLoader}.
            device (Any): device.
            criterion (Any): loss function.
            optimizer (Any): optimizer.
            logger (Any): using logging.logger to print and log training information.
            print_freq (int): logging frequency.eg. 10 means logger will print information when 10 batches are trained or evaluated.
            num_epochs (int): training epochs
            tensorboard_plugin(Any): torch.utils.tensorboard.SummaryWriter
        Returns:
            model: trained model.

        """
    # Send the model to GPU
    model = model.to(device)
    since = time.time()
    best_model_wts = copy.deepcopy(model.state_dict())
    for epoch in range(cfgs.epochs):
        # TODO (zhz): seperate train and val to two functions
        for phase in ['train', 'val']:
            # statistics
            batch_time = AverageMeter('Time', ':6.3f')
            data_time = AverageMeter('Data', ':6.3f')
            losses = AverageMeter('Loss', ':.4e')
            top1 = AverageMeter('Acc@1', ':6.5f')
            top5 = AverageMeter('Acc@5', ':6.5f')
            progress = ProgressMeter(
                len(dataloaders[phase]),
                [batch_time, data_time, losses, top1, top5],
                prefix="Epoch: [{}]".format(epoch))

            if phase == 'train':
                model.train()  # Set model to training mode
            else:
                model.eval()   # Set model to evaluate mode

            # Iterate over data.
            end = time.time()
            for i, (inputs, labels) in enumerate(dataloaders[phase]):
                # measure data loading time
                data_time.update(time.time() - end)

                inputs = inputs.to(device)
                labels = labels.to(device)

                # zero the parameter gradients
                optimizer.zero_grad()

                # forward
                # track history if only in train
                with torch.set_grad_enabled(phase == 'train'):

                    outputs = model(inputs)
                    loss = criterion(outputs, labels)

                    # backward + optimize only if in training phase
                    if phase == 'train':
                        loss.backward()
                        optimizer.step()

                # measure accuracy and record loss
                # TODO(zhz): revise the num classes
                acc1 = accuracy(outputs, labels, task="multiclass",
                                num_classes=cfgs.num_classes, top_k=1)
                acc5 = accuracy(outputs, labels, task="multiclass",
                                num_classes=cfgs.num_classes, top_k=5)
                losses.update(loss.item(), inputs.size(0))
                top1.update(acc1, inputs.size(0))
                top5.update(acc5, inputs.size(0))

                if phase == 'train':
                    if exp_tracker_framework == "neptune":
                        exp_tracker['train/loss'].append(loss)
                        exp_tracker['train/acc1'].append(acc1)
                        exp_tracker['train/acc5'].append(acc5)
                    elif exp_tracker_framework == "tensorboard":
                        exp_tracker.add_scalar('loss/train', loss, i)
                        exp_tracker.add_scalar('acc1/train', acc1, i)
                        exp_tracker.add_scalar('acc5/train', acc5, i)
                    elif exp_tracker_framework == "wandb":
                        exp_tracker.log({"train/loss": loss.item(), "train/acc1": acc1, "train/acc5": acc5})
                    else:
                        raise NotImplementedError

                else:
                    raise NotImplementedError

                # measure elapsed time
                batch_time.update(time.time() - end)
                end = time.time()
                if i % cfgs.print_freq == 0:
                    logger.info(progress.display(i))
    time_elapsed = time.time() - since
    logger.info('Training complete in {:.0f}m {:.0f}s'.format(
        time_elapsed // 60, time_elapsed % 60))

    # load best model weights
    model.load_state_dict(best_model_wts)

    return model


import time

my_words = [elt.strip() for elt in open("sonnet_words.txt", "r").readlines()]
#print("My words ",my_words)
word_list = [elt.strip() for elt in open("sowpods.txt", "r").readlines()]
word_set=set(word_list)
#print( "Word Dict" , word_dict)
counter = 0

start = time.time()
for word in my_words:
    if word not in word_set:
        print(word)
        counter += 1

stop = time.time()

print("Total new words: %d" % counter)
print("Total Elapsed time : %1f seconds" %(stop-start))


#import numpy as np
#A=testmatrix.txt
#row_sum = np.sum(A, axis=0)#---
#col_sum = np.sum(A, axis=1)#|||

#row_sum.sort(reberse=True)
## I have to sort all the columns according to this result ,but I cannot find the way

#col_sum.sort(reberse=True)
##also

from operator import itemgetter

def get_matrix():
    #matrix = [
    #    [13, 14, 15, 16],
    #    [9, 10, 11, 12],
    #    [5, 6, 7, 8],
    #    [1, 2, 3, 4],
    #]

    matrix = [
        [10, 5, 4, 20],
        [9, 33, 27, 16],
        [11, 6, 55, 3],
    ]

    print("Load matrix from:")
    print("1 - File")
    print("Other key - Hard coded")
    if input() == "1":
        matrix = get_matrix_from_file()
    return matrix

#Reads a file and returns a matrix [[0],[],..,[n]]
def get_matrix_from_file():
    matrix = []
    with open("textmatrix.txt", "r") as file:
        for line in file:
            str_values = line.rstrip('\n').split()
            if len(str_values) > 0:
                matrix.append(([int(i) for i in str_values]))
    return matrix

#Returns a list of tuples with index and sum of each row in a matrix,
#sorted by sum in descending order.
def get_sorted_rows_sum(matrix):
    result = {}
    row_count = len(matrix)
    for row_index in range(row_count):
        result[row_index] = sum(matrix[row_index])
    return sorted(result.items(), key=itemgetter(1))

#Returns a list of tuples with index and sum of each column in a matrix,
#sorted by sum in descending order.
def get_sorted_columns_sum(matrix):
    result = {}
    col_count = len(matrix[0])
    for col_index in range(col_count):
        result[col_index] = sum([row[col_index] for row in matrix])
    return sorted(result.items(), key=itemgetter(1))

def get_row_sorted_matrix(rows_sum_index, matrix):
    sorted_matrix = []
    for cur_index, cur_sum in rows_sum_index:
        sorted_matrix.append(matrix[cur_index])
    return sorted_matrix

def get_col_sorted_matrix(cols_sum_index, matrix):
    sorted_matrix = []
    for row in matrix:
        sorted_row = []
        for cur_index, cur_sum in cols_sum_index:
            sorted_row.append(row[cur_index])
        sorted_matrix.append(sorted_row)
    return sorted_matrix

def print_matrix(matrix):
    #nested list comprehension code would be:
    #print('\n'.join([''.join(['{:4}'.format(item) for item in row])
    #  for row in col_sorted_matrix]))

    for row in matrix:
        print(''.join(['| {:4}'.format(str(col).zfill(3)) for col in row]),
              end='|\n')

if __name__ == '__main__':
    #Gets the base matrix
    matrix = get_matrix()

    #Gets a list[(index, sum)] containing the sum of each row/col, and the index each row/col in relation to the original matrix.
    #The returned list will be sorted in ascending order by the sum of the row/col.
    row_sum_list = get_sorted_rows_sum(matrix)
    col_sum_list = get_sorted_columns_sum(matrix)

    #created matrices sorted by sum of rows and cols
    row_sorted_matrix = get_row_sorted_matrix(row_sum_list, matrix)
    col_sorted_matrix = get_col_sorted_matrix(col_sum_list, matrix)

    #Prints data
    print('Row sum and indexes')
    print(row_sum_list)
    print('Col sum and indexes')
    print(col_sum_list)
    print()
    print('Original matrix')
    print_matrix(matrix)
    print()
    print('Matrix with ROWS sorted by their sums')
    print_matrix(row_sorted_matrix)
    print()
    print('Matrix with COLS sorted by their sums')
    print_matrix(col_sorted_matrix)
#END


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

# read train data set
BIXI_data = pd.read_csv("Data sets/Bixi Montreal Rentals 2018/Output from Alteryx/2018_BIXI_Stations_Temperature.csv", encoding= 'unicode_escape')

# explore the amount of unique variables
BIXI_data.columns = ['Month', 'Day', 'Hour', 'start_date', 'start_station_code', 'end_date',
                     'end_station_code', 'duration_sec', 'is_member', 'latitude',
                     'longitude', 'Temperature', 'Dew_point', 'Humidity',
                     'Wind_dir', 'Wind_spd', 'Stn_pressure']

print('Month:\n', BIXI_data.Month.value_counts(sort=False))
print('Day:\n', BIXI_data.Day.value_counts(sort=False))
print('Hour:\n', BIXI_data.Hour.value_counts(sort=False))
print('start_station_code:\n', BIXI_data.start_station_code.value_counts())
print('end_station_code:\n', BIXI_data.end_station_code.value_counts())
print('duration_sec:\n', BIXI_data.duration_sec.value_counts())
print('is_member:\n', BIXI_data.is_member.value_counts())
print('Temp (°C):\n', BIXI_data.Temperature.value_counts())
print('Dew Point Temp (°C):\n', BIXI_data.Dew_point.value_counts())
print('Rel Hum (%):\n', BIXI_data.Humidity.value_counts())
print('Wind Dir (10s deg):\n', BIXI_data.Wind_dir.value_counts())
print('Wind Spd (km/h):\n', BIXI_data.Wind_spd.value_counts())
print('Stn Press (kPa):\n', BIXI_data.Stn_pressure.value_counts())

# read data with new features created using Alteryx
all_BIXI_data = pd.read_csv("Data sets/Bixi Montreal Rentals 2018/Output from Alteryx/2018_BIXI_Stations_Temperature_Ratio_DoW_Bins_Demand.csv", encoding= 'unicode_escape')

# split into numerical values
df_numerical = all_BIXI_data[['Month', 'Day', 'Hour', 'is_Weekend', 'Duration_Bin', 'Temp_Bin', 'Hum_Bin', 'Wind_spd', 'Demand']]

# plot a heatmap showing the correlation between all numerical columns
print(df_numerical.corr())

#correlation heatmap of dataset
def correlation_heatmap(df):
    _ , ax = plt.subplots(figsize =(14, 12))
    colormap = sns.diverging_palette(220, 10, as_cmap = True)

    _ = sns.heatmap(
        df.corr(),
        cmap = colormap,
        square=True,
        cbar_kws={'shrink':.9 },
        ax=ax,
        annot=True,
        linewidths=0.1,vmax=1.0, linecolor='white',
        annot_kws={'fontsize':11 }
    )

    plt.title('Pearson Correlation of Features', y=1.05, size=15)

correlation_heatmap(df_numerical)
plt.show()


# uncompyle6 version 3.7.4
# Python bytecode 3.7 (3394)
# Decompiled from: Python 3.7.9 (tags/v3.7.9:13c94747c7, Aug 17 2020, 18:58:18) [MSC v.1900 64 bit (AMD64)]
# Embedded file name: T:\InGame\Gameplay\Scripts\Server\routing\waypoints\waypoint_stitching.py
# Compiled at: 2018-07-26 01:14:31
# Size of source mod 2**32: 4034 bytes
import itertools
from sims4.tuning.geometric import TunableDistanceSquared
from sims4.tuning.tunable import HasTunableSingletonFactory, AutoFactoryInit, TunableRange, TunableVariant
import sims4.math

class _WaypointStitchingBase(HasTunableSingletonFactory, AutoFactoryInit):

    def __call__(self, waypoints, loops=1):
        raise NotImplementedError

class _WaypointStitchingWaypoints(_WaypointStitchingBase, HasTunableSingletonFactory, AutoFactoryInit):
    FACTORY_TUNABLES = {'max_distance':TunableDistanceSquared(description="\n            If the route's cumulative distance is more than this value, the\n            route is split.\n            ",
       default=500),
     'max_goals':TunableRange(description='\n            If the route has more than this number of goals, the route is\n            split.\n            ',
       tunable_type=int,
       minimum=0,
       default=200)}

    def __call__(self, waypoints, loops=1):
        waypoint_groups = [[]]
        group_dist = 0
        group_num_goals = 0
        previous_centroid = None
        for goals in itertools.chain.from_iterable(itertools.repeat(waypoints, loops)):
            num_goals = len(goals)
            centroid = sum([goal.position for goal in goals], sims4.math.Vector3.ZERO()) / num_goals
            dist = (previous_centroid - centroid).magnitude_2d_squared() if previous_centroid is not None else 0
            current_group = waypoint_groups[(-1)]
            if len(current_group) > 1:
                if dist + group_dist > self.max_distance:
                    if num_goals + group_num_goals > self.max_goals:
                        waypoint_groups.append(current_group)
                        current_group = [current_group[(-1)]]
                        group_dist = 0
                        group_num_goals = len(current_group[0])
            current_group.append(goals)
            group_dist += dist
            group_num_goals += num_goals
            previous_centroid = centroid

        yield from waypoint_groups
        if False:
            yield None

class _WaypointStitchingNone(_WaypointStitchingBase, HasTunableSingletonFactory, AutoFactoryInit):

    def __call__(self, waypoints, loops=1):
        for _ in range(loops):
            for goals in waypoints:
                yield [
                 goals]

class WaypointStitchingVariant(TunableVariant):

    def __init__(self, *args, **kwargs):
        variants = {'waypoints':_WaypointStitchingWaypoints.TunableFactory(),
         'none':_WaypointStitchingNone.TunableFactory(),
         'default':'none'}
        kwargs.update(variants)
        (super().__init__)(*args, **kwargs)


# Parse logs to create graphs

# Returns the time in nanosec
def parse_time(line):
    # Find the start index
    factor = 1
    end_ind = 4
    if line.endswith("ns\n"):
        factor = 1
    elif line.endswith("µs\n"):
        factor = 1000
    elif line.endswith("ms\n"):
        factor = 10 ** 6
    elif line.endswith("s\n"):
        end_ind = 3
        factor = 10 ** 9
    else:
        print(line)
        raise Exception("Invalid line")

    start_ind = -1
    first_dot = False
    for i in range(len(line) - end_ind, -1, -1):
        # print(line[i])
        if not line[i].isdigit():
            if not first_dot:
                first_dot = True
            else:
                start_ind = i + 1
                break

    t = line[start_ind : len(line) - end_ind + 1]
    t = float(t) * factor
    return t

def parse_int(line):
    return int(line.split(":")[1].strip())

class expr_res:
    def init_comm_time(self):
        return (
            self.init_partial_output_comm_time
            + self.init_commit_latency_combine_time
            + self.prover_init_time
        )

    def first_round_time(self):
        return (
            self.first_round_comm_time
            + self.first_round_latency
            + self.first_round_combine_time
        )

    def second_round_time(self):
        return (
            self.second_round_comm_time
            + self.second_round_latency
            + self.second_round_combine_time
        )

    def third_fourth_round_time(self):
        return (
            self.third_round_local_time
            + self.third_round_comm_time
            + self.fourth_round_local_time
            + self.fourth_round_comm_time
        )

    def pc_proof_time(self):
        return (
            self.pc_proof_init_time
            + self.pc_proof_create_time
            + self.local_eval_time / self.num_servers
            # + self.poly_eval_time/self.num_servers
            + self.eval_combine_time
            + self.local_prf_time
            + self.prf_combine_time
        )

    def total_prover_time(self):
        return (
            self.init_comm_time()
            + self.first_round_time()
            + self.second_round_time()
            + self.third_fourth_round_time()
            + self.pc_proof_time()
        )

    def total_communication_cost(self):
        per_party_cost = (
            self.init_round_communication_cost
            + self.first_round_communication_cost
            + self.second_round_communication_cost
        )
        return self.num_servers * per_party_cost

    def auditor_time(self):
        return self.total_verify_time

    def __init__(self, filename):
        self.prover_init_time = 0
        self.init_partial_output_comm_time = 0
        self.first_round_comm_time = 0
        self.second_round_comm_time = 0
        self.init_round_communication_cost = 0
        self.local_eval_time = 0
        self.eval_combine_time = 0
        self.local_prf_time = 0
        with open(filename) as f:
            for i, line in enumerate(f.readlines()):
                if "statement length" in line:
                    self.stmt_len = int(line.split(":")[1].strip())
                elif "Number of constraints" in line:
                    self.num_constraints = int(line.split(":")[1].strip())
                elif "Number of servers" in line:
                    self.num_servers = int(line.split(":")[1].strip())
                elif "End:     KZG10::Setup" in line:
                    self.setup_time = parse_time(line)
                elif "End:     Marlin::Index " in line:
                    self.index_time = parse_time(line)
                if "End:     Computing Partial Commitments to output" in line:
                    self.init_partial_output_comm_time = max(
                        self.init_partial_output_comm_time, parse_time(line)
                    )
                elif "End:     Initial Commitment and Output Check time" in line:
                    self.init_commit_latency_combine_time = parse_time(line)
                elif "··End:     Initize prover for all servers" in line:
                    self.prover_init_time = max(self.prover_init_time, parse_time(line))
                elif "··End:     Committing to first round polys" in line:
                    self.first_round_comm_time = max(
                        self.first_round_comm_time, parse_time(line)
                    )
                elif "··End:     First Round communication" in line:
                    self.first_round_latency = parse_time(line)
                elif "··End:     Combining partial commitments" in line:
                    self.first_round_combine_time = parse_time(line)
                elif "··End:     Committing to second round polys" in line:
                    self.second_round_comm_time = max(
                        self.second_round_comm_time, parse_time(line)
                    )
                elif "··End:     Second Round communication" in line:
                    self.second_round_latency = parse_time(line)
                elif "··End:     Second Round Combine time" in line:
                    self.second_round_combine_time = parse_time(line)
                elif "··End:     AHP::Prover::ThirdRound" in line:
                    self.third_round_local_time = parse_time(line)
                elif "End:     Committing to third round polys" in line:
                    self.third_round_comm_time = parse_time(line)
                elif "··End:     AHP::Prover::FourthRound" in line:
                    self.fourth_round_local_time = parse_time(line)
                elif "··End:     Committing to fourth round polys" in line:
                    self.fourth_round_comm_time = parse_time(line)
                elif "··End:     PC proof init time" in line:
                    self.pc_proof_init_time = parse_time(line)
                elif "End:     Evaluating polynomials over query set" in line:
                    self.poly_eval_time = parse_time(line)
                elif "End:     PC Proof creation time" in line:
                    self.pc_proof_create_time = parse_time(line)
                elif "End:     Marlin::Verify" in line:
                    self.total_verify_time = parse_time(line)
                elif "End:     Check the statement time" in line:
                    self.stmt_only_verify_time = parse_time(line)
                # Communication related parsing.
                elif "Init Round Total Communication:" in line:
                    self.init_round_communication_cost += parse_int(line)
                elif "First round  Total shares size in bytes:" in line:
                    self.first_round_communication_cost = parse_int(line)
                elif "Second Round Total shares in bytes:" in line:
                    self.second_round_communication_cost = parse_int(line)
                elif "Argument size in bytes:" in line:
                    print(line)
                    self.proof_size = parse_int(line)
                    print(self.proof_size)
                # More detail times
                elif "····End:     Combine evaluation" in line:
                    self.eval_combine_time += parse_time(line)
                elif "···End:     Local polynomial evaluation" in line:
                    self.local_eval_time += parse_time(line)
                elif "End:     Generating local Proof shares" in line:
                    self.local_prf_time = max(self.local_prf_time, parse_time(line))
                elif "End:     Combining MPC proofs!" in line:
                    self.prf_combine_time = parse_time(line)
                elif "End:     Auditer average time" in line:
                    self.auditer_avg_time = parse_time(line) / 100
                # TODO proofs parse

num_mpc_server_vec = [2, 8,  32]
stmt_len_vec = [4, 8, 16, 32, 64]
num_contraints_vec = [2 ** i for i in range(11, 21)]
import numpy as np
import matplotlib.pyplot as plt
import matplotlib

def plot_prover_time_vs_constraints(plt, expr_dict, num_servers):
    import copy

    stmt_len = 8
    x = np.array(num_contraints_vec)
    y_vals = []
    for i, x_elem in enumerate(x):
        y = []
        x_new = []
        for count in range(10):
            if expr_dict[(x_elem, num_servers, stmt_len, count)] is not None:
                print(x_elem, num_servers)
                y.append(
                    expr_dict[(x_elem, num_servers, stmt_len, count)].total_prover_time() / 10 ** 9
                )
        y_vals.append(y)

    y_vals = np.array(y_vals)
    for i,row in enumerate(y_vals):
        y_vals[i] = np.array(row)

    errors_bar_y = np.zeros(len(x))
    y_vals_avg = np.zeros(len(x))
    print(y_vals)
    for i,row in enumerate(y_vals):
        errors_bar_y[i] = 1.95*np.std(y_vals[i])/(len(y_vals[i])**0.5)
        y_vals_avg[i] = np.average(y_vals[i])

    print(errors_bar_y)

    print(y_vals_avg)

    plt.minorticks_off()
    plt.errorbar(
        x,
        y_vals_avg*1000/x,
        yerr = errors_bar_y*1000/x,
        # marker=matplotlib.markers.CARETRIGHTBASE,
        # capthick=1,
        capsize=5,
        # uplims=True,
        # lolims=True,
        label=str(num_servers) + " servers",
    )

def plot_auditor_time_vs_stmt_len(plt, expr_dict, num_constraint):
    import copy

    num_server = 1
    x = np.array(stmt_len_vec)
    y = []
    for i, x_elem in enumerate(x):
        y = []
        x_new = []
        for count in range(10):
            if expr_dict[(x_elem, num_servers, stmt_len, count)] is not None:
                print(x_elem, num_servers)
                y.append(
                    expr_dict[(x_elem, num_servers, stmt_len, count)].auditer_avg_time / 10 ** 6
                )
        y_vals.append(y)

    y_vals = np.array(y_vals)
    for i,row in enumerate(y_vals):
        y_vals[i] = np.array(row)

    errors_bar_y = np.zeros(len(x))
    y_vals_avg = np.zeros(len(x))
    print(y_vals)
    for i,row in enumerate(y_vals):
        errors_bar_y[i] = 1.95*np.std(y_vals[i])/(len(y_vals[i])**0.5)
        y_vals_avg[i] = np.average(y_vals[i])

    print(errors_bar_y)

    print(y_vals_avg)

    plt.minorticks_off()
    plt.errorbar(
        x,
        y_vals_avg,
        yerr = errors_bar_y,
        marker=matplotlib.markers.CARETDOWNBASE,
        label=str(num_servers) + " servers",
    )

def plot_proof_size_vs_stmt_len(plt, expr_dict, num_contraints):
    import copy

    num_server = 8
    x = np.array(stmt_len_vec)
    y = []
    x_new = []
    for i, x_elem in enumerate(x):
        if expr_dict[(num_server, num_contraints, x_elem)] is not None:
            # print(x_elem, num_contraints)
            y.append(expr_dict[(num_server, num_contraints, x_elem)].proof_size)
            x_new.append(x_elem)
    x, y = np.array(x_new), np.array(y)
    print(x, y)
    plt.minorticks_off()
    plt.plot(
        x,
        y,
        marker=matplotlib.markers.CARETDOWNBASE,
        label="$2^{" + str((num_contraints.bit_length() - 1)) + "}$ constraints",
    )

def plot_comm_cost_vs_stmt_len(plt, expr_dict, stmt_len):
    import copy

    num_contraints = 2 ** 10
    x = np.array(num_mpc_server_vec)
    y = []
    x_new = []
    for i, x_elem in enumerate(x):
        if expr_dict[(x_elem, num_contraints, stmt_len)] is not None:
            # print(x_elem, num_contraints)
            y.append(
                expr_dict[(x_elem, num_contraints, stmt_len)].total_communication_cost()
                / 2 ** 20
            )
            x_new.append(x_elem)
    x, y = np.array(x_new), np.array(y)
    # print(x, y)
    plt.minorticks_off()
    plt.plot(
        x,
        y,
        marker=matplotlib.markers.CARETDOWNBASE,
        label="$2^{" + str((stmt_len.bit_length() - 1)) + "}$ statement length",
    )

two_powers_text = ["$" + str(2) + "^{" + str(i) + "}$" for i in range(0, 21)]

if __name__ == "__main__":
    run_name = "auction_logs"

    expr_dict = dict()
    for i in range(10):
        for num_mpc_server in num_mpc_server_vec:
            for num_constraints in num_contraints_vec:
                for stmt_len in [8]:
                    out_filename = (
                        "./bench_logs/"
                        + run_name
                        + "/params_"
                        + str(num_constraints)
                        + "_"
                        + str(num_mpc_server)
                        + "_"
                        + str(stmt_len)
                        + "_"
                        + str(i)
                        + ".txt"
                    )
                    try:
                        print(out_filename)
                        expr_dict[(num_constraints, num_mpc_server, stmt_len, i)] = expr_res(
                            out_filename
                        )
                    except FileNotFoundError:
                        expr_dict[(num_constraints, num_mpc_server, stmt_len, i)] = None
                    except:
                        raise

    fig, ((ax1, ax2),(ax3, ax4)) = plt.subplots(2, 2)
    # fig = plt.figure()
    ax1 = plt.figure(figsize=(7,3)).add_subplot()
    ax1.set_ylim(0, 2)
    # ax2 = plt.figure().add_subplot()
    # ax3 = plt.figure().add_subplot()
    # ax4 = plt.figure().add_subplot()

    ax1.tick_params(axis='both', which='major', labelsize=15)
    # ax2.tick_params(axis='both', which='major', labelsize=11)
    # ax3.tick_params(axis='both', which='major', labelsize=11)
    # ax4.tick_params(axis='both', which='major', labelsize=11)

    ax1.set_xscale("log")
    # ax1.set_yscale("log")
    ax1.set_xlabel("Number of constraints", fontsize = 15)
    ax1.set_ylabel("Prover time per \n constraint (ms)", fontsize = 15)

    ax1.set_xticks(num_contraints_vec)
    ax1.grid(True, linestyle = '--')
    ax1.set_xticklabels(two_powers_text[10 : 21 + 1])
    for num_servers in num_mpc_server_vec:
    	plot_prover_time_vs_constraints(ax1, expr_dict, num_servers)
    ax1.legend(fontsize = 13, ncol =3)
    plt.tight_layout()
    # ax2.set_xscale("log")
    # # ax2.set_yscale("log")
    # ax2.set_xlabel("length of statement $|X|$ (Group elements)", fontsize=14)
    # ax2.set_ylabel("Auditor time (ms)", fontsize=14)

    # ax2.grid(True, linestyle="--")
    # ax2.set_xticks(stmt_len_vec)
    # ax2.set_xticklabels(two_powers_text[2 : 6 + 1])
    # ax2.set_yticks(np.linspace(0, 100, 11, dtype="int"))
    # ax2.set_ylim(0, 100)
    # ax2.set_yticklabels(np.linspace(0, 100, 11, dtype="int"))
    # for num_contraints in num_contraints_vec[::2]:
    #     plot_auditor_time_vs_stmt_len(ax2, expr_dict, num_contraints)
    # ax2.legend(fontsize=12)

    # ax3.set_ylabel("Proof size(bytes)", fontsize = 14)
    # ax3.set_xlabel("length of statement $|X|$ (Group elements)", fontsize = 14)

    # ax3.grid(True, linestyle = '--')
    # ax3.set_xticks(stmt_len_vec)
    # ax3.set_xticklabels(two_powers_text[2 : 6 + 1])
    # for num_contraints in num_contraints_vec[::2]:
    # 	plot_proof_size_vs_stmt_len(ax3, expr_dict, num_contraints)
    # ax3.legend(fontsize = 12)

    # ax4.set_ylabel("Communication Cost(MegaBytes)", fontsize = 14)
    # ax4.set_xlabel("Number of MPC servers(n)", fontsize = 14)

    # ax4.grid(True, linestyle = '--')
    # ax4.set_xticks(num_mpc_server_vec)
    # ax4.set_xticklabels(two_powers_text[1 : 6 + 1])
    # for stmt_len in stmt_len_vec:
    # 	plot_comm_cost_vs_stmt_len(ax4, expr_dict, stmt_len)
    # ax4.legend(fontsize = 12)

    plt.show()