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()


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 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())


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 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/'


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"


import os
from ..auth.auth_handler import create_jwt
from postmarker.core import PostmarkClient
from ..models.attendee import Attendee
from fastapi import APIRouter, HTTPException, Depends, Request
from pyairtable.api.table import Table
from ..dependencies import get_registration_table
from email_validator import validate_email, EmailNotValidError
from datetime import timedelta
import requests
import bisect
from sentry_sdk import capture_message
from dotenv import load_dotenv

load_dotenv()

POSTMARK_SERVER_TOKEN = os.getenv("POSTMARK_SERVER_TOKEN")
BLACKLIST_PATH = os.getenv("BLACKLIST_PATH")
RECAPTCHA_SECRET_KEY = os.getenv("RECAPTCHA_SECRET_KEY")

VERIFY_PATH = "https://register.losaltoshacks.com/verify"

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

postmark = PostmarkClient(server_token=POSTMARK_SERVER_TOKEN, verbosity=3)

class EmailDomainValidator:
    def __init__(self):
        path = os.path.join(
            os.path.dirname(__file__), "../disposable_email_blocklist.txt"
        )
        with open(path) as fin:
            self.sorted_blacklist = tuple(fin.read().splitlines())

    def validate(self, email: str):
        index = bisect.bisect_left(self.sorted_blacklist, email)
        # Returns false if the domain is found in the blacklist, and true if not
        return not (
            index < len(self.sorted_blacklist) and self.sorted_blacklist[index] == email
        )

domain_validator = EmailDomainValidator()

@router.post("/attendee")
async def add_attendee(
    attendee: Attendee, request: Request, table: Table = Depends(get_registration_table)
):
    # Check if the reCAPTCHA token is valid
    # recaptcha = requests.post(
    #     "https://www.google.com/recaptcha/api/siteverify",
    #     data={
    #         "secret": RECAPTCHA_SECRET_KEY,
    #         "response": attendee.token,
    #     },
    # ).json()

    # if not recaptcha["success"]:
    #     e = HTTPException(
    #         status_code=400, detail="reCAPTCHA Error: " + str(recaptcha["error-codes"])
    #     )
    #     capture_message("reCAPTCHA Error: " + str(recaptcha["error-codes"]))
    #     raise e

    # if recaptcha["action"] != "submit":
    #     e = HTTPException(
    #         status_code=400,
    #         detail="Invalid reCAPTCHA action for this route.",
    #     )
    #     capture_message(
    #         f'Invalid reCAPTCHA action for this route. Action was {recaptcha["action"]}'
    #     )
    #     raise e

    # if recaptcha["score"] < 0.5:
    #     e = HTTPException(
    #         status_code=400,
    #         detail=f'Failed reCAPTCHA (score was {recaptcha["score"]}). Are you a bot?',
    #     )
    #     capture_message(f'Failed reCAPTCHA (score was {recaptcha["score"]}).')
    #     raise e

    # Check emails are valid
    try:
        email = validate_email(attendee.email, check_deliverability=True)
        attendee.email = email.ascii_email
    except EmailNotValidError as e:
        raise HTTPException(
            status_code=400,
            detail=f"Your email address is not valid. {e}",
        )

    try:
        parent_email = validate_email(attendee.parent_email, check_deliverability=True)
        attendee.parent_email = parent_email.ascii_email
    except EmailNotValidError as e:
        raise HTTPException(
            status_code=400,
            detail=f"Your parent/legal guardian's email address is not valid. {e}",
        )

    if attendee.email == attendee.parent_email:
        raise HTTPException(
            status_code=400,
            detail="Attendee email and parent email cannot be the same.",
        )

    if not domain_validator.validate(
        email.ascii_domain
    ) or not domain_validator.validate(parent_email.ascii_domain):
        e = HTTPException(
            status_code=400,
            detail="Do not use temporary email addresses.",
        )
        raise e

    # Verify both attendee and parent emails
    res = table.create(attendee.getAirtableFields())
    attendee_id = res["id"]

    # Tokens expire in 6 weeks
    expire_delta = timedelta(weeks=6)
    student_token = create_jwt(
        {"id": attendee_id, "type": "student"}, expires_delta=expire_delta
    )
    parent_token = create_jwt(
        {"id": attendee_id, "type": "parent"}, expires_delta=expire_delta
    )

    postmark.emails.send_with_template(
        TemplateAlias="email-verification",
        TemplateModel={
            "name": attendee.first_name,
            "action_url": f"{VERIFY_PATH}?token={student_token}",
        },
        From="hello@losaltoshacks.com",
        To=attendee.email,
    )

    postmark.emails.send_with_template(
        TemplateAlias="email-verification",
        TemplateModel={
            "name": attendee.first_name + "'s parent/guardian",
            "action_url": f"{VERIFY_PATH}?token={parent_token}",
        },
        From="hello@losaltoshacks.com",
        To=attendee.parent_email,
    )

    return res


# 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))}")


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


# 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')


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 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')


from transformers import BertTokenizer, BertModel
import torch

class Bert:

    def __init__(self):
        self.model = BertModel.from_pretrained("bert-base-uncased", output_hidden_states = True)
        self.tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
        self.device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
        self.dimensions = 768

    def getEmbedding(self, data):
        with torch.no_grad():
            tab = self.tokenizer(
                    data,
                    padding=True,
                    truncation=True,
                    return_tensors="pt"
            ).to(self.device)

            self.model = self.model.to(self.device)
            output = self.model(**tab)
            return [i[0] for i in output.last_hidden_state]


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

import os
import time
import stat
import io
import ctypes
import struct
import zlib

# https://users.cs.jmu.edu/buchhofp/forensics/formats/pkzip.html
# https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
# https://en.wikipedia.org/wiki/Zip_(file_format)#File_headers

ZIP_VERSION = b'\x14'  # copied from output of the zipfile Python module
BLOCKSIZE = io.DEFAULT_BUFFER_SIZE # usually 8192
USE_DATA_DESCRIPTOR = True   # required to be true for compliance to the spec

LOCAL_FILE_HEADER_SIZE = 30 # local file header + data descriptor (local file footer)
if USE_DATA_DESCRIPTOR:
    LOCAL_FILE_HEADER_SIZE += 16
CENTRAL_DIRECTORY_FILE_HEADER_SIZE = 46
CENTRAL_DIRECTORY_FOOTER_SIZE = 22

FLAGS = (1 << 3 | 1 << 11) # flags: hide checksum (bit 3), UTF-8 names (bit 11)

# See: https://unix.stackexchange.com/a/14727/234161
# To quote, this is the layout used on Unix:
#     TTTTsstrwxrwxrwx0000000000ADVSHR
#     ^^^^____________________________ file type as explained above
#         ^^^_________________________ setuid, setgid, sticky
#            ^^^^^^^^^________________ permissions
#                     ^^^^^^^^________ This is the "lower-middle byte" your post mentions
#                             ^^^^^^^^ DOS attribute bits
EXTERNAL_ATTRIBUTES = (stat.S_IFREG | 0o664) << 16 # use a sane default

class ZipFileChanged(Exception):
    '''
    Raised when the stat() filesize isn't the same as the read filesize.
    '''
    pass

class ZipFile:
    def __init__(self, path, zipname, st, offset):
        self.path = path
        self.zipname = zipname
        self.st = st
        self.localHeaderOffset = offset

    def localSize(self):
        '''
        Calculate the size of the local header + data.
        Every file will take up this space plus the space in the central directory.
        '''
        return LOCAL_FILE_HEADER_SIZE + len(self.zipname) + self.st.st_size

    def centralDirectorySize(self):
        '''
        Return the size of the file entry in the central directory.
        '''
        return CENTRAL_DIRECTORY_FILE_HEADER_SIZE + len(self.zipname)

    def totalSize(self):
        '''
        Return all space this file takes in the zipfile.
        '''
        return self.localSize() + self.centralDirectorySize()

    def dos_time(self):
        '''
        Convert a timestamp to the weird DOS format the ZIP format uses - which
        uses a simple naive date/time encoding function instead of storing the
        GMT time in seconds like so many other formats do.
        '''
        ts = time.localtime(self.st.st_mtime)
        return (ts.tm_sec // 2) | (ts.tm_min << 5) | (ts.tm_hour << 11)

    def dos_date(self):
        '''
        See dos_time
        '''
        ts = time.localtime(self.st.st_mtime)
        return (ts.tm_mday) | (ts.tm_mon << 5) | ((ts.tm_year - 1980) << 9)

class ZipSeeker:
    def __init__(self):
        self.files = []

    def add(self, path, zipname=None):
        '''
        Add a file to this ZIP.
        Doesn't actually read the file, only stat()s it.
        You may need to provide another name for use within the ZIP.
        '''
        if zipname is None:
            zipname = path
        st = os.stat(path)
        offset = 0
        if len(self.files):
            offset = self.files[-1].localHeaderOffset + self.files[-1].localSize()
        self.files.append(ZipFile(path, zipname.encode('utf-8'), st, offset))

    def size(self):
        '''
        Calculate and return the zip file size before generating it.
        '''
        size = sum(map(lambda f: f.totalSize(), self.files))
        size += CENTRAL_DIRECTORY_FOOTER_SIZE
        return size

    def lastModified(self):
        '''
        Return the last last-modified time of all files in this ZIP file.
        '''
        return max(map(lambda f: f.st.st_mtime, self.files))

    def centralDirectorySize(self):
        '''
        Internal helper function.
        Calculate the length of the central directory (all entries in the
        central directory, excluding the end-of-central-directory entry).
        '''
        size = 0
        for file in self.files:
            size += CENTRAL_DIRECTORY_FILE_HEADER_SIZE + len(file.zipname) # central directory header
        return size

    def centralDirectoryStart(self):
        '''
        Internal helper function.
        Calculate the start index of the central directory.
        '''
        size = 0
        for file in self.files:
            size += file.st.st_size                            # (uncompressed) file itself
            size += LOCAL_FILE_HEADER_SIZE + len(file.zipname) # central directory header
        return size

    def blocks(self):
        for file in self.files:

            # local file header
            # length is 30 bytes (LOCAL_FILE_HEADER_SIZE)
            yield struct.pack('<IccHHHHIIIHH',
                0x04034b50,             # 4-byte signature ("PK\x03\x04")
                ZIP_VERSION, b'\x00',   # 2-byte PKZIP version
                FLAGS,                  # 2-byte flags
                0,                      # 2-byte compression (no compression)
                file.dos_time(),        # 2-byte modtime in MS-DOS format
                file.dos_date(),        # 2-byte moddate in MS-DOS format
                0,                      # 4-byte checksum - stored in data descriptor
                file.st.st_size,        # 4-byte compressed size
                file.st.st_size,        # 4-byte uncompressed size
                len(file.zipname),      # 2-byte filename length
                0)                      # 2-byte extra field length

            # Write the zip filename
            yield file.zipname

            # actual file data (without compression)
            checksum = 0
            size = 0
            fp = open(file.path, 'rb')
            buf = fp.read(BLOCKSIZE)
            while buf:
                size += len(buf)
                if size > file.st.st_size:
                    raise ZipFileChanged('file %s at least %d bytes too big' % (repr(file.zipname), size - file.st.st_size))
                checksum = zlib.crc32(buf, checksum) & 0xffffffff
                yield buf
                buf = fp.read(BLOCKSIZE)
            fp.close()
            if size != file.st.st_size:
                raise ZipFileChanged('file %s with size %d doesn\'t match st_size %d' % (repr(file.zipname), size, file.st.st_size))
            file.checksum = checksum

            # Data descriptor
            # Not strictly necessary, but doesn't add much overhead and might
            # help ZIP readers. It is strictly required by the standard (MUST)
            # but ZIP readers seem to work fine without it.
            # length is 16 bytes (see LOCAL_FILE_HEADER_SIZE)
            if USE_DATA_DESCRIPTOR:
                yield struct.pack('<IIII',
                    0x08074b50, # 4-byte signature: "PK\x07\x08"
                    checksum,   # 4-byte checksum
                    size,       # 4-byte compressed size
                    size)       # 4-byte uncompressed size

        # Write the central directory file headers
        # Length is 46 bytes + file name (CENTRAL_DIRECTORY_FILE_HEADER_SIZE)
        for file in self.files:
            yield struct.pack('<IccccHHHHIIIHHHHHII',
                0x02014b50,                 # 4-byte signature: "PK\x01\x02"
                ZIP_VERSION, b'\x03',       # 2-byte system and version, copied from Python zipfile output
                ZIP_VERSION, b'\x00',       # 2-byte PKZIP version needed
                FLAGS,                      # 2-byte flags
                0,                          # 2-byte compression (no compression)
                file.dos_time(),            # 2-byte last modified time (local time)
                file.dos_date(),            # 2-byte last modified date (local time)
                file.checksum,              # 4-byte CRC-32 checksum
                file.st.st_size,            # 4-byte compressed size (not read due to flag bit 3 being set)
                file.st.st_size,            # 4-byte uncompressed size (not read due to flag bit 3 being set)
                len(file.zipname),          # 2-byte filename length
                0,                          # 2-byte extra field length
                0,                          # 2-byte comment length (no comment)
                0,                          # 2-byte disk number (no split archives, always 0)
                0,                          # 2-byte internal attributes, TODO detect text files
                EXTERNAL_ATTRIBUTES,        # 4-byte external attributes
                file.localHeaderOffset)     # 4-byte offset (index) of local header

            # file name
            yield file.zipname

        # Write the end of central directory record

        # length is 22 bytes (CENTRAL_DIRECTORY_FOOTER_SIZE)
        yield struct.pack('<IHHHHIIH',
            0x06054b50,                     # 4-byte signature: "PK\x05\x06"
            0,                              # 2-byte disk number - always 0 (we don't split)
            0,                              # 2-byte disk with central directory - always 0 (we don't split)
            len(self.files),                # 2-byte number of central directory entries on this disk
            len(self.files),                # 2-byte total number of central directory entries
            self.centralDirectorySize(),    # 4-byte index of start of central directory
            self.centralDirectoryStart(),   # 4-byte size of central directory (without this footer)
            0)                              # 2-byte comment length - we don't use comments

    def blocksOffset(self, start=0, end=None):
        # TODO: optimize start=0 and end=None

        pos = 0
        for block in self.blocks():
            if pos+len(block) <= start:
                # stream hasn't started
                pos += len(block)
                continue
            if end is not None and pos >= end:
                # EOF
                break

            # start index in the block
            if pos >= start:
                startblock = 0
            else:
                startblock = start - pos

            # end index of the block
            if end is None or pos+len(block) <= end:
                endblock = len(block)
            else:
                endblock = end - pos

            if startblock == 0 and endblock == len(block):
                yield block
            else:
                yield block[startblock:endblock]

            pos += len(block)

    def writeStream(self, out, start=0, end=None):
        for block in self.blocksOffset(start, end):
            out.write(block)


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

import sys
import logging

from executions import Program

def main():
    program_name = sys.argv[2]
    logging.basicConfig(level=logging.DEBUG, format="%(asctime)s [%(levelname)s] %(message)s", filename="process-dtrace.log", filemode='w')
    logging.debug("Starting work with %s" % program_name)

    console = logging.StreamHandler()
    console.setLevel(logging.INFO)
    formatter = logging.Formatter("%(message)s")
    console.setFormatter(formatter)
    logging.getLogger('').addHandler(console)

    program = Program()
    program.debug = True
    for i in range(int(sys.argv[1])+1):
        program.read_program_points("%s.%d" % (program_name, i))

    #program.read_program_invariants("%s.invariants" % program_name)
    for i in range(int(sys.argv[1])+1):
        program.read_program_executions("%s.%d" % (program_name, i), True)

    for i in range(int(sys.argv[1])+1):
        program.read_program_executions("%s.%d" % (program_name, i))

    logging.debug("Finished work with %s" % program_name)

if __name__ == '__main__':
    main()


"""
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)


from django.core.management.base import BaseCommand

from mailer.models import Message

class Command(BaseCommand):
    help = 'Update server name in emails'

    def add_arguments(self, parser):
        # Positional arguments
        parser.add_argument('from_server', nargs='+', type=str)
        parser.add_argument('to_server', nargs='+', type=str)

    def handle(self, *args, **options):
        from_server = options.get('from_server')[0]
        to_server = options.get('to_server')[0]
        found = 0

        self.stdout.write(self.style.SUCCESS('Changing from "{}" to "{}"'.format(from_server, to_server)))

        for message in Message.objects.all():
            email = message.email

            if from_server in email.body:
                email.body = email.body.replace(from_server, to_server)
                message.email = email
                message.save(update_fields=['message_data'])
                found += 1

        self.stdout.write(self.style.SUCCESS('{} found'.format(found)))


def solution(bridge_length, weight, truck_weights):
    answer=1
    from collections import deque
    bridge=deque([0]*bridge_length)
    total=0
    for truck in range(len(truck_weights)):
        while True:
            removed=bridge[0]
            if total-removed+truck_weights[truck]>weight:
                bridge.popleft()
                bridge.append(0)
                total-=removed
                answer+=1
            else:
                break
        if truck==len(truck_weights)-1:
            answer+=bridge_length
            return answer
        total=total-bridge.popleft()+truck_weights[truck]
        bridge.append(truck_weights[truck])
        answer+=1

    return answer


from typing import Any, List

from common import TestResult
from proc import SubprocessAppController
from runner import Runner
from validation import TestExecutionValidator, validate_all
from web import TestGetUrl, TestPostToUrl

class NetworkMetricsCollectedValidator(TestExecutionValidator):

    def __init__(self, applicable_tests: List[str]):
        self.applicable_tests = applicable_tests

    def should_validate(self, name: str, scoped: bool) -> bool:
        return scoped and name in self.applicable_tests

    def validate(self, test_data: Any, scope_messages: List[str]) -> TestResult:
        return validate_all(
            (any("net.tx" in msg for msg in scope_messages if "#proc:nginx" in msg), "No 'net.tx' metrics is collected"),
            (any("net.rx" in msg for msg in scope_messages if "#proc:nginx" in msg), "No 'net.rx' metrics is collected")
        )

def configure(runner: Runner, config):
    app_controller = SubprocessAppController(["nginx", "-g", "daemon off;"], "nginx", config.scope_path,
                                             config.logs_path)
    get_home_page = TestGetUrl(url="http://localhost/", requests=10000, app_controller=app_controller)
    post_file = TestPostToUrl(url="http://localhost/log/", requests=10000, post_file="/opt/test-runner/post.json",
                              app_controller=app_controller)
    runner.add_test_execution_validators([NetworkMetricsCollectedValidator([get_home_page.name, post_file.name])])
    runner.add_tests([get_home_page, post_file])


from tkinter import *
from tkinter import filedialog
from PIL import ImageTk, Image
import os
from tkinter import messagebox

root = Tk()
root.minsize(650,650)
root.maxsize(650,650)
root.configure(background='gray')

open_img = ImageTk.PhotoImage(Image.open("open.png"))
save_img = ImageTk.PhotoImage(Image.open("save.png"))
exit_img = ImageTk.PhotoImage(Image.open("exit.jpg"))

label_file_name = Label(root, text="File name")
label_file_name.place(relx=0.28, rely=0.03, anchor=CENTER)

input_file_name = Entry(root)
input_file_name.place(relx=0.46, rely=0.03, anchor=CENTER)

my_text=Text(root,height=35,width=80)
my_text.place(relx=0.5,rely=0.55,anchor=CENTER)

#name = ""
#def openFile():
  #  global name

open_buttom=Button(root,image=open_img,text="OpenFile",commamd=openFile)
open_button.place(rex=0.5,rely=0.3,anchor=CENTER)
save_button=Button(root,image=open_img,text="SaveFile",commamd=openFile)
save_button.place(rex=0.11,rely=0.3,anchor=CENTER)
exit_buttom=Button(root,image=open_img,text="ExitFile",commamd=openFile)
exit_button.place(rex=0.17,rely=0.3,anchor=CENTER)

root.mainloop()


"""
@author: amal machtalay
"""

import torch
from torch import optim
from torchvision import models

def save_checkpoint(model, optimizer, name, epoch, image_datasets, path):
    model.class_to_idx = image_datasets['training'].class_to_idx
    torch.save({'name': name,
                'epoch': epoch,
                'class_to_idx': model.class_to_idx,
                'model_state_dict': model.state_dict(),
                'optimizer_state_dict': optimizer.state_dict(),
                'classifier': model.classifier,
                }, path)
    return 0

# function that loads a checkpoint and rebuilds the model
def rebuild_model(path):
    loaded_checkpoint = torch.load(path)
    loaded_name = loaded_checkpoint['name']
    loaded_epoch = loaded_checkpoint['epoch']
    ### Remember to first initialize the model and optimizer, then load the dictionary
    model = models.vgg16(pretrained=True)
    model.class_to_idx = loaded_checkpoint['class_to_idx']
    model.load_state_dict = loaded_checkpoint['model_state_dict']
    model.classifier = loaded_checkpoint['classifier']
    optimizer = optim.SGD(model.classifier.parameters(), lr=0.001)
    optimizer.load_state_dict(loaded_checkpoint['optimizer_state_dict'])

    return loaded_name, model, optimizer, loaded_epoch


'''
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


from argparse import ArgumentParser
from threading import Thread, current_thread
import logging
import time

from sloq import SlowQueue

def main(args=None):
    prog = ArgumentParser()
    prog.add_argument("-n", type=int, default=10, metavar="TASK_COUNT",
                      help="The number of tasks")
    prog.add_argument("-t", type=float, default=1, metavar="TASK_INTERVAL",
                      help="The tick: seconds between tasks being released")
    prog.add_argument("-w", type=int, default=3, metavar="WORKER_COUNT",
                      help="Number of workers")
    prog.add_argument("-d", type=float, default=0, metavar="TASK_DURATION",
                      help="Duration of a single task")
    prog.add_argument("-s", type=float, default=0, metavar="MAX_SLAM",
                      help="The maximum amount of slam to allow")
    args = prog.parse_args(args)

    logger = logging.getLogger(__name__)
    logger.setLevel(logging.INFO)
    logger.addHandler(logging.StreamHandler())

    test_queue(logger, args.t, args.n, args.d, args.w, args.s)

def test_queue(logger, tick=1, tasks=10, task_duration=0, worker_count=3,
               slam=0):
    start_time = time.time()
    sloq = SlowQueue(release_tick=tick, max_slam=slam)

    # Begin the workers
    for w in xrange(0, worker_count):
        Thread(target=test_worker, args=(logger, start_time, sloq)).start()

    # Populate the queue
    for task in xrange(0, tasks):
        sloq.put((task, task_duration))
    for w in xrange(0, worker_count):
        sloq.put((None, None))

    sloq.join()

def test_worker(logger, start_time, queue):
    while True:
        task, sleep = queue.get()
        if task is None:
            logger.info("%s, Done" % current_thread().name)
            queue.task_done()
            return
        else:
            logger.info("%s, Elapsed time: %0.2f, Task: %r",
                        current_thread().name, time.time() - start_time, task)
            if sleep:
                time.sleep(sleep)
            queue.task_done()

if __name__ == "__main__":
    main()


import configparser

from constants.Constants import Constants as const
from .OptimizerParamsFactory import OptimizerParamsFactory
from model.OptimizerFactory import OptimizerFactory

class ConfigParams(object):

    def __init__(self, file):

        config = configparser.ConfigParser()
        config.read_file(open(file))

        # Model
        self.architecture = config.get(const.ConfigSection.model, "architecture")
        # Valid only for mobilenet
        if self.architecture == "mobilenet":
            self.mobilenetAlpha = config.getfloat(const.ConfigSection.model, "mobilenetAlpha", fallback=1.0)
        self.inputSize = config.getint(const.ConfigSection.model, "inputSize", fallback=224)
        self.inputChannels = config.getint(const.ConfigSection.model, "inputChannels", fallback=3)
        self.preprocessType = config.get(const.ConfigSection.model, "preprocessType", fallback="dummy")

        # HyperParameters
        self.epochs = config.getint(const.ConfigSection.hyperparameters, "epochs")
        self.batchSize = config.getint(const.ConfigSection.hyperparameters, "batchSize")
        self.patience = config.getint(const.ConfigSection.hyperparameters, "patience")
        optimizerType = config.get(const.ConfigSection.hyperparameters, "optimizer")
        optimizerParams = OptimizerParamsFactory.createOptimizerParams(optimizerType, config)
        self.optimizer = OptimizerFactory.create(optimizerParams)


from RestrictedPython import compile_restricted_function, safe_builtins, limited_builtins, utility_builtins

someglobalvar = 123

myscript = """
import math
import tempfile
import io

#folgende befehle fuehren zu fehlern
#f = open("app.py", "rb")
#f = NamedTemporaryFile(delete=False)

def g(x):
	#return x + 1 + someglobalvar <--- kein Zugriff auf someglobalvar moeglich
	return h(x + 1)

result = math.exp(g(f(data)))
return result
"""

#globale variablen innerhalb der sandbox
safe_locals = {}
safe_globals = safe_builtins
additional_globals = {'data' : 2, 'f' : lambda x: x**2}
safe_globals.update(additional_globals)

#Kompilieren der Hauptfunktion
main_function_name = 'main'
main_function_compiled = compile_restricted_function(p = '', body = myscript, name = main_function_name, filename = '<inline code>')

#Kompilieren der Hilfsfunktion
support_function_name = 'h'
support_function_parameters = 'x'
support_function_body = 'return -x'
support_function_compiled = compile_restricted_function(p = support_function_parameters, body = support_function_body, name = support_function_name, filename = '<inline code>')

#Erstellen des Funktionszeigers der Hilfsfunktion
exec(support_function_compiled.code, safe_globals, safe_locals)
support_function_compiled_pointer = safe_locals[support_function_name]

print((support_function_compiled_pointer(123))) #Test der Hilfsfunktion

#Hinzufuegen der Hilfsfunktion zu den globalen Variablen der Sandbox, damit diese genutzt werden kann
updated_globals = {support_function_name : support_function_compiled_pointer}
safe_globals.update(updated_globals)

#Erzeugen des Funktionszeigers der Hauptfunktion
exec(main_function_compiled.code, safe_globals, safe_locals)
main_compiled_pointer = safe_locals[main_function_name]

print(main_compiled_pointer(*[], **{})) #Test der Hauptfunktion

#update der globalen variable 'data'
updated_globals = {'data' : 3}
safe_globals.update(updated_globals)

#update von 'h'
support_function_compiled = compile_restricted_function(p = support_function_parameters, body = 'return +x', name = support_function_name, filename = '<inline code>')
exec(support_function_compiled.code, safe_globals, safe_locals)
support_function_compiled_pointer = safe_locals[support_function_name]
updated_globals = {support_function_name : support_function_compiled_pointer}
safe_globals.update(updated_globals)

#erneute Kompilierung
import types
main_compiled_update_pointer = types.FunctionType(
	main_compiled_pointer.__code__,
	safe_globals,
	'<' + main_function_name + '>',
	main_compiled_pointer.__defaults__ or ())

print(main_compiled_update_pointer(*[], **{})) #Test der Hauptfunktion


import cv2
import base64
import json
import time
import os
import numpy

class Recognition:
    """
    This class contain everything required to detect, learn or recognize a face
    """
    db_path = "database/"
    name_list_path = "database/"

    def __init__(self, haar_cascade_file_path):
        self.haar_cascade_file_path = haar_cascade_file_path

    def take_pictures(self, number_of_pics):
        (images, labels) = ([], [])
        (width, height) = (130, 100)
        face_cascade = cv2.CascadeClassifier(self.haar_cascade_file_path)
        webcam = cv2.VideoCapture(0)
        count = 1
        while count < number_of_pics + 1:
            ret_val, im = webcam.read()
            time.sleep(1)
            if ret_val:
                gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
                faces = face_cascade.detectMultiScale(gray, 1.3, 4)
                for (x, y, w, h) in faces:
                    cv2.rectangle(im, (x, y), (x + w, y + h), (0, 255, 0), 2)
                    face = gray[y:y + h, x:x + w]
                    face_resize = cv2.resize(face, (width, height))
                    images.append(face_resize)
                    labels.append(count)
                count += 1
        webcam.release()
        cv2.destroyAllWindows()
        return images, labels

    @staticmethod
    def create_recognize_msg(db_name, images):
        retval, image = cv2.imencode('.png', images[0])
        json_string = {'data': {'type': "recognize", 'db_name': db_name, 'image0': base64.b64encode(image)}}
        return json.dumps(json_string)

    @staticmethod
    def create_learn_msg(db_name, person_name, info, images):
        json_string = {'data': {'type': "learn", 'db_name': db_name, 'person_name': person_name, 'info': info,
                                'number_of_images': len(images)}}
        i = 0
        for image in images:
            retval, im = cv2.imencode('.png', image)
            json_string['data']['image' + str(i)] = base64.b64encode(im)
            i += 1

        return json.dumps(json_string)

    def get_image_name(self, label, db_name):
        names_list = self.image_name_list(self.db_path)
        if db_name in names_list:
            fh = open(self.name_list_path + db_name + ".txt", "r")
            list_items = fh.readlines()
            if label >= 0:
                for item in list_items:
                    if int(item.split(":")[:-1][0]) == label:
                        return item.split(":")[1]
            else:
                return list_items[-1].split(":")[:-1][0]
        else:
            return 0

    def set_image_name(self, person_name, db_name, info):
        last_id = self.get_image_name(-1, db_name)
        fh = open(self.name_list_path + db_name + ".txt", "a")
        fh.write(str(int(last_id) + 1) + ":" + person_name + ":" + info)
        fh.write("\n")
        fh.close()
        return int(last_id) + 1

    def get_image_info(self, label, db_name):
        names_list = self.image_name_list(self.db_path)
        if db_name in names_list:
            fh = open(self.name_list_path + db_name + ".txt", "r")
            list_items = fh.readlines()
            if label >= 0:
                for item in list_items:
                    if int(item.split(":")[:-1][0]) == label:
                        return item.split(":")[2]
        else:
            return 0

    @staticmethod
    def db_list(db_path):
        names = []
        for filename in os.listdir(db_path):
            names.append(filename.split(".")[:-1][0])
        return names

    @staticmethod
    def image_name_list(path):
        names = []
        for filename in os.listdir(path):
            names.append(filename.split(".")[:-1][0])
        return names

    def learn_person(self, db_name, person_name, info, images):
        dbs = self.db_list('database/')
        label_list = [self.set_image_name(person_name, db_name, info), self.set_image_name(person_name, db_name, info)]
        (image, label) = [numpy.array(lists) for lists in [images, label_list]]
        if db_name in dbs:
            model = cv2.face.LBPHFaceRecognizer_create()  # 125 #110
            model.read(self.db_path + db_name + ".xml")
            model.update(image, label)
            model.write(self.db_path + db_name + ".xml")
        else:
            model = cv2.face.LBPHFaceRecognizer_create()  # 125 #110
            model.train(image, label)
            model.write(self.db_path + db_name + ".xml")

    def recognize_person(self, db_name, images):
        dbs = self. db_list('database/')
        if db_name in dbs:
            model = cv2.face.LBPHFaceRecognizer_create()  # 125 #110
            model.read(self.db_path + db_name + ".xml")
            for faces in images:
                prediction = model.predict(faces)
                if prediction[1] < 125:
                    rec = self.get_image_name(prediction[0], db_name)
                    info = self.get_image_info(prediction[0], db_name)
                    return rec, info
                else:
                    return "Unknown"
        else:
            return None


import babel
import babel.numbers
import babel.plural
from typing import Any, Callable, Dict, List, TYPE_CHECKING, Tuple, Union, cast
from typing_extensions import Literal

from fluent.syntax import ast as FTL

from .builtins import BUILTINS
from .prepare import Compiler
from .resolver import CurrentEnvironment, Message, Pattern, ResolverEnvironment
from .utils import native_to_fluent

if TYPE_CHECKING:
    from .types import FluentNone, FluentType

PluralCategory = Literal['zero', 'one', 'two', 'few', 'many', 'other']

class FluentBundle:
    """
    Bundles are single-language stores of translations.  They are
    aggregate parsed Fluent resources in the Fluent syntax and can
    format translation units (entities) to strings.

    Always use `FluentBundle.get_message` to retrieve translation units from
    a bundle. Generate the localized string by using `format_pattern` on
    `message.value` or `message.attributes['attr']`.
    Translations can contain references to other entities or
    external arguments, conditional logic in form of select expressions, traits
    which describe their grammatical features, and can use Fluent builtins.
    See the documentation of the Fluent syntax for more information.
    """

    def __init__(self,
                 locales: List[str],
                 functions: Union[Dict[str, Callable[[Any], 'FluentType']], None] = None,
                 use_isolating: bool = True):
        self.locales = locales
        self._functions = {**BUILTINS, **(functions or {})}
        self.use_isolating = use_isolating
        self._messages: Dict[str, Union[FTL.Message, FTL.Term]] = {}
        self._terms: Dict[str, Union[FTL.Message, FTL.Term]] = {}
        self._compiled: Dict[str, Message] = {}
        # The compiler is not typed, and this cast is only valid for the public API
        self._compiler = cast(Callable[[Union[FTL.Message, FTL.Term]], Message], Compiler())
        self._babel_locale = self._get_babel_locale()
        self._plural_form = cast(Callable[[Any], Callable[[Union[int, float]], PluralCategory]],
                                 babel.plural.to_python)(self._babel_locale.plural_form)

    def add_resource(self, resource: FTL.Resource, allow_overrides: bool = False) -> None:
        # TODO - warn/error about duplicates
        for item in resource.body:
            if not isinstance(item, (FTL.Message, FTL.Term)):
                continue
            map_ = self._messages if isinstance(item, FTL.Message) else self._terms
            full_id = item.id.name
            if full_id not in map_ or allow_overrides:
                map_[full_id] = item

    def has_message(self, message_id: str) -> bool:
        return message_id in self._messages

    def get_message(self, message_id: str) -> Message:
        return self._lookup(message_id)

    def _lookup(self, entry_id: str, term: bool = False) -> Message:
        if term:
            compiled_id = '-' + entry_id
        else:
            compiled_id = entry_id
        try:
            return self._compiled[compiled_id]
        except LookupError:
            pass
        entry = self._terms[entry_id] if term else self._messages[entry_id]
        self._compiled[compiled_id] = self._compiler(entry)
        return self._compiled[compiled_id]

    def format_pattern(self,
                       pattern: Pattern,
                       args: Union[Dict[str, Any], None] = None
                       ) -> Tuple[Union[str, 'FluentNone'], List[Exception]]:
        if args is not None:
            fluent_args = {
                argname: native_to_fluent(argvalue)
                for argname, argvalue in args.items()
            }
        else:
            fluent_args = {}

        errors: List[Exception] = []
        env = ResolverEnvironment(context=self,
                                  current=CurrentEnvironment(args=fluent_args),
                                  errors=errors)
        try:
            result = pattern(env)
        except ValueError as e:
            errors.append(e)
            result = '{???}'
        return (result, errors)

    def _get_babel_locale(self) -> babel.Locale:
        for lc in self.locales:
            try:
                return babel.Locale.parse(lc.replace('-', '_'))
            except babel.UnknownLocaleError:
                continue
        # TODO - log error
        return babel.Locale.default()


class Standalone(object):
    """Common class for all standalone applications"""
    def __init__(self, args, user_options):

        # stores the arguments
        self.args = args
        self.user_options = user_options

        if len(args) == 1:
            # shows the help message if no arguments provided
            self.help()
        else:
            # The user values should be used to update the
            # user_options
            options = self.user_options.parse_args(args[1:])
            # Should update the CNOConfig file with the provided options
            for key in self.user_options.config.keys():
                for option in self.user_options.config[key]._get_names():
                    value = getattr(options, option)
                    setattr(getattr( getattr(self.user_options.config, key), option ), 'value', value)
            self.options = options

    def help(self):
        self.user_options.parse_args(["prog", "--help"])

    def report(self):
        """Create report and shows report (or not)"""
        if self.options.onweb is True:
            self.trainer.report(show=True)
        elif self.options.report is True:
            self.trainer.report(show=False)
        else:
            from easydev.console import red
            print(red("No report requested; nothing will be saved or shown"))
            print("use --on-web or --report options")


import sys
import pygame
import random

pygame.init()
pygame.display.set_caption("who is the caption")
window = pygame.display.set_mode((500,500))
font = pygame.font.Font("思源黑体.otf",35)

wheel_pics = []
for i in range(25):
    filename = './pics/' + str(i) + '.png'
    pic = pygame.image.load(filename)
    wheel_pics.append(pic)
start_pic = pygame.image.load('start.png')
window.fill((255,255,255))
window.blit(start_pic,(0,0))
pygame.display.flip()

with open('names.txt',encoding = 'utf8') as f:
    name_list = []
    for i in range(6):
        name_list.append(f.readline().strip())

choice = random.choice(name_list)

print(choice)

rolling = False
pic_index = 0
clock = pygame.time.Clock()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                rolling = True
    if rolling:
        window.fill((255,255,255))
        window.blit(wheel_pics[pic_index % 25],(0,0))
        pic_index += 1
        if pic_index >= 25:
            rolling = False
            pic_index = 0
            choice = random.choice(name_list)
            text = font.render(choice, True, (255,255,255))
            window.blit(text,(215,220))

    pygame.display.flip()
    clock.tick(30)

Game Handling/Chosing the name/新的作品-55-1.py


import os
import sys
import numpy as np
import PIL
import torch
import torch.utils.data
import torchvision
import matplotlib.pyplot as plt
import pickle

class AkMinitImagenetDataset(torch.utils.data.Dataset):
    def __init__(self, rootdir_dataset, str_trainortestorinducing):
        #grab args ===
        self.rootdir_dataset = rootdir_dataset
        self.str_trainortestorinducing = str_trainortestorinducing
        #make internals ==
        assert(isinstance(str_trainortestorinducing, str))
        assert(self.str_trainortestorinducing in [
            "train", "test", "inducing"
        ])
        fname_train = "MiniImagenet/miniImageNet_category_split_train_phase_train.pickle"
        fname_test = "MiniImagenet/miniImageNet_category_split_train_phase_test.pickle"
        #"MiniImagenet/miniImageNet_category_split_test.pickle"

        fname_pkl = fname_test if(str_trainortestorinducing == "test") else fname_train
        with open(os.path.join(self.rootdir_dataset, fname_pkl), 'rb') as f:
            content_pkl = pickle.load(f, encoding='latin1')
            self.X = content_pkl['data'] #[N x 84 x 84 x 3]
            self.Y = content_pkl['labels']

        #make the transforms ===
        tfm_colornormalization = torchvision.transforms.Normalize(
            (0.4914, 0.4822, 0.4465),
            (0.2023, 0.1994, 0.2010)
        )
        if(self.str_trainortestorinducing == "train"):
            self.tfm = torchvision.transforms.Compose([
                torchvision.transforms.ToPILImage(),
                torchvision.transforms.ToTensor(),
                torchvision.transforms.RandomHorizontalFlip(p=0.5),
                tfm_colornormalization
            ])
        elif(self.str_trainortestorinducing == "inducing"):
            self.tfm = torchvision.transforms.Compose([
                torchvision.transforms.ToPILImage(),
                torchvision.transforms.ToTensor(),
                tfm_colornormalization
            ])
        elif(self.str_trainortestorinducing == "test"):
            self.tfm = torchvision.transforms.Compose([
                torchvision.transforms.ToPILImage(),
                torchvision.transforms.ToTensor(),
                tfm_colornormalization
            ])
        else:
            raise Exception("Unknown str_trainortestorinducing: {}".format(
                self.str_trainortestorinducing
            ))
    def __len__(self):
        return self.X.shape[0]

    def __getitem__(self, n):
        xn = self.X[n,:,:,:] #[84x84x3]
        yn = self.Y[n]
        return self.tfm(xn), yn, n