#!/usr/bin/env python3

import time

from phue import Bridge

def main():

    b = Bridge('192.168.1.7')

    # If the app is not registered and the button is not pressed, press the button and call connect() (this only needs to be run a single time)
    b.connect()

    # Get the bridge state (This returns the full dictionary that you can explore)
    b.get_api()

    #lights = ['Ceiling 5', 'Ceiling 6']
    lights = [
        'Front Flood 1',
        'Front Flood 2',
        'Porch',
        'Gate Left Top',
        'Gate Left Bottom',
        'Gate Right Top',
        'Gate Right Bottom'
    ]

    colours = {
        "orange": {"hue": 4819,
                   "sat": 254},
        "purple": {"hue": 53212,
                   "sat": 254},
        "green":  {"hue": 25600,
                   "sat": 254}}

    command = {
        "on" : True,
        "bri": 254,
        "hue": colours["orange"]["hue"],
        "sat": colours["orange"]["sat"],
        "transitiontime": 50
    }

    try:
        print("Running...press Ctrl-C to interrupt")

        while True:

            for colour in colours:
                command["bri"] = 254
                command["hue"] = colours[colour]['hue']
                command["sat"] = colours[colour]['sat']
                b.set_light(lights, command)

                time.sleep(13)

                command["bri"] = 10
                b.set_light(lights, command)
                time.sleep(6)

    except KeyboardInterrupt:
        pass

    # Reset the lights back to white and turn off when finished
    command["on"] = False
    command["bri"] = 254
    command["hue"] = 15335
    command["sat"] = 121
    command["transitiontime"] = 0

    b.set_light(lights, command)

    #print(f"GOT: {b.get_light('Ceiling 5')}")

#-----------------------------
if __name__ == "__main__":
    main()


def print_formatted(number):
    width = number.bit_length()
    res=""
    for i in range(1,number+1):
        res = res +\
        str(i).rjust(width)+" "+\
        str(oct(i).replace('0o','')).rjust(width)+" "+\
        str(hex(i).replace('0x','')).rjust(width).upper()+" "+\
        str(bin(i).replace('0b','')).rjust(width)+" "
        if(i!=number):
            res = res + '\n'

    print(res)

if __name__ == '__main__':
    n = int(input())
    print_formatted(n)


import os,sys,argparse
# import fileinput,time
import glob
import re,bisect
import pandas as pd
import numpy as np
from scipy import stats
import matplotlib
# matplotlib.use('Agg')
import matplotlib.pyplot as plt
matplotlib.rcParams['font.size']=14
import seaborn as sns
sns.set(font_scale=1.)
sns.set_style("whitegrid", {'axes.grid' : False})
import scipy
import scipy.optimize
sns.set_style("ticks")
matplotlib.rcParams["font.sans-serif"] = ["Arial"]
from matplotlib import gridspec

outdir = 'f1_CP_heatmap'
os.makedirs(outdir+os.sep+'_csv',exist_ok=True)

indirs = ['CP_TFBS_vs_TFMS','CP_TFBS_with_motif_vs_TFMS','CP_TFBS_without_motif_vs_TFMS']
group_info = {
'CP': {'data_median_fisher':
       ['log10-distance CP t-stats',
        'log10-distance CP pvalue Fisher-TF-Celltype-combined'],
       'data_fisher_fisher':
       ['log10-distance CP t-stats',
        'log10-distance CP pvalue Fisher-data-combined Fisher-TF-Celltype-combined'],
    },
'SE': {'data_median_fisher':
       ['fisher_exact_s',
        'fisher_exact_p Fisher-TF-Celltype-combined'],
       'data_fisher_fisher':
       ['fisher_exact_s',
        'fisher_exact_p Fisher-data-combined Fisher-TF-Celltype-combined'],
    }}

or_thre = {'CP':9,'SE':6}
label_name = {'CP':'log2 T-statistics','SE':'log2 Odds Ratio'}

index_cutoff = 18
celltype_cutoff = 6

for indir in indirs[:]:
    # outdir = 'f1_CP_heatmap'
    # os.makedirs(outdir,exist_ok=True)
    infile = '../f2_TFMS_TFBS_CP/{}/per_TF_per_Celltype_{}.csv'.format(indir,indir)
    df = pd.read_csv(infile,index_col = 0)
    # === data for CP and SE enrichment
    for or_key in group_info.keys():
        for fisher_P_key in group_info[or_key].keys():
            s_col = group_info[or_key][fisher_P_key][0]
            p_col = group_info[or_key][fisher_P_key][1]
            df_s = pd.DataFrame()
            df_p = pd.DataFrame()
            basename = '{}_{}_{}'.format(or_key,fisher_P_key,indir)
            # print(p_col,basename)
            for ii in df.index:
                factor = ii.split('_')[0]
                celltype = ii.split('_')[1]
                df_s.loc[factor,celltype] = df.loc[ii,s_col]
                df_p.loc[factor,celltype] = df.loc[ii,p_col]
            df_s.to_csv(outdir+os.sep+'_csv/{}_OR.csv'.format(basename))
            df_p.to_csv(outdir+os.sep+'_csv/{}_P.csv'.format(basename))

            # ==== select the TF/cell-type with sufficient data
            selected_index = df_s.notnull().sum(axis=1).sort_values(ascending=False).index[:index_cutoff][::-1]
            selected_col = df_s.notnull().sum(axis=0).sort_values(ascending=False).index[:celltype_cutoff]
            df_s = df_s.loc[selected_index,selected_col]
            df_s = np.log2(df_s)
            df_p = df_p.loc[selected_index,selected_col]
            df_p = df_p.replace(0,1e-299)

            # == plot the heatmap
            plt.figure(figsize=(4,7))
            width_ratios = [4,.0,0,.5]
            gs = gridspec.GridSpec(3,4,width_ratios=width_ratios,wspace=.15,hspace=.5)
            pal = plt.cm.bwr
            vmax = or_thre[or_key]
            vmin = -1*vmax
            norm = matplotlib.colors.Normalize(vmin=vmin, vmax=vmax)
            color_map = matplotlib.cm.ScalarMappable(norm=norm, cmap=pal)
            scatter_scale = 1
            ax = plt.subplot(gs[:,0])
            for ii in np.arange(len(df_s.index)):
                for jj in np.arange(len(df_s.columns)):
                    odds_ratio = df_s.loc[df_s.index[ii],df_s.columns[jj]]
                    pvalue = df_p.loc[df_s.index[ii],df_s.columns[jj]]
                    if not np.isnan(odds_ratio):
                        color = color_map.to_rgba(odds_ratio)
                        size = -1*np.log10(pvalue)#;print(size)
                        ax.scatter(jj,ii,s = scatter_scale*size, color = color)
                        # ax.text(jj,ii,df_s.index[ii],fontsize=12)
                    else:
                        ax.scatter(jj,ii,marker = 'x',s = scatter_scale*30, color = 'k')

            ax.set_yticks(np.arange(len(df_s.index)))
            ax.set_yticklabels(df_s.index,fontsize=13)
            ax.set_xticks(np.arange(len(df_s.columns)))
            ax.set_xticklabels(df_s.columns,rotation=60,fontsize=13)
            ax.set_xlim([-.5,len(df_s.columns)-.5])
            ax.set_title('{}\n{}\n{}'.format(or_key,fisher_P_key,indir),fontsize=13,va='bottom')

            # color bar
            ax = plt.subplot(gs[0,3])
            cbar = matplotlib.colorbar.ColorbarBase(ax,cmap=pal,norm=norm,orientation='vertical')
            cbar.set_ticks([vmin,0,vmax])
            cbar.set_label(label_name[or_key],labelpad=-60,fontsize=12)
            # pvalue legend
            ax = plt.subplot(gs[1,3])
            ax.scatter(0,.15,s = -1*scatter_scale*np.log10(1e-10), color = 'k')
            ax.scatter(0,.5,s = -1*scatter_scale*np.log10(1e-50), color = 'k')
            ax.scatter(0,.9,s = -1*scatter_scale*np.log10(1e-100), color = 'k')
            ax.text(0,.0,'1e-10',ha='center')
            ax.text(0,.3,'1e-50',ha='center')
            ax.text(0,.67,'1e-100',ha='center')
            ax.set_xlim([-1,1])
            ax.set_ylim([.0,1.05])
            ax.set_title('P-value',ha='center',fontsize=13)
            ax.axis('off')

            # heatmap of AICAP
            # read TF AICAP
            # idrfile = '../f2_TF_motif_IDR_SE_mutation/data/13059_2021_2456_MOESM2_ESM.xlsx'
            # idrdf = pd.read_excel(idrfile,sheet_name='condition 2(1,6-HD-2)',index_col=0)
            # vmin=-2;vmax=2
            # ax = plt.subplot(gs[:,1])
            # plot_data = idrdf.loc[df_s.index[::-1],['AICAP']]
            # plot_data = np.log2(plot_data)
            # g = sns.heatmap(plot_data,cmap=plt.cm.PiYG,cbar=False\
            #                 ,vmax=vmax,vmin=vmin\
            #                 ,ax=ax\
            #                 ,xticklabels=True,yticklabels=False)
            # ax.set_xticklabels(['AICAP'],rotation=60)
            # # color bar
            # ax = plt.subplot(gs[2,3])
            # norm = matplotlib.colors.Normalize(vmin=vmin, vmax=vmax)
            # cbar = matplotlib.colorbar.ColorbarBase(ax,cmap=plt.cm.PiYG,norm=norm,orientation='vertical')
            # # cbar.set_ticks([-1,0,2])
            # cbar.set_label('log2 AICAP',labelpad=-55,fontsize=13)

            plt.savefig('{}/{}.pdf'.format(outdir,basename),bbox_inches='tight',pad_inches=0.02,transparent=True)
            plt.show()
            plt.close()


"""
Double DQN Agent
Strategy:
Linear Epsilon Decay

"""

import gym
import resource_gen
from agents import agent_base_pfrl
import numpy as np
from datasets import ReplayBuffer
import pfrl
import torch
import torch.nn
from agents import agent_v1
from torch.utils.tensorboard import SummaryWriter
from datetime import datetime
from constants import MODEL_PATH, HIST_PATH, DATA_PATH
from iex_parser import Parser, DEEP_1_0
from utils import load_file
import sys
import os

# Initializations for the DEEP (LOB) Processing
file = str(HIST_PATH / 'data_feeds_20201124_20201124_IEXTP1_DEEP1.0.pcap.gz')
allowed_types = ['price_level_update']
allowed_stocks_file = DATA_PATH / 'DEEP_2021_03_01-12_17_52_AM_T_100000_N_50'
allowed_stocks = load_file(filename=allowed_stocks_file)

# process_hist_data(file=file, allowed_types=allowed, hist_type="DEEP", max_count=300000)
reader = Parser(file, DEEP_1_0).__enter__()
reader_it = iter(reader)

# Env params
exp_id = "exp2_{}".format(datetime.now().strftime("%Y_%m_%d-%I_%M_%S_%p"))

env = gym.make("res-env-v0", reader_it=reader_it, allowed_types=allowed_types,
               allowed_stocks=allowed_stocks)

writer = SummaryWriter(MODEL_PATH / 'runs/{}'.format(exp_id))

# Model params
q_func = agent_v1.LobNet(num_stocks=len(allowed_stocks))
optimizer = torch.optim.Adam(q_func.parameters(), eps=1e-4)
gamma = 0.99
# Use epsilon-greedy for exploration
explorer = pfrl.explorers.LinearDecayEpsilonGreedy(
    start_epsilon=1.0, end_epsilon=0.1,
    decay_steps=50000, random_action_func=env.action_space_d.sample)
# DQN uses Experience Replay.
# Specify a replay buffer and its capacity.
replay_buffer = pfrl.replay_buffers.ReplayBuffer(capacity=10 ** 6)
# Set the device id to use GPU. To use CPU only, set it to -1.
gpu = 0

# Now create an agent that will interact with the environment.
agent_params = pfrl.agents.DoubleDQN(
    q_func,
    optimizer,
    replay_buffer,
    gamma,
    explorer,
    replay_start_size=500,
    update_interval=1,
    target_update_interval=500,
    gpu=gpu,
)

agent = agent_base_pfrl.AgentBase(env=env,
                                  agent=agent_params,
                                  episodes=1,
                                  print_freq=1000,
                                  save_freq=10000,
                                  max_episode_len=100000,  # Put -1 for infinite steps
                                  exp_id=exp_id,
                                  writer=writer)

agent.fit()


from typing import List
class Solution:
    def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]:
        answer = []
        sumSoFar = sum([num for num in nums if num%2==0])
        for val, i in queries:

            if nums[i]%2 == 0:

                # integer was even till now.
                # substract the value from the total sum.
                # (otherwise the value would be added twice)
                sumSoFar -= nums[i]

            # add new value val to the value at index i.
            nums[i] += val

            if nums[i]%2 == 0:
                # if new value is even, add to total sum.
                sumSoFar += nums[i]

            # append total sum of iteration to answer list.
            answer.append(sumSoFar)
        return answer

nums = [1,2,3,4]
queries = [[1,0],[-3,1],[-4,0],[2,3]]
x = Solution()
x.sumEvenAfterQueries(nums, queries)


#!/usr/bin/env python

import sympy as sp
from sympy.solvers import solve
import numpy as np
import matplotlib.pyplot as plt

def nth_derivative_at_zero(uhat,s,n):
  '''
  returns the the nth derivative of u(x) evaluated at zero given the laplace
  transform of u(x)
  PARAMETERS
  ----------
    uhat: laplace transform of u. This is a symbolic function of s.
    s: the laplace domain variable
    n: the derivative order

  RETURNS
  -------
    nth_derivative: symbolic expression for the nth derivative of u evaluated
                    at zero
  '''
  assert n >= 0
  if n == 0:
    expr = s*uhat
    zeroth_derivative = expr.limit(s,np.inf)
    return zeroth_derivative
  elif n > 0:
    expr = s**(n+1)*uhat
    # note that m goes up to n-1
    for m in range(0,n):
      expr -= s**(m+1)*nth_derivative_at_zero(uhat,s,n-m-1)
    nth_derivative = expr.limit(s,np.inf)
    return nth_derivative

def inverse_laplace_transform_series_expansion(uhat,s,t,N):
  '''
  returns the Taylor series expansion of u(x) about zero given its laplace
  transform

  PARAMETERS
  ----------
    uhat: symbolic expression for the laplace transform of u(x)
    s: laplace domain variable
    N: order of the Taylor series expansion

  RETURNS
  -------
    series: symbolic expression for the series expansion of the u(x)
          about x=0
  '''
  series = 0
  # note that n goes up to N
  for n in range(N+1):
    # sum each term in the taylor series expansion
    series += (nth_derivative_at_zero(uhat,s,n)*t**n)/sp.factorial(n)
  return series

x,s = sp.symbols('x s')

u = 3*x**2 + 2*x + 1 # test function
uhat = sp.laplace_transform(u,x,s)[0] # test function laplace transform
u_expansion = inverse_laplace_transform_series_expansion(uhat,s,x,2)
assert sp.simplify(u - u_expansion) == 0

'''

Maxwell viscoelastic 2D two layered earthquake model
  I am demonstrating here that the surface deformation resulting from viscous
  relaxation following an earthquake in a layered halfspace is, to first order,
  a linear function of the viscosities in each layer.

Variables used
  x: distance from fault strike
  t: time
  D: locking depth of the fault
  H: thickness of the top layer
  mu1,mu2: shear modulus of the first and second layer
  eta1,eta2: viscosity of the first and second layer

'''
x,t,D,H,theta = sp.symbols('x t D H theta')
mu1,mu2 = sp.symbols('mu1 mu2')
eta1,eta2 = sp.symbols('eta1 eta2')
b = sp.Function('b')(t)
n = sp.symbols('n') # dummy summation variable
nmax = 3 # The number of terms used to approximate the infinite series

'''
The surface displacements for a layered viscous halfspace can be found by using
the displacements for a layered elastic halfspace and the correspondence
principle. The surface displacements for a layered elastic halfspace are:
'''
gamma = (mu1-mu2)/(mu1+mu2)
W = sp.Function('W')(n)
# uncomment for explicit W
#W = 1/sp.pi*(sp.atan((D + 2*n*H)/x) + sp.atan((D - 2*n*H)/x))
u_elastic = b*(sp.Rational(1,2)*W.subs(n,0) + sp.summation(gamma**n*W,(n,1,nmax)))

'''
The correpondence principle says that the Laplace transform of displacements
in an elastic medium is the same form as the displacements in a viscoelastic
medium. The laplace transform of displacements for an elastic medium are:
'''
uhat_elastic = sp.laplace_transform(u_elastic,t,s)

'''
Replacing the shear modulii with the equivalent maxwell viscoelastic shear
modulii in the laplace domain gives us the laplace transform of displacements
in the viscoelastic model
'''
mu1hat = s/((s/mu1) + (1/eta1)) # equivalent shear modulii
mu2hat = s/((s/mu2) + (1/eta2))
uhat_viscoelastic = uhat_elastic.subs(((mu1,mu1hat),(mu2,mu2hat)))

'''
for the sake of simplicity, although this does not reduce the generality of our
conclusions, we assume that the viscoelastic model has a homogeneous shear modulus
'''
uhat_viscoelastic = uhat_viscoelastic.subs(mu2,mu1)

'''
We are interested in the viscous deformation and so we remove the elastic
component of deformation.  Doing so also removes a singularity in the
displacement field at the time of the earthquake making subsequent computations
easier.
'''
uhat_viscous = uhat_viscoelastic - uhat_elastic.subs(mu2,mu1)

'''
Using the initial value theorem, the first term in the Taylor series expansion
of the viscous deformation is b(t) convolved with:
'''
bhat = sp.laplace_transform(b,t,s)
u_viscous_term0 = inverse_laplace_transform_series_expansion(uhat_viscous/bhat,s,t,0)
sp.pprint(sp.expand(u_viscous_term0))

'''
We can find the next few  terms of the Taylor series expansion of viscous
deformation using an extension of the initial value theorem.  The viscous
deformation is b(t) convolved with:
'''
u_viscous_term0to2 = inverse_laplace_transform_series_expansion(uhat_viscous/bhat,s,t,2)
sp.pprint(sp.collect(sp.expand(u_viscous_term0to2),t))

'''
An approximation for early postseismic deformation can then be found by
combining the elastic deformation with the first term in the Taylor series of the
viscous component of deformation.
'''
u_elastic = u_elastic.subs(mu2,mu1)
u_approx = u_elastic + sp.integrate(b.subs(t,theta)*u_viscous_term0,(theta,0,t))
sp.pprint(u_approx)

'''
Maxwell viscoelastic 2D three layered earthquake model
  This procedure can be repeated for a three layered viscoelastic earthquake
  model.  We start with the solution for the three layered elastic solution from
  Chinnery and Jovanovich 1972. WE ADOPT THE NOTATION FROM CHINNERY AND
  JOVANOVICH 1972 which is described below

variables used:
  mu3,mu2,mu1: shear modulus for the top, middle, and bottom layer (mu4 from
               Chinnery and Jovanovich is assumed 0)
  eta3,eta2,eta1: viscosity for the top, middle, and bottom layer
  h3,h2: thichness of the top and middle layer
  p: depth to the bottom of the fault. (down is positive)
'''

l_max = 2
m_max = 2
n_max = 2

h2,h3,p,y = sp.symbols('h2 h3 p y')
mu1,mu2,mu3 = sp.symbols('mu1 mu2 mu3')
eta1,eta2,eta3 = sp.symbols('eta1 eta2 eta3')
M,N = sp.symbols('M N')

a1    = (mu2 - mu1) / (mu2 + mu1)
a2    = (mu3 - mu2) / (mu3 + mu2)
a3    = -1
b1    = 2*mu1 / (mu1 + mu2)
b2    = 2*mu2 / (mu2 + mu3)
b3    = 2
d1    = 2*mu2 / (mu1 + mu2)
d2    = 2*mu3 / (mu2 + mu3)
W = sp.Function('W')(M,N)
# uncomment for explicit W
#W = 1/pi*(sp.atan((-2*M*h2 - N*h3 + p)/y) + a3*sp.atan((-2*M*h2 - N*h3 - p)/y))

def P(l,m,n):
  N = sp.factorial(n+1) * sp.factorial(l+m-1)
  D = sp.factorial(l) * sp.factorial(n) * sp.factorial(l-1) * sp.factorial(m)
  return N / D

def Q(l,m,n):
  N = sp.factorial(n+l) * sp.factorial(l+m)
  D = sp.factorial(l) * sp.factorial(n) * sp.factorial(l) * sp.factorial(m)
  return N / D

u = -b3*sp.Rational(-1,2)*W.subs(((N,0),(M,0)))
for n in range(n_max):
  coeff = a2*b3*(-a2*a3)**n
  u += coeff*W.subs(((M,0),(N,n+1)))

for l in range(1,l_max):
  for m in range(m_max):
    for n in range(n_max):
      coeff = a2*b3*(-a1*a2)**m*(-a2*a3)**n*(-a2*a3*d2*b2)**l*P(l,m,n)
      u += coeff*W.subs(((M,l+m),(N,l+n+1)))

for l in range(l_max):
  for m in range(m_max):
    for n in range(n_max):
      coeff = a1*d2*b2*b3*(-a1*a3)**m*(-a2*a3)**n*(-a1*a3*d2*b2)**l*Q(l,m,n)
      u += coeff*W.subs(((M,l+m+1),(N,l+n+1)))

u_elastic = sp.Rational(1,2)*b*u

'''
Assuming a homogeneous shear modulus, mu1, the Laplace transform of the
viscous component of deformation is:
'''
uhat_elastic = sp.laplace_transform(u_elastic,t,s)
mu1hat = s/((s/mu1) + (1/eta1))
mu2hat = s/((s/mu1) + (1/eta2))
mu3hat = s/((s/mu1) + (1/eta3))
uhat_viscoelastic = uhat_elastic.subs(((mu1,mu1hat),(mu2,mu2hat),(mu3,mu3hat)))
uhat_viscous = uhat_viscoelastic - uhat_elastic.subs(((mu2,mu1),(mu3,mu1)))

'''
The first term in the Taylor series expansion of the viscous component of
deformation is b(t) convolved with (this will take a few minutes):
'''
u_viscous_term0 = inverse_laplace_transform_series_expansion(uhat_viscous/bhat,s,t,0)
u_viscous_term0 = sp.expand(u_viscous_term0)
sp.pprint(u_viscous_term0)

'''
An approximation for early postseismic deformation can then be found by
combining the elastic deformation with the first term in the Taylor series of the
viscous component.
'''
theta = sp.symbols('theta')
u_elastic = u_elastic.subs(((mu2,mu1),(mu3,mu1)))
u_approx = u_elastic + sp.integrate(b.subs(t,theta)*u_viscous_term0,(theta,0,t))
sp.pprint(u_approx)


from __future__ import print_function

from os import path
import sys

from setuptools import setup

packages = [
    'paramsurvey_tooling',
]

# remember: keep requires synchronized with requirements.txt
requires = ['paramsurvey[ray]', 'psutil']

test_requirements = ['pytest', 'pytest-cov', 'coveralls', 'pyfakefs']

package_requirements = ['twine', 'setuptools', 'setuptools-scm[toml]']

extras_require = {
    'test': test_requirements,
    'package': package_requirements,
}

# if somehow we are running under python 2, which shouldn't happen,
# print a useful diagnostic instead of crashing on open(encoding=)

if sys.version_info[0] < 3:
    print('Somehow we are running under Python 2, which is not supported.')
    print('Python sys.executable:\n')
    print(sys.executable)
    exit(1)

this_directory = path.abspath(path.dirname(__file__))
with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f:
    description = f.read()

setup(
    name='paramsurvey_tooling',
    description='A toolkit to make paramsurvey easy to use on clusters and cloud',
    use_scm_version=True,
    long_description=description,
    long_description_content_type='text/markdown',
    author='Greg Lindahl and others',
    author_email='lindahl@pbm.com',
    url='https://github.com/wumpus/paramsurvey-tooling',
    packages=packages,
    python_requires=">=3.6.*",
    extras_require=extras_require,
    install_requires=requires,
    entry_points='''
        [console_scripts]
        pstool = paramsurvey_tooling.cli:main
    ''',
    license='Apache 2.0',
    classifiers=[
        'Development Status :: 3 - Alpha',
        'Environment :: Console',
        'Operating System :: POSIX :: Linux',
        'Environment :: MacOS X',
        'Intended Audience :: Information Technology',
        'Intended Audience :: Developers',
        'Natural Language :: English',
        'License :: OSI Approved :: Apache Software License',
        'Programming Language :: Python',
        #'Programming Language :: Python :: 3.5',  # setuptools-scm problem
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3.7',
        'Programming Language :: Python :: 3.8',
        'Programming Language :: Python :: 3.9',
        'Programming Language :: Python :: 3.10',
        'Programming Language :: Python :: 3 :: Only',
    ],
)


import matplotlib.pyplot as plt
from random import randint, uniform
import os
#os.system('clear')
def hum(n,x,y):
    a = []
    b = []
    c = []
    s = 0
    for i in range(n):
        val = float(input("guess:"))
        valrand = randint(x,y)
        print("machine:",valrand,"homme:",val)
        if valrand == val:
            print("NICE")
        else:
            print("failed")
        if valrand == val:
            s +=1
        a.append(i)
        title = "humaine score is "+str(s)+"/"+str(i)
        b.append(val)
        c.append(valrand)
        want = int(input("if you wanna rep graphique no:[0] yes:[1]>>:"))
        if want == 1:
            plt.plot(a,b,label="guess")
            plt.plot(a,c,label="aleatoire")
            plt.title(title)
            plt.legend()
            plt.show()
        else:
            continue
    plt.plot(a,b,label="guess")
    plt.plot(a,c,label="aleatoire")
    title = "humaine score is "+str(s)+"/"+str(n)
    plt.legend()
    plt.title(title)
    plt.show()
    hum(n)
def mach(n,x,y):
    a = []
    b = []
    c = []
    s = 0
    for i in range(n):
        val1 = randint(x,y)
        val2 = randint(x,y)
        print("machine1:",val1,"machine2:",val2)
        if val1 == val2:
            s +=1
        a.append(i)
        title = "humaine score is "+str(s)+"/"+str(i)
        b.append(val1)
        c.append(val2)
    plt.plot(a,b,label="machine1")
    plt.plot(a,c,label="macine2")
    title = "machine1 score is "+str(s)+"/"+str(n)
    plt.legend()
    plt.title(title)
    plt.show()
#mach(10,0,10)
#hum(10,0,10)


# OrderedDictionary

from collections import OrderedDict
class LRUCache(OrderedDict):

    def __init__(self, capacity: int):
        """
        :type capacity: int
        """

        self.capacity = capacity

    def get(self, key: int):
        """
        :type key:int
        :rtype: int

        """
        if key not in self:
            return -1
        self.move_to_end(key)
        return self[key]

    def put(self, key: int, value: int):
        """
        :type key:int
        :type value:int
        :rtype:void
        """

        if key in self:
            self.move_to_end(key)
        self[key] = value
        if len(self) > self.capactiy:
            self.popitem(last = False)

# TC:O(1)
# SC:O(capacity)
# Link: https://leetcode.com/problems/lru-cache/


import json
import playsound
from datetime import date
import os
import speech_recognition as sr
import random
import time
import datetime
import json
from main.data.speech.RealtimeSpeech import SpeechSynthesis

class Initialisation:
    def __init__(self):

        # Define A Path for Json File Containing DB Data for User
        self.path = './user.json'
        # existence(variable -> checking for existence of path)
        self.existence = os.path.exists(self.path)

        # check_existence
        self.check_existence()

    def check_existence(self):
        # while existence
        if self.existence:
            self.runMain()

        # while existence not true
        else:
            self.welcomeUser()
            self.yourName()

    """ Task Execution """

    def speakData(self, data):
        try:
            synthesis = SpeechSynthesis()
            synthesis.synthesize(data)

        except:
            print(data)

    def listen_Data(self):
        try:
            # use the microphone as source for input.
            with sr.Microphone() as source:

                # Define the Recognizer
                r = sr.Recognizer()

                # the surrounding noise level
                r.adjust_for_ambient_noise(source, duration=0.2)

                # Use Pause Threshold
                r.pause_threshold = 1

                print("Listening...")
                audio = r.listen(source)

                # Using google to recognize audio
                inputtext = r.recognize_google(audio)

                # Lower input text
                inputtext = inputtext.lower()

                # Replace empty spaces
                inputtext = inputtext.replace(" ", "")

                return inputtext

        except Exception as e:
            # print(e)
            # Say that again will be printed in case of improper voice
            print("Please Say that Again...")
            return self.listen_Data()

        except sr.UnknownValueError:
            print("Google Speech Recognition could not understand audio")

        except sr.RequestError as e:
            print(
                "Could not request results from Google Speech Recognition service{0}".format(e))

    def db_save(self, name, weather_api_key, compute_engine_api_key):

        dateCreated = str(datetime.date.today().day) + "/" + \
            str(datetime.date.today().month) + \
            "/" + str(datetime.date.today().year)
        jsonData = {"name": name, "dateCreated": dateCreated,
                    "wolframalpha_api": compute_engine_api_key}

        # Dump Data
        jsonString = json.dumps(jsonData)

        # Open Json File
        jsonFile = open("user.json", "w")

        # Write data
        jsonFile.write(jsonString)
        jsonFile.close()
        jsonData = {"weather_api": weather_api_key}
        jsonString = json.dumps(jsonData)

        # Open Json File
        jsonFile = open("core/packages/Weather/user.json", "w")

        # Write data
        jsonFile.write(jsonString)
        jsonFile.close()

    def startDocumentation(self, inputtext):
        try:
            # Begin the app for marketing/explaining purposes
            self.speakData(
                f"{inputtext}, Follow through the documentation, and once you are done, just close the window and I'll be ready.")
            # Change Active Directory
            os.chdir("app")
            # if node_modules exists
            if os.path.isdir("node_modules"):
                # Start the app
                os.system("npm start")

            self.speakData(
                "Installing Desktop App Packages. Executing Data. Predicting Output. Loading files... ")
            self.speakData(
                "This will take some time depending on your internet connection. Please wait. ")
            # Install node_modules/electron packages
            os.system("npm install --save-dev electron")
            # Start the app
            self.speakData("And Done. Starting the app. ")
            os.system("npm start")

        except Exception as AppStartBundleException:
            self.speakData(AppStartBundleException)
            self.speakData(
                "Sorry. I have encountered an issue. Please try again later. For now you can manually try installing packages. Follow these instructions. ")
            self.speakData("Open your terminal.")
            self.speakData("Go to the directory where you installed Seven.")
            self.speakData("Change Directory to app by typing cd app")
            self.speakData("Type npm install --save-dev electron")
            self.speakData("Try again")

    """ Proccess Execution """

    def runMain(self):
        # json_file(variable -> opening the file)
        with open(self.path) as json_file:

            # data(variable -> reading the file)
            data = json.load(json_file)

            # close json_file
            json_file.close()

            # cd main/python3 main.py (redirect to main)
            os.chdir("main")
            os.system("python3  main.py")

    def welcomeUser(self):
        # welcome_user(variable -> welcome user)
        # Introduce Seven
        response = random.choice(["A new user I see. Welcome. Welcome to Seven. Allow me to introduce myself. I am Seven, made by Rishabh Mishra, Sarthak Rawool, and Shubham Mishra. And about me well, I'm a damn brilliant guy! Here let me get you through setup",
                                  "Hey. I am Seven. Let me introduce myself, As you know me I am Seven. I was made by a few brilliant idiots Rishabh Mishra, Shubham Mishra, Sarthak Rawool. The Guys, eh? Anyways, let me get you through setup. ", "Here's looking at you Kid. I'm Seven. I was made by Rishabh Mishra, Shubham Mishra, and Sarthak Rawool, Ya. Let me get you through setup. "])
        self.speakData(response)

    def yourName(self):
        # your_name(variable -> your name)
        # Introduce your name

        self.speakData("What is your name? ")

        # your_name(variable -> your name)
        # Get your name

        self.your_name = self.listen_Data()

        if self.your_name:
            self.speakData("Is {} your name? ".format(self.your_name))

            self.your_name_confirm = self.listen_Data()
            if "yes" in self.your_name_confirm.lower() or "yeah" in self.your_name_confirm.lower():
                self.speakData("Okay. Let's get started. ")

            else:
                self.yourName()

        self.apprun = input(
            "Do you want to run the app locally too (Y/N) [NPM is required]: ")
        self.weather_input = input(
            "Please enter your weather api key (If you do not have one, Please make one at https://openweathermap.org/api): ")
        self.compute_engine_input = input(
            "Please enter your WolframAlpha API key (If you do not have one, Please make one at https://products.wolframalpha.com/api/): ")
        self.db_save(self.your_name, self.weather_input,
                     self.compute_engine_input)
        if self.apprun != "N":
            self.startDocumentation(self.your_name)
            print("Processing, it will take less than a minute...")
            os.system("python3  ../main/main.py")
        else:
            print("Processing, it will take less than a minute...")
            os.system("python3  ../main/main.py")

###############################
# Run Intialiasation Process
###############################
Initialisation = Initialisation()


#!/usr/bin/python3
"""

script to add all arguments to  Python list, and then save them to file
"""

from sys import argv
save_to_json_file = __import__("5-save_to_json_file").save_to_json_file
load_from_json_file = __import__("6-load_from_json_file").load_from_json_file

filename = "add_item.json"

try:
    json_list = load_from_json_file(filename)
except FileNotFoundError:
    json_list = []

for arg in argv[1:]:
    json_list.append(arg)

save_to_json_file(json_list, filename)
"""adonijah kiplimo"""


import json
import os
from sqlalchemy import Column, Integer, Float, DateTime, create_engine, MetaData
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

metadata = MetaData()

class Readings(Base):
    __tablename__ = 'readings'

    id = Column(Integer, primary_key=True)
    date_time = Column(DateTime)
    soil_temperature_1 = Column(Float)
    soil_temperature_2 = Column(Float)
    soil_moisture_1 = Column(Float)
    soil_temperature_2 = Column(Float)
    ambient_temperature = Column(Float)
    ambient_humidity = Column(Float)
    ambient_pressure = Column(Float)

    def __repr__(self):
        return "f{self.date_time}"

DBSession = sessionmaker(bind=engine)
session = DBSession()
Base.metadata.create_all(engine)


from __future__ import print_function

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

from dcgenerator import Generator
from dcdiscriminator import Discriminator

class DCGAN(object):
    def __init__(self, batch_size, in_shape, g_depths, d_depths, colorspace_dim=3,
            s_size=4, n_latent=100, eta=0.0002, beta1=0.5):
        self.batch_size = batch_size
        self.s_size = s_size
        self.n_latent = n_latent # Also known as z-dim
        self.in_shape = in_shape
        self.colorspace_dim = colorspace_dim

        # Input image placeholder
        self.x_placeholder = tf.placeholder(tf.float32, [None, in_shape[0],
            in_shape[1], colorspace_dim])
        # Latent probability distribution sample placeholder
        self.z_placeholder = tf.placeholder(tf.float32, [None, 1, 1, n_latent])

        # Generator, given sample from latent space
        self.x_hat = Generator(self.z_placeholder, g_depths,
                colorspace_dim=colorspace_dim, s_size=s_size, reuse=False,
                training=True).model[-1]
        # Discriminator for fake image
        self.y_hat = Discriminator(self.x_hat, d_depths, batch_size,
                colorspace_dim=colorspace_dim, reuse=False,
                training=True).model[-1]
        # Discriminator for real
        self.y = Discriminator(self.x_placeholder, d_depths, batch_size,
                colorspace_dim=colorspace_dim, reuse=True,
                training=True).model[-1]

        # Loss tensors
        self.d_loss = Discriminator.loss(self.y, self.y_hat)
        self.g_loss = Generator.loss(self.y_hat)

        # Variable list
        self.gen_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                scope="generator")
        self.dis_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                scope="discriminator")

        print("Generator variables:")
        print(self.gen_vars)
        print("Discriminator variables:")
        print(self.dis_vars)

        # Optimizers
        self.d_optimizer = tf.train.AdamOptimizer(eta,
                beta1=beta1).minimize(self.d_loss, var_list=self.dis_vars)
        self.g_optimizer = tf.train.AdamOptimizer(eta,
                beta1=beta1).minimize(self.g_loss, var_list=self.gen_vars)

        # Session
        self.session = tf.InteractiveSession()
        self.session.run(tf.global_variables_initializer())
        self.saver = tf.train.Saver(max_to_keep=None)

    def sample(self, z_mu):
        return self.session.run(self.x_hat, feed_dict={
            self.z_placeholder: z_mu
        })

    def generate_file(self, z_mus, step, n=20, img_dir="images/"):
        out = np.empty( (self.in_shape[0] * n, self.in_shape[1] * n,
            self.colorspace_dim) )
        if self.colorspace_dim == 1:
            out = np.empty( (self.in_shape[0] * n, self.in_shape[1] * n) )
        for x_idx in range(n):
            for y_idx in range(n):
                img = self.sample(z_mus[x_idx + y_idx * n])
                if self.colorspace_dim == 1:
                    out[x_idx * self.in_shape[0] : (x_idx+1) * self.in_shape[0],
                        y_idx * self.in_shape[1] : (y_idx+1) * self.in_shape[1]] = img[0].reshape(self.in_shape)
                else:
                    out[x_idx * self.in_shape[0] : (x_idx+1) * self.in_shape[0],
                        y_idx * self.in_shape[1] : (y_idx+1) * self.in_shape[1]] = img[0].reshape(
                                tuple(list(self.in_shape) + [ self.colorspace_dim ]) )
        plt.imsave(img_dir + str(step) + ".png", out, cmap="hsv")

    def train(self, dataset, img_size=64, num_steps=5000, d_steps=1, save=True):
        z_mus = [np.random.uniform(-1,1,[1,1,1,self.n_latent]) for _ in range(400)]

        for step in range(num_steps):
            batch_x, _ = dataset.next_batch(self.batch_size)
            batch_x = batch_x.reshape((-1, img_size, img_size, self.colorspace_dim))
            batch_x = tf.image.resize_images(batch_x, list(self.in_shape)).eval()
            batch_x = (batch_x - 0.5) / 0.5
            for _ in range(d_steps):
                batch_z = np.random.uniform(-1, 1, [self.batch_size,
                    1, 1, self.n_latent])
                self.session.run(self.d_optimizer, feed_dict={
                    self.x_placeholder: batch_x,
                    self.z_placeholder: batch_z,
                })
            batch_z = np.random.uniform(-1, 1, [self.batch_size, 1, 1,
                self.n_latent])
            self.session.run(self.g_optimizer, feed_dict={
                self.z_placeholder: batch_z,
            })

            if step % 10 == 0:
                print("Step: %r of %r" % (step, num_steps))
            if step % 500 == 0:
                self.generate_file(z_mus, step)
            if save and step % 1000 == 0:
                with open("checkpoints/animeds-step_%r.pyobj" % (step,), 'w') as _f:
                    pickle.dump(dataset, _f)
                print("Saved to:", self.saver.save(self.session, "checkpoints/step_%r.ckpt" % (step,)))


import io
import json
from dataclasses import field

import qrcode
from flasgger import swag_from
from flask import Blueprint, jsonify, request, send_file
from flask_limiter import Limiter
from qrcode.image.pil import PilImage

from extensions.deployment.service.deployment_service import DeploymentService
from sdk import convertibleclass
from sdk.auth.validators import validate_project_and_client_id
from sdk.common.constants import SWAGGER_DIR
from sdk.common.utils import inject
from sdk.common.utils.convertible import required_field, default_field, meta
from sdk.common.utils.validators import (
    remove_none_values,
    default_version_meta,
    not_empty,
)
from sdk.phoenix.config.server_config import PhoenixServerConfig
from sdk.versioning.models.version_field import VersionField

api = Blueprint("deployment_public_route", __name__, url_prefix="/api/public/v1beta")

@api.before_app_first_request
def init_limit():
    limiter = inject.instance(Limiter)
    limiter.limit("4/minute")(api)

@api.route("/activation-code/<code>", methods=["GET"])
@swag_from(f"{SWAGGER_DIR}/check_activation_code.yml")
def check_activation_code(code):
    (
        deployment,
        code_type,
        care_plan_group_id,
    ) = DeploymentService().retrieve_deployment_with_code(code)

    if not deployment:
        return None, 400

    return (
        jsonify(
            remove_none_values(
                {
                    "type": code_type,
                    "privacyPolicyUrl": deployment.privacyPolicyUrl,
                    "eulaUrl": deployment.eulaUrl,
                    "termAndConditionUrl": deployment.termAndConditionUrl,
                    "mfaRequired": deployment.mfaRequired,
                    "security": deployment.security,
                }
            )
        ),
        201,
    )

@convertibleclass
class Region:
    BUCKET = "bucket"
    CLIENT_ID = "clientId"
    PROJECT_ID = "projectId"
    END_POINT_URL = "endPointUrl"
    MINIMUM_VERSION = "minimumVersion"
    BUCKET_REGION = "bucketRegion"
    COUNTRY_CODE = "countryCode"
    STAGE = "stage"

    bucket: str = required_field()
    bucketRegion: str = field(default="us-west-2")
    clientId: str = required_field(metadata=meta(not_empty))
    countryCode: str = field(default="gb")
    endPointUrl: str = required_field()
    projectId: str = required_field(metadata=meta(not_empty))
    stage: str = field(default="DYNAMIC")
    minimumVersion: VersionField = default_field(metadata=default_version_meta())

    def to_qrcode(self):
        return qrcode.make(json.dumps(self.to_dict(include_none=False)))

    @classmethod
    def validate(cls, instance):
        validate_project_and_client_id(instance.clientId, instance.projectId)

@api.route("/region", methods=["GET"])
def get_region():
    args = request.args or {}
    client_id = args.get("clientId")
    response_type = args.get("type")
    if not client_id:
        return "Not Found", 404

    config: PhoenixServerConfig = inject.instance(PhoenixServerConfig)
    data = {
        Region.BUCKET: config.server.storage.defaultBucket,
        Region.CLIENT_ID: client_id,
        Region.PROJECT_ID: config.server.project.id,
        Region.END_POINT_URL: request.host_url.replace("http://", "https://", 1),
    }
    client = config.server.project.get_client_by_id(client_id)
    if client and client.minimumVersion:
        data.update({Region.MINIMUM_VERSION: client.minimumVersion})

    region = Region.from_dict(data)
    if response_type == "qrCode":
        qr_code: PilImage = region.to_qrcode()
        output = io.BytesIO()
        qr_code.save(output, format="PNG")
        output.seek(0, 0)
        return send_file(output, mimetype="image/png")
    else:
        return jsonify(region.to_dict(include_none=False)), 200


from __future__ import division

__all__ = ['PropertyPackageConstants', 'IDEAL_PKG', 'NRTL_PKG', 'UNIFAC_PKG',
           'UNIFAC_DORTMUND_PKG', 'PR_PKG', 'SRK_PKG']

from fluids.numerics import brenth, ridder, derivative, numpy as np

from chemicals.utils import log, exp
from fluids.constants import R, pi, N_A
from chemicals.identifiers import IDs_to_CASs
from chemicals.rachford_rice import flash_inner_loop
from thermo.eos_mix import PRMIX, SRKMIX
from thermo.eos import PR, SRK
from thermo.chemical import Chemical
from thermo.mixture import Mixture
from thermo.property_package import *

IDEAL_PKG = 'Ideal'
NRTL_PKG = 'NRTL'
UNIFAC_PKG = 'Unifac'
UNIFAC_DORTMUND_PKG = 'Unifac Dortmund'

PR_PKG = 'PR'
SRK_PKG = 'SRK'

property_packages = [IDEAL_PKG, NRTL_PKG, UNIFAC_PKG, UNIFAC_DORTMUND_PKG,
                     PR_PKG, SRK_PKG]
property_packages_cubic = [PR_PKG, SRK_PKG]

property_package_to_eos = {PR_PKG: PRMIX, SRK_PKG: SRKMIX}
property_package_to_eos_pures = {PR_PKG: PR, SRK_PKG: SRK}

property_package_names_to_objs = {IDEAL_PKG: IdealCaloric,
                                  NRTL_PKG: Nrtl, # Not complete  - enthalpy missing
                                  UNIFAC_PKG: UnifacCaloric,
                                  UNIFAC_DORTMUND_PKG: UnifacDortmundCaloric,
                                  PR_PKG: GceosBase,
                                  SRK_PKG: GceosBase,
                                 }

class PropertyPackageConstants(object):
    '''Class to store kijs, as well as allow properties to be edited; load
    them from the database and then be ready to store them.
    '''
    def __init__(self, mixture, name=IDEAL_PKG, **kwargs):
        if isinstance(mixture, list):
            self.CASs = IDs_to_CASs(mixture)
            self.Chemicals = [Chemical(CAS) for CAS in self.CASs]
        elif isinstance(mixture, Mixture):
            self.Chemicals = mixture.Chemicals
        self.name = name

        if name not in property_packages_cubic:
            eos_mix = PRMIX
            eos = PR
        else:
            eos_mix = property_package_to_eos[name]
            eos = property_package_to_eos_pures[name]

        self.eos = eos
        self.eos_mix = eos_mix

        self.eos_in_a_box = [eos_mix]

        self.pkg_obj = property_package_names_to_objs[self.name]

        self.set_chemical_constants()
        self.set_Chemical_property_objects()
        self.set_TP_sources()

        self.kwargs = kwargs
        self.pkg = self.new_package()

    def new_package(self):
        pkg_args = {'VaporPressures': self.VaporPressures,
                   'Tms': self.Tms, 'Tbs': self.Tbs, 'Tcs': self.Tcs,
                   'Pcs': self.Pcs, 'omegas': self.omegas, 'VolumeLiquids': self.VolumeLiquids,
                   'HeatCapacityLiquids': self.HeatCapacityLiquids,
                   'HeatCapacityGases': self.HeatCapacityGases,
                   'EnthalpyVaporizations': self.EnthalpyVaporizations,
                   'VolumeLiquids': self.VolumeLiquids,
                   'VolumeGases': self.VolumeGases,
                    'eos': self.eos, 'eos_mix': self.eos_mix,
                    'MWs': self.MWs,
                    'atomss': self.atomss,
                    'Hfs': self.Hfgms,
                    'Gfs': self.Gfgms,
                   }
        pkg_args.update(self.kwargs)

        if self.name == UNIFAC_PKG:
            pkg_args['UNIFAC_groups'] = self.UNIFAC_groups
        elif self.name == UNIFAC_DORTMUND_PKG:
            pkg_args['UNIFAC_groups'] = self.UNIFAC_Dortmund_groups

        return self.pkg_obj(**pkg_args)

    def from_json(self, json):
        self.__dict__.update(json)
        self.set_Chemical_property_objects()
        self.set_TP_sources()

transfer_methods = ['set_chemical_constants', 'set_Chemical_property_objects',
                   'set_TP_sources', 'UNIFAC_Dortmund_groups', 'UNIFAC_groups',
                   'atomss']

try:
    for method in transfer_methods:
        attr = Mixture.__dict__[method]
        setattr(PropertyPackageConstants, method, attr)
except:
    for method in transfer_methods:
        setattr(PropertyPackageConstants, method, getattr(Mixture, method))


import paths
import numpy as np
import ccdproc as ccdp
import figure_utils as utils
import matplotlib.pyplot as plt

DIM = 1030
raw_bias_path = paths.data / "raw_photometry" / "CALIB" / "BIAS"
files = [
    "EFOSC.2000-12-30T05:17:49.057.fits",
    "EFOSC.2000-12-29T04:39:50.783.fits",
    "EFOSC.2000-12-28T22:11:19.687.fits",
    "EFOSC.2000-12-30T04:19:55.000.fits",
]

bias_collection = ccdp.ImageFileCollection(location=raw_bias_path, filenames=files)

biases = list(bias_collection.ccds(ccd_kwargs={"unit": "adu"}))

fig, ax = plt.subplots(2, 2, figsize=(10, 10))
ax = ax.flatten()
for i, bias in enumerate(biases):
    print(f"Mean bias {i + 1}: {np.mean(bias.data)}")
    utils.show_image(bias, fig=fig, ax=ax[i], cbar_label="Signal [ADU]")
    ax[i].set_title(f"Bias {i + 1}", fontsize=16)

fig.suptitle("Randomly Selected Bias Frames", fontsize=20)
fig.tight_layout()
fig.savefig(paths.figures / "random_bias_frames.pdf")


from PIL import Image

from django.template import Library
from django.conf import settings
register = Library()

@register.filter
def pdf_and_docx_to_png(file_path):
    """Converts a PDF or DOCX file to a PNG image.

    Args:
        file_path: The path to the PDF or DOCX file.

    Returns:
        A PIL Image object.
    """
    if file_path.endswith('.pdf'):
        image = Image.open(settings.BASE_DIR / file_path)
        image = image.convert('RGB')
        return image
    elif file_path.endswith('.docx'):
        from docx import Document
        document = Document(file_path)
        image = Image.open(document.part.blob)
        image = image.convert('RGB')
        return image
    else:
        raise Exception('Unsupported file type.')


#!/bin/env python
"""
    Initialize the logging environment.
"""

#
# A simple logger for the name server
#
import sys
import logging
import logConfig

"""
 Look in the current directory for .logcollectorrc .
 If not there, look in  $HOME/.logcollectorrc
 Any user flags will override config file settings.

 The config parameters will look similar to:
    {
        append: True,    # Append logs to existing file.
                         # Creates a file if the filed does not exist.
        log_file:"/home/me/simple/logs.log",  # Name of log file.
        noisy:  False,   # Echo all collected logs to console.
                         # Used in debugging.
        port:   5570,    # Port to listen for messages.
    }

"""
DEFAULT_COLLECTOR_CONFIG_FILE = '.logcollectorrc'

ALREADY_INITED = False  # Allow multiple calls without penalty

def loggerInit():
    """
    Initialize the logger, but only once.
    """
    global ALREADY_INITED

    if ALREADY_INITED:
        return

    # Embed the app name in the log
    logging.basicConfig(filename=logConfig.LOG_FILENAME,
                        format='%(asctime)s.%(msecs)03d\t%(levelname)s' +
                        '\t%(message)s',
                        datefmt='%Y-%m-%dT%H:%M:%S',
                        level=logging.NOTSET)   # NOTSET means log EVERYTHING!
    if logConfig.TESTING:
        # If testing, echo logs to stdout as well as logfile.
        log = logging.getLogger('')
        log.addHandler(logging.StreamHandler(sys.stdout))
        logging.info('Logging Started')
    ALREADY_INITED = True

if __name__ == '__main__':
    logConfig.TESTING = True
    loggerInit()
    # Call again to make sure the init flag works.
    loggerInit()


import os
from pathlib import Path
import xml.etree.ElementTree as ET
from loguru import logger

import cv2
import numpy as np
import random

from .datasets_wrapper import Dataset
from .voc import AnnotationTransform
from yolox.evaluators.voc_eval import voc_eval, det_image_visualization

def write_filesnames(filenames, path):
    num_files = 0
    with open(path, "w") as f:
        for filename in filenames:
            f.write(filename)
            f.write("\n")  # file is created with a final blank line
            num_files += 1
        f.close()
    logger.info(f"ImageSet file created in {path}: {num_files} lines written.")

def retrieve_annotation(data_dir, filename):
    # bad case: cannot find annotation file.
    annopath = os.path.join(data_dir, "Annotations", "{}.xml")
    return ET.parse(annopath.format(filename)).getroot()

def build_imageset(data_dir, set_type="all"):
    images_dir = os.path.join(data_dir, "Images")
    imageset_dir = os.path.join(data_dir, "ImageSets")
    Path(imageset_dir).mkdir(exist_ok=True)

    image_filenames = set()
    for image_file in Path(images_dir).iterdir():
        filename = image_file.with_suffix("").parts[-1]
        if set_type == "all":
            # just add all files; don't retrieve annotations
            image_filenames.add(filename)
            continue
        # retrieve respective annotation file to filter based on criteria
        root = retrieve_annotation(data_dir, filename)
        if set_type == "pure":  # skip all pseudo and bad
            if check_element(root, "pseudo", 1) or check_element(root, "bad", 1):
                continue
        else:
            if check_element(
                root, set_type, 0, default=True
            ):  # if unable to find, true by default
                continue
        image_filenames.add(filename)

    write_filesnames(image_filenames, os.path.join(imageset_dir, f"{set_type}.txt"))

    return image_filenames

def check_element(root, element, value, default=False):
    elem = root.find(element)
    if elem is not None:
        try:
            return int(elem) == value
        except:
            return default
    else:
        return default

EXPECTED_FILE_STRUCTURE = (
    "\ndata_dir\n"
    + "  - Annotations\n"
    + "    - \{img_id\}.xml files\n"
    + "  - Images\n"
    + "    - \{img_id\}.jpg files\n"
)

class CustomVOC(Dataset):
    """ YOLOX dataset class for custom build voc.
    Unlike official VOC, the expected file structure is as such:
    data_dir
        - Annotations
            - (img_id).xml files
        - Images
            - (img_id).jpg files
    """

    def __init__(
        self,
        data_dir,
        class_names,
        img_size=(512, 512),
        preproc=None,
        target_transform=None,
        dataset_name="CustomVOC",
        cache=False,
        remove_fraction=0.0,
        keep_difficult=True,
        keep_fake=True,
        keep_truncated=True,
        keep_occluded=True,
        set_type="all",
    ):
        super().__init__(
            img_size
        )  # Cannot inherit from VOCDet due to file struct ref in init.

        self.root = data_dir
        if target_transform is None:
            self.target_transform = AnnotationTransform(
                class_to_ind=dict(
                    [(label, index) for index, label in enumerate(class_names)]
                ),
                keep_difficult=keep_difficult,
                keep_fake=keep_fake,
                keep_occluded=keep_occluded,
                keep_truncated=keep_truncated,
            )
        else:
            self.target_transform = target_transform
        self.img_size = img_size
        self.preproc = preproc

        # dataset file details:
        self.class_names = class_names

        self._annopath = os.path.join(data_dir, "Annotations", "{}.xml")
        self._imgpath = os.path.join(data_dir, "Images", "{}.jpg")
        self._setpath = os.path.join(data_dir, "ImageSets", "all.txt")
        # TODO: add imagesets for annotations of different properties

        # check files:
        self._assert_folder_exists("Annotations")
        self._assert_folder_exists("Images")
        if not os.path.exists(self._setpath):  # Create if not exist.
            build_imageset(data_dir)  # FYI: used for validation.

        # Different compared to vars in VOCDet:
        self.name = dataset_name
        self.imgs = None
        if cache:
            self._cache_images()

        # populate the image ids for the dataset loader:
        self.ids = list()
        # TODO: use respective imageset file for image ids
        for filename in build_imageset(data_dir, set_type=set_type):
            if random.random() < remove_fraction:
                continue
            self.ids.append(filename)
        self.annotations = self._load_coco_annotations()

    def __len__(self):
        return len(self.ids)

    def _load_coco_annotations(self):
        return [self.load_anno_from_ids(_ids) for _ids in range(len(self.ids))]

    def _cache_images(self):
        logger.warning(
            "\n********************************************************************************\n"
            "You are using cached images in RAM to accelerate training.\n"
            "This requires large system RAM.\n"
            "Make sure you have 60G+ RAM and 19G available disk space for training VOC.\n"
            "********************************************************************************\n"
        )
        max_h = self.img_size[0]
        max_w = self.img_size[1]
        cache_file = self.root + "/img_resized_cache_" + self.name + ".array"
        if not os.path.exists(cache_file):
            logger.info(
                "Caching images for the first time. This might take about 3 minutes for VOC"
            )
            self.imgs = np.memmap(
                cache_file,
                shape=(len(self.ids), max_h, max_w, 3),
                dtype=np.uint8,
                mode="w+",
            )
            from tqdm import tqdm
            from multiprocessing.pool import ThreadPool

            NUM_THREADs = min(8, os.cpu_count())
            loaded_images = ThreadPool(NUM_THREADs).imap(
                lambda x: self.load_resized_img(x), range(len(self.annotations)),
            )
            pbar = tqdm(enumerate(loaded_images), total=len(self.annotations))
            for k, out in pbar:
                self.imgs[k][: out.shape[0], : out.shape[1], :] = out.copy()
            self.imgs.flush()
            pbar.close()
        else:
            logger.warning(
                "You are using cached imgs! Make sure your dataset is not changed!!\n"
                "Everytime the self.input_size is changed in your exp file, you need to delete\n"
                "the cached data and re-generate them.\n"
            )

        logger.info("Loading cached imgs...")
        self.imgs = np.memmap(
            cache_file,
            shape=(len(self.ids), max_h, max_w, 3),
            dtype=np.uint8,
            mode="r+",
        )

    def load_anno_from_ids(self, index):
        img_id = self.ids[index]
        target = ET.parse(self._annopath.format(img_id)).getroot()

        assert self.target_transform is not None
        res, img_info = self.target_transform(target)
        height, width = img_info

        r = min(self.img_size[0] / height, self.img_size[1] / width)
        res[:, :4] *= r
        resized_info = (int(height * r), int(width * r))

        return (res, img_info, resized_info)

    def load_anno(self, index):
        return self.annotations[index][0]

    def load_resized_img(self, index):
        img = self.load_image(index)
        r = min(self.img_size[0] / img.shape[0], self.img_size[1] / img.shape[1])
        resized_img = cv2.resize(
            img,
            (int(img.shape[1] * r), int(img.shape[0] * r)),
            interpolation=cv2.INTER_LINEAR,
        ).astype(np.uint8)

        return resized_img

    def load_image(self, index):
        img_id = self.ids[index]
        img = cv2.imread(self._imgpath.format(img_id), cv2.IMREAD_COLOR)
        assert img is not None

        return img

    def pull_item(self, index):
        """Returns the original image and target at an index for mixup

        Note: not using self.__getitem__(), as any transformations passed in
        could mess up this functionality.

        Argument:
            index (int): index of img to show
        Return:
            img, target
        """
        if self.imgs is not None:
            target, img_info, resized_info = self.annotations[index]
            pad_img = self.imgs[index]
            img = pad_img[: resized_info[0], : resized_info[1], :].copy()
        else:
            img = self.load_resized_img(index)
            target, img_info, _ = self.annotations[index]

        return img, target, img_info, index

    @Dataset.mosaic_getitem
    def __getitem__(self, index):
        img, target, img_info, img_id = self.pull_item(index)

        if self.preproc is not None:
            img, target = self.preproc(img, target, self.input_dim)

        return img, target, img_info, img_id

    def evaluate_detections(self, all_boxes, output_dir=None):
        # need to overwrite super class method for this
        # the dataset does not support the requirements.

        # write detections to a file so they can be retrieved:
        self._write_voc_results_file(all_boxes)

        IouTh = np.linspace(
            0.5, 0.95, int(np.round((0.95 - 0.5) / 0.05)) + 1, endpoint=True
        )
        mAPs = []
        for iou in IouTh:
            mAP = self.calculate_mAP(iou_threshold=iou)
            mAPs.append(mAP)

        print("--------------------------------------------------------------")
        print("map_5095:", np.mean(mAPs))
        print("map_50:", mAPs[0])
        print("--------------------------------------------------------------")
        return np.mean(mAPs), mAPs[0]

    def _write_voc_results_file(self, all_boxes):
        for cls_ind, cls in enumerate(self.class_names):
            cls_ind = cls_ind
            if cls == "__background__":
                continue
            print("Writing {} VOC results file".format(cls))
            filename = self._get_voc_results_file_template().format(cls)
            with open(filename, "wt") as f:
                for im_ind, index in enumerate(self.ids):
                    # index = index[1] -> ids = img_id
                    dets = all_boxes[cls_ind][im_ind]
                    if dets == []:
                        continue
                    for k in range(dets.shape[0]):
                        f.write(
                            "{:s} {:.3f} {:.1f} {:.1f} {:.1f} {:.1f}\n".format(
                                index,
                                dets[k, -1],
                                dets[k, 0] + 1,
                                dets[k, 1] + 1,
                                dets[k, 2] + 1,
                                dets[k, 3] + 1,
                            )
                        )

    def _get_voc_results_file_template(self):
        # similar to super but reference to year is removed.
        filename = "comp4_det_test" + "_{:s}.txt"
        filedir = os.path.join(self.root, "results")  # , "VOC", "Main")
        if not os.path.exists(filedir):
            os.makedirs(filedir)
        path = os.path.join(filedir, filename)
        return path

    def visualize_detections(self):
        class_visualizations = {}

        for label in self.class_names:
            if label == "__background__":
                continue
            detpath = self._get_voc_results_file_template().format(label)

            class_visualizations[label] = det_image_visualization(
                detpath, self._imgpath, label
            )
        return class_visualizations

    def calculate_mAP(self, iou_threshold=0.5):
        imagesetfile = self._setpath
        cachedir = os.path.join(self.root, "annotations_cache")  # , "VOC", "train")
        if not os.path.exists(cachedir):
            os.makedirs(cachedir)

        aps = []

        for label in self.class_names:
            if label == "__background__":
                continue

            # cool that this can be done (formating outside func call):
            filename = self._get_voc_results_file_template().format(label)
            rec, prec, ap = voc_eval(
                filename,
                self._annopath,
                imagesetfile,
                label,
                cachedir,
                ovthresh=iou_threshold,
            )

            aps += [ap]  # extend list

            if iou_threshold == 0.5:
                print("AP for {} = {:.4f}".format(label, ap))
        if iou_threshold == 0.5:
            print("Mean AP = {:.4f}".format(np.mean(aps)))

        return np.mean(aps)

    def _assert_folder_exists(self, folder):
        assert os.path.exists(os.path.join(self.root, folder)), (
            f"Could not find {folder}. Check that the dataset file structure is correct:"
            + EXPECTED_FILE_STRUCTURE
        )


import requests
import inspect

# Designed to be the Parent class for all the FindBySomething classes
class FindByX:

    def __init__(self):
        stack = inspect.stack()
        # Storing the child class name when its object is created
        self.class_name = stack[1][0].f_locals["self"].__class__.__name__

    # Generates url to query the API
    # NOTE: the calling function's Class must be present as key in the config.json to generate corresponding URL
    def generate_url(self, domain, config):
        url = domain
        try:
            class_name = self.class_name

            if class_name in config:
                config = config[class_name]
                url = url + '&'.join([f"{key}={value}" for key, value in config.items()])
                return url

        except Exception as error:
            print('No information found for ' + class_name)
            print('Exiting program to avoid over information sharing.')
            exit(0)

    # Used to get data from the API
    def execute_request(self, request_url):
        try:
            response = requests.get(request_url)
            if response.status_code == requests.codes.ok:
                response_json = response.json()
                return response_json
            else:
                print('Response Status Code:', response.status_code)
                print(response.text)
                raise Exception(response)
        except Exception as error:
            print(error)
            exit(0)


#!/usr/bin/env python
import subprocess, os, sys
listShellInput = "ls -a " + os.path.dirname(os.path.realpath(sys.argv[0])) + "/*.sh"
directoryContents = subprocess.Popen(listShellInput, shell=True, stdout=subprocess.PIPE)
directoryContentsString = directoryContents.communicate()[0]
#print (directoryContentsString.decode('ascii'))
directoryContentsOutput = ""
for line in directoryContentsString.decode('ascii').split('\n'):
	line = line[:-3]
	print(line)
	line = line.rsplit('/',1)[-1]
	directoryContentsOutput = directoryContentsOutput + line + "\n"
#print(directoryContentsOutput)
result = subprocess.Popen(['/usr/bin/bash'], shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
roficommand = 'echo "' + directoryContentsOutput + '" | rofi -dmenu run -lines 4 -bw 20 -eh 2 -width 600 -padding 40 -p " > "'
# roficommand = 'echo "' + directoryContentsOutput + '" | rofi -dmenu -lines 3 -eh 2 -width 100 -fullscreen -opacity "85" -bw 0 -bc "$bg-color" -bg "$bg-color" -fg "$text-color" -hlbg "$bg-color" -hlfg "#9575cd" -font "Ubuntu 18" -p " > "'
stdout = result.stdin.write(str(roficommand).encode())
screenChoice = result.communicate()[0].decode('ascii')
#now to actually execute the shell
screenChoice = screenChoice[:-1] + ".sh"
actualExecution = subprocess.Popen(['/usr/bin/bash'], shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
finalCommand = os.path.dirname(os.path.realpath(sys.argv[0])) + "/" + screenChoice
stdout = actualExecution.stdin.write(str(finalCommand).encode())


"""
Small utility for creating shareable links to Civis services.
"""
import argparse

import civis

def list_services(args):
    client = civis.APIClient()
    services = client.services.list()
    print("List of services:")
    for service in services:
        print(f"\tID: {service['id']}\tName: {service['name']}")

def share_service(args):
    client = civis.APIClient()
    service = client.services.get(args.id)
    try:
        response = client.services.post_tokens(args.id, args.name)
        url = f"{service['current_url']}/civis-platform-auth?token={response['token']}"
        print(f"Share service id {args.id} with the following URL: {url}")
    except civis.base.CivisAPIError as e:
        if "Name has already been taken" in str(e):
            print(
                f"The share name {args.name} is already in use. "
                "Please choose another"
            )
        else:
            raise e

def unshare_service(args):
    client = civis.APIClient()
    tokens = client.services.list_tokens(args.id)
    try:
        token = next(t for t in tokens if t["name"] == args.name)
        client.services.delete_tokens(args.id, token["id"])
        print(f"Successfully unshared {args.name}")
    except StopIteration:
        print(f"Could not find share token with the name {args.name}")

def main():
    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers()

    list_parser = subparsers.add_parser("list")
    list_parser.set_defaults(func=list_services)

    share_parser = subparsers.add_parser("share")
    share_parser.add_argument("id", type=int, help="The ID of the service to share")
    share_parser.add_argument("name", type=str, help="A name to give the share URL")
    share_parser.set_defaults(func=share_service)

    unshare_parser = subparsers.add_parser("unshare")
    unshare_parser.add_argument("id", type=int, help="The ID of the service to unshare")
    unshare_parser.add_argument(
        "name", type=str, help="The name of the service to unshare"
    )
    unshare_parser.set_defaults(func=unshare_service)

    args = parser.parse_args()
    args.func(args)

if __name__ == "__main__":
    main()


import sys
sys.path.append("../..")
sys.path.append("..")

from limits.limits import *
from hypothesis.strategies import *
from hypothesis import given
from timeout import run_with_timeout
from typing import *

numbers = lists(integers(), min_size=1, max_size=MAX_SEQUENCE_LEN)
strategy = numbers
if not isinstance(strategy, tuple):
    strategy = (strategy,)

def multiply_num(numbers):
    total = 1
    for x in numbers:
        total *= x
    return total/len(numbers)

@given(tuples(*strategy))
def test_fuzz(args):
    run_with_timeout(0.3, multiply_num, *args)


"""
Get admissions. Fannie Barskhian 2019.
"""
from data.applications import Applications
from school.admissions import get
class Run(object):
    """
    Invoking the admissions logic.
    """

    def print_list(self):
        """
        Print admitted and waitlisted candidate details.
        """
        applications = Applications(1000)
        admitted = get(applications)
        admitted_applicants = admitted[0]
        waitlisted_applicants = admitted[1]

        print("------------Admitted Applicants-----------------")
        for index, applicant in enumerate(admitted_applicants):
            print("%10d%22d" %(index + 1,applicant["application_id"]))
        print("-----------Waitlisted Applicants----------------")
        for index, applicant in enumerate(waitlisted_applicants):
            print("%10d%22d" %(index + 1,applicant["application_id"]))

if __name__ == "__main__":
    Run().print_list()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
from __future__ import unicode_literals
from lineup import Pipeline
from carpentry.workers.steps import (
    PrepareSSHKey,
    PushKeyToGithub,
    LocalRetrieve,
    RunBuild,
    CheckAndLoadBuildFile,
    PrepareShellScript,
    DockerDependencyStopper,
    DockerDependencyRunner,
)

class RunBuilder(Pipeline):

    """A very simple builder that just runs subprocesses in the
    machine where carpentry is installed.
    """
    name = 'local-builder'
    steps = [
        PrepareSSHKey,
        PushKeyToGithub,
        LocalRetrieve,
        CheckAndLoadBuildFile,
        PrepareShellScript,
        DockerDependencyRunner,
        RunBuild,
        DockerDependencyStopper,
    ]


#Ryan Sikora
#This program will take the data from seedsData.txt and use K-Means clustering
#to correctly sort the kernels into three groups, it will also compute each cluster's
#cohesion, separation, and integerity

import random,pylab

#figure 22.6 (repeated)
def minkowskiDist(v1, v2, p):
    """Assumes v1 and v2 are equal-length arrays of numbers
       Returns Minkowski distance of order p between v1 and v2"""
    dist = 0.0
    for i in range(len(v1)):
        dist += abs(v1[i] - v2[i])**p
    newdist = dist**(1/float(p))
    return newdist

#Figure 23.2
class Example(object):

    def __init__(self, name, features, label = None):
        #Assumes features is an array of floats
        self.name = name
        self.features = features
        self.label = label

    def dimensionality(self):
        return len(self.features)

    def getFeatures(self):
        return self.features[:]

    def getLabel(self):
        return self.label

    def getName(self):
        return self.name

    def distance(self, other):
        return minkowskiDist(self.features, other.getFeatures(), 2)

    def __str__(self):
        return self.name +':'+ str(self.features) + ':'\
               + str(self.label)

#Figure 23.3
class Cluster(object):

    def __init__(self, examples):
        """Assumes examples a non-empty list of Examples"""
        self.examples = examples
        self.centroid = self.computeCentroid()

    def update(self, examples):
        """Assume examples is a non-empty list of Examples
           Replace examples; return amount centroid has changed"""
        oldCentroid = self.centroid
        self.examples = examples
        self.centroid = self.computeCentroid()
        return oldCentroid.distance(self.centroid)

    def computeCentroid(self):
        vals = pylab.array([0.0]*self.examples[0].dimensionality())
        for e in self.examples: #compute mean
            vals += e.getFeatures()
        centroid = Example('centroid', vals/len(self.examples))
        return centroid

    def getCentroid(self):
        return self.centroid

    def variability(self):
        totDist = 0.0
        for e in self.examples:
            totDist += (e.distance(self.centroid))**2
        return totDist

    def members(self):
        for e in self.examples:
            yield e

    def __str__(self):
        names = []
        kama = []
        rosa = []
        canadian = []
        for e in self.examples:
            names.append(e.getName())
        names.sort()
        for e in self.examples:
            f = e.getFeatures()
            if f[7] == 1:
                kama.append(f)
            if f[7] == 2:
                rosa.append(f)
            if f[7] == 3:
                canadian.append(f)
        result = 'Cluster with centroid '\
               + str(self.centroid.getFeatures()) + ' contains:\n  ' + str(len(kama)) + ' Kama ' + str(len(rosa)) + ' Rosa ' + str(len(canadian)) + ' Canadian.'

        return result[:]

#Figure 23.4
def dissimilarity(clusters):
    totDist = 0.0
    for c in clusters:
        totDist += c.variability()
    return totDist

def trykmeans(examples, numClusters, numTrials, verbose = False):
    """Calls kmeans numTrials times and returns the result with the
          lowest dissimilarity"""
    best = kmeans(examples, numClusters, verbose)
    minDissimilarity = dissimilarity(best)
    trial = 1
    while trial < numTrials:
        try:
            clusters = kmeans(examples, numClusters, verbose)
        except ValueError:
            continue #If failed, try again
        currDissimilarity = dissimilarity(clusters)
        if currDissimilarity < minDissimilarity:
            best = clusters
            minDissimilarity = currDissimilarity
        trial += 1
    return best

#figure 23.5
def kmeans(examples, k, verbose = False):
    #Get k randomly chosen initial centroids, create cluster for each
    initialCentroids = random.sample(examples, k)
    clusters = []
    for e in initialCentroids:
        clusters.append(Cluster([e]))

    #Iterate until centroids do not change
    converged = False
    numIterations = 0
    while not converged:
        numIterations += 1
        #Create a list containing k distinct empty lists
        newClusters = []
        for i in range(k):
            newClusters.append([])

        #Associate each example with closest centroid
        for e in examples:
            #Find the centroid closest to e
            smallestDistance = e.distance(clusters[0].getCentroid())
            index = 0
            for i in range(1, k):
                distance = e.distance(clusters[i].getCentroid())
                if distance < smallestDistance:
                    smallestDistance = distance
                    index = i
            #Add e to the list of examples for appropriate cluster
            newClusters[index].append(e)

        for c in newClusters: #Avoid having empty clusters
            if len(c) == 0:
                raise ValueError('Empty Cluster')

        #Update each cluster; check if a centroid has changed
        converged = True
        for i in range(k):
            if clusters[i].update(newClusters[i]) > 0.0:
                converged = False

        if verbose:
            print('Iteration #' + str(numIterations))
            for c in clusters:
                print(c)
            print('') #add blank line
    return clusters

def feature(featureDatafile):
    infile=open(featureDatafile,'r')
    feature={}
    badlist = []
    ID=1
    for line in infile: #adding all the data to the feature dictionary
        feature[ID]=line.replace('\n',' ').split('\t')
        ID+=1
    infile.close()
    for i in feature: #getting rid of all dictionary entries that are empty
        if feature[i] == [' ']:
            badlist.append(i)
    for b in badlist:
        del feature[b]
    return feature

def makeExamples(f):
    #using a feature dictionary, creates examples for use in clustering
    examples=[]
    for key in f:
        examples.append(Example(str(key),[float(x) for x in f[key][:]]))
    return examples

def cohesion(cluster):
    '''Given a cluster, it computes the reciprocal of the sum of each example's distance from the centroid'''
    centroid = cluster.getCentroid()
    coh = 0
    for c in cluster.examples:
        cc = c.distance(centroid)
        coh = coh + cc
    return 1/coh

def separation(cluster1,cluster2):
    '''Given two clusters, it computes the distance between the two centroids'''
    dist = minkowskiDist(cluster1.getCentroid().getFeatures(),cluster2.getCentroid().getFeatures(),2)
    return dist

def integerity(cluster):
    '''Given a cluster, it computers the proportion of the cluster comprised by the majority type in the cluster'''
    kama = []
    rosa = []
    canadian = []
    for i in cluster.examples:
        f = i.getFeatures()
        if f[7] == 1:
            kama.append(f)
        if f[7] == 2:
            rosa.append(f)
        if f[7] == 3:
            canadian.append(f)
    maxm = max([len(kama),len(rosa),len(canadian)])
    integ = maxm*((len(kama)+len(rosa)+len(canadian)))**-1
    return integ

#makes the examples using the generated future dictionary
e = makeExamples(feature('seedsData.txt'))

#computes the best kmeans cluster
best = trykmeans(e,3,500)

#defines each cluster as Cluster 1, Cluster 2, Cluster 3
print('Cluster 1 is a ' + str(best[0]) + '\n')
print('Cluster 2 is a ' + str(best[1]) + '\n')
print('Cluster 3 is a ' + str(best[2])+ '\n')

#creates a list of each cluster's cohesion
cohesions = []
coh1 = cohesion(best[0])
coh2 = cohesion(best[1])
coh3 = cohesion(best[2])

#prints each cluster's cohesion
print('The cohesion of Cluster 1 is ' + str(coh1))
print('The cohesion of Cluster 2 is ' + str(coh2))
print('The cohesion of Cluster 3 is ' + str(coh3) + '\n')

#finds the max cohesion
cohesions.append(coh1)
cohesions.append(coh2)
cohesions.append(coh3)
maxcoh = max(cohesions)

#finds the cluster with the max cohesion and prints it
if maxcoh == cohesions[0]:
    print('The largest cohesion is from Cluster 1 with a cohesion of ' + str(maxcoh) + '\n')
elif maxcoh == cohesions[1]:
    print('The largest cohesion is from Cluster 2 with a cohesion of ' + str(maxcoh) + '\n')
elif maxcoh == cohesions[2]:
    print('The largest cohesion is from Cluster 3 with a cohesion of ' + str(maxcoh) + '\n')

#creates a list of each possible separation combination
seps = []
sep12 = separation(best[0],best[1])
sep23 = separation(best[1],best[2])
sep13 = separation(best[0],best[2])

#prints each separation combination
print('The separation between Cluster 1 and Cluster 2 is ' + str(sep12))
print('The separation between Cluster 2 and Cluster 3 is ' + str(sep23))
print('The separation between Cluster 1 and Cluster 3 is ' + str(sep13) + '\n')

#finds the max separation
seps.append(sep12)
seps.append(sep23)
seps.append(sep13)
maxsep = max(seps)

#finds the pair of clusters with the highest separation
if maxsep == seps[0]:
    print('The largest separation is between Cluster 1 and Cluster 2 with separation ' + str(maxsep) + '\n')
elif maxsep == seps[1]:
    print('The largest separation is between Cluster 2 and Cluster 3 with separation ' + str(maxsep) + '\n')
elif maxsep == seps[2]:
    print('The largest separation is between Cluster 1 and Cluster 3 with separation ' + str(maxsep) + '\n')

#creates a list and computes all integerities
integs = []
integ1 = integerity(best[0])
integ2 = integerity(best[1])
integ3 = integerity(best[2])

#prints each integerity
print('The integerity of Cluster 1 is ' + str(integ1))
print('The integerity of Cluster 2 is ' + str(integ2))
print('The integerity of Cluster 3 is ' + str(integ3) + '\n')

#computes the max integerity
integs.append(integ1)
integs.append(integ3)
integs.append(integ2)
maxinteg = max(integs)

#finds the cluster with the highest integerity and prints it
if maxinteg == integs[0]:
    print('The largest integerity is from Cluster 1 with an integerity of ' + str(maxinteg) + '\n')
elif maxinteg == integs[1]:
    print('The largest integerity is from Cluster 2 with an integerity of ' + str(maxinteg) + '\n')
elif maxinteg == integs[2]:
    print('The largest integerity is from Cluster 3 with an integerity of ' + str(maxinteg) + '\n')

#Computes the global integerity of the cluster grouping
globalinteg = 0
for i in range(len(integs)):
    globalinteg = globalinteg + integs[i]

avg = globalinteg/len(integs)
print('The total Global Integerity is ' + str(avg))


from odoo import models, fields, api

from odoo.exceptions import ValidationError

class ConstructionWorkLine(models.Model):
    _name = "cstr.work.line"
    _description = "Construction Work Line Item"

    work_id = fields.Many2one("cstr.work", string="Work")
    description = fields.Char("Description")
    amount = fields.Float("Amount")
    start_date = fields.Date("Start Date")
    end_date = fields.Date("End Date")
    work_line_progress_bar = fields.Float(string='Progresso', default=0.0)
    unity = fields.Selection(
        [
            ("sq_m", "Square Meter"),
            ("sq_ft", "Square Foot"),
            ("cubic_m", "Cubic Meter"),
            ("cubic_ft", "Cubic Foot"),
            ("linear_m", "Linear Meter"),
            ("linear_ft", "Linear Foot"),
            ("kg", "Kilogram"),
            ("lb", "Pound"),
            ("unit", "Unit"),
            ("liter", "Liter"),
            ("gallon", "Gallon"),
        ],
        string="Unit of Measure",    )

    @api.model
    def create(self, vals):
        if 'work_line_progress_bar' in vals:
            if not 0.0 <= vals['work_line_progress_bar'] <= 100.0:
                raise ValidationError("O valor do progresso deve estar entre 0.0 e 100.0")
        return super('cstr.work.line', self).create(vals)

    def write(self, vals):
        if 'work_line_progress_bar' in vals:
            if not 0.0 <= vals['work_line_progress_bar'] <= 100.0:
                raise ValidationError("O valor do progresso deve estar entre 0.0 e 100.0")
        return super('cstr.work.line', self).write(vals)


from django import forms
from .models import Pergunta, Simulado
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User

class RespostaForm(forms.Form):
    def __init__(self, *args, **kwargs):
        questoes = kwargs.pop('perguntas')  # Removendo 'perguntas' do kwargs
        super(RespostaForm, self).__init__(*args, **kwargs)
        for pergunta in questoes:
            alternativas = pergunta.alternativa_set.all()
            self.fields[f'pergunta_{pergunta.id}'] = forms.ChoiceField(
                choices=[(alternativa.id, alternativa.texto) for alternativa in alternativas],
                widget=forms.RadioSelect,
                label=pergunta.enunciado,
                required=False,
                initial=self.initial.get(f'pergunta_{pergunta.id}')  # Definindo as alternativas iniciais
            )

class CustomUserCreationForm(UserCreationForm):
    class Meta:
        model = User
        fields = ['username', 'first_name', 'last_name', 'email', 'password1', 'password2']


# based on https://github.com/pypa/sampleproject
# MIT License

# Always prefer setuptools over distutils
from setuptools import setup, find_packages
from os import path
from io import open

here = path.abspath(path.dirname(__file__))

# Get the long description from the README file
with open(path.join(here, 'README.md'), encoding='utf-8') as f:
    long_description = f.read()

# Extract version from cbsodata.py
for line in open(path.join("batchgen", "__init__.py")):
    if line.startswith('__version__'):
        exec(line)
        break

setup(
    name='batchgen',
    version=__version__,  # noqa
    description='Universal Batch Generator',
    long_description=long_description,
    long_description_content_type='text/markdown',
    url='https://github.com/UtrechtUniversity/hpc-batch-gen',
    author='Utrecht University',
    author_email='r.d.schram@uu.nl',
    classifiers=[
        # How mature is this project? Common values are
        #   3 - Alpha
        #   4 - Beta
        #   5 - Production/Stable
        'Development Status :: 3 - Alpha',

        # Pick your license as you wish
        'License :: OSI Approved :: MIT License',

        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3.7',
    ],
    keywords='batch systems parallelization',
    packages=find_packages(exclude=['samples']),

    install_requires=[],

    extras_require={
    },

    # package_data={
    #     'sample': ['data/package_data.dat'],
    # },
    # data_files=[('my_data', ['data/data_file'])],

    entry_points={
        'console_scripts': [
            'batchgen=batchgen.__main__:main'],
    },

    project_urls={
        'Bug Reports':
            'https://github.com/UtrechtUniversity/hpc-batch-gen/issues',
        'Source': 'https://github.com/UtrechtUniversity/hpc-batch-gen/',
    },
)


import requests

def schema2entity(name):
    data = requests.get("http://localhost:3333/api/entity/schema/19?name="+str(name))
    if(len(data.json())>0):
        return "Q"+str(data.json()[0]['_fields'][0]['identity']['low'])
    return None

def children(schema):
    if(len(schema['children'])>0):
        for child in schema['children']:
            print(str(child['id'])+':'+str(child['name']))
            entity = schema2entity(child['name'])
            if(entity is not None):
                requests.put('http://localhost:3333/api/schemas/'+str(child['id'])+'/'+str(entity))
            children(child)

data = requests.get('http://localhost:3333/api/schemas/19')
print(data.json()['children'])
for schema in data.json()['children']:
    print(str(schema['id'])+':'+str(schema['name']))
    entity = schema2entity(schema['name'])
    requests.put('http://localhost:3333/api/schemas/'+str(schema['id'])+'/'+str(entity))
    print(entity)
    children(schema)


import os
import unittest

import carla
import srunner.challenge.utils.route_configuration_parser as parser
from srunner.challenge.challenge_evaluator_routes import ChallengeEvaluator
from srunner.challenge.utils.route_manipulation import interpolate_trajectory
from srunner.scenariomanager.carla_data_provider import CarlaActorPool
from srunner.scenariomanager.carla_data_provider import CarlaDataProvider

"""
The idea of this test is to check if sampling is able to sample random sequencial images
inside a batch

"""

class Arguments():
    def __init__(self):
        self.agent = None
        self.use_docker = False
        self.host = '127.0.0.1'
        self.port = 2000
        self.split = 'dev_track_1'

class TestRouteGenerator(unittest.TestCase):

    def __init__(self, name='runTest'):
        unittest.TestCase.__init__(self, name)
        self.root_route_file_position = 'srunner/challenge/'

    def test_route_parser(self):

        args = Arguments()
        client = carla.Client(args.host, int(args.port))
        client.set_timeout(25.0)
        challenge = ChallengeEvaluator(args)

        filename = os.path.join(self.root_route_file_position, 'all_towns_traffic_scenarios.json')
        world_annotations = parser.parse_annotations_file(filename)
        # retrieve routes
        # Which type of file is expected ????

        filename = os.path.join(self.root_route_file_position, 'routes_training.xml')
        list_route_descriptions = parser.parse_routes_file(filename)

        # For each of the routes to be evaluated.
        for route_description in list_route_descriptions:
            if route_description['town_name'] == 'Town03' or route_description['town_name'] == 'Town01':
                continue
            print (" TOWN: ", route_description['town_name'])
            challenge.world = client.load_world(route_description['town_name'])
            # Set the actor pool so the scenarios can prepare themselves when needed
            CarlaActorPool.set_world(challenge.world)

            CarlaDataProvider.set_world(challenge.world)
            # find and filter potential scenarios
            # Returns the interpolation in a different format

            challenge.world.wait_for_tick()

            gps_route, route_description['trajectory'] = interpolate_trajectory(challenge.world,
                                                                                route_description['trajectory'])

            potential_scenarios_definitions, existent_triggers = parser.scan_route_for_scenarios(route_description,
                                                                                world_annotations)

            for trigger_id, possible_scenarios in potential_scenarios_definitions.items():

                print ("  For trigger ", trigger_id, " --  ", possible_scenarios[0]['trigger_position'])
                for scenario in possible_scenarios:
                    print ("      ", scenario['name'])

            challenge.cleanup(ego=True)


'''Ch12Ex2: Change your socket program so that it counts the number of characters it has received and
stops displaying any text after it has shown 3000 characters. The program should retrieve the entire document
and count the total number of characters and display the count of the number of characters at the end of the
document.

'''

import socket
urlstr = input('Please enter a URL like http://data.pr4e.org: ')
try:
    urlstr = urlstr.strip()
    hoststr = urlstr.split('/')
    host = hoststr[2]
    port = 80
    mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    mysock.connect((host, port))
except:
    print('The URL was incorrectly entered or does not exist. Please try again.')
    quit()
cmd = 'GET '+urlstr+' HTTP/1.1\r\nHost: '+host+'\r\n\r\n'
cmd = cmd.encode()
mysock.send(cmd)
char = 0
strprint = ''
while True:
    data = mysock.recv(512)
    if (len(data) < 1):
        break
    datastream = str(data.decode())
    for x in datastream:
        char +=1
        if char <= 3000:
            strprint = strprint+x
print(strprint)
print('Character count: ',char)
mysock.close()


"""
Consider two strings, ``s`` and ``t`` and a non-negative integer ``k``.

Check if the characters in ``t`` occur in ``s`` in an arbitrary order as a subsequence.
The distance between two matching characters from ``t`` in ``s`` must not be larger
than ``k``.

The distance between two characters is given as the number of characters in-between.
For example, the distance between the characters "a" and "b" in "a12345b" is 5.

Here are a couple of examples:

* ``s = "abbbc", t = "cab", k = 1``. The result is ``True``.
* ``s = "abbbbc", t = "cab", k = 1``. The result if ``False``. The distance between
  either "a" and "b" or "b" and "c" is larger than 1.
* ``s = "abc"``

"""

from typing import Counter, Set

from icontract import require, ensure

@require(lambda dist: dist >= 0)
@ensure(lambda src, chars, result: result is False if len(chars) > len(src) else True)
@ensure(
    lambda src, chars, result: (result is True)
    if (sorted(chars) in sorted(src))
    else True
)
@ensure(lambda src, chars, result: (result is True) if (chars in src) else True)
def matches(src: str, chars: str, dist: int) -> bool:
    return stretch(src, Counter(chars), len(src) - len(chars), dist)

def stretch(src: str, chars: Counter[str], initialdist: int, dist: int) -> bool:
    if sum(chars.values()) == 0:
        return True
    prefix = src[: initialdist + 1]
    if all(chars[ch] == 0 for ch in prefix):
        return False
    for idx, ch in enumerate(prefix):
        if chars[ch] <= 0:
            continue
        subch = chars.copy()
        subch[ch] -= 1
        if stretch(src[idx + 1 :], subch, dist, dist):
            return True
    return False