import json
import requests

game_id = 453253378
r = requests.post(
	'https://www.codingame.com/services/gameResultRemoteService/findByGameId',
	json = [str(game_id), None]
)
replay = r.json()
print(replay)
with open(f'{game_id}.json', 'w+') as f:
	f.write(json.dumps(replay))


from collections import defaultdict

class Solution(object):
    def shortestAlternatingPaths(self, n, red_edges, blue_edges):
        """
        :type n: int
        :type red_edges: List[List[int]]
        :type blue_edges: List[List[int]]
        :rtype: List[int]
        """
        redGraph = defaultdict(list)
        blueGraph = defaultdict(list)

        for i, j in red_edges:
            redGraph[i].append(j)
        print(redGraph)
        for i, j in blue_edges:
            blueGraph[i].append(j)

        red_seen = set()
        blue_seen = set()
        queue = []
        queue.append((0, 0))
        queue.append((0, 1))
        red_seen.add(0)
        blue_seen.add(0)

        step = 0
        res = [float("inf") for i in range(n)]

        while queue:
            size = len(queue)
            while size > 0:
                node, color = queue.pop(0)
                if color == 0:
                    for nex in blueGraph[node]:
                        if nex not in blue_seen:
                            queue.append((nex, 1))
                            blue_seen.add(nex)
                            res[nex] = min(res[nex], step + 1)
                else:
                    for nex in redGraph[node]:
                        if nex not in red_seen:
                            queue.append((nex, 0))
                            red_seen.add(nex)
                            res[nex] = min(res[nex], step + 1)
                size -= 1
            step += 1
        res[0] = 0
        return [i if i != float("inf") else -1 for i in res]

python/BFS/Shortest Path with Alternating Colors/Shortest Path with Alternating Colors.py


"""Calculate the liquid volume of an elliptical cylinder on its side.
For example a milk tank on a truck"""
from hcsr04sensor import sensor

trig_pin = 17
echo_pin = 27
# default values
# temperature = 20 celcius
# unit = "metric"
# gpio_mode = GPIO.BCM

# Get litres in a cylinder
height = 30  # cm height of tank on it side
width = 45  # cm width of tank on its side
length = 96  # cm length of tank on its side
sdistance = 32  # cm from the sensor to the bottom
value = sensor.Measurement(trig_pin, echo_pin)
# for imperial add temp and unit and change all cm values to inches
# value = sensor.Measurement(trig_pin, echo_pin, 68, "imperial")
distance = value.raw_distance()

water_depth_metric = value.depth(distance, sdistance)

volume_litres = value.elliptical_side_cylinder_volume(
    water_depth_metric, height, width, length
)
print(
    "The liquid volume of the elliptical cylinder on its side is {} litres".format(
        round(volume_litres, 1)
    )
)


import pathlib
import pickle
import uuid

import attrs
import tensorflow as tf

import morefun.composite_genotypes as cg
import morefun.grammars.backbones as bb
import morefun.grammars.structured_grammatical_evolution as sge
import morefun.grammars.upper_grammars as ugr
import morefun.neural_networks.connections as conn
import morefun.neural_networks.layers as gl
import morefun.neural_networks.optimizers as optimizers
from morefun.grammars.backbones import parse as parse_backbone
from morefun.grammars.optimizers import parse as parse_optimizer

@attrs.frozen(cache_hash=True)
class Phenotype:
    """
    This class exists mostly to be tracked by the NoveltyTracker.
    """

    backbone: bb.Backbone = attrs.field(repr=False)
    connections: conn.ConnectionsSchema = attrs.field(repr=False)
    optimizer: optimizers.Optimizer = attrs.field(repr=False)

    genotype_uuid: uuid.UUID = attrs.field(eq=False, order=False, repr=True)

    def save(this, path: pathlib.Path) -> None:
        serialized = pickle.dumps(this, protocol=pickle.HIGHEST_PROTOCOL)
        path.parent.mkdir(parents=True, exist_ok=True)
        path.write_bytes(serialized)

    @staticmethod
    def load(path: pathlib.Path) -> "Phenotype":
        deserialized = pickle.loads(path.read_bytes())
        assert isinstance(deserialized, Phenotype)
        return deserialized

def translate(
    genotype: cg.CompositeGenotype,
    grammar: ugr.Grammar,
) -> Phenotype:
    tokenstream = sge.map_to_tokenstream(genotype.backbone_genotype, grammar)
    backbone = parse_backbone(tokenstream)
    optimizer = parse_optimizer(tokenstream)
    return Phenotype(
        backbone,
        genotype.connections_genotype,
        optimizer,
        genotype.unique_id,
    )

def make_input_output_tensors(
    phenotype: Phenotype,
    input_layer: gl.Input,
) -> tuple[tf.Tensor, tf.Tensor]:
    output_layer = conn.connect_backbone(
        phenotype.backbone,
        phenotype.connections,
        input_layer,
    )
    known_tensors: dict[gl.ConnectableLayer, tf.Tensor] = {}
    output_layer.to_tensor(known_tensors)
    return known_tensors[input_layer], known_tensors[output_layer]


# Import the socket and math libraries
import socket
import math

# Define the server's IP address and port number
HOST = "127.0.0.1"
PORT = 65432

# Print a message indicating the server is starting and listening for connections
print("Server is starting - listening for connections at IP", HOST, "and port", PORT)

# Define a function to calculate expressions based on user commands
def calculate_expression(expression):
    try:
        parts = expression.split()
        if len(parts) == 3 and parts[1] == "^":
            base = float(parts[0])
            exponent = float(parts[2])
            result = math.pow(base, exponent)
            return f"The result of {expression} is {result}."
        elif len(parts) == 3 and parts[0] == "root":
            degree = float(parts[1])
            number = float(parts[2])
            result = math.pow(number, 1.0 / degree)
            return f"The {degree}-th root of {number} is {result}."
        else:
            return "Invalid expression format. Use 'calculate base ^ exponent' or 'calculate root degree number'."
    except Exception as e:
        return f"Error: {str(e)}"

# Create a socket object using IPv4 and TCP
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    # Bind the socket to the server's address and port
    s.bind((HOST, PORT))
    # Start listening for incoming connections
    s.listen()
    # Accept a client connection and get the client's address
    conn, addr = s.accept()
    with conn:
        print(f"Connected established with {addr}")
        while True:
            # Receive data from the client
            data = conn.recv(1024)
            if not data:
                break

            # Decode the received data as a string
            message = data.decode('utf-8')
            print(f"Received command from client: '{message}'")

            # Perform actions based on the received message
            if message.startswith("calculate "):
                _, expression = message.split(" ", 1)
                response = calculate_expression(expression)
            else:
                response = "Invalid command."

            # Send the response back to the client after encoding it as bytes
            conn.sendall(response.encode('utf-8'))
            print(f"Sent response to client: '{response}'")

        # Print a message indicating the server is done
        print("Server is done!")


from datetime import datetime

# GMT string to GMT Time

dateString = "Oct 28 23:59:59 2020 GMT"
dateFormatter = "%b %d %H:%M:%S %Y GMT"
print(datetime.strptime(dateString, dateFormatter))

# GMT Time to GMT String

x = datetime.now()       #現在時間
print(x.strftime("%Y-%m-%d %H:%M:%S"))

# x = datetime(2018, 6, 1)    # 設定 datetime 物件的時間


class students:

    school = "IT"

    def __init__(self, m1, m2, m3):
        self.m1 = m1
        self.m2 = m2
        self.m3 = m3

    def avg(self):
        return (self.m1 + self.m2 + self.m3)/3

    @classmethod
    def getschool(cls):
        return cls.school

    @staticmethod
    def inf():
        print("student details")

s1 = students(75, 70, 80)
s2 = students(65, 75, 70)

print(s1.avg())
print(s2.avg())

print(students.getschool())

students.inf()


import os
import binascii
from datetime import datetime

def get_file_header(path):
    """
    Retrieve the first two hexadecimal numbers of a file
    """
    data = open(path, 'rb')
    hex_str = binascii.b2a_hex(data.read(2))
    return str(hex_str[:2], 'utf8'), str(hex_str[2:4], 'utf8')

"""
JPEG file header hexadecimal is 0xFFD8FF
PNG file header hexadecimal is 0x89504E
GIF file header hexadecimal is 0x474946
"""
def identify_file_type(directory):
    first_value, second_value = get_file_header(directory)
    first_value = int(first_value, 16)
    second_value = int(second_value, 16)
    code_key = first_value ^ 0xFF
    file_type = 'jpg'

    if first_value ^ 0xFF == second_value ^ 0xD8:
        code_key = first_value ^ 0xFF
        file_type = 'jpg'
    elif first_value ^ 0x89 == second_value ^ 0x50:
        code_key = first_value ^ 0x89
        file_type = 'png'
    elif first_value ^ 0x47 == second_value ^ 0x49:
        code_key = first_value ^ 0x47
        file_type = 'gif'

    return code_key, file_type

def process_file(path, filename):
    file_path = os.path.join(path, filename)
    modification_time = datetime.fromtimestamp(os.path.getmtime(file_path)).strftime('%Y-%m-%d %H.%M.%S')
    code_key, file_type = identify_file_type(file_path)

    directory_name = path.split('\\')[-1]
    if not os.path.exists(directory_name):
        os.mkdir(directory_name)

    source_file = open(file_path, "rb")
    destination_file = open(directory_name + '\\' + modification_time + '-' + filename[:10] + '.' + file_type, "wb")

    for current_byte in source_file:
        for byte in current_byte:
            destination_file.write(bytes([byte ^ code_key]))

    source_file.close()
    destination_file.close()

def process_directory(path):
    files = os.listdir(path)
    print('Total files:{}'.format(len(files)))
    i = 0
    for filename in files:
        try:
            print('{} ... {}'.format(i, filename))
            process_file(path, filename)
            i += 1
        except:
            pass

if __name__=="__main__":
    process_directory(r"C:\Users\FrogHarkness\Documents\WeChat Files\wxid_5d6v5u1a026912\FileStorage\Image\2021-11")


from trainer import *

class MetaLearningMSELoss(nn.Module):

    def __init__(self, reduction='mean', verbose=False):
        super(MetaLearningMSELoss, self).__init__()
        self.lf = MSELoss(reduction=reduction)
        self.verbose = verbose

    def forward(self, predict, target, task_predict):
        """Replaces meta-model predictions with task model predictions
        and after that regular MSE loss is calculated during forward pass.
        During backward pass gradients of test loss from a task-model
        w.r.t. initial weights of meta-model will be calculated.
        """
        if self.verbose: print("initial test loss:", self.lf(task_predict, target))
        if self.verbose: print("predict before replace:", predict)
        if self.verbose: print("meta loss before replace:", self.lf(predict, target))

        # replace meta model prediction with task model prediction
        predict.data = task_predict.data

        # calculate loss
        loss = self.lf(predict, target)

        if self.verbose: print("predict after replace:", predict)
        if self.verbose: print("meta loss after replace:", self.lf(predict, target), loss)

        # check the last bias gradient
        if self.verbose: print("bias grad:", 2 * (predict - target).mean())

        return loss


from sklearn.preprocessing import MinMaxScaler, StandardScaler, MaxAbsScaler, RobustScaler
from sklearn.model_selection import train_test_split
import numpy as np
from torch.utils.data import TensorDataset, DataLoader
import torch

def get_scaler(scaler):
    scalers = {
        "minmax": MinMaxScaler,
        "standard": StandardScaler,
        "maxabs": MaxAbsScaler,
        "robust": RobustScaler,
    }
    if scaler == "minmax":
        return scalers.get(scaler.lower())((3,4.5))
    return scalers.get(scaler.lower())()

def get_data(scaler_type,test_ratio,batch_size,tskone=False):
    if scaler_type == "minmax":
        scaler = get_scaler(scaler_type)
    else:
        scaler = get_scaler(scaler_type)
    outscaler = StandardScaler()

    # load numpy arrays
    x = np.load('./processed_data/anasig_arr.npy')
    y = np.load('./processed_data/displ.npy')
    spikes = np.load('./processed_data/spikes.npy')

    # split and transform dataset
    x_train,x_val,y_train,y_val,spikes_train,spikes_val = train_test_split(x,y,spikes,test_size=test_ratio,shuffle=False)
    x_train_scaled = torch.Tensor(scaler.fit_transform(x_train))
    x_val_scaled = torch.Tensor(scaler.transform(x_val))
    y_train_scaled = torch.Tensor(outscaler.fit_transform(y_train.reshape(-1,1)))
    y_val_scaled = torch.Tensor(outscaler.transform(y_val.reshape(-1,1)))
    spikes_train = torch.Tensor(spikes_train)
    spikes_val = torch.Tensor(spikes_val)

    # put in dataloader
    train = TensorDataset(x_train_scaled,y_train_scaled,spikes_train)
    val = TensorDataset(x_val_scaled,y_val_scaled,spikes_val)
    train_loader = DataLoader(train,batch_size=batch_size,shuffle=False,drop_last=True)
    val_loader = DataLoader(val,batch_size=batch_size,shuffle=False,drop_last=True)
    return train_loader,val_loader,x_train_scaled,x_val_scaled,y_train,y_val,outscaler


from keras.models import Sequential, Model
from keras.layers import *
from crf import CRF
from keras.optimizers import Nadam
from multipliers import *
import tensorflow as tf
from keras.initializers import *

def return_input(cfg):
    x_in = Input((cfg['maxlen'],), name='word')
    pos_tag_in = Input((cfg['maxlen'],), name='pos_tag')
    py_in = Input((cfg['maxlen'],), name='pinyin')
    radical_in = Input((cfg['maxlen'],), name='radical')
    bound_in = Input((cfg['maxlen'],), name='bound')

    x = Embedding(cfg['vocab'], cfg['word_dim'], trainable=True, name='emb')(x_in)
    x = SpatialDropout1D(0.2)(x)

    pos_tag = Embedding(cfg['num_pg'], 16, name='embpos')(pos_tag_in)
    bound = Embedding(cfg['num_bound'], 4, name='embbound')(bound_in)
    pinyin = Embedding(cfg['num_pinyin'], 16)(py_in)
    radical = Embedding(cfg['num_radical'], 16)(radical_in)
    x = concatenate([pos_tag, x, bound], axis=-1)

    return x,{
        'word':x_in,
        'pos_tag':pos_tag_in,
        'bound':bound_in,
    }

def rnn_model(cfg):

    x,inputs = return_input(cfg)
    mask = Lambda(lambda x: K.cast(K.greater(x, 0), 'float32'))(inputs['word'])
    x = Lambda(lambda x: x[0] * K.expand_dims(x[1], axis=-1))([x, mask])

    x = Bidirectional(CuDNNGRU(cfg['unit1'], return_sequences=True, name='layer1'), merge_mode='sum')(x)
    x = Lambda(lambda x: x[0] * K.expand_dims(x[1], axis=-1))([x, mask])
    x = SpatialDropout1D(0.3)(x)
    x = Bidirectional(CuDNNGRU(cfg['unit2'], return_sequences=True, name='layer2',), merge_mode='sum')(x)
    x = Lambda(lambda x: x[0] * K.expand_dims(x[1], axis=-1))([x, mask])

    x = Conv1D(cfg['num_tags'],kernel_size=1,padding='same')(x)
    def softmax(x):
        x = K.exp(x-K.max(x,axis=2,keepdims=True))
        return x/K.sum(x,axis=2,keepdims=True)
    output = Lambda(softmax)(x)

    def focalloss(y_true, y_pred):
        num_c = K.int_shape(y_pred)[-1]

        pad_mask = K.cast(K.greater(y_true, -1), 'float32')
        o_mask = K.cast(K.equal(y_true, 0), 'float32')

        y_true = K.one_hot(K.cast(y_true[:, :, 0], 'int32'), num_c)

        base_loss =  y_true * K.log(y_pred) * pad_mask * ((1-y_pred)**cfg['gamma'])

        o_loss = cfg['o_w'] * o_mask * base_loss
        loss_2 = (1-cfg['o_w']) * (1-o_mask)  * base_loss

        return -K.sum(o_loss+loss_2)/K.sum(pad_mask)

    model = Model(inputs=list(inputs.values()), outputs=[output])

    multipliers = {
        'emb': 0.1,
        'embbound': 0.1,
        'embpos': 0.1,
        'layer1': cfg['lr_layer1']
    }
    model.compile(optimizer=M_Nadam(cfg['lr'], multipliers=multipliers), loss=focalloss)
    return model

def crf_model(cfg):

    x, inputs = return_input(cfg)
    mask = Lambda(lambda x: K.cast(K.greater(x, 0), 'float32'))(inputs['word'])
    x = Lambda(lambda x: x[0] * K.expand_dims(x[1], axis=-1))([x, mask])

    x = Bidirectional(CuDNNGRU(cfg['unit1'], return_sequences=True, name='gru1'), merge_mode='sum')(x)
    x = Lambda(lambda x: x[0] * K.expand_dims(x[1], axis=-1))([x, mask])
    x = SpatialDropout1D(0.3)(x)

    x = Bidirectional(CuDNNGRU(cfg['unit2'], return_sequences=True, name='gru2'), merge_mode='sum')(x)
    x = Lambda(lambda x: x[0] * K.expand_dims(x[1], axis=-1))([x, mask])
    x = SpatialDropout1D(0.15)(x)

    crf = CRF(cfg['num_tags'], sparse_target=True,name='crf')
    output = crf(x, mask=mask)

    model = Model(inputs=list(inputs.values()), outputs=[output])

    multipliers = {
        'emb':0.1,
        'embbound':0.1,
        'embpos':0.1,
        # 'crf':cfg['lr_crf'],
        'gru1':cfg['lr_layer1']
    }
    model.compile(optimizer=M_Nadam(cfg['lr'],multipliers=multipliers), loss=crf.loss_function)
    return model

if __name__ == '__main__':
    pass


import numpy as np
import pytorch_lightning as pl
import torch as th
from torch.utils.data.dataloader import DataLoader

from minerl3161.hyperparameters import MineRLDQNHyperparameters
from minerl3161.pl_pretraining import pl_dataset, pl_model
from minerl3161.buffers import ReplayBuffer
from minerl3161.utils import sample_pt_state

def test_pl_dataset():
    # MineRLDataset's basically just wrap a ReplayBuffer
    n_buffer = 1000

    obs_space = {
        "state": np.zeros(5)
    }

    buffer = ReplayBuffer(n_buffer, obs_space=obs_space)

    for _ in range(n_buffer):
        buffer.add(
            {"state": np.random.rand(5)},
            np.random.randint(10),
            {"state": np.random.rand(5)},
            np.random.rand(),
            np.rint(np.random.rand())
        )

    dataset = pl_dataset.MineRLDataset(buffer)

    assert len(buffer) == len(dataset)

    sample_buffer = buffer[0]
    sample_dataset = dataset[0]

    assert sample_buffer[1] == sample_dataset[1]  # action
    assert sample_buffer[3] == sample_dataset[3]  # reward
    assert sample_buffer[4] == sample_dataset[4]  # done

    # assert states and next states have the same keys in both dataset and buffer
    assert list(sample_buffer[0].keys()) == list(sample_dataset[0].keys()) == \
        list(sample_buffer[2].keys()) == list(sample_dataset[2].keys())

    for key in sample_buffer[0].keys():
        # assert states in both match
        assert (sample_buffer[0][key] == sample_dataset[0][key]).all()

        # assert next states in both match
        assert (sample_buffer[2][key] == sample_dataset[2][key]).all()

def test_pl_model():
    # test we can create the model
    obs_space = {
        "state": np.zeros(5)
    }

    n_actions = 2

    hyperparams = MineRLDQNHyperparameters()
    hyperparams.feature_names = list(obs_space.keys())

    model = pl_model.DQNPretrainer(
        obs_space=obs_space,
        n_actions=n_actions,
        hyperparams=hyperparams
    )

    # test we can do a forward pass on the model

    random_state = sample_pt_state(observation_space=obs_space, features=list(obs_space.keys()), batch=3)

    output = model.forward(random_state)

    # create a small dataset
    n_buffer = 20

    obs_space = {
        "state": np.zeros(5)
    }

    buffer = ReplayBuffer(n_buffer, obs_space=obs_space)

    for _ in range(n_buffer):
        buffer.add(
            {"state": np.random.rand(5)},
            np.random.randint(n_actions, size=(1,1)),
            {"state": np.random.rand(5)},
            np.random.rand(),
            np.rint(np.random.rand())
        )

    dataset = pl_dataset.MineRLDataset(buffer)

    n_train = int(len(dataset) * 0.8)
    n_val = len(dataset) - n_train
    train_set, val_set = th.utils.data.random_split(dataset, [n_train, n_val])

    train_loader = DataLoader(train_set, batch_size=5, shuffle=True)
    val_loader = DataLoader(val_set, batch_size=5, shuffle=False)

    # training
    trainer = pl.Trainer(max_epochs=2)
    trainer.fit(model, train_loader, val_loader)


datalst = ["arun-20","raavi-40","raja-21","somu-5"]
newlst = []
for elem in datalst:
    val = int(elem.split("-")[1])
    if val >= 18 :
        newlst.append(elem.split("-")[0]+ " can vote")
    else :
        newlst.append(elem.split("-")[0]+ " can not vote")

print(newlst)

#comprehension
canvote = [e.split("-")[0] for e in datalst if int(e.split("-")[1])>=18]
print(canvote)

#map
canvote = list(filter(lambda e :  int(e.split("-")[1])>=18 , datalst ))
print(canvote)


"""
# Definition for a Node.
class Node:
    def __init__(self, val, left, right):
        self.val = val
        self.left = left
        self.right = right
"""

# time and space complexity is terrible
# just used this as a practice to convert BST to a DLL
# Gotta find more optimized solution
# a more optimized approach would be to resturucture them into DLL
# while traversing

class DLL:
    def __init__(self):
        self.head = None
        self.tail = None

    def setTail(self, val):
        self.tail = Node(val)

    def setHead(self, val):
        self.head = Node(val)
        self.tail = self.head

    def insertNode(self, val):
        if not self.head:
            self.setHead(val)
            return

        current = self.head
        while current.right:
            current = current.right

        new = Node(val)
        current.right = new
        new.left = current

        self.tail = new

    def attachTailToHead(self):
        head = self.head
        tail = self.tail

        head.left = tail
        tail.right = head

class Solution:
    def treeToDoublyList(self, root: 'Node') -> 'Node':

        if not root:
            return

        res = self.inOrderTrav(root, [])

        l = DLL()

        for i in range(len(res)):
            l.insertNode(res[i])

            if i == len(res) - 1:
                print(i)
                l.attachTailToHead()

        return l.head

    #inorder gets ascending BST values
    def inOrderTrav(self, node, res):
        if not node:
            return

        self.inOrderTrav(node.left, res)
        res.append(node)
        self.inOrderTrav(node.right, res)

        return res


import sqlite3

VeriTabani=sqlite3.connect("kitaplar.sqlite")

imlec=VeriTabani.cursor()
imlec.execute("CREATE TABLE kitaplik (yazar,kitap)")
# Eğer tablo birkere oluşturulduysa birdaha oluşturmaz hata verir.
# rm *sqlite  # terminale yazılarak sqlite ile oluşturulmuş olanlar kaldırılır (remain)
# SQLite uygulamasına girilerek tablo görüntülenebiliyor.

VeriTabani.close()


number = int(input("Enter number to check whether it is prime or not : "))
if number > 1:
    is_prime = True
    for previousnumber in range(2, number):      #when 2 comes range(2,2) will come so loop will not be executed
        if number % previousnumber == 0:
            print(number, "is composite.")
            is_prime = False
            break
    if is_prime == True:
        print(number, "is prime.")
elif number == 1:
    print("1 is neither prime nor composite.")
else:
    print("Enter number greater than 1.")


import os
import subprocess
import unittest

class TestPanelConvert(unittest.TestCase):
    def test_run_panel_convert(self):
        test_file_dir = os.path.dirname(__file__)
        autogluon_dir = os.path.join(test_file_dir, "../../src/autogluon_dashboard/")
        aggregate_file_dir = os.path.join(test_file_dir, f"{autogluon_dir}utils/")
        autogluon_dir = os.path.join(test_file_dir, "../../src/autogluon_dashboard/")
        aggregate_file_dir = os.path.join(test_file_dir, f"{autogluon_dir}utils/")
        process = subprocess.run(["python3", f"{aggregate_file_dir}aggregate_file.py"], capture_output=True, text=True)
        self.assertEqual(process.returncode, 0, f"Aggregate Script did not run successfully. {process.stderr}")
        agg_file_location = os.path.join(os.path.join(test_file_dir, f"{autogluon_dir}"), "index.py")
        web_files_dir = os.path.join(f"{autogluon_dir}", "web_files/")
        per_dataset_test_loc = os.path.join(test_file_dir, "resources/all_data.csv")
        aggregated_test_loc = os.path.join(test_file_dir, "resources/aggregated.csv")
        hware_test_loc = os.path.join(test_file_dir, "resources/metrics.csv")
        os.environ["PER_DATASET_S3_PATH"] = per_dataset_test_loc
        os.environ["AGG_DATASET_S3_PATH"] = aggregated_test_loc
        os.environ["HWARE_METRICS_S3_PATH"] = hware_test_loc
        process = subprocess.run(
            [
                "panel",
                "convert",
                f"{agg_file_location}",
                "--to",
                "pyodide-worker",
                "--out",
                f"{web_files_dir}",
                "--requirements",
                "pandas",
                "holoviews",
                "hvplot",
            ],
            capture_output=True,
            text=True,
        )

        self.assertEqual(process.returncode, 0, f"panel convert did not run successfully. {process.stderr}")
        self.assertEqual(process.stderr.find("Error"), -1, f"panel convert did not run successfully. {process.stderr}")

if __name__ == "__main__":
    unittest.main()


import numpy as np
import os
from collections import OrderedDict
from tqdm import tqdm
import torch
import torch.optim as optim
from torch.utils.data import DataLoader
import torch.nn.functional as F
import torch.backends.cudnn as cudnn

from dataset import BrainDataset, FACE_OBJECT, MALE_FEMALE, ARTIFICIAL_NATURAL, CLASSIFY_ALL
from dataset import DATA_TYPE_TRAIN, DATA_TYPE_VALIDATION, DATA_TYPE_TEST
from model import get_combined_model
from logger import Logger
import options
import export
from early_stopping import EarlyStopping
from utils import get_device, fix_state_dict, save_result, save_predictions, get_test_subject_ids, fix_run_seed

def train_epoch(model, device, train_loader, optimizers, epoch, logger):
    model.train()

    threshold = torch.Tensor([0.5]).to(device)

    correct = 0
    count = epoch * len(train_loader)

    pbar = tqdm(total=len(train_loader))

    for batch_idx, sample_batched in enumerate(train_loader):
        data_f = sample_batched['fmri_data']
        data_e = sample_batched['eeg_data']
        label  = sample_batched['label']
        data_f, data_e, label = data_f.to(device), data_e.to(device), label.to(device)

        for optimizer in optimizers:
            optimizer.zero_grad()
        output = model(data_f, data_e)
        loss = F.binary_cross_entropy(output, label)
        loss.backward()
        for optimizer in optimizers:
            optimizer.step()

        result = (output > threshold).float() * 1
        pred = torch.sum(result == label).item()
        correct += pred

        count += 1
        logger.log("f_loss/train", loss.item(), count)

        pbar.update()

    accuracy = 100.0 * correct / len(train_loader.dataset)

    pbar.close()

    logger.log("f_accuracy/train", accuracy, epoch)
    return accuracy

def eval_epoch(model, device, validation_loader, epoch, logger, record_result=False):
    model.eval()
    validation_loss = 0
    correct = 0

    threshold = torch.Tensor([0.5]).to(device)

    if record_result:
        recorded_labels = []
        recorded_preds = []

    with torch.no_grad():
        for sample_batched in validation_loader:
            data_f = sample_batched['fmri_data']
            data_e = sample_batched['eeg_data']
            label  = sample_batched['label']
            data_f, data_e, label = data_f.to(device), data_e.to(device), label.to(device)

            output = model(data_f, data_e)

            if record_result:
                recorded_labels += list(label.cpu().detach().numpy().reshape(-1))
                recorded_preds += list(output.cpu().detach().numpy().reshape(-1))

            validation_loss = F.binary_cross_entropy(output,
                                               label,
                                               reduction='sum').item()
            # Sum up batch loss
            result = (output > threshold).float() * 1
            pred = torch.sum(result == label).item()
            correct += pred

    validation_loss /= len(validation_loader.dataset)

    accuracy = 100.0 * correct / len(validation_loader.dataset)

    if logger != None:
        logger.log("f_loss/validation", validation_loss, epoch)
        logger.log("f_accuracy/validation", accuracy, epoch)

    if record_result:
        return accuracy, (recorded_labels, recorded_preds)
    else:
        return accuracy

def merge_state_dict(part_state_dict, combined_state_dict):
    for name, param in part_state_dict.items():
        if name not in combined_state_dict:
            continue
        combined_state_dict[name].copy_(param)

def load_pretrained_models(model, device, classify_type, fold, fix_weights,
                           preload_fmri_dir, preload_eeg_dir):
    fmri_model_path = "{}/model_ct{}_{}.pt".format(preload_fmri_dir, classify_type, fold)
    eeg_model_path  = "{}/model_ct{}_{}.pt".format(preload_eeg_dir, classify_type, fold)
    combined_state = model.state_dict()

    fmri_state = torch.load(fmri_model_path, map_location=device)
    eeg_state  = torch.load(eeg_model_path,  map_location=device)

    # Remove "module." from the key name in state_dict when training with data_parallel
    fmri_state = fix_state_dict(fmri_state)
    eeg_state = fix_state_dict(fmri_state)

    merge_state_dict(fmri_state, combined_state)
    merge_state_dict(eeg_state,  combined_state)

    model.load_state_dict(combined_state)

    if fix_weights:
        model.fix_preloads()

def train_fold(args, classify_type, fold):
    device, use_cuda = get_device(args.gpu)
    kwargs = {'num_workers': 4, 'pin_memory': True} if use_cuda else {}

    test_subject_ids = get_test_subject_ids(args.test_subjects)

    train_loader = DataLoader(BrainDataset(data_type=DATA_TYPE_TRAIN,
                                           classify_type=classify_type,
                                           data_seed=args.data_seed,
                                           use_fmri=True,
                                           use_eeg=True,
                                           data_dir=args.data_dir,
                                           fmri_frame_type=args.fmri_frame_type,
                                           fmri_offset_tr=args.fmri_offset_tr,
                                           eeg_normalize_type=args.eeg_normalize_type,
                                           eeg_frame_type=args.eeg_frame_type,
                                           eeg_duration_type=args.eeg_duration_type,
                                           use_smooth=args.smooth,
                                           average_trial_size=args.average_trial_size,
                                           average_repeat_size=args.average_repeat_size,
                                           fold=fold,
                                           test_subjects=test_subject_ids,
                                           subjects_per_fold=args.subjects_per_fold,
                                           unmatched=args.unmatched,
                                           debug=args.debug),
                              batch_size=args.batch_size,
                              shuffle=True,
                              **kwargs)
    validation_loader = DataLoader(BrainDataset(data_type=DATA_TYPE_VALIDATION,
                                                classify_type=classify_type,
                                                data_seed=args.data_seed,
                                                use_fmri=True,
                                                use_eeg=True,
                                                data_dir=args.data_dir,
                                                fmri_frame_type=args.fmri_frame_type,
                                                fmri_offset_tr=args.fmri_offset_tr,
                                                eeg_normalize_type=args.eeg_normalize_type,
                                                eeg_frame_type=args.eeg_frame_type,
                                                eeg_duration_type=args.eeg_duration_type,
                                                use_smooth=args.smooth,
                                                average_trial_size=args.average_trial_size,
                                                average_repeat_size=args.average_repeat_size,
                                                fold=fold,
                                                test_subjects=test_subject_ids,
                                                subjects_per_fold=args.subjects_per_fold,
                                                unmatched=args.unmatched,
                                                debug=args.debug),
                             batch_size=args.batch_size,
                             shuffle=True,
                             **kwargs)

    if args.run_seed >= 0:
        # Fix random seeds at runtime
        fix_run_seed(args.run_seed + fold)

    fmri_ch_size = train_loader.dataset.fmri_ch_size

    # TODO: combiend modelはeeg_duration_typeに対応していない
    assert (eeg_duration_type == "normal")

    model = get_combined_model(args.model_type, fmri_ch_size,
                               args.kernel_size,
                               args.level_size,
                               args.level_hidden_size,
                               args.residual,
                               args.combined_hidden_size,
                               args.combined_layer_size,
                               args.parallel,
                               device)

    # Loading of pretrained model
    if args.preload_fmri_dir is not None or args.preload_eeg_dir is not None:
        # If either preload_fmri_dir or preload_eeg_dir is None, prelaod will not be done
        load_pretrained_models(model, device, classify_type, fold, args.fix_preloads,
                               args.preload_fmri_dir, args.preload_eeg_dir)

    lr = args.lr
    weight_decay = args.weight_decay

    lr_eeg = args.lr_eeg
    lr_fmri = args.lr_fmri
    if lr_eeg is None:
        # Use lr if not specified
        lr_eeg = lr
    if lr_fmri is None:
        # Use lr if not specified
        lr_fmri = lr

    weight_decay_eeg = args.weight_decay_eeg
    weight_decay_fmri = args.weight_decay_fmri
    if weight_decay_eeg is None:
        # Use weight_decay if not specified
        weight_decay_eeg = weight_decay
    if weight_decay_fmri is None:
        # Use weight_decay if not specified
        weight_decay_fmri = weight_decay

    optimizer_fc = optim.Adam(model.parameters_fc(),
                              lr=lr,
                              weight_decay=weight_decay)
    optimizer_eeg = optim.Adam(model.parameters_eeg(),
                               lr=lr_eeg,
                               weight_decay=weight_decay_eeg)
    optimizer_fmri = optim.Adam(model.parameters_fmri(),
                                lr=lr_fmri,
                                weight_decay=weight_decay_fmri)

    optimizers = [optimizer_fc, optimizer_eeg, optimizer_fmri]

    save_dir = args.save_dir
    log_path = os.path.join(save_dir, "log/log_ct{}_{}".format(classify_type, fold))
    logger = Logger(log_path)

    #ignore_epochs = 10
    # No ignore epochs for preloaded model
    ignore_epochs = 0

    early_stopping = EarlyStopping(patience=args.patience,
                                   ignore_epochs=ignore_epochs,
                                   save_dir=save_dir,
                                   fold=fold,
                                   classify_type=classify_type,
                                   debug=args.debug)

    if args.epochs == 0:
        # In the case of no training, only save the model
        early_stopping.save(model)

    for epoch in range(args.epochs):
        train_accuracy = train_epoch(model, device, train_loader, optimizers, epoch, logger)
        print('{}: Train: Accuracy: {:.0f}%'.format(epoch, train_accuracy))

        validation_accuracy  = eval_epoch(model, device, validation_loader, epoch, logger)
        print('{}: Validation:  Accuracy: {:.0f}%'.format(epoch, validation_accuracy))

        if args.early_stopping:
            stop = early_stopping.check_stopping(validation_accuracy, train_accuracy, epoch, model)
            if stop:
                print("stop early: Max Validation Accuracy: {:.0f}%".format(
                    early_stopping.max_validation_accuracy))
                break

        logger.flush()

    logger.close()

    if args.early_stopping:
        return early_stopping.accompanied_train_accuracy, early_stopping.max_validation_accuracy
    else:
        return train_accuracy, validation_accuracy

def train_ten_folds(args, classify_type):
    train_accuracies = []
    validation_accuracies = []

    for fold in range(args.fold_size):
        print("train fold: {}".format(fold))
        train_accuracy, validation_accuracy = train_fold(args, classify_type, fold)

        train_accuracies.append(train_accuracy)
        validation_accuracies.append(validation_accuracy)

    train_accurcy_mean = np.mean(train_accuracies)
    train_accurcy_std = np.std(train_accuracies)
    validation_accurcy_mean = np.mean(validation_accuracies)
    validation_accurcy_std = np.std(validation_accuracies)

    train_result_str = ""
    validation_result_str = ""
    for train_accuracy in train_accuracies:
        train_result_str += "{:.2f}, ".format(train_accuracy)
    for validation_accuracy in validation_accuracies:
        validation_result_str += "{:.2f}, ".format(validation_accuracy)

    print(train_result_str)
    print(validation_result_str)

    results = OrderedDict([
        ("train_accurcy_mean",train_accurcy_mean),
        ("validation_accurcy_mean",validation_accurcy_mean),
        ("train_accurcy_std",train_accurcy_std),
        ("validation_accurcy_std",validation_accurcy_std),
        ("train_result",train_result_str),
        ("validation_result",validation_result_str)])

    save_result(args.save_dir, classify_type, results)

def test_fold(args, classify_type, fold):
    device, use_cuda = get_device(args.gpu)
    kwargs = {'num_workers': 4, 'pin_memory': True} if use_cuda else {}

    test_subject_ids = get_test_subject_ids(args.test_subjects)

    test_loader = DataLoader(BrainDataset(data_type=DATA_TYPE_TEST,
                                          classify_type=classify_type,
                                          data_seed=args.data_seed,
                                          use_fmri=True,
                                          use_eeg=True,
                                          data_dir=args.data_dir,
                                          fmri_frame_type=args.fmri_frame_type,
                                          fmri_offset_tr=args.fmri_offset_tr,
                                          eeg_normalize_type=args.eeg_normalize_type,
                                          eeg_frame_type=args.eeg_frame_type,
                                          eeg_duration_type=args.eeg_duration_type,
                                          use_smooth=args.smooth,
                                          average_trial_size=args.average_trial_size,
                                          average_repeat_size=args.average_repeat_size,
                                          fold=fold,
                                          test_subjects=test_subject_ids,
                                          subjects_per_fold=args.subjects_per_fold,
                                          unmatched=args.unmatched,
                                          debug=args.debug),
                             batch_size=args.batch_size,
                             shuffle=False,
                             **kwargs)

    if args.run_seed >= 0:
        # Fix random seeds at runtime
        fix_run_seed(args.run_seed + fold)

    fmri_ch_size = test_loader.dataset.fmri_ch_size

    model = get_combined_model(args.model_type, fmri_ch_size,
                               args.kernel_size,
                               args.level_size,
                               args.level_hidden_size,
                               args.residual,
                               args.combined_hidden_size,
                               args.combined_layer_size,
                               args.parallel,
                               device)

    # No need to load a pretrained model here

    model_path  = "{}/model_ct{}_{}.pt".format(args.save_dir, classify_type, fold)
    state = torch.load(model_path, map_location=device)
    state = fix_state_dict(state)

    model.load_state_dict(state)

    test_accuracy, (recorded_labels, recorded_preds) = eval_epoch(
        model, device, test_loader, 0, None, record_result=True)

    return test_accuracy, (recorded_labels, recorded_preds)

def test_ten_folds(args, classify_type):
    print("start ten fold test: classify_type={}".format(classify_type))

    test_accuracies = []

    for fold in range(args.fold_size):
        print("test fold: {}".format(fold))
        test_accuracy, (recorded_labels, recorded_preds) = test_fold(
            args, classify_type, fold)
        test_accuracies.append(test_accuracy)
        # Save predicted values
        save_predictions(args.save_dir, classify_type, fold, recorded_labels, recorded_preds)

    test_accurcy_mean = np.mean(test_accuracies)
    test_accurcy_std = np.std(test_accuracies)

    test_result_str = ""
    for test_accuracy in test_accuracies:
        test_result_str += "{:.2f}, ".format(test_accuracy)

    print(test_result_str)

    results = OrderedDict([
        ("test_accurcy_mean",test_accurcy_mean),
        ("test_accurcy_std",test_accurcy_std),
        ("test_result",test_result_str)])

    save_result(args.save_dir, classify_type, results, for_test=True)

def main():
    args = options.get_combined_args()
    options.save_args(args)

    if args.test == False:
        # Train
        if args.classify_type == CLASSIFY_ALL:
            train_ten_folds(args, classify_type=FACE_OBJECT)
            train_ten_folds(args, classify_type=MALE_FEMALE)
            train_ten_folds(args, classify_type=ARTIFICIAL_NATURAL)
            title = "exp: {}".format(args.save_dir)
            export.export_results(args.save_dir, title)
        else:
            train_ten_folds(args, classify_type=args.classify_type)
    else:
        # Test
        if args.classify_type == CLASSIFY_ALL:
            test_ten_folds(args, classify_type=FACE_OBJECT)
            test_ten_folds(args, classify_type=MALE_FEMALE)
            test_ten_folds(args, classify_type=ARTIFICIAL_NATURAL)
        else:
            test_ten_folds(args, classify_type=args.classify_type)

if __name__ == '__main__':
    main()


from OptoTransportAnalysis.Transport import TransportData
from OptoTransportAnalysis.Optics import OpticsData
import OptoTransportAnalysis as ota
import OptoTransportAnalysis.Transport as td
import OptoTransportAnalysis.Optics as od

def main():
    # first test: check if user input works to instantiate TransportData object by comparing filename
    testData = TransportData()
    print(f'Filename of testData object is {testData.filename}')
    match = input('Does this match the chosen filename? y/n')
    assert(match == 'y'), 'Chosen filename does not match testData filename'

    # second test: check if user input works to instantiate OpticsData object by comparing filename
    testData = OpticsData()
    print(f'Filename of testData object is {testData.filename}')
    match = input('Does this match the chosen filename? Enter \'y\' if so')
    assert(match == 'y'), 'Chosen filename does not match testData filename'

    # third test: check if giving filename in argument works
    testFile = '/Users/jackbarlow/Dropbox/Jack B/data/ISBT/Stencil mask 10L & EBL 11L/bluefors 20220909/databases/52_ISBT_0928_DCBiasSweep_2fTest.db'
    testData = TransportData(testFile)
    assert(testData.filename == testFile), 'testData filename does not match testFile'

    print("Test complete, no errors")
    return

if __name__ == "__main__":
    main()


# Write a Python function that checks whether a passed in string is palindrome or not.
def isPalindrome(str):
    for i in range(0, int(len(str)/2)):
        if str[i] != str[len(str)-i-1]:
            return False
    return True
str = input("Enter the string: ")
ans = isPalindrome(str)
if ans:
    print("String is PALINDROME")
else:
    print("String is NOT PALINDROME")


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

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import boto3
#from final_local.models import Ads_motorbike, db_connect, create_table

class GumtreeScraperPipeline(object):
    def __init__(self):
        """
        Initializes database connection and sessionmaker
        Creates tables
        """
        self.dynamodb = boto3.resource('dynamodb', region_name='us-east-2', endpoint_url="https://dynamodb.us-east-2.amazonaws.com")

    def process_item(self, item, spider):
        """Save ads in the database
        This method is called for every item pipeline component
        """
        table = self.dynamodb.Table('ads_motorbike2')

        try:
            print("try")
            response = table.get_item(
            Key={
                'title': item['title'],
            }
            )
            print(response['Item']['title'])
        except:
            print("Adding in the DB:")
            table.put_item(
             Item=dict(item)
            )

        return item


"""Saved serializers."""

# Django REST Framework
from rest_framework import serializers

# Models
from app.posts.models import Saved, CategorySaved

# Serializers
from app.posts.serializers import PostModelSerializer

class CategorySavedModelSerializer(serializers.ModelSerializer):
    """Category saved model serializer."""

    user = serializers.StringRelatedField(read_only=True)

    class Meta:
        """Meta options."""
        model = CategorySaved
        fields = ['user', 'name']
        read_only_fields = ['user']

    def create(self, data):
        """Create category."""
        return CategorySaved.objects.create(**data, user=self.context['user'])

class SavedPostModelSerializer(serializers.ModelSerializer):
    """Saved post model serializer."""

    user = serializers.StringRelatedField(read_only=True)
    post = PostModelSerializer(read_only=True)
    saved_category = serializers.StringRelatedField()

    class Meta:
        """Meta options."""
        model = Saved
        fields = [
            'user','post',
            'saved_category'
        ]

        read_only_fields = [
            'user', 'post',
            'saved_category'
        ]

    def create(self, data):
        """Create saved."""
        saved = Saved.objects.create(
            user=self.context['user'],
            post=self.context['post'],
            saved_category=self.context['saved_category'])
        return saved


import fileinput
import re
import os
import sys

# python3 preprocessing_user.py yelp_academic_dataset_user.json
new_review_file = open("yelp_user_clean.json", "w")
counter = 0
for line in fileinput.input():
	counter += 1
	line = re.sub('("friends".*\],\s)',"", line.rstrip())
	votes_matcher = re.compile('"votes":\s\\{([^}]*)},')
	line = votes_matcher.sub('\\1,', line)
	compliment_matcher = re.compile('"compliments":\s(\\{[^}]*})')
	m = compliment_matcher.search(line)
	x = eval(m.group(1))
	#print(x)
	total = sum([int(x[key]) for key in x])
	line = re.sub('"compliments":\s(\{[^}]*})', '"compliments": '+str(total), line.rstrip())
	line = re.sub('("type":.*",\s)', "", line.rstrip())
	elite_matcher = re.compile('"elite":\s([^\]]*\])')
	m = elite_matcher.search(line)
	x = m.group(1)
	if x != "[]":
		line = re.sub('"elite":\s([^\]]*\])', '"elite": 1', line.rstrip())
	else:
		line = re.sub('"elite":\s([^\]]*\])', '"elite": 0', line.rstrip())
	new_review_file.write(line + "\n")
	if counter%50000 == 0:
		print(str(counter) + " records processed")
		sys.stdout.flush()
print("Processing complete, "+str(counter) +" records processed!")
sys.stdout.flush()
new_review_file.close()


import torch
from langchain.llms import CTransformers
from langchain.chains import LLMChain
from langchain import PromptTemplate
import gradio as gr
import time

custom_prompt_template = """
You are an AI Chatbot named Sunny, created by 'Sic Team', and your task is to provide information to users and chat with them based on given user's query. Below is the user's query.
Query: {query}

You just return the helpful message in English and always try to provide relevant answers to the user's query.
"""

def set_custom_prompt():
    prompt = PromptTemplate(
        template=custom_prompt_template, input_variables=['query'])
    return prompt

def load_model():
    # Check if GPU is available
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    print(f"Using device: {device}")

    # Load the locally downloaded model here, specifying the device
    llm = CTransformers(
        model="TheBloke/zephyr-7B-beta-GGUF",
        model_type="mistral",
        max_new_tokens=4384,
        temperature=0.2,
        repetition_penalty=1.13,
        device=device  # Set the device explicitly during model initialization
    )

    return llm

def chain_pipeline():
    llm = load_model()
    main_prompt = set_custom_prompt()
    main_chain = LLMChain(prompt=main_prompt, llm=llm)
    return main_chain

llmchain = chain_pipeline()

def bot(query):
    llm_response = llmchain.run({"query": query})
    return llm_response

with gr.Blocks(title='Sunny', css="footer {visibility: hidden}; background-color: #000; overflow: hidden") as main:
    gr.HTML("""
        <style>
            ::-webkit-scrollbar {
                width: 10px; /* Set the width of the scrollbar */
            }

            ::-webkit-scrollbar-track {
                background: #000; /* Set the track background color to black */
            }

            ::-webkit-scrollbar-thumb {
                background-color: #fff; /* Set the thumb color to black */
                border-radius: var(--scroll-radius);
                height: 10px;

            }
        </style>
    """)

    gr.Markdown("# Sunny Chatbot")
    chatbot = gr.Chatbot([], elem_id="chatbot", height=660)
    msg = gr.Textbox()
    clear = gr.ClearButton([msg, chatbot])
    css="footer {visibility: hidden}"

    def respond(message, chat_history):
        bot_message = bot(message)
        chat_history.append((message, bot_message))
        time.sleep(2)
        return "", chat_history

    msg.submit(respond, [msg, chatbot], [msg, chatbot])

main.launch(share=False)


import pandas as pd
from habanero import Crossref
from tqdm import tqdm
from fuzzywuzzy import fuzz

def get_user_input(prompt, valid_columns):
    while True:
        user_input = input(prompt)
        if user_input in valid_columns:
            return user_input
        else:
            print("Column not found. Try again.")

def main():
    # Create a CrossRef client
    cr = Crossref()

    # Initialize df as None outside the try-except block
    df = None

    # Ask the user for the file name and validate its existence
    while True:
        filename = input("Enter the file name: ")
        try:
            df = pd.read_excel(f"{filename}")
            break
        except FileNotFoundError:
            print("File not found. Try again.")

    # Ask the user for column names and validate their existence
    if df is not None:
        title_column = get_user_input("Enter the column name containing the article title: ", df.columns)
        first_author_column = get_user_input("Enter the column name containing the first author's name (leave blank if not applicable): ", df.columns)
        doi_column = get_user_input("Enter the column name containing the DOI number: ", df.columns)
        doi_link_column = get_user_input("Enter the column name containing the DOI link: ", df.columns)

    # Initialize a new DataFrame for the updated data
    updated_df = df.copy()

    # Create a tqdm progress bar
    with tqdm(total=len(df)) as pbar:
        # Create a dictionary to store query and result information
        query_results = {}

        # Iterate over the selected rows
        for index, row in df.iterrows():
            article_title = row[title_column]
            first_author_name = row[first_author_column] if first_author_column else ""

            if pd.notna(article_title):
                if pd.notna(first_author_name):
                    query = f"title:{article_title} author:{first_author_name}"
                else:
                    query = f"title:{article_title}"
                try:
                    results = cr.works(query=query, limit=10)

                    # Convert the article title to lowercase
                    article_title_lower = article_title.lower()

                    # Initialize variables for closest result
                    closest_result = None
                    closest_score = None

                    # Iterate through the results to find the closest match
                    for result in results['message']['items'][:10]:
                        result_title_lower = result['title'][0].lower()
                        similarity_score = fuzz.ratio(article_title_lower, result_title_lower)

                        if closest_result is None or similarity_score > closest_score:
                            closest_result = result
                            closest_score = similarity_score

                    # Check if a close match was found
                    if closest_result is not None:
                        doi = closest_result.get('DOI', '')
                        doi_url = f"https://doi.org/{doi}"
                        updated_df.at[index, doi_column] = doi
                        updated_df.at[index, doi_link_column] = doi_url

                except Exception as e:
                    print(f"An error occurred while querying CrossRef: {str(e)}")

            # Update the progress bar
            pbar.update(1)

    # Save the updated DataFrame to a new Excel file
    output_filename = input("Enter the output file name (or leave blank to overwrite the input file): ").strip()
    if not output_filename:
        output_filename = filename
    updated_df.to_excel(output_filename, index=False, engine='openpyxl')
    print(f"Updated data saved to {output_filename}")

if __name__ == "__main__":
    main()


from __future__ import absolute_import
from __future__ import print_function
import os,sys,codecs,numpy as np,random,imp,time,random
#try:
    #import ujson as json
import simplejson as json
#except ImportError:
#	import json
from datetime import datetime as dt
import unicodecsv as csv
from collections import defaultdict,Counter
from .config import CONFIG
import six
from six.moves import range
from tqdm import tqdm
import random,shutil
import inspect
DEFAULT_PATH_KEY='_path'
DEFAULT_EXT = 'txt'

PATH_KEY=CONFIG.get('PATH_KEY','')
PATH_EXT=CONFIG.get('PATH_EXT','').replace('.','')

if not PATH_KEY: PATH_KEY=DEFAULT_PATH_KEY
if not PATH_EXT: PATH_EXT=DEFAULT_PATH_EXT

TXT_MAXCOLS=25000
from smart_open import open
from tqdm import tqdm

def slingshot_single_shot(stone,path):
    return stone(path)

def get_paths_already_finished_from_cache(cache_path):
    for path,result in stream_results(cache_path,flatten=False,progress=False):
        if result:
            if type(path)==list: path=tuple(path)
            if type(path)==dict: path=tuple(sorted(list(path.items())))
            yield path

def now(now=None):
    import datetime as dt
    if not now:
        now=dt.datetime.now()
    elif type(now) in [int,float,str]:
        now=dt.datetime.fromtimestamp(now)

    return '{0}-{1}-{2} {3}:{4}:{5}'.format(now.year,str(now.month).zfill(2),str(now.day).zfill(2),str(now.hour).zfill(2),str(now.minute).zfill(2),str(now.second).zfill(2))

data_slingshot='.data_slingshot/'

def shoot(func, objects, path_src=None, savedir=None, overwrite=False, **kwargs):
    # init savedir
    funcname = func if type(func)==str else func.__name__
    if type(func)==str and (not path_src or not os.path.exists(path_src)):
        print('`func` must be real function object, or if a string, `path_src` must be specified.')
        return

    # init
    if not savedir: savedir=f'_data_/{funcname}'
    # if not savedir: savedir=f'_data_/{funcname}/{now().split()[0]}'
    if os.path.exists(savedir) and overwrite: shutil.rmtree(savedir)
    if not os.path.exists(savedir): os.makedirs(savedir)
    if not 'resume' in kwargs: kwargs['resume']=True
    if not 'nosave' in kwargs: kwargs['nosave']=True
    path_code=f'{savedir}/func.py'
    path_objs=f'{savedir}/input.jsonl'

    # get source of function
    if type(func)!=str:
        func_txt=inspect.getsource(func)
        with open(path_code,'w') as of:
            of.write(func_txt)
    else:
        shutil.copyfile(path_src,path_code)

    # save objects
    with open(path_objs,'w') as of:
        for obj in tqdm(objects,total=len(objects),desc='[Slingshot] Preparing input'):
            of.write(json.dumps(obj)+'\n')

    # call command
    cmd=f'slingshot -code {path_code} -func {funcname} -pathlist {path_objs} -savedir {savedir} -resume'
    for k,v in kwargs.items():
        cmd+=f' -{k} {v}' if v is not True else f' -{k}'
    return cmd

def gather(savedir):
    return list(stream_results(os.path.join(savedir,'cache')))

def slingshot(path_sling=None,stone_name=None,stone_args=None,paths=None,llp_corpus=None,limit=None,path_source=None,stone=None,path_key=PATH_KEY,path_ext=None,path_prefix='',path_suffix='',cache_results=True,cache_path=None,save_results=True,results_dir=None,shuffle_paths=True,do_stream_results=True,save_txt=True,txt_maxcols=TXT_MAXCOLS,sling_args=[],sling_kwargs={},num_runs=1,oneshot=False,llp_pass_text=False,llp_method='',progress_bar=True,savecsv='',resume=False,overwrite=False,parallel=1,llp_pass_path='path'):
    """
    Main function
    """
    progress_bar=True# if not sbatch else False

    # Load code-sling and stone-function
    if not stone and not llp_method:
        stone=load_stone_in_sling(path_sling,stone_name)
        if not stone:
            return

    # One shot?
    if oneshot:
        return stone(path_source,*sling_args,**sling_kwargs)

    # Load paths
    all_paths = None
    if llp_corpus and llp_corpus!='None':
        try:
            import llp
            #print(llp,dir(llp))
            corpus = llp.load_corpus(llp_corpus)
            #print(corpus.path_metadata)
            #print(llp_corpus, corpus)
            #print('llp_pass_path',llp_pass_path)
            #print('llp_pass_text',llp_pass_text)
            #print('llp_method',llp_method)
            #print(corpus.texts())
            all_paths = [(text.addr if (llp_pass_text or llp_method) else getattr(text,llp_pass_path)) for text in corpus.texts()][:limit]
            #print(all_paths[:10])
            if shuffle_paths:
                import random
                random.shuffle(all_paths)
        except ImportError as e:
            print('!!',e)
            pass

    # Get paths!
    if not all_paths:
        all_paths = load_paths(path_source,path_ext,limit,shuffle_paths,path_key,path_prefix,path_suffix) if not paths else paths

    # Break if these weren't returned
    if not all_paths: return

    # Report back
    #print('>> [Slingshot] # of paths:',len(all_paths))

    # Cache stuff
    if not results_dir: results_dir=f'data_slingshot/{stone_name}'
    if not cache_path: cache_path=os.path.join(results_dir,'cache')
    if cache_results:
        if overwrite and os.path.exists(cache_path): delete_files_from_folder(cache_path)
        if overwrite and os.path.exists(results_dir): delete_files_from_folder(results_dir)
        if not os.path.exists(cache_path):
            try:
                os.makedirs(cache_path)
            except FileExistsError:
                pass
        if resume:
            paths_done = set(list(get_paths_already_finished_from_cache(cache_path)))
            print('\n>> [Slingshot] already finished %s of %s' % (len(paths_done),len(all_paths)))
            try:
                all_paths=list(set(all_paths)-paths_done)
            except TypeError:
                pass

        import random
        if shuffle_paths: random.shuffle(all_paths)

    ## de-tuple?
    all_paths = [
        dict(p) if (p and type(p)==tuple and set(len(x) for x in p)=={2}) else p
        for p in all_paths
    ]

    ## RUNS?
    # Multiply paths by runs
    all_paths = [(path,run+1) for path in all_paths for run in range(num_runs)]

    t1 = dt.now()
    if parallel < 2:
        paths=all_paths
        results=[]
        looper=all_paths if not progress_bar else tqdm(all_paths,file=sys.stdout,desc='Slingshot',position=0,ncols=100)
        if cache_results and cache_path:
            cache_fn = 'results.jsonl'
            cache_fnfn = os.path.join(cache_path,cache_fn)
            cache_writer = open(cache_fnfn, mode='a+',encoding='utf-8')
        rank = 0

    else:
        # Start MPI
        from mpi4py import MPI

        comm = MPI.COMM_WORLD
        size = comm.Get_size()
        rank = comm.Get_rank()
        #if rank==0: print()
        #print('>> [Slingshot] initializing MPI with size %s and rank %s' % (size,rank))

        # Am I the seed process?
        if rank == 0:
            # Farm out paths to other processes
            segments = np.array_split(all_paths,size) if size>1 else [all_paths]
            print('>> [Slingshot] %s paths divided into %s segments' % (len(all_paths), len(segments)))

        # Or am I a process created by the seed?
        else:
            segments = None

        # Scatter the segments (if rank is 0?)
        segment = comm.scatter(segments, root=0)

        # Parse the segment.
        paths = segment

        # cache results?
        cache_writer=None
        if cache_results and cache_path:
            cache_fn = 'results.rank=%s.jsonl' % str(rank).zfill(4)
            cache_fnfn = os.path.join(cache_path,cache_fn)
            cache_writer = open(cache_fnfn, mode='a+',encoding='utf-8')

        #progress_bar=True
        desc=f'>> [Slingshot] Compute worker {rank}'
        looper=paths if not progress_bar else tqdm(paths,file=sys.stdout,desc=desc,position=rank)

    # let's go! loop over the paths
    results=[]
    num_paths=len(paths)
    pronoun='their'
    zlen=len(str(num_paths))
    zlen_rank=3 #len(str(size))

    for i,(path,run) in enumerate(looper):
        #################################################
        # THIS IS WHERE THE STONE FITS INTO THE SLINGSHOT

        sling_kwargs2=dict(list(sling_kwargs.items()))
        #sling_kwargs2['results_dir']=results_dir
        if num_runs>1: sling_kwargs2['run']=run

        path_store = None

        if llp_pass_text and not llp_method:
            idx = path
            text = corpus.text(path)
            path_store = text.addr
            path = text.path_txt

        if llp_method:
            idx = path
            try:
                text = corpus.text(path)
                path = text.addr
                stone = getattr(text,llp_method)
            except AttributeError as e:
                print("!!",e)
                return
            result = stone(*sling_args,**sling_kwargs2)

        else:
            try:
                result=stone(path,*sling_args,**sling_kwargs2)
            except TypeError:
                result=stone(path,*sling_args,**sling_kwargs)

        if result is not None:
            path_store = path if not path_store else path_store
            path_result=(path_store,result)
            if not do_stream_results: results+=[path_result]
            #if cache_writer:
            if cache_writer:
                #cache_writer.write(path_result) # when using jsonlines
                #try:
                #jsonl=json.dumps(path_result)
                jsonl=json.dumps(path_result,ignore_nan=True)
                cache_writer.write(jsonl + '\n')
                #except:
                #	print "!! could not write to results file!"
        #################################################
        #print('>> Clone #%s slings %s at #%s of %s %s enemies!' % (str(rank).zfill(zlen_rank),stone_name,str(i+1).zfill(zlen),pronoun,num_paths))
        #print('>> Clone #%s slings %s at Target #%s (of %s)' % (str(rank).zfill(zlen_rank),stone_name,str(i+1).zfill(zlen),num_paths))
        #if not progress_bar: print('>> Clone #%s -- slings --> Target #%s / %s' % (str(rank).zfill(zlen_rank),str(i+1).zfill(zlen),num_paths))
    if cache_writer: cache_writer.close()

    # Gather the results
    if parallel>1: RESULTS = comm.gather(results, root=0)

    # If I am the seed process again
    if rank == 0:
        t3 = dt.now()
        print('>> [Slingshot] Finished parsing in %s seconds.' % (t3-t1).total_seconds())

        # Save results...
        if save_results and results_dir:
            # Make dir...
            if not os.path.exists(results_dir): os.makedirs(results_dir)

            # Copy pathlist
            results_fnfn_pathlist = os.path.join(results_dir,'pathlist.txt')
            results_fnfn_metadata = os.path.join(results_dir,'metadata.txt')
            save_results_pathlist(results_fnfn_pathlist,results_fnfn_metadata,all_paths,path_source)

            # Stream-save TSV
            results_fnfn_txt = os.path.join(results_dir,'results.txt') if not savecsv else savecsv
            if save_txt: save_results_txt(results_fnfn_txt,cache_path,txt_maxcols)

        # Exit
        t4 = dt.now()
        print('>> [Slingshot] Finished everything in %s seconds!' % (t4-t1).total_seconds())

def save_results_pathlist(results_fnfn_pathlist,results_fnfn_metadata,paths,path_source):
    with open(results_fnfn_pathlist,'w') as of:
        for (path,run) in paths:
            try:
                of.write(str(path)+'\n')
            except TypeError:
                of.write(str(path.addr) + '\n')

    path_pathlists = CONFIG.get('PATH_PATHLISTS','')
    pathlist_path_source = os.path.join(path_pathlists,path_source if path_source else '')

    #print(path_source,'!?!?!?')
    if not path_source:
        pass
    elif os.path.exists(path_source) and is_csv(path_source):
        from shutil import copyfile
        copyfile(path_source, results_fnfn_metadata)
    elif os.path.exists(pathlist_path_source) and is_csv(path_source):
        from shutil import copyfile
        copyfile(path_source, results_fnfn_metadata)

def load_stone_in_sling(path_sling,stone_name, exts=['','.py','.ipynb']):
    if not path_sling or not stone_name:
        print('!! sling or stone not specified')
        return
    elif not os.path.exists(path_sling):
        new_path=None
        for ext in exts:
            abs_path_sling_ext=os.path.join(CONFIG['PATH_SLINGS'],path_sling+ext)
            #print(abs_path_sling_ext)
            if os.path.exists(abs_path_sling_ext):
                new_path=abs_path_sling_ext
                break
        if not new_path:
            print("!!",path_sling,"does not exist")
            return
        path_sling=new_path

    if path_sling.endswith('.py'):
        try:
            import importlib.util
            spec = importlib.util.spec_from_file_location("sling", path_sling)
            sling = importlib.util.module_from_spec(spec)
            spec.loader.exec_module(sling)
        except ImportError:
            import imp
            sling = imp.load_source('sling', path_sling)

        stone = getattr(sling,stone_name)
        return stone

    elif path_sling.endswith('.ipynb'):
        import nbimporter
        nbimporter.options['only_defs'] = CONFIG.get('NBIMPORTER_ONLY_DEFS',False)

        ppath,pfn = os.path.split(path_sling)
        pname,pext = os.path.splitext(pfn)

        NBL = nbimporter.NotebookLoader(path=[ppath])
        sling = NBL.load_module(pname)
        stone = getattr(sling,stone_name)
        return stone

    elif path_sling.endswith('.R'):
        from rpy2.robjects import r as R
        # load all source
        with open(path_sling) as f:
            code=f.read()

            R('library(RJSONIO)')
            rfunc = R(code)
            #print('done!')
            stone = lambda _path: rconvert(rfunc(_path))
            return stone

def get_all_paths_from_folder(rootdir,ext='.txt'):
    for root, subdirs, files in os.walk(rootdir,followlinks=True):
        for fn in files:
            if fn.endswith(ext):
                yield os.path.join(root,fn)

def get_paths_from_csv(_fnfn,path_key=PATH_KEY,path_ext=PATH_EXT,path_prefix='',path_suffix='',sep='\t'):
    paths=[]
    #with codecs.open(_fnfn,encoding='utf-8') as pf:
    if not path_key: path_key=DEFAULT_PATH_KEY
    with open(_fnfn) as pf:
        reader=csv.DictReader(pf,delimiter=sep)
        for dx in reader:
            path=dx.get(path_key,'')
            if not path: continue
            if path_prefix: path=os.path.join(path_prefix,path)
            if path_suffix: path=path+path_suffix
            if path: paths+=[path]
    return paths

def is_csv(_fnfn,sep='\t'):
    if not os.path.exists(_fnfn): return False
    if os.path.isdir(_fnfn): return False
    with open(_fnfn) as pf:
        first_line=pf.readline()
        return sep in first_line

def get_paths_from_pathlist(_fnfn,sep='\t',path_key=PATH_KEY,path_prefix='',path_suffix=''):
    _fnfn_is_csv=is_csv(_fnfn,sep=sep)
    if _fnfn_is_csv:
        return get_paths_from_csv(_fnfn,path_key=path_key,sep=sep,path_prefix=path_prefix,path_suffix=path_suffix)
    elif _fnfn.endswith('jsonl'):
        import json
        jsons=[]
        with open(_fnfn) as f:
            for ln in f:
                try:
                    d=json.loads(ln.strip())
                    jsons.append(d)
                except (ValueError,json.JSONDecodeError) as e:
                    pass
        return jsons
    else:
        with open(_fnfn) as pf:
            paths=[line.strip() for line in pf]
            paths=[x for x in paths if x]
            if path_prefix: paths=[os.path.join(path_prefix,x) for x in paths]
            if path_suffix: paths=[path+path_suffix for x in paths]
            return paths

def load_paths(path_source,path_ext,limit,shuffle_paths,path_key=PATH_KEY,path_prefix='',path_suffix=''):
    paths=None
    if path_source:
        if os.path.isdir(path_source):
            paths=list(get_all_paths_from_folder(path_source,path_ext)) if path_source else None
        elif os.path.exists(path_source):
            paths=get_paths_from_pathlist(path_source,path_key=path_key,path_prefix=path_prefix,path_suffix=path_suffix)
        else:
            path_pathlists = CONFIG.get('PATH_PATHLISTS','')
            pathlist_path_source = os.path.join(path_pathlists,path_source)
            if os.path.exists(pathlist_path_source):
                paths=get_paths(pathlist_path_source)
    #paths=[p for p in paths if os.path.exists(p)]
    if not paths:
        print('!! no paths given or found at %s' % path_source if path_source else '')
        return
    #paths=sorted(list(set(paths)))
    paths=list(paths)
    paths=paths[:limit]
    if shuffle_paths:
        random.shuffle(paths)
    paths=[tuple(x) if type(x)==list else x for x in paths]
    paths=[tuple(sorted(list(x.items()))) if x==dict else x for x in paths]
    return paths

def save_results_txt(results_fnfn_txt,path_cache,txt_maxcols=TXT_MAXCOLS,sep='\t'):
    now=time.time()

    # First find KEYS
    KEYS=set()
    if txt_maxcols: Count=Counter()

    for path,result in stream_results(path_cache,flatten=True):
        if hasattr(result,'keys'):
            if txt_maxcols:
                Count.update(list(result.keys()))
            else:
                KEYS=set(result.keys())
    if txt_maxcols: KEYS={x for x,y in Count.most_common(txt_maxcols)}

    then,now=now,time.time()
    #print('>> save_txt: found %s keys in %ss' % (len(KEYS),int(now-then)))
    #print(list(KEYS)[:100],'...')

    if KEYS:
        # Then loop again to write
        header=['_path']+sorted([six.text_type(x) for x in KEYS])
        #with codecs.open(results_fnfn_txt,'w',encoding='utf-8') as results_f_txt:#, jsonlines.open(results_fnfn_json) as reader:
        with open(results_fnfn_txt,'w') as results_f_txt:
            results_f_txt.write(sep.join(header) + '\n')
            for path,result in stream_results(path_cache,flatten=True):
                result['_path']=path
                orow=[str(result.get(h,'')).replace(sep,' ').replace('\r\n',' ').replace('\r',' ').replace('\n',' ') for h in header]
                results_f_txt.write(sep.join(orow) + '\n')
            #print('>> saved:',results_fnfn_txt)
            then,now=now,time.time()
            #print('>> save_txt: saved in %ss' % int(now-then))

def rconvert(robj):
    from rpy2.robjects import r as R
    from rpy2.robjects import pandas2ri
    pandas2ri.activate()
    rjson=R['toJSON'](robj, collapse=' ')
    rjson_str = pandas2ri.ri2py(rjson)[0]
    pyobj = json.loads(rjson_str)
    sys.stdout.flush()
    return pyobj

### Loading results

def stream_results(path_cache,ext='.jsonl',flatten=False,progress=True):
    if 'jsonl' in os.path.basename(path_cache).split('.'):
        for path,data in stream_jsonl(path_cache,flatten=flatten,progress=progress):
            #if '.ipynb' in path: continue
            yield (path,data)
    else:
        for fn in sorted(os.listdir(path_cache)):
            if (not fn.endswith(ext) and not fn.endswith(ext+'.gz')): continue
            fnfn=os.path.join(path_cache,fn)
            #print('>> streaming:',fnfn,'...')
            for path,data in stream_jsonl(fnfn,flatten=flatten,progress=progress):
                #if '.ipynb' in path: continue
                yield (path,data)

def stream_jsonl(fn,flatten=False,progress=True):
    #from xopen import xopen
    import ujson as json
    if progress: from tqdm import tqdm

    #with xopen(fn) as f:
    #print('>> [Slingshot] streaming:',fn,'...')
    if progress: num_lines = get_num_lines(fn)
    with open(fn) as f:
        looper = tqdm(f,total=num_lines,desc=f'[Slingshot] Reading {os.path.basename(fn)}') if progress else f
        for ln in looper:
            try:
                path,data=json.loads(ln)
            except ValueError:
                continue
            if not flatten or type(data)!=list:
                yield (path,data)
            else:
                for i,data_x in enumerate(data):
                    yield (path if not i else '',data_x)

def writegen(fnfn,generator,header=None,args=[],kwargs={}):
    if 'jsonl' in fnfn.split('.'): return writegen_jsonl(fnfn,generator,args=args,kwargs=kwargs)
    with open(fnfn,'w') as of:
        for i,dx in enumerate(generator()):
            if not header: header=sorted(dx.keys())
            if not i: of.write('\t'.join(header) + '\n')
            of.write('\t'.join([six.text_type(dx.get(h,'')) for h in header]) + '\n')
    print('>> saved:',fnfn)

def get_num_lines(filename):
    from smart_open import open

    def blocks(files, size=65536):
        while True:
            b = files.read(size)
            if not b: break
            yield b

    with open(filename, 'r', errors='ignore') as f:
        numlines=sum(bl.count("\n") for bl in blocks(f))

    return numlines

def iter_move(fn,force=False,prefix=''):
    if os.path.exists(fn):
        iter_fn=iter_filename(fn,force=force,prefix=prefix)
        iter_dir=os.path.dirname(iter_fn)
        if not os.path.exists(iter_dir): os.makedirs(iter_dir)
        shutil.move(fn,iter_fn)
        print(f'>> moved: {fn} --> {iter_fn}')

def iter_filename(fnfn,force=False,prefix='',inbetweener='_'):
    if os.path.exists(fnfn) or force:
        fndir,fn=os.path.split(fnfn)
        filename,ext = os.path.splitext(fn)
        fnum=2 if not force else 1
        maybe_fn=os.path.join(fndir, prefix + filename + ext)
        while os.path.exists(maybe_fn):
            fnum+=1
            maybe_fn=os.path.join(fndir, prefix + filename + inbetweener + str(fnum) + ext)
        fnfn = maybe_fn
    return fnfn

def delete_files_from_folder(path,toprint=True):
    if os.path.exists(path):
        for root, dirs, files in os.walk(path):
            for fn in files:
                fnfn=os.path.join(root, fn)
                os.unlink(fnfn)
                if toprint: print('>> deleted:',fnfn)


from re import I

N = int(input())
maior = 0
menor = 0
soma = 0
i = 0

while i < N:
    x = int(input())

    if i == 0:
        maior = x
        menor = x

    if x > maior:
        maior = x

    if x < menor:
        menor = x

    soma += x
    i += 1

print(maior)
print(menor)
print(soma)

Exercises C - Repetition/C013 - Maior, menor e soma.py


def string_letter_count(text):

    counts = dict()
    final = dict()
    h = ""

    words = list(text)
    print(words)
    for word in words:

        counts[word] = counts.get(word, 0) + 1
    for word,value in counts.item():
        h = h + value + word
    return h
print(string_letter_count("This is a test sentence." ))


import random
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from IPython.display import display
import inspect

# Version con mosquitos pero sin genotipos y sin muerte.
# AUN NO METO GENOTIPOS

# Infeccion de mosquito:
# - Mosquito suceptible pica a humano infectado.
# - Queda infectado hasta que se muere
# - Asumo que mosquito infectado puede picar infinita cantidad de veces antes de morir.

# Infeccion de humano:
# - Mosquito infectado pica a humano susceptible.

#TODO matar mosquitos: prob o conteo de días
#TODO assumption de encounter siendo igual h-m, m-h

class Mosquito:
    def __init__(self, id, state):
        self.id = id
        self.state = state

class Human:
    def __init__(self, id, state):
        self.id = id
        self.state = state

class SIRmodel:
    def __init__(self, n_humans, n_mosquitoes, init_inf_hum, init_inf_mos, encounter_p,  biting_p, daysCured):

        self.n_humans = n_humans
        self.n_mosquitoes = n_mosquitoes
        self.init_inf_hum = init_inf_hum
        self.init_inf_mos = init_inf_mos
        self.encounter_p = encounter_p
        self.biting_p = biting_p
        self.daysCured = daysCured
        self.gamma = 1/self.daysCured
        self.population_hum = []
        self.population_mos = []
        self.data = pd.DataFrame(columns=["Time_step", "Id", "State"])
        self.counts = pd.DataFrame(columns=["S", "I", "R"])
        self.initialize_pop_hum()
        self.initialize_pop_mos()

    def initialize_pop_mos(self):

        for i in range(self.n_mosquitoes):
            state = "S"
            mosquito = Mosquito(i, state)
            self.population_mos.append(mosquito)

        sample_infected = random.sample(self.population_mos, self.init_inf_mos)

        for mosquito in sample_infected:
            index = self.population_mos.index(mosquito)
            self.population_mos[index].state = "I"

    def initialize_pop_hum(self):

        for i in range(self.n_humans):
            state = "S"
            human = Human(i, state)
            self.population_hum.append(human)

        sample_infected = random.sample(self.population_hum, self.init_inf_hum)

        for human in sample_infected:
            index = self.population_hum.index(human)
            self.population_hum[index].state = "I"

        for i in range(len(self.population_hum)):
            self.data.loc[i]=(0, self.population_hum[i].id, self.population_hum[i].state)

        self.conteos(0)

    def change_state(self):
        for human in self.population_hum:
            # Human recovery and mosquito infection
            if human.state == "I":
                for mosquito in self.population_mos:
                    if mosquito.state == "S":
                        #TODO dejo multi, transmission o que aca
                        if random.random() > self.encounter_p:
                            # Si el encuentro se da, se tiene otra siguiente p a superar, que está relacionada con el biting rate.
                            if random.random() > self.biting_p:
                                mosquito.state ="I"

                # Pensar bien si dejarlo así o tipo si pasan tantos días ya se recupera.
                if random.random() < self.gamma:
                    human.state= "R"

            # Human infection
            elif human.state == "S":

                # Para cada mosquito va a tener una probabilidad de encuentro, si esta es mayor a cierto valor el encuentro se da.
                # Revisar si hacer mejor por grid
                # No voy a revisar a todos pues porque me importan son los infectados
                for mosquito in self.population_mos:
                    if mosquito.state == "I" and random.random() > self.encounter_p:
                    # Si el encuentro se da, se tiene otra siguiente p a superar, que está relacionada con el biting rate.
                        if random.random() > self.biting_p:
                            human.state ="I"
                            # Si se infecta pues se asume que nadie más lo pica
                            break

    def update(self, t):
        self.change_state()
        for human in self.population_hum:
            self.data.loc[len(self.data)] = (t,  human.id, human.state)

    def conteos(self, t):
        conteo = self.data.where(self.data["Time_step"]==t)["State"].value_counts()
        state = conteo.index.tolist()
        if 'S' in state:
            S = conteo['S']
        else:
            S=0
        if 'I' in state:
            I = conteo['I']
        else:
            I=0
        if 'R' in state:
            R = conteo['R']
        else:
            R=0

        self.counts.loc[t] = (S, I, R)

    def run(self, n_steps):
        for t in range(1, n_steps):
            self.update(t)
            self.conteos(t)
        return self.data, self.counts

# Create DengueABM instance

# (n_humans, n_mosquitoes, init_inf_hum, init_inf_mos, encounter_p,  biting_p, daysCured)

model = SIRmodel(30, 500, 1, 5, 0.9, 0.9, 20)
# Run simulation
df_data, df_conteos = model.run(200)

time = range(len(df_conteos))
# Plot results
plt.plot(time, df_conteos["S"].tolist(), label='Susceptible')
plt.plot(time, df_conteos["I"].tolist(), label='Infected')
plt.plot(time, df_conteos["R"].tolist(),label='Recovered')
plt.xlabel('Time Step')
plt.ylabel('Number of Humans')
plt.legend()
plt.show()
infect = 0
for i in model.population_mos:
    if i.state == "I":
        infect+=1

print(infect)


"""Добавление связи между заказам и таблицы с временем заказов

Revision ID: 940dafae9a62
Revises: 82e629e61ca9
Create Date: 2021-03-19 22:46:19.052728

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '940dafae9a62'
down_revision = '82e629e61ca9'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('order_assign_time', sa.Column('order_id', sa.Integer(), nullable=True))
    op.create_foreign_key(None, 'order_assign_time', 'order', ['order_id'], ['order_id'])
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'order_assign_time', type_='foreignkey')
    op.drop_column('order_assign_time', 'order_id')
    # ### end Alembic commands ###

migrations/versions/940dafae9a62_добавление_связи_между_заказам_и_.py


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 13 15:55:49 2019

@author: zhengshoujian
"""

import tensorflow as tf

def focal_loss(predictions,labels,alpha,gamma):
	zeros = tf.zeros_like(predictions,dtype = predictions.dtype)
	pos_corr = tf.where(labels>zeros,labels - predictions,zeros)
	neg_corr = tf.where(labels>zeros,zeros,predictions)
	fl_loss = -alpha*(pos_corr**alpha)*tf.log(predictions) - (1 - alpha)*(neg_corr**gamma)*tf.log(1.0 - predictions)
	return tf.reduce_sum(fl_loss)


'''
Q1. Given two strings s1 and s2, return the lowest ASCII sum of deleted characters to make two strings equal.
'''

def minimumDeleteSum(s1, s2):
    dp = [[0] * (len(s2) + 1) for _ in range(len(s1) + 1)]

    for i in range(len(s1) - 1, -1, -1):
        dp[i][len(s2)] = dp[i+1][len(s2)] + ord(s1[i])
    for j in range(len(s2) - 1, -1, -1):
        dp[len(s1)][j] = dp[len(s1)][j+1] + ord(s2[j])

    for i in range(len(s1) - 1, -1, -1):
        for j in range(len(s2) - 1, -1, -1):
            if s1[i] == s2[j]:
                dp[i][j] = dp[i+1][j+1]
            else:
                dp[i][j] = min(dp[i+1][j] + ord(s1[i]),
                                dp[i][j+1] + ord(s2[j]))

    return dp[0][0]

s1 = "sea"
s2 = "eat"
print(minimumDeleteSum(s1, s2))

'''
Q2. Given a string s containing only three types of characters: '(', ')' and '*', return true if s is valid.
'''

def checkValidString(s):
    if not s: return True
    LEFTY, RIGHTY = '(*', ')*'

    n = len(s)
    dp = [[False] * n for _ in s]
    for i in range(n):
        if s[i] == '*':
            dp[i][i] = True
        if i < n-1 and s[i] in LEFTY and s[i+1] in RIGHTY:
            dp[i][i+1] = True

    for size in range(2, n):
        for i in range(n - size):
            if s[i] == '*' and dp[i+1][i+size]:
                dp[i][i+size] = True
            elif s[i] in LEFTY:
                for k in range(i+1, i+size+1):
                    if (s[k] in RIGHTY and
                            (k == i+1 or dp[i+1][k-1]) and
                            (k == i+size or dp[k+1][i+size])):
                        dp[i][i+size] = True

    return dp[0][-1]
s = "()"
print(checkValidString(s))

'''
Q3. Given two strings word1 and word2, return *the minimum number of **steps** required to make* word1 *and* word2 *the same*.

In one **step**, you can delete exactly one character in either string.
'''

def editDistance(str1, str2, m, n):

	if m == 0:
		return n

	if n == 0:
		return m

	if str1[m-1] == str2[n-1]:
		return editDistance(str1, str2, m-1, n-1)

	return 1 + min(editDistance(str1, str2, m, n-1),
				editDistance(str1, str2, m-1, n),
				editDistance(str1, str2, m-1, n-1)
				)

str1 = "sea"
str2 = "eat"
print (editDistance(str1, str2, len(str1), len(str2)))

'''
Q4. You need to construct a binary tree from a string consisting of parenthesis and integers.

The whole input represents a binary tree. It contains an integer followed by zero, one or two pairs of parenthesis. The integer represents the root's value and a pair of parenthesis contains a child binary tree with the same structure.
You always start to construct the **left** child node of the parent first if it exists.
'''

class newNode:
    def __init__(self, data):
        self.data = data
        self.left = self.right = None

def preOrder(node):
	if (node == None):
		return
	print(node.data, end=' ')
	preOrder(node.left)
	preOrder(node.right)

def findIndex(Str, si, ei):
	if (si > ei):
		return -1

	s = []
	for i in range(si, ei + 1):

		if (Str[i] == '('):
			s.append(Str[i])

		elif (Str[i] == ')'):
			if (s[-1] == '('):
				s.pop(-1)

				if len(s) == 0:
					return i

	return -1

def treeFromString(Str, si, ei):

	if (si > ei):
		return None

	root = newNode(ord(Str[si]) - ord('0'))
	index = -1

	if (si + 1 <= ei and Str[si + 1] == '('):
		index = findIndex(Str, si + 1, ei)

	if (index != -1):

		root.left = treeFromString(Str, si + 2,
								index - 1)

		root.right = treeFromString(Str, index + 2,
									ei - 1)
	return root

if __name__ == '__main__':
	Str = "4(2(3)(1))(6(5))"
	root = treeFromString(Str, 0, len(Str) - 1)
	preOrder(root)

'''
Q5. Given an array of characters chars, compress it using the following algorithm:

Begin with an empty string s. For each group of **consecutive repeating characters** in chars:

- If the group's length is 1, append the character to s.
- Otherwise, append the character followed by the group's length.

The compressed string s **should not be returned separately**, but instead, be stored **in the input character array chars**. Note that group lengths that are 10 or longer will be split into multiple characters in chars.

After you are done **modifying the input array,** return *the new length of the array*.

You must write an algorithm that uses only constant extra space.
'''

def compress(chars):
    i = 0
    res = 0
    while i < len(chars):
        group_length = 1
        while (i + group_length < len(chars)
                and chars[i + group_length] == chars[i]):
            group_length += 1
        chars[res] = chars[i]
        res += 1
        if group_length > 1:
            str_repr = str(group_length)
            chars[res:res+len(str_repr)] = list(str_repr)
            res += len(str_repr)
        i += group_length
    return res

chars = ["a","a","b","b","c","c","c"]
print(compress(chars))

'''
Q6. Given two strings s and p, return *an array of all the start indices of* p*'s anagrams in* s.
You may return the answer in **any order**.

An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase,
typically using all the original letters exactly once.
'''

def findAnagrams(s, p):
    window = len(p)
    mapper = {}
    def __mapper():
        nonlocal mapper
        for i in p:
            if i in mapper.keys(): mapper[i] += 1
            else: mapper[i] = 1
    __mapper()
    ans = []
    st = 0
    ed = 0
    while ed < len(s):
        if s[ed] in mapper.keys():
            mapper[s[ed]] -= 1
            if mapper[s[ed]] == 0:
                del mapper[s[ed]]
            if not bool(mapper):
                ans.append(st)
                mapper[s[st]] = 1
                st += 1
            ed += 1
        else:
            if st == ed:
                st = ed = ed + 1
            else:
                if s[st] in mapper.keys():
                    mapper[s[st]] += 1
                else:
                    mapper[s[st]] = 1
                st += 1
    return ans

s = "cbaebabacd"
p = "abc"
print(findAnagrams(s, p))

'''
Q7. Given an encoded string, return its decoded string.

The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer.

You may assume that the input string is always valid; there are no extra white spaces, square brackets are well-formed, etc. Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there will not be input like 3a or 2[4].

The test cases are generated so that the length of the output will never exceed 105.
'''

def decodeString(s):
    st = []
    num = 0
    res = ''

    for ch in s:
        if ch.isnumeric():
            num = num * 10 + int(ch)
        elif ch == '[':
            st.append(res)
            st.append(num)
            res = ''
            num = 0
        elif ch == ']':
            cnt = st.pop()
            prev = st.pop()
            res = prev + cnt * res
        else:
            res += ch
    return res

s = "3[a]2[bc]"
print(decodeString(s))

'''
Q8. Given two strings s and goal, return true *if you can swap two letters in* s *so the result is equal to* goal*, otherwise, return* false*.*

Swapping letters is defined as taking two indices i and j (0-indexed) such that i != j and swapping the characters at s[i] and s[j].

- For example, swapping at indices 0 and 2 in "abcd" results in "cbad".
'''

def buddyStrings(s, goal):
    if len(s) != len(goal):
        return False

    if s == goal:

        frequency = [0] * 26
        for ch in s:
            frequency[ord(ch) - ord('a')] += 1
            if frequency[ord(ch) - ord('a')] == 2:
                return True

        return False

    firstIndex = -1
    secondIndex = -1

    for i in range(len(s)):
        if s[i] != goal[i]:
            if firstIndex == -1:
                firstIndex = i
            elif secondIndex == -1:
                secondIndex = i
            else:

                return False

    if secondIndex == -1:
        return False

    return s[firstIndex] == goal[secondIndex] and s[secondIndex] == goal[firstIndex]

s = "ab"
goal = "ba"
print(buddyStrings(s, goal))


#File to test the image that is processed
from utils import *
import numpy as np
import cv2 as cv

cap = cv.VideoCapture(0)

if not cap.isOpened():
    print("Cannot open camera")
    exit()

fourcc = cv.VideoWriter_fourcc(*'MJPG')
out = cv.VideoWriter('output.avi', fourcc, 20.0, (640,  480))

while True:
    # Capture frame-by-frame
    ret, frame = cap.read()

    # if frame is read correctly ret is True
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break

    out.write(frame)

    print("working")

    if cv.waitKey(1) == ord('q'):
        break

# When everything done, release the capture
cap.release()
out.release()
cv.destroyAllWindows()


# 20. Valid Parentheses
# https://leetcode.com/problems/valid-parentheses/

class Solution:
    def isValid(self, s: str) -> bool:
        map = {')':'(', '}':'{', ']':'['}
        open_brackets = map.values()
        stack = []
        for bracket in s:
            if bracket in open_brackets:
                stack.append(bracket)
            else:
                if not stack: return False
                if map[bracket] != stack[-1]:
                    return False
                else:
                    stack.pop()
        return len(stack) == 0


# Task 3.2

bigList = [divideThree for divideThree in range(100,200) if divideThree % 3 == 0]
print(bigList)

feelings = ["happy", "sad", "excited", "sad", "excited", "angry", "frightened"]
listFeelings = []
listFeelings.append(feelings)
print(listFeelings)

feelingsA = [a for a in feelings if "a" in a]
print(feelingsA)


'''
for communication between the Unreal Engine game client and the backend server requires defining endpoints,
handling requests, and establishing a protocol for data exchange.
Below, I'll provide a simple example using Python with Flask for RESTful API
'''

from flask import Flask, request, jsonify

app = Flask(__name__)

# Sample data (replace with your game data)
game_data = {"score": 0, "level": 1}

@app.route('/api/game_data', methods=['GET', 'PUT'])
def get_or_update_game_data():
    if request.method == 'GET':
        return jsonify(game_data)
    elif request.method == 'PUT':
        new_data = request.json  # Expecting JSON input
        game_data.update(new_data)
        return jsonify({"message": "Game data updated successfully"})

if __name__ == '__main__':
    app.run(debug=True)  # Run the server


def read_from_system_in():
    data = {}
    input_data = input().split(",")
    while not input_data.__contains__("end"):
        full_name = input_data[0] + " " + input_data[1]
        index = input_data[2]
        score = [int(input_data[4]), int(input_data[5]), int(input_data[6])]
        subject = {input_data[3]: score}
        if index in data:
            if full_name in data[index]:
                current_subjects = data[index][full_name]
                data[index][full_name] = current_subjects + [subject]
            else:
                data[index] = {full_name: [subject]}
        else:
            data[index] = {full_name: [subject]}
        input_data = input().split(",")
    return data

def calculate_grade(score):
    total_score = sum(score, 0)
    if total_score in range(0, 51, 1):
        return "5"
    elif total_score in range(51, 61, 1):
        return "6"
    elif total_score in range(61, 71, 1):
        return "7"
    elif total_score in range(71, 81, 1):
        return "8"
    elif total_score in range(81, 91, 1):
        return "9"
    elif total_score in range(91, 101, 1):
        return "10"

if __name__ == "__main__":
    students = read_from_system_in()
    for key in students.keys():
        student_name = [name for name in students[key].keys()][0]
        print("Student: " + student_name )
        for subject in students[key][student_name]:
            subject_name = [ name  for name in subject.keys() ][0]
            grade = calculate_grade(subject[subject_name])
            print("    " + subject_name + ": " + grade)
        print("")


from rapidsms.webui.utils import render_to_response
from models import *
from datetime import datetime, timedelta
from utilities.export import export
from rapidsms.webui.utils import paginated
from django.contrib.auth.decorators import login_required, permission_required

@login_required
@permission_required("logger.can_view")
def index(req):
    template_name="logger/index.html"
    incoming = IncomingMessage.objects.order_by('received')
    outgoing = OutgoingMessage.objects.order_by('sent')
    all = []
    [ all.append(msg) for msg in incoming ]
    [ all.append(msg) for msg in outgoing]
    # sort by date, descending
    all.sort(lambda x, y: cmp(y.date, x.date))
    context = {}
    context['messages'] = paginated(req, all, per_page=50)
    return render_to_response(req, template_name, context )

def csv_in(req, format='csv'):
    context = {}
    if req.user.is_authenticated():
        return export(IncomingMessage.objects.all())
    return export(IncomingMessage.objects.all(), ['id','text','backend','domain','received'])

def csv_out(req, format='csv'):
    context = {}
    if req.user.is_authenticated():
        return export(OutgoingMessage.objects.all())
    return export(OutgoingMessage.objects.all(), ['id','text','backend','domain','sent'])