import collections, util, math, random
import numpy as np

############################################################

############################################################
# Problem 2a

class ValueIteration(util.MDPAlgorithm):

    # Implement value iteration.  First, compute V_opt using the methods
    # discussed in class.  Once you have computed V_opt, compute the optimal
    # policy pi.  Note that ValueIteration is an instance of util.MDPAlgrotithm,
    # which means you will need to set pi and V (see util.py).
    def solve(self, mdp, epsilon=0.001):
        mdp.computeStates()

        v_opt_old = dict.fromkeys(mdp.states, 0)
        self.pi = dict.fromkeys(mdp.states, None)

        max_change = float('inf')
        while max_change > epsilon: #while v_opt is changing, keep looping
            self.V = dict.fromkeys(mdp.states, -float('inf'))

            for state in mdp.states: #loop through states
                for action in mdp.actions(state): #loop though actions

                    q_opt = 0 #for adding up the possibilities from successor states
                    for succesor in mdp.succAndProbReward(state, action): #loop through sucessor states
                        q_opt += succesor[1] * (succesor[2] + mdp.discount()*v_opt_old[succesor[0]])

                    if q_opt > self.V[state]: #take best action and set optimal policy
                        self.V[state] = q_opt
                        self.pi[state] = action

            #compute stopping criterion
            delta_dict = {key: abs(self.V[key] - v_opt_old.get(key, 0)) for key in self.V.keys()}
            max_change = delta_dict[max(delta_dict, key=delta_dict.get)]
            v_opt_old = self.V.copy()

############################################################
class ser_fd(util.MDP):
    def __init__(self):
        """
        cardValues: array of card values for each card type
        multiplicity: number of each card type
        """

    # Return the start state. First is a indicator of whether on upper or lower trajectory, second is step in the task
    def startState(self):
        return (0, 0)

    # Return set of actions possible from |state|.
    def actions(self, state):
        A, stage = state
        if stage == 0 or stage == 1:
            return ['Next']
        else:
            return ['Go', 'Dont']

    # Return a list of (newState, prob, reward) tuples corresponding to edges
    # coming out of |state|.
    def succAndProbReward(self, state, action):
        succ_prob_reward = []
        stim, stage = state

        #stage 0, A
        #stage 1, B which leads to dot
        #stage 2, B which doesnt lead to dot

        if stage is None:
            return []
        elif stage == 0:
            succ_prob_reward.append(((0,1),.5,0))
            succ_prob_reward.append(((1,1),.5,0))
        elif stage == 1 and stim == 0: #A trial
            succ_prob_reward.append(((0,2),.95,0))
            succ_prob_reward.append(((1,2),.05,0))
        elif stage == 1 and stim == 1:
            succ_prob_reward.append(((0,2),.05,0))
            succ_prob_reward.append(((1,2),.95,0))
        elif stage == 2 and stim ==  0 and action == 'Go': #B+ and respond
            succ_prob_reward.append(((None,None),.95,1))
            succ_prob_reward.append(((None,None),.05,-1))
        elif stage == 2 and stim ==  0 and action == 'Dont': #B+ and dont respond
            succ_prob_reward.append(((None,None),.95,-.1))
            succ_prob_reward.append(((None,None),.05,.1))
        elif stage == 2 and stim ==  1 and action == 'Go': #B- and respond
            succ_prob_reward.append(((None,None),.95,-1))
            succ_prob_reward.append(((None,None),.05,1))
        elif stage == 2 and stim ==  1 and action == 'Dont': #B- and dont respond
            succ_prob_reward.append(((None,None),.95,.1))
            succ_prob_reward.append(((None,None),.05,-.1))

        return succ_prob_reward

    def discount(self):
        return 1

############################################################
class ser_fd_features(util.MDP):
    def __init__(self):
        """
        cardValues: array of card values for each card type
        multiplicity: number of each card type
        """

    # Return the start state. First is a indicator of whether on upper or lower trajectory, second is step in the task
    def startState(self):
        return (0,0,0)

    # Return set of actions possible from |state|.
    def actions(self, state):
        A,B,stage = state
        if stage == 0:
            return ['Next']
        else:
            return ['Go', 'Dont']

    # Return a list of (newState, prob, reward) tuples corresponding to edges
    # coming out of |state|.
    def succAndProbReward(self, state, action):
        succ_prob_reward = []
        A,B,stage = state

        if stage is None:
            return []
        elif stage == 0:
            succ_prob_reward.append(((1,100,1),.5,0))
            succ_prob_reward.append(((0,100,1),.5,0))
        elif stage == 1 and A == 1 and action == 'Dont': #A trial
            succ_prob_reward.append(((2,1,2),1,0))
        elif stage == 1 and A == 1 and action == 'Go': #A trial
            succ_prob_reward.append(((2,1,2),1,-1))
        elif stage == 1 and A == 0 and action == 'Dont':
            succ_prob_reward.append(((3,1,2),1,0))
        elif stage == 1 and A == 0 and action == 'Go':
            succ_prob_reward.append(((3,1,2),1,-1))

        # elif stage == 2 and A == 2 and action == 'Go': #A -> B+ and respond
        #     succ_prob_reward.append(((None,None,None),.95,1))
        #     succ_prob_reward.append(((None,None,None),.05,-1))
        # elif stage == 2 and A == 2 and action == 'Dont': #A -> B+ and dont respond
        #     succ_prob_reward.append(((None,None,None),.95,-.1))
        #     succ_prob_reward.append(((None,None,None),.05,.1))
        # elif stage == 2 and A == 3 and action == 'Go': #B- and respond
        #     succ_prob_reward.append(((None,None,None),.95,-1))
        #     succ_prob_reward.append(((None,None,None),.05,1))
        # elif stage == 2 and A == 3 and action == 'Dont': #B- and dont respond
        #     succ_prob_reward.append(((None,None,None),.95,.1))
        #     succ_prob_reward.append(((None,None,None),.05,-.1))

        elif stage == 2 and A == 2 and action == 'Go': #A -> B+ and respond
            succ_prob_reward.append(((None,None,None),.95,-1))
            succ_prob_reward.append(((None,None,None),.05,1))
        elif stage == 2 and A == 2 and action == 'Dont': #A -> B+ and dont respond
            succ_prob_reward.append(((None,None,None),.95,.1))
            succ_prob_reward.append(((None,None,None),.05,-.1))
        elif stage == 2 and A == 3 and action == 'Go': #B- and respond
            succ_prob_reward.append(((None,None,None),.95,1))
            succ_prob_reward.append(((None,None,None),.05,-1))
        elif stage == 2 and A == 3 and action == 'Dont': #B- and dont respond
            succ_prob_reward.append(((None,None,None),.95,-.1))
            succ_prob_reward.append(((None,None,None),.05,.1))
        return succ_prob_reward

    def discount(self):
        return 1

############################################################
class sim_fd_features(util.MDP):
    def __init__(self):
        """
        cardValues: array of card values for each card type
        multiplicity: number of each card type
        """

    # Return the start state. First is a indicator of whether on upper or lower trajectory, second is step in the task
    def startState(self):
        return (0,0,0)

    # Return set of actions possible from |state|.
    def actions(self, state):
        A,B,stage = state
        if stage == 0:
            return ['Next']
        else:
            return ['Go', 'Dont']

    # Return a list of (newState, prob, reward) tuples corresponding to edges
    # coming out of |state|.
    def succAndProbReward(self, state, action):
        succ_prob_reward = []
        A,B,stage = state

        if stage is None:
            return []
        elif stage == 0:
            succ_prob_reward.append(((1,1,1),.5,0))
            succ_prob_reward.append(((0,1,1),.5,0))
        elif stage == 1 and A == 1 and action == 'Go': #AB+ and respond
            succ_prob_reward.append(((None,None,None),.95,1))
            succ_prob_reward.append(((None,None,None),.05,-1))
        elif stage == 1 and A == 1 and action == 'Dont': #A -> B+ and dont respond
            succ_prob_reward.append(((None,None,None),.95,-.1))
            succ_prob_reward.append(((None,None,None),.05,.1))
        elif stage == 1 and A == 0 and action == 'Go': #B- and respond
            succ_prob_reward.append(((None,None,None),.95,-1))
            succ_prob_reward.append(((None,None,None),.05,1))
        elif stage == 1 and A == 0 and action == 'Dont': #B- and dont respond
            succ_prob_reward.append(((None,None,None),.95,.1))
            succ_prob_reward.append(((None,None,None),.05,-.1))
        # elif stage == 1 and A == 1 and action == 'Go': #AB+ and respond
        #     succ_prob_reward.append(((None,None,None),.95,-1))
        #     succ_prob_reward.append(((None,None,None),.05,1))
        # elif stage == 1 and A == 1 and action == 'Dont': #A -> B+ and dont respond
        #     succ_prob_reward.append(((None,None,None),.95,.1))
        #     succ_prob_reward.append(((None,None,None),.05,-.1))
        # elif stage == 1 and A == 0 and action == 'Go': #B- and respond
        #     succ_prob_reward.append(((None,None,None),.95,1))
        #     succ_prob_reward.append(((None,None,None),.05,-1))
        # elif stage == 1 and A == 0 and action == 'Dont': #B- and dont respond
        #     succ_prob_reward.append(((None,None,None),.95,-.1))
        #     succ_prob_reward.append(((None,None,None),.05,.1))

        return succ_prob_reward

    def discount(self):
        return 1

class phase_2_blocking(util.MDP):
    def __init__(self):
        """
        cardValues: array of card values for each card type
        multiplicity: number of each card type
        """

    # Return the start state. First is a indicator of whether on upper or lower trajectory, second is step in the task
    def startState(self):
        return (0,0,0)

    # Return set of actions possible from |state|.
    def actions(self, state):
        A,B,stage = state
        if stage == 0:
            return ['Next']
        else:
            return ['Go', 'Dont']

    # Return a list of (newState, prob, reward) tuples corresponding to edges
    # coming out of |state|.
    def succAndProbReward(self, state, action):
        succ_prob_reward = []
        A,B,stage = state

        if stage is None:
            return []
        elif stage == 0:
            succ_prob_reward.append(((1,10,1),.5,0))
            succ_prob_reward.append(((11,12,1),.5,0))
        elif stage == 1 and A == 1 and action == 'Go': #AC+ and respond
            succ_prob_reward.append(((None,None,None),.95,1))
            succ_prob_reward.append(((None,None,None),.05,-1))
        elif stage == 1 and A == 1 and action == 'Dont': #AC+ and dont respond
            succ_prob_reward.append(((None,None,None),.95,-.1))
            succ_prob_reward.append(((None,None,None),.05,.1))
        elif stage == 1 and A == 11 and action == 'Go': #DE- and respond
            succ_prob_reward.append(((None,None,None),.95,-1))
            succ_prob_reward.append(((None,None,None),.05,1))
        elif stage == 1 and A == 11 and action == 'Dont': #DE- and dont respond
            succ_prob_reward.append(((None,None,None),.95,.1))
            succ_prob_reward.append(((None,None,None),.05,-.1))

        return succ_prob_reward

    def discount(self):
        return 1

############################################################
# Performs Q-learning.  Read util.RLAlgorithm for more information.
# actions: a function that takes a state and returns a list of actions.
# discount: a number between 0 and 1, which determines the discount factor
# featureExtractor: a function that takes a state and action and returns a list of (feature name, feature value) pairs.
# explorationProb: the epsilon value indicating how frequently the policy
# returns a random action
class QLearningAlgorithm(util.RLAlgorithm):
    def __init__(self, actions, discount, featureExtractor, init_weights = collections.Counter(), explorationProb=0.2):
        self.actions = actions
        self.discount = discount
        self.featureExtractor = featureExtractor
        self.explorationProb = explorationProb
        self.weights = init_weights
        self.numIters = 0

    # Return the Q function associated with the weights and features
    def getQ(self, state, action):
        score = 0
        for f, v in self.featureExtractor(state, action):
            score += self.weights[f] * v
        return score

    # This algorithm will produce an action given a state.
    # Here we use the epsilon-greedy algorithm: with probability
    # |explorationProb|, take a random action.
    def getAction(self, state):
        self.numIters += 1
        if random.random() < self.explorationProb:
            return random.choice(self.actions(state))
        else:
            return max((self.getQ(state, action), action) for action in self.actions(state))[1]

    # Call this function to get the step size to update the weights.
    def getStepSize(self):
        return 1.0 / math.sqrt(self.numIters)

    # We will call this function with (s, a, r, s'), which you should use to update |weights|.
    # Note that if s is a terminal state, then s' will be None.  Remember to check for this.
    # You should update the weights using self.getStepSize(); use
    # self.getQ() to compute the current estimate of the parameters.
    def incorporateFeedback(self, state, action, reward, newState):
        if newState == None:
            maxQ = 0
        else:
            maxQ = max((self.getQ(newState, act), act) for act in self.actions(newState))[0]
        r = reward + self.discount*maxQ - self.getQ(state, action)

        for f, v in self.featureExtractor(state, action):
            self.weights[f] = self.weights[f] + self.getStepSize() * r * v

# Return a singleton list containing indicator feature for the (state, action)
# pair.  Provides no generalization.
def identityFeatureExtractor(state, action):
    featureKey = (state, action)
    featureValue = 1
    return [(featureKey, featureValue)]

############################################################
def serFeatureExtractor(state,action):
    A,B,stage = state
    if stage == None:
        return []
    featureValue = 1 #indicator features
    features = []
    if A == 0:
        featureKey = ('noA',action)
    elif A == 1:
        featureKey = ('A',action)
    elif A == 2:
        featureKey = ('pastA',action)
    elif A==3:
        featureKey = ('no_pastA',action)
    elif A == 11:
        featureKey = ('D',action)

    features.append((featureKey,featureValue))

    if B == 0:
        featureKey = ('noB',action)
    elif B==1:
        featureKey = ('B',action)
    elif B==10:
        featureKey = ('C',action)
    elif B==12:
        featureKey = ('E',action)
    features.append((featureKey,featureValue))

    #conjunctive features
    if A == 2 and B == 1:
        featureKey = ('pastA','B',action)
    if A == 1 and B == 1:
        featureKey = ('A','B',action)
    if A == 1 and B == 10:
        featureKey = ('A','C',action)
    if A == 11 and B == 12:
        featureKey = ('D','E',action)
    features.append((featureKey,featureValue))

    return features

# # run algorithms
# mdp = sim_fd_features()
# QLearning = QLearningAlgorithm(mdp.actions,mdp.discount(),serFeatureExtractor)
# rew_q = util.simulate(mdp, QLearning,numTrials=1000, maxIterations=1000)
#
# valueIter = ValueIteration()
# valueIter.solve(mdp)
# print valueIter.V
# print QLearning.weights
#
#
# #run subsequent
# mdp_phase2 = phase_2_blocking()
# QLearning_phase2 =  QLearningAlgorithm(mdp.actions,mdp.discount(),serFeatureExtractor,init_weights = QLearning.weights)
# rew_q2= util.simulate(mdp_phase2, QLearning_phase2,numTrials=25, maxIterations=1000)
#
# #get state q values
# for state in valueIter.pi:
#     print state
#     # print valueIter.pi[state]
#
#     actions = QLearning.actions(state)
#     if len(actions) > 1:
#        for action in actions:
#            print action
#            print QLearning.getQ(state,action)
#     else:
#         print actions
#         print QLearning.getQ(state,actions[0])
#
# ##get feature values
# print QLearning_phase2.weights
# print QLearning_phase2.weights[('C','Go')]
# print QLearning_phase2.weights[('C','Dont')]

c_go_out = '/Users/ianballard/Dropbox/fd/c_go_ser_fn.txt'
c_dont_out = '/Users/ianballard/Dropbox/fd/c_dont_ser_fn.txt'
num_iters = 100
c_go_weights = []
c_dont_weights = []
for i in range(0,num_iters):
    mdp = ser_fd_features()
    QLearning = QLearningAlgorithm(mdp.actions,mdp.discount(),serFeatureExtractor)
    rew_q = util.simulate(mdp, QLearning,numTrials=1000, maxIterations=1000)

    #run subsequent
    mdp_phase2 = phase_2_blocking()
    QLearning_phase2 =  QLearningAlgorithm(mdp.actions,mdp.discount(),serFeatureExtractor,init_weights = QLearning.weights)
    rew_q2= util.simulate(mdp_phase2, QLearning_phase2,numTrials=25, maxIterations=1000)

    c_go_weights.append(QLearning_phase2.weights[('C','Go')])
    c_dont_weights.append(QLearning_phase2.weights[('C','Dont')])

np.savetxt(c_go_out,c_go_weights)
np.savetxt(c_dont_out,c_dont_weights)


"""
2021.10.15
"""
from typing import List
import math

class BF:
    """
    TLE
    """
    def maxSubArray(self, nums: List[int]) -> int:
        n = len(nums)
        from math import inf
        max_ = float(-inf)
        for i in range(n):
            sum_ = 0
            for j in range(i, n):
                sum_ += nums[j]
                if sum_ > max_:
                    max_ = sum_
        return max_

class Kadane:
    """
    Kadane's algorithm: DP, greedy-like intuition behind it
    - The difficult part of the problem is figuring out when a negative number is "worth" keeping in a subarray
    - Whenever the sum of the array is negative, we know the entire array is not worth keeping,since the sum always subtract from the total
    which means the optimal solution would not include this kind of sub-array.
    so we'll reset it back to the empty array.
        - we can just keep an integer variable `current_subarray` and add the value of each element there.
        - When it becomes negative, we reset it to 0 (an empty array)

    - TC: O(n)
    """
    def maxSubArray(self, nums: List[int]) -> int:
        max_subarray = nums[0]
        current_subarray = nums[0]
        for i in range(1, len(nums)):
            # If current_subarray is negative, throw it away. Otherwise, keep adding to it: max(num, current_subarray+num)
            if current_subarray < 0:
                current_subarray = 0
            current_subarray += nums[i]

            max_subarray = max(max_subarray, current_subarray)
        return max_subarray

class DC:
    """
    TC: O(nlog(n))
    SC: O(log(n)), recursive depth

    This solution will make use of recursion
    """
    def maxSubArray(self, nums: List[int]) -> int:
        def findBestSubarray(nums, left, right):
            if left > right:
                return -math.inf

            mid = (left + right) // 2
            curr = best_left_sum = best_right_sum = 0

            # Iterate from the middle to the beginning
            for i in range(mid-1, left-1, -1):
                curr += nums[i]
                best_left_sum = max(best_left_sum, curr)

            # Reset curr and iterate from the middle to the end
            curr = 0
            for i in range(mid+1, right+1):
                curr += nums[i]
                best_right_sum = max(best_right_sum, curr)

            # The best_combined_sum uses the middle element and the best possible sum from each half
            best_combined_sum = nums[mid] + best_left_sum + best_right_sum

            # Find the best subarray possible from both halves
            left_half = findBestSubarray(nums, left, mid-1)
            right_half = findBestSubarray(nums, mid+1, left)

            # The largest of the 3 is the answer for any given input array
            return max(best_combined_sum, left_half, right_half)

        # helper function is designed to solve this problem for any array.
        return findBestSubarray(nums, 0, len(nums)-1)

if __name__ == "__main__":
    nums = [-2,1,-3,4,-1,2,1,-5,4]  # 6
    S = Kadane()
    print(S.maxSubArray(nums))


import json

def analyze(account_id, region, boto_session, boto_config, policy_analysis_function):
    refactor_spaces_client = boto_session.client(
        "migration-hub-refactor-spaces", config=boto_config, region_name=region
    )
    environments_paginator = refactor_spaces_client.get_paginator("list_environments")

    # Iterate all environments
    for environments_page in environments_paginator.paginate():
        for environment in environments_page["EnvironmentSummaryList"]:
            # Fetch the environment policy
            get_resource_policy_response = refactor_spaces_client.get_resource_policy(Identifier=environment["Arn"])

            # Skip if the environment does not have a policy configured
            if not json.loads(get_resource_policy_response["Policy"]):
                continue

            policy_analysis_function(
                account_id=account_id,
                region=region,
                boto_session=boto_session,
                resource_type="AWS::RefactorSpaces::Environment",
                resource_name=environment["Arn"].split("/")[-1],
                resource_arn=environment["Arn"],
                policy_document=get_resource_policy_response["Policy"],
                policy_type="RESOURCE_POLICY",
            )


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Author : zsy
Date : 2017/12/21"""
import os

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

import utils

tf.flags.DEFINE_string('data_dir', '/home/zsy/datasets/mnist-data', 'mnist data')
tf.flags.DEFINE_string('train_dir', '/home/zsy/train_dir/dcgan-mnist', 'events and ckpt dir')
tf.flags.DEFINE_integer('batch_size', 128, 'batch size m')
tf.flags.DEFINE_boolean('train', True, 'if training')
tf.flags.DEFINE_integer('num_steps', 30000, 'traing steps')

FLAGS = tf.flags.FLAGS

class DCGANmnist(object):
    def __init__(self):
        self.mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)
        self.netbuilder = utils.NetBuilder()
        self.z_dim = 100
        self.g_size = [4, 7, 14, 28]
        self.g_channels = [512, 256, 128, 1]
        self.g_last_channel = 1
        self.d_channels = [64, 128, 256, 512]
        self.d_last_channel = 1
        self.x_dim = self.mnist.train.images.shape[1]
        self.lr = 0.0002
        # 2 G after 1 D
        self.k = 2

    def generator(self, input, training=True):
        g_out1 = self.netbuilder.affine(input, self.z_dim, self.g_channels[0] * self.g_size[0] * self.g_size[0],
                                        activation=tf.nn.relu)
        g_out1 = tf.reshape(g_out1, shape=[-1, self.g_size[0], self.g_size[0], self.g_channels[0]])

        g_out2 = self.netbuilder.conv2d_trans(g_out1, [5, 5], [2, 2],
                                              [tf.shape(input)[0], self.g_size[1], self.g_size[1], self.g_channels[1]])
        g_out2 = tf.nn.relu(tf.layers.batch_normalization(g_out2, training=training))

        g_out3 = self.netbuilder.conv2d_trans(g_out2, [5, 5], [2, 2],
                                              [tf.shape(input)[0], self.g_size[2], self.g_size[2], self.g_channels[2]])
        g_out3 = tf.nn.relu(tf.layers.batch_normalization(g_out3, training=training))

        g_out4 = self.netbuilder.conv2d_trans(g_out3, [5, 5], [2, 2],
                                              [tf.shape(input)[0], self.g_size[3], self.g_size[3], self.g_channels[3]])
        output = tf.nn.sigmoid(g_out4)
        return output

    def discriminator(self, input, training=True):
        d_out1 = tf.layers.conv2d(input, self.d_channels[0], [5, 5], strides=(2, 2), padding='same')
        d_out1 = tf.nn.leaky_relu(d_out1, alpha=0.2)

        d_out2 = tf.layers.conv2d(d_out1, self.d_channels[1], [5, 5], strides=(2, 2), padding='same')
        d_out2 = tf.nn.leaky_relu(tf.layers.batch_normalization(d_out2, training=training), alpha=0.2)

        d_out3 = tf.layers.conv2d(d_out2, self.d_channels[2], [5, 5], strides=(2, 2), padding='same')
        d_out3 = tf.nn.leaky_relu(tf.layers.batch_normalization(d_out3, training=training), alpha=0.2)

        d_out4 = tf.layers.conv2d(d_out3, self.d_channels[3], [5, 5], strides=(2, 2), padding='same')
        d_out4 = tf.nn.leaky_relu(tf.layers.batch_normalization(d_out4, training=training), alpha=0.2)

        d_out4 = tf.layers.flatten(d_out4)
        output = tf.layers.dense(d_out4, 1, activation=tf.nn.sigmoid)
        return output

    def inference(self):
        self.z = tf.random_uniform([FLAGS.batch_size, self.z_dim], -1., 1.)
        self.x = tf.placeholder(tf.float32, [None, self.x_dim])
        # z
        with tf.variable_scope('generator'):
            self.gen_input = self.generator(self.z)
            gen_images = tf.reshape(self.gen_input, shape=[-1, 28, 28, 1])
            tf.summary.image('gen_images', gen_images, max_outputs=10)
        with tf.variable_scope('discriminator'):
            z_logit = self.discriminator(self.gen_input)
            # x
        with tf.variable_scope('discriminator', reuse=True):
            x = tf.reshape(self.x, [-1, 28, 28, 1])
            x_logit = self.discriminator(x)
            tf.summary.scalar('x_logit', tf.reduce_mean(x_logit))
            tf.summary.scalar('z_logit', tf.reduce_mean(z_logit))
        return z_logit, x_logit

    def loss(self):
        z_logit, x_logit = self.inference()
        d_loss = tf.reduce_mean(-(tf.log(1e-10 + x_logit) + tf.log(1e-10 + 1 - z_logit)))
        # d_loss = tf.reduce_mean(-(tf.log(1e-10 + x_logit) -tf.log(1e-10 + z_logit)))
        # g_loss = tf.reduce_mean(tf.log(1e-10 + 1 - z_logit))
        g_loss = tf.reduce_mean(-tf.log(1e-10 + z_logit))
        return d_loss, g_loss

    def train(self):
        global_step = tf.train.get_or_create_global_step()
        summary_writer = tf.summary.FileWriter(FLAGS.train_dir)
        d_loss, g_loss = self.loss()
        tf.summary.scalar('d_loss', d_loss)
        tf.summary.scalar('g_loss', g_loss)
        # merge_all should behind all summaries
        summary_op = tf.summary.merge_all()

        opt_d = tf.train.AdamOptimizer(self.lr, beta1=0.5)
        opt_g = tf.train.AdamOptimizer(self.lr, beta1=0.5)
        param_d = [v for v in tf.trainable_variables() if not v.name.startswith('generator')]
        print('param_d', [v.name for v in param_d])
        grads_d = tf.gradients(d_loss, param_d)
        param_g = [v for v in tf.trainable_variables() if not v.name.startswith('discriminator')]
        print('param_g', [v.name for v in param_g])
        grads_g = tf.gradients(g_loss, param_g)
        train_op_d = opt_d.apply_gradients(zip(grads_d, param_d), global_step=global_step)
        train_op_g = opt_g.apply_gradients(zip(grads_g, param_g))

        with tf.Session() as sess:
            index = 0
            utils.init_or_load_var(None, sess, None, global_step, initializer=tf.random_normal_initializer(stddev=0.02))
            for local_step in range(FLAGS.num_steps):
                batch_x, _ = self.mnist.train.next_batch(FLAGS.batch_size)
                feed_dict = {self.x: batch_x}
                # D
                _, d_loss_str, global_step_str = sess.run(
                    [train_op_d, d_loss, global_step], feed_dict=feed_dict)

                # update G twice
                for i in range(self.k):
                    _, g_loss_str = sess.run(
                        [train_op_g, g_loss], feed_dict=feed_dict)

                if global_step_str % 100 == 0:
                    print('step: %d\t\td_loss: %.2f\t\tg_loss: %.2f' % (global_step_str, d_loss_str, g_loss_str))
                    summary_str = sess.run(summary_op, feed_dict=feed_dict)
                    summary_writer.add_summary(summary_str, global_step_str)
                if global_step_str % 1000 == 0:
                    self.generate(sess, feed_dict, index)
                    index += 1

    def generate(self, sess, feed_dict, index):
        samples = sess.run(self.gen_input, feed_dict=feed_dict)

        fig = utils.plot(samples[0:16, :])
        path = os.path.join(FLAGS.train_dir, 'out/')
        if not os.path.exists(path):
            os.makedirs(path)
        plt.savefig(os.path.join(path + '{}.png'.format(str(index).zfill(3))), bbox_inches='tight')
        plt.close(fig)

def main(_):
    gan = DCGANmnist()
    gan.train()

if __name__ == '__main__':
    tf.app.run()


from __future__ import division
import torchvision.transforms as transforms_old
import torch
import math
import sys
import random
from PIL import Image

try:
    import accimage
except ImportError:
    accimage = None
import numpy as np
import numbers
import types
import collections
import warnings
import cv2
from torchvision.transforms import functional as F

if sys.version_info < (3, 3):
    Sequence = collections.Sequence
    Iterable = collections.Iterable
else:
    Sequence = collections.abc.Sequence
    Iterable = collections.abc.Iterable

__all__ = ["Compose", "ToTensor",
           "Lambda", "RandomApply", "RandomResizedCrop",
           "ColorJitter", "RandomGrayscale"]

_pil_interpolation_to_str = {
    Image.NEAREST: 'PIL.Image.NEAREST',
    Image.BILINEAR: 'PIL.Image.BILINEAR',
    Image.BICUBIC: 'PIL.Image.BICUBIC',
    Image.LANCZOS: 'PIL.Image.LANCZOS',
    Image.HAMMING: 'PIL.Image.HAMMING',
    Image.BOX: 'PIL.Image.BOX',
}

class ToPIL(object):
    def __call__(self, sample_rgb, sample_aux):
        img1 = sample_rgb
        img2 = sample_aux
        img1 = transforms_old.functional.to_pil_image(img1)
        img2 = transforms_old.functional.to_pil_image(img2)
        return img1, img2

class RandomResizedCrop(object):
    """Crop the given PIL Image to random size and aspect ratio.

    A crop of random size (default: of 0.08 to 1.0) of the original size and a random
    aspect ratio (default: of 3/4 to 4/3) of the original aspect ratio is made. This crop
    is finally resized to given size.
    This is popularly used to train the Inception networks.

    Args:
        size: expected output size of each edge
        scale: range of size of the origin size cropped
        ratio: range of aspect ratio of the origin aspect ratio cropped
        interpolation: Default: PIL.Image.BILINEAR
    """

    def __init__(self, size, scale=(0.08, 1.0), ratio=(3. / 4., 4. / 3.), interpolation=Image.BILINEAR):
        if isinstance(size, tuple):
            self.size = size
        else:
            self.size = (size, size)
        if (scale[0] > scale[1]) or (ratio[0] > ratio[1]):
            warnings.warn("range should be of kind (min, max)")

        self.interpolation = interpolation
        self.scale = scale
        self.ratio = ratio

    @staticmethod
    def get_params(img, scale, ratio):
        """Get parameters for ``crop`` for a random sized crop.

        Args:
            img (PIL Image): Image to be cropped.
            scale (tuple): range of size of the origin size cropped
            ratio (tuple): range of aspect ratio of the origin aspect ratio cropped

        Returns:
            tuple: params (i, j, h, w) to be passed to ``crop`` for a random
                sized crop.
        """
        area = img.size[0] * img.size[1]

        for attempt in range(10):
            target_area = random.uniform(*scale) * area
            log_ratio = (math.log(ratio[0]), math.log(ratio[1]))
            aspect_ratio = math.exp(random.uniform(*log_ratio))

            w = int(round(math.sqrt(target_area * aspect_ratio)))
            h = int(round(math.sqrt(target_area / aspect_ratio)))

            if w <= img.size[0] and h <= img.size[1]:
                i = random.randint(0, img.size[1] - h)
                j = random.randint(0, img.size[0] - w)
                return i, j, h, w

        # Fallback to central crop
        in_ratio = img.size[0] / img.size[1]
        if (in_ratio < min(ratio)):
            w = img.size[0]
            h = int(round(w / min(ratio)))
        elif (in_ratio > max(ratio)):
            h = img.size[1]
            w = int(round(h * max(ratio)))
        else:  # whole image
            w = img.size[0]
            h = img.size[1]
        i = (img.size[1] - h) // 2
        j = (img.size[0] - w) // 2
        return i, j, h, w

    def __call__(self, img_rgb, img_aux):
        """
        Args:
            img (PIL Image): Image to be cropped and resized.

        Returns:
            PIL Image: Randomly cropped and resized image.
        """
        i, j, h, w = self.get_params(img_rgb, self.scale, self.ratio)
        return F.resized_crop(img_rgb, i, j, h, w, self.size, self.interpolation), F.resized_crop(img_aux, i, j, h, w,
                                                                                self.size,
                                                                                self.interpolation)

    def __repr__(self):
        interpolate_str = _pil_interpolation_to_str[self.interpolation]
        format_string = self.__class__.__name__ + '(size={0}'.format(self.size)
        format_string += ', scale={0}'.format(tuple(round(s, 4) for s in self.scale))
        format_string += ', ratio={0}'.format(tuple(round(r, 4) for r in self.ratio))
        format_string += ', interpolation={0})'.format(interpolate_str)
        return format_string

class RandomHorizontalFlip(object):
    """Horizontally flip the given PIL Image randomly with a given probability.

    Args:
        p (float): probability of the image being flipped. Default value is 0.5
    """

    def __init__(self, p=0.5):
        self.p = p

    def __call__(self, img_rgb, img_aux):
        """
        Args:
            img (PIL Image): Image to be flipped.

        Returns:
            PIL Image: Randomly flipped image.
        """
        if random.random() < self.p:
            return F.hflip(img_rgb), F.hflip(img_aux)
        return img_rgb, img_aux

    def __repr__(self):
        return self.__class__.__name__ + '(p={})'.format(self.p)

class RandomTransforms(object):
    """Base class for a list of transformations with randomness

    Args:
        transforms (list or tuple): list of transformations
    """

    def __init__(self, transforms):
        assert isinstance(transforms, (list, tuple))
        self.transforms = transforms

    def __call__(self, *args, **kwargs):
        raise NotImplementedError()

    def __repr__(self):
        format_string = self.__class__.__name__ + '('
        for t in self.transforms:
            format_string += '\n'
            format_string += '    {0}'.format(t)
        format_string += '\n)'
        return format_string

class Compose(object):
    """Composes several transforms together.

    Args:
        transforms (list of ``Transform`` objects): list of transforms to compose.

    Example:
        >>> transforms.Compose([
        >>>     transforms.CenterCrop(10),
        >>>     transforms.ToTensor(),
        >>> ])
    """

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

    def __call__(self, img_rgb, img_aux):
        for t in self.transforms:
            img_rgb, img_aux = t(img_rgb, img_aux)
        return img_rgb, img_aux

    def __repr__(self):
        format_string = self.__class__.__name__ + '('
        for t in self.transforms:
            format_string += '\n'
            format_string += '    {0}'.format(t)
        format_string += '\n)'
        return format_string

class RandomApply(RandomTransforms):
    """Apply randomly a list of transformations with a given probability

    Args:
        transforms (list or tuple): list of transformations
        p (float): probability
    """

    def __init__(self, transforms, p=0.5):
        super(RandomApply, self).__init__(transforms)
        self.p = p

    def __call__(self, img_rgb, img_aux):
        if self.p < random.random():
            return img_rgb, img_aux
        for t in self.transforms:
            img_rgb, img_aux = t(img_rgb, img_aux)
        return img_rgb, img_aux

    def __repr__(self):
        format_string = self.__class__.__name__ + '('
        format_string += '\n    p={}'.format(self.p)
        for t in self.transforms:
            format_string += '\n'
            format_string += '    {0}'.format(t)
        format_string += '\n)'
        return format_string

class ColorJitter(object):
    """Randomly change the brightness, contrast and saturation of an image.

    Args:
        brightness (float or tuple of float (min, max)): How much to jitter brightness.
            brightness_factor is chosen uniformly from [max(0, 1 - brightness), 1 + brightness]
            or the given [min, max]. Should be non negative numbers.
        contrast (float or tuple of float (min, max)): How much to jitter contrast.
            contrast_factor is chosen uniformly from [max(0, 1 - contrast), 1 + contrast]
            or the given [min, max]. Should be non negative numbers.
        saturation (float or tuple of float (min, max)): How much to jitter saturation.
            saturation_factor is chosen uniformly from [max(0, 1 - saturation), 1 + saturation]
            or the given [min, max]. Should be non negative numbers.
        hue (float or tuple of float (min, max)): How much to jitter hue.
            hue_factor is chosen uniformly from [-hue, hue] or the given [min, max].
            Should have 0<= hue <= 0.5 or -0.5 <= min <= max <= 0.5.
    """

    def __init__(self, brightness=0, contrast=0, saturation=0, hue=0):
        self.brightness = self._check_input(brightness, 'brightness')
        self.contrast = self._check_input(contrast, 'contrast')
        self.saturation = self._check_input(saturation, 'saturation')
        self.hue = self._check_input(hue, 'hue', center=0, bound=(-0.5, 0.5),
                                     clip_first_on_zero=False)

    def _check_input(self, value, name, center=1, bound=(0, float('inf')), clip_first_on_zero=True):
        if isinstance(value, numbers.Number):
            if value < 0:
                raise ValueError("If {} is a single number, it must be non negative.".format(name))
            value = [center - value, center + value]
            if clip_first_on_zero:
                value[0] = max(value[0], 0)
        elif isinstance(value, (tuple, list)) and len(value) == 2:
            if not bound[0] <= value[0] <= value[1] <= bound[1]:
                raise ValueError("{} values should be between {}".format(name, bound))
        else:
            raise TypeError("{} should be a single number or a list/tuple with lenght 2.".format(name))

        # if value is 0 or (1., 1.) for brightness/contrast/saturation
        # or (0., 0.) for hue, do nothing
        if value[0] == value[1] == center:
            value = None
        return value

    @staticmethod
    def get_params(brightness, contrast, saturation, hue):
        """Get a randomized transform to be applied on image.

        Arguments are same as that of __init__.

        Returns:
            Transform which randomly adjusts brightness, contrast and
            saturation in a random order.
        """
        transforms = []

        if brightness is not None:
            brightness_factor = random.uniform(brightness[0], brightness[1])
            transforms.append(Lambda(lambda img: F.adjust_brightness(img, brightness_factor)))

        if contrast is not None:
            contrast_factor = random.uniform(contrast[0], contrast[1])
            transforms.append(Lambda(lambda img: F.adjust_contrast(img, contrast_factor)))

        if saturation is not None:
            saturation_factor = random.uniform(saturation[0], saturation[1])
            transforms.append(Lambda(lambda img: F.adjust_saturation(img, saturation_factor)))

        if hue is not None:
            hue_factor = random.uniform(hue[0], hue[1])
            transforms.append(Lambda(lambda img: F.adjust_hue(img, hue_factor)))

        random.shuffle(transforms)
        transform = transforms_old.Compose(transforms)

        return transform

    def __call__(self, img_rgb, img_aux):
        """
        Args:
            img (PIL Image): Input image.

        Returns:
            PIL Image: Color jittered image.
        """
        transform = self.get_params(self.brightness, self.contrast,
                                    self.saturation, self.hue)
        return transform(img_rgb),transform(img_aux)

    def __repr__(self):
        format_string = self.__class__.__name__ + '('
        format_string += 'brightness={0}'.format(self.brightness)
        format_string += ', contrast={0}'.format(self.contrast)
        format_string += ', saturation={0}'.format(self.saturation)
        format_string += ', hue={0})'.format(self.hue)
        return format_string

class Lambda(object):
    """Apply a user-defined lambda as a transform.

    Args:
        lambd (function): Lambda/function to be used for transform.
    """

    def __init__(self, lambd):
        assert callable(lambd), repr(type(lambd).__name__) + " object is not callable"
        self.lambd = lambd

    def __call__(self, img):
        return self.lambd(img)

    def __repr__(self):
        return self.__class__.__name__ + '()'

class RandomGrayscale(object):
    """Randomly convert image to grayscale with a probability of p (default 0.1).

    Args:
        p (float): probability that image should be converted to grayscale.

    Returns:
        PIL Image: Grayscale version of the input image with probability p and unchanged
        with probability (1-p).
        - If input image is 1 channel: grayscale version is 1 channel
        - If input image is 3 channel: grayscale version is 3 channel with r == g == b

    """

    def __init__(self, p=0.1):
        self.p = p

    def __call__(self, img_rgb, img_aux):
        """
        Args:
            img (PIL Image): Image to be converted to grayscale.

        Returns:
            PIL Image: Randomly grayscaled image.
        """
        num_output_channels = 1 if img_rgb.mode == 'L' else 3
        if random.random() < self.p:
            return F.to_grayscale(img_rgb, num_output_channels=num_output_channels), F.to_grayscale(img_aux,
                                                                                num_output_channels=num_output_channels)
        return img_rgb, img_aux

    def __repr__(self):
        return self.__class__.__name__ + '(p={0})'.format(self.p)

class GaussianBlur(object):
    # Implements Gaussian blur as described in the SimCLR paper
    def __init__(self, kernel_size, min=0.1, max=2.0):
        self.min = min
        self.max = max
        # kernel size is set to be 10% of the image height/width
        self.kernel_size = kernel_size

    def __call__(self, sample_rgb, sample_aug):
        sample_rgb = np.array(sample_rgb)
        sample_aug = np.array(sample_aug)

        # blur the image with a 50% chance
        prob = np.random.random_sample()

        if prob < 0.5:
            #            print(self.kernel_size)
            sigma = (self.max - self.min) * np.random.random_sample() + self.min
            sample_rgb = cv2.GaussianBlur(sample_rgb, (self.kernel_size, self.kernel_size), sigma)
            sample_aug = cv2.GaussianBlur(sample_aug, (self.kernel_size, self.kernel_size), sigma)
        return sample_rgb, sample_aug

class ToTensor(object):
    """Convert a ``PIL Image`` or ``numpy.ndarray`` to tensor.

    Converts a PIL Image or numpy.ndarray (H x W x C) in the range
    [0, 255] to a torch.FloatTensor of shape (C x H x W) in the range [0.0, 1.0]
    if the PIL Image belongs to one of the modes (L, LA, P, I, F, RGB, YCbCr, RGBA, CMYK, 1)
    or if the numpy.ndarray has dtype = np.uint8

    In the other cases, tensors are returned without scaling.
    """

    def __call__(self, pic, pic_aug):
        """
        Args:
            pic (PIL Image or numpy.ndarray): Image to be converted to tensor.

        Returns:
            Tensor: Converted image.
        """
        return F.to_tensor(pic), F.to_tensor(pic_aug)

    def __repr__(self):
        return self.__class__.__name__ + '()'


import whisper
import os
import timeit

#transcribe audio files and save the output as txt file in transcription folder
#if there is no transcription folder, create one

def transcribe(audio_file_path, model_type):
    #get transcription file name and get complete path by combining it with audio folder path
    transcription_file_name = os.path.basename(audio_file_path)
    transcription_file = os.path.join('new_audio', transcription_file_name)

    #transcribe audio file
    start_time = timeit.default_timer()

    model = whisper.load_model(model_type)
    result = model.transcribe(transcription_file, fp16=False)

    transcription_time = timeit.default_timer() - start_time

    print("Time Taken to transcribe the audio file:",transcription_time)

    whisper_output = result['text']

    #get file name without extension
    transcription_file_name_without_extension = os.path.splitext(transcription_file_name)[0]

    #save the transcription output as txt file in transcription folder
    #if there is no transcription folder, create one
    if not os.path.exists('transcription'):
        os.makedirs('transcription')

    #save the transcription output as txt file in transcription folder
    with open(os.path.join('transcription', transcription_file_name_without_extension + model_type +'.txt'), 'w') as f:
        f.write(whisper_output)

if __name__ == '__main__':
    #get audio files from audio folder
    audio_files = os.listdir('new_audio')

    #transcribe audio files and save the output as txt file in transcription folder
    for audio_file in audio_files:
        transcribe(audio_file, 'tiny.en')

    # for audio_file in audio_files:
    #     transcribe(audio_file, 'base.en')

    # for audio_file in audio_files:
    #     transcribe(audio_file, 'medium.en')


import face_recognition
import cv2

# Get a reference to webcam #0 (the default one)
video_capture = cv2.VideoCapture(0)

# Load a sample picture and learn how to recognize it.
obama_image = face_recognition.load_image_file("obama.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]

# Load a second sample picture and learn how to recognize it.
biden_image = face_recognition.load_image_file("biden.jpg")
biden_face_encoding = face_recognition.face_encodings(biden_image)[0]

# Load a second sample picture and learn how to recognize it.
derrick_image = face_recognition.load_image_file("Derrick.jpg")
derrick_face_encoding = face_recognition.face_encodings(derrick_image)[0]

# Load a second sample picture and learn how to recognize it.
j_image = face_recognition.load_image_file("J-2018.jpg")
j_face_encoding = face_recognition.face_encodings(j_image)[0]
#
# Load a second sample picture and learn how to recognize it.
evie_image = face_recognition.load_image_file("Evie.jpg")
evie_face_encoding = face_recognition.face_encodings(evie_image)[0]

# Load a second sample picture and learn how to recognize it.
thao_image = face_recognition.load_image_file("Thao2.jpg")
thao_face_encoding = face_recognition.face_encodings(thao_image)[0]

# Load a second sample picture and learn how to recognize it.
mai_image = face_recognition.load_image_file("Mai2.jpg")
mai_face_encoding = face_recognition.face_encodings(thao_image)[0]

# Load a second sample picture and learn how to recognize it.
tam_image = face_recognition.load_image_file("Tam.jpg")
tam_face_encoding = face_recognition.face_encodings(tam_image)[0]

# Create arrays of known face encodings and their names
known_face_encodings = [
    obama_face_encoding,
    biden_face_encoding,
    derrick_face_encoding,
    j_face_encoding,
    evie_face_encoding,
    thao_face_encoding,
    tam_face_encoding,
    mai_face_encoding
]
known_face_names = [
    "Barack Obama",
    "Joe Biden",
    "Derrick",
    "Jacob",
    "Evie",
    "Thao",
    "Tam",
    "Mai"
]

# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True

while True:
    # Grab a single frame of video
    ret, frame = video_capture.read()

    # Resize frame of video to 1/4 size for faster face recognition processing
    small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

    # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
    rgb_small_frame = small_frame[:, :, ::-1]

    # Only process every other frame of video to save time
    if process_this_frame:
        # Find all the faces and face encodings in the current frame of video
        face_locations = face_recognition.face_locations(rgb_small_frame)
        face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)

        face_names = []
        for face_encoding in face_encodings:
            # See if the face is a match for the known face(s)
            matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
            name = "Unknown"

            # If a match was found in known_face_encodings, just use the first one.
            if True in matches:

                # first_match_index = matches.index(True)
                # name = known_face_names[first_match_index]

                # find the indexes of all matched faces then initialize a
                # dictionary to count the total number of times each face
                # was matched
                matchedIdxs = [i for (i, b) in enumerate(matches) if b]
                counts = {}

                # loop over the matched indexes and maintain a count for
                # each recognized face face
                for i in matchedIdxs:
                    # name = data["names"][i]

                    name = known_face_names[i]
                    # counts[name] = counts.get(name, 0) + 1

                    counts[name] = counts.get(name, 0) + 1

                # determine the recognized face with the largest number of
                # votes (note: in the event of an unlikely tie Python will
                # select first entry in the dictionary)
                name = max(counts, key=counts.get)
                print(name, counts[name])

            face_names.append(name)

    process_this_frame = not process_this_frame

    # Display the results
    for (top, right, bottom, left), name in zip(face_locations, face_names):
        # Scale back up face locations since the frame we detected in was scaled to 1/4 size
        top *= 4
        right *= 4
        bottom *= 4
        left *= 4

        # Draw a box around the face
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

        # Draw a label with a name below the face
        cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)

    # Display the resulting image
    cv2.imshow('Video', frame)

    # Hit 'q' on the keyboard to quit!
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()


#!/usr/bin/env python2
import sys
import argparse
from npcaller.basecaller import Basecaller

def main(filelist, output, template, complement, ncores):
    bc = Basecaller(ncores=ncores, template_model=template, complement_model=complement)
    files = [l.strip() for l in filelist.readlines()]
    bc.process_files(files)
    bc.write_to_fasta(output)

if __name__ == "__main__":
    argp = argparse.ArgumentParser("Read fast5-files from folder perform the basecalling according to a model. ")
    argp.add_argument("-f", "--filelist", required=True, type=argparse.FileType('r'),
                      help="a list of fast5 files to be called, one per line")
    argp.add_argument("-o", "--output", required=True, type=argparse.FileType('w'),
                      help="fasta file with the called reads")
    argp.add_argument("-t", "--template", required=False, type=argparse.FileType('rb'),
                      help="path to template model")
    argp.add_argument("-c", "--complement", required=False, type=argparse.FileType('rb'),
                      help="path to complement model")
    argp.add_argument("-n", "--ncores", required=False, type=int, default=None,
                      help="#CPU cores")

    args = argp.parse_args()
    if args.template is None and args.complement is None:
        print("Template and/or Complement model must be specified")
        sys.exit(1)

    main(args.filelist, args.output, args.template, args.complement, args.ncores)


import json
from QuestionsParser import QuestionsParser

math_800_url = "https://codeforces.com/problemset?order=BY_SOLVED_ASC&tags=math%2C800-800"
number_theory_800_url = "https://codeforces.com/problemset?order=BY_SOLVED_ASC&tags=number+theory%2C800-800"
implementation_800_url = "https://codeforces.com/problemset?order=BY_SOLVED_ASC&tags=implementation%2C800-800"
constructive_algo_800_url = "https://codeforces.com/problemset?order=BY_SOLVED_ASC&tags=constructive+algorithms%2C800-800"
greedy_800_url = "https://codeforces.com/problemset?order=BY_SOLVED_ASC&tags=greedy%2C800-800"

urls_list = [math_800_url, number_theory_800_url, implementation_800_url, constructive_algo_800_url, greedy_800_url]

questions_parser = QuestionsParser(urls_list)
all_questions_list = questions_parser.parse_questions(10)
with open("Questions.json","w") as questions_file:
    json.dump(all_questions_list, questions_file, indent=4)


from django.contrib import admin
from import_export.admin import ImportExportModelAdmin, ImportExportActionModelAdmin

from .models import (
    Faculty,
    Building,
    Location,
    Section,
)

class SectionInline(admin.TabularInline):
    model = Section
    extra = 0

@admin.register(Faculty)
class FacultyAdmin(ImportExportModelAdmin):
    class Media:
        css = {
            'all': ('css/custom_admin.css', )  # Include extra css
        }
    inlines = [SectionInline]
    list_display = ('code', 'name', 'created_by', 'created_at',
                    'last_modified_by', 'last_modified_at')

class LocationInline(admin.TabularInline):
    model = Location
    exclude = ['info']
    extra = 0

@admin.register(Building)
class BuildingAdmin(ImportExportModelAdmin):
    class Media:
        css = {
            'all': ('css/custom_admin.css', )     # Include extra css
        }
    inlines = [LocationInline]
    list_display = ('name', 'code', 'created_by', 'created_at',
                    'last_modified_by', 'last_modified_at')

@admin.register(Location)
class LocationAdmin(ImportExportModelAdmin):
    list_display = ('room', 'building', 'created_by',
                    'created_at', 'last_modified_by', 'last_modified_at')

@admin.register(Section)
class SectionAdmin(ImportExportModelAdmin):
    list_display = ('code', 'name', 'faculty', 'created_by',
                    'created_at', 'last_modified_by', 'last_modified_at')


import logging as log
import pytest
from helpers.otg import otg

@pytest.mark.all
def test_config_serdes_perf():
    test_const = {
        "flowCounts": [1, 2, 16, 64, 128, 256, 1024],
        "flowCount": 1,
        "pktRate": 50,
        "pktCount": 100,
        "pktSize": 128,
        "txMac": "00:00:01:01:01:01",
        "rxMac": "00:00:01:01:01:02",
        "txIp": "1.1.1.1",
        "rxIp": "1.1.1.2",
        "txUdpPort": 5000,
        "rxUdpPort": 6000,
    }

    dist_tables = []

    for flow_count in test_const["flowCounts"]:
        test_const["flowCount"] = flow_count
        test_case = "config_serdes_%d_flows" % (2 * flow_count)

        api = otg.OtgApi()
        c = config_serdes_perf_config(api, test_const)

        c_json = c.serialize(encoding=c.JSON)
        c_yaml = c.serialize(encoding=c.YAML)

        log.info("TEST CASE: %s", test_case)
        for i in range(1, api.test_config.otg_iterations + 1):
            log.info("ITERATION: %d\n\n", i)

            api.config_to_json(api.new_config_from_json(c_json))
            api.config_to_yaml(api.new_config_from_yaml(c_yaml))

            api.plot.append_zero()

        api.log_plot(test_case)
        dist_tables.append(api.plot.to_table())

    for d in dist_tables:
        log.info(d)

def config_serdes_perf_config(api, tc):
    c = api.api.config()
    p1 = c.ports.add(name="p1", location=api.test_config.otg_ports[0])
    p2 = c.ports.add(name="p2", location=api.test_config.otg_ports[1])

    ly = c.layer1.add(name="ly", port_names=[p1.name, p2.name])
    ly.speed = api.test_config.otg_speed

    if api.test_config.otg_capture_check:
        ca = c.captures.add(name="ca", port_names=[p1.name, p2.name])
        ca.format = ca.PCAP

    for i in range(1, tc["flowCount"] + 1):
        f = c.flows.add(name="f%s-%d" % (p1.name, i))
        f.tx_rx.port.tx_name = p1.name
        f.tx_rx.port.rx_name = p2.name
        f.duration.fixed_packets.packets = tc["pktCount"]
        f.rate.pps = tc["pktRate"]
        f.size.fixed = tc["pktSize"]
        f.metrics.enable = True

        eth, ip, udp = f.packet.ethernet().ipv4().udp()

        eth.src.value = tc["txMac"]
        eth.dst.value = tc["rxMac"]

        ip.src.value = tc["txIp"]
        ip.dst.value = tc["rxIp"]

        udp.src_port.value = tc["txUdpPort"]
        udp.dst_port.value = tc["rxUdpPort"]

    for i in range(1, tc["flowCount"] + 1):
        f = c.flows.add(name="f%s-%d" % (p2.name, i))
        f.tx_rx.port.tx_name = p2.name
        f.tx_rx.port.rx_name = p1.name
        f.duration.fixed_packets.packets = tc["pktCount"]
        f.rate.pps = tc["pktRate"]
        f.size.fixed = tc["pktSize"]
        f.metrics.enable = True

        eth, ip, udp = f.packet.ethernet().ipv4().udp()

        eth.src.value = tc["rxMac"]
        eth.dst.value = tc["txMac"]

        ip.src.value = tc["rxIp"]
        ip.dst.value = tc["txIp"]

        udp.src_port.value = tc["rxUdpPort"]
        udp.dst_port.value = tc["txUdpPort"]

    log.info("Config:\n%s", c)
    return c


from nltk.probability import FreqDist
from nltk import classify
from nltk.classify.naivebayes import NaiveBayesClassifier
import joblib

class NaiveModel:

    training = []
    word_collection = []

    def closeFile(self):
        self.file.close()

    def read_in_chunks(self,chunk_size=1024):
        while True:
            data = self.file.read(chunk_size)
            if not data:
                break
            yield data

    def assign_class(self,filename,category):
        try:
            self.file = open(filename)
        except Exception as e:
            print(e)
        self._class = category

        for piece in self.read_in_chunks():
            sentences = piece.split('\n')
            # self.training.append(([word for word in sentence.lower().split() for sentence in sentences],self._class))
            for sentence in sentences:
                self.training.append(([word for word in sentence.lower().split()],self._class))

        #print(self.training)

    def get_word_features(self):
        for (words,sentiment) in self.training:
            self.word_collection.extend(words)

        self.wordList = FreqDist(self.word_collection)
        joblib.dump(self.wordList, 'word_features.pkl', 3)

        print(self.wordList.keys())

    '''
        checks if the passed list of words
        is contained in the list 'word_features'
    '''
    def extract_features(self, document):
        document_words = set(document)
        features = {}
        for word in self.wordList:
            features['contains(%s)' % word] = (word in document_words)
        return features

    def train(self):
        training_set = classify.apply_features(self.extract_features,self.training)
        classifier = NaiveBayesClassifier.train(training_set)
        joblib.dump(classifier, 'naive_classifier.pkl', 3)

naiveModel = NaiveModel()
naiveModel.assign_class('../Datasets/processedDataset/k-fold/train/normalCombined.txt','normal')
naiveModel.assign_class('../Datasets/processedDataset/k-fold/train/anomalousTrafficTest.txt','anomalous')
naiveModel.get_word_features()
naiveModel.train()


from datetime import datetime

dia1 = input()
horario1 = input()
full_date1 = datetime.strptime(dia1+' - '+horario1, 'Dia %d - %H : %M : %S')
dia2 = input()
horario2 = input()
full_date2 = datetime.strptime(dia2+' - '+horario2, 'Dia %d - %H : %M : %S')
result = full_date2-full_date1
hours = result.seconds//3600
minute = (result.seconds//60) % 60
seconds = result.seconds - hours*3600 - minute*60
print(f'{result.days} dia(s)')
print(f'{hours} hora(s)')
print(f'{minute} minuto(s)')
print(f'{seconds} segundo(s)')


"""
VERSION
- Python 3

FUNCTION
- Download images from urls.
"""

from urllib.request import urlretrieve
import os
import multiprocessing
from optparse import OptionParser
import pandas as pd
import socket

socket.setdefaulttimeout(10)

op = OptionParser()
op.add_option('--clss', action='store', type=str,
              help='The image class (e.g., vietnam war).')
op.add_option('--source', action='store', type=str,
              help='The media source.')
(opts, args) = op.parse_args()

PATH_HOME = os.path.expanduser('~') + '/Projects/iow'
PATH_DATA = PATH_HOME + '/data/IR/' + opts.source \
    + '/urls_image/' + opts.clss + '/'
PATH_OUT = PATH_HOME + '/data/DP/vision/' + opts.source + '/' \
    + opts.clss + '/'
N_THREADS = 16

if not os.path.exists(PATH_OUT):
    os.makedirs(PATH_OUT)
# ___________________________________________________________________________

def download(lock, ns, info_image):
    try:
        lock.acquire()
        ns.counter_total += 1
        lock.release()
        name_image = info_image['name']
        url_image = info_image['url']
        if name_image not in os.listdir(PATH_OUT):
            urlretrieve(url_image, PATH_OUT + name_image)
            print('Downloaded an image: %s' % name_image)
            lock.acquire()
            ns.counter += 1
            lock.release()
        else:
            # print('File already exists: %s' % name_image)
            return
    except Exception as e:
        print(e)
        return
# ___________________________________________________________________________

def main():
    infos_image = []
    for name_file in os.listdir(PATH_DATA):
        info_image = pd.read_pickle(PATH_DATA + name_file)
        infos_image.append(info_image)
    '''
    Enabling multiprocessing.
    '''
    manager = multiprocessing.Manager()
    ns = manager.Namespace()
    ns.counter = 0
    ns.counter_total = 0
    lock = manager.Lock()
    processes = multiprocessing.Pool(N_THREADS)
    from functools import partial
    func = partial(download, lock, ns)
    processes.map(func, infos_image)
    print('The image retrieval is finished (%d/%d).'
          % (ns.counter, ns.counter_total))

if __name__ == '__main__':
    main()


from distutils.cmd import Command
from distutils.command.install import INSTALL_SCHEMES
import os
import re
from setuptools import setup
from distutils.command.build import build as distutils_build
from setuptools.command.install import install as setuptools_install
from setuptools.command.sdist import sdist as setuptools_sdist

import wirecloud.platform

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'

    def disable(self):
        self.HEADER = ''
        self.OKBLUE = ''
        self.OKGREEN = ''
        self.WARNING = ''
        self.FAIL = ''
        self.ENDC = ''

class build(distutils_build):

    """Customized setuptools build command - compile po files before creating the distribution package."""

    sub_commands = [('compiletranslations', None)] + distutils_build.sub_commands

class sdist(setuptools_sdist):

    """Customized setuptools sdist command - compile po files before creating the distribution package."""

    sub_commands = [('compiletranslations', None)] + setuptools_sdist.sub_commands

class compiletranslations(Command):

    description = 'compile message catalogs to MO files via django compilemessages'
    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):

        try:
            from django.core.management import call_command
        except:
            import pip
            pip.main(['install', 'Django>=1.6,<1.10'])

            from django.core.management import call_command

        oldwd = os.getcwd()
        wirecloud_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'wirecloud'))
        for subpath in os.listdir(wirecloud_path):
            current_path = os.path.join(wirecloud_path, subpath)
            if os.path.isdir(os.path.join(current_path, 'locale')):
                os.chdir(current_path)
                try:
                    call_command('compilemessages')
                except Exception as e:
                    print('Error compiling translations for module %(module)s: %(error)s' % {'module': subpath.replace('/', '.'), 'error': e})

        os.chdir(oldwd)

class install(setuptools_install):

    """Customized setuptools install command - prints info about the license of Wirecloud after installing it."""
    def run(self):
        setuptools_install.run(self)

        print('')
        print(bcolors.HEADER + 'License' + bcolors.ENDC)
        print('')
        print("Wirecloud is licensed under a AGPLv3+ license with a classpath-like exception \n" +
              "that allows widgets/operators and mashups to be licensed under any propietary or \n" +
              "open source license.")
        print('')
        license_file = os.path.join(self.install_purelib, 'wirecloud', 'LICENSE')
        print('A copy of the license has been installed at: ' + bcolors.WARNING + license_file + bcolors.ENDC)

for scheme in INSTALL_SCHEMES.values():
    scheme['data'] = scheme['purelib']

setup(
    name='wirecloud',
    version=wirecloud.platform.__version__,
    description='Widgets Container and Mashup edition tools for composing end-user centric mashup applications.',
    long_description='',
    author='CoNWeT Lab',
    author_email='wirecloud@conwet.com',
    url='http://github.com/Wirecloud/wirecloud',
    license='AGPLv3+ with classpath-like exception',
    packages=('wirecloud',),
    entry_points={
        'console_scripts': (
            'wirecloud-admin = wirecloud.commons.wirecloud_admin:execute_from_command_line',
        ),
    },
    include_package_data=True,
    install_requires=(
        'Django>=1.6,<1.10',
        'south>=1.0,<2.0',
        'lxml>=2.3',
        'django-appconf>=1.0.1,<2.0',
        'django_compressor>=1.4,<3.0',
        'rdflib>=3.2.0',
        'requests>=2.1.0',
        'selenium>=2.41',
        'pytz',
        'django_relatives',
        'user-agents',
        'regex',
        'markdown',
        'whoosh>=2.5.6',
        'pycrypto',
        'pyScss>=1.3.4,<2.0',
        'Pygments',
        'pillow',
    ),
    extras_require={
        ":python_version < '3.2'": ('futures>=2.1.3',),
    },
    tests_require=(
        'django-nose',
        'mock>=1.0,<2.0',
    ),
    setup_requires={
        'wheel>=0.24',
    },
    classifiers=(
        'Development Status :: 5 - Production/Stable',
        'Environment :: Web Environment',
        'Framework :: Django',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: JavaScript',
        'Topic :: Software Development :: Libraries :: Python Modules',
    ),
    cmdclass={
        'build': build,
        'install': install,
        'sdist': sdist,
        'compiletranslations': compiletranslations
    },
)


from aiogram import executor
from pymongo.errors import DuplicateKeyError

import config
from manipulationWithName import returnWithoutSmiles
from simple import *
from config import chat_report, release, test
from mongoDB import usersNameExists, createColl, setDefaultNameUser, updateDefaultNameUser, deleteChatTrigger, \
    triggerChatExists
from user_triggersDB import deleteUserTrigger, triggerUserExists
from tools import textWithoutCommand

import chatTriggers
import userTriggers
import gamepoll.gamepoll
import gamepoll.addingList

@dp.message_handler(commands=['setme', 'setMe', 'set_me'], commands_prefix='!/.')
async def setMyDefaultName(message: types.Message):
    msg = message.from_user
    if message.reply_to_message and message.from_user.id == config.creator:
        msg = message.reply_to_message.from_user

    name = textWithoutCommand(message.text)
    if name is None:
        await message.answer('Используйте /setme [Ваше имя] где вместо [Ваше имя] ставите ваш '
                             'никнейм или имя. ', parse_mode='HTML')
        return

    if not usersNameExists(msg.id):
        setDefaultNameUser(user_id=msg.id, name=name,
                           telegram_name=msg.first_name,
                           username=msg.username)
    else:
        updateDefaultNameUser(user_id=msg.id, name=name)
    await message.answer(f'{name} установлен.')

@dp.message_handler(commands=['setname', 'set_name'], commands_prefix='!/.')
async def setMyDefaultNameByCoping(message: types.Message):
    if message.reply_to_message and message.from_user.id == config.creator:
        name = message.reply_to_message.from_user.first_name
        msg = message.reply_to_message.from_user
    else:
        name = message.from_user.first_name
        msg = message.from_user

    if not usersNameExists(msg.id):
        setDefaultNameUser(user_id=msg.id, name=name,
                           telegram_name=msg.first_name,
                           username=msg.username)
    else:
        updateDefaultNameUser(user_id=msg.id, name=name)

    await message.answer(f'{name} установлен.')

@dp.message_handler(commands=['check'], commands_prefix='!/.')
async def checkHowItWorks(message: types.Message):
    nickname = textWithoutCommand(message.text)
    if not nickname is None:
        ready_nick = returnWithoutSmiles(nickname)
        if ready_nick == 0:
            result = [i for i in nickname if i in normal_char or i in caps_normal_char]
            ready_nick = "".join(result).lstrip()
            if len(ready_nick) == 0:
                ready_nick = nickname
        await message.answer(f"🎻ʀᴇ| {ready_nick} 🌅")

@dp.message_handler(content_types=['new_chat_members'])
async def addNewChatToColl(msg: types.Message):
    if msg["new_chat_member"]["id"] == test or msg["new_chat_member"]["id"] == release:
        buttons = [
            types.InlineKeyboardButton(text="Канал бота", url="https://t.me/savonarola_chan"),
        ]
        keyboard = types.InlineKeyboardMarkup(row_width=1)
        keyboard.add(buttons[0])
        await msg.answer('Привет. Я бот для турнирных чатов. Документация еще не готова но осталось совсем немного. '
                         'Но уже есть телеграм канал где объясняются некоторые фишки бота.', reply_markup=keyboard)
        try:
            createColl(chat_id=msg.chat.id, name=msg.chat.title)

            m = await bot.send_message(chat_id=chat_report, text=f'NEW\ngroup: {msg.chat.title}, id: {msg.chat.id}')
            await bot.pin_chat_message(chat_id=chat_report, message_id=m['message_id'])
        except DuplicateKeyError:
            pass
    elif msg["new_chat_member"]["id"] == creator:
        await msg.answer(f'Привет, {msg.from_user.first_name}!⚡')

@dp.message_handler(commands=['del', 'delete', 'del_trigger'], is_admin=True, commands_prefix='!/.')
async def deleteTrigger(message: types.Message):
    try:
        trigger_name = message.text.split()[1]
        if not message.chat.type == 'private':
            deleteChatTrigger(chatId=message.chat.id, trigger_name=trigger_name)
            if not triggerChatExists(chatId=message.chat.id, trigger_name=trigger_name):
                await message.answer(f'Триггер {trigger_name} не существовал в списке.')
            else:
                await message.answer(f'Триггер {trigger_name} был удален из списка чата.')
    except IndexError:
        await message.answer('/del_trigger [имя_триггера]', parse_mode='HTML')

@dp.message_handler(commands=['del', 'delete', 'del_trigger'], commands_prefix='!/.')
async def deleteTrigger2(message: types.Message):
    try:
        trigger_name = message.text.split()[1]
        if message.chat.type == 'private':
            deleteUserTrigger(user_id=message.from_user.id, trigger_name=trigger_name)
            if not triggerUserExists(user_id=message.from_user.id, trigger_name=trigger_name):
                await message.answer(f'Триггер {trigger_name} не существовал в вашем списке.')
            else:
                await message.answer(f'Триггер {trigger_name} был удален из вашего списка.')
    except IndexError:
        await message.answer('/del_trigger [имя_триггера]', parse_mode='HTML')

@dp.message_handler(content_types=["text"])
async def PinMafiaList(message: types.Message):
    if message.chat.id == config.command_chat or message.chat.id == -1001829614132:
        if message.text.startswith('Запомни своих союзников'):
            await bot.pin_chat_message(chat_id=message.chat.id, message_id=message.message_id)
        elif message.text.startswith("Добро пожаловать во"):
            await bot.delete_message(chat_id=message.chat.id, message_id=message.message_id)
    elif message.chat.id == -1001591495273:
        if message.text.startswith('Запомни своих союзников'):
            await bot.pin_chat_message(chat_id=message.chat.id, message_id=message.message_id)

if __name__ == "__main__":
    executor.start_polling(dp, skip_updates=True)


from collections import OrderedDict
import time, os
curdir = os.path.expanduser("~")
import numpy as np
np.set_printoptions(precision=8, suppress=True, linewidth=400, threshold=100)
import tensorflow as tf
tf.keras.backend.set_floatx('float64')
# tf.config.run_functions_eagerly(True)
tf.keras.backend.set_epsilon(tf.experimental.numpy.finfo(tf.keras.backend.floatx()).eps) # 1e-7 default
import matplotlib.pyplot as plt
import gymnasium as gym
import gym_util, model_util as util, model_nets as nets
# CUDA 11.8.0_522.06, CUDNN 8.6.0.163, tensorflow-gpu==2.10.0, tensorflow_probability==0.18.0
physical_devices_gpu = tf.config.list_physical_devices('GPU')
for i in range(len(physical_devices_gpu)): tf.config.experimental.set_memory_growth(physical_devices_gpu[i], True)

class GeneralAI(tf.keras.Model):
    def __init__(self, arch, env, trader, env_render, save_model, chkpts, max_episodes, max_steps, learn_rates, latent_size, latent_dist, mixture_multi, net_lstm, net_attn, aio_max_latents, aug_data_step, aug_data_pos):
        super(GeneralAI, self).__init__()
        compute_dtype = tf.dtypes.as_dtype(self.compute_dtype)
        self.float_max = tf.constant(compute_dtype.max, compute_dtype)
        self.float_maxroot = tf.constant(tf.math.sqrt(self.float_max), compute_dtype)
        self.float_eps = tf.constant(tf.experimental.numpy.finfo(compute_dtype).eps, compute_dtype)
        self.float_eps_max = tf.constant(1.0 / self.float_eps, compute_dtype)
        self.loss_scale = tf.math.exp(tf.math.log(self.float_eps_max) * (1/2))
        self.float64_zero = tf.constant(0, tf.float64)

        self.arch, self.env, self.trader, self.env_render, self.save_model = arch, env, trader, env_render, save_model
        self.chkpts, self.max_episodes, self.max_steps, self.learn_rates = tf.constant(chkpts, tf.int32), tf.constant(max_episodes, tf.int32), tf.constant(max_steps, tf.int32), {}
        for k,v in learn_rates.items(): self.learn_rates[k] = tf.constant(v, compute_dtype)
        self.initializer = tf.keras.initializers.GlorotUniform(time.time_ns())

        self.obs_spec, self.obs_zero, _ = gym_util.get_spec(env.observation_space, space_name='obs', compute_dtype=self.compute_dtype, net_attn_io=net_attn['io'], aio_max_latents=aio_max_latents, mixture_multi=mixture_multi)
        self.action_spec, _, self.action_zero_out = gym_util.get_spec(env.action_space, space_name='actions', compute_dtype=self.compute_dtype, mixture_multi=mixture_multi)
        self.obs_spec_len, self.action_spec_len = len(self.obs_spec), len(self.action_spec)
        self.action_total_size = tf.constant(np.sum([np.prod(feat['step_shape']) for feat in self.action_spec]),compute_dtype)
        self.gym_step_shapes = [feat['step_shape'] for feat in self.obs_spec] + [tf.TensorShape((1,1)), tf.TensorShape((1,1)), tf.TensorShape((1,2)) if trader else tf.TensorShape((1,1))]
        self.gym_step_dtypes = [feat['dtype'] for feat in self.obs_spec] + [tf.float64, tf.bool, tf.float64]
        self.rewards_zero, self.dones_zero = tf.constant([[0]],tf.float64), tf.constant([[False]],tf.bool)
        self.step_zero, self.step_size_one = tf.constant([[0]]), tf.constant([[1]])

        latent_spec = {'dtype':compute_dtype, 'latent_size':latent_size, 'num_latents':1, 'max_latents':aio_max_latents}
        latent_spec.update({'inp':512, 'midp':256, 'outp':512, 'evo':64})
        if latent_dist == 'd': latent_spec.update({'dist_type':'d', 'num_components':latent_size, 'event_shape':(latent_size,)}) # deterministic
        if latent_dist == 'c': latent_spec.update({'dist_type':'c', 'num_components':0, 'event_shape':(latent_size, latent_size)}) # categorical # TODO https://keras.io/examples/generative/vq_vae/
        if latent_dist == 'mx': latent_spec.update({'dist_type':'mx', 'num_components':int(latent_size/16), 'event_shape':(latent_size,)}) # continuous

        if aug_data_step: self.obs_spec += [{'space_name':'step', 'name':'', 'event_shape':(1,), 'event_size':1, 'channels':1, 'step_shape':tf.TensorShape((1,1)), 'num_latents':1}]
        self.obs_spec += [{'space_name':'reward_prev', 'name':'', 'event_shape':(1,), 'event_size':1, 'channels':1, 'step_shape':tf.TensorShape((1,1)), 'num_latents':1}]
        inputs = {'obs':self.obs_zero, 'step':[self.step_zero], 'reward_prev':[self.rewards_zero], 'return_goal':[self.rewards_zero]}

        if arch in ('PG',):
            opt_spec = [{'name':'action', 'type':'a', 'schedule_type':'', 'learn_rate':self.learn_rates['action'], 'float_eps':self.float_eps}]
            stats_spec = [{'name':'rwd', 'b1':0.99, 'b2':0.99, 'dtype':tf.float64}, {'name':'loss', 'b1':0.99, 'b2':0.99, 'dtype':compute_dtype}, {'name':'delta', 'b1':0.99, 'b2':0.99, 'dtype':compute_dtype}]
            self.action = nets.ArchFull('A', inputs, opt_spec, stats_spec, self.obs_spec, self.action_spec, latent_spec, obs_latent=False, net_blocks=2, net_lstm=net_lstm, net_attn=net_attn, num_heads=4, memory_size=max_steps, latent_multi=1, aug_data_pos=aug_data_pos)
            outputs = self.action(inputs)
            self.action.optimizer_weights = util.optimizer_build(self.action.optimizer['action'], self.action.trainable_variables)
            util.net_build(self.action, self.initializer)

        self.metrics_spec()
        self.reset_states = tf.function(self.reset_states, experimental_autograph_options=tf.autograph.experimental.Feature.LISTS)
        self.reset_states()
        arch_run = getattr(self, arch); arch_run = tf.function(arch_run, experimental_autograph_options=tf.autograph.experimental.Feature.LISTS); setattr(self, arch, arch_run)

    def metrics_spec(self):
        metrics_loss = OrderedDict()
        metrics_loss['2rewards*'] = {'-rewards_ma':np.float64, '-rewards_total+':np.float64, 'rewards_final=':np.float64}
        metrics_loss['1steps'] = {'steps+':np.int64}
        if self.arch == 'PG':
            metrics_loss['1nets*'] = {'-loss_ma':np.float64, '-loss_action':np.float64}
            metrics_loss['1extras'] = {'loss_action_returns':np.float64}
            metrics_loss['1extras2*'] = {'actlog0':np.float64, 'actlog1':np.float64}

        for loss_group in metrics_loss.values():
            for k in loss_group.keys():
                if k.endswith('=') or k.endswith('+'): loss_group[k] = [0 for i in range(self.max_episodes)]
                else: loss_group[k] = [[] for i in range(self.max_episodes)]
        self.metrics_loss = metrics_loss

    def metrics_update(self, *args):
        args = list(args)
        log_metrics, episode, idx = args[0], args[1], 2
        for loss_group in self.metrics_loss.values():
            for k in loss_group.keys():
                if log_metrics[idx-2]:
                    if k.endswith('='): loss_group[k][episode] = args[idx]
                    elif k.endswith('+'): loss_group[k][episode] += args[idx]
                    else: loss_group[k][episode] += [args[idx]]
                idx += 1
        return np.asarray(0, np.int32) # dummy

    def env_reset(self, dummy):
        obs, info = self.env.reset(); reward, done = 0.0, False
        if self.env_render: self.env.render()
        if hasattr(self.env,'np_struc'): rtn = gym_util.struc_to_feat(obs)
        else: rtn = gym_util.space_to_feat(obs, self.env.observation_space)
        metrics = info['metrics'] if 'metrics' in info else [0]
        rtn += [np.asarray([[reward]], np.float64), np.asarray([[done]], bool), np.asarray([metrics], np.float64)]
        return rtn
    def env_step(self, *args): # args = tuple of ndarrays
        if hasattr(self.env,'np_struc'): action = gym_util.out_to_struc(list(args), self.env.action_dtype)
        else: action = gym_util.out_to_space(args, self.env.action_space, [0])
        obs, reward, terminated, truncated, info = self.env.step(action); done = (terminated or truncated)
        if self.env_render: self.env.render()
        if hasattr(self.env,'np_struc'): rtn = gym_util.struc_to_feat(obs)
        else: rtn = gym_util.space_to_feat(obs, self.env.observation_space)
        metrics = info['metrics'] if 'metrics' in info else [0]
        rtn += [np.asarray([[reward]], np.float64), np.asarray([[done]], bool), np.asarray([metrics], np.float64)]
        return rtn

    def checkpoints(self, *args):
        model_files = ""
        for net in self.layers:
            model_file = self.model_files[net.name]
            net.save_weights(model_file)
            model_files += ' '+model_file.split('/')[-1]
        print("SAVED{}".format(model_files))
        return np.asarray(0, np.int32) # dummy

    def reset_states(self, use_img=False):
        for net in self.layers:
            if hasattr(net, 'reset_states'): net.reset_states(use_img=use_img)

    def PG_actor(self, inputs, return_goal):
        print("tracing -> GeneralAI PG_actor")
        obs, actions = [None]*self.obs_spec_len, [None]*self.action_spec_len
        for i in range(self.obs_spec_len): obs[i] = tf.TensorArray(self.obs_spec[i]['dtype'], size=1, dynamic_size=True, infer_shape=False, element_shape=self.obs_spec[i]['step_shape'][1:])
        for i in range(self.action_spec_len): actions[i] = tf.TensorArray(self.action_spec[i]['dtype_out'], size=1, dynamic_size=True, infer_shape=False, element_shape=self.action_spec[i]['step_shape'][1:])
        rewards = tf.TensorArray(tf.float64, size=1, dynamic_size=True, infer_shape=False, element_shape=(1,))
        dones = tf.TensorArray(tf.bool, size=1, dynamic_size=True, infer_shape=False, element_shape=(1,))
        returns = tf.TensorArray(tf.float64, size=0, dynamic_size=True, infer_shape=False, element_shape=(1,))

        step = tf.constant(0)
        while not inputs['dones'][-1][0]:
            for i in range(self.obs_spec_len): obs[i] = obs[i].write(step, inputs['obs'][i][-1])

            action = [None]*self.action_spec_len
            inputs_step = {'obs':inputs['obs'], 'step':[tf.reshape(step,(1,1))], 'reward_prev':[inputs['rewards']], 'return_goal':[return_goal]}
            action_logits = self.action(inputs_step)
            for i in range(self.action_spec_len):
                action_dist = self.action.dist[i](action_logits[i])
                action[i] = action_dist.sample()

            action_dis = [None]*self.action_spec_len
            for i in range(self.action_spec_len):
                actions[i] = actions[i].write(step, action[i][0])
                action_dis[i] = util.discretize(action[i][0], self.action_spec[i])

            np_in = tf.numpy_function(self.env_step, action_dis, self.gym_step_dtypes)
            for i in range(len(np_in)): np_in[i].set_shape(self.gym_step_shapes[i])
            inputs['obs'], inputs['rewards'], inputs['dones'] = np_in[:-3], np_in[-3], np_in[-2]

            rewards = rewards.write(step, inputs['rewards'][-1])
            dones = dones.write(step, inputs['dones'][-1])
            returns = returns.write(step, [self.float64_zero])
            returns_updt = returns.stack()
            returns_updt = returns_updt + inputs['rewards'][-1]
            returns = returns.unstack(returns_updt)
            step += 1

        outputs = {}
        out_obs, out_actions = [None]*self.obs_spec_len, [None]*self.action_spec_len
        for i in range(self.obs_spec_len): out_obs[i] = obs[i].stack()
        for i in range(self.action_spec_len): out_actions[i] = actions[i].stack()
        outputs['obs'], outputs['actions'], outputs['rewards'], outputs['dones'], outputs['returns'] = out_obs, out_actions, rewards.stack(), dones.stack(), returns.stack()
        return outputs, inputs

    def PG_learner_onestep(self, inputs, training=True):
        print("tracing -> GeneralAI PG_learner_onestep")
        loss = {}
        loss_actions_lik = tf.TensorArray(self.compute_dtype, size=1, dynamic_size=True, infer_shape=False, element_shape=(1,))
        loss_actions = tf.TensorArray(self.compute_dtype, size=1, dynamic_size=True, infer_shape=False, element_shape=(1,))
        metric_actlog = tf.TensorArray(self.compute_dtype, size=1, dynamic_size=True, infer_shape=False, element_shape=(2,))

        inputs_rewards = tf.concat([self.rewards_zero, inputs['rewards']], axis=0)
        returns = inputs['returns'][0:1]
        for step in tf.range(tf.shape(inputs['dones'])[0]):
            obs = [None]*self.obs_spec_len
            for i in range(self.obs_spec_len): obs[i] = inputs['obs'][i][step:step+1]; obs[i].set_shape(self.obs_spec[i]['step_shape'])
            action = [None]*self.action_spec_len
            for i in range(self.action_spec_len): action[i] = inputs['actions'][i][step:step+1]; action[i].set_shape(self.action_spec[i]['step_shape'])
            returns_calc = tf.squeeze(tf.cast(returns,self.compute_dtype),axis=-1)

            inputs_step = {'obs':obs, 'step':[tf.reshape(step,(1,1))], 'reward_prev':[inputs_rewards[step:step+1]], 'return_goal':[returns]}
            with tf.GradientTape() as tape_action:
                action_logits = self.action(inputs_step)
                action_dist = [None]*self.action_spec_len
                for i in range(self.action_spec_len):
                    action_dist[i] = self.action.dist[i](action_logits[i])
                loss_action_lik = util.loss_likelihood(action_dist, action)
                loss_action = loss_action_lik * returns_calc
            if loss_action_lik > self.float_eps:
                gradients = tape_action.gradient(loss_action, self.action.trainable_variables)
                self.action.optimizer['action'].apply_gradients(zip(gradients, self.action.trainable_variables))
            loss_actions_lik = loss_actions_lik.write(step, loss_action_lik / self.action_total_size)
            loss_actions = loss_actions.write(step, loss_action)
            metric_actlog = metric_actlog.write(step, action_logits[0][0][0:2])

        loss['action_lik'], loss['action'], loss['actlog'] = loss_actions_lik.concat(), loss_actions.concat(), metric_actlog.stack()
        return loss

    def PG(self):
        print("tracing -> GeneralAI PG"); tf.print("RUNNING")
        return_goal, ma, ma_loss, snr_loss = tf.constant([[-self.loss_scale.numpy()]], tf.float64), tf.constant(0,tf.float64), self.float_maxroot, tf.constant(1,self.compute_dtype)
        episode = tf.constant(0)
        while episode < self.max_episodes:
            tf.autograph.experimental.set_loop_options(parallel_iterations=1)
            np_in = tf.numpy_function(self.env_reset, [tf.constant(0)], self.gym_step_dtypes)
            for i in range(len(np_in)): np_in[i].set_shape(self.gym_step_shapes[i])
            inputs = {'obs':np_in[:-3], 'rewards':np_in[-3], 'dones':np_in[-2]}

            self.reset_states(); outputs, inputs = self.PG_actor(inputs, return_goal)
            rewards_total = outputs['returns'][0][0]
            util.stats_update(self.action.stats['rwd'], rewards_total); avg, ma, ema, snr, std = util.stats_get(self.action.stats['rwd'])

            self.reset_states(); loss = self.PG_learner_onestep(outputs)
            util.stats_update(self.action.stats['loss'], tf.math.reduce_mean(loss['action_lik'])); avg_loss, ma_loss, ema_loss, snr_loss, std_loss = util.stats_get(self.action.stats['loss'])
            self.action.optimizer['action'].learning_rate = self.learn_rates['action'] * snr_loss

            log_metrics = [True,True,True,True,True,True,True,True,True,True,True,True,True,True]
            metrics = [log_metrics, episode, ma, tf.math.reduce_sum(outputs['rewards']), outputs['rewards'][-1][0], tf.shape(outputs['rewards'])[0],
                ma_loss, tf.math.reduce_mean(loss['action_lik']),
                tf.math.reduce_mean(loss['action']),
                tf.math.reduce_mean(loss['actlog'][:,0]), tf.math.reduce_mean(loss['actlog'][:,1]),
            ]
            dummy = tf.numpy_function(self.metrics_update, metrics, [tf.int32])

            if self.save_model:
                if episode > tf.constant(0) and episode % self.chkpts == tf.constant(0): tf.numpy_function(self.checkpoints, [tf.constant(0)], [tf.int32])
            episode += 1

def params(): pass
load_model, save_model, chkpts = False, False, 5000
max_episodes = 100
latent_size = 16
latent_dist = 'd' # 'd' = deterministic, 'c' = categorical, 'mx' = continuous(mixture)
mixture_multi = 4 # mixture distribution size,multiply num components
net_lstm = False
net_attn = {'net':True, 'io':True, 'out':True, 'ar':True}
aio_max_latents = 16
aug_data_step, aug_data_pos = True, True

device_type = 'GPU' # use GPU for large networks (over 8 total net blocks?) or output data (512 bytes?)
device_type = 'CPU'

machine, device, extra = 'dev', 0, ''

trader, env_async, env_async_clock, env_async_speed, env_reconfig, chart_lim = False, False, 0.001, 160.0, False, 0.003
env_name, max_steps, env_render, env_reconfig, env = 'CartPole', 512, False, True, gym.make('CartPole-v1', max_episode_steps=500) # (4) float32    ()2 int64    500  475.0
# env_name, max_steps, env_render, env_reconfig, env = 'LunarLand', 1024, False, True, gym.make('LunarLander-v2') # (8) float32    ()4 int64    1000  200.0

arch = 'PG'; learn_rates = {'action':4e-6} # Policy Gradient agent, PG loss

if __name__ == '__main__':
    if env_async: import envs_local.async_wrapper as envaw_; env_name, env = env_name+'-asyn', envaw_.AsyncWrapperEnv(env, env_async_clock, env_async_speed, env_render)
    if env_reconfig: import envs_local.reconfig_wrapper as envrw_; env_name, env = env_name+'-r', envrw_.ReconfigWrapperEnv(env)
    with tf.device("/device:{}:{}".format(device_type,(device if device_type=='GPU' else 0))):
        model = GeneralAI(arch, env, trader, env_render, save_model, chkpts, max_episodes, max_steps, learn_rates, latent_size, latent_dist, mixture_multi, net_lstm, net_attn, aio_max_latents, aug_data_step, aug_data_pos)
        name = "gym-{}-{}-{}-a{}{}-{}".format(arch, env_name, machine, device, extra, time.strftime("%y-%m-%d-%H-%M-%S"))

        ## load models
        model_files, name_arch = {}, ""
        for net in model.layers:
            model_name = "{}-{}-a{}".format(net.arch_desc, machine, device)
            model_file = "{}/tf-data-models-local/{}.h5".format(curdir, model_name); loaded_model = False
            model_files[net.name] = model_file
            if (load_model or net.name == 'M') and tf.io.gfile.exists(model_file):
                net.load_weights(model_file, by_name=True, skip_mismatch=True)
                print("LOADED {} weights from {}".format(net.name, model_file)); loaded_model = True
            name_opt = "-O{}{}".format(net.opt_spec['type'], ('' if net.opt_spec['schedule_type']=='' else '-S'+net.opt_spec['schedule_type'])) if hasattr(net, 'opt_spec') else ''
            name_arch += "   {}{}-{}".format(net.arch_desc, name_opt, 'load' if loaded_model else 'new')
        model.model_files = model_files

        ## run
        print("RUN {}\n{}".format(name, name_arch))
        arch_run = getattr(model, arch)
        t1_start = time.perf_counter_ns()
        arch_run()
        total_time = (time.perf_counter_ns() - t1_start) / 1e9 # seconds
        env.close()

        ## metrics
        metrics_loss = model.metrics_loss
        for loss_group in metrics_loss.values():
            for k in loss_group.keys():
                for j in range(len(loss_group[k])):
                    loss_group[k][j] = 0 if loss_group[k][j] == [] else np.mean(loss_group[k][j])

        total_steps = int(np.nansum(metrics_loss['1steps']['steps+']))
        step_time = total_time/total_steps
        learn_rates_txt, attn_txt = "", ""
        for k,v in learn_rates.items(): learn_rates_txt += "  {}:{:.0e}".format(k,v)
        for k,v in net_attn.items(): attn_txt += " {}".format(k) if v else ''
        title = "{}    [{}-{}]\n{}\ntime:{}    steps:{}    t/s:{:.8f}    ms:{}".format(name, device_type, tf.keras.backend.floatx(), name_arch, util.print_time(total_time), total_steps, step_time, max_steps)
        title += "     |     attn:{}    al:{}".format(attn_txt, aio_max_latents)
        title += "     |     a-clk:{}    a-spd:{}    aug:{}{}".format(env_async_clock, env_async_speed, ('S' if aug_data_step else ''), ('P' if aug_data_pos else ''))
        title += "     |   {}".format(learn_rates_txt); print(title)

        import matplotlib as mpl
        mpl.rcParams['axes.prop_cycle'] = mpl.cycler(color=['blue','lightblue','green','lime','red','lavender','turquoise','cyan','magenta','salmon','yellow','gold','black','brown','purple','pink','orange','teal','coral','darkgreen','tan'])
        plt.figure(num=name, figsize=(34, 18), tight_layout=True)
        xrng, i, vplts = np.arange(0, max_episodes, 1), 0, 0
        for loss_group_name in metrics_loss.keys(): vplts += int(loss_group_name[0])

        for loss_group_name, loss_group in metrics_loss.items():
            rows, col, m_min, m_max, combine, yscale = int(loss_group_name[0]), 0, [0]*len(loss_group), [0]*len(loss_group), loss_group_name.endswith('*'), ('log' if loss_group_name[1] == '~' else 'linear')
            if combine: spg = plt.subplot2grid((vplts, 1), (i, 0), rowspan=rows, xlim=(0, max_episodes), yscale=yscale); plt.grid(axis='y',alpha=0.3)
            for metric_name, metric in loss_group.items():
                metric = np.asarray(metric, np.float64); m_min[col], m_max[col] = np.nanquantile(metric, chart_lim), np.nanquantile(metric, 1.0-chart_lim)
                if not combine: spg = plt.subplot2grid((vplts, len(loss_group)), (i, col), rowspan=rows, xlim=(0, max_episodes), ylim=(m_min[col], m_max[col]), yscale=yscale); plt.grid(axis='y',alpha=0.3,which='both')
                if metric_name.startswith('-'): plt.plot(xrng, metric, alpha=1.0, label=metric_name)
                else: plt.plot(xrng, util.ewma(metric, window=max_episodes//10+2), alpha=1.0, label=metric_name); plt.plot(xrng, metric, alpha=0.3)
                plt.ylabel('value'); plt.legend(loc='upper left'); col+=1
            if combine: spg.set_ylim(np.min(m_min), np.max(m_max))
            if i == 0: plt.title(title)
            i+=rows
        out_file = "output/{}.png".format(name)
        plt.savefig(out_file)
        print("SAVED {}   (run \033[94mpython serve.py\033[00m to access webserver on port 8080)".format(out_file))

        ## save models
        if save_model:
            for net in model.layers:
                model_file = model.model_files[net.name]
                net.save_weights(model_file)
                print("SAVED {} weights to {}".format(net.name, model_file))


import torch
import numpy as np

class AverageMeter(object):
    def __init__(self):
        self.reset()

    def reset(self):
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def update(self, val, n=1):
        self.val = val
        self.sum += val * n
        self.count += n
        self.avg = self.sum / self.count

class Crop(object):
    def __init__(self, output_size):
        assert isinstance(output_size, (int, tuple, list))
        if isinstance(output_size, int):
            self.output_size = (output_size, output_size)
        else:
            assert len(output_size) == 2
            self.output_size = output_size

    def __call__(self, sample):
        image, label = sample["image"], sample["label"]
        top, left = sample["top"], sample["left"]
        new_h, new_w = self.output_size
        sample["image"] = image[top: top + new_h,
                          left: left + new_w]
        sample["label"] = label[top: top + new_h,
                          left: left + new_w]

        return sample

class Flip(object):
    def __call__(self, sample):
        flag_lr = sample["flip_lr"]
        flag_ud = sample["flip_ud"]
        if flag_lr == 1:
            sample["image"] = np.fliplr(sample["image"])
            sample["label"] = np.fliplr(sample["label"])
        if flag_ud == 1:
            sample["image"] = np.flipud(sample["image"])
            sample["label"] = np.flipud(sample["label"])

        return sample

class ToTensor(object):
    def __call__(self, sample):
        image, label = sample["image"], sample["label"]
        image = np.ascontiguousarray(image.transpose((2, 0, 1))[np.newaxis, :])
        label = np.ascontiguousarray(label.transpose((2, 0, 1))[np.newaxis, :])
        sample["image"] = torch.from_numpy(image).float()
        sample["label"] = torch.from_numpy(label).float()
        return sample

def normalize(x, centralize=False, normalize=False, val_range=255.0):
    if centralize:
        x = x - val_range / 2
    if normalize:
        x = x / val_range

    return x

def normalize_reverse(x, centralize=False, normalize=False, val_range=255.0):
    if normalize:
        x = x * val_range
    if centralize:
        x = x + val_range / 2

    return x


#coding=utf-8

import tornado.web
import tornado.ioloop
class IndexHandle(tornado.web.RequestHandler):

	def get(self):
	    """定义请求函数"""
	    self.write('hello wukang')

if __name__ == '__main__':
    app=tornado.web.Application([
            (r'/',IndexHandle),
                              ])
    app.listen(8000)
    tornado.ioloop.IOLoop.current().start()