from flask import  request, jsonify
from flask_cors import CORS, cross_origin
from Module import app, mysql
from Utilities import auths
from Module import venue
import datetime
import uuid

class Association():
    def __init__(self, data):
        if not 'title' in data:
            raise ValueError("Title is required")
        if not 'description' in data:
            raise ValueError("Title is required")
        if not 'type' in data:
            raise ValueError("Assoication type is required")
        self.association_id = uuid.uuid4()
        self.title = data['title']
        self.profile_image = data['profileImage'] if 'profileImage' in data else None
        self.description = data['description']
        self.website = data['website'] if 'website' in data else None
        self.email = data['email'] if 'email' in data else None
        self.introduction = data['introduction'] if 'introduction' in data else None
        self.country_code = data['countryCode'] if 'countryCode' in data else None
        self.phone_no = data['phoneNo'] if 'phoneNo' in data else None
        self.type = data['type']
        self.official = False
        self.approved = False
        self.parent_id = data['parentId'] if 'parentId' in data else None

def insert_association(cur, data):
    new_association = Association(data)
    query_string = "INSERT INTO association ( \
                    `association_id`, \
                    `title`, \
                    `description`, \
                    `profile_image`, \
                    `website`, \
                    `email`, \
                    `country_code`, \
                    `phone_no`, \
                    `introduction`, \
                    `type`, \
                    `official`, \
                    `approved`, \
                    `parent_id` \
                    ) \
                    VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
    result = cur.execute(query_string, (new_association.association_id,
                                        new_association.title,
                                        new_association.description,
                                        new_association.profile_image,
                                        new_association.website,
                                        new_association.email,
                                        new_association.country_code,
                                        new_association.phone_no,
                                        new_association.introduction,
                                        new_association.type,
                                        new_association.official,
                                        new_association.approved,
                                        new_association.parent_id,
    ))
    return new_association.association_id

def insert_approve_request(cur, association_id):
    query_string = "INSERT INTO approve_request ( \
                    `association_id`, \
                    `request_type` \
                    ) \
                    VALUES (%s, %s)"
    cur.execute(query_string, (association_id, 'association'))

def insert_association_role(cur, current_user, association_id):
    user = auths.get_user(current_user)
    query_string = "INSERT INTO association_role ( \
        `sid`,\
        `association_id`, \
        `role`\
        ) VALUES (%s, %s, %s)"
    cur.execute(query_string, (user['sid'], association_id, "Admin"))

def insert_member(cur, current_user, association_id):
    user = auths.get_user(current_user)
    query_string = "INSERT INTO member( \
        `sid`,\
        `association_id` \
        ) VALUES (%s, %s)"
    cur.execute(query_string, (user['sid'], association_id))

def get_manage_role(cur, association_id, sid):
    query_string = "select role \
                         from association_role left join user_private \
                            on association_role.sid=user_private.sid \
                         where association_id=%s \
                            and user_private.sid=%s"
    result = cur.execute(query_string, (association_id, sid))
    if result <= 0 :
        return None
    return cur.fetchone()

# route function
def create_association(current_user, data):
        cur = mysql.connection.cursor()
        association_id = insert_association(cur, data)
        insert_approve_request(cur, association_id)
        insert_association_role(cur, current_user,association_id)
        insert_member(cur, current_user,association_id)

        if not data['venue']['venueId']:
                data['venue']['venueId'] = venue.insert_venue(cur, data['venue'])

        mysql.connection.commit()
        cur.close()
        return association_id

# route function
def get_association(association_id):
    try:
        data = {}
        # default set user as guest
        data['user'] = 'Guest'

        cur = mysql.connection.cursor()
        # find if there is association with this id
        query_string = "select * \
                        from association \
                        where association_id=%s"
        result = cur.execute(query_string, (association_id,))
        # if no such association, return
        if result <= 0:
            return jsonify({"message": "Association not found"}), 404

        data['association'] = cur.fetchone()
        approved = data['association']['approved']
        # check if client is a user
        user = auths.get_user_from_headers()
        if not user:
            # if the association is not approved
            if approved == 0:
                return jsonify({"message": "Association not found"}), 404

            return jsonify(data), 200

        # check if user is a manager of the association
        role = get_manage_role(cur, association_id, user['sid'])
        # if not approved association and user is not a manager
        if approved == 0 and not role :
            return {"message": "Association not found"}, 404
        print(data['association'])
        cur.close()
        if role:
            data['user'] = role['role']
        return jsonify(data), 200
    except Exception as err:
        print(err)
        return jsonify({'message' : str(err)}), 500

# route function
def get_associations_by_user(current_user):
    return_data = []
    try:
        cur = mysql.connection.cursor()
        user = auths.get_user(current_user)
        query_string = "select * \
                        from member left join association \
                            on association.association_id=member.association_id \
                        where sid=%s and approved=true"
        result = cur.execute(query_string, (user['sid'],))
        if result > 0:
            return_data = cur.fetchall()
        cur.close()
        return jsonify(return_data), 200
    except Exception as err:
        print(err)
        return jsonify({'message' : str(err)}), 500

# route function
def edit_association(current_user, association_id):
    try:
        data = request.get_json()
        cur = mysql.connection.cursor()
        new_association = Association(data)
        query_string = "UPDATE association SET \
                        title=%s, \
                        description=%s, \
                        profile_image=%s, \
                        website=%s, \
                        email=%s, \
                        country_code=%s, \
                        phone_no=%s, \
                        introduction=%s, \
                        type=%s WHERE \
                        association_id=%s"
        result = cur.execute(query_string, (new_association.title,
                                            new_association.description,
                                            new_association.profile_image,
                                            new_association.website,
                                            new_association.email,
                                            new_association.country_code,
                                            new_association.phone_no,
                                            new_association.introduction,
                                            new_association.type,
                                            association_id,
        ))
        mysql.connection.commit()
        cur.close()
        return jsonify({'association_id': association_id}), 201
    except ValueError as err:
        return jsonify({"inputError": str(err) }), 400
    except Exception as err:
        print(err)
        return jsonify({'internalError': 'Please try again later'}), 500

# route function
def add_member(current_user):
    try:
        data = request.get_json()
        cur = mysql.connection.cursor()
        user = auths.get_user(current_user)
        success_list = []
        fail_list = []

        # check if user is a manager of this association
        role = get_manage_role(cur, data['associationId'], user['sid'])
        if not role:
            return {"message": "Unauthorized"}, 401
        for sid in data['sIDs']:
            if cur.execute('select * from member where sid=%s and association_id=%s', (user['sid'],data['associationId'])):
                fail_list.append(sid)
            query_string = "insert into member ( \
                            `sid`, \
                            `association_id` \
                            ) \
                            values (%s, %s)"
            cur.execute(query_string, (sid, data['associationId']))
            success_list.append(sid)
        mysql.connection.commit()
        cur.close()

        return jsonify({"success": success_list}), 200

    except Exception as err:
        print(err)
        return jsonify({'message' : str(err)}), 500

# route function
def add_manager(current_user):
    try:
        data = request.get_json()
        cur = mysql.connection.cursor()
        user = auths.get_user(current_user)
        success_list = []
        fail_list = []

        # check if user is a admin of this association
        role = get_manage_role(cur, data['associationId'], user['sid'])

        if not role or role['role'] != 'Admin':
            return {"message": "Unauthorized"}, 404
        for user in data['users']:
            # skip if user is already a manager
            if cur.execute('select * from association_role where sid=%s and association_id=%s', (user['sid'],data['associationId'])):
                fail_list.append(user['sid'])
                continue
            query_string = "insert into association_role ( \
                            `sid`, \
                            `association_id`, \
                            `role` \
                            ) \
                            values (%s, %s, %s)"
            cur.execute(query_string, (user['sid'], data['associationId'], user['role']))
            success_list.append(user['sid'])
        mysql.connection.commit()
        cur.close()

        return jsonify({"success": success_list, "fail": fail_list}), 200

    except Exception as err:
        print(err)
        return jsonify({'message' : str(err)}), 500


import cv2
import numpy as np
from hough_circles_acc import *
from hough_peaks import *

def find_circles(edge_img, radius_range=[1,2], threshold=100, nhood_size=10):
    n = radius_range[1] - radius_range[0]
    H_size = (n,) + edge_img.shape
    H = np.zeros(H_size, dtype=np.uint)
    centers = ()
    radii = np.arange(radius_range[0], radius_range[1])
    valid_radii = np.array([], dtype=np.uint)
    num_circles = 0
    for i in range(len(radii)):
        H[i] = hough_circles_acc(edge_img, radii[i])
        peaks = hough_peaks(H[i], numpeaks=10, threshold=threshold,
                            nhood_size=nhood_size)
        if peaks.shape[0]:
            valid_radii = np.append(valid_radii, radii[i])
            centers = centers + (peaks,)
            for peak in peaks:
                cv2.circle(edge_img, tuple(peak[::-1]), radii[i]+1, (0,0,0), -1)
        #  cv2.imshow('image', edge_img); cv2.waitKey(0); cv2.destroyAllWindows()
        num_circles += peaks.shape[0]
        print('Progress: %d%% - Circles: %d\033[F\r'%(100*i/len(radii), num_circles))
    print('Circles detected: %d          '%(num_circles))
    centers = np.array(centers)
    return centers, valid_radii.astype(np.uint)


import json

from argparse import ArgumentParser
from flask import Flask, request, make_response
from flask_cors import CORS
from chatAPIs.gptAPI import ChatGPTAPI
from chatAPIs.speechAPI import recogwav, speechtts
from chatAPIs.xfscoreAPI import speechscore

app = Flask(__name__)
app.config.from_object(__name__)
CORS(app, resources=r'/*')

cet4Content = """I want you to act as a spoken English teacher and improver. I will speak to you in English and you
will reply to me in English to practice my spoken English.You should use vocabulary no higher than CET-4. I want you
to keep your reply neat, limiting the reply to 100 words. I want you to strictly correct my grammar mistakes, typos,
and factual errors. I want you to ask me a question in your reply. Now let's start practicing, you could ask me a
question first. Remember, I want you to strictly correct my grammar mistakes, typos, and factual errors.Reply only in
English and within 100 words.The first sentence should welcome the user and give a topic guide"""

cet6Content = """I want you to act as a spoken English teacher and improver. I will speak to you in English and you
will reply to me in English to practice my spoken English.You should use vocabulary no higher than CET-6. I want you
to keep your reply neat, limiting the reply to 100 words. I want you to strictly correct my grammar mistakes, typos,
and factual errors. I want you to ask me a question in your reply. Now let's start practicing, you could ask me a
question first. Remember, I want you to strictly correct my grammar mistakes, typos, and factual errors.Reply only in
English and within 100 words.The first sentence should welcome the user and give a topic guide"""

IELTSContent = """I want you to be an oral English examiner. I will speak to you in English.
The speaking test is divided into three parts, each described as follows:
In Part 1, you'll start by asking general questions about familiar topics such as work, family, studies, and hobbies.
This section should contain three questions.You should ask one question, and after I answer this question, you can ask
me next question.
In Part 2, you give the user a topic. The topic format should follow the following two examples:
example1 for part2:
Describe a (piece of) (good) law in your country.
You should say:
· what the law is
· how you first learned about this law
· who benefits from this law (or, who is affected by this law)
and explain why you think this is a good law.
example2 for part2:
Describe an interesting historic site (in your country) that you visited.
You should say:
· where it was
· what you saw at this site (or, what it looked like)
· what role it played in history
and explain what interested you about that place
I need to speak on the subject.
In Part 3 of the speaking test, you will have a two-way discussion with me. Please ask questions related to the
questions in Part 2.
I want you to keep your questions concise and coherent, and limit them to 50 words or less.
All replies should be in English. The first sentence should welcome the user and give a topic guide.
Now start part1.You should ask me questions one by one."""

MESSAGES = [{"role": "user",
             "content": ""}]
IS_INIT = False

@app.route('/')
def hello_world():  # put application's code here
    return 'Hello World!'

@app.route('/getmsg', methods=['GET', 'POST'])
def getmsg():
    if request.method == 'POST':
        global MESSAGES, IS_INIT
        chatgpt = ChatGPTAPI()
        if request.args.get('isinit') == "false":
            MESSAGES = []
            print("init")
            msg = None
            print('level:' + request.args.get('level'))
            if request.args.get('level') == "0":
                MESSAGES.append({"role": "user",
                                 "content": cet4Content})
            elif request.args.get('level') == "1":
                MESSAGES.append({"role": "user",
                                 "content": cet6Content})
            elif request.args.get('level') == "2":
                MESSAGES.append({"role": "user",
                                 "content": IELTSContent})
            print(MESSAGES[0])
        else:
            data = request.data
            jData = json.loads(data)
            uMsg = jData['userMsg']
            print("msg: " + uMsg)
            MESSAGES.append({"role": "user", "content": uMsg})
        MESSAGES, completion = chatgpt.chat(MESSAGES)
        gpt_res = completion.choices[0].message.content
        audio_res = speechtts(gpt_res).decode()
        res = make_response('', 200, )
        res.data = json.dumps({'response': gpt_res, 'audio': audio_res})
        return res
    return make_response('', 402, )

@app.route('/getmsgreco', methods=['GET', 'POST'])
def getmsgreco():
    if request.method == 'POST':
        if request.args.get('isinit') == "true":
            print("wav recognizing...")
            user_wav = request.files['file']
            user_wav.save('static/' + user_wav.filename)
            msg = recogwav(user_wav.filename)
            print("msg: " + msg)
            score = speechscore('static/' + user_wav.filename, msg)
            res = make_response('', 200, )
            res.data = json.dumps({'userMsg': msg, 'score': score})
            return res
    return make_response('', 402, )

if __name__ == '__main__':
    parser = ArgumentParser()  # 创建一个参数接收的解释器，由此对象（这里是：parser)来负责解释参数信息
    parser.add_argument('-p', '--port', default=5000, type=int, help='port to listen on')
    args = parser.parse_args()  # 通过parse_args()方法尝试对收到的参数关键字进行解释
    port = args.port  # 从args对象中取出其中的参数关键字--port 参数的内容，也可能是获取到预设的默认值
    app.run(host='0.0.0.0', port=port)


import geopandas as gp
from sys import argv
from os import path, makedirs
import shutil
import csv

if __name__ == '__main__':
    if len(argv) < 2:
        print("Usage: python transform.py <raw_shp_file_path>")
        exit(-1)

    origin = argv[1][:-4]
    dest = f"gen_students_data/{argv[1][16:-4]}"
    print(f"Converting:\n\t`{origin}`\n  to output in\n\t`{dest}`\n")

    dest_path = dest.split("/")
    student_folders = ''.join([dest_path[0], "/", dest_path[1]])

    # Create needed student folders.
    print("[1/3]  Creating student folders and copying files..")
    if not path.exists(student_folders):
        makedirs(student_folders)

    # Copy .shp and .shx files.
    shutil.copy(origin + ".shp", dest + ".shp")
    shutil.copy(origin + ".shx", dest + ".shx")

    # Load files and compute neighbours.
    print("[2/3]  Loading geodata and computing neighbours..")
    geo_df = gp.read_file(f"{argv[1]}")
    shapes = geo_df.geometry.tolist()
    neighbours = [[] for _ in range(len(shapes))]

    for i, shape in enumerate(shapes):
        for j, other in enumerate(shapes):
            if i != j and shape.intersects(other):
                neighbours[i].append(j)

    # Write created neighbour list to csv.
    print("[3/3]  Writing neighbours to file..")
    with open(student_folders + "/" + dest_path[2] + ".csv", "w") as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(["Id", "Neighbours"])

        for i, n_list in enumerate(neighbours):
            writer.writerow([i, str(n_list)])


from datetime import datetime

from flask import abort, make_response

def get_timestamp():
    return datetime.now().strftime('%Y-%m-%d %H:%M:%S')

USER = {
    "1000": {
        "user_name": "Caio Rolando",
        "user_id": "1000",
        "timestamp": get_timestamp()
    },
    "1001": {
        "user_name": "Gabriel Lima",
        "user_id": "1001",
        "timestamp": get_timestamp()
    },
    "1002": {
        "user_name": "Ednaldo Pereira",
        "user_id": "1002",
        "timestamp": get_timestamp()
    }
}

def read_all():
    return list(USER.values())

def read_one(user_id):
    if user_id in USER:
        return USER[user_id]
    abort(404, f"User not found with id {user_id}")

def create(user):
    if not user["user_id"] in USER:
        USER[user["user_id"]] = {
            "user_name": user["user_name"],
            "user_id": user["user_id"],
            "timestamp": get_timestamp()
        }
    return USER[user["user_id"]]


import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

from tensorflow.keras.optimizers import Adam
from tensorflow.keras.layers import Input
from tensorflow.keras.models import Model

from ..CGAN import *

class CGAN():
    def __init__(self, image_size, num_classes, latent_dim, generator, discriminator):
        # Input shape
        self.img_rows, self.cols, self.channels = image_size
        self.img_shape = image_size
        self.num_classes = num_classes
        self.latent_dim = latent_dim

        optimizer = Adam(0.0002, 0.5)

        # Build and compile the discriminator
        self.discriminator = build_discriminator(self.img_shape, self.num_classes)

        #self.discriminator.build(input_shape= [(None, *self.img_shape), (None, 1,)])

        self.discriminator.compile(loss=['binary_crossentropy'],
            optimizer=optimizer,
            metrics=['accuracy'])

        # Build the generator
        self.generator = build_generator(latent_dim= self.latent_dim, num_classes= self.num_classes)
        #self.generator.build(input_shape= [(None, self.latent_dim), (None, 1,)])

        # The generator takes noise and the target label as input
        # and generates the corresponding class of that label
        noise = Input(shape=(self.latent_dim,))
        label = Input(shape=(1,))
        img = self.generator([noise, label])

        # For the combined model we will only train the generator
        self.discriminator.trainable = False

        # The discriminator takes generated image as input and determines validity
        # and the label of that image
        valid = self.discriminator([img, label])

        # The combined model  (stacked generator and discriminator)
        # Trains generator to fool discriminator
        self.combined = Model([noise, label], valid)
        self.combined.compile(loss=['binary_crossentropy'],
            optimizer=optimizer)

    def train(self, epochs, dataset, batch_size=128, sample_interval=50):
        D_loss, G_loss, acc = [], [], []
        # Load iterator dataset assuming DataLoader with one batch (full data)
        X_train, y_train = next(iter(dataset))

        # Adversarial ground truths
        valid = np.ones((batch_size, 1))
        fake = np.zeros((batch_size, 1))

        for epoch in range(epochs):

            # ---------------------
            #  Train Discriminator
            # ---------------------

            # Select a random half batch of images
            idx = np.random.randint(0, X_train.shape[0], batch_size)
            imgs, labels = X_train[idx], y_train[idx]

            # Sample noise as generator input
            noise = np.random.normal(0, 1, (batch_size, self.latent_dim))

            # Generate a half batch of new images
            gen_imgs = self.generator.predict([noise, labels])

            # Train the discriminator
            d_loss_real = self.discriminator.train_on_batch([imgs, labels], valid)
            d_loss_fake = self.discriminator.train_on_batch([gen_imgs, labels], fake)
            d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)

            # ---------------------
            #  Train Generator
            # ---------------------

            # Condition on labels
            sampled_labels = np.random.randint(0, self.num_classes, batch_size).reshape(-1, 1)

            # Train the generator
            g_loss = self.combined.train_on_batch([noise, sampled_labels], valid)

            # Plot the progress
            D_loss.append(d_loss[0])
            G_loss.append(g_loss)
            acc.append(100*d_loss[1])
            print ("%d [D loss: %f, acc.: %.2f%%] [G loss: %f]" % (epoch, d_loss[0], 100*d_loss[1], g_loss))

            # If at save interval => save generated image samples
            if epoch % sample_interval == 0:
                self.sample_images(epoch)

    def sample_images(self, epoch):
        r, c = 2, 4
        noise = np.random.normal(0, 1, (r * c, self.latent_dim))
        sampled_labels = np.array([[0], [0], [0], [0], [1], [1], [1], [1]]).reshape(-1, 1)

        gen_imgs = self.generator.predict([noise, sampled_labels])

        # Rescale images 0 - 1
        gen_imgs = gen_imgs

        fig, axs = plt.subplots(r, c)
        for i in range(r):
            if i == 0:
                cnt = 0
                title= "Cow"
            elif i == 1:
                cnt = 4
                title= "Horse"
            for j in range(c):
                axs[i, j].imshow(np.asarray(gen_imgs[cnt,:,:, :]* 127.5 + 127.5, dtype= np.uint8))
                axs[i, j].set_title(title)
                axs[i, j].axis('off')
                cnt += 1
        plt.show()
        plt.close()


from context import ROOT_DIR
from sklearn.preprocessing import StandardScaler

import numpy     as np

import csv
import dataUtils
import entityUtils
import os
import random

### EVALUATION ####

def detected(output):
	return np.sum((output > 0.5).astype(float))

def positive(labels):
	return np.sum(labels)

def true_positive(output, labels):
	return np.sum((output + labels > 1.5).astype(float))

def precision(output, labels):
	return true_positive(output, labels)/detected(output)

def recall(output, labels):
	return true_positive(output, labels)/positive(labels)

def f_measure(output, labels):
	p = precision(output, labels)
	r = recall(output, labels)

	return 2*p*r/(p+r)

### UTILS ###

def shuffle(instances, labels):
	assert len(instances) == len(labels), 'instances and labels must have the same number of elements'

	idx = range(len(instances))
	random.shuffle(idx)

	x = np.array([instances[i] for i in idx])
	y = np.array([labels[i] for i in idx])

	return x, y

def split(instances, labels, nb_batch):
	assert len(instances) == len(labels), 'instances and labels must have the same number of elements'

	len_batch = len(instances)//nb_batch
	sections  = [(i+1)*len_batch for i in range(nb_batch-1)]

	return np.split(instances, sections), np.split(labels, sections)

def get_nb_metric(antipattern):
	if antipattern == 'god_class':
		return 7

	if antipattern == 'feature_envy':
		return 13

###   GETTERS   ###

def get_labels(systemName, antipattern):
	entities = dataUtils.getEntities(systemName, antipattern)
	if antipattern == 'feature_envy':
		entities = map(lambda x: entityUtils.normalizeMethodName(x.split(';')[0]) + ';' + x.split(';')[1], entities)

	antipatterns = dataUtils.getAntipatterns(systemName, antipattern)

	labels = []
	for entity in entities:
		if entity in antipatterns:
			labels.append([1.])
		else:
			labels.append([0.])

	return np.array(labels)

def get_instances(systemName, antipattern):
	metricFile = os.path.join(ROOT_DIR, 'data', 'metric_files', systemName, antipattern, 'commit_0.csv')

	with open(metricFile, 'rb') as csvfile:
		reader = csv.DictReader(csvfile, delimiter=';')
		instances =  np.array([[float(row[key]) for key in row if (key != 'Class') & (key != 'Method')] for row in reader])

		# Normalization
		scaler = StandardScaler()
		scaler.fit(instances)
		return scaler.transform(instances)

def get_came_instances(systemName, antipattern, history_length):
	entities = dataUtils.getEntities(systemName, antipattern)
	instances = np.zeros((len(entities), history_length, get_nb_metric(antipattern)))
	for j in range(history_length):
		metricFile = os.path.join(ROOT_DIR, 'data', 'metric_files', systemName, antipattern, 'commit_' + str(j) + '.csv')
		if os.path.exists(metricFile):
			with open(metricFile, 'rb') as csvfile:
				reader = csv.DictReader(csvfile, delimiter=';')
				for i, row in enumerate(reader):
					k = 0
					for key in reader.fieldnames:
						if (key != 'Class') & (key != 'Method'):
							instances[i, j, k] = float(row[key])
							k += 1
		else:
			break

	# Normalization
	scaler = StandardScaler()
	scaler.fit(instances[:,0,:])

	for i in range(len(entities)):
		instances[i, :, :] = scaler.transform(instances[i, :, :])

	return instances


import logging
from penvy.env.EnvConfig import EnvConfig
from penvy.PenvyConfig import PenvyConfig
from benvy.databricks.repos.project_root_resolver import resolve_project_root

class BootstrapConfig(EnvConfig):
    def get_parameters(self) -> dict:
        poetry_version = PenvyConfig().get_parameters()["poetry"]["install_version"]

        return {
            "project": {
                "dir": resolve_project_root(),
            },
            "poetry": {
                "version": poetry_version,
                "home": "/root/.poetry",
                "executable": "/root/.poetry/bin/poetry",
                "archive_url": f"https://github.com/python-poetry/poetry/releases/download/{poetry_version}/{self.__get_archive_filename(poetry_version)}",
                "install_script_url": f"https://raw.githubusercontent.com/python-poetry/poetry/{poetry_version}/get-poetry.py",
                "archive_path": f"/dbfs/FileStore/jars/daipe/poetry/{self.__get_archive_filename(poetry_version)}",
                "install_script_path": "/dbfs/FileStore/jars/daipe/poetry/get-poetry.py",
            },
            "logger": {
                "name": "daipe-bootstrap",
                "level": logging.INFO,
            },
        }

    def __get_archive_filename(self, version: str) -> str:
        return f"poetry-{version}-linux.tar.gz" if version < "1.2.0" else f"poetry-{version}.tar.gz"


from django.core.management.commands.compilemessages import Command as CompileMessagesCommand

class Command(CompileMessagesCommand):
    program_options = [option for option in CompileMessagesCommand.program_options if option != '--check-format']

    def add_arguments(self, parser):
        super().add_arguments(parser)
        parser.add_argument(
            '--check-format', action='store_true', help='Enable message format checking',
        )

    def handle(self, **options):
        if options['check_format']:
            self.program_options = self.program_options + ['--check-format']
        return super().handle(**options)


from django.urls import path, include

from . import views

app_name = 'users'

urlpatterns = [
    path('register/', views.register, name='register'),
    path('users/', views.users_list, name='users_list'),
    path('users/follow/<int:pk>/', views.follow_to_user, name='follow_to_user'),
    path('users/unfollow/<int:pk>/', views.unfollow_from_user, name='unfollow_from_user'),
    path('users/block/<username>/', views.block_user, name='block_user'),
    path('users/unblock/<username>/', views.unblock_user, name='unblock_user'),
    path('users/followed/', views.followed_to_list, name='followed_to_list'),
    path('users/followers/', views.followers_list, name='followers_list'),
    path('users/<username>/', views.user_detail, name='user_detail'),
    path('', include('django.contrib.auth.urls')),
]


from simpletransformers.question_answering import QuestionAnsweringModel

model = QuestionAnsweringModel('bert', 'qa_models/',  use_cuda=False)

class QA():
    def __init__(self, model):
        self.model_qa = model
    def get_answer_from_text(self, text, question):
        to_predict = [
            {'context': text,
            'qas': [{'question': question, 'id': '0'}]}]
        return self.model_qa.predict(to_predict)[0]['answer']

qaClass = QA(model)
# qaClass.get_answer_from_text("Od 1929 Wisława Szymborska mieszkała w Krakowie, gdzie debiutowała w 1945 na łamach „Dziennika Polskiego” wierszem Szukam słowa",  'Jakim wierszem debiutowała Szymborska?')


from drf_yasg.utils import swagger_auto_schema
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView

from restapi.models.volume import Volume
from restapi.serializers.volume_serializer import VolumeSerializer
from utils.sound_manager import SoundManager

class VolumeView(APIView):
    @swagger_auto_schema(
        responses={
            '200': VolumeSerializer(),
        },
        operation_id='Get volume',
        operation_description='Get current volume'
    )
    def get(self, request):
        """
        Get the volume status
        """
        volume = Volume()
        serializer = VolumeSerializer(volume)

        return Response(serializer.data)

    @swagger_auto_schema(
        responses={
            '200': VolumeSerializer(),
            '400': "Bad request"
        },
        operation_id='Set volume',
        operation_description='Set new volume level'
    )
    def post(self, request):
        serializer = VolumeSerializer(data=request.data)

        if serializer.is_valid():
            SoundManager.set_volume(serializer.validated_data["volume"])
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


import os, sys
from pathlib import Path

instructions_dir = Path(__file__).parent.parent.resolve()
if str(instructions_dir) not in sys.path: sys.path.append(str(instructions_dir))
os.chdir(instructions_dir)

import streamlit as st

import st_dependencies
st_dependencies.styling()

import platform
is_local = (platform.processor() != "")

from pages.sec0_prereqs import (
    sec00_home,
    sec01_prereqs,
    sec02_einops,
)

import streamlit_antd_components as sac

with st.sidebar:
    st.markdown('')

    CHAPTER = sac.steps([
        sac.StepsItem(title='Home'),
        sac.StepsItem(title='Core Concepts / Knowledge'),
        sac.StepsItem(title='Einops, Einsum & tensors'),
    ], size='small', return_index=True)

    function = [
        sec00_home.section,
        sec01_prereqs.section,
        sec02_einops.section,
    ][CHAPTER]

function()


import hashlib
import json
import os
import pickle
from collections import defaultdict
from os import path
from typing import List, Dict
import math

import numpy as np
import torch
from numpy.random import RandomState

from .parsing.common import PathsLoader
from .parsing.utils import load_id_storage, Tokenizer, save_id_storage

def build_vocabulary(graphs_src: str, dicts_src: str, dst: str,
                     subtokens_bound: int, types_bound: int):
    tokens_dict = load_id_storage(path.join(dicts_src, "node_tokens.csv"))
    types_dict = load_id_storage(path.join(dicts_src, "var_types.csv"))
    edges_dict = load_id_storage(path.join(dicts_src, "edge_types.csv"))

    tokens_cnt = np.zeros(len(tokens_dict), dtype=np.int32)
    types_cnt = np.zeros(len(types_dict), dtype=np.int32)

    for relative_path, filename, abs_path in PathsLoader(graphs_src, file_pattern=r'.*\.pkl').load():
        with open(abs_path, 'rb') as f:
            nodes_tokens, nodes_types, _ = pickle.load(f)
            for i in nodes_tokens:
                tokens_cnt[i] += 1
            for i in nodes_types:
                types_cnt[i] += 1

    subtokens_index = defaultdict(int)
    tokenizer = Tokenizer()
    for key, value in tokens_dict.items():
        for subtoken in tokenizer.tokenize(key):
            subtoken = subtoken
            if subtoken:
                subtokens_index[subtoken] += tokens_cnt[value]

    subtokens_dict = {'': 0}
    subtokens_cnt = np.zeros(len(subtokens_index) + 1, dtype=np.int32)
    for subtoken in sorted(subtokens_index, key=subtokens_index.get, reverse=True):
        subtokens_cnt[len(subtokens_dict)] = subtokens_index[subtoken]
        subtokens_dict[subtoken] = len(subtokens_dict)

    subtokens_vocab = __filter_dict(subtokens_dict, subtokens_cnt, subtokens_bound)
    types_vocab = __filter_dict(types_dict, types_cnt, types_bound)
    os.makedirs(dst, exist_ok=True)
    save_id_storage(subtokens_vocab, path.join(dst, "subtokens.csv"))
    save_id_storage(types_vocab, path.join(dst, "node_types.csv"))
    save_id_storage(edges_dict, path.join(dst, "edges_types.csv"))
    __save_counts(tokens_dict, tokens_cnt, path.join(dst, "tokens_counts.csv"))
    __save_counts(types_dict, types_cnt, path.join(dst, "types_counts.csv"))
    __save_counts(subtokens_dict, subtokens_cnt, path.join(dst, "subtokens_counts.csv"))

def __load_labels(src, name):
    labels = {}
    with open(path.join(src, f'{name}_metrics.csv'), 'r', encoding='utf-8') as labels_file:
        labels_file.readline()
        for line in labels_file:
            sample_id, _, values = line.split(',', maxsplit=2)
            values = list(map(float, values.split(',')))
            if any(math.isnan(x) or math.isinf(x) for x in values):
                print('Skip')
                continue
            labels[sample_id] = torch.as_tensor(values, dtype=torch.float)
    return labels

def prepare_data(graphs_src: str, labels_src: str, dicts_src: str, dst: str,
                 n_subtokens=3, seed=0):
    subtokens_dict = load_id_storage(path.join(dicts_src, "subtokens.csv"))
    types_dict = load_id_storage(path.join(dicts_src, "node_types.csv"))
    edges_dict = load_id_storage(path.join(dicts_src, "edges_types.csv"))

    old_tokens_dict = load_id_storage(path.join(graphs_src, "node_tokens.csv"))
    tokens_mappings = __get_mappings(old_tokens_dict, subtokens_dict, n_subtokens)

    old_types_dict = load_id_storage(path.join(graphs_src, "var_types.csv"))
    types_mappings = __get_single_mappings(old_types_dict, types_dict)

    train_labels = __load_labels(labels_src, 'train')
    validation_labels = __load_labels(labels_src, 'val')
    test_labels = __load_labels(labels_src, 'test')

    __prepare_data(graphs_src, dst, 'test', tokens_mappings, types_mappings,
                   len(edges_dict), test_labels)
    __prepare_data(graphs_src, dst, 'val', tokens_mappings, types_mappings,
                   len(edges_dict), validation_labels)
    __prepare_data(graphs_src, dst, 'train', tokens_mappings, types_mappings,
                   len(edges_dict), train_labels)

def __save_counts(id_storage, counts, dst):
    with open(dst, 'w', encoding='utf-8') as file:
        file.write(f"token_id,count,token\n")
        for token, token_id in id_storage.items():
            file.write(f"{token_id},{counts[token_id]},\"{token}\"\n")

def __filter_dict(id_storage, counts, bound):
    result = {'': 0}
    for token, token_id in id_storage.items():
        if token and counts[token_id] >= bound:
            result[token] = len(result)
    return result

def __prepare_data(src, dst, name,
                   tokens_mappings, types_mappings, n_edges,
                   labels):
    id_list = []
    tokens_list = []
    var_types_list = []
    indexes_list = []
    tags_list = []
    for folder, _, files in os.walk(path.join(src, name)):
        for filename in files:
            sample_id = filename[:-4]
            if sample_id not in labels:
                continue
            tokens, types, indexes = __load_graph(path.join(folder, filename),
                                                  tokens_mappings, types_mappings, n_edges)
            id_list.append(path.join(folder, filename))
            tokens_list.append(tokens)
            var_types_list.append(types)
            indexes_list.append(indexes)
            tags_list.append(labels[sample_id])
            if indexes.max().item() >= 20 * len(tokens):
                print("ALARM!")
    batch_dst = os.path.join(dst, f"{name}")
    os.makedirs(batch_dst)
    torch.save(id_list, path.join(batch_dst, "ids.pkl"))
    torch.save(tokens_list, path.join(batch_dst, "tokens.pkl"))
    torch.save(var_types_list, path.join(batch_dst, "types.pkl"))
    torch.save(indexes_list, path.join(batch_dst, "indexes.pkl"))
    torch.save(torch.stack(tags_list),
               path.join(batch_dst, "labels.pkl"))

def __load_graph(src, tokens_mappings, types_mappings, n_edges):
    with open(src, 'rb') as file:
        old_tokens, old_types, edges_sparse = pickle.load(file)
        from_edge, to_edge, edge_type = edges_sparse
        return (
            torch.as_tensor(tokens_mappings[old_tokens], dtype=torch.long),
            torch.as_tensor(types_mappings[old_types], dtype=torch.long),
            torch.as_tensor([to_edge, from_edge.astype(np.int64) * n_edges + edge_type.astype(np.int64)],
                            dtype=torch.long)
        )

def __get_single_mappings(old_types: Dict[str, int], new_types: Dict[str, int]):
    types_mappings = np.zeros(len(old_types), dtype=np.int32)
    print(types_mappings.shape)
    for token, token_id in old_types.items():
        types_mappings[token_id] = new_types.get(token, 0)
    return types_mappings

def __get_mappings(old_tokens: Dict[str, int], new_subtokens: Dict[str, int], n_subtokens: int):
    tokens_mappings = np.zeros((len(old_tokens), n_subtokens), dtype=np.int32)
    print(tokens_mappings.shape)
    tokenizer = Tokenizer()
    for token, token_id in old_tokens.items():
        ptr = 0
        for subtoken in tokenizer.tokenize(token):
            new_id = new_subtokens.get(subtoken, 0)
            if new_id != 0:
                tokens_mappings[token_id][ptr] = new_id
                ptr += 1
                if ptr == n_subtokens:
                    break
    return tokens_mappings

def __tensor_from_sparse_indexes(sparse_list, n):
    values, indexes = sparse_list
    buffer = np.zeros(n, dtype=np.int64)
    buffer[indexes] = values
    return torch.as_tensor(buffer, dtype=torch.long)

def __select_batch(relative_path, files: List[str], seed, batch_id, n_batches):
    files.sort()
    local_seed = int(hashlib.sha1(relative_path.encode('utf-8')).hexdigest(), 16) % (2 ** 32)
    batch_ids = RandomState(seed ^ local_seed).randint(0, n_batches, len(files), dtype=np.int)
    return [file for file, i in zip(files, batch_ids) if i == batch_id]

def __produce_embeddings(src, dst, tokens_mappings, types_mappings, n_edges,
                         labels, solutions_limit, model, seed, device='cuda'):
    model.to(device)
    model.eval()
    id_list = []
    vectors_list = []
    sizes_list = []
    tags_list = []
    for folder, _, files in os.walk(src):
        relative_path = os.path.relpath(folder, src)
        if relative_path not in labels:
            continue
        tokens_list = []
        var_types_list = []
        indexes_list = []
        for filename in files:
            tokens, types, indexes = __load_graph(path.join(folder, filename),
                                                  tokens_mappings, types_mappings, n_edges)
            tokens_list.append(tokens)
            var_types_list.append(types)
            indexes_list.append(indexes)
        vectors = torch.zeros((solutions_limit, 128), dtype=torch.float)
        with torch.no_grad():
            for batch_id in range(0, 10):
                from_ind = 100 * batch_id
                to_ind = min(from_ind + 100, len(files))
                if to_ind <= from_ind:
                    break
                matrix, tokens, mask, types, heads = combine(indexes_list[from_ind:to_ind],
                                                             tokens_list[from_ind:to_ind],
                                                             var_types_list[from_ind:to_ind], 22)
                vectors[from_ind:to_ind] = model(types.to(device), tokens.to(device),
                                                 mask.to(device), matrix.to(device)).cpu()[heads, :]
        id_list.append(relative_path)
        tags_list.append(labels[relative_path])
        vectors_list.append(vectors)
        sizes_list.append(len(files))
        print(relative_path)
    os.makedirs(path.dirname(dst), exist_ok=True)
    torch.save((id_list,
                torch.stack(vectors_list),
                torch.as_tensor(tags_list, dtype=torch.float),
                torch.as_tensor(sizes_list)), dst)


'''
EXERCICIO DE PROBLEMA INICIAL
u'(t) = 2u(t)
u(0) = 1
'''

import matplotlib.pyplot as plt
import numpy as np

def f(t, u):
    u = 2*u
    return u

def ext(t):
    u = np.exp(2*t)
    return u

u_n = 1
t_n = 0

u = []
t = []
u_ext = []

h = 0.001

for i in range (0, 2000):
    t.append(t_n)
    u.append(u_n)
    u_ext.append(ext(t_n))
    u_n += f(t_n, u_n) * h
    t_n += h

lbl1 = "método de Euler"
lbl2 = "solução analítica"

plt.plot(t, u_ext, label= lbl2)
plt.plot(t, u, label= lbl1)
plt.legend()
plt.show()


import json
from typing import Any

from PySide6.QtCore import QObject, Signal, Slot
from PySide6.QtNetwork import QNetworkReply

class CMSReply(QObject):
    """
    Represents a CMS reply object.
    Custom QObject class to handle network replies and emit signals with parsed data.
    """

    finished = Signal(
        dict
    )  # Signal to be emitted when the reply is finished, with the parsed data as an argument

    def __init__(self, reply: QNetworkReply, parent) -> None:
        super().__init__(parent)
        self.reply: QNetworkReply = reply
        self.reply.finished.connect(self.response_serializer)

    @Slot()
    def response_serializer(self) -> None:
        """
        Slot method to handle the network reply and parse the JSON data.

        This method checks if there was an error in the network reply.
        If there is no error, it reads the raw data from the network reply, decodes it as UTF-8, and parses it as JSON.
        If the parsed data is a list, it converts it to a dictionary using the 'id' field as the key.
        Finally, it emits the 'finished' signal with the parsed data and deletes the network reply.

        Returns
        -------
        None
        """

        # Check if there was an error in the network reply
        if self.reply.error() != QNetworkReply.NetworkError.NoError:
            print(f"Error: {self.reply.errorString()}")

        # Read the raw data from the network reply and decode it as UTF-8 as parse as JSON
        raw_data: str = self.reply.readAll().data().decode("utf-8")
        self.data = self.parse_json(raw_data)

        # If the parsed data is a list, convert it to a dictionary using the 'id' field as the key
        if isinstance(self.data, list):
            self.data = {item.get("id"): item for item in self.data}

        self.finished.emit(self.data)
        self.reply.deleteLater()

    @staticmethod
    def parse_json(data: str) -> dict[Any, Any]:
        """
        Parses JSON data.

        Args
        ----
        data : str
            The JSON data to be parsed.

        Returns
        -------
        dict
            The parsed JSON data as a dictionary. If the JSON data cannot be decoded, an empty dictionary is returned.
        """

        try:
            parsed_json: dict = json.loads(data)
        except json.decoder.JSONDecodeError:
            parsed_json = dict()
        return parsed_json


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf
import tensorflow_probability as tfp
from trfl import gen_distribution_ops

def l2_project(support, weights, new_support):
  """Projects distribution (support, weights) onto new_support.

  Args:
    support: Tensor defining support of a categorical distribution(s). Must be
      of rank 1 or of the same rank as `weights`. The size of the last dimension
      has to match that of `weights`.
    weights: Tensor defining weights on the support points.
    new_support: Tensor holding positions of a new support.
  Returns:
    Projection of (support, weights) onto the new_support.
  """
  return gen_distribution_ops.project_distribution(
      support, weights, new_support, 1)

def hard_cumulative_project(support, weights, new_support, reverse):
  """Produces a cumulative categorical distribution on a new support.

  Args:
    support: Tensor defining support of a categorical distribution(s). Must be
      of rank 1 or of the same rank as `weights`. The size of the last dimension
      has to match that of `weights`.
    weights: Tensor defining weights on the support points.
    new_support: Tensor holding positions of a new support.
    reverse: Whether to evalute cumulative from the left or right.
  Returns:
    Cumulative distribution on the supplied support.
    The foolowing invariant is maintained across the last dimension:
    result[i] = (sum_j weights[j] for all j where support[j] < new_support[i])
                if reverse == False else
                (sum_j weights[j] for all j where support[j] > new_support[i])
  """
  return gen_distribution_ops.project_distribution(
      support, weights, new_support, 3 if reverse else 2)

def factorised_kl_gaussian(dist1_mean,
                           dist1_covariance_or_scale,
                           dist2_mean,
                           dist2_covariance_or_scale,
                           both_diagonal=False):
  """Compute the KL divergence KL(dist1, dist2) between two Gaussians.

  The KL is factorised into two terms - `kl_mean` and `kl_cov`. This
  factorisation is specific to multivariate gaussian distributions and arises
  from its analytic form.
  Specifically, if we assume two multivariate Gaussian distributions with rank
  k and means, M1 and M2 and variance S1 and S2, the analytic KL can be written
  out as:

  D_KL(N0 || N1) = 0.5 * (tr(inv(S1) * S0) + ln(det(S1)/det(S0)) - k +
                         (M1 - M0).T * inv(S1) * (M1 - M0))

  The terms on the first row correspond to the covariance factor and the terms
  on the second row correspond to the mean factor in the factorized KL.
  These terms can thus be used to independently control how much the mean and
  covariance between the two gaussians can vary.

  This implementation ensures that gradient flow is equivalent to calling
  `tfp.distributions.kl_divergence` once.

  More details on the equation can be found here:
  https://stats.stackexchange.com/questions/60680/kl-divergence-between-two-multivariate-gaussians

  Args:
    dist1_mean: The mean of the first Multivariate Gaussian distribution.
    dist1_covariance_or_scale: The covariance or scale of the first Multivariate
      Gaussian distribution. In cases where *both* distributions are Gaussians
      with diagonal covariance matrices (for instance, if both are instances of
      `tfp.distributions.MultivariateNormalDiag`), then the `scale` can be
      passed in instead and the `both_diagonal` flag must be set to `True`.
      A more efficient sparse computation path is used in this case. For all
      other cases, the full covariance matrix must be passed in.
    dist2_mean: The mean of the second Multivariate Gaussian distribution.
    dist2_covariance_or_scale: The covariance or scale tensor of the second
      Multivariate Gaussian distribution, as for `dist1_covariance_or_scale`.
    both_diagonal: A `bool` indicating that both dist1 and dist2 are diagonal
      matrices. A more efficient sparse computation is used in this case.

  Returns:
    A tuple consisting of (`kl_mean`, `kl_cov`) which correspond to the mean and
    the covariance factorisation of the KL.
  """
  if both_diagonal:
    dist1_mean_rank = dist1_mean.get_shape().ndims
    dist1_covariance_or_scale.get_shape().assert_has_rank(dist1_mean_rank)
    dist2_mean_rank = dist2_mean.get_shape().ndims
    dist2_covariance_or_scale.get_shape().assert_has_rank(dist2_mean_rank)

    dist_type = tfp.distributions.MultivariateNormalDiag
  else:
    dist_type = tfp.distributions.MultivariateNormalFullCovariance

  # Recreate the distributions but with stop gradients on the mean and cov.
  dist1_stop_grad_mean = dist_type(
      tf.stop_gradient(dist1_mean), dist1_covariance_or_scale)
  dist2 = dist_type(dist2_mean, dist2_covariance_or_scale)

  # Now create a third distribution with the mean of dist1 and the variance of
  # dist2 and appropriate stop_gradients.
  dist3 = dist_type(dist1_mean, dist2_covariance_or_scale)
  dist3_stop_grad_mean = dist_type(
      tf.stop_gradient(dist1_mean), dist2_covariance_or_scale)

  # Finally get the two components of the KL between dist1 and dist2
  # using dist3
  kl_mean = tfp.distributions.kl_divergence(dist3, dist2)
  kl_cov = tfp.distributions.kl_divergence(dist1_stop_grad_mean,
                                           dist3_stop_grad_mean)
  return kl_mean, kl_cov


import logging
from scripts.get_cfg import config
from scripts.base_path import LOGS_FILE_PATH

class Logger(object):

    def __init__(self):
        self.log = logging.getLogger(config.get_value('log', 'loger_name'))  # 日志解释器对象
        self.log.setLevel(config.get_value('log', 'loger_level'))  # 日志解释器日志等级

        # 日志输出方式
        console_log = logging.StreamHandler()  # 控制台输出
        file_log = logging.FileHandler(LOGS_FILE_PATH, encoding='utf-8')  # 文件输出

        # 日志输出格式
        simple_log = logging.Formatter(config.get_value('log', 'simple'))
        more_log = logging.Formatter(config.get_value('log', 'more'))

        console_log.setFormatter(simple_log)
        file_log.setFormatter(more_log)

        # 输出等级
        console_log.setLevel(config.get_value('log', 'console_level'))
        file_log.setLevel(config.get_value('log', 'file_level'))

        self.log.addHandler(console_log)
        self.log.addHandler(file_log)

    def get_log(self):
        return self.log

loger = Logger().get_log()


import cv2
from skimage import io
from matplotlib import pyplot as plt

# Read image
img=cv2.imread("cat.jpg")
text='I am not a SMELLY cat'
font=cv2.FONT_HERSHEY_SCRIPT_SIMPLEX
line=cv2.FILLED

# cv2.putText(image,text,position,fontType,fontScale,colorOfText,thicknessOfText,LineType)

cv2.putText(img,text,org=(10,30),fontFace=font,fontScale=0.7,color=(255,23,87),thickness=1,lineType=line)

io.imshow(img)
plt.title("Text on Image")
plt.show()


from __future__ import absolute_import, print_function, unicode_literals

__metaclass__ = type

from datetime import (
    datetime,
    timedelta,
    )

import pytz
from zope.component import getUtility
from zope.security.proxy import removeSecurityProxy

from lp.buildmaster.interfaces.builder import IBuilderSet
from lp.registry.interfaces.person import IPersonSet
from lp.soyuz.tests.test_publishing import SoyuzTestPublisher
from lp.testing import (
    person_logged_in,
    TestCaseWithFactory,
    )
from lp.testing.layers import LaunchpadFunctionalLayer
from lp.testing.sampledata import (
    ADMIN_EMAIL,
    BOB_THE_BUILDER_NAME,
    )

class TestBuildStartEstimation(TestCaseWithFactory):

    layer = LaunchpadFunctionalLayer

    def setUp(self):
        super(TestBuildStartEstimation, self).setUp()
        self.admin = getUtility(IPersonSet).getByEmail(ADMIN_EMAIL)
        with person_logged_in(self.admin):
            self.publisher = SoyuzTestPublisher()
            self.publisher.prepareBreezyAutotest()
            for buildd in getUtility(IBuilderSet):
                buildd.builderok = True
        self.distroseries = self.factory.makeDistroSeries()
        self.bob = getUtility(IBuilderSet).getByName(BOB_THE_BUILDER_NAME)
        das = self.factory.makeDistroArchSeries(
            distroseries=self.distroseries, processor=self.bob.processor,
            architecturetag='i386')
        with person_logged_in(self.admin):
            self.distroseries.nominatedarchindep = das
        self.publisher.addFakeChroots(distroseries=self.distroseries)

    def job_start_estimate(self, build):
        return build.buildqueue_record.getEstimatedJobStartTime()

    def test_estimation(self):
        pkg = self.publisher.getPubSource(
            sourcename=self.factory.getUniqueString(),
            distroseries=self.distroseries)
        build = pkg.createMissingBuilds()[0]
        now = datetime.now(pytz.UTC)
        estimate = self.job_start_estimate(build)
        self.assertTrue(estimate > now)

    def test_disabled_archives(self):
        pkg1 = self.publisher.getPubSource(
            sourcename=self.factory.getUniqueString(),
            distroseries=self.distroseries)
        [build1] = pkg1.createMissingBuilds()
        build1.buildqueue_record.lastscore = 1000
        # No user-serviceable parts inside
        removeSecurityProxy(build1.buildqueue_record).estimated_duration = (
            timedelta(minutes=10))
        pkg2 = self.publisher.getPubSource(
            sourcename=self.factory.getUniqueString(),
            distroseries=self.distroseries)
        [build2] = pkg2.createMissingBuilds()
        build2.buildqueue_record.lastscore = 100
        now = datetime.now(pytz.UTC)
        # Since build1 is higher priority, it's estimated dispatch time is now
        estimate = self.job_start_estimate(build1)
        self.assertEqual(5, (estimate - now).seconds)
        # And build2 is next, so must take build1's duration into account
        estimate = self.job_start_estimate(build2)
        self.assertEqual(600, (estimate - now).seconds)
        # If we disable build1's archive, build2 is next
        with person_logged_in(self.admin):
            build1.archive.disable()
        estimate = self.job_start_estimate(build2)
        self.assertEqual(5, (estimate - now).seconds)


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

import os
from django.conf.urls.defaults import *
from django.views.generic.list_detail import object_list as _object_list
from django.views.decorators.vary import vary_on_cookie
from compounddb.models import LibraryHeader
from django.views.decorators.cache import cache_page

# Uncomment the next two lines to enable the admin:

from django.contrib import admin
admin.autodiscover()

root = os.path.dirname(__file__)

@cache_page(60 * 60)
@vary_on_cookie
def object_list(*args, **kwargs):
    return _object_list(*args, **kwargs)

all_libraries = \
    LibraryHeader.objects.extra(select={'count': 'select count(*) from compounddb_compound_library where library_id=(select compounddb_library.id from compounddb_library where compounddb_library.header_id = compounddb_libraryheader.id order by compounddb_library.created_time desc limit 1)'
                                })

urlpatterns = patterns(  # Example:
                         # (r'^cmngtest/', include('cmngtest.foo.urls')),
                         # Uncomment the admin/doc line below and add 'django.contrib.admindocs'
                         # to INSTALLED_APPS to enable admin documentation:
                         # (r'^admin/doc/', include('django.contrib.admindocs.urls')),
                         # Uncomment the next line to enable the admin:
                         # (r'^admin/(.*)', admin.site.root),
                         # (r'^static/(?P<path>.*)', 'django.views.static.serve',
                         # ....{'document_root': root + '/static/'}),
    '',
    url(r'^annotation/$', 'compounddb.views.annotation_search',
        name='annotation-search'),
    url(r'^structure/$', 'compounddb.views.structure_search',
        name='structure-search'),
    url(r'^(?P<library>[,a-zA-Z0-9 _-]+)/(:(?P<page>[0-9]+))?$',
        'compounddb.views.library_content', name='library_content'),
    url(r'^(?P<library>[,a-zA-Z0-9 _-]+)/(?P<cid>[a-zA-Z0-9 _-]+)/png',
        'compounddb.views.compound_image', name='compound_image'),
    url(r'^(?P<library>[,a-zA-Z0-9 _-]+)/(?P<cid>[a-zA-Z0-9 _-]+)/(?P<resource>\w*)$'
        , 'compounddb.views.compound_detail', name='compound_detail'),
    url(r'^$', object_list, dict(queryset=all_libraries),
        name='compound_libraries'),
    url(r'^_ajax$', 'compounddb.views.ajax', name='ajax'),
    )


"""
RadiationSource.py

Author: Jordan Mirocha
Affiliation: University of Colorado at Boulder
Created on 2011-01-28.

Description: Radiation source class, contains all functions needed to calculate the spectrum
of an object.  Stole from rt1d, with slight modification

"""

import re, h5py
import numpy as np
from .Constants import *
from scipy.integrate import quad
from .ComputeCrossSections import PhotoIonizationCrossSection as sigma_E

try:
    from mpi4py import MPI
    rank = MPI.COMM_WORLD.rank
    size = MPI.COMM_WORLD.size
except ImportError:
    rank = 0
    size = 1

np.seterr(all = 'ignore')   # exp overflow occurs when integrating BB - will return 0 as it should for x large

SchaererTable = {
                "Mass": [5, 9, 15, 25, 40, 60, 80, 120, 200, 300, 400, 500, 1000],
                "Temperature": [4.44, 4.622, 4.759, 4.85, 4.9, 4.943, 4.97, 4.981, 4.999, 5.007, 5.028, 5.029, 5.026],
                "Luminosity": [2.87, 3.709, 4.324, 4.89, 5.42, 5.715, 5.947, 6.243, 6.574, 6.819, 6.984, 7.106, 7.444]
                }

small_number = 1e-3
big_number = 1e5
ls = ['-', '--', ':', '-.']
E_th = [13.6, 24.6, 54.4]

"""
SourceType = 0  (monochromatic)     Just need DiscreteSpectrum and PhotonLuminosity
SourceType = 1  (star)              Need temperature, PhotonLuminosity
SourceType = 2  (popIII star)       Need mass
SourceType = 3  (BH)                Need Mass, epsilon

SpectrumType = 0 (monochromatic)
SpectrumType = 1 (blackbody)
SpectrumType = 2 (blackbody, but temperature and luminosity from Schaerer)
SpectrumType = 3 (multi-color disk)
SpectrumType = 4 (simple power-law)
"""

def RadiationSource(pf):
    if pf['spectrum_file'] is not None:
        return RadiationSourceUserDefined(pf)
    elif pf['spectrum_func'] is not None:
        return pf['spectrum_func']
    else:
        return RadiationSourceModel(pf)

class RadiationSourceModel(object):
    def __init__(self, pf):
        self.pf = pf

        self.SpectrumPars = listify(pf)

        self.N = len(self.SpectrumPars['type'])

        # Cast types to int to avoid indexing complaints
        self.SpectrumPars['Type'] = \
            [int(elem) for elem in self.SpectrumPars['type']]

        self.Emin = min(self.SpectrumPars['Emin'])
        self.Emax = min(self.SpectrumPars['Emax'])

        if self.N == 1:
            self.Type = self.SpectrumPars['type'][0]

        self.last_renormalized = 0

        # SourceType 0, 1, 2
        self.Lph = pf['source_Qdot']

        # SourceType = 1, 2
        self.T = pf['source_temperature']

        # SourceType >= 3
        self.M = self.M0 = pf['source_mass']
        self.epsilon = pf['source_epsilon']

        if 3 in self.SpectrumPars['Type']:
            self.r_in = self.DiskInnermostRadius(self.M0)
            self.r_out = pf['source_Rmax'] * self.GravitationalRadius(self.M0)
            self.fcol = self.SpectrumPars['fcol'][self.SpectrumPars['type'].index(3)]
            self.T_in = self.DiskInnermostTemperature(self.M0)
            self.T_out = self.DiskTemperature(self.M0, self.r_out)

        # Number of ionizing photons per cm^2 of surface area for BB of temperature self.T.
        # Use to solve for stellar radius (which we need to get Lbol).  The factor of pi gets rid of the / sr units
        if pf['source_type'] in [1, 2]:
            self.LphNorm = np.pi * 2. * (k_B * self.T)**3 * \
                quad(lambda x: x**2 / (np.exp(x) - 1.),
                13.6 * erg_per_ev / k_B / self.T, big_number, epsrel = 1e-12)[0] / h**3 / c**2
            self.R = np.sqrt(self.Lph / 4. / np.pi / self.LphNorm)
            self.Lbol = 4. * np.pi * self.R**2 * sigma_SB * self.T**4
        else:
            self.Lbol = self.BolometricLuminosity(0.0)

        # Normalize spectrum
        self.LuminosityNormalizations = self.NormalizeSpectrumComponents(0.0)

    def GravitationalRadius(self, M):
        """
        Half the Schwartzchild radius.
        """
        return G * M * g_per_msun / c**2

    def SchwartzchildRadius(self, M):
        return 2. * self.GravitationalRadius(M)

    def MassAccretionRate(self, M = None):
        return self.BolometricLuminosity(0, M = M) / self.epsilon / c**2

    def DiskInnermostRadius(self, M):
        """
        Inner radius of disk.  Unless SourceISCO > 0, will be set to the
        inner-most stable circular orbit for a BH of mass M.
        """
        if not self.pf['source_isco']:
            return 6. * self.GravitationalRadius(M)
        else:
            return self.pf['source_isco']

    def DiskInnermostTemperature(self, M):
        """
        Temperature (in Kelvin) at inner edge of the disk.
        """
        return (3. * G * M * g_per_msun * self.MassAccretionRate(M) / \
            8. / np.pi / self.DiskInnermostRadius(M)**3 / sigma_SB)**0.25

    def DiskTemperature(self, M, r):
        return ((3. * G * M * g_per_msun * self.MassAccretionRate(M) / \
            8. / np.pi / r**3 / sigma_SB) * \
            (1. - (self.DiskInnermostRadius(M) / r)**0.5))**0.25

    def BlackHoleMass(self, t):
        """
        Compute black hole mass after t (seconds) have elapsed.  Relies on
        initial mass self.M, and (constant) radiaitive efficiency self.epsilon.
        """

        return self.M0 * np.exp(((1.0 - self.epsilon) / self.epsilon) * t / t_edd)

    def Intensity(self, E, i, Type, t):
        """
        Return quantity *proportional* to fraction of bolometric luminosity emitted
        at photon energy E.  Normalization handled separately.
        """

        if Type == 0:
            Lnu = self.F[0]
        elif Type in [1, 2]:
            Lnu = self.BlackBody(E)
        elif Type == 3:
            Lnu = self.MultiColorDisk(E, i, Type, t)
        elif Type == 4:
            Lnu = self.PowerLaw(E, i, Type, t)
        else:
            Lnu = 0.0

        if self.SpectrumPars['logN'][i] > 0:
            return Lnu * np.exp(-10**self.SpectrumPars['logN'][i] * (sigma_E(E, 0) + y * sigma_E(E, 1)))
        else:
            return Lnu

    def Spectrum(self, E, t=0.0, only=None):
        """
        Return fraction of bolometric luminosity emitted at energy E.
        """

        if type(E) in [float, np.float64]:
            E = [E]

        # Renormalize if t > 0
        if t != self.last_renormalized:
            self.last_renormalized = t
            self.M = self.BlackHoleMass(t)
            self.r_in = self.DiskInnermostRadius(self.M)
            self.r_out = self.pf['souce_Rmax'] * self.GravitationalRadius(self.M)
            self.T_in = self.DiskInnermostTemperature(self.M)
            self.T_out = self.DiskTemperature(self.M, self.r_out)
            self.Lbol = self.BolometricLuminosity(t)
            self.LuminosityNormalizations = self.NormalizeSpectrumComponents(t)

        emission = np.zeros_like(E)
        for h, bin in enumerate(E):
            for i, Type in enumerate(self.SpectrumPars['type']):
                if not (self.SpectrumPars['Emin'][i] <= E[h] <= self.SpectrumPars['Emax'][i]):
                    continue

                if only is not None and Type != only:
                    continue

                emission[h] += self.LuminosityNormalizations[i] * \
                    self.Intensity(E[h], i, Type, t) / self.Lbol

        return emission

    def BlackBody(self, E, T = None):
        """
        Returns specific intensity of blackbody at self.T.
        """

        if T is None:
            T = self.T

        nu = E * erg_per_ev / h
        return 2.0 * h * nu**3 / c**2 / (np.exp(h * nu / k_B / T) - 1.0)

    def PowerLaw(self, E, i, Type, t = 0.0):
        """
        A simple power law X-ray spectrum - this is proportional to the *energy* emitted
        at E, not the number of photons.
        """

        return E**-self.SpectrumPars.PowerLawIndex[i]

    def MultiColorDisk(self, E, i, Type, t = 0.0):
        """
        Soft component of accretion disk spectra.
        """

        # If t > 0, re-compute mass, inner radius, and inner temperature
        if t > 0 and self.pf['source_time_evolution'] > 0 and t != self.last_renormalized:
            self.M = self.BlackHoleMass(t)
            self.r_in = self.DiskInnermostRadius(self.M)
            self.r_out = self.pf['source_Rmax'] * self.GravitationalRadius(self.M)
            self.T_in = self.DiskInnermostTemperature(self.M)
            self.T_out = self.DiskTemperature(self.M, self.r_out)

        integrand = lambda T: (T / self.T_in)**(-11. / 3.) * self.BlackBody(E, T) / self.T_in
        return quad(integrand, self.T_out, self.T_in)[0]

    def NormalizeSpectrumComponents(self, t = 0):
        """
        Normalize each component of spectrum to some fraction of the bolometric luminosity.
        """

        Lbol = self.BolometricLuminosity(t)

        normalizations = np.zeros(self.N)
        for i, component in enumerate(self.SpectrumPars['Type']):
            integral, err = quad(self.Intensity, self.SpectrumPars['EminNorm'][i],
                self.SpectrumPars['EmaxNorm'][i], args = (i, component, t,))
            normalizations[i] = self.SpectrumPars['fraction'][i] * Lbol / integral

        return normalizations

    def BolometricLuminosity(self, t = 0.0, M = None):
        """
        Returns the bolometric luminosity of a source in units of erg/s.  For accreting black holes, the
        bolometric luminosity will increase with time, hence the optional 't' argument.
        """

        if self.pf['source_type'] == 1:
            return self.Lbol

        if self.pf['source_type'] == 2:
            return 10**SchaererTable["Luminosity"][SchaererTable["Mass"].index(self.M)] * lsun

        if self.pf['source_type'] > 2:
            Mnow = self.BlackHoleMass(t)
            if M is not None:
                Mnow = M
            return self.epsilon * 4.0 * np.pi * G * Mnow * g_per_msun * m_p * c / sigma_T

    def PlotSpectrum(self, color = 'k', components = True, t = 0, normalized = True,
        bins = 100, mp = None):
        import pylab as pl

        if not normalized:
            Lbol = self.BolometricLuminosity(t)
        else:
            Lbol = 1

        E = np.logspace(np.log10(min(self.SpectrumPars['EminNorm'])),
            np.log10(max(self.SpectrumPars['EmaxNorm'])), bins)
        F = []

        for energy in E:
            F.append(self.Spectrum(energy, t = t))

        if components and self.N > 1:
            EE = []
            FF = []
            for i, component in enumerate(self.SpectrumPars['type']):
                tmpE = np.logspace(np.log10(self.SpectrumPars['Emin'][i]),
                    np.log10(self.SpectrumPars['Emax'][i]), bins)
                tmpF = []
                for energy in tmpE:
                    tmpF.append(self.Spectrum(energy, t = t, only = component))

                EE.append(tmpE)
                FF.append(tmpF)

        if mp is None:
            self.ax = pl.subplot(111)
        else:
            self.ax = mp

        self.ax.loglog(E, np.array(F) * Lbol, color = color, ls = ls[0])

        if components and self.N > 1:
            for i in range(self.N):
                self.ax.loglog(EE[i], np.array(FF[i]) * Lbol, color = color, ls = ls[i + 1])

        self.ax.set_ylim(1e-3 * np.max(F), 1.1 * np.max(F))
        self.ax.set_xlabel(r'$h\nu \ (\mathrm{eV})$')

        if normalized:
            self.ax.set_ylabel(r'$L_{\nu} / L_{\mathrm{bol}}$')
        else:
            self.ax.set_ylabel(r'$L_{\nu}$')

        pl.draw()

class RadiationSourceUserDefined(object):
    def __init__(self, pf):
        self.pf = pf
        self.fn = pf['spectrum_file']

        self.initialize()

    def initialize(self):
        """
        Read data from file.  Should be two columns: energy (eV), and the
        specific luminosity (erg/s/eV) in each bin.  If hdf5, datasets should
        be called 'E' and 'L_E'.  If ASCII, names don't matter, but E should
        be first column, L_E the second.
        """

        # Can actually just supply as arrays too.
        if type(self.fn) in [tuple, list, np.array]:
            self.E, self.L_E = self.fn
        elif re.search('hdf5', self.fn) or re.search('h5', self.fn):
            f = h5py.File(self.fn)
            self.E = f['E'].value
            self.L_E = f['L_E'].value

            #if len(self.L_E) > 1:
            #    self.t = self.Age = f['time_yr'].value * s_per_yr
            #    self.Nt = len(self.t)
            #    i = self.get_time_index(self.pf['source_age'] * s_per_myr)
            #    self.L_E = self.L_E[i]

            f.close()
            if rank == 0:
                print("Read {}.".format(self.fn))

        else:
            self.E, self.L_E = np.loadtxt(self.fn, unpack=True)
            if rank == 0:
                print("Read {}.".format(self.fn))

        self.Emin = max(np.min(self.E), E_th[0])
        self.Emax = np.max(self.E)

        # Threshold indices
        self.i_Eth = np.zeros(3, dtype=int)
        for i, energy in enumerate(E_th):
            loc = np.argmin(np.abs(energy - self.E))

            if self.E[loc] < energy:
                loc += 1

            self.i_Eth[i] = int(loc)

        self.Lbol = self.BolometricLuminosity()

    def Spectrum(self, E=None, t=0.0):
        """
        Return (normalized) specific luminosity.
        """

        return self.L_E / self.Lbol

    def Intensity(self, E = None):
        return self.L_E

    def BolometricLuminosity(self, t=0.0):
        return np.trapz(self.L_E, self.E)

    def get_time_index(self, t):
        i = np.argmin(np.abs(t - self.t))
        return max(min(i, self.Nt - 2), 0)

def listify(pf):
    """
    Turn any Spectrum parameter into a list, if it isn't already.
    """

    if type(pf['spectrum_type']) is not list:
        ntypes = 1
    else:
        ntypes = len(pf['spectrum_type'])

    Spectrum = {}
    for par in pf.keys():
        if par[0:8] != 'spectrum':
            continue

        new_name = par[9:]
        if type(pf[par]) is not list:
            Spectrum[new_name] = [pf[par]]
        else:
            Spectrum[new_name] = pf[par]

    return Spectrum


import torch.nn as nn
class Conv1dNet(nn.Module):
    def __init__(self, num_classes):
        super(Conv1dNet, self).__init__()
        self.conv1 = nn.Conv1d(1, 8, kernel_size=15, stride=2, padding=7, bias=False)
        self.bn1 = nn.BatchNorm1d(8)
        self.relu1 = nn.ReLU(inplace=True)
        self.maxpool1 = nn.MaxPool1d(kernel_size=3, stride=2, padding=1)

        # self.dp1 = nn.Dropout(p=0.2)

        self.conv2 = nn.Conv1d(8, 16, kernel_size=15, stride=2, padding=7, bias=False)
        self.bn2 = nn.BatchNorm1d(16)
        self.relu2 = nn.ReLU(inplace=True)
        self.maxpool2 = nn.MaxPool1d(kernel_size=3, stride=2, padding=1)

        # self.dp2 = nn.Dropout(p=0.2)

        self.conv3 = nn.Conv1d(16, 32, kernel_size=15, stride=2, padding=7, bias=False)
        self.bn3 = nn.BatchNorm1d(32)
        self.relu3 = nn.ReLU(inplace=True)
        self.maxpool3 = nn.MaxPool1d(kernel_size=3, stride=2, padding=1)

        # self.dp3 = nn.Dropout(p=0.1)

        self.avgpool = nn.AdaptiveAvgPool1d(1)
        self.fc = nn.Linear(32, num_classes)

    def forward(self, x):
        # print("Original",x.shape)
        x = self.conv1(x)
        # print("conv1", x.shape)
        x = self.bn1(x)
        # print("bn1",x.shape)
        x = self.relu1(x)
        # print("relu1",x.shape)
        x = self.maxpool1(x)
        # print("maxpool1",x.shape)

        # x = self.dp1(x)

        x = self.conv2(x)
        # print("conv2", x.shape)
        x = self.bn2(x)
        # print("bn2", x.shape)
        x = self.relu2(x)
        # print("relu2", x.shape)
        x = self.maxpool2(x)
        # print("maxpool2", x.shape)

        # x = self.dp2(x)

        x = self.conv3(x)
        # print("conv3", x.shape)
        x = self.bn3(x)
        # print("bn3", x.shape)
        x = self.relu3(x)
        # print("relu3", x.shape)
        x = self.maxpool3(x)
        # print("maxpool3", x.shape)
        # x = self.dp3(x)

        x = self.avgpool(x)
        # print("avgpool", x.shape)
        x = x.view(x.size(0), -1)
        # print("view", x.shape)
        x = self.fc(x)
        # print("fc", x.shape)
        return x


import asyncio
from collections.abc import Mapping
from typing import Optional

from tensorflow_federated.proto.v0 import computation_pb2 as pb
from tensorflow_federated.python.core.impl.computation import computation_base
from tensorflow_federated.python.core.impl.executors import cardinality_carrying_base
from tensorflow_federated.python.core.impl.executors import ingestable_base
from tensorflow_federated.python.core.impl.types import computation_types
from tensorflow_federated.python.core.impl.types import placements
from tensorflow_federated.python.core.impl.types import type_serialization

def CreateDataDescriptor(arg_uris: list[str], arg_type: computation_types.Type):
  """Constructs a `DataDescriptor` instance targeting a `tff.DataBackend`.

  Args:
    arg_uris: List of URIs compatible with the data backend embedded in the
      given `tff.framework.SyncExecutionContext`.
    arg_type: The type of data referenced by the URIs. An instance of
      `tff.Type`.

  Returns:
    Instance of `DataDescriptor`
  """
  arg_type_proto = type_serialization.serialize_type(arg_type)
  args = [
      pb.Computation(data=pb.Data(uri=uri), type=arg_type_proto)
      for uri in arg_uris
  ]
  return DataDescriptor(
      None,
      args,
      computation_types.FederatedType(arg_type, placements.CLIENTS),
      len(args),
  )

class CardinalityFreeDataDescriptor(ingestable_base.Ingestable):
  """Represent data-yielding computations with unspecified cardinalities.

  Instances of this class are objects that may combine a federated computation
  that returns federated data and an argument to be supplied as input to this
  computation, or alternatively the argument alone. These objects are designed
  to be recognized by the runtime. When ingesting those objects (e.g., as they
  are passed as arguments to a computation invocation), the runtime ingests
  the argument, and (if provided) invokes the computation contained
  in this descriptor on this argument to cause the data to materialize within
  the runtime (but without marshaling it out and returning it to user).

  In the typical usage of this helper class, the embedded argument is a set of
  handles, and the embedded computation transforms those handles into physical
  `tf.data.Dataset` instances. This transformation occurs on the clients (i.e.,
  in the TFF runtime worker processes).

  Alternatively, the argument itself may consist of computations to be locally
  executed on the clients. In this case, the computation can be omitted.
  """

  def __init__(
      self,
      comp: Optional[computation_base.Computation],
      arg: object,
      arg_type: computation_types.Type,
  ):
    """Constructs this data descriptor from the given computation and argument.

    Args:
      comp: The computation that materializes the data, of some type `(T -> U)`
        where `T` is the type of the argument `arg` and `U` is the type of the
        materialized data that's being produced. This can be `None`, in which
        case it's assumed to be an identity function (and `T` in that case must
        be identical to `U`).
      arg: The argument to be passed as input to `comp` if `comp` is not `None`,
        or to be treated as the computed result. Must be recognized by the TFF
        runtime as a payload of type `T`.
      arg_type: The type of the argument (`T` references above). An instance of
        `tff.Type`.

    Raises:
      ValueError: if the arguments don't satisfy the constraints listed above.
    """
    self._comp = comp
    self._arg = arg
    self._arg_type = arg_type
    if self._comp is not None:
      if (
          self._comp.type_signature.parameter is None
          or not self._comp.type_signature.parameter.is_assignable_from(
              self._arg_type
          )
      ):
        raise ValueError(
            'Argument type {} incompatible with the computation '
            'parameter {}.'.format(
                str(self._arg_type), str(self._comp.type_signature.parameter)
            )
        )
      self._type_signature = self._comp.type_signature.result
    else:
      self._type_signature = self._arg_type

  @property
  def type_signature(self):
    return self._type_signature

  async def ingest(self, executor):
    if isinstance(self._arg, ingestable_base.Ingestable):
      arg_coro = self._arg.ingest(executor)
    else:
      if self._comp is not None:
        expected_arg_type = self._comp.type_signature.parameter
      else:
        expected_arg_type = self._arg_type
      arg_coro = executor.create_value(self._arg, expected_arg_type)
    if self._comp is not None:
      comp_val, arg_val = await asyncio.gather(
          executor.create_value(self._comp, self._comp.type_signature), arg_coro
      )
      return await executor.create_call(comp_val, arg_val)
    else:
      return await arg_coro

class DataDescriptor(
    CardinalityFreeDataDescriptor, cardinality_carrying_base.CardinalityCarrying
):
  """Represents fully-specified data-yielding computations.

  Similar to CardinalityFreeDataDescriptor, but additionally accepts a
  cardinality argument, allowing callers to explcitly specify the number
  of clients this data descriptor is intended to represent.
  """

  def __init__(
      self,
      comp: Optional[computation_base.Computation],
      arg: object,
      arg_type: computation_types.Type,
      cardinality: Optional[int] = None,
  ):
    """Constructs this data descriptor from the given computation and argument.

    Args:
      comp: The computation that materializes the data, of some type `(T -> U)`
        where `T` is the type of the argument `arg` and `U` is the type of the
        materialized data that's being produced. This can be `None`, in which
        case it's assumed to be an identity function (and `T` in that case must
        be identical to `U`).
      arg: The argument to be passed as input to `comp` if `comp` is not `None`,
        or to be treated as the computed result. Must be recognized by the TFF
        runtime as a payload of type `T`.
      arg_type: The type of the argument (`T` references above). An instance of
        `tff.Type`.
      cardinality: If of federated type, placed at clients, this int specifies
        the number of clients represented by this DataDescriptor.

    Raises:
      ValueError: if the arguments don't satisfy the constraints listed above.
    """
    super().__init__(comp, arg, arg_type)
    self._cardinality: dict[placements.PlacementLiteral, int] = {}
    if isinstance(self._type_signature, computation_types.FederatedType):
      if self._type_signature.placement is placements.CLIENTS:
        if cardinality is None:
          raise ValueError('Expected `cardinality` to not be `None`.')
        self._cardinality[placements.CLIENTS] = cardinality
      else:
        if cardinality is not None:
          raise ValueError(
              f'Expected `cardinality` to be `None`, found: {cardinality}.'
          )

  @property
  def cardinality(self) -> Mapping[placements.PlacementLiteral, int]:
    return self._cardinality


"""Outpost Websocket URLS"""
from django.urls import path

from authentik.outposts.api.outposts import OutpostViewSet
from authentik.outposts.api.service_connections import (
    DockerServiceConnectionViewSet,
    KubernetesServiceConnectionViewSet,
    ServiceConnectionViewSet,
)
from authentik.outposts.consumer import OutpostConsumer
from authentik.root.middleware import ChannelsLoggingMiddleware

websocket_urlpatterns = [
    path("ws/outpost/<uuid:pk>/", ChannelsLoggingMiddleware(OutpostConsumer.as_asgi())),
]

api_urlpatterns = [
    ("outposts/instances", OutpostViewSet),
    ("outposts/service_connections/all", ServiceConnectionViewSet),
    ("outposts/service_connections/docker", DockerServiceConnectionViewSet),
    ("outposts/service_connections/kubernetes", KubernetesServiceConnectionViewSet),
]


import json
import os
import requests  # previously run in Terminal: >pip3.8 install requests

from Config.config import Config
from requests.exceptions import HTTPError
from datetime import date

def app(config_set, process_date=None):
    if not process_date:
        process_date = 'latest'  # str(date.today())

    data_path = os.path.join('..', config_set['directory'], process_date)
    os.makedirs(data_path, exist_ok=True)

    try:
        for currency in config_set['symbols']:
            url = config_set['url'] + '/' + process_date
            params = {'access_key': config_set['access_key'], 'symbols': currency}
            base_cur = config_set['base']

            response = requests.get(url, params=params)
            # print(response)  if 400 - client error
            # response.status_code
            response.raise_for_status()

            file_name = f'{currency}_{base_cur}.json'
            with open(os.path.join(data_path, file_name), 'w') as json_file:
                data = response.json()
                rates = data['rates']
                json.dump(rates, json_file)

    except HTTPError:
        print('Error!')

# for direct call this function app()
if __name__ == '__main__':
    config = Config(os.path.join('..', 'Config', 'config.yaml'))
    print(config.get_config('currency_app'))

    date_list = ['2021-06-20', '2021-06-21']
    for dt in date_list:
        app(config.get_config('currency_app'), dt)      # date_list
        app(config.get_config('currency_app'))          # latest date


import os
import time
from unittest.mock import patch
import tempfile

from django.conf import settings
from rest_framework import status
from rest_framework.exceptions import ErrorDetail
from rest_framework.reverse import reverse
from rest_framework.test import APITestCase

class TestCreate(APITestCase):

    def setUp(self):
        super(TestCreate, self).setUp()
        self.url = reverse('api:backup:list_create')

        self.valid_file = "/tmp/test_upload.mp3"
        self.invalid_file = "/tmp/test_upload.not_supported"

        # settings.MEDIA_ROOT = tempfile.mkdtemp()

    def tearDown(self) -> None:
        path = os.path.join(settings.MEDIA_ROOT, 'backup_mp3/test_upload.mp3')
        if os.path.exists(path):
            os.remove(path)

    @staticmethod
    def _create_test_file(path):
        f = open(path, 'w')
        f.write('test123\n')
        f.close()
        f = open(path, 'rb')
        return {'backup_file': f}

    def test_upload_valid_backup_file(self):
        data = self._create_test_file(self.valid_file)

        response = self.client.post(self.url, data, format='multipart')
        expected = {'id': 1, 'backup_file': 'backup_mp3/test_upload.mp3'}
        self.assertEquals(expected, response.data)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

    def test_upload_invalid_backup_file(self):
        data = self._create_test_file(self.invalid_file)
        response = self.client.post(self.url, data, format='multipart')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)


import scipy.io as sio
from sklearn.preprocessing import OneHotEncoder

# def encode_onehot(labels):
#     labels = labels.reshape(-1, 1)
#     enc = OneHotEncoder()
#     enc.fit(labels)
#     labels_onehot = enc.transform(labels).toarray()
#     return labels_onehot

# path = "/home/hangni/WangYC/HAN/data/imdb/imdb5k.mat"
path = '//home/hangni/WangYC/HAN/data/acm/ACM3025.mat'
data = sio.loadmat(path)
# print(type(data))
print(data.keys())

feature = data['feature']
print(feature.shape)
# print(feature)
# print(feature.shape)

# labels = data['label']
# print(labels)
# print(labels.shape)

# labels_onehot = encode_onehot(labels)
# print(labels_onehot)
# print(labels_onehot.shape)


#!/usr/bin/env python

"""
Pandoc filter to convert all regular text to uppercase.
Code, link URLs, etc. are not affected.
"""
import sys
from pandocfilters import toJSONFilter, Table, Plain, RawInline

def stdout_to_stderr(func):
    """Connects STDOUT to STDERR for the duration of a
    function, and then reconnects STDOUT to its original
    source."""
    def inner(*args, **kwargs):
        old_stdout = sys.stdout
        sys.stdout = sys.stderr
        result = func(*args, **kwargs)
        sys.stdout = old_stdout
        return result
    return inner

@stdout_to_stderr
def minipage_tables(key, value, format, meta):
    if key == 'Table':
        output=[]
        output.append(Plain([RawInline('tex', '\\begin{minipage}{\\textwidth}')]))
        output.append(Table(*value))
        output.append(Plain([RawInline('tex', '\\end{minipage}')]))
        return output

if __name__ == "__main__":
    toJSONFilter(minipage_tables)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun May  5 13:43:24 2019

@author: rajs
"""

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import random
import math
import time

SEED = 4028

random.seed(SEED)
torch.manual_seed(SEED)
torch.backends.cudnn.deterministic = True

numFutureCells = 8
BatchSize = 32
Featurelen = 4096
HiddenSize = 4096
Classes = 21

x = torch.randn(BatchSize,Featurelen )
hx = torch.randn(BatchSize, HiddenSize)
cx = torch.randn(BatchSize, HiddenSize)
r  = torch.zeros(BatchSize, Classes)

ohx = hx
ocx = cx

fts_bar = []
Act_bar = []

DecRNNCell = nn.LSTMCell(Classes, HiddenSize)
LinLayerR0 = nn.Linear(HiddenSize, HiddenSize)
LinLayerRp = nn.Linear(HiddenSize, Classes)
LinLayerRr = nn.Linear(Classes, Classes)

LinLayerS0 = nn.Linear(HiddenSize, Featurelen)
Avgpool = nn.AvgPool1d(numFutureCells)
StaRNNCell = nn.LSTMCell(2* Featurelen, HiddenSize)

LinLayerSn = nn.Linear(HiddenSize, Classes)

hx = LinLayerR0(hx)
for i in range(numFutureCells):
  hx, cx = DecRNNCell(r, (hx, cx))
  fts_bar.append(hx)
  p = LinLayerRp(hx)
  r = LinLayerRr(p)
  Act_bar.append(p)

fts_bar = torch.stack(fts_bar)
Act_bar = torch.stack(Act_bar)

fts_bar = fts_bar.permute(1,2,0)
hbar = Avgpool(fts_bar)
hbar = hbar.view(-1,HiddenSize)

xbar = LinLayerS0(hbar)
xbar = F.relu(xbar)

xcat = torch.cat((x, xbar), 1)

hx, cx = StaRNNCell(xcat, (ohx, ocx))
Act = LinLayerSn(hx)


# -*- coding: utf-8 -*-
from tqdm import tqdm
import os,os.path as op
import numpy as np
import re,datetime

start_date,end_date = '20181001','20190331'

def Pro_Hours(effect):
    binary = [0 for i in range(24)]
    for hour in effect:
        binary[hour] = 1
    return binary

def Add_List(la,lb):
#    return [la[i]+lb[i] for i in range(len(la))]
    return list(sum(np.array([la,lb])))

def GetBetweenDates(begin_date,end_date):
    date_list = []
    begin_date = datetime.datetime.strptime(begin_date,"%Y%m%d")
    end_date = datetime.datetime.strptime(end_date,"%Y%m%d")
    while begin_date <= end_date:
        date_str = begin_date.strftime("%Y%m%d")
        date_list.append(date_str)
        begin_date += datetime.timedelta(days=1)
    return date_list

def Reshape_Dict(records,DateList):
    result = {}
    for date in DateList:
        result[date] = [0 for i in range(24)]
    for record in records.values():
        for key,val in record.items():
            if key in result.keys():
                result[key] = Add_List(result[key],val)
            else:
                result[key] = val
    return dict([(key,result[key]) for key in sorted(result.keys())])

def Get_Matrix(txt_path,DateList):
    records = {}
    txt = open(txt_path).read()
    lines = [i for i in txt.split('\n') if len(i)>0]
    for line in lines:
        user = line.split('\t')[0]
        days = line.split('\t')[1].split(',')
        record = {}
        for day in days:
            date = day.split('&')[0]
            hours = Pro_Hours([eval(re.sub(r'^0','',hour)) for hour in day.split('&')[1].split('|')])
            record[date] = hours
        records[user] = record
    matrix = list(Reshape_Dict(records,DateList).values())
    return np.array(matrix).reshape(-1,7,24).transpose(1,0,2)

def Print_Arr(array):
    arrlist = list(array)
    for i in range(len(arrlist)):
        arr = arrlist[i]
        if int(np.array(arr.shape).shape[0])>=2:
            Print_Arr(arr)
        else:
            print('[{}]:\n{}'.format(i+1,arr))
    print()

#DateList_npy = 'DateList.npy'
#DateList = GetBetweenDates(start_date,end_date)
#np.save(DateList_npy,np.array(DateList))
#DateList = list(np.load(DateList_npy))

#txt_path = './000011.txt'
#Z = Get_Matrix(txt_path)

#array = np.load('000011.npy')
#Print_Arr(array)
#Print_Arr(Z)
#print(Z==array)