# -*- coding: utf-8 -*-

__author__ = 'Bruce Frank Wong'

from typing import Dict, List
from datetime import datetime
import math

from . import QWDirection, QWOffset, QWOrderStatus

class QWPosition(object):
    order_id: str
    fill_datetime: datetime
    price: float
    lots: int
    direction: QWDirection

class QWPositionManager(object):
    _capital_available: float
    _price_per_lot: float
    _position_list: List[QWPosition]

    def __init__(self, capital_available: float, price_per_lot: float) -> None:
        self._capital_available = capital_available
        self._price_per_lot = price_per_lot
        self._position_list = []

    def add(self, position: QWPosition) -> None:
        self._position_list.append(position)

    def remove(self, position: QWPosition) -> None:
        self._position_list.remove(position)

    def remove_by_order_id(self, order_id: str) -> None:
        position: QWPosition
        for position in self._position_list:
            if position.order_id == order_id:
                self._position_list.remove(position)
                break

    @property
    def max_lots(self) -> int:
        return math.floor(self._capital_available / self._price_per_lot)

    @property
    def available_lots(self) -> int:
        return self.max_lots - self.total_lots

    @property
    def total_lots(self) -> int:
        return self.total_lots_for_buy + self.total_lots_for_sell

    @property
    def total_lots_for_sell(self) -> int:
        lots: int = 0
        position: QWPosition
        for position in self._position_list:
            if position.direction == QWDirection.Sell:
                lots += position.lots
        return lots

    @property
    def total_lots_for_buy(self) -> int:
        lots: int = 0
        position: QWPosition
        for position in self._position_list:
            if position.direction == QWDirection.Buy:
                lots += position.lots
        return lots

    def lots_at_price(self, price: float) -> int:
        lots: int = 0
        position: QWPosition
        for position in self._position_list:
            if position.price == price:
                lots += position.lots
        return lots

    def position_at_price(self, price: float) -> List[QWPosition]:
        result: List[QWPosition] = []
        position: QWPosition
        for position in self._position_list:
            if position.price == price:
                result.append(position)
        return result

    def position_at_price_for_buy(self, price: float) -> List[QWPosition]:
        result: List[QWPosition] = []
        position: QWPosition
        for position in self._position_list:
            if position.price == price and position.direction == QWDirection.Long:
                result.append(position)
        return result

    def position_at_price_for_sell(self, price: float) -> List[QWPosition]:
        result: List[QWPosition] = []
        position: QWPosition
        for position in self._position_list:
            if position.price == price and position.direction == QWDirection.Short:
                result.append(position)
        return result


#!/usr/bin/env python
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
from builtins import str
from builtins import range

import os, datetime, sys

import nipype.interfaces.io as nio           # Data i/o
import nipype.interfaces.fsl as fsl          # fsl
import nipype.interfaces.utility as util     # utility
import nipype.pipeline.engine as pe          # pypeline engine
import nipype.algorithms.modelgen as model   # model generation

from nipype.interfaces.nipy.preprocess import Trim # Trim leading and trailing volumes

import utils # code by AM specific to this project but multiple workflows

fsl.FSLCommand.set_default_output_type('NIFTI_GZ')

#Set up model fitting workflow (we assume data has been preprocessed with fmriprep)
modelfit = pe.Workflow(name='modelfit')

#Custom interface wrapping function Tsv2subjectinfo
tsv2subjinfo = pe.MapNode(util.Function(function=utils.tsv2subjectinfo, input_names=['events_file', 'exclude', 'confounds_file', 'trim_indices'],
                         output_names=['subject_info']), name="tsv2subjinfo", iterfield=['events_file', 'confounds_file'])
modelspec = pe.MapNode(interface=model.SpecifyModel(), name="modelspec", iterfield=['subject_info'])
level1design = pe.MapNode(interface=fsl.Level1Design(), name="level1design", iterfield=['session_info'])
modelgen = pe.MapNode(interface=fsl.FEATModel(), name='modelgen', iterfield=["fsf_file", "ev_files"])

trim = pe.MapNode(interface=Trim(), name="trim", iterfield=['in_file'])
applymask = pe.MapNode(interface=fsl.ApplyMask(), name="applymask", iterfield=["in_file", "mask_file"])

modelestimate = pe.MapNode(interface=fsl.FILMGLS(), name='modelestimate',
                        iterfield=['design_file', 'in_file', 'tcon_file'])

# combine copes, varcopes, and masks across multiple sessions
copemerge = pe.MapNode(interface=fsl.Merge(dimension='t'), iterfield=['in_files'], name="copemerge")
varcopemerge = pe.MapNode(interface=fsl.Merge(dimension='t'), iterfield=['in_files'], name="varcopemerge")
maskemerge = pe.MapNode(interface=fsl.Merge(dimension='t'), iterfield=['in_files'], name="maskemerge")

# set up and estimate fixed-effects cross-session analysis
level2model = pe.Node(interface=fsl.L2Model(), name='l2model')
flameo = pe.MapNode(interface=fsl.FLAMEO(run_mode='fe'), name="flameo", iterfield=['cope_file', 'var_cope_file'])

modelfit.connect([
    (tsv2subjinfo, modelspec, [('subject_info', 'subject_info')]),
    (trim, modelspec, [('out_file', 'functional_runs')]),
    (modelspec, level1design, [('session_info', 'session_info')]),
    (level1design, modelgen, [('fsf_files', 'fsf_file'),
                              ('ev_files', 'ev_files')]),
    (modelgen, modelestimate, [('design_file', 'design_file'),
                              ('con_file','tcon_file')]),
    (trim, applymask, [('out_file', 'in_file')]),
    (applymask, modelestimate, [('out_file', 'in_file')]),
    (modelestimate, copemerge, [(('copes', utils.sort_copes), 'in_files')]),
    (modelestimate, varcopemerge, [(('varcopes', utils.sort_copes), 'in_files')]),
    (modelestimate, level2model, [(('copes', utils.num_copes), 'num_copes')]),
    (copemerge, flameo, [('merged_file', 'cope_file')]),
    (varcopemerge, flameo, [('merged_file', 'var_cope_file')]),
    (level2model, flameo, [('design_mat', 'design_file'),
                           ('design_con', 't_con_file'),
                           ('design_grp', 'cov_split_file')]),
    (maskemerge, flameo, [(('merged_file', utils.pickfirst),'mask_file')])
])

# Data input and configuration!
BIDSDataGrabber = pe.Node(util.Function(function=utils.get_files,
      input_names=["subject_id", "session", "task", "raw_data_dir", "preprocessed_data_dir", "space", "run"],
      output_names=["bolds", "masks", "events", "TR", "confounds"]),
      name="BIDSDataGrabber")

# What event/trial types, if any, to exclude
modelfit.inputs.tsv2subjinfo.exclude = None

modelfit.inputs.modelspec.input_units = 'secs'
modelfit.inputs.modelspec.high_pass_filter_cutoff = 128.

modelfit.inputs.level1design.bases = {'dgamma': {'derivs': False}}
modelfit.inputs.level1design.model_serial_correlations = True

modelfit.inputs.modelestimate.smooth_autocorr = True
modelfit.inputs.modelestimate.mask_size = 5
modelfit.inputs.modelestimate.threshold = 0 # 0 is nipype default, setting until intensity normalization is decided

hemi_wf = pe.Workflow(name="fixedeffects")

# output
datasink = pe.Node(nio.DataSink(), name='datasink')

modelfit.connect([
  (modelgen, datasink, [('design_image', 'design_image'), ('design_file', 'design_file')]),
  (modelestimate, datasink, [('results_dir', 'results_dir')]),
  (applymask, datasink, [('out_file', 'epi_masked_trimmed')]),
  (flameo, datasink, [('stats_dir', 'stats_dir')])
])

hemi_wf.connect([
                    (BIDSDataGrabber, modelfit, [('events', 'tsv2subjinfo.events_file'),
                                              ('confounds', 'tsv2subjinfo.confounds_file'),
                                              ('bolds', 'trim.in_file'),
                                              ('masks', 'applymask.mask_file'),
                                              ('masks', 'maskemerge.in_files'),
                                              ('TR', 'modelspec.time_repetition'),
                                              ('TR', 'level1design.interscan_interval')])
                    ])


from django.shortcuts import render, redirect
from .models import Note
# Create your views here.
def index(request):
    context = {
        'notes': Note.objects.all(),
        # 'new_post_form' : PostForm()
    }
    return render(request, 'notes/index.html', context)

def addNote(request):
    if request.method == "POST":
        Note.objects.newNote(request.POST['note'])
    context = {
    	'posts': Note.objects.all()
    }
    return render(request, 'notes/post_index.html', context)

def delnote(request,id):
    Note.objects.delNote(id)
    return redirect('/')


import cv2
import numpy as np
import glob
for img in glob.glob("*.png"):

    np.set_printoptions(threshold=np.inf)
    cv2.namedWindow("Tracking")

    image = cv2.imread(img)
    im_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    (thresh, im_bw) = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
    mesh = np.where(im_bw == 0)
    cords = list(zip(*mesh))
    x, y = map(list, zip(*cords))
    cropped = image[min(x):max(x),min(y):max(y)]

    frame=cropped

    im_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    (thresh, im_bw) = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

    im_bw=np.where(im_bw==0, 5, im_bw)
    im_bw=np.where(im_bw==255, 0, im_bw)
    im_bw=np.where(im_bw== 5,255, im_bw)

    res = cv2.bitwise_and(frame, frame, mask=im_bw)
    src=res
    tmp = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
    _,alpha = cv2.threshold(tmp,0,255,cv2.THRESH_BINARY)
    b, g, r = cv2.split(src)
    rgba = [b,g,r, alpha]
    dst = cv2.merge(rgba,4)

    cv2.imwrite("./Cropped/"+img, dst)

    cv2.imshow("frame", dst)
    cv2.imshow("mask", im_bw)
    #cv2.imshow("res", res)

    key = cv2.waitKey(1)


import numpy as np
import pytest
from scipy.fft import dct, idct

from pytracer.test.utils import trace

def main():
    N = 100
    t = np.linspace(0, 20, N, endpoint=False)
    x = np.exp(-t/3)*np.cos(2*t)
    y = dct(x, norm='ortho')
    window = np.zeros(N)
    window[:20] = 1
    yr = idct(y*window, norm='ortho')
    s = np.sum(np.abs(x-yr)**2) / np.sum(np.abs(x)**2)
    print(s)

    window = np.zeros(N)
    window[:15] = 1
    yr = idct(y*window, norm='ortho')
    s = np.sum(np.abs(x-yr)**2) / np.sum(np.abs(x)**2)
    print(s)

@pytest.mark.usefixtures("cleandir")
def test_trace_only(bash):
    trace(__file__, bash)

@pytest.mark.usefixtures("cleandir", "parse")
def test_trace_parse(nsamples, bash):
    for _ in range(nsamples):
        trace(__file__, bash)

if '__main__' == __name__:
    main()


from datetime import datetime

class Trade:
    def __init__(self, date, time, type, qty, strike, expiry, kind, exchange, tradeId, product, side=None):
        self.date = date
        self.time = time
        self.type = type
        self.qty = qty
        self.strike = strike
        self.expiry = expiry
        self.kind = kind
        self.exchange = exchange
        self.tradeId = tradeId
        self.product = product
        self.side = side

    def isWithinOneMinute(self, anotherTrade):
        t1 = datetime.strptime(self.time, "%H:%M:%S")
        t2 = datetime.strptime(anotherTrade.time, "%H:%M:%S")
        delta = t1 - t2
        return 0 <= delta.total_seconds() <= 60

    def isFrontRunner(self, anotherTrade):
        if self.type == anotherTrade.type:
            return False
        if not self.isWithinOneMinute(anotherTrade):
            return False
        if self.product != anotherTrade.product:
            return False
        if self.kind != anotherTrade.kind:
            return False
        if self.exchange == 'CBOE':
            if (self.qty < 0 and anotherTrade.qty >= 0) or (self.qty >= 0 and anotherTrade.qty < 0):
                return False
        else:
            if self.side != anotherTrade.side:
                return False
        if self.expiry != anotherTrade.expiry:
            return False
        if self.strike != anotherTrade.strike:
            return False

        # All conditions have been met for the front runner
        return True


import numpy as np
import torch
import torchvision.transforms as transforms
from torch.utils.data import Dataset, DataLoader
import os
from PIL import Image
import torchvision.transforms.functional as TF

class VideoDataset(Dataset):
    '''Video Dataset'''

    def __init__(self, path='./', data_type='world', nframes=10, patch_size=32, norm=True, transform=None):
        '''
        Params:
            path (str): path to frame folder
            data_type (str): either 'world' or 'head', or 'retinal'
            transform: type of transform
        '''

        if not data_type in ('ducks','world','head','retinal'):
            print(f'Unrecognized task: {data_type}, should be one of (world, head, or retinal)')
        self.data_type = data_type
        self.nframes = nframes
        self.patch_size = patch_size
        self.transform = transform
        self.norm=norm

        if data_type == 'ducks':
            self.path = path
        else:
            self.path = os.path.join(path, data_type)

        self.png_paths = [os.path.join(self.path,name) for name in os.listdir(self.path) if '.png' in name]
        self.range = len(self.png_paths) - nframes

        def meanstdnorm(tensor):
            mean = torch.mean(tensor)
            std = torch.std(tensor)
            tensor = (tensor - mean) / std
            return(tensor)

        def randcrop(tensor):
            _, _, xshape, yshape = tensor.shape
            randx = np.random.randint(0,xshape-self.patch_size)
            randy = np.random.randint(0,yshape-self.patch_size)
            tensor = tensor[:,:,randx:randx+self.patch_size,randy:randy+self.patch_size]
            return(tensor)

        self.normfunc = meanstdnorm
        self.cropfunc = randcrop

    def __len__(self):
        return self.range

    def __getitem__(self, idx):
        #print(self.png_paths[idx+1])
        #frames = []
        #for i in range(self.nframes):
        #    frame = Image.open(self.png_paths[idx+i])
        #    frames.append(TF.to_tensor(frame))
        frames = torch.stack([TF.to_tensor(Image.open(self.png_paths[idx+i])).squeeze(0) for i in range(self.nframes)]).unsqueeze(0)
        sample = {'frames': frames}
        sample['frames'] = self.cropfunc(sample['frames'])
        if self.norm:
            sample['frames'] = self.normfunc(sample['frames'])
        if self.transform:
            sample['frames'] = self.transform(sample['frames'])
        return(sample)


"""
This script, data_handling.py, is dedicated to handling data operations for the basic analytics part of the OrestisCompany analytics dashboard.
It primarily focuses on reading and processing CSV files that contain basic analytics data.

Functionality:
1. read_data_basic:
    Reads CSV files from the '/app/data/analytics/basic' directory.
    It offers optional sorting functionality based on specified columns.

Key Parameters:
- file_name: The name of the CSV file to be read.
- sort_by: Optional. The column name on which the data frame should be sorted.
- ascending: Optional. A boolean that defines the sorting order (ascending or descending).

The function tries to read the specified file and, if sorting parameters are provided, sorts the data accordingly. In case of any errors, it safely returns None. This function is crucial for retrieving and preparing basic analytics data for visualization in the dashboard.

Usage:
    To read and optionally sort a basic analytics data file, call:
    df = read_data_basic('file_name.csv', sort_by='column_name', ascending=True/False)
"""

import pandas as pd
import os

def read_data_basic(file_name, sort_by=None, ascending=False):
    DATA_DIR = '/app/data/analytics/basic'
    try:
        df = pd.read_csv(os.path.join(DATA_DIR, file_name))
        if sort_by and sort_by in df.columns:
            df = df.sort_values(by=sort_by, ascending=ascending)
        return df
    except:
        return None


import pandas as pd
import numpy as np
import matplotlib.pyplot
from sklearn import linear_model as lm
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer

df = pd.read_csv("homeprices.csv")

dumies = pd.get_dummies(df.town)

df_dumies = pd.concat([df,dumies],axis='columns')

y= df_dumies.price

df_dumies.drop(['town','west windsor','price'],axis="columns",inplace=True )

x = df_dumies

model = lm.LinearRegression()

model.fit(x,y)

predict_ww = model.predict([[3400,0,0]])
predict_rw = model.predict([[2800,0,1]])
predict_mt = model.predict([[2600,1,0]])

print(f"The predicted price for house in west windsor is: {predict_ww}")
print(f"The predicted price for house in robinsville is: {predict_rw}")
print(f"The predicted price for house in monroe township is: {predict_mt}")

#new metrhod

from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer
le = LabelEncoder()

dfle = df

dfle.town = le.fit_transform(dfle.town)

x = dfle[['town','area']].values

y = dfle.price.values

ct = ColumnTransformer([('town',OneHotEncoder(),[0])],remainder='passthrough')

x=ct.fit_transform(x)

x= x[:,1:]

model.fit(x,y)

predict_WW = model.predict([[0,1,3400]])
predict_RW = model.predict([[1,0,2800]])
predict_MT = model.predict([[0,0,2600]])

print(f"The predicted price for house in west windsor is: {predict_WW}")
print(f"The predicted price for house in robinsville is: {predict_RW}")
print(f"The predicted price for house in monroe township is: {predict_MT}")


import pygame
#local Imports
from .snake import Prey
from .snake import Snake

class Screen:
	"""Manage the pygame screen with its properties"""

	def __init__(self, width, height, number_spaces, frame_rate):
		self.width      = width
		self.height     = height
		self.number_spaces = number_spaces
		self.frame_rate = frame_rate

		self.rows    = self.width // self.number_spaces[0]
		self.columns = self.height // self.number_spaces[1]

		self.running = True

	def start_pygame(self):
		"""Initialize the pygame objects to run the screen"""

		#Instance the pygame objects
		pygame.init()
		self.screen = pygame.display.set_mode([self.width, self.height])
		pygame.display.set_caption('Game Window')
		#Control the frame rate
		self.clock = pygame.time.Clock()

	def update_window_frame(self, game_items):
		"""Function that draw the components in the screen"""
		#The Game is design with discretes steps

		#Black backgorund
		self.screen.fill((0,0,0))

		#Lines
		for point in range(self.rows, self.height, self.rows):
			pygame.draw.line(self.screen, (255, 255, 255), (0, point), (self.width, point))

		for point in range(self.columns, self.width, self.columns):
			pygame.draw.line(self.screen, (255, 255, 255), (point, 0), (point, self.height))

		#Draw all the items of the game
		for item in game_items.values():
			for objects in item.values():
				objects.draw(self.screen, self.rows, self.columns)

		pygame.display.update()

	def events_handler(self, game_items):
		"""Manage the evetns to execute the game items actions"""

		#Exit of the screen game with the x button
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				pygame.quit()
				exit()

			#Manage the movements capabilities of the mobile items
			for item in game_items['mobile_items'].values():
				item.movement(event)

		#In each frame continues the previous movement
		for item in game_items['mobile_items'].values():
			item.displacement()

	def game_cycle(self, items):
		"""Infinite loop while the game is executing"""

		while self.running:
			pygame.event.get()
			dt = self.clock.tick(self.frame_rate)

			self.update_window_frame(items)
			self.events_handler(items)
			items['mobile_items']['Snake'].eat(items['static_items']['Prey'])


from sklearn import metrics

import sys, os

sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
import torch
from torch.utils.data import DataLoader
import pickle
from rdkit import Chem
from rdkit import rdBase
from tqdm import tqdm
import numpy as np
# from Dataset.get_dataset import get_dataset,get_lookup_tables
from Dataset.get_Vocab import VocabDatasets,Vocabulary,collate_fn
from Model.model import CharRNN
import argparse
# from Utils.utils.metric import tonimato
from step2_train_charnn import pretrain

from Utils.utils.metric import fraction_valid_smiles
from torch import optim
import pandas as pd
from torch.nn.utils.rnn import pad_sequence
from collections import Counter
from Utils.utils.data_utils import Variable
from rdkit.Chem import MolFromSmiles
import heapq
from rdkit import rdBase
from rdkit.Chem import AllChem,MACCSkeys,Descriptors
from rdkit import DataStructs
rdBase.DisableLog('rdApp.error')

from Utils.utils.scorefunction import pair_log
#CC(NC(=O)OC(C)(C)C)c1nc(CO)nn1Cc1ccccc1

def add_optimizer_args(parser):
    group = parser.add_argument_group("optimizering options")
    group.add_argument("--restore_from", help="Checkpoint to load", type=bool, default=False)
    group.add_argument("--hidden", help="Model hidden size", type=int, default="256")

    group.add_argument("--test_size", help="sample size", type=int, default="1000")
    group.add_argument("--num_layers", help="Model num layers", type=int, default="3")
    group.add_argument("--dropout", help="random sample point ", type=float, default="0.2")

    group.add_argument("--lr", help="Learning rate", type=float, default=0.005)
    group.add_argument("--beta1", help="Adam beta 1", type=float, default=0.9)
    group.add_argument("--beta2", help="Adam beta 2", type=float, default=0.998)
    group.add_argument("--eps", help="Adam epsilon", type=float, default=1e-9)
    group.add_argument("--weight_decay", help="Adam weight decay", type=float, default=1e-2)
    group.add_argument("--save_logs", help="path to save logs", type=str, default='/home/chengkaiyang/Main/optdouble/charnn/optmetrics.csv')
    group.add_argument("--gpu", help="Adam weight decay", type=str, default='True')
    group.add_argument("--max_length", help="max length to sample", type=int, default=100)
    group.add_argument("--n_batch", help="batch size to sample", type=int, default=50)
    group.add_argument("--save_logss", help="path to save logs", type=str, default='/home/chengkaiyang/Main/optdouble/charnn/metrics.csv')
    # group.add_argument("--train_p", help="Checkpoint to load", type=str, default='trainfilter.csv')
    # group.add_argument("--valid_p", help="Checkpoint to load", type=str, default='testfilter.csv')
    group.add_argument("--save_dir", help="path to save check", type=str, default='/home/chengkaiyang/Main/optdouble/charnn')

    group.add_argument("--restore_agent_from", help="Checkpoint to load", type=str, default='/home/chengkaiyang/Main/s/model.2.pt')
    group.add_argument("--restore_prior_from", help="Checkpoint to load", type=str, default='/home/chengkaiyang/Main/s/model.199.pt')
    group.add_argument("--vocab_path", help="Vocab path to load", type=str, default='/home/chengkaiyang/Main/datanew/data/Voc.txt')
    group.add_argument("--score_function", help="score function to choose", type=str, default='fingerprint')
    group.add_argument("--testmol", help="test mol similarity ", type=str, default='CC(C)(C)OC(=O)N1CC[NH2+]CC1C(N)=O')
    group.add_argument("--cuda", help="use gpu device", type=str, default='cuda:0')
    group.add_argument("--sthreshold", help="similarity threshold", type=float, default=0.7)
    group.add_argument("--lthreshold", help="logp threshold", type=float, default=3.5)

#     # file paths
#     group.add_argument("--train_bin", help="Train npz", type=str, default="")
#     group.add_argument("--valid_bin", help="Valid npz", type=str, default="")

    return group
def reback(model,toens,n_batch):
    smis=[]

    for i in range(n_batch):
        stri=''
        for j in range(len(toens[i].cuda().cpu().numpy())):
            smi=model.vocabulary.reversed_vocab[int(toens[i][j])]
            stri+=smi
        if Chem.MolFromSmiles(stri):
            smis.append(stri)

    return smis

def fp_print(smiles,query_fp):

    score=[]
    for s in smiles:
        t=similarity(s,query_fp)

        score.append(t)

    return score

def similarity(a, b):
    if a is None or b is None:
        return 0.0
    amol = Chem.MolFromSmiles(a)
    bmol = Chem.MolFromSmiles(b)
    if amol is None or bmol is None:
        return 0.0

    fp1 = MACCSkeys.GenMACCSKeys(amol)
    fp2 = MACCSkeys.GenMACCSKeys(bmol)
    return DataStructs.FingerprintSimilarity(fp1, fp2)

def train_agent(query_fp,args,restore_prior_from=None,
                restore_agent_from=None,
                scoring_function=None,
                scoring_function_kwargs=None,
                save_dir=None, learning_rate=0.001,
                batch_size=None, n_steps=5000,
                num_processes=0, sigma=100,
                experience_replay=0):

    voc = Vocabulary(init_from_file=args.vocab_path)

    Agent = CharRNN(voc,args)

    if args.gpu:
        device=args.cuda

        Agent=Agent.to(device)
    else:

        Agent=Agent
    optimizer = torch.optim.Adam(Agent.parameters(), lr=args.lr)
    state = torch.load(restore_agent_from)
    # pretrain_state_dict = state['state_dict']
    pretrain_state_dict=state['state_dict']

    Agent.load_state_dict(pretrain_state_dict)
    df1 = pd.DataFrame(columns = ['smiles', 'best score','avg score'])
    df1.to_csv(args.save_logss)

    print("Model initialized, starting training...")
    raw_score=0
    raw_smi=args.testmol

    for step in range(n_steps):
        train_ss=[]
        val_ss=[]

        # Sample from Agent
        seqs = Agent.sample(args.n_batch)
        smiles=reback(Agent,seqs,args.n_batch)

        # nseqs= Prior.sample(args.n_batch)
        # smiless=reback(Prior,nseqs,args.n_batch)
        score=pair_log(smiles)
        # idx = heapq.nlargest(200, range(len( score)),  score.__getitem__)
        # smiles=[smiles[id]  for id in idx]
        s=np.mean(score)
        if s>=args.lthreshold:
            print('get threshold')
            break
        max_score=score.index(max(score))

        max_smiles=smiles[max_score]

        idx=[i for i,a in enumerate(score) if a >s]

        smiles=[smiles[id]  for id in idx]

        dict1 = {'smiles': smiles}
        df = pd.DataFrame(dict1)

        df.to_csv(args.save_logs,mode='w')
        moldatatr=VocabDatasets(fname=args.save_logs,voc=voc)
        train_data = DataLoader(moldatatr, batch_size=10, shuffle=True, drop_last=True,
                      collate_fn=collate_fn)
        for i in tqdm(range(3), desc='Processing'):

            train_loss=pretrain(args,Prio=Agent,optimize=optimizer,train_dat=train_data,epoc=1)

            # with open(os.path.join(save_dir, "validsampled"), 'a') as f:
            #     f.write("{}\n".format(train_loss))
        lists = [max_smiles,max(score), s]
        data = pd.DataFrame([lists])
        data.to_csv(args.save_logss,mode='a',header=False,index=False)#
        torch.save(Agent.state_dict(), os.path.join(save_dir, 'Agent.ckpt'))
        raw_smi=max_smiles
        raw_score=max(score)

    # torch.save(Agent.state_dict(), os.path.join(save_dir, 'Agent.ckpt'))

if __name__ == "__main__":
    parser = argparse.ArgumentParser("preprocess and train")
    group=add_optimizer_args(parser)

    # parsing.add_train_args(parser)
    # parser.add_argument_group
    args = parser.parse_args()

    train_agent(query_fp=args.testmol,args=args,save_dir=args.save_dir,restore_agent_from=args.restore_agent_from,scoring_function=args.score_function,restore_prior_from=args.restore_prior_from,scoring_function_kwargs={})


# slower than dijkstra, edges are weighted, can be used to find negative weight cycles
# do n-1 iterations where n is num of vertices

class Graph:
    def __init__(self, vertices):
        self.V = vertices # No. of vertices
        self.graph = []

    def addEdge(self, u, v, w):
        self.graph.append([u, v, w])

    def bellman_ford(self, src):
        dist = [float('inf')]*self.V
        dist[src] = 0

        for _ in range(self.V-1):
            for u, v, w in self.graph:
                if u != v and dist[v] > w+dist[u]:
                    dist[v] = w+dist[u]

        # now one more iteration to check if cycle exists
        for u, v, w in self.graph:
            if u != v and dist[v] > w+dist[u]:
                print('Negative cycle detected!')

        # print distances from source
        print('Vertex  | Distance')
        print('-'*17)
        for i in range(self.V):
            print(f'\t{i}   |\t  {dist[i]}')
        print('-'*17)

g = Graph(5)
g.addEdge(0, 1, -1)
g.addEdge(0, 2, 4)
g.addEdge(1, 2, 3)
g.addEdge(1, 3, 2)
g.addEdge(1, 4, 2)
g.addEdge(3, 2, 5)
g.addEdge(3, 1, 1)
g.addEdge(4, 3, -3)
g.bellman_ford(0)


from tkinter import *
import tkinter.filedialog
import tkinter.messagebox
from venda import Venda

APP_TITLE = 'Meu Sistema'
root = Tk()

def showJanela(event=None):
    window = tkinter.Toplevel(root)
    window.title('Registro de Vendas')
    window.geometry('500x500')
    window.transient(root)
    Venda(window)
    root.wait_window(window)

def showSobre(event=None):
    tkinter.messagebox.showinfo('Sobre',
                                '{}{}'.format(APP_TITLE, 'Meu Sistema Gráfico\ncom Python e Tkinter'))

def sair(event=None):
    if tkinter.messagebox.askokcancel("Sair", "Deseja realmente sair??"):
        root.destroy()

def montarMenu(event=None):
    menu_bar = Menu(root)
    arc_menu = Menu(menu_bar, tearoff=0)
    aux_menu = Menu(menu_bar, tearoff=0)
    menu_bar.add_cascade(label='Arquivo', underline=0, menu=arc_menu)
    menu_bar.add_cascade(label='Ajuda', underline=0, menu=aux_menu)
    root.config(menu=menu_bar)

    arc_menu.add_command(label='Venda', compound='left', command=showJanela)
    arc_menu.add_separator()
    arc_menu.add_command(label='Sair', compound='left', command=sair)

    aux_menu.add_command(label='Sobre', compound='left', command=showSobre)

def principal():
    montarMenu()
    root.protocol("WM_DELETE_WINDOW", sair)
    root.title(APP_TITLE)
    root.geometry("600x600")
    root.mainloop()

if __name__ == '__main__':
    principal()


from flask import flash
from vote.models.categorie import Categorie
from vote.models.candidat import Candidat
from vote.models.utilisateur import Utilisateur
from vote.models.vote import Vote
from vote import db

"""
a = {'nom':'Rimbaud', 'prénom' : 'Arthur', 'classe': 'T°2'}
b = {'nom':'Dufaud', 'prénom' : 'Bertrand', 'classe': 'T°3'}
c = {'nom':'Brava', 'prénom' : 'Caroline', 'classe': 'T°2'}
d = {'nom':'Brava', 'prénom' : 'Carla', 'classe': 'T°1'}
candidat = a, b, c, d
"""

def obtenirCandidats(idCategorie):
    genre = Categorie.query.get(idCategorie).genre
    classe = Categorie.query.get(idCategorie).niveau[0]
    liste = Candidat.query.filter(
        Candidat.genre == genre, Candidat.classe.like("%" + classe + "%")
    ).all()
    candidats = []
    for eleve in liste:
        provisoire = {
            "nom": eleve.nom,
            "prénom": eleve.prenom,
            "classe": eleve.classe,
            "identifiant": eleve.id,
        }
        candidats.append(provisoire)
    return candidats

dico = {"M": "Garçons", "F": "Filles", "T": "Terminale", "P": "Première", "S": "Seconde"}

def obtenirCategorie(idCategorie):
    cat = Categorie.query.get(idCategorie)
    return cat

def jeVote(idCategorie, idCandidat, idUser):
    v = Vote(id_user=idUser, id_candidat=idCandidat, id_categorie=idCategorie)
    db.session.add(v)
    db.session.commit()
    flash("A voté")

def categoriesVotees(idUser):
    liste = Vote.query.filter(Vote.id_user == idUser).all()
    catVotees = []
    for cat in liste:
        catVotees.append(cat.id_categorie)
    return catVotees


from marketing_automation.active_campaign.entities.active_campaign_contact_automation import ActiveCampaignContactAutomation
from marketing_automation.active_campaign.entities.active_campaign_contact_note import ActiveCampaignContactNote
from marketing_automation.active_campaign.entities.active_campaign_deal import ActiveCampaignDeal
from marketing_automation.active_campaign.entities.active_campaign_deal_note import ActiveCampaignDealNote
from marketing_automation.active_campaign.entities.active_campaign_deal_task import ActiveCampaignDealTask
from unified.core.actions import Actions
from marketing_automation.active_campaign import util
from marketing_automation.active_campaign.entities.active_campaign_account import ActiveCampaignAccount
from marketing_automation.active_campaign.entities.active_campaign_tracked_event import ActiveCampaignTrackedEvent
import json
import requests

class ActiveCampaignActions(Actions):
    """ActiveCampaign Actions wrapper class.

        Extends from common Actions class.
    """

    def get_account_fields_data(self, account_entity, context):
        """ Get the account custom  fields data"""

        custom_fields_data = util.rest('GET', 'accountCustomFieldMeta', context, None).text
        custom_fields_data = json.loads(custom_fields_data)

        fields = []
        custom_fields_data = custom_fields_data["accountCustomFieldMeta"]

        if account_entity.address_1 is not None:
            for obj in custom_fields_data:
                if obj["fieldLabel"] == "Address 1":
                    field_data = {
                        "customFieldId": obj["id"],
                        "fieldValue": account_entity.address_1
                    }
                    fields.append(field_data)

        if account_entity.address_2 is not None:
            for obj in custom_fields_data:
                if obj["fieldLabel"] == "Address 2":
                    field_data = {
                        "customFieldId": obj["id"],
                        "fieldValue": account_entity.address_2
                    }
                    fields.append(field_data)

        if account_entity.city is not None:
            for obj in custom_fields_data:
                if obj["fieldLabel"] == "City":
                    field_data = {
                        "customFieldId": obj["id"],
                        "fieldValue": account_entity.city
                    }
                    fields.append(field_data)

        if account_entity.state is not None:
            for obj in custom_fields_data:
                if obj["fieldLabel"] == "State/Province":
                    field_data = {
                        "customFieldId": obj["id"],
                        "fieldValue": account_entity.state
                    }
                    fields.append(field_data)

        if account_entity.postal_code is not None:
            for obj in custom_fields_data:
                if obj["fieldLabel"] == "Postal Code":
                    field_data = {
                        "customFieldId": obj["id"],
                        "fieldValue": account_entity.postal_code
                    }
                    fields.append(field_data)

        if account_entity.country is not None:
            for obj in custom_fields_data:
                if obj["fieldLabel"] == "Country":
                    field_data = {
                        "customFieldId": obj["id"],
                        "fieldValue": account_entity.country
                    }
                    fields.append(field_data)

        if account_entity.phone_number is not None:
            for obj in custom_fields_data:
                if obj["fieldLabel"] == "Phone Number":
                    field_data = {
                        "customFieldId": obj["id"],
                        "fieldValue": account_entity.phone_number
                    }
                    fields.append(field_data)

        if account_entity.description is not None:
            for obj in custom_fields_data:
                if obj["fieldLabel"] == "Description":
                    field_data = {
                        "customFieldId": obj["id"],
                        "fieldValue": account_entity.description
                    }
                    fields.append(field_data)

        if account_entity.number_of_employees is not None:
            for obj in custom_fields_data:
                if obj["fieldLabel"] == "Number of Employees":
                    field_data = {
                        "customFieldId": obj["id"],
                        "fieldValue": account_entity.number_of_employees
                    }
                    fields.append(field_data)

        if account_entity.annual_revenue is not None:
            for obj in custom_fields_data:
                if obj["fieldLabel"] == "Annual Revenue":
                    field_data = {
                        "customFieldId": obj["id"],
                        "fieldValue": account_entity.annual_revenue
                    }
                    fields.append(field_data)

        if account_entity.industry is not None:
            for obj in custom_fields_data:
                if obj["fieldLabel"] == "Industry/Vertical":
                    field_data = {
                        "customFieldId": obj["id"],
                        "fieldValue": account_entity.industry
                    }
                    fields.append(field_data)
        return fields

    def create_account(self, context, payload):
        """ Create a new account."""

        account_entity = ActiveCampaignAccount(**payload)

        # Get custom feilds data
        fields = self.get_account_fields_data(account_entity, context)

        if len(fields)!=0:
            account_data = {
                "account": {
                    "name": account_entity.name,
                    "accountUrl": account_entity.website,
                    "fields": fields
                }
            }
        else:
            account_data = {
                "account": {
                    "name": account_entity.name,
                    "accountUrl": account_entity.website,
                }
            }

        resp = util.rest('POST', 'accounts', context, account_data)
        return json.loads(resp.text)

    def update_account(self, context, payload):
        """ Create a new account."""

        account_entity = ActiveCampaignAccount(**payload)

        # Get custom fields data
        fields = self.get_account_fields_data(account_entity, context)

        if len(fields)!=0:
            account_data = {
                "account": {
                    "name": account_entity.name,
                    "accountUrl": account_entity.website,
                    "fields": fields
                }
            }
        else:
            account_data = {
                "account": {
                    "name": account_entity.name,
                    "accountUrl": account_entity.website,
                }
            }

        resp = util.rest('PUT', f'accounts/{account_entity.account_id}', context, account_data).text
        return json.loads(resp)

    def add_contact_note(self, context, payload):
        """Add a note to contact"""

        note_entity = ActiveCampaignContactNote(**payload)
        note_data = {
            "note": {
                "note": note_entity.note,
                "relid": note_entity.contact_id,
                "reltype": "Subscriber",
            }
        }

        client = util.get_client(context)
        return client.notes.create_a_note(note_data)

    def add_contact_to_automation(self, context, payload):
        """Add a contact to an automation."""

        contact_entity = ActiveCampaignContactAutomation(**payload)
        contact_data = {
            "contactAutomation": {
                "contact": int(contact_entity.contact_id),
                "automation": int(contact_entity.automation_id)
            }
        }

        client = util.get_client(context)
        return client.contacts.add_a_contact_to_an_automation(contact_data)

    def get_deal_stages_id(self, deal_entity,  context):
        """ Get deal Stages data"""

        stage_data = util.rest('GET', 'dealStages', context, None).text
        stage_data = json.loads(stage_data)
        stage_data = stage_data["dealStages"]

        for obj in stage_data:
            if deal_entity.stage == obj["title"]:
                id = obj["id"]
                return id
        return None

    def create_deal(self, context, payload):
        """Create a new deal."""

        deal_entity = ActiveCampaignDeal(**payload)

        # Get stage id
        stage_id = self.get_deal_stages_id(deal_entity, context)

        deal_data = {
            "deal": {
                "contact": deal_entity.contact_id,
                "account": deal_entity.account_id,
                "currency": deal_entity.currency,
                "group": deal_entity.pipeline_id,
                "owner": deal_entity.owner_id,
                "stage": stage_id,
                "title": deal_entity.title,
                "value": deal_entity.value,
            }
        }

        client = util.get_client(context)
        response = client.deals.create_a_deal(deal_data)
        id = response["deal"]["id"]

        custom_fields_data = {
            "dealCustomFieldDatum":{
                "customFieldId": 1,
                "dealId": id,
                "fieldValue": deal_entity.forecasted_close_date
            }
        }

        update_forecast_date = util.rest('POST', 'dealCustomFieldData', context, custom_fields_data).text
        return response

    def update_deal(self, context, payload):
        """Update an existing deal"""

        deal_entity = ActiveCampaignDeal(**payload)

        # Get Stage Id
        stage_id = self.get_deal_stages_id(deal_entity, context)

        deal_data = {
            "deal": {
                "contact": deal_entity.contact_id,
                "account": deal_entity.account_id,
                "currency": deal_entity.currency,
                "group": deal_entity.pipeline_id,
                "owner": deal_entity.owner_id,
                "stage": stage_id,
                "title": deal_entity.title,
                "value": deal_entity.value,
            }
        }

        client = util.get_client(context)
        response = client.deals.update_a_deal(deal_entity.deal_id, deal_data)

        custom_fields_data = {
            "dealCustomFieldDatum":{
                "customFieldId": 1,
                "dealId": deal_entity.deal_id,
                "fieldValue": deal_entity.forecasted_close_date
            }
        }

        update_forecast_date = util.rest('POST', 'dealCustomFieldData', context, custom_fields_data).text
        return response

    def add_note_to_deal(self, context, payload):
        """Create a deal note"""

        note_entity = ActiveCampaignDealNote(**payload)
        note_data = {
            "note": {
                "note": note_entity.note
            }
        }

        client = util.get_client(context)
        return client.deals.create_a_deal_note(note_entity.deal_id, note_data)

    def create_deal_task(self, context, payload):
        """Create a deal task"""

        task_entity = ActiveCampaignDealTask(**payload)
        resp = util.rest('GET', 'dealTasktypes', context, None).text
        resp = json.loads(resp)

        for dealtype in resp["dealTasktypes"]:
            if dealtype["title"] == task_entity.task_type:
                task_type_id = dealtype["id"]

        task_data = {
            "dealTask": {
                "title": task_entity.title,
                "relid": task_entity.deal_id,
                "note": task_entity.note,
                "duedate": task_entity.due_date,
                "dealTasktype": task_type_id,
                "assignee": task_entity.assignee_id,
            }
        }

        resp = util.rest('POST', 'dealTasks', context, task_data).text
        return json.loads(resp)

    def create_tracked_event(self, context, payload):

        headers= {
            "Content-Type":"application/x-www-form-urlencoded"
        }
        event_info = ActiveCampaignTrackedEvent(**payload)
        data={
            "actid": event_info.account_id,
            "key": event_info.event_key,
            "event":event_info.event_name,
            "eventdata": int(event_info.event_value),
            "visit":{"email":event_info.contact_email_address}
        }
        resp = requests.request('POST', 'https://trackcmp.net/event', headers=headers, data=data).text
        return json.loads(resp)


import serial

print("Sensor Demo")

usbport = '/dev/ttyAMA0'
ser = serial.Serial(usbport, 38400)

ser.write(bytes("L1\r",'UTF-8'))
ser.write(bytes("C\r",'UTF-8'))

line=""

while True:
	data = ser.read().decode('UTF-8')
	if(data == "\r"):
		print("Received:", line)
		line=""
	else:
		line = line + data


import os
import pandas as pd
import numpy as np
import requests
import pickle
import nltk

from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics.pairwise import cosine_similarity
from collections import Counter

def cleanhtml(raw_html):
  cleantext = re.sub(CLEANR, '', raw_html)
  return cleantext

def getGameFromSteam(appid):
	response = requests.get(rf"https://store.steampowered.com/api/appdetails?appids={appid}")

	if response and "data" in response.json()[f"{appid}"]:
		return response.json()[f"{appid}"]["data"]
	else:
		return None

def loadObject(PATH):
	with open(PATH, 'rb') as inp:
		new_game = pickle.load(inp)
	return new_game

def saveObject(obj, USERS_PATH):
	with open(USERS_PATH, 'wb') as file:
		pickle.dump(obj, file, pickle.HIGHEST_PROTOCOL)
	return True

def initializeDataset(PATH):
	try:
		dataset = pd.read_csv(PATH)
	except:
		with ZipFile('dataset.zip', 'r') as zip_ref:
			zip_ref.extractall('')
		dataset = pd.read_csv(PATH)
	dataset.dropna(inplace=True, subset=['game_description', 'name', 'genre', 'popular_tags', 'developer', 'publisher'])

	# splittig the genre string to create a list of genre
	pop_tags_col_index = np.where(dataset.columns.values == "popular_tags")[0][0]

	dataset["genre"] = dataset.genre.str.split(',')
	dataset["popular_tags"] = dataset.popular_tags.str.split(',')

	if 'priority' not in dataset.columns:
		dataset["priority"] = 0

	# extracting percentage of positive review from dataset
	dataset["positive_review_percentage"] = dataset.all_reviews.str.extract(r'(\d+(?=%))')

	# extracting total number of reviews from dataset
	dataset["review_count"] = dataset.all_reviews.str.replace(",", "")
	dataset["review_count"] = dataset.review_count.str.extract(r'(\d+(?=))').replace(np.NaN, 0)
	dataset["review_count"] = dataset.review_count.astype(int)

	dataset = dataset[dataset.review_count > 300]

	# Extracting appid from dataset
	dataset["appid"] = dataset.url.str.extract(r'(\d+(?=/))')
	dataset.dropna(inplace=True, subset=["appid"])

	dataset["appid"] = dataset["appid"].astype(int)
	dataset["steam_appid"] = dataset["appid"].astype(int)

	dataset["positive_review_count"] = dataset.all_reviews.str.extract(r'(\d+(?=))')

	dataset = dataset.drop_duplicates(subset=['steam_appid'])
	dataset.set_index('appid', inplace=True)

	saveObject(dataset, 'dataset.pkl')
	return dataset

def loadDataset(PATH, DATASET_FILE='steam_games.csv'):
	try:
		with open(PATH, 'rb') as inp:
			dataset = pickle.load(inp)
		print("[ Processed Dataset Found ! Loading dataset ]")
		return dataset
	except:
		print("[ !! Processed Dataset NOT found !! ]")
		return initializeDataset(DATASET_FILE)

def countCategory(dataset, col):
	tags = dataset.dropna(subset=[col]) # dropping all games with where tags is na
	tags = [j for sub in tags[col].values for j in sub]
	tags = Counter(tags) # generating a counter of all tags

	return tags

def allCharsInString(string, chars):
	curr = 0
	for char in string:
		if curr == len(chars):
			return True
		if char == chars[curr]:
			curr += 1
	return curr == len(chars)

def hasTags(arr, tags):
	if type(arr) == float:
		return False

	for tag in tags:
		if tag not in arr:
			return False
	return True

def excludeTags(arr, tags):
	for tag in tags:
		if hasTags(arr, [tag]):
			return False
	return True

def searchGameByName(dataset, name):
	resultBySubstr = np.vectorize(lambda x: name.lower() in x.lower())(dataset["name"].dropna().values)
	resultByAllChar = np.vectorize(lambda x: allCharsInString(x.lower(), name.lower()))(dataset["name"].dropna().values)

	data = dataset[dataset["name"].isna()==False]

	topResults = data[resultBySubstr].sort_values(by=['priority', 'positive_review_percentage', 'review_count'], ascending=False)
	lastResults =data[resultByAllChar].sort_values(by=['priority', 'positive_review_percentage', 'review_count'], ascending=False)

	return pd.concat([topResults, lastResults])

def searchGameByTagAndName(dataset, tags, name, exclusions=[]):
	if tags==[] and name == '' and exclusions==[]:
		return dataset.sort_values(by=['priority', 'review_count', 'positive_review_percentage'], ascending=False)

	data = searchGameByName(dataset, name)

	filterTagsFunc = np.vectorize(lambda x: hasTags(x, tags) and excludeTags(x, exclusions))

	# resultByGenre = filterTagsFunc(data["genre"].values)
	# resultByPopularTag = filterTagsFunc(data["popular_tags"].values)

	# topResults = data[resultByPopularTag]
	# lastResults = data[resultByGenre]

	# result = pd.concat([topResults, lastResults]).drop_duplicates(subset=['name'])

	result = data[filterTagsFunc(data["popular_tags"].values)].drop_duplicates(subset='name')

	return result.sort_values(by=["priority"], ascending=[False])

def getLikedGames(dataset, user):
	return dataset[dataset.steam_appid.isin( user.gamesLiked.keys() )]

def searchGameByDeveloper(dataset, developer):
	resultBySubstr = np.vectorize(lambda x: developer.lower() in x.lower())(dataset["developer"].dropna().values)
	resultByAllChar = np.vectorize(lambda x: allCharsInString(x.lower(), developer.lower()))(dataset["developer"].dropna().values)
	data = dataset[dataset["developer"].isna()==False]

	return pd.concat([data[resultBySubstr], data[resultByAllChar]])

def getSimilarGames(dataset, similarity_matrix, new_games, appid, count):
    index = new_games[new_games.name == dataset.name[appid]].index[0]
    distances = similarity_matrix[index]
    similar_games = sorted( list(enumerate(distances)) ,reverse = True,key = lambda x : x[1])[1:count]

    result = dataset.loc[new_games.iloc[ list(map(lambda x: x[0], similar_games)) ]['steam_appid'].astype(int)]
    return result

def getSimilarGamesByList(dataset, similarity_matrix, new_games, arr, count):
	res = []
	for appid in arr:
		index = new_games[new_games.name == dataset.name[appid]].index[0]
		distances = similarity_matrix[index]
		similar_games = sorted( list(enumerate(distances)) ,reverse = True,key = lambda x : x[1])[1:count]
		res.extend(similar_games)

	res = sorted( res ,reverse = True,key = lambda x : x[1])
	result = dataset.loc[new_games.iloc[ list(map(lambda x: x[0], res)) ]['steam_appid'].astype(int)]
	return result

def createSimilarityMatrix(new_games):
	try:
		similarity = loadObject("data/similarity.pkl")
		print("[ similarity matrix found! ]")

	except (OSError, IOError) as e:

		print("[ !!similarity matrix not found!! creating now ] ")

		cv = CountVectorizer(max_features = 5000, stop_words = 'english')
		vectors = cv.fit_transform(new_games.tags).toarray()
		similarity = cosine_similarity(vectors)

		pickle.dump(similarity,open('data/similarity.pkl','wb'), protocol=4)

	print("[ Similarity matrix loaded ]")
	return similarity

new_games = loadObject("data/new_games.pkl")
similarity_matrix = createSimilarityMatrix(new_games)

print("[ Recommendation system loaded! ]")

if __name__ == "__main__":
	dataset = loadDataset("dataset.pkl")
	print(dataset.head().name)
	# print(getSimilarGamesByList(dataset, similarity_matrix, new_games, [379720, 578080],10).name)


func = lambda a,b:  print("The sum of two numbers is : ", (a+b))

type(func)

func(2, 10)

def new_func(list1):
    list2 = []

    for i in range(10):
        for j in range(5):
            result = i * j
            list2.append(result)
    return list2 + list1

print(new_func([10,20,30,40,50]))

# This whole new_func() can be done using one line code only,

new_func_advance = lambda mylist: [ x*y for x in range(10) for y in range(5)] + mylist

print(new_func_advance([10,20,30,40,50]))

# and it's done........... using only 1 line

def product10(a):    # created a function which takes input and multiplies it with 10
    return a * 10

r1 = range(10)       # created a range function of 10 elements

b = map(product10, r1)     # map the first function to range function

list(b)   # showing results using list, this will show a list of 0-9 numbers multiplied with 10

# now doing the same with a lambda function

b1 = map( (lambda a : a * 10 ) , r1 )   # mapping the lambda function with range function

list(b1)   # showing results using list, this will show a list of 0-9 numbers multiplied with 10

# now applying filter function

b2 = filter( (lambda a : a > 5 ) , r1 )   # filtering the lambda function with range function using condition

list(b2)


import PySimpleGUI as sg
import pandas as pd
import os
import firebase_handler
import time
# Add some color to the window
sg.theme('DarkTeal9')

FIRE_BASE_FILE = '/csv_file1'  # path inside firebase for the online xlsx (excel) file
FIRE_BASE_FILE_TMP_LOCAL = './csv_file1_tmp.xlsx'  # path for local copy for data from firebase
EXCEL_FILE = './Data_Entry.xlsx'  # local file
df = pd.read_excel(EXCEL_FILE)  # local file
firebase_handler.set_up_fire_base()  # might not work if firebase is not configure

default_size = (20, 1)
layout_home = [
    [sg.Button('Open local file and edit it manually')],
    [sg.Text('Please fill out the following fields:')],
    [sg.Text('Name', size=default_size), sg.InputText(key='Name')],
    [sg.Text('City', size=default_size), sg.InputText(key='City')],
    [sg.Text('Favorite Colour', size=default_size), sg.Combo(['Green', 'Blue', 'Red'], key='Favorite Colour')],
    [sg.Text('I speak', size=default_size),
                            sg.Checkbox('German', key='German'),
                            sg.Checkbox('Spanish', key='Spanish'),
                            sg.Checkbox('English', key='English')],
    [sg.Text('No. of Children', size=default_size), sg.Spin([i for i in range(0,16)], initial_value=0, key='Children')],
    [sg.Submit(), sg.Button('Clear'), sg.Exit()]
]

layout_firebase = [
    [sg.Text('This will work only if pre-configured', visible=True)],
    [sg.Button('Open file from fire base!')],
    [sg.Button('Update file to fire base', visible=False)],
    [sg.Exit()]
]

layout = [
    [sg.TabGroup([[sg.Tab('Home(local)', layout_home, visible=True),
                   sg.Tab('FireBase', layout_firebase, visible=True)
                   ]])],
    ]

window = sg.Window('Simple data entry form', layout, size=(500, 280), font=('Arial', 15))

def clear_input():
    for key in values:
        window[key]('')
    return None

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == 'Exit':
        break
    if event == 'Clear':
        clear_input()
    if event == 'Open local file and edit it manually':
        os.system(f'open {EXCEL_FILE}')
    if event == 'Submit':
        new_record = pd.DataFrame(values, index=[0])
        df = pd.concat([df, new_record], ignore_index=True)
        df.to_excel(EXCEL_FILE, index=False)
        sg.popup('Data saved!')
        clear_input()
    if event == 'Open file from fire base!':
        df = firebase_handler.get_csv_from_json_from_fire_base(FIRE_BASE_FILE)
        if df is None:
            sg.popup('Something did not work! check firebase_handler.py and firebase_key.json')
            continue
        df.to_excel(FIRE_BASE_FILE_TMP_LOCAL, index=False)
        time.sleep(0.07)
        os.system(f'open {FIRE_BASE_FILE_TMP_LOCAL}')
        window['Update file to firebase'].update(visible=True)
    if event == 'Update file to firebase':
        df = pd.read_excel(FIRE_BASE_FILE_TMP_LOCAL)
        # print(df)
        firebase_handler.update_csv_as_json_to_fire_base(df, FIRE_BASE_FILE)
        sg.popup('Data was update to firebase!')

window.close()


"""
Written by Zain Kamal (zain.eris.kamal@rutgers.edu).
https://github.com/Humboldt-Penguin/redplanet

For more information, call `help(GRS)` or directly view docstring in `GRS/__init__.py`.

"""

############################################################################################################################################

from redplanet import utils

import pooch
import numpy as np
import matplotlib.pyplot as plt
import PIL

import os
# import inspect

############################################################################################################################################
""" module variables """

datapath = utils.getPath(pooch.os_cache('redplanet'), 'GRS')
'''
Path where pooch downloads/caches data.
'''

filepath_mola = ''
'''
Only try to download the mola map if the user calls `GRS.visualize()` with `overlay=True`.
'''

__nanval: float = -1e10
'''
Value given to pixels where data is not defined (i.e. "NOT_APPLICABLE_CONSTANT"). In the data, this is 9999.999.
We choose an extremely large negative value so we can easily filter/mask it when using the data or plotting. This errs on the side of caution.
'''

def get_nanval() -> float:
    return __nanval

__grid_spacing = 5 # degrees
__lat_range = np.arange(87.5, -87.5 *1.0001, -__grid_spacing)
__lon_range = np.arange(177.5, -177.5 *1.0001, -__grid_spacing)
__lon_range_cycled = np.arange(182.5, -182.5 *1.0001, -__grid_spacing)
'''
We opt to hardcode these values in the case of GRS because it's static. It's not hard to programmatically calculate these values in other cases -- the code for such is included but commented out below.
'''

__meta_dat: dict = {}
'''
`meta_dat` is formatted as `meta_dat[element_name][quantity]`, where
    - `element_name` is from ['al','ca','cl','fe','h2o','k','si','s','th']
    - `quantity` is from:
        - 'concentration' = Concentration of the element.
        - 'sigma' = The error associated with the concentration measurement.

Calling `meta_dat` as such returns a 2D numpy array containing the original dataset where all units are in concentration out of one (i.e. original wt% * 0.01 or ppm * 0.000001). For some index [i,j], `i` is longitude from `__lon_range[0]` to `__lon_range[-1]`, and `j` is latitude from `__lat_range[0]` to `__lat_range[-1]`.
'''

def get_meta_dat() -> dict:
    return __meta_dat

############################################################################################################################################
""" initialize (run upon import) """

'''download data (or access from cache) and load into `__meta_dat`'''
def __init() -> None:
    """
    DESCRIPTION:
    ------------
        Download data (or load from cache) and format into usable dictionary `__meta_dat`.

    REFERENCES:
    ------------
        2022_Mars_Odyssey_GRS_Element_Concentration_Maps:
            > Rani, A., Basu Sarbadhikari, A., Hood, D. R., Gasnault, O., Nambiar, S., & Karunatillake, S. (2022). 2001 Mars Odyssey Gamma Ray Spectrometer Element Concentration Maps. https://doi.org/https://doi.org/10.1029/2022GL099235
            - Data downloaded from https://digitalcommons.lsu.edu/geo_psl/1/
            - Data reuploaded to https://drive.google.com/file/d/1Z5Esv-Y4JAQvC84U-VataKJHIJ9OA4_8/view?usp=sharing for significantly increased downloading speeds

    """

    '''load from pooch download/cache -- turn off the logger so we don't get unnecessary output every time a file is downloaded for the first time'''
    logger = pooch.get_logger()
    logger.disabled = True

    filepaths = pooch.retrieve(
        fname='2022_Mars_Odyssey_GRS_Element_Concentration_Maps.zip',
        url=r'https://drive.google.com/file/d/1Z5Esv-Y4JAQvC84U-VataKJHIJ9OA4_8/view?usp=sharing',
        known_hash='sha256:45e047a645ae8d1bbd8e43062adab16a22786786ecb17d8e44bfc95f471ff9b7',
        path=datapath,
        downloader=utils.download_gdrive_file,
        processor=pooch.Unzip(),
    )

    logger.disabled = False

    for filepath in filepaths:

        filename = os.path.basename(filepath)
        if 'README' in filename: continue

        element_name = filename.split('_')[0].lower()

        '''initialize entry in `meta_dat`'''
        __meta_dat[element_name] = {}

        '''import data from files to np.ndarrays'''
        dat = np.loadtxt(filepath, skiprows=1)
        dat = np.where(dat == 9999.999, get_nanval(), dat)

        """ ==> we hardcode these values because know the data is 5x5 degree grid
        lat_range = utils.unique(dat[:, 0])
        lon_range = utils.unique(dat[:, 1])

        if len(np.unique(np.diff(lon_range).round(decimals=3))) != 1:
            raise Exception('Longitude values are not evenly spaced. This is not supported by the interpolation model.')
        if len(np.unique(np.diff(lat_range).round(decimals=3))) != 1:
            raise Exception('Latitude values are not evenly spaced. This is not supported by the interpolation model.')

        '''edge case (part 1/2): longitude is cyclical, but data is not, so we duplicate one extra column on each edge of data & lon_range'''
        grid_spacing = np.unique(np.diff(lon_range).round(decimals=3))[0] # grid_spacing based on lon values, so it might be negative if lon is decreasing. but this is okay, it allows the lon cycling to work out.
        meta_dat[element_name]['grid spacing [degrees]'] = abs(grid_spacing)
        lon_range_cycled = np.array([lon_range[0]-grid_spacing, *lon_range, lon_range[-1]+grid_spacing]) # even if grid_spacing is negative, this will work out.
        """

        data_names = ['concentration', 'sigma']

        for i in range(len(data_names)):
            this_data = dat[:, 2+i]

            '''reshape to 2D, transpose to get [lon,lat] indexing'''
            this_data = this_data.reshape(__lat_range.shape[0], __lon_range.shape[0]).T
            # for index (i,j), `i` is longitude from `lon_range[0]` to `lon_range[-1]`, `j` is latitude from `lat_range[0]` to `lat_range[-1]`

            '''units/corrections'''
            if element_name == 'th':
                correction=0.000001 # correct ppm to concentration out of 1
            else:
                correction=0.01 # correct weight percent to concentration out of 1
            this_data = np.where(this_data != get_nanval(), this_data*correction, this_data)

            '''edge case (part 2/2): longitude is cyclical, but data is not, so we duplicate one extra column on each edge of data & lon_range'''
            left_edge = this_data[0, :]
            right_edge = this_data[-1, :]
            this_data = np.array([right_edge, *this_data, left_edge])

            '''add to `meta_dat`'''
            __meta_dat[element_name][data_names[i]] = this_data

    '''use this to pre-compute volatile concentration so we're accessing once instead of thrice. make appropriate changes in `get` as well. note that adding raw data into one grid and then doing bilinear interpolation is not different from doing bilinear interpolation individually and adding them up.'''
    data_names = ['concentration', 'sigma']
    __meta_dat['cl+h2o+s'] = {}

    for data_name in data_names:
        __meta_dat['cl+h2o+s'][data_name] = __meta_dat['cl'][data_name] + __meta_dat['h2o'][data_name] + __meta_dat['s'][data_name]
        __meta_dat['cl+h2o+s'][data_name] = np.where(__meta_dat['cl+h2o+s'][data_name] < 0, get_nanval(), __meta_dat['cl+h2o+s'][data_name])

############################################################################################################################################
""" functions """

def __checkCoords(
    lon: float,
    lat: float
):

    if not (-180 <= lon <= 180):
        raise ValueError(f'Longitude {lon} is not in range [-180, 180].')
    if not (-87.5 <= lat <= 87.5):
        raise ValueError(f'Latitude {lat} is not in range [-87.5, 87.5].')

def get(
    element_name: str,
    lon: float,
    lat: float,
    normalize=False,
    quantity='concentration'
) -> float:
    """
    DESCRIPTION:
    ------------
        Get GRS-derived concentration/sigma of an element at a desired coordinate.

    PARAMETERS:
    ------------
        element_name : str
            Element for which you want to make a global concentration map. Options are ['al','ca','cl','fe','h2o','k','si','s','th']. Casing does not matter.

        lon : float
            Longitude in range [-180, 180] (lon=0 cuts through Arabia Terra).

        lat : float
            Latitude in range [-87.5, 87.5].

        normalize : bool (default False)
            If True, normalize to a volatile-free (Cl, H2O, S) basis.
                > "For such measurement [from GRS] to represent the bulk chemistry of the martian upper crust, it must be normalized to a volatile-free basis (22). That equates to a 7 to 14% increase in the K, Th, and U abundances (22), which we applied to the chemical maps by renormalizing to Cl, stoichiometric H2O, and S-free basis."
                Source: "Groundwater production from geothermal heating on early Mars and implication for early martian habitability", Ojha et al. 2020, https://www.science.org/doi/10.1126/sciadv.abb1669

        quantity : str (default 'concentration')
            Quantity to plot. Options are ['concentration', 'sigma'].

    RETURN:
    ------------
        float
            Surface concentration of an element at the desired coordinate, using bilinear interpolation if that coordinate is not precisely defined by the data
                - Units are in concentration out of one (i.e. original wt% * 0.01 or ppm * 0.000001)
                - If a nearby "pixel" (original 5x5 bin) is unresolved by GRS, just return the nanval.

    NOTES:
    ------------
        Our approaches to this computation have been, in order: sloppy manual calculation -> scipy.interpolate.RegularGridInterpolator -> optimized manual calculation (current). Relative to the current approach, the first approach is 2-3x slower (expected due to obvious optimizations), and the second approach is ~10x slower (unexpected, this seems to be a known bug with scipy). See more discussion here: https://stackoverflow.com/questions/75427538/regulargridinterpolator-excruciatingly-slower-than-interp2d/76566214#76566214.

    """

    __checkCoords(lon, lat)

    def bilinear_interpolation(x: float, y: float, points: list) -> float:
        '''
        Credit for this function: https://stackoverflow.com/a/8662355/22122546

        Interpolate (x,y) from values associated with four points.

        points: list
            four triplets:  (x, y, value).

        See formula at:  http://en.wikipedia.org/wiki/Bilinear_interpolation
        '''

        points = sorted(points)               # order points by x, then by y
        (x1, y1, q11), (_x1, y2, q12), (x2, _y1, q21), (_x2, _y2, q22) = points

        # if x1 != _x1 or x2 != _x2 or y1 != _y1 or y2 != _y2:
        #     raise ValueError('points do not form a rectangle')
        # if not x1 <= x <= x2 or not y1 <= y <= y2:
        #     raise ValueError('(x, y) not within the rectangle')

        return (q11 * (x2 - x) * (y2 - y) +
                q21 * (x - x1) * (y2 - y) +
                q12 * (x2 - x) * (y - y1) +
                q22 * (x - x1) * (y - y1)
            ) / ((x2 - x1) * (y2 - y1) + 0.0)

    # if element_name in __volatiles:
    #     normalize = False
    #     # The function header defaults `normalize=True`, so a well-meaning user calling `GRS.visualize('h2o')` will encounter the exception 'Cannot normalize a volatile...'. T

    if not normalize: # just return the bilinear interpolation on the raw data

        # since `lon_range_cycled` and `lat_range` are decreasing rather than increasing, we do some trickery on top of `np.searchsorted()` to get the desired indices.
        i_lon = __lon_range_cycled.shape[0] - np.searchsorted(np.flip(__lon_range_cycled), lon) # note that i_lon is derived from lon_range_cycled, not lon_range, so only use it to index that!
        j_lat = __lat_range.shape[0] - np.searchsorted(np.flip(__lat_range), lat)

        element_name = element_name.lower()

        points = (
            (
                __lon_range_cycled[i_lon - 1],
                __lat_range[j_lat - 1],
                __meta_dat[element_name][quantity][i_lon - 1, j_lat - 1]
            ),
            (
                __lon_range_cycled[i_lon],
                __lat_range[j_lat - 1],
                __meta_dat[element_name][quantity][i_lon, j_lat - 1]
            ),
            (
                __lon_range_cycled[i_lon - 1],
                __lat_range[j_lat],
                __meta_dat[element_name][quantity][i_lon - 1, j_lat]
            ),
            (
                __lon_range_cycled[i_lon],
                __lat_range[j_lat],
                __meta_dat[element_name][quantity][i_lon, j_lat]
            )
        )

        # ### alternative version to the above that uses list comprehension as opposed to hard-coding -- functionally equivalent, possibly faster. i don't know.
        # points = [
        #     (
        #         lon_range_cycled[i_lon-1+i],
        #         lat_range[j_lat-1+j],
        #         meta_dat[element_name][quantity][i_lon-1+i, j_lat-1+j]
        #     )
        #     for i, j in [(i, j) for i in range(2) for j in range(2)]
        # ]

        val = bilinear_interpolation(lon, lat, points)

        return val if val >= 0 else get_nanval() # This line ensures that where GRS data is undefined, we return exactly the nanval. Without this, we might return very large negative values that approach the nanval. Examples here: https://files.catbox.moe/khetcp.png & https://files.catbox.moe/sri8m4.png

    else: # Uses recursion. See docstring for more details on `normalize=True` parameter.

        __volatiles = ('cl', 'h2o', 's')

        if element_name in __volatiles:
            raise Exception('Cannot normalize a volatile (Cl, H2O, or S) to a volatile-free basis.')

        raw_concentration = get(element_name=element_name, lon=lon, lat=lat, normalize=False, quantity=quantity)
        if raw_concentration < 0:
            return get_nanval()

        '''option 1/2: compute sum of volatiles by accessing/summing each volatile individually'''
        # sum_volatile_concentration = 0
        # for volatile in __volatiles:
        #     volatile_concentration = get(element_name=volatile, lon=lon, lat=lat, normalize=False, quantity=quantity)
        #     if volatile_concentration < 0:
        #         return get_nanval()
        #     sum_volatile_concentration += volatile_concentration
        # return raw_concentration/(1-sum_volatile_concentration)

        '''option 2/2: compute sum of volatiles by accessing pre-computed sum of volatiles, noticeably faster. pre-computing is done in section "initialize (run upon import)". '''
        sum_volatile_concentration = get(element_name='cl+h2o+s', lon=lon, lat=lat, normalize=False, quantity=quantity)
        val = raw_concentration/(1-sum_volatile_concentration)
        return val if val >= 0 else get_nanval()

def visualize(
    element_name: str,
    normalize=False,
    quantity='concentration',
    lon_bounds: tuple = (-180,180),
    lat_bounds: tuple = (-60,60),
    grid_spacing: float = 5,
    colormap='viridis',
    overlay=False,
    transparency_data=0.6,
    transparency_mola=0.9,
    figsize=(10,7)
):
    """
    DESCRIPTION:
    ------------
        Create a map of concentration/sigma for some element.

    PARAMETERS:
    ------------
        element_name : str
            Element for which you want to make a global concentration map. Options are ['al','ca','cl','fe','h2o','k','si','s','th']. Casing does not matter.

        normalize : bool (default False)
            If True, normalize to a volatile-free (Cl, H2O, S) basis. See `get` docstring for more details.

        quantity : str (default 'concentration')
            Quantity to plot. Options are ['concentration', 'sigma'].

        lon_bounds, lat_bounds : tuple(2 floats) (default entire map)
            Bounding box for visualization. Longitude in range [-180, 180], latitude in range [-87.5, 87.5].

        grid_spacing : float (default 5 degrees)
            Spacing between "pixels" in degrees. Note that original data is 5x5 degree bins, so anything smaller than that will be interpolated. Also note that smaller resolutions will take longer to plot.

        colormap : str (default 'jet')
            Colormap to use. See https://matplotlib.org/stable/tutorials/colors/colormaps.html for options.

        overlay : bool (default False)
            If True, overlay a transparent MOLA map on top of the GRS map. Note that this will take longer to plot.

        transparency_data : float (default 0.6)
            If overlay=True, set transparency of the GRS data from 0 (transparent) to 1 (opaque).

        transparency_mola : float (default 0.9)
            If overlay=True, set transparency of the MOLA map from 0 (transparent) to 1 (opaque).

        figsize : (float, float) (default (10,7))
            Width, height in inches.

    NOTES:
    ------------
        The default arguments for `lon_bounds`, `lat_bounds`, and `grid_spacing` will display the original 5x5 bins from the data.

    REFERENCES:
    ------------
        'Mars_HRSC_MOLA_BlendShade_Global_200mp_v2_resized-7.tif'
            > Fergason, R.L, Hare, T.M., & Laura, J. (2017). HRSC and MOLA Blended Digital Elevation Model at 200m. Astrogeology PDS Annex, U.S. Geological Survey.
            - Original download link: https://astrogeology.usgs.gov/search/map/Mars/Topography/HRSC_MOLA_Blend/Mars_HRSC_MOLA_BlendShade_Global_200mp_v2
            - The original file is 5 GB which is unnecessarily high resolution. We downsample the file by reducing the width/height by a factor of 7. Maps with other reduction factors as well as the code to do so can be found here: https://drive.google.com/drive/u/0/folders/1SuURWNQEX3xpawN6a-LEWIduoNjSVqAF.

    """

    lon_left, lon_right = lon_bounds
    lat_bottom, lat_top = lat_bounds

    __checkCoords(lon_left, lat_bottom)
    __checkCoords(lon_left, lat_top)
    __checkCoords(lon_right, lat_bottom)
    __checkCoords(lon_right, lat_top)

    '''
    *** If adapting visualization function, modify this function ***
    '''
    def plotThis(lon, lat):
        val = get(element_name=element_name, lon=lon, lat=lat, normalize=normalize, quantity=quantity)
        return val

    '''dataset to be plotted'''
    dat = [[
        plotThis(lon,lat)
        for lon in np.arange(lon_left, lon_right, grid_spacing)]
        for lat in np.arange(lat_bottom, lat_top, grid_spacing)]

    '''apply mask'''
    dat = np.asarray(dat)
    # dat = np.ma.masked_where((dat < 0), dat)
    dat = np.ma.masked_where((dat == get_nanval()), dat)

    '''plotting'''

    fig = plt.figure(figsize=figsize)
    ax = plt.axes()

    '''mola overlay'''
    if overlay:

        global filepath_mola

        if filepath_mola == '':
            logger = pooch.get_logger()
            logger.disabled = True
            filepath_mola = pooch.retrieve(
                fname='Mars_HRSC_MOLA_BlendShade_Global_200mp_v2_resized-7.tif',
                url=r'https://drive.google.com/file/d/1i278DaeaFCtY19vREbE35OIm4aFRKXiB/view?usp=sharing',
                known_hash='sha256:93d32f9b404b7eda1bb8b05caa989e55b219ac19a005d720800ecfe6e2b0bb6c',
                path=utils.getPath(pooch.os_cache('redplanet'), 'Maps'),
                downloader=utils.download_gdrive_file
            )
            logger.disabled = False

        PIL.Image.MAX_IMAGE_PIXELS = 116159282 + 1 # get around PIL's "DecompressionBombError: Image size (-n- pixels) exceeds limit of 89478485 pixels, could be decompression bomb DOS attack." error

        mola = PIL.Image.open(filepath_mola)

        width, height = mola.size

        left = ( (lon_left+180) / 360 ) * width
        right = ( (lon_right+180) / 360 ) * width
        top = ( (-lat_top+90) / 180 ) * height          # lat values are strange because PIL has (0,0) at the top left. don't think too hard about it, this works.
        bottom = ( (-lat_bottom+90) / 180 ) * height

        mola = mola.crop((left, top, right, bottom))

        im_mola = ax.imshow(mola, cmap='gray', extent=[lon_left, lon_right, lat_bottom, lat_top], alpha=transparency_mola)

        im_dat = ax.imshow(dat[::-1], cmap=colormap, extent=[lon_left, lon_right, lat_bottom, lat_top], alpha=transparency_data)
    else:
        im_dat = ax.imshow(dat[::-1], cmap=colormap, extent=[lon_left, lon_right, lat_bottom, lat_top])

    def chem_cased(s1: str) -> str:
        """Convert a string to chemist's casing."""
        # s2 = ''
        # for i, c in enumerate(s1):
        #     if i == 0 or not(s1[i-1].isalpha()):
        #         s2 += c.upper()
        #     else:
        #         s2 += c
        # return s2
        s2 = [c.upper() if i == 0 or not s1[i-1].isalpha() else c for i, c in enumerate(s1)]
        return ''.join(s2)

    '''titles'''
    ax.set_title(f'{"Normalized" if normalize else "Raw"} {chem_cased(element_name)} Map from GRS')
    ax.set_xlabel('Longitude')
    ax.set_ylabel('Latitude')

    '''axis formatter'''
    ax.xaxis.set_major_formatter('{x}$\degree$')
    ax.yaxis.set_major_formatter('{x}$\degree$')

    # (aesthetic preference)
    if lon_bounds == (-180,180):
        x_spacing = 60
        ax.set_xticks(np.linspace(lon_left, lon_right, int((lon_right-lon_left)/x_spacing)+1))

    '''x ticks'''
    '''Option 1: Set the spacing between x ticks'''
    # x_spacing = 60
    # ax.set_xticks(np.linspace(lon_left, lon_right, int((lon_right-lon_left)/x_spacing)+1))
    '''Option 2: Set the number of x ticks'''
    # x_ticks = 7
    # ax.set_xticks(np.linspace(lon_left, lon_right, x_ticks))

    '''y ticks'''
    '''Option 1: Set the spacing between y ticks'''
    # y_spacing = 25
    # ax.set_yticks(np.linspace(lat_bottom, lat_top, int((lat_top-lat_bottom)/y_spacing)+1))
    '''Option 2: Set the number of y ticks'''
    # y_ticks = 7
    # ax.set_yticks(np.linspace(lat_bottom, lat_top, y_ticks))

    '''color bar'''
    cax = fig.add_axes([ax.get_position().x1+0.02,ax.get_position().y0,0.02,ax.get_position().height])
    cbar = plt.colorbar(im_dat, cax=cax)
    cbar.set_label(f'{chem_cased(element_name)} {quantity.capitalize()} [out of 1]', y=0.5)

    plt.show()

############################################################################################################################################
__init()


import numpy as np
import torch
from model import Classification2DModel, Classification1DModel
from utils import confusion_matrix

def calculate_test_accuracy(dim, gen_test, num_classes):
    device = torch.device("cpu")
    Acc = []
    conf_matrix = torch.zeros(num_classes, num_classes)
    if dim == '1D':
        weight = torch.load('weight/1Dmodel.pth', map_location=device)
        model = Classification1DModel().to(device)
    elif dim == '2D':
        weight = torch.load('weight/2Dmodel.pth', map_location=device)
        model = Classification2DModel().to(device)
    else:
        print('Model dimension is wrong, ues 1D or 2D instead')
        return 0
    model.load_state_dict(weight)
    print('Start Test')
    correct = 0
    total = 0
    test_acc = 0
    with torch.no_grad():
        for iteration, batch in enumerate(gen_test):
            images = batch[0]
            labels = batch[1]
            outputs = model(images)
            _, predicted = torch.max(outputs.data, 1)
            total = total + labels.size(0)
            correct = correct + (predicted == labels).sum().item()
            test_acc = 100 * correct / total
            Acc.append(test_acc)
            conf_matrix = confusion_matrix(predicted, labels, conf_matrix).cpu()
        print('Finish Test')
        accuracy = np.round(np.average(np.array(Acc)),2)
    return accuracy, conf_matrix


# -*- coding: utf-8 -*-
from copy import deepcopy
from lib.graph import Graph
from lib.helper import Helper

class CoverabilityGraph(Graph):
    # states_list contains tuples: (id_state, parent_id_state, [net states], {state_id: transition_id}, transition_id)
    def _create_coverability_graph(self):
        queue = [(0, 0, self.transitions)]
        states_list = [(0, 0, self._get_network_state(self.transitions), {}, None)]
        stop_condition = 20
        state_id = 0
        while queue and state_id < stop_condition:
            state = queue.pop(0)
            parent_state_id = state[0]
            transitions = state[2]

            transitions_ids_to_do = []
            for transition in transitions:
                if transition.is_doable():
                    priority_queue = Helper.get_competitive_transitions_priority_queue(transitions, transition)
                    if priority_queue.get() is transition:
                        transitions_ids_to_do.append(transition.id)

            for transition_id in transitions_ids_to_do:
                new_transitions_state = deepcopy(transitions)
                transition = Helper.find_transition_by_id(new_transitions_state, transition_id)
                transition.run_transition()
                new_network_state = self._get_network_state(new_transitions_state)

                state_id += 1
                states_list_element = (state_id, parent_state_id, new_network_state, {}, transition.id)

                predecessors = self._get_predecessors(states_list, states_list_element)
                self._check_infinite_state(predecessors, new_network_state)
                found_state = self._find_state_based_on_network_state(states_list, new_network_state)

                if found_state is not None:
                    state_id -= 1
                    id = found_state[0]
                    parent_network_state = self._find_state_by_id(states_list, parent_state_id)
                    parent_network_state[3][id] = transition.id
                else:
                    states_list.append(states_list_element)
                    queue.append((state_id, parent_state_id, new_transitions_state))

        return states_list

    def _get_predecessors(self, states_list, state):
        predecessors_list = []
        state_id = state[0]
        parent_id = state[1]
        finish = False
        while not (state_id == 0 and finish is True):
            if state_id == 0:
                finish = True
            parent = self._find_state_by_id(states_list, parent_id)

            if state_id != 0:
                predecessors_list.append(parent)

            parent_id = parent[1]
            state_id = parent[0]

        return predecessors_list

    def _check_infinite_state(self, predecessors, network_state):
        for predecessor in predecessors:
            if predecessor[2] != network_state:
                exist = True
                for i in xrange(len(predecessor[2])):
                    if predecessor[2][i] > network_state[i] and predecessor[2][i] != float("inf"):
                        exist = False
                        break
                if exist:
                    for i in xrange(len(predecessor[2])):
                        if predecessor[2][i] < network_state[i] or predecessor[2][i] == float("inf"):
                            network_state[i] = float("inf")
                    break

    def get_graph(self, parsed_inf=False):
        coverability_graph = self._create_coverability_graph()
        if parsed_inf:
            for state in coverability_graph:
                for index, token in enumerate(state[2]):
                    if token == float("inf"):
                        state[2][index] = "∞"

        return coverability_graph


import os
import numpy as np

data_dir = "/Users/anirudh/Downloads/CSCI_5832_NLP/hw2"
train_data_file = "hw2_training_sets.txt"
test_data_file = "test_set.txt"

vocab_dict = {}
bigram_dict = {}

# Alpha value for additive smoothing
ALPHA = 1e-9

def bigram_probability(word_i_1, word_i, vocab_size):
	try:
		numerator = bigram_dict[(word_i_1, word_i)] + ALPHA
		denominator = vocab_dict[word_i_1] + (vocab_size*ALPHA)
		return numerator / denominator
	except Exception as e:
		numerator = 0 + ALPHA
		denominator = vocab_dict[word_i_1] + (vocab_size*ALPHA)
		return numerator / denominator

def sentence_probability(word_list, vocab_size):
	# Introducing Laplace smoothing or additive smoothing
	# https://en.wikipedia.org/wiki/Additive_smoothing
	log_prob = 0.0
	for i in range(1, len(word_list)):
		log_prob += np.log(bigram_probability(word_i_1=word_list[i-1], word_i=word_list[i], vocab_size=vocab_size))
	prob = 10**log_prob
	return prob

def calculate_sentence_perplexity(word_list, vocab_size, sentence_length):
	log_perplexity = 0.0
	for i in range(1, len(word_list)):
		prob_bigram = bigram_probability(word_i_1=word_list[i-1], word_i=word_list[i], vocab_size=vocab_size)
		log_perplexity += np.log10(prob_bigram)
	log_perplexity = -log_perplexity
	log_perplexity = log_perplexity/sentence_length
	perplexity = 10**log_perplexity
	return perplexity

def normalized_sentence_probability(word_list, vocab_size, sentence_length):
	log_prob = 0.0
	for i in range(1, len(word_list)):
		log_prob += np.log(bigram_probability(word_i_1=word_list[i-1], word_i=word_list[i], vocab_size=vocab_size))
	log_prob = log_prob/sentence_length
	normalized_prob = 10**log_prob
	return normalized_prob

def create_vocab(sent):
	for word in sent:
		try:
			vocab_dict[word] += 1.0
		except Exception as e:
			# print(f"Seeing word: {word} =| for the first time! Adding to vocab.")
			vocab_dict[word] = 1.0

def create_bigram_vocab(word_list):
	for i in range(1, len(word_list)):
		bigram = (word_list[i-1], word_list[i])
		try:
			bigram_dict[bigram] += 1.0
		except Exception as e:
			# print(f"Seeing bigram: {bigram} =| for the first time! Adding to vocab.")
			bigram_dict[bigram] = 1.0

def add_unk_to_vocab():
	# Adding UNK to bigram matrix
	vocab_dict["<UNK>"] = 0
	for word in vocab_dict.keys():
		if word != "<s>" and word != "</s>":
			# Create bigram with [<UNK> word]
			bigram = ("<UNK>", word)
			try:
				bigram_dict[bigram] += 1.0
			except Exception as e:
				# print(f"Seeing bigram: {bigram} =| for the first time! Adding to vocab.")
				bigram_dict[bigram] = 1.0
			# Create bigram with [word <UNK>]
			bigram = (word, "<UNK>")
			try:
				bigram_dict[bigram] += 1.0
			except Exception as e:
				# print(f"Seeing bigram: {bigram} =| for the first time! Adding to vocab.")
				bigram_dict[bigram] = 1.0
			# Add two occurence of <UNK> in the vocab
			vocab_dict["<UNK>"] += 2.0
		elif word == "<s>":
			bigram = (word, "<UNK>")
			try:
				bigram_dict[bigram] += 1.0
			except Exception as e:
				# print(f"Seeing bigram: {bigram} =| for the first time! Adding to vocab.")
				bigram_dict[bigram] = 1.0
			vocab_dict["<UNK>"] += 1.0
		elif word == "</s>":
			bigram = ("<UNK>", word)
			try:
				bigram_dict[bigram] += 1.0
			except Exception as e:
				# print(f"Seeing bigram: {bigram} =| for the first time! Adding to vocab.")
				bigram_dict[bigram] = 1.0
			vocab_dict["<UNK>"] += 1.0

def create_model(data, add_unk=False):
	for sentence in data:
		# print(f"Adding sentence: {sentence} to the model count")
		# Splitting the pre-tokenized training data wo create a word list and appending start and end token
		word_list = sentence.split()
		word_list.append("</s>")
		word_list.insert(0, "<s>")
		# Don't bias the data with empty strings in the training data
		if len(word_list) == 2:
			continue
		# print(word_list)
		create_vocab(word_list)
		create_bigram_vocab(word_list)
	if add_unk:
		add_unk_to_vocab()
	# print("Model Created and Loaded!")

def fill_unk_in_test_sentence(word_list):
	for i in range(len(word_list)):
		if word_list[i] not in vocab_dict.keys():
			word_list[i] = "<UNK>"
	return word_list

def evaluate_model(data, vocab_size):
	for sentence in data:
		print(f"Evaluating sentence: {sentence}")
		# Splitting the pre-tokenized sentence on space
		word_list = sentence.split()
		word_list.append("</s>")
		word_list.insert(0, "<s>")
		word_list = fill_unk_in_test_sentence(word_list=word_list)
		prob = sentence_probability(word_list=word_list, vocab_size=vocab_size)
		normalized_prob = normalized_sentence_probability(word_list=word_list, sentence_length=len(word_list),
														  vocab_size=len(word_list))
		perplexity = calculate_sentence_perplexity(word_list=word_list, sentence_length=len(word_list),
												   vocab_size=len(word_list))
		print(f"Probability: {prob}, Normalized probability: {normalized_prob}, Perplexity: {perplexity}")

if __name__ == "__main__":
	with open(os.path.join(data_dir, train_data_file)) as f:
		train_data = f.readlines()

	with open(os.path.join(data_dir, test_data_file)) as f:
		test_data = f.readlines()

	create_model(data=train_data)
	vocab_size = len(vocab_dict.keys())
	evaluate_model(data=test_data, vocab_size=vocab_size)

"""
How to handle unknown words in test sample.
I add a UNK token to the vocab and with every word in vocab except <s> and </s>
create 2 bigrams as follows:
	1. (word_in_vocab, <UNK>)
	2. (<UNK>, word_in_vocab)
	and increase the count of UNK in the vocab by 2
For <s>
	add a bigram as (<s>, <UNK>)
	and increase the count of UNK in the vocab by 1
For </s>
	add a bigram as (<UNK>, </s>)
	and increase the count of UNK in the vocab by 1

Now, this was for creating the language model.

For evaluating:
Replace all the tokens in the evaluating sentence which are not in the vocab by UNK and
then evaluate the sentence as is
"""


""" si el numero es menor a uno o mayor a 100 Cartel de no esta permitido y el contador  avanza """
""" si es menor al numero pensado ,debe aparecer un cartel de es menor y el contador si avanza """
""" si es mayor al numero pensado ,debe aparecer un cartel de es mayor y el contador si avanza """
""" si acertó ,debe aparecer un cartel de acertado y cuantos intentos le ha tomado """
import random
intentos=0
minimo=1
maximo=100
numeroAleatorio = random.randint(minimo,maximo)
b=0

while intentos<=4:
    numero=int(input('Adivina el numero'))
    intentos=intentos+1
    if intentos==4:
        break
    elif numero==numeroAleatorio:
        print(f"Has acertado en {intentos} intentos")
        b=1
        break
    elif numero<minimo or numero>maximo  :
        print(f"El numero no esta en el rango permitido , intentos {intentos}")
        continue
    elif numero<numeroAleatorio   :
        print(f"El numero es menor al numero Aleatorio, intentos {intentos}")
        continue
    elif numero>numeroAleatorio   :
        print(f"El numero es mayor al numero Aleatorio, intentos {intentos}")
        continue

print("Los intentos han acabado")
if (b==1):
    print("Felicidades ,ganaste el juego" )
else:
    print("Vuelve a intentarlo ,quizas ganes la proxima" )


def minNumberOfJumps(array):
    return helper(0, array, 0)

def helper(current_idx, array, current_jumps):
    if current_idx >= len(array) - 1:
        return current_jumps

    min_array = []
    for i in range(1, array[current_idx] + 1):
        jumps = helper(current_idx + i, array, current_jumps + 1)
        min_array.append(jumps)

    return min(min_array)

def minNumberOfJumps2(array):
    dp = [float('inf') for _ in array]
    dp[0] = 0
    for i in range(1, len(array)):
        for j in range(i):
            if j + array[j] < i:
                continue
            dp[i] = min(dp[i], dp[j] + 1)

    return dp[-1]

print(minNumberOfJumps2([1, 1]))


#!/usr/bin/python3

import os
import pickle
import re
import time

import numpy as np
from miniutils.progress_bar import parallel_progbar

def extract_shape_from_image(npz_file, debug):
    if debug:
        start = time.time()

    numpy_array = np.load(npz_file, mmap_mode="r")

    # Extract file
    if len(numpy_array.files) != 1:
        raise ValueError("Only one file per npz is supported. Please fix: {}".format(npz_file))

    # Extract volume-name
    filename_pattern = "\/([^\/]*)$"
    filename_prog = re.compile(filename_pattern)
    _, volume_name, _ = filename_prog.split(npz_file)

    if debug:
        print('Finished metadata collection for {}, duration: {}s'.format(
            volume_name, int(time.time() - start)))

    return volume_name, numpy_array[numpy_array.files[0]].shape

# Extract needed image file metadata
def create_meta_dirs(image_dir, img_file_suffix, be_nice, debug):
    # Only use free resources on the machine (don't block the machine)
    if be_nice:
        os.nice(18)

    print("Extracting meta-data from images in {}".format(image_dir))

    # Create a list of images to process
    image_files = dict()
    files = []
    for f in os.listdir(image_dir):
        if f.endswith(img_file_suffix):
            files += [(os.path.join(image_dir, f), debug)]

    # Extract the image shapes in parallel
    image_shapes = parallel_progbar(extract_shape_from_image, files, starmap=True)

    # Store the results in format:
    # image_files[file] = shape
    for img in image_shapes:
        npz_file, npz_file_shape = img
        image_files[npz_file] = npz_file_shape

    dataset_size = len(image_files.keys())

    print("Found {} images for meta collection".format(dataset_size))

    with open(os.path.join(image_dir, 'image_meta.pkl'), 'wb') as f:
        pickle.dump(image_files, f)

def create_meta(dataset_dir,
                img_file_suffix=".npz",
                be_nice=True,
                debug=False):
    print("Welcome to the meta-data tool. It will extract important meta-data for training now.")

    # Locate the images
    image_dir = os.path.join(dataset_dir, 'preprocessed')
    create_meta_dirs(image_dir, img_file_suffix, be_nice, debug)

    # Check separate unlabeled image dir
    testset_dir = os.path.join(dataset_dir, "predictionset_preprocessed")
    if os.path.exists(testset_dir):
        create_meta_dirs(testset_dir, img_file_suffix, be_nice, debug)

if __name__ == "__main__":
    # create_meta("/data/datasets/Task07_Pancreas")
    create_meta("/data/datasets/Task06_Lung")
    # create_meta("/data/datasets/Task03_Liver")
    # create_meta("/data/datasets/Task31_KidneySegmentation")


import requests
from bs4 import BeautifulSoup
import re
import pandas as pd

url     = 'https://www.classcentral.com/collection/ivy-league-moocs?subject=data-science'
text    = requests.get(url).text
soup    = BeautifulSoup(text, 'html.parser')

def main(args=None):
    found = findContent()
    # print(type(found))
    output = doingSomething(found)
    # print(output)

def findContent():
    content = soup.find(id = 'page-collection')
    if content:
        page = content.select_one('div[class*="contain-page"]')
        if page:
            widthPage = page.select('div[class*="width-page"]')
            if widthPage:
                for width in widthPage:
                    results = width.select_one('div[class*="results"]')
                    if results:
                        result = results.select_one('ol[class*="course-list"]')
                        if result:
                            each = result.select('li')
                            if each:
                                return each
                            else:
                                print('no each')
                        else:
                            print('no result')
                    else:
                        print('no results')

def doingSomething(found):
    for course in found:
        print('='*60)
        print(course.title)

if __name__ == '__main__':
    main()


import pygame
import random

class Snake:
	def __init__(self, window_width, window_height, space):
		self.reset(window_width, window_height, space)

	def reset(self, window_width, window_height, space):
		self.sprite_width = 50
		self.sprite_height = 50
		self.color = (26, 188, 156)

		self.sprites = [pygame.Surface((self.sprite_width, self.sprite_height)) for i in range(3)]

		self.rects = [sprite.get_rect() for sprite in self.sprites]
		for i in range(len(self.rects)):
			self.sprites[i].fill(self.color)
			space.remove((window_width // 2 - self.sprite_width // 2, window_height // 2 + self.sprite_height * i - self.sprite_height // 2))
			self.rects[i].center = (window_width // 2, window_height // 2 + self.sprite_height * i)

		self.dx = 0
		self.dy = -1
		self.speed = 50

	def update(self, window_width, window_height, space):
		space.append(self.rects[-1].topleft)
		for i in range(-1, -len(self.sprites), -1):
			self.rects[i].center = self.rects[i - 1].center

		self.rects[0].left += self.dx * self.speed
		self.rects[0].top += self.dy * self.speed
		if self.rects[0].left >= window_width:
			self.rects[0].left = 0
		if self.rects[0].right <= 0:
			self.rects[0].right = window_width
		if self.rects[0].top >= window_height:
			self.rects[0].top = 0
		if self.rects[0].bottom <= 0:
			self.rects[0].bottom = window_height
		try:
			space.remove(self.rects[0].topleft)
		except:
			pass

class Food:
	def __init__(self, space):
		self.sprite_width = 50
		self.sprite_height = 50
		self.color = (230, 126, 34)
		self.surface = pygame.Surface((self.sprite_width, self.sprite_height))
		self.rect = self.surface.get_rect()
		self.surface.fill(self.color)
		self.appear(space)

	def appear(self, space):
		self.rect.topleft = random.choice(space)


import sqlite3
import pandas as pd
import tkinter as tk
from tkinter import ttk

# Connect to the database
conn = sqlite3.connect('mydatabase.db')

# Query the database and convert the results to a DataFrame
query = 'SELECT * FROM mytable'
df = pd.read_sql(query, conn)

# Create the GUI window and table
root = tk.Tk()
root.title('SQLite Table Dashboard')
table = ttk.Treeview(root)

# Add columns to the table
table['columns'] = ('User Name', 'Platform Name', 'Starting Time', 'Version Number')

# Format the columns
table.column('#0', width=0, stretch=tk.NO)
table.column('User Name', anchor=tk.CENTER, width=100)
table.column('Platform Name', anchor=tk.CENTER, width=100)
table.column('Starting Time', anchor=tk.CENTER, width=150)
table.column('Version Number', anchor=tk.CENTER, width=100)

# Add headings to the columns
table.heading('#0', text='', anchor=tk.CENTER)
table.heading('User Name', text='User Name', anchor=tk.CENTER)
table.heading('Platform Name', text='Platform Name', anchor=tk.CENTER)
table.heading('Starting Time', text='Starting Time', anchor=tk.CENTER)
table.heading('Version Number', text='Version Number', anchor=tk.CENTER)

# Insert the data into the table
for index, row in df.iterrows():
    table.insert('', index, text='', values=(row['user_name'], row['platform_name'], row['starting_time'], row['version_number']))

# Pack the table and run the GUI loop
table.pack(expand=tk.YES, fill=tk.BOTH)
root.mainloop()