"""
DEDA Unit 5
Basic Statistics and Visualisation in Python
Authors: Junjie Hu and Isabell Fetzer, 20220924 WKH
"""

"""
Interactive Graphs
"""
"""
Plotly enables Python users to create beautiful interactive visualisations.
"""
# pip install yfinance
from datetime import datetime, timedelta
import pandas as pd
import plotly.graph_objects as go
import yfinance as yf
import numpy as np

CURRENCY = 'EUR'
def getData(crypto):
  # Define time window
    now = datetime.now()
    current_date = now.strftime("%Y-%m-%d")
    last_year_date = (now - timedelta(days=365)).strftime("%Y-%m-%d")
    start = pd.to_datetime(last_year_date)
    end = pd.to_datetime(current_date)
    data = yf.download(tickers=f'{crypto}-{CURRENCY}', start = start, end = end , interval = '1d')
    return data

# Call function to retrieve data on Bitcoin
btc_data = getData('BTC')
# eth_data = getData('ETH')
# Plot graph
fig = go.Figure(
        data = [
            go.Candlestick(
                x = btc_data.index,
                open = btc_data.Open,
                high = btc_data.High,
                low = btc_data.Low,
                close = btc_data.Close
            )
        ] )
fig.update_layout(
    title = f'Time Series with Range slider for BTC',
    xaxis_title = 'Date',
    yaxis_title = f'Price ({CURRENCY})',
    xaxis_rangeslider_visible = True
)
fig.update_yaxes(tickprefix=CURRENCY)
fig.show()

"""
Generating numbers in numpy array
"""

# Giving the seed of random number generator
np.random.seed(1)
# Generate a 20*20 matrix with uniformly distributed random integers between 0 to 50
A = np.random.randint(low=0, high=50, size=(20, 20))
print(A)

"""
Matrix Operations
"""
"""
NumPy allows for many matrix operations.The inalg.inv() computes the (multiplicative) inverse of a matrix. The dot() method returns the dot product of two arrays. Identity() returns the identity array and eye() returns a  two-dimensional  array with ones on the diagonal and zeros elsewhere.
"""
# Note: A here is the random matrix from above
A_inv = np.linalg.inv(A) # Inverse matrix
print(A_inv)
dot_result = np.dot(A, A_inv) # Matrix multiplication operation
print(dot_result)
idn_matrix = np.identity(20) # Generate a 20*20 identity matrix
print(idn_matrix)
# Using .allclose() function to evaluate two matrices are equal within tolerance
np.allclose(dot_result, np.eye(20))  # True

"""
Eigenvalues
"""
"""
The transpose() function from Numpy can be used to calculate the transpose of a matrix. The linalg.eig() method computes the eigenvalues of a squared array while diag() extracts a diagonal or construct a diagonal array.
"""
A = [[3, 1],[1, 1]] # [[3, 1],[1, 1]] would create a 2x2 matrix
A_eig = np.linalg.eig(A)
# Now the Jordan decomposition A = Gamma*Lambda*Gamma^T
E_val = A_eig[0]
Gamma = A_eig[1]
Lambda = np.diag(E_val)
# Check the result, you might get something within numerical eps
AA = np.dot( np.dot(Gamma, Lambda), np.transpose(Gamma) )
print( np.allclose(AA, A) )  # True
# Calculation of the square root of A
Lambda12 = np.sqrt(Lambda)
A12 = np.dot( np.dot(Gamma, Lambda12), np.transpose(Gamma) )

"""
Fourier Transformation
"""
from PIL import Image
from numpy.fft import fft,ifft
import numpy as np
# Open the image by using Python Imaging Library(PIL)
image_before=Image.open('berlin_view.jpg')
# Decoding and encoding image to float number
image_int=np.fromstring(image_before.tobytes(), dtype=np.int8)
# Processing Fourier transform
fft_transformed=fft(image_int)
# Filter the lower frequency, i.e. employ a high pass
fft_transformed=np.where(np.absolute(fft_transformed) < 9e4,0,fft_transformed)
# Inverse Fourier transform
fft_transformed=ifft(fft_transformed)
# Keep the real part
fft_transformed=np.int8(np.real(fft_transformed))
# Output the image
image_output=Image.frombytes(image_before.mode, image_before.size, fft_transformed)
image_output.show()

"""
Normal Distribution
"""
"""
To create an inert active plot for the probability and cumulative density function of the normal distribution we use the Plotly package.
"""
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
import plotly.express as px
from ipywidgets import interact, interactive, fixed, interact_manual
# Set up figure size of plot
plt.rcParams['figure.figsize'] = (10,8) # length and width
plt.rcParams['figure.dpi'] = 120 # general box size

"""
We define the general environment of our plot: size, colour and style.
"""
# Plot the cdf and pdf in one figure
def f(mu,sigma, colour):
  fig = plt.figure(figsize=(10, 8))
  fig.subplots_adjust( hspace = 0.3)
  fig.patch.set_facecolor('#eeefef')
  plt.style.use('classic')

  # upper plot: pdf
  plt.subplot(2, 1, 1)  # (rows, columns, which one)
  x_axis = np.linspace(mu - 3*sigma, mu + 3*sigma, 1000)
  plt.plot(x_axis, stats.norm.pdf(x_axis, mu, sigma), c= colour, linewidth= 2)
  plt.xlabel('X')
  plt.ylabel('pdf')
  plt.ylim(0,1)
  plt.xlim(-10,10)
  plt.title(f'Probability Density Function pdf for $\mu= {mu}$ and $\sigma= {round(sigma,2)}$', fontweight="bold")
  # lower plot: cdf
  plt.subplot(2, 1, 2)
  plt.plot(x_axis, stats.norm.cdf(x_axis, mu, sigma), c= colour, linewidth= 2)
  plt.xlabel('X')
  plt.ylabel('cdf')
  plt.ylim(0,1)
  plt.xlim(-10,10)
  plt.title(f'Cumulative Density Function cdf for $\mu= {mu}$ and $\sigma= {round(sigma,2)}$', fontweight="bold")

colours = ['red', 'green', 'blue']
interact(f, mu=(-10, 10,1), sigma=(0.5, 5, 0.5), colour = colours)
plt.savefig("filename.png", transparent=True)

"""
Kernel Density Estimation
"""

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
from sklearn.neighbors import KernelDensity
# Code reference: http://scikit-learn.org/stable/auto_examples/neighbors/plot_kde_1d.html
# Set up figure size of plot
plt.rcParams['figure.figsize'] = (10,8) # length and width
plt.rcParams['figure.dpi'] = 120 # general box size
N = 200
np.random.seed(1)
# Create 2 normal distributed data set
norm_data_1 = np.random.normal(0, 1, int(0.3 * N))
norm_data_2 = np.random.normal(5, 1, int(0.7 * N))
norm_data = np.concatenate((norm_data_1, norm_data_2))
# Create x axis range
X_plot = np.linspace(-5, 10, 1000)
# Create linear combination of 2 normal distributed random variable
norm_linear = (0.3 * norm(0, 1).pdf(X_plot) + 0.7 * norm(5, 1).pdf(X_plot))
fig, ax = plt.subplots()
# Plot the real distribution
ax.fill(X_plot, norm_linear, fc='black', alpha=0.2, label='Linear combination')
# Use 3 different kernels to estimate
for kernel in ['gaussian', 'tophat', 'epanechnikov']:
    # Initial an object to use kernel function to fit data, bandwidth will    affect the result
    kde = KernelDensity(kernel=kernel, bandwidth=0.5).fit(norm_data.reshape(-1, 1))
    # Evaluate the density model on the data
    log_dens = kde.score_samples(X_plot.reshape(-1, 1))
    ax.plot(X_plot, np.exp(log_dens), '-', label="kernel = '{0}'".format(kernel))
# Add text on the plot, position argument can be arbitrary
ax.text(6, 0.38, "N={0} points".format(N))
# Add a legend to the left outside of the plot
ax.legend(loc='upper left', bbox_to_anchor = (1,0.5))
# Plot the random points, squeeze them into narrow space
ax.plot(norm_data, -0.005 - 0.01 * np.random.random(norm_data.shape[0]), '+k')
# Set x-axis y-axis limit to adjust the figure
ax.set_xlim(-4, 9)
ax.set_ylim(-0.03, 0.4)
fig.savefig('kernel_estimation.png', dpi=300, transparent = True, bbox_inches='tight')
plt.show()

"""
Box-Muller Method on 2-dim normal distribution
"""
"""
Using the Box-Muller Method to generate 2-dim normally distributed variables.
"""
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(100)
# For mu = (0,0), covariance matrix Sigma = identity matrix
n = 500  # Number of random numbers
msize = 0.1  # determines the size of the plotted points
# a good size might be msize=5 for n=500 pts and msize=0.1 for n>50K
a = np.random.exponential(scale=1, size=n)
phi = np.random.uniform(low=0, high=2 * np.pi, size=n)
# Change to cartesian coordinates
x = a * np.cos(phi)
y = a * np.sin(phi)
plt.figure(figsize=(7, 7))
plt.plot(x, y,'.r', markersize=msize)
plt.savefig('2dimnormal.png', dpi = 300, transparent = True)

"""
For covariance matrix Sigma = A: Y = X/sqrt(Sigma) ~ N(0,I) => Y*sqrt(Sigma)
"""
# Calculate sqrt(A) with Jordan decomposition
A = [[3, 1], [1, 1]]
A_eig = np.linalg.eig(A)
E_val = A_eig[0]
Gamma = A_eig[1]
Lambda = np.diag(E_val)
np.sqrt(Lambda)
Lambda12 = np.sqrt(Lambda)
A12 = np.dot(np.dot(Gamma, Lambda12), np.transpose(Gamma))
# Solve with matrix multiplication
c = [x, y]
tfxy = np.dot(A12, c)
plt.figure(2, figsize=(6, 4))
plt.plot(tfxy[0], tfxy[1], 'ro', markersize=msize)


#!/usr/bin/python
#-*-coding:utf-8-*-
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# copyright 2016 WShuai, Inc.
# All Rights Reserved.

# @author: WShuai, WShuai, Inc.

from PIL import Image

class CompressHandler(object):
    def __init__(self, dest_width, dest_height, dest_quality):
        self.compress_width = dest_width
        self.compress_height = dest_height
        self.compress_quality = dest_quality
        return

    def compress_file(self, source_image_file, dest_image_file):
        im = Image.open(source_image_file)
        ori_w,ori_h = im.size
        widthRatio = heightRatio = None
        ratio = 1

        if (ori_w and ori_w > self.compress_width) or (ori_h and ori_h > self.compress_height):
            if self.compress_width and ori_w > self.compress_width:
                widthRatio = float(self.compress_width) / ori_w
            if self.compress_height and ori_h > self.compress_height:
                heightRatio = float(self.compress_height) / ori_h

            if widthRatio and heightRatio:
                if widthRatio < heightRatio:
                    ratio = widthRatio
                else:
                    ratio = heightRatio

            if widthRatio and not heightRatio:
                ratio = widthRatio
            if heightRatio and not widthRatio:
                ratio = heightRatio

            newWidth = int(ori_w * ratio)
            newHeight = int(ori_h * ratio)
        else:
            newWidth = ori_w
            newHeight = ori_h

        im.resize((newWidth,newHeight),Image.ANTIALIAS).save(dest_image_file, quality=self.compress_quality)
        return


import re

f = open(r"E:\ws\blade\ws\python_demo\xdfErrorLog\log.txt", 'r', encoding='GB2312')
content = f.readlines()

for line in content:
	line = re.sub(r'^\t', '', line)

print(content)

f.close()


from __future__ import print_function
import os
import numpy as np
from keras.models import load_model
import h5py
import math
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
import math
from tqdm import tqdm
## This code tests the model trained from scratch.

USE_TITANX=True

#############
### READING THE DATASET
# Define the external SSD where the dataset resides in
audio_prefix = 'seq3a'
if USE_TITANX:
    data_dir = '/home/zanoi/ZANOI/auditory_hallucinations_data/TopAngle_data/'
else:
    data_dir = '/Volumes/SAMSUNG_SSD_256GB/ADV_CV/data/'
file_name = data_dir + 'TopAngle_dataX_dataY.h5'

# Open the h5py file
with h5py.File(file_name, 'r') as hf:
    print("Reading data from file..")
    dataX = hf['dataX'][:]
    dataY = hf['dataY'][:]
print("dataX.shape:", dataX.shape)
print("dataY.shape:", dataY.shape)

print("np.max(dataY)", np.max(dataY))
print("np.min(dataY)", np.min(dataY))

# Flatten out the data
print ("Flatenning data")
(num_frames, frame_h, frame_w, channels) = dataX.shape  # (19045, 224, 224, 3)
dataX = np.reshape(dataX,(-1, frame_h, frame_w, channels))  # (8377,224,224,3)
audio_vector_dim = dataY.shape[1]
dataY = np.reshape(dataY,(-1, audio_vector_dim))  # (8377,18)
print("dataX.shape:", dataX.shape)
print("dataY.shape:", dataY.shape)

def loadModel():
    ### Query user for the desired model
    dir = '../trained_models'

    files = [fname for fname in os.listdir(dir)
             if fname.endswith('.h5')]

    print("Hello, I found %s models in the directory '%s':" % (str(len(files)), dir))
    for i, file in enumerate(files):
        print("[%d]" % i, file)
    model_idx = input("Select index [i] of desired model: ")
    #model_idx = 0  # I'm just testing one model right now
    model_name = files[int(model_idx)]
    model_dir = dir + "/" + model_name

    print("Loading weights from: " + model_dir)
    model = load_model(model_dir)
    print("Model loaded successfully")
    print (model.summary())
    return model

# Load model for prediction
model = loadModel()

# We will predict window lengths of 300
window_length = 300
num_windows = math.floor(num_frames/window_length)

for i in tqdm(range(num_windows)):
    pred_idx = i*window_length
    end_idx = pred_idx + window_length

    trainPredict = model.predict(dataX)
    #print ("trainPredict.shape", trainPredict.shape)
    #print ("dataY.shape", dataY.shape)
    trainScore = math.sqrt(mean_squared_error(dataY[pred_idx:end_idx,:], trainPredict[pred_idx:end_idx,:]))
    print('Train score: %.3f RMSE' % (trainScore))

    ##### PLOT RESULTS
    trainPlot = model.predict(dataX[pred_idx:end_idx,:])
    print(trainPlot.shape)
    plt.subplot(3,1,1)
    plt.imshow(trainPlot.T, aspect='auto')
    plt.title('CNN-LSTM prediction / Overfit on Top Angles only')
    plt.ylabel('Note bins')
    plt.xlabel('Time (frames)')
    plt.subplot(3,1,2)
    plt.title('Ground Truth')
    plt.ylabel('Note bins')
    plt.xlabel('Time (frames)')
    plt.annotate('RMSE: %.3f' % (trainScore), xy=(5, 5), xytext=(5, 33))
    plt.imshow(dataY[pred_idx:end_idx,:].T, aspect='auto')
    plt.subplot(3,1,3)
    plt.imshow(dataY[pred_idx:end_idx,:].T, aspect='auto')
    plt.colorbar()
    plt.tight_layout()
    plt.draw()
    plt.savefig('../model_tester/CNN_LSTM_scratch_figures/CNN_LSTM_scratch-pred_TopAngle' + str(i) + '.png')
    #plt.show()
    plt.close()

print ("--- {EVERYTHING COMPLETE HOMIEEEEEEEEE} ---")


from cmath import log, exp
import Logger
from FeatureExtractor import FeatureExtractor as fe

# Naive-Bayes Classifier with bag-of-words approach
class NBClassifier:
    def __init__(self, extractor: fe):
        self.train_set = extractor.train
        self.test_set = extractor.test
        self.extractor = extractor
        self.comment_count = 0
        self.spam_comment_count = 0
        self.train_ham_words = {}
        self.train_spam_words = {}
        self.ham_word_count = 0
        self.spam_word_count = 0
        self.P_spam = 0
        self.word_probability_log = {}

    def train_and_evaluate(self):
        # Counting comments
        for record in self.train_set:
            self.comment_count += 1
            if record[1] == '1':
                self.spam_comment_count += 1

        # Spam comment ratio
        self.P_spam = self.spam_comment_count / float(self.comment_count)

        # Splitting the training set into spam and ham categories
        for record in self.train_set:
            comment = str.lower(record[0]).split()
            comment_word_set = set()
            # Adding words to the comment's word set
            for word in comment:
                # Removing non-alphanumeric characters from the word
                word = fe.filter_non_alphanumeric(word)
                if word != '':
                    comment_word_set.add(word)

            # Adding the comment word set to the global word set
            for word in comment_word_set:
                if record[1] == '1':
                    self.train_spam_words[word] = self.train_spam_words.get(word, 0) + 1
                    self.spam_word_count += 1
                else:
                    self.train_ham_words[word] = self.train_ham_words.get(word, 0) + 1
                    self.ham_word_count += 1

        self.evaluate()

    # Calculates the conditional probability of given word's occurrence given the containing comment's class
    # Uses laplace-smoothing (add-one smoothing with the default value) to avoid 0 probability for unknown words
    # P(word|spam/notSpam)
    def conditional_word_probability(self, word: str, spam: bool, alpha: float = 1):
        if spam:
            return (self.train_spam_words.get(word, 0) + alpha) / float(
                self.spam_word_count + alpha * (self.spam_word_count + self.ham_word_count))
        return (self.train_ham_words.get(word, 0) + alpha) / float(
            self.ham_word_count + alpha * (self.spam_word_count + self.ham_word_count))

    # Returns with the logarithm of the given word's conditional probability given the containing comment's class
    def conditional_word_probability_log(self, word: str, spam: bool):
        # The values are cached in a dictionary, so we don't have to calculate them for the same word twice
        return self.word_probability_log.get((word, spam),
                                             log(self.conditional_word_probability(word, spam)))

    # Calculates the conditional probability of the comment's occurrence given the class
    # Based on the contained word's conditional probabilities
    # P(comment|spam/notSpam)
    def conditional_comment_probability(self, comment: str, spam: bool):
        probability = 0
        # Summing the log(probability) for each word
        for word in str.lower(comment).split():
            word = fe.filter_non_alphanumeric(word)
            probability += self.conditional_word_probability_log(word, spam)
        # Returning with standard probability value
        return exp(probability).real

    # Classifies the given comment
    def classify(self, comment: str):
        P_spam_comment = self.P_spam * self.conditional_comment_probability(comment, True)
        P_ham_comment = (1 - self.P_spam) * self.conditional_comment_probability(comment, False)
        return P_spam_comment > P_ham_comment

    # Evaluates the model with the test set, using accuracy as metric
    def evaluate(self):
        correct_count = 0
        for record in self.test_set:
            classification = self.classify(record[0])
            record_class = False
            if record[1] == '1':
                record_class = True

            if classification == record_class:
                correct_count += 1

        Logger.log('Naive-Bayes',
                   correct_count / float(len(self.test_set)) * 100)


# Python program for implementation of MergeSort
def mergeSort(arr):
    if len(arr)>1:
        #finding the middle element in an array
        mid=len(arr)//2
        # dividing the array elements into left and right halves
        L=arr[:mid]
        R=arr[mid:]
        mergeSort(L)
        mergeSort(R)

        i=j=k=0
        while i <len(L) and j < len(R):
            if L[i]<R[j]:
                arr[k]=L[i]
                i=i+1
                k=k+1
            else:
                arr[k]=R[j]
                j=j+1
                k=k+1
        while i<len(L):
            arr[k]=L[i]
            i=i+1
            k=k+1
        while j <len(R):
            arr[k]=R[j]
            j=j+1
            k=k+1

# Code to print the list
def printList(arr):

    #write your code here
    for i in range(len(arr)):
        print(arr[i],end=" ")
    print()
# driver code to test the above code
if __name__ == '__main__':
    arr = [12, 11, 13, 5, 6, 7]
    print ("Given array is", end="\n")
    printList(arr)
    mergeSort(arr)
    print("Sorted array is: ", end="\n")
    printList(arr)


import numpy as np
import matplotlib.pyplot as plt
import plotly.io as pio

class AIRL_ColorMap(object):
    @classmethod
    def get_2D_colormap(cls, data: np.ndarray, angle_offset=0, rank_based_coloring=True, center=None, numpy_max=None, max_l=None):

        if rank_based_coloring:
            arg_sort = np.argsort(data, axis=0)
            res = np.zeros_like(data)
            for i in range(np.size(data, 0)):
                for j in range(np.size(data, 1)):
                    res[arg_sort[i, j], j] = i
            data = res

        data = data.reshape((-1, 2))
        N = np.size(data, axis=0)
        minXY = np.min(data, axis=0)
        maxXY = np.max(data, axis=0)
        # center = (maxXY + minXY) / 2
        # print("center", np.median(data, axis=0), center)
        if center is None:
            center = np.median(data, axis=0)

        # center data
        data = data - center

        # "normalise data"
        # print("max", np.max(np.abs(data), axis=0), numpy_max)
        if numpy_max is None:
            data = data / np.max(np.abs(data), axis=0)
        else:
            data = data / numpy_max

        angle = np.arctan2(data[:,1], data[:,0]) + angle_offset
        angle = angle.reshape((-1, 1))
        print(np.min(angle))

        l = np.cbrt(np.sum(data ** 2, axis=1))
        if max_l is None:
            l = l / np.max(l)
        else:
            l = l / max_l
        # print("max_l", np.max(l), max_l)
        l = l.reshape((-1, 1))

        C = cls.get_color_function(angle)

        C = C * 0.7 + 0.3
        C = l * C + (1. - l) * 0.75
        return C

    @classmethod
    def get_color_function(cls, t: np.ndarray) -> np.ndarray:
        t = t.flatten()
        r = (np.sin(t + 1.3 * np.pi) / 2. + 0.5) * 0.8 + 0.1
        g = (np.sin(t) / 2.) * 0.6 + 0.6
        b = 1. - ((np.sin(t + 1.6 * np.pi) / 2.) + 0.5)

        return np.vstack((r, g, b)).T

if __name__ == '__main__':

    N = 2500
    data = np.random.rand(2 * N).reshape((-1, 2))
    print(AIRL_ColorMap.get_2D_colormap(data))
    plt.scatter(data[:,0], data[:,1], c=AIRL_ColorMap.get_2D_colormap(data))
    plt.show()

    colormap = AIRL_ColorMap.get_2D_colormap(data)
    trace = dict(type='scatter',
                 x=data[:,0],
                 y=data[:,1],
                 mode='markers',
                 marker=dict(color=[
                     f'rgb({colormap[i, 0]}, {colormap[i, 1]}, {colormap[i, 2]})' for i
                     in range(data.shape[0])], size=10)
                 )

    pio.show(trace)


import gzip

import numpy as np
import matplotlib.pyplot as plt

from mytorch.utils import get_file
from mytorch.transforms import Compose, Flatten, ToFloat, Normalize

class Dataset:
    def __init__(self, train=True, transform=None, target_transform=None):
        self.train = train
        self.transform = transform
        self.target_transform = target_transform
        if self.transform is None:
            self.transform = lambda x: x
        if self.target_transform is None:
            self.target_transform = lambda x: x

        self.data = None
        self.label = None
        self.prepare()

    def __getitem__(self, index):
        assert np.isscalar(index)
        if self.label is None:
            return self.transform(self.data[index]), None
        else:
            return self.transform(self.data[index]), \
                   self.target_transform(self.label[index])

    def prepare(self):
        pass

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

class Spiral(Dataset):
    def prepare(self):
        self.data, self.label = get_spiral(self.train)

def get_spiral(train=True):
    seed = 1984 if train else 2020
    np.random.seed(seed)

    num_data, num_class, input_dim = 100, 3, 2
    data_size = num_class * num_data
    x = np.zeros((data_size, input_dim), dtype=np.float32)
    t = np.zeros(data_size, dtype=np.int)

    for j in range(num_class):
        for i in range(num_data):
            rate = i / num_data
            radius = 1.0 * rate
            theta = j * 4.0 + 4.0 * rate + np.random.randn() * 0.2
            ix = num_data * j + i
            x[ix] = np.array([radius * np.sin(theta),
                              radius * np.cos(theta)]).flatten()
            t[ix] = j
    # Shuffle
    indices = np.random.permutation(num_data * num_class)
    x = x[indices]
    t = t[indices]
    return x, t

# =============================================================================
# MNIST-like dataset: MNIST / CIFAR /
# =============================================================================
class MNIST(Dataset):

    def __init__(self, train=True,
                 transform=Compose([Flatten(), ToFloat(),
                                     Normalize(0., 255.)]),
                 target_transform=None):
        super().__init__(train, transform, target_transform)

    def prepare(self):
        url = 'http://yann.lecun.com/exdb/mnist/'
        train_files = {'target': 'train-images-idx3-ubyte.gz',
                       'label': 'train-labels-idx1-ubyte.gz'}
        test_files = {'target': 't10k-images-idx3-ubyte.gz',
                      'label': 't10k-labels-idx1-ubyte.gz'}

        files = train_files if self.train else test_files
        data_path = get_file(url + files['target'])
        label_path = get_file(url + files['label'])

        self.data = self._load_data(data_path)
        self.label = self._load_label(label_path)

    def _load_label(self, filepath):
        with gzip.open(filepath, 'rb') as f:
            labels = np.frombuffer(f.read(), np.uint8, offset=8)
        return labels

    def _load_data(self, filepath):
        with gzip.open(filepath, 'rb') as f:
            data = np.frombuffer(f.read(), np.uint8, offset=16)
        data = data.reshape(-1, 1, 28, 28)
        return data

    def show(self, row=10, col=10):
        H, W = 28, 28
        img = np.zeros((H * row, W * col))
        for r in range(row):
            for c in range(col):
                img[r * H:(r + 1) * H, c * W:(c + 1) * W] = self.data[
                    np.random.randint(0, len(self.data) - 1)].reshape(H, W)
        plt.imshow(img, cmap='gray', interpolation='nearest')
        plt.axis('off')
        plt.show()

    @staticmethod
    def labels():
        return {0: '0', 1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6', 7: '7', 8: '8', 9: '9'}

class ImageNet(Dataset):
    def __init__(self):
        NotImplemented()

    @staticmethod
    def labels():
        url = 'https://gist.githubusercontent.com/yrevar/942d3a0ac09ec9e5eb3a/raw/238f720ff059c1f82f368259d1ca4ffa5dd8f9f5/imagenet1000_clsidx_to_labels.txt'
        path = get_file(url)
        with open(path, 'r') as f:
            labels = eval(f.read())
        return labels


import os, sys, glob, subprocess, apsw, itertools,argparse,multiprocessing,marshal,sqlite3,time
from multiprocessing import Process, Manager
from functools import partial
import pandas as pd
from sqlite3 import Error

def pos_dic():
    temp_dic = {'CDK7': ['ZINC000000101319', 'ZINC000077485665', 'ZINC000004192450', 'ZINC000033278339', 'ZINC000467398634','ZINC000095526640'], \
    'FLT3':['ZINC000000008662', 'ZINC000058014319', 'ZINC000005814210', 'ZINC000006448596','ZINC000012322830', 'ZINC000096221013', \
    'ZINC000100498577', 'ZINC000013126934', 'ZINC000000575002', 'ZINC000009245306','ZINC000013148149', 'ZINC000217901089', \
    'ZINC000000057655', 'ZINC000000210268', 'ZINC000023334750', 'ZINC000000248216','ZINC000001001156', 'ZINC000000435149', \
    'ZINC000006508912', 'ZINC000015843244', 'ZINC000021114661', 'ZINC000057964036','ZINC000004137279', 'ZINC000019702144', \
    'ZINC000079020838', 'ZINC000000111962', 'ZINC000078733090', 'ZINC000075144163','ZINC000097201518', 'ZINC000065272148', \
    'ZINC000072404457', 'ZINC000085391937', 'ZINC000001075920', 'ZINC000000060207','ZINC000075544404'], \
    'EGFR':['ZINC000001076595', 'ZINC000217912735', 'ZINC000085877625', 'ZINC000020827506', 'ZINC000217901089','ZINC000001068379', \
    'ZINC000009281884', 'ZINC000249700260', 'ZINC000009059860'], \
    'ERBB4':['ZINC000096396694', 'ZINC000032751234', 'ZINC000101747211', 'ZINC000089977150', 'ZINC000033260731','ZINC000072333497']}
    return temp_dic

def sql_connection_K():
    try :
        con = apsw.Connection('./Data/DB_Table/ZINC_K.db')
        return con
    except Error:
        print(Error)
        sys.exit(1)
        return

def Key_dic_load():
    didi = {}
    with open('./Data/Table/Result_0.dat','rb') as F:
        for i in marshal.load(F):
            didi[i[0]]= i[1]
    print("input Template Key Load OK")
    return didi

def find_zid(alp):
    cursorObj = conK.cursor()
    query = 'SELECT * FROM Key_Table WHERE Key=\'%s\''%(alp)
    cursorObj.execute(query)
    rows = cursorObj.fetchall()
    cursorObj.close()
    if not rows:
        pass
    else:
        ppp = unicode.encode(rows[0][1],'utf-8')
        p_set = set(ppp.split(','))
        print("Find Key %s ID set OK"%alp)
        return p_set

def SK_search():
    Ncpu = multiprocessing.cpu_count()
    mlist = Manager().list()
    b = 0
    with open('../../1D_Out_SCF/SCF_Out.list.Per.SMILES.txt','r') as F:
        flines = F.readlines()
        len_d = len(flines)//10
        div_list = list(divide_list(flines,len_d))
        for li in div_list:
            b += 1
            print('Checking ID START Step %s by multiprocessing'%str(b))
            func = partial(check_intersection,mlist=mlist)
            pool = multiprocessing.Pool(Ncpu-2)
            pool.map(func,li)
            pool.close()
            pool.join()
            print('Checking ID END Step %s'%str(b))
            with open('result_%s.txt'%str(b),'w') as W:
                for line2 in mlist:
                    W.write(line2 + '\n')
            mlist = Manager().list()

def check_intersection(line,mlist):
    tline = line.strip().split(',')
    zid = tline[2]
    if zid in tset:
        mlist.append(','.join(tline))
    else:
        pass

def dropop(df,i):
    df1 = df[df[0] == i]
    df2 = df1.drop_duplicates()
    return df2

def make_dir(a):
    try:
        print('Checking Directory')
        if os.path.exists(a):
            os.chdir(a)
            pass
        else:
            os.makedirs(a)
            os.chdir(a)
    except:
        print('DIR CREATE ERROR')

def divide_list(ls,n):
    for i in range(0,len(ls),n):
        yield ls[i:i+n]

def make_complete_file():
    print('Integration Files')
    df_list = []
    for i in glob.glob('result_*.txt'):
        df = pd.read_csv(i,header=None)
        df_list.append(df)
    df1 = pd.concat(df_list)
    df_list[:] = []
    for i in sorted(df1[0].drop_duplicates()):
        df2 = dropop(df1,i)
        df_list.append(df2)
    df_fin = pd.concat(df_list)
    df_fin.to_csv('SK_result.csv',index=False)
    print('Completely make SK_result.csv')

if __name__ == "__main__":
    print('Scaffold - Key Search START')
    conK = sql_connection_K()
    key_dic = Key_dic_load()

    Key_dics = Manager().dict()
    mlist = Manager().list()
    tset = set()
    df_list = []

    global conK,tset

    make_dir('./Data/SKR_Out/SK_result')
    print('N. Of Keys : ' + str(len(key_dic.keys())))
    for i in key_dic.keys():
        id_set = find_zid(i)
        tset = tset|id_set

    SK_search()
    make_complete_file()
    os.chdir('../../../')
    print('Scaffold - Key Search END')


from __future__ import division

import copy
import re

import numpy as np
import scipy.signal
from scipy import optimize
# import matplotlib.pyplot as plt

def my_rms(arr):
    #return np.sqrt(arr.dot(arr)/arr.size)
    return np.std(arr)

def extend_window(w, r):
    # Inputs:
    #   w: An array of 2 elements. Normally, this will be a window like [t1, t2]
    #   r: A float used as a ratio to extend w
    # Outputs: A rescaled version of w
    mp = 0.5*(w[1]+w[0])  # Midpoint
    new_len = (w[1]-w[0])*(1+r)  # Length of new window
    return [mp-new_len/2, mp+new_len/2]

def freq_filter(freqs, lower=None, upper=None):
    # Inputs:
    #   freqs: An array of frequency bins
    #   lower: The lower frequency to cut-off at
    #   upper: The upper frequency to cut-off at
    # Outputs: An array of indices where the the frequency in freqs is between lower and upper
    if lower is None and upper is None:
        return freqs
    if lower is None:
        return np.where([x <= upper for x in freqs])
    if upper is None:
        return np.where([x >= lower for x in freqs])
    return np.where([lower <= x <= upper for x in freqs])

def closest_index(arr, el):
    # Inputs:
    #   arr: A 1-dimensional array
    #   el: Any element
    # Outputs: The FIRST index of the item in arr that is closest to el.
    # Notes: Arr does NOT have to be sorted.
    return np.argmin(np.abs(arr-el))

def spectrum_sums(spectrum, fr, n, lowerf=None, upperf=None):
    # Inputs:
    #   spectrum: The output 2d spectrum from a spectogram
    #   fr: A list of frequency bins corresponding to the spectrum
    #   n: Number of bins
    #   lowerf: The lower frequency to cut-off at
    #   upperf: The upper frequency to cut-off at
    # Outputs: A compressed 1d array where each element is the sum of a bin from spectrum, only counting
    #          frequencies between lowerf and upperf
    out = []
    good_indices = freq_filter(fr, lowerf, upperf)
    for subn in range(n):
        out.append(np.trapz(spectrum[good_indices[0], subn], dx=np.mean(np.diff(fr))))
    return out

def rescale_window(w1, w2):
    # Inputs:
    #   w1: An array with 2 elements
    #   w2: An array with 2 elements
    # Outputs: A rescaled version of w2 so tha the endpoints of w2 match w1 but the number of elements remain the same
    y1, y2 = min(w1), max(w1)
    x1, x2 = min(w2), max(w2)
    if x1 == x2:
        return 0*w2
    a = (y1-y2)/(x1-x2)
    b = (x1*y2-x2*y1)/(x1-x2)
    return a*w2+b

def corr_signal(tau, dt, t0, n, fit_type=0, shift=10):
    # Inputs:
    #   tau: Time constant on exponential decay
    #   dt: Step size for the x-axis
    #   t0: Where the exponential signal will start. Not important when used with correlation
    #   N: Number of points requested
    #   fit_type: The type of signal to create. See corr_signal_type_templates.py for a better explanation.
    #               fit_type = 0 --> Exponential decay
    #               fit_type = 1 --> Constant 1 followed by exponential decay (continuous)
    #               fit_type = 2 --> Linear increase followed by exponential decay
    #               fit_type = 3 --> Log increase followed by exponential decay
    #               fit_type = 4 --> 0 value followed by an exponential decrease. Discontinuous.
    # Outputs:
    #   t: t-values for plotting
    #   y: y-values of our filter signal.
    # After careful analysis, we've determined that there reaches a point in the filtered piezo signal that
    # exhibits a sharp increase followed by an exponential decay. This function returns a brief exponential
    # decay function for use with convolution/correlation.
    shift = int(np.ceil(shift))
    t = np.linspace(t0, t0+dt*n, n)
    y = np.exp(-(t-t0)/tau)
    ycopy = copy.deepcopy(y)
    if fit_type == 0:
        pass
    elif fit_type == 1:
        for subn in range(len(y) - shift):
            y[subn+shift] = ycopy[subn]
        y[0:shift] = 1
    elif fit_type == 2:
        for subn in range(len(y) - shift):
            y[subn + shift] = ycopy[subn]
        y[0:shift] = (t[0:shift] - t0)/(shift*dt)
    elif fit_type == 3:
        for subn in range(len(y) - shift):
            y[subn + shift] = ycopy[subn]
            y[0:shift] = np.log((t[0:shift] + 1 - t0)) / np.log(shift*dt + 1)
    elif fit_type == 4:
        for subn in range(len(y) - shift):
            y[subn+shift] = ycopy[subn]
            y[0:shift] = 0
    return t, y

# Uncomment the below code if you want to test a new fit_type quickly to make sure the shape is what you want
# fit_type = 0
# t,y = corr_signal(1.5, 0.1, 0, 45, fit_type, 20) # <-- Uncomment to test different fit types
# plt.ioff()
# plt.plot(t,y)
# plt.show()
# 1/0 # <-- Just to stop the program here

def find_t0_from_corr(corrt, corry):
    # Inputs:
    #   corrt: Time-values of the correlation signal
    #   corry: Y-values of the correlation signal
    # Outputs: The time of the maximum in corry such that corrt is less than or equal to 0.
    n = np.where(corrt >= 0)
    corry[n] = 0
    return corrt[np.argmax(corry)]

def calculate_t0(piezo_waveform, piezo_timebase, led_on, tau,
                 lower=20000, upper=40000, piezo_fit_type=0):
    # Inputs:
    #   piezo_waveform: A piezo waveform, generally this should have the LED pulses subtracted
    #   piezo_timebase: The times of each element in the piezo_waveform
    #   tau: The time constant we are trying to fit to the exponential decay that occurs
    #        immediately after the bubble forms
    #   lower: The lower frequency threshold for cutting off the spectrogram
    #   upper: The upper frequency threshold for cutting off the spectrogram
    #   piezo_fit_type: The type of fit to use when trying to match the filtered piezo signal. Defaults to 0.
    #   view_plots: Boolean. If true, will display some plots for analysis.
    # Outputs: A dictionary of results for the Acoustic Analysis.
    try:
        timebase = piezo_timebase
        textent = [min(timebase), max(timebase)]
        dt = np.mean(np.diff(timebase))
        fr, bn, sp = scipy.signal.spectrogram(piezo_waveform, fs=1./dt, nfft=512, noverlap=450,
                                              mode="psd", window="hanning", nperseg=512)
        n = len(bn)
        sp_sums = spectrum_sums(sp, fr, n, lower, upper)
        sp_sums = scipy.signal.medfilt(sp_sums)
        rescaled_t = rescale_window(textent, bn)
        corr_dt = np.mean(np.diff(rescaled_t))
        corr_n = 1000
        corr_t, corr_y = corr_signal(tau, corr_dt, rescaled_t[0], corr_n, fit_type=piezo_fit_type)
        corr = np.correlate(sp_sums, corr_y, "same")
        corr_t = rescaled_t - 0.5 * corr_n * corr_dt
        test_t0 = find_t0_from_corr(corr_t, corr) # This is the t0 we begin to look backwards from
        # Establish a baseline for our lookback algorithm
        #   But first we take the log of the [integrated] spectrogram signal
        log_sp_sums = np.log(sp_sums)
        first_on = np.argmax(led_on)
        first_off = np.argmin(led_on[first_on:])
        second_on = np.argmax(led_on[first_off:])
        t_first_off = timebase[first_off]
        t_second_on = timebase[second_on]
        if not np.any(led_on):
            return np.nan
        rescaled_t_first_off_index = np.argmin(np.abs(rescaled_t - t_first_off))
        rescaled_t_second_on_index = np.argmin(np.abs(rescaled_t - t_second_on))

        baseline = np.average(log_sp_sums[rescaled_t_first_off_index+1:rescaled_t_second_on_index])

        baseline_rms = my_rms(log_sp_sums[rescaled_t_first_off_index+1:rescaled_t_second_on_index])

        test_t0_index = np.argmin(np.abs(rescaled_t - test_t0))
        rescaled_dt = np.mean(np.diff(rescaled_t))
        t_thresh = 100e-6
        n_lookback = int(np.floor(t_thresh/rescaled_dt))
        pts_lookbacked_sofar = 0

        while True:
            to_test = log_sp_sums[test_t0_index-n_lookback-pts_lookbacked_sofar:test_t0_index-pts_lookbacked_sofar]

            if np.all(to_test<(baseline+5*baseline_rms)):
                break
            pts_lookbacked_sofar += 1
            if test_t0_index-n_lookback-pts_lookbacked_sofar <= 0:
                pts_lookbacked_sofar = -1
                break
        if pts_lookbacked_sofar != -1:
            t0 = rescaled_t[test_t0_index-pts_lookbacked_sofar] + rescaled_dt/2
            # plt.ioff()
            # plt.plot(rescaled_t, log_sp_sums, color="cyan")
            # plt.axhline(baseline, color="b")
            # plt.axhline(baseline+5*baseline_rms, color="k")
            # plt.axvline(t0, color="g")
            # plt.axvline(test_t0, color="r")
            # plt.axvline(t_first_off, color="k")
            # plt.axvline(t_second_on, color="k")
            # plt.show()

        else:
            t0 = np.nan
        return t0
    except Exception as e:
        return np.nan

def BandPass2(yd, f_low, f_high):
    fband = np.array([f_low, f_high])
    b, a = scipy.signal.butter(2, fband / (2.5e6 / 2.0), btype='bandpass', output='ba')
    yd_f = scipy.signal.filtfilt(b, a, yd)
    return yd_f

def CalcPiezoE(yd, td, t_wins, f_bins, t0):
    piezoE = np.zeros((t_wins.shape[0],
                       f_bins.shape[0] - 1),
                      dtype=np.float64) + np.nan

    if np.isnan(t0):
        return piezoE

    dt = td[1] - td[0]
    t_wins = t_wins + t0
    t_wins_ix = np.intp(np.round((t_wins - td[0]) / dt))
    t_wins_ix[t_wins_ix < 0] = 0
    t_wins_ix[t_wins_ix > td.shape[0]] = td.shape[0]

    for i_win in range(t_wins.shape[0]):
        this_yd = yd[t_wins_ix[i_win][0]:t_wins_ix[i_win][1]]
        if len(this_yd) < 2:
            continue
        fft_amp = np.fft.rfft(this_yd)
        fft_pow = (np.abs(fft_amp) ** 2) * dt / len(this_yd)

        df = 1 / (dt * len(this_yd))
        fd = df * (np.arange(len(fft_amp), dtype=np.float64) + 1)
        f_bins_ix = np.intp(np.round((f_bins / df) - 1))
        f_bins_ix[f_bins_ix < 0] = 0
        f_bins_ix[f_bins_ix > len(fft_amp)] = len(fft_amp)

        fft_en = fft_pow * (fd ** 2)
        for i_f in range(len(f_bins) - 1):
            piezoE[i_win, i_f] = df *\
                np.sum(fft_en[f_bins_ix[i_f]:f_bins_ix[i_f + 1]])

    return piezoE

def AcousticAnalysis(ev, tau, piezo_fit_type=0,
                     f_high=np.float64(40e3), f_low=np.float64(6e3),
                     led_amp=np.float64(-0.1), led_tau=np.float64(2e-4),
                     bs_win=np.float64([-0.15, -0.12]),
                     t0_win=np.float64([-0.12, 0]),
                     meansamp=np.intp(1e4), notbs_win=np.float64(2e-4),
                     t_wins=np.float64([[[-2e-2, -1e-2],
                                        [-1e-3, 9e-3],
                                        [-2e-4, 4e-3]],
                                        [[-2e-2, -1e-2],
                                         [-1e-3, 9e-3],
                                         [-2e-4, 4e-3]]],                                ),
                     f_bins=np.float64([[1e2, 1e3, 1e4, 1e5],
                                       [1e2, 1e3, 1e4, 1e5]]),
                     corr_lowerf=20000, corr_upperf=40000):
    # Inputs:
    #   ev: Event data (from GetEvent)
    #   tau: The expected time-constant of the exponential decay from the filtered piezo signal
    #   piezo1_fit_type: See corr_signal_types.py
    #   piezo2_fit_type: See corr_signal_Types.py
    #   f_high: High frequency used for calculating Piezo Energy
    #   f_low: Low frequency used for calculating Piezo Energy
    #   led_amp: ??
    #   led_tau: Exponential decay time constant of led-fall off (?? I think)
    #   bs_win: ??
    #   t0_win: ??
    #   meansamp: ??
    #   t_wins: ??
    #   f_bins: Frequency bins for calculating Piezo energy
    #   corr_lowerf: Lower frequency for cutting off the spectrogram of the Piezo signal
    #   corr_upperf: Higher frequency for cutting off the spectrogram of the Piezo signal
    # Outputs: A dictionary of values for the various variables we are interested in. For a list, take a look
    #          at default_output right below.
    default_output = dict(bubble_t0=np.zeros(2)-1,  # seconds
                          peak_t0=np.zeros(2)-1,  # seconds
                          piezoE=np.zeros((2,
                                           t_wins.shape[0],
                                           f_bins.shape[1] - 1),
                                          dtype=np.float64) + np.nan,
                          piezo_list=np.zeros(2)-1,
                          piezo_freq_binedges=f_bins,
                          piezo_time_windows=t_wins,
                          led_switch_off_amp=np.zeros((2, 2)),
                          led_tau=np.zeros((2, 1)),
                          led_switch_on_amp=np.zeros((2, 2)))
    out = default_output
    try:
        if not ev["fastDAQ"]["loaded"]:
            return default_output
        piezoname_list = []
        piezo_list = []
        for k in list(ev["fastDAQ"].keys()):
            m = re.search("Piezo(\d+)", k)
            if m:
                piezoname_list.append(m.group(0))
                piezo_list.append(int(m.group(1)))
        piezo_list = np.int32(piezo_list)
        ixx = np.argsort(piezo_list)
        piezo_list = piezo_list[ixx] # Sorted piezo list
        piezoname_list = [piezoname_list[ix] for ix in ixx]
        out["piezo_list"] = piezo_list
        out["bubble_t0"] = np.zeros(piezo_list.shape, dtype=np.float64) + np.nan
        out["peak_t0"] = np.zeros(piezo_list.shape, dtype=np.float64) + np.nan
        out["piezoE"] = np.zeros((piezo_list.shape[0], t_wins.shape[0], f_bins.shape[0]-1),
                                 dtype=np.float64) + np.nan
        out["led_switch_on_amp"] = np.zeros(piezo_list.shape, dtype=np.float64) + np.nan
        out["led_switch_off_amp"] = np.zeros(piezo_list.shape, dtype=np.float64) + np.nan
        out["led_tau"] = np.zeros(piezo_list.shape, dtype=np.float64) + np.nan

        fastdaq_time = ev["fastDAQ"]["time"]
        dt = ev["fastDAQ"]["caldata"]["dt"][0]

        t0_win_ix = np.intp(np.round((t0_win - fastdaq_time[0]) / dt))
        bs_win_ix = np.intp(np.round((bs_win - fastdaq_time[0]) / dt))

        k_lowpass = f_high * 2 * np.pi * dt
        k_highpass = f_high * 2 * np.pi * dt

        if "CAMgate" in ev["fastDAQ"].keys():
            led_on = ev["fastDAQ"]["CAMgate"] < -0.5
            led_switch = np.diff(np.int8(led_on))
            led_switch_on = led_switch == 1
            led_switch_off = led_switch == -1
            led_switch_on_time = fastdaq_time[:-1][led_switch_on]
            led_switch_off_time = fastdaq_time[:-1][led_switch_off]
        else:
            led_switch_on_time = np.zeros(0)
            led_switch_off_time = np.zeros(0)
    except:
        return out

    for i_piezo in range(piezo_list.shape[0]):
        try:

            raw_piezo = ev["fastDAQ"][piezoname_list[i_piezo]]
            filtered_piezo = BandPass2(raw_piezo - np.mean(raw_piezo[:meansamp]), f_low, f_high)
            led_k = 1./led_tau
            piezo_led_on = np.sum((fastdaq_time[:, None] > led_switch_on_time) *
                                  np.exp(led_k * np.minimum(led_switch_on_time -
                                                            fastdaq_time[:, None],
                                                            np.float64([0]))),
                                  axis=1)
            piezo_led_off = np.sum((fastdaq_time[:, None] > led_switch_off_time) *
                                   np.exp(led_k * np.minimum(led_switch_off_time -
                                                             fastdaq_time[:, None],
                                                             np.float64([0]))),
                                   axis=1)

            filtered_piezo_led_on = BandPass2(piezo_led_on, f_low, f_high)
            filtered_piezo_led_off = BandPass2(piezo_led_off, f_low, f_high)
            fit_func = lambda x, on_amp, off_amp: on_amp*filtered_piezo_led_on[x] + off_amp*filtered_piezo_led_off[x]
            tdata = np.arange(0, bs_win_ix[1])
            ydata = filtered_piezo[tdata]
            x0_guess = np.array([1., -1.])
            led_amp, conv = optimize.curve_fit(fit_func, tdata, ydata, x0_guess)

            yd = filtered_piezo - (led_amp[0]*filtered_piezo_led_on + led_amp[1]*filtered_piezo_led_off)
            led_only = led_amp[0] * piezo_led_on + led_amp[1] * piezo_led_off
            out["led_switch_on_amp"][i_piezo] = led_amp[0]
            out["led_switch_off_amp"][i_piezo] = led_amp[1]
            out["led_tau"][i_piezo] = led_tau
            peak_index = np.argmax(np.abs(yd[t0_win_ix[0]:t0_win_ix[1]])) + t0_win_ix[0]
            out["peak_t0"][i_piezo] = fastdaq_time[peak_index]
            t0 = calculate_t0(piezo_waveform=raw_piezo - led_only, piezo_timebase=fastdaq_time,
                              tau=tau, led_on=led_on, lower=corr_lowerf, upper=corr_upperf,
                              piezo_fit_type=piezo_fit_type)
            if abs(t0) < 10e-6:
                t0 = np.nan # Then it's essentially 0
            if abs(t0-fastdaq_time[0]) < 10e-6:
                t0 = np.nan
            out["bubble_t0"][i_piezo] = t0
            t0_index = closest_index(fastdaq_time, t0)

            out["piezoE"][i_piezo,:,:] = CalcPiezoE(raw_piezo - led_only, fastdaq_time,
                                                   t_wins, f_bins, fastdaq_time[t0_index])

        except:
            continue
    return out

if __name__ == "__main__":
    import os
    from SBCcode.DataHandling.GetSBCEvent import GetEvent as GE
    events = {#"20171006_3": [38],
              "20171007_6": [9,10,11,12],
              }
    raw_dir = "/bluearc/storage/SBC-17-data/"
    tau = 0.0038163479219674467
    out = []
    for k,v in events.items():
        ev_path = os.path.join(raw_dir, k)
        for ev in v:
            ev_data = GE(ev_path, ev, "fastDAQ")
            out.append(AcousticAnalysis(ev_data, tau))

    for x in out:
        print(x["bubble_t0"])


import rospy
import cv2
import youtube_dl
import pafy
import numpy as np
import matplotlib.pyplot as plt
import time, math, random

cap=cv2.VideoCapture('./kmu_track.mkv')

ym_per_pix=30/720
xm_per_pix=3.7/720

def warpping(image):
    (h,w)=(image.shape[0],image.shape[1])

    source=np.float32([[w//2-30,h*0.53],[w//2+60,h*0.53],[w*0.3,h],[w,h]])
    des=np.float32([[0,0],[w-350,0],[400,h],[w-150,h]])

    transform_matrix=cv2.getPerspectiveTransform(source,des)
    minv=cv2.getPerspectiveTransform(des,source)
    _image=cv2.warpPerspective(image,transform_matrix,(w,h))

    return _image, minv

def color_filter(image):
    hls=cv2.cvtColor(image, cv2.COLOR_BGR2HLS)

    low=np.array([20,150,20])
    up=np.array([255,255,255])

    yellow_low=np.array([0,85,81])
    yellow_up=np.array([190,255,255])

    yellow_mask=cv2.inRange(hls,yellow_low,yellow_up)
    white_mask=cv2.inRange(hls,low,up)
    mask=cv2.bitwise_or(yellow_mask,white_mask)
    masked=cv2.bitwise_and(image,image,mask=mask)

    return masked

def roi(image):
    x=int(image.shape[1])
    y=int(image.shape[0])

    _shape=np.array([[int(0.1*x),int(y)],[int(0.1*x),int(0.1*y)],[int(0.4*x),int(y)],[int(0.7*x),int(y)],[int(0.7*x),int(0.1*y)],[int(0.9*x),int(0.1*y)],[int(0.9*x),int(y)],[int(0.2*x),int(y)]])
    mask=np.zeros_like(image)

    if len(image.shape)>2:
        channel_count=image.shape[2]
        ignore_mask_color=(255,)*channel_count
    else:
        ignore_mask_color=255
        pos,frame=process_image(img)
        steer_angle=0
        draw_steer(frame,steer_angle)

    cv2.fillPoly(mask,np.int32([_shape]),ignore_mask_color)
    masked_image=cv2.bitwise_and(image,mask)

    return masked_image

def plothistogram(image):
    histogram=np.sum(image[image.shape[0]//2:,:],axis=0)
    midpoint=np.int(histogram.shape[0]/2)
    leftbase=np.argmax(histogram[:midpoint])
    rightbase=np.argmax(histogram[midpoint:])+midpoint

    return leftbase, rightbase

def slide_window_search(binary_warped, left_current, right_current):
    out_img=np.dstack((binary_warped, binary_warped,binary_warped))

    nwindows=4
    window_height=np.int(binary_warped.shape[0]/nwindows)
    nonzero = binary_warped.nonzero()
    nonzero_y=np.array(nonzero[0])
    nonzero_x=np.array(nonzero[1])
    margin = 100
    minpix = 50
    left_lane=[]
    right_lane=[]
    color=[0,255,0]
    thickness = 2

    for w in range(nwindows):
        win_y_low = binary_warped.shape[0]-(w+1)*window_height
        win_y_high= binary_warped.shape[0]-w*window_height
        win_xleft_low=left_current-margin
        win_xleft_high=left_current+margin
        win_xright_low=right_current-margin
        win_xright_high=right_current+margin

        cv2.rectangle(out_img,(win_xleft_low,win_y_low),(win_xleft_high,win_y_high),color,thickness)
        cv2.rectangle(out_img,(win_xright_low,win_y_low),(win_xright_high,win_y_high),color,thickness)
        good_left = ((nonzero_y>=win_y_low)&(nonzero_y<win_y_high)&(nonzero_x>=win_xleft_low)&(nonzero_x<win_xleft_high)).nonzero()[0]
        good_right = ((nonzero_y>=win_y_low)&(nonzero_y<win_y_high)&(nonzero_x>=win_xright_low)&(nonzero_x<win_xright_high)).nonzero()[0]
        left_lane.append(good_left)
        right_lane.append(good_right)

        if len(good_left)>minpix:
            left_current = np.int(np.mean(nonzero_x[good_left]))
        if len(good_right)>minpix:
            right_current = np.int(np.mean(nonzero_x[good_right]))

    left_lane = np.concatenate(left_lane)
    right_lane=np.concatenate(right_lane)

    leftx=nonzero_x[left_lane]
    lefty=nonzero_y[left_lane]
    rightx=nonzero_x[right_lane]
    righty=nonzero_y[right_lane]

    left_fit=np.polyfit(lefty,leftx,2)
    right_fit=np.polyfit(righty,rightx,2)

    ploty=np.linspace(0,binary_warped.shape[0]-1,binary_warped.shape[0])
    left_fitx=left_fit[0]*ploty**2+left_fit[1]*ploty+left_fit[2]
    right_fitx=right_fit[0]*ploty**2+right_fit[1]*ploty+right_fit[2]

    ltx=np.trunc(left_fitx)
    rtx=np.trunc(right_fitx)

    out_img[nonzero_y[left_lane],nonzero_x[left_lane]]=[255,0,0]
    out_img[nonzero_y[right_lane],nonzero_x[right_lane]]=[0,0,255]

    #plt.imshow(out_img)
    #plt.plot(left_fitx,ploty,color='yellow')
    #plt.plot(right_fitx,ploty,color='yellow')
    #plt.xlim(0,1280)
    #plt.ylim(720,0)
    #plt.show()

    ret={'left_fitx':ltx,'right_fitx':rtx,'ploty':ploty}

    return ret

def draw_lane_lines(original_image,warped_image,Minv,draw_info):
    left_fitx=draw_info['left_fitx']
    right_fitx=draw_info['right_fitx']
    ploty=draw_info['ploty']

    warp_zero=np.zeros_like(warped_image).astype(np.uint8)
    color_warp=np.dstack((warp_zero,warp_zero,warp_zero))

    pts_left=np.array([np.transpose(np.vstack([left_fitx,ploty]))])
    pts_right=np.array([np.flipud(np.transpose(np.vstack([right_fitx,ploty])))])
    pts=np.hstack((pts_left, pts_right))

    mean_x=np.mean((left_fitx,right_fitx),axis=0)
    pts_mean=np.array([np.flipud(np.transpose(np.vstack([mean_x,ploty])))])

    cv2.fillPoly(color_warp,np.int_([pts]),(216,168,74))
    cv2.fillPoly(color_warp,np.int_([pts_mean]),(216,168,74))

    newwarp=cv2.warpPerspective(color_warp,Minv,(original_image.shape[1],original_image.shape[0]))
    result=cv2.addWeighted(original_image,1,newwarp,0.4,0)

    return pts_mean, result

while True:
    retval,img=cap.read()

    if not retval:
        break

    warpped_img,minverse=warpping(img)

    w_f_img=color_filter(warpped_img)

    w_f_r_img=roi(w_f_img)

    _gray=cv2.cvtColor(w_f_r_img,cv2.COLOR_BGR2GRAY)
    ret,thresh=cv2.threshold(_gray,160,255,cv2.THRESH_BINARY)

    leftbase,rightbase=plothistogram(thresh)

    draw_info=slide_window_search(thresh,leftbase,rightbase)

    meanPts, result=draw_lane_lines(img,thresh,minverse,draw_info)

    cv2.imshow("asdf", result)

    key=cv2.waitKey(25)

    if key==27:
        break

if cap.isOpened():
    cap.release()

cv2.destroyAllWindows()


import random
import board
import players
import time
import entities

print("\n----------------------------SNAKES AND LADDERS----------------------------")

# Object of Board
b = board.Board(entities.all_snakes, entities.all_ladders)

time.sleep(0.5)
# Object of Players
all_players = []
n = int(input("\nEnter the number of Players : "))
time.sleep(0.5)
for x in range(0, n):
    player = players.Player(input(f"\nEnter player {x + 1}: "))
    all_players.append(player)

# Turn function
def next_turn(pr):
    i = all_players.index(pr)
    if i == len(all_players) - 1:
        i = -1
    return all_players[i + 1]

def dice():
    dice_roll = random.randint(1, 6)
    print(f"Dice rolled is {dice_roll}")
    return dice_roll

def check_snake_bite(new_position, player_name):
    # Snake Bite Check.
    for i in entities.all_snakes:
        if i.head == new_position:
            new_position = i.tail
            print(f"Ohh! {player_name}, You got bit by Snake. You moved from {i.head} to {i.tail}")
            return True, new_position
    return False, new_position

def check_ladder_jump(new_position, player_name):
    # Ladder Jump Check.
    for i in entities.all_ladders:
        if i.tail == new_position:
            new_position = i.head
            print(f"Hurray! {player_name}, You got a ladder jump. You moved from {i.tail} to {i.head}")
            return True, new_position
    return False, new_position

def play(curr_player):
    time.sleep(0.5)
    print("-" * 80)
    print(f"It's {curr_player.name}'s turn. \nCurrent position : {curr_player.position}")
    input("\nPress 'ENTER' to Roll the Dice \n")
    print(f"\nRolling the dice...\n")
    time.sleep(0.5)

    d = dice()
    new_position = curr_player.position + d
    print(f"You moved from {curr_player.position} to {new_position}")
    bite, new_position = check_snake_bite(new_position, curr_player.name)
    jump, new_position = check_ladder_jump(new_position, curr_player.name)

    if new_position <= b.size:
        curr_player.position = new_position
    else:
        print("Oh!! You rolled more. Waste of a turn.")

    return d, bite, jump

def main():
    p = all_players[0]

    while True:
        dice_roll, bite, jump = play(p)
        if p.position == b.size:
            print(f"Congratulations!!. {p} won")
            break

        if dice_roll == 6 and bite is True:
            print("Unlucky. You rolled 6 but got bitten by the snake.")
            p = next_turn(p)

        elif dice_roll != 6:
            p = next_turn(p)
        else:
            print("Hurray. You got bonus move!!")

main()


'''
In this kata we want to convert a string into an integer. The strings simply
represent the numbers in words.

Examples:

    "one" => 1
    "twenty" => 20
    "two hundred forty-six" => 246
    "seven hundred eighty-three thousand nine hundred and nineteen" => 783919

Additional Notes:

    The minimum number is "zero" (inclusively)
    The maximum number, which must be supported is 1 million (inclusively)
    The "and" in e.g. "one hundred and twenty-four" is optional, in some cases
    it's present and in others it's not
    All tested numbers are valid, you don't need to validate them
'''
def parse_int(string):
    nums = {'zero': 0, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5,
            'six': 6, 'seven': 7, 'eight': 8, 'nine': 9, 'ten': 10, 'eleven': 11,
            'twelve': 12, 'thirteen': 13, 'fourteen': 14, 'fifteen': 15, 'sixteen':16,
            'seventeen': 17, 'eighteen': 18, 'nineteen': 19, 'twenty': 20,
            'thirty': 30, 'forty': 40, 'fifty': 50, 'sixty': 60, 'seventy': 70,
            'eighty': 80, 'ninety': 90 }
    power, result = 1, 0
    for num in reversed(string.split()):
        if num == 'and':
            continue

        if num == 'hundred':
            power *= 100
            continue

        if num == 'thousand':
            power = 1000
            continue

        if num == 'million':
            power = 1000000
            continue

        if '-' in num:
            be = num.split('-')
            result += (nums.get(be[0], 0) + nums.get(be[1], 0)) * power
        else:
            result += nums.get(num, 0) * power

    return result

if __name__ == '__main__':
    print(parse_int("one million twenty hundred forty-six"))


import cv2
import numpy as np

MIN_MATCH_COUNT = 10
sift = cv2.xfeatures2d.SIFT_create()

img = cv2.imread('ball.png',0)
kp1, des1 = sift.detectAndCompute(img,None)

cap = cv2.VideoCapture('tennis_balls.mov')

while cap.isOpened():
	ret, frame = cap.read()
	gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
	kp2, des2 = sift.detectAndCompute(gray, None)

	FLANN_INDEX_KDTREE = 0
	index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
	search_params = dict(checks=50)

	flann = cv2.FlannBasedMatcher(index_params, search_params)
	matches = flann.knnMatch(des1, des2, k=2)

	good = []
	for m,n in matches:
		if m.distance<0.7*n.distance:
			good.append(m)

	if len(good)>MIN_MATCH_COUNT:
		src_pts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1,1,2)
		dst_pts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1,1,2)

		M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
		matchesMask = mask.ravel().tolist()

		h, w = img.shape[:2]
		pts = np.float32([[0,0],[0,h-1],[w-1,h-1],[w-1,0]]).reshape(-1,1,2)
		dst = cv2.perspectiveTransform(pts, M)

		frame = cv2.polylines(frame,[np.int32(dst)],True, 255,3,cv2.LINE_AA)

	else:
		print ("Not enough matches are found - %d/%d"%(len(good, MIN_MATCH_COUNT)))
		matchesMask = None

	draw_params = dict(matchColor=(0,255,0),singlePointColor=None,matchesMask=matchesMask,flags=2)
	image = cv2.drawMatches(img,kp1,frame,kp2,good,None,**draw_params)
	cv2.imshow('plot', image)

	if cv2.waitKey(40)==27:
		break

cap.release()
cv2.destroyAllWindows()


import os

def pomocnik(slownik, item, iloscmyslnikow):
    for rzecz in range(len(slownik[item])):
        justowanie =' '* (len(iloscmyslnikow)-1) + '|' + '-' * (len(os.path.split(item)[1])-1)
        plik = slownik[item][rzecz]

        czy_plik = os.path.join(item, plik)
        if rzecz == len(slownik[item])-1 and os.path.isfile(os.path.join(czy_plik)):
            print('|',justowanie.replace('|','\\'), plik,sep = '')
        else:
            print('|',justowanie, plik,sep = '')
        if os.path.isdir(os.path.join(item,plik)):
            justowanie += '|'
            pomocnik(slownik, os.path.join(item,plik), justowanie)

def main(directory):
    slownik = {roots:dirs + files for (roots, dirs, files) in os.walk(directory)}

    for i in slownik.keys():
        slownik[i] = sorted(slownik[i])

    myslniki ='|'+'-'*(len(directory)-1)
    myslniki2 ='\\'+'-'*(len(directory)-1)

    print(directory)
    rzecz = slownik[directory]
    for item in range(len(rzecz)):
        if item == len(rzecz) -1 and os.path.isfile(os.path.join(directory, rzecz[item])):
            print(myslniki2, rzecz[item], sep='')
        else:
            print(myslniki, rzecz[item], sep='')
        sciezka = os.path.join(directory, rzecz[item])
        if os.path.isdir(sciezka):
            pomocnik(slownik, sciezka, myslniki)

if __name__ == '__main__':
    import sys
    main('./')


from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QGridLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QStackedWidget, QSplitter, QWidget

class StackedWidget(QWidget):
    def __init__(self, parent=None):
        super(StackedWidget, self).__init__(parent)

        self.splitter = QSplitter()

        self.stacked1 = QStackedWidget()
        self.stacked2 = QStackedWidget()
        self.nextBtn = QPushButton('Next')
        self.prevBtn = QPushButton('Previous')
        self.pageLabel = self.make_entry(QLineEdit('1'))
        self.pageLabel.setMaxLength(8)
        self.ofLabel = self.make_entry(QLabel('of'))
        self.totalLabel = self.make_entry(QLabel())

        self.nextBtn.clicked.connect(self.next_page)
        self.prevBtn.clicked.connect(self.previous_page)
        self.pageLabel.returnPressed.connect(self.change_page)

        self.set_label()

        self.splitter.addWidget(self.stacked1)
        self.splitter.addWidget(self.stacked2)

        box = QHBoxLayout()

        box.addWidget(self.prevBtn)
        box.addWidget(self.pageLabel)
        box.addWidget(self.ofLabel)
        box.addWidget(self.totalLabel)
        box.addWidget(self.nextBtn)

        grid = QGridLayout()

        grid.addLayout(box, 0, 0)
        grid.addWidget(self.splitter, 1, 0)

        self.setLayout(grid)

    def make_entry(self, widget):
        widget.setAlignment(Qt.AlignCenter)
        widget.setMaximumWidth(50)
        return widget

    def next_page(self):
        index = self.stacked1.currentIndex()
        if index < self.stacked1.count() - 1:
            self.stacked1.setCurrentIndex(index + 1)
            self.stacked2.setCurrentIndex(index + 1)
            self.pageLabel.setText(str((index + 1) + 1))

    def previous_page(self):
        index = self.stacked1.currentIndex()
        if index > 0:
            self.stacked1.setCurrentIndex(index - 1)
            self.stacked2.setCurrentIndex(index - 1)
            self.pageLabel.setText(str(index))

    def change_page(self):
        index = int(self.pageLabel.text())
        self.stacked1.setCurrentIndex(index - 1)
        self.stacked2.setCurrentIndex(index - 1)

    def set_label(self):
        count = self.stacked1.count()
        self.totalLabel.setText(str(count))

    def clear_stack(self):
        while self.stacked1.count() > 0:
            w = self.stacked1.widget(0)
            self.stacked1.removeWidget(w)
            q = self.stacked2.widget(0)
            self.stacked2.removeWidget(q)


import sys
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf

class Char():
    def __init__(self,screen):
        self.screen = screen
        self.image = pygame.image.load("images/68_avatar_middle.jpg")
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()
        self.rect.centerx = self.screen_rect.centerx
        self.rect.centery = self.screen_rect.centery
    def blitme(self):
        """在指定位置绘制飞船"""
        self.screen.blit(self.image,self.rect)

def run_game():
    # 初始化游戏并创建一个屏幕对象
    pygame.init()
    screen = pygame.display.set_mode((1200,800))
    pygame.display.set_caption("Test")
    char = Char(screen)

    # 设置背景色
    bg_color = (125,125,255)

    # 开始游戏的主循环
    while True:
        # 监视键盘和鼠标事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        # 每次循环是都重绘屏幕
        screen.fill(bg_color)
        char.blitme()

        # 让最近绘制的屏幕可见
        pygame.display.flip()

##def run_game():
##	# 初始化pygame、设置和屏幕对象
##	pygame.init()
##	ai_settings = Settings()
##	screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
##	pygame.display.set_caption("Alien Invasion")
##
##	# 创建一艘飞船
##	ship = Ship(screen)
##
##	# 开始游戏的主循环
##	while True:
##		# 监视键盘和鼠标事件
##		gf.check_events()
##		gf.update_screen(ai_settings,screen,ship)
##
run_game()


# 第三方的用于发送网络请求的一个模块,经常用于爬虫
import requests, json

response = requests.get('http://api.map.baidu.com/telematics/v3/weather?location=郑州市&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?').text
print(type(response), response)

res_result = json.loads(response)
print(type(res_result), res_result)
# 取error的值
error = res_result['error']
print("error:", error)
# 取status的值
status = res_result['status']
# 取data的值
data = res_result['date']
print(status)
# 取results的值,取出来的结果是一个列表,列表里面包含着一个字典.
results = res_result['results']
print(results)
# 从results这个列表中取出来这个字典
dict1 = results[0]
print(type(dict1), dict1)
currentCity = dict1['currentCity']
print(type(currentCity), currentCity)
pm_25 = dict1['pm25']
print(type(pm_25), pm_25)
# 从dict1这个字典当中,取出键是index的值,对应的是一个列表.
index = dict1['index']
print(type(index), index)

# 使用for循环遍历index这个列表
for dex in index:
    # 取出来的每一个dex都是一个字典
    # print(type(dex), dex)
    des = dex['des']
    tipt = dex['tipt']
    title = dex['title']
    zs = dex['zs']
    print(des, tipt, title, zs)

#解析天气预报
for weather_data in dict1['weather_data']:
    date = weather_data['date']
    weather = weather_data['weather']
    wind = weather_data['wind']
    temp = weather_data['temperature']
    print("%s   %s   %s   %s"%(data,weather,wind,temp))


"""
Interfaz gráfica
Yael Chavoya

La interfaz gráfica se divide en dos partes:
- La implementación del acomodo y formato de la interfaz se encuentra en este archivo
- La implementación de la funcionalidad de la interfaz se encuentra en gui.py
"""
from tkinter import font
from tkinter.scrolledtext import ScrolledText
from tkinter.ttk import Notebook
import tkinter as tk
from ui.scrollframe import VScrollFrame

# Longitud de los bloques que muestran texto, en caracteres (tk.Message)
char_width = 600

class MainWindow(tk.Frame):
    """
    Clase de la ventana principal. Implementa solamente el acomodo y formato de la interfaz
    """

    def __init__(self, title, compress_fn, decompress_fn):
        """
        Inicializa la ventana con el acomodo inicial

        :param title: El título de la ventana
        :param compress_fn: La función a llamar al presionar el botón de comprimir
        :param decompress_fn: La función a llamar al presionar el botón de descomprimir
        """
        tk.Frame.__init__(self)

        # Información básica de la ventana
        root = self.master
        root.title(title)
        root.geometry(f'{char_width + 150}x400')

        # Configurar la fuente a usar cuando se requiera hacer énfasis
        self.bold_font = font.Font(family='TkTextFont', size=10, weight='bold')

        # Configurar las pestañas como VScrollFrames

        tab_root = Notebook(root)

        tab_compress = VScrollFrame(tab_root)
        tab_decompress = VScrollFrame(tab_root)

        tab_root.add(tab_compress, text='Comprimir')
        tab_root.add(tab_decompress, text='Descomprimir')

        tab_root.pack(expand=1, fill=tk.BOTH)
        tab_cmp_root = tab_compress.interior
        tab_dcp_root = tab_decompress.interior

        # Asignar las acciones de los botones

        self.cmd_compress = compress_fn
        self.cmd_decompress = decompress_fn

        # Configurar el layout de cada pestaña

        self._setup_cmp(tab_cmp_root)
        self._setup_dcp(tab_dcp_root)

    @staticmethod
    def clipboard_copy(target: str):
        """
        Copia una cadena específica al portapapeles

        :param target: El texto a copiar
        """
        cb = tk.Tk()
        cb.withdraw()
        cb.clipboard_clear()
        cb.clipboard_append(target)
        cb.update()
        cb.destroy()

    def _setup_cmp(self, tab_cmp_root):
        """
        Configura la pestaña de comprimir

        :param tab_cmp_root: El Frame donde configurar los elementos
        """

        # =================================
        # Configurar la entrada del texto original

        row = 0

        (tk.Label(tab_cmp_root, text='Texto a comprimir:', font=self.bold_font)
            .grid(row=row, column=0, pady=10, padx=10, sticky=tk.W))

        row += 1

        self.original_entry = ScrolledText(tab_cmp_root, width=50, height=6, wrap=tk.WORD)
        self.original_entry.grid(row=row, column=0, columnspan=2, pady=10, padx=10, sticky=tk.W+tk.E)

        self.btn_1 = tk.Button(tab_cmp_root, text="Comprimir", command=self.cmd_compress)
        self.btn_1.grid(row=row, column=2, pady=10, padx=10, sticky=tk.N)

        # =================================
        # Configurar la salida del texto comprimido

        row += 1

        (tk.Label(tab_cmp_root, text='Texto comprimido en binario:', font=self.bold_font)
            .grid(row=row, column=0, columnspan=2, padx=10, sticky='sw'))

        row += 1

        self.strv_compressed = tk.StringVar()
        (tk.Message(tab_cmp_root, width=char_width, justify='left', textvariable=self.strv_compressed, relief='groove')
            .grid(row=row, pady=10, padx=10, column=0, columnspan=2, sticky='w'))

        tk.Button(tab_cmp_root, text="Copiar", command=self.btn_cp_cmp).grid(row=row, padx=10, column=2, sticky=tk.N)

        # =================================
        # Configurar la salida del código para descomprimir

        row += 1

        (tk.Label(tab_cmp_root, text='Código para descomprimir:', font=self.bold_font)
            .grid(row=row, column=0, columnspan=2, padx=10, sticky=tk.S+tk.W))

        row += 1

        self.strv_encoded = tk.StringVar()
        (tk.Message(tab_cmp_root, width=char_width, justify='left', textvariable=self.strv_encoded, relief='groove')
            .grid(row=row, pady=10, padx=10, column=0, columnspan=2, sticky=tk.W))

        tk.Button(tab_cmp_root, text="Copiar", command=self.btn_cp_cod).grid(row=row, padx=10, column=2, sticky=tk.N)

        # =================================
        # Configurar la salida de la razón de compresión

        row += 1

        self.strv_rate = tk.StringVar()
        (tk.Label(tab_cmp_root, textvariable=self.strv_rate, font=self.bold_font)
            .grid(row=row, column=0, columnspan=3, padx=10, sticky=tk.S+tk.W))

        # =================================
        # Configurar cuáles filas y columnas se expanden al cambiar el tamaño de la ventana

        tab_cmp_root.grid_columnconfigure(1, weight=1)
        tab_cmp_root.grid_rowconfigure(1, weight=1)
        tab_cmp_root.grid_rowconfigure(3, weight=1)
        tab_cmp_root.grid_rowconfigure(5, weight=1)

    def _setup_dcp(self, tab_dcp_root):
        """
        Configura la pestaña de descomprimir

        :param tab_dcp_root: El Frame donde configurar los elementos
        """

        # =================================
        # Configurar la entrada del texto a descomprimir

        row = 0

        (tk.Label(tab_dcp_root, text='Texto a descomprimir:', font=self.bold_font)
            .grid(row=row, column=0, pady=10, padx=10, sticky=tk.W))

        self.btn_2 = tk.Button(tab_dcp_root, text="Descomprimir", command=self.cmd_decompress)
        self.btn_2.grid(row=1, column=2, pady=10, padx=10, rowspan=4, sticky=tk.S)

        row += 1

        self.decompress_entry = ScrolledText(tab_dcp_root, width=50, height=6, wrap=tk.CHAR)
        self.decompress_entry.grid(row=row, column=0, columnspan=2, sticky=tk.W+tk.E)

        # =================================
        # Configurar la entrada del código para descomprimir

        row += 1

        (tk.Label(tab_dcp_root, text='Código para descomprimir:', font=self.bold_font)
            .grid(row=row, column=0, pady=10, padx=10, sticky=tk.W))

        row += 1

        self.code_entry = ScrolledText(tab_dcp_root, width=50, height=6, wrap=tk.CHAR)
        self.code_entry.grid(row=row, column=0, columnspan=2, sticky=tk.W+tk.E)

        # =================================
        # Configurar la salida del texto descomprimido

        row += 1

        (tk.Label(tab_dcp_root, text="Texto original:", font=self.bold_font)
            .grid(row=row, column=0, columnspan=2, padx=10, pady=0, sticky=tk.SW))

        row += 1

        self.strv_original2 = tk.StringVar()
        (tk.Message(tab_dcp_root, width=char_width, justify='left', textvariable=self.strv_original2, relief='groove')
            .grid(row=row, pady=10, padx=10, column=0, columnspan=2, sticky='w'))

        tk.Button(tab_dcp_root, text="Copiar", command=self.btn_cp_ori).grid(row=row, padx=10, column=2, sticky=tk.N)

        # =================================
        # Configurar cuáles filas y columnas se expanden al cambiar el tamaño de la ventana

        tab_dcp_root.grid_columnconfigure(1, weight=1)
        tab_dcp_root.grid_rowconfigure(row, weight=1)

    def btn_cp_cmp(self):
        """
        Copiar el texto comprimido al portapapeles
        """
        self.clipboard_copy(self.strv_compressed.get())

    def btn_cp_cod(self):
        """
        Copiar el código al portapapeles
        """
        self.clipboard_copy(self.strv_encoded.get())

    def btn_cp_ori(self):
        """
        Copiar el texto descomprimido al portapapeles
        """
        self.clipboard_copy(self.strv_original2.get())


#
# This module contains global constants that are shared between classes and functions.
# Author: Tay Joc Cing
# Date: 17 Apr 2015

APP_NAME = "Geoguru"
APP_AUTHOR = "Tay Joc Cing"
APP_VERSION = "0.53"

CMD_VERBOSE = 'verbose'
CMD_LOAD = 'load'
CMD_HI = 'hi'
CMD_QUIT = 'exit'
CMD_CR = ''
CMD_QUERY = 'select'

IM_GREETING = APP_NAME + " " + APP_VERSION + " by " + APP_AUTHOR
IM_PROMPT = ">"


import sys
import json as js
import datetime as dt
import numpy as np
import pandas as pd

def to_date(row):
    return str(row.month) + "/" + str(row.day) + "/20" + str(row.year)
def to_time(row):
    if row.pm == "AM" or row.hour == 12:
        return "{:02d}:{:02d}:{:02d}".format(row.hour, row.minute, row.second)
    else:
        return "{:02d}:{:02d}:{:02d}".format(row.hour + 12, row.minute, row.second)

def hashtag(file_):
    """Extract-Transform-Load the Tweet data."""
    days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    year = np.cumsum(days)

    df = pd.read_csv(file_, names=["month", "day", "year", "hour", "minute", "second", "pm",
                                   "handle", "likes", "retweets", "O", "E"])

    with open("./data/followers.json", "r") as fp:
        f = js.load(fp)
    for row in df[~df["O"]].itertuples():
        if row.handle.lower() in f:
            if row.E != "sciencepolicy":
                if "external" in f[row.handle.lower()]:
                    f[row.handle.lower()]["external"] += 1
                else:
                    f[row.handle.lower()]["external"] = 1
            else:
                if "cspc" in f[row.handle.lower()]:
                    f[row.handle.lower()]["cspc"] += 1
                else:
                    f[row.handle.lower()]["cspc"] = 1
        else:
            if row.E != "sciencepolicy":
                f[row.handle.lower()] = {"cspc": 1}
            else:
                f[row.handle.lower()] = {"external": 1}
    with open("./data/followers.json", "w") as fp:
        js.dump(f, fp, indent=1)

    df = df[df.O == True].copy()
    df["date"] = df[["month", "day", "year"]].apply(lambda row: to_date(row), axis=1)
    df["time"] = df[["hour", "minute", "second", "pm"]].apply(lambda row: to_time(row), axis=1)
    df["handle"] = df["handle"].map(lambda x: 1)
    df["Date"] = pd.to_datetime(df[["date", "time"]].apply(lambda row: row.date + "T" + row.time,
                                                          axis=1), utc=True)
    df["Date"] = [x.astimezone("america/toronto") for x in df["Date"]]
    df.drop(['date', 'time', 'month', 'day', 'year', 'hour', 'minute', 'second', 'pm', 'O', 'E'],
            axis = 1, inplace = True)
    df.set_index('Date', inplace=True)
    df.rename({"handle": "T", "likes": "F", "retweets": "RT"}, axis=1, inplace=True)
    df = df.resample("1D").sum()
    df.reset_index(inplace=True)
    df["Date"] = pd.to_datetime(["{}-{}-{}".format(x.year, x.month, x.day) for x in df["Date"]])

    DF = pd.read_csv("./data/hashtag.csv")
    DF["Date"] = pd.to_datetime(DF["Date"])
    DF = pd.concat((DF, df), ignore_index=True)
    DF.set_index("Date", inplace=True)
    DF.to_csv("./data/hashtag.csv")

def activity(file_):
    df = pd.read_csv(file_)
    df.drop(["engagement rate", "permalink clicks", "app opens", "app installs", "email tweet",
             "dial phone", "media engagements", "promoted impressions", "promoted engagements",
             "promoted engagement rate", "promoted retweets", "promoted replies", "promoted likes",
             "promoted user profile clicks", "promoted url clicks", "promoted hashtag clicks",
             "promoted detail expands", "promoted permalink clicks", "promoted app opens",
             "promoted app installs", "promoted follows", "promoted email tweet",
             "promoted dial phone", "promoted media views", "promoted media engagements"],
             axis = 1, inplace = True)
    df.rename(index = str, columns = {"Date": "Date", "Tweets published": "Tweets",
                                      "impressions": 'Impressions', "engagements": "Engagements",
                                      "retweets": "Retweets", "replies": "Replies",
                                      "likes": "Likes", "user profile clicks": "Profile",
                                      "url clicks": "URL", "hashtag clicks": "Hashtag",
                                      "detail expands": "Details", "follows": "Follows",
                                      "media views": "Media"}, inplace = True)
    df["Date"] = pd.to_datetime(df["Date"])
    for c in df.columns[1:]:
        df[c] = df[c].map(lambda x: int(x))

    DF = pd.read_csv("./data/activity.csv")
    DF["Date"] = pd.to_datetime(DF["Date"])
    DF = pd.concat((DF, df), ignore_index=True)
    DF.set_index("Date", inplace=True)
    DF.to_csv("./data/activity.csv")

if __name__ == "__main__":
    if sys.argv[1] == "hashtag":
        hashtag(sys.argv[2])
    elif sys.argv[1] == "activity":
        activity(sys.argv[2])
    else:
        raise ValueError("{} command not recognized".format(sys.argv[1]))


import tensorflow as tf
import model_helper as helper
from utils import misc_util as misc
import os
import time
import numpy as np
from PIL import Image

tf.logging.set_verbosity(tf.logging.INFO)

def get_model_creator(model_type):
    # from models.vgg16 import VGG16
    from models.inception_v3 import InceptionV3
    from models.vgg_16 import VGG16B
    from models.resnet_v1 import ResNetV1
    if model_type == "VGG_16":
        return VGG16B
    elif model_type == "InceptionV3":
        return InceptionV3
    elif model_type == "ResNetV1":
        return ResNetV1

def start_sess_and_load_model(infer_model, ckpt_dir):
    sess = tf.Session(
        graph=infer_model.graph, config=misc.get_config_proto())
    with infer_model.graph.as_default():
        loaded_infer_model, global_step = helper.create_or_load_model(
          infer_model.model, ckpt_dir, sess)
    return sess, loaded_infer_model, global_step

def infer(hparams, ckpt_dir, scope=None, target_session=""):
    output_dir = os.path.join(hparams.base_dir, "infer")
    if not misc.check_file_existence(output_dir):
        tf.gfile.MakeDirs(output_dir)
    model_creator = get_model_creator(hparams.model_type)
    infer_model = helper.create_infer_model(model_creator, hparams, scope)
    infer_sess, loaded_infer_model, global_step = start_sess_and_load_model(
        infer_model, ckpt_dir)
    tf.logging.info("Restore model from global step %d" % global_step)
    # Summary
    summary_name = "infer_summary"
    summary_path = os.path.join(output_dir, summary_name)
    if not tf.gfile.Exists(summary_path):
        tf.gfile.MakeDirs(summary_path)
    summary_writer = tf.summary.FileWriter(
        summary_path, infer_model.graph)
    infer_results = []
    tf.logging.info("Ready to infer")
    start_time = time.time()
    step = 0
    detect_results = list()
    dataset_dir = os.path.join(hparams.data_dir, "test")
    filenames = tf.gfile.ListDirectory(dataset_dir)
    for filename in filenames:
        im = Image.open(os.path.join(dataset_dir, filename))
        image_feed = np.expand_dims(np.asarray(im, dtype='float32'), axis=0)
        size_feed = np.asarray([im.size[0], im.size[1], 3], dtype='int32')
        tf.logging.info("Start infer step:%d" % step)
        step_result, detect_result = loaded_infer_model.infer(infer_sess, image_feed=image_feed, size_feed=size_feed)
        summary_writer.add_summary(step_result.summary, global_step)
        infer_results.append(step_result.bboxes)
        detect_results.append(detect_result)
        step += 1
    tf.logging.info("Finish infer <spend time:%d>" % (time.time() - start_time))
    misc.draw_detect_results(detect_results)


from toee import *
from utilities import *
from combat_standard_routines import *

def san_dialog( attachee, triggerer ):
	game.global_flags[70] = 1
	if (attachee.leader_get() != OBJ_HANDLE_NULL):
		triggerer.begin_dialog( attachee, 350 )			## otis in party
	elif (game.global_vars[903] == 32 and attachee.map != 5051 and attachee.map != 5056):
		triggerer.begin_dialog( attachee, 560 )			## have attacked 3 or more farm animals with otis in party and not in nulb exterior or nulb smithy
	elif ((triggerer.item_find(2202) != OBJ_HANDLE_NULL) or (triggerer.item_find(3008) != OBJ_HANDLE_NULL)):
		triggerer.begin_dialog( attachee, 330 )			## you have otis chainmail and-or longsword in inventory
	elif ((game.quests[32].state == qs_completed) and (game.global_flags[74] == 0)):
		triggerer.begin_dialog( attachee, 270 )			## have completed bribery for profit quest and have not revealed otis secret
	elif (game.quests[31].state == qs_completed):
		triggerer.begin_dialog( attachee, 200 )			## have completed a second trip for otis quest
	elif (game.global_flags[73] == 1):
		triggerer.begin_dialog( attachee, 120 )			## otis has been to toee with you
	elif (game.quests[63].state == qs_accepted):
		triggerer.begin_dialog( attachee, 700 )			## bribery for justice quest accepted
	else:
		triggerer.begin_dialog( attachee, 1 )			## none of the above
	return SKIP_DEFAULT

def san_dying( attachee, triggerer ):
	if should_modify_CR( attachee ):
		modify_CR( attachee, get_av_level() )
	attachee.float_line(12014,triggerer)
	if (attachee.leader_get() != OBJ_HANDLE_NULL):
		game.global_vars[29] = game.global_vars[29] + 1
	return RUN_DEFAULT

def san_enter_combat( attachee, triggerer ):
	if (triggerer.type == obj_t_pc):
		leader = attachee.leader_get()
		if (leader != OBJ_HANDLE_NULL):
			leader.follower_remove(attachee)
		elmo = find_npc_near( attachee, 8000)
		if (elmo != OBJ_HANDLE_NULL):
			attachee.float_line(380,triggerer)
			leader = elmo.leader_get()
			if (leader != OBJ_HANDLE_NULL):
				leader.follower_remove(elmo)
			elmo.attack(triggerer)
		if anyone( triggerer.group_list(), "has_follower", 8001 ):
			paida = find_npc_near( triggerer, 8001 )
			if (paida != OBJ_HANDLE_NULL):
				triggerer.follower_remove(paida)
				paida.float_line( 20000,triggerer )
				paida.runoff(paida.location-3)
		if anyone( triggerer.group_list(), "has_follower", 8015 ):
			meleny = find_npc_near( triggerer, 8015 )
			if (meleny != OBJ_HANDLE_NULL):
				triggerer.follower_remove(meleny)
				meleny.float_line( 20000,triggerer )
				meleny.attack(triggerer)
		if anyone( triggerer.group_list(), "has_follower", 8021 ):
			ydey = find_npc_near( triggerer, 8021 )
			if (ydey != OBJ_HANDLE_NULL):
				triggerer.follower_remove(ydey)
				ydey.float_line( 20000,triggerer )
				ydey.attack(triggerer)
		if anyone( triggerer.group_list(), "has_follower", 8022 ):
			murfles = find_npc_near( triggerer, 8022 )
			if (murfles != OBJ_HANDLE_NULL):
				triggerer.follower_remove(murfles)
				murfles.float_line( 20000,triggerer )
				murfles.attack(triggerer)
		if anyone( triggerer.group_list(), "has_follower", 8031 ):
			thrommel = find_npc_near( triggerer, 8031 )
			if (thrommel != OBJ_HANDLE_NULL):
				triggerer.follower_remove(thrommel)
				thrommel.float_line( 20000,triggerer )
				thrommel.attack(triggerer)
		if anyone( triggerer.group_list(), "has_follower", 8039 ):
			taki = find_npc_near( triggerer, 8039 )
			if (taki != OBJ_HANDLE_NULL):
				triggerer.follower_remove(taki)
				taki.float_line( 20000,triggerer )
				taki.attack(triggerer)
		if anyone( triggerer.group_list(), "has_follower", 8054 ):
			burne = find_npc_near( triggerer, 8054 )
			if (burne != OBJ_HANDLE_NULL):
				triggerer.follower_remove(burne)
				burne.float_line( 20000,triggerer )
				burne.attack(triggerer)
		if anyone( triggerer.group_list(), "has_follower", 8060 ):
			morgan = find_npc_near( triggerer, 8060 )
			if (morgan != OBJ_HANDLE_NULL):
				triggerer.follower_remove(morgan)
				morgan.float_line( 20000,triggerer )
				morgan.attack(triggerer)
		if anyone( triggerer.group_list(), "has_follower", 8069 ):
			pishella = find_npc_near( triggerer, 8069 )
			if (pishella != OBJ_HANDLE_NULL):
				triggerer.follower_remove(pishella)
				pishella.float_line( 20000,triggerer )
				pishella.attack(triggerer)
		if anyone( triggerer.group_list(), "has_follower", 8071 ):
			rufus = find_npc_near( triggerer, 8071 )
			if (rufus != OBJ_HANDLE_NULL):
				triggerer.follower_remove(rufus)
				rufus.float_line( 20000,triggerer )
				rufus.attack(triggerer)
		if anyone( triggerer.group_list(), "has_follower", 8072 ):
			spugnoir = find_npc_near( triggerer, 8072 )
			if (spugnoir != OBJ_HANDLE_NULL):
				triggerer.follower_remove(spugnoir)
				spugnoir.float_line( 20000,triggerer )
				spugnoir.attack(triggerer)
		if anyone( triggerer.group_list(), "has_follower", 8714 ):
			holly = find_npc_near( triggerer, 8714 )
			if (holly != OBJ_HANDLE_NULL):
				triggerer.follower_remove(holly)
				holly.float_line( 20000,triggerer )
				holly.attack(triggerer)
		if anyone( triggerer.group_list(), "has_follower", 8730 ):
			ronald = find_npc_near( triggerer, 8730 )
			if (ronald != OBJ_HANDLE_NULL):
				triggerer.follower_remove(ronald)
				ronald.float_line( 20000,triggerer )
				ronald.attack(triggerer)
	return RUN_DEFAULT

def san_heartbeat( attachee, triggerer ):
	if (not game.combat_is_active()):
		if (game.global_vars[903] >= 3):
			if (attachee != OBJ_HANDLE_NULL):
				leader = attachee.leader_get()
				if (leader != OBJ_HANDLE_NULL):
					leader.follower_remove(attachee)
					attachee.float_line(22000,triggerer)
		if (game.global_flags[362] == 0):
			for obj in game.obj_list_vicinity(attachee.location,OLC_PC):
				if (is_safe_to_talk(attachee,obj)):
					if ((obj.item_find(2202) != OBJ_HANDLE_NULL) or (obj.item_find(3008) != OBJ_HANDLE_NULL)):
						obj.begin_dialog(attachee,330)
						game.global_flags[362] = 1
						return RUN_DEFAULT
		if (game.global_flags[72] == 0):
			for obj in game.obj_list_vicinity(attachee.location,OLC_NPC):
				if (obj.name == 8000):
					if (is_safe_to_talk(attachee,obj)):
						leader = obj.leader_get()
						if (leader == OBJ_HANDLE_NULL):
							leader = attachee.leader_get()
						if (leader != OBJ_HANDLE_NULL):
							leader.begin_dialog(attachee,400)
							return RUN_DEFAULT
		if (game.global_flags[366] == 1):
			leader = attachee.leader_get()
			if (leader != OBJ_HANDLE_NULL):
				attachee.turn_towards(leader)
				attachee.float_line(12023,leader)
				leader.follower_remove(attachee)
				attachee.attack(leader)
				game.global_flags[366] = 0
				return RUN_DEFAULT
		if (game.global_flags[367] == 1):
			leader = attachee.leader_get()
			if (leader != OBJ_HANDLE_NULL):
				attachee.turn_towards(leader)
				attachee.float_line(10014,leader)
				leader.follower_remove(attachee)
				game.global_flags[367] = 0
				return RUN_DEFAULT
	return RUN_DEFAULT

def san_join( attachee, triggerer ):
	if (attachee.map == 5051):
		for chest in game.obj_list_vicinity(attachee.location,OLC_CONTAINER):
			if (chest.name == 1202):
				chest.item_transfer_to( attachee, 2202 )
				chest.item_transfer_to( attachee, 3008 )
				chest.item_transfer_to( attachee, 12038 )
				chest.item_transfer_to( attachee, 12040 )
				reg_warhammer = attachee.item_find(4077)
				if (reg_warhammer != OBJ_HANDLE_NULL):
					reg_warhammer.destroy()
				reg_chainmail = attachee.item_find(6019)
				if (reg_chainmail != OBJ_HANDLE_NULL):
					reg_chainmail.destroy()
				attachee.item_wield_best_all()
		mag_longsword = attachee.item_find(2202)
		if mag_longsword != OBJ_HANDLE_NULL:
			mag_longsword.item_flag_set(OIF_NO_TRANSFER)
		mag_chainmail = attachee.item_find(3008)
		if mag_chainmail != OBJ_HANDLE_NULL:
			mag_chainmail.item_flag_set(OIF_NO_TRANSFER)
		blu_sapph = attachee.item_find(12038)
		blu_sapph.item_flag_set(OIF_NO_TRANSFER)
		amber = attachee.item_find(12040)
		amber.item_flag_set(OIF_NO_TRANSFER)
	else:
		itemA = attachee.item_find(4077)
		if (itemA != OBJ_HANDLE_NULL):
			itemA.destroy()
		itemB = attachee.item_find(6019)
		if (itemB != OBJ_HANDLE_NULL):
			itemB.destroy()
		mag_sword = create_item_in_inventory( 4122, attachee )
		mag_sword.item_flag_set(OIF_NO_TRANSFER)
		mag_armor = create_item_in_inventory( 6102, attachee )
		mag_armor.item_flag_set(OIF_NO_TRANSFER)
		blu_sapp1 = create_item_in_inventory( 12038, attachee )
		blu_sapp1.item_flag_set(OIF_NO_TRANSFER)
		blu_sapp2 = create_item_in_inventory( 12038, attachee )
		blu_sapp2.item_flag_set(OIF_NO_TRANSFER)
		amber1 = create_item_in_inventory( 12040, attachee )
		amber1.item_flag_set(OIF_NO_TRANSFER)
		amber2 = create_item_in_inventory( 12040, attachee )
		amber2.item_flag_set(OIF_NO_TRANSFER)
		amber3 = create_item_in_inventory( 12040, attachee )
		amber3.item_flag_set(OIF_NO_TRANSFER)
		amber4 = create_item_in_inventory( 12040, attachee )
		amber4.item_flag_set(OIF_NO_TRANSFER)
		amber5 = create_item_in_inventory( 12040, attachee )
		amber5.item_flag_set(OIF_NO_TRANSFER)
		attachee.item_wield_best_all()
	return RUN_DEFAULT

def san_disband( attachee, triggerer ):
	for obj in triggerer.group_list():
		if (obj.name == 8021):
			triggerer.follower_remove(obj)
		if (obj.name == 8022):
			triggerer.follower_remove(obj)
	for pc in game.party:
		attachee.ai_shitlist_remove( pc )
		attachee.reaction_set( pc, 50 )
	return RUN_DEFAULT

def san_new_map( attachee, triggerer ):
	if ((attachee.map == 5066) or (attachee.map == 5067) or (attachee.map == 5105) or (attachee.map == 5079) or (attachee.map == 5080)):
		game.global_flags[73] = 1
		if (game.quests[31].state == qs_accepted):
			game.quests[31].state = qs_completed
	elif ((attachee.map == 5062) or (attachee.map == 5112)):
		game.global_flags[73] = 1
		leader = attachee.leader_get()
		if (leader != OBJ_HANDLE_NULL):
			if ((leader.stat_level_get(stat_alignment) == LAWFUL_EVIL) or (leader.stat_level_get(stat_alignment) == CHAOTIC_EVIL) or (leader.stat_level_get(stat_alignment) == NEUTRAL_EVIL)):
				percent = group_percent_hp(leader)
				if ((percent < 30) or (game.global_flags[74] == 1)):
					game.global_flags[366] = 1
	elif (attachee.map == 5051):
		if (((game.global_flags[73] == 1) and (game.quests[31].state == qs_unknown)) or (game.quests[31].state == qs_completed)):
			leader = attachee.leader_get()
			if (leader != OBJ_HANDLE_NULL):
				game.global_flags[367] = 1
	return RUN_DEFAULT

def make_elmo_talk( attachee, triggerer, line):
	npc = find_npc_near(attachee,8000)
	if (npc != OBJ_HANDLE_NULL):
		triggerer.begin_dialog(npc,line)
		npc.turn_towards(attachee)
		attachee.turn_towards(npc)
	else:
		triggerer.begin_dialog(attachee,410)
	return SKIP_DEFAULT

def make_saduj_talk( attachee, triggerer, line):
	npc = find_npc_near(attachee,14689)
	if (npc != OBJ_HANDLE_NULL):
		triggerer.begin_dialog(npc,line)
		npc.turn_towards(attachee)
		attachee.turn_towards(npc)
	return SKIP_DEFAULT

def talk_to_screng( attachee, triggerer, line):
	npc = find_npc_near(attachee,8021)
	if (npc != OBJ_HANDLE_NULL):
		triggerer.begin_dialog(npc,line)
		npc.turn_towards(attachee)
		attachee.turn_towards(npc)
	else:
		triggerer.begin_dialog(attachee,470)
	return SKIP_DEFAULT

def make_lila_talk( attachee, triggerer, line):
	npc = find_npc_near(attachee,14001)
	if (npc != OBJ_HANDLE_NULL):
		triggerer.begin_dialog(npc,line)
		npc.turn_towards(attachee)
		attachee.turn_towards(npc)
	else:
		triggerer.begin_dialog(attachee,610)
	return SKIP_DEFAULT

def switch_to_thrommel( attachee, triggerer):
	npc = find_npc_near(attachee,8031)
	if (npc != OBJ_HANDLE_NULL):
		triggerer.begin_dialog(npc,40)
		npc.turn_towards(attachee)
		attachee.turn_towards(npc)
	else:
		triggerer.begin_dialog(attachee,510)
	return SKIP_DEFAULT

def chain_it( attachee, triggerer ):
	itemA = attachee.item_find(2202)
	if (itemA != OBJ_HANDLE_NULL):
		itemA.item_flag_unset(OIF_NO_TRANSFER)
	itemB = attachee.item_find(3008)
	if (itemB != OBJ_HANDLE_NULL):
		itemB.item_flag_unset(OIF_NO_TRANSFER)
	return


import cv2
import numpy as np
from pynput.mouse import Button, Controller
import wx

mouse = Controller()
app = wx.App(False)
(sx,sy) = (1980,1080)
(camx,camy) = (600,800)

cap = cv2.VideoCapture(0)
cap.set(3,camx)
cap.set(4,camy)

lowerbound = np.array([33,80,40])
upperbound = np.array([102,255,255])
kernelopen = np.ones((5,5))
kernelclose = np.ones((20,20))
#font = cv2.cv.InitFont(cv2.cv.CV_FONT_HERSHEY_SIMPLEX,2,0.5,0,3,1)

mlocold=np.array([0,0])
mouseloc  = np.array([0,0])
dampingfactor = 2
pinchflag = 0

while True:
    _ , img = cap.read()

    img = cv2.resize(img,(600,800))

    imghsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)

    mask = cv2.inRange(imghsv,lowerbound,upperbound)

    maskopen = cv2.morphologyEx(mask,cv2.MORPH_OPEN,kernelopen)
    maskclose = cv2.morphologyEx(maskopen,cv2.MORPH_CLOSE,kernelclose)

    maskfinal = maskclose

    cont,h = cv2.findContours(maskfinal.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

    #cv2.drawContours(img,cont,-1,(255,0,0),2)

    # for Drwaing the boxes around the color segmented image

    #for i in range(len(cont)):
     #   x,y,w,h = cv2.boundingRect(cont[i])
      #  cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
        #cv2.cv.PutText(cv2.cv.fromarray(img),str(i+1),(x,y+h),)

    if len(cont) == 2:
        if pinchflag==1:
            pinchflag=0
            mouse.release(Button.left)

        x1,y1,w1,h1 = cv2.boundingRect(cont[0])
        x2,y2,w2,h2 = cv2.boundingRect(cont[1])
        cv2.rectangle(img,(x1,y1),(x1+w1,y1+h1),(250,0,0),2)
        cv2.rectangle(img,(x2,y2),(x2+w2,y2+h2),(250,0,0),2)

        cx1 = int(x1+w1/2)
        cy1 = int(y1+h1/2)

        cx2 = int(x2+w2/2)
        cy2 = int(y2+h2/2)

        cx = int((cx1+cx2)/2)
        cy = int((cy1+cy2)/2)

        cv2.line(img,(cx1,cy1),(cx2,cy2),(255,0,0),2)
        cv2.circle(img,(cx,cy),2,(0,0,255),2)

        mouseloc = mlocold + ((cx,cy)-mlocold)//dampingfactor

        mouse.position = (sx-(mouseloc[0]*sx//camx),sy-(mouseloc[1]*sy//camy))

        #mouse.release(Button.left)
        while mouse.position != (sx-(mouseloc[0]*sx//camx),sy-(mouseloc[1]*sy//camy)):
            pass

        mlocold = mouseloc

    elif (len(cont)==1):
        if pinchflag == 0:
            pinchflag = 1
            mouse.press(Button.left)

        x,y,w,h = cv2.boundingRect(cont[0])
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)

        cx = int((x+w)/2)
        cy = int((y+h)/2)

        #cv2.circle(img,(cx,cy),(w+h)//4,(0,0,255),2)
        mouseloc = mlocold + ((cx,cy)-mlocold)//dampingfactor

        mouse.position = (sx-(mouseloc[0]*sx//camx),sy-(mouseloc[1]*sy//camy))

        #mouse.release(Button.left)
        while mouse.position != (sx-(mouseloc[0]*sx//camx),sy-(mouseloc[1]*sy//camy)):

            pass

        mlocold = mouseloc

    #cv2.imshow("msakclose",maskclose)
    #cv2.imshow("maskopen",maskopen)
    #cv2.imshow("mask",mask)
    cv2.imshow("original",img)
    cv2.waitKey(10)