print("ASSIGNMENT-1\nNAME-SUNPREET SINGH\nSID-21103118\n\n")

# QUESTION 1
print("Question 1\n")

#Python Program for finding average of three numbers

iFirstNo=input("Enter first number\n")
iSecondNo=input("Enter second number\n")
iThirdNo=input("Enter third number\n")

#Average of three numbers
fAvg=(int(iFirstNo)+int(iSecondNo)+int(iThirdNo))/3
print("The average of three numbers is :")
print(fAvg)

# Question 2
print("\n\nQuestion 2\n")

# Computing tax payable by the user

# Standard Deduction= $10000
iStdDed=10000
# Dependent Deduction per Dependent= $3000
iDependDed=3000
# No. of Dependents
iDependNo=input("Enter the Number of Dependents\n")
# Total Dependent Deduction= Dependent Deduction per Dependent * No.of dependents
# And declaring No. of Dependents as int variable
iTotalDependDed=iDependDed*int(iDependNo)

# Gross Income of the user
iGrossIncome=input("Enter your Gross Income\n")

# Total Taxable Income= Gross Income - (Standard Deduction + Total Dependent Deduction)
iTaxableIncome=int(iGrossIncome)-int(iStdDed)-int(iTotalDependDed)

# Tax rate= 20%
# Total Tax Payable= Taxable Income * 20%
iTax=(float((iTaxableIncome)*20)/100)
print("Total Tax Payable =",iTax)

# Question 3
print("\n\nQuestion 3\n")

# Storing Student Credentials

iSID=int(input("Enter your SID\n"))
sName=input("Enter your name\n")
# Input of M,F,U for Male,Female and Unknown respectively
sGender=input("For Gender\nEnter M,F,U for Male,Female and Unknown respectively\n")
sCourseName=input("Enter your Course Name\n")
fCGPA=float(input("Enter your CGPA\n"))

#Creating list to get the student credentials
indStudentCred=[iSID,sName,sGender,sCourseName,fCGPA]

# Printing Student Credentials
print(indStudentCred)

# Question 4
print("\n\nQuestion 4\n")

# Sorting entered marks of 5 students

# Getting input of marks of 5 students from the user
iStudent1=input("Enter marks of Student 1\n")
iStudent2=input("Enter marks of Student 2\n")
iStudent3=input("Enter marks of Student 3\n")
iStudent4=input("Enter marks of Student 4\n")
iStudent5=input("Enter marks of Student 5\n")

# Creating a list of the marks
indSortMarks=[iStudent1,iStudent2,iStudent3,iStudent4,iStudent5]

# Sorting marks of the 5 students
indSortMarks.sort()

print(indSortMarks)

# Question 5
print("\n\nQuestion 5\n")

# Creating list of 6 colors; Red, Green, White, Black, Pink, Yellow
indColor=['Red','Green','White','Black','Pink','Yellow']

# Removing 4th element i.e. Black from the list
indColor.remove(indColor[3])
print("a.",indColor)

indColor=['Red','Green','White','Black','Pink','Yellow']
# Removing Black and Pink from the list and replacing them with Purple
indColor[3:5]=['Purple']
print("\nb.",indColor)


from django import forms
from users.models import Project, Project_pictures, Comment, Donation, Reply
import datetime

class AddProjectForm(forms.ModelForm):
    class Meta:
        model = Project
        fields = (
            "title",
            "details",
            "total_target",
            "start_date",
            "end_date",
            "category",
            "tags",
        )
        error_messages = {
            'total_target': {
                'min_value': "Invalid value, target must be greater than zero",
            },
        }
        widgets = {
            "tags": forms.TextInput(attrs={"data-role": "tagsinput", "name": "tags"}),
            "start_date": forms.DateInput(attrs={"type": "date"}),
            "end_date": forms.DateInput(attrs={"type": "date"}),
        }

    def clean(self):
        cleaned_data = super().clean()
        start_date = cleaned_data.get("start_date")
        end_date = cleaned_data.get("end_date")
        if end_date <= start_date:
            msg = "End date should be greater than start date."
            self.add_error("end_date", msg)
        if end_date < datetime.date.today() or end_date == datetime.date.today():
            msg = "End date should be greater than today date."
            self.add_error("end_date", msg)
        if start_date < datetime.date.today():
            msg = "Start date should be greater than today date."
            self.add_error("start_date", msg)

class ImageForm(forms.ModelForm):
    class Meta:
        model = Project_pictures
        fields = ("picture",)
        labels = {"picture": "Images"}
        widgets = {
            "picture": forms.ClearableFileInput(attrs={"multiple": True}),
        }

class CommentForm(forms.ModelForm):
    class Meta:
        model = Comment
        fields = ("content",)
        labels = {"content": ""}
        widgets = {
            "content": forms.TextInput(
                attrs={
                    "class": "comment-input",
                    "placeholder": "New Comment....",
                }
            ),
        }

class ReplyForm(forms.ModelForm):
    class Meta:
        model = Reply
        fields = ("content",)
        labels = {"content": ""}
        widgets = {
            "content": forms.TextInput(
                attrs={
                    "class": "comment-input w-100",
                    "placeholder": "Add reply....",
                }
            ),
        }

class DonateForm(forms.ModelForm):
    class Meta:
        model = Donation
        fields = ("amount",)
        labels = {"amount": ""}
        widgets = {
            "amount": forms.NumberInput(
                attrs={
                    "class": "form-control",
                    "placeholder": "0",
                    "id":"amount"
                }
            ),
        }


import torch
# import torch.utils
import torch.nn as nn
import torch.nn.functional as F
import torchvision.transforms as transforms
from torchvision.datasets import CIFAR10
import torch.optim as optim

torch.manual_seed(1234)
ratio = 0.1
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
transform = transforms.Compose(
    [transforms.ToTensor(),
     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
train_set = CIFAR10(root="task2_data", train=True,
                    transform=transform, download=True)
test_set = CIFAR10(root="task2_data", train=False,
                   transform=transform, download=True)
train_set, val_set = torch.utils.data.random_split(train_set,
                                                   [int((1 - ratio) * len(train_set)),
                                                    int(ratio * len(train_set))])
test_dataset = torch.utils.data.DataLoader(test_set, batch_size=1, shuffle=False)
train_dataset = torch.utils.data.DataLoader(train_set, batch_size=1, shuffle=False)

class MyModel(torch.nn.Module):
    num_features = 32 * 32 * 3
    num_categories = 10
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(MyModel.num_features, 100)
        self.fc2 = nn.Linear(100, 100)
        self.fc3 = nn.Linear(100, MyModel.num_categories)

        self.optimizer = optim.SGD(self.parameters(), lr=0.001)
        self.criterion = torch.nn.MSELoss()
        # self.loss = 0.0

    def forward(self, x):
        x = torch.flatten(x, 1)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

    def loss_func(self, local_data, local_labels):

        output = self.forward(local_data)
        y = local_labels
        self.optimizer.zero_grad()
        self.loss = self.criterion(output, y)
        return self.loss

    def backward(self, loss):
        self.loss.backward()
        self.optimizer.step()

model = MyModel().to(device)
my_loss = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.001)

num_epochs = 200
for epoch in range(num_epochs):
  sum_loss = 0
  for i, (images, labels) in enumerate(train_dataset):
    images = images.to(device)
    labels = labels.to(device)

    output = model(images)
    loss = my_loss(output, labels)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    sum_loss += loss.item()

  print('Epoch [%d] Train Loss: %.4f'% (epoch+1, sum_loss/i))

with torch.no_grad():
  correct = total = 0
  for images, labels in test_dataset:
    images = images.to(device)
    labels = labels.to(device)

    output = model(images)
    _, predicted_labels = torch.max(output, 1)
    correct += (predicted_labels == labels).sum()
    total += labels.size(0)
  print('Percent correct: %.3f %%' % ((100 * correct) / (total + 1)))


from odoo.tests.common import TransactionCase

class TestUsers(TransactionCase):

    def test_name_search(self):
        """ Check name_search on user. """
        User = self.env['res.users']

        test_user = User.create({'name': 'Flad the Impaler', 'login': 'vlad'})
        like_user = User.create({'name': 'Wlad the Impaler', 'login': 'vladi'})
        other_user = User.create({'name': 'Nothing similar', 'login': 'nothing similar'})
        all_users = test_user | like_user | other_user

        res = User.name_search('vlad', operator='ilike')
        self.assertEqual(User.browse(i[0] for i in res) & all_users, test_user)

        res = User.name_search('vlad', operator='not ilike')
        self.assertEqual(User.browse(i[0] for i in res) & all_users, all_users)

        res = User.name_search('', operator='ilike')
        self.assertEqual(User.browse(i[0] for i in res) & all_users, all_users)

        res = User.name_search('', operator='not ilike')
        self.assertEqual(User.browse(i[0] for i in res) & all_users, User)

        res = User.name_search('lad', operator='ilike')
        self.assertEqual(User.browse(i[0] for i in res) & all_users, test_user | like_user)

        res = User.name_search('lad', operator='not ilike')
        self.assertEqual(User.browse(i[0] for i in res) & all_users, other_user)


import json
import requests

FB = 'https://graph.facebook.com'

"""Python client library for the Facebook ads API."""

class AdsAPI(object):
    """A client for the Facebook Ads API."""

    def __init__(self, ad_account_id, access_token=None, expires=None):
        self.ad_account_id = ad_account_id
        self.access_token = access_token
        self.expires = expires
        self.payload = {'access_token': access_token}

    def get_ad_account_info(self, fields):
        url = '%s/%s' % (FB, self.ad_account_id)
        r = requests.get(url, params=self.payload)
        return r.json()

    def get_ad_account_users(self):
        url = '%s/%s/users' % (FB, self.ad_account_id)
        r = requests.get(url, params=self.payload)
        return r.json()

    def get_broad_targeting_categories(self):
        url = '%s/%s/broadtargetingcategories' % (FB, self.ad_account_id)
        r = requests.get(url, params=self.payload)
        return r.json()

    def get_connection_objects(self):
        url = '%s/%s/connectionobjects' % (FB, self.ad_account_id)
        r = requests.get(url, params=self.payload)
        return r.json()

    def get_ad_stats(self):
        url = '%s/%s/stats' % (FB, self.ad_account_id)
        r = requests.get(url, params=self.payload)
        return r.json()

    def get_ad_campaign_stats(self, campaign_ids=None):
        """Return campaign stats for the given list of campaign ids."""

        payload = {'campaign_ids': json.dumps(campaign_ids)}
        payload.update(self.payload)
        url = '%s/%s/adcampaignstats' % (FB, self.ad_account_id)
        r = requests.get(url, params=payload)
        return r.json()

    def get_ad_group_stats(self, adgroup_ids=None, stats_mode=None):
        payload = {'adgroup_ids': json.dumps(adgroup_ids),
                   'stats_mode': stats_mode}
        payload.update(self.payload)
        url = '%s/%s/adgroupstats' % (FB, self.ad_account_id)
        r = requests.get(url, params=payload)
        return r.json()

    def get_keyword_stats(self, adgroup_id):
        url = '%s/%s/keywordstats' % (FB, adgroup_id)
        r = requests.get(url, params=self.payload)
        return r.json()

    def get_conversion_stats(self, campaign_ids=None, adgroup_ids=None):
        url = '%s/%s/adcampaignconversions' % (FB, self.ad_account_id)
        r = requests.get(url, params=self.payload)
        return r.json()

    def get_reach_estimate(self, currency, targeting_spec):
        payload = {'currency': currency,
                   'targeting_spec': json.dumps(targeting_spec)}
        payload.update(self.payload)
        url = '%s/%s/reachestimate' % (FB, self.ad_account_id)
        r = requests.get(url, params=payload)
        return r.json()

    def get_targeting_description(self, adgroup_id):
        url = '%s/%s/targetingsentencelines' % (FB, adgroup_id)
        r = requests.get(url, params=self.payload)
        return r.json()

    def search(self, query, type, want_localized_name):
        payload = {'q': query, 'type': type,
                   'want_localized_name': want_localized_name}
        payload.update(self.payload)
        url = '%s/search' % FB
        r = requests.get(url, params=payload)
        return r.json()

    def create_ad_creative(self):
        url = '%s/%s/adcreatives' % (FB, self.ad_account_id)
        r = requests.post(url, params=self.payload)
        return r.json()


def reorder_list(head):
    # we can use 2 pointer approach

    # find the middle of the list
    slow, fast = head, head.next

    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next

    # then we reverse the second half
    second = slow.next
    prev = slow.next = None

    while second:
        tmp = second.next
        second.next = prev
        prev = second
        second = tmp

    # then we merge 2 halves
    first, second = head, prev

    while second:
        tmp1, tmp2 = first.next, second.next
        first.next = second
        second.next = tmp1
        first, second = tmp1, tmp2


import networkx as nx
import random
import matplotlib.pyplot as plt

def main():
    # Watts Strogatz Ring Graph
    # do it at various levels
    ring_lattice = nx.watts_strogatz_graph(100, 4, 0)
    # nx.draw(ring_lattice)
    # plt.draw()
    # plt.show()
    print("Ring simple ", simple_contagion(ring_lattice, 0))
    print("Ring complex", complex_contagion(ring_lattice, 0, 2))

    # 2 dimensional lattice with Moore neighborhoods
    #moore_lattice = create_moore_lattice(100, 100, 0.000001, 1)
    # nx.draw(moore_lattice)
    # plt.draw()
    # plt.show()
    # print("Moore simple ", simple_contagion(moore_lattice, (0, 0)))
    # print("Moore complex", complex_contagion(moore_lattice, (0, 0), 2))

    generate_plot()

# simple contagion propagation
def simple_contagion(in_graph, start_node):
    infected_nodes = set()
    # pick the starting node.
    infected_nodes.add(start_node)
    # add the ego network of that node.
    infected_nodes = infected_nodes.union(set(in_graph.neighbors(start_node)))

    time_step_number = 0
    while len(infected_nodes) < in_graph.number_of_nodes():
        nodes_to_add = set()
        for node in infected_nodes:
            to_infect_node = random.choices(list(in_graph.neighbors(node)))[0]
            nodes_to_add.add(to_infect_node)

        # print(nodes_to_add)
        infected_nodes = infected_nodes.union(nodes_to_add)
        # print(time_step_number, ":", infected_nodes)
        time_step_number += 1

        # while there are nodes being added to the graph, continue looping
        # For each infected node, pick a neighbor of that node and infect it (even if it is already infected)
        # print out the state of infected nodes.

    return time_step_number

def complex_contagion(in_graph, start_node, a):
    """

    :param in_graph: networkx graph object to run the complex contagion on
    :param a: number of activated nodes required for activation
    :return:
    """

    infected_nodes = set()
    # pick the starting node.
    infected_nodes.add(start_node)
    # add the ego network of that node.
    infected_nodes = infected_nodes.union(set(in_graph.neighbors(start_node)))

    time_step_number = 0

    # TODO: not a good end condition for complex
    while len(infected_nodes) < in_graph.number_of_nodes() * 0.99 and time_step_number <= 500000:
        nodes_to_add = set()
        for node in infected_nodes:
            to_infect_node = random.choices(list(in_graph.neighbors(node)))[0]

            # Check this node has enough critical mass to infect
            if len(infected_nodes.intersection(set(in_graph.neighbors(to_infect_node)))) >= a:
                nodes_to_add.add(to_infect_node)

            time_step_number += 1

            if len(infected_nodes) >= in_graph.number_of_nodes() * 0.99 or time_step_number > 500000:
                break

        # print(nodes_to_add)
        infected_nodes = infected_nodes.union(nodes_to_add)
        # print(time_step_number, ":", infected_nodes)

    return time_step_number

def generate_plot():
    # print("Moore simple ", simple_contagion(moore_lattice, (0, 0)))
    # print("Moore complex", complex_contagion(moore_lattice, (0, 0), 2))
    #for i in [-6, -4, -3, -2, -1, -0.9, -0.8, -0.7, -0.69, -0.68, -0.67, -0.66, -0.65, -0.64, -0.63, -0.62]:
    for i in [-6, -4, -3, -2, -1.9, -1.8, -1.7, -1.6, -1.5, -1.4, -1.39, -1.38, -1.37, -1.36, -1.35, -1.34, -1.33, -1.32, -1.31, -1.3, -1.2, -1.1, -1]:
        moore_lattice = create_moore_lattice(50, 50, 10**i, 1)
        print("Complex", i, complex_contagion(moore_lattice, (25, 25), 3))

def create_moore_lattice(m, n, p, radius=1):

    moore_lattice = nx.generators.lattice.grid_2d_graph(m, n)

    for node in moore_lattice.nodes():
        nodes_to_add = set()
        for row in range(node[0] - radius, node[0] + radius + 1):
            for column in range(node[1] - radius, node[1] + radius + 1):
                if row < 0 or row > m - 1 or column < 0 or column > n - 1:
                    continue
                nodes_to_add.add((row, column))
        for add_node in nodes_to_add:
            if node != add_node:
                moore_lattice.add_edge(node, add_node)

    for node in moore_lattice.nodes():
        neighbors = list(moore_lattice.neighbors(node))
        edges_to_be_deleted = []
        for neighbor in neighbors:
            delete_edge = random.choices([True, False], [p, 1 - p])[0]
            if delete_edge:
                edges_to_be_deleted.append((node, neighbor))

        unconnected = set(n for n in moore_lattice.nodes() if n not in neighbors)
        for i in range(len(edges_to_be_deleted)):
            dest = random.sample(unconnected, 1)[0]
            unconnected.remove(dest)
            moore_lattice.add_edge(node, dest)

        for edge in edges_to_be_deleted:
            moore_lattice.remove_edge(edge[0], edge[1])

    return moore_lattice

if __name__ == '__main__':
    main()


from time import sleep

class Dog:
    def __init__(self, name, idade, sexo):
        self.name = name
        self.idade = idade
        self.sexo = sexo

    def latir(self):
        print("Au Au")

    def dormir(self):
        print(f"{self.name} está dormindo...")

class Carro:
    def __init__(self, marca, cor):
        self.marca = marca
        self.cor = cor
        self.is_started = False

    def start(self):
        print("Ligando o carro...")
        sleep(5)
        print("Carro ligado!")
        self.is_started = True

    def run(self, km):
        if self.is_started:
            print(f"Andando {km} Kilômetros")
            sleep(10)
        else:
            print("Você precisa ligar o carro primeiro!")

if __name__ == '__main__':
    pedro = Dog("Pedro", 2, 'M')
    maria = Dog("Maria", 2, 'F')

    pedro.latir()
    maria.dormir()

    carro1 = Carro("BMW", "Branco")

    carro1.run(10)

    carro1.start()

    carro1.run(10)


#result gives the largest distance of values farthest

def solution(A):
    #hash = {}
    N = len(A)
    currMax = 0
    max = N-1

    for i in xrange(N):
        key = A[i]
        buffResult = N - 1 - i
        window = 0
        for j in range(N-1,i,-1):
            curr = A[j]
            if(key == curr):
                buffResult = buffResult - window
            else:
                window = window +1

        currMax = max(currMax,buffResult)

    return currMax
solution([1,2,3,4])


from enum import Enum, IntEnum
from typing import List, Optional, Set
from odmantic import Model
import datetime

class EntryType (str, Enum):
    """[summary]
        An entry performed by the user.
    [description]
        A user can record of three types of entries:
        1) Traumatic Event
        2) Journal Entry
        3) Incident
    """
    trauma = "Traumatic Event"
    journal = "Journal"
    incident = "Incident"

class IncidentSeverity (int, Enum):
    """[summary]
        In the case of an incident entry a severity level of the incident.
    [description]
        A user can determine the severity of the incident based on how it affected them.
    """
    uncomfortable = 1
    difficult_to_function = 2
    very_difficult_to_function = 3
    unable_to_function = 4
    required_medical_assistance = 5

class IncidentType (str, Enum):
    """[summary]
        In the case of an incident entry the type of incident.
    [description]
        Identification of what happened during the incident.  Can be more than one.
    """
    panic_attack = "Panic Attack"
    nightmare = "Nightmare"
    excessive_anger_outburst = "Excessive Anger Outburst"
    disassociation  = "Disassociation"
    disconnected = "Disconnected"
    flashback = "Flashback"
    auditory_flashback = "Auditory Flashback"
    intrusive_thoughts = "Intrusive Thoughts"
    hyper_arousal = "Hyper Arousal"
    other = "Other"

class Entry (Model):
    """[summary]
        Base data structure of the PAT API.
    [description]
        All API calls will perform CRUD operations against the record using the appID. No personal identifiable data should be submitted.

    """
    appID : str
    entry_type : EntryType
    entry_date : datetime.date
    journal_entry : Optional[str] = None
    trauma_entry : Optional[str] = None
    incident_severity : Optional[IncidentSeverity]
    incident_type : Optional[Set[IncidentType]] = None
    location : Optional[str] = None
    location_description : Optional[str] = None

    class Config:
        schema_extra = {
            "example": {
                "appID" : "00000001",
                "entry_type" : "Incident",
                "entry_date" : "2020-1-1",
                "journal_entry" : "This is a jouurnal entry which is an optional field and free form text entry",
                "trauma_entry" : "This is a trauma entry which is just like a journal entry, but flagged as a traumatic event to make it easier to share with a healthcare provider.",
                "incident_severity" : 3,
                "incident_type" : "nightmare",
                "location" : "111 Microsoft Way",
                "location_description" : "Work"
            }
        }


import pytest
from gpaw.utilities import compiled_with_libvdwxc

from ase.build import bulk

from gpaw import GPAW, Davidson, Mixer, PW
from gpaw.xc.libvdwxc import vdw_mbeef

from gpaw.test import gen

pytestmark = pytest.mark.skipif(not compiled_with_libvdwxc(),
                                reason='not compiled_with_libvdwxc()')

@pytest.mark.mgga
def test_vdw_libvdwxc_mbeef():
    setup = gen('Si', xcname='PBEsol')

    system = bulk('Si')
    calc = GPAW(mode=PW(200), xc=vdw_mbeef(),
                kpts=(2, 2, 2),
                nbands=4,
                convergence=dict(density=1e-6),
                mixer=Mixer(1.0),
                eigensolver=Davidson(4),
                setups={'Si': setup})
    system.calc = calc
    e = system.get_potential_energy()
    ref = -60.53662105617721
    err = abs(e - ref)
    print('e=%r ref=%r err=%r' % (e, ref, err))
    # It would be reasonable to put 1e-6 as tolerance,
    # but the value changes by 4e-4 depending on libxc version.
    # See https://gitlab.com/gpaw/gpaw/issues/161 .
    assert err < 1e-3, err


from ast import List
import random
from typing import Callable, Tuple
import functools

class Genetics:
    def __init__(self, domain: tuple, population_len: int, mutation_rate: float = 0, evaluation_cb: Callable[[float, float], float] = lambda x, y: 0, minima=False, seed=None) -> None:
        self.domain = domain
        self.length = population_len
        self.mutation_rate = mutation_rate
        self.evaluate = evaluation_cb
        self.population = []
        self.minima = minima
        if seed == None:
            _seed = random.random()
            print("Semilla:", _seed)
            random.seed(_seed)
            return

        random.seed(seed)

    def gen_initial_population(self) -> List:
        self.population = [(random.uniform(self.domain[0], self.domain[1]), (random.uniform(
            self.domain[0], self.domain[1]))) for i in range(self.length)]
        return self.population

    def __evaluate(self) -> List:
        minima = -1 if self.minima else 1
        evaluations = [(i, minima*self.evaluate(i[0], i[1]))
                       for i in self.population]
        evaluations.sort(key=lambda i: i[1])
        return evaluations

    def evaluate_population(self) -> List:
        evaluations = self.__evaluate()
        return [(ev[0], -ev[1] if self.minima else ev[1]) for ev in evaluations]

    def format_evaluations(evaluations: List) -> str:
        # evaluations = self.evaluate_population()
        res = ""
        for eval in evaluations:
            res += f"({eval[0][0]},{eval[0][1]})\t->\t{eval[1]}\n"
        return res

    def __normalize_evaluations(evaluations: List) -> List:
        total = functools.reduce(lambda res, e: res+e[1], evaluations, 0.0)
        return [(e[0], e[1]/total) for e in evaluations]

    def __accumulate_evaluations(normalized: List) -> List:
        accumulator = 0
        res = []
        for norm in normalized:
            accumulator += norm[1]
            res.append((norm[0], accumulator))
        return res

    def __choose_parents(accumulated: List) -> List:
        parents = []
        for n in range(0, len(accumulated), 2):
            tolerance = 10
            while tolerance > 0:
                r_a = random.uniform(0, 1)
                r_b = random.uniform(0, 1)
                parent_a = None
                parent_b = None
                for parent in accumulated:
                    if parent_a == None and parent[1] >= r_a:
                        parent_a = parent
                    if parent_b == None and parent[1] >= r_b and parent != parent_a:
                        parent_b = parent
                    if parent_a != None and parent_b != None:
                        break
                if parent_a != None and parent_b != None:
                    break
                tolerance -= 1
            if parent_b == None or parent_a == None:
                raise Exception(
                    "No hay suficiente material genético. Se están creando clones!!")
            parents.append(parent_a)
            parents.append(parent_b)
        return parents

    def __mutate(self, individual: Tuple) -> Tuple:
        r = random.uniform(0, 1)
        if r < self.mutation_rate:
            left = random.randrange(0, 1)
            if left == 1:
                return (random.uniform(self.domain[0], self.domain[1]), individual[1])
            return (individual[0], random.uniform(self.domain[0], self.domain[1]))
        return individual

    def __combine_and_mutate(self, parents: List) -> List:
        children = []
        for i in range(0, len(parents), 2):
            parent_a = parents[i][0]
            parent_b = parents[i+1][0]
            child_a = self.__mutate((parent_a[0], parent_b[1]))
            child_b = self.__mutate((parent_b[0], parent_a[1]))
            children.append(child_a)
            children.append(child_b)
        return children

    def next_generation(self) -> List:
        evaluations = self.__evaluate()
        evaluations = Genetics.__normalize_evaluations(evaluations)
        evaluations = Genetics.__accumulate_evaluations(evaluations)
        parents = Genetics.__choose_parents(evaluations)
        children = self.__combine_and_mutate(parents)
        self.population = children
        return children


n, m, v, u = map(int, input().split())
print(v, u)

mos = []
for i in range(m):
    a = list(map(int, input().split()))
    mos.append(a)
print(mos)

k = int(input())
if k != 0:
    zat = list(map(int, input().split()))
else:
    zat = []
print(zat)

ways = []
c = False
for i in mos:
    if v in i:
        try:
            n = i[i.index(v)+1]
        except:
            n = i[i.index(v)-1]
        if n not in zat:
            ways.append([v, n])
print(ways)
print()

for i in ways:
    for b in mos:
        if i[-1] in b:
            try :
                n = b[b.index(i[-1])+1]
            except:
                n = b[b.index(i[-1])-1]
            if n not in i and n not in zat:
                i.append(n)
        print(ways)
        if i[-1] == u:
            c = True
            print('break')
            break

if c == True:
    print('YES')
else:
    print('NO')


import os, sys
import pickle

sys.path.append("../../../")
from data.otb import *
from forecasters import load_forecaster

import torch as tc

if __name__ == "__main__":
    dsld = loadOTB("../datasets/otb", 100, bb_format="xyxy")
    ld_names = ['val1', 'val2', 'test']
    lds = [dsld.val1, dsld.val2, dsld.test]
    root = "../datasets/otb_precomp"

    model = load_forecaster(None)
    model.cuda()
    model.eval()

    # extract response
    for ld_name, ld in zip(ld_names, lds):
        subroot = os.path.join(root, ld_name)
        os.makedirs(subroot, exist_ok=True)
        i = 0
        for xs, ys in ld:
            xs = [x.cuda() for x in xs]
            ys = ys.cuda()

            yhs, yhs_var = model(xs)
            ys = model.baseF.encode_bb(xs, ys, model.baseF.opts)

            for y, yh, yh_var in zip(ys, yhs, yhs_var):
                fn = os.path.join(subroot, "%d.pk"%(i))
                print(fn)
                yh = yh.detach().cpu()
                yh_var = yh_var.detach().cpu()
                y = y.detach().cpu()
                pickle.dump((yh, yh_var, y), open(fn, "wb"))
                i += 1


import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
import re
import string
import joblib
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import GradientBoostingClassifier, RandomForestClassifier

# Load data
data_fake = pd.read_csv("Fake.csv")
data_true = pd.read_csv("True.csv")

# Add label to data
data_fake["class"] = 0
data_true['class'] = 1

# Remove last 10 rows from each dataset for manual testing
data_fake_manual_testing = data_fake.tail(10)
for i in range(23480, 23470, -1):
    data_fake.drop([i], axis=0, inplace=True)

data_true_manual_testing = data_true.tail(10)
for i in range(21416, 21406, -1):
    data_true.drop([i], axis=0, inplace=True)

# Merge data
data_merge = pd.concat([data_fake, data_true], axis=0)
data_merge = data_merge.sample(frac=1).reset_index(drop=True)

# Clean data
def clean_text(text):
    text = text.lower()
    text = re.sub('\[.*?\]', '', text)
    text = re.sub("\\W", " ", text)
    text = re.sub('https?://\S+|www.\.\S+', '', text)
    text = re.sub('<.*?>+', '', text)
    text = re.sub('[%s]' % re.escape(string.punctuation), '', text)
    text = re.sub('\n', '', text)
    text = re.sub('\w*\d\w*', '', text)
    return text

data_merge['text'] = data_merge['text'].apply(clean_text)

# Split data into train and test sets
x = data_merge['text']
y = data_merge['class']
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=42)

# Vectorize data
vectorizer = TfidfVectorizer()
xv_train = vectorizer.fit_transform(x_train)
xv_test = vectorizer.transform(x_test)
joblib.dump(vectorizer, 'vectorizer.pkl')

# Train models
models = {'Logistic Regression': LogisticRegression(), 'Decision Tree': DecisionTreeClassifier(),
          'Gradient Boosting': GradientBoostingClassifier(), 'Random Forest': RandomForestClassifier()}
best_model = None
best_score = 0

for name, model in models.items():
    model.fit(xv_train, y_train)
    joblib.dump(model, f'{name}.pkl')
    score = model.score(xv_test, y_test)
    if score > best_score:
        best_score = score
        best_model = model
    print(f"{name} score: {score}")
    pred = model.predict(xv_test)
    print(classification_report(y_test, pred))

# Use best model for manual testing
def out_label(n):
    if n == 0:
        return "FAKE NEWS"
    else :
        return "IT IS NOT"


#!/usr/bin/python
# -*- coding: utf-8 -*-
#
#Author: Read AUTHORS file.
#License: Read COPYING file.

import xml.etree.cElementTree as etree
from os import path
from pkg import pkg
from constants import const
from ..lib import eventHandler, listdir, getfile, ratioCalc

class pkgdir(object):

    def __init__(self, actCode):
        self.__clear()
        self.onError = eventHandler()       #(ActionCode, Error code, data)
        self.onProcessing = eventHandler()   #(ActionCode, ProcessRatio, data)
        self.__actCode = actCode

    def __clear(self):
        self.__path = ""
        self.__packages = {}        #{"pkg name": pkg obj, ...}

    def setTarget(self, p):
        self.__clear()
        self.__path = path.normpath(p)

    def loadList(self):
        pkgdirs = listdir(self.__path)
        if not pkgdirs:
            return False
        return(pkgdirs)

    def loadPackageInfo(self, d):
        xml = getfile( path.join(self.__path, d, const.PACKAGE_INFO_FILE) )
        if xml:
            package = pkg(xml)
            if package.right:
                self.__packages[package.n] = package
                return(True)
        return(False)

    def load(self, p):
        #
        self.__clear()
        self.__path = path.normpath(p)
        pkgdirs = listdir(self.__path)
        if not pkgdirs:
            return False
        totalPackages = len(pkgdirs)
        for d in pkgdirs:
            xml = getfile(
                    path.join(self.__path, d, const.PACKAGE_INFO_FILE)
                )
            if not xml:
                self.__clear()
                return(False)
            package = pkg(xml)
            if package.right:
                self.__packages[package.n] = package
                self.onProcessing.raiseEvent(
                            self.__actCode,
                            ratioCalc(totalPackages, len(self.__packages)),
                            package.n
                        )
            else:
                self.__clear()
                return(False)
        return(True)

    def getPackages(self):
        return(self.__packages)


"""Module to write data classes information."""

# Python packages
import yaml
import logging as log

# write_data_class ------------------------------------------------------------
def write_data_class(data, file='data.yaml'):
    """Take a data object and write its attributes.

    Args:
        data: object with __dict__ attribute.
        file (:obj:`str`): filename.
    """
    log.debug('Write data')
    with open(file, 'w') as f:
        yaml.dump(data.__dict__, f, default_flow_style=False)
# --------------------------------------------------------------------------- #

# write_solution_as_csv -------------------------------------------------------
def write_solution_as_csv(
        data_dict, header=True, mode='w', file='data.csv', dec=".", sep=";"):
    """Take a data object and write its attributes.

    Args:
        data_dict (:obj:`dict`): dictionary.
        header (:obj:`bool`): if True write the header.
        mode (:obj:`str`): write mode: 'w' or 'a'.
        file (:obj:`str`): filename.
        dec (:obj:`str`): decimal point.
        sep (:obj:`str`): column separator
    """
    with open(file, mode) as f:
        if header:
            f.write(sep.join(data_dict.keys()) + '\n')

        f.write(sep.join([str(v).replace(".", dec)
                          if isinstance(v, (int, float)) else
                          v if v is not None else "None"
                          for v in data_dict.values()]) + '\n')
# --------------------------------------------------------------------------- #


from datetime import timedelta, datetime
import os
from airflow import DAG
from google.cloud import storage
from airflow.utils.dates import days_ago
from airflow.operators.dummy_operator import DummyOperator
from airflow.operators.bash import BashOperator
from airflow.decorators import task, dag
from airflow.operators.email import EmailOperator
from airflow.providers.google.cloud.operators.dataproc import (
    DataprocCreateClusterOperator,
    DataprocDeleteClusterOperator,
    DataprocSubmitJobOperator,
    ClusterGenerator
)
from airflow.providers.google.cloud.sensors.dataproc import DataprocJobSensor
from airflow.providers.google.cloud.transfers.gcs_to_bigquery import GCSToBigQueryOperator

GOOGLE_CONN_ID = "google_cloud_default"
PROJECT_ID="data-engineering-rj"
BUCKET_NAME = 'fhvhv-data-lake'
CLUSTER_NAME = 'fhvhvcluster'
REGION = 'us-central1'
PYSPARK_FILENAME ='spark_processing.py'
PYSPARK_URI = f'gs://fhvhv-data-lake/spark-job/{PYSPARK_FILENAME}'
LOCAL_PATH = os.environ.get("AIRFLOW_HOME", "/opt/airflow/")
BIGQUERY_TABLE = 'data-engineering-rj.fhvhv_analysis.fhvhv_trips_data'
PROCESSED_DATA_PATH = 'gs://fhvhv-data-lake/output_fhv_data/trips_data/*.parquet'

'''
Process:

- create a dataproc cluster
- upload a pyspark file to gcs bucket
- submit spark job to dataproc cluster
- excute pyspark job(load data from gcs-> transform data -> submit data to GCS -> Submit data to bigquery )
- delete the cluster
- submit processed data from GCS to BigQuery

'''

PYSPARK_JOB = {
    "reference":{"project_id":PROJECT_ID},
    "placement":{"cluster_name":CLUSTER_NAME},
    "pyspark_job":{"main_python_file_uri":PYSPARK_URI}

}

CLUSTER_CONFIG = ClusterGenerator(
    project_id = PROJECT_ID,
    zone="us-central1-a",
    master_machine_type="n1-standard-2",
    worker_machine_type="n1-standard-2",
    num_workers=2,
    worker_disk_size=40,
    master_disk_size=30,
    storage_bucket=BUCKET_NAME,
).make()

default_args = {
    'owner': 'Rohit Joshi',
    'depends_on_past': False,
    'email_on_failure': True,
    'email_on_retry': False,
    'email_on_success':True,
    'retries': 1,
    'start_date':  days_ago(1),
    'retry_delay': timedelta(minutes=3),
    'email_on_success': False,
    'schedule_interval':'@once',
    'email': ['rohitjoshi9july@gmail.com']
}

@task(task_id="upload_pyspark_file")
def upload_to_gcs(bucket_name, filename):
    local_path = f"/opt/{filename}"
    target_path = f"spark-job/{filename}"
    storage.blob._DEFAULT_CHUNKSIZE = 5 * 1024 * 1024
    storage.blob._MAX_MULTIPART_SIZE = 5 * 1024 * 1024

    client = storage.Client()
    bucket = client.bucket(bucket_name)

    blob = bucket.blob(target_path)
    blob.upload_from_filename(local_path)
    print("file added successfully")

with DAG("Spark_FHVHV_ETL", default_args = default_args) as dag:
    os.environ['GOOGLE_APPLICATION_CREDENTIALS ']= '/home/rohit/.gc/de-cred.json' #path of google service account credentials
    start_pipeline = DummyOperator(
        task_id= "start_pipeline",
        dag=dag
    )

    #create dataproc cluster
    create_cluster = DataprocCreateClusterOperator(
        task_id="create_dataproc_cluster",
        project_id=PROJECT_ID,
        cluster_config=CLUSTER_CONFIG,
        region=REGION,
        cluster_name =CLUSTER_NAME,
        priority_weight=4

    )

    #upload pyspark code to gcs
    upload_pyspark_file = upload_to_gcs(BUCKET_NAME, PYSPARK_FILENAME)

    #submit pyspark job to dataproc
    execute_pyspark_task = DataprocSubmitJobOperator(
        task_id="submit_pyspark_job",
        job=PYSPARK_JOB,
        region=REGION,
        project_id=PROJECT_ID,
        priority_weight=2
    )

    #delete cluster after processing
    delete_cluster = DataprocDeleteClusterOperator(
        task_id="delete_dataproc_cluster",
        project_id=PROJECT_ID,
        cluster_name=CLUSTER_NAME,
        region=REGION,
        priority_weight=1
    )

    #submit processed data from GCS to BQ
    gcs_to_bq = GCSToBigQueryOperator(
        task_id= "submit_processed_data_to_bigquery",
        bucket= BUCKET_NAME,
        source_objects=[PROCESSED_DATA_PATH],
        destination_project_dataset_table=BIGQUERY_TABLE,
        source_format='parquet',
        autodetect=True,
        cluster_fields=['trip_month']

    )

    finish_pipeline = DummyOperator(
        task_id="finish_pipeline",
        dag=dag
    )

    start_pipeline >> create_cluster >> upload_pyspark_file >> execute_pyspark_task >> delete_cluster >> gcs_to_bq >> finish_pipeline


import torch.nn as nn
import torch
import torch.nn.functional as F

up_kwargs = {'mode': 'bilinear', 'align_corners': True}
norm_layer = nn.BatchNorm2d

class Conv2dReLU(nn.Sequential):
    def __init__(
            self,
            in_channels,
            out_channels,
            kernel_size,
            padding=0,
            stride=1,
            use_batchnorm=True,
    ):

        conv = nn.Conv2d(
            in_channels,
            out_channels,
            kernel_size,
            stride=stride,
            padding=padding,
            bias=not (use_batchnorm),
        )
        relu = nn.ReLU(inplace=True)

        if use_batchnorm:
            bn = nn.BatchNorm2d(out_channels)
        else:
            bn = nn.Identity()

        super(Conv2dReLU, self).__init__(conv, bn, relu)

class SCSEAttention(nn.Module):
    def __init__(self, in_channels, reduction=16):
        super().__init__()
        self.cSE = nn.Sequential(
            nn.AdaptiveAvgPool2d(1),
            nn.Conv2d(in_channels, in_channels // reduction, 1),
            nn.ReLU(inplace=True),
            nn.Conv2d(in_channels // reduction, in_channels, 1),
            nn.Sigmoid(),
        )
        self.sSE = nn.Sequential(nn.Conv2d(in_channels, 1, 1), nn.Sigmoid())

    def forward(self, x):
        return x * self.cSE(x) + x * self.sSE(x)

class DecoderBlock(nn.Module):
    def __init__(
            self,
            in_channels,
            skip_channels,
            out_channels,
            use_batchnorm=True,
            use_attention=False,
    ):
        super().__init__()
        self.conv1 = Conv2dReLU(
            in_channels + skip_channels,
            out_channels,
            kernel_size=3,
            padding=1,
            use_batchnorm=use_batchnorm,
        )

        self.conv2 = Conv2dReLU(
            out_channels,
            out_channels,
            kernel_size=3,
            padding=1,
            use_batchnorm=use_batchnorm,
        )
        self.use_attention = use_attention
        if self.use_attention:
            self.attention1 = SCSEAttention(in_channels=in_channels + skip_channels)
            self.attention2 = SCSEAttention(in_channels=out_channels)

    def forward(self, x, skip=None):
        x = F.interpolate(x, scale_factor=2, **up_kwargs)
        if skip is not None:
            x = torch.cat([x, skip], dim=1)
            if self.use_attention:
                x = self.attention1(x)
        x = self.conv1(x)
        x = self.conv2(x)
        if self.use_attention:
            x = self.attention2(x)

        return x

class CenterBlock(nn.Sequential):
    def __init__(self, in_channels, out_channels, use_batchnorm=True):
        conv1 = Conv2dReLU(
            in_channels,
            out_channels,
            kernel_size=3,
            padding=1,
            use_batchnorm=use_batchnorm,
        )
        conv2 = Conv2dReLU(
            out_channels,
            out_channels,
            kernel_size=3,
            padding=1,
            use_batchnorm=use_batchnorm,
        )
        super().__init__(conv1, conv2)

class UNetHead(nn.Module):
    def __init__(
            self,
            in_channels,
            num_classes=6,
            n_blocks=4,
            use_batchnorm=True,
            use_attention=False,
            center=False,
            in_index=[0, 1, 2, 3],
    ):
        super(UNetHead, self).__init__()
        self.in_index = in_index
        decoder_channels = [in_channels[i] // 4 for i in self.in_index]
        if n_blocks != len(decoder_channels):
            raise ValueError(
                "Model depth is {}, but you provide `decoder_channels` for {} blocks.".format(
                    n_blocks, len(decoder_channels)
                )
            )
        encoder_channels = in_channels[::-1]  # reverse channels to start from head of encoder

        # computing blocks input and output channels
        head_channels = encoder_channels[0]
        in_channels = [head_channels] + list(decoder_channels[:-1])
        skip_channels = list(encoder_channels[1:]) + [0]
        out_channels = decoder_channels

        if center:
            self.center = CenterBlock(
                head_channels, head_channels, use_batchnorm=use_batchnorm
            )
        else:
            self.center = nn.Identity()
        # combine decoder keyword arguments
        kwargs = dict(use_batchnorm=use_batchnorm, use_attention=use_attention)
        blocks = [
            DecoderBlock(in_ch, skip_ch, out_ch, **kwargs)
            for in_ch, skip_ch, out_ch in zip(in_channels, skip_channels, out_channels)
        ]
        self.blocks = nn.ModuleList(blocks)
        self.head = nn.Conv2d(out_channels[-1], num_classes, kernel_size=1)

    def _transform_inputs(self, inputs):
        if isinstance(self.in_index, (list, tuple)):
            inputs = [inputs[i] for i in self.in_index]
        elif isinstance(self.in_index, int):
            inputs = inputs[self.in_index]
        return inputs

    def forward(self, features):

        features = self._transform_inputs(features)
        features = features[::-1]  # reverse channels to start from head of encoder

        head = features[0]
        skips = features[1:]

        x = self.center(head)
        for i, decoder_block in enumerate(self.blocks):
            skip = skips[i] if i < len(skips) else None
            x = decoder_block(x, skip)
        x = self.head(x)
        return x


import cv2
import numpy as np
import os
import time

label_names = ['scissors']

for label_name in label_names:
  IMG_SAVE_PATH = 'test_images2'
  IMG_CLASS_PATH = os.path.join(IMG_SAVE_PATH, label_name)
  try:
    os.mkdir(IMG_SAVE_PATH)
  except FileExistsError:
    pass
  try:
    os.mkdir(IMG_CLASS_PATH)
  except FileExistsError:
    pass

  cap = cv2.VideoCapture(0)
  start = False
  count = 300
  while True:
    ret, frame = cap.read()
    if not ret:
      continue

    if count == 400:
      break

    cv2.rectangle(frame, (50, 50), (400, 400), (255, 255, 255), 2)

    if start:
      roi = frame[50:400, 50:400]
      save_path = os.path.join(IMG_CLASS_PATH, '{}.jpg'.format(count+1))
      cv2.imwrite(save_path, roi)
      count += 1

    font = cv2.FONT_HERSHEY_SIMPLEX
    cv2.putText(frame, f"Collecting {count}", (5,50), font, 0.7, (0, 255, 255), 2, cv2.LINE_AA)
    cv2.imshow("Collecting images", frame)
    time.sleep(0.2)
    k = cv2.waitKey(10)
    if k == ord('a'):
      start = not start
    if k == ord('q'):
      break
  print(f"\n{count} images saved to {IMG_CLASS_PATH}")
  cap.release()
  cv2.destroyAllWindows()


"""
TACCJM Hug Server

Server for managing instances of TACCJobManager classes via http endpoints
using the hug framework.

TODO: Add more extensive logging
"""
import pdb
import os
import hug
import falcon
import logging
import json
from typing import Union, List, Tuple
from taccjm.TACCJobManager import TACCJobManager, TJMCommandError

__author__ = "Carlos del-Castillo-Negrete"
__copyright__ = "Carlos del-Castillo-Negrete"
__license__ = "MIT"

# Initialize server logger
logger = logging.getLogger()
logging.basicConfig(level=logging.DEBUG)

# Dictionary containing all job manager instances being managed
# Note there could be multiple instance if managing more than one system
JM = {}

@hug.exception(ValueError)
def handle_custom_exceptions(exception):
    """Handling exceptions when ValueError due to input thrown."""

    # Raise 400 Bad Request if invalid data type passed
    err = falcon.HTTPError(falcon.HTTP_400, "BadRequest", str(exception))
    logger.error(str(err))
    raise err

@hug.exception(FileNotFoundError)
def handle_custom_exceptions(exception):
    """Handling exceptions when resource can't be found."""

    # Raise 404 not found error if local or remoate path don't exist
    err = falcon.HTTPError(falcon.HTTP_404, "NotFound", str(exception))
    logger.error(str(err))
    raise err

@hug.exception(PermissionError)
def handle_custom_exceptions(exception):
    """Handling exception when don't have access to resource"""

    # Raise 403 forbidden if dont have permissions to access either paath
    err = falcon.HTTPError(falcon.HTTP_403, "Forbidden", str(exception))
    logger.error(str(err))
    raise err

@hug.exception(TJMCommandError)
def handle_custom_exceptions(exception):
    """Handling other command errors"""

    # Log TCJMCommandError message
    logger.error(f"{str(exception)}")

    # Raise 500 internal server error for unanticipated error
    err = falcon.HTTPError(falcon.HTTP_500, "Command error", str(exception))

    logger.error(str(err))
    raise err

def _check_init(jm_id):
    """Check if Job Manager is initalized"""
    if jm_id not in JM.keys():
        raise falcon.HTTPError(falcon.HTTP_404,
                "jm_error", f"TACCJM {jm_id} does not exist.")

@hug.post('/init')
def init_jm(jm_id:str, system:str, user:str, psw:str, mfa:str):
    """Initialize Job Manager Instances"""
    global JM

    if jm_id not in JM.keys():
        try:
            logger.info(f"INIT - Initializing TACCJM {jm_id}.")
            JM[jm_id] = TACCJobManager(system, user=user, psw=psw, mfa=mfa,
                    working_dir=jm_id)
            logger.info(f"SUCCESS - TACCJM {jm_id} initialized successfully.")

            ret = {'jm_id':jm_id,
                    'sys':JM[jm_id].system,
                   'user':JM[jm_id].user,
                   'apps_dir':JM[jm_id].apps_dir,
                   'jobs_dir':JM[jm_id].jobs_dir,
                   'scripts_dir':JM[jm_id].scripts_dir,
                   'trash_dir':JM[jm_id].trash_dir}
            return ret
        except ValueError as v:
            # Raise Not Found HTTP code for non TACC system
            msg = f"Unable to initialize {jm_id} on {system} for {user}: {v}"
            raise falcon.HTTPError(falcon.HTTP_404, "jm_error", msg)
        except Exception as e:
            # Raise Unauthorized HTTP code for bad login to system
            msg = f"Unable to initialize {jm_id} on {system} for {user}: {e}"
            raise falcon.HTTPError(falcon.HTTP_401, "jm_error", msg)
    else:
        # Raise Conflict HTTP error
        raise falcon.HTTPError(falcon.HTTP_409,
                "jm_error", f"TACCJM {jm_id} already exists.")

@hug.get('/list')
def list_jm():
    """Show initialized job managers"""
    out = []
    for jm in JM.keys():
        out.append({'jm_id':jm, 'sys':JM[jm].system, 'user':JM[jm].user,
            'apps_dir':JM[jm].apps_dir, 'jobs_dir':JM[jm].jobs_dir})
    return out

@hug.get('/{jm_id}')
def get_jm(jm_id:str):
    """Get Job Manager managed by this server if it exists"""

    _check_init(jm_id)

    jm = {'jm_id':jm_id, 'sys':JM[jm_id].system, 'user':JM[jm_id].user,
          'apps_dir':JM[jm_id].apps_dir, 'jobs_dir':JM[jm_id].jobs_dir,
          'trash_dir':JM[jm_id].trash_dir, 'scripts_dir':JM[jm_id].scripts_dir}
    return jm

@hug.get('/{jm_id}/queue')
def get_queue(jm_id:str, user:str=None):
    """Show job queue for user on system."""

    _check_init(jm_id)

    return JM[jm_id].showq(user=user)

@hug.get('/{jm_id}/allocations')
def allocations(jm_id:str):
    """List all allocatiosn for user on system."""

    _check_init(jm_id)

    return JM[jm_id].get_allocations()

@hug.get('/{jm_id}/files/list')
def list_files(jm_id:str, path:str="."):
    """List files on system"""
    _check_init(jm_id)

    logger.info(f'Getting files from {path}')
    files = JM[jm_id].list_files(path=path)
    _ = [f.pop('ls_str') for f in files]

    return files

@hug.get('/{jm_id}/files/peak')
def peak_file(jm_id:str, path:str, head:int=-1, tail:int=-1):
    """Peak File

    Extract first or last lines of a file via head/tail command.
    """
    _check_init(jm_id)

    return JM[jm_id].peak_file(path, head=head, tail=tail)

@hug.put('/{jm_id}/files/upload')
def upload(jm_id:str, local_path:str, remote_path:str, file_filter:str='*'):
    """File Upload

    Upload file or folder to TACC system for given job manager

    """
    _check_init(jm_id)

    JM[jm_id].upload(local_path, remote_path, file_filter=file_filter)

@hug.get('/{jm_id}/files/download')
def download(jm_id:str, remote_path:str, local_path:str, file_filter:str='*'):
    """File Download

    Download file or folder to TACC system for given job manager to local path

    """
    _check_init(jm_id)

    JM[jm_id].download(remote_path, local_path, file_filter=file_filter)

@hug.delete('/{jm_id}/files/remove')
def remove(jm_id:str, remote_path:str):
    """Remove file. In reality just moves file to JM's trash directory.

    """
    _check_init(jm_id)

    JM[jm_id].remove(remote_path)

@hug.put('/{jm_id}/files/restore')
def restore(jm_id:str, remote_path:str):
    """Restore file. Restore previously removed file in trash directory to original location.

    """
    _check_init(jm_id)

    JM[jm_id].restore(remote_path)

@hug.put('/{jm_id}/files/write')
def write(jm_id:str, data, remote_path:str):
    """Write file

    Write text or json data directly to a file path on JM's remote system.
    """
    _check_init(jm_id)

    JM[jm_id].write(data, remote_path)

@hug.get('/{jm_id}/files/read')
def read(jm_id:str, remote_path:str, data_type:str='text'):
    """Read file

    Read text or json file directly from path on remote system managed by by
    job manager instance.
    """
    _check_init(jm_id)

    return JM[jm_id].read(remote_path, data_type=data_type)

@hug.get('/{jm_id}/apps/list')
def list_apps(jm_id:str):
    """List Apps

    Gets all apps.
    """
    _check_init(jm_id)

    return JM[jm_id].get_apps()

@hug.get('/{jm_id}/apps/{app_id}')
def get_app(jm_id:str, app_id:str):
    """Get App

    Get configuration for a deploy HPC Application.

    """
    _check_init(jm_id)

    return JM[jm_id].get_app(app_id)

@hug.post('/{jm_id}/apps/deploy')
def deploy_app(jm_id:str,
               local_app_dir:str='.',
               app_config_file:str="app.json",
               overwrite:bool=False) -> dict:
    """Deploy App

    Deploy an application from local directory to TACC system

    """
    _check_init(jm_id)

    logger.info(f"{jm_id} - deploying app - {local_app_dir}/{app_config_file}")

    return JM[jm_id].deploy_app(
                local_app_dir=local_app_dir,
                app_config_file=app_config_file,
                overwrite=overwrite)

@hug.get('/{jm_id}/jobs/list')
def list_jobs(jm_id:str):
    """Gets all jobs."""
    _check_init(jm_id)

    return JM[jm_id].get_jobs()

@hug.get('/{jm_id}/jobs/{job_id}')
def get_job(jm_id:str, job_id:str):
    """Get Job

    Get job configuration for job deployed on TACC system.

    """
    _check_init(jm_id)

    return JM[jm_id].get_job(job_id)

@hug.post('/{jm_id}/jobs/deploy')
def deploy_job(jm_id:str,
               job_config:str=None,
               local_job_dir:str='.',
               job_config_file:str='job.json',
               stage:bool=True,
               **kwargs):
    """ Deploy a job to TACC system. """
    _check_init(jm_id)

    msg = f"{jm_id} - deploying job at path {local_job_dir}/{job_config_file}"
    logger.info(msg)

    return JM[jm_id].deploy_job(job_config = None if job_config is None else json.loads(job_config),
                                local_job_dir=local_job_dir,
                                job_config_file=job_config_file,
                                stage=stage,
                                **kwargs)

@hug.put('/{jm_id}/jobs/{job_id}/submit')
def submit_job(jm_id:str, job_id:str):
    """Submit job

    Submit a job to the Slurm Job Queue on given TACC system

    """
    _check_init(jm_id)

    return JM[jm_id].submit_job(job_id)

@hug.put('/{jm_id}/jobs/{job_id}/cancel')
def cancel_job(jm_id:str, job_id:str):
    """Cancel Job

    Cancels a job that has been submitted to the SLURM job queue on given
    TACC system

    """
    _check_init(jm_id)

    return JM[jm_id].cancel_job(job_id)

@hug.delete('/{jm_id}/jobs/{job_id}/remove')
def remove_job(jm_id:str, job_id:str):
    """Cleanup Job

    Removes job directory (Sends it to trash) on given TACC system.

    """

    return JM[jm_id].remove_job(job_id)

@hug.post('/{jm_id}/jobs/{job_id}/restore')
def restore_job(jm_id:str, job_id:str):
    """Cleanup Job

    Restores a job directory from the trash dir on given TACC system.

    """

    return JM[jm_id].restore_job(job_id)

@hug.get('/{jm_id}/jobs/{job_id}/files/list')
def list_job_files(jm_id:str, job_id:str, path:str=''):
    """List Job files

    List files in a job directory.

    """
    _check_init(jm_id)

    logger.info(f'Getting files from {path}')
    files = JM[jm_id].ls_job(job_id, path=path)
    _ = [f.pop('ls_str') for f in files]

    return files

@hug.get('/{jm_id}/jobs/{job_id}/files/download')
def download_job_file(jm_id:str, job_id:str, path:str, dest_dir:str='.'):
    """Download Job file/folder

    Download a file/folder from a job directory

    """
    _check_init(jm_id)

    return JM[jm_id].download_job_file(job_id, path, dest_dir=dest_dir)

@hug.get('/{jm_id}/jobs/{job_id}/files/read')
def read_job_file(jm_id:str, job_id:str, path:str, data_type:str='text'):
    """Read Job file

    Read a job text or json file and return contents directly.

    """
    _check_init(jm_id)

    return JM[jm_id].read_job_file(job_id, path, data_type=data_type)

@hug.put('/{jm_id}/jobs/{job_id}/files/upload')
def upload_job_file(jm_id:str, job_id:str,
        path:str, dest_dir:str='.', file_filter:str='*'):
    """Upload Job File/Folder

    Uplaod a file/folder to a job's directory

    """
    _check_init(jm_id)

    JM[jm_id].upload_job_file(job_id, path, dest_dir=dest_dir,
                file_filter=file_filter)

@hug.put('/{jm_id}/jobs/{job_id}/files/write')
def write_job_file(jm_id:str, job_id:str, data, path:str):
    """Write Job file

    Write text or json data to a file in a job directory directly.

    """
    _check_init(jm_id)

    return JM[jm_id].write_job_file(job_id, data, path)

@hug.get('/{jm_id}/jobs/{job_id}/files/peak')
def peak_job_file(jm_id:str,
        job_id:str, path:str, head:int=-1, tail:int=-1):
    """Peak Job File

    Extract first or last lines of a file in job directory via head/tail command.
    """
    _check_init(jm_id)

    return JM[jm_id].peak_job_file(job_id, path, head=head, tail=tail)

@hug.get('/{jm_id}/scripts/list')
def list_scripts(jm_id:str):
    """List Scripts

    """
    _check_init(jm_id)

    return JM[jm_id].list_scripts()

@hug.post('/{jm_id}/scripts/deploy')
def deploy_script(jm_id:str, script_name:str, local_file:str=None):
    """Deploy Script

    """
    _check_init(jm_id)

    JM[jm_id].deploy_script(script_name, local_file=local_file)

@hug.put('/{jm_id}/scripts/run')
def run_script(jm_id:str, script_name:str,
        job_id:str=None, args:hug.types.multiple=[]):
    """Run Script

    """
    _check_init(jm_id)

    return JM[jm_id].run_script(script_name, job_id=job_id, args=args)

@hug.delete('/{jm_id}/trash/empty')
def empty_trash(jm_id:str, filter_str:str='*'):
    """ Empty trash directory """
    _check_init(jm_id)

    return JM[jm_id].empty_trash(filter_str=filter_str)


import argparse
import inspect

def parse_funct_arguments(fn, args=None, free_arguments=None):
    if free_arguments is None:
        free_arguments = []
    fn_parser = argparse.ArgumentParser()
    sign = inspect.signature(fn)
    for pname, pval in sign.parameters.items():
        if pname not in free_arguments:
            fn_parser.add_argument('--'+pname, default=pval.default, type=pval.annotation)
    fn_args, unk = fn_parser.parse_known_args(args)

    def new_fn(*args, **kwargs):
        return fn(*args, **kwargs, **vars(fn_args))

    return new_fn, vars(fn_args), unk


'''
JexoSim
2.0
Recipe 3 with ability to split timeline into chunks - used to test split method

'''
from jexosim.modules import exosystem, telescope, channel, backgrounds
from jexosim.modules import detector, timeline, light_curve, systematics, noise, output
from jexosim.pipeline.run_pipeline import pipeline_stage_1, pipeline_stage_2
from jexosim.lib.jexosim_lib import jexosim_msg, jexosim_plot, write_record
from astropy import units as u
from datetime import datetime
import pickle, os
import numpy as np

class recipe_3a(object):
    def __init__(self, opt):

        output_directory = opt.common.output_directory.val
        filename=""

        self.results_dict ={}
        self.results_dict['simulation_mode'] = opt.simulation.sim_mode.val
        self.results_dict['simulation_realisations'] = opt.simulation.sim_realisations.val
        self.results_dict['ch'] =  opt.observation.obs_channel.val
        self.noise_dict ={}
        self.feasibility =1

        opt.pipeline.useSignal.val=1
        opt.simulation.sim_use_fast.val =1
        opt.pipeline.split  = 0
        opt.noise.ApplyRandomPRNU.val=1

        opt.timeline.apply_lc.val = 0
        opt.timeline.useLDC.val = 0
        opt.pipeline.useAllen.val =1

        opt.pipeline.pipeline_auto_ap.val = 0 # for noise budget keep this to a fixed value (i.e. choose 0) so same for all sims

        opt.timeline.obs_time.val = 0*u.hr
        opt.timeline.n_exp.val = 1000.0

        noise_list = [0,2,3,4,5,6,7,8,9,12,13]
        # noise_list = [0,2,3,4,5,6,7,8,9]
        # noise_list = [2,9,12]

        start = 0
        end = int(start + opt.no_real)

        nb_dict = {'rn'             :[1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
               'sn'                 :[1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1],
               'spat'               :[1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0],
               'spec'               :[1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
               'emm_switch'         :[1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0],
               'zodi_switch'        :[1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0],
               'dc_switch'          :[1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
               'source_switch'      :[1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0],
               'diff'               :[0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1],
               'jitter_switch'      :[1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
               'fano'               :[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
               'sunshield_switch'   :[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
               'noise_tag': [ 'All noise','All photon noise','Source photon noise','Dark current noise',
                        'Zodi noise','Emission noise','Read noise','Spatial jitter noise',
                        'Spectral jitter noise','Combined jitter noise','No noise - no background','No noise - all background', 'Fano noise', 'Sunshield noise'],
                        'color': ['0.5','b', 'b','k','orange','pink', 'y','g','purple','r', '0.8','c', 'c','brown']
              }

        for i in noise_list:
            self.noise_dict[nb_dict['noise_tag'][i]] ={}

        for j in range(start,end):
            seed = np.random.randint(100000000)
            for i in noise_list:

                opt.noise.EnableReadoutNoise.val = nb_dict['rn'][i]
                opt.noise.EnableShotNoise.val = nb_dict['sn'][i]
                opt.noise.EnableSpatialJitter.val= nb_dict['spat'][i]
                opt.noise.EnableSpectralJitter.val= nb_dict['spec'][i]
                opt.noise.EnableFanoNoise.val= nb_dict['fano'][i]
                opt.background.EnableEmission.val = nb_dict['emm_switch'][i]
                opt.background.EnableZodi.val = nb_dict['zodi_switch'][i]
                opt.background.EnableSunshield.val = nb_dict['sunshield_switch'][i]
                opt.background.EnableDC.val  =  nb_dict['dc_switch'][i]
                opt.background.EnableSource.val  = nb_dict['source_switch'][i]
                opt.diff = nb_dict['diff'][i]
                opt.noise_tag = nb_dict['noise_tag'][i]
                opt.color = nb_dict['color'][i]

                jexosim_msg('==========================================', 1)
                jexosim_msg('Noise source:%s'%(opt.noise_tag), 1)

                np.random.seed(seed)
                opt = self.run_JexoSimA(opt)
                opt = self.run_JexoSimA1(opt)

                n_ndr0 = opt.n_ndr*1
                ndr_end_frame_number0 = opt.ndr_end_frame_number*1
                frames_per_ndr0 = opt.frames_per_ndr*1
                duration_per_ndr0 = opt.duration_per_ndr*1
                n_exp0 = opt.n_exp

                if n_ndr0 > 10000:

                    opt.pipeline.split = 1
                    if opt.diagnostics ==1 :
                       jexosim_msg ('number of NDRs > 10000: using split protocol', opt.diagnostics)
                else:
                    opt.pipeline.split = 0

                opt.pipeline.split = 1

              # =============================================================================
              #  split simulation into chunks to permit computation - makes no difference to final results
              #  =============================================================================
                if opt.pipeline.split ==1:

                   jexosim_msg('Splitting data series into chunks', opt.diagnostics)
                   # uses same QE grid and jitter timeline but otherwise randomoses noise
                   ndrs_per_round = opt.effective_multiaccum*int(5000/opt.multiaccum)
                   ndrs_per_round = opt.effective_multiaccum*int(50/opt.multiaccum)

                   total_chunks = len(np.arange(0, n_ndr0, ndrs_per_round))

                   idx = np.arange(0, n_ndr0, ndrs_per_round) # list of starting ndrs

                   for i in range(len(idx)):
                       jexosim_msg('=== Chunk %s / %s====='%(i+1, total_chunks), opt.diagnostics)

                       if idx[i] == idx[-1]:
                           opt.n_ndr = n_ndr0 - idx[i]

                           opt.ndr_end_frame_number = ndr_end_frame_number0[idx[i]:]
                           opt.frames_per_ndr=  frames_per_ndr0[idx[i]:]
                           opt.duration_per_ndr = duration_per_ndr0[idx[i]:]

                       else:
                           opt.n_ndr = idx[i+1]- idx[i]

                           opt.ndr_end_frame_number = ndr_end_frame_number0[idx[i]: idx[i+1]]
                           opt.frames_per_ndr=  frames_per_ndr0[idx[i]: idx[i+1]]
                           opt.duration_per_ndr = duration_per_ndr0[idx[i]: idx[i+1]]

                       opt.n_exp = int(opt.n_ndr/opt.effective_multiaccum)

                       if i == 0:
                           opt.use_external_jitter = 0

                           opt = self.run_JexoSimB(opt)
                           opt  = self.run_pipeline_stage_1(opt)
                           if opt.pipeline.pipeline_auto_ap.val == 1:
                               opt.pipeline.pipeline_ap_factor.val= opt.AvBest
                           if (opt.noise.EnableSpatialJitter.val  ==1 or opt.noise.EnableSpectralJitter.val  ==1 or opt.noise.EnableAll.val == 1) and opt.noise.DisableAll.val != 1:
                               opt.input_yaw_jitter, opt.input_pitch_jitter, opt._input_frame_osf = opt.yaw_jitter, opt.pitch_jitter, opt.frame_osf

                       else:
                           opt.pipeline.pipeline_auto_ap.val = 0
                           opt.use_external_jitter = 1 # uses the jitter timeline from the first realization

                           opt = self.run_JexoSimB(opt)
                           opt  = self.run_pipeline_stage_1(opt)

                       jexosim_msg('Aperture used %s'%(opt.pipeline.pipeline_ap_factor.val), opt.diagnostics)
                       binnedLC = opt.pipeline_stage_1.binnedLC
                       data = opt.pipeline_stage_1.opt.data_raw

                       #After chunks processed, now recombine
                       if i ==0:
                           data_stack = data
                           binnedLC_stack = binnedLC
                       else:
                           data_stack = np.dstack((data_stack,data))
                           binnedLC_stack = np.vstack((binnedLC_stack, binnedLC))

                   aa = data_stack.sum(axis=0)
                   bb = aa.sum(axis=0)
                   jexosim_plot('test_from_sim', opt.diagnostics,
                            ydata=bb[opt.effective_multiaccum::opt.effective_multiaccum] )

                   aa = binnedLC_stack.sum(axis=1)
                   jexosim_plot('test_from_pipeline', opt.diagnostics,
                                        ydata=aa)

                   opt.n_ndr  = n_ndr0
                   opt.ndr_end_frame_number  = ndr_end_frame_number0
                   opt.frames_per_ndr  = frames_per_ndr0
                   opt.duration_per_ndr = duration_per_ndr0
                   opt.n_exp = n_exp0

                elif opt.pipeline.split ==0:

                   opt  = self.run_JexoSimB(opt)
                   if j==start:  # first realization sets the ap, then the other use the same one
                       opt.pipeline.pipeline_auto_ap.val= 1
                   else:
                       opt.pipeline.pipeline_auto_ap.val= 0
                   opt  = self.run_pipeline_stage_1(opt)
                   if j==start:  # first realization sets the ap, then the other use the same one
                       if opt.pipeline.pipeline_apply_mask.val == 1:
                           opt.pipeline.pipeline_ap_factor.val= opt.AvBest

                   binnedLC_stack  = opt.pipeline_stage_1.binnedLC
                   data_stack = opt.pipeline_stage_1.opt.data_raw
                   jexosim_plot('testvvv', opt.diagnostics,
                                ydata=binnedLC_stack.sum(axis=1) )

                #take binnedLC_stack and now complete through pipeline stage 2
                opt.pipeline_stage_1.binnedLC = binnedLC_stack
                opt = self.run_pipeline_stage_2(opt)
                self.pipeline = opt.pipeline_stage_2
                self.opt = opt

                self.noise_dict[opt.noise_tag]['wl'] = self.pipeline.binnedWav

                if j == start:
                    self.noise_dict[opt.noise_tag]['signal_std_stack'] = self.pipeline.ootNoise
                    self.noise_dict[opt.noise_tag]['signal_mean_stack'] = self.pipeline.ootSignal
                    if opt.pipeline.useAllen.val == 1:
                        self.noise_dict[opt.noise_tag]['fracNoT14_stack'] = self.pipeline.noiseAt1hr

                    self.noise_dict[opt.noise_tag]['signal_std_mean'] = self.pipeline.ootNoise
                    self.noise_dict[opt.noise_tag]['signal_mean_mean'] = self.pipeline.ootSignal
                    if opt.pipeline.useAllen.val == 1:
                        self.noise_dict[opt.noise_tag]['fracNoT14_mean'] = self.pipeline.noiseAt1hr

                    self.noise_dict[opt.noise_tag]['signal_std_std'] = np.zeros(len(self.pipeline.binnedWav))
                    self.noise_dict[opt.noise_tag]['signal_mean_std'] = np.zeros(len(self.pipeline.binnedWav))
                    if opt.pipeline.useAllen.val == 1:
                        self.noise_dict[opt.noise_tag]['fracNoT14_std'] = np.zeros(len(self.pipeline.binnedWav))

                    self.noise_dict[opt.noise_tag]['bad_map'] = opt.bad_map
                    self.noise_dict[opt.noise_tag]['example_exposure_image'] = opt.exp_image
                    self.noise_dict[opt.noise_tag]['pixel wavelengths'] = opt.x_wav_osr[1::3].value

                else:
                    self.noise_dict[opt.noise_tag]['signal_std_stack'] = np.vstack((self.noise_dict[opt.noise_tag]['signal_std_stack'], self.pipeline.ootNoise))
                    self.noise_dict[opt.noise_tag]['signal_mean_stack'] = np.vstack((self.noise_dict[opt.noise_tag]['signal_mean_stack'], self.pipeline.ootSignal))
                    if opt.pipeline.useAllen.val == 1:
                        self.noise_dict[opt.noise_tag]['fracNoT14_stack'] = np.vstack((self.noise_dict[opt.noise_tag]['fracNoT14_stack'], self.pipeline.noiseAt1hr))

                    self.noise_dict[opt.noise_tag]['signal_std_mean'] = self.noise_dict[opt.noise_tag]['signal_std_stack'].mean(axis=0)
                    self.noise_dict[opt.noise_tag]['signal_mean_mean'] = self.noise_dict[opt.noise_tag]['signal_mean_stack'].mean(axis=0)
                    if opt.pipeline.useAllen.val == 1:
                        self.noise_dict[opt.noise_tag]['fracNoT14_mean'] = self.noise_dict[opt.noise_tag]['fracNoT14_stack'].mean(axis=0)

                    self.noise_dict[opt.noise_tag]['signal_std_std'] = self.noise_dict[opt.noise_tag]['signal_std_stack'].std(axis=0)
                    self.noise_dict[opt.noise_tag]['signal_mean_std'] = self.noise_dict[opt.noise_tag]['signal_mean_stack'].std(axis=0)
                    if opt.pipeline.useAllen.val == 1:
                        self.noise_dict[opt.noise_tag]['fracNoT14_std'] = self.noise_dict[opt.noise_tag]['fracNoT14_stack'].std(axis=0)

                self.noise_dict[opt.noise_tag]['bad_map'] = opt.bad_map
                self.noise_dict[opt.noise_tag]['example_exposure_image'] = opt.exp_image
                self.noise_dict[opt.noise_tag]['pixel wavelengths'] = opt.x_wav_osr[1::3].value

                self.results_dict['noise_dic'] = self.noise_dict

            # dump pickle file at end of each cycle of noise sims
            if opt.simulation.sim_output_type.val == 1:
                time_tag = (datetime.now().strftime('%Y_%m_%d_%H%M_%S'))
                self.results_dict['time_tag'] =  time_tag

                # if j != start:
                #     os.remove(filename)  # delete previous temp file
                # filename = '%s/Noise_budget_%s_TEMP.pickle'%(output_directory, opt.lab)
                # with open(filename, 'wb') as handle:
                #     pickle.dump(self.results_dict , handle, protocol=pickle.HIGHEST_PROTOCOL)

                if j == end-1:
                    # os.remove(filename)  # delete previous temp file
                    filename = '%s/Noise_budget_%s_%s.pickle'%(output_directory, opt.lab, time_tag)
                    with open(filename, 'wb') as handle:
                        pickle.dump(self.results_dict , handle, protocol=pickle.HIGHEST_PROTOCOL)

                        jexosim_msg('Results in %s'%(filename), 1)
                        self.filename = 'Noise_budget_%s_%s.pickle'%(opt.lab, time_tag)

                        write_record(opt, output_directory, self.filename, opt.params_file_path)

    def run_JexoSimA(self, opt):
      jexosim_msg('Exosystem', 1)
      exosystem.run(opt)
      jexosim_msg('Telescope', 1)
      telescope.run(opt)
      jexosim_msg('Channel', 1)
      channel.run(opt)
      jexosim_msg('Backgrounds', 1)
      backgrounds.run(opt)
      jexosim_msg('Detector', 1)
      detector.run(opt)
      if opt.observation_feasibility ==1: # if detector does not saturate continue
          jexosim_msg('Timeline', 1)
          timeline.run(opt)
          jexosim_msg('Light curve', 1)
          light_curve.run(opt)
          return opt
      else: # if detector saturates end sim
          return opt

    def run_JexoSimA1(self, opt):
      jexosim_msg('Systematics', 1)
      systematics.run(opt)
      return opt

    def run_JexoSimB(self, opt):
      jexosim_msg('Noise', 1)
      noise.run(opt)
      return opt

    def run_pipeline_stage_1(self, opt):
      jexosim_msg('Pipeline stage 1', 1)
      opt.pipeline_stage_1 = pipeline_stage_1(opt)
      return opt

    def run_pipeline_stage_2(self, opt):
      jexosim_msg('Pipeline stage 2', 1)
      opt.pipeline_stage_2 = pipeline_stage_2(opt)
      return opt


import time
import json
from typing import *
import prestodb  # pip install presto-python-client

class PrestoClient:
    def __init__(self):
        props = {"enable_hive_syntax": "true"}
        self.conn = prestodb.dbapi.connect(
            host="presto.bigo.sg",
            port=8285,
            user="baijialuo",
            catalog="hive",
            schema="default",
            source="live_moderation_pipeline",
            session_properties=props,
        )

    def query(self, sql):
        cur = self.conn.cursor()
        cur.execute(sql)
        return cur.fetchall()

    def query_yield(self, sql):
        cur = self.conn.cursor()
        cur.execute(sql)
        while True:
            item = cur.fetchone()
            if item is None:
                return
            yield item

    def close(self):
        self.conn.close()

if __name__ == "__main__":
    START_DAY = "2020-08-10 15:25:18"
    END_DAY = "2020-08-10"
    PROJECT_NAME = "auto-deploy"
    sql = r'select rtime, submodule, cost_ms, message from vlog.likee_evg_content where  rtime >= "{}" and "{}" >= day and project_name = "{}"'.format(
        START_DAY, END_DAY, PROJECT_NAME)
    client = PrestoClient()
    res = client.query(sql)
    for item in res:
        print('rtime:{}\nsubmodule:{}\ncost_ms:{}\nmessage:{}'.format(
            item[0], item[1], item[2], json.loads(item[3])
        ))

    for item in client.query_yield(sql):
        print('rtime:{}\nsubmodule:{}\ncost_ms:{}\nmessage:{}'.format(
            item[0], item[1], item[2], json.loads(item[3])
        ))


import asyncio
import aiofiles

# Define an async function to write data to a file
async def write_to_file(filename, data):
    # Open the file for writing using async with, which ensures the file is closed
    # when we're done with it
    async with aiofiles.open(filename, 'w') as f:
        # Write the data to the file using the await keyword
        await f.write(data)

# Define an async function to read data from a file
async def read_from_file(filename):
    # Open the file for reading using async with, which ensures the file is closed
    # when we're done with it
    async with aiofiles.open(filename, 'r') as f:
        # Read the contents of the file using the await keyword
        data = await f.read()
        # Return the data as a string
        return data

# Define the main coroutine, which will run when we execute the script
async def main():
    # Set up a filename and some data to write to the file
    filename = 'example.txt'
    data = 'Hello, world!'

    # Create tasks to write and read the file concurrently
    write_task = asyncio.create_task(write_to_file(filename, data))
    read_task = asyncio.create_task(read_from_file(filename))

    # Wait for both tasks to complete
    await asyncio.gather(write_task, read_task)

    # Print the contents of the file to the console
    print(read_task.result())

# Run the main coroutine using asyncio.run, which creates and manages the event loop
if __name__ == '__main__':
    asyncio.run(main())


from PIL import Image
import unittest

from lightly.transforms import GaussianBlur

class TestGaussianBlur(unittest.TestCase):

    def test_on_pil_image(self):
        for w in range(1, 100):
            for h in range(1, 100):
                gaussian_blur = GaussianBlur(w * 0.1)
                sample = Image.new('RGB', (w, h))
                gaussian_blur(sample)


import os

import mongoengine

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

SECRET_KEY = '6c8+rfm8e6#bt&13$+4#(btshtf7#^)y3e@58=lzrxd5j4%q(m'

DEBUG = True

ALLOWED_HOSTS = ['*']

REST_FRAMEWORK = {
    'UNAUTHENTICATED_USER': None
}

# Application definition

INSTALLED_APPS = [
    'rest_framework',
    'django.contrib.staticfiles',
    'corsheaders',
    'django_pds',
    'api'
]

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django_pds.middlewares.UrlPathExistsMiddleware',
    'django.middleware.common.CommonMiddleware'
]

CORS_ORIGIN_ALLOW_ALL = True

CORS_ALLOW_METHODS = (
    'DELETE',
    'GET',
    'OPTIONS',
    'PATCH',
    'POST',
    'PUT',
)

ROOT_URLCONF = 'mongo_django.urls'

TEMPLATES = []

WSGI_APPLICATION = 'mongo_django.wsgi.application'

DATABASES = {}

# connecting to mongodb

DATABASE_NAME = '89595B8A-13EE-43F3-9D06-0D34D71D161B'

mongoengine.connect(DATABASE_NAME)

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/

STATIC_URL = '/static/'


# importacao de bibliotecas
import requests
import csv
# import datetime (NAO SERA UTILIZADO E NAO FUNCIONOU NO PYCHARM)
from PIL import Image
from urllib.parse import quote
from time import sleep

# recebendo os dados da API
url = 'https://api.covid19api.com/dayone/country/brazil'
requisicao = requests.get(url)
dados_api = requisicao.json()

# criando uma lista de listas (final_data) com os dados recebidos da API, para ser usada em um arquivo CSV
dados_finais = []
for dicionario in dados_api:
    dados_finais.append([dicionario['Confirmed'], dicionario['Deaths'], dicionario['Recovered'],
                         dicionario['Active'], dicionario['Date'][:10]])
dados_finais.insert(0, ['Confirmados', 'Obitos', 'Recuperados', 'Ativos', 'Data'])

# nomeando as posicoes de cada lista
confirmados = 0
mortos = 1
recuperados = 2
ativos = 3
data = 4

# criando o arquivo CSV (final_data ja e uma lista de listas, facilitando esse processo) NAO SERA USADO PARA O GRAFICO
with open('brasil-covid.csv', 'w', newline='') as arquivo:
    escritor = csv.writer(arquivo)
    escritor.writerows(dados_finais)

# transformando a string da data em um formato de data real (NAO FUNCIONOU NO PYCHARM, MAS NAO IREI USAR)
'''for dados_diarios in range(1, len(dados_finais)):
    dados_finais[dados_diarios][data] = datetime.datetime.strptime(final_data[dados_diarios][data], '%Y-%m-%d')'''

# criando funcao para gerar datasets para a API geradora de graficos chamada quickchart
def gerar_dados(lista_dados, etiqueta):
    if len(lista_dados) > 1:
        conjunto_dados = []
        for contador in range(len(lista_dados)):
            conjunto_dados.append({
                'label': etiqueta[contador],
                'data': lista_dados[contador]
            })
        return conjunto_dados
    else:
        return [{
            'label': etiqueta,
            'data': lista_dados
        }]

# criando funcao para colocar titulo no grafico
def titulo_grafico(titulo=''):
    if titulo != '':
        mostrar = 'true'
    else:
        mostrar = 'false'
    return {
        'display': mostrar,
        'title': titulo
    }

# criando funcao para gerar o codigo(dicionario) do grafico
def gerar_grafico(etiquetasx, lista_dados, etiquetasy, tipo='bar', titulo=''):
    conjunto_dados = gerar_dados(lista_dados, etiquetasy)
    opcoes = titulo_grafico(titulo)
    dadosdografico = {
        'type': tipo,
        'data': {
            'labels': etiquetasx,
            'datasets': conjunto_dados
        },
        'options': opcoes
    }
    return dadosdografico

# criando funcao para requisitar o grafico da API
def requrer_grafico(dadosdografico):
    url_base = 'https://quickchart.io/chart?c='
    req = requests.get(f'{url_base}{str(dadosdografico)}')
    conteudo = req.content
    return conteudo

# criando funcao para salvar a imagem do grafico
def salvar_imagem_grafico(caminho, conteudo_binario):
    with open(caminho, 'wb') as imagem_grafico:
        imagem_grafico.write(conteudo_binario)

# criando funcao para exibir a imagem do grafico na tela
def mostrar_grafico(caminho):
    imagem = Image.open(caminho)
    imagem.show()

# criando funcao para gerar qr code
def requerer_qrcode(url_texto):
    url_link = quote(url_texto)
    url_final = f'https://quickchart.io/qr?text={url_link}'
    req = requests.get(url_final)
    conteudo = req.content
    return conteudo

# PROGRAMA PRINCIPAL

# 1o. - criando dados para informar para as funcoes
try:
    while True:
        intervalo_dados = int(input('Com qual intervalo deseja gerar o grafico? [20 ou mais]\n'))
        if 20 <= intervalo_dados <= 400:
            break
        else:
            print('OPCAO INVALIDA!', end=' ')
except:
    sleep(1)
    print('ERRO - OPCAO INVALIDA!')
    sleep(1)
    print('INTERVALO PADRAO DE 50 DIAS ESCOLHIDO!')
    sleep(3)
finally:
    intervalo_dados = 50
etiquetas_x = []
for dados_diarios in dados_finais[1::intervalo_dados]:
    etiquetas_x.append(dados_diarios[data])
dados_confirmados = []
dados_recuperados = []
for dados_diarios in dados_finais[1::intervalo_dados]:
    dados_confirmados.append(dados_diarios[confirmados])
    dados_recuperados.append(dados_diarios[recuperados])
lista_de_dados_final = [dados_confirmados, dados_recuperados]
etiquetas_y = ['Confirmados', 'Recuperados']

# 2o. - chamandos as funcoes e exibindo o grafico
grafico = gerar_grafico(etiquetas_x, lista_de_dados_final, etiquetas_y, titulo='Confirmados x Recuperados')
conteudo_do_grafico = requrer_grafico(grafico)
salvar_imagem_grafico('meu-primeiro-grafico.png', conteudo_do_grafico)
mostrar_grafico('meu-primeiro-grafico.png')

# 3o. - criando um qr code do grafico
conteudo_qrcode = requerer_qrcode(f'https://quickchart.io/chart?c={grafico}')
salvar_imagem_grafico('qr_code_meu_primeiro_grafico.png', conteudo_qrcode)
mostrar_grafico('qr_code_meu_primeiro_grafico.png')


pkgname = "gtar"
pkgver = "1.35"
pkgrel = 0
build_style = "gnu_configure"
configure_args = [
    "--program-prefix=g",
    "gl_cv_struct_dirent_d_ino=yes",
]
configure_gen = []
hostmakedepends = ["texinfo"]
makedepends = ["acl-devel"]
pkgdesc = "GNU tape archiver"
maintainer = "q66 <q66@chimera-linux.org>"
license = "GPL-3.0-or-later"
url = "http://www.gnu.org/software/tar"
source = f"$(GNU_SITE)/tar/tar-{pkgver}.tar.xz"
sha256 = "4d62ff37342ec7aed748535323930c7cf94acf71c3591882b26a7ea50f3edc16"
hardening = ["vis", "cfi"]


#! /usr/bin/python3
# -*- coding: utf-8 -*-

import rospy
import subprocess
import threading
from tkinter import *
from geometry_msgs.msg import Twist
import subprocess
from std_msgs.msg import String

import threading

class UI:

    def __init__(self, master=None):

        self._enable = False

        self.__real_robot_bt = Button(master, text="Real Robot",bg='#49A', width = 10)
        self.__virtual_robot_bt = Button(master, text="Virtual Robot",bg="green", width = 10)

        #Botones
        self.__real_robot_bt.bind("<Button-1>", self.real_robot_launch)
        self.__virtual_robot_bt.bind("<Button-1>", self.virtual_robot_launch)
        self.__real_robot_bt.grid(row=0, column=0, columnspan=1)
        self.__virtual_robot_bt.grid(row=1, column=0, columnspan=1)

        #Etiquetas
        self.__label1 = Label(master,text="SELECCIONE EL TIPO DE ROBOT")
        self.__label1.grid(column=4, row=0, padx=10, pady=1)
        self.__label1.config(bg="white")

        self.__label2 = Label(master,text="",bg='white')
        self.__label2.grid(column=4, row=1, padx=10, pady=1)
        self.__label2.config(bg="white")

        #Imagen
        self.__image=PhotoImage(file="src/kinova_UI/image/kinovaPhoto.png")
        self.__imageLable=Label(master,image=self.__image)
        self.__imageLable.config(bg="white")
        self.__imageLable.place(x=100, y=100, width=200, height=80)

        self.__robot_selected = False

        # Lista de comandos
        self.__cmd = []

    def real_robot_launch(self, event):
        if not self.__robot_selected:
            self.__robot_selected = True

            subprocess.run(["gnome-terminal","--", "sh", "-c", "roslaunch controllers scullion.launch type:=real"])

            #Para que no de error de conexión
            rospy.sleep(3)

            #Se inicializa el nodo
            rospy.init_node("kinova_ui")

            # Suscriptor al nodo del control por voz
            rospy.Subscriber("/voice_ui", String, self.__cb)

            # Suscriptor al nodo de la cámara
            rospy.Subscriber("/ready", String, self.__cb)

            self.__label1.config(text="¿QUÉ INGREDIENTE DESEA?")

    def virtual_robot_launch(self, event):
        if not self.__robot_selected:
            self.__robot_selected = True
            #Abre terminal y ejecuta el comando
            subprocess.run(["gnome-terminal","--", "sh", "-c","roslaunch controllers scullion.launch"])

            #Para que no de error de conexión
            rospy.sleep(3)

            #Se inicializa el nodo
            rospy.init_node("kinova_ui")

            # Suscriptor al nodo del control por voz
            rospy.Subscriber("/voice_ui", String, self.__cb)

            # Suscriptor al nodo de la cámara
            rospy.Subscriber("/ready", String, self.__cb)

            self.__label1.config(text="¿QUÉ INGREDIENTE DESEA?")

    #Resetea las etiquetas
    def reset_label(self, kinova_frame):

        self.__label2.config(text="")

    # Bucle de control
    def control_loop(self,kinova_frame):

            # Bucle de control
            if not rospy.is_shutdown():

                # Comprueba que hay un comando a realizar
                if len(self.__cmd) > 0:

                    # Saca el comando de la lista de comandos
                    command = self.__cmd.pop(0)
                    command = command.lower().split()

                    #Mensaje para comunicar a la interfaz el ingrediente que se coge
                    mensaje = String()

                    # Realiza el pick and place si tiene que dar un ingrediente, luego pone la tupla a False (no hay ingrediente en la zona de almacén)
                    if len(command) == 1:

                        if command[0] == "sal":
                            self.__label2.config(text="Cogiendo la sal")
                            kinova_frame.after(15000, self.reset_label,kinova_frame)

                        elif command[0] == "azúcar":
                            self.__label2.config(text="Cogiendo el azúcar")
                            kinova_frame.after(15000, self.reset_label,kinova_frame)

                        elif command[0] == "pimienta":
                            self.__label2.config(text="Cogiendo la pimienta")
                            kinova_frame.after(15000, self.reset_label,kinova_frame)

                    elif len(command) > 1:

                        X = float(command[0])
                        Y = float(command[1])
                        # Realiza el pick and place si tiene que devolver un ingrediente, luego pone la tupla a True (hay ingrediente en la zona de almacén)
                        if command[2] == "0":
                            self.__label2.config(text="Devolviendo la sal")
                            kinova_frame.after(15000, self.reset_label,kinova_frame)

                        elif command[2] == "1":
                            self.__label2.config(text="Devolviendo la pimienta")
                            kinova_frame.after(15000, self.reset_label,kinova_frame)

                        elif command[2] == "2":
                            self.__label2.config(text="Devolviendo el azúcar")
                            kinova_frame.after(15000, self.reset_label,kinova_frame)

                kinova_frame.after(500, self.control_loop,kinova_frame)

    # Callback de la interfaz por voz y cámara: recoge los comandos y los almacena
    def __cb(self, data):
        self.__cmd.append(data.data)

def main():

    try:

        kinova_frame = Tk()
        kinova_frame.geometry("370x200")
        kinova_frame.title("Kinova UI")
        kinova_frame['bg']= 'white'
        kinova_frame.resizable(width=False, height=False)

        kinova_UI=UI(kinova_frame)

        kinova_UI.control_loop(kinova_frame)
        kinova_frame.mainloop()

    except rospy.ROSInterruptException:
        return
    except KeyboardInterrupt:
        return

if __name__ == '__main__':

    main()


# import sys
from collections import deque
# from io import StringIO
#
# test_input1 = """11, 6, 8, 1
# 3, 1, 9, 10, 5, 9, 1
# """
#
#
# test_input2 = """10, 9, 8, 7, 5
# 5, 10, 9, 8, 7
# """
#
# test_input3 = """12, -3, 14, 3, 2, 0
# 10, 15, 4, 6, 3, 1, 22, 1
# """
#
# sys.stdin = StringIO(test_input1)
# sys.stdin = StringIO(test_input2)
# sys.stdin = StringIO(test_input3)

orders = deque([int(x) for x in input().split(', ') if 0 < int(x) <= 10])
employees = [int(x) for x in input().split(', ')]
total_pizzas = 0

while orders and employees:

    current_order = orders[0]
    current_employee = employees[-1]

    if current_order <= current_employee:
        total_pizzas += current_order
        orders.popleft()
        employees.pop()
    if current_order > current_employee:
        total_pizzas += current_employee
        rest = current_order - current_employee
        employees.pop()
        orders[0] = rest

if not orders:
    print("All orders are successfully completed!")
    print(f"Total pizzas made: {total_pizzas}")
    print(f"Employees: {', '.join(map(str, employees))}")
elif not employees:
    print("Not all orders are completed.")
    print(f"Orders left: {', '.join(map(str, orders))}")


import pandas as pd
from scipy.stats import spearmanr, pearsonr, kendalltau
import os
import numpy as np

base_path = 'output_alignment/alignment_checkpoint/'
dfs = []
for file in os.listdir(base_path):
    dfs.append(pd.read_csv(os.path.join(base_path, file)))
df1 = pd.concat(dfs)

# df1 = pd.read_csv('output_alignment/alignmnets_y0_com_bal_v1.csv')

df1_beta_star = (df1['E1'] / df1['Ea_hat']) ** 0.5
df1['FBeta*'] = ((1 + df1_beta_star ** 2) * df1['Ea']) / (df1['E1'] + df1_beta_star ** 2 * df1['Ea_hat'])
measures = ['NC', 'EC', 'ICS', 'S3', 'F1', 'ACC', 'BM', 'MK', 'MCC', 'F0.1', 'F0.33', 'F3', 'F10', 'FBeta*', 'BMK2', 'Ilia']
df1['BMK2'] = (df1['BM'] + df1['MK']) / 2.0
df1['Ilia'] = (df1['TP'] / df1['E1'] + df1['TN'] / (df1['omega'] - df1['E1'])) / 2.0
df2 = pd.DataFrame(columns=measures)
static_measures = ['NC', 'S3', 'F1', 'ACC', 'MCC']
for measure in static_measures:
    df2[measure] = df1[measure]

dynamic_measures = [('EC', 'ICS'), ('BM', 'MK'), ('F0.1', 'F10'), ('F0.33', 'F3')]
for m1, m2 in dynamic_measures:
    df2[m1] = df1[m2]
    df2[m2] = df2[m1]

df2_beta_star = 1 / ((df1['E1'] / df1['Ea_hat']) ** 0.5)
df2['FBeta*'] = ((1 + df2_beta_star ** 2) * df1['Ea']) / (df1['E1'] + df2_beta_star ** 2 * df1['Ea_hat'])
df2['BMK2'] = (df2['BM'] + df2['MK']) / 2.0
df = pd.concat([df1[measures], df2[measures]])
print('number of rows: ' + str(len(df)))

rho, p_value = spearmanr(df[measures])
pd.DataFrame(rho, columns=measures, index=measures).to_csv('output_alignment/spearman_correlation_ba_er_ws.csv')

# rho = np.corrcoef(df[measures])

pc = pd.DataFrame(columns=measures, index=['NC'])
for measure in measures:
    pc[measure]['NC'] = pearsonr(df['NC'], df[measure])[0]
pc.to_csv('output_alignment/pearson_correlation_ba_er_ws.csv')

kt = pd.DataFrame(columns=measures, index=['NC'])
for measure in measures:
    kt[measure]['NC'] = kendalltau(df['NC'], df[measure])[0]
kt.to_csv('output_alignment/kendalltau_correlation_ba_er_ws.csv')