import math
import numpy as np
import csv
from itertools import chain
import random

Feature_number=4
all_Feature=False
CUS_NUMBER=50
Training_number=50
count_setosa = 0
count_versicolor = 0
count_virginica = 0

k=0
l=40
fold = []
Output = []
variance = []
h =np.array([0.01,0.5,10])

confussion_matrix = [[0] * 3] * 3
confussion_matrix1 = [[0] * 3] * 3
confussion_matrix2 = [[0] * 3] * 3
confussion_matrix3 = [[0] * 3] * 3
confussion_matrix4 = [[0] * 3] * 3

Iris_setosa= []
setosa_train1=[]
setosa_train2=[]
setosa_train3=[]
setosa_train4=[]
setosa_train5=[]

Iris_versicolor=[]
versicolor_train1=[]
versicolor_train2=[]
versicolor_train3=[]
versicolor_train4=[]
versicolor_train5=[]

Iris_virginica=[]
virginica_train1=[]
virginica_train2=[]
virginica_train3=[]
virginica_train4=[]
virginica_train5=[]

maximum_density = np.array([])

Iris=[]
label=["sepal_length", "sepal_width", "petal_length", "petal_width", "class"]

def get_mean_vector(A):
    mean_vector=[]
    for i in range(Feature_number):
        sum=0
        for value in A[i]:
            sum=sum+ float(value)#accumulate all element in row i
        mean_vector.append(float(sum/len(A[i])))#add average value to MEAN_VECTOR
    return mean_vector

def norm_pdf_multivariate(x,mu,sigma):

    size = len(x)
    if size == len(mu) and (size, size) == sigma.shape:
        det = np.linalg.det(sigma)
        if det == 0:
            raise NameError("The covariance matrix can't be singular")

        norm_const = 1.0/ ( math.pow((2*3.1416),float(size)/2) * math.pow(det,1.0/2) )
        zip_object = zip(x, mu)
        x_mu = []
        for list1_i, list2_i in zip_object:
             x_mu.append(list1_i-list2_i)

        numpy_array_x_mu = np.array(x_mu)
        inv = np.linalg.inv(sigma)
        result = math.pow(math.e, -0.5 * (numpy_array_x_mu .dot( inv ).dot( numpy_array_x_mu.T)))
        return norm_const * result
    else:
        raise NameError("The dimensions of the input don't match")

def data_processing():
    X=-1
    fn=open("iris.data","r")
    for row in csv.DictReader(fn,label):
        X=X+1
        for i in range(Feature_number):
            Iris.append(row[label[i]])
            if str(row["class"]) == "Iris-setosa":
                if all_Feature== True:
                    Iris_setosa.append(row[label[i]])
                else:
                    if X%(Training_number/CUS_NUMBER)==0 and len(Iris_setosa)<CUS_NUMBER*4:
                        Iris_setosa.append(row[label[i]])
            elif str(row["class"]) == "Iris-versicolor":
                if all_Feature== True:
                    Iris_versicolor.append(row[label[i]])
                else:
                    if X%(Training_number/CUS_NUMBER)==0 and len(Iris_versicolor)<CUS_NUMBER*4:
                        Iris_versicolor.append(row[label[i]])
            else:
                    if all_Feature== True:
                        Iris_virginica.append(row[label[i]])
                    else:
                        if X%(Training_number/CUS_NUMBER)==0 and len(Iris_virginica)<CUS_NUMBER*4:
                            Iris_virginica.append(row[label[i]])
    fn.close()

def to_matrix(l, n):
    return [l[i:i+n] for i in range(0, len(l), n)]

def data_availablity(test_I):

     setosa = Iris_setosa
     versicolor = Iris_versicolor
     virginica = Iris_virginica
     setosa = to_matrix(setosa,4)
     #print(setosa)
     versicolor = to_matrix(versicolor,4)
     virginica = to_matrix(virginica,4)
     setosa = [[float(y) for y in x] for x in setosa]
     versicolor = [[float(y) for y in x] for x in versicolor]
     virginica = [[float(y) for y in x] for x in virginica]

     for i in range(50):
      if(test_I == setosa[i]):
          return 'setosa';
      elif(test_I == versicolor[i]):
          return 'versicolor'
      elif(test_I == virginica[i]):
          return 'virginica';

def get_variance(data):
      n = len(data)
      mean = sum(data) / n
      deviations = [(x - mean) ** 2 for x in data]
      variance = sum(deviations) / n
      return variance

def generate_parzebWindow_confussion_matrix(test_data,train_data,bandWidth,mean_setosa,mean_versicolor,
                               mean_virginica,covMatrix_setosa,covMatrix_versicolor,covMatrix_virginica):
      C_matrix = np.array([[0] * 3] * 3)
      Iris_setosa_Posteriror = 0
      Iris_versicolor_Posterior = 0
      Iris_virginica_Posterior = 0
      total_setosa_Posteriror = 0
      total_versicolor_Posterior = 0
      total_virginica_Posterior = 0

      for i in range(len(test_data)):
          for j in range(len(train_data)):
               zip_object = zip(test_data[i], train_data[j])
               X_XI= []
               for list1_i, list2_i in zip_object:
                 X_XI.append(list1_i-list2_i)

               newList = [x / bandWidth for x in X_XI]
               Iris_setosa_Posteriror =  (norm_pdf_multivariate(newList, mean_setosa, covMatrix_setosa))
               total_setosa_Posteriror = total_setosa_Posteriror + Iris_setosa_Posteriror
               Iris_versicolor_Posterior = (norm_pdf_multivariate(newList, mean_versicolor, covMatrix_versicolor))
               total_versicolor_Posterior = total_versicolor_Posterior + Iris_versicolor_Posterior
               Iris_virginica_Posterior = norm_pdf_multivariate(newList, mean_virginica, covMatrix_virginica)
               total_virginica_Posterior = total_virginica_Posterior + Iris_virginica_Posterior

          setosa_density= (1/(40*pow(bandWidth, 4)))*total_setosa_Posteriror
          versicolor_dansity = (1/(40*pow(bandWidth, 4)))*total_versicolor_Posterior
          virginica_density = (1/(40*pow(bandWidth, 4)))*total_virginica_Posterior
          #print(setosa_density,versicolor_dansity,virginica_density,"\n")
          maximum_density = max(setosa_density,versicolor_dansity,virginica_density)
          #print(maximum_density)
          class_name = data_availablity(test_data[i])
          #print(class_name)
          if((class_name == 'setosa') and maximum_density == setosa_density):
                  C_matrix[0][0] = C_matrix[0][0] + 1;
          elif((class_name == 'setosa') and maximum_density == versicolor_dansity):
                  C_matrix[0][1] = C_matrix[0][1] + 1;
          elif((class_name== 'setosa') and maximum_density == virginica_density):
                  C_matrix[0][2]= C_matrix[0][2] + 1;
          elif((class_name == 'versicolor') and maximum_density == setosa_density):
                 C_matrix[1][0] = C_matrix[1][0] + 1;
          elif((class_name == 'versicolor') and maximum_density == versicolor_dansity):
                  C_matrix[1][1]= C_matrix[1][1] + 1;
          elif((class_name == 'versicolor') and maximum_density == virginica_density):
                  C_matrix[1][2]=C_matrix[1][2] + 1;
          elif((class_name== 'virginica') and maximum_density == setosa_density):
                  C_matrix[2][0] = C_matrix[2][0] + 1;
          elif((class_name== 'virginica') and maximum_density == versicolor_dansity):
                 C_matrix[2][1]=C_matrix[2][1] + 1;
          elif((class_name== 'virginica') and maximum_density == virginica_density):
                  C_matrix[2][2] = C_matrix[2][2] + 1;
          #print(C_matrix)

      return  C_matrix

if __name__ == "__main__":

    data_processing()

    for i in range(5):
        fold.append(Iris_setosa[k:l])
        fold.append(Iris_versicolor[k:l])
        fold.append(Iris_virginica[k:l])
        k = l
        l= l+40

    fold = list(chain.from_iterable(fold))
    a = (to_matrix(fold,4))

test_x = a[0:30]
train_y = a[30:150]

test_x1 = a[30:60]

train_y1 = a[0:30]

for i  in range(60,150):
    train_y1.append(a[i])

test_x2 = a[60:90]
train_y2 = a[0:60]
for i  in range(90,150):
    train_y2.append(a[i])

test_x3 = a[90:120]
train_y3 = a[0:90]
for i  in range(120,150):
    train_y3.append(a[i])

test_x4 = a[120:150]
train_y4 = a[0:120]

train_y = [[float(y) for y in x] for x in train_y]
test_x = [[float(y) for y in x] for x in test_x]

bandWidth = h[0]

random.shuffle(train_y)
random.shuffle(test_x)

for i in range(len(train_y)):
    if(data_availablity(train_y[i])== 'setosa'):
        setosa_train1.append(train_y[i])
    elif(data_availablity(train_y[i]) == 'versicolor'):
        versicolor_train1.append(train_y[i])
    else: virginica_train1.append(train_y[i])

setosa_train1 = np.array(setosa_train1)
#mean_setosa_result1 = get_mean_vector(setosa_train1)
mean_setosa_result1 = np.mean(setosa_train1, axis= 0)
variance_setosa1 = np.var(setosa_train1, axis = 0)

covMatrix_setosa_result1 = [[0] * 4] * 4
covMatrix_setosa_result1 = [[0] * len(variance_setosa1) for _ in range(len(variance_setosa1))]
for i, e in enumerate(variance_setosa1):
    covMatrix_setosa_result1[i][i] = e

covMatrix_setosa_result1= np.array(covMatrix_setosa_result1)

versicolor_train1 = np.array(versicolor_train1)
#mean_versicolor_result1 = get_mean_vector(versicolor_train1)
mean_versicolor_result1 = np.mean(versicolor_train1,axis =0)
covMatrix_versicolor_result1 = [[0] * 4] * 4
variance_versicolor1= np.var(versicolor_train1, axis = 0)

covMatrix_versicolor_result1 = [[0] * len(variance_versicolor1) for _ in range(len(variance_versicolor1))]
for i, e in enumerate(variance_versicolor1):
    covMatrix_versicolor_result1[i][i] = e

covMatrix_versicolor_result1 = np.array(covMatrix_versicolor_result1)

virginica_train1 = np.array(virginica_train1)
#mean_virginica_result1 = get_mean_vector(virginica_train1)
mean_virginica_result1 = np.mean(virginica_train1, axis=0)

covMatrix_virginica_result1= [[0] * 4] * 4
variance_virginica1= np.var(virginica_train1, axis = 0)

covMatrix_virginica_result1 = [[0] * len(variance_virginica1) for _ in range(len(variance_virginica1))]
for i, e in enumerate(variance_virginica1):
    covMatrix_virginica_result1[i][i] = e

covMatrix_virginica_result1 = np.array(covMatrix_virginica_result1)

confussion_matrix = np.array(generate_parzebWindow_confussion_matrix(test_x,train_y,bandWidth,mean_setosa_result1,mean_versicolor_result1,
                                                        mean_virginica_result1,covMatrix_setosa_result1,
                                                        covMatrix_versicolor_result1,covMatrix_virginica_result1))

Sum1 = np.trace(confussion_matrix)
Accuracy1 = (Sum1/30)*100
variance.append(Accuracy1)
print("confussion matrix of first fold:\n",confussion_matrix,"\n")
print("Accuracy of First fold:", Accuracy1,"%")

train_y1 = [[float(y) for y in x] for x in train_y1]
test_x1 = [[float(y) for y in x] for x in test_x1]

random.shuffle(train_y1)
random.shuffle(test_x1)

for i in range(len(train_y1)):
    if(data_availablity(train_y1[i])== 'setosa'):
        setosa_train2.append(train_y1[i])
    elif(data_availablity(train_y1[i]) == 'versicolor'):
        versicolor_train2.append(train_y1[i])
    else: virginica_train2.append(train_y1[i])

setosa_train2 = np.array(setosa_train2)
#mean_setosa_result2 = get_mean_vector(setosa_train2)
mean_setosa_result2 = np.mean(setosa_train2, axis =0)
variance_setosa2 = np.var(setosa_train2, axis = 0)

covMatrix_setosa_result2 = [[0] * 4] * 4
covMatrix_setosa_result2 = [[0] * len(variance_setosa2) for _ in range(len(variance_setosa2))]
for i, e in enumerate(variance_setosa2):
    covMatrix_setosa_result2[i][i] = e

covMatrix_setosa_result2= np.array(covMatrix_setosa_result2)

versicolor_train2 = np.array(versicolor_train2)
#mean_versicolor_result2 = get_mean_vector(versicolor_train2)
mean_versicolor_result2 = np.mean(versicolor_train2, axis=0)
covMatrix_versicolor_result2 = [[0] * 4] * 4
variance_versicolor2= np.var(versicolor_train2, axis = 0)

covMatrix_versicolor_result2 = [[0] * len(variance_versicolor2) for _ in range(len(variance_versicolor2))]
for i, e in enumerate(variance_versicolor2):
    covMatrix_versicolor_result2[i][i] = e

covMatrix_versicolor_result2 = np.array(covMatrix_versicolor_result2)

virginica_train2 = np.array(virginica_train2)
#mean_virginica_result2 = get_mean_vector(virginica_train2)
mean_virginica_result2 = np.mean(virginica_train2, axis=0)

covMatrix_virginica_result2= [[0] * 4] * 4
variance_virginica2= np.var(virginica_train2, axis = 0)

covMatrix_virginica_result2 = [[0] * len(variance_virginica2) for _ in range(len(variance_virginica2))]
for i, e in enumerate(variance_virginica2):
    covMatrix_virginica_result2[i][i] = e

covMatrix_virginica_result2 = np.array(covMatrix_virginica_result2)

confussion_matrix1 = np.array(generate_parzebWindow_confussion_matrix(test_x1,train_y1,bandWidth,mean_setosa_result2,mean_versicolor_result2,
                                                       mean_virginica_result2,covMatrix_setosa_result2,
                                                       covMatrix_versicolor_result2,covMatrix_virginica_result2))

Sum2 = np.trace(confussion_matrix1)
Accuracy2 = (Sum2/30)*100
variance.append(Accuracy2)
print("confussion matrix of second fold:\n",confussion_matrix1,"\n")
print("Accuracy of 2nd fold:", Accuracy2,"%")

train_y2 = [[float(y) for y in x] for x in train_y2]
test_x2 = [[float(y) for y in x] for x in test_x2]

random.shuffle(train_y2)
random.shuffle(test_x2)

for i in range(len(train_y2)):
    if(data_availablity(train_y2[i])== 'setosa'):
        setosa_train3.append(train_y2[i])
    elif(data_availablity(train_y2[i]) == 'versicolor'):
        versicolor_train3.append(train_y2[i])
    else: virginica_train3.append(train_y2[i])

setosa_train3 = np.array(setosa_train3)
#mean_setosa_result3 = get_mean_vector(setosa_train3)
mean_setosa_result3 = np.mean(setosa_train3, axis=0)
variance_setosa3 = np.var(setosa_train3, axis = 0)

covMatrix_setosa_result3 = [[0] * 4] * 4
covMatrix_setosa_result3 = [[0] * len(variance_setosa3) for _ in range(len(variance_setosa3))]
for i, e in enumerate(variance_setosa3):
    covMatrix_setosa_result3[i][i] = e

covMatrix_setosa_result3 = np.array(covMatrix_setosa_result3)

versicolor_train3 = np.array(versicolor_train3)
#mean_versicolor_result3 = get_mean_vector(versicolor_train3)
mean_versicolor_result3 = np.mean(versicolor_train3, axis =0)
covMatrix_versicolor_result3 = [[0] * 4] * 4
variance_versicolor3= np.var(versicolor_train3, axis = 0)

covMatrix_versicolor_result3 = [[0] * len(variance_versicolor3) for _ in range(len(variance_versicolor3))]
for i, e in enumerate(variance_versicolor3):
    covMatrix_versicolor_result3[i][i] = e

covMatrix_versicolor_result3 = np.array(covMatrix_versicolor_result3)

virginica_train3 = np.array(virginica_train3)
#mean_virginica_result3 = get_mean_vector(virginica_train3)
mean_virginica_result3 = np.mean(virginica_train3, axis=0)

covMatrix_virginica_result3= [[0] * 4] * 4
variance_virginica3= np.var(virginica_train3, axis = 0)

covMatrix_virginica_result3 = [[0] * len(variance_virginica3) for _ in range(len(variance_virginica3))]
for i, e in enumerate(variance_virginica3):
    covMatrix_virginica_result3[i][i] = e

covMatrix_virginica_result3 = np.array(covMatrix_virginica_result3)

confussion_matrix2 = np.array(generate_parzebWindow_confussion_matrix(test_x2,train_y2,bandWidth,mean_setosa_result3,mean_versicolor_result3,
                                                       mean_virginica_result3,covMatrix_setosa_result3,
                                                        covMatrix_versicolor_result3,covMatrix_virginica_result3))
Sum3 = np.trace(confussion_matrix2)
Accuracy3 = (Sum3/30)*100
variance.append(Accuracy3)
print("confussion matrix of 3rd fold:\n",confussion_matrix2,"\n")
print("Accuracy of 3rd fold:", Accuracy3,"%")

train_y3 = [[float(y) for y in x] for x in train_y3]
test_x3 = [[float(y) for y in x] for x in test_x3]

random.shuffle(train_y3)
random.shuffle(test_x3)

for i in range(len(train_y3)):
    if(data_availablity(train_y3[i])== 'setosa'):
        setosa_train4.append(train_y3[i])
    elif(data_availablity(train_y3[i]) == 'versicolor'):
        versicolor_train4.append(train_y3[i])
    else: virginica_train4.append(train_y3[i])

setosa_train4 = np.array(setosa_train4)
#mean_setosa_result4 = get_mean_vector(setosa_train4)
mean_setosa_result4 = np.mean(setosa_train4, axis =0)
variance_setosa4 = np.var(setosa_train4, axis = 0)

covMatrix_setosa_result4 = [[0] * 4] * 4
covMatrix_setosa_result4 = [[0] * len(variance_setosa4) for _ in range(len(variance_setosa4))]
for i, e in enumerate(variance_setosa4):
    covMatrix_setosa_result4[i][i] = e

covMatrix_setosa_result4 = np.array(covMatrix_setosa_result4)

versicolor_train4 = np.array(versicolor_train4)
#mean_versicolor_result4 = get_mean_vector(versicolor_train4)
mean_versicolor_result4 = np.mean(versicolor_train4, axis =0)
covMatrix_versicolor_result4 = [[0] * 4] * 4
variance_versicolor4= np.var(versicolor_train4, axis = 0)

covMatrix_versicolor_result4 = [[0] * len(variance_versicolor4) for _ in range(len(variance_versicolor4))]
for i, e in enumerate(variance_versicolor4):
    covMatrix_versicolor_result4[i][i] = e

covMatrix_versicolor_result4 = np.array(covMatrix_versicolor_result4)

virginica_train4 = np.array(virginica_train4)
#mean_virginica_result4 = get_mean_vector(virginica_train4)
mean_virginica_result4 = np.mean(virginica_train4, axis=0)

covMatrix_virginica_result4= [[0] * 4] * 4
variance_virginica4= np.var(virginica_train4, axis = 0)

covMatrix_virginica_result4 = [[0] * len(variance_virginica4) for _ in range(len(variance_virginica4))]
for i, e in enumerate(variance_virginica4):
    covMatrix_virginica_result4[i][i] = e

covMatrix_virginica_result4 = np.array(covMatrix_virginica_result4)

confussion_matrix3 = np.array(generate_parzebWindow_confussion_matrix(test_x3,train_y3,bandWidth,mean_setosa_result4,mean_versicolor_result4,
                                                        mean_virginica_result4,covMatrix_setosa_result4,
                                                        covMatrix_versicolor_result4,covMatrix_virginica_result4))

Sum4 = np.trace(confussion_matrix3)
Accuracy4 = (Sum4/30)*100
variance.append(Accuracy4)
print("confussion matrix of 4th fold:\n",confussion_matrix3,"\n")
print("Accuracy of 4th fold:", Accuracy4,"%")

train_y4 = [[float(y) for y in x] for x in train_y4]
test_x4 = [[float(y) for y in x] for x in test_x4]

random.shuffle(train_y4)
random.shuffle(test_x4)

for i in range(len(train_y4)):
    if(data_availablity(train_y4[i])== 'setosa'):
        setosa_train5.append(train_y4[i])
    elif(data_availablity(train_y4[i]) == 'versicolor'):
        versicolor_train5.append(train_y4[i])
    else: virginica_train5.append(train_y4[i])

setosa_train5 = np.array(setosa_train5)
#mean_setosa_result5 = get_mean_vector(setosa_train5)
mean_setosa_result5 = np.mean(setosa_train5, axis=0)
variance_setosa5 = np.var(setosa_train5, axis = 0)

covMatrix_setosa_result5 = [[0] * 4] * 4
covMatrix_setosa_result5 = [[0] * len(variance_setosa5) for _ in range(len(variance_setosa5))]
for i, e in enumerate(variance_setosa5):
    covMatrix_setosa_result5[i][i] = e

covMatrix_setosa_result5 = np.array(covMatrix_setosa_result5)

versicolor_train5 = np.array(versicolor_train5)
#mean_versicolor_result5 = get_mean_vector(versicolor_train5)
mean_versicolor_result5 = np.mean(versicolor_train5, axis =0)
covMatrix_versicolor_result5 = [[0] * 4] * 4
variance_versicolor5= np.var(versicolor_train5, axis = 0)

covMatrix_versicolor_result5 = [[0] * len(variance_versicolor5) for _ in range(len(variance_versicolor4))]
for i, e in enumerate(variance_versicolor4):
    covMatrix_versicolor_result5[i][i] = e

covMatrix_versicolor_result5 = np.array(covMatrix_versicolor_result5)

virginica_train5 = np.array(virginica_train5)
#mean_virginica_result5 = get_mean_vector(virginica_train5)
mean_virginica_result5 = np.mean(virginica_train5, axis=0)

covMatrix_virginica_result5= [[0] * 4] * 4
variance_virginica5= np.var(virginica_train5, axis = 0)

covMatrix_virginica_result5 = [[0] * len(variance_virginica5) for _ in range(len(variance_virginica5))]
for i, e in enumerate(variance_virginica5):
    covMatrix_virginica_result5[i][i] = e

covMatrix_virginica_result5 = np.array(covMatrix_virginica_result5)

confussion_matrix4 = np.array(generate_parzebWindow_confussion_matrix(test_x4,train_y4,bandWidth,mean_setosa_result5,mean_versicolor_result5,
                                                        mean_virginica_result5,covMatrix_setosa_result5,
                                                        covMatrix_versicolor_result5,covMatrix_virginica_result5))
Sum5 = np.trace(confussion_matrix4)
Accuracy5 = (Sum5/30)*100
variance.append(Accuracy5)
print("confussion matrix of 5th fold:\n",confussion_matrix4,"\n")
print("Accuracy of 5th fold:", Accuracy5,"% \n")

Avg_accuracy = (Accuracy1+Accuracy2+Accuracy3+Accuracy4+Accuracy5)/5
Variance = get_variance(variance)
print("Avarage accuracy:",Avg_accuracy,"%" "with variance:",Variance)


import collections
f = open("movie.txt","r")

print("Count number of movies in the file")
list_1 = []
for i in f:
    list_1.append(i.split())
print(len(list_1))
f.close()

print("Add a new movie detail (War Amit 180 2019) at the end of file.")
f = open("movie.txt","a")
f.write("\nWar Amit 180 2019")
f.close()

print("Display details of all movies where production cost is more than 80 Crores")
f = open("movie.txt","r")

list_1 = []
for i in f:
    list_1.append(i.split())
t = []
for i in list_1:
    t.append(int(2))
print(list_1)

for i in list_1:
    if int(i[2]) > 80:
        print(i)

print("Display first five movie details.")
for i in range(5):
    print(list_1[i])
print("Display director name who has worked in more than two movies.")
t = []
for i in list_1:
    t.append(i[1])
k = collections.Counter(t)
print(k)
for i in k:
    if k[i] > 1:
        print(i)


from asyncio.windows_events import NULL
import pygame
class Selector:
  def __init__(self,w,h,x,y):
    self.width = w
    self.height = h
    self.x = x
    self.y = y
    self.isSelected = False
    self.selectedColor = -1
    self.selectedTube = NULL
  def draw(self,surface):
    coord = [(self.x,self.y), (self.x+self.width,self.y),(self.x+self.width/2,self.y+self.height)]
    pygame.draw.polygon(surface, (255,255,255), coord)
  def setPos(self,x,y):
    self.x = x + self.width/2
    self.y = y - self.height * 2
  def select(self,selection_pair_C_T):
    selected_tube_class_str = str(type(selection_pair_C_T[1]))
    if(selection_pair_C_T[0] >=0 and selection_pair_C_T[0] < 14 and selected_tube_class_str == "<class 'Tube.Tube'>"):
        self.isSelected = True
        self.selectedColor, self.selectedTube = selection_pair_C_T
  def unselect(self):
    self.isSelected = False
    self.selectedColor, self.selectedTube = (-1, NULL)


import scrapy
import json
import csv
from ..items import FoodyItem

OUTPUT_DIRECTORY =  "/Users/user/Desktop/Crawl/foody/OUTPUT/foody_output.json"

class CrawlfoodySpider(scrapy.Spider):
    name = 'crawlFoody'
    allowed_domains = ['www.foody.vn']
    total_comment = 0
    num_of_page = 1
    url = 'https://www.foody.vn/__get/Place/HomeListPlace?t=1631867059243&page={}&lat=10.823099&lon=106.629664&count=12&districtId=21&cateId=&cuisineId=&isReputation=&type=1'

    def start_requests(self):
        yield scrapy.Request(self.url.format(self.num_of_page),callback=self.parse)

    def parse(self, response):
        if response.status == 200 and self.total_comment <= 4000:
            res = response.json()
            for i in res['Items']:
                item = {'ResId': i['Id'], 'ResName': i['Name']}
                if i['Url'] is not None:
                    yield scrapy.Request(url='https://www.foody.vn'+i['Url'], callback=self.parse_res, meta={'item': item})

    def parse_res(self,response):
        item = response.meta.get('item')
        resid = item['ResId']
        url = 'https://www.foody.vn/__get/Review/ResLoadMore?t=1632416315325&Type=1&fromOwner=&isLatest=true&ExcludeIds=&ResId=' + str(
            resid) + '&Count=20'
        score = response.css('div.microsite-top-points span ::text').getall()
        resinfo = {
            'ResId': item['ResId'],
            'ResName': item['ResName'],
            'streetAddress': response.css('span[itemprop="streetAddress"] ::text').get(),
            'district': response.css('span[itemprop="addressLocality"] ::text').get(),
            'region': response.css('span[itemprop="addressRegion"] ::text').get(),
            'Res_rating': response.css('div[itemprop="ratingValue"] ::text').get().strip("\r\n "),
            'Res_pos_score': score[0],
            'Res_price_score': score[1],
            'Res_food_score': score[2],
            'Res_atmosphere_score': score[3],
            'Res_services_score': score[4],
        }
        # print(response.css('div.res-common-info  div.disableSection span[itemprop="streetAddress"] ::text').get())
        yield scrapy.Request(url, callback=self.parse_comment,meta={'item':resinfo})

    def parse_comment(self, response):
        reviews = response.json()
        # print(len(reviews))
        meta1 = response.meta.get('item')
        print('_____LEN______',len(reviews['Items']))
        for review in reviews['Items']:
            if len(review['Pictures']) > 0:
                self.total_comment += 1
                item = {
                    # 'ResId': response.meta.get('ResId'),
                    'RevId': review['Id'],
                    'UserId': review['Owner']['Id'],
                    'UserName': review['Owner']['DisplayName'],
                    'Rating': review['AvgRating'],
                    'Comment': review['Description'],
                    'image_urls': [picture['Url'] for picture in review['Pictures']]
                }
                item.update(meta1)
                url = 'https://www.foody.vn/__get/Review/GetReviewInfo?reviewId={}'
                # print(review.css('div.review-user a::attr(data-user)').get())
                yield scrapy.Request(url.format(item['RevId']) ,callback=self.parse_comment_score, meta={'item': item})
            # print(item)
        print('------Total----', self.total_comment)
        self.num_of_page += 1
        # if self.num_of_page <= 10:
        if self.num_of_page < 100:
            yield scrapy.Request(self.url.format(self.num_of_page),callback=self.parse)

    def parse_comment_score(self,response):
        item = response.meta.get('item')
        comment_attr = response.json()
        item2 = {
            'Food_score_cmt': comment_attr['Food'],
            'Services_score_cmt': comment_attr['Services'],
            'Atmosphere_score_cmt': comment_attr['Atmosphere'],
            'Position_score_cmt': comment_attr['Position'],
            'Price_score_cmt': comment_attr['Price']
        }
        item.update(item2)
        url = 'https://www.foody.vn/__get/Review/GetUserInfoReview?userId={}'
        yield scrapy.Request(url.format(item['UserId']),callback=self.parse_user_info, meta={'item': item})
    def parse_user_info(self,response):
        item = response.meta.get('item')
        user_info = response.json()
        item3 = {
            'Total_reviews': user_info['TotalReviews'],
            'Followers': user_info['TotalFollowers']
        }
        item.update(item3)
        # food_item = FoodyItem()
        # for key in item:
        #     food_item[key] = item[key]
        yield item

    def close(self, reason):
        start_time = self.crawler.stats.get_value('start_time')
        finish_time = self.crawler.stats.get_value('finish_time')
        print("Total run time: ", finish_time - start_time)


import logging
from datetime import date

from flask.app import Flask

def get_logger(name=__name__, level=logging.INFO):
    """Returns a logger object"""

    logger = logging.getLogger(name)

    if not len(logger.handlers):
        logger.setLevel(level)
        console = logging.StreamHandler()
        console.setLevel(logging.DEBUG)
        formatter = logging.Formatter(
            "%(name)s %(levelname)s [%(filename)s:%(lineno)s - %(funcName)s] %(message)s"
        )
        console.setFormatter(formatter)
        logger.addHandler(console)
    return logger

def get_app_rules(app: Flask):
    rules = []
    for rule in app.url_map.iter_rules():
        methods = ",".join(sorted(rule.methods)) if rule.methods else ""
        rules.append(
            {
                "endpoint": rule.endpoint,
                "methods": methods,
                "name": str(rule),
            }
        )
    rules_sorted = sorted(rules, key=lambda x: x["name"])
    return rules_sorted

def date_today_str() -> str:

    today = date.today()

    # dd/mm/YY
    return today.strftime("%Y-%m-%d")


import sys
import csv
import sqlite3

# creates db if one does not exist
# conn = sqlite3.connect('some.db')
class CSVTool(object):

    def __init__(self, path=None):
        self.path = None
        self.csv_name = None
        self.db = None

    def dump_to_db(self, csv_path):
        # store connection
        self.db = sqlite3.connect('csi.db')
        # get cursor object
        cur = self.db.cursor()
        # sql staement to create a tbale with only ID field
        sqlst = 'CREATE TABLE csi(ID int PRIMARY KEY NOT NULL)'
        # preform the above statement
        cur.execute(sqlst)
        with open(csv_path) as csv_file:
            read_csv = csv.DictReader(csv_file)
            import pdb; pdb.set_trace()
            for row in read_csv:
                sqlst = """

                """
                cur.execute(sqlst.format(int(read_csv.line_num)))
            self.db.commit()
            self.db.close()
if __name__ == '__main__':
    ct = CSVTool()
    ct.dump_to_db('csi.csv')


# -*- coding: utf-8 -*-
'''
这段代码将做出函数f(x)=e^2-2在[0,2]之间的图像，并描画出在该曲线下面的近似矩形

'''
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import math
num = 4
x = np.linspace(0,2,1000)
y = np.power(np.e,x)-2
fig = plt.figure(figsize=(8,8))
x1 = np.linspace(0,2,num+1)
width=2.0/num
ax1 = fig.add_subplot(111,aspect='equal')
for xn in range(num):
    ax1.add_patch(patches.Rectangle((x1[xn],0),width,np.power(np.e,x1[xn]+0.25)-2))
plt.plot(x,y,label='e^2-2',color='red',linewidth=2)
plt.show()


import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from skfmm import travel_time, distance
from scipy.interpolate import interp1d
import os
from utils import plot_2d_image
from math import log10

plt.style.use('ggplot')

def transform_normal_scores(scores, nscore):
    # for now, the values of our scores are less than the values of the nscore.
    # we dont have to worry about more extreme values. ultimately, we need some way to map an arbitrary distribution
    # function to these normal values.
    x = nscore['nscore']
    y = nscore['x']
    f = interp1d(x,y)
    return f(scores)

def linear_taper(n, inds=(0,-1), vals=(0.0,1.0) ):
    """
    Returns normalized coefficient for linear taper between (start, end) and
    values (start_value, end_value)
    Args:
        n (int) : length of taper
        inds (tuple) : indexes of taper, default n
        vals (tuple) : coresponding to inds, default {0, 1.0}
    Returns:
        coef (ndarray) : coefficient {0 .. 1.0} of linear taper over indexes = inds with
        values = vals
    """
    import numpy as np

    # vars
    ix = np.arange(n)
    coef = np.ones(n)

    # linear model
    delta_y = vals[1] - vals[0]
    if inds == (0,-1):
        delta_x = n
    else:
        delta_x = inds[1] - inds[0]
    slope = delta_y / delta_x
    intercept = vals[0] - slope * inds[0]
    coef[inds[0]:inds[1]] = slope * ix[inds[0]:inds[-1]] + intercept

    # returns
    return coef

def boundary_taper( field, taper_width=10, free_surface=True, values=0 ):
    """
    returns a field tapered along to boundary to zero.
    can add taper to some percentage later.

    field (2d ndarray) : rupture field to taper.
    taper_width (int) : boundary to taper
    free_surface (bool) : (true) taper the free surface
                          (false) do NOT taper free surface
    values sequence or int (optional) : ending values for taper. default is zero.  value should be specfied
                                        in terms of percentages.

    return
    tapered_field (ndarray) : tapered field with shape = field.shape
    """
    ny, nx = field.shape
    if free_surface:
        baseline = np.ones( (ny-2*taper_width, nx-2*taper_width) )
        padded = np.pad( baseline, ((taper_width,taper_width), (taper_width,taper_width)), 'linear_ramp', end_values=values )
    else:
        baseline = np.ones( (ny-taper_width, nx-2*taper_width) )
        padded = np.pad( baseline, ((0,taper_width), (taper_width,taper_width)), 'linear_ramp', end_values=values )

    assert field.shape == padded.shape

    return field*padded

def main():
    plot_on = True
    tapering = True
    writing = True
    layered = False

    src_dir = './source_models/'
    output_name = 'sokrg-bbp_source1'
    out_dir = './source_models/source1'

    if not os.path.isdir( out_dir ):
        os.makedirs( out_dir )

    params = {
              'nx' : 273,
              'nz' : 136,
              'dx' : 100,
              'ihypo' : (120, 136),
              'fault_top' : 0,
              'avg_slip'  : 0.74,
              'std_slip'  : 0.42,
              'avg_psv'   : 1.42,
              'std_psv'   : 0.7,
              'avg_vrup'  : 0.79,
              'std_vrup'   : 0.04,
             }

    # read normal score transforms
    slip_sc = pd.read_csv('slip_nscore_transform_table.csv')
    psv_sc = pd.read_csv('psv_nscore_transform_table.csv')
    vrup_sc = pd.read_csv('vrup_nscore_transform_table.csv')

    # extract data
    slip_sim1 = np.fromfile(src_dir + 'slip_sim1.bin').reshape(params['nz'], params['nx'])
    psv_sim1 = np.fromfile(src_dir + 'psv_sim1.bin').reshape(params['nz'], params['nx'])
    vrup_sim1 = np.fromfile(src_dir + 'vrup_sim1.bin').reshape(params['nz'], params['nx'])

    if layered:
        material = np.loadtxt("bbp1d_1250_dx_25.asc")[params['fault_top']:params['fault_top'] + params['nz'], :]
        vp = material[:,1]*1e3
        vs = material[:,2]*1e3
        rho = material[:,3]*1e3

        vs = np.repeat(vs, params['nx']).reshape(params['nz'], params['nx'])
        rho = np.repeat(rho, params['nx']).reshape(params['nz'], params['nx'])

    else:
        vs = 3464*np.ones((params['nz'], params['nx']))
        rho = 2700*np.ones((params['nz'], params['nx']))

    # cut size of model down for computational ease
    slip = slip_sim1[:-1, :-1]
    psv = psv_sim1[:-1, :-1]

    # psv=(psv-psv.mean())/psv.std()
    vrup = vrup_sim1[:-1, :-1]
    vs = vs[:-1, :-1]
    rho = rho[:-1, :-1]

    # update parameters
    params['nx'] = params['nx'] - 1
    params['nz'] = params['nz'] - 1

    if tapering:
        # transform from normal-scores
        slip = transform_normal_scores(slip, slip_sc)
        psv = transform_normal_scores(psv, psv_sc)
        vrup = transform_normal_scores(vrup, vrup_sc)
        # according to xu et. al, 2016 for landers
        # taper = linear_taper( slip_sim1.shape[0], inds=(0, int(4000/params['dx'])), vals = (0.8, 1.0) )
        # slip = np.repeat(taper, params['nx']).reshape(params['nz'], params['nx']) * (slip_sim1 * params['std_slip'] + params['avg_slip'])

        # taper = linear_taper( psv_sim1.shape[0], inds=(0, int(4000/params['dx'])), vals = (0.5, 1.0) )
        # psv = np.repeat(taper, params['nx']).reshape(params['nz'], params['nx']) * (psv_sim1 * params['std_psv'] + params['avg_psv'])

        #vrup_norm = vrup_sim1 * params['std_vrup'] + params['avg_vrup']
        #taper = linear_taper( vrup_norm.shape[0], inds=(0, int(4000/params['dx'])), vals = (0.2, 1.0) )
        #vrup = np.repeat(taper, params['nx']).reshape(params['nz'], params['nx']) * vrup_norm * vs

        # from simulations, slip tapers larger
        taper_width = 37
        slip = boundary_taper(slip,
                                taper_width=taper_width,
                                free_surface=True,
                                values=np.array(((0.60, 0.05), (0.05,0.05))) )

        # taper to 30% of mean along-strike psv at z = taper_width * dx
        taper_width = 12
        ny,nx=psv.shape
        baseline = np.ones( (ny-4*taper_width, nx-2*taper_width) )
        padded = np.pad( baseline,
                         ((3*taper_width,taper_width), (taper_width,taper_width)),
                         'linear_ramp',
                         end_values=np.array(((0.30, 0.05), (0.05,0.05))) )

        psv = padded * psv
        vrup = vrup * vs

    # else:
    #     slip = slip_sim1 * params['std_slip'] + params['avg_slip']
    #     psv = psv_sim1 * params['std_psv'] + params['avg_psv']
    #     vrup_norm = vrup_sim1 * params['std_vrup'] + params['avg_vrup']
    #     vrup = vrup_norm * vs

    trup = compute_trup(vrup, params)

    if plot_on:
        # print(f'slip: min, max ({slip.min():.2f}, {slip.max():.2f})')
        # print(f'psv: min, max ({psv.min():.2f}, {psv.max():.2f})')
        # print(f'vrup: min, max ({vrup.min():.2f}, {vrup.max():.2f})')
        # print(f'trup: min, max ({trup.min():.2f}, {trup.max():.2f})')

        x = np.arange(0, params['nx'])
        z = np.arange(0, params['nz'])
        plotting_data = {'data':slip, 'contour':trup}
        plot_2d_image( plotting_data, "slip-" + output_name + ".pdf" , nx = params['nx'], nz = params['nz'], dx = params['dx']*1e-3,
                                              clabel = "Slip (m)", xlabel = "Distance (km)", ylabel = "Distance (km)",
                                              surface_plot = True, contour = True, clim=(0, slip.max()), cmap='jet' )
        plot_2d_image( psv, "psv-" + output_name + ".pdf", nx = params['nx'], nz = params['nz'], dx = params['dx']*1e-3,
                                             clabel = r'$V^{peak}$ (m/s)', xlabel = "Distance (km)", ylabel = "Distance (km)",
                                              surface_plot = False, contour = False, clim=(0, psv.max()), cmap='jet' )
        plot_2d_image( trup, "trup-" + output_name + ".pdf", nx = params['nx'], nz = params['nz'], dx = params['dx']*1e-3,
                                              clabel = "Trup (s)", xlabel = "Distance (km)", ylabel = "Distance (km)",
                                              surface_plot = False, contour = True, clim=(0,20) )
        plot_2d_image( vrup/vs, "vrup-" + output_name + ".pdf", nx = params['nx'], nz = params['nz'], dx = params['dx']*1e-3,
                                              clabel = r'$V_{rup}/c_s$', xlabel = "Distance (km)", ylabel = "Distance (km)",
                                              surface_plot = False, contour = False, cmap='viridis', clim=(0, 1.0) )

        plot_2d_image( vs, "vs-" + output_name + ".pdf", nx = params['nx'], nz = params['nz'], dx = params['dx']*1e-3,
                                              clabel = r'$V_{rup}/c_s$', xlabel = "Distance (km)", ylabel = "Distance (km)",
                                              surface_plot = False, contour = False, cmap='jet' )

# generate strike, dip, and rake
    nhat1 = np.fromfile("nhat1", "f").reshape(801,2601)
    nhat2 = np.absolute(np.fromfile("nhat2", "f").reshape(801,2601))
    nhat3 = np.fromfile("nhat3", "f").reshape(801,2601) # make vector point "up"

    # NOTE: starting at x=1000 to reduce model size for small model
    nhat1 = nhat1[::4, ::4]
    nhat2 = nhat2[::4, ::4]
    nhat3 = nhat3[::4, ::4]

    print(nhat1.shape)
    print(nhat2.shape)
    print(nhat3.shape)

#fienen "the three-point problem"
# project onto horizontal plane, calculate angle between
    dip = get_dip(nhat1, nhat2, nhat3)
    strike = get_strike(nhat1, nhat3)
    #rake = np.ones(strike.shape)*180.0 # constant rake
    rake = strike - 90 # strike is 270 and rake is 180

# compute moment
    moment = get_moment(slip, vs, rho, params)
    print('moment')
    print(moment.sum())
    print(2./3 * (log10(moment.sum()) - 9.1))

# write to file for input
    if writing:
        dtype = '<f4'
        print('writing files...')
        # start at 1000 to reduce the size of the source simulation.
        vs.astype(dtype).tofile(os.path.join(out_dir, output_name + '_vs.bin'))
        rho.astype(dtype).tofile(os.path.join(out_dir, output_name + '_rho.bin'))
        slip.astype(dtype).tofile(os.path.join(out_dir, output_name + '_slip.bin'))
        psv.astype(dtype).tofile(os.path.join(out_dir, output_name + '_psv.bin'))
        vrup.astype(dtype).tofile(os.path.join(out_dir, output_name + '_vrup.bin'))
        trup.astype(dtype).tofile(os.path.join(out_dir, output_name + '_trup.bin'))
        strike.astype(dtype).tofile(os.path.join(out_dir, output_name + '_strike.bin'))
        dip.astype(dtype).tofile(os.path.join(out_dir, output_name + '_dip.bin'))
        rake.astype(dtype).tofile(os.path.join(out_dir, output_name + '_rake.bin'))
        moment.astype(dtype).tofile(os.path.join(out_dir, output_name + '_moment.bin'))

"""
Helping functions.
"""
def get_dip(nhat1, nhat2, nhat3):
    nz,nx = nhat1.shape
    dip = np.ones([nz,nx])

    for i in range(nz):
        for j in range(nx):
            nproj = (nhat1[i,j], 0, nhat3[i,j])
            n = (nhat1[i,j], nhat2[i,j], nhat3[i,j])
            norm = lambda v: np.sqrt(v[0]**2+v[1]**2+v[2]**2)
            scaling = 1.0 / ( norm(nproj) * norm(n) )
            arg = scaling*(n[0]**2+n[2]**2)
            if np.isclose(1.0, arg):
                arg = 1.0
            arg=np.arccos(arg)
            theta = np.rad2deg(arg)
            dip[i,j] = 90 - theta
    return dip

def get_moment(slip, vs, rho, params):
    mu = vs * vs * rho
    area = params['dx'] * params['dx']
    moment = mu * area * slip
    return moment

def get_strike(nhat1, nhat3):
    nz,nx = nhat1.shape
    strike = np.ones([nz,nx])
    for i in range(nz):
        for j in range(nx):
            nproj = (nhat1[i,j], 0, nhat3[i,j])
            x3 = (1,0,0)
            norm = lambda v: np.sqrt(v[0]**2+v[1]**2+v[2]**2)
            scaling = 1.0 / ( norm(x3) * norm( nproj) )
            theta = np.rad2deg(scaling * np.arccos(nproj[2]))
            if nhat1[i,j] > 0 and nhat3[i,j] > 0:
                strike[i,j] = 270 + theta
            elif nhat1[i,j] < 0 and nhat3[i,j] > 0:
                strike[i,j] = 270 - theta
            elif nhat1[i,j] < 0 and nhat3[i,j] < 0:
                # in 3rd quad
                strike[i,j] = 270 - theta
            elif nhat1[i,j] > 0 and nhat3[i,j] < 0:
                # in 4th quad
                strike[i,j] = theta - 90
    return strike

def source_time_function():
    pass

def compute_trup(vrup, params):
    phi = np.ones( (params['nz'], params['nx']) ) #* params['dx']
    print(phi.shape)
    ihypo = params['ihypo']
    phi[ ihypo[0], ihypo[1] ] = -1
    trup = travel_time( phi, speed=vrup, dx=params['dx'] )
    return np.array(trup)

if __name__ == "__main__":
    main()


from aiogram.types import Message, ReplyKeyboardRemove
from aiogram.dispatcher.filters.builtin import Text
from aiogram.dispatcher import FSMContext
from states.StateStart import StateStart

from loader import dp

@dp.message_handler(Text(equals="Mukammal Telegram Bot"),state=StateStart.Kurs)
async def MTBKurs(msg: Message, state: FSMContext):
    message = ("Kurs nomi: Mukammal Telegram Bot\n"
               "\n"
              "Kurs muallifi: Anvar Narzullayev\n"
               "\n"
              "Kurs narxi: 247.000 so'm\n"
               "\n"
              "Kurs manzili:https://praktikum.mohirdev.uz/dashboard/practicums/645264c869d972027cd033c3")
    await msg.answer(message, reply_markup=ReplyKeyboardRemove())
    await state.finish()

@dp.message_handler(Text(equals="C++ Asoslari"),state=StateStart.Kurs)
async def CppKurs(msg: Message, state: FSMContext):
    message = ("Kurs nomi: C++ Asoslari\n"
               "\n"
              "Kurs muallifi: Qudrat Abduraximov\n"
               "\n"
              "Kurs narxi: 1.000.000 so'm\n"
               "\n"
              "Kurs manzili:https://praktikum.mohirdev.uz/dashboard/practicums/645264a369d972027cd02963/purchase")
    await msg.answer(message, reply_markup=ReplyKeyboardRemove())
    await state.finish()

@dp.message_handler(Text(equals="Python Asoslari"),state=StateStart.Kurs)
async def PythonKurs(msg: Message, state: FSMContext):
    message = ("Kurs nomi: Python Asoslari\n"
               "\n"
              "Kurs muallifi: Anvar Narzullayev\n"
               "\n"
              "Kurs narxi: Bepul\n"
               "\n"
              "Kurs manzili:https://praktikum.mohirdev.uz/dashboard/practicums/645264af69d972027cd02d52/free")
    await msg.answer(message, reply_markup=ReplyKeyboardRemove())
    await state.finish()

@dp.message_handler(Text(equals="Professional Node.JS"),state=StateStart.Kurs)
async def NodeJsKurs(msg: Message, state: FSMContext):
    message = ("Kurs nomi: Professional Node.JS\n"
               "\n"
              "Kurs muallifi: Behruz Xolmominov\n"
               "\n"
              "Kurs narxi: 247.000 so'm\n"
               "\n"
              "Kurs manzili:https://praktikum.mohirdev.uz/dashboard/practicums/645264bd69d972027cd030f4/purchase")
    await msg.answer(message, reply_markup=ReplyKeyboardRemove())
    await state.finish()

@dp.message_handler(Text(equals="Django3: Python Full stack"),state=StateStart.Kurs)
async def DjangoFsKurs(msg: Message, state: FSMContext):
    message = ("Kurs nomi: Django3: Python Full stack\n"
               "\n"
              "Kurs muallifi: Anvar Narzullayev\n"
               "\n"
              "Kurs narxi: Bepul\n"
               "\n"
              "Kurs manzili:https://praktikum.mohirdev.uz/dashboard/practicums/645264bf69d972027cd03180/free")
    await msg.answer(message, reply_markup=ReplyKeyboardRemove())
    await state.finish()

@dp.message_handler(Text(equals="Python: Ma'lumotlar tuzilmasi"),state=StateStart.Kurs)
async def PythonMtKurs(msg: Message, state: FSMContext):
    message = ("Kurs nomi: Python: Ma'lumotlar tuzilmasi\n"
               "\n"
              "Kurs muallifi: Anvar Narzullayev\n"
               "\n"
              "Kurs narxi: Bepul\n"
               "\n"
              "Kurs manzili:https://praktikum.mohirdev.uz/dashboard/practicums/645264b769d972027cd02ea8/free")
    await msg.answer(message, reply_markup=ReplyKeyboardRemove())
    await state.finish()

@dp.message_handler(Text(equals="PostgreSQL"),state=StateStart.Kurs)
async def PSQLKurs(msg: Message, state: FSMContext):
    message = ("Kurs nomi: PostgreSQL\n"
               "\n"
              "Kurs muallifi: Behruz Xolmominov\n"
               "\n"
              "Kurs narxi: Bepul\n"
               "\n"
              "Kurs manzili:https://praktikum.mohirdev.uz/dashboard/practicums/645264ad69d972027cd02d02/free")
    await msg.answer(message, reply_markup=ReplyKeyboardRemove())
    await state.finish()

@dp.message_handler(Text(equals="Yii2 Framework"),state=StateStart.Kurs)
async def Yii2Kurs(msg: Message, state: FSMContext):
    message = ("Kurs nomi: Yii2 Framework\n"
               "\n"
              "Kurs muallifi: Sardor Dushamov\n"
               "\n"
              "Kurs narxi: Bepul\n"
               "\n"
              "Kurs manzili:https://praktikum.mohirdev.uz/dashboard/practicums/645264ac69d972027cd02cc0/free")
    await msg.answer(message, reply_markup=ReplyKeyboardRemove())
    await state.finish()

@dp.message_handler(Text(equals="Django Rest Framework"),state=StateStart.Kurs)
async def DjangoKurs(msg: Message, state: FSMContext):
    message = ("Kurs nomi: Django Rest Framework\n"
              "Kurs muallifi: Muhammad Ertmatov\n"
              "Kurs narxi: Bepul\n"
              "Kurs manzili:https://praktikum.mohirdev.uz/dashboard/practicums/64526477ecc3657ffcec10e5/free")
    await msg.answer(message, reply_markup=ReplyKeyboardRemove())
    await state.finish()


import os
import re
import requests

import logging
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters

# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    level=logging.INFO)

logger = logging.getLogger(__name__)

# Declaramos algunas variables necesarias para el Bot
PORT = int(os.environ.get('PORT', 5000))
SECRET_KEY = os.environ['TELEGRAM_BOT_API_KEY']
HEROKU_APP = os.environ['HEROKU_APP']

# Definimos algunos comandos básicos que funcionaran como handlers.

def saludo(update, context):
    """Manda un mensaje cuando el usuario ingresa /saludo """
    update.message.reply_text('Hola! Soy el BOT de Análisis Matemático! Sirvo como un ayuda-memoria para la cursada')
    update.message.reply_text('Estoy desarrollado por estudiantes de la universidad, no soy una herramienta oficial de la misma ni de la cátedra de Análisis Matemático.')
    update.message.reply_text('Para más información, revisá [mi repositorio en github](https://github.com/matiasnoriega/bot-telegram-analisis)', parse_mode='MarkdownV2')

def factoreo(update, context):
    """ Devuelve tabla de casos mas comunes de factoreo """
    update.message.reply_photo(photo=open('assets/factoreo.png', 'rb'))

def resolvente(update, context):
    """ Devuelve tabla de casos mas comunes de factoreo """
    update.message.reply_photo(photo=open('assets/resolvente.png', 'rb'))

def ayuda(update, context):
    """Manda un mensaje cuando el usuario ingresa /ayuda """
    update.message.reply_text('Probá el comando /factoreo para recibir los casos más comunes de factoreo o /derivar funcion para ver la derivada de f(x)!')

def derivadas(update, context):
    """ Retorna casos de derivadas"""
    if not context.args:
        caso = None
    else:
        caso = context.args[0]

    if caso == None:
        update.message.reply_photo(photo=open('assets/tabla_derivadas.png', 'rb'))
    elif caso == 'suma':
        update.message.reply_photo(photo=open('assets/derivadas_suma.png', 'rb'))
    elif caso == 'resta':
        update.message.reply_photo(photo=open('assets/derivadas_resta.png', 'rb'))
    elif caso == 'division':
        update.message.reply_photo(photo=open('assets/derivadas_division.png', 'rb'))
    elif caso == 'multiplicacion':
        update.message.reply_photo(photo=open('assets/derivadas_multiplicacion.png', 'rb'))
    else:
        update.message.reply_text('Intenta de nuevo con alguna de las opciones: suma, resta, division o multiplicacion o vacio para la tabla de derivadas!')

def derivar(update, context):
    """ Toma la función base y muestra su derivada """
    #se declara un diccionario de patrones para contrastar la funcion recibida
    patron_funciones = {
        '^[0-9]+$': '0',
        'x$': '1',
        '^[0-9]+x$': 'kx',
    }

    #Tomamos el primer argumento que se le pasa al comando como función f(x)
    funcion = context.args[0]
    resultado = None

    for key in patron_funciones.keys():
        #compilamos el patron de esta entrada particular del diccionario para compararlo con la funcion recibida
        #y guardamos el resultado en la variable match
        regex_matcher = re.compile(key)
        match = regex_matcher.match(funcion)
        #si hubo una coincidencia, guardamos en la variable resultado el valor asociado a esa clave en el diccionario
        #devolvemos resultado y detenemos el ciclo for
        if match:
            resultado = patron_funciones.get(key)
            break

    # Si encuentra una coincidencia usa la API de Latex2PNG
    if(resultado):

        if(resultado == 'kx'):
            r = re.compile("([0-9]+)(x)")
            m = r.match(funcion)
            resultado = m.group(1)

        # Data para mandar a la API de latex2png
        payload = "{\n\"auth\": {\n\"user\": \"guest\",\n\"password\": \"guest\"\n},\n\"latex\": \"%(latex)s\" ,\n\"resolution\": 600,\n\"color\": \"111111\"\n}"%{'latex': resultado}
        # Headers del Request
        headers = {
            'Content-Type': 'application/json'
        }

        r = requests.request("POST", 'http://latex2png.com/api/convert', headers=headers, data=payload)

        json_response = r.json()
        img_url = json_response["url"]
        update.message.reply_photo('http://latex2png.com' + img_url)

    else:
        update.message.reply_text('No encontré una función base que coincida con '+ context.args[0])

def error(update, context):
    """Loggea los errores causados por el Updater."""
    logger.warning('Update "%s" caused error "%s"', update, context.error)

def main():
    """Inicia el bot."""
    # Se crea un objeto Updater pasandole el token del bota
    updater = Updater(SECRET_KEY, use_context=True)

    # Obtenemos el dispatcher, del updater, para poder registrar los handlers (métodos)
    disp = updater.dispatcher

    # Para cada comando en telegram se asigna un handler
    disp.add_handler(CommandHandler("start", saludo))
    disp.add_handler(CommandHandler("saludo", saludo))
    disp.add_handler(CommandHandler("ayuda", ayuda))
    disp.add_handler(CommandHandler("resolvente", resolvente))
    disp.add_handler(CommandHandler("factoreo", factoreo))
    disp.add_handler(CommandHandler("derivadas", derivadas, pass_args=True))
    disp.add_handler(CommandHandler("derivar", derivar, pass_args=True))

    # loggea todos los errores
    disp.add_error_handler(error)

    # Corre el bot hasta que se presione CTRL+C o el proceso reciba un SIGINT,
    # SIGTERM o SIGABRT.
    updater.start_webhook(listen="0.0.0.0",
                          port=int(PORT),
                          url_path=SECRET_KEY)
    updater.bot.setWebhook(
        HEROKU_APP + SECRET_KEY)

    # Dejamos el bot en modo idle para que siga escuchando.
    updater.idle()

if __name__ == '__main__':
    main()


from datetime import timedelta
from django.utils import timezone

from celery import shared_task

from django.contrib.auth.models import User
from .models import ReadingStatistics, ReadingSession

@shared_task
def update_reading_statistics():
    # Завдання Celery для оновлення статистики читання користувачів

    print("Завантаження даних...")

    # Отримуємо всіх користувачів
    users = User.objects.all()

    for user in users:
        now = timezone.now()  # Поточний час

        # Визначаємо дати для вибірки (7 та 30 днів назад)
        seven_days_ago = now - timedelta(days=7)
        thirty_days_ago = now - timedelta(days=30)

        # Фільтруємо сесії читання для користувача
        sessions = ReadingSession.objects.filter(user=user, end_time__isnull=False)

        # Фільтруємо сесії читання за останні 7 днів
        sessions_7_days = sessions.filter(end_time__gte=seven_days_ago, end_time__lte=now)

        # Фільтруємо сесії читання за останні 30 днів
        sessions_30_days = sessions.filter(end_time__gte=thirty_days_ago, end_time__lte=now)

        # Розраховуємо загальний час читання за 7 та 30 днів
        total_reading_7_days = sum(
            (session.end_time - session.start_time).total_seconds() / 3600 for session in sessions_7_days)
        total_reading_30_days = sum(
            (session.end_time - session.start_time).total_seconds() / 3600 for session in sessions_30_days)

        # Оновлюємо запис в моделі ReadingStatistics для користувача
        reading_statistics, created = ReadingStatistics.objects.get_or_create(user=user)
        reading_statistics.total_reading_7_days = total_reading_7_days
        reading_statistics.total_reading_30_days = total_reading_30_days
        reading_statistics.save()


import streamlit as st
from PIL import Image
from detection import process
import numpy as np
from torchdetect import process
import os

st.set_page_config(
    page_title="Image Detection",
    layout="wide",
    initial_sidebar_state="expanded"
 )
st.title('Tuberculosis Detection from sputum sample')
st.markdown("## inference using torch.hub")

uploaded_file = st.file_uploader(label="Choose a file", type=['jpg', 'jpeg','png'])

idebar = st.sidebar
conf_level = st.sidebar.slider("conf_level", min_value=0.0, max_value=1.0, value=0.55)
IoU = st.sidebar.slider("IoU", min_value=0.0, max_value=1.0, value=0.45)

if uploaded_file is not None:
    image = Image.open(uploaded_file).convert('RGB')

    col1, col2 = st.columns([0.5, 0.5])

    #Col 1
    with col1:
        st.markdown('
Input Image
', unsafe_allow_html=True)
        st.image(image,caption="Sputum sample")
        imgpath = os.path.join('data/uploads', uploaded_file.name)

        with open(imgpath, mode="wb") as f:
            f.write(uploaded_file.getbuffer())

    with col2:
        st.markdown('
Detected Image
', unsafe_allow_html=True)
        img,total,runtime = process(imgpath,conf_level,IoU)
        st.image(img,caption="Tuberculosis detected")
        st.markdown(f'
Bacteria detected : {total}
', unsafe_allow_html=True)
        st.markdown(f'
{runtime}
', unsafe_allow_html=True)

    # with col2:
    #     st.markdown('
Detected Image
', unsafe_allow_html=True)
    #     image_arr,total =  process(np.array(image))
    #     st.image(Image.fromarray(image_arr),caption="Tuberculosis detected")
    #     st.markdown(f'
Bacteria detected : {total}
', unsafe_allow_html=True)


import sys
import os
import json
import threading

BASE_DIR = os.getcwd()+"/.."
EOF = ""

def setPlatformCfg(env):
    cfgPath = BASE_DIR+"/stays-platform/config.json"
    cfg = ""
    with open(cfgPath) as f:
        cfg = json.load(f)
    with open(cfgPath, "w") as f:
        cfg['env'] = env
        txt = json.dumps(cfg, indent=2) + EOF
        f.write(txt)

def setUiCfg(env):
    cfgPath = BASE_DIR+"/stays-ui/config.json"
    cfg = ""
    with open(cfgPath, 'r') as f:
        cfg = json.load(f)
    with open(cfgPath, "w") as f:
        cfg['env'] = env
        cfg['platform']['env'] = env
        txt = json.dumps(cfg, indent=2) + EOF
        f.write(txt)

def build():
    os.chdir(BASE_DIR)
    os.system("docker-compose build")

def tag(env, service):
    os.chdir(BASE_DIR)
    os.system("docker tag stays_"+service +
              " gcr.io/stays-"+env+"/stays-"+service)

def push(env, service):
    os.chdir(BASE_DIR)
    os.system("docker push gcr.io/stays-"+env+"/stays-"+service)

def deploy(env, service):
    os.chdir(BASE_DIR)
    os.system("gcloud run deploy stays-"+service +
              " --image gcr.io/stays-"+env+"/stays-"+service+" --project stays-"+env+" --region us-central1")

def fullDeploy(env, service):
    tag(env, service)
    push(env, service)
    deploy(env, service)

if __name__ == "__main__":
    if(len(sys.argv) < 1):
        print("Please specify environment")
        exit(-1)
    env = sys.argv[1]
    if env != "dev" and env != "prod":
        print("Invalid environment!")
        exit(-1)
    else:
        setPlatformCfg(env)
        setUiCfg(env)
        build()
        t1 = threading.Thread(target=fullDeploy, args=(env, "platform"))
        t2 = threading.Thread(target=fullDeploy, args=(env, "ui"))
        t1.start()
        t2.start()


# coding:utf-8
import sys
import os
import numpy as np
import cv2
import matplotlib.pyplot as plt
import torch
import torch.nn.functional as F
sys.path.append('../synthesize_blur')
import viz_flow as viz

data_dir = "./"

old_path = data_dir + "1.jpeg"
new_path = data_dir + "2.jpeg"

gap = [0, 100]

feature_params = dict(maxCorners=100000,
                      qualityLevel=0.001,
                      minDistance=10,
                      blockSize=7)

lk_params = dict(winSize=(15, 15),
                 maxLevel=3,
                 criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))

def find_last(s, t):
        start = 0
        ret = -1
        while True:
            result = s.find(t, start)
            if result != -1:
                ret = result
            else:
                break
            start = result + 1
        return ret

def viz_lk(flow, path):
    H, W, _ = flow.shape
    f = np.abs(flow[:, :, 0]) + np.abs(flow[:, :, 1])
    f = (f - f.min()) / (f.max() - f.min()+1e-8)
    f *= 255
    f = cv2.GaussianBlur(f, (11, 11), 0)
    f = torch.FloatTensor(f).view(1, 1, H, W)
    f = F.max_pool2d(f, kernel_size=7, stride=7)
    f = f.squeeze().numpy()
    f = cv2.GaussianBlur(f, (5,5), 0)
    plt.imshow(f)
    plt.savefig(path)
    #plt.show()
    plt.clf()

    return f

def cal_lk(old_path, new_path):
    old_img = cv2.imread(old_path)
    H, W, _ = old_img.shape
    old_gray = cv2.cvtColor(old_img, cv2.COLOR_BGR2GRAY)
    new_img = cv2.imread(new_path)
    new_gray = cv2.cvtColor(new_img, cv2.COLOR_BGR2GRAY)
    print(old_img.shape)

    color = np.random.randint(0, 255, (100000, 3))
    mask = np.zeros_like(old_img)

    p0 = cv2.goodFeaturesToTrack(old_gray, mask=None, **feature_params)
    p1, st, err = cv2.calcOpticalFlowPyrLK(
        old_gray, new_gray, p0, None, **lk_params)

    # select good points
    good_new = p1[st == 1]
    good_old = p0[st == 1]
    good = good_new - good_old

    flow = np.zeros((H, W, 2))
    for i, (new, old) in enumerate(zip(good_new, good_old)):
        a, b = new.ravel()
        c, d = old.ravel()
        gapx = abs(a-c)
        gapy = abs(b-d)
        if gapx >= gap[0] and gapy >= gap[0] and gapx <= gap[1] and gapy <= gap[1]:
            mask = cv2.line(mask, (a, b), (c, d), color[i].tolist(), 2)
            frame = cv2.circle(new_img, (a, b), 5, color[i].tolist(), -1)
            flow[int(d)-1, int(c)-1] = np.array((d-b, c-a))
    img = cv2.add(frame, mask)

    flag = find_last(new_path, '.')
    path = new_path[:flag] + '_flow.png'
    viz_lk(flow, path)

    # plot
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    #'''
    plt.subplot(221)
    plt.imshow(img)
    plt.subplot(222)
    plt.imshow(mask,  cmap=plt.cm.gray)
    plt.subplot(223)
    #'''

    good_x = good[:, 0]
    good_y = good[:, 1]
    good_x = good_x.clip(good_x.mean()-3*good_x.std(), good_x.mean()+3*good_x.std())
    good_y = good_y.clip(good_y.mean()-3*good_y.std(), good_y.mean()+3*good_y.std())
    n1, bins1, patches1 = plt.hist(
        good_x, bins=100, range=(good_x.min(), good_x.max()), density=False, facecolor='green', alpha=0.75)
    plt.subplot(224)
    n2, bins2, patches2 = plt.hist(
        good_y, bins=100, range=(good_y.min(), good_y.max()), density=False, facecolor='blue', alpha=0.75)
    plt.savefig(new_path[:flag] + '_total.png')
    #'''
    width = 0.02
    fig, ax = plt.subplots()
    n1 /= n1.sum()
    n2 /= n2.sum()

    rects1 = ax.bar(bins1[:-1], n1, width,
                    label="x displacement", lw=1, alpha=0.4, facecolor='orange')
    rects2 = ax.bar(bins2[:-1], n2, width,
                    label="y displacement", lw=1, alpha=0.4, facecolor='green')
    bin_min = min(bins1.min(), bins2.min())
    rects3 = ax.bar(bin_min, 0.3, width, lw=1, facecolor='blue', alpha=0.4, label='reference')
    plt.legend(loc="upper left")
    #'''
    #plt.show()
    plt.savefig(new_path[:flag] + '_hist.png')
    plt.clf()

def main(data_list):
    with open(data_list, 'r') as f:
        paths = f.readlines()
    count = 0
    for pair in paths:
        old_path, new_path = pair[:-1].split(' ')
        cal_lk(old_path, new_path)
        print(old_path)
        count += 1

if __name__ == '__main__':
    #data_list = './motion_trans.txt'
    data_list = './motion.txt'
    main(data_list)


import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np

# input parameter
den = 8880.0
cp = 386.0
cond = 398.0
temp_bc = 100.0
temp_init = 0.0
lx = 1.0
nx = 101
tend = 20000.0
dt = 0.1
tout = 100.0

alpha = cond / (den * cp)
dx = lx / (nx - 1)
nt = int(tend / dt)
nout = int(tout / dt)

#initial condition
temp = np.full(nx, temp_init)
time = 0.0

temp_new = np.zeros(nx)

# Boundary condition
temp[0] = temp_bc # Dirichlet @ x=0
temp[nx-1] = temp[nx-2] # Neumann @ x=Lx

# graph data array
ims = []
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

gx = np.zeros(nx)
for i in range(nx):
    gx[i] = i * dx

# time loop
for n in range(1, nt+1):
    # FTCS
    for i in range(1, nx-1):
        temp_new[i] = temp[i] + dt * alpha * (temp[i+1] - 2.0 * temp[i] + temp[i-1]) / (dx * dx)

    # update
    for i in range(1, nx-1):
        temp[i] = temp_new[i]

    # Boundary condition
    temp[0] = temp_bc # Dirichlet @ x=0
    temp[nx-1] = temp[nx-2] # Neumann @ x=Lx

    time += dt

    if n % nout == 0:
        print('n: {0:7d}, time: {1:8.1f}, temp: {2:10.6f}'.format(n, time, temp[nx-1]))
        im_line = ax.plot(gx, temp, 'b')
        im_time = ax.text(0, 110, 'Time = {0:8.1f} [s]'.format(time))
        ims.append(im_line + [im_time])

# graph plot
ax.set_xlabel('x [m]')
ax.set_ylabel('Temperature [C]')
ax.grid()
anm = animation.ArtistAnimation(fig, ims, interval=50)
anm.save('animation.gif', writer='pillow')
plt.show()


from typing import TYPE_CHECKING, Any, Dict, List, Type, TypeVar, Union

import attr

from ..models.submodel_kind import SubmodelKind
from ..types import UNSET, Unset

if TYPE_CHECKING:
    from ..models.administrative_information import AdministrativeInformation
    from ..models.embedded_data_specification import EmbeddedDataSpecification
    from ..models.extension import Extension
    from ..models.lang_string_name_type import LangStringNameType
    from ..models.lang_string_text_type import LangStringTextType
    from ..models.qualifier import Qualifier
    from ..models.reference import Reference
    from ..models.submodel_element import SubmodelElement

T = TypeVar("T", bound="Submodel")

@attr.s(auto_attribs=True)
class Submodel:
    """
    Attributes:
        submodel_elements (Union[Unset, List['SubmodelElement']]):
        kind (Union[Unset, SubmodelKind]):
        supplemental_semantic_ids (Union[Unset, List['Reference']]):
        id (Union[Unset, str]):
        administration (Union[Unset, AdministrativeInformation]):
        category (Union[Unset, str]):
        extensions (Union[Unset, List['Extension']]):
        qualifiers (Union[Unset, List['Qualifier']]):
        display_name (Union[Unset, List['LangStringNameType']]):
        description (Union[Unset, List['LangStringTextType']]):
        id_short (Union[Unset, str]):
        embedded_data_specifications (Union[Unset, List['EmbeddedDataSpecification']]):
        semantic_id (Union[Unset, Reference]):
    """

    submodel_elements: Union[Unset, List["SubmodelElement"]] = UNSET
    kind: Union[Unset, SubmodelKind] = UNSET
    supplemental_semantic_ids: Union[Unset, List["Reference"]] = UNSET
    id: Union[Unset, str] = UNSET
    administration: Union[Unset, "AdministrativeInformation"] = UNSET
    category: Union[Unset, str] = UNSET
    extensions: Union[Unset, List["Extension"]] = UNSET
    qualifiers: Union[Unset, List["Qualifier"]] = UNSET
    display_name: Union[Unset, List["LangStringNameType"]] = UNSET
    description: Union[Unset, List["LangStringTextType"]] = UNSET
    id_short: Union[Unset, str] = UNSET
    embedded_data_specifications: Union[Unset, List["EmbeddedDataSpecification"]] = UNSET
    semantic_id: Union[Unset, "Reference"] = UNSET
    additional_properties: Dict[str, Any] = attr.ib(init=False, factory=dict)

    def to_dict(self) -> Dict[str, Any]:
        submodel_elements: Union[Unset, List[Dict[str, Any]]] = UNSET
        if not isinstance(self.submodel_elements, Unset):
            submodel_elements = []
            for submodel_elements_item_data in self.submodel_elements:
                submodel_elements_item = submodel_elements_item_data.to_dict()

                submodel_elements.append(submodel_elements_item)

        kind: Union[Unset, str] = UNSET
        if not isinstance(self.kind, Unset):
            kind = self.kind.value

        supplemental_semantic_ids: Union[Unset, List[Dict[str, Any]]] = UNSET
        if not isinstance(self.supplemental_semantic_ids, Unset):
            supplemental_semantic_ids = []
            for supplemental_semantic_ids_item_data in self.supplemental_semantic_ids:
                supplemental_semantic_ids_item = supplemental_semantic_ids_item_data.to_dict()

                supplemental_semantic_ids.append(supplemental_semantic_ids_item)

        id = self.id
        administration: Union[Unset, Dict[str, Any]] = UNSET
        if not isinstance(self.administration, Unset):
            administration = self.administration.to_dict()

        category = self.category
        extensions: Union[Unset, List[Dict[str, Any]]] = UNSET
        if not isinstance(self.extensions, Unset):
            extensions = []
            for extensions_item_data in self.extensions:
                extensions_item = extensions_item_data.to_dict()

                extensions.append(extensions_item)

        qualifiers: Union[Unset, List[Dict[str, Any]]] = UNSET
        if not isinstance(self.qualifiers, Unset):
            qualifiers = []
            for qualifiers_item_data in self.qualifiers:
                qualifiers_item = qualifiers_item_data.to_dict()

                qualifiers.append(qualifiers_item)

        display_name: Union[Unset, List[Dict[str, Any]]] = UNSET
        if not isinstance(self.display_name, Unset):
            display_name = []
            for display_name_item_data in self.display_name:
                display_name_item = display_name_item_data.to_dict()

                display_name.append(display_name_item)

        description: Union[Unset, List[Dict[str, Any]]] = UNSET
        if not isinstance(self.description, Unset):
            description = []
            for description_item_data in self.description:
                description_item = description_item_data.to_dict()

                description.append(description_item)

        id_short = self.id_short
        embedded_data_specifications: Union[Unset, List[Dict[str, Any]]] = UNSET
        if not isinstance(self.embedded_data_specifications, Unset):
            embedded_data_specifications = []
            for embedded_data_specifications_item_data in self.embedded_data_specifications:
                embedded_data_specifications_item = embedded_data_specifications_item_data.to_dict()

                embedded_data_specifications.append(embedded_data_specifications_item)

        semantic_id: Union[Unset, Dict[str, Any]] = UNSET
        if not isinstance(self.semantic_id, Unset):
            semantic_id = self.semantic_id.to_dict()

        field_dict: Dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update({})
        if submodel_elements is not UNSET:
            field_dict["submodelElements"] = submodel_elements
        if kind is not UNSET:
            field_dict["kind"] = kind
        if supplemental_semantic_ids is not UNSET:
            field_dict["supplementalSemanticIds"] = supplemental_semantic_ids
        if id is not UNSET:
            field_dict["id"] = id
        if administration is not UNSET:
            field_dict["administration"] = administration
        if category is not UNSET:
            field_dict["category"] = category
        if extensions is not UNSET:
            field_dict["extensions"] = extensions
        if qualifiers is not UNSET:
            field_dict["qualifiers"] = qualifiers
        if display_name is not UNSET:
            field_dict["displayName"] = display_name
        if description is not UNSET:
            field_dict["description"] = description
        if id_short is not UNSET:
            field_dict["idShort"] = id_short
        if embedded_data_specifications is not UNSET:
            field_dict["embeddedDataSpecifications"] = embedded_data_specifications
        if semantic_id is not UNSET:
            field_dict["semanticId"] = semantic_id

        return field_dict

    @classmethod
    def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
        from ..models.administrative_information import AdministrativeInformation
        from ..models.embedded_data_specification import EmbeddedDataSpecification
        from ..models.extension import Extension
        from ..models.lang_string_name_type import LangStringNameType
        from ..models.lang_string_text_type import LangStringTextType
        from ..models.qualifier import Qualifier
        from ..models.reference import Reference
        from ..models.submodel_element import SubmodelElement

        d = src_dict.copy()
        submodel_elements = []
        _submodel_elements = d.pop("submodelElements", UNSET)
        for submodel_elements_item_data in _submodel_elements or []:
            submodel_elements_item = SubmodelElement.from_dict(submodel_elements_item_data)

            submodel_elements.append(submodel_elements_item)

        _kind = d.pop("kind", UNSET)
        kind: Union[Unset, SubmodelKind]
        if isinstance(_kind, Unset):
            kind = UNSET
        else:
            kind = SubmodelKind(_kind)

        supplemental_semantic_ids = []
        _supplemental_semantic_ids = d.pop("supplementalSemanticIds", UNSET)
        for supplemental_semantic_ids_item_data in _supplemental_semantic_ids or []:
            supplemental_semantic_ids_item = Reference.from_dict(supplemental_semantic_ids_item_data)

            supplemental_semantic_ids.append(supplemental_semantic_ids_item)

        id = d.pop("id", UNSET)

        _administration = d.pop("administration", UNSET)
        administration: Union[Unset, AdministrativeInformation]
        if isinstance(_administration, Unset):
            administration = UNSET
        else:
            administration = AdministrativeInformation.from_dict(_administration)

        category = d.pop("category", UNSET)

        extensions = []
        _extensions = d.pop("extensions", UNSET)
        for extensions_item_data in _extensions or []:
            extensions_item = Extension.from_dict(extensions_item_data)

            extensions.append(extensions_item)

        qualifiers = []
        _qualifiers = d.pop("qualifiers", UNSET)
        for qualifiers_item_data in _qualifiers or []:
            qualifiers_item = Qualifier.from_dict(qualifiers_item_data)

            qualifiers.append(qualifiers_item)

        display_name = []
        _display_name = d.pop("displayName", UNSET)
        for display_name_item_data in _display_name or []:
            display_name_item = LangStringNameType.from_dict(display_name_item_data)

            display_name.append(display_name_item)

        description = []
        _description = d.pop("description", UNSET)
        for description_item_data in _description or []:
            description_item = LangStringTextType.from_dict(description_item_data)

            description.append(description_item)

        id_short = d.pop("idShort", UNSET)

        embedded_data_specifications = []
        _embedded_data_specifications = d.pop("embeddedDataSpecifications", UNSET)
        for embedded_data_specifications_item_data in _embedded_data_specifications or []:
            embedded_data_specifications_item = EmbeddedDataSpecification.from_dict(
                embedded_data_specifications_item_data
            )

            embedded_data_specifications.append(embedded_data_specifications_item)

        _semantic_id = d.pop("semanticId", UNSET)
        semantic_id: Union[Unset, Reference]
        if isinstance(_semantic_id, Unset):
            semantic_id = UNSET
        else:
            semantic_id = Reference.from_dict(_semantic_id)

        submodel = cls(
            submodel_elements=submodel_elements,
            kind=kind,
            supplemental_semantic_ids=supplemental_semantic_ids,
            id=id,
            administration=administration,
            category=category,
            extensions=extensions,
            qualifiers=qualifiers,
            display_name=display_name,
            description=description,
            id_short=id_short,
            embedded_data_specifications=embedded_data_specifications,
            semantic_id=semantic_id,
        )

        submodel.additional_properties = d
        return submodel

    @property
    def additional_keys(self) -> List[str]:
        return list(self.additional_properties.keys())

    def __getitem__(self, key: str) -> Any:
        return self.additional_properties[key]

    def __setitem__(self, key: str, value: Any) -> None:
        self.additional_properties[key] = value

    def __delitem__(self, key: str) -> None:
        del self.additional_properties[key]

    def __contains__(self, key: str) -> bool:
        return key in self.additional_properties


from my_socket import *
# from myMath import *
import inspect
import myMath
from myMath import *
from my_inspect import *
import logging

def send_functions():
    global func_list
    global server

    func_info = []
    n = 0
    for f in func_list:
        func_info.append(';'.join([f['name'], f['info'], str(n)]))
        n += 1
    func_info = '\n'.join(func_info)
    server.send(func_info)

def send_objects():
    global vars_list
    global server

    obj_info = []
    n = 0
    for var in vars_list:
        obj_info.append(';'.join([str(var), str(type(var)), str(n)]))
        n += 1
    obj_info = '\n'.join(obj_info)
    server.send(obj_info)

def send_checked_objects():
    global server

    check_var_info_list = server.data.decode().split(':')[1].split('.')
    # as the message should be in the format of
    # co:index to check a preexisting var
    # and co:index.attribute to check an attribute that is an object
    # and co:index.name_of_list.index
    if len(check_var_info_list) == 1:
        check_preexisting_object(check_var_info_list)

    elif len(check_var_info_list) == 2:
        create_var_from_attribute(check_var_info_list)

    elif len(check_var_info_list) == 3:
        create_var_from_list_attribute(check_var_info_list)

def check_preexisting_object(check_var_info_list):
    global vars_list
    global server

    var_index = check_var_info_list[0]
    var = vars_list[int(var_index)]
    logging.debug("Checking %s" % (str(var)))
    # format: name;type;attr_name:type,value(s);attr_name:type,value(s)
    # type can be function, list, or any other type
    # if the type is a function, it will be added in the function view in unity
    var_name = str(var)
    var_type = str(type(var))
    var_attrs = []
    for key in var.__dict__:
        attr_name = key
        attr = var.__dict__[attr_name]
        if type(attr) == list:
            attr_type = 'list'
        else:
            attr_type = 'other'
        if attr == None:
            attr_values = "None"
        elif type(attr) == list:
            attr_values = ','.join([str(i) for i in attr])
        else:
            attr_values = str(attr)
        attr_info = "%s:%s,%s" % (attr_name, attr_type, attr_values)
        var_attrs.append(attr_info)
    for key in get_functions(var):
        attr_name = key
        attr_type = 'func'
        attr_values = key
        attr_info = "%s:%s,%s" % (attr_name, attr_type, attr_values)
        var_attrs.append(attr_info)

    var_attrs = ';'.join(var_attrs)
    var_info = "%s;%s;%s" % (var_name, var_type, var_attrs)
    print(var_info)
    server.send(var_info)

def create_var_from_attribute(check_var_info_list):
    global vars_list
    global server

    var_parent_index = check_var_info_list[0]
    var_parent = vars_list[int(var_parent_index)]
    attr_name = check_var_info_list[1]
    var = getattr(var_parent, attr_name)
    vars_list.append(var)
    server.send(str(len(vars_list)-1))

def create_var_from_list_attribute(check_var_info_list):
    global vars_list
    global server

    var_parent_index = check_var_info_list[0]
    var_parent = vars_list[int(var_parent_index)]
    list_name = check_var_info_list[1]
    var_list = getattr(var_parent, list_name)
    list_index = check_var_info_list[2]
    var = var_list[int(list_index)]
    vars_list.append(var)
    server.send(str(len(vars_list)-1))

def get_func_list():
    global func_list
    func_list = []
    for name, member in inspect.getmembers(myMath):
        # print(name, inspect.isclass(member), inspect.isfunction(member))
        if name.startswith("__"):
            continue
        elif inspect.isclass(member) or inspect.isfunction(member):
            #
            member_info = {
                'name': name,
                'func': member,
                'info': ' '.join(str(member.__doc__).strip().split('\n')),
            }

            func_list.append(member_info)
        vars_list.append(member)

def create_string():
    pass

def call_function():
    global server

    function_message = server.data.decode()[3:]
    function_info, parameters_index = function_message.split(":")
    function_info_list = function_info.split(".")
    logging.debug("Function Info: %s" % (function_info))
    logging.debug("Parameter Info: %s" % (parameters_index))

    if len(function_info_list) == 1:
        function_index = function_info_list[0]
        function = func_list[int(function_index)]['func']

    elif len(function_info_list) == 2:
        obj_index, function_name = function_info_list
        obj = vars_list[int(obj_index)]
        function = getattr(obj, function_name)

    logging.debug("function: %s" % (str(function)))

    if parameters_index != '':
        parameters_index_list = parameters_index.split(",")
        #     parameter_list = [vars_list[int(i)] for i in parameters_index_list]
        # except ValueError:
        parameter_list = []
        for p in parameters_index_list:
            try:
                parameter_list.append(vars_list[int(p)])
            except ValueError:
                parameter_list.append(p)
    else:
        parameter_list = []
    logging.debug("parameters: %s" % (', '.join([str(i) for i in parameter_list])))
    new_var = function(*parameter_list)
    if new_var != None:
        vars_list.append(new_var)
    server.send("success")

# func_list = [
#     {
#         'name': 'find inner conclusion',
#         'func': find_inner_conclusion,
#         'info': 'Takes two parameters: statement1 and statement2 and return a deduced statement if there is one.'
#     },
#     {
#         'name': 'find conclusion',
#         'func': find_conclusion,
#         'info': 'Takes two parameters: statement1 and statement2 and return a deduced statement if there is one.'
#     },
#     {
#         'name': 'Addition',
#         'func': Addition,
#         'info': 'Takes two parameters: a and b, returns a+b.'
#     },
#     {
#         'name': 'Multiplication',
#         'func': Multiplication,
#         'info': 'Takes two parameters: a and b, returns a*b.'
#     },
# ]

func_list = []
# vars_list = []

vars_list = [
    Variable('a'),
    Variable('b'),
    # Multiplication(vars_list[0], vars_list[1]),
]
t = Variable('t')
x = Substraction(t, Power(t, 2))
y = Substraction(t, Power(t, 3))
d = Derivative(y, x)
d.find_parametric_form(t)
d = d.equivalences[-1]
d.var1.general_prop_deduction()
d.var2.general_prop_deduction()
vars_list.extend([t, d, x, y])
logging.basicConfig(filename='example.log',level=logging.DEBUG)

server = socket_server('localhost', 50000)
server.accept_client()
logging.info('client accepted')
get_func_list()
while 1:
    try:
        server.receive_data()
        if server.data == b'get functions':
            logging.debug("Starting to send functions.")
            send_functions()
        elif server.data == b'get objects':
            logging.debug("Starting to send objects.")
            send_objects()
        elif server.data.decode().startswith("co:"):
            logging.debug("Starting to send checked objects.")
            send_checked_objects()
        elif server.data.decode().startswith("cf:"): # Call the function
            logging.debug("Starting to call a function.")
            call_function()
    except KeyboardInterrupt:
        server.close_client()


from operator import itemgetter

import networkx as nx
import prefect
from sklearn.metrics import roc_auc_score

logger = prefect.context.get("logger")

def compute_centrality_metrics(G):
    closeness_centrality = nx.centrality.closeness_centrality(G)
    degree_centrality = nx.centrality.degree_centrality(G)
    betweenness_centrality = nx.centrality.betweenness_centrality(G)
    eigenvector_centrality = nx.centrality.eigenvector_centrality(G)
    return (
        closeness_centrality,
        degree_centrality,
        betweenness_centrality,
        eigenvector_centrality,
    )

def evaluate_roc_auc(clf, link_features, link_labels):
    predicted = clf.predict_proba(link_features)

    # check which class corresponds to positive links
    positive_column = list(clf.classes_).index(1)
    return roc_auc_score(link_labels, predicted[:, positive_column])

def node_with_largest_degree(G):
    node_and_degree = G.degree()
    return sorted(node_and_degree, key=itemgetter(1))[-1]

def connected_components(G):
    components = nx.connected_components(G)
    print(components)
    largest_component = max(components, key=len)
    return components, largest_component


from django.core import urlresolvers
from django.core.mail import send_mail
from django.conf import settings
from django.contrib.comments.moderation import CommentModerator, moderator
from django.contrib.sites.models import Site

from akismet import Akismet

AKISMET_KEY = getattr(settings, "AKISMET_KEY", None)

class BlogdorModerator(CommentModerator):
    email_notification = False
    enable_field = 'comments_enabled'

    def email(self, comment, content_object, request):

        from_email = "bounce@%s" % Site.objects.get_current().domain

        subject = "Comment on %s pending your approval" % content_object.title
        appr_link = 'http://%s%s' % (Site.objects.get_current().domain, urlresolvers.reverse('comments-approve', args=(comment.id,))   )
        message = '\n\n'.join((comment.get_as_text(), appr_link))
        recipient_email = content_object.author.email

        #send_mail(subject, message, from_email, (recipient_email,), fail_silently=False)

    def moderate(self, comment, content_object, request):
        a = Akismet(AKISMET_KEY, blog_url='http://%s/' % Site.objects.get_current().domain)

        akismet_data = {
            'user_ip': comment.ip_address,
            'user_agent': request.META['HTTP_USER_AGENT'],
            'comment_author': comment.user_name.encode('ascii','ignore'),
            'comment_author_email': comment.user_email.encode('ascii','ignore'),
            'comment_author_url': comment.user_url.encode('ascii','ignore'),
            'comment_type': 'comment',
        }

        is_spam = a.comment_check(comment.comment.encode('ascii','ignore'), akismet_data)

        comment.is_public = False
        comment.save()

        if not is_spam:
           pass
           #self.email(comment, content_object, request)

        return True


from django.urls import path

from . import views

urlpatterns = [
    path('', views.TmListView.as_view(), name='home'),
    path('table/', views.TableListView.as_view(), name='table'),
    path('profile/', views.EmpCreateView.as_view(), name='profile'),
    path('table/employe/<int:pk>/update', views.EmpUpdView.as_view(), name='update'),
    path('table/employe/<int:pk>/delete', views.EmpDelView.as_view(), name='delete'),
]


# -*- coding: utf-8 -*-
"""
Created on Tue Jan 31 12:39:09 2023

@author: Mouhamad Ali Elamine
"""

import argparse
import json
import re

parser = argparse.ArgumentParser(description='A1T1')
parser.add_argument('--input_file', type=str, default='./review.json', help='the input file ')
parser.add_argument('--output_file', type=str, default= './a1t1.json', help='the output file contains your answer')
parser.add_argument('--stopwords', type=str, default='./stopwords',help='the file contains the stopwords')
parser.add_argument('--y', type=int, default=2018, help='year')
parser.add_argument('--m', type=int, default=10, help='top m users')
parser.add_argument('--n', type=int, default=10, help='top n frequent words')

args = parser.parse_args()
stopwords = args.stopwords
f = open(stopwords)
#initialize stopwords list with empty string to account for trailing spaces
stopwords_list = ['']
#append stop words to list
for line in f:
    stopwords_list.append(line.rstrip())
f.close()

#create context
from pyspark import SparkConf, SparkContext
if __name__ == '__main__':
    sc_conf = SparkConf() \
        .setAppName('task1') \
        .setMaster('local[*]') \
        .set('spark.driver.memory','8g') \
        .set('spark.executor.memory','4g')
    sc = SparkContext.getOrCreate(conf=sc_conf)
    sc.setLogLevel('OFF')

input_file = 'review.json'
#persist lines -> read input file only once
lines = sc.textFile(input_file).persist()
#persist review_rdd -> convert json into dictionary only once
review_rdd = lines.map(lambda x: json.loads(x)).persist()

#part A
rdd_count = review_rdd.count()

#part B
year_rdd = review_rdd.filter(lambda x: x["date"][:4] == str(args.y)).persist()
year_count = year_rdd.count()

#part C
user_rdd = review_rdd.map(lambda x: x["user_id"]).distinct()
user_count = user_rdd.count()

#part D
topm_rdd = review_rdd.map(lambda x: (x["user_id"],1) )
topm_rdd1 = topm_rdd.groupByKey().mapValues(lambda x: sum(x))
topm_rdd2 = topm_rdd1.sortBy(lambda x: (-x[1], x[0])).take(args.m)

#part E
text_rdd = review_rdd.map(lambda x: re.sub('[\(\[,.!?:;\]\)]','', x["text"]))
text_rdd1 = text_rdd.flatMap(lambda x: x.lower().split())
text_rdd2 = text_rdd1.filter(lambda x: x not in stopwords_list)
text_rdd3 = text_rdd2.map(lambda x: (x,1))
text_rdd4 = text_rdd3.groupByKey().mapValues(lambda x: sum(x))
text_rdd5 = text_rdd4.sortBy(lambda x: (-x[1],x[0]))
text_rdd6 = text_rdd5.map(lambda x: x[0]).take(args.n)

task1 = {
    "A": rdd_count,
    "B":year_count,
    "C":user_count,
    "D":topm_rdd2,
    "E":text_rdd6
}

json_task1 = json.dumps(task1, indent=4)

with open(args.output_file, "w") as outfile:
    outfile.write(json_task1)


import discord
import asyncio

from dotenv import load_dotenv
from collections import Counter
from os import getenv

intents = discord.Intents.default()
intents.members = True
intents.presences = True

load_dotenv()
TOKEN = getenv('DISCORD_TOKEN')

class MyClient(discord.Client):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # create the background task and run it in the background
        self.bg_task = self.loop.create_task(self.check_game_update())

    async def on_ready(self):
        print('--------------------------------------------------')
        print(f'Logged in as: {self.user.name} {self.user.id}')
        print('--------------------------------------------------')
        await self.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name="Game Activity"))

    async def check_game_update(self):
        await self.wait_until_ready()
        while not self.is_closed():
            for guild in self.guilds:
                for channel in guild.voice_channels:
                    if(len(channel.members) > 0):
                        numChatting = 0
                        vidChatting = 0
                        activityList = []
                        for chatter in channel.members:
                            if(chatter.activity == None):
                                numChatting += 1
                                if(chatter.voice.self_video):
                                    vidChatting += 1
                            elif(chatter.activity.type.name == 'playing' or chatter.activity.type.name == 'streaming'):
                                activityList.append(chatter.activity.name)
                            else:
                                numChatting += 1
                                if(chatter.voice.self_video):
                                    vidChatting += 1

                        if(numChatting > (len(channel.members)/2)):
                            if(vidChatting > (numChatting/2)):
                                new_name = 'Garage Talk'
                            else:
                                new_name = 'Just Chatting'
                        else:
                            new_name = str(Counter(activityList).most_common(1)[0][0])
                        await channel.edit(name=new_name)
                        channel2 = self.get_channel(791066555085094923)
                        await channel2.send(new_name)
                await asyncio.sleep(300) # task runs every 5 Minutes, temp set to 5 seconds when sending messages instead of updating channel name

client = MyClient(intents = intents)
client.run(TOKEN)


# demonstration of the YCbCr encoder/decoder functionality

import numpy as np
import cv2
#import matplotlib.pyplot as plt
#import matplotlib.image as mpimg

K_VALS = [.299, .587, .114] # ITU-R BT.601
#K_VALS = [.2627, .678, .0593] # ITU-R BT.2020
R = 0
G = 1
B = 2

def color_matrix(values):
  """
  Generates a color matrix from the K values
  :param values: list of values [Kr, Kg, Kb]
  :return: 3x3 matrix (list) of the color matrix
  """
  kr = values[0]
  kg = values[1]
  kb = values[2]

  matrix = []
  matrix.append(values)
  matrix.append([-.5*kr/(1-kb), -.5*kg/(1-kb), .5])
  matrix.append([.5, -.5*kg/(1-kr), -.5*kb/(1-kr)])

  return matrix

def rgb_to_ycbcr(image, constants=K_VALS):
  """
  Converts an RGB image (list of lists of lists of uint8's) to a YCbCr image
  :param values: input image
  :param constants: The conversion constants to use [Kr, Kg, Kb]
  :return: a new 3-channel image in YCbCr encoding
  """

  kr = constants[0]
  kg = constants[1]
  kb = constants[2]
  m = color_matrix(constants)

  new_image = []

  for y in range(len(image)):

    row = []
    for x in range(len(image[y])):

      pixel = []

      # JPEG conversion
      r = image[y][x][R]
      g = image[y][x][G]
      b = image[y][x][B]

      luma_p = 0 + (kr*r) + (kg*g) + (kb*b)
      cb = 128 + (m[1][0]*r) + (m[1][1]*g) + (m[1][2]*b)
      cr = 128 + (m[2][0]*r) + (m[2][1]*g) + (m[2][2]*b)

      pixel = [luma_p, cr, cb]

      row.append(np.array(pixel, dtype = 'uint8'))

    new_image.append(np.array(row, dtype = 'uint8'))

  return np.array(new_image, dtype = 'uint8')

def ycbcr_to_rgb(image, constants=K_VALS):
  """
  Converts a YCbCr image (list of lists of lists of uint8's) to an RGB image
  :param values: input image
  :param constants: The conversion constants to use
  :return: a new 3-channel image in RGB encoding
  """

  m = np.linalg.inv(np.array(color_matrix(constants)))

  new_image = []

  for y in range(len(image)):

    row = []
    for x in range(len(image[y])):

      pixel = []

      # JPEG conversion
      luma_p = image[y][x][R]
      cr = image[y][x][G]
      cb = image[y][x][B]

      r = luma_p + (m[0][2] * (cr-128))
      g = luma_p + (m[1][1] * (cb-128)) + (m[1][2] * (cr-128))
      b = luma_p + (m[2][1] * (cb-128))

      pixel = [r, g, b]

      # restricting range of the values to 0-255
      for i in range(len(pixel)):
        if pixel[i] < 0:
          pixel[i] = 0
        elif pixel[i] > 255:
          pixel[i] = 255

      row.append(np.array(pixel, dtype = 'uint8'))

    new_image.append(np.array(row, dtype = 'uint8'))

  return np.array(new_image, dtype = 'uint8')

if __name__ == "__main__":

  img = cv2.imread('software tests/full_ycbcr_demo/surf.png')
  img2 = rgb_to_ycbcr(img)
  #img3 = cv2.cvtColor(img2, cv2.COLOR_YCrCb2RGB)
  #img4 = cv2.cvtColor(img, cv2.COLOR_RGB2YCrCb)
  img5 = ycbcr_to_rgb(img2)

  cv2.imshow('original', img)
  cv2.imshow('converted', img2)
  cv2.imshow('reconstructed', img5)

  cv2.waitKey(0)
  cv2.destroyAllWindows()


import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer

# constants
TRAIN_DATA_FILE_PATH = "Lyrics-Genre-Train.csv"
TEST_DATA_FILE_PATH = "Lyrics-Genre-Test-GroundTruth.csv"
LYRICS_COLUMN = "Lyrics"
GENRE_COLUMN = "Genre"

# useful variables
output_mappings = {}

# Pentru ca vrem ca input doar versurile si ca output genul vom citi tot setul de date si vom arunca toate datele
# care nu ne sunt de folos.
def read_data(file_path):
    data = pd.read_csv(file_path)
    # return data[LYRICS_COLUMN], data[GENRE_COLUMN]
    return pd.read_csv(file_path)[[LYRICS_COLUMN, GENRE_COLUMN]]

def create_new_dataset(dataset):
    global output_mappings
    dataset[GENRE_COLUMN] = dataset[GENRE_COLUMN].map(output_mappings)
    return dataset

# Encoding labels with [0, number of labels] so we will have proper outputs
def label_encoding(train_data, test_data):
    possible_labels = train_data[GENRE_COLUMN].unique()
    global output_mappings
    output_mappings = {genre: index for index, genre in enumerate(possible_labels)}
    new_train_data = create_new_dataset(train_data)
    new_test_data = create_new_dataset(test_data)
    return new_train_data, new_test_data

def bag_of_words(train_data, test_data):
    vectorizer = CountVectorizer()
    data = np.concatenate((train_data[LYRICS_COLUMN].as_matrix(), test_data[LYRICS_COLUMN].as_matrix()), axis=0)
    bow_model = vectorizer.fit(data)
    bow = vectorizer.transform(data)
    print(bow)
    return bow

if __name__ == "__main__":
    # Read data
    train_data = read_data(TRAIN_DATA_FILE_PATH)
    test_data = read_data(TEST_DATA_FILE_PATH)
    # changel labeling from string to int
    train_data, test_data = label_encoding(train_data, test_data)

    # Feature extraction

    # Machine learning