#!/usr/bin/env python3

#This script is contributed by luckfamousa, see https://github.com/JsBergbau/MiTemperature2/issues/82
#Thank you very much for your support

import sys, json
import hashlib
from datetime import datetime
import requests

# install dependency `pip install fhir.resources`
# in fact you do not really need this because we just use it for formal validation of our FHIR resources
# which should happen on your FHIR server anyways
from fhir.resources.observation import Observation

# you need to fill in your specific configuration here
fhirServer = "https://myfhirserver.io/R4"
token = "you still need to implement the specific authentication and authorization mechanism of your specific FHIR server"

# this will let the FHIR server autogenerate a logical ID for your resource
def postFHIR(obj):
  headers = {
    "Authorization": "Bearer {}".format(token),
    "Content-Type": "application/fhir+json"
  }
  return requests.post("{}/{}".format(fhirServer, type(obj).__name__), data=obj.json(), headers=headers)

# this tells the FHIR server to accept the logical ID you specified.
def putFHIR(obj):
  headers = {
    "Authorization": "Bearer {}".format(token),
    "Content-Type": "application/fhir+json"
  }
  return requests.put("{}/{}/{}".format(fhirServer, type(obj).__name__, obj.id), data=obj.json(), headers=headers)

# modify if you have configured different parameters
device = sys.argv[2]
temperature = float(sys.argv[3])
humidity = int(sys.argv[4])
battery = float(sys.argv[5])
timestamp = int(sys.argv[6])

# encode measurements in a FHIR Observation resource
# see: https://www.hl7.org/fhir/observation.html
obsj = {
  "resourceType": "Observation",
  "code": {
    "coding": [{
      "system": "https://github.com/pvvx/ATC_MiThermometer",
      "code": "LYWSD03MMC"
  }]},
  "status": "final",
  "effectiveDateTime": datetime.utcfromtimestamp(timestamp).strftime('%Y-%m-%dT%H:%M:%SZ'),
  "component": [
    {
      "code": {
        "coding": [{
          "system": "http://loinc.org",
          "code": "60832-3",
          "display": "Room temperature"
      }]},
      "valueQuantity": {
        "value": temperature,
        "unit": "Celsius",
        "system": "http://unitsofmeasure.org",
        "code": "Cel"
      }
    },
    {
      "code": {
        "coding": [{
          "system": "http://loinc.org",
          "code": "65643-9",
          "display": "Room humidity"
      }]},
      "valueQuantity": {
        "value": humidity,
        "unit": "%",
        "system": "http://unitsofmeasure.org",
        "code": "%"
      }
    },
    {
      "code": {
        "coding": [{
          "system": "http://loinc.org",
          "code": "LP6802-5",
          "display": "Sensor battery"
      }]},
      "valueQuantity": {
        "value": battery,
        "unit": "Volt",
        "system": "http://unitsofmeasure.org",
        "code": "V"
      }
    }
  ],
  # This line assumes that you have previously created the sensor as a device in the FHIR server
  # and calculated its logical ID as a hash from the MAC address, as shown here.
  # You could do that for example by `putFHIR(createDevice(device))`
  "device": {
    "reference": "Device/{}".format(hashlib.sha256(device.encode("utf-8")).hexdigest())
  }
}

def createDevice(mac):
  devj = {
    "resourceType": "Device",
    "id": hashlib.sha256(mac.encode("utf-8")).hexdigest(),
    "identifier": [{
      "system": "https://github.com/pvvx/ATC_MiThermometer",
      "value": mac
    }],
    "status": "active",
    "deviceName": [{
      "name": "Xiaomi Mijia Smart Bluetooth Thermometer & Hygrometer measurement",
      "type": "user-friendly-name"
    },{
      "name": "LYWSD03MMC",
      "type": "model-name"
    }],
    "type": {
      "coding": [{
        "system": "http://snomed.info/sct",
        "code": "27991004"
      },{
        "system": "http://snomed.info/sct",
        "code": "470090000"
      }
    ]}
  }
  return Device.parse_obj(devj)

# send to FHIR server and print verbose output for debugging
response = postFHIR(Observation.parse_obj(obsj))
print(response.__dict__)


from typing import List

class Solution:
    def numUniqueEmails(self, emails: List[str]) -> int:
        s = set()

        for email in emails:
            s.add(self.parseEmail(email))
        return len(s)

    def parseEmail(self, s: str) -> str:
        res = []
        local = s.split("@")
        for i in local[0]:
            if i == "+":
                break
            elif i != ".":
                res.append(i)

        return "".join(res) + "@" + local[1]


class Solution:
    def sumEvenAfterQueries(self, A: 'List[int]', queries: 'List[List[int]]') -> 'List[int]':
        result, evenSum = [], sum([num for num in A if num % 2 == 0])

        for q in queries:
            if A[q[1]] % 2 == 0:
                evenSum -= A[q[1]]
            A[q[1]] += q[0]
            if A[q[1]] % 2 == 0:
                evenSum += A[q[1]]
            result.append(evenSum)

        return result


#!/usr/bin/env python3
from __future__ import print_function
import requests
import json
import time
import sys
import base64
import jsonpickle
import random

def doRawImage(addr, debug=False):
    # prepare headers for http request
    headers = {'content-type': 'image/png'}
    img = open('Flatirons_Winter_Sunrise_edit_2.jpg', 'rb').read()
    # send http request with image and receive response
    image_url = addr + '/api/rawimage'
    response = requests.post(image_url, data=img, headers=headers)
    if debug:
        # decode response
        print("Response is", response)
        print(json.loads(response.text))

def doAdd(addr, debug=False):
    headers = {'content-type': 'application/json'}
    # send http request with numbers and receive response
    add_url = addr + "/api/add/5/10"
    response = requests.post(add_url, headers=headers)
    if debug:
        # decode response
        print("Response is", response)
        print(json.loads(response.text))

def generateVector(n):
    vector = []
    for i in range(n):
        vector.append(random.random())
    return vector

def doDotProduct(addr, debug=False):
    headers = {'content-type': 'application/json'}
    v1 = generateVector(100)
    v2 = generateVector(100)
    dot_url = addr + "/api/dotproduct"
    data = {"a": v1, "b": v2}
    response = requests.post(dot_url, data=jsonpickle.encode(data), headers=headers)
    if debug:
        # decode response
        print("Response is", response)
        print(json.loads(response.text))

def doJsonImage(addr, debug=False):
    # prepare headers for http request
    headers = {'content-type': 'application/json'}
    img = open('Flatirons_Winter_Sunrise_edit_2.jpg', 'rb').read()
    # send http request with image and receive response
    image_url = addr + '/api/jsonimage'
    img_encoded = base64.b64encode(img)
    # img_encoded = io.BytesIO()
    # base64.encode(img, img_encoded)
    request = {"image": img_encoded}
    response = requests.post(image_url, data=jsonpickle.encode(request), headers=headers)
    if debug:
        # decode response
        print("Response is", response)
        print(json.loads(response.text))

if len(sys.argv) < 3:
    print(f"Usage: {sys.argv[0]} <server ip> <cmd> <reps>")
    print(f"where <cmd> is one of add, rawImage, sum or jsonImage")
    print(f"and <reps> is the integer number of repititions for measurement")

host = sys.argv[1]
cmd = sys.argv[2]
reps = int(sys.argv[3])

addr = f"http://{host}:5000"
print(f"Running {reps} reps against {addr}")

if cmd == 'rawImage':
    start = time.perf_counter()
    for x in range(reps):
        doRawImage(addr)
    delta = ((time.perf_counter() - start)/reps)*1000
    print("Took", delta, "ms per operation")
elif cmd == 'add':
    start = time.perf_counter()
    for x in range(reps):
        doAdd(addr)
    delta = ((time.perf_counter() - start)/reps)*1000
    print("Took", delta, "ms per operation")
elif cmd == 'jsonImage':
    start = time.perf_counter()
    for x in range(reps):
        doJsonImage(addr, debug=False)
    delta = ((time.perf_counter() - start)/reps)*1000
    print("Took", delta, "ms per operation")
elif cmd == 'dotProduct':
    start = time.perf_counter()
    for x in range(reps):
        doDotProduct(addr, debug=False)
    delta = ((time.perf_counter() - start)/reps)*1000
    print("Took", delta, "ms per operation")
else:
    print("Unknown option", cmd)


from flask.views import MethodView
from flask import request, jsonify
from db.cloudant.cloudant_manager import CloudantManager
from validators.appointment_val import AppointmentVal

cm = CloudantManager()
appointment_schema = AppointmentVal()

class Appointment(MethodView):
    def get(self):
        try:
            id_u = request.args.get('idu')
            print(id_u)
            cm.connect_service()
            my_db = cm.connect_db('health-db')
            if my_db == 'error':
                raise Exception
            user_docs = cm.get_query_by(my_db, id_u, 'id_p')
            print(user_docs)
            list_appointments = []
            for result in user_docs:
                try:
                    appointment_id = result['doc']['id_a']
                    id_u = result['doc']['id_p']
                    new_appointment = {
                        'id_a': appointment_id,
                        'id_d': result['doc']['id_d'],
                        'id_p': id_u,
                        'date_a': result['doc']['date_a'],
                        'reason': result['doc']['reason']
                    }
                    print(new_appointment)
                    list_appointments.append(new_appointment)
                except:
                    pass
            return jsonify({'appointments': list_appointments}), 200
        except:
            return jsonify({'st': "error"}), 403

    def post(self):
        try:
            appointment = request.get_json()
            errors = appointment_schema.validate(appointment)
            if errors:
                return jsonify({'st': errors}), 403
            conn = cm.connect_service()
            my_db = cm.connect_db('health-db')
            if my_db == 'error':
                raise Exception
            doc_msg = cm.add_doc(my_db, appointment)
            if doc_msg == "ok":
                return jsonify({'st': 'ok'}), 200
            elif doc_msg == "error":
                return jsonify({'st': 'error'}), 403
            else:
                return jsonify({'st': 'nothing'}), 403
        except:
            return jsonify({'st': "bad"}), 403


import os
import time
import argparse
import torch
from tqdm import tqdm
from utils import *
from distributed_model import *

# Function to parse arguments
def get_args():
    parser = argparse.ArgumentParser('AL training')

    # model arguments
    parser.add_argument('--emb-dim', type=int,
                        help='word embedding dimension', default=300)
    parser.add_argument('--label-emb', type=int,
                        help='label embedding dimension', default=128)
    parser.add_argument('--l1-dim', type=int,
                        help='lstm1 hidden dimension', default=300)
    parser.add_argument('--vocab-size', type=int, help='vocab-size', default=30000)
    parser.add_argument('--max-len', type=int, help='max input length', default=128)
    parser.add_argument('--dataset', type=str, default='ag_news', choices=['ag_news', 'dbpedia_14', 'banking77', 'emotion', 'rotten_tomatoes','imdb', 'clinc_oos', 'yelp_review_full', 'sst2',
                                                                           'paint','ailerons',"criteo","ca_housing","kdd99"])
    parser.add_argument('--word-vec', type=str, default='glove')

    # training arguments
    parser.add_argument('--lr', type=float, help='lr', default=0.001)
    parser.add_argument('--batch-train', type=int, help='batch-size', default=128)
    parser.add_argument('--batch-test', type=int, help='batch-size', default=1024)
    parser.add_argument('--one-hot-label', type=bool,
                        help='if true then use one-hot vector as label input, else integer', default=True)
    parser.add_argument('--epoch', type=int, default=20)

    # dir arguments
    parser.add_argument('--save-dir', type=str, default='./ckpt/')
    parser.add_argument('--out-dir', type=str, default='./result/')

    # YLP: multi-layer arguments
    parser.add_argument('--load-dir', type=str, default=None)
    parser.add_argument('--num-layer', type=int, default=3)
    parser.add_argument('--model', type=str, default='lstmal')
    parser.add_argument('--task', type=str, default="text")
    parser.add_argument('--feature-dim', type=int, default=0)
    parser.add_argument('--lr-schedule', type=str, default="plateau")
    parser.add_argument('--train-mask', type=int, default=None)
    parser.add_argument('--prefix-mask', action='store_true')
    parser.add_argument('--side-dim', type=str, default=None)
    parser.add_argument('--same-emb', action='store_true')

    args = parser.parse_args()
    if args.side_dim is not None:
        args.side_dim = [int(dim) for dim in args.side_dim.split("-")]
        args.num_layer = len(args.side_dim)

    return args

# Function to get the layer mask
def get_mask(args):
    if args.train_mask != None:
        if args.prefix_mask:
            layer_mask = {*range(args.train_mask)}
        else:
            layer_mask = {args.train_mask-1}
    else:
        layer_mask = {*range(args.num_layer)}
    return layer_mask

# Function to initialize the RNN model (Linear, LSTM, Transformer) based on training method (multi-layer AL, sideInput)
def initialize_model(args, vocab, word_vec, class_num:int):
    if args.model == 'lstmal':
        model = LSTM_AL(vocab_size=len(vocab), num_layer=args.num_layer, emb_dim=args.emb_dim, l1_dim=args.l1_dim, lab_dim=args.label_emb, class_num=class_num, word_vec=word_vec, lr=args.lr)
    elif args.model == 'linearal':
        model = Linear_AL(vocab_size=len(vocab), num_layer=args.num_layer, emb_dim=args.emb_dim, l1_dim=args.l1_dim, lab_dim=args.label_emb, class_num=class_num, word_vec=word_vec, lr=args.lr)
    elif args.model == 'transformeral':
        model = Transformer_AL(vocab_size=len(vocab), num_layer=args.num_layer, emb_dim=args.emb_dim, l1_dim=args.l1_dim, lab_dim=args.label_emb, class_num=class_num, word_vec=word_vec, lr=args.lr)

    elif args.model == 'transformeralside':
        model = Transformer_AL_Side(vocab_size=len(vocab), num_layer=args.num_layer, side_dim=args.side_dim, same_emb=args.same_emb,
                                        emb_dim=args.emb_dim, l1_dim=args.l1_dim, lab_dim=args.label_emb, class_num=class_num, word_vec=word_vec, lr=args.lr)
    elif args.model == 'linearalside':
        model = Linear_AL_Side(vocab_size=len(vocab), num_layer=args.num_layer, side_dim=args.side_dim, same_emb=args.same_emb,
                                        emb_dim=args.emb_dim, l1_dim=args.l1_dim, lab_dim=args.label_emb, class_num=class_num, word_vec=word_vec, lr=args.lr)
    elif args.model == 'lstmalside':
        model = LSTM_AL_Side(vocab_size=len(vocab), num_layer=args.num_layer, side_dim=args.side_dim, same_emb=args.same_emb,
                                        emb_dim=args.emb_dim, l1_dim=args.l1_dim, lab_dim=args.label_emb, class_num=class_num, word_vec=word_vec, lr=args.lr)

    return model

# Function for training model 1 epoch
def train(model:ALModel_Template, data_loader, current_epoch:int, task:str="text", layer_mask=None):
    if task == "text":
        cor, num, tot_loss = 0, 0, []
        y_out, y_tar = torch.Tensor([]),torch.Tensor([])
        data_loader = tqdm(data_loader)

        model.train(True, layer_mask)

        for (x, y) in data_loader:
            # training step
            x, y = x.cuda(), y.cuda()
            losses = model(x, y)
            tot_loss.append(losses)

            # validation step
            with torch.no_grad():
                pred = model.inference(x)
                y_out = torch.cat((y_out, pred.cpu()), 0)
                y_tar = torch.cat((y_tar, y.cpu().int()), 0).int()
                cor += (pred.argmax(-1).view(-1) == y.view(-1)).sum().item()
                num += x.size(0)

                data_loader.set_description(f'Train {current_epoch} | Acc {cor/num} ({cor}/{num})')
        train_acc = cor/num

        print(f'Train Epoch{current_epoch} Acc {train_acc} ({cor}/{num})')

# Function for adaptive testing 1 epoch
def test_adapt(model:ALModel_Template, data_loader, threshold=0.1, max_depth=None):
    model.eval()
    cor, num = 0, 0
    for x, y in data_loader:
        x, y = x.cuda(), y.cuda()
        pred, entr = model.inference_adapt(x, threshold=threshold, max_depth=max_depth)
        cor += (pred.argmax(-1).view(-1) == y.view(-1)).sum().item()
        num += x.size(0)

    valid_acc = cor/num
    return valid_acc

# Function for regular testing (shortcut) 1 epoch
def test(model:ALModel_Template, data_loader, shortcut=None, task="text"):
    if task == "text":
        model.eval()
        cor, num = 0, 0

        for x, y in data_loader:
            x, y = x.cuda(), y.cuda()
            pred = model.inference(x, shortcut)
            cor += (pred.argmax(-1).view(-1) == y.view(-1)).sum().item()
            num += x.size(0)

        valid_acc = cor/num
        return valid_acc

def main():
    # Time tracking for initialization
    init_start_time = time.process_time()

    # Initialize arguments and path settings
    args = get_args()
    os.makedirs(args.save_dir)
    path_name = f"{args.dataset}_{args.model}_l{args.num_layer}"
    save_path = f"{args.save_dir}/{path_name}"
    out_path = f"{args.out_dir}/{path_name}"
    if args.load_dir is not None:
        load_path = f"{args.load_dir}/{path_name}"

    # Load necessary data
    train_loader, valid_loader, class_num, vocab = get_data(args)
    layer_mask = get_mask(args)
    word_vec = get_word_vector(vocab, args.word_vec)

    # Initialize the model
    model = initialize_model(args, vocab, word_vec, class_num, path_name)

    if args.load_dir is not None:
        # Load model from the ckpt
        model.load_state_dict(torch.load(f'{load_path}.pt'))
    else:
        model.apply(initialize_weights)
    model = model.cuda()
    model.summary()

    torch.cuda.synchronize()
    print("init_time %s"%(time.process_time()-init_start_time))
    print('Start Training')
    total_train_time = time.process_time()

    # Training and validation loop
    best_acc = 0
    best_para = -1
    best_epoch = -1
    for epoch in range(args.epoch):
        # Time tracking for training
        ep_train_start_time = time.process_time()

        # Training step
        train(model, train_loader, epoch, task=args.task, layer_mask=layer_mask)

        torch.cuda.synchronize()
        print("ep%s_train_time %s"%(epoch ,time.process_time()-ep_train_start_time))

        # Validation steps for shortcuts and adaptive testing
        with torch.no_grad():
            # shortcut testing step
            for layer in range(model.num_layer):
                ep_test_start_time = time.process_time()
                acc = test(model, valid_loader, shortcut=layer+1, task=args.task)
                criteria = acc
                if args.lr_schedule != None:
                    model.schedulerStep(layer,criteria)
                torch.cuda.synchronize()
                print(f'Test Epoch{epoch} layer{layer} Acc {acc}')
                print("ep%s_l%s_test_time %s"%(epoch, layer ,time.process_time()-ep_test_start_time))
                if layer in layer_mask and criteria >= best_acc:
                    best_acc = criteria
                    best_para = layer
                    best_epoch = epoch
                    print("Save ckpt to", f'{save_path}.pt', " ,ep",epoch)
                    torch.save(model.state_dict(), f'{save_path}.pt')

            # adaptive testing step
            test_threshold = [.1,.2,.3,.4,.5,.6,.7,.8,.9]
            for threshold in test_threshold:
                test_start_time = time.process_time()
                model.data_distribution = [0 for _ in range(model.num_layer)]
                acc = test_adapt(model, valid_loader, threshold=threshold, max_depth=args.train_mask)
                criteria = acc
                torch.cuda.synchronize()
                print(f'Test threshold {threshold} Acc {acc}')
                print("t%s_test_time %s"%( threshold ,time.process_time()-test_start_time))
                print("data_distribution ",model.data_distribution)
                if criteria >= best_acc:
                    best_acc = criteria
                    best_epoch = epoch
                    print("Save ckpt to", f'{save_path}.pt', " ,ep",epoch)
                    torch.save(model.state_dict(), f'{save_path}.pt')

    print('Best Acc', best_acc, best_epoch, best_para)

    torch.cuda.synchronize()
    print("total_train+valid_time %s"%(time.process_time()-total_train_time))

if __name__ == '__main__':
    main()


# Hacer Akıncı
# 150200007

# Lagrange Interpolation Method

import numpy as np
import matplotlib.pyplot as plt

x = np.array([-1.2, 0.3, 1.1])
y = np.array([-5.76, -5.61, -3.69])

def LImethod(num):
    f_x = 0
    for i in range(len(x)):
        res = 1
        for j in range(len(x)):
            if i != j:
                res = res * ((num - x[j]) / (x[i] - x[j]))
        f_x = f_x + res*y[i]
    return f_x

d = np.arange(-2.0, 2, 0.0001)

plt.plot(x, y, 'bo', label= 'Data')
plt.plot(d, LImethod(d), 'y-')
plt.legend(loc='best')
plt.show()

# 0Print the result of input for the constructed polynomial

num = float(input("Enter x: "))
print(LImethod(num))


def capitalize(string):
        lst=string.split(" ")
        for i in range(len(lst)):
                lst[i]=lst[i].capitalize()
        str=" ".join(lst)
        return str

if __name__ == '__main__':
    string = input()
    capitalized_string = capitalize(string)
    print(capitalized_string)


#!/usr/bin/python3

from sys import argv as a

try:
  n = int(a[1])
except:
  print(f"{a[1]} isn't an int")
  exit(1)

SYM='⟁'
top, bottom = '', ''

for i in range(n):
  if i%2 == 0:
    top += f"{SYM*i + SYM: ^{n*2}}\n"
    bottom += f"{SYM*i + SYM: ^{n}}{SYM*i + SYM: ^{n}}\n"
print(f"{top}{bottom}")


import numpy as np
import matplotlib.pyplot as plt
import theano
from theano import tensor as T

class Layer:

    def __init__(self, weights_init, bias_init, activation):
        '''
        Individual layer constructor
        :param weights_init: initialized weight matrix connecting nodes between layers
        :param bias_init: initialized bias vector for the layer
        :param activation: activation function for the layer's output
        '''

        dim_output, dim_input = weights_init.shape

        assert bias_init.shape == (dim_output,)

        self.weights = theano.shared(value=weights_init.astype(theano.config.floatX), name='weights', borrow=True)
        self.bias = theano.shared(value=bias_init.reshape(dim_output, 1).astype(theano.config.floatX), name='bias',
                                  borrow=True, broadcastable=(False, True))

        self.activation = activation

        self.params = [self.weights, self.bias]

    def output(self, x):
        '''
        Computes an output based on processing an input vector through a weight matrix,
        adding a bias, and then feeding through an activation function: a(Wx + b)
        Note: activation is element-wise for output vector
        :param x: input feature vector
        :return: the final computational output of the layer (a vector)
        '''

        lin_output = T.dot(self.weights, x) + self.bias
        return lin_output if self.activation is None else self.activation(lin_output)

class MLP:

    def __init__(self, topology):
        '''
        Multilayer perceptron constructor
        :param topology: description of layer sequence defining the MLP
        '''

        self.layers = []

        for n_input, n_output in zip(topology[:-1], topology[1:]):
            self.layers.append(Layer(np.random.randn(n_output, n_input),
                                     np.ones(n_output), T.nnet.sigmoid))

        self.params = []
        for layer in self.layers:
            self.params += layer.params

    def output(self, x):
        '''
        Computes an output based on processing an input vector through multiple layers of the MLP
        :param x: input feature vector
        :return: the final computational output of the MLP
        '''

        # recursively compute the output through each layer
        for layer in self.layers:
            x = layer.output(x)
        return x

    def error(self, x, y):
        '''
        Cost function to be minimized using gradient descent method
        :param x: input
        :param y: target output
        :return: error function
        '''
        return T.sum(-(y * T.log(self.output(x)) + (1 - y) * T.log(1 - self.output(x))))

    def gradient_updates(self, cost, learning_rate):
        '''
        Provides the updates to weight and bias parameters in the MLP
        :param cost: cost function for determining derivatives w.r.t. parameters
        :param learning_rate: rate of gradient descent
        :return: updated parameter list
        '''

        updates = []

        for param in self.params:
            updates.append((param, param - learning_rate * T.grad(cost, param)))

        return updates

# EXAMPLE: XOR Function
inputs = np.vstack(np.array([[0, 0, 1, 1], [0, 1, 0, 1]])).astype(theano.config.floatX)
targets = np.array([1, 0, 0, 1]).astype(theano.config.floatX)

# First, set the size of each layer (and the number of layers)
# Input layer size is training data dimensionality (2)
# Output size is just 1-d: 0 or 1
# Finally, let the hidden layers be twice the size of the input.
# If we wanted more layers, just add another layer size to this list.

# topology = [inputs.shape[0], inputs.shape[0]*2, 1]
topology = [2, 3, 3, 1]
mlp = MLP(topology)

# Create Theano variables for the MLP input and targets
mlp_input = T.matrix('mlp_input')
mlp_target = T.vector('mlp_target')

# Learning rate
learning_rate = 0.01

# Create definition for computing the cost of the network given an input
cost = mlp.error(mlp_input, mlp_target)

# Create a theano function for training the network - parameters are updated based on the cost definition
train = theano.function([mlp_input, mlp_target], cost, updates=mlp.gradient_updates(cost, learning_rate))

# Create a theano function for computing the MLP output given some input
mlp_output = theano.function([mlp_input], mlp.output(mlp_input))

iteration = 0
cost = []
max_iteration = 30000
while iteration < max_iteration:

    current_cost = train(inputs, targets)
    cost.append(current_cost)
    iteration += 1

output = mlp_output(inputs)

for i in range(len(inputs[1])):

    print('The output for x1 = %d | x2 = %d is %.2f' % (inputs[0][i], inputs[1][i], output[0][i]))

# plot the cost minimization:
plt.plot(cost)
plt.show()


import sys
sys.setrecursionlimit(10**5)

# 효율적이지 못한 코드다.
# 최대한 시간을 줄이기 위해 노력했는데 현재 나의 레벨에서 이 수준이 최선이었다.
# 트리의 순회에 대해서 더 깊이있게 공부해야할듯 싶다.

def solve(inS, inE, postS, postE):
    if inS > inE or postS > postE:
        return
    root = postorder[postE]
    answer.append(root)
    # 0 1 2 3 4 5
    rootIdx = -1
    for i in range(inS, inE+1):
        if inorder[i] == root:
            rootIdx = i
            break
    if rootIdx == -1:
        return
    left = rootIdx-inS
    right = inE-rootIdx
    solve(inS, inS + left-1, postS, postS + left-1)
    solve(rootIdx+1, rootIdx + right, postS + left, postS + left + right - 1)

n = int(sys.stdin.readline())
inorder = list(map(int, sys.stdin.readline().split()))
postorder = list(map(int, sys.stdin.readline().split()))
answer = []
solve(0, n-1, 0, n-1)
print(*answer)


def encloseTags(tag,text,*attributes):
    outP = "<" + tag
    for attribute in attributes:
        outP += " " + attribute[0] + "=\"" + attribute[1] + "\""
    return outP + ">\n" + text + "\n</" + tag + ">"

class Project():
    def __init__(self,name,description,shortDescription=None,skills=[],git=None):
        self.name = name
        if shortDescription == None:
            self.shortDescription = description
        else:
            self.shortDescription = shortDescription
        self.description = description
        self.skills = skills
        self.git = git

    def __str__(self):
        return "\n".join(["Name: " + self.name,"Short description: " + self.shortDescription,"Description: " + self.description])

    def createPage(self):
        with open("base.html","r") as file:
            file = file.readlines()
            file[1] = self.name.replace("-"," ")
            file[3] = ''.format(self.name)
            content = ""
            d = self.description.split("\n")

            for line in d:
                content += encloseTags("p",line)

            content += encloseTags("p",encloseTags("a","Link to GitHub",["href",self.git]))

            file[4] = content

            for skill in self.skills:
                file[6] += encloseTags("li",skill)

            outP = "\n".join(file)
        return outP
    def createCard(self):
        outP = encloseTags("div",''.format(self.name.replace("-"," ")) + encloseTags("h3",self.name.replace("-"," ")) + encloseTags("p",self.shortDescription) + encloseTags("a","View project",["href","{}.html".format(self.name.replace("-"," "))]),["class","project-card"])
        return outP


import argparse
import logging
import os.path
import sys
from os.path import dirname, join, realpath, exists, basename

STYLE_DIR = dirname(dirname(dirname(dirname(realpath(__file__)))))
sys.path.append(STYLE_DIR)
from common.utils import set_logger_file, parse_buildings_csv, STYLES, str2bool

from preprocess.blender.scene_utils import cleanup
from preprocess.blender.io_utils import load_obj, save_obj
from preprocess.blender.scene_utils import add_scene, add_world, get_scene, set_active_scene, remove_objects
from preprocess.blender.buildnet.group_helper import *

LOGGER = logging.getLogger(__name__)

STYLISTIC_ELEMENTS_THRESHOLD_JSON = join(STYLE_DIR, "resources", "BUILDNET_GROUP_THRESHOLDS_v2.json")
STYLISTIC_PARTS_THR_FACTORS = json.load(open(STYLISTIC_ELEMENTS_THRESHOLD_JSON))
OBB_CMD = join(STYLE_DIR, "preprocess/oriented_bounding_box/cgal_impl/cmake-build-release/OrientedBboxC")

# Scene 1 has the original (normalized) building composed by many components with textures and a camera to render
# Scene 2 has the (normalized) building loaded as ply format, i.e. one united mesh, to be used for ray tracing.
# Scene 3 has the (normalized) building loaded as obj format with objects having no materials, so that we use red.
add_scene()
add_world()
add_scene()
add_world()
scenes_str = ['Scene', 'Scene.001', 'Scene.002']
worlds_str = ['World', 'World.001', 'World.002']

obj_scene = get_scene(scenes_str[0])

def get_elements_with_style(scene):
    elements = []
    for obj in scene.objects:
        if "__" in obj.name:
            elements.append(obj.name)
    return elements

def main_group(obj_inp_file, groups_dir, debug, on_gpu, transparent_back):
    groups_file = join(groups_dir, "groups.json")
    group_colors_file = join(groups_dir, "g_colours.json")
    gr_prefix = "_grouped_" if not transparent_back else "tr_group_"
    if exists(groups_file) \
            and any(gr_prefix in f for f in os.listdir(groups_dir)) \
            and any(".ply" in f for f in os.listdir(groups_dir)):
        LOGGER.warning("Won't process {}".format(groups_dir))
        return
    log_file = join(groups_dir, "logs.log")

    # One scene with the loaded obj and the camera
    remove_objects(except_names=['Camera'])
    set_active_scene(obj_scene.name)
    load_obj(obj_inp_file)

    elements_with_style = get_elements_with_style(obj_scene)
    LOGGER.info("elements with style: {}".format(", ".join(elements_with_style)))
    if len(elements_with_style) == 0:
        LOGGER.warning("No elements with style ==> nothing to be done.")
        save_groups({}, groups_file)
        return

    if exists(groups_file):
        LOGGER.info("Groups exist and will be loaded for further processing.")
        all_groups = list(load_groups(groups_file).values())
        g_colors = load_colors(group_colors_file)
    else:
        all_groups, g_colors = create_groups(elements_with_style, group_colors_file, groups_file, debug)

    grouped_obj_file = join(groups_dir, "grouped.obj")
    if not any(gr_prefix in f for f in os.listdir(groups_dir)):
        LOGGER.info("Colorizing groups")
        colorize(all_groups, g_colors, remove_material=True, all_same=False)
        LOGGER.info("Rendering groups")
        visualize_groups(groups_dir, obj_scene, on_gpu=on_gpu, transparent_back=transparent_back)

    if not exists(grouped_obj_file):
        save_obj(grouped_obj_file)

    if not any(".ply" in f for f in os.listdir(groups_dir)):
        LOGGER.info("Creating ply files per group")
        group_ply_filenames = save_ply_groups(all_groups, groups_dir, obj_scene)
        for group_ply_filename in group_ply_filenames:
            print('{} "{}" >> "{}"'.format(OBB_CMD, group_ply_filename, log_file))
            os.system('{} "{}" >> "{}"'.format(OBB_CMD, group_ply_filename, log_file))  # "" is needed due to spaces
    cleanup(True)

def create_groups(elements_with_style, group_colors_file, groups_file, debug):
    LOGGER.info("Creating groups...")
    all_groups = []  # [ [group1_element1name, group1_element2name,...], [group2_element1name, ...], ...]
    for stylistic_part, threshold_factor in STYLISTIC_PARTS_THR_FACTORS.items():
        LOGGER.info(f"{stylistic_part}:{threshold_factor}")
        for style in STYLES:
            current_elements = [element for element in elements_with_style
                                if stylistic_part.lower() in element.lower()
                                and style.lower() in element.lower()]
            if len(current_elements) > 0:
                debug_dir = None
                if debug:
                    debug_dir = os.path.join(os.path.dirname(groups_file), "debug", stylistic_part, style)
                groups = ComponentsGrouper(LOGGER, factor=float(threshold_factor), debug_dir=debug_dir)\
                    .add_all(current_elements)\
                    .group()\
                    .get()
                LOGGER.info("{} groups of {}".format(len(groups), stylistic_part))
                all_groups += groups
    g_colors = get_colors(all_groups)
    save_colors(g_colors, group_colors_file)
    save_groups(all_groups, groups_file)
    return all_groups, g_colors

def process_building(input_file, output_dir, debug, on_gpu, transparent_back):
    os.makedirs(output_dir, exist_ok=True)
    LOGGER.info("grouping...")
    main_group(input_file, output_dir, debug, on_gpu, transparent_back)

if __name__ == '__main__':

    if '--' in sys.argv:
        argv = sys.argv[sys.argv.index('--') + 1:]
        parser = argparse.ArgumentParser()
        parser.add_argument('-root_data', type=str, default="/media/christina/Data/ANNFASS_data/BUILDNET_Buildings")
        parser.add_argument('-obj_dir', type=str, default="normalizedObj")
        parser.add_argument('-group_dir', type=str, default="groups")
        parser.add_argument('-buildings_csv', type=str, default="buildings.csv")
        parser.add_argument('-logs_dir', type=str, default="/media/christina/Data/ANNFASS_data/BUILDNET_Buildings/logs")
        parser.add_argument('-debug', type=str2bool, default=False)
        parser.add_argument('-on_gpu', type=str2bool, default=True)
        parser.add_argument('-transparent_back', type=str2bool, default=True)
        args = parser.parse_known_args(argv)[0]
    else:
        raise Exception('please give args')

    if not exists(args.logs_dir):
        os.makedirs(args.logs_dir)
    _log_file = join(args.logs_dir, basename(args.buildings_csv).replace('.csv', '.log'))
    LOGGER = set_logger_file(_log_file, LOGGER)

    LOGGER.info("Starting...")

    LOGGER.info("root_data {}".format(args.root_data))
    LOGGER.info("obj_dir {}".format(args.obj_dir))
    LOGGER.info("group_dir {}".format(args.group_dir))
    LOGGER.info("buildings_csv {}".format(args.buildings_csv))

    buildings = parse_buildings_csv(join(args.root_data, args.buildings_csv))
    LOGGER.info("buildings: {}".format(buildings))

    for building in buildings:
        obj_file = join(args.root_data, args.obj_dir, building, "{}.obj".format(building))
        if not exists(obj_file):
            LOGGER.info("{} doesn't exist. Won't process.".format(obj_file))
            continue
        group_dir = join(args.root_data, args.group_dir, building)
        LOGGER.info("Processing {}".format(obj_file))
        process_building(obj_file, group_dir, args.debug, args.on_gpu, args.transparent_back)

    LOGGER.info("Ending...")

# /home/graphicslab/OtherApps/blender-2.91.2-linux64/blender --background --python group_as_component.py -- -root_data /media/graphicslab/BigData/zavou/ANNFASS_DATA/BUILDNET_Buildings -obj_dir normalizedObj -group_dir group_debug -buildings_csv buildings_debug.csv -logs_dir /media/graphicslab/BigData/zavou/ANNFASS_DATA/BUILDNET_Buildings/grouplogs -debug True -on_gpu False


class Solution:

    # 2022/03/27
    # HashSet [O(m+n):83%]
    def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:
        set1 = set(nums1)
        set2 = set(nums2)
        ans1, ans2 = set(), set()
        for v in nums1:
            if v not in set2:
                ans1.add(v)
        for v in nums2:
            if v not in set1:
                ans2.add(v)
        return [ans1, ans2]

algo/HashMap/__2215__Find the Difference of Two Arrays.py


"""
https://www.practicepython.org/exercise/2014/03/19/07-list-comprehensions.html

https://youtu.be/3dt4OGnU5sM?t=206"""

a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

# first 'n' is what i want - always declare this first, 2nd 'n' is part of the for loop.
even = [number for number in a if number % 2 == 0]
print(even)


import matplotlib.pyplot as plt
import math

Fn = 'Данила'
ln = 'Хохлов'
Var = 22
N = len(Fn) + len(ln)
xi = [22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]
pi = [0.1, 0.15, 0.02, 0.15, 0.05, 0.01, 0.06, 0.1, 0.2, 0.06, 0.03, 0.07]
print(xi)
print(pi)
print(N)
#Проверяем верно ли все вывелось
s = sum(pi)
print(s)
if len(xi) == len(pi) and (sum(pi) == 1):
   print('размерность массивов одинакова')
   print('сумма вероятностей = 1')

M = sum([pi[i] * xi[i] for i in range(N)])
print('Матожидание', M)
D= sum([pi[i] * (xi[i] ** 2) for i in range(N)])- M ** 2
print('Дисперсия', D)
CKO = math.sqrt(D)
print('СКО', CKO)

fig, ax = plt.subplots(1) # от числа зависит сколько будет окон
plt.plot(xi,pi) # собственно откладываем по разным осям точки
plt.xlabel('xi') # называем оси
plt.ylabel('pi')
moda = max(pi)
Me = round(len(xi) / 2)
print('Мода распределения', moda)
print('медиана', Me)
plt.vlines(Me + Var, 0, pi[Me])
plt.vlines(pi.index(moda) + Var, 0, moda) #index показывает какой по счету будет величайшее число
plt.show()
fig. savefig('hohlov_UIR1/Функция плотности распределения.jpg')

a = []

#for i in range(0, len(xi)):
 # a.append(pi[n] + pi[i])
#fig, ax = plt.subplots(1) # от числа зависит сколько будет окон
#plt.plot(xi,a) # собственно откладываем по разным осям точки
#plt.xlabel('xi') # называем оси
#plt.ylabel('a')


# ======================================
#exit(0)
# ======================================

import numpy as np
from libs import *

mydb = mysql_connect('database_v3')

#fields = ['cat_1', 'cat_2', 'cat_3', 'cat_4', 'cat_5', 'cat_6', 'main_category', 'sub_category', 'gender', 'google_product_category']
fields = ['cat_1']

def find_skus():

    _sql = ' select id_sku, cat_1, cat_2, cat_3, cat_4, cat_5, cat_6, ' \
           ' main_category, sub_category, gender, google_product_category '\
           ' from skus'
    mydb.reconnect()
    mycursor = mydb.cursor()
    mycursor.execute(_sql)
    _skus = np.array(mycursor.fetchall(), dtype=str)
    return _skus

field_id = 0

skus = find_skus()
skus_num = len(skus)
skus_num_digits = len(str(skus_num))

mycursor = mydb.cursor()

list_terms = []
for i, sku in enumerate(skus):

    cat_val = sku[field_id + 1]

    for j, field in enumerate(fields):

        if sku[j + 1] != 'None' and sku[j + 1] != 'none' and j != field_id:

            aux_val = sku[j + 1]

            if cat_val == aux_val:

                value_aux = '(' + str(fields[field_id]) + ' - ' + str(fields[j]) + ')'
                print(value_aux)
                list_terms.append(value_aux)

                #'''
                _sql = ' update skus set ' \
                       + fields[j] + ' = null ' \
                       ' where id_sku = ' + str(sku[0])+';'
                mycursor.execute(_sql)
                mydb.commit()
                #'''

np.savetxt('./dups/dups_' + fields[field_id] + '.txt', list_terms, fmt="%s")


import os
import logging
from logging.handlers import SMTPHandler, RotatingFileHandler
from flask import Flask, current_app

from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager
from flask_mail import Mail
from flask_bootstrap import Bootstrap

#from config import Config
## app = Flask(__name__)
print("OS environment", os.environ.get("FLASK_ENV"))

db = SQLAlchemy()
migrate = Migrate()
login = LoginManager()
login.login_view = 'auth.login'
login.login_message = "Please log in"
bootstrap = Bootstrap()
mail = Mail()

def create_app():

    app = Flask(__name__, instance_relative_config=False)

    if os.environ.get("FLASK_ENV") == "development":
        print("OS environment is DEVELOPMENT: ", os.environ.get("FLASK_ENV"))
        app.config.from_object('config.DevConfig')
    else:
        print("OS environment is PRODUCTION: ", os.environ.get("FLASK_ENV"))
        app.config.from_object('config.ProdConfig')

    # Initialize Plugins
    db.init_app(app)
    migrate.init_app(app, db)
    login.init_app(app)
    mail.init_app(app)
    bootstrap.init_app(app)

    # with app.app_context():

    #     app.register_blueprint(auth.auth_bp)
    #     app.register_blueprint(main.main_bp)

    #     return app
    print("Entering app.register phase now:")
    from app.errors import errors_bp
    app.register_blueprint(errors_bp)

    from app.auth import auth_bp
    app.register_blueprint(auth_bp, url_prefix='/auth')

    from app.main import main_bp
    app.register_blueprint(main_bp)

    # PJ 20201019: dont disable MAIL and logging
    # if not app.debug and not app.testing:
    if 1:
        if app.config['MAIL_SERVER']:
            auth = None
            if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWORD']:
                auth = (app.config['MAIL_USERNAME'],
                        app.config['MAIL_PASSWORD'])
            secure = None
            if app.config['MAIL_USE_TLS']:
                secure = ()
            mail_handler = SMTPHandler(
                mailhost=(app.config['MAIL_SERVER'], app.config['MAIL_PORT']),
                fromaddr='no-reply@' + app.config['MAIL_SERVER'],
                toaddrs=app.config['ADMINS'], subject='App Failure',
                credentials=auth, secure=secure)
            mail_handler.setLevel(logging.ERROR)
            app.logger.addHandler(mail_handler)

        if app.config['LOG_TO_STDOUT']:
            stream_handler = logging.StreamHandler()
            stream_handler.setLevel(logging.INFO)
            app.logger.addHandler(stream_handler)
        else:
            if not os.path.exists('logs'):
                os.mkdir('logs')
            file_handler = RotatingFileHandler('logs/microblog.log',
                                               maxBytes=10240, backupCount=10)
            file_handler.setFormatter(logging.Formatter(
                '%(asctime)s %(levelname)s: %(message)s '
                '[in %(pathname)s:%(lineno)d]'))
            file_handler.setLevel(logging.INFO)
            app.logger.addHandler(file_handler)

        app.logger.setLevel(logging.INFO)
        app.logger.info('App startup')

    return app

from app import models


import os
import numpy as np

from RecSysFramework.DataManager import Dataset
from RecSysFramework.DataManager.Reader import DataReader
from RecSysFramework.DataManager.Splitter import LeaveKOut
from RecSysFramework.ExperimentalConfig import EXPERIMENTAL_CONFIG

from RecSysFramework.Utils import generate_URM_test_negative
from RecSysFramework.Utils import load_compressed_csr_matrix, save_compressed_csr_matrix

from utils import create_dataset_from_folder, compress_urm, read_ratings, output_scores, row_minmax_scaling, evaluate

class FakeReader(DataReader):

    DATASET_SUBFOLDER = "WSDMCup/"

    def _load_from_original_file(self):
        raise Exception("Should not call this!!")

if __name__ == "__main__":

    splitter = EXPERIMENTAL_CONFIG['splitter']

    for exam_folder in EXPERIMENTAL_CONFIG['test-datasets']:

        exam_train, exam_valid, urm_exam_valid_neg, urm_exam_test_neg = create_dataset_from_folder(exam_folder)
        exam_user_mapper, exam_item_mapper = exam_train.get_URM_mapper()
        exam_folder_profile_lengths = np.ediff1d(exam_train.get_URM().indptr)
        exam_valid_users_to_evaluate = np.arange(urm_exam_valid_neg.shape[0])[np.ediff1d(urm_exam_valid_neg.indptr) > 0]
        exam_test_users_to_evaluate = np.arange(urm_exam_test_neg.shape[0])[np.ediff1d(urm_exam_test_neg.indptr) > 0]

        exam_urm = exam_train.get_URM() + exam_valid.get_URM()
        dataset = Dataset(
            exam_folder, base_folder=EXPERIMENTAL_CONFIG['dataset_folder'] + exam_folder,
            URM_dict={"URM_all": exam_urm},
            URM_mappers_dict={"URM_all": exam_train.get_URM_mapper()}
        )

        print(exam_urm.shape)

        for fold in range(EXPERIMENTAL_CONFIG['n_folds']):

            urm_neg = generate_URM_test_negative(exam_train.get_URM(), exam_valid.get_URM(), random_seed=fold + 100, negative_samples=99, type="fixed")
            save_compressed_csr_matrix(urm_neg, EXPERIMENTAL_CONFIG['dataset_folder'] + exam_folder + os.sep + "urm_valid_neg_{}.npz".format(fold))

            dataset_name = "{}-{}".format(exam_folder, fold)
            folder = EXPERIMENTAL_CONFIG['dataset_folder'] + exam_folder + os.sep + dataset_name + os.sep

            train, test = splitter.split(dataset, random_seed=fold + 10)
            splitter.save_split((train, test), save_folder_path=folder)

            print(train.get_URM().shape)
            URM_test_negative = generate_URM_test_negative(
                train.get_URM(), test.get_URM(), random_seed=fold, negative_samples=99, type="fixed"
            )
            save_compressed_csr_matrix(URM_test_negative, folder + "urm_neg.npz")

            for i in range(EXPERIMENTAL_CONFIG['n_folds']):
                URM_test_negative = generate_URM_test_negative(
                    train.get_URM(), test.get_URM(), random_seed=fold + 1 + i, negative_samples=99, type="fixed"
                )
                save_compressed_csr_matrix(URM_test_negative, folder + "urm_neg_{}.npz".format(i))


# -*- coding: UTF-8 -*-
# pylint: disable=useless-object-inheritance
# pylint: disable=super-with-arguments
"""
This module provides low-level helper functionality during step imports.

.. warn:: Do not use directly

    It should not be used directly except in behave Runner classes
    that need to provide the correct context (step_registry, matchers, etc.)
    instead of using the global module specific variables.
"""

from __future__ import absolute_import
from contextlib import contextmanager
from threading import Lock
from types import ModuleType
import os.path
import sys
import six

from behave import step_registry as _step_registry
from behave.matchers import StepMatcherFactory
from behave.step_registry import StepRegistry

# -----------------------------------------------------------------------------
# UTILITY FUNCTIONS:
# -----------------------------------------------------------------------------
def setup_api_with_step_decorators(module, step_registry):
    _step_registry.setup_step_decorators(module, step_registry)

def setup_api_with_matcher_functions(module, step_matcher_factory):
    # -- PUBLIC API: Same as behave.api.step_matchers
    module.use_default_step_matcher = step_matcher_factory.use_default_step_matcher
    module.use_step_matcher = step_matcher_factory.use_step_matcher
    module.step_matcher = step_matcher_factory.use_step_matcher
    module.register_type = step_matcher_factory.register_type

class SimpleStepContainer(object):
    def __init__(self, step_registry=None):
        if step_registry is None:
            step_registry = StepRegistry()
        self.step_matcher_factory = StepMatcherFactory()
        self.step_registry = step_registry
        self.step_registry.step_matcher_factory = self.step_matcher_factory

# -----------------------------------------------------------------------------
# FAKE MODULE CLASSES: For step imports
# -----------------------------------------------------------------------------
# class FakeModule(object):
class FakeModule(ModuleType):
    ensure_fake = True

    # -- SUPPORT FOR: behave.step_registry.setup_step_decorators()
    def __setitem__(self, name, value):
        assert "." not in name
        setattr(self, name, value)

class StepRegistryModule(FakeModule):
    """Provides a fake :mod:`behave.step_registry` module
    that can be used during step imports.
    """
    __all__ = [
        "given", "when", "then", "step",
        "Given", "When", "Then", "Step",
    ]

    def __init__(self, step_registry):
        super(StepRegistryModule, self).__init__("behave.step_registry")
        self.registry = step_registry
        setup_api_with_step_decorators(self, step_registry)

class StepMatchersModule(FakeModule):
    __all__ = [
        "use_default_step_matcher",
        "use_step_matcher",
        "step_matcher",  # -- DEPRECATING
        "register_type"
    ]

    def __init__(self, step_matcher_factory):
        super(StepMatchersModule, self).__init__("behave.matchers")
        self.step_matcher_factory = step_matcher_factory
        setup_api_with_matcher_functions(self, step_matcher_factory)
        self.make_matcher = step_matcher_factory.make_matcher
        # -- DEPRECATED-FUNCTION-COMPATIBILITY
        # self.get_matcher = self.make_matcher
        # self.matcher_mapping = matcher_mapping or _matchers.matcher_mapping.copy()
        # self.current_matcher = current_matcher or _matchers.current_matcher

        # -- INJECT PYTHON PACKAGE META-DATA:
        # REQUIRED-FOR: Non-fake submodule imports (__path__).
        here = os.path.dirname(__file__)
        self.__file__ = os.path.abspath(os.path.join(here, "matchers.py"))
        self.__name__ = "behave.matchers"
        # self.__path__ = [os.path.abspath(here)]

class BehaveModule(FakeModule):
    __all__ = StepRegistryModule.__all__ + StepMatchersModule.__all__

    def __init__(self, step_registry, step_matcher_factory=None):
        if step_matcher_factory is None:
            step_matcher_factory = step_registry.step_step_matcher_factory
        assert step_matcher_factory is not None
        super(BehaveModule, self).__init__("behave")
        setup_api_with_step_decorators(self, step_registry)
        setup_api_with_matcher_functions(self, step_matcher_factory)
        self.use_default_step_matcher = step_matcher_factory.use_default_step_matcher
        assert step_registry.step_matcher_factory == step_matcher_factory

        # -- INJECT PYTHON PACKAGE META-DATA:
        # REQUIRED-FOR: Non-fake submodule imports (__path__).
        here = os.path.dirname(__file__)
        self.__file__ = os.path.abspath(os.path.join(here, "__init__.py"))
        self.__name__ = "behave"
        self.__path__ = [os.path.abspath(here)]
        self.__package__ = None

class StepImportModuleContext(object):

    def __init__(self, step_container):
        self.step_registry = step_container.step_registry
        self.step_matcher_factory = step_container.step_matcher_factory
        assert self.step_registry.step_matcher_factory == self.step_matcher_factory
        self.step_registry.step_matcher_factory = self.step_matcher_factory

        step_registry_module = StepRegistryModule(self.step_registry)
        step_matchers_module = StepMatchersModule(self.step_matcher_factory)
        behave_module = BehaveModule(self.step_registry, self.step_matcher_factory)
        self.modules = {
            "behave": behave_module,
            "behave.matchers": step_matchers_module,
            "behave.step_registry": step_registry_module,
        }
        # self.default_matcher = self.step_matchers_module.current_matcher

    def reset_current_matcher(self):
        self.step_matcher_factory.use_default_step_matcher()

_step_import_lock = Lock()
unknown = object()

@contextmanager
def use_step_import_modules(step_container):
    """
    Redirect any step/type registration to the runner's step-context object
    during step imports by using fake modules (instead of using module-globals).

    This allows that multiple runners can be used without polluting the
    global variables in problematic modules
    (:mod:`behave.step_registry`, mod:`behave.matchers`).

    .. sourcecode:: python

        # -- RUNNER-IMPLEMENTATION:
        def load_step_definitions(self, ...):
            step_container = self.step_container
            with use_step_import_modules(step_container) as import_context:
                # -- USE: Fake modules during step imports
                ...
                import_context.reset_current_matcher()

    :param step_container:
        Step context object with step_registry, step_matcher_factory.
    """
    orig_modules = {}
    import_context = StepImportModuleContext(step_container)
    with _step_import_lock:
        # -- CRITICAL-SECTION (multi-threading protected)
        try:
            # -- SCOPE-GUARD SETUP: Replace original modules with fake ones.
            for module_name, fake_module in six.iteritems(import_context.modules):
                orig_module = sys.modules.get(module_name, unknown)
                orig_modules[module_name] = orig_module
                sys.modules[module_name] = fake_module

            # -- USE: Fake modules for step imports.
            yield import_context
        finally:
            # -- SCOPE-GUARD CLEANUP: Restore original modules.
            for module_name, orig_module in six.iteritems(orig_modules):
                if orig_module is unknown:
                    del sys.modules[module_name]
                else:
                    sys.modules[module_name] = orig_module


# 양의 정수 n에 대해서 d(n)을 n과 n의 각 자리수를 더하는 함수라고 정의하자.
# 예를 들어, d(75) = 75+7+5 = 87이
def functionD(n):
	if n > 10000:
		return -1
	# 123
	# 12
	sum = n
	for i in str(n):
		sum += int(i)
	return sum

num_arr = [i for i in range(1, 10001)]
# for i in range(1, 10001):
for i in range(1, 10001):
	num = i
	if num_arr[i - 1] == 0:
		continue
	while True:
		num = functionD(num)
		if num > 10000:
			break
		num_arr[num - 1] = 0

for i in num_arr:
	if i != 0:
		print(i)


""" URLs for the userprofile app """

from django.conf.urls import url

from eshop import views as eshop_views

from . import views

__author__ = 'Aupourbau Koumar'
__copyright__ = 'Copyright 2018, Maison Mahdil'

# URLs
urlpatterns = [
    url(
        r'^$',
        views.profile,
        name='profile'
    ),
    url(
        r'^adresse/ajouter/$',
        eshop_views.UserAddressCreateView2.as_view(),
        name='address_create'
    ),
]


import os
import sqlite3
from pathlib import Path

from bgmi import __version__
from bgmi.config import BGMI_PATH, cfg
from bgmi.utils import print_error, print_info

OLD = BGMI_PATH.joinpath("old")

def exec_sql(sql: str, db: Path = cfg.db_path) -> None:
    try:
        print_info(f"Execute {sql}")
        conn = sqlite3.connect(db)
        conn.execute(sql)
        conn.commit()
        conn.close()
    except sqlite3.OperationalError:  # pragma: no cover
        print_error("Execute SQL statement failed", exit_=False)

def update_database() -> None:
    if not os.path.exists(OLD):
        with open(OLD, "w", encoding="utf8") as f:
            f.write(__version__)
    else:
        with open(OLD, "r+", encoding="utf8") as f:
            f.read()
            f.seek(0)
            f.write(__version__)

    # if v < "1.0.25":
    #     pass


"""
Contains the MNISTModel class the other scripts are built to verify, as well as: the training and validation datasets;
    a Trainer class built to train the model on the datasets; and a helper function to load an instance of the MNISTModel
    from a state dictionary file.
"""

import random
from statistics import mean

import torch
from matplotlib import pyplot as plt
from torch import nn
from torch.utils.data import DataLoader
from torchvision.datasets import mnist
from torchvision.transforms.v2 import Compose, ToImage, ToDtype
from torchvision.transforms.v2.functional import to_pil_image
from tqdm import tqdm, trange

to_tensor = Compose([ToImage(), ToDtype(torch.float32, scale=True)])
train_dataset = mnist.MNIST(root="./data", train=True, transform=to_tensor, download=True)
validation_dataset = mnist.MNIST(root="./data", train=False, transform=to_tensor, download=True)

def load_MNIST_model(layer_sizes=(784, 100, 100, 10), activation=None, flatten=True, filename="mnist_model.pt"):
    """
    Construct an MNISTModel and load state dictionary from a file.
    :param layer_sizes: An iterable of the number of neurons in each layer. Defaults to (28x28, 100, 100, 10), ie,
                        an MLP for the MNIST problem using 2 hidden layers, 784 input neurons, and 10 output classes.
    :param activation: The activation module used in the original model. If None, uses default from MNISTModel class.
    :param flatten: Whether the original model had an nn.Flatten layer at the start. Default True.
    :param filename: Where to look for the saved state dictionary. Default mnist_model.pt.
    :return: The reinstantiated model, loaded with saved weights.
    """
    model = MNISTModel(layer_sizes=layer_sizes, activation=activation, flatten=flatten)
    model.load_state_dict(torch.load(filename))
    return model

class MNISTModel(nn.Module):
    """
    A generic MLP with defaults designed for the MNIST classification problem: 784 input pixels and 10 classes.
    """

    def __init__(self, layer_sizes=(784, 100, 100, 10), activation=None, flatten=True):
        """
        Creates an MLP for the MNIST classification problem.
        :param layer_sizes: An iterable of the number of neurons in each layer. Defaults to (28x28, 100, 100, 10), ie,
                        2 hidden layers of 100 neurons each, 784 input neurons, and 10 output classes.
        :param activation: An activation module to use between Linear layers. If None, uses nn.ReLU()
        :param flatten: Whether to include an nn.Flatten() layer at the start of the net. Default True
        """
        super().__init__()
        activation = activation or nn.ReLU()

        modules = nn.ModuleList()
        if flatten:
            modules.append(nn.Flatten())
        for num_in, num_out in zip(layer_sizes[:-2], layer_sizes[1:-1]):
            modules.append(nn.Linear(num_in, num_out))
            modules.append(activation)
        modules.append(nn.Linear(layer_sizes[-2], layer_sizes[-1]))

        self.net = nn.Sequential(*modules)

    def forward(self, x):
        return self.net(x)

class MNISTTrainer:
    """
    A helper class designed to train an MNISTModel on the MNIST dataset.
    """

    def __init__(self, model, train_dataloader, validation_dataloader=None, criterion=None, optimizer=None,
                 device=None):
        """
        :param model: The model to train. Written for MNISTModel
        :param train_dataloader: The training dataloader. Written to use the MNIST dataset. Images should be tensors.
        :param validation_dataloader: If None, validation will not be performed; otherwise, it will be done every epoch.
        :param criterion: Loss function. If None, uses nn.CrossEntropyLoss()
        :param optimizer: Optimizer. If None, uses Adam with lr=3e-4
        :param device: The device to train on. If None, uses the cpu.
        """
        self.model = model
        self.train_dataloader = train_dataloader
        self.validation_dataloader = validation_dataloader
        self.optimizer = optimizer or torch.optim.Adam(self.model.parameters(), lr=3e-4)
        self.criterion = criterion or nn.CrossEntropyLoss()
        self.device = device or torch.device("cpu")
        self.model.to(self.device)

    def train_step(self, x, y):
        x, y = x.to(self.device), y.to(self.device)
        loss = self.criterion(self.model(x), y)
        loss.backward()
        self.optimizer.step()
        self.optimizer.zero_grad()
        return loss.item()

    def validation_step(self, x, y):
        with torch.inference_mode():
            x, y = x.to(self.device), y.to(self.device)
            y_hat = self.model(x)
            loss = self.criterion(y_hat, y)
            return loss.item(), (y_hat.argmax(1) == y).float().mean().item()

    def train_one_epoch(self, epoch_number):
        loop = tqdm(self.train_dataloader, leave=False)
        loop.set_description(f"In Trainer, epoch {epoch_number + 1}")
        losses = []
        for i, (x, y) in enumerate(loop):
            loss = self.train_step(x, y)
            losses.append(loss)
            if not i % (len(self.train_dataloader) / 10):
                loop.set_postfix({"loss": loss})
        return losses, mean(losses)

    def validation_epoch(self):
        loop = tqdm(self.validation_dataloader, leave=False)
        loop.set_description(f"In Trainer, validation")
        losses = []
        accuracies = []
        for x, y in loop:
            loss, accuracy = self.validation_step(x, y)
            losses.append(loss)
            accuracies.append(accuracy)
            loop.set_postfix({"loss": loss})
        return mean(losses), mean(accuracies)

    def train_epochs(self, num_epochs):
        loop = trange(num_epochs)
        loop.set_description(f"In Trainer, training progress")
        all_losses = []
        mean_validation_losses = []
        mean_validation_accuracies = []
        for epoch_number in loop:
            losses, mean_loss = self.train_one_epoch(epoch_number)
            all_losses += losses
            if self.validation_dataloader is not None:
                mean_validation_loss, mean_validation_accuracy = self.validation_epoch()
                mean_validation_losses.append(mean_validation_loss)
                mean_validation_accuracies.append(mean_validation_accuracy)
                loop.set_postfix(
                    {"validation_accuracy": mean_validation_accuracy, "validation_loss": mean_validation_loss,
                     "train_loss": mean_loss})
            else:
                loop.set_postfix({"train_loss": mean_loss})
        return all_losses if self.validation_dataloader is None else all_losses, mean_validation_losses, mean_validation_accuracies

if __name__ == "__main__":
    # Example usage:

    # Train a default MNISTModel on the dataset for 15 epochs
    train_dataloader = DataLoader(train_dataset, batch_size=1000)
    validation_dataloader = DataLoader(validation_dataset, batch_size=1000)
    if torch.cuda.is_available():
        device = torch.device("cuda")
    elif torch.backends.mps.is_available():
        device = torch.device("mps")
    else:
        device = torch.device("cpu")
    trainer = MNISTTrainer(model=MNISTModel(), train_dataloader=train_dataloader,
                           validation_dataloader=validation_dataloader, device=device)

    epochs = 15
    all_losses, mean_validation_losses, mean_validation_accuracies = trainer.train_epochs(epochs)

    # Plot losses and accuracies
    plt.figure(1)
    plt.title("Training losses")
    plt.xlabel("Step")
    plt.ylabel("Cross entropy loss")
    plt.plot(range(epochs * len(train_dataloader)), all_losses)
    plt.figure(2)
    plt.title("Mean validation losses")
    plt.xlabel("After epoch")
    plt.ylabel("Cross entropy loss")
    plt.plot(range(1, epochs + 1), mean_validation_losses)
    plt.figure(3)
    plt.title("Mean validation accuracies")
    plt.xlabel("After epoch")
    plt.ylabel("Accuracy")
    plt.plot(range(1, epochs + 1), mean_validation_accuracies)
    plt.show()

    # Verify qualitatively using a random image from the validation dataset
    model = trainer.model
    model.eval()
    print(f"Model has {sum(p.numel() for p in model.parameters())} parameters.")
    x, y = random.choice(validation_dataset)
    to_pil_image(x.reshape(1, 28, 28)).resize((280, 280)).show()
    with torch.inference_mode():
        print(f"For the shown image, true label is {y}; model predicted {model(x.to(device)).argmax(1).item()}")

    # Save
    torch.save(model.state_dict(), "mnist_model.pt")


import datetime
import io
import re

class Packet:
    def __init__(self, key, payload, timestamp=None, attachments=None):
        self.validate_key(key)

        if timestamp is None:
            timestamp = datetime.datetime.utcnow()
        else:
            self.validate_timestamp(timestamp)

        if attachments:
            self.validate_attachments(attachments)

        self.key = key
        self.payload = payload
        self.timestamp = timestamp
        self.attachments = attachments or {}

    @classmethod
    def fromdict(cls, d):
        timestamp = d.get('timestamp')
        if timestamp:
            timestamp = datetime.datetime.strptime(timestamp,
                                                   '%Y-%m-%d %H:%M:%S.%f')

        return cls(d['key'], d['payload'], timestamp=timestamp)

    @staticmethod
    def validate_key(key):
        if not isinstance(key, str):
            msg = "key must be a non string"
            raise TypeError(msg)

        if not key:
            msg = "key can't be an empty string"
            raise ValueError(msg)

        if key[0] == '.' or key[-1] == '.':
            msg = "key can't start or end with a dot"
            raise ValueError(msg)

        if '..' in key:
            msg = "key has a double dot"
            raise ValueError(msg)

        if not re.match(r'^[a-z0-9_\-\.]+$', key):
            msg = "key can only contain [a-z0-9], undercores or dashes"
            raise ValueError(msg)

    @staticmethod
    def validate_timestamp(timestamp):
        if not isinstance(timestamp, datetime.datetime):
            raise TypeError(timestamp, "expected datetime object")

        if timestamp > datetime.datetime.utcnow():
            raise ValueError(timestamp, "timestamp is in the future")

    @staticmethod
    def validate_attachments(attachments):
        if not isinstance(attachments, dict):
            raise TypeError(attachments, "expected dict")

        if not all([x and isinstance(x, str) for x in attachments.keys()]):
            raise TypeError(attachments, "expected dict(str->data)")

        for name in attachments:
            if not name or not isinstance(name, str):
                raise TypeError(attachments, "expected dict(str->IOBase)")

            read_meth = getattr(attachments[name], 'read', None)
            if not callable(read_meth):
                raise TypeError(attachments, "expected dict(str->Readable)")

    def asdict(self):
        return {
            'key': self.key,
            'payload': self.payload,
            'timestamp': str(self.timestamp),
        }

class Message(Packet):
    def __init__(self, key, code, text, fmt, items, timestamp=None, attachments=None):
        payload = {
            'code': code,
            'text': text,
            'format': format,
            'items': items
        }

        super().__init__(key, payload, timestamp, attachments)

    @property
    def code(self):
        return self.payload['code']

    @property
    def text(self):
        return self.payload['text']


import pandas as pd
import random
import gym
from gym import spaces
import numpy as np
import math
#from pathlib import Path
from sklearn.preprocessing import MinMaxScaler, StandardScaler
from collections import deque
import os
from ta.volatility import BollingerBands
from ta.volume import VolumeWeightedAveragePrice
from ta.trend import EMAIndicator, ADXIndicator, SMAIndicator
from ta.momentum import RSIIndicator, StochasticOscillator

# position constant
LONG = 0
SHORT = 1
FLAT = 2

# action constant
BUY = 0
SELL = 1
HOLD = 2

class OhlcvEnv(gym.Env):

    def __init__(self, window_size, path, train=True, show_trade=True):
        self.train= train
        self.show_trade = show_trade
        self.df = path
        print('COMECA AQUI', self.df)
        self.actions = ["LONG", "SHORT", "FLAT"]
        #self.fee = 0.00024
        self.seed()

        self.window_size = window_size

        #self.df.dropna(inplace=True)
        self.closingPrices = self.df['close'].values
        #self.df = self.df[['open','high','low','close','volume', 'vwap', 'ema9', 'ema21', 'ema200','adx','-di', '+di', 'volume_21']].values
        self.df = self.df[['open','high','low','close','volume', 'vwap', 'ema9', 'ema21', 'ema200','adx','-di', '+di', 'volume_21',"close_win",'ema9_win', 'ema21_win']].values
        #self.df = self.df[['open','high','low','close','volume', 'bb_bbh', 'bb_bbl', 'vwap', 'ema9', 'ema21', 'ema200','rsi','adx','-di', '+di', 'volume_21', 'estocastico', 'estocastico_signal']].values
        #self.df = self.df[['open','high','low','close','volume']].values
        self.n_features = self.df.shape[1]
        self.shape = (self.window_size, self.n_features +3)
        # defines action space
        self.action_space = spaces.Discrete(len(self.actions))
        self.observation_space = spaces.Box(low=-np.inf, high=np.inf, shape=self.shape, dtype=np.float32)
        print('second', self.df)

    #    names = ['Data','timestamp','open','high','low','close','volume']
    #    raw_df= pd.read_csv('WDO_15MIN.csv' , header = 0, names = names,  sep = ';', index_col=False)
    #    print(raw_df)    #        #
    #    self.df = raw_df
    #    self.df.dropna(inplace=True) # drops Nan rows
    #    self.closingPrices = self.df['close'].values
    #    indicator_bb = BollingerBands(close=self.df["close"], window=20, window_dev=2, fillna = True)
    #    vwap = VolumeWeightedAveragePrice(high=self.df["high"], low=self.df["low"], close=self.df["close"], volume=self.df["volume"], fillna=True )
    #    ema9 = EMAIndicator(close=self.df["close"], window=9, fillna=True )
    #    ema21 = EMAIndicator(close=self.df["close"], window=21, fillna=True )
    #   ema200 = EMAIndicator(close=self.df["close"], window=200, fillna=True )
    #    rsi = RSIIndicator(close=self.df["close"], fillna=True)
    #    adx = ADXIndicator(high=self.df["high"], low=self.df["low"], close=self.df["close"], window = 8, fillna=True)
    #   volume_21 = SMAIndicator(close=self.df["volume"], window=21, fillna=True )
    #    estocastico = StochasticOscillator(high=self.df["high"], low=self.df["low"], close=self.df["close"], fillna=True)
    #    self.df['bb_bbm'] = indicator_bb.bollinger_mavg()
    #    self.df['bb_bbh'] = indicator_bb.bollinger_hband()
    #    self.df['bb_bbl'] = indicator_bb.bollinger_lband()
    #    self.df['vwap'] = vwap.volume_weighted_average_price()
    #    self.df['ema9'] = ema9.ema_indicator()
    #    self.df['ema21'] = ema9.ema_indicator()
    #    self.df['ema200'] = ema9.ema_indicator()
    #    self.df['rsi'] = rsi.rsi()
    #    self.df['adx'] = adx.adx()
    #    self.df['-di'] = adx.adx_neg()
    #    self.df['+di'] = adx.adx_pos()
    #    self.df['volume_21'] = volume_21.sma_indicator()
    #    self.df['estocastico']  = estocastico.stoch()
    #    self.df['estocastico_signal'] = estocastico.stoch_signal()
    #    print(self.df)
    #    self.df = self.df[['open','high','low','close','volume', 'bb_bbh', 'bb_bbl', 'vwap', 'ema9', 'ema21', 'ema200','rsi','adx','-di', '+di', 'volume_21', 'estocastico', 'estocastico_signal']].values
        #self.df = self.df[['open','high','low','close','volume', 'vwap', 'ema9', 'ema21', 'ema200','rsi', 'volume_21']].values
        #self.df = self.df.values
    #    print('second', self.df)

    def normalize_frame(self, frame):
        offline_scaler = MinMaxScaler()
        observe = frame[..., :-3]
        observe = offline_scaler.fit_transform(observe)
        agent_state = frame[..., -3:]
        temp = np.concatenate((observe, agent_state), axis=1)

        return temp

    def step(self, action):
        s, r, d, i = self._step(action)
        self.state_queue.append(s)
        #print(s)
        return self.normalize_frame(np.concatenate(tuple(self.state_queue))), r, d, i

    def _step(self, action):

        if self.done:
            return self.state, self.reward, self.done, {}
        self.reward = 0
        self.action = HOLD  # hold
        if action == BUY: # buy
            if self.position == FLAT:
                self.position = LONG
                self.action = BUY
                self.entry_price = self.closingPrice
            elif self.position == SHORT:
                self.position = FLAT
                self.action = BUY
                self.exit_price = self.closingPrice
                fee = (self.closingPrice/1000)*0.8
                self.reward += (((self.entry_price - self.exit_price)*10)-fee)/10
                self.krw_balance = self.krw_balance + self.reward
                self.entry_price = 0
                self.n_short += 1
        elif action == 1: #short
            if self.position == FLAT:
                self.position = SHORT
                self.action = 1
                self.entry_price = self.closingPrice
            elif self.position == LONG:
                self.position = FLAT
                self.action = 1
                self.exit_price = self.closingPrice
                fee = (self.closingPrice/1000)*0.8
                self.reward += (((self.exit_price - self.entry_price )*10)-fee)/10
                self.krw_balance = self.krw_balance + self.reward
                self.entry_price = 0
                self.n_long += 1

        if(self.position == LONG):
            fee = (self.closingPrice/1000)*0.8
            temp_reward = (((self.closingPrice - self.entry_price)*10)-fee)/10
            new_portfolio = self.krw_balance + temp_reward

        elif(self.position == SHORT):
            fee = (self.closingPrice/1000)*0.8
            temp_reward = (((self.entry_price - self.closingPrice)*10)-fee)/10
            new_portfolio = self.krw_balance + temp_reward

        else:
            temp_reward = 0
            new_portfolio = self.krw_balance

        self.portfolio = new_portfolio
        self.current_tick += 1
        self.history.append((self.action, self.current_tick, self.closingPrice, self.portfolio, self.reward))
        if(self.show_trade and self.current_tick%25 == 0):
            print("Tick: {0}/ Portfolio (krw-won): {1}".format(self.current_tick, self.portfolio))
            print("Long: {0}/ Short: {1}".format(self.n_long, self.n_short))
            #print(self.history[-1])
            #print(self.krw_balance)

        self.state = self.updateState()
        info = {'portfolio':np.array([self.portfolio]),
                                                    "history":self.history,
                                                    "n_trades":{'long':self.n_long, 'short':self.n_short}}
        if (self.current_tick > (self.df.shape[0]) - self.window_size-1):
            self.done = True
            self.reward = self.get_profit()

        return self.state, self.reward, self.done, info

    def get_profit(self):
        if(self.position == LONG):
            fee = (self.closingPrice/1000)*0.8
            profit = (((self.closingPrice - self.entry_price)*10)-fee)/10

        elif(self.position == SHORT):
            fee = (self.closingPrice/1000)*0.8
            profit = (((self.entry_price - self.closingPrice)*10)-fee)/10

        else:
            profit = 0
        return profit

    def reset(self):

        self.current_tick = 0
        print("START ENV")

        # positions
        self.n_long = 0
        self.n_short = 0

        self.history = []
        self.krw_balance = 0
        self.portfolio = float(self.krw_balance)
        self.profit = 0
        self.closingPrice = self.closingPrices[self.current_tick]

        self.action = HOLD
        self.position = FLAT
        self.done = False

        self.state_queue = deque(maxlen=self.window_size)
        self.state = self.preheat_queue()
        return self.state

    def preheat_queue(self):
        while(len(self.state_queue) < self.window_size):
            rand_action = 2
            s, r, d, i= self._step(rand_action)
            self.state_queue.append(s)
        return self.normalize_frame(np.concatenate(tuple(self.state_queue)))

    def updateState(self):
        def one_hot_encode(x, n_classes):
            return np.eye(n_classes)[x]
        self.closingPrice = float(self.closingPrices[self.current_tick])
        prev_position = self.position
        one_hot_position = one_hot_encode(prev_position,3)
        #state = self.df[self.current_tick]
        state = np.concatenate((self.df[self.current_tick], one_hot_position))
        return state.reshape(1,-1)


from unittest.mock import ANY, patch

import funcy
import pytest
from click.testing import CliRunner

import ballet
from ballet.cli import cli as _cli

@pytest.fixture
def cli():
    runner = CliRunner()
    return funcy.partial(runner.invoke, _cli)

def test_cli_version(cli):
    result = cli('--version')
    assert result.exit_code == 0
    assert ballet.__version__ in result.output

@pytest.mark.parametrize(
    'create_github_repo',
    [True, False],
    ids=['--create-github-repo', '--no-create-github-repo'],
)
@patch('ballet.templating.render_project_template')
def test_quickstart(mock_render, cli, create_github_repo):
    cmd = 'quickstart'
    if create_github_repo:
        cmd += ' --create-github-repo'
    result = cli(cmd)

    # want to do a partial match on kwargs
    # compat: call.kwargs introduced in py3.8
    mock_render.assert_called_once()
    _, kwargs = mock_render.call_args
    assert kwargs['create_github_repo'] == create_github_repo

    assert 'Generating new ballet project' in result.output

@patch('ballet.update.update_project_template')
def test_update_project_template(mock_update, cli):
    result = cli('update-project-template --push')
    mock_update.assert_called_once_with(
        push=True, project_template_path=None)
    assert 'Updating project template' in result.output

@patch('ballet.templating.start_new_feature')
def test_start_new_feature(mock_start, cli):
    result = cli('start-new-feature')
    mock_start.assert_called_once_with(branching=True)
    assert 'Starting new feature' in result.output

@patch('ballet.project.Project.from_path')
@patch('ballet.validation.main.validate')
def test_validate_all(mock_validate, mock_from_path, cli):
    result = cli('validate -A')
    mock_validate.assert_called_once_with(ANY, True, True, True, True)
    assert 'Validating project' in result.output


import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from FunctionsDLCA import *

def plot(lat_size, particles, file_name, probability=0, center=False):
    cluster = np.loadtxt(file_name, delimiter=',')

    x, y, indexes, *_ = np.hsplit(cluster, cluster.shape[1])
    x = x.flatten()
    y = y.flatten()
    indexes = indexes.flatten()

    if center:
        x, y = moveToCenter(lat_size, x, y)

    cividis = cm.get_cmap("cividis", int(np.amax(indexes))+1)

    fig = plt.figure(figsize= (9,9))
    ax = fig.add_subplot(1, 1, 1)

    ax.set_title("DLCA Cluster L = {}, Particles = {} \n Probability = {}".format(lat_size, particles, probability), fontsize = 16)
    rad = 1 / 2
    for xi, yi, index in zip(x, y,indexes):
        circ = plt.Circle((xi,yi), rad, color = cividis(index / (np.amax(indexes) + 1)))
        ax.add_patch(circ)
    ax.set_xlim(0, lat_size)
    ax.set_ylim(0, lat_size)
    ax.set_xlabel("x", fontsize = 16)
    ax.set_ylabel("y", fontsize = 16)
    plt.xticks(fontsize = 16)
    plt.yticks(fontsize = 16)

if __name__ == "__main__":
    lat_size = int(sys.argv[1])
    particles = int(sys.argv[2])

    if '-p' in sys.argv:
        index = sys.argv.index('-p')
        prob = float(sys.argv[index + 1])
    else:
        prob = float(0)

    if '-n' in sys.argv:
        index = sys.argv.index('-n')
        file_name = sys.argv[index + 1]

    elif '-m' in sys.argv:
        file_name = f"Results/ClusterSize{lat_size}Particles{particles}Prob{prob:.6f}.csv"

    elif '-pm' in sys.argv:
        file_name = f"Partial Results/PartialClusterSize{lat_size}Particles{particles}Prob{prob:.6f}.csv"

    elif '-me' in sys.argv:
        file_name = f"Results/EdgeClusterSize{lat_size}Particles{particles}Prob{prob:.6f}.csv"

    elif '-pe' in sys.argv:
        file_name = f"Partial Results/EdgePartialClusterSize{lat_size}Particles{particles}Prob{prob:.6f}.csv"

    else:
        file_name = f"Results/ClusterSize{lat_size}Particles{particles}Prob{prob:.6f}.csv"

    if '-c' in sys.argv:
        center = True
    else:
        center = False

    plot(lat_size, particles, file_name, probability=prob, center=center)

    plt.show()


# -*- coding: utf-8 -*-
"""
test_zmq_push_log_hander.py

"""
import json
import logging
import os
import sys
try:
    import unittest2 as unittest
except ImportError:
    import unittest
import zlib

import zmq

from old_log_inn.zmq_push_log_handler import ZMQPushLogHandler

_test_addresses = ["ipc:///tmp/sockets/test1.socket",
                   "tcp://127.0.0.1:4000",
                   "ipc:///tmp/sockets/test2.socket"]
_poll_timeout = 5 * 1000

class TestZMQPushLogHandler(unittest.TestCase):
    """
    """
    def setUp(self):
        self._zmq_context = zmq.Context()

    def tearDown(self):
        if hasattr(self, "_zmq_context") and self._zmq_context is not None:
            self._zmq_context.term()
            self._zmq_context = None

    def test_single_socket(self):
        """
        test log handler functionality using a single PULL socket
        """
        os.environ["PYTHON_ZMQ_LOG_HANDLER"] = _test_addresses[0]
        log_path = "aaa/bbb/ccc.log"

        # create the handler first so it can do the work of preparing
        # the sockets
        handler = ZMQPushLogHandler(log_path, zmq_context=self._zmq_context)
        logging.root.addHandler(handler)
        logging.root.setLevel(logging.DEBUG)

        # listen on a single PULL socket
        pull_socket = self._zmq_context.socket(zmq.PULL)
        pull_socket.bind(_test_addresses[0])

        poller = zmq.Poller()
        poller.register(pull_socket, zmq.POLLIN)

        log = logging.getLogger("test")
        log.info("pork")

        result_list = poller.poll(timeout=_poll_timeout)
        self.assertEqual(len(result_list), 1)

        compressed_header = pull_socket.recv(zmq.NOBLOCK)
        self.assertTrue(pull_socket.rcvmore)
        compressed_body = pull_socket.recv()

        header = json.loads(zlib.decompress(compressed_header))
        body = zlib.decompress(compressed_body)

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


#!/usr/bin/python

import sys

sys.path.append('/storage/cylin/bin/pipeline/')
import utils
from collections import defaultdict
import numpy
#very quick script to add the BRD4 in degree to the out degree table

projectFolder = '/storage/cylin/grail/projects/rasmc_all/' #will have to set this appropriately for torta or your local

#this is the out degree table from the 161027_rasmc_edge_weights.R script
out_path = '%stables/RN6_RASMC_0_tss_TF_OUT_DEGREE.txt' % (projectFolder)

#this is the enhancer tf assignment from crc
enhancer_path = '%scrc/rasmc_h3k27ac_0_tss/rasmc_h3k27ac_0_tss_ENHANCER_TF_TABLE.txt' % (projectFolder)

#this is the signal table
signal_path = '%ssignalTables/rasmc_h3k27ac_0_tss_BRD4_ENH_TF_IN_DEGREE_signal_table.txt' % (projectFolder)

#this is the expression table
exp_path = '%scufflinks/rasmc_rna_cuffnorm/output/rasmc_rna_exprs_fpkm_means.txt' % (projectFolder)

#this is the edge table path
edge_path = '%scrc/rasmc_h3k27ac_0_tss/rasmc_h3k27ac_0_tss_EDGE_TABLE.txt' % (projectFolder)

#load everybody in
out_table = utils.parseTable(out_path,'\t')

enhancer_table = utils.parseTable(enhancer_path,'\t')

signal_table = utils.parseTable(signal_path,'\t')

exp_table = utils.parseTable(exp_path,'\t')

edge_table = utils.parseTable(edge_path,'\t')

#first make an expression dictionary for each gene
#want each entry to be a list with the avg. exp at [0, 2+PDGF, 24+PDGF]

exp_dict = defaultdict(list)

for line in exp_table[1:]:
    exp_dict[line[0].upper()] = [float(line[1]),float(line[2]),float(line[4])]

#for each tf make a dict to hold AUC and then summarize in degree
#get this from the signal_table
brd4_auc = {'rasmc_unstim':defaultdict(float),
            'rasmc_2h':defaultdict(float),
            'rasmc_24h':defaultdict(float),
            }
for line in signal_table[1:]:

    gene_list = line[0].split(',')
    coords = [int(x) for x in line[1].split(':')[-1].split('-')]
    length = coords[1] - coords[0]
    for gene in gene_list:
        brd4_auc['rasmc_unstim'][gene]+=((float(line[8])+float(line[9]))/2)*length
        brd4_auc['rasmc_2h'][gene]+=((float(line[10])+float(line[11]))/2)*length
        brd4_auc['rasmc_24h'][gene]+=((float(line[12])+float(line[13]))/2)*length

        #sanity check for a gene w/ multiple SEs
        if gene == 'JUND':
            print('JUND')
            print(brd4_auc['rasmc_unstim']['JUND'])
            print(((float(line[8])+float(line[9]))/2)*length)

#sanity check for a pair of TFs that have the same SE
print('genes w/ same SE')
print(brd4_auc['rasmc_unstim']['NR1D1'])
print(brd4_auc['rasmc_unstim']['THRA'])

#now we have AUC for every single tf

#load in the out degree table

#set up the output table
pca_table_median = [['GENE','BRD4_DELTA_IN_2HR','BRD4_DELTA_IN_24HR','BRD4_DELTA_OUT_2HR','BRD4_DELTA_OUT_24HR','EXP_DELTA_IN_2HR','EXP_DELTA_IN_24HR','EXP_DELTA_OUT_2HR','EXP_DELTA_OUT_24HR']]

#get the median for each time point
#getting the median BRD4 signal across all TFs to median scale BRD4 IN Degree
brd4_unstim_median = numpy.median([brd4_auc['rasmc_unstim'][gene] for gene in brd4_auc['rasmc_unstim'].keys()])

brd4_2h_median = numpy.median([brd4_auc['rasmc_2h'][gene] for gene in brd4_auc['rasmc_2h'].keys()])

brd4_24h_median = numpy.median([brd4_auc['rasmc_24h'][gene] for gene in brd4_auc['rasmc_24h'].keys()])

print(brd4_unstim_median,brd4_2h_median,brd4_24h_median)

for line in out_table[1:]:
    gene = line[0]
    #just get the BRD4 out degree (nothing else)
    brd4_out_2 = float(line[2])
    brd4_out_24 = float(line[5])

    #ended up using just median scaled data
    brd4_in_2_median = numpy.log2((brd4_auc['rasmc_2h'][gene]/brd4_2h_median)/(brd4_auc['rasmc_unstim'][gene]/brd4_unstim_median))
    brd4_in_24_median = numpy.log2((brd4_auc['rasmc_24h'][gene]/brd4_24h_median)/(brd4_auc['rasmc_unstim'][gene]/brd4_unstim_median))

    #now getting the expression IN and OUT degree
    exp_in_2 = numpy.log2(exp_dict[gene][1]/exp_dict[gene][0])
    exp_in_24 = numpy.log2(exp_dict[gene][2]/exp_dict[gene][0])

    #for out degree we need to get all target genes
    #get all of the target edges from the edge table
    target_genes = utils.uniquify([line[1] for line in edge_table[1:] if line[0] == gene])

    #max exp values should be > 1
    # sad_genes = [target for target in target_genes if max(exp_dict[target]) < 1]
    # for sadness in sad_genes:
    #     print(exp_dict[sadness])
    # sys.exit()

    #now we can get the vector of log2 fold expression for each one
    #want the average exp change
    exp_out_2 = numpy.mean([numpy.log2(exp_dict[target][1]/exp_dict[target][0]) for target in target_genes])
    exp_out_24 = numpy.mean([numpy.log2(exp_dict[target][2]/exp_dict[target][0]) for target in target_genes])

    #for median_normalized
    signal_line_median = [brd4_in_2_median,brd4_in_24_median,brd4_out_2,brd4_out_24,exp_in_2,exp_in_24,exp_out_2,exp_out_24]
    #rounding
    signal_line_median = [round(x,4) for x in signal_line_median]
    new_line_median = [gene]+signal_line_median
    pca_table_median.append(new_line_median)

# out_path = '%s/tables/161202_rasmc_tf_pca.txt' % (projectFolder)
# utils.unParseTable(pca_table,out_path,'\t')

out_path_median = '%s/tables/rasmc_tf_pca_median.txt' % (projectFolder)
utils.unParseTable(pca_table_median,out_path_median,'\t')


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

import sys
import random

import CardSet as CS

num_players = 5
cs = CS.CardSet(num_players)
i = 0
for crank in cs.cards:
    c = cs.cards[crank]()
    print(str(i) + ": " + str(c))
    i += 1


def Prob1(a):
    mens="hola"
    Menssaj= mens + " "  + a
    return Menssaj

def nomed(nom,edad):
    msj =("Hola " , nom , " tienes " , edad , " años " )

    return msj

def añoNacimiento(aoa,aon):
    return aoa-aon

if __name__ == '__main__':

    #print("Ingresa tu nombre")
    #a=input()
    #print(Prob1(a))

    #print("Ingresa tu Nombre")
    #nom = input()
    #print("Ingresa tu edad")
    #edad=input()
    #print(nomed(nom,edad))

    print("Ingresa tu nombre")
    nom = input()
    print("Ingresa el año actual")
    aoa = input()
    print("Ingresa el año de tu nacimiento")
    aon = input()
    print("Hola" + nom + "Tienes" + añoNacimiento(aoa,aon) + "años")

def funcion3(a: int, b: int, c: str) -> str:  # Asignar como valores de entrada a y b cuyo tipo de dato seran de tipo integer(int) y c será de tipo string(str) y el valor de salida será tipo string(str)
    edad = a - b
    return funcion2(c, edad)

if __name__ == "__main__":
    print("Escribir una funcion que reciba un mensaje y un nombre  escriba en pantalla '<mensaje> <nombre>'")
    print(funcion1("Hola", "Marco"))

    print("Escribir una funcion que reciba el nombre y la edad de una persona. EL mensaje de saluda tiene que ser: 'Hola <nombre> tienes <edad> años'")
    print(funcion2("Marco", 20))

    print("Escribir una funcion que recibe el año actual y el año de nacimiento, usando la funcion anterior enviando esta como argumento obtenga el mensaje: 'Hola <nombre> tienes <edad> años'")
    print(funcion3(2022, 2002, "Marco"))


from tqdm import tqdm
import random
from rdflib import graph
from ontolearn.knowledge_base import KnowledgeBase
from ontolearn.owlapy.render import DLSyntaxObjectRenderer
from ontolearn.refinement_operators import ExpressRefinement
import os
import json

class ConceptDescriptionGenerator:
    """
    Learning problem generator.
    """

    def __init__(self, knowledge_base, refinement_operator, depth=2, max_length=10, num_rand_samples=150):
        self.kb = knowledge_base
        self.rho = refinement_operator
        self.depth = depth
        self.num_rand_samples = num_rand_samples
        self.max_length = max_length

    def apply_rho(self, concept):
        return {ref for ref in self.rho.refine(concept, max_length=self.max_length)}

    def generate(self):
        roots = self.apply_rho(self.kb.generator.thing)
        Refinements = set()
        Refinements.update(roots)
        print("|Thing refinements|: ", len(roots))
        roots_sample = random.sample(list(roots), k=self.num_rand_samples)
        print("Size of sample: ", len(roots_sample))
        for root in tqdm(roots_sample, desc="Refining roots..."):
            Refinements.update(self.apply_rho(root))
        return Refinements

class RDFTriples:
    """The knowledge graph/base is converted into triples of the form: individual_i ---role_j---> concept_k or
    individual_i ---role_j---> individual_k and stored in a txt file for the computation of embeddings."""

    def __init__(self, source_kg_path):
        self.Graph = graph.Graph()
        self.Graph.parse(source_kg_path)
        self.source_kg_path = source_kg_path

    def export_triples(self, export_folder_name='triples'):
        if not os.path.exists(os.path.join(self.source_kg_path[:self.source_kg_path.rfind("/")], export_folder_name)):
            os.mkdir(os.path.join(self.source_kg_path[:self.source_kg_path.rfind("/")], export_folder_name))
        if os.path.isfile(os.path.join(self.source_kg_path[:self.source_kg_path.rfind("/")], export_folder_name,
                                       "train.txt")):
            print("\n*** Embedding triples exist ***\n")
            return
        train_file = open("%s/train.txt" % os.path.join(self.source_kg_path[:self.source_kg_path.rfind("/")],
                                                        export_folder_name), mode="w")
        for s, p, o in self.Graph:
            s = s.expandtabs()[s.expandtabs().rfind("/")+1:]
            p = p.expandtabs()[p.expandtabs().rfind("/")+1:]
            o = o.expandtabs()[o.expandtabs().rfind("/")+1:]
            if s and p and o:
                train_file.write(s+"\t"+p+"\t"+o+"\n")
        train_file.close()
        print("*********************Finished exporting triples*********************\n")

class KB2Data:
    """
    This class takes an owl file, loads it into a knowledge base using ontolearn.knowledge_base.KnowledgeBase.
    A refinement operator is used to generate a large number of concepts, from which we filter and retain the shortest
    non-redundant concepts. Finally, we aim at training a deep neural network to predict the syntax of concepts from
    their instances. Hence, we export each concept and its instances (eventually positive and negative examples) into
    json files.
    """

    def __init__(self, path, rho_name="ExpressRefinement", depth=5, max_child_length=25, refinement_expressivity=0.6,
                 downsample_refinements=True, k=10, num_rand_samples=150, min_num_pos_examples=1):
        self.path = path
        self.dl_syntax_renderer = DLSyntaxObjectRenderer()
        self.kb = KnowledgeBase(path=path)
        self.num_examples = self.find_optimal_number_of_examples()
        self.min_num_pos_examples = min_num_pos_examples
        atomic_concepts = frozenset(self.kb.ontology().classes_in_signature())
        self.atomic_concept_names = frozenset([self.dl_syntax_renderer.render(a) for a in atomic_concepts])
        rho = ExpressRefinement(knowledge_base=self.kb, max_child_length=max_child_length, sample_fillers_count=k,
                                downsample=downsample_refinements, use_inverse=False, use_card_restrictions=False,
                                use_numeric_datatypes=False, use_time_datatypes=False, use_boolean_datatype=False,
                                expressivity=refinement_expressivity)
        self.lp_gen = ConceptDescriptionGenerator(knowledge_base=self.kb, refinement_operator=rho, depth=depth,
                                                  num_rand_samples=num_rand_samples)

    def find_optimal_number_of_examples(self):
        if self.kb.individuals_count() >= 600:
            return min(self.kb.individuals_count()//2, 1000)
        return self.kb.individuals_count()

    def generate_descriptions(self):
        print()
        print("#"*60)
        print("Started generating data on the "+self.path.split("/")[-1].split(".")[0]+" knowledge base")
        print("#"*60)
        print()
        All_individuals = set(self.kb.individuals())
        print("Number of individuals in the knowledge base: {} \n".format(len(All_individuals)))
        Concepts = self.lp_gen.generate()
        non_redundancy_hash_map = dict()
        show_some_length = True
        for concept in tqdm(sorted(Concepts, key=lambda c: self.kb.concept_len(c)), desc="Filtering process..."):
            if not self.kb.individuals_set(concept) in non_redundancy_hash_map and \
                    self.min_num_pos_examples <= self.kb.individuals_count(concept):
                non_redundancy_hash_map[self.kb.individuals_set(concept)] = concept
            else:
                continue
            if self.kb.concept_len(concept) >= 15 and show_some_length:
                print("A long concept found: ", self.kb.concept_len(concept))
                show_some_length = False
        print("Concepts generation done!\n")
        print("Number of atomic concepts: ", len(self.atomic_concept_names))
        print("Longest concept length: ",
              max({l for l in [self.kb.concept_len(c) for c in non_redundancy_hash_map.values()]}), "\n")
        print("Total number of concepts: ", len(non_redundancy_hash_map), "\n")
        self.train_concepts = list(non_redundancy_hash_map.values())
        print("Data generation completed")
        return self

    def sample_examples(self, pos, neg):
        if min(len(pos), len(neg)) >= self.num_examples//2:
            if len(pos) > len(neg):
                num_neg_ex = self.num_examples//2
                num_pos_ex = self.num_examples-num_neg_ex
            else:
                num_pos_ex = self.num_examples//2
                num_neg_ex = self.num_examples-num_pos_ex
        elif len(pos) > len(neg):
            num_neg_ex = len(neg)
            num_pos_ex = self.num_examples-num_neg_ex
        elif len(pos) < len(neg):
            num_pos_ex = len(pos)
            num_neg_ex = self.num_examples-num_pos_ex
        positive = random.sample(pos, min(num_pos_ex, len(pos)))
        negative = random.sample(neg, min(num_neg_ex, len(neg)))
        return positive, negative

    def save_data(self):
        data = dict()
        for concept in tqdm(self.train_concepts, desc="Sample examples and save data..."):
            pos = set(self.kb.individuals(concept))
            neg = set(self.kb.individuals())-pos
            if len(neg) == 0:
                continue
            pos = [ind.get_iri().as_str().split("/")[-1] for ind in pos]
            neg = [ind.get_iri().as_str().split("/")[-1] for ind in neg]
            positive, negative = self.sample_examples(pos, neg)
            concept_name = self.dl_syntax_renderer.render(concept.get_nnf())
            data[concept_name] = {'positive examples': positive, 'negative examples': negative}
        data = list(data.items())
        os.makedirs(f'{self.path[:self.path.rfind("/")]}/training_data/', exist_ok=True)
        with open(f'{self.path[:self.path.rfind("/")]}/training_data/Data.json', 'w') as file_train:
            json.dump(dict(data), file_train, indent=3, ensure_ascii=False)
        print(f'Data saved at {self.path[:self.path.rfind("/")]}')