#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 29 13:41:07 2022

@author: patrice
edited by Sam Valman
"""

import albumentations as A
import skimage.io as io
from osgeo import gdal_array
from osgeo import gdal
import numpy as np
import glob
import os

#%%
'''
To start, you need a conda env with alnumentations (from pip) and osgeo

Then, get a set of images of at least 1000 x 1000.  Store both as geotifs with the mask and the image having a different, but predictable name,
eg S2image_001.tif for the image and Mask_001.tif for the associated mask.  The mask needs to cover the whole sample image and
give a class for each pixel

Then create destination folders for training and validation.

Script will produce Target samples for each image.  these will be saved as image_XXXXXXX.jpg for the image and
image_XXXXXXX.png for the mask.  Ready for the FCN training code

'''

maskFiles=glob.glob('D:\Training_data\desert\doodle_good\Mask*.tif', recursive=True)
outrootT='C:/Users/lgxsv2/TrainingData/ZZ_Tiramasu/TR/image_' #edit the folder, but leave the: image_
outrootV='C:/Users/lgxsv2/TrainingData/ZZ_Tiramasu/VA/image_'
#%%
BaseSize=224
MaxScale=5
test=False #a few samples to test that data is ok.  Check that mask and images match,  If not, change the flipmask variable.
Target=1000 #if test is false, generate this many samples PER TILE - this is not per tile this is per image
flipmask=True #flip the mask due to gdal odd reading
ValidationSplit=0.2
ls = []

#define the augmentation. Here we randomly select images with sizes from 224 to 224* 5 and resize them to 224.  Then apply random flips
aug = A.Compose([
    A.RandomSizedCrop(min_max_height=[BaseSize, BaseSize*MaxScale], height=BaseSize, width=BaseSize, w2h_ratio=1, always_apply=True, p=1.0),
    A.VerticalFlip(p=0.5),
    A.HorizontalFlip(p=0.5),
    A.Transpose(p=0.5),
    A.RandomRotate90(p=0.5)]
        )

#create a random sequence of numbers to minimise correlation in the image sequence
# new pool so doesnt overlap with original images
new_pool_start = 9999999

# Create the new pool with the same length as the original pool
pool = np.asarray(range(new_pool_start, new_pool_start + new_pool_start))
np.random.shuffle(pool)

#loop through the tiles
count=1
if test:
    # for i in range(len(maskFiles)):

    #     if flipmask:
    #         mask=np.flipud(gdal_array.LoadFile(maskFiles[i]))

    #     else:
    #         mask=gdal_array.LoadFile(maskFiles[i])
    #     #edit this line carefully so that it reconstructs the filepath of the image associated to a mask
    #     imFile=os.path.join(os.path.join(os.path.dirname(os.path.dirname(maskFiles[i])), 'train'),os.path.basename(maskFiles[i])[5:])
    #     image=np.flipud(np.rollaxis(gdal_array.LoadFile(imFile), 0, 3))
    #     if image.shape[2]>3:
    #         image=image[:,:,1:]

    #     for n in range(2):
    #         augmented = aug(image=image, mask=mask)
    #         image_light = augmented['image']
    #         image_light = image_light.astype(np.float32)

    #         image_light = ((image_light*255)/65535).astype(np.uint8)
    #         #.astype(np.uint8)
    #         # image_light = (image_light - np.min(image_light)) / (np.max(image_light) - np.min(image_light)) * 255
    #         # image_light = image_light.astype(np.uint8)

    #         mask_light = augmented['mask'] #.astype(np.uint8)
    #         # mask_light = mask_light.astype(np.float32)

    #         # make sure same method is applied to the holdout image.
    #         # mask_light = ((mask_light - np.min(mask_light)) / (np.max(mask_light) - np.min(mask_light)) * 255).astype(np.uint8)
    #         # mask_light = ((mask_light*255)/65535).astype(np.uint8)

    #         if np.random.random()>ValidationSplit:
    #             io.imsave(outrootT+str(pool[count]).zfill(7)+'.jpg', image_light, check_contrast=False, quality=95)
    #             io.imsave(outrootT+str(pool[count]).zfill(7)+'.png', mask_light, check_contrast=False)
    #             count+=1
    #         else:
    #             io.imsave(outrootV+str(pool[count]).zfill(7)+'.jpg', image_light, check_contrast=False, quality=95)
    #             io.imsave(outrootV+str(pool[count]).zfill(7)+'.png', mask_light, check_contrast=False)
    #             count+=1
        print('Finished test augmentation. Check images. Delete if they are incorect')
else:

    for i in range(len(maskFiles)):

        try:
            if flipmask:
                mask=np.flipud(gdal_array.LoadFile(maskFiles[i]))
            else:
                mask=gdal_array.LoadFile(maskFiles[i])
            #edit this line carefully so that it reconstructs the filepath of the image associated to a mask
            imFile=os.path.join(os.path.join(os.path.dirname(os.path.dirname(maskFiles[i])), 'train'),os.path.basename(maskFiles[i])[5:])
            image = gdal_array.LoadFile(imFile)
            image=np.flipud(np.rollaxis(image, 0, 3))

            if image.shape[2]>3:
                image=image[:,:,1:]
            image = image.astype(np.float32)
            # common_mean = 3000
            # image = image+(common_mean - image.mean())
            # image = image/3000
            # image = ((image*255)/3000).astype(np.uint8)
            # image=np.flipud(np.rollaxis(image, 0, 3))

            n=0
            noAvailable =0
            while n < Target:
                augmented = aug(image=image, mask=mask)

                image_light = augmented['image']#.astype(np.uint8)
                azp = np.all(image_light == 0, axis=-1)
                azp = np.count_nonzero(azp)
                if azp>20:
                    continue
                elif np.count_nonzero(augmented['mask']==1)<20000:
                    noAvailable +=1
                    if noAvailable==200 and n<200:
                        n=99999999999
                    continue
                else:
                    n+=1
                    # image_light = image_light.astype(np.float32)
                    # image_light = image_light/3000
                    image_light = ((image_light/6000)*255)
                    image_light = np.clip(image_light, 0, 255)
                    image_light = image_light.astype(np.uint8)

                    # image_light = ((image_light*255)/6000).astype(np.uint8)
                    # # image_light = ((image_light - np.min(image_light)) / (np.max(image_light) - np.min(image_light)) * 255).astype(np.uint8)
                    # image_light = ((image_light*255)/65535).astype(np.uint8)

                    mask_light = augmented['mask'].astype(np.uint8)

                    if np.random.random()>ValidationSplit:
                        io.imsave(outrootT+str(pool[count]).zfill(7)+'.jpg', image_light, check_contrast=False, quality=95)
                        io.imsave(outrootT+str(pool[count]).zfill(7)+'.png', mask_light, check_contrast=False)
                        count+=1
                    else:
                        io.imsave(outrootV+str(pool[count]).zfill(7)+'.jpg', image_light, check_contrast=False, quality=95)
                        io.imsave(outrootV+str(pool[count]).zfill(7)+'.png', mask_light, check_contrast=False)
                        count+=1
            print('Finished augmentation')
        except ValueError:
            print('too SMall')
            ls.append(maskFiles[i])


import os
from PyPDF2 import PdfMerger

Prefix = ['DOR', 'PGU']
filePath  = os.getcwd()+ '\\'
fileList  = []

for CodePrefix in Prefix:
   for path, currentDirectory, files in os.walk(filePath):
      for fileName in files:
         if fileName.startswith(CodePrefix):
            fileList.append(fileName)

for fileName in fileList:
    if fileName.startswith("PGU"):
        pbr=(filePath + fileName).rpartition('_')[-1]
        pbr=pbr.rpartition('.')[0]

# Append & Merge all PDFs starting with similar names
for CodePrefix in Prefix:
    merger = PdfMerger()
    for fileName in fileList:
        if CodePrefix not in fileName:
            continue
        merger.append(filePath + fileName)

    merger.write(filePath + CodePrefix + '.pdf')
    merger.close()

# Combine PGU and DOR
pdfs = ['PGU.pdf', 'DOR.pdf']

merger = PdfMerger()

for pdf in pdfs:
    merger.append(pdf)

folder_name = os.path.basename(os.getcwd())
merger.write(pbr + '_Merged_' + folder_name + '.pdf')
merger.close()

# Delete file DOR and PGU
os.remove('PGU.pdf')
os.remove('DOR.pdf')


class BSTNode:
  def __init__(self, parent, key):
    self.key = key
    self.parent = parent
    self.left = None
    self.right = None

  def _str(self):
    label = str(self.key)
    if self.left is None:
      left_lines, left_pos, left_width = [], 0, 0
    else:
      left_lines, left_pos, left_width = self.left._str()
    if self.right is None:
      right_lines, right_pos, right_width = [], 0, 0
    else:
      right_lines, right_pos, right_width = self.right._str()
    middle = max(right_pos + left_width - left_pos + 1, len(label), 2)
    pos = left_pos + middle // 2
    width = left_pos + middle + right_width - right_pos
    while len(left_lines) < len(right_lines):
      left_lines.append(' ' * left_width)
    while len(right_lines) < len(left_lines):
      right_lines.append(' ' * right_width)
    if (middle - len(label)) % 2 == 1 and self.parent is not None and \
      self is self.parent.left and len(label) < middle:
      label += '.'
    label = label.center(middle, '.')
    if label[0] == '.':label = ' ' + label[1:]
    if label[-1] == '.': label = label[:-1] + ' '
    lines = [' ' * left_pos + label + ' ' * (right_width - right_pos),
             ' ' * left_pos + '/' + ' ' * (middle - 2) +
             '\\' + ' ' * (right_width - right_pos)] + \
      [left_line + ' ' * (width - left_width - right_width) + right_line
        for left_line, right_line in zip(left_lines, right_lines)]
    return lines, pos, width

  def __str__(self):
    return '\n'.join(self._str()[0])

  def find(self, k):
    if k == self.key:
      return self
    elif k < self.key:
      if self.left is None:
        return None
      else:
        return self.left.find(k)
    else:
      if self.right is None:
        return None
      else:
        return self.right.find(k)

  def find_min(self):
    current = self
    while current.left is not None:
      current = current.left
    return current

  def next_larger(self):
    if self.right is not None:
      return self.right.find_min()
    current = self
    while current.parent is not None and current is current.parent.right:
      current = current.parent
    return current.parent

  def insert(self, node):
    if node is None:
      return
    if node.key < self.key:
      if self.left is None:
        node.parent = self
        self.left = node
      else:
        self.left.insert(node)
    else:
      if self.right is None:
        node.parent = self
        self.right = node
      else:
        self.right.insert(node)

  def delete(self):
    if self.left is None or self.right is None:
      if self is self.parent.left:
        self.parent.left = self.left or self.right
        if self.parent.left is not None:
          self.parent.left.parent = self.parent
      else:
        self.parent.right = self.left or self.right
        if self.parent.right is not None:
          self.parent.right.parent = self.parent
      return self
    else:
      s = self.next_larger()
      self.key, s.key = s.key, self.key
      return s.delete()

  def check_ri(self):
    if self.left is not None:
      if self.left.key > self.key:
        raise RuntimeError("BST RI violated by a left node key")
      if self.left.parent is not self:
        raise RuntimeError("BST RI violated by a left node parent pointer")
      self.left.check_ri()
    if self.right is not None:
      raise RuntimeError("BST RI violated by a right node key")
    if self.right.parent is not self:
      raise RuntimeError("BST RI violated by a right node parent pointer")
      sel.right.check_ri()

class MinBSTNode(BSTNode):
  def __init__(self, parent, key):
    super(MinBSTNode, self).__init__(parent, key)
    self.min = self

  def find_min(self):
    return self.min

  def insert(self, node):
    if node is None:
      return
    if node.key < self.key:
      if node.key < self.min.key:
        self.min = node
      if self.left is None:
        node.parent = self
        self.left = node
      else:
        self.left.insert(node)
    else:
      if self.right is None:
        node.parent = self
        self.right = node
      else:
        self.right.insert(node)

  def delete(self):
    if self.left is None or self.right is None:
      if self is self.parent.left:
        self.parent.left = self.left or self.right
        if self.parent.left is not None:
          self.parent.left.parent = self.parent
          self.parent.min = self.parent.left.min
        else:
          self.parent.min = self.parent

        c = self.parent
        while c.parent is not None and c is c.parent.left:
          c.parent.min = c.min
          c = c.parent
      else:
        self.parent.right = self.left or self.right
        if self.parent.right is not None:
          self.parent.right.parent = self.parent
      return self
    else:
      s = self.next_larger()
      self.key, s.key = s.key, self.key
      return s.delete()

class BST:
  def __init__(self, klass = BSTNode):
    self.root = None
    self.klass = klass

  def __str__(self):
    if self.root is None:
      return '<empty tree>'
    return str(self.root)

  def find(self, k):
    return self.root and self.root.find(k)

  def find_min(self):
    return self.root and self.root.find_min()

  def insert(self, k):
    node = self.klass(None, k)
    if self.root is None:
      self.root = node

    else:
      self.root.insert(node)
    return node

  def delete(self, k):
    node = self.find(k)
    if node is None:
      return None
    if node is self.root:
      pseudoroot = self.klass(None, 0)
      pseudoroot.left = self.root
      self.root.parent = pseudoroot
      deleted = self.root.delete()
      self.root = pseudoroot.left()
      if self.root is not None:
        self.root.parent = None
      return deleted
    else:
      return node.delete()

  def next_larger(self, k):
    node = self.find(k)
    return node and node.next_larger()

  def check_ri(self):
    if self.root is not None:
      if self.root.parent is not None:
        raise RuntimeError("BST RI violated by the root node's parent "
                                   "pointer.")
      self.root.check_ri()

class MinBST(BST):
  def __init__(self):
    super(MinBST, self).__init__(MinBSTNode)

def test(args= None, BSTtype = BST):
  import random, sys
  if not args:
    args = sys.argv[1:]
  if not args:
    print('usage: %s <number-of-random-items | item item item ...>' % \
          sys.argv[0])
    sys.exit()
  elif len(args) == 1:
    items = (random.randrange(100) for i in range(int(args[0])))
  else:
    items = [int(i) for i in args]

  tree = BSTtype()
  print(tree)
  for item in items:
      tree.insert(item)
      print("")
      print(tree)
  # data = tree.root._str()
  # for n in range(len(data)):
  #   print(data[n])

if __name__ == '__main__': test()


class Solution(object):
    def isInterleave(self, s1, s2, s3):
        """
        :type s1: str
        :type s2: str
        :type s3: str
        :rtype: bool
        """
        n1 = len(s1)
        n2 = len(s2)
        n3 = len(s3)
        if n1 + n2 != n3:
            return False

        dp = [[False for _ in range(n2+1)] for _ in range(n1+1)]
        dp[0][0] = True
        for i in range(1, n1+1):
            dp[i][0] = dp[i-1][0] and s1[i-1]==s3[i-1]
        for j in range(1, n2+1):
            dp[0][j] = dp[0][j-1] and s2[j-1]==s3[j-1]

        for i in range(1, n1+1):
            for j in range(1, n2+1):
                dp[i][j] = (s1[i-1]==s3[i+j-1] and dp[i-1][j]) or (s2[j-1]==s3[i+j-1] and dp[i][j-1])

        return dp[n1][n2]


# -*- coding: utf-8 -*-
"""
Created on Fri Oct 22 19:51:20 2021

@author: Juan Antonio
Title: main graficas

ver = 2.0
"""

import pandas as pd
import numpy as np
import cv2 as cv
import os
import functions_graph2 as graph2
import matplotlib.pyplot as plt
#variables

lista_global_ltd = list()
lista_global_lxmd = list()
lista_extras = list()
#Graficacion
x = str(input('Nombre de simulación: '))
h = int(input('histo pres:'))
directories = os.listdir('assets')
try:
    os.mkdir(f'assets_graphs\\{x}')
except:
    pass

for directory in directories:
    rute = f'assets_graphs\\{x}\\{directory}'
    try:
        os.mkdir(rute)
    except:
        pass
    data = pd.read_excel(f'assets\\{directory}\\Datos.xlsx')

    c = data.circulo_cords
    t = data.Tiempo
    r = data.regla_inc_a

    lx = list(c.map(lambda x: int(x[1: x.index(',')])))
    ly = list(c.map(lambda x: int(x[x.index(',')+1:len(x)-1])))

    lt = list(t.map(lambda x: x))

    lxc = list(r.map(lambda x: int(x[1: x.index(',')])))
    lyc = list(r.map(lambda x: int(x[x.index(',')+1:len(x)-1])))

    a,b,d,e = graph2.gd(lt,lx, ema_r = 1, ema_i = 5, popi = 2, name = 'lx', ref = rute)

    d1mb = graph2.dif(d)
    d1ma = graph2.dif(a)
    graph2.gd(list(range(len(d1mb))), d1mb, ema_r = 2, ema_i = 5, name = 'lxmd', ref=rute)
    graph2.gd(list(range(len(d1ma))), d1ma, ema_r = 1, ema_i = 5, name= 'ltd', ref= rute)

    rango, varianza, desviacion, cov, media, moda, mediana = graph2.md(d1mb)
    lista_loc = [['lxmd',d1mb],['rango',rango],['varianza',varianza],
                 ['desviacion_e', desviacion],['coeficiente_de_variacion',cov],
                 ['media', media],['moda',moda],['mediana',mediana]]
    graph2.excel(lista_loc, 'Data', rute)

    graph2.hist(d1mb, h, ruidomax=2, ruidomin=2, name='lxmdhist', ref =rute)

    extra = graph2.dif(b)

    extra = graph2.razon(extra)
    for el in extra:
        lista_extras.append(el)
    for el in d1mb:
        lista_global_lxmd.append(el)
    for el in d1ma:
        lista_global_ltd.append(el)

# Grabacion general AA

rango, varianza, desviacion, cov, media, moda, mediana = graph2.md(lista_global_lxmd,
                                                                   popd=len(directories)*4,
                                                                   popi=len(directories)*4)
lista_global_lxmd.sort()
graph2.pop(len(directories)*4, 1, lista_global_lxmd)
lista_global_f = [['lxmd',lista_global_lxmd],['ltmd', lista_extras],['rango',rango],['varianza',varianza],
                 ['desviacion_e', desviacion],['coeficiente_de_variacion',cov],
                 ['media', media],['moda',moda],['mediana',mediana]]

try:
    os.mkdir(f'assets_graphs\\{x}\\AA')
except:
    pass

graph2.hist(lista_global_lxmd, h,ruidomax = len(directories)*4,
            ruidomin = len(directories)*2*0,name ='lxmdhist',
            ref=f'assets_graphs\\{x}\\AA')

graph2.excel(lista_global_f, 'Data', f'assets_graphs\\{x}\\AA')


import threading
import time

class MyThread(threading.Thread):
    def __init__(self):
        super(MyThread, self).__init__()

    def run(self):
        for i in range(3):
            print("I am %s ,%s" % (self.name, i))
            time.sleep(1)

for i in range(3):
    t = MyThread()
    t.start()


import numpy as np
import sys
import pickle
import math

amino_acid_set = { "A": 1, "C": 2, "E": 3, "D": 4, "G": 5, "F": 6, "I": 7, "H": 8, "K": 9, "M": 10, "L": 11,
                "N": 12, "Q": 13, "P": 14, "S": 15, "R": 16, "T": 17, "W": 18, "V": 19, "Y": 20, "X": 21 }

amino_acid_num = 21

ss_set = {"C": 1, "H": 2, "E": 3}
ss_number = 3

physicochemical_set={'A': 1, 'C': 3, 'B': 7, 'E': 5, 'D': 5, 'G': 2, 'F': 1,
            'I': 1, 'H': 6, 'K': 6, 'M': 1, 'L': 1, 'O': 7, 'N': 4,
            'Q': 4, 'P': 1, 'S': 4, 'R': 6, 'U': 7, 'T': 4, 'W': 2,
            'V': 1, 'Y': 4, 'X': 7, 'Z': 7}

residue_list = list(amino_acid_set.keys())
ss_list = list(ss_set.keys())

new_key_list = []
for i in residue_list:
    for j in ss_list:
        str_1 = str(i)+str(j)
        new_key_list.append(str_1)

new_value_list = [x+1 for x in list(range(amino_acid_num*ss_number))]

seq_ss_set = dict(zip(new_key_list,new_value_list))
seq_ss_number = amino_acid_num*ss_number #75

def label_sequence(line, pad_prot_len, res_ind):
    X = np.zeros(pad_prot_len)

    for i, res in enumerate(line[:pad_prot_len]):
        X[i] = res_ind[res]

    return X

def label_seq_ss(line, pad_prot_len, res_ind):
    line = line.strip().split(',')
    X = np.zeros(pad_prot_len)
    for i ,res in enumerate(line[:pad_prot_len]):
        X[i] = res_ind[res]
    return X

def sigmoid(x):
    return 1 / (1 + math.exp(-x))

sigmoid_array=np.vectorize(sigmoid)

def padding_sigmoid_pssm(x,N):
    x = sigmoid_array(x)
    padding_array = np.zeros([N,x.shape[1]])
    if x.shape[0]>=N: # sequence is longer than N
        padding_array[:N,:x.shape[1]] = x[:N,:]
    else:
        padding_array[:x.shape[0],:x.shape[1]] = x
    return padding_array

def padding_intrinsic_disorder(x,N):
    padding_array = np.zeros([N,x.shape[1]])
    if x.shape[0]>=N: # sequence is longer than N
        padding_array[:N,:x.shape[1]] = x[:N,:]
    else:
        padding_array[:x.shape[0],:x.shape[1]] = x
    return padding_array

if __name__ == '__main__':
    input_file = sys.argv[1]
    f = open(input_file)
    pep_set = set()
    prot_set = set()
    pep_ss_set = set()
    prot_ss_set = set()

    for line in f.readlines()[1:]: # if the file has headers and pay attention to the columns (whether have peptide binding site labels)
        prot, pep, label, pep_ss, prot_ss  = line.strip().split('\t')
        pep_set.add(pep)
        prot_set.add(prot)
        pep_ss_set.add(pep_ss)
        prot_ss_set.add(prot_ss)

    f.close()
    pep_len = [len(pep) for pep in pep_set]
    prot_len = [len(seq) for seq in prot_set]
    pep_ss_len = [len(pep_ss) for pep_ss in pep_ss_set]
    prot_ss_len = [len(seq_ss) for seq_ss in prot_ss_set]

    pep_len.sort()
    prot_len.sort()
    pep_ss_len.sort()
    prot_ss_len.sort()
    pad_pep_len = 50
    pad_prot_len = prot_len[int(0.8*len(prot_len))-1]
    print('num of peptides', len(pep_len), 'pad_pep_len', pad_pep_len)
    print('prot_set', len(prot_len), 'pad_prot_len', pad_prot_len)
    print('num of peptide ss', len(pep_ss_len), 'pad_pep_len', pad_pep_len)
    print('prot_ss_set', len(prot_ss_len), 'pad_prot_len', pad_prot_len)
    np.save('./preprocessing/pad_pep_len',pad_pep_len)
    np.save('./preprocessing/pad_prot_len',pad_prot_len)
    np.save('./preprocessing/pad_pep_len',pad_pep_len)
    np.save('./preprocessing/_pad_prot_len',pad_prot_len)

    # load raw dense features, the directory dense_feature_dict and proprocessing need to be created first.
    with open('./dense_feature_dict/Protein_pssm_dict', 'rb') as f: # value: (sequence_length, 20) without sigmoid
        protein_pssm_dict = pickle.load(f)

    with open('./dense_feature_dict/Protein_Intrinsic_dict', 'rb') as f: # value: (sequence_length, 3): long, short, anchor
        protein_intrinsic_dict = pickle.load(f)

    with open('./dense_feature_dict/Peptide_Intrinsic_dict_v3', 'rb') as f: # value: (sequence_length, 3): long, short, anchor
        peptide_intrinsic_dict = pickle.load(f)

    peptide_feature_dict = {}
    protein_feature_dict = {}

    peptide_ss_feature_dict = {}
    protein_ss_feature_dict = {}

    peptide_2_feature_dict = {}
    protein_2_feature_dict = {}

    peptide_dense_feature_dict = {}
    protein_dense_feature_dict = {}

    protein_intrinsic_feature_dict = {}
    f = open(input_file)
    for line in f.readlines()[1:]:
        prot, pep, label, pep_ss, prot_ss  = line.strip().split('\t')
        if pep not in peptide_feature_dict:
            feature = label_sequence(pep, pad_pep_len, amino_acid_set)
            peptide_feature_dict[pep] = feature
        if prot not in protein_feature_dict:
            feature = label_sequence(prot, pad_prot_len, amino_acid_set)
            protein_feature_dict[prot] = feature
        if pep_ss not in peptide_ss_feature_dict:
            feature = label_seq_ss(pep_ss, pad_pep_len, seq_ss_set)
            peptide_ss_feature_dict[pep_ss] = feature
        if prot_ss not in protein_ss_feature_dict:
            feature = label_seq_ss(prot_ss, pad_prot_len, seq_ss_set)
            protein_ss_feature_dict[prot_ss] = feature
        if pep not in peptide_2_feature_dict:
            feature = label_sequence(pep, pad_pep_len, physicochemical_set)
            peptide_2_feature_dict[pep] = feature
        if prot not in protein_2_feature_dict:
            feature = label_sequence(prot, pad_prot_len, physicochemical_set)
            protein_2_feature_dict[prot] = feature
        if pep not in peptide_dense_feature_dict:
            feature = padding_intrinsic_disorder(peptide_intrinsic_dict[pep], pad_pep_len)
            peptide_dense_feature_dict[pep] = feature
        if prot not in protein_dense_feature_dict:
            feature_pssm = padding_sigmoid_pssm(protein_pssm_dict[prot], pad_prot_len)
            feature_intrinsic = padding_intrinsic_disorder(protein_intrinsic_dict[prot], pad_prot_len)
            feature_dense = np.concatenate((feature_pssm, feature_intrinsic), axis=1)
            protein_dense_feature_dict[prot] = feature_dense
        if prot not in protein_intrinsic_feature_dict:
            feature_intrinsic = padding_intrinsic_disorder(protein_intrinsic_dict[prot], pad_prot_len)
            protein_intrinsic_feature_dict[prot] = feature_intrinsic

    f.close()

    with open('./preprocessing/peptide_feature_dict','wb') as f:
        pickle.dump(peptide_feature_dict,f)
    with open('./preprocessing/protein_feature_dict','wb') as f:
        pickle.dump(protein_feature_dict,f)
    with open('./preprocessing/peptide_ss_feature_dict','wb') as f:
        pickle.dump(peptide_ss_feature_dict,f)
    with open('./preprocessing/protein_ss_feature_dict','wb') as f:
        pickle.dump(protein_ss_feature_dict,f)
    with open('./preprocessing/peptide_2_feature_dict','wb') as f:
        pickle.dump(peptide_2_feature_dict,f)
    with open('./preprocessing/protein_2_feature_dict','wb') as f:
        pickle.dump(protein_2_feature_dict,f)
    with open('./preprocessing/peptide_dense_feature_dict','wb') as f:
        pickle.dump(peptide_dense_feature_dict,f)
    with open('./preprocessing/protein_dense_feature_dict','wb') as f:
        pickle.dump(protein_dense_feature_dict,f)


# -*- coding: utf8 -*-
"""
  Created on 2018/5/21.
"""
from console.repositories.message_repo import msg_repo, announcement_repo
from console.models.main import UserMessage
from goodrain_web.tools import JuncheePaginator
from www.utils.crypt import make_uuid
from console.constants import MessageType
from django.db.models import Q

class MessageService(object):
    def sync_announcements_for_user(self, user):
        msgs = msg_repo.get_user_announcements(user.user_id)
        noticed_msg_ids = [msg.announcement_id for msg in msgs]
        announcements = announcement_repo.get_enabled_announcements().exclude(announcement_id__in=noticed_msg_ids)
        msg_list = []
        for announce in announcements:
            msg_list.append(
                UserMessage(
                    message_id=make_uuid(),
                    receiver_id=user.user_id,
                    content=announce.content,
                    is_read=False,
                    msg_type=MessageType.ANNOUNCEMENT,
                    announcement_id=announce.announcement_id,
                    title=announce.title,
                    level=announce.level,
                    create_time=announce.create_time))
        UserMessage.objects.bulk_create(msg_list)
        # 更新已有的公告
        old_announcements = announcement_repo.get_enabled_announcements().filter(announcement_id__in=noticed_msg_ids)
        for announce in old_announcements:
            usermessage_query = msg_repo.get_usermessage_queryset(announce.announcement_id)
            usermessage_query.update(content=announce.content, title=announce.title, level=announce.level)
        # 删除已经删除的公告
        announcements_id_list = announcement_repo.get_all_announcements_id()
        msg_repo.get_all_usermessage().exclude(announcement_id__in=announcements_id_list).delete()

        # 删除关闭启用的公告
        close_announcements = announcement_repo.get_close_announcements().filter(announcement_id__in=noticed_msg_ids)
        close_announcements_list = [obj.announcement_id for obj in close_announcements]
        msg_repo.get_all_usermessage().filter(announcement_id__in=close_announcements_list).delete()

    def get_user_msgs(self, user, page_num, page_size, msg_type, is_read):
        query = Q()
        if msg_type:
            query &= Q(msg_type=msg_type)
        if is_read is not None:
            query &= Q(is_read=is_read)
        if msg_type and is_read:
            msgs = msg_repo.get_user_all_msgs(user.user_id).order_by("-create_time")
        else:
            msgs = msg_repo.get_user_all_msgs(user.user_id).filter(query).order_by("-create_time")
        msg_paginator = JuncheePaginator(msgs, int(page_size))
        total = msg_paginator.count
        page_msgs = msg_paginator.page(page_num)
        return page_msgs, total

    def update_user_msgs(self, user, action, msg_id_list):
        if action == "mark_read":
            UserMessage.objects.filter(receiver_id=user.user_id, ID__in=msg_id_list).update(is_read=True)
        else:
            UserMessage.objects.filter(receiver_id=user.user_id, ID__in=msg_id_list).update(is_read=False)

    def delete_user_msgs(self, user, msg_id_list):
        UserMessage.objects.filter(receiver_id=user.user_id, ID__in=msg_id_list).delete()

msg_service = MessageService()


from flask import Flask,render_template,request,session,redirect,url_for,flash,jsonify,Blueprint

from flask_restful import Resource, Api, reqparse
import re
from flask_mail import Mail,Message

from exts import db, mail
from models import User

my_bp=Blueprint('email and description',__name__)
api=Api(my_bp)
class submitAPI(Resource):
    def post(self):
        # data = request.get_json()
        # email=data["email"] #这里需要修改表单接收的名称
        # description=data["description"]  #这里需要修改表单接收的名称
        parser = reqparse.RequestParser()
        parser.add_argument('description', type=str, help="description错误")
        parser.add_argument('email', type=str, help="email错误")
        args = parser.parse_args()
        email = args.get("email")
        description = args.get("description")
        pattern = r'^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$'  # 正则表达式模式
        match_result = re.match(pattern, email)
        if match_result:
            if not email or not description:
                return jsonify({'code':-1,'msg': 'Missing required fields'})

            user = User(email=email, description=description)
            db.session.add(user)
            db.session.commit()
            msg = Message('邮件主题', sender='m626164783@163.com', recipients=['626164783@qq.com'])
            # 设置邮件内容
            # msg.body = '用户提交时间:'+str(user.submission_time)+'\n用户联系方式：'+user.phone_number+'\n用户需求内容：'+user.description
            msg.body="用户提交时间：{}\n用户联系方式：{}\n用户需求内容：{}\n".format(str(user.submission_time),user.email,user.description)
            mail.send(msg)
            return jsonify({'code':0,'msg': 'Form submitted successfully'})
        else:
            return jsonify({'code':-1,'msg': 'Email format incorrect'})

class pageAPI(Resource):
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('id', type=str, help="id验证错误")
        parser.add_argument('email', type=str, help="email验证错误")
        args = parser.parse_args()
        email = args.get("email")
        id = args.get("id")

        if id:
            users = User.query.filter_by(id=id).all()
        elif email:
            users = User.query.filter_by(email=email).all()
        elif email and id:
            users = User.query.filter_by(email=email, id=id).all()

        if not users:
            return jsonify({'code': 0, 'msg': "没有找到匹配的用户"})

        if len(users) == 1:
            user = users[0]
            response = {
                'code': 0,
                'msg': "分页查询成功",
                'data': {
                    'id': user.id,
                    'email': user.email,
                    "description": user.description,
                    'submission_time': user.submission_time
                }
            }
            return jsonify(response)
        else:
            user_list = []
            for user in users:
                user_data = {
                    'id': user.id,
                    'email': user.email,
                    "description": user.description,
                    'submission_time': user.submission_time
                }
                user_list.append(user_data)

            response = {
                'code': 0,
                'msg': "分页查询成功",
                'data': {
                    'total': len(users),
                    'datas': user_list
                }
            }
            return jsonify(response)

api.add_resource(submitAPI,"/submit")
api.add_resource(pageAPI,"/page")


import torch
from torch.utils.data import Dataset
import numpy as np
from sklearn.decomposition import PCA
import random
from sklearn.preprocessing import scale

class HSIDataset(Dataset):
    def __init__(self, hsi, gt, patch_size=1):
        '''
        :param hsi: [h, w, bands]
        :param gt: [h, w]
        :param patch_size: scale
        '''
        super(HSIDataset, self).__init__()
        self.hsi = self.add_mirror(hsi, patch_size)  # [h, w, bands]
        self.gt = gt  # [h, w]
        self.patch_size = patch_size
        # 标签数据的索引
        self.indices = tuple(zip(*np.nonzero(gt)))

    # 添加镜像
    @staticmethod
    def add_mirror(data, patch_size):
        dx = patch_size // 2
        if dx != 0:
            h, w, c = data.shape
            mirror = np.zeros((h + 2 * dx, w + 2 * dx, c))
            mirror[dx:-dx, dx:-dx, :] = data
            for i in range(dx):
                # 填充左上部分镜像
                mirror[:, i, :] = mirror[:, 2 * dx - i, :]
                mirror[i, :, :] = mirror[2 * dx - i, :, :]
                # 填充右下部分镜像
                mirror[:, -i - 1, :] = mirror[:, -(2 * dx - i) - 1, :]
                mirror[-i - 1, :, :] = mirror[-(2 * dx - i) - 1, :, :]
        else:
            mirror = data
        return mirror

    def __len__(self):
        return len(self.indices)

    def __getitem__(self, index):
        '''
        :param index:
        :return: 元素光谱信息， 元素的空间信息， 标签
        '''
        x, y = self.indices[index]
        # 领域: [patchsz, patchsz, bands]
        neighbor_region = self.hsi[x: x + self.patch_size, y: y + self.patch_size, :]
        # 类别
        target = self.gt[x, y] - 1
        return torch.tensor(neighbor_region, dtype=torch.float), torch.tensor(target, dtype=torch.long)

# random
# class HSIDatasetPair_(HSIDataset):
#     def __getitem__(self, index):
#         import time
#         begin_time = time.time()
#         x, y = self.indices[index]
#         target = self.gt[x, y]
#         end_time = time.time()
#         print('Finish Time 1: {}'.format(end_time - begin_time))
#         # 正样本
#         begin_time = time.time()
#         pos_indices = tuple(zip(*np.nonzero(self.gt == target)))
#         (x_pos, y_pos) = random.sample(pos_indices, 1)[0]
#         # x_pos, y_pos = pos_indices[random.randint(0, len(pos_indices) - 1)]
#         # 负样本
#         neg_indices = tuple(zip(*np.nonzero(self.gt != target)))
#         (x_neg, y_neg) = random.sample(neg_indices, 1)[0]
#         end_time = time.time()
#         print('Finish Time 2:{}'.format(end_time - begin_time))
#         # x_neg, y_neg = neg_indices[random.randint(0, len(neg_indices) - 1)]
#         # 领域
#         begin_time = time.time()
#         neighbor_region = self.hsi[x: x + self.patch_size, y: y + self.patch_size, :]
#         neighbor_region_pos = self.hsi[x_pos: x_pos + self.patch_size, y_pos: y_pos + self.patch_size, :]
#         neighbor_region_neg = self.hsi[x_neg: x_neg + self.patch_size, y_neg: y_neg + self.patch_size, :]
#         end_time = time.time()
#         print('Finish Time 3: {}'.format(end_time - begin_time))
#         begin_time = time.time()
#         pos_sample = np.stack([neighbor_region, neighbor_region_pos], axis=0)
#         neg_sample = np.stack([neighbor_region, neighbor_region_neg], axis=0)
#
#         sample = np.stack([pos_sample, neg_sample], axis=0)
#         end_time = time.time()
#         print('Finish Time 4: {}'.format(end_time - begin_time))
#         return torch.tensor(sample, dtype=torch.float), torch.tensor([True, False], dtype=torch.long)
#         # return torch.from_numpy(sample), torch.tensor([True, False], dtype=torch.long)

class HSIDatasetPair(HSIDataset):
    def __init__(self, hsi, gt, patch_size=1):
        super().__init__(hsi, gt, patch_size)
        nc = self.gt.max()
        non_zero = gt != 0
        self.pos_pool = [tuple(zip(*np.nonzero(gt == i))) for i in range(1, nc + 1)]
        self.neg_pool = [tuple(zip(*np.nonzero((gt != i) & non_zero))) for i in range(1, nc + 1)]
    def __getitem__(self, index):
        # import time
        # begin_time = time.time()
        x, y = self.indices[index]
        target = self.gt[x, y]
        # end_time = time.time()
        # print('Finish Time 1: {}'.format(end_time - begin_time))
        # # 正样本
        # begin_time = time.time()
        # pos_indices = tuple(zip(*np.nonzero(self.gt == target)))
        # (x_pos, y_pos) = random.sample(pos_indices, 1)[0]
        x_pos, y_pos = random.sample(self.pos_pool[target - 1], 1)[0]
        # x_pos, y_pos = pos_indices[random.randint(0, len(pos_indices) - 1)]
        # 负样本
        # neg_indices = tuple(zip(*np.nonzero(self.gt != target)))
        # (x_neg, y_neg) = random.sample(neg_indices, 1)[0]
        x_neg, y_neg = random.sample(self.neg_pool[target - 1], 1)[0]
        # end_time = time.time()
        # print('Finish Time 2: {}'.format(end_time - begin_time))
        # x_neg, y_neg = neg_indices[random.randint(0, len(neg_indices) - 1)]
        # 领域
        # begin_time = time.time()
        neighbor_region = self.hsi[x: x + self.patch_size, y: y + self.patch_size, :]
        neighbor_region_pos = self.hsi[x_pos: x_pos + self.patch_size, y_pos: y_pos + self.patch_size, :]
        neighbor_region_neg = self.hsi[x_neg: x_neg + self.patch_size, y_neg: y_neg + self.patch_size, :]
        # end_time = time.time()
        # print('Finish Time 3: {}'.format(end_time - begin_time))
        #
        # begin_time = time.time()
        pos_sample = np.stack([neighbor_region, neighbor_region_pos], axis=0)
        neg_sample = np.stack([neighbor_region, neighbor_region_neg], axis=0)

        sample = np.stack([pos_sample, neg_sample], axis=0)
        # end_time = time.time()
        # print('Finish Time 4: {}'.format(end_time - begin_time))
        # print(self.gt[x, y], self.gt[x_pos, y_pos], self.gt[x_neg, y_neg])
        return torch.tensor(sample, dtype=torch.float), torch.tensor([True, False], dtype=torch.long)

class HSIDatasetPairNaive(HSIDataset):
    def __init__(self, hsi, gt, patch_size=1):
        super().__init__(hsi, gt, patch_size)
        # nc = self.gt.max()
        # non_zero = gt != 0
        # self.pos_pool = [tuple(zip(*np.nonzero(gt == i))) for i in range(1, nc + 1)]
        # self.neg_pool = [tuple(zip(*np.nonzero((gt != i) & non_zero))) for i in range(1, nc + 1)]
        self.pair_indices = []
        n = len(self.indices)
        for i in range(n):
            for j in range(i+1, n):
                self.pair_indices.append((i, j))

    def __len__(self):
        return len(self.pair_indices)

    def __getitem__(self, index):
        index1, index2 = self.pair_indices[index]
        x1, y1 = self.indices[index1]
        x2, y2 = self.indices[index2]
        target1, target2 = self.gt[x1, y1], self.gt[x2, y2]
        # x, y = self.indices[index]
        # target = self.gt[x, y]
        # x_pos, y_pos = random.sample(self.pos_pool[target - 1], 1)[0]
        # x_neg, y_neg = random.sample(self.neg_pool[target - 1], 1)[0]

        # 领域
        neighbor_region_1 = self.hsi[x1: x1 + self.patch_size, y1: y1 + self.patch_size, :]
        neighbor_region_2 = self.hsi[x2: x2 + self.patch_size, y2: y2 + self.patch_size, :]
        sample_pair = np.stack([neighbor_region_1, neighbor_region_2], axis=0)

        return torch.tensor(sample_pair, dtype=torch.float), torch.tensor(target1 == target2, dtype=torch.long)

class DatasetInfo(object):
    info = {'PaviaU': {
        'data_key': 'paviaU',
        'label_key': 'paviaU_gt',
        'hidden_size': 17
    },
        'Salinas': {
            'data_key': 'salinas_corrected',
            'label_key': 'salinas_gt',
            'hidden_size': 12
    },  'KSC': {
            'data_key': 'KSC',
            'label_key': 'KSC_gt',
            'hidden_size': 14
    },  'Houston':{
            'data_key': 'Houston',
            'label_key': 'Houston2018_gt'
    },  'Indian':{
            'data_key': 'indian_pines_corrected',
            'label_key': 'indian_pines_gt',
            'hidden_size': 12
    },  'Pavia':{
            'data_key': 'pavia',
            'label_key': 'pavia_gt'
    },  'gf5':{
            'data_key': 'gf5',
            'label_key': 'gf5_gt'
        }}

# class Data_prefetcher():
#     def __init__(self, loader: torch.utils.data.DataLoader, device: torch.device):
#         self.loader = iter(loader)
#         self.stream = torch.cuda.Stream(device)
#         # 预加载
#         self.preload()
#
#     def preload(self):
#         try:
#             self.next_input, self.next_target = next(self.loader)
#         except StopIteration:
#             self.next_input = None
#             self.next_target = None
#             return
#         with torch.cuda.stream(self.stream):
#             self.next_input = self.next_input.cuda(non_blocking=True)
#             self.next_target = self.next_target.cuda(non_blocking=True)
#             # # With Amp, it isn't necessary to manually convert data to half.
#             # # if args.fp16:
#             # #     self.next_input = self.next_input.half()
#             # # else:
#             # self.next_input = self.next_input.float()
#             # self.next_input = self.next_input.sub_(self.mean).div_(self.std)
#
#     def next(self):
#         torch.cuda.current_stream().wait_stream(self.stream)
#         input = self.next_input
#         target = self.next_target
#         self.preload()
#         return input, target

# def batch_sampler(dataset, batch_size, shuffle):
#     size = len(dataset)
#     indices = list(range(size))
#     import random
#     if shuffle:
#         random.shuffle(indices)
#     import math
#     batches = int(math.ceil(size/batch_size))
#     for i in range(batches):
#         begin = i * batch_size
#         end = (i + 1) * batch_size
#         data, gt = [], []
#         for j in indices[begin: end]:
#             tmp_data, tmp_gt = dataset[j]
#             data.append(tmp_data)
#             gt.append(tmp_gt)
#         yield torch.stack(data), torch.stack(gt)

# from scipy.io import loadmat
# import numpy as np
# m = loadmat('data/PaviaU/PaviaU.mat')
# data = m['paviaU']
# m = loadmat('data/PaviaU/PaviaU_gt.mat')
# label = m['paviaU_gt']
# data, label = data.astype(np.float32), label.astype(np.long)
# # label += 1
# dataset = HSIDataset(data, label, patch_size=21)
# from torch.utils.data import DataLoader
# loader = DataLoader(dataset, batch_size=128, num_workers=10, shuffle=True)
# import time
# times = []
# iteration = 0
# begin_time = time.time()
# for x, target in loader:
#     end_time = time.time()
#     print('Finish Time {}: {}'.format(iteration, end_time - begin_time))
#     times.append(end_time - begin_time)
#     iteration += 1
#     begin_time = time.time()
# print(np.mean(times))

# times = []
# loader = batch_sampler(dataset, 128, True)
# begin_time = time.time()
# iteration = 0
# for x, target in loader:
#     end_time = time.time()
#     # print(x.shape, target.shape)
#     print('Finish Time {}: {}'.format(iteration, end_time - begin_time))
#     times.append(end_time - begin_time)
#     iteration += 1
#     begin_time = time.time()
# print(np.mean(times))

# for i in range(len(dataset)):
#     begin_time = time.time()
#     x, target = dataset[0]
#     end_time = time.time()
#     print('Finish Time: {}'.format(end_time - begin_time))
# w = data.shape[1]
# index = 150
# l, c = index // w, index % w
# spectra = data[l, c]
# neighbor_region, target = dataset[index]
# print(torch.equal(torch.from_numpy(spectra), neighbor_region[21 // 2, 21 // 2]))
# print(target)
# print(label[l, c])

# from scipy.io import loadmat
# m = loadmat('data/PaviaU/PaviaU.mat')
# data = m['paviaU']
# m = loadmat('data/PaviaU/PaviaU_gt.mat')
# gt = m['paviaU_gt']
# import numpy as np
# data, gt = data.astype(np.float), gt.astype(np.int32)
# dataset = HSIDatasetPair(data, gt, 5)
# from torch.utils.data import DataLoader
# loader = DataLoader(dataset, batch_size=128, num_workers=4, shuffle=True, pin_memory=True)
# import time
# times = []
# iteration = 0
# begin_time = time.time()
# for x, target in loader:
#     end_time = time.time()
#     print('Finish Time {}: {}'.format(iteration, end_time - begin_time))
#     times.append(end_time - begin_time)
#     iteration += 1
#     begin_time = time.time()
#     print(x.shape, target.shape)
#     exit(0)
# print(np.mean(times))
# times = []
# iteration = 0
# loader = batch_sampler(dataset, batch_size=128, shuffle=True)
# begin_time = time.time()
# for x, target in loader:
#     end_time = time.time()
#     print('Finish Time {}: {}'.format(iteration, end_time - begin_time))
#     times.append(end_time - begin_time)
#     iteration += 1
#     begin_time = time.time()
# print(np.mean(times))
# import time
# for i in range(len(dataset)):
#     begin_time = time.time()
#     x = dataset[i]
#     end_time = time.time()
#     print('Finish Time: {}'.format(end_time - begin_time))
#     break
# exit(0)
# x, target = dataset[100]
# print('Finish Time: {}'.format(end_time - begin_time))
# print(x.shape, target)
#
# from scipy.io import loadmat
# m = loadmat('data/PaviaU/PaviaU.mat')
# data = m['paviaU']
# m = loadmat('data/PaviaU/PaviaU_gt.mat')
# gt = m['paviaU_gt']
# import numpy as np
# data, gt = data.astype(np.float), gt.astype(np.int32)
# dataset = HSIDatasetPairMultiClass_(data, gt, 7)
# x, target = dataset[0]
# print(x.shape, target)

# data = np.random.rand(9).reshape((3,3,1))
# gt = np.arange(9).reshape((3,3))
# dataset = HSIDatasetPairNaive(data, gt, patch_size=1)
# print(len(dataset))
# print(data)
# x, y = dataset[0]
# print(x, y)


import apache_beam as beam
import sys

def my_grep(line, term):
   if line.startswith(term):
      yield line

if __name__ == '__main__':
   p = beam.Pipeline(argv=sys.argv)
   input = '../javahelp/src/main/java/com/google/cloud/training/dataanalyst/javahelp/*.java'
   output_prefix = '/tmp/output'
   searchTerm = 'import'

   # find all lines that contain the searchTerm
   (p
      | 'GetJava' >> beam.io.ReadFromText(input)
      | 'Grep' >> beam.FlatMap(lambda line: my_grep(line, searchTerm) )
      | 'write' >> beam.io.WriteToText(output_prefix)
   )

   p.run().wait_until_finish()


import discord
import os
import json
import datetime
import random
from discord.ext import commands
from core.classes import cog_ext

options = ("大吉", "吉", "小吉", "平", "小凶", "凶", "大凶", "你命由你不由天，自己幸福自己拼")

class luck (cog_ext):
  @commands.command ()
  async def 今日運勢 (self, ctx):
    f = open ("./datas/luck.json", "r", encoding = "utf-8")
    last_time = json.load (f)
    f.close ()

    f = open ("./datas/luck.json", "w", encoding = "utf-8")
    if (str (ctx.author.id) not in last_time):
      today_luck = random.choice (options)
      await ctx.reply (today_luck)
      last_time[str (ctx.author.id)] = [str (ctx.message.created_at.date ()), today_luck]

      json.dump (last_time, f)
      f.close ()
      return

    delta = ctx.message.created_at.date () - datetime.date.fromisoformat (last_time[str (ctx.author.id)][0])

    if (delta.days == 0):
      today_luck = last_time[str (ctx.author.id)][1]
      await ctx.reply (f"你是要問幾次啦，就跟你說今天是 {today_luck} 了")
    else:
      today_luck = random.choice (options)
      await ctx.reply (today_luck)
      last_time[str (ctx.author.id)] = [str (ctx.message.created_at.date ()), today_luck]

    # print (datetime.date.today () - datetime.date.fromisoformat (last_time[ctx.author.id]))
    json.dump (last_time, f)
    f.close ()

def setup (bot):
  bot.add_cog (luck (bot))


"""
This playbook prepares a Risk Notable for investigation. First, it ensures that a "Risk Notable" links back to the original notable event with a card pinned to the HUD. It then posts a link to this container in the comment field of Enterprise Security. Finally, it updates the container name, description, and severity to reflect the data in the Notable artifact.\t
"""

import phantom.rules as phantom
import json
from datetime import datetime, timedelta

def on_start(container):
    phantom.debug('on_start() called')

    # call 'event_id_filter' block
    event_id_filter(container=container)

    return

def event_id_filter(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
    phantom.debug("event_id_filter() called")

    ################################################################################
    # Only proceed if the event_id value is present. The event_id is also sometimes
    # called a Notable ID.
    ################################################################################

    # collect filtered artifact ids and results for 'if' condition 1
    matched_artifacts_1, matched_results_1 = phantom.condition(
        container=container,
        conditions=[
            ["artifact:*.cef.event_id", "!=", ""]
        ],
        name="event_id_filter:condition_1",
        scope="all")

    # call connected blocks if filtered artifacts or results
    if matched_artifacts_1 or matched_results_1:
        mark_evidence_artifact(action=action, success=success, container=container, results=results, handle=handle, filtered_artifacts=matched_artifacts_1, filtered_results=matched_results_1)

    # collect filtered artifact ids and results for 'if' condition 2
    matched_artifacts_2, matched_results_2 = phantom.condition(
        container=container,
        logical_operator="and",
        conditions=[
            ["artifact:*.cef.event_id", "!=", ""],
            ["artifact:*.name", "==", "Field Values"]
        ],
        name="event_id_filter:condition_2",
        scope="all")

    # call connected blocks if filtered artifacts or results
    if matched_artifacts_2 or matched_results_2:
        artifact_update_notable(action=action, success=success, container=container, results=results, handle=handle, filtered_artifacts=matched_artifacts_2, filtered_results=matched_results_2)

    return

def artifact_update_notable(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
    phantom.debug("artifact_update_notable() called")

    filtered_artifact_0_data_event_id_filter = phantom.collect2(container=container, datapath=["filtered-data:event_id_filter:condition_2:artifact:*.id"], scope="all")

    parameters = []

    # build parameters list for 'artifact_update_notable' call
    for filtered_artifact_0_item_event_id_filter in filtered_artifact_0_data_event_id_filter:
        parameters.append({
            "name": "Notable Artifact",
            "tags": None,
            "label": None,
            "severity": None,
            "cef_field": None,
            "cef_value": None,
            "input_json": None,
            "artifact_id": filtered_artifact_0_item_event_id_filter[0],
            "cef_data_type": None,
        })

    ################################################################################
    ## Custom Code Start
    ################################################################################

    # Write your custom code here...

    ################################################################################
    ## Custom Code End
    ################################################################################

    phantom.custom_function(custom_function="community/artifact_update", parameters=parameters, name="artifact_update_notable")

    return

def mark_evidence_artifact(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
    phantom.debug("mark_evidence_artifact() called")

    id_value = container.get("id", None)
    filtered_artifact_0_data_event_id_filter = phantom.collect2(container=container, datapath=["filtered-data:event_id_filter:condition_1:artifact:*.id"], scope="all")

    parameters = []

    # build parameters list for 'mark_evidence_artifact' call
    for filtered_artifact_0_item_event_id_filter in filtered_artifact_0_data_event_id_filter:
        parameters.append({
            "container": id_value,
            "content_type": "artifact_id",
            "input_object": filtered_artifact_0_item_event_id_filter[0],
        })

    ################################################################################
    ## Custom Code Start
    ################################################################################

    # Write your custom code here...

    ################################################################################
    ## Custom Code End
    ################################################################################

    phantom.custom_function(custom_function="community/mark_evidence", parameters=parameters, name="mark_evidence_artifact", callback=asset_get_splunk)

    return

def asset_get_splunk(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
    phantom.debug("asset_get_splunk() called")

    parameters = []

    parameters.append({
        "asset": "splunk",
    })

    ################################################################################
    ## Custom Code Start
    ################################################################################

    # Write your custom code here...

    ################################################################################
    ## Custom Code End
    ################################################################################

    phantom.custom_function(custom_function="community/asset_get_attributes", parameters=parameters, name="asset_get_splunk", callback=asset_get_splunk_callback)

    return

def asset_get_splunk_callback(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
    phantom.debug("asset_get_splunk_callback() called")

    format_es_url(action=action, success=success, container=container, results=results, handle=handle, filtered_artifacts=filtered_artifacts, filtered_results=filtered_results)
    format_es_note(action=action, success=success, container=container, results=results, handle=handle, filtered_artifacts=filtered_artifacts, filtered_results=filtered_results)
    format_event_name(action=action, success=success, container=container, results=results, handle=handle, filtered_artifacts=filtered_artifacts, filtered_results=filtered_results)

    return

def format_es_url(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
    phantom.debug("format_es_url() called")

    ################################################################################
    # Format a URL for the link back to the Notable ID. Change the port number as
    # needed.
    ################################################################################

    template = """https://{0}/en-US/app/SplunkEnterpriseSecuritySuite/incident_review?earliest={1}&latest=now&search=event_id%3D{2}"""

    # parameter list for template variable replacement
    parameters = [
        "asset_get_splunk:custom_function_result.data.configuration.device",
        "filtered-data:event_id_filter:condition_1:artifact:*.cef.info_min_time",
        "filtered-data:event_id_filter:condition_1:artifact:*.cef.event_id"
    ]

    ################################################################################
    ## Custom Code Start
    ################################################################################

    # Write your custom code here...

    ################################################################################
    ## Custom Code End
    ################################################################################

    phantom.format(container=container, template=template, parameters=parameters, name="format_es_url", scope="all")

    pin_es_url(container=container)

    return

def pin_es_url(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
    phantom.debug("pin_es_url() called")

    ################################################################################
    # Pin the Enterprise Security URL
    ################################################################################

    format_es_url = phantom.get_format_data(name="format_es_url")

    ################################################################################
    ## Custom Code Start
    ################################################################################

    # Write your custom code here...

    ################################################################################
    ## Custom Code End
    ################################################################################

    phantom.pin(container=container, data=format_es_url, message="Enterprise Security URL", name="es_url", pin_style="grey", pin_type="card")

    container = phantom.get_container(container.get('id', None))

    return

def format_es_note(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
    phantom.debug("format_es_note() called")

    ################################################################################
    # Format a note with the current event information.
    ################################################################################

    template = """SOAR event created: {0}\nComplete details can be found here: {1}/summary/evidence"""

    # parameter list for template variable replacement
    parameters = [
        "container:id",
        "container:url"
    ]

    ################################################################################
    ## Custom Code Start
    ################################################################################

    # Write your custom code here...

    ################################################################################
    ## Custom Code End
    ################################################################################

    phantom.format(container=container, template=template, parameters=parameters, name="format_es_note", scope="all")

    update_notable(container=container)

    return

def update_notable(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
    phantom.debug("update_notable() called")

    # phantom.debug('Action: {0} {1}'.format(action['name'], ('SUCCEEDED' if success else 'FAILED')))

    ################################################################################
    # Update the notable event  in Enterprise Security with a link back to this container
    ################################################################################

    filtered_artifact_0_data_event_id_filter = phantom.collect2(container=container, datapath=["filtered-data:event_id_filter:condition_1:artifact:*.cef.event_id"], scope="all")
    format_es_note = phantom.get_format_data(name="format_es_note")

    parameters = []

    # build parameters list for 'update_notable' call
    for filtered_artifact_0_item_event_id_filter in filtered_artifact_0_data_event_id_filter:
        if filtered_artifact_0_item_event_id_filter[0] is not None:
            parameters.append({
                "status": "in progress",
                "comment": format_es_note,
                "event_ids": filtered_artifact_0_item_event_id_filter[0],
            })

    ################################################################################
    ## Custom Code Start
    ################################################################################

    # Write your custom code here...

    ################################################################################
    ## Custom Code End
    ################################################################################

    phantom.act("update event", parameters=parameters, name="update_notable", assets=["splunk"])

    return

def format_event_name(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
    phantom.debug("format_event_name() called")

    ################################################################################
    # Format the event name as 'Source: Risk Object'
    ################################################################################

    template = """{0}: {1}"""

    # parameter list for template variable replacement
    parameters = [
        "filtered-data:event_id_filter:condition_1:artifact:*.cef.source",
        "filtered-data:event_id_filter:condition_1:artifact:*.cef.risk_object"
    ]

    ################################################################################
    ## Custom Code Start
    ################################################################################

    # Write your custom code here...

    ################################################################################
    ## Custom Code End
    ################################################################################

    phantom.format(container=container, template=template, parameters=parameters, name="format_event_name", scope="all")

    container_update_info(container=container)

    return

def container_update_info(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
    phantom.debug("container_update_info() called")

    id_value = container.get("id", None)
    filtered_artifact_0_data_event_id_filter = phantom.collect2(container=container, datapath=["filtered-data:event_id_filter:condition_1:artifact:*.cef.urgency","filtered-data:event_id_filter:condition_1:artifact:*.cef.source"], scope="all")
    format_event_name = phantom.get_format_data(name="format_event_name")

    parameters = []

    # build parameters list for 'container_update_info' call
    for filtered_artifact_0_item_event_id_filter in filtered_artifact_0_data_event_id_filter:
        parameters.append({
            "name": format_event_name,
            "tags": None,
            "label": None,
            "owner": None,
            "status": None,
            "severity": filtered_artifact_0_item_event_id_filter[0],
            "input_json": None,
            "description": filtered_artifact_0_item_event_id_filter[1],
            "sensitivity": None,
            "container_input": id_value,
        })

    ################################################################################
    ## Custom Code Start
    ################################################################################

    # Write your custom code here...

    ################################################################################
    ## Custom Code End
    ################################################################################

    phantom.custom_function(custom_function="community/container_update", parameters=parameters, name="container_update_info", callback=artifact_update_severity)

    return

def artifact_update_severity(action=None, success=None, container=None, results=None, handle=None, filtered_artifacts=None, filtered_results=None, custom_function=None, **kwargs):
    phantom.debug("artifact_update_severity() called")

    filtered_artifact_0_data_event_id_filter = phantom.collect2(container=container, datapath=["filtered-data:event_id_filter:condition_1:artifact:*.cef.urgency","filtered-data:event_id_filter:condition_1:artifact:*.id"], scope="all")

    parameters = []

    # build parameters list for 'artifact_update_severity' call
    for filtered_artifact_0_item_event_id_filter in filtered_artifact_0_data_event_id_filter:
        parameters.append({
            "name": None,
            "tags": None,
            "label": None,
            "severity": filtered_artifact_0_item_event_id_filter[0],
            "cef_field": None,
            "cef_value": None,
            "input_json": None,
            "artifact_id": filtered_artifact_0_item_event_id_filter[1],
            "cef_data_type": None,
        })

    ################################################################################
    ## Custom Code Start
    ################################################################################

    # Write your custom code here...

    ################################################################################
    ## Custom Code End
    ################################################################################

    phantom.custom_function(custom_function="community/artifact_update", parameters=parameters, name="artifact_update_severity")

    return

def on_finish(container, summary):
    phantom.debug("on_finish() called")

    ################################################################################
    ## Custom Code Start
    ################################################################################

    # This function is called after all actions are completed.
    # summary of all the action and/or all details of actions
    # can be collected here.

    # summary_json = phantom.get_summary()
    # if 'result' in summary_json:
        # for action_result in summary_json['result']:
            # if 'action_run_id' in action_result:
                # action_results = phantom.get_action_results(action_run_id=action_result['action_run_id'], result_data=False, flatten=False)
                # phantom.debug(action_results)

    ################################################################################
    ## Custom Code End
    ################################################################################

    return


from typing import Any, List

from app import crud, schemas
from app.api import deps
from fastapi import APIRouter, Depends, Security
from app import models
from app.constants.role import Role
from sqlalchemy.orm import Session

router = APIRouter(prefix="/permission", tags=["permissions"])

@router.get("/", response_model=List[schemas.Permission])
def get_permissions(
    db: Session = Depends(deps.get_db),
    current_user: models.User = Security(
        deps.get_current_active_user,
        scopes=[
            Role.ADMIN["name"],
            Role.ASSISTANT["name"],
            Role.SUPER_ADMIN["name"],
            Role.USER["name"]
        ],
    ),
) -> Any:
    """
    Retrieve all available user permissions.
    """
    permissions = crud.user_permission.get_by_user_id(
        db, user_id=current_user.id)
    return permissions


# Program : Find the reverse of a number.
# Input : number = 12345
# Output : 54321
# Explanation : The number is reversed.
# Language : Python3

# O(n) time | O(1) space
def reverse_a_number(number):

    # Intialize the reverse as zero.
    reverse = 0

    # Do till the number is greater than zero.
    while number > 0:
        # Find the last digit.
        last_digit = number % 10
        # Add the last digit to the reverse.
        reverse = (reverse*10) + last_digit
        # Divide the number by 10 to find the next last digit.
        number = number // 10

    # Return the reverse.
    return reverse

# Main function.
if __name__ == '__main__':

    # Declare the number.
    number = 12345

    # Find the reverse of the number and store the result in the answer variable.
    answer = reverse_a_number(number)

    # Print the answer.
    print(answer)


"""
demo on naive gaussian bayes  vs  multivariate gaussian bayes
"""

import numpy as np
import matplotlib.pyplot as plt

def make_data(m=2000):
    Σ1 = [[2,1], [1,1]]
    Σ2 = [[2,-1],[-1,1]]

    X1 = np.random.multivariate_normal(mean=[0]*len(Σ1), cov=Σ1, size=m//2)
    X2 = np.random.multivariate_normal(mean=[0]*len(Σ2), cov=Σ2, size=m//2)
    y = [0]*len(X1) + [1]*len(X2)
    X = np.concatenate([X1,X2], axis=0)
    return(X,y)

#==========================================================================

X,y = make_data()

#naive gaussian bayes
from sklearn.naive_bayes import GaussianNB
md = GaussianNB().fit(X,y)
accuracy = md.score(X,y)
print("naive gaussian bayes =", accuracy.round(2))

#multivariate gaussian bayes model
mask = np.equal(y, 1)
Σ1 = np.cov(X[~mask].T, ddof=0)
μ1 = np.mean(X[~mask], axis=0)

Σ2 = np.cov(X[mask].T, ddof=0)
μ2 = np.mean(X[mask], axis=0)

from scipy.stats import multivariate_normal

d1 = multivariate_normal(mean=μ1, cov=Σ1)
d2 = multivariate_normal(mean=μ2, cov=Σ2)

priors = {c:y.count(c) for c in sorted(set(y))}
# priors are not necessary because we intentionally have a perfectly balanced data-set

P = [[d1.pdf(x)*priors[y], d2.pdf(x)*priors[y]] for x,y in zip(X,y)]
ypred = [pp.index(max(pp)) for pp in P]
accuracy = sum(y1==y2 for y1,y2 in zip(y,ypred)) / len(ypred)
print("multivariate gaussian bayes acuracy =", round(accuracy,2))

#vizualize
plt.figure(figsize=(12,5))
plt.subplot(121)
plt.scatter(*X.T, c=y, marker='.', cmap=plt.cm.RdBu)
plt.axis('equal')

plt.subplot(122)
plt.scatter(*X.T, c=ypred, marker='.', cmap=plt.cm.RdBu)


'''
Created on 07.06.2020
@author: Abdul
plotting tool for microplane models
'''

from math import \
    cos, sin, pi

from numpy import \
    array, zeros, trace, \
    einsum, zeros_like,\
    identity, linspace, hstack,\
    sqrt, ones

from traits.api import \
    Constant, \
    HasTraits, \
    Property, cached_property

import matplotlib.pyplot as plt

import numpy as np

class MATS2DMicroplane(HasTraits):

    #-----------------------------------------------
    # number of microplanes
    #-----------------------------------------------
    n_mp = Constant(360)

    #-----------------------------------------------
    # get the normal vectors of the microplanes
    #-----------------------------------------------
    _MPN = Property(depends_on='n_mp')

    @cached_property
    def _get__MPN(self):
        # microplane normals:

        alpha_list = linspace(0, 2 * pi, self.n_mp)

        MPN = array([[cos(alpha), sin(alpha)] for alpha in alpha_list])

        return MPN

    #-------------------------------------
    # get the weights of the microplanes
    #-------------------------------------
    _MPW = Property(depends_on='n_mp')

    @cached_property
    def _get__MPW(self):
        # Note that the values in the array must be multiplied by 6 (cf. [Baz05])!
        # The sum of of the array equals 0.5. (cf. [BazLuz04]))
        # The values are given for an Gaussian integration over the unit
        # hemisphere.
        MPW = ones(self.n_mp) / self.n_mp * 2

        return MPW

    #-------------------------------------------------------------------------
    # Cached elasticity tensors
    #-------------------------------------------------------------------------

    elasticity_tensors = Property(
        depends_on='E, nu, dimensionality, stress_state')

    @cached_property
    def _get_elasticity_tensors(self):
        '''
        Intialize the fourth order elasticity tensor for 2D or 2D plane strain or 2D plane stress
        '''
        # ----------------------------------------------------------------------------
        # Lame constants calculated from E and nu
        # ----------------------------------------------------------------------------

        # first Lame paramter
        la = self.E * self.nu / ((1 + self.nu) * (1 - 2 * self.nu))
        # second Lame parameter (shear modulus)
        mu = self.E / (2 + 2 * self.nu)

        # -----------------------------------------------------------------------------------------------------
        # Get the fourth order elasticity and compliance tensors for the 2D-case
        # -----------------------------------------------------------------------------------------------------

        # construct the elasticity tensor (using Numpy - einsum function)
        delta = identity(2)
        D_ijkl = (einsum(',ij,kl->ijkl', la, delta, delta) +
                  einsum(',ik,jl->ijkl', mu, delta, delta) +
                  einsum(',il,jk->ijkl', mu, delta, delta))

        return D_ijkl

    #-------------------------------------------------------------------------
    # Setup for computation within a supplied spatial context
    #-------------------------------------------------------------------------
    D4_e = Property

    def _get_D4_e(self):
        # Return the elasticity tensor
        return self.elasticity_tensors

    #-------------------------------------------------------------------------
    '''Kinematic constraint N-T split '''
    #-------------------------------------------------------------------------

    # get the dyadic product of the microplane normals
    _MPNN = Property(depends_on='n_mp')

    @cached_property
    def _get__MPNN(self):
        # dyadic product of the microplane normals

        MPNN_nij = einsum('ni,nj->nij', self._MPN, self._MPN)
        return MPNN_nij

    # get the third order tangential tensor (operator) for each microplane
    _MPTT = Property(depends_on='n_mp')

    @cached_property
    def _get__MPTT(self):
        # Third order tangential tensor for each microplane
        delta = identity(2)
        MPTT_nijr = 0.5 * (einsum('ni,jr -> nijr', self._MPN, delta) +
                           einsum('nj,ir -> njir', self._MPN, delta) - 2 *
                           einsum('ni,nj,nr -> nijr', self._MPN, self._MPN, self._MPN))
        return MPTT_nijr

    def _get_e_N_arr(self, eps_eng):
        return einsum('nij,ij->n', self._MPNN, eps_eng)

    def _get_e_T_vct_arr(self, eps_eng):
        MPTT_ijr = self._get__MPTT()
        return einsum('nijr,ij->nr', MPTT_ijr, eps_eng)

    #-------------------------------------------------------------------------
    '''Kinematic constraint V-D-T split '''
    #-------------------------------------------------------------------------

    # get the dyadic product of the microplane normals
    _MPVV_1 = Property(depends_on='n_mp')

    @cached_property
    def _get__MPVV_1(self):
        # dyadic product of the microplane normals
        I_ij = identity(2)
        MPVV_ij = 1/3 * I_ij
        return MPVV_ij

    # get the dyadic product of the microplane normals
    _MPDD_1 = Property(depends_on='n_mp')

    @cached_property
    def _get__MPDD_1(self):
        # dyadic product of the microplane normals
        I_ij = identity(2)
        MPVV_ij = 1/3 * I_ij

        MPNN_nij = einsum('ni,nj->nij', self._MPN, self._MPN)

        MPDD_ij = einsum('nij->nij', MPNN_nij - MPVV_ij)

        return MPDD_ij

    # get the third order tangential tensor (operator) for each microplane
    _MPTT_1 = Property(depends_on='n_mp')

    @cached_property
    def _get__MPTT_1(self):
        # Third order tangential tensor for each microplane
        delta = identity(2)
        MPTT_nijr = 0.5 * (einsum('ni,jr -> nijr', self._MPN, delta) +
                           einsum('nj,ir -> njir', self._MPN, delta) - 2 *
                           einsum('ni,nj,nr -> nijr', self._MPN, self._MPN, self._MPN))
        return MPTT_nijr

    def _get_e_V1_arr(self, eps_eng):
        return einsum('ij,ij->', self._MPVV_1, eps_eng)

    def _get_e_D1_arr(self, eps_eng):
        return einsum('nij,ij->n', self._MPDD_1, eps_eng)

    def _get_e_T1_vct_arr(self, eps_eng):
        MPTT_ijr = self._get__MPTT_1()
        return einsum('nijr,ij->nr', MPTT_ijr, eps_eng)

    #-------------------------------------------------------------------------
    '''Kinematic constraint V-D split '''
    #-------------------------------------------------------------------------

    # get the dyadic product of the microplane normals
    _MPVV = Property(depends_on='n_mp')

    @cached_property
    def _get__MPVV(self):
        # dyadic product of the microplane normals
        I_ij = identity(2)
        MPVV_ij = 1/3 * I_ij
        return MPVV_ij

    # get the third order tangential tensor (operator) for each microplane
    _MPDD = Property(depends_on='n_mp')

    @cached_property
    def _get__MPDD(self):
        # Third order tangential tensor for each microplane
        delta = identity(2)
        MPDD_nijr = 0.5 * (einsum('ni,jr -> nijr', self._MPN, delta) +
                           einsum('nj,ir -> njir', self._MPN, delta)) -\
                             1/3. * einsum('nk,ki,jr -> nijr', self._MPN, delta, delta)
        return MPDD_nijr

    def _get_e_V_arr(self, eps_eng):
        return einsum('ij,ij->', self._MPVV, eps_eng)

    def _get_e_D_vct_arr(self, eps_eng):
        MPDD_ijr = self._get__MPDD()
        return einsum('nijr,ij->nr', MPDD_ijr, eps_eng)

if __name__ == '__main__':
    #==========================================================================
    # Check the model behavior at the single material point
    #==========================================================================

    model = MATS2DMicroplane()

    n_mp = model.n_mp

    p = 1.0  # ratio of strain eps_11 (for bi-axial loading)
    m = -0.2  # ratio of strain eps_22 (for bi-axial loading)

    # monotonic loading - comp
    n = 25  # number of increments
    s_levels = linspace(0, 0.001, 2)
    #s_levels[0] = 0
    eps_max = 0.001

    s_levels.reshape(-1, 2)[:, 0] = eps_max
    s_levels[0] = 0
    #s_levels.reshape(-1, 2)[:, 1] = eps_min
    s_history_1 = s_levels.flatten()
    s_arr_1 = hstack([linspace(s_history_1[i], s_history_1[i + 1], n)
                      for i in range(len(s_levels) - 1)])

    eps_1 = array([array([[0 * s_arr_1[i],  p* s_arr_1[i]],
                          [ p* s_arr_1[i] , 0 * s_arr_1[i]]]) for i in range(0, len(s_arr_1))])

    idx = np.where(eps_1[:, 0, 0] == eps_max)
    print(idx[0])

    #--------------------------------------
    # construct the arrays
    #--------------------------------------

    sigma_kk_1 = zeros(len(s_arr_1) + 1)

    eps_N_1 = zeros((len(eps_1[:, 0]), n_mp))
    eps_V_1 = zeros((len(eps_1[:, 0]), n_mp))
    eps_D_1 = zeros((len(eps_1[:, 0]), n_mp))

    eps_TT_1 = zeros((len(eps_1[:, 0]), n_mp, 2))
    eps_DD_1 = zeros((len(eps_1[:, 0]), n_mp, 2))

    for i in range(0, len(eps_1[:, 0])):

        eps_N_1[i, :] = model._get_e_N_arr(eps_1[i, :])
        eps_TT_1[i, :, :] = model._get_e_T_vct_arr(eps_1[i, :])

        eps_V_1[i, :] = model._get_e_V1_arr(eps_1[i, :])
        eps_D_1[i, :] = model._get_e_D1_arr(eps_1[i, :])

        eps_V_1[i, :] = model._get_e_V_arr(eps_1[i, :])
        eps_DD_1[i, :, :] = model._get_e_D_vct_arr(eps_1[i, :])

#     '''====================================================
#     plotting
#     ===================================================='''

    rads = np.arange(0, (2 * np.pi), (2 * np.pi) / n_mp)

    '=================================================================='
    'Plotting in increments'
    '=================================================================='

    #===================
    # Normal strain
    #===================
    plt.subplot(111)

    for i in range(0, len(eps_1[:, 0])):
        plt.polar(rads, eps_N_1[i, :])
    plt.title(' Normal strain for all microplanes')
    plt.show()

    #===================
    # Tangential  strain
    #===================
    plt.subplot(111)
    for i in range(0, len(eps_1[:, 0])):

        norm = sqrt(
            einsum('...i,...i->... ', eps_TT_1[i, :], eps_TT_1[i, :]))

        plt.polar(rads, norm)
    plt.title('Tangential strain for all microplanes')
    plt.show()

    #===================
    # Volumetric strain
    #===================
    plt.subplot(111)

    for i in range(0, len(eps_1[:, 0])):
        plt.polar(rads, eps_V_1[i, :])
    plt.title(' Volumetric strain for all microplanes')
    plt.show()

    #===================
    # Deviatoric strain (V-D-T)
    #===================
    plt.subplot(111)

    for i in range(0, len(eps_1[:, 0])):
        plt.polar(rads, eps_D_1[i, :])
    plt.title(' Deviatoric (V-D-T) strain for all microplanes')
    plt.show()

    #===================
    # Deviatoric  strain
    #===================
    plt.subplot(111)
    for i in range(0, len(eps_1[:, 0])):

        norm = sqrt(
            einsum('...i,...i->... ', eps_DD_1[i, :], eps_DD_1[i, :]))

        plt.polar(rads, norm)
    plt.title('Deviatoric (V-D) strain for all microplanes')
    plt.show()

    plt.show()


import os
import ftplib

d = {1: "experimental",
     2: "developers",
     3: "go-developers",
     4: "optional",
     5: "developers-optional",
     6: "go-developers-optional",
     7: "rc-optional",
     8: "testing",
     9: "go-testing",
     10: "rc",
     11: "critical"}

def create_direcrory():
    for key, val in d.items():
        print(key, ".", val)
    try:
        user_input = int(input("Choose your option: (example 1)>"))
        return d[user_input]
    except Exception as e:
        print("Error", e.__doc__)
        exit(1)

def create_files_directory(user_input):
    for file in os.listdir(path="."):
        if file.endswith(".deb"):
            f = open(os.path.join(file) + "." + user_input, "tw", encoding='utf-8')
            f.close()

def search_file():
    for filename in os.listdir(path="."):
        if filename.endswith(".deb") or (filename.split('.')[-2]) == 'deb':
            yield filename

def open_directory(filename):
    ftp = ftplib.FTP('ftp', 'login', 'pass')
    ftp.cwd('/repo-channel')
    f = open(filename, "rb")
    ftp.storbinary('STOR ' + filename, f)
    f.close()
    ftp.retrlines('NLST')
    ftp.quit()
    # os.remove(filename)

create_files_directory(create_direcrory())
search_file()
mygenerator = search_file()
for filename in mygenerator:
    open_directory(filename)


from encoder.params_model import model_embedding_size as speaker_embedding_size
from utils.argutils import print_args
from synthesizer.inference import Synthesizer
from encoder import inference as encoder
from vocoder import inference as vocoder
from pathlib import Path
import sounddevice as sd
import numpy as np
import librosa
import argparse
import torch
import sys

def load_model(in_fpath, parser):

	parser.add_argument("-e", "--enc_model_fpath", type=Path,
		        default="encoder/saved_models/pretrained.pt",
		        help="Path to a saved encoder")
	parser.add_argument("-s", "--syn_model_dir", type=Path,
		        default="synthesizer/saved_models/logs-pretrained/",
		        help="Directory containing the synthesizer model")
	parser.add_argument("-v", "--voc_model_fpath", type=Path,
		        default="vocoder/saved_models/pretrained/pretrained.pt",
		        help="Path to a saved vocoder")
	parser.add_argument("--low_mem", action="store_true", help=\
	"If True, the memory used by the synthesizer will be freed after each use. Adds large "
	"overhead but allows to save some GPU memory for lower-end GPUs.")
	parser.add_argument("--no_sound", action="store_true", help=\
	"If True, audio won't be played.")
	args = parser.parse_args()
	encoder.load_model(args.enc_model_fpath)
	synthesizer = Synthesizer(args.syn_model_dir.joinpath("taco_pretrained"), low_mem=args.low_mem)
	vocoder.load_model(args.voc_model_fpath)

	preprocessed_wav = encoder.preprocess_wav(in_fpath)
	original_wav, sampling_rate = librosa.load(in_fpath)
	preprocessed_wav = encoder.preprocess_wav(original_wav, sampling_rate)
	embed = encoder.embed_utterance(preprocessed_wav)

	return synthesizer, sampling_rate, embed

def generate_wav(text, num_generated, synthesizer, sampling_rate, embed, debug = False):
	texts = [text]
	embeds = [embed]
	specs = synthesizer.synthesize_spectrograms(texts, embeds)
	spec = specs[0]
	generated_wav = vocoder.infer_waveform(spec, True, False)
	generated_wav = np.pad(generated_wav, (0, synthesizer.sample_rate), mode="constant")
	print("zeros=", np.count_nonzero(generated_wav==0), "\n")
	fpath = "output_%02d.wav" % num_generated
	librosa.output.write_wav(fpath, generated_wav.astype(np.float32),
			     synthesizer.sample_rate)
	if debug:
		sd.stop()
		sd.play(generated_wav, synthesizer.sample_rate)


class Solution:
    """
    @param A: an array
    @return: total of reverse pairs
    """

    def reversePairs(self, A):
        # write your code here
        self.copy = [0] * len(A)
        return self.inverse_pairs_core(A, 0, len(A) - 1)

    def inverse_pairs_core(self, data, l, r):
        if l >= r:
            return 0

        middle = (l + r) >> 1
        result = self.inverse_pairs_core(data, l, middle) + self.inverse_pairs_core(data, middle + 1, r)
        i, j, k = l, middle + 1, l

        while i <= middle and j <= r:
            if data[i] > data[j]:
                self.copy[k] = data[j]
                j += 1
                result += middle - i + 1
            else:
                self.copy[k] = data[i]
                i += 1
            k += 1

        while i <= middle:
            self.copy[k] = data[i]
            i += 1
            k += 1

        while j <= r:
            self.copy[k] = data[j]
            j += 1
            k += 1
        for i in range(l, r + 1):
            data[i] = self.copy[i]

        return result


import argparse
import asyncio
from datetime import datetime
from itertools import repeat

import aiohttp

stats = []

async def fetch(session, url):
    now = datetime.now()
    try:
        async with session.get(url) as response:
            return await response.text()
    finally:
        stats.append(datetime.now() - now)

async def main():
    parser = argparse.ArgumentParser(description="Simple http benchmark")
    parser.add_argument("url")
    parser.add_argument(
        "--number", "-n", type=int, default=100, help="Number of requests"
    )
    parser.add_argument("--concurrency", "-c", type=int, default=10, help="Concurrency")

    args = parser.parse_args()

    pending = set()
    fetch_iter = repeat(lambda: fetch(session, args.url), args.number)

    start = datetime.now()
    async with aiohttp.ClientSession() as session:
        while True:
            for f, _ in zip(fetch_iter, range(args.concurrency - len(pending))):
                pending.add(f())

            if not pending:
                break
            done, pending = await asyncio.wait(
                pending, return_when=asyncio.FIRST_COMPLETED
            )
    diff = (datetime.now() - start) / args.number
    reqps = 1 / diff.total_seconds()
    print(f"Requests per second: {reqps}")

    # for s in sorted(stats):
    # print(s)

if __name__ == "__main__":
    asyncio.run(main())


#!/usr/bin/python3

import requests
import BeautifulSoup

url = 'http://158.69.76.135/level2.php'

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0"}

r = requests.get(url, headers = headers)
soup = BeautifulSoup(r.content, features="lxml")
results = soup.find('span', {'id': 'Results'}).text
print(results)


import aima.logic
import aima.utils

def get_clauses():
    clauses = list()

    clause = input("New Clause: ")

    while clause != "":
        clauses.append(aima.utils.expr(clause))
        clause = input("New Clause: ")

    print()
    return clauses

def tell_kb(kb):
    tell = input("\nTell: ")

    while tell != "":
        kb.tell(aima.utils.expr(tell))
        tell = input("Tell: ")
    print()

def ask_kb(kb):
    ask = input("\nAsk: ")

    while ask != "":
        print(list(aima.logic.fol_fc_ask(kb, aima.utils.expr(ask))))
        ask = input("Ask: ")
    print()

def main():

    clauses = get_clauses()

    kb = aima.logic.FolKB(clauses)

    menu = "0. Exit.\n1. Tell Knowledge Base.\n2. Ask Knowledge Base.\n"

    entry = input(menu)
    while entry != '0':
        if entry == '1':
            tell_kb(kb)
        elif entry == '2':
            ask_kb(kb)
        else:
            print()
        entry = input(menu)

if __name__ == "__main__":

    main()


import pandas as pd                     # pandas is a data frame library
import matplotlib.pyplot as plt         # matplotlib.pyplot plots data
import numpy as np                      # numpy provides N-dim object support
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import Imputer
from sklearn.naive_bayes import GaussianNB
from sklearn import metrics
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import LogisticRegressionCV

df = pd.read_csv("./data/pima-data.csv")
# print(df.shape)
# print(df.head(5))
#
# print(df.tail(5))
#
# print(df.isnull().values.any())

corr = df.corr()
# fig, ax = plt.subplots(figsize=(11, 11))
# ax.matshow(corr)
# plt.xticks(range(len(corr.columns)), corr.columns)
# plt.yticks(range(len(corr.columns)), corr.columns)

# print(df.corr())
# print("column deleted")
del df['skin']
# print(df.corr())

diabetes_map = {True : 1, False : 0}
df['diabetes'] = df['diabetes'].map(diabetes_map)
# print(df.head())

# Spliting data
# 70% for traning, 30% for testing

feature_col_name = ['num_preg', 'glucose_conc', 'diastolic_bp', 'thickness', 'insulin', 'bmi', 'diab_pred', 'age']
predicted_class_name = ['diabetes']

x = df[feature_col_name].values
y = df[predicted_class_name].values
split_test_size = 0.30

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=split_test_size, random_state=45) # test_size = 0.30 is 30%, 42 is the answer to everything

print("{0:2.2f}% in traning set".format((len(x_train)/len(df.index))*100))
print("{0:2.2f}% in test set".format((len(x_test)/len(df.index))*100))

num_true = len(df.loc[df['diabetes'] == True])
num_false = len(df.loc[df['diabetes'] == False])
print("Original true cases: {0} ({1:2.2f}%)".format(num_true, ((num_true*100) / len(df.index))))
print("Original false cases: {0} ({1:2.2f}%)".format(num_false, ((num_false*100) / len(df.index))))

num_true = len(y_train[[y_train[:] == 1]])
num_false = len(y_train[[y_train[:] == 0]])
print("Training true cases: {0} ({1:2.2f}%)".format(num_true, ((num_true*100) / len(y_train))))
print("Training false cases: {0} ({1:2.2f}%)".format(num_false, ((num_false*100) / len(y_train))))

num_true = len(y_test[[y_test[:] == 1]])
num_false = len(y_test[[y_test[:] == 0]])
print("Test true cases: {0} ({1:2.2f}%)".format(num_true, ((num_true*100) / len(y_test))))
print("Test false cases: {0} ({1:2.2f}%)".format(num_false, ((num_false*100) / len(y_test))))

# Hidden missing vaues

# print(df.head())
#
# print("")
# print("# of rows in dataframe {0}".format(len(df)))
# print("# of missing values in num_preg {0}".format(len(df.loc[df['num_preg'] == 0])))
# print("# of missing values in glucose_conc {0}".format(len(df.loc[df['glucose_conc'] == 0])))
# print("# of missing values in diastolic_bp {0}".format(len(df.loc[df['diastolic_bp'] == 0])))
# print("# of missing values in thickness {0}".format(len(df.loc[df['thickness'] == 0])))
# print("# of missing values in insulin {0}".format(len(df.loc[df['insulin'] == 0])))
# print("# of missing values in bmi {0}".format(len(df.loc[df['bmi'] == 0])))
# print("# of missing values in diab_pred {0}".format(len(df.loc[df['diab_pred'] == 0])))
# print("# of missing values in age {0}".format(len(df.loc[df['age'] == 0])))

# Impute with mean all 0 readings
fill_0 = Imputer(missing_values=0, strategy="mean", axis=0)
x_train = fill_0.fit_transform(x_train)
x_test = fill_0.fit_transform(x_test)

# Training initial Algorithm - Naive Bayes

# Create Gaussian Naive Bayes model object and train it with the data
# nb_model = GaussianNB()
# nb_model.fit(x_train, y_train.ravel())
# # Performance on training data
# nb_predict_train = nb_model.predict(x_train)
# # Accuracy
# print("Accuracy train data: {0:.4f}".format(metrics.accuracy_score(y_train, nb_predict_train)))
# # Performance on testing data
# nb_predict_test = nb_model.predict(x_test)
# # Accuracy
# print("Accuracy test data: {0:.4f}".format(metrics.accuracy_score(y_test, nb_predict_test)))
#
# # Metrics
#
# print("")
# print("Confusion Metrics")
# print("{0}".format(metrics.confusion_matrix(y_test, nb_predict_test)))
#
# print("")
# print("Classification Metrics")
# print(metrics.classification_report(y_test, nb_predict_test))
#
#
# # Random Forest Algorithm
# rf_model = RandomForestClassifier(random_state=42)
# rf_model.fit(x_train, y_train.ravel())
# rf_predict_train = rf_model.predict(x_train)
# print("Accuracy  RandomForestClassifier train data {0:.4f}".format(metrics.accuracy_score(y_train, rf_predict_train)))
# rf_predict_test = rf_model.predict(x_test)
#
#
# print("")
# print("Confusion Metrics")
# print("{0}".format(metrics.confusion_matrix(y_test, rf_predict_test)))
# print("")
# print("Classification Metrics")
# print(metrics.classification_report(y_test, rf_predict_test))
#
#
# # Logistic Regression
#
# lr_model = LogisticRegression(C=0.7, random_state=42)
# lr_model.fit(x_train, y_train.ravel())
# lr_predict_test = lr_model.predict(x_test)
# # Training metrics
# print("Accuracy  Logistic Regression test data {0:.4f}".format(metrics.accuracy_score(y_test, lr_predict_test)))
# print("")
# print("Confusion Metrics")
# print("{0}".format(metrics.confusion_matrix(y_test, lr_predict_test)))
# print("")
# print("Classification Metrics")
# print(metrics.classification_report(y_test, lr_predict_test))
#
# C_start = 0.1
# C_end = 0.5
# C_inc = 0.1
#
# C_values, recall_scores = [], []
#
# C_val = C_start
# best_recall_score = 0
# while (C_val < C_end):
#     C_values.append(C_val)
#     lr_model_loop = LogisticRegression(C=C_val, random_state=42)
#     lr_model_loop.fit(x_train, y_train.ravel())
#     lr_predict_loop_test = lr_model_loop.predict(x_test)
#     recall_score = metrics.recall_score(y_test, lr_predict_loop_test)
#     recall_scores.append(recall_score)
#     if (recall_score > best_recall_score):
#         best_recall_score = recall_score
#         best_lr_predict_test = lr_predict_loop_test
#
#     C_val = C_val + C_inc
#
# best_recall_score_C_val = C_values[recall_scores.index(best_recall_score)]
# print("1st max value of {0:.3f} occured at C={1:.3f}".format(best_recall_score, best_recall_score_C_val))
#
#
# # Logistic Regression with class_weight parameter
#
# C_start = 0.1
# C_end = 0.5
# C_inc = 0.1
#
# C_values, recall_scores = [], []
#
# C_val = C_start
# best_recall_score = 0
# while (C_val < C_end):
#     C_values.append(C_val)
#     lr_model_loop = LogisticRegression(C=C_val, class_weight="balanced", random_state=42)
#     lr_model_loop.fit(x_train, y_train.ravel())
#     lr_predict_loop_test = lr_model_loop.predict(x_test)
#     recall_score = metrics.recall_score(y_test, lr_predict_loop_test)
#     recall_scores.append(recall_score)
#     if (recall_score > best_recall_score):
#         best_recall_score = recall_score
#         best_lr_predict_test = lr_predict_loop_test
#
#     C_val = C_val + C_inc
#
# best_recall_score_C_val = C_values[recall_scores.index(best_recall_score)]
# print("with class weight -- 1st max value of {0:.3f} occured at C={1:.3f}".format(best_recall_score, best_recall_score_C_val))
#
#
# lr_model = LogisticRegression(C=best_recall_score_C_val, class_weight="balanced", random_state=42)
# lr_model.fit(x_train, y_train.ravel())
# lr_predict_test = lr_model.predict(x_test)
# # Training metrics
# print("Accuracy  Logistic Regression test data {0:.4f}".format(metrics.accuracy_score(y_test, lr_predict_test)))
# print("")
# print("Confusion Metrics")
# print("{0}".format(metrics.confusion_matrix(y_test, lr_predict_test)))
# print("")
# print("Classification Metrics")
# print(metrics.classification_report(y_test, lr_predict_test))

# LogisticRegressionCV Algo
def lrcvmodel():
    lr_cv_model = LogisticRegressionCV(n_jobs=-1, random_state=42, Cs=3, cv=10, refit=False, class_weight="balanced")
    lr_cv_model.fit(x_train, y_train.ravel())
    lr_cv_predict_test = lr_cv_model.predict(x_test)
    # Training metrics
    print("Accuracy  Logistic RegressionCV test data {0:.4f}".format(metrics.accuracy_score(y_test, lr_cv_predict_test)))
    print("")
    print("Confusion Metrics")
    print("{0}".format(metrics.confusion_matrix(y_test, lr_cv_predict_test)))
    print("")
    print("Classification Metrics")
    print(metrics.classification_report(y_test, lr_cv_predict_test))

if __name__ == '__main__':
    lrcvmodel()