#execute.py

account = '3ddba2981c42cecdfee9e845e5615ffc721f1daa'

import curl_API as cl
import json

begin = 5 # 截止块，截止后的区块链都是严格按照格式生成的可检索的块
username = 'lover_test' # 公用区块链的私有用户

def init_acccount():
  resultjs = cl.invoke('addUser',
              '''["lover_test","20"]''',
              account)
  print( resultjs )

def add_relationship(user_id1, user_id2, info):
  resultjs = cl.invoke('addAsset',
              '''["{}&#@{}&#@{}","test","to record","ZIG","1","lover_test"]'''.format(user_id1,user_id2,info),
              account)
  print( resultjs )

def del_relationship(user_id1, user_id2, info):
  resultjs = cl.invoke('deleteAsset',
              '''["{}&#@{}&#@{}"]'''.format(user_id1,user_id2,info),
              account)
  print( resultjs )

from datetime import datetime
from datetime import timedelta

def search_relationship(user_id):
  d = []
  global begin
  resultjs = cl.blocks()
  result = json.loads(resultjs)

  for block in result:

    if block["id"]<begin:
      continue

    transactions = block["transactions"][0]
    if transactions["args"][-1]!=username:
      continue
    elif transactions["args"][0] == "addAsset":
      branch = 'add'
    elif transactions["args"][0] == "deleteAsset":
      branch = 'del'
    else:
      continue

    when =  transactions["timestamp"]
    # "2018-05-12T15:37:14.000Z"
    when = ( datetime.strptime(when,"%Y-%m-%dT%H:%M:%S.000Z") + timedelta(hours = 8) ).strftime("%Y.%m.%d %H:%M")
    # "2018.5.12 15:37"
    try:
      who1,who2,why = transactions["args"][1].split('&#@')
    except IndexError:
      print(block)
      raise(IndexError)
    if who1 == user_id:
      who = who2
    elif who2 == user_id:
      who = who1
    else:
      continue

    d.append( {"id":block["id"],"when":when,"who":who,"why":why,"type":branch} )

  d = sorted(d,key = lambda x:x["id"],reverse = True)
  answer = {"datas":d}
  print(json.dumps(answer, ensure_ascii=False ,indent=2))


import csv
import os

import numpy as np
import torch
from pytorch_lightning import LightningDataModule
from torch.utils.data import Dataset, DataLoader, Subset
from torch.utils.data.dataset import random_split
from tqdm import tqdm

class DensityContrastiveDataset(Dataset):

    def __init__(self, dataset_dir, seed=42, subset=None):
        self.dataset_dir = dataset_dir
        self.seed = seed
        self.subset = int(subset * 5) if subset is not None else None

        raw_data, self.density_file, self.scale_data = self.__loadfile()

        torch.manual_seed(seed)
        np.random.seed(seed)
        indices = torch.randperm(self.scale_data.shape[0])

        self.density_file = [self.density_file[index] for index in indices]
        self.scale_data = self.scale_data[indices]

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

    def __getitem__(self, index):
        f1, f2 = self.density_file[index]
        d1 = torch.load(os.path.join(self.dataset_dir, f1))
        d2 = torch.load(os.path.join(self.dataset_dir, f2))
        scale = self.scale_data[index]
        mol = '_'.join(f1.split('_')[:2]) + '.xyz'

        _, xidx, yidx, zidx = torch.where(d2 > 0.001)
        xmin, xmax = xidx.aminmax()
        ymin, ymax = yidx.aminmax()
        zmin, zmax = zidx.aminmax()

        xshift = torch.randint(0 - xmin, 65 - xmax, (1,))
        yshift = torch.randint(0 - ymin, 65 - ymax, (1,))
        zshift = torch.randint(0 - zmin, 65 - zmax, (1,))

        d2 = d2.roll((xshift, yshift, zshift), (1, 2, 3))

        return d1, d2, scale, mol

    def __loadfile(self):
        mol_energy_file = os.path.join(self.dataset_dir, 'mol_energy_nwchem.csv')
        assert os.path.exists(mol_energy_file), f'mol_energy file does not exist!'

        n_data = 0
        raw_data = {}
        with open(mol_energy_file, 'r') as f:
            reader = csv.reader(f)
            for mol_scale, energy in reader:

                if self.subset is not None and n_data == self.subset:
                    break

                _, mol_id, scale = mol_scale.split('_')
                if mol_id not in raw_data.keys():
                    raw_data[mol_id] = {scale: energy}
                else:
                    raw_data[mol_id][scale] = energy

                n_data += 1

        choices = np.random.choice(['1|3', '1|2', '1', '2', '3'], size=len(raw_data), replace=True)

        density_file, scale_data = [], []

        for mol_id, scale in tqdm(zip(raw_data.keys(), choices), total=len(choices)):
            if scale == '1|3':
                scale_data.append(1/3)
            elif scale == '1|2':
                scale_data.append(0.5)
            else:
                scale_data.append(float(scale))

            density_file.append(['dsgdb9nsd_{}_1.pth'.format(mol_id), 'dsgdb9nsd_{}_{}.pth'.format(mol_id, scale)])

        scale_data = torch.tensor(scale_data, dtype=torch.float)
        return raw_data, density_file, scale_data

class ContrastiveDataModule(LightningDataModule):
    def __init__(
            self,
            dataset_dir,
            subset=None,
            train_ratio=0.8,
            seed=42,
            batch_size=32,
            shuffle=True,
            num_workers=0,
            pin_memory=True,
            drop_last=False,
            *args,
            **kwargs
    ):
        super().__init__(*args, **kwargs)

        self.dataset_dir = dataset_dir
        self.train_ratio = train_ratio
        self.seed = seed
        self.batch_size = batch_size
        self.shuffle = shuffle
        self.num_workers = num_workers
        self.pin_memory = pin_memory
        self.drop_last = drop_last
        self.subset = subset
        self.dataset = DensityContrastiveDataset(dataset_dir, seed, subset=subset)
        self._num_samples = int(self.train_ratio * len(self.dataset))

        n_data = len(self.dataset)
        train_split = int(n_data * train_ratio)
        dataset_train, dataset_val = random_split(
            self.dataset,
            [train_split, len(self.dataset) - train_split],
            generator=torch.Generator().manual_seed(seed)
        )

        self._train_dataloader = DataLoader(
            dataset_train,
            batch_size=batch_size,
            shuffle=shuffle,
            num_workers=num_workers,
            drop_last=drop_last,
            pin_memory=pin_memory,
            generator=torch.Generator().manual_seed(seed)
        )

        self._val_dataloader = DataLoader(
            dataset_val,
            batch_size=batch_size,
            shuffle=False,
            num_workers=num_workers,
            drop_last=drop_last,
            pin_memory=pin_memory
        )

    @property
    def num_samples(self):
        return self._num_samples

    def train_dataloader(self):
        return self._train_dataloader

    def val_dataloader(self):
        return self._val_dataloader

class DensityDataset(Dataset):

    def __init__(self, dataset_dir, seed=42, subset=None, scale=None):
        self.dataset_dir = dataset_dir
        self.seed = seed
        self.scale = scale
        self.subset = subset

        self.mol_files, self.scale_data, self.target = self.__loadfile()

        torch.manual_seed(seed)
        indices = torch.randperm(self.target.shape[0])

        self.mol_files = [self.mol_files[index] for index in indices]
        self.target = self.target[indices]
        self.scale_data = self.scale_data[indices]

        if scale is None:
            self.scale_indices = torch.stack(
                [(self.scale_data == s).nonzero().flatten() for s in [1 / 3, 1 / 2, 1, 2, 3]])

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

    def __getitem__(self, index):
        mol_file = self.mol_files[index]
        density_data = torch.load(os.path.join(self.dataset_dir, '{}.pth'.format(mol_file)))
        return density_data, self.target[index]

    def __loadfile(self):
        mol_energy_file = os.path.join(self.dataset_dir, 'mol_energy_nwchem.csv')
        assert os.path.exists(mol_energy_file), f'mol_energy file does not exist!'

        n_data = 0
        mol_scales, energies, scale_data = [], [], []
        with open(mol_energy_file, 'r') as f:
            reader = csv.reader(f)
            for mol_scale, energy in reader:
                if self.subset is not None and n_data >= self.subset:
                    break
                scale = mol_scale.split('_')[-1]

                if self.scale is not None:
                    if scale != self.scale:
                        continue

                if scale == '1|3':
                    scale_data.append(1 / 3)
                elif scale == '1|2':
                    scale_data.append(0.5)
                else:
                    scale_data.append(float(scale))

                mol_scales.append(mol_scale)
                energies.append(float(energy))

                n_data += 1

        energies = torch.tensor(energies, dtype=torch.float).unsqueeze(-1)
        scale_data = torch.tensor(scale_data, dtype=torch.float)
        return mol_scales, scale_data, energies

class MainDataModule(LightningDataModule):
    def __init__(
            self,
            dataset_dir,
            subset=None,
            scale='1',
            train_ratio=0.8,
            test_ratio=0.1,
            seed=42,
            batch_size=32,
            num_workers=0,
            pin_memory=True,
            drop_last=False,
            *args,
            **kwargs
    ):
        super().__init__(*args, **kwargs)

        self.dataset_dir = dataset_dir
        self.train_ratio = train_ratio
        self.test_ratio = test_ratio
        self.seed = seed
        self.batch_size = batch_size
        self.num_workers = num_workers
        self.pin_memory = pin_memory
        self.drop_last = drop_last
        self.subset = subset
        self.scale = scale
        self.dataset = DensityDataset(dataset_dir, seed, subset=subset, scale=scale)

        n_data = len(self.dataset)
        train_split = int(n_data * (1 - test_ratio) * train_ratio)
        test_split = int(n_data * test_ratio)
        dataset_train, dataset_val, dataset_test = random_split(
            self.dataset,
            [train_split, len(self.dataset) - train_split - test_split, test_split],
            generator=torch.Generator().manual_seed(seed)
        )

        test_scale_indices = [[i for i in dataset_test.indices if i in scale_index] for scale_index in
                              self.dataset.scale_indices]
        self._num_samples = len(dataset_train)

        train_kwargs = {
            'batch_size': batch_size,
            'shuffle': True,
            'num_workers': num_workers,
            'drop_last': drop_last,
            'pin_memory': pin_memory,
            'generator': torch.Generator().manual_seed(seed)
        }

        val_kwargs = {
            'batch_size': batch_size,
            'shuffle': False,
            'num_workers': num_workers,
            'drop_last': drop_last,
            'pin_memory': pin_memory,
        }

        self._train_dataloader = DataLoader(dataset_train, **train_kwargs)
        self._val_dataloader = DataLoader(dataset_val, **val_kwargs)

        dataset_tests = [Subset(self.dataset, test_scale_index) for test_scale_index in test_scale_indices]
        self._test_dataloaders = [DataLoader(d, **val_kwargs) for d in dataset_tests]

    @property
    def num_samples(self):
        return self._num_samples

    def train_dataloader(self):
        return self._train_dataloader

    def val_dataloader(self):
        return self._val_dataloader

    def test_dataloader(self):
        return self._test_dataloaders

class MainScale1DataModule(LightningDataModule):
    def __init__(
            self,
            dataset_dir,
            subset=None,
            train_ratio=0.8,
            seed=42,
            batch_size=32,
            num_workers=0,
            pin_memory=True,
            drop_last=False,
            *args,
            **kwargs
    ):
        super().__init__(*args, **kwargs)

        self.dataset_dir = dataset_dir
        self.train_ratio = train_ratio
        self.seed = seed
        self.batch_size = batch_size
        self.num_workers = num_workers
        self.pin_memory = pin_memory
        self.drop_last = drop_last
        self.subset = subset
        self.dataset = DensityDataset(dataset_dir, seed, subset=subset, scale='1')

        n_data = len(self.dataset)
        train_split = int(n_data * train_ratio)
        dataset_train, dataset_val = random_split(
            self.dataset,
            [train_split, len(self.dataset) - train_split],
            generator=torch.Generator().manual_seed(seed)
        )

        self._num_samples = len(dataset_train)

        train_kwargs = {
            'batch_size': batch_size,
            'shuffle': True,
            'num_workers': num_workers,
            'drop_last': drop_last,
            'pin_memory': pin_memory,
            'generator': torch.Generator().manual_seed(seed)
        }

        val_kwargs = {
            'batch_size': batch_size,
            'shuffle': False,
            'num_workers': num_workers,
            'drop_last': drop_last,
            'pin_memory': pin_memory,
        }

        self._train_dataloader = DataLoader(dataset_train, **train_kwargs)
        self._val_dataloader = DataLoader(dataset_val, **val_kwargs)

    @property
    def num_samples(self):
        return self._num_samples

    def train_dataloader(self):
        return self._train_dataloader

    def val_dataloader(self):
        return self._val_dataloader


'''
Advent of Code 2019, Day 14
NanoReactor - Make FUEL from ORE
'''
import math as m
import re
from collections import namedtuple
from pprint import pprint

Element = namedtuple('Element', 'name qty')
RX = namedtuple('RX', 'out comps')

def get_data(filein):
    d_rxs = {}
    with open(filein, 'r') as f:
        data = f.readlines()
    # Massage the data into Reactions, Components
    # Assumes Rx outputs are a single element & only one Rx type for an element
    # Reactions = {out: comps}
    # comps = {self: -Qty, **Elem:Qty}
    for line in data:
        # sp = re.split(r'\s*=>\s*', line.rstrip())
        comps = re.findall(r'(\d+)\s([A-Z]+)', line)  # Grouped: (Qty,Elem)
        dcomps = {el: q for (q,el) in comps}
        qty, elem = comps[-1]
        dcomps.update({elem: f'-{qty}'})
        tcomps = tuple(Element(el, int(q)) for (el, q) in dcomps.items())
        d_rxs.update({elem: tcomps})
    pprint(d_rxs)
    t_rxs = tuple(RX(rx, comps) for (rx, comps) in d_rxs.items())
    return t_rxs

def climb_rx_ladder(RXs, Top="ORE", Bottom='FUEL', Need=1):
    Store = {Bottom: Need}
    rxs = dict(RXs)
    order = 0
    stop_Flag = False
    while stop_Flag is False and order < 100:   # Continue cycle until Store is all TOP of ladder
        stop_Flag = True
        for elem in list(Store.keys()):
            if Store[elem] <= 0 or elem == Top:
                continue
            else:
                stop_Flag = False
                rx = dict(rxs[elem])  #Expand the tuple of comps to a dict
                rx_cycles = -m.ceil(Store[elem] // rx[elem])  # NeedToMake//MakePerRx (ignore extra)
                #print(rx, rx_cycles)
                add_to_store(Store, rx, rx_cycles)
        order += 1
        pprint(f'RxOrder:{order}, Store:{Store}')
    print(f'RxOrder:{order}, {Top}:{Store[Top]}')
    return Store

def add_to_store(d1, d2, rep=1):
  return d1.update(
    {elem: (d2[elem]*rep + d1.get(elem, 0)) for elem,qty in d2.items()})

''' Driving Function '''
def main():
    # filein = r"X:\Projects\Python\AdventOfCode\AOC2019_14.json"
    # filein = r"C:\Users\csmol\Google Drive\Engineering\Python\Python Challenge\AOC2019_14_Puzzle.txt"
    filein = r"AOC2019_14_Puzzle.txt"
    RXs = get_data(filein)
    pprint(dict(RXs))

    ORE_Avail = 10**12
    FUEL_unit = 1
    RXoutputs = climb_rx_ladder(RXs, Top='ORE', Bottom='FUEL', Need=FUEL_unit)
    ORE_need_for_one = RXoutputs['ORE']
    print(f'ORE needed for 1 FUEL: {ORE_need_for_one}')
    FUEL_made = ORE_Avail // ORE_need_for_one
    while True:
        RXoutputs = climb_rx_ladder(RXs, Top='ORE', Bottom='FUEL', Need=FUEL_made)
        ORE_needed = RXoutputs['ORE']
        ORE_leftover = ORE_Avail - ORE_needed
        FUEL_inc = ORE_leftover // ORE_need_for_one
        print(f'FUEL inc: {FUEL_inc}, ExtraORE: {ORE_leftover}')
        if FUEL_inc < 1:
            break
        else:
            FUEL_made += FUEL_inc
    print(f'Final FUEL: {FUEL_made}, ExtraORE: {ORE_leftover}')
    #logging.debug(f'{rxmap}')

if __name__ == "__main__":
    main()

# Part A Puzzle Input, 1 FUEL = x ORE
# Part B

#Unused functions
def Find_Dict_Max_Value(dict1):
  return max(dict1.items(), key=lambda x: x[1])

def add_dict_values(dbase, dadd):
  '''Merges two dicts, summing values of common elements. Returns full dict'''
  temp = {elem: qty + dbase.get(elem, 0) for elem, qty in dadd.items()}
  dbase.update(temp)
  return dbase

def rxOrder(Rxs, store):
  rx_net = {}
  for elem in store.keys():
    if elem == 'ORE':
      continue
    else:
      rx_rep = -m.ceil(store[elem] / Rxs[elem][elem])
      rx_run = {e:q*rx_rep for e,q in Rxs[elem].items()}
      add_dict_values(rx_net, rx_run)
  print(rx_net)
  add_dict_values(store, rx_net)
  return store


from PySide2.QtCore import (QCoreApplication, QMetaObject, QObject, QPoint,
    QRect, QSize, QUrl, Qt)
from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QFont,
    QFontDatabase, QIcon, QLinearGradient, QPalette, QPainter, QPixmap,
    QRadialGradient)
from PySide2.QtWidgets import *

class Ui_Form(object):
    def setupUi(self, Form):
        if Form.objectName():
            Form.setObjectName(u"Form")
        Form.resize(1000, 494)
        self.logoutpic = QLabel(Form)
        self.logoutpic.setObjectName(u"logoutpic")
        self.logoutpic.setGeometry(QRect(30, 410, 41, 41))
        self.logoutpic.setPixmap(QPixmap(u"menuPage/images/logoutIcon.png"))
        self.pagestatus = QLabel(Form)
        self.pagestatus.setObjectName(u"pagestatus")
        self.pagestatus.setGeometry(QRect(460, 10, 351, 71))
        self.sharedrivebutton = QPushButton(Form)
        self.sharedrivebutton.setObjectName(u"sharedrivebutton")
        self.sharedrivebutton.setGeometry(QRect(70, 357, 121, 31))
        self.sharedrivebutton.setStyleSheet(u"background-color: rgb(255, 255, 255);")
        self.sharedrivepic = QLabel(Form)
        self.sharedrivepic.setObjectName(u"sharedrivepic")
        self.sharedrivepic.setGeometry(QRect(30, 350, 41, 41))
        self.sharedrivepic.setPixmap(QPixmap(u"menuPage/images/shareIcon.png"))
        self.mydrivebutton = QPushButton(Form)
        self.mydrivebutton.setObjectName(u"mydrivebutton")
        self.mydrivebutton.setGeometry(QRect(72, 297, 121, 31))
        self.mydrivebutton.setStyleSheet(u"background-color: rgb(255, 255, 255);")
        self.logoutbutton = QPushButton(Form)
        self.logoutbutton.setObjectName(u"logoutbutton")
        self.logoutbutton.setGeometry(QRect(70, 417, 121, 31))
        self.logoutbutton.setStyleSheet(u"background-color: rgb(255, 255, 255);")
        self.adddocbutton = QPushButton(Form)
        self.adddocbutton.setObjectName(u"adddocbutton")
        self.adddocbutton.setGeometry(QRect(72, 237, 121, 31))
        self.adddocbutton.setStyleSheet(u"\n"
"background-color: rgb(255, 255, 255);")
        self.adddocbutton.setCheckable(False)
        self.adddocbutton.setAutoRepeat(False)
        self.adddrivepic = QLabel(Form)
        self.adddrivepic.setObjectName(u"adddrivepic")
        self.adddrivepic.setGeometry(QRect(30, 230, 41, 41))
        self.adddrivepic.setPixmap(QPixmap(u"menuPage/images/add_icon.png"))
        self.mydrivepic = QLabel(Form)
        self.mydrivepic.setObjectName(u"mydrivepic")
        self.mydrivepic.setGeometry(QRect(30, 290, 41, 41))
        self.mydrivepic.setPixmap(QPixmap(u"menuPage/images/myDirveIcon.jpg"))
        self.menupic = QLabel(Form)
        self.menupic.setObjectName(u"menupic")
        self.menupic.setGeometry(QRect(60, 40, 151, 141))
        self.menupic.setPixmap(QPixmap(u"menuPage/images/menu_user_icon.png"))
        self.wallpaper = QLabel(Form)
        self.wallpaper.setObjectName(u"wallpaper")
        self.wallpaper.setGeometry(QRect(0, 0, 1000, 500))
        self.wallpaper.setPixmap(QPixmap(u"menuPage/images/menuBackground.png"))
        self.listWidget = QListWidget(Form)
        self.listWidget.setObjectName(u"listWidget")
        self.listWidget.setGeometry(QRect(275, 130, 681, 331))
        self.wallpaper.raise_()
        self.logoutpic.raise_()
        self.pagestatus.raise_()
        self.sharedrivebutton.raise_()
        self.sharedrivepic.raise_()
        self.mydrivebutton.raise_()
        self.logoutbutton.raise_()
        self.adddocbutton.raise_()
        self.adddrivepic.raise_()
        self.mydrivepic.raise_()
        self.menupic.raise_()
        self.listWidget.raise_()

        self.retranslateUi(Form)

        QMetaObject.connectSlotsByName(Form)
    # setupUi

    def retranslateUi(self, Form):
        Form.setWindowTitle(QCoreApplication.translate("Form", u"Form", None))
        self.logoutpic.setText("")
        self.pagestatus.setText(QCoreApplication.translate("Form", u"
My Drive
", None))
        self.sharedrivebutton.setText(QCoreApplication.translate("Form", u"Share Drive", None))
        self.sharedrivepic.setText("")
        self.mydrivebutton.setText(QCoreApplication.translate("Form", u"My Drive", None))
        self.logoutbutton.setText(QCoreApplication.translate("Form", u"Log Out", None))
        self.adddocbutton.setText(QCoreApplication.translate("Form", u"Add Document", None))
        self.adddrivepic.setText("")
        self.mydrivepic.setText("")
        self.menupic.setText("")
        self.wallpaper.setText("")
    # retranslateUi


#!/usr/bin/python

import math
import vtk
import GraphicsPrimitives
import euclid

def Transform(M, Points):
  return [M*euclid.Point3(*p) for p in Points]

def Perp(u):
  """Randomly picks a reasonable perpendicular vector"""
  v = euclid.Vector3(1, 0, 0)
  u_prime = u.cross(v)
  if u_prime.magnitude_squared() < 0.01:
    v = euclid.Vector3(0, 1, 0)
    u_prime = u.cross(v)
  return u_prime.normalized()

def VandFtoPoly(Vertices,
                Faces):
  points = vtk.vtkPoints()
  for v in Vertices:
     points.InsertNextPoint(v)

  polygons = vtk.vtkCellArray()
  for f in Faces:
    poly = vtk.vtkPolygon()
    Pids = poly.GetPointIds()
    Pids.SetNumberOfIds(len(f))
    for i,pid in enumerate(f):
      Pids.SetId(i,pid)
    polygons.InsertNextCell(poly)

  polygonPolyData = vtk.vtkPolyData()
  polygonPolyData.SetPoints(points)
  polygonPolyData.SetPolys(polygons)
  return polygonPolyData

def VandFtoActor(Vertices,
                 Faces,
                 Color,
                 Opacity=1.0,
                 DoSetWireframe = False):
  Actor = vtk.vtkActor()
  poly = VandFtoPoly(Vertices,Faces)
  mapper = vtk.vtkPolyDataMapper()
  mapper.SetInputData(poly)
  Actor.SetMapper(mapper)
  Actor.GetProperty().SetColor(*Color)
  Actor.GetProperty().SetOpacity(Opacity)

  if DoSetWireframe:
    Actor.GetProperty().SetRepresentationToWireframe()
  return Actor

def AddVandFtoRender(Renderer,
                     VandF,
                     Pos=None,
                     Dir=None,
                     Scale=(1,1,1),
                     Color=(1,0,0),
                     Opacity=1.0,
                     DoSetWireframe = False):
  """Add an actor to a vtk scene"""
  Vertices,Faces = VandF

  if Pos is None:
    Pos = (0,0,0)
    MPos = euclid.Matrix4.new_identity()
  else:
    MPos = euclid.Matrix4.new_translate(*Pos)

  if Dir is None:
    MDir = euclid.Matrix4.new_identity()
  else:
    Dir = euclid.Vector3(*Dir)
    Dir /= math.sqrt(Dir.magnitude_squared())
    nX = Perp(Dir)
    nY = Dir.cross(nX)
    MDir = euclid.Matrix4.new_rotate_triple_axis(nX,
                                                 nY,
                                                 Dir)

  M = (MDir * euclid.Matrix4.new_scale(*Scale)).pre_translate(*Pos)
  Vertices = Transform(M,Vertices)
  Actor = VandFtoActor(Vertices,Faces,Color,Opacity,DoSetWireframe)
  Renderer.AddActor(Actor)

  return Actor

def AddLine(Renderer,
            PolyLine,
            LineWidth=5,
            Color=(1,0,0)):
  if isinstance(PolyLine, vtk.vtkObject):
    if not PolyLine.GetClassName() == 'vtkPolyData':
      raise Exception('No support for %s'%PolyLine.GetClassName())
    polygonPolyData = PolyLine
  else:
    points = vtk.vtkPoints()
    for v in PolyLine:
       points.InsertNextPoint(v)
    lines = vtk.vtkCellArray()
    for i in range(len(PolyLine)-1):
      line = vtk.vtkLine()
      line.GetPointIds().SetId(i,i)
      line.GetPointIds().SetId(i+1,i+1)
      lines.InsertNextCell(line)

    polygonPolyData = vtk.vtkPolyData()
    polygonPolyData.SetPoints(points)
    polygonPolyData.SetLines(lines)
  mapper = vtk.vtkPolyDataMapper()
  mapper.SetInputData(polygonPolyData)
  Actor=vtk.vtkActor()
  Actor.GetProperty().SetLineWidth(LineWidth)
  Actor.GetProperty().SetColor(*Color)
  Actor.SetMapper(mapper)
  Renderer.AddActor(Actor)

def AddArrowToRenderer(Renderer,
                       Pos=None,
                       Len=10,
                       Dir=None,
                       CylinderSlicesNum=16,
                       ColorHead=(1,0,0),
                       ColorCyl=(0.5,0.5,0.5),
                       Stem=0.25):
  """Add an actor to a vtk scene"""
  cylV,cylF = GraphicsPrimitives.cylinder(num_slices=CylinderSlicesNum)
  headV,headF = GraphicsPrimitives.cone(num_slices=CylinderSlicesNum)

  if Pos is None:
    Pos = (0,0,0)
    MPos = euclid.Matrix4.new_identity()
  else:
    MPos = euclid.Matrix4.new_translate(*Pos)

  if Dir is None:
    MDir = euclid.Matrix4.new_identity()
  else:
    Dir = euclid.Vector3(*Dir).normalized()
    nX = Perp(Dir)
    nY = Dir.cross(nX)
    MDir = euclid.Matrix4.new_rotate_triple_axis(nX,
                                                 nY,
                                                 Dir)

  M = (MDir * euclid.Matrix4.new_scale(Stem,Stem,Len).translate(0,0,0.5)).pre_translate(*Pos)
  cylV = Transform(M,cylV)

  M = (MDir * euclid.Matrix4.new_scale(Stem*2,Stem*2,Stem*3).pre_translate(0,0,Len)).pre_translate(*Pos)
  headV = Transform(M,headV)

  # Add the cylinder and the head to the renderer
  ArrowActor = VandFtoActor(headV,headF,ColorHead)
  AxisActor = VandFtoActor(cylV,cylF,ColorCyl)
  Renderer.AddActor(ArrowActor)
  Renderer.AddActor(AxisActor)

  return ArrowActor, AxisActor

if __name__ == '__main__':
  # Placing graphics primitives in a vtk scene
  ren = vtk.vtkRenderer()
  renWin = vtk.vtkRenderWindow()
  renWin.AddRenderer(ren)
  renWin.SetSize(600,600)

  AddArrowToRenderer(ren,Pos=(0,0,0),Len=5,Dir=(1,0,0), ColorHead=(1,0,0), Stem = 0.1)
  AddArrowToRenderer(ren,Pos=(0,0,0),Len=5,Dir=(0,1,0), ColorHead=(0,1,0), Stem = 0.1)
  AddArrowToRenderer(ren,Pos=(0,0,0),Len=5,Dir=(0,0,1), ColorHead=(0,0,1), Stem = 0.1)

  AddVandFtoRender(ren,
                   GraphicsPrimitives.icosahedron(subdivide=5),
                   Pos=(2,2,2),
                   #Dir=(1,1,1),
                   Scale=(.01,.01,.01),
                   Color=(1,0,0)
                   )

  AddLine(ren,
          [(2,2,2),(3,3,3), (2,3,2)],
          LineWidth = 1)

  # enable user interface interactor
  iren = vtk.vtkRenderWindowInteractor()
  iren.SetRenderWindow(renWin)
  inStyle = vtk.vtkInteractorStyleSwitch()
  inStyle.SetCurrentStyleToTrackballCamera()
  iren.SetInteractorStyle(inStyle)

  ren.SetBackground(0.1, 0.2, 0.4)
  renWin.Render()
  iren.Start()


### argument 1 [$moviequery] : search query for google.com/movies
### argument 2 [$location]   : search near (default = 94043)
### www.google.com/movies?q=$movieName&near=$location&date=0

import sys
import requests, re
import itertools
import simplejson, urllib

def main(args):
  if len(args) == 4:
    getMovieResults(movie=args[1], location=args[2], date=args[3])
  elif len(args) == 3:
    getAllMovieResults(location=args[1], date=args[2])

  #getMovieResults(movie='pixels', location='94043')
  #getMovieResults(movie='pixels', location='94043', date='1')
  #getMovieResults(movie='antman', location='94002', date='0')
  #getAllMovieResults('94043', '0')

def getAllMovieResults(location, date):

  if type(location) is str and location.isdigit() and len(location) == 5:
    if type(date) is str and date.isdigit() and (date == '0' or date == '1' or date == '2'):
      print(location, date)
    else:
      print("Bad date input")
  else:
    print("Bad zipcode input")

  rootUrl = "http://www.google.com/movies"
  payload = {'near': location, 'date': date}

  ### clean up payload
  for k, v in payload.items():
    if v == '':
      del payload[k]

  htmlSource = requests.get(rootUrl, params=payload)
  print('[' + htmlSource.url + ']')

  #movieNamePattern = "
.*?(.*?).*?
"
  theaterNamePattern = "
.*?
(.*?)

(.*?)

"
  results_theaterNames = re.split(theaterNamePattern, htmlSource.text)

  dataBase = list()
  for i in range(len(results_theaterNames)/3):
    theaterName = results_theaterNames[3*i+1]
    #print(theaterName)

    theaterAddress = results_theaterNames[3*i+1+1]
    #print(theaterAddress)

    theaterData = results_theaterNames[3*i+1+1+1]
    #print(theatreData)

    ### drive time to theatre
    driveTimeUrl = "http://maps.googleapis.com/maps/api/distancematrix/json?origins={0}&destinations={1}&mode=driving&language=en-EN&sensor=false".format(location,theaterAddress)
    driveTimeResult= simplejson.load(urllib.urlopen(driveTimeUrl))
    driveTime = driveTimeResult['rows'][0]['elements'][0]['duration']['text']
    #print(driveTime)

    theaterDataPattern = "

(.*?)
"
    results_theaterData = re.split(theaterDataPattern, theaterData)

    for j in range(len(results_theaterData)/2):
      movieName = results_theaterData[2*j+1]
      #print(movieName)

      showtimesData = results_theaterData[2*j+1+1]
      #print(showtimesData)

      showtimesPattern = ".*?"
      results_showtimes = re.findall(showtimesPattern, showtimesData)
      #print(results_showtimes)

      n = len(results_showtimes)
      #print n

      dataBase.extend([dict([('date', date), ('time', time), ('movieName', movieName), ('theater', theaterName), ('driveTime', driveTime), ('theaterAddress', theaterAddress)]) for (date, time) in results_showtimes])

  #print dataBase

  ### sort dataBase by showtime
  dataBase.sort(key=lambda x: x['time'])
  date0           = str(max([len(x['date']) for x in dataBase]))
  time0           = str(max([len(x['time']) for x in dataBase]))
  movie0          = str(max([len(x['movieName']) for x in dataBase]))
  theaterName0    = str(max([len(x['theater']) for x in dataBase]))
  driveTime0      = str(max([len(x['driveTime']) for x in dataBase]))
  theaterAddress0 = str(max([len(x['theaterAddress']) for x in dataBase]))

  displayString = '{{0:<{0}}}  {{1:<{1}}}  {{2:<{2}}}  {{3:<{3}}}  {{4:<{4}}}  {{5:<{5}}}'.format(date0, time0, movie0, theaterName0, driveTime0, theaterAddress0)
  #print(displayString)

  if len(dataBase) > 0:

    print(displayString.format('DATE', 'TIME', 'MOVIE', 'THEATER', 'DRIVE TIME', 'ADDRESS'))
    print(displayString.format('----', '----', '-----', '-------', '----------', '-------'))

    for idx, x in enumerate(dataBase):
      if (idx < 25):
        print(displayString.format(x['date'], x['time'], x['movieName'], x['theater'], x['driveTime'], x['theaterAddress']))

def getMovieResults(movie, location, date):

  if type(movie) is str and len(movie) > 0:
    if type(location) is str and location.isdigit() and len(location) == 5:
      if type(date) is str and date.isdigit() and (date == '0' or date == '1' or date == '2'):
        print(movie, location, date)
      else:
        print("Bad date input")
    else:
      print("Bad zipcode input")
  else:
    print("Bad movie input")

  rootUrl = "http://www.google.com/movies"
  payload = {'q': movie, 'near': location, 'date': date}

  ### clean up payload
  for k, v in payload.items():
    if v == '':
      del payload[k]

  htmlSource = requests.get(rootUrl, params=payload)
  print('[' + htmlSource.url + ']')

  movieNamePattern = "
.*?(.*?).*?
"
  movieList = re.split(movieNamePattern, htmlSource.text)

  #print len(movieList), len(movieList)/2
  dataBase = list()
  for i in range(len(movieList)/2):
    #print movie
    movieName = movieList[2*i+1]
    #print movieName
    movieData = movieList[2*i+1+1]
    #print movieData
    #theatreNamePattern = "
.*?
.*?(.*?)"
    theaterNamePattern = "
.*?
(.*?)

(.*?)
"
    results = re.split(theaterNamePattern, movieData)

    for j in range(len(results)/3):
      theatreName = results[3*j+1]
      theatreAddress = results[3*j+1+1]

      ### drive time to theatre
      driveTimeUrl = "http://maps.googleapis.com/maps/api/distancematrix/json?origins={0}&destinations={1}&mode=driving&language=en-EN&sensor=false".format(location,theatreAddress)
      driveTimeResult= simplejson.load(urllib.urlopen(driveTimeUrl))
      driveTime = driveTimeResult['rows'][0]['elements'][0]['duration']['text']

      showtimesData = results[3*j+3]
      #print(showtimesData)
      showtimesPattern = ".*?"
      #showtimesPattern = "(\d\d:\d\d).*?>.*?"
      movieShowtimes = re.findall(showtimesPattern, showtimesData)
      n = len(movieShowtimes)
      dataBase.extend([dict([('date', date), ('time', time), ('movieName', movieName), ('theatre', theatreName), ('driveTime', driveTime), ('theatreAddress', theatreAddress)]) for (date, time) in movieShowtimes])

  #print dataBase
  ### sort dataBase by showtime
  dataBase.sort(key=lambda x: x['time'])
  displayString = '{0:<10}  {1:<6}  {2:<30}  {3:<30}  {4:<10}  {5:<50}'
  if len(dataBase) > 0:
    print(displayString.format('DATE', 'TIME', 'MOVIE', 'THEATRE', 'DRIVE TIME', 'ADDRESS'))
    print(displayString.format('----', '----', '-----', '-------', '----------', '-------'))
    for idx, x in enumerate(dataBase):
      if (idx < 10):
        print(displayString.format(x['date'], x['time'], x['movieName'], x['theatre'], x['driveTime'], x['theatreAddress']))

if __name__ == "__main__":
  main(sys.argv)


#!/usr/bin/env python

# Upload files named on ARGV as Slack emoji.
# https://github.com/smashwilson/slack-emojinator

from __future__ import print_function
import subprocess
import argparse
import os
import re
import requests

from bs4 import BeautifulSoup

try:
    raw_input
except NameError:
    raw_input = input

URL = "https://{team_name}.slack.com/customize/emoji"

def _session(args):
    assert args.cookie, "Cookie required"
    assert args.team_name, "Team name required"
    session = requests.session()
    session.headers = {'Cookie': args.cookie}
    session.url = URL.format(team_name=args.team_name)
    return session

def _argparse():
    parser = argparse.ArgumentParser(
        description='Bulk upload emoji to slack'
    )
    parser.add_argument(
        '--team-name', '-t',
        default=os.getenv('SLACK_TEAM'),
        help='Defaults to the $SLACK_TEAM environment variable.'
    )
    parser.add_argument(
        '--message', '-m',
        default="",
        help='original text to send'
    )
    parser.add_argument(
        '--cookie', '-c',
        default=os.getenv('SLACK_COOKIE'),
        help='Defaults to the $SLACK_COOKIE environment variable.'
    )
    args = parser.parse_args()
    if not args.team_name:
        args.team_name = raw_input('Please enter the team name: ').strip()
    if not args.cookie:
        args.cookie = raw_input('Please enter the "/customize/emoji" cookie: ').strip()
    if not args.message:
        args.message = raw_input('please enter message: ').strip()
    return args

def main():
    make_emoji("ok")
    args = _argparse()
    session = _session(args)
    existing_emojis = get_current_emoji_list(session)
    uploaded = 0
    skipped = 0

    words = args.message.split(" ")
    for word in args.message.split(" "):
        filename = make_emoji(word)
        emoji_name = word.lower()
        print("Processing {}.".format(filename))
        if emoji_name in existing_emojis:
            print("Skipping {}. Emoji already exists".format(emoji_name))
            skipped += 1 # ¯\_(ツ)_/¯
        else:
            upload_emoji(session, emoji_name, filename)
            print("{} upload complete.".format(filename))
            uploaded += 1 # ¯\_(ツ)_/¯

        subprocess.run(
            "rm {filename}".format(filename=filename),
            shell=True,
            check=False
        )

    slackmoji_text = " ".join(list(map(":{0}:".format, words)))
    send_slack_msg(slackmoji_text)
    print('\nUploaded {} emojis. ({} already existed)'.format(uploaded, skipped))

def send_slack_msg(msg):
    subprocess.run(
        'osascript paste_in_slack.scpt "{0}"'.format(msg),
        shell=True,
        check=False
    )

def make_emoji(word):
    emoji_text = word.upper()
    emoji_name = word.lower()

    subprocess.run("""
        convert \
            -size 128x128 \
            -gravity center \
            -fill black \
            -kerning -1 \
            -font /System/Library/Fonts/Helvetica.ttc \
            label:{emoji_text} \
            "PNG32:tmp-{emoji_name}.png";
        """.format(emoji_text=emoji_text, emoji_name=emoji_name),
        shell=True,
        check=False
    )

    subprocess.run("""
        convert \
            "tmp-{emoji_name}.png" \
            -transparent white \
            -fuzz 2% \
            "{emoji_name}.png";
        """.format(emoji_text=emoji_text, emoji_name=emoji_name),
        shell=True,
        check=False
    )

    subprocess.run(
        "rm tmp-{emoji_name}.png".format(emoji_name=emoji_name),
        shell=True,
        check=False
    )

    return "{emoji_name}.png".format(emoji_name=emoji_name)

def get_current_emoji_list(session):
    r = session.get(session.url)
    r.raise_for_status()
    x = re.findall("data-emoji-name=\"(.*?)\"", r.text)
    return x

def upload_emoji(session, emoji_name, filename):
    # Fetch the form first, to generate a crumb.
    r = session.get(session.url)
    r.raise_for_status()
    soup = BeautifulSoup(r.text, "html.parser")
    crumb = soup.find("input", attrs={"name": "crumb"})["value"]

    data = {
        'add': 1,
        'crumb': crumb,
        'name': emoji_name,
        'mode': 'data',
    }
    files = {'img': open(filename, 'rb')}
    r = session.post(session.url, data=data, files=files, allow_redirects=False)
    r.raise_for_status()
    # Slack returns 200 OK even if upload fails, so check for status of 'alert_error' info box
    if b'alert_error' in r.content:
        soup = BeautifulSoup(r.text, "html.parser")
        crumb = soup.find("p", attrs={"class": "alert_error"})
        print("Error with uploading %s: %s" % (emoji_name, crumb.text))

if __name__ == '__main__':
    main()


"""
VIEW DICTIONARY
ADD WORD AND MEANING
UPDATE WORD AND MEANING
DELETE WORD FROM RECORD
CLEAR THE ENTRY
EXIT
"""

"""
--
-- Database: `dictionary`
--

-- --------------------------------------------------------

--
-- Table structure for table `english`
--

CREATE TABLE `english` (
  `sno` int(11) NOT NULL,
  `word` varchar(25) NOT NULL,
  `definition` varchar(100) NOT NULL DEFAULT ''
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `english`
--

INSERT INTO `english` (`sno`, `word`, `definition`) VALUES
(1, 'table', 'to keep things'),
(2, 'rose', 'a flower in garden'),
(3, 'apple', 'a fruit'),
(4, 'head', 'a part of body'),
(5, 'pen', 'a tool to write'),
(6, 'bottle', 'to hold a quantity of water or liquid'),
(7, 'note', 'to note things'),
(8, 'pencil', 'to write something'),
(9, 'cold', 'to feel cool and freezed'),
(10, 'eraser', 'to eraser the writen things'),
(11, 'window', 'for the purpose of air and light'),
(12, 'water', 'to provide energy');
"""

from tkinter import *
import tkinter as tk
from tkinter import ttk
from PIL import ImageTk,Image
from tkinter import messagebox
import pyttsx3

root=Tk()
import mysql.connector

root.title("Dictionary")
root.geometry("1366x768+0+0")  #resolution of ur screen
root.config(bg="#90EE90")
root.state("zoomed") #to get screen in full screen

leftFrame=Frame(root,bg="#87CEEB")   #for radiobuttons
leftFrame.pack(side=TOP,fill=X)

bottomFrame=Frame(root,bg="#90EE90",width=1988,height=520)   #for fill details
bottomFrame.pack(side=TOP,fill=X)

rightFrame = Frame(root, width=550)    #for treeview
rightFrame.place(relx = 1, x =-590, y = 30)

#****************************************************************************************

def getData(event):             #to get values when the record is clicked
    selected_row = tv.focus()
    data = tv.item(selected_row)
    global row
    row = data["values"]
    #print(row)
    i_sno.set(row[0])
    i_name.set(row[1])
    i_meaning.set(row[2])
def save():         #add the record
    global c
    db = mysql.connector.connect(host="localhost",user="root",passwd="",database="dictionary")
    con = db.cursor()
    a=i_name.get()         #to get the values from entry box
    b=i_meaning.get()
    if a=="" or b=="" :
        messagebox.showerror("Error in Input", "Please Fill all the details")
        return
    con.execute("insert into english(word, definition) values ('"+a+"','"+b+"')")
    engine = pyttsx3.init()
    engine.say("Word added successfully!")
    engine.runAndWait()
    messagebox.showinfo("Add word", "Word added successfully!")
    db.commit()
    db.close()
    aa.grid_forget()                #to vanish the label and entry box
    name.grid_forget()
    name_1.grid_forget()
    meaning_1.grid_forget()
    meaning.grid_forget()
    submit.grid_forget()
    lab1.grid_forget()
    lab2.grid_forget()
    clear()
def update():
    global c
    db = mysql.connector.connect(host="localhost",user="root",passwd="",database="dictionary")
    con = db.cursor()
    a=i_name.get()
    b=i_meaning.get()
    d=i_sno.get()
    if a=="" or b=="" or d=="":
        messagebox.showerror("Error in Input", "Please Fill all the details")
        return
    con.execute("update english set word='"+a+"', definition='"+b+"' where sno='%s'"%(d))
    db.commit()
    db.close()
    engine = pyttsx3.init()
    engine.say("Word updated successfully, click on to view dictionary to see the changes made ")
    engine.runAndWait()
    messagebox.showinfo("Update word", "Updation successful!")
    bb.grid_forget()
    name.grid_forget()
    name_1.grid_forget()
    meaning_1.grid_forget()
    meaning.grid_forget()
    sno.grid_forget()
    submit.grid_forget()
    sno_1.grid_forget()
    lab1.grid_forget()
    lab2.grid_forget()
    clear()
def delete():
    global c
    db = mysql.connector.connect(host="localhost",user="root",passwd="",database="dictionary")
    con = db.cursor()
    e=i_sno.get()
    if e=="":
        messagebox.showerror("Error in Input", "Please Fill all the details")
        return
    con.execute("delete from english where sno='%s'"%(e))
    db.commit()
    db.commit()
    db.close()
    engine = pyttsx3.init()
    engine.say("Word deleted")
    engine.runAndWait()
    messagebox.showinfo("Delete word", "Word deleted successfully!")
    clear()
    sno.grid_forget()
    sno_1.grid_forget()
    d.grid_forget()
    submit.grid_forget()

def clear():
    if i_sno != ""  :
        i_sno.set("")                 #to clear the entry box
        i_name.set("")
        i_meaning.set("")

    else:
        i_name.set("")
        i_meaning.set("")

def search():                     #like query
    db = mysql.connector.connect(host="localhost",user="root",passwd="",database="dictionary")
    con = db.cursor()
    c=i_name.get()
    if c=="":
        messagebox.showerror("Error in Input", "Please Fill all the details")
        return
    sql="select sno,word,definition from english where word like '%{a}%';".format(a=c)
    con.execute(sql)
    records = con.fetchall()

    tv.delete(*tv.get_children())       #it clear the treeview and re-enter the records
    for row in records:
            tv.insert("",END,values=row)
    db.commit()
    db.close()
    clear()

def define():               #to view meaning of a word typed
    db = mysql.connector.connect(host="localhost",user="root",passwd="",database="dictionary")
    con = db.cursor()
    c=i_name.get()
    if c=="":
        messagebox.showerror("Error in Input", "Please Fill all the details")
        return
    sql="select definition from english where word='%s'"%(c);
    con.execute(sql)
    records = con.fetchone()
    if records==None:
        messagebox.showinfo("Word not found", "Word not found!! \t Enter a valid word!")

    engine = pyttsx3.init()
    engine.say("The meaning of "+c+ " is " + " " +records[0])
    engine.runAndWait()
    messagebox.showinfo("Meaning", "The meaning of "+c+ " is " + " " +records[0])

    db.commit()
    db.close()
    clear()
    sno.grid_forget()
    d.grid_forget()
    submit.grid_forget()
    submit1.grid_forget()

img=Image.open("mic1.jpg")
re=img.resize((30,30),Image.ANTIALIAS)
new=ImageTk.PhotoImage(re)
img1=Image.open("mic1.jpg")
re1=img1.resize((30,30),Image.ANTIALIAS)
new1=ImageTk.PhotoImage(re1)

i_sno=IntVar()
i_name = StringVar()
i_meaning = StringVar()
def radio():
    select=int(var.get())
    if(select==1):
        tv.delete(*tv.get_children())   #it clear the treeview and re-enter the records
        db = mysql.connector.connect(host="localhost",user="root",passwd="",database="dictionary")
        con = db.cursor()
        sql = "select * from english order by sno"
        con.execute(sql)
        records = con.fetchall()

        for row in records:
            tv.insert("",END,values=row)
        db.commit()
        db.close()

    if(select==2):
        global name,name_1,aa,meaning,meaning_1,submit,lab,lab1,lab2,submit1

        aa=Label(bottomFrame,text = "Add Word",font=("Arial Bold", 20),fg="black",bg="#90EE90")
        aa.grid(row=2,column=4,padx=10,pady=10,columnspan=2)
        name=Label(bottomFrame,text="Word",font=("Arial Bold", 14),fg="black",bg="#90EE90")
        name.grid(row=4,column=3,padx=5,pady=10,sticky=W)
        name_1=Entry(bottomFrame,textvariable=i_name,width=30,font=("Arial Bold", 14),fg="black",bg="white")
        name_1.grid(row=4,column=4,padx=5,sticky=W)
        lab1=Button(bottomFrame,text="hii",image=new,bd=0,bg="#90EE90")
        lab1.grid(row=4,column=6)

        meaning=Label(bottomFrame,text="Meaning",font=("Arial Bold", 14),fg="black",bg="#90EE90")
        meaning.grid(row=6,column=3,padx=5,sticky=W)
        meaning_1=Entry(bottomFrame,textvariable=i_meaning,width=30,font=("Arial Bold", 14),fg="black",bg="white")
        meaning_1.grid(row=6,column=4,padx=5,sticky=W)
        lab2=Button(bottomFrame,text="hii",image=new,bd=0,bg="#90EE90")
        lab2.grid(row=6,column=6)
        submit=Button(bottomFrame,text="ADD",command=save,width=10,font=("Arial Bold", 14),fg="black",bg="#87CEEB")
        submit.grid(row=7,column=4,padx=5,pady=10,columnspan=2)

    if(select==3):
        global bb,sno,sno_1
        bb=Label(bottomFrame,text = "Update word",font=("Arial Bold", 20),fg="black",bg="#90EE90")
        bb.grid(row=2,column=4,padx=10,pady=10,columnspan=2)
        sno=Label(bottomFrame,text="sno",font=("Arial Bold", 14),fg="black",bg="#90EE90")
        sno.grid(row=4,column=3,padx=5,pady=10,sticky=W)
        sno_1=Entry(bottomFrame,textvariable=i_sno,width=30,font=("Arial Bold", 14),fg="black",bg="white")
        sno_1.grid(row=4,column=4,padx=5,pady=10,sticky=W)

        name=Label(bottomFrame,text="Word",font=("Arial Bold", 14),fg="black",bg="#90EE90")
        name.grid(row=6,column=3,padx=5,pady=10,sticky=W)
        name_1=Entry(bottomFrame,textvariable=i_name,width=30,font=("Arial Bold", 14),fg="black",bg="white")
        name_1.grid(row=6,column=4,padx=5,sticky=W)
        lab1=Button(bottomFrame,text="hii",image=new,bd=0,bg="#90EE90")
        lab1.grid(row=6,column=6)

        meaning=Label(bottomFrame,text="Meaning",font=("Arial Bold", 14),fg="black",bg="#90EE90")
        meaning.grid(row=8,column=3,padx=5,sticky=W)
        meaning_1=Entry(bottomFrame,textvariable=i_meaning,width=30,font=("Arial Bold", 14),fg="black",bg="white")
        meaning_1.grid(row=8,column=4,padx=5,sticky=W)
        lab2=Button(bottomFrame,text="hii",image=new,bd=0,bg="#90EE90")
        lab2.grid(row=8,column=6)
        submit=Button(bottomFrame,text="UPDATE",command=update,width=10,font=("Arial Bold", 14),fg="black",bg="#87CEEB")
        submit.grid(row=9,column=4,padx=5,pady=10,columnspan=2)

    if(select==4):
        global d
        d=Label(bottomFrame,text = "Delete word",font=("Arial Bold", 20),fg="black",bg="#90EE90")
        d.grid(row=2,column=4,padx=10,pady=10,columnspan=2)
        sno=Label(bottomFrame,text="sno",font=("Arial Bold", 14),fg="black",bg="#90EE90")
        sno.grid(row=4,column=3,padx=5,pady=10,sticky=W)
        sno_1=Entry(bottomFrame,textvariable=i_sno,width=30,font=("Arial Bold", 14),fg="black",bg="white")
        sno_1.grid(row=4,column=4,padx=5,pady=10,sticky=W)
        submit=Button(bottomFrame,text="DELETE",command=delete,width=10,font=("Arial Bold", 14),fg="black",bg="#87CEEB")
        submit.grid(row=5,column=4,padx=5,pady=10,columnspan=2)

    if (select==5):
        d=Label(bottomFrame,text = "Enter a part of word you remember!!",font=("Arial Bold", 20),fg="black",bg="#90EE90")
        d.grid(row=2,column=4,padx=10,pady=10,columnspan=2)
        sno=Entry(bottomFrame,textvariable=i_name,width=30,font=("Arial Bold", 14),fg="black",bg="white")
        sno.grid(row=4,column=4,padx=5,pady=10,sticky=W)
        submit=Button(bottomFrame,text="SEARCH",command=search,width=10,font=("Arial Bold", 14),fg="black",bg="#87CEEB")
        submit.grid(row=4,column=5,padx=5,pady=10,columnspan=2)
        submit1=Button(bottomFrame,text="DEFINITION",command=define,width=10,font=("Arial Bold", 14),fg="black",bg="#87CEEB")
        submit1.grid(row=5,column=5,padx=5,pady=10,columnspan=2)

    if(select==6):
        clear()

var = IntVar()
name=Label(leftFrame,text="DICTIONARY",font=("Arial Bold", 30),fg="black",bg="#87CEEB")
name.grid(row=0,column=3,sticky=W,padx=10,pady=20)

R1 = Radiobutton(leftFrame, text="View Dictionary",variable=var, value=1,command=radio,bg="#87CEEB",font=("Arial Bold",14,"bold"))
R1.grid(row=2,column=2,sticky=W,padx=10)
R2 = Radiobutton(leftFrame, text="Add word",variable=var, value=2,command=radio,bg="#87CEEB",font=("Arial Bold",14,"bold"))
R2.grid(row=3,column=2,sticky=W,padx=10)
R3 = Radiobutton(leftFrame, text="Update word",variable=var, value=3,command=radio,bg="#87CEEB",font=("Arial Bold",14,"bold"))
R3.grid(row=4,column=2,sticky=W,padx=10)
R4 = Radiobutton(leftFrame, text="Delete word",variable=var, value=4,command=radio,bg="#87CEEB",font=("Arial Bold",14,"bold"))
R4.grid(row=5,column=2,sticky=W,padx=10)
R7=Radiobutton(leftFrame,text="Search",variable=var,command=radio,value=5,bg="#87CEEB",font=("Arial Bold",14,"bold"))
R7.grid(row=6,column=2,sticky=W,padx=10)
R5 = Radiobutton(leftFrame, text="Clear",variable=var, value=6,command=radio,bg="#87CEEB",font=("Arial Bold",14,"bold"))
R5.grid(row=7,column=2,sticky=W,padx=10)
R6=Radiobutton(leftFrame,text="Exit",variable=var,command=root.destroy,value=7,bg="#87CEEB",font=("Arial Bold",14,"bold"))
R6.grid(row=8,column=2,sticky=W,padx=10)

style=ttk.Style()
#pick theme for treeview
style.theme_use("alt")

"""
style.configure("Treeview",background="silver",foreground="black",font=("Calibri",16,"bold"),rowheight=25,fieldbackground="silver")
#change selected color
style.map('Treeview',background=[('selected','green')])

tv=ttk.Treeview(rightFrame,column=(1,2,3))
tv.heading("1",text='ID')
tv.column(1,width=10)
tv.heading("2",text='WORD')
tv.column(1,width=30)
tv.heading("3",text="MEANING")
tv.column(1,width=140)

tv['show']='headings'
tv.bind("<ButtonRelease-1>",getData)
tv.pack(fill=X)
"""

#rightFrame scrollbar for treeview
tree_scroll = Scrollbar(rightFrame)
tree_scroll.pack(side=RIGHT,fill=Y)

#creating tree view
tv=ttk.Treeview(rightFrame,yscrollcommand=tree_scroll.set)
tree_scroll.config(command=tv.yview)   #configure scrollbar for treeview

tv['columns']=("ID","WORD","MEANING")
tv.column("#0",width=0,stretch=NO)
tv.column("ID",anchor=CENTER,width=50)
tv.column("WORD",anchor=W,width=140)
tv.column("MEANING",anchor=W,width=250)

tv.heading("#0",text="",anchor=W)
tv.heading("ID",text="ID",anchor=CENTER)
tv.heading("WORD",text="WORD",anchor=CENTER)
tv.heading("MEANING",text="MEANING",anchor=CENTER)
tv['show']='headings'
tv.bind("<ButtonRelease-1>",getData)    #butt-1 is for leftclick of mouse
tv.pack(fill=X)

root.mainloop()     #to display in screen


#!/usr/bin/python

import sys
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['mathtext.fontset'] = 'stix'
mpl.rcParams['font.family'] = 'STIXGeneral'

data = []
data.append(pd.read_csv('results/hist_work_v6_4800.dat', delimiter='\t'))
data.append(pd.read_csv('results/hist_work_v10_48.dat', delimiter='\t'))
data.append(pd.read_csv('results/hist_work_v30_48.dat', delimiter='\t'))
label = ['v = 6/4800', 'v = 10/48', 'v=30/48']

bins = int(sys.argv[1])

fig, ax = plt.subplots(1,3,layout='tight')
for j in range(len(data[0].columns)):
    avg = np.average(data[0].iloc[:,j])
    for i in range(len(data)):
        bin_heights, bin_borders, _ = ax[j].hist(data[i].iloc[:,j]-avg,bins,histtype='step', label=label[i])

fig.suptitle('Work histograms')
fig.text(0.5, 0.01, 'Work', ha='center')
ax[0].grid()
ax[0].set_title(r'$x_{c}=-6$')
ax[1].grid()
ax[1].set_title(r'$x_{c}=-9$')
ax[2].grid()
ax[2].set_title(r'$x_{c}=-3$')
ax[1].legend()

#plt.show()
plt.savefig('results/hist_work.png', format='png',bbox_inches='tight',dpi=300)


#!/usr/bin/env python
# coding: utf-8

# In[64]:

import cupy as cp # linear algebra
from cupy import asnumpy
from random import shuffle
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import matplotlib.pyplot as plt
import scipy as sp
from scipy import stats
import matplotlib.pyplot as plt
import seaborn as sns
import pprint

import sklearn
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import f1_score, make_scorer, confusion_matrix, classification_report, precision_recall_curve, plot_precision_recall_curve, average_precision_score, auc
from xgboost import XGBRegressor
from math import sqrt
from sklearn.metrics import mean_squared_error

import seaborn as sns
from hyperopt import hp, fmin, tpe, Trials, STATUS_OK, space_eval
import xgboost as xgb
import shap
import os

# In[65]:

data = pd.read_csv('C:\data_interview_test.csv',sep=":")

# In[66]:

data= data["receipt_id"].str.split(":", n = 13, expand = True)
data["receipt_id"]= data[0]
data.drop(columns =[0], inplace = True)
data["company_id"]= data[1]
data.drop(columns =[1], inplace = True)
data["matched_transaction_id"]= data[2]
data.drop(columns =[2], inplace = True)
data["feature_transaction_id"]= data[3]
data.drop(columns =[3], inplace = True)
data["DateMappingMatch"]= data[4]
data.drop(columns =[4], inplace = True)
data["AmountMappingMatch"]= data[5]
data.drop(columns =[5], inplace = True)
data["DescriptionMatch"]= data[6]
data.drop(columns =[6], inplace = True)
data["DifferentPredictedTime"]= data[7]
data.drop(columns =[7], inplace = True)
data["TimeMappingMatch"]= data[8]
data.drop(columns =[8], inplace = True)
data["PredictedNameMatch"]= data[9]
data.drop(columns =[9], inplace = True)
data["ShortNameMatch"]= data[10]
data.drop(columns =[10], inplace = True)
data["DifferentPredictedDate"]= data[11]
data.drop(columns =[11], inplace = True)
data["PredictedAmountMatch"]= data[12]
data.drop(columns =[12], inplace = True)
data["PredictedTimeCloseMatch"]= data[13]
data.drop(columns =[13], inplace = True)
data.head()

# In[29]:

data.shape

# In[30]:

data['label'] = (data.matched_transaction_id == data.feature_transaction_id).astype(int)
data.hist('label')
print(data.label.value_counts())

# In[67]:

data['label']= (data.matched_transaction_id == data.feature_transaction_id).astype(int)
x,y = data[list(data.columns[4:])].astype(float), data.label
x=x.drop(['label'], axis=1)
x.head()

# In[32]:

#splitting a testing set from the data
X_train, X_test, Y_train, Y_test = train_test_split(x, y, test_size = 0.20, stratify = y, random_state = 42)
#splitting a validation set from the training set to tune parameters
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.20, stratify = Y_train, random_state = 42)

# In[33]:

train = xgb.DMatrix(X_train, label=Y_train)
test = xgb.DMatrix(X_test, label=Y_test)
watchlist = [(test, 'eval'), (train, 'train')]

xgb_params = {
            'objective': 'binary:logistic',
            'tree_method': 'gpu_hist',
            'max_depth': 2,
            'eta':0.1,
            'silent':1,
            'subsample':0.5,
            'colsample_bytree': 0.05,

}

clf = xgb.train( xgb_params,train, num_boost_round=10000,                 )

preds = clf.predict(test)

# In[34]:

print(classification_report(asnumpy(Y_test),preds.round(0)))
print( " In the Confusion Matrix below, the digonal values represent correct classification for each class : ")
labels = ['label-0', 'label-1']
#print(confusion_matrix((y_test),(preds.round(0).astype(int))))

cm = sklearn.metrics.confusion_matrix(asnumpy(Y_test),asnumpy(preds.round(0).astype(int)))

ax= plt.subplot()
sns.heatmap(cm.astype(int), annot=True,fmt='g', ax = ax); #annot=True to annotate cells

# labels, title and ticks
ax.set_xlabel('Predicted labels');ax.set_ylabel('True labels');
ax.set_title('Confusion Matrix');
ax.xaxis.set_ticklabels(labels); ax.yaxis.set_ticklabels(labels);

# In[83]:

data['label']= (data.DateMappingMatch).astype(float)
data.hist('label')
print(" Distribuition of DateMappingMatch feature values")

# In[36]:

org_data = data.copy()
feat_col=data.columns[4:-1]
le = LabelEncoder()
for col in feat_col:
    var_count = data.groupby(col).agg({col:'count'})
    var_count.columns = ['%s_count'%col]
    var_count = var_count.reset_index()
    data = data.merge(var_count,on=col,how='left')
    le.fit(data['%s_count'%col])
    encoded = le.transform(data['%s_count'%col])
    data['%s_count'%col] = encoded/encoded.max()

x,y = data[list(data.columns[4:])].astype(float), data.label
x=x.drop(['label'], axis=1)
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=42,shuffle=True)

train = xgb.DMatrix(X_train, label=y_train)
test = xgb.DMatrix(X_test, label=y_test)
watchlist = [(test, 'eval'), (train, 'train')]

xgb_params = {
            'objective': 'binary:logistic',
            'tree_method': 'gpu_hist',
            'max_depth': 2,
            'eta':0.1,
            'silent':1,
            'subsample':0.5,
            'colsample_bytree': 0.05,

}

clf = xgb.train( xgb_params,train, num_boost_round=10000,                 )

preds = clf.predict(test)
print(classification_report(asnumpy(y_test),preds.round(0)))
print( " In the Confusion Matrix below, the digonal values represent correct classification for each class : ")
labels = ['label-0', 'label-1']
#print(confusion_matrix((y_test),(preds.round(0).astype(int))))

cm = sklearn.metrics.confusion_matrix(asnumpy(y_test),asnumpy(preds.round(0).astype(int)))

ax= plt.subplot()
sns.heatmap(cm.astype(int), annot=True,fmt='g', ax = ax);

# labels, title and ticks
ax.set_xlabel('Predicted labels');ax.set_ylabel('True labels');
ax.set_title('Confusion Matrix');
ax.xaxis.set_ticklabels(labels); ax.yaxis.set_ticklabels(labels);

# In[37]:

data = org_data
data['label']= (data.matched_transaction_id == data.feature_transaction_id).astype(int)
x,y = data[list(data.columns[4:])].astype(float), data.label
x=x.drop(['label'], axis=1)
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=42,shuffle=True)

train = xgb.DMatrix(X_train, label=y_train)
test = xgb.DMatrix(X_test, label=y_test)
watchlist = [(test, 'eval'), (train, 'train')]

xgb_params = {
            'objective': 'binary:logistic',
            'tree_method': 'gpu_hist',
            'max_depth': 2,
            'eta':0.1,
            'silent':1,
            'subsample':0.5,
            'colsample_bytree': 0.05,
            'scale_pos_weight':13,
}

clf = xgb.train( xgb_params,train, num_boost_round=20000,                 )

preds = clf.predict(test)

print(classification_report(asnumpy(y_test),preds.round(0)))
print( " In the Confusion Matrix below, the digonal values represent correct classification for each class : ")
labels = ['label-0', 'label-1']
#print(confusion_matrix((y_test),(preds.round(0).astype(int))))

cm = sklearn.metrics.confusion_matrix(asnumpy(y_test),asnumpy(preds.round(0).astype(int)))

ax= plt.subplot()
sns.heatmap(cm.astype(int), annot=True,fmt='g', ax = ax); #annot=True to annotate cells

# labels, title and ticks
ax.set_xlabel('Predicted labels');ax.set_ylabel('True labels');
ax.set_title('Confusion Matrix');
ax.xaxis.set_ticklabels(labels); ax.yaxis.set_ticklabels(labels);

# In[38]:

tp = np.logical_and(preds.round(0)==1,asnumpy(y_test)==1)
fp = np.logical_and(preds.round(0)==1,asnumpy(y_test)==0)
xtest = X_test.copy()
xtest['scores']=preds
xtest.scores[fp].describe()

# In[39]:

xtest.scores[tp].describe()

# In[40]:

data = org_data
data['label']= (data.matched_transaction_id == data.feature_transaction_id).astype(int)
x,y = data[list(data.columns[4:])].astype(float), data.label
x=x.drop(['label'], axis=1)
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=42,shuffle=True)

train = xgb.DMatrix(X_train, label=y_train)
test = xgb.DMatrix(X_test, label=y_test)
watchlist = [(test, 'eval'), (train, 'train')]

xgb_params = {
            'objective': 'binary:logistic',
            'tree_method': 'gpu_hist',
            'max_depth': 2,
            'eta':0.1,
            'silent':1,
            'subsample':0.5,
            'colsample_bytree': 0.05,
            'scale_pos_weight':8,
}

clf = xgb.train( xgb_params,train, num_boost_round=20000,                 )

preds = clf.predict(test)

print(classification_report(asnumpy(y_test),preds.round(0)))
print( " In the Confusion Matrix below, the digonal values represent correct classification for each class : ")
labels = ['label-0', 'label-1']
#print(confusion_matrix((y_test),(preds.round(0).astype(int))))

cm = sklearn.metrics.confusion_matrix(asnumpy(y_test),asnumpy(preds.round(0).astype(int)))

ax= plt.subplot()
sns.heatmap(cm.astype(int), annot=True,fmt='g', ax = ax); #annot=True to annotate cells

# labels, title and ticks
ax.set_xlabel('Predicted labels');ax.set_ylabel('True labels');
ax.set_title('Confusion Matrix');
ax.xaxis.set_ticklabels(labels); ax.yaxis.set_ticklabels(labels);

# In[41]:

tp = np.logical_and(preds.round(0)==1,asnumpy(y_test)==1)
fp = np.logical_and(preds.round(0)==1,asnumpy(y_test)==0)
xtest = X_test.copy()
xtest['scores']=preds
print ( "false postive stats")
print(xtest.scores[fp].describe())
print()
print ( "True postive stats")
print(xtest.scores[tp].describe())

# In[47]:

from xgboost import XGBRegressor
from math import sqrt
from sklearn.metrics import mean_squared_error
from hyperopt import fmin, tpe, hp, STATUS_OK, Trials, space_eval

# In[48]:

def prep_data(data):
    data['label']= (data.matched_transaction_id == data.feature_transaction_id).astype(int)
    feat_col=data.columns[4:-1]
    le = LabelEncoder()
    for col in feat_col:
        var_count = data.groupby(col).agg({col:'count'})
        var_count.columns = ['%s_count'%col]
        var_count = var_count.reset_index()
        data = data.merge(var_count,on=col,how='left')
        le.fit(data['%s_count'%col])
        encoded = le.transform(data['%s_count'%col])
        data['%s_count'%col] = encoded/encoded.max()
    x,y = data[list(data.columns[4:])].astype(float), data.label
    x=x.drop(['label'], axis=1)
    return x,y
x,y = prep_data(data)
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=42,shuffle=True)

space = {
        'max_depth':hp.choice('max_depth', np.arange(10, 25, 1, dtype=int)),
        'n_estimators':hp.choice('n_estimators', np.arange(1000, 10000, 10, dtype=int)),
        'colsample_bytree':hp.quniform('colsample_bytree', 0.5, 1.0, 0.1),
        'min_child_weight':hp.choice('min_child_weight', np.arange(250, 350, 10, dtype=int)),
        'subsample':hp.quniform('subsample', 0.7, 0.9, 0.1),
        'eta':hp.quniform('eta', 0.1, 0.3, 0.1),

        'objective':'reg:squarederror',

        'tree_method':'gpu_hist',
        'eval_metric': 'rmse',
    }

def score(params):
    model = XGBRegressor(**params)

    model.fit(X_train, y_train, eval_set=[(X_train, y_train), (X_test, y_test)],
              verbose=False, early_stopping_rounds=10)
    Y_pred = model.predict(X_test).clip(0, 20)
    score = sqrt(mean_squared_error(y_test, Y_pred))
    print(score)
    return {'loss': score, 'status': STATUS_OK}

def optimize(trials, space):

    best = fmin(score, space, algo=tpe.suggest, max_evals=10)
    return best

trials = Trials()
best_params = optimize(trials, space)

# Return the best parameters
space_eval(space, best_params)

# In[68]:

#splitting a testing set from the data
X_train, X_test, Y_train, Y_test = train_test_split(x, y, test_size = 0.20, stratify = y, random_state = 42)
#splitting a validation set from the training set to tune parameters
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.20, stratify = Y_train, random_state = 42)

# In[69]:

#creating a scorer from the f1-score metric
f1_scorer = make_scorer(f1_score)

# In[70]:

# defining the space for hyperparameter tuning
space = {'eta': hp.uniform("eta", 0.1, 1),
        'max_depth': hp.quniform("max_depth", 3, 18, 1),
        'gamma': hp.uniform ('gamma', 1,9),
        'reg_alpha' : hp.quniform('reg_alpha', 50, 200, 1),
        'reg_lambda' : hp.uniform('reg_lambda', 0, 1),
        'colsample_bytree' : hp.uniform('colsample_bytree', 0.5, 1),
        'min_child_weight' : hp.quniform('min_child_weight', 0, 10, 1),
        'n_estimators': hp.quniform('n_estimators', 100, 200, 10)
        }

# In[71]:

#defining function to optimize
def hyperparameter_tuning(space):
    clf = xgb.XGBClassifier(n_estimators = int(space['n_estimators']),       #number of trees to use
                            eta = space['eta'],                              #learning rate
                            max_depth = int(space['max_depth']),             #depth of trees
                            gamma = space['gamma'],                          #loss reduction required to further partition tree
                            reg_alpha = int(space['reg_alpha']),             #L1 regularization for weights
                            reg_lambda = space['reg_lambda'],                #L2 regularization for weights
                            min_child_weight = space['min_child_weight'],    #minimum sum of instance weight needed in child
                            colsample_bytree = space['colsample_bytree'],    #ratio of column sampling for each tree
                            nthread = -1)                                    #number of parallel threads used

    evaluation = [(X_train, Y_train), (X_val, Y_val)]

    clf.fit(X_train, Y_train,
            eval_set = evaluation,
            early_stopping_rounds = 10,
            verbose = False)

    pred = clf.predict(X_val)
    pred = [1 if i>= 0.5 else 0 for i in pred]
    f1 = f1_score(Y_val, pred)
    print ("SCORE:", f1)
    return {'loss': -f1, 'status': STATUS_OK }

# In[72]:

# run the hyper paramter tuning
trials = Trials()
best = fmin(fn = hyperparameter_tuning,
            space = space,
            algo = tpe.suggest,
            max_evals = 100,
            trials = trials)

print (best)

# In[73]:

#plotting feature space and f1-scores for the different trials
parameters = space.keys()
cols = len(parameters)

f, axes = plt.subplots(nrows=1, ncols=cols, figsize=(20,5))
cmap = plt.cm.jet
for i, val in enumerate(parameters):
    xs = np.array([t['misc']['vals'][val] for t in trials.trials]).ravel()
    ys = [-t['result']['loss'] for t in trials.trials]
    xs, ys = zip(*sorted(zip(xs, ys)))
    axes[i].scatter(xs, ys, s=20, linewidth=0.01, alpha=0.25, c=cmap(float(i)/len(parameters)))
    axes[i].set_title(val)
    axes[i].grid()

# In[74]:

#printing best model parameters
print(best)

# In[75]:

#initializing XGBoost Classifier with best model parameters
best_clf = xgb.XGBClassifier(n_estimators = int(best['n_estimators']),
                            eta = best['eta'],
                            max_depth = int(best['max_depth']),
                            gamma = best['gamma'],
                            reg_alpha = int(best['reg_alpha']),
                            min_child_weight = best['min_child_weight'],
                            colsample_bytree = best['colsample_bytree'],
                            nthread = -1)

# In[76]:

#fitting XGBoost Classifier with best model parameters to training data
best_clf.fit(X_train, Y_train)

# In[77]:

#using the model to predict on the test set
Y_pred = best_clf.predict(X_test)

# In[78]:

#printing f1 score of test set predictions
print('The f1-score on the test data is: {0:.2f}'.format(f1_score(Y_test, Y_pred)))

# In[79]:

#creating a confusion matrix and labels
cm = confusion_matrix(Y_test, Y_pred)
labels = ['label-0', 'label-1']

# In[80]:

#plotting the confusion matrix
sns.heatmap(cm, annot = True, xticklabels = labels, yticklabels = labels, fmt = 'd')
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title('Confusion Matrix')

# In[81]:

#printing classification report
print(classification_report(Y_test, Y_pred))

# In[82]:

Y_score = best_clf.predict_proba(X_test)[:, 1]
average_precision = average_precision_score(Y_test, Y_score)
fig = plot_precision_recall_curve(best_clf, X_test, Y_test)
fig.ax_.set_title('Precision-Recall Curve: AP={0:.2f}'.format(average_precision))


import tkinter as tk
from tkinter import messagebox
import customtkinter
import gens.mutants as mtn
import gens.scientists as sts
import gens.stalkers as stk
import os

mut = mtn.mutantGen()
sci = sts.scientistGen()
stl = stk.stalkerGen()

class myGUI:

    def __init__(self):

        customtkinter.set_appearance_mode("system")
        customtkinter.set_default_color_theme("dark-blue")

        self.root = customtkinter.CTk()
        self.root.geometry("800x500")
        self.root.title("PAFLGen Enhanced")

        self.label = customtkinter.CTkLabel(self.root, text="Welcome to PAFLGen Enhanced! Have fun!", font=('Arial', 16), wraplength=500, justify="center")
        self.label.pack(padx=10, pady=10)

        self.masterLabel = customtkinter.CTkLabel(self.root, text="", font=('Arial', 16), wraplength=500, justify="center")

        self.savedLabel = customtkinter.CTkLabel(self.root, text="", font=('Arial', 16), justify="center")

        self.savedEntryLabel = customtkinter.CTkLabel(self.root, text="Type your filename below, then hit save!", font=('Arial', 16), justify="center")
        self.savedEntryTip = customtkinter.CTkLabel(self.root, text="Note: Files will be saved to /generated/ as a .txt file!\nPutting PAFLGen Enhanced in its own folder is recommended.", font=('Arial', 10), justify="center")
        self.savedEntry = customtkinter.CTkEntry(self.root, font=('Arial', 12))

        def mutantPress():
            mut = mtn.mutantGen()
            self.masterLabel.configure(text=mut)
            self.masterLabel.update()

        def stalkerPress():
            stl = stk.stalkerGen()
            self.masterLabel.configure(text=stl)
            self.masterLabel.update()

        def scientistPress():
            sci = sts.scientistGen()
            self.masterLabel.configure(text=sci)
            self.masterLabel.update()

        #def printMutant():
            #self.masterLabel.configure(text=mut)

        def saveToFile():
            #ocCounter = 0
            #currentDirectory = os.getcwd()
            currentDirectory = os.path.dirname(__file__)
            relDirectory = "generated"
            filePath = os.path.join(currentDirectory, relDirectory)
            if not os.path.exists(filePath):
                os.makedirs(filePath)
            #while os.path.exists(filePath + "/PAFL%s.txt" % ocCounter):
                #ocCounter += 1
            self.masterLabel.update()
            self.lastOC = self.masterLabel.cget("text")
            strLastOC = str(self.lastOC)
            #print(strLastOC)

            ocFile = self.savedEntry.get()
            with open(filePath + "/%s" % ocFile + '.txt', 'w', encoding='utf-8') as f:
                f.write(strLastOC)
                f.close

            self.savedLabel.configure(text="Saved %s" % ocFile + '.txt!')
            #ocFile = open(filePath + "/PAFL%s.txt" % ocCounter, "w")
            #ocFile.write(strLastOC)
            #ocFile.close
            #self.savedLabel.configure(text="Saved PAFL%s.txt!" % ocCounter)

        '''def saveToFile():
            ocCounter = 0
            #currentDirectory = os.getcwd()
            currentDirectory = os.path.dirname(__file__)
            relDirectory = "generated"
            filePath = os.path.join(currentDirectory, relDirectory)
            if not os.path.exists(filePath):
                os.makedirs(filePath)
            while os.path.exists(filePath + "/PAFL%s.txt" % ocCounter):
                ocCounter += 1
            self.masterLabel.update()
            self.lastOC = self.masterLabel.cget("text")
            strLastOC = str(self.lastOC)
            #print(strLastOC)
            ocFile = open(filePath + "/PAFL%s.txt" % ocCounter, "w")
            ocFile.write(strLastOC)
            ocFile.close
            self.savedLabel.configure(text="Saved PAFL%s.txt!" % ocCounter)'''

        #self.masterLabelVariable = customtkinter.StringVar()
        self.masterLabel = customtkinter.CTkLabel(self.root, text="", font=('Arial', 16), wraplength=750, justify="center")

        self.buttonFrame = customtkinter.CTkFrame(self.root)
        self.buttonFrame.columnconfigure(0, weight=1)
        self.buttonFrame.columnconfigure(1, weight=1)
        self.buttonFrame.columnconfigure(2, weight=1)

        # self.stalkerLabel = customtkinter.CTkLabel(self.root, text="x", font=('Arial', 12))
        self.stalkerButton = customtkinter.CTkButton(self.buttonFrame, text="Stalkers", font=('Arial', 12), command=stalkerPress)
        self.stalkerButton.grid(row=0, column=0, padx=5, sticky=tk.W+tk.E)

        self.mutantButton = customtkinter.CTkButton(self.buttonFrame, text="Mutants", font=('Arial', 12), command=mutantPress)
        self.mutantButton.grid(row=0, column=1, padx=5, sticky=tk.W+tk.E)

        self.scientistButton = customtkinter.CTkButton(self.buttonFrame, text="Scientists", font=('Arial', 12), command=scientistPress)
        self.scientistButton.grid(row=0, column=2, padx=5, sticky=tk.W+tk.E)

        #self.saveButton = customtkinter.CTkButton(self.buttonFrame, text="Save!", font=('Arial', 12), command=saveToFile)
        self.saveButton = customtkinter.CTkButton(self.buttonFrame, text="Save!", font=('Arial', 12), command=saveToFile)
        self.saveButton.grid(row=1, column=1, padx=5, pady=10, sticky=tk.W+tk.E)

        self.buttonFrame.pack(fill='x')
        self.savedEntryLabel.pack(padx=10, pady=5)
        self.savedEntryTip.pack(padx=10)
        self.savedEntry.pack(padx=10, pady=5)
        self.masterLabel.pack(padx=10, pady=10)

        self.savedLabel.pack(pady=10)

        '''self.anotherBtn = tk.Button(self.root, text="TEST")
        self.anotherBtn.place(x=200, y=200, height=100, width=100)'''

        self.root.protocol("WM_DELETE_WINDOW", self.on_closing)
        self.root.mainloop()
        #self.root.destroy()

    def on_closing(self):
        if messagebox.askyesno(title="Quit?", message="Do you really want to quit? Your progress will be lost."):
            self.root.destroy()

myGUI()


import os
from pathlib import Path

from bento.extra.requests import RequestsTool
from bento.violation import Violation
from tests.test_tool import context_for

THIS_PATH = Path(os.path.dirname(__file__))
BASE_PATH = THIS_PATH / ".." / ".." / ".."
SIMPLE_INTEGRATION_PATH = BASE_PATH / "tests/integration/simple"
SIMPLE_TARGETS = [
    SIMPLE_INTEGRATION_PATH / "bar.py",
    SIMPLE_INTEGRATION_PATH / "foo.py",
    SIMPLE_INTEGRATION_PATH / "init.js",
    SIMPLE_INTEGRATION_PATH / "package-lock.json",
    SIMPLE_INTEGRATION_PATH / "package.json",
]

def test_run_no_base_violations(tmp_path: Path) -> None:
    tool = RequestsTool(
        context_for(tmp_path, RequestsTool.TOOL_ID, SIMPLE_INTEGRATION_PATH)
    )
    tool.setup()
    violations = tool.results(SIMPLE_TARGETS)

    assert not violations

def test_run_flask_violations(tmp_path: Path) -> None:
    base_path = BASE_PATH / "tests/integration/requests"
    tool = RequestsTool(context_for(tmp_path, RequestsTool.TOOL_ID, base_path))
    tool.setup()
    violations = tool.results([base_path / "bad.py"])

    expectation = [
        Violation(
            tool_id="r2c.requests",
            check_id="use-timeout",
            path="bad.py",
            line=3,
            column=5,
            message="requests will hang forever without a timeout. Consider adding a timeout (recommended 10 sec).",
            severity=2,
            syntactic_context="r = requests.get('http://MYURL.com', auth=('user', 'pass'))",
            filtered=None,
            link="https://bento.dev/checks/en/latest/flake8-requests/use-timeout",
        ),
        Violation(
            tool_id="r2c.requests",
            check_id="no-auth-over-http",
            path="bad.py",
            line=3,
            column=5,
            message="auth is possibly used over http://, which could expose credentials. possible_urls: ['http://MYURL.com']",
            severity=2,
            syntactic_context="r = requests.get('http://MYURL.com', auth=('user', 'pass'))",
            filtered=None,
            link="https://bento.dev/checks/en/latest/flake8-requests/no-auth-over-http",
        ),
    ]

    violations_important_info = set(
        map(
            lambda viol: (viol.tool_id, viol.check_id, viol.line, viol.column),
            violations,
        )
    )
    expectation_important_info = set(
        map(
            lambda viol: (viol.tool_id, viol.check_id, viol.line, viol.column),
            expectation,
        )
    )
    assert violations_important_info == expectation_important_info


"""Позитивный тест"""
import allure
import pytest

@pytest.mark.usefixtures('clear_test_reports_and_logs')
@allure.severity('CRITICAL')
@allure.feature('Positive keys')
@allure.epic('API Тестирование портала "https://reqres.in/"')
@pytest.mark.parametrize('id_user',
                         ['20',
                          '32'])
@allure.title('Удаление пользователя')
def test_delete_user_v1(base, id_user):

    response = base.api_v1.delete_api(id_user=id_user)
    base.asserts.assert_empty_body(response=response, exp_status_code=204)


import contextlib, itertools, json, os, shutil, subprocess, sys, tempfile, time

import numpy as np
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer

@contextlib.contextmanager
def named_pipe():
    dirname = tempfile.mkdtemp()
    try:
        path = os.path.join(dirname, 'fifo')
        os.mkfifo(path)
        yield path
    finally:
        shutil.rmtree(dirname)

def vectorize_corpus(X_train, Y_train, X_test):
    vectorizer = CountVectorizer(stop_words='english', min_df=1,
                                 ngram_range=(1,2), max_features=2**16)
    X_train = vectorizer.fit_transform(X_train)
    X_test = vectorizer.transform(X_test)

    tfidf = TfidfTransformer(use_idf=False)
    X_train = tfidf.fit_transform(X_train, np.transpose(np.array(Y_train)))
    X_test = tfidf.transform(X_test)

    return X_train.toarray(), X_test.toarray()

def output_reviews(X, Y, prints_num_lines=False, prints_num_elems=False,
                   stdout=sys.stdout):
    if prints_num_lines:
        stdout.write(str(len(X)) + '\n')
    if prints_num_elems:
        stdout.write(str(len(X) + np.count_nonzero(X)) + '\n')

    for label, review in itertools.izip(Y, X):
        line = str(label)
        for j in review.nonzero()[0]:
            line += ' {0}:{1}'.format(j + 1, review[j])
        stdout.write(line + '\n')

def main(training_infile, test_infile):
    X_train = []
    Y_train = []
    X_test = []
    Y_test = []

    with open(training_infile, 'r') as f:
        for line in f:
            review = json.loads(line)
            X_train.append(review['text'])
            Y_train.append(review['stars'])
    with open(test_infile, 'r') as f:
        for line in f:
            review = json.loads(line)
            X_test.append(review['text'])
            Y_test.append(review['stars'])

    t0 = time.time()
    X_train, X_test = vectorize_corpus(X_train, Y_train, X_test)
    print('\bdone with featurizing in {0} seconds\n'.format(time.time() - t0))

    proc_train = subprocess.Popen(['./libsvm-3.20/svm-train',
                                   '-c', '32', '-g', '0.03125',
                                   '/dev/null'],
                                  stdin=subprocess.PIPE,
                                  stdout=subprocess.PIPE)
    t1 = time.time()
    output_reviews(X_train, Y_train,
                   prints_num_lines=True, prints_num_elems=True,
                   stdout=proc_train.stdin)
    t = time.time() - t1
    proc_train.stdin.close()

    with named_pipe() as test_path, named_pipe() as model_path:
        proc_predict = subprocess.Popen(['./libsvm-3.20/svm-predict',
                                         test_path,  # test file
                                         model_path, # model file
                                         'predict.out'])

        t1 = time.time()
        with open(model_path, 'w+') as model_pipe:
            for line in proc_train.stdout:
                model_pipe.write(line)
        with open(test_path, 'w+') as test_pipe:
            output_reviews(X_test, Y_test,
                           prints_num_lines=True, prints_num_elems=False,
                           stdout=test_pipe)
        print('\bdone with IO in {0} seconds\n'.format(time.time() - t1 + t))

    print('\nall done in {0} seconds\n'.format(time.time() - t0))

if __name__ == "__main__":
    if len(sys.argv) != 3:
        print("Usage: featurize <training-input> <test-input>")
        exit(1)
    main(*sys.argv[1:])


from collections import Counter

import gym
import numpy as np
import pytest

from rl_agents.agents import ExpectedSarsaAgent, QLearningAgent, SarsaAgent
from rl_agents.agents.functions import QMatrixFunction, QTableFunction
from rl_agents.agents.policies import (
    BoltzmanPolicy,
    EDecreasePolicy,
    EGreedyPolicy,
)
from rl_agents.runners import simple_tab_runner

def gen_list():
    pis = [
        EGreedyPolicy(0.1),
        BoltzmanPolicy(10),
        EDecreasePolicy(0.9, 0.1, 0.9),
    ]
    funcs = [QMatrixFunction, QTableFunction]
    ags = [QLearningAgent, SarsaAgent, ExpectedSarsaAgent]
    mets = ["zeros", "ones", "random"]
    return [
        (v1, v2, v3, v4)
        for v1 in ags
        for v2 in pis
        for v3 in funcs
        for v4 in mets
    ]

@pytest.mark.parametrize("AgentC, policy, FunctionC, method", gen_list())
def test_frozen_lake(AgentC, policy, FunctionC, method):
    env = gym.make("FrozenLake-v0")
    q_func_kwargs = {"method": method}
    agent = AgentC(
        n_states=env.nS,
        n_actions=env.nA,
        alpha=0.4,
        gamma=0.5,
        policy=policy,
        q_function=FunctionC,
        q_func_kwargs=q_func_kwargs,
    )
    _ = simple_tab_runner(env, agent, 200)

@pytest.mark.parametrize("epsilon", [0.1, 0.5, 0.9])
def test_egreedy_policy(epsilon):
    n_tests = 10000
    # Best Q value is the index 1
    q_values = np.array([1, 5, 2, 1, 3])
    # Test valid values:
    policy = EGreedyPolicy(epsilon)
    counter = Counter([policy(q_values) for _ in range(n_tests)])
    # Test max value:
    assert counter.most_common(1)[0][0] == 1
    # Test Counting is approx epsilon:
    counts_max = counter.most_common()[0][1]
    counts_min = counter.most_common()[-1][1]
    assert counts_max > (1 - epsilon) * n_tests
    assert counts_min == pytest.approx(epsilon * n_tests / 5, rel=100)
    # Test get_values method:
    assert type(policy.get_values(q_values)) == type(q_values)
    assert len(policy.get_values(q_values)) == len(q_values)

@pytest.mark.parametrize(
    "epsilon, epsilon_min, decay", [(0.9, 0.5, 0.9), (0.5, 0.1, 0.8)]
)
def test_edecrease_policy(epsilon, epsilon_min, decay):
    n_tests = 10000
    # Best Q value is the index 1
    q_values = np.array([1, 5, 2, 1, 3])
    # Test valid values:
    policy = EDecreasePolicy(epsilon, epsilon_min, decay)
    ls = []
    for _ in range(n_tests):
        ls.append(policy(q_values))
        policy.update()
    counter = Counter(ls)
    # Test max value:
    assert counter.most_common(1)[0][0] == 1
    assert policy.epsilon == epsilon_min

@pytest.mark.parametrize("temperature", [10, 5, 1, 0.8])
def test_boltzman_policy(temperature):
    n_tests = 10000
    # Best Q value is the index 1
    q_values = np.array([1, 5, 2, 1.5, 3])
    true_order = [1, 4, 2, 3, 0]
    # Test valid values:
    policy = BoltzmanPolicy(temperature)
    counter = Counter([policy(q_values) for _ in range(n_tests)])
    print(counter.most_common())
    print(policy.get_values(q_values))
    # Test max value:
    assert counter.most_common(1)[0][0] == 1
    # Test Order:
    counter_order = [n[0] for n in counter.most_common()]
    assert counter_order == true_order
    # Test get_values method:
    assert type(policy.get_values(q_values)) == type(q_values)
    assert len(policy.get_values(q_values)) == len(q_values)

def test_policies_errors():
    with pytest.raises(ValueError):
        EGreedyPolicy(-0.1)
    with pytest.raises(ValueError):
        EGreedyPolicy(1.1)
    with pytest.raises(ValueError):
        EDecreasePolicy(1.1, 0.1, 0.9)
    with pytest.raises(ValueError):
        EDecreasePolicy(-0.2, 0.2, 0.8)
    with pytest.raises(ValueError):
        BoltzmanPolicy(-500)

def test_functions_errors():
    with pytest.raises(ValueError):
        QMatrixFunction(2, 2, "blabla")
    with pytest.raises(ValueError):
        QTableFunction(2, 2, "blablo")

def test_functions():
    # Test Initialization:
    q_func0 = QMatrixFunction(2, 2, "zeros")
    q_func1 = QMatrixFunction(2, 2, "ones")
    q_funcR = QMatrixFunction(2, 2, "random")
    assert q_func0(0, 0) == 0
    assert q_func1(0, 0) == 1
    assert q_funcR(0, 0) != q_funcR(0, 1)
    # Test update:
    q_value = 5.5
    q_func0.update(0, 0, q_value)
    q_func1.update(0, 0, q_value)
    q_funcR.update(0, 0, q_value)
    assert q_func0(0, 0) == 5.5
    assert q_func1(0, 0) == 5.5
    assert q_funcR(0, 0) == 5.5


###
# A segment is a route from start_location to end_location.  It can have:
#  - a start date
#  - a duration (drive time)
#  - associated GeoJSON
#  - an ID (numeric, consecutive, filename)
###
import os
import requests
import polyline
import json
from dataclasses import dataclass, asdict, field
from datetime import date

SEGMENT_DIR = './segments'

@dataclass
class Segment():
    start_loc: str
    end_loc: str
    start_date: date = None
    seg_id: int = None
    features: list = field(default_factory=list)
    avoid: str = 'highways'  # format multiples as "highways|ferries|tolls"
    # seg_hash: Optional[str]

    MAX_SEGMENT_ID = 0  # class attribute

    def save(self):
        # Save this segment as a file
        # The s_id should be the next consecutive segment_id
        # Somehow, this should be atomic...
        Segment.MAX_SEGMENT_ID += 1
        self.seg_id = Segment.MAX_SEGMENT_ID

        segment = asdict(self)
        fname = self.get_fname(self.seg_id)
        print(f"Saving {fname}: {self.start_loc} to {self.end_loc}")
        with open(fname, 'w') as out:
            json.dump(segment, out)

    def get_route(self, custom_label=None):
        payload = {'origin': self.start_loc,
                   'destination': self.end_loc,
                   'avoid': self.avoid,
                   'key': os.environ['FIREBASE_APIKEY']}
        request_url = 'https://maps.googleapis.com/maps/api/directions/json'
        try:
            r = requests.get(request_url, params=payload)
            r.raise_for_status()  # throw errors on HTTP errors
            results = r.json()
            route = results['routes'][0]['overview_polyline']['points']
            coords = polyline.decode(route)
        except Exception as e:
            print("Error querying for directions for {0} TO {1} -- {2}"
                  .format(self.start_loc, self.end_loc, str(e)))
            print("Requests error: {0}".format(r.text))
            raise e
            return

        # reverse order to comply with geojson spec
        coords_list = [[lon, lat] for lat, lon in coords]

        default_name = "{0} to {1}".format(self.start_loc, self.end_loc)

        self.features.append({
            "type": "Feature",
            "properties": {
                "name": custom_label or default_name
            },
            "geometry": {
                "type": "MultiLineString",
                "coordinates": [coords_list]
            }
        })
        return [[lat, lon] for lat, lon in coords]

    @classmethod
    def get_fname(cls, s_id):
        return os.path.join(SEGMENT_DIR, f"{s_id:0>7}")

    @classmethod
    def load_by_id(cls, s_id):
        if int(s_id) > cls.MAX_SEGMENT_ID:
            cls.MAX_SEGMENT_ID = int(s_id)
        fname = cls.get_fname(s_id)
        return cls.load_by_path(fname)

    @classmethod
    def load_by_path(cls, fname):
        with open(fname, 'r') as infile:
            seg_data = json.load(infile)
        return cls(**seg_data)

    @classmethod
    def load_all(cls):
        segments = []
        for seg in os.listdir(SEGMENT_DIR):
            segments.append(cls.load_by_id(seg))
        return segments

    @classmethod
    def cleanup(cls):
        for seg in os.listdir(SEGMENT_DIR):
            print(f"Checking segment {seg}")
            fname = cls.get_fname(seg)
            with open(fname, 'r') as infile:
                d = json.load(infile)
            if 'seg_hash' in d.keys():
                print("found hash")
                del(d['seg_hash'])
            else:
                print("no hash")
            segment = asdict(cls(**d))
            with open(fname, 'w') as out:
                json.dump(segment, out)

if __name__ == '__main__':
    Segment.cleanup()


import csv
import json
import os
from datetime import datetime

import pandas as pd

datetime_format = '%Y-%m-%d %H:%M:%S'
# datetime_format = '%d-%m-%Y %H:%M:%S'
threshold = 900  # in seconds

def find_csv_breaking_points(filename, datetime_format, threshold):
    breaking_points = []
    with open(filename, 'r') as csvfile:
        reader = csv.reader(csvfile)
        header = next(reader)  # read the header row
        data = sorted(reader, key=lambda x: datetime.strptime(x[0], datetime_format))  # sort the data by datetime column

        prev_time = None
        for i, row in enumerate(data):
            current_time = datetime.strptime(row[0], datetime_format)
            if prev_time is not None:
                if abs(((current_time - prev_time).total_seconds())) > threshold:
                    breaking_points.append(i)
            prev_time = current_time
    return breaking_points

def json_to_csv(json_file_path):
    print("Converting JSON to CSV...")
    # Load the JSON data to dataframes
    with open(json_file_path) as json_file:
        data = json.load(json_file)

    print("Processing data...")
    # data to dataframe
    df = pd.DataFrame(data)
    # transpose the dataframe
    df = df.transpose()

    # save to csv
    df.to_csv("data.csv", header=False)

    # read the csv file
    df = pd.read_csv("data.csv", header=None)

    # rename the columns
    df = df.rename(columns={0: 'ds', 1: 'y', 2: 'time', 3: 'weekday', 4: 'holiday'})

    # keep only first two columns
    df = df.iloc[:, :2]

    # ds to datetime type
    # df["ds"] = pd.to_datetime(df["ds"])
    # df["ds"] = pd.to_datetime(df["ds"]), format("%d.%m.%Y %H:%M:%S")
    df["ds"] = pd.to_datetime(df["ds"], format="%d.%m.%Y %H:%M:%S")
    print("Sorting data...")
    # sort the dataframe by ds
    df = df.sort_values(by=['ds'])

    # reset the index
    df = df.reset_index(drop=True)

    # Create a new DataFrame with a datetime range that spans the entire time period
    # df start date
    start_date = df['ds'].min()
    end_date = df['ds'].max()

    # create a new dataframe from start date to end date with 15 minutes interval
    new_df = pd.DataFrame(pd.date_range(start_date, end_date, freq='15min'), columns=['ds'])

    # Fill new_df values from df
    new_df = new_df.merge(df, on='ds', how='left')

    # Fill missing sales values with zeros no decimal
    new_df['y'] = new_df['y'].fillna(0).astype(int)

    # save the dataframe as a csv file
    new_df.to_csv("Filled.csv", index=False)

    # create results folder
    if not os.path.exists("data"):
        os.makedirs("data")

    folder_name = "data-" + str(pd.to_datetime('today').strftime("%Y%m%d-%H%M%S"))

    # create a folder for the results
    if not os.path.exists("data/" + folder_name):
        os.mkdir("data/" + folder_name)

    # save the dataframe as a csv file
    new_df.to_csv("data/" + folder_name + "/processed_data.csv", index=False)
    new_df.to_csv("data/processed_data.csv", index=False)

    bp_indices = find_csv_breaking_points("data/" + folder_name + "/processed_data.csv", datetime_format, threshold)
    if len(bp_indices) == 0:
        print("\nThe dataset is continuous")
    else:
        print("\nThe dataset has", len(bp_indices), "breaking points at indices:", bp_indices)

    return folder_name + "/processed_data.csv"


import shutil
import traceback
from time import time
from typing import *
import os

from lib.tools.get_accounts import get_user_token
from lib.vk_html.black_list import black_list_html
from lib.vk_html.conversations_list import conversations_list_html
from lib.vk_html.docs import docs_html
from lib.vk_html.friends import friends_html
from lib.vk_html.gifts import gifts_html
from lib.vk_html.groups import groups_html
from lib.vk_html.index import index_html
from lib.vk_html.info import page_info_html
from lib.vk_html.photos import photos_html
from lib.vk_html.stories import stories_html
from lib.vk_html.videos import videos_html

from lib.controller.response import Response
from lib.controller.account import Account

class Main:
    def __init__(self):
        self.account = None
        if not os.path.exists(r'tokens'):
            os.mkdir(r'tokens')
        if not os.path.exists(r'users'):
            os.mkdir(r'users')

    def load_accounts(self) -> None:
        token = get_user_token()
        self.account = Account(token)

    def load_data(self, account: Account) -> Iterator[Response]:
        try:
            t = time()
            token = account.token
            name = account.name

            yield Response('Загрузка пользователя (%s)' % name)

            if os.path.exists(r'users/%s' % name):
                shutil.rmtree(r'users/%s' % name)

            os.mkdir(r'users/%s' % name)
            os.mkdir(r'users/%s/%s' % (name, name))
            os.mkdir(r'users/%s/%s/html' % (name, name))
            os.mkdir(r'users/%s/%s/html/messages' % (name, name))
            shutil.copy('files_to_copy/favicon.ico', 'users/%s/%s/html/favicon.ico' % (name, name))
            shutil.copy('files_to_copy/style.css', 'users/%s/%s/html/style.css' % (name, name))

            index_html(name, token)
            page_info_html(name, token)
            yield Response('    Загрузка фотографий')
            photos_html(name, token)
            yield Response('    Загрузка видеозаписей')
            videos_html(name, token)
            yield Response('    Загрузка друзей и друзей друзей')
            for x in friends_html(name, token):
                yield Response(x, response_type=Response.Type.PERCENT)
            yield Response('    Загрузка групп')
            groups_html(name, token)
            yield Response('    Загрузка чёрного списка')
            black_list_html(name, token)
            yield Response('    Загрузка документов')
            docs_html(name, token)
            yield Response('    Загрузка подарков')
            gifts_html(name, token)
            yield Response('    Загрузка историй')
            stories_html(name, token)
            yield Response('    Загрузка сообщений')
            for x in conversations_list_html(name, token):
                yield Response(x, response_type=Response.Type.PERCENT)

            yield Response('    Загрузка завершена', response_type=Response.Type.SEPARATOR)

            delta = time() - t
            yield Response('Выполнено за %.2f сек\n' % delta)
        except:
            yield Response(traceback.format_exc(), response_type=Response.Type.ERROR)