__author__ = 'shmakovs'

import pandas as pd
import subprocess
import os
from ete2 import Tree

import RecombinationHelper

# export PATH=$PATH:~wolf/bin/

# run_mafft_cls G7Pyrococcus.fasta > G7Pyrococcus.ali
# FastTree -gamma -wag G7Pyrococcus.ali > G7Pyrococcus.tmp
# /home/yutinn/scripts/tree_reading/root_tree.pl -r G7Pyrococcus.tmp > G7Pyrococcus.tre

def MakeDistanceMatricies(Database, ResFolder, Grain, GrainClustersFileName, RoofHeight = 40):
    print("Start dist " + Grain)
    GrainFolder = ResFolder + "/Grains/" + Grain
    DistMatrixFileName = GrainFolder + "/" + Grain + ".dist"
    TreeFileName = GrainFolder + "/" + Grain + ".tre"

    if not os.path.exists(GrainFolder):
        os.makedirs(GrainFolder)

    ClusterCount = sum(1 for line in open(GrainClustersFileName))
    print("Cluster count " + str(ClusterCount))

    GrainTree = Tree()
    for ClusterID in range(1, ClusterCount + 1):
        ClusterTree = Tree(MakeClusterTree(GrainFolder, GrainClustersFileName, ClusterID, Database))
        GrainTree.add_child(ClusterTree, dist = RoofHeight - GetTreeHeight(ClusterTree))
        print("Tree added " + str(ClusterID))

    # save tree
    GrainTree.write(format=1, outfile=TreeFileName)

    print("dist matr start")
    DistMatrix = RecombinationHelper.MakeDistMatrix(GrainTree)
    print("dist matr done")
    GIs = RecombinationHelper.GetGIsFromNames(GrainTree.get_leaf_names())
    print("writing")
    DistMatrix.to_csv(DistMatrixFileName, sep = "\t", columns = GIs)
    print("done")

def GetTreeHeight(t):
    MaxHeight = 0

    Leaves = t.get_leaves()

    for i in range(0, len(Leaves)):
        MaxHeight = max(MaxHeight, t.get_distance(Leaves[i]))

    return MaxHeight

def MakeClusterTree(GrainFolder, GrainClustersFileName, ClusterID, Database):
    ClusterIDs = RecombinationHelper.GetSelectedClusterIDs(GrainClustersFileName, ClusterID)
    return RecombinationHelper.MakeTree(ClusterIDs, GrainFolder, Database)

def AddClusters(GIs, Database, ResFolder, Grain, IslandsFileName, IsCas = False, IsUclust = False, TopHeight = 20):
    TempGIsFileName = ResFolder + "/temp_GIs.txt"
    TempFastaFileName = ResFolder + "/temp.fa"
    GrainClustersFileName = ResFolder + "/" + Grain + ".clust"
    with open(TempGIsFileName, "w+") as TempFile:
        TempFile.writelines((str(GI) + "\n") for GI in GIs)

    subprocess.call(
        "blastdbcmd -db " + Database + " -entry_batch " + TempGIsFileName + " > " + TempFastaFileName, shell = True)

    devnull = open(os.devnull, 'wb')
    if IsUclust:
        subprocess.call(
            "run_uclust -s=0.4 " + TempFastaFileName + " > " + GrainClustersFileName,
            shell = True, stdout = devnull, stderr = devnull)
    else:
        subprocess.call(
            "blastclust -i " + TempFastaFileName + " -a 4 -p T -L 0.3 -S 0.3 -o " + GrainClustersFileName,
            shell = True, stdout = devnull, stderr = devnull)

    subprocess.call(
        "python ../Fishing/Pipeline/Git/Fishing/recombination/SubsetAnnotationAssembly.py -i " + IslandsFileName +
        " -l " + GrainClustersFileName + " -g " + Grain, shell = True)

    if IsCas:
        MakeDistanceMatricies(Database, ResFolder, Grain, GrainClustersFileName, TopHeight)

# assigns cluster for each grain

GrainsFileName = "profFaX.tab"
IslandsFileName = "islands_blastclust_all.txt"
#IslandsFileName = "IslandAll.tsv"
#IslandsFileName = "islands_pyr.txt"
#Database = "/panfs/pan1/compgen/db/Prok1402"
Database = "/panfs/pan1/compgen/db/Prok1402"

ResFolder = "HighHeight_withTrees"

GrainsDF = pd.read_csv(GrainsFileName, sep = "\t",
    names = ["Profile", "Gene", "Family"], index_col = False)

GrainsPool = set(GrainsDF["Gene"])
print(GrainsPool)

AnnotationDF = pd.read_csv(IslandsFileName, sep = "\t", comment = "=",
    names = ["FakeGI", "Pos", "Strand", "Name", "No", "GeneNo", "Profile", "IsCas"], index_col = False)
AnnotationDF = AnnotationDF.fillna("")

print("test")

for Grain in GrainsPool:
    Profiles = GrainsDF[GrainsDF["Gene"] == Grain]["Profile"].tolist()
    print(Grain)

    GIs = RecombinationHelper.GetGIsByProfileList(AnnotationDF, Profiles)
    if len(GIs) > 2:
        AddClusters(GIs, Database, ResFolder, Grain, IslandsFileName, True, False, 20)

GIs = AnnotationDF[AnnotationDF["IsCas"] != "+"]["FakeGI"].tolist()
AddClusters(GIs, Database, ResFolder, "NonCas", IslandsFileName, False, False, 20)

# # add tabs to non clusters
# OutputFile = open("temp.ann", "w+")
#
# with open(IslandsFileName, "r") as AnnotationFile:
#     Content = AnnotationFile.readlines()
#     for Line in Content:
#         if "===" in Line:
#             OutputFile.write(Line)
#
#         elif "CLUSTER_" in Line:
#             OutputFile.write(Line)
#         else:
#             OutputFile.write(Line[:-1] + "\t" + "\n")
#
# OutputFile.close()


'''
The copy owns the data and any changes made to the copy will not affect original array
The view does not own the data and any changes made to the view will affect the original array
'''
import numpy as np

arr = np.array([1, 2, 3, 4, 5])
x = arr.copy() #new array

arr = np.array([1, 2, 3, 4, 5])
x = arr.view() #image of original
x[0] = 42

print(arr)
print(x)

#Print the value of the base attribute to check if an array owns it's data or not:
arr = np.array([1, 2, 3, 4, 5])

x = arr.copy()
y = arr.view()

print(x.base) #out: None
print(y.base) #out: [1, 2, 3, 4, 5]


from time import time
import get_traj_data

import ann

import numpy as np
import matplotlib.pyplot as plt

import pickle

import subprocess
import os

all_data = get_traj_data.concatenate(0, 50)

P_min = np.min(all_data["P"])
P_max = np.max(all_data["P"])

T_min = np.min(all_data["T"])
T_max = np.max(all_data["T"])

def test_single_batch(Y, c, i, theta=None, tau=0.0001):
    t = time()

    d = 4
    K = 5
    h = 1
    it_max = 4000000
    tol = 1e-4

    (F, dF, Js, theta) = ann.train_ANN_and_make_model_function(
        Y, c, d, K, h,
        it_max, tol, tau=tau, padding_mode="zeros", log="log.txt", theta=theta,
        y_min=P_min, y_max=P_max, c_min=T_min, c_max=T_max)

    print(f"took {time() - t:.2f} seconds to train")

    plt.plot(Js)
    plt.yscale("log")
    plt.savefig(f"{i}.png")
    plt.clf()
    plt.cla()

    print(f"J = {Js[-1]}")

    return theta

foldername = "T_trained_4x5_new"
# foldername = "T_trained_4x5_bad"

def startup():
    subprocess.call(f"mkdir results/{foldername}", shell=True)
    batch_data = get_traj_data.generate_data(batch=0)
    os.chdir(f"results/{foldername}")
    Y = batch_data["P"][:, :1000]
    c = batch_data["T"][:1000]
    theta = test_single_batch(Y, c, 0, tau=0.0003)

    with open("theta.pickle", "wb") as file:
        pickle.dump(theta, file)

    os.chdir("../..")

def resume():
    os.chdir(f"results/{foldername}")
    with open("theta.pickle", "rb") as file:
        theta = pickle.load(file)

    for i in range(0, 25):
        os.chdir("../..")
        batch_data = get_traj_data.generate_data(batch=i)
        os.chdir(f"results/{foldername}")
        Y = batch_data["P"]
        c = batch_data["T"]
        theta = test_single_batch(Y, c, i + 1, theta=theta, tau=0.0001)

    with open("theta.pickle", "wb") as file:
        pickle.dump(theta, file)

    os.chdir("../..")

def resume_with_big_data(lo, hi, plot_index=0):
    os.chdir(f"results/{foldername}")
    with open("theta.pickle", "rb") as file:
        theta = pickle.load(file)

    os.chdir("../..")
    batch_data = get_traj_data.concatenate(lo, hi)
    os.chdir(f"results/{foldername}")

    Y = batch_data["P"]
    c = batch_data["T"]

    theta = test_single_batch(Y,
                              c,
                              plot_index,
                              theta=theta,
                              tau=0.00002 / (hi - lo))

    with open("theta.pickle", "wb") as file:
        pickle.dump(theta, file)

    os.chdir("../..")

if __name__ == "__main__":
    # startup()
    # resume()
    # for i in range(19, 20):
    #     resume_with_big_data(i, i + 2, i)
    # resume_with_big_data(0, 20, 21)
    # resume_with_big_data(10, 30, 22)
    # resume_with_big_data(20, 40, 23)
    resume_with_big_data(0, 40, 24)


import tensorflow as tf
import numpy as np
import pandas as pd
import pymysql
#import os
import sys

from sys import argv

script, usuario_id = argv

usuario_id = int(usuario_id)
#usuario_id = 1
#print (usuario_id)

#mysql_connection = pymysql.connect(host='localhost',
 #                   user='root',
  #                  password='',
   #                 db='libros',
    #                charset='utf8',
     #               cursorclass= pymysql sudo apt-get install python3-pymysql.cursors.DictCursor)

mysql_connection = pymysql.connect(host='localhost',
                    user='root',
                    password='',
                    db='libros',
                    charset='utf8')

cursor = mysql_connection.cursor()

sql1 = "SELECT * FROM `bibliografia`"
sql2 = "SELECT * FROM `user_libros`"
libros_df = pd.read_sql(sql1, mysql_connection)
ratings_df = pd.read_sql(sql2, mysql_connection)

#print (libros_df.head(5))

#ratings_df = pd.read_csv(r'data/busquedas.csv', sep=',', header=None, engine='python')
#print (ratings_df.head(5))

libros_df.columns = ['bibliografiaID', 'Title', 'Autor1','Autor2','Volumen']
#ratings_df.columns = ['UserID', 'bibliografiaID', 'Rating', 'Timestamp']
ratings_df.columns = ['ID','UserID','bibliografiaID','busquedaID', 'Rating', 'Creado','Modificado']
#ratings_df = ratings_df[np.isfinite(ratings_df['bibliografiaID'])]
#ratings_df['bibliografiaID'] = ratings_df.bibliografiaID.astype(int)
#Dropping unecessary columns
ratings_df = ratings_df.drop(['ID','busquedaID','Creado','Modificado'], axis=1)
#print (ratings_df.head(5))

user_rating_df = ratings_df.pivot(index='UserID', columns='bibliografiaID', values='Rating')
#print (user_rating_df.head())

norm_user_rating_df = user_rating_df.fillna(0) / 5.0
trX = norm_user_rating_df.values

#print (trX[0:5])

hiddenUnits = 20
visibleUnits =  len(user_rating_df.columns)
vb = tf.placeholder("float", [visibleUnits]) #Cantidad de libros unicos
hb = tf.placeholder("float", [hiddenUnits]) #Numero de caracteristicas que va aprender
W = tf.placeholder("float", [visibleUnits, hiddenUnits])

#Fase 1: Input Processing, Forward Pass

v0 = tf.placeholder("float", [None, visibleUnits])
_h0 = tf.nn.sigmoid(tf.matmul(v0, W) + hb)
h0 = tf.nn.relu(tf.sign(_h0 - tf.random_uniform(tf.shape(_h0))))

#Fase 2: Reconstruction

_v1 = tf.nn.sigmoid(tf.matmul(h0, tf.transpose(W)) + vb)
v1 = tf.nn.relu(tf.sign(_v1 - tf.random_uniform(tf.shape(_v1))))
h1 = tf.nn.sigmoid(tf.matmul(v1, W) + hb)

#Tasa de aprendizaje
alpha = 1.0
#Crear gradientes
w_pos_grad = tf.matmul(tf.transpose(v0), h0)
w_neg_grad = tf.matmul(tf.transpose(v1), h1)

#Calcula la Divergencia Contrastiva para maximizar
CD = (w_pos_grad - w_neg_grad) / tf.to_float(tf.shape(v0)[0])

#Crear métodos para actualizar los pesos y bias

update_w = W + alpha * CD
update_vb = vb + alpha * tf.reduce_mean(v0 - v1, 0)
update_hb = hb + alpha * tf.reduce_mean(h0 - h1, 0)

err = v0 - v1
err_sum = tf.reduce_mean(err * err)

#Current weight
cur_w = np.zeros([visibleUnits, hiddenUnits], np.float32)
#Current visible unit biases
cur_vb = np.zeros([visibleUnits], np.float32)
#Current hidden unit biases
cur_hb = np.zeros([hiddenUnits], np.float32)
#Previous weight
prv_w = np.zeros([visibleUnits, hiddenUnits], np.float32)
#Previous visible unit biases
prv_vb = np.zeros([visibleUnits], np.float32)
#Previous hidden unit biases
prv_hb = np.zeros([hiddenUnits], np.float32)
sess = tf.Session()
sess.run(tf.global_variables_initializer())

epochs = 20
batchsize = 100
errors = []
for i in range(epochs):
    for start, end in zip( range(0, len(trX), batchsize), range(batchsize, len(trX), batchsize)):
        batch = trX[start:end]
        cur_w = sess.run(update_w, feed_dict={v0: batch, W: prv_w, vb: prv_vb, hb: prv_hb})
        cur_vb = sess.run(update_vb, feed_dict={v0: batch, W: prv_w, vb: prv_vb, hb: prv_hb})
        cur_nb = sess.run(update_hb, feed_dict={v0: batch, W: prv_w, vb: prv_vb, hb: prv_hb})
        prv_w = cur_w
        prv_vb = cur_vb
        prv_hb = cur_hb
    errors.append(sess.run(err_sum, feed_dict={v0: trX, W: cur_w, vb: cur_vb, hb: cur_hb}))

   # print (errors[-1])

#Seleccionando el usuario de entrada
mock_user_id = usuario_id

inputUser = trX[mock_user_id-1].reshape(1, -1)
#print (inputUser[0:5])

#Alimentar el usuario de entrada y reconstruir la entrada

hh0 = tf.nn.sigmoid(tf.matmul(v0, W) + hb)
vv1 = tf.nn.sigmoid(tf.matmul(hh0, tf.transpose(W)) + vb)
feed = sess.run(hh0, feed_dict={ v0: inputUser, W: prv_w, hb: prv_hb})
rec = sess.run(vv1, feed_dict={ hh0: feed, W: prv_w, vb: prv_vb})

#print(rec)

#Se recomiendan 10 libros a nuestro usuario de prueba clasificándolos por sus puntajes proporcionados por nuestro modelo.
scored_libros_df_mock = libros_df[libros_df['bibliografiaID'].isin(user_rating_df.columns)]
scored_libros_df_mock = scored_libros_df_mock.assign(RecommendationScore = rec[0])
#print (scored_libros_df_mock.sort_values(["RecommendationScore"], ascending=False).head(20))

libros_df_mock = ratings_df[ratings_df['UserID'] == mock_user_id]

#combinar libros_df con ratings_df por bibliografiaID
merged_df_mock = scored_libros_df_mock.merge(libros_df_mock, on='bibliografiaID', how='outer')

#borrar columnas innecesarias
merged_df_mock = merged_df_mock.drop('Title', axis=1).drop(['UserID','Autor1','Autor2','Volumen',], axis=1)

rec = merged_df_mock[merged_df_mock["Rating"].isnull()].sort_values(["RecommendationScore"], ascending=False).head(10)
rec = rec.drop('Rating',axis=1)
print (rec)
#print (merged_df_mock.sort_values(["RecommendationScore"], ascending=False).head(20))


import unittest
from os.path import join, abspath

from numpy.testing import assert_allclose

from tests.tabulation.nonadiabatic_defect_equilibrium.rebless import run
from spitfire.chemistry.library import Library
from spitfire.chemistry.ctversion import check as cantera_version_check

if cantera_version_check('atleast', 2, 5, None):
    tol_args = {'atol': 1e-14} if cantera_version_check('pre', 2, 6, None) else {'atol': 1e-7, 'rtol': 3e-3}
    class Test(unittest.TestCase):
        def test(self):
            output_library = run()

            gold_file = abspath(join('tests',
                                     'tabulation',
                                     'nonadiabatic_defect_equilibrium',
                                     'gold.pkl'))
            gold_library = Library.load_from_file(gold_file)

            for prop in gold_library.props:
                self.assertIsNone(assert_allclose(gold_library[prop], output_library[prop], **tol_args))

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


from unittest import TestCase
from blog import blog

class BlogTest(TestCase):
    def test_create_blog(self):
        b = blog.Blog('Test', 'Test Author')

        self.assertEqual('Test', b.title)
        self.assertEqual('Test Author', b.author)
        self.assertListEqual([], b.posts)

    def test_repr(self):
        b = blog.Blog('Test', 'Test Author')
        b2 = blog.Blog('My Day', 'Izzyevermore')

        self.assertEqual(b.__repr__(), 'Test by Test Author (0 posts)')
        self.assertEqual(b2.__repr__(), 'My Day by Izzyevermore (0 posts)')

    def test_repr_mutiple_posts(self):
        b = blog.Blog('Test', 'Test Author')
        b.posts = ['test']
        b2 = blog.Blog('My Day', 'Izzyevermore')
        b2.posts = ['test', 'another']

        self.assertEqual(b.__repr__(), 'Test by Test Author (1 post)')
        self.assertEqual(b2.__repr__(), 'My Day by Izzyevermore (2 posts)')


buget=float(input())
extras=int(input())
price_pre_extra=float(input())
art=buget/10
costume=0
if extras>150:
    costume=extras*price_pre_extra*9/10
else: costume=extras*price_pre_extra

if buget-costume-art>=0:
    print(f'Action!')
    print(f'Wingard starts filming with {abs(buget-costume-art):.2f} leva left.')
else: print(f'Not enough money!\nWingard needs {abs(buget-costume-art):.2f} leva more.')

2.Conditional statements/Godzill and Kong.py


import numpy as np
import gym

from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam

from trickster.agent import PPO
from trickster.rollout import Rolling, Trajectory, RolloutConfig
from trickster.experience import Experience
from trickster.utility import visual

env = gym.make("CartPole-v1")
input_shape = env.observation_space.shape
num_actions = env.action_space.n

actor = Sequential([Dense(16, activation="relu", input_shape=input_shape),
                    Dense(16, activation="relu"),
                    Dense(num_actions, activation="softmax")])
actor.compile(loss="categorical_crossentropy", optimizer=Adam(1e-4))

critic = Sequential([Dense(16, activation="relu", input_shape=input_shape),
                     Dense(16, activation="relu"),
                     Dense(1, activation="linear")])
critic.compile(loss="mse", optimizer=Adam(1e-5))

agent = PPO(actor,
            critic,
            action_space=2,
            memory=Experience(max_length=10000),
            reward_discount_factor_gamma=0.99,
            entropy_penalty_coef=0.005)

rollout = Rolling(agent, env, config=RolloutConfig(max_steps=300))
test_rollout = Trajectory(agent, gym.make("CartPole-v1"))

rewards = []
actor_loss = []
actor_utility = []
actor_kld = []
actor_entropy = []
advantages = []
critic_loss = []

for episode in range(1, 2001):

    rollout.roll(steps=64, verbose=0, push_experience=True)
    agent_history = agent.fit(batch_size=32, verbose=0, reset_memory=True)

    actor_loss.append(np.mean(agent_history["actor_loss"]))
    actor_utility.append(np.mean(agent_history["actor_utility"]))
    actor_kld.append(np.mean(agent_history["actor_kld"]))
    actor_entropy.append(np.mean(agent_history["actor_entropy"]))
    advantages.append(np.mean(agent_history["advantage"]))
    critic_loss.append(np.mean(agent_history["critic_loss"]))

    test_history = test_rollout.rollout(verbose=0, push_experience=False, render=False)
    rewards.append(test_history["reward_sum"])

    print("\rEpisode {:>4} RWD {:>5.2f} A {:>5.2f} ACTR {:>7.4f} UTIL {:>7.4f} KKL {:>7.4f} ENTR {:>7.4f} CRIT {:>7.4f}"
          .format(
            episode,
            np.mean(rewards[-10:]),
            np.mean(advantages[-10:]),
            np.mean(actor_loss[-10:]),
            np.mean(actor_utility[-10:]),
            np.mean(actor_kld[-10:])*1000,
            np.mean(actor_entropy[-10:]),
            np.mean(critic_loss[-10:])), end="")
    if episode % 10 == 0:
        print()

visual.plot_vectors([rewards, actor_loss, actor_utility, actor_kld, actor_entropy, critic_loss],
                    ["Reward", "Actor Loss", "Actor Utility", "Actor KLD", "Actor Entropy", "Critic Loss"],
                    smoothing_window_size=10)


# -*- coding: utf-8 -*-
#
#	jython_cassandra_manipulate.py
#
#					Sep/06/2013
import	sys
import	string
import	datetime
from time import localtime,strftime
#
from text_manipulate import dict_append_proc
# ---------------------------------------------------------------
def cassandra_to_dict_proc (conn):
	print ("*** cassandra_to_dict_proc ***")
	dict_aa = {}
#
	sql_str="select key,name,population,date_mod from cities;"
	st = conn.createStatement()
	rset = st.executeQuery (sql_str)
#
	while rset.next():
		key = rset.getString(1)
		name = rset.getString(2)
		population = rset.getString(3)
		date_mod = rset.getString(4)
		dict_aa = dict_append_proc (dict_aa,key,name,population,date_mod)
#
	return	dict_aa
#
# ---------------------------------------------------------------
def dict_to_cassandra_proc (dict_aa,conn):
	sql_str="drop table cities;"
	st = conn.createStatement()
	st.execute (sql_str);
#
	sql_str="create table cities (key varchar primary key,name text,population int,date_mod timestamp);"
	st.execute (sql_str)
#
	for key in dict_aa.keys():
		unit_aa = dict_aa[key]
		name = unit_aa['name']
		population = unit_aa['population']
		date_mod = unit_aa['date_mod']
#
		str_aa = "insert into cities (key,name,population,date_mod) values "
		str_bb = "('" + key + "','" + name + "'," + str(population) + ",'" + date_mod + "')"
		sql_str = str_aa + str_bb
		print (sql_str)
		st.execute (sql_str)
#
	st.close()
# ---------------------------------------------------------------
def cassandra_update_proc (conn,key,population_in):
	print ("*** cassandra_update_proc ***")
#
	str_ddx = strftime ("%Y-%m-%d",localtime ())
	sql_str="update cities set population =" + str(population_in)
	sql_str += ", date_mod = '" + str_ddx + "'"
	sql_str += " where key = '" + key + "';"

	st = conn.createStatement()
	st.execute (sql_str)
	st.close()
# ---------------------------------------------------------------
def cassandra_delete_proc (conn,key):
	print ("*** cassandra_delete_proc ***")
#
	sql_str="delete from cities where key = '" + key + "';"

	st = conn.createStatement()
	st.execute (sql_str)
	st.close()
# ---------------------------------------------------------------


# coding:utf-8

import cv2
import numpy as np

# 读取图片
img = cv2.imread("test.jpg")

# 通过NumPy模块，创建新的空数组，图像使用NumPy数组的属性来表示图像的尺寸和通道信息
emptyImage = np.zeros(img.shape, np.uint8)

# 复制图片
emptyImage2 = img.copy()

# 通过cvtColor获得图像副本
emptyImage3=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# 将图片转为黑白图片
#emptyImage3[...]=0

# 显示图片
cv2.imshow("EmptyImage", emptyImage)
cv2.imshow("Image", img)
cv2.imshow("EmptyImage2", emptyImage2)
cv2.imshow("EmptyImage3", emptyImage3)

# 保存图像
"""
# 第一个参数是保存的路径及文件名，第二个是图像矩阵。其中，imwrite()有个可选的第三个参数，如下：
# 第三个参数针对特定的格式： 对于JPEG，其表示的是图像的质量，用0-100的整数表示，默认为95。 注意，cv2.IMWRITE_JPEG_QUALITY类型为Long，必须转换成int。
#                         对于PNG，第三个参数表示的是压缩级别。cv2.IMWRITE_PNG_COMPRESSION，从0到9,压缩级别越高，图像尺寸越小。默认级别为3：
"""
cv2.imwrite("test2.jpg", img, [int(cv2.IMWRITE_JPEG_QUALITY), 5])
cv2.imwrite("test3.jpg", img, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
cv2.imwrite("test.png", img, [int(cv2.IMWRITE_PNG_COMPRESSION), 0])
cv2.imwrite("test2.png", img, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])

cv2.waitKey(0)
cv2.destroyAllWindows()


# Definition for an interval.
class Interval:
   def __init__(self, s=0, e=0):
        self.start = s
        self.end = e

import math
class Solution:
    def eraseOverlapIntervals(self, intervals: 'List[Interval]') -> 'int':
        intervals.sort(key=lambda x: x.start)
        count = 0
        prev = -math.inf
        for interval in intervals:
            if interval.start < prev:
                count += 1
                prev = min(prev, interval.end)
            else:
                prev = interval.end
        return count


from tkinter import *
from math import *
root = Tk()
root.title("Calculator")
root.geometry("300x200")

entryBlock = Entry(root, bd=8, width=30)
entryBlock.grid(row=0, column=1, columnspan=5)

def setText(txt):
    l = len(entryBlock.get())
    entryBlock.insert(l, txt)

def clear1():
    txt = entryBlock.get()
    entryBlock.delete(0, END)
    entryBlock.insert(0, txt[:-1])

def clear():
    entryBlock.delete(0, END)

def sqroot():
    txt = sqrt(float(entryBlock.get()))
    entryBlock.delete(0, END)
    entryBlock.insert(0, txt)

def negation():
    txt = entryBlock.get()
    if txt[0] == "-":
        entryBlock.delete(0, END)
        entryBlock.insert(0, txt[1:])
    elif txt[0] == "+":
        entryBlock.delete(0, END)
        entryBlock.insert(0, "-"+txt[1:])
    else:
        entryBlock.insert(0, "-")

def equals():
    try:
        s = entryBlock.get()
        for i in range(0, len(s)):
            if s[i] == "+" or s[i] == "-" or s[i] == "*" or s[i] == "/" or s[i] == "%":
                expr = str(float(s[:i]))+s[i:]
                break
            elif s[i] == ".":
                expr = s
                break
        entryBlock.delete(0, END)
        entryBlock.insert(0, eval(expr))
    except Exception:
        entryBlock.delete(0, END)
        entryBlock.insert(0, "INVALID EXPRESSION")

back1 = Button(root, text="<--", command=lambda: clear1(), width=10)
back1.grid(row=1, column=1, columnspan=2)

can = Button(root, text="C", command=lambda: clear(), width=4)
can.grid(row=1, column=3)

neg = Button(root, text="+/-", command=lambda: negation(), width=4)
neg.grid(row=1, column=4)

sqr = Button(root, text=u'\u221A', command=lambda: sqroot(), width=4)
sqr.grid(row=1, column=5)

nine = Button(root, text="9", command=lambda: setText("9"), width=4)
nine.grid(row=2, column=1)

eight = Button(root, text="8", command=lambda: setText("8"), width=4)
eight.grid(row=2, column=2)

seven = Button(root, text="7", command=lambda: setText("7"), width=4)
seven.grid(row=2, column=3)

six = Button(root, text="6", command=lambda: setText("6"), width=4)
six.grid(row=3, column=1)

five = Button(root, text="5", command=lambda: setText("5"), width=4)
five.grid(row=3, column=2)

four = Button(root, text="4", command=lambda: setText("4"), width=4)
four.grid(row=3, column=3)

three = Button(root, text="3", command=lambda: setText("3"), width=4)
three.grid(row=4, column=1)

two = Button(root, text="2", command=lambda: setText("2"), width=4)
two.grid(row=4, column=2)

one = Button(root, text="1", command=lambda: setText("1"), width=4)
one.grid(row=4, column=3)

zero = Button(root, text="0", command=lambda: setText("0"), width=10)
zero.grid(row=5, column=1, columnspan=2)

dot = Button(root, text=".", command=lambda: setText("."), width=4)
dot.grid(row=5, column=3)

div = Button(root, text="/", command=lambda: setText("/"), width=4)
div.grid(row=2, column=4)

mul = Button(root, text="*", command=lambda: setText("*"), width=4)
mul.grid(row=3, column=4)

minus = Button(root, text="-", command=lambda: setText("-"), width=4)
minus.grid(row=4, column=4)

plus = Button(root, text="+", command=lambda: setText("+"), width=4)
plus.grid(row=5, column=4)

mod = Button(root, text="%", command=lambda: setText("%"), width=4)
mod.grid(row=2, column=5)

byx = Button(root, text="1/x", command=lambda: setText("%"), width=4)
byx.grid(row=3, column=5)

equal = Button(root, text="=", command=lambda: equals(), width=4, height=3)
equal.grid(row=4, column=5, rowspan=2)

root.mainloop()


import numpy as np
import cv2
import copy
try: # python 2
    import tkFileDialog
except ImportError: # python 3
    pass
import os.path
import sys
# step one: load fly video
# step two: choose area that contains fly arena
# step three: select arena width
# step four: align microphone 1
# step three: choose area that shows blinky light
# select center position
# select initial flies (M/F)
# choose mating frame

def updateImage(x, y):
    global arenaX, arenaY, rectRadius, rectCenterX, rectCenterY, showFrame

    xdelta = abs(arenaX - x)
    ydelta = abs(arenaY - y)
    delta = max(xdelta, ydelta)

    rectRadius = delta / 2
    rectCenterX = arenaX
    rectCenterY = arenaY

    img = copy.copy(showFrame)
    cv2.circle(img, (arenaX, arenaY), delta, (0, 255, 0), 3)
    cv2.imshow('display', img)

def areaCallback(event, x, y, flags, param):
    global currentlyDrawing, arenaX, arenaY

    if event == cv2.EVENT_LBUTTONDOWN and currentlyDrawing == False:
        arenaX = x
        arenaY = y

    if event == cv2.EVENT_LBUTTONDOWN:
        currentlyDrawing = True
    elif event == cv2.EVENT_LBUTTONUP:
        currentlyDrawing = False

    if currentlyDrawing:
        updateImage(x, y)

def updateCenterImage(x, y):
    global arenaX, arenaY, showFrame

    img = copy.copy(showFrame)
    cv2.circle(img, (x, y), 5, (0, 255, 0), 3)
    cv2.imshow('display', img)

def centerCallback(event, x, y, flags, param):
    global currentlyDrawing, arenaX, arenaY

    if event == cv2.EVENT_LBUTTONDOWN and currentlyDrawing == False:
        arenaX = x
        arenaY = y

    if event == cv2.EVENT_LBUTTONDOWN:
        currentlyDrawing = True
        arenaX = x
        arenaY = y
    if event == cv2.EVENT_LBUTTONUP:
        currentlyDrawing = False

    if currentlyDrawing:
        updateCenterImage(x, y)

def findFliesCallback(event, x, y, flags, param):
    global nFlies, fly1CenterX, fly1CenterY, fly2CenterX, fly2CenterY

    if event == cv2.EVENT_LBUTTONUP:
        nFlies = nFlies + 1
        print('nFlies = ' + str(nFlies))
        updateCenterImage(x, y)
        flyCenters[nFlies - 1, 0] = x
        flyCenters[nFlies - 1, 1] = y

def trackerCallback(position):
    global angle

    M = cv2.getRotationMatrix2D((width / 4, height / 4), position - 180, 1)
    angle = position
    img = copy.copy(showFrame)
    img = cv2.warpAffine(img, M, (int(width / 2), int(height / 2)))
    cv2.imshow('display', img)

def doNothing(event, x, y, flags, param):
    pass

def getStartTime(dirName):
    trackingFile = os.path.join(dirName, 'startTrackingFrame.txt')
    if (os.path.isfile(trackingFile)):
        f = open(trackingFile)
        startFrame = int(f.readline())
        print('starting frame is ' + str(startFrame))
        f.close()
        return startFrame
    else:
        return 1

# these are the global variables that we want to assign (make these cmd args?)
doDefineArena = True
doAlignMike = True
doBlinky = True
currentlyDrawing = False
arenaX = 0
arenaY = 0
delta = 0
rectCenterX = 0
rectCenterY = 0
rectRadius = 0
angle = 0
nFlies = 0
flyCenters = np.zeros((100, 2))
fly1CenterX = 0
fly1CenterY = 0
fly2CenterX = 0
fly2CenterY = 0

# do the damage
if len(sys.argv) > 1:
    movieName = sys.argv[1]
else:
    movieName = tkFileDialog.askopenfilename()
print(movieName)
# startFrame = getStartTime(movieName[0:(movieName.rindex('/')+1)])
startFrame = getStartTime(os.path.dirname(movieName))

vr = cv2.VideoCapture(movieName)
vr.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('H', '2', '6', '4'))
NumberOfFrames = int(vr.get(cv2.CAP_PROP_FRAME_COUNT))
fps = vr.get(cv2.CAP_PROP_FPS)
width = int(vr.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(vr.get(cv2.CAP_PROP_FRAME_HEIGHT))

if (startFrame != 1):
    vr.set(cv2.CAP_PROP_POS_FRAMES, startFrame + 1)
ret, frame = vr.read()

# fixed issue with reading early frames on windows
if np.max(frame)==0:
    vr.set(cv2.CAP_PROP_POS_FRAMES, np.min((100, NumberOfFrames)))
    vr.read()
    vr.set(cv2.CAP_PROP_POS_FRAMES, startFrame + 1)
    ret, frame = vr.read()

if not ret:
    print("error reading frame")

print((width, height))
cv2.namedWindow('display')
cv2.resizeWindow('display', width, height)

# choose fly arena
#frame2 = copy.copy(frame)
#cv2.putText(frame2, 'Choose arena', (50,50), cv2.FONT_HERSHEY_PLAIN, 2, 255)
#showFrame = cv2.resize(frame2,(int(width/2),int(height/2)))
#cv2.imshow('display',showFrame)
#cv2.setMouseCallback('display',areaCallback)
#cv2.waitKey(0)

if doDefineArena:
    # choose arena center
    print((width, height))
    frame2 = copy.copy(frame)
    cv2.putText(frame2, 'Choose arena (press enter when done)', (50, 50), cv2.FONT_HERSHEY_PLAIN, 2, 255)
    showFrame = cv2.resize(frame2, (int(width / 2), int(height / 2)))
    cv2.imshow('display', showFrame)
    cv2.setMouseCallback('display', centerCallback)
    cv2.waitKey(0)
    centerX, centerY = arenaX, arenaY

    # choose arena width
    frame2 = copy.copy(frame)
    cv2.putText(frame2, 'Select LEFT boundary', (50, 50), cv2.FONT_HERSHEY_PLAIN, 2, 255)
    showFrame = cv2.resize(frame2,(int(width / 2), int(height / 2)))
    cv2.imshow('display', showFrame)
    cv2.setMouseCallback('display',centerCallback)  #boundary callback
    cv2.waitKey(0)
    leftX, leftY = arenaX, arenaY

    frame2 = copy.copy(frame)
    cv2.putText(frame2, 'Select RIGHT boundary', (50, 50), cv2.FONT_HERSHEY_PLAIN, 2, 255)
    showFrame = cv2.resize(frame2, (int(width / 2), int(height / 2)))
    cv2.imshow('display', showFrame)
    cv2.setMouseCallback('display', centerCallback)  #boundary callback
    cv2.waitKey(0)
    rightX, rightY = arenaX, arenaY

    radius = np.sqrt((leftX - rightX) ** 2 + (leftY - rightY) ** 2) / 2
    # show detected circle
    frame2 = copy.copy(frame)
    cv2.putText(frame2, 'Here is the arena...I hope that is correct!', (50, 50), cv2.FONT_HERSHEY_PLAIN, 2, 255)
    showFrame = cv2.resize(frame2, (int(width / 2), int(height / 2)))
    cv2.circle(showFrame, (centerX, centerY), int(radius),(0, 255, 0), 3)
    cv2.imshow('display', showFrame)
    cv2.waitKey(0)

else:
    centerX, centerY = width / 4, height / 4 # order may be wrong...
    # rectCenterX, rectCenterY = width/2, height/2 # order may be wrong...
    leftX, leftY = 10 / 3, 10 / 3
    rightX, rightY = (width - 10) / 3, (height - 10) / 3
    radius = np.sqrt((leftX - rightX) ** 2 + (leftY - rightY) ** 2) / 2

# select flies
frame2 = copy.copy(frame)
cv2.putText(frame2, 'Select ALL flies', (50, 50), cv2.FONT_HERSHEY_PLAIN, 2, 255)
showFrame = cv2.resize(frame2, (int(width / 2), int(height / 2)))
cv2.imshow('display', showFrame)
cv2.setMouseCallback('display', findFliesCallback)  #boundary callback
cv2.waitKey(0)
leftX, leftY = arenaX, arenaY

flyCenters = flyCenters[0:nFlies, :]

if doAlignMike:
    # align microphone 1
    frame2 = copy.copy(frame)
    cv2.putText(frame2, 'Align microphone 1', (50, 50), cv2.FONT_HERSHEY_PLAIN, 2, 255)
    cv2.createTrackbar('imageRotation', 'display', 180, 360, trackerCallback)
    showFrame = cv2.resize(frame2, (int(width / 2), int(height / 2)))
    cv2.setMouseCallback('display', doNothing)
    cv2.imshow('display', showFrame)
    cv2.waitKey(0)

# we want to show the estimated position of the microphones to make sure we have them all...
# maybe this should be shown in the 'align microphone' phase

if doBlinky:
    # choose blinky light area
    frame2 = copy.copy(frame)
    cv2.putText(frame2, 'Select blinky light', (50, 50), cv2.FONT_HERSHEY_PLAIN, 2, 255)
    showFrame = cv2.resize(frame2, (int(width / 2), int(height / 2)))
    cv2.imshow('display', showFrame)
    cv2.setMouseCallback('display', areaCallback)  # boundary callback
    cv2.waitKey(0)

vr.release()
cv2.destroyAllWindows()

# save the data
# f=open(movieName[0:len(movieName)-4] + '_annotated.dat','w')
f = open(os.path.splitext(movieName)[0] + '_annotated.dat', 'w')
f.write(str(width) + '\n')
print('width = ' + str(width))
f.write(str(height) + '\n')
print('height = ' + str(height))
f.write(str(centerX * 2) + '\n')
print('centerX = ' + str(centerX * 2))
f.write(str(centerY * 2) + '\n')
print('centerY = ' + str(centerY * 2))
f.write(str(radius * 2) + '\n')
print('radius = ' + str(radius * 2))
f.write(str(rectCenterX * 2) + '\n')
print('rectCenterX = ' + str(rectCenterX * 2))
f.write(str(rectCenterY * 2) + '\n')
print('rectCenterY = ' + str(rectCenterY * 2))
f.write(str(rectRadius * 2) + '\n')
print('rectRadius = ' + str(rectRadius * 2))
f.write(str(angle) + '\n')
print('angle = ' + str(angle))
f.write(str(nFlies))  # ugggggghhhhh I forgot the + '\n' !!!!!!!
print('nFlies = ' + str(nFlies))

for ii in range(nFlies):
    f.write(str(flyCenters[ii, 0] * 2) + '\n')
    f.write(str(flyCenters[ii, 1] * 2) + '\n')
    print('fly center = ' + str((flyCenters[ii, 0], flyCenters[ii, 1])))

f.close()


from flask import Blueprint, render_template, jsonify, redirect

viewer = Blueprint("viewer", __name__, template_folder="templates")

# @viewer.route("/view/<db>/<dataset_id>")
def view_s3(public_db, dataset_id):

    if public_db not in ("sra", "img"):
        return "Database not supported", 404

    import boto3

    conn = boto3.client("s3")

    key = f"sigs/{dataset_id}.sig"

    params = {
        "Bucket": f"wort-{public_db}",
        "Key": key,
        "ResponseContentType": "application/json",
        "ResponseContentEncoding": "gzip",
    }

    url = conn.generate_presigned_url("get_object", Params=params, ExpiresIn=100)

    return redirect(url)


import pandas as pd
import sys
from SpeedDetection import *

def main():
	data_path=sys.argv[1]
	out_path=sys.argv[2]
	data = pd.read_csv('%s'%(data_path))
	caculate_process(data,out_path)
if __name__ == '__main__':
	main()


# Copyright 2020 The FastEstimator Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import unittest
from copy import deepcopy

import tensorflow as tf
import torch

import fastestimator as fe
from fastestimator.architecture.pytorch import LeNet as LeNet_torch
from fastestimator.architecture.tensorflow import LeNet as LeNet_tf
from fastestimator.dataset.data import mnist
from fastestimator.op.numpyop.univariate import ExpandDims, Minmax
from fastestimator.op.tensorop.gradient import GradientOp
from fastestimator.op.tensorop.loss import CrossEntropy
from fastestimator.op.tensorop.model import ModelOp, UpdateOp
from fastestimator.test.unittest_util import MultiLayerTorchModel, is_equal, one_layer_tf_model

class TestUpdateOp(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.state = {'mode': 'train', 'epoch': 1, 'warmup': False, "deferred": {}, "scaler": None}
        cls.tf_input_data = tf.Variable([[2.0, 1.5, 1.0], [1.0, -1.0, -0.5]])
        cls.torch_input_data = torch.tensor([[1.0, 1.0, 1.0, -0.5], [0.5, 1.0, -1.0, -0.5]],
                                            dtype=torch.float32,
                                            requires_grad=True)
        cls.torch_y = torch.tensor([[5], [7]], dtype=torch.float32)
        cls.tf_y = tf.constant([[-6], [1]])

    def test_tf_input(self):
        def feed_forward(op, model):
            with tf.GradientTape(persistent=True) as tape:
                self.state['tape'] = tape
                pred = fe.backend.feed_forward(model, self.tf_input_data)
                loss = fe.backend.mean_squared_error(y_pred=pred, y_true=self.tf_y)
                op.forward(data=loss, state=self.state)

        model = fe.build(one_layer_tf_model, optimizer_fn="adam")
        op = UpdateOp(model=model, loss_name='loss')
        weights_before = model.layers[1].get_weights()
        strategy = tf.distribute.get_strategy()
        if isinstance(strategy, tf.distribute.MirroredStrategy):
            strategy.run(feed_forward, args=(op, model))
        else:
            feed_forward(op, model)
        weights_after = model.layers[1].get_weights()
        self.assertFalse(is_equal(weights_before, weights_after))

    def test_torch_input(self):
        def update(x, op, model):
            pred = fe.backend.feed_forward(model, x)
            loss = fe.backend.mean_squared_error(y_pred=pred, y_true=self.torch_y.to(device))
            op.forward(data=loss, state=self.state)

        model = fe.build(model_fn=MultiLayerTorchModel, optimizer_fn="adam")
        op = UpdateOp(model=model, loss_name='loss')
        if torch.cuda.is_available():
            device = torch.device('cuda')
            model = model.to(device)
            if torch.cuda.device_count() > 1:
                weights_before = deepcopy(model.module.fc1.weight.cpu().data.numpy())
            else:
                weights_before = deepcopy(model.fc1.weight.cpu().data.numpy())
            update(self.torch_input_data.to(device), op, model)
            if torch.cuda.device_count() > 1:
                weights_after = model.module.fc1.weight.cpu().data.numpy()
            else:
                weights_after = model.fc1.weight.cpu().data.numpy()
        else:
            device = torch.device('cpu')
            model = model.to(device)
            weights_before = deepcopy(model.fc1.weight.data.numpy())
            update(self.torch_input_data, op, model)
            weights_after = model.fc1.weight.data.numpy()
        self.assertFalse(is_equal(weights_before, weights_after))

    def test_tf_model_end_to_end_gradient(self):
        train_data, _ = mnist.load_data()
        pipeline = fe.Pipeline(train_data=train_data,
                               batch_size=4,
                               ops=[ExpandDims(inputs="x", outputs="x"), Minmax(inputs="x", outputs="x")])

        model = fe.build(model_fn=LeNet_tf, optimizer_fn="adam")
        network = fe.Network(ops=[
            ModelOp(model=model, inputs="x", outputs="y_pred"),
            CrossEntropy(inputs=("y_pred", "y"), outputs="ce"),
            GradientOp(model=model, finals="ce", outputs="gradients"),
            UpdateOp(model=model, gradients="gradients", loss_name="ce")
        ])
        estimator = fe.Estimator(pipeline=pipeline, network=network, epochs=2, max_train_steps_per_epoch=10)
        estimator.fit()

    def test_torch_model_end_to_end_gradient(self):
        train_data, _ = mnist.load_data()
        pipeline = fe.Pipeline(train_data=train_data,
                               batch_size=4,
                               ops=[ExpandDims(inputs="x", outputs="x", axis=0), Minmax(inputs="x", outputs="x")])

        model = fe.build(model_fn=LeNet_torch, optimizer_fn="adam")
        network = fe.Network(ops=[
            ModelOp(model=model, inputs="x", outputs="y_pred"),
            CrossEntropy(inputs=("y_pred", "y"), outputs="ce"),
            GradientOp(model=model, finals="ce", outputs="gradients"),
            UpdateOp(model=model, gradients="gradients", loss_name="ce")
        ])
        estimator = fe.Estimator(pipeline=pipeline, network=network, epochs=2, max_train_steps_per_epoch=10)
        estimator.fit()


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

"""
Привязка урлов к функциям сервиса чатбота https://github.com/Koziev/chatbot.
"""

from __future__ import print_function

from flask import request
from flask import render_template
from flask import redirect
from flask import jsonify

#from bot_service import flask_app
from .dialog_form import DialogForm
from .dialog_phrase import DialogPhrase
from .rest_service_core import flask_app

user_id = 'anonymous'

@flask_app.route('/start', methods=["GET"])
def start():
    # Чтобы заранее заставить бота загрузить все модели с диска.
    return redirect('/index')
    pass

@flask_app.route('/', methods=["GET", "POST"])
@flask_app.route('/index', methods=["GET", "POST"])
def index():
    # покажем веб-форму, в которой пользователь сможет ввести
    # свою реплику и увидеть ответ бота
    form = DialogForm()
    bot = flask_app.config['bot']

    # todo - сохранять и восстанавливать историю диалога через БД...
    DIALOG_HISTORY = 'dialog_history'
    if DIALOG_HISTORY not in flask_app.config:
        flask_app.config[DIALOG_HISTORY] = dict()

    if user_id not in flask_app.config[DIALOG_HISTORY]:
        flask_app.config[DIALOG_HISTORY][user_id] = []

    phrases = flask_app.config[DIALOG_HISTORY][user_id][:]

    if form.validate_on_submit():
        # Пользователь ввел свою реплику, обрабатываем ее.
        # flash('full_name={}'.format(form.full_name.data))
        utterance = form.utterance.data
        if len(utterance) > 0:
            phrases.append(DialogPhrase(utterance, user_id, False))
            bot.push_phrase(user_id, utterance)

    if request.method == 'GET':
        bot.start_conversation(user_id)

    while True:
        answer = bot.pop_phrase(user_id)
        if len(answer) == 0:
            break
        phrases.append(DialogPhrase(answer, 'chatbot', True))

    flask_app.config[DIALOG_HISTORY][user_id] = phrases

    return render_template('dialog_form.html', hphrases=phrases, form=form)

@flask_app.route('/start_conversation', methods=["GET"])
def start_conversation():
    user = request.args.get('user', 'anonymous')
    bot_id = request.args.get('bot', None)  # TODO - использовать, когда появится мультипрофильность
    bot = flask_app.config['bot']
    bot.start_conversation(user)
    return jsonify({'processed': True})

@flask_app.route('/push_phrase', methods=["GET"])
def push_phrase():
    phrase = request.args['phrase']
    user = request.args.get('user', 'anonymous')
    bot_id = request.args.get('bot', None)  # TODO - использовать, когда появится мультипрофильность
    bot = flask_app.config['bot']
    bot.push_phrase(user, phrase)
    return jsonify({'processed': True})

@flask_app.route('/pop_phrase', methods=["GET"])
def pop_phrase():
    user = request.args.get('user', 'anonymous')
    bot_id = request.args.get('bot', None)  # TODO - использовать, когда появится мультипрофильность
    bot = flask_app.config['bot']
    reply = bot.pop_phrase(user)
    return jsonify({'reply': reply})

@flask_app.route('/cancel_all_running_items', methods=["GET"])
def cancel_all_running_items():
    user = request.args.get('user', 'anonymous')
    bot_id = request.args.get('bot', None)  # TODO - использовать, когда появится мультипрофильность
    bot = flask_app.config['bot']

    bot.cancel_all_running_items(user)
    bot.reset_session(user)
    bot.cancel_all_running_items(user)
    bot.reset_usage_stat()
    bot.reset_added_facts()

    return jsonify({'processed': True})


nums = int(input("how many fibonacci numbers: "))
a1 = 1
a2 = 1
while nums < 2:
  nums = int(input("enter a number: "))
if nums >= 2:
  list_fib = [a1, a2]
  for i in range(3, nums+1):
    a3 = a1 + a2
    list_fib.append(a3)
    a1 = a2
    a2 = a3
print(list_fib)


# Thirdparty libraries
import numpy as np
import tensorflow as tf
import data_reader

from scipy import signal
import os
from clusterone import get_data_path, get_logs_path

sess = tf.InteractiveSession()

PATH_TO_LOCAL_LOGS = os.path.expanduser('~/Desktop/Clusterone/ML_Ising/logs/')
ROOT_PATH_TO_LOCAL_DATA = os.path.expanduser('~/Desktop/Clusterone/ML_Ising')

try:
  job_name = os.environ['JOB_NAME']
  task_index = os.environ['TASK_INDEX']
  ps_hosts = os.environ['PS_HOSTS']
  worker_hosts = os.environ['WORKER_HOSTS']
except:
  job_name = None
  task_index = 0
  ps_hosts = None
  worker_hosts = None

flags = tf.app.flags

# Training related flags
flags.DEFINE_string("data_dir",
                    get_data_path(
                        dataset_name = "kelvinchngphysicist/2d10", #all mounted repo
                        local_root = ROOT_PATH_TO_LOCAL_DATA,
                        local_repo = "2d10",
                        path = ''
                        ),
                    "Path to store logs and checkpoints. It is recommended"
                    "to use get_logs_path() to define your logs directory."
                    "so that you can switch from local to clusterone without"
                    "changing your code."
                    "If you set your logs directory manually make sure"
                    "to use /logs/ when running on ClusterOne cloud.")
flags.DEFINE_string("log_dir",
                     get_logs_path(root=PATH_TO_LOCAL_LOGS),
                    "Path to dataset. It is recommended to use get_data_path()"
                    "to define your data directory.so that you can switch "
                    "from local to clusterone without changing your code."
                    "If you set the data directory manually makue sure to use"
                    "/data/ as root path when running on ClusterOne cloud.")

FLAGS = flags.FLAGS

n_x = 10

Ising = data_reader.insert_file_info(FLAGS.data_dir+"2D%d"%(n_x)+"_p_%.1d.txt", np.arange(1,3), load_test_data_only=False)
Ising = Ising.categorize_data()

n_output_neuron = 2

filter_d = 2
filter_h = 2

n_feature_map1 = 16
n_feature_map2 = 8
n_fully_connected_neuron = 16

batch_size = 50
n_train_data = 50000

n_steps = 8000
save_freq = 200
n_save = n_steps/save_freq
n_train_data = len(Ising.train.labels)
n_epochs = (float(n_steps)*batch_size)/n_train_data
epochs = np.linspace(0,n_epochs,n_save)

eta_policy = 'cyclical_traig' # 'test'
etaMax = 0.00035
eta0 = 1e-6
n_train_iter = int(float(n_train_data)*n_epochs/batch_size)

if eta_policy in ['test'] :
    eta_lower_bound = max(1e-16,eta0)
    eta0 = eta_lower_bound
    print('Learning rate', '%s - %s'  %(eta_lower_bound, etaMax))
    eta = ((eta_lower_bound/etaMax)**np.linspace(1,0,n_train_iter))*etaMax

if eta_policy in ['cyclical_traig', 'Cyclial triangular'] :
    eta0 = etaMax/4.
    n_train_iter_per_epoch =n_train_iter/n_epochs

    if n_epochs > 9 :
        cycle_multiplier=min(2,n_epochs)
    else :
        cycle_multiplier=1

    eta_tmp = signal.triang(n_train_iter_per_epoch*cycle_multiplier)*(etaMax-eta0)+eta0
    cycle = int(n_epochs/float(cycle_multiplier))

    eta = np.ones(n_train_iter)*eta0
    for i in range(cycle) :
        start = int((i*cycle_multiplier)*n_train_iter_per_epoch)
        end   = int(((i+1)*cycle_multiplier)*n_train_iter_per_epoch)
        eta[start:end] = eta_tmp

def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)

def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)

def conv2d(x, W):
  return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='VALID')

def max_pool_2x2(x):
  return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
                        strides=[1, 2, 2, 1], padding='VALID')

def bn(inputs, training=True) :
    return tf.contrib.layers.batch_norm(inputs,
                                         center=True,
                                         scale=True,
                                         is_training=training)

x = tf.placeholder(tf.float32, [None, n_x*n_x])
y_ = tf.placeholder(tf.float32, [None, n_output_neuron])

# Feature extraction layer -----------------------------------------------------------

# First Convolution Layer
W_conv1 = weight_variable([filter_d,filter_h,1,n_feature_map1])
b_conv1 = bias_variable([n_feature_map1])

x_image = tf.reshape(x, [-1,n_x,n_x,1])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)

# Second Convolution Layer
W_conv2 = weight_variable([filter_d,filter_h,n_feature_map1,n_feature_map2])
b_conv2 = bias_variable([n_feature_map2])

h_conv2 = tf.nn.relu(conv2d(h_conv1, W_conv2) + b_conv2)

# Classification layer ---------------------------------------------------------------

# Fully-connected Layer
W_fc1 = weight_variable([8*8*n_feature_map2, n_fully_connected_neuron])
b_fc1 = bias_variable([n_fully_connected_neuron])

h_conv1_flat = tf.reshape(h_conv2, [-1, 8*8*n_feature_map2])
h_fc1 = tf.nn.relu(tf.matmul(h_conv1_flat, W_fc1) + b_fc1)

# Dropout
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

# Readout layer
W_fc2 = weight_variable([n_fully_connected_neuron,n_output_neuron])
b_fc2 = bias_variable([n_output_neuron])

y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)

learning_rate = tf.placeholder(tf.float32,shape=[])

# Train and Evaluate the Model
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv), reduction_indices=[1]))
# tf.train.exponential_decay(learning_rate, global_step, decay_steps, decay_rate, staircase=False)
# Use adaptive learning rate
#global_step = tf.Variable(0, trainable=False)
# eta = tf.train.exponential_decay(eta0, global_step*batch_size, n_train_data, decay_rate)
train_step = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# Before Variables can be used within a session, they must be initialized
# using that session.
sess.run(tf.global_variables_initializer())

# Training

training_table = np.zeros((n_save,4))
training_table[:,0] = epochs

n = 0
for i in range(n_steps):

    batch = Ising.train.next_batch(50)
    if eta_policy in ['test'] :
      if i % save_freq == 0:
        test_accuracy = accuracy.eval(feed_dict={
            x: Ising.test.images, y_: Ising.test.labels, keep_prob: 1.0})
        training_table[n,0], training_table[n,1] = eta[i], test_accuracy
        print('step %d, eta %g, testing accuracy %g' % (i, eta[i], test_accuracy))
        n+=1
    else :
      if i % save_freq == 0:
        train_accuracy = accuracy.eval(feed_dict={
            x: Ising.train.images, y_: Ising.train.labels, keep_prob: 1.0})
        test_accuracy = accuracy.eval(feed_dict={
            x: Ising.test.images, y_: Ising.test.labels, keep_prob: 1.0})
        training_table[n,1], training_table[n,2], training_table[n,3] = eta[i], train_accuracy, test_accuracy
        print('step %d, training accuracy %g, testing accuracy %g' % (i, train_accuracy, test_accuracy))
        n+=1
    train_step.run(feed_dict={x: batch[0], y_: batch[1], learning_rate: eta[i], keep_prob: 0.5})

if eta_policy in ['test'] :
    np.savetxt(FLAGS.log_dir+"/Ising2D10_LRtest_eta%.3e_%.3e.txt"%(eta0,etaMax),training_table[:,:2])
else :
    np.savetxt(FLAGS.log_dir+"/Ising2D10_CLR_eta%.3e_%.3e.txt"%(eta0,etaMax),training_table)

if eta_policy not in ['test'] :
    n_test_data = len(Ising.test.labels)

    print('test accuracy %g' % accuracy.eval(feed_dict={
        x: Ising.test.images, y_: Ising.test.labels, keep_prob: 1.0}))

    n_temp = len(np.unique(Ising.test.temps))
    table = np.zeros((n_temp,2))
    table[:,0] = np.unique(Ising.test.temps)

    for i in range(n_test_data) :
        table[Ising.test.temps[i]==table[:,0],1] += np.argmax(y_conv.eval(feed_dict={x: Ising.test.images[i,:].reshape(1,100), keep_prob: 1.0}))

    np.savetxt(FLAGS.log_dir+"/Ising2D10_output_eta%.3e_%.3e.txt"%(eta0,etaMax),table)


# https://projecteuler.net/problem=74
from math import factorial
from time import time
T = time()

def sum_factorial_digits(n):
    result = 0
    for digit in str(n):
        result += factorial(int(digit))

    return result

def chain_length(n):
    chain = [n]
    length_of_chain = 0
    while chain.count(chain[-1]) != 2:
        length_of_chain += 1
        chain.append(sum_factorial_digits(chain[-1]))

    return length_of_chain

count = 0
for i in range(1, 10**6):
    if chain_length(i) == 60:
        count += 1

print(count)
print("time elapsed:", time() - T)


import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("Socket successfully created")

port = 12345
s.bind(('localhost', port))
print("socket binded to %s" %(port))

s.listen(5)
print("socket is listening")

conn, addr = s.accept()
print('Got connection from : ',addr)

conn.send(b'Thank you for connecting')

data = conn.recv(1024).decode('utf-8')
print("The sent string is : "+str(data))

data1 = data[::-1]

conn.send(data1.encode('utf-8'))

if(data1==data):
    conn.send(b'The string is palindrome')
else:
    conn.send(b'The string is not palindrome')
conn.close()

'''Output :
> python tcp_server.py
Socket successfully created
socket binded to 12345
socket is listening
Got connection from :  ('127.0.0.1', 52551)
The sent string is : neen
'''

Computer Networks/Codes/pktfiles and codes/tcp_server.py


import logging
import datetime
import Pyro.core

class InterceptorManager(object):
    _ins = None
    def ins():
        if not InterceptorManager._ins:
            InterceptorManager._ins = InterceptorManager()
        return InterceptorManager._ins
    ins = staticmethod(ins)

    def __init__(self):
        self.interceptor_keys = {}
        self.cur_key_number = 0

    def register(self):
        key = "i_" + str(self.cur_key_number)

        self.interceptor_keys[key] = datetime.datetime.now()
        logging.info("registered interceptor: " + key)
        self.cur_key_number = self.cur_key_number + 1
        return key

    def update_alive_heartbeat(self, key):
        logging.debug("update_alive_heartbeat is called from: " + key)
        self.interceptor_keys[key] = datetime.datetime.now()

    def update_interceptor_keys(self):
        time_out_keys = []
        timedelta = datetime.timedelta(seconds=10)

        for k, v in self.interceptor_keys.iteritems():
            if v < datetime.datetime.now() - timedelta:
                time_out_keys.append(k)

        for k in time_out_keys:
            logging.info("time out removed interceptor: " + k)
            self.interceptor_keys.pop(k)

class NexusServiceForInterceptor(Pyro.core.ObjBase):
    def register(self):
        manager = InterceptorManager.ins()
        register_key = manager.register()

        logging.info("interceptor is registered: %s" % register_key)
        return register_key

    def get_interceptor_list(self):
        manager = InterceptorManager.ins()
        return manager.interceptor_keys

    def is_alive(self, key):
        manager = InterceptorManager.ins()
        manager.update_alive_heartbeat(key)
        return True


#calss header
class _INFLAMMATORY():
	def __init__(self,):
		self.name = "INFLAMMATORY"
		self.definitions = [u'intended or likely to cause anger or hate: ', u'causing or related to swelling and pain in the body']

		self.parents = []
		self.childen = []
		self.properties = []
		self.jsondata = {}

		self.specie = 'adjectives'

	def run(self, obj1, obj2):
		self.jsondata[obj2] = {}
		self.jsondata[obj2]['properties'] = self.name.lower()
		return self.jsondata


def define_class(text):
    if text == "0":
        svg_class = "st0"
    else:
        svg_class = "st1"
    return svg_class

def get_header(w, h):
    # print(w)
    # w = 420
    w = w*10
    h = h+30
    header = """<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 22.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg"
    xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
    viewBox="0 0 """ + str(w) + """ """ + str(h) + """" style="enable-background:new 0 0 """ + str(w) + """ """ + str(h) + """;" xml:space="preserve">
<style type="text/css">
    .st0{fill:#FFFFFF;padding: 0;margin: 0;border: 0;stroke: 0}
    .st1{padding: 0;margin: 0;border: 0;stroke: 0}
</style>"""
    return header

def get_footer():
    footer = """</svg>"""

    return footer


#!/usr/bin/python3
""" Cleans Up the whole deployment"""
from datetime import datetime
from fabric.api import *
import tarfile
import os

env.hosts = ["35.227.16.88", "54.144.228.69"]
env.user = "ubuntu"

def do_clean(number=0):
    """ Deletes """
    number = int(number)
    if number < 2:
        number = 1
    number += 1
    number = str(number)
    with lcd("versions"):
        local("ls -1t | grep web_static_.*\.tgz | tail -n +" +
              number + " | xargs -I {} rm -- {}")
    with cd("/data/web_static/releases"):
        run("ls -1t | grep web_static_ | tail -n +" +
            number + " | xargs -I {} rm -rf -- {}")


class Node:
    def __init__(self, data=None):
        self.data = data
        self.next = None

    def __str__(self):
        return "<Node %s>" % str(self.data)

if __name__ == "__main__":
    nd1 = Node(8)
    nd2 = Node("ucu")
    nd3 = Node("Apple")
    nd1.next = nd2
    nd2.next = nd3

    tmp = nd1
    while tmp is not None:
        print(tmp, end="=>")
        tmp = tmp.next
    print()


from twisted.internet.defer import inlineCallbacks
from twisted.internet import reactor
from autobahn.twisted.wamp import ApplicationSession, ApplicationRunner
from twisted.internet.task import LoopingCall

import sys

class MyComponent(ApplicationSession):

    @inlineCallbacks
    def onJoin(self, details):
        res = yield self.call('com.pentest.sleep')
        print(res)
        reactor.stop()

if __name__ == '__main__':
    runner = ApplicationRunner(
        url=u"ws://rentouch2.nine.ch:8091",
        realm=u"realm1",
        debug_wamp=False,  # optional; log many WAMP details
        debug=False,  # optional; log even more details
    )
    runner.run(MyComponent)


from rest_framework import relations, serializers
from tests import models
import rest_framework

class CommentSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        fields = ("id", "url", "post", "body", )
        model = models.Comment

class PersonSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        fields = ("id", "url", "name", )
        model = models.Person

class PostSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        fields = ("id", "url", "title", "author", "comments", )
        model = models.Post

class MaximalPersonSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        fields = ("id", "url", "name", "favorite_post", "liked_comments")
        model = models.Person

class MinimalCommentSerializer(CommentSerializer):

    class Meta(CommentSerializer.Meta):
        fields = ("id", "url", "body", )

class MinimalPostSerializer(PostSerializer):

    class Meta(PostSerializer.Meta):
        fields = ("id", "url", "title", )

class NestedCommentSerializer(CommentSerializer):
    post = MinimalPostSerializer()

class NestedPostSerializer(PostSerializer):
    comments = MinimalCommentSerializer(many=True)

class PkCommentSerializer(CommentSerializer):
    post = relations.PrimaryKeyRelatedField(queryset=models.Post.objects)

single_related_kwargs = {}

if rest_framework.__version__.split(".")[0] >= "3":
    single_related_kwargs = {"allow_null": True}

class PkMaximalPersonSerializer(MaximalPersonSerializer):
    favorite_post = relations.PrimaryKeyRelatedField(required=False, queryset=models.Post.objects, **single_related_kwargs)
    liked_comments = relations.PrimaryKeyRelatedField(required=False, many=True, queryset=models.Comment.objects)


from django.shortcuts import render
from . import models as quiz_game_models
import random

# Create your views here.
def question_view(request, question_id=None):
    try:
        question = quiz_game_models.Question.objects.get(question_id=question_id)
        correct_answer = quiz_game_models.QuestionAnswer.objects.get(question=question, correct=True)
        get_answers = quiz_game_models.QuestionAnswer.objects.filter(question=question, correct=False)
        answers = list(get_answers)
        answers.append(correct_answer)
        random.shuffle(answers)
        context = {
            'question': question,
            'answers': answers
        }
        return render(request, 'quiz_game/question_view.html', context=context)
    except quiz_game_models.Question.DoesNotExist:
        return render(request, 'base.html')

def question_check(request, question_id=None, answer_id=None, answers=None):
    question = quiz_game_models.Question.objects.get(question_id=question_id)
    answer = quiz_game_models.Answer.objects.get(aid=answer_id)
    result = quiz_game_models.QuestionAnswer.objects.get(answer=answer, question=question)

    context = {
        'question': question,
        'result': result,
        'answers': 'lol'
    }

    return render(request, 'quiz_game/question_result.html', context=context)


#!/usr/bin/python
# Author: Ellis Adigvom <ellisadigvom@gmail.com>
from bar import Bar
import time
import os
import widgets

PID_FILE = '/home/ellis/.bar_pid'

if __name__ == '__main__':
    bar = Bar()
    bar.foreground = 'black'
    bar.height = '25'
    bar.font = ['siji:size=8', 'ohsnap:size=8']
    bar.padding = '  '

    mpdwidget = widgets.MpdWidget()
    mpdwidget.background = 'dark_green'
    mpdwidget.icon = ''

    wifiwidget = widgets.WiFiWidget()
    wifiwidget.background = 'dark_blue'
    wifiwidget.icon = ''

    batterywidget = widgets.BatteryWidget()
    batterywidget.icon = ''
    batterywidget.hide_value = 80
    batterywidget.background = 'blue'

    clockwidget = widgets.ClockWidget()
    clockwidget.background = 'red'
    clockwidget.icon = ''

    workspacewidget = widgets.BSPWMWorkspaceWidget()
    workspacewidget.labels = [i for i in 'rchlinux']
    workspacewidget.background = 'dark_blue'

    mailwidget = widgets.MailWidget()
    mailwidget.background = 'dark_red'
    mailwidget.icon = ''

    bar.add(mpdwidget, 'r')
    bar.add(wifiwidget, 'r')
    bar.add(batterywidget, 'r')
    bar.add(mailwidget, 'r')
    bar.add(clockwidget, 'r')
    bar.add(workspacewidget, 'l')

    bar.start()

    with open(PID_FILE, 'w') as f:
        f.write(str(os.getpid()))
        f.write('\n')
        f.write(str(bar.get_pid()))

    while 1:
        time.sleep(30)


# -*- coding:utf-8 -*-
from mako import runtime, filters, cache
UNDEFINED = runtime.UNDEFINED
__M_dict_builtin = dict
__M_locals_builtin = locals
_magic_number = 10
_modified_time = 1431513043.265439
_enable_loop = True
_template_filename = 'htdocs/form_response.mako'
_template_uri = 'form_response.mako'
_source_encoding = 'utf-8'
_exports = []

def inputs(form_args):
    """
    Creates list of input elements
    """
    element = ""
    for name, value in form_args.items():
        element += "<input type=\"hidden\" name=\"%s\" value=\"%s\"/>" % (name,
                                                                          value)
    return element

def render_body(context,**pageargs):
    __M_caller = context.caller_stack._push_frame()
    try:
        __M_locals = __M_dict_builtin(pageargs=pageargs)
        action = context.get('action', UNDEFINED)
        form_args = context.get('form_args', UNDEFINED)
        __M_writer = context.writer()
        __M_writer(u'\n\n<html>\n  <head>\n    <title>Submit This Form</title>\n  </head>\n  <body onload="javascript:document.forms[0].submit()">\n    <form method="post" action=')
        __M_writer(unicode(action))
        __M_writer(u'>\n        ')
        __M_writer(unicode(inputs(form_args)))
        __M_writer(u'\n    </form>\n  </body>\n</html>\n')
        return ''
    finally:
        context.caller_stack._pop_frame()

"""
__M_BEGIN_METADATA
{"source_encoding": "utf-8", "line_map": {"34": 11, "35": 18, "36": 18, "37": 19, "38": 19, "44": 38, "15": 1, "27": 0}, "uri": "form_response.mako", "filename": "htdocs/form_response.mako"}
__M_END_METADATA
"""


import datetime
from enum import Enum

from marshmallow_enum import EnumField

from app import db, ma

'''
-----
Enums
-----
'''

class SenderType(Enum):
    USER = "USER"
    BOT = "BOT"

class PersonType(Enum):
    LANDLORD = "LANDLORD"
    TENANT = "TENANT"

class ClaimCategory(Enum):
    LEASE_TERMINATION = "LEASE_TERMINATION"
    RENT_CHANGE = "RENT_CHANGE"
    NONPAYMENT = "NONPAYMENT"
    DEPOSITS = "DEPOSITS"

class DocumentType(Enum):
    LEASE = "LEASE"

'''
-----------------
SQLAlchemy Models
-----------------
'''

class Conversation(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(40), nullable=False)
    person_type = db.Column(db.Enum(PersonType))
    claim_category = db.Column(db.Enum(ClaimCategory))
    current_fact = db.Column(db.String(50))
    messages = db.relationship('Message')
    facts = db.relationship('Fact')
    files = db.relationship('File')

class Message(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    conversation_id = db.Column(db.Integer, db.ForeignKey('conversation.id'))
    sender_type = db.Column(db.Enum(SenderType), nullable=False)
    timestamp = db.Column(db.DateTime, default=datetime.datetime.utcnow)
    text = db.Column(db.Text, nullable=False)
    file_request = db.relationship('FileRequest', uselist=False, backref='message')
    possible_answers = db.Column(db.Text)
    enforce_possible_answer = db.Column(db.Boolean)

    def request_file(self, document_type):
        file_request = FileRequest(document_type=document_type)
        self.file_request = file_request

class FileRequest(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    message_id = db.Column(db.Integer, db.ForeignKey('message.id'))
    document_type = db.Column(db.Enum(DocumentType), nullable=False)

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

class Fact(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    conversation_id = db.Column(db.Integer, db.ForeignKey('conversation.id'))
    name = db.Column(db.String(50), nullable=False)
    value = db.Column(db.String(50), nullable=False)

    def __repr__(self):
        return "{}:{}".format(self.name, self.value)

class File(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    conversation_id = db.Column(db.Integer, db.ForeignKey('conversation.id'))
    name = db.Column(db.String(50), nullable=False)
    type = db.Column(db.String(50), nullable=False)
    path = db.Column(db.String(100))
    timestamp = db.Column(db.DateTime, default=datetime.datetime.utcnow)

# Create tables
print("Creating database tables from models.py")
db.create_all()

'''
-------------------
Marshmallow Schemas
-------------------
'''

class FileRequestSchema(ma.ModelSchema):
    # Enum
    document_type = EnumField(DocumentType, by_value=True)

    class Meta:
        fields = ['document_type']

class MessageSchema(ma.ModelSchema):
    # Enum
    sender_type = EnumField(SenderType, by_value=True)

    # One to one
    file_request = ma.Nested(FileRequestSchema)

    class Meta:
        model = Message

class FactSchema(ma.ModelSchema):
    class Meta:
        model = Fact

class FileSchema(ma.ModelSchema):
    class Meta:
        fields = ('id', 'name', 'type', 'timestamp')

class ConversationSchema(ma.ModelSchema):
    # Enum
    person_type = EnumField(PersonType, by_value=True)
    claim_category = EnumField(ClaimCategory, by_value=True)

    # One to many
    messages = ma.Nested(MessageSchema, many=True)
    facts = ma.Nested(FactSchema, many=True)

    class Meta:
        fields = ('id', 'name', 'person_type', 'messages', 'facts')


import os
from django.shortcuts import render_to_response, render, get_object_or_404
from django.contrib.auth.models import User
from openshift.models import SkillEntry, InterestEntry
#from openshift.models import UserProfile, SkillEntry

def members_profile(request, username):
    member = get_object_or_404(User.objects.filter(username=username))
    skills = SkillEntry.objects.filter(user=member.id).order_by('proficiency')
    interests = InterestEntry.objects.filter(user=member.id).order_by('level')

    for interest in interests:
        interest.label_class = 'default'
        if interest.level == InterestEntry.NOT_MUCH_INTERESTED:
            interest.level_text = 'Not much Interested'
            interest.label_class = 'info'
        elif interest.level == InterestEntry.INTERESTED:
            interest.level_text = 'Interested'
            interest.label_class = 'success'
        elif interest.level == InterestEntry.VERY_INTERESTED:
            interest.level_text = 'Very Interested'
            interest.label_class = 'important'

    for skill in skills:
        skill.label_class = 'default'
        if skill.proficiency == SkillEntry.STUDENT:
            skill.proficiency_text = 'Student'
            skill.label_class = 'info'
        elif skill.proficiency == SkillEntry.BEGINNER:
            skill.proficiency_text = 'Beginner'
            skill.label_class = 'success'
        elif skill.proficiency == SkillEntry.INTERMEDIATE:
            skill.proficiency_text = 'Intermediate'
            skill.label_class = 'warning'
        elif skill.proficiency == SkillEntry.ADVANCED:
            skill.proficiency_text = 'Advanced'
            skill.label_class = 'important'
    #for user in users:
      #  skills = []
     #   SkillEntry.objects.filter(user=user)
        #skills.append(skill.name)
        #user.skills = skills.join(',')
    return render(request, 'members_profile.html', { 'member': member, 'skills': skills, 'interests': interests } )


#!/usr/bin/env python
from sqlalchemy import orm
# cancerhermit
from pgvcs import aggregate, function, _regprocedure

class regprocedure(_regprocedure):
    """init_on_load class assigment only"""
    __table__ = _regprocedure.__table__

    @orm.reconstructor
    def init_on_load(self):
        super(type(self),self).init_on_load()
        if self.proisagg==True:
            self.__class__ = aggregate
        else:
            self.__class__ = function


#Peter Smith
#2016
#Unnamed
#Indev 1.5

try:
    input = raw_input
    print("using python 2\n\n")
except:
    print("using python 3\n\n")
    pass

import random
import math
from bruce_imports import randomPick
from map1 import MAP1_SPACES
from Map import Map
from Inventory import Inventory
from Item import Basic_Sword, Great_Sword, Basic_Shield

OPP_TYPE_CHANCES_1 = [
    ('Ogre', 50),
    ('Dragon', 40),
    ('Giant Ogre', 10),
 ]

class Entity(object):
    """
    Players and opponents
    """
    level = int(1)
    attackDamage = int(0)
    attackCount = int(0)
    maxHealth = int(0)
    health = maxHealth
    defense = int(0)
    def __init__(self):
        self.inventory = Inventory()
    def attack(self, other, otherInstantDefend):
        """
        Called when an entity(self) attacks another entity(other) during a fight. Reduces "other" health.

        otherInstantDefend = whether or not "other" is attempting to block an attack

        returns:
        otherInstantDamage = the damage dealt to "other" from a singular attack
        otherTotalInstantDamage = the total damage dealt to "other", factoring in attackCount
        """
        otherInstantDamage = int(self.attackDamage - other.defense)
        if otherInstantDamage <= 0:
            otherInstantDamage = 1
        otherTotalInstantDamage = int(otherInstantDamage * self.attackCount)
        if otherInstantDefend == 1:
            ## otherTotalInstantDamage = 0
            selfAttackSuccessCount = self.attackCount - other.inventory.offHandItem.defendCount
            otherTotalInstantDamage = selfAttackSuccessCount * self.attackDamage
        other.health -= otherTotalInstantDamage
        return otherInstantDefend, otherTotalInstantDamage
    pass

class Opponent(Entity):
    typeString = "Type"
    behavior = int(0)
    behaviorString = "Behavior"
    expReward = int(100)
    pass

class Player(Entity):
    expStorage = int(0)
    expQuota = int(500)
    skillPoints = int(0)
    statSTR = int(0)
    statDEF = int(0)
    statAGI = int(0)
    statELITE = int(0)
    statDEATH = int(0)
    def playerUpgrade(self, skillPoints):
        print("Upgrade strength, defense, or agility.")
        while True:
            print("\nMake sure they add up to "+str(skillPoints)+".\n")
            statSTR = int(input("Strength will increase attack damage: "))
            statDEF = int(input("\nDefense will decrease damage to the player: "))
            statAGI = int(input("\nAgility will allow you attack more than once per turn: "))
            if statSTR + statDEF + statAGI == skillPoints:
                print("\nGreat! Let's adventure.")
                break
        self.statSTR += statSTR
        self.statDEF += statDEF
        self.statAGI += statAGI
        self.playerStatUpdate()

    def playerStatUpdateBegin(self):
        if self.statELITE == 0 and self.statDEATH == 0:
            self.level = int(1)
            self.maxHealth = int(20)
            self.health = self.maxHealth
        elif self.statELITE == 1:
            self.level = int(1337)
            self.maxHealth = int(1337)
            self.health = self.maxHealth
        elif self.statDEATH == 1:
            self.level = int(1)
            self.maxHealth = int(1)
            self.health = int(1)

    def playerStatUpdate(self):
        weaponReqAgi = self.inventory.mainHandItem.reqAGI
        weaponAttackDamage = self.inventory.mainHandItem.baseDamage
        bonusAttackDamage = self.statSTR/2
        if self.statELITE == 0 and self.statDEATH == 0:
            self.attackDamage = weaponAttackDamage + bonusAttackDamage
            self.attackCount = int(1) + (self.statAGI/weaponReqAgi)
            self.defense = self.statDEF/2
        elif self.statELITE == 1:
            self.attackDamage = int(1337)
            self.attackCount = int(1337)
            self.defense = int(1337)
        elif self.statDEATH == 1:
            self.attackDamage = weaponAttackDamage + bonusAttackDamage
            self.attackCount = int(1) + (self.statAGI/weaponReqAgi)
            self.defense = int(0)
    pass

current_map = Map(MAP1_SPACES)
current_space = None

def doGame():
    global current_space
    player = Player()
    while True:
        print("Choose your stats (strength, defense, and agility).")
        while True:
            print("\nMake sure they add up to 12.\n")
            statSTR = int(input("Strength will increase attack damage: "))
            statDEF = int(input("\nDefense will decrease damage to the player: "))
            statAGI = int(input("\nAgility will allow you attack more than once per turn: "))
            if statSTR + statDEF + statAGI == 12:
                player.statELITE = int(0)
                player.statDEATH = int(0)
                print("\nGreat! Let's adventure.")
                break
            elif statSTR + statDEF + statAGI == 4011:
                player.statELITE = int(1)
                player.statDEATH = int(0)
                print("\nElite mode activated. Let's adventure.")
                break
            if statSTR == 0 and statDEF == 0 and statAGI == 0:
                player.statDEATH = int(1)
                player.statELITE = int(0)
                print("\nLet's hope you're ready for this...")
                break

        player.statSTR = statSTR
        player.statDEF = statDEF
        player.statAGI = statAGI

        player.inventory.equipMainHand(Basic_Sword())
        player.inventory.equipOffHand(Basic_Shield())

        weaponAttackDamage = player.inventory.mainHandItem.baseDamage
        weaponReqAgi = player.inventory.mainHandItem.reqAGI

        player.playerStatUpdateBegin()
        player.playerStatUpdate()

        player.expStorage = int(0)

        if player.statDEATH == 0:
            print("\nplayer health is "+str(player.health)+".\nplayer attack damage is "+str(player.attackDamage)+".\nplayer attack count is "+str(player.attackCount)+".\nplayer defense is "+str(player.defense))
        elif player.statDEATH == 1:
            print("\nplayer health is unimportant.\nplayer attack damage is "+str(player.attackDamage)+".\nplayer attack count is "+str(player.attackCount)+".\nplayer defense is unimportant.")

        current_space = current_map.spaceAtPos(0)

        allowOpp = True

        while True:
            if current_space.possOppTypes and allowOpp:
                # make opponent and fight it
                doOpp(player)
                allowOpp = False
                if player.health <= 0:
                    break
            else:
                # decide what to do when there isn't an opponent
                print(str(current_space.description))
                action = input("What is your action? Try 'list'\n")
                if action == "list" or action == "l" or action == "List" or action == "L":
                    print("List - list all available actions\n"
                          "Stats - show stats of player\n"
                          "Upgrade - upgrade your stats (must have leveled up)\n"
                          "Move [direction] - move somewhere else\n")
                elif action == "stats" or action == "s" or action == "Stats" or action == "S":
                    if player.statDEATH == 0:
                        print("Player's level = "+str(player.level)+
                              "\nPlayer's experience = "+str(player.expStorage)+
                              "\nExperience needed to level up = "+str(player.expQuota)+
                              "\nPlayer's health = "+str(player.health)+
                              "\nPlayer's attack damage = "+str(player.attackDamage)+
                              "\nPlayer's attack count = "+str(player.attackCount)+
                              "\nPlayer's defense = "+str(player.defense))
                    elif player.statDEATH == 1:
                        print("Player's level = "+str(player.level)+
                              "\nPlayer's experience = "+str(player.expStorage)+
                              "\nExperience needed to level up = "+str(player.expQuota)+
                              "\nPlayer's attack damage = "+str(player.attackDamage)+
                              "\nPlayer's attack count = "+str(player.attackCount))
                elif action in ["move east", "east", "e"]:
                    current_space = current_map.spaceAtPos(current_space.position + 1)
                    allowOpp = True
                elif action in ["move west", "west", "w"]:
                    current_space = current_map.spaceAtPos(current_space.position - 1)
                    allowOpp = True
                elif action in ["upgrade", "Upgrade", "u", "U", "up", "Up"]:
                    if player.skillPoints <= 0:
                        print("Level up to gain skill points.")
                    elif player.skillPoints > 0:
                        player.playerUpgrade(player.skillPoints)
                elif action == "gs":
                    player.inventory.equipMainHand(Great_Sword())
                    player.playerStatUpdate()
                    print("Gs ok")
                elif action == "sp":
                    player.skillPoints += 3
                    print("Sp ok")
                elif action in ["backpack", "Backpack", "b", "B", "inventory", "Inventory", "inv", "Inv", "i", "I", "pack", "Pack", "Storage", "storage"]:
                    if player.inventory.offHandItem.nameString == "THP":
                        print("\nBoth hands = "+player.inventory.mainHandItem.nameString+"\n")
                    else:
                        print("\nOff hand = "+player.inventory.offHandItem.nameString+"\nMain hand = "+player.inventory.mainHandItem.nameString+"\n")
                    if player.inventory.backpackList:
                        for item in player.inventory.backpackList:
                            print(item.nameString)
                    else:
                        print("Nothing here...")

def doOpp(player):
    """
    Makes opponent and makes it interact with a player
    """

    opp = Opponent()

    oppType = None
    while oppType not in current_space.possOppTypes:
        oppType = randomPick(OPP_TYPE_CHANCES_1)

    opp.typeString = str(oppType)
    if oppType == 'Ogre':
        opp.typeString = "Ogre"
        opp.attackDamage = int(6)
        opp.attackCount = int(1)
        opp.maxHealth = int(30)
        opp.health = opp.maxHealth
        opp.defense = int(0)
        opp.expReward = int(100)
    elif oppType == 'Dragon':
        opp.typeString = "Dragon"
        opp.attackDamage = int(5)
        opp.attackCount = int(3)
        opp.maxHealth = int(20)
        opp.health = opp.maxHealth
        opp.defense = int(0)
        opp.expReward = int(120)
    elif oppType == 'Giant Ogre':
        opp.typeString = "Giant Ogre"
        opp.attackDamage = int(10)
        opp.attackCount = int(1)
        opp.maxHealth = int(90)
        opp.health = opp.maxHealth
        opp.defense = int(0)
        opp.expReward = int(500)
    else:
        print("INTERNAL ERROR: oppType is strange")

    opp.behavior = random.randint(0,2)
    opp.behaviorString = str(opp.behavior)
    if opp.behavior == 0:
        opp.behaviorString = "Ordinary"
    elif opp.behavior == 1:
        opp.behaviorString = "Aggressive"
    elif opp.behavior == 2:
        opp.behaviorString = "Defensive"

    opp.level = None
    while opp.level not in current_space.possOppLevels:
        opp.level = random.randint(1,3)

    opp.levelString = str(opp.level)
    if opp.level == 2:
        opp.expReward = opp.expReward + int(round(0.3*opp.expReward))
        opp.maxHealth = opp.maxHealth + int(round(0.1*opp.maxHealth))
        opp.health = opp.maxHealth
        opp.attackDamage = opp.attackDamage + 1
    elif opp.level == 3:
        opp.expReward = opp.expReward + int(round(0.5*opp.expReward))
        opp.maxHealth = opp.maxHealth + int(round(0.2*opp.maxHealth))
        opp.health = opp.maxHealth
        opp.attackDamage = opp.attackDamage + 2
    while True:
        runOrFight = input("A Level "+str(opp.level)+" "+opp.typeString+" approaches. Run or fight?\n")
        if runOrFight in ["run", "r", "Run", "R", "fight", "f", "Fight", "F"]:
            break
        else:
            pass
    if runOrFight in ["run", "r", "Run", "R"]:
        pass
    elif runOrFight in ["fight", "f", "Fight", "F"]:
        doFight(player, opp)

def doFight(player, opp):
        oppInstantDefend = 0
        playerInstantDefend = 0
        turn = int(0)
        oppAttackTension = int(0)
        oppDefendTension = int(0)
        while True:
            if opp.health <= 0:
                print("\nYou have vanquished the "+opp.typeString+"!\n")
                player.expStorage += opp.expReward
                if player.expStorage >= player.expQuota:
                    player.level += 1
                    player.expQuota += 500 * player.level
                    player.skillPoints += 3
                break
            elif player.health <= 0:
                print("\nYou have been vanquished by the "+opp.typeString+"!\n\n")
                break
            elif turn % 2 == 0:
                action = input("What is your action? Try 'list'\n")
                if action == "list" or action == "l" or action == "List" or action == "L":
                    print("List - list all available actions\n"
                          "Stats - show stats of player and opponent\n"
                          "Run - flee from the enemy\n"
                          "Observe - bide your time and observe the enemy\n"
                          "Attack - go on the offensive and attack the enemy!\n"
                          "Defend - defend against an oncoming attack from the enemy\n")

                elif action == "stats" or action == "s" or action == "Stats" or action == "S":
                    if player.statDEATH == 0:
                        print("Player's level = "+str(player.level)+
                              "\n\nPlayer's experience = "+str(player.expStorage)+
                              "\nExperience needed to level up = "+str(player.expQuota)+
                              "\nPlayer's health = "+str(player.health)+
                              "\nPlayer's attack damage = "+str(player.attackDamage)+
                              "\nPlayer's attack count = "+str(player.attackCount)+
                              "\nPlayer's defense = "+str(player.defense))

                        print("\nOpponent's level = "+str(opp.level)+
                              "\nOpponent's health = "+str(opp.health)+
                              "\nOpponent's attack damage = "+str(opp.attackDamage)+
                              "\nOpponent's attack count = "+str(opp.attackCount)+
                              "\nOpponent's defense = "+str(opp.defense))
                    elif player.statDEATH == 1:
                        print("Player's level = "+str(player.level)+
                              "\n\nPlayer's experience = "+str(player.expStorage)+
                              "\nExperience needed to level up = "+str(player.expQuota)+
                              "\nPlayer's attack damage = "+str(player.attackDamage)+
                              "\nPlayer's attack count = "+str(player.attackCount))

                        print("\nOpponent's level = "+str(opp.level)+
                              "\nOpponent's health = "+str(opp.health)+
                              "\nOpponent's attack damage = "+str(opp.attackDamage)+
                              "\nOpponent's attack count = "+str(opp.attackCount)+
                              "\nOpponent's defense = "+str(opp.defense))

                elif action == "observe" or action == "o" or action == "Observe" or action == "O":
                    if opp.behavior == 0:
                        print("It seems just like any other "+opp.typeString+". You watch each other carefully.")
                    elif opp.behavior == 1:
                        print("The "+opp.typeString+" seems more aggressive than usual. You watch each other carefully.")
                    elif opp.behavior == 2:
                        print("The "+opp.typeString+" seems cautious and calculating. You watch each other carefully.")
                    turn += 1
                    oppInstantDefend = 0

                elif action == "attack" or action == "a" or action == "Attack" or action == "A":
                    oppInstantDefend, oppTotalInstantDamage = player.attack(opp, oppInstantDefend)
                    if oppInstantDefend == 0:
                        if player.attackCount == 1:
                            print("You swing your weapon at the "+opp.typeString+". They took "+str(oppTotalInstantDamage)+" damage.")
                        else:
                            print("You swing your weapon at the "+opp.typeString+" "+str(player.attackCount)+" times. They took "+str(oppTotalInstantDamage)+" damage.")
                    elif oppInstantDefend == 1:
                        if player.attackCount == 1:
                            print("You swing your weapon at the "+opp.typeString+". They block the attack!")
                        else:
                            print("You swing your weapon at the "+opp.typeString+" "+str(player.attackCount)+" times. They block each attack!")
                    turn += 1
                    oppInstantDefend = 0

                elif action == "defend" or action == "d" or action == "Defend" or action == "D":
                    playerInstantDefend = 1
                    print("You prepare to block the "+opp.typeString+"'s next attack.")
                    turn += 1
                    oppInstantDefend = 0

                elif action == "run" or action == "r" or action == "Run" or action == "R":
                    runAttack = random.randint(1,3)
                    if runAttack == 3:
                        player.health -= int(round(opp.attackDamage * 0.67))
                        if player.health > 0:
                            print("The "+opp.typeString+" is too quick for you and hits you as you flee. You escape with a minor injury.\n")
                        else:
                            print("Tough luck! The "+opp.typeString+" hits you as you flee and it is too much.\n")
                            print("\nYou have been vanquished by the "+opp.typeString+"!\n\n")

                    elif runAttack == 1 or runAttack == 2:
                        print("The "+opp.typeString+" attacks as you run. You barely escape unscathed.\n")
                    break
            else:
                if opp.behavior == 0:
                    oppAttackTension += int(4)
                    oppDefendTension += int(3)
                elif opp.behavior == 1:
                    oppAttackTension += int(6)
                    oppDefendTension += int(2)
                elif opp.behavior == 2:
                    oppAttackTension += int(3)
                    oppDefendTension += int(6)
                if oppAttackTension >= 12:
                    playerInstantDefend, playerTotalInstantDamage = opp.attack(player, playerInstantDefend)
                    if playerInstantDefend == 0:
                        if opp.attackCount == 1:
                            print("\nThe "+opp.typeString+" attacks. You took "+str(playerTotalInstantDamage)+" damage.\n")
                        else:
                            print("\nThe "+opp.typeString+" attacks "+str(opp.attackCount)+" times. You took "+str(playerTotalInstantDamage)+" damage.\n")
                    elif playerInstantDefend == 1:
                        if opp.attackCount == 1:
                            print("\nThe "+opp.typeString+" attacks, but you block it with your shield.\n")
                        else:
                            print("\nThe "+opp.typeString+" attacks "+str(opp.attackCount)+" times. You block each one with your shield.\n")
                    oppAttackTension = 0
                elif oppDefendTension >= 12:
                    oppInstantDefend = 1
                    print("\nOpponent does nothing.\n")
                else:
                    print("\nOpponent does nothing.\n")
                turn += 1
                playerInstantDefend = 0

doGame()


import biom
import math
import numpy as np
import pandas as pd
import torch
from torch.utils.data import Dataset

class BiomDataset(Dataset):
    """Loads a `.biom` file.

    Parameters
    ----------
    filename : Path
        Filepath to biom table
    metadata_file : Path
        Filepath to sample metadata
    batch_category : str
        Column name forr batch indices
    """
    def __init__(
            self,
            table: biom.Table,
            metadata: pd.DataFrame = None,
            batch_category: str = None):
        super(BiomDataset).__init__()
        self.table = table
        self.metadata = metadata
        self.batch_category = batch_category
        self.populate()

    def populate(self):

        if self.metadata is not None:
            # match the metadata with the table
            ids = set(self.table.ids()) & set(self.metadata.index)
            filter_f = lambda v, i, m: i in ids
            self.table = self.table.filter(filter_f, axis='sample')
            self.metadata = self.metadata.loc[self.table.ids()]
            if self.metadata.index.name is None:
                raise ValueError('`Index` must have a name either'
                                 '`sampleid`, `sample-id` or #SampleID')
            self.index_name = self.metadata.index.name
            self.metadata = self.metadata.reset_index()

        self.batch_indices = None
        if self.batch_category is not None and self.metadata is not None:
            batch_cats = np.unique(self.metadata[self.batch_category].values)
            self.batch_cats = pd.Series(
                np.arange(len(batch_cats)), index=batch_cats)
            self.batch_indices = np.array(
                list(map(lambda x: self.batch_cats.loc[x],
                         self.metadata[self.batch_category].values)))

    def __len__(self) -> int:
        return len(self.table.ids())

    def __getitem__(self, i):
        """ Returns all of the samples for a given subject

        Returns
        -------
        counts : np.array
            OTU counts for specified samples.
        batch_indices : np.array
            Membership ids for batch samples. If not specified, return None.
        """
        sample_idx = self.table.ids()[i]
        if self.batch_indices is not None:
            batch_indices = self.batch_indices[i]
        else:
            batch_indices = None
        counts = self.table.data(id=sample_idx, axis='sample')
        return counts, batch_indices

    def __iter__(self):
        worker_info = torch.utils.data.get_worker_info()
        start = 0
        end = self.__len__()

        if worker_info is None:  # single-process data loading
            for i in range(end):
                yield self.__getitem__(i)
        else:
            worker_id = worker_info.id
            w = float(worker_info.num_workers)
            t = (end - start)
            w = float(worker_info.num_workers)
            per_worker = int(math.ceil(t / w))
            worker_id = worker_info.id
            iter_start = start + worker_id * per_worker
            iter_end = min(iter_start + per_worker, end)
            for i in range(iter_start, iter_end):
                yield self.__getitem__(i)

def _sample2dict(feature_data, feature_ids, i, sample_ids=None):
    # This is taking code from here
    # https://github.com/qiime2/q2-sample-classifier/
    # blob/master/q2_sample_classifier/utilities.py#L64
    features = np.empty(1, dtype=dict)
    row = feature_data[i]
    features = {feature_ids[ix]: d
                for ix, d in zip(row.indices, row.data)}
    return features, i

class Q2BiomDataset(BiomDataset):
    """ This is specific to q2 sample classifiers. """
    def __init__(self, table: biom.Table):
        super(Q2BiomDataset).__init__()
        self.feature_ids = table.ids('observation')
        self.feature_data = table.matrix_data.T.tocsr()

    def __getitem__(self, i):
        return _sample2dict(self.feature_data, self.feature_ids, i)

def collate_single_f(batch):
    counts_list = np.vstack([b[0] for b in batch])
    counts = torch.from_numpy(counts_list).float()
    return counts

def collate_batch_f(batch):
    counts_list = np.vstack([b[0] for b in batch])
    batch_ids = np.vstack([b[1] for b in batch])
    counts = torch.from_numpy(counts_list).float()
    batch_ids = torch.from_numpy(batch_ids).long()
    return counts, batch_ids.squeeze()

def collate_q2_f(batch):
    features = [b[0] for b in batch]
    sample_idx = np.vstack([b[1] for b in batch])
    return features, sample_idx.squeeze()