import os
import matplotlib.pyplot as plt
import glob
import numpy as np
import pandas as pd
import tensorflow as tf
from keras.layers import Dense, Dropout, Reshape, Conv1D, BatchNormalization, Activation, AveragePooling1D, GlobalAveragePooling1D, Lambda, Input, Concatenate, Add, UpSampling1D, Multiply
from keras.models import Model
from keras.objectives import mean_squared_error
from keras import backend as K
from keras.losses import binary_crossentropy, categorical_crossentropy
from keras.callbacks import ModelCheckpoint, EarlyStopping, TensorBoard, ReduceLROnPlateau,LearningRateScheduler
from keras.initializers import random_normal
from keras.optimizers import Adam, RMSprop, SGD
from keras.callbacks import Callback

from sklearn.metrics import cohen_kappa_score, f1_score
from sklearn.model_selection import KFold, train_test_split

df_train = pd.read_csv("data/train.csv")
df_test = pd.read_csv("data/test.csv")

# I don't use "time" feature
train_input = df_train["signal"].values.reshape(-1,4000,1)#number_of_data:1250 x time_step:4000
train_input_mean = train_input.mean()
train_input_sigma = train_input.std()
train_input = (train_input-train_input_mean)/train_input_sigma
test_input = df_test["signal"].values.reshape(-1,10000,1)#
test_input = (test_input-train_input_mean)/train_input_sigma

#train_target = df_train["open_channels"].values.reshape(-1,4000,1)#regression
train_target = pd.get_dummies(df_train["open_channels"]).values.reshape(-1,4000,11)#classification

idx = np.arange(train_input.shape[0])
train_idx, val_idx = train_test_split(idx, random_state = 111,test_size = 0.2)

val_input = train_input[val_idx]
train_input = train_input[train_idx]
val_target = train_target[val_idx]
train_target = train_target[train_idx]

print("train_input:{}, val_input:{}, train_target:{}, val_target:{}".format(train_input.shape, val_input.shape, train_target.shape, val_target.shape))

def cbr(x, out_layer, kernel, stride, dilation):
    x = Conv1D(out_layer, kernel_size=kernel, dilation_rate=dilation, strides=stride, padding="same")(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)
    return x

def se_block(x_in, layer_n):
    x = GlobalAveragePooling1D()(x_in)
    x = Dense(layer_n//8, activation="relu")(x)
    x = Dense(layer_n, activation="sigmoid")(x)
    x_out=Multiply()([x_in, x])
    return x_out

def resblock(x_in, layer_n, kernel, dilation, use_se=True):
    x = cbr(x_in, layer_n, kernel, 1, dilation)
    x = cbr(x, layer_n, kernel, 1, dilation)
    if use_se:
        x = se_block(x, layer_n)
    x = Add()([x_in, x])
    return x

def Unet(input_shape=(None,1)):
    layer_n = 64
    kernel_size = 7
    depth = 2

    input_layer = Input(input_shape)
    input_layer_1 = AveragePooling1D(5)(input_layer)
    input_layer_2 = AveragePooling1D(25)(input_layer)

    ########## Encoder
    x = cbr(input_layer, layer_n, kernel_size, 1, 1)#1000
    for i in range(depth):
        x = resblock(x, layer_n, kernel_size, 1)
    out_0 = x

    x = cbr(x, layer_n*2, kernel_size, 5, 1)
    for i in range(depth):
        x = resblock(x, layer_n*2, kernel_size, 1)
    out_1 = x

    x = Concatenate()([x, input_layer_1])
    x = cbr(x, layer_n*3, kernel_size, 5, 1)
    for i in range(depth):
        x = resblock(x, layer_n*3, kernel_size, 1)
    out_2 = x

    x = Concatenate()([x, input_layer_2])
    x = cbr(x, layer_n*4, kernel_size, 5, 1)
    for i in range(depth):
        x = resblock(x, layer_n*4, kernel_size, 1)

    ########### Decoder
    x = UpSampling1D(5)(x)
    x = Concatenate()([x, out_2])
    x = cbr(x, layer_n*3, kernel_size, 1, 1)

    x = UpSampling1D(5)(x)
    x = Concatenate()([x, out_1])
    x = cbr(x, layer_n*2, kernel_size, 1, 1)

    x = UpSampling1D(5)(x)
    x = Concatenate()([x, out_0])
    x = cbr(x, layer_n, kernel_size, 1, 1)

    #regressor
    #x = Conv1D(1, kernel_size=kernel_size, strides=1, padding="same")(x)
    #out = Activation("sigmoid")(x)
    #out = Lambda(lambda x: 12*x)(out)

    #classifier
    x = Conv1D(11, kernel_size=kernel_size, strides=1, padding="same")(x)
    out = Activation("softmax")(x)

    model = Model(input_layer, out)

    return model

def augmentations(input_data, target_data):
    #flip
    if np.random.rand()<0.5:
        input_data = input_data[::-1]
        target_data = target_data[::-1]

    return input_data, target_data

def Datagen(input_dataset, target_dataset, batch_size, is_train=False):
    x=[]
    y=[]

    count=0
    idx_1 = np.arange(len(input_dataset))
    #idx_2 = np.arange(len(input_dataset))
    np.random.shuffle(idx_1)
    #np.random.shuffle(idx_2)

    while True:
        for i in range(len(input_dataset)):
            input_data = input_dataset[idx_1[i]]
            target_data = target_dataset[idx_1[i]]
            #input_data_mix = input_dataset[idx_2[i]]
            #target_data_mix = target_dataset[idx_2[i]]

            if is_train:
                input_data, target_data = augmentations(input_data, target_data)
                #input_data_mix, target_data_mix = augmentations(input_data_mix, target_data_mix)

            x.append(input_data)
            y.append(target_data)
            count+=1
            if count==batch_size:
                x=np.array(x, dtype=np.float32)
                y=np.array(y, dtype=np.float32)
                inputs = x
                targets = y
                x = []
                y = []
                count=0
                yield inputs, targets

class macroF1(Callback):
    def __init__(self, model, inputs, targets):
        self.model = model
        self.inputs = inputs
        self.targets = np.argmax(targets, axis=2).reshape(-1)

    def on_epoch_end(self, epoch, logs):
        pred = np.argmax(self.model.predict(self.inputs), axis=2).reshape(-1)
        f1_val = f1_score(self.targets, pred, average="macro")
        print("val_f1_macro_score: ", f1_val)

def model_fit(model, train_inputs, train_targets, val_inputs, val_targets, n_epoch, batch_size=32):
    hist = model.fit_generator(
        Datagen(train_inputs, train_targets, batch_size, is_train=True),
        steps_per_epoch = len(train_inputs) // batch_size,
        epochs = n_epoch,
        validation_data=Datagen(val_inputs, val_targets, batch_size),
        validation_steps = len(val_inputs) // batch_size,
        callbacks = [lr_schedule, macroF1(model, val_inputs, val_targets)],
        shuffle = False,
        verbose = 1
        )
    return hist

def lrs(epoch):
    if epoch<35:
        lr = learning_rate
    elif epoch<50:
        lr = learning_rate/10
    else:
        lr = learning_rate/100
    return lr

######
K.clear_session()
model = Unet()
#print(model.summary())

learning_rate=0.0005
n_epoch=100
batch_size=32

lr_schedule = LearningRateScheduler(lrs)

#regressor
#model.compile(loss="mean_squared_error",
#              optimizer=Adam(lr=learning_rate),
#              metrics=["mean_absolute_error"])

#classifier
model.compile(loss=categorical_crossentropy,
              optimizer=Adam(lr=learning_rate),
              metrics=["accuracy"])

hist = model_fit(model, train_input, train_target, val_input, val_target, n_epoch, batch_size)

pred = np.argmax((model.predict(val_input)+model.predict(val_input[:,::-1,:])[:,::-1,:])/2, axis=2).reshape(-1)
gt = np.argmax(val_target, axis=2).reshape(-1)
print("SCORE_oldmetric: ", cohen_kappa_score(gt, pred, weights="quadratic"))
print("SCORE_newmetric: ", f1_score(gt, pred, average="macro"))

pred = np.argmax((model.predict(test_input)+model.predict(test_input[:,::-1,:])[:,::-1,:])/2, axis=2).reshape(-1)

df_sub = pd.read_csv("data/sample_submission.csv", dtype={'time':str})
df_sub.open_channels = np.array(np.round(pred,0), np.int)
df_sub.to_csv("submission.csv",index=False)


from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Conv1D
from tensorflow.keras.layers import MaxPooling1D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.layers import MaxPooling3D
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import LSTM
from tensorflow.keras.layers import ConvLSTM2D
from tensorflow.keras.layers import Bidirectional
from tensorflow.keras.layers import GRU
from tensorflow.keras.layers import LeakyReLU
from kerastuner.tuners import RandomSearch
from kerastuner.tuners import BayesianOptimization
from tensorflow.keras.callbacks import EarlyStopping
import itertools
import numpy as np
import pandas as pd
import pickle
import random
import os
import statistics
import pickle
#os.chdir("C:/githubrepo/CapstoneA/") #Zack and Andy's github data folder
from analysis.finalcountdown_stage4 import Final_Accuracy
from analysis.zg_Load_Data import Load_Data
from analysis.zg_layer_generator_01 import Layer_Generator
from analysis.zg_model_tuning_01 import Model_Tuning
from analysis.ah_Model_Info import Model_Info

import warnings
warnings.filterwarnings("ignore")

#==============================================================================
#Returns the percentage of layers that are of the given type
def Percent_Layer_Type(layers, s):
	count = len([i for i in layers if i.startswith(s)])
	return float(count)/float(len(layers))

#Gets the average "size" of a layer
def Avg_Num_Nodes(hyp):
	val = 0
	count = 0
	for key in hyp:
		if "units" in hyp[key]:
			count += 1
			val += hyp[key]["units"][0]

	if count == 0:
		return 0

	return int(float(val)/float(count))

def Get_Accuracy(y_true, y_pred):
	correct = 0

	for i in np.arange(len(y_true)):
		if y_true[i][y_pred[i]] == 1:
			correct += 1.0

	return correct/float(len(y_true))

class FakeTuner:
	def Choice(self, name = "", ls = []):
		# hp.Choice("model_structures_index", self.model_structures_index)
		return random.choice(ls)

class FakeDataset:
	def __init__(self, xtrain, xtest, ytrain, ytest, ws):
		self.x_train = xtrain
		self.x_test = xtest
		self.y_train = ytrain
		self.y_test = ytest
		self.window_size = ws
		return
#==============================================================================

#Load in the accuracy files
fa = Final_Accuracy()
fa.Help()
acc = fa.Load_The_File()

print(acc["Conv1D"][0])

colnames = ["key", "index", "val_accuracy", "train_accuracy", "depth", "optimizer"]
#key, acc, val_acc, layer depth, optimizer
rows = []
for key in acc:
	i = 0
	for model in acc[key]:
		row = []
		row.append(key)						#key
		row.append(i)						#index of where it was in orig
		row.append(*model[0][3])			#validation accuracy
		row.append(*model[0][1])			#training accuracy
		row.append(len(model[0][0]))		#depth
		row.append(model[1]["optimizer"])	#optimizer
		row.append(Percent_Layer_Type(model[0][0], "Dropout"))
		row.append(Percent_Layer_Type(model[0][0], "GRU"))
		row.append(Percent_Layer_Type(model[0][0], "LSTM"))
		row.append(Percent_Layer_Type(model[0][0], "BidirectionalGRU"))
		row.append(Percent_Layer_Type(model[0][0], "BidirectionalLSTM"))
		row.append(Percent_Layer_Type(model[0][0], "Dense"))
		row.append(Percent_Layer_Type(model[0][0], "Conv1D"))
		row.append(Percent_Layer_Type(model[0][0], "ConvLSTM2D"))
		row.append(Percent_Layer_Type(model[0][0], "MaxPooling"))
		row.append(model[0][5][0])
		row.append(model[0][5][1])
		row.append(model[0][5][2])
		row.append(Avg_Num_Nodes(model[1]))
		rows.append(row)
		i += 1

#Makes the rows into a dataframe
df = pd.DataFrame(rows)
#Sets the names of the columns
colnames.append("lay_%dropout")
colnames.append("lay_%gru")
colnames.append("lay_%lstm")
colnames.append("lay_%bgru")
colnames.append("lay_%blstm")
colnames.append("lay_%dense")
colnames.append("lay_%conv1d")
colnames.append("lay_%cl2d")
colnames.append("lay_%pool")
colnames.append("window_size")
colnames.append("overlap%")
colnames.append("batch_size")
colnames.append("avg_num_nodes")
df.columns = colnames

#==============================================================================

#Stage 5 function
def Stage_Five(best_setups, best_idx, model_structures_type):
	#Putting the structures into this ordering
	model_structures = []
	hyp_str = []
	for setup in best_setups:
		model_structures.append(setup[0][0])
		hyp_str.append(setup[1])

	results_here = []
	for i in np.arange(len(model_structures)):
		with open("data/step5_hyp/" + model_structures_type + "_ModelStr_Hyp.pkl", "wb") as fp:   #Pickling
			pickle.dump(hyp_str[i], fp)

		window_size = best_setups[i][0][5][0]
		overlap_per = float(best_setups[i][0][5][1])/float(100.0)
		batch_size = best_setups[i][0][5][2]

		try:
			#List of model accuracies for the 28 validations
			val_acc = []
			test_acc = []
			val_indices = []
			test_indices = []
			for val_index in np.arange(28):
				print("SUBJECT:", val_index)
				#ConvLSTM2D has extra stuff, so if this is that it gets the parameters
				lay_gen = Layer_Generator()
				clstm_params = {}
				if model_structures_type == "ConvLSTM2D":
					clstm_params = lay_gen.Generate_Layer_Parameters()["ConvLSTM2D"]

				#print(clstm_params)

				#Loading in the dataset
				data_params = {'dataset' : 'firebusters',
								'train_p' : 0.8,
								'w_size' : window_size,
								'o_percent' : overlap_per,
								'LOSO' : True,
								'clstm_params' : clstm_params,
								'valIndex' : val_index
								}
				dataset = Load_Data(**data_params)

				#The indices of the test set (0-27), the other ones are made into the train
				test_set_size = 3
				testIndices = random.sample(list(np.arange(27)), test_set_size)
				#Based on the test indices, it makes the training/test sets
				x_train, y_train, x_test, y_test = dataset.GetTrainTestFromFolds(testIndices)
				x_val = dataset.x_test
				y_val = dataset.y_test

				#Because we send it the dataset, we make a fake dataset class that will
				#contain the data so that it can be referenced
				fake_dataset = FakeDataset(x_train, x_test, y_train, y_test, window_size)
				mt = Model_Tuning([model_structures[i]],
									fake_dataset,
									m_tuning = "val_" + model_structures_type,
									parent_fldr = "",
									fldr_name = "",
									fldr_sffx = "")

				hp = FakeTuner()
				model = mt.The_Model(hp)
				callbacks = [EarlyStopping(monitor='val_accuracy', mode='max', patience = 8, restore_best_weights=True)]
				result_train = model.fit(x_train, y_train, validation_data=(x_test, y_test),
									  epochs = 60, batch_size = batch_size, callbacks=callbacks)
				result_val = model.predict_classes(x_val)

				accuracy = Get_Accuracy(y_val, result_val)
				val_acc.append(accuracy)

				#Because of the way that the indices are made, when the val dude
				#is removed, the id list works like a stack. So this makes it so that
				#the values are able to be compared during our analysis.
				for k in np.arange(len(testIndices)):
					if testIndices[k] >= val_index:
						testIndices[k] = testIndices[k] + 1

				test_indices.append(testIndices)
				val_indices.append(val_index)
				test_acc.append(max(result_train.history["val_accuracy"][-9:]))
				#END FOR LOOP

			data_params = best_setups[i][0][5]
			first_idx = [model_structures[i], best_idx[i], test_acc, test_indices, val_acc, val_indices, data_params]
			res_loop = [first_idx, hyp_str[i]]
			#Appends the results from this model/validation set
			results_here.append(res_loop)
			#Delete the pkl file
			os.remove("data/step5_hyp/" + model_structures_type + "_ModelStr_Hyp.pkl")
			#SAVE THE RESULTS TO A FILE INCREMENTALLY
			with open("data/step5_res/" + model_structures_type + "_" + str(best_idx[i]) + "_results.pkl", "wb") as fp:   #Pickling
				pickle.dump(res_loop, fp)
			#END FOR LOOP
		except:
			print("ERROR with", model_structures_type, "number", best_idx[i])

	#SAVE THE RESULTS TO A FILE
	with open("data/step5_res/" + model_structures_type + "_results.pkl", "wb") as fp:   #Pickling
		pickle.dump(results_here, fp)

	return

#==============================================================================

#Chooses the ones with the highest accuracy based on conditions
#ConvLSTM2D          65
#Conv1D              34
#df_best = df[ (abs(df["val_accuracy"] - df["train_accuracy"]) <= 0.03) & (df["val_accuracy"] >= 0.9) ]
#BidirectionalGRU    48
#BidirectionalLSTM   46
#df_best = df[ (abs(df["val_accuracy"] - df["train_accuracy"]) <= 0.05) & (df["val_accuracy"] >= 0.9) ]
#LSTM                38
#df_best = df[ (abs(df["val_accuracy"] - df["train_accuracy"]) <= 0.08) & (df["val_accuracy"] >= 0.9) ]
#GRU                 37
#df_best = df[ (abs(df["val_accuracy"] - df["train_accuracy"]) <= 0.1) & (df["val_accuracy"] >= 0.8) ]
#Dense               34
df_best = df[ (abs(df["val_accuracy"] - df["train_accuracy"]) <= 0.3) & (df["val_accuracy"] >= 0.6) ]

#print( df_best[["key"]].apply(pd.value_counts) )

#Model Category
#["BidirectionalGRU", "BidirectionalLSTM", "Conv1D", "ConvLSTM2D", "Dense", "GRU", "LSTM"]
model_structures_type = "Dense"

#Select the model category
df_best = df_best[df_best["key"] == model_structures_type]

#Gets the best indices (indices are from the original pkl file)
best_idx = list(df_best["index"])

#Randomly chooses 20 of them to run
best_idx = random.sample(best_idx, 20)

#Pull out those best models
best_setups = acc[model_structures_type]
best_setups = [best_setups[i] for i in best_idx ]

Stage_Five(best_setups, best_idx, model_structures_type)


"""
    ulmo.usgs.eros.core
    ~~~~~~~~~~~~~~~~~~~~~

    This module provides access to data provided by the `United States Geological
    Survey`_ `Earth Resources Observation and Science (EROS) Center`_ application
    services web site.

    The `DCP message format`_ includes some header information that is parsed and
    the message body, with a variable number of characters. The format of the
    message body varies widely depending on the manufacturer of the transmitter,
    data logger, sensors, and the technician who programmed the DCP. The body can
    be simple ASCII, sometime with parameter codes and time-stamps embedded,
    sometimes not. The body can also be in 'Pseudo-Binary' which is character
    encoding of binary data that uses 6 bits of every byte and guarantees that
    all characters are printable.

    .. _United States Geological Survey: http://www.usgs.gov/
    .. _Earth Resources Observation and Science (EROS) Center: http://nimbus.cr.usgs.gov/app_services.php

"""
from __future__ import print_function

from geojson import Feature, FeatureCollection, Polygon
import hashlib
import logging
import os
import pandas as pd
import requests
import subprocess
from ulmo import util
import zipfile

# eros services base urls
EROS_INVENTORY_URL = 'http://nimbus.cr.usgs.gov/index_service/Index_Service_JSON2.asmx'
EROS_VALIDATION_URL = 'http://extract.cr.usgs.gov/requestValidationServiceClient/sampleRequestValidationServiceProxy/getTiledDataDirectURLs2.jsp?TOP=%s&BOTTOM=%s&LEFT=%s&RIGHT=%s&LAYER_IDS=%s&JSON=true'

# default file path (appended to default ulmo path)
DEFAULT_FILE_PATH = 'usgs/eros/'

ext = {
        'geotiff': '.tif',
        'img': '.img',
        'jpg2000': '.jp2',
        '': '',
    }

# configure logging
LOG_FORMAT = '%(message)s'
logging.basicConfig(format=LOG_FORMAT)
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)

def get_attribute_list(as_dataframe=True):
    """retrieve list of metadata attributes for dataset

    Parameters
    ----------
    as_dataframe : ``True`` (default) or ``False``
        if ``True`` return pandas dataframe

    Returns
    -------
        available metadata attributes
    """
    url = EROS_INVENTORY_URL + '/return_Attribute_List'
    return _call_service(url, {}, as_dataframe)

def get_available_datasets(bbox, attrs=None, as_dataframe=True):
    """retrieve available datasets for a given bounding box.

    Parameters
    ----------
    bbox : (sequence of float|str)
        bounding box of in geographic coordinates of area to download tiles
        in the format (min longitude, min latitude, max longitude, max latitude)
    attrs: comma separated list of str
        metadata attributes to retrieve, `None` (default) retrieves all
    as_dataframe : ``True`` (default) or ``False``
        if ``True`` return pandas dataframe

    Returns
    -------
    datasets : dict or pandas DataFrame
        returns availabel datasets
    """
    xmin, ymin, xmax, ymax = [float(n) for n in bbox]
    epsg = 4326

    if attrs is None:
        attrs = ','.join(get_attribute_list().drop(37)['name'].tolist()) #attr 37->LYR_URL causing null returns

    payload = {
                'Attribs': attrs,
                'Xmin': xmin,
                'Ymin': ymin,
                'Xmax': xmax,
                'Ymax': ymax,
                'EPSG': epsg,
        }

    url = EROS_INVENTORY_URL + '/return_Attributes_Download_Only'
    return _call_service(url, payload, as_dataframe)

def get_available_formats(product_key, as_dataframe=True):
    """retrieve list of data formats available for dataset

    Parameters
    ----------
    product_key : str
        dataset name. (see get_available_datasets for list)
    as_dataframe : ``True`` (default) or ``False``
        if ``True`` return pandas dataframe

    Returns
    -------
        available data formats
    """
    url = EROS_INVENTORY_URL + '/return_Download_Options'
    payload = {'ProductIDs': product_key}
    return _call_service(url, payload, as_dataframe)

def get_raster_availability(product_key, bbox, fmt=None):
    """retrieve metadata for raster tiles that cover the given bounding box
    for the specified data layer.

    Parameters
    ----------
    product_key : str
        dataset layer name. (see get_available_layers for list)
    bbox : (sequence of float|str)
        bounding box of in geographic coordinates of area to download tiles
        in the format (min longitude, min latitude, max longitude, max latitude)
    fmt : str
        desired data format. if `None`, geotiff followed by img will be given preference

    Returns
    -------
    metadata : geojson FeatureCollection
        returns metadata including download urls as a FeatureCollection
    """

    xmin, ymin, xmax, ymax = [float(n) for n in bbox]
    layer, fmt = _layer_id(product_key, fmt)

    url = EROS_VALIDATION_URL % (ymax, ymin, xmin, xmax, layer)
    print('retrieving raster availability from %s' % url)
    r = requests.get(url)

    tiles = r.json()['REQUEST_SERVICE_RESPONSE']['PIECE']

    features = []
    for tile in tiles:
        features.append(Feature(geometry=Polygon(_bbox2poly(tile['BBOX'])), id=tile['ID'],
                    properties={'download url': _extract_url(tile['DOWNLOAD_URL']),'product_key': product_key, 'format': fmt}
                    ))

    return FeatureCollection(features)

def get_raster(product_key, bbox, fmt=None, path=None, check_modified=False, mosaic=False):
    """downloads National Elevation Dataset raster tiles that cover the given bounding box
    for the specified data layer.

    Parameters
    ----------
    product_key : str
        dataset name. (see get_available_datasets for list)
    bbox : (sequence of float|str)
        bounding box of in geographic coordinates of area to download tiles
        in the format (min longitude, min latitude, max longitude, max latitude)
    fmt : ``None`` or str
        available formats vary in different datasets. If ``None``, preference will be given
        to geotiff and then img, followed by whatever fmt is available
    path : ``None`` or path
        if ``None`` default path will be used
    update_cache: ``True`` or ``False`` (default)
        if ``False`` then tiles will not be re-downloaded if they exist in the path
    check_modified: ``True`` or ``False`` (default)
        if tile exists in path, check if newer file exists online and download if available.
    mosaic: ``True`` or ``False`` (default)
        if ``True``, mosaic and clip downloaded tiles to the extents of the bbox provided. Requires
        rasterio package and GDAL.

    Returns
    -------
    raster_tiles : geojson FeatureCollection
        metadata as a FeatureCollection. local url of downloaded data is in feature['properties']['file']
    """
    raster_tiles = _download_tiles(get_raster_availability(product_key, bbox, fmt),
        path, check_modified)

    if mosaic:
        if path is None:
            path = os.path.join(util.get_ulmo_dir(), DEFAULT_FILE_PATH)
        util.mkdir_if_doesnt_exist(os.path.join(path, 'by_boundingbox'))
        uid = util.generate_raster_uid(product_key, xmin, ymin, xmax, ymax)
        output_path = os.path.join(path, 'by_boundingbox', uid + '.tif')

        if os.path.isfile(output_path) and not update_cache:
            return output_path

        raster_files = [tile['properties']['file'] for tile in raster_tiles['features']]
        util.mosaic_and_clip(raster_files, xmin, ymin, xmax, ymax, output_path)

        return [output_path]

    return raster_tiles

def get_themes(as_dataframe=True):
    """retrieve list of data themes available

    Parameters
    ----------
    as_dataframe : ``True`` (default) or ``False``
        if ``True`` return pandas dataframe

    Returns
    -------
        available data themes
    """
    url = EROS_INVENTORY_URL + '/return_Themes'
    return _call_service(url, {}, as_dataframe)

def _bbox2poly(bbox):
    xmin = bbox['LEFT']
    xmax = bbox['RIGHT']
    ymin = bbox['BOTTOM']
    ymax = bbox['TOP']

    return [[(xmin,ymin), (xmin,ymax), (xmax, ymax), (xmax, ymin), (xmin, ymin)]]

def _call_service(url, payload, as_dataframe):
    payload['callback'] = ''
    print('calling %s with payload %s' % (url, repr(payload)))
    r = requests.get(url, params=payload)
    if as_dataframe:
        df = pd.DataFrame(r.json()['items'])
        if df.empty:
            return df
        df.index = df['ID']
        del df['ID']
        return df
    else:
        return r.json()

def _download_tiles(tiles, path=None, check_modified=False):

    if path is None:
        path = os.path.join(util.get_ulmo_dir(), DEFAULT_FILE_PATH)

    for tile in tiles['features']:

        metadata = tile['properties']
        layer_path = os.path.join(path, metadata['product_key'])
        tile['properties']['file'] = util.download_tiles(layer_path, metadata['download url'], metadata['format'], check_modified)[0]

    return tiles

def _extract_url(url):
    headers = requests.head(url).headers
    if 'location' in list(headers.keys()):
        url = headers['location']

    return url

def _layer_id(product_key, fmt=None):
    available_formats = get_available_formats(product_key)
    if not available_formats.empty:
        available_formats = available_formats['outputformat'][0].lower()
    else:
        available_formats = ''

    if fmt is None:
        if 'geotiff' in available_formats:
            fmt = 'geotiff'
        elif 'img' in available_formats:
            fmt = 'img'
        else:
            fmt = available_formats.split(',')[0].split('-')[-1]
    else:
        if fmt not in available_formats:
            raise ValueError('file format %s not available for product %s' % (fmt, product_key))

    pos = available_formats.find(fmt)
    layer_id = product_key + available_formats[pos-3:pos-1]

    return layer_id, ext[fmt]


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

import tensorflow as tf

if __name__ == '__main__':

    mnist = tf.keras.datasets.fashion_mnist
    (X_train, Y_train), (X_test, Y_test) = mnist.load_data()

    X_train = X_train.reshape(X_train.shape[0],X_train.shape[1],X_train.shape[2], 1) / 255.0
    X_test = X_test.reshape(X_test.shape[0],X_test.shape[1],X_test.shape[2],1) / 255.0

    model = tf.keras.models.Sequential([
        tf.keras.layers.Conv2D(filters=32, kernel_size=(3,3), activation='relu', input_shape=(28,28,1), name='conv_2d_1'),
        tf.keras.layers.MaxPooling2D((2,2), (2,2), name='max_pool_1'),
        tf.keras.layers.Conv2D(filters=32, kernel_size=(3,3), activation='relu', name='conv_2d_2'),
        tf.keras.layers.MaxPooling2D(2,2,name='max_pool_2'),
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(units=32, activation='relu'),
        tf.keras.layers.Dense(units=10, activation='softmax'),
    ])

    model.compile(
        optimizer='adam',
        loss='sparse_categorical_crossentropy',
        metrics=['accuracy'],
    )

    model.fit(X_train, Y_train, epochs=1)

    test_loss = model.evaluate(X_test, Y_test)

    print(test_loss)

    print(model.summary())

    classifications = model.predict(X_test)

    print(classifications[3])
    print(Y_test[3])


#!/usr/bin/env python

""" Construct a rotation matrix to correct measured star locations to expected locations. """

__author__ = "Yeb Havinga"
__license__ = "MIT"

from model import rotate
from model.star import Star, StarList
from numpy import array
from astropy import units as u
from astropy.coordinates import SkyCoord, cartesian_to_spherical, spherical_to_cartesian, EarthLocation, AltAz
from astropy.time import Time
from scipy.io import savemat
from model import device

my_location = EarthLocation(lat=52.253*u.deg, lon=5.253*u.deg, height=0*u.m)

real = StarList()
observed = StarList()

def info_message(name, az, alt):
    print("""
Point the telescope to {name} at az:{az} alt:{alt}, and press Enter when found, 's' to skip
""".format(name=name, az=az, alt=alt))
    return input()

def coord_to_altaz(coord):
    altaz = coord.transform_to(AltAz(obstime=Time.now(), location=my_location))
    return altaz

def get_star(name):
    coord = SkyCoord.from_name(name)
    altaz = coord_to_altaz(coord)
    r = info_message(name, altaz.az.to_string(decimal=True), altaz.alt.to_string(decimal=True))
    if r == 's':
        return
    altaz = coord_to_altaz(coord)
    print(altaz)
    cart = array(spherical_to_cartesian(1, altaz.alt, altaz.az))
    real.add_star(Star(x=cart[0], y=cart[1], z=cart[2]))
    x, y, z = device.get_info()
    observed.add_star(Star(x=x, y=y, z=z))

def calculate_rotation_matrix():
    R, t, avmse = rotate.findRotationMatrix(rotate.cart_list_to_matrix(observed.cart_list()), rotate.cart_list_to_matrix(real.cart_list()))
    print("Calculated rotation matrix with mse %.2f" % avmse)
    savemat('rotate.mat', {'R': R, 't': t, 'mse': avmse})
    print("Rotation matrix saved")
    return R, t

def save_lists():
    real.save_list('calibrate_real_list.mat')
    observed.save_list('calibrate_observed_list.mat')
    print("Real and observed lists saved")

def save_estimated(R):
    estimated = rotate.rotate(R, t, rotate.cart_list_to_matrix(observed.cart_list()))
    est_cart_list = rotate.matrix_to_cart(estimated)
    xl = [cart[0] for cart in est_cart_list]
    yl = [cart[1] for cart in est_cart_list]
    zl = [cart[2] for cart in est_cart_list]
    savemat('calibrate_estimated_list.mat', {'x': xl, 'y': yl, 'z': zl})

get_star('Arcturus')
get_star('Vega')
get_star('Dubhe')
get_star('Deneb')
get_star('Polaris')

R, t = calculate_rotation_matrix()
save_lists()
save_estimated(R)


def solution(land):
    N = len(land)
    dp = [[0, 0, 0, 0] for _ in range(N + 1)]
    dp[1] = land[0]
    for dp_idx in range(1, N + 1):
        for i in range(4):
            dp[dp_idx][i] = (
                max(
                    dp[dp_idx - 1][(i + 1) % 4],
                    dp[dp_idx - 1][(i + 2) % 4],
                    dp[dp_idx - 1][(i + 3) % 4],
                )
                + land[dp_idx - 1][i]
            )
    return max(dp[N])

if __name__ == "__main__":
    tests = [
        [[[1, 2, 3, 5], [5, 6, 7, 8], [4, 3, 2, 1]], 16],
    ]
    for test in tests:
        land, result = test
        print(solution(land) == result)


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

data_dir = "../Data/"
backup_dir = "../Backup/"

print("-------------\n|IMPORT|\n-------------")

import os, time, pickle
import numpy as np
from numpy import *
import tensorflow as tf
#from tensorflow import reset_default_graph
import datetime
from sklearn import metrics

import data_helpers_1
from data_helpers_1 import Dataset
from model1 import SADHA

#reset_default_graph()

os.environ["CUDA_VISIBLE_DEVICES"]="1"

print("-------------\n|INIT|\n-------------")

n_class = 2
dataset = 'politi'
embedding_dim = 100
hidden_size = 100
max_sen_len = 100
max_doc_len = 40

print("emdedding_dim :", embedding_dim, ", hidden_size :", hidden_size, ", max_sen_len :", max_sen_len, ", max_doc_len :", max_doc_len)

Test_split = 0.2
VALIDATION_SPLIT = 0.2
lr = 0.001
batch_size = 100
num_epochs = 10
evaluate_every = 50

print("batch_size :", batch_size, ", num_epochs :", num_epochs, ", evaluate_every :", evaluate_every)

allow_soft_placement = True
log_device_placement = False

print("-------------\n|LOAD DATA|\n-------------")

with open(data_dir+'docVectorCollection_withpara.pickle', 'rb') as handle:
    doc_data = pickle.load(handle)
with open(data_dir+'labelVectorCollection_withpara.pickle', 'rb') as handle:
    label_data = pickle.load(handle)
with open(data_dir+'claimVectorCollection_withpara.pickle', 'rb') as handle:
    claim_data = pickle.load(handle)
with open(data_dir+'SpeakerVectorCollection_withpara.pickle', 'rb') as handle:
    speaker_data = pickle.load(handle)
with open(data_dir+'DocSourceCollection_withpara.pickle', 'rb') as handle:
    source_doc_data = pickle.load(handle)
with open(data_dir+'TopicVectorCollection_withpara.pickle', 'rb') as handle:
    topic_data = pickle.load(handle)

print('len doc_data :', len(doc_data))
print()
print('claim example :', claim_data[0])
print('doc example :', doc_data[0])
print()
print('number of label :', len(set(label_data)), '- example :', label_data[0], ',', label_data[60])
print('number of speacker :', len(set(speaker_data)), '- example :', speaker_data[0], ',', speaker_data[60])
print('number of source :', len(set(source_doc_data)), '- example :', source_doc_data[0], ',', source_doc_data[60])
print('number of topic :', len(set(topic_data)), '- example :', topic_data[0], ',', topic_data[60])

print("-------------\n|DATASET CREATION|\n-------------")

train_doc = doc_data[:int((len(doc_data)+1)*.80)]
train_label = label_data[:int((len(doc_data)+1)*.80)]
train_claim = claim_data[:int((len(doc_data)+1)*.80)]
train_speaker = speaker_data[:int((len(doc_data)+1)*.80)]
train_source_doc = source_doc_data[:int((len(doc_data)+1)*.80)]
train_topic = topic_data[:int((len(doc_data)+1)*.80)]

test_doc = doc_data[int(len(train_doc)*.80+1):]
test_label = label_data[int(len(train_doc)*.80+1):]
test_claim = claim_data[int(len(train_doc)*.80+1):]
test_speaker = speaker_data[int(len(train_doc)*.80+1):]
test_source_doc = source_doc_data[int(len(train_doc)*.80+1):]
test_topic = topic_data[int(len(train_doc)*.80+1):]

Ftrain_doc = train_doc[:int((len(train_doc)+1)*.80)]
Ftrain_label = train_label[:int((len(train_doc)+1)*.80)]
Ftrain_claim = train_claim[:int((len(train_doc)+1)*.80)]
Ftrain_speaker = train_speaker[:int((len(train_doc)+1)*.80)]
Ftrain_source_doc = train_source_doc[:int((len(train_doc)+1)*.80)]
Ftrain_topic = train_topic[:int((len(train_doc)+1)*.80)]

val_doc = train_doc[int(len(train_doc)*.80+1):]
val_label = train_label[int(len(train_doc)*.80+1):]
val_claim = train_claim[int(len(train_doc)*.80+1):]
val_speaker = train_speaker[int(len(train_doc)*.80+1):]
val_source_doc = train_source_doc[int(len(train_doc)*.80+1):]
val_topic = train_topic[int(len(train_doc)*.80+1):]

print("-------------\n|DATA STRUCTURE|\n-------------")
print("Type :")
print("doc_data :", type(doc_data))
print("label_data :", type(label_data))
print("claim_data :", type(claim_data))
print("speaker_data :", type(speaker_data))
print("\nLenght :")
print("doc_data :", len(doc_data))
print("label_data :", len(label_data))
print("claim_data :", len(claim_data))
print("speaker_data :", len(speaker_data))
print("\nType [0]:")
print("doc_data[0] :", type(doc_data[0]))
print("label_data[0] :", type(label_data[0]))
print("claim_data[0] :", type(claim_data[0]))
print("speaker_data[0] :", type(speaker_data[0]))
print("\nSlice[0]:")
print("doc_data[0] :", doc_data[0])
print("label_data[0] :", label_data[0])
print("claim_data[0] :", claim_data[0])
print("speaker_data[0] :", speaker_data[0])

all_d = Dataset(doc_data, label_data, claim_data, speaker_data, source_doc_data, topic_data)
trainset = Dataset(Ftrain_doc, Ftrain_label, Ftrain_claim, Ftrain_speaker, Ftrain_source_doc, Ftrain_topic)
devset = Dataset(val_doc, val_label, val_claim, val_speaker, val_source_doc, val_topic)
testset = Dataset(test_doc, test_label, test_claim, test_speaker, test_source_doc, test_topic)

alldata = np.concatenate([trainset.t_docs, devset.t_docs, testset.t_docs], axis=0)

print("-------------\n|EMBEDDINGFILE CREATION|\n-------------")

embeddingfile, wordsdict = data_helpers_1.load_embedding(data_dir+"glove.6B.100d.txt", alldata, embedding_dim)

spkrdict, domdict, tpcdict = all_d.get_spkr_dom_tpc_dict()

print("-------------\n|BATCHES GENERATION|\n-------------")

trainbatches = trainset.batch_iter(spkrdict, domdict, tpcdict, wordsdict, n_class, batch_size, num_epochs, max_sen_len, max_doc_len)
devset.genBatch(spkrdict, domdict, tpcdict, wordsdict, batch_size, max_sen_len, max_doc_len, n_class)
testset.genBatch(spkrdict, domdict, tpcdict, wordsdict, batch_size, max_sen_len, max_doc_len, n_class)
print("Testset:", type(testset))
print("spkr:", len(testset.spkr))
print("dom:", len(testset.dom))
print("tpc:", len(testset.tpc))
print("label:", len(testset.label))
print("docs:", len(testset.docs))
print("sen_len:", len(testset.sen_len))
print("doc_len:", len(testset.doc_len))

print("-------------\n|BEGINNING OF THE TRAINING|\n-------------")

with tf.Graph().as_default():
    session_config = tf.ConfigProto(
        allow_soft_placement=allow_soft_placement,
        log_device_placement=log_device_placement
    )
    session_config.gpu_options.allow_growth = False
    session_config.gpu_options.allocator_type = 'BFC'

    #config = tf.ConfigProto(device_count = {'GPU': 1})
    sess = tf.Session(config=session_config)

    with sess.as_default():

        print("-------------\n|MODEL CREATION|\n-------------")

        print(" max_sen_len : ", max_sen_len)
        print(" max_doc_len : ", max_doc_len)
        print(" n_class : ", n_class)
        print(" embeddingfile : ", embeddingfile)
        print(" embedding_dim : ", embedding_dim)
        print(" hidden_size : ", hidden_size)
        print(" spkr_num : ", len(spkrdict))
        print(" dom_num : ", len(domdict))
        print(" tpc_num : ", len(tpcdict))

        SADHA = SADHA(
            max_sen_len = max_sen_len,
            max_doc_len = max_doc_len,
            class_num = n_class,
            embedding_file = embeddingfile,
            embedding_dim = embedding_dim,
            hidden_size = hidden_size,
            spkr_num = len(spkrdict),
            dom_num = len(domdict),
            tpc_num = len(tpcdict)
        )

        SADHA.build_model()

        # Define Training procedure
        global_step = tf.Variable(0, name="global_step", trainable=False)
        optimizer = tf.train.AdamOptimizer(lr)
        grads_and_vars = optimizer.compute_gradients(SADHA.loss)
        train_op = optimizer.apply_gradients(grads_and_vars, global_step=global_step)

        # Save dict
        timestamp = str(int(time.time()))
        checkpoint_dir = os.path.abspath(backup_dir+timestamp)
        checkpoint_prefix = os.path.join(checkpoint_dir, "model")
        if not os.path.exists(checkpoint_dir):
            os.makedirs(checkpoint_dir)
        saver = tf.train.Saver(tf.global_variables(), max_to_keep=1)

        print("-------------\n|DICT STORING|\n-------------")
        print("path:", checkpoint_dir)
        print("path2:", checkpoint_prefix)
        with open(checkpoint_dir + "/wordsdict.txt", 'wb') as f:
            pickle.dump(wordsdict, f)
        with open(checkpoint_dir + "/spkrdict.txt", 'wb') as f:
            pickle.dump(spkrdict, f)
        with open(checkpoint_dir + "/domdict.txt", 'wb') as f:
            pickle.dump(domdict, f)
        with open(checkpoint_dir + "/tpcdict.txt", 'wb') as f:
            pickle.dump(tpcdict, f)
        with open(data_dir+'testset.pickle', 'wb') as handle:
            pickle.dump(testset, handle, protocol=pickle.HIGHEST_PROTOCOL)

        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())

        def train_step(batch):
            u, p, t, x, y, sen_len, doc_len = zip(*batch)
            feed_dict = {
                SADHA.spkrid: u,
                SADHA.domid: p,
                SADHA.tpcid: t,
                SADHA.input_x: x,
                SADHA.input_y: y,
                SADHA.sen_len: sen_len,
                SADHA.doc_len: doc_len
            }
            _, step, loss, accuracy = sess.run(
                [train_op, global_step, SADHA.loss, SADHA.accuracy],
                feed_dict)
            time_str = datetime.datetime.now().isoformat()
            print("{}: step {}, loss {:g}, acc {:g}".format(time_str, step, loss, accuracy))

        def predict_step(u, p, t, x, y, sen_len, doc_len, name=None):
            feed_dict = {
                SADHA.spkrid: u,
                SADHA.domid: p,
                SADHA.tpcid: t,
                SADHA.input_x: x,
                SADHA.input_y: y,
                SADHA.sen_len: sen_len,
                SADHA.doc_len: doc_len
            }
            step, loss, correct_num, C_matrix, prediction= sess.run([global_step, SADHA.loss, SADHA.correct_num, SADHA.C_matrix, SADHA.prediction], feed_dict)
            return correct_num, C_matrix, prediction

        def predict(dataset, name=None):
            acc = 0
            for i in range(dataset.epoch):
                if ((i+1)*100) < len(dataset.t_docs):
                    correct_num, C_matrix, prediction = predict_step(dataset.spkr[i], dataset.dom[i], dataset.tpc[i], dataset.docs[i], dataset.label[i], dataset.sen_len[i], dataset.doc_len[i], name)
                    acc += correct_num
            acc = acc * 1.00 / dataset.data_size
            return acc, C_matrix, prediction

        topacc = 0.
        better_dev_acc = 0.
        predict_round = 0

        for tr_batch in trainbatches:
            train_step(tr_batch)
            current_step = tf.train.global_step(sess, global_step)
            if current_step % evaluate_every == 0:
                predict_round += 1
                print("\nEvaluation round %d:" % (predict_round))
                dev_acc, C_matrix, prediction = predict(devset, name="dev")
                print("Predict DEV :", C_matrix)
                test_acc, C_matrix, prediction  = predict(testset, name="test")
                print("Predict DEV :", C_matrix)
                if dev_acc >= better_dev_acc:
                    better_dev_acc = dev_acc
                    topacc = test_acc
                    path = saver.save(sess, checkpoint_prefix, global_step=current_step)
                    print("Saved model checkpoint to {}\n".format(path))


from utils.utils import check_type, mount_message
from copy import deepcopy

class GameProcess:
    def __init__(self, data, game, address, queue_send, rooms):
        check_type(data, dict)
        self.data = data
        self.game = game
        self.address = address
        self.queue_send = queue_send
        self.rooms = rooms

    def _init_game(self):
        id_room = self.data["id_room"]
        room = self.rooms[id_room]

        test = False
        for i in room["players"]:
            if i["address"] == self.address:
                test = True
                break

        self.queue_send.put(mount_message("game_init", (test,)))
        self._send_state()

    def _exit_game(self):
        aux = self.game.remove_player(self.address)

        if aux is not None and aux:
            self.queue_send.put(mount_message("game_end"))

    def _end_game(self):
        self.game.end_game()

    def _send_state(self):
        id_room = self.game.actual_id
        if id_room is None:
            return

        try:
            room = self.rooms[id_room]
        except KeyError:
            self.game.actual_id = None
            self.queue_send.put(mount_message("game_end"))
            self.queue_send.put(mount_message("game_alert", ("The room no longer leaves.", )))

            return

        test = True
        for i in room["players"]:
            if i["address"] == self.address:
                test = False
                break

        if test and len(room["players"]) != 0:
            self.game.actual_id = None
            self.queue_send.put(mount_message("game_end"))
            self.queue_send.put(mount_message("game_alert", ("Leave the room!", )))
            return

        if room["word"] == room["word_discover"]:
            self.game.actual_id = None
            self.queue_send.put(mount_message("game_end"))

            if len(room["players"]) == 1:
                winner = 0
            else:
                winner = (room["rounds"] - 1) % len(room["players"])

            self.queue_send.put(mount_message(
                "game_alert", (f"The Game is End!\nThe Winner is {room['players'][winner]['name_player']}", )))
            return

        aux = deepcopy(room)
        aux["admin"] = aux["players"][0]["address"] == self.address
        _round = aux["rounds"] % len(aux["players"])
        aux["your"] = aux["players"][_round]["address"] == self.address
        del aux["word"]
        del aux["password"]
        for j in aux["players"]:
            del j["address"]

        aux["id_room"] = id_room

        self.queue_send.put(mount_message("game_att_state", (aux,)))

    def _make_play(self):
        aux = self.game.make_player(self.data["text"])

        if aux is None:
            self._send_state()
        else:
            self.queue_send.put(mount_message("game_alert", (aux, )))

    def start(self):
        aux = self.data["subtype"]
        if aux == "init":
            self._init_game()
        elif aux == "make_play":
            self._make_play()
        elif aux == "exit":
            self._exit_game()
        elif aux == "end":
            self._end_game()
        elif aux == "att_request":
            self._send_state()
        else:
            raise RuntimeError("Error game process.")

Jogo da Forca Online/server/logic/game/game_process.py


from matplotlib.pyplot import *   # this line just loads the plotting libraries so that we can easily plot graphs

# start by creating a list of all the possibilities for boys (let's just guess that the answer lies somewhere between 0 and 10 boys)
boys = range(1,10)

# the clue for the first equation states that each brother sees 3 times as many sisters as brothers
#     girls = 3*(boys-1)    ... note that we subtracted one from the boys to account for the boy that is observing

girls=[]  # easiest way to start with an empty girls list and add each time so that the girls and boys points "line up"
for numofboys in boys:
	girls.append(3*(numofboys-1))   # calculate and add the corresponding number of girls at each point

# draw the graph of boys vs girls for the first equation
plot(boys,girls)

# next equation states that each sister sees the same number of brothers and sisters
#      girls-1 = boys     ... note that we subtracted one from the girls for the girl that is observing
#  =>  girls = boys+1     ... we rearranged the equation for calculating girls, rather than boys, so that we can reuse
#                             the existing list of boy points, rather than setting up a set of girl points.

girls=[]
for numofboys in boys:
	girls.append(numofboys+1)   # add each point as we calculate it

# draw the graph of boys vs girls for the second equation (important to plot boys and girls on the same axes each time)
plot(boys,girls)

# pretty up the graph with headings and labels etc
title("Where do the lines cross?");  xlabel("boys");  ylabel("girls")

# show the graph to find the answer
show()


from .base import *

import queue
import threading

class ThreadingStart(StreamerBase):
    def __init__(self, prev):
        self._queue = queue.Queue()
        self._drained_lock = threading.Lock()
        self._drained = False
        runner = ThreadReservoirRunner(self, self._queue)
        super(ThreadingStart, self).__init__(prev, runner, 'threading_start')
        self._len = len(prev)

    def process(self, sample):
        return sample

    def queue(self):
        return self._queue

    def set_drained(self):
        self._drained_lock.acquire()
        self._drained = True
        self._drained_lock.release()

    def reset_drained(self):
        self._drained = False

    def get_drained(self):
        self._drained_lock.acquire()
        v = self._drained
        self._drained_lock.release()
        return v

class _ThreadingEnd(StreamerBase):
    def __init__(self, prev, num_threads):
        self._queue = queue.Queue()
        self._drained_lock = threading.Condition()
        self._drained = 0
        runner = ThreadSinkRunner(self, self._queue, num_threads)
        super(_ThreadingEnd, self).__init__(prev, runner, 'threading_end')

    def process(self, sample):
        return sample

    def queue(self):
        return self._queue

    def set_drained(self):
        self._drained_lock.acquire()
        self._drained += 1
        self._drained_lock.release()

    def reset_drained(self):
        self._drained = 0

    def get_drained(self):
        self._drained_lock.acquire()
        v = self._drained
        self._drained_lock.release()
        return v

class Threading(StreamerBase):
    def __init__(self, prev, num_threads):
        object.__init__(self)
        main_graph = prev.graph
        self._graph = main_graph
        self._nth = num_threads

        # Find start node & create end node running on subgraph
        start_node = [v for v in main_graph.dependencies(prev)
                      if isinstance(v, ThreadingStart)]
        if len(start_node) <= 0:
            raise ValueError('Cannot find master-prefetch pair')
        if len(start_node) > 1:
            raise ValueError('Too many prefetch master')
        self._sub_root = start_node[0]
        self._sub_sink = _ThreadingEnd(prev, num_threads)
        self._length = len(self._sub_sink)

        # Create sub-graph
        sub_graph = main_graph.isolate(self._sub_root, self._sub_sink, self)
        for subgraph_node in sub_graph:
            subgraph_node.graph = sub_graph

        # Set runner
        self._runner = ThreadRunner(self, num_threads, self._sub_root, self._sub_sink)
        self._name = 'threading'

    def process(self, sample):
        return sample

    def __len__(self):
        return self._length

class ThreadRunner(RunnerBase):
    def __init__(self, node, num_threads, sub_root, sub_sink):
        super(ThreadRunner, self).__init__(node)
        self._nth = num_threads
        self._sub_root = sub_root
        self._sub_sink = sub_sink
        self._in_queue = sub_root.queue()
        self._out_queue = sub_sink.queue()

        self._prev_drained = False
        self._end_count = 0

    def reset(self):
        while not self._in_queue.empty():
            self._in_queue.get()
        while not self._out_queue.empty():
            self._out_queue.get()
        self._sub_root.reset_drained()
        self._sub_sink.reset_drained()

        self._prev_drained = False
        self._end_count = 0

    def get(self):
        self.status = WAITING
        if self._out_queue.empty():
            return
        try:
            sample = self._out_queue.get(timeout=0.001)
        except Exception as e:
            return
        return sample

    def put(self, sample):
        self._in_queue.put(sample)

    def update(self):
        # Check prev nodes are drained
        if not self._prev_drained:
            node = self._node
            predecessors_status = [v.runner.status for v in node.graph.predecessors(node)]
            all_drained_test = predecessors_status.count(DRAINED) == len(predecessors_status)
            if all_drained_test:
                self._prev_drained = True
                self._sub_root.set_drained()

        end_count_test = self._sub_sink.get_drained() == self._nth
        queue_empty_test = self._out_queue.empty()
        if end_count_test and queue_empty_test:
            self.status = DRAINED
        elif queue_empty_test:
            self.status = WAITING
        else:
            self.status = READY

    def finalize(self):
        pass

class ThreadReservoirRunner(RunnerBase):
    def __init__(self, node, queue_):
        assert isinstance(queue_, queue.Queue)

        super(ThreadReservoirRunner, self).__init__(node)
        self._in_queue = queue_
        self._drained = False

    def reset(self):
        self.status = READY
        self._drained = False

    def get(self):
        self.status = WAITING
        try:
            sample = self._in_queue.get(timeout=0.001)
        except Exception as e:
            return
        return sample

    def put(self, sample):
        raise NotImplementedError('This function should not be called')

    def update(self):
        queue_empty_test = self._in_queue.empty()
        drained_test = self._node.get_drained()
        if queue_empty_test and drained_test:
            self.status = DRAINED
        elif queue_empty_test:
            self.status = WAITING
        else:
            self.status = READY

    def finalize(self):
        pass

class ThreadSinkRunner(Runner):
    def __init__(self, node, queue_, num_threads):
        assert isinstance(queue_, queue.Queue)

        super(ThreadSinkRunner, self).__init__(node)
        self._out_queue = queue_
        self._nth = num_threads
        self._threads = []

    def reset(self):
        self.status = READY
        self._threads.clear()

    def run(self):
        def _run():
            for sample in graph_run(self._node.graph, self._node, False):
                self._out_queue.put(sample)

        for _ in range(self._nth):
            self._threads.append(threading.Thread(target=_run))
        for t in self._threads:
            t.start()

    def update(self):
        super(ThreadSinkRunner, self).update()
        if self.status == DRAINED:
            self.status = READY
            self._node.set_drained()
        if self._node.get_drained() == self._nth:
            self.status = DRAINED

    def finalize(self):
        for t in self._threads:
            t.join()


import ROOT
from ROOT import gStyle, gPad
import tdrstyle
from optimizer import makeCutROC, makeComparisons
import L1Trigger.L1EGRateStudies.trigHelpers as trigHelpers
from drawRateEff import drawDRHists, draw2DdeltaRHist, loadHists

""" Use this script to make a slew of standard control plots
    for the L1 EG Crystal Algo """

gStyle.SetOptStat(0)
ROOT.gROOT.SetBatch(True)

date = '20170508v3'
#date = '20170503v1'
date = 'v2'
newEffFileName = 'r2_phase2_singleElectron_%s.root' % (date)
newPhotonFileName = 'r2_phase2_singlePhoton_%s.root' % (date)
newRateFileName = 'r2_phase2_minBias_%s.root' % (date)

rateFile = ROOT.TFile( newRateFileName, 'r' )
effFile = ROOT.TFile( newEffFileName, 'r' )

#crystal_tree = effFile.Get("analyzer/crystal_tree")
#rate_tree = rateFile.Get("analyzer/crystal_tree")
#''' Do 2D color plots 1st b/c of TDR style '''
## 1) 2D delta Eta vs delta Phi plot
#dynCrystal2DdeltaRHist = effFile.Get("analyzer/dyncrystalEG_2DdeltaR_hist")
#c = ROOT.TCanvas('c', 'c', 800, 700)
#c.SetName("dyncrystalEG_2D_deltaR_electron")
#c.SetTitle("")
#draw2DdeltaRHist(dynCrystal2DdeltaRHist, c)
#
## 2) 2D pt resolution vs. gen pt
##recoGenPtHist = effFile.Get("analyzer/reco_gen_pt")
#recoGenPtHist = ROOT.TH2D('recoGenPtHist','recoGenPtHist',50,0,50,30,-0.3,0.3)
#crystal_tree.Draw('(cluster_pt-gen_pt)/gen_pt:gen_pt >> recoGenPtHist')
#tdrRecoGenPtHist = effFile.Get("analyzer/l1extraParticlesUCT:All_reco_gen_pt")
#
#''' Track to cluster reco resolution '''
#c.SetCanvasSize(1200,600)
#c.Divide(2)
#title1 = "L1EGamma Crystal (Electrons)"
#title2 = "L1EGamma Crystal (Fake)"
#c.Clear()
#
#recoGenPtHist.SetTitle("Electron: Crystal EG algorithm pT resolution")
#oldAlgRecoGenPtHist = effFile.Get("analyzer/l1extraParticlesUCT:All_reco_gen_pt")
#oldAlgRecoGenPtHist.SetTitle("Electron: Tower EG alg. momentum error")
#oldAlgRecoGenPtHist.GetYaxis().SetTitle("Relative Error (reco-gen)/gen")
#oldAlgRecoGenPtHist.SetMaximum(50)
#oldAlgRecoGenPtHist.SetLineColor(ROOT.kRed)
#c.SetCanvasSize(1200,600)
#c.Divide(2,1)
#c.cd(1)
#gPad.SetGridx(1)
#gPad.SetGridy(1)
#recoGenPtHist.Draw("colz")
#recoGenPtHist.GetYaxis().SetTitleOffset(1.4)
#c.cd(2)
#gPad.SetGridx(1)
#gPad.SetGridy(1)
#oldAlgRecoGenPtHist.Draw("colz")
#oldAlgRecoGenPtHist.GetYaxis().SetTitleOffset(1.4)
#c.Print("plots/reco_gen_pt_electron.png")
#del c

canvasSize = 800

eTree = effFile.Get("analyzer/crystal_tree")
rTree = rateFile.Get("analyzer/crystal_tree")

tdrstyle.setTDRStyle()
c = ROOT.TCanvas('c','c',canvasSize,canvasSize)

from loadCuts import getCutMap
cutMap = getCutMap()
Isolation9 = cutMap['90x500MeV']['isolation']
showerShapes9 = cutMap['90x500MeV']['showerShape']
cut_ss_cIso9 = showerShapes9+"*"+Isolation9
cut_with_e2x2OverE2x5Window = cut_ss_cIso9+"*((e2x2/e2x5)>0.9)"
cut_ss_cIso_TrkMatch = cut_ss_cIso9+"*(trackDeltaR<.1)"

cut_none = "(1.)"
cut_ss = showerShapes9

rocAry = [1000, 0.0, 1.]
#rocAry = [200, 0.0, .3]
textR = "Scanning 0.0 < #DeltaR(Trk, L1EG) < 1.0"
#rebase20 = "(cluster_pt>20)*(abs(cluster_pt-trackPt)/cluster_pt < 0.5)"
rebase20 = "(cluster_pt>20)"
rebase30 = "(cluster_pt>30)"
rebaseAll = cut_ss_cIso9

#makeCutROC( "testDRCutsBaselineCuts20_ss_cIso", eTree, rTree, "trackDeltaR", rocAry, cut_ss_cIso9+"*"+rebase20, rebaseAll+"*"+rebase20, textR )
makeComparisons( cut_ss_cIso9, "E_e2x5OverE5x5 Iso", False, ["",""], 'cluster_pt' )
makeComparisons( cut_with_e2x2OverE2x5Window, "E_e2x5OverE5x5 Iso PhoWindow", False, ["",""], 'cluster_pt' )
makeComparisons( cut_ss_cIso_TrkMatch, "E_e2x5OverE5x5 Iso TrkMatch", False, ["",""], 'cluster_pt' )

#gStyle.SetOptStat(0)
#gStyle.SetTitleFont(42, "p")
#gStyle.SetTitleColor(1)
#gStyle.SetTitleTextColor(1)
#gStyle.SetTitleFillColor(10)
#gStyle.SetTitleFontSize(0.05)
#gStyle.SetTitleFont(42, "XYZ")
#gStyle.SetLabelFont(42, "XYZ")
#
#
#effMap = {
#    'newAlgDRHist' : ('L1EGamma Crystal', 'analyzer/dyncrystalEG_deltaR'),
#    'newAlgDEtaHist' : ('L1EGamma Crystal', 'analyzer/dyncrystalEG_deta'),
#    'newAlgDPhiHist' : ('L1EGamma Crystal', 'analyzer/dyncrystalEG_dphi'),
#    'UCTAlgDRHist' : ('Phase 1 TDR', 'analyzer/l1extraParticlesUCT:All_deltaR'),
#    'UCTAlgDEtaHist' : ('Phase 1 TDR', 'analyzer/l1extraParticlesUCT:All_deta'),
#    'UCTAlgDPhiHist' : ('Phase 1 TDR', 'analyzer/l1extraParticlesUCT:All_dphi'),
#}
#
#effHistsKeys = trigHelpers.getKeysOfClass( effFile, "analyzer", "TGraphAsymmErrors")
#hists = loadHists( rateFile )
#effHists = loadHists( effFile, effMap )
#
#
#
#gStyle.SetOptStat(0)
#xrange = [0., 50.]
#c = ROOT.TCanvas('c', 'c', 800, 600)
#
#''' POSITION RECONSTRUCTION '''
## Delta R Stuff
#c.SetGridx(0)
#c.SetGridy(0)
#c.SetName("dyncrystalEG_deltaR")
#c.SetTitle("")
#drawDRHists([effHists['newAlgDRHist'], effHists['UCTAlgDRHist']], c, 0.)
#
## Delta Eta / Phi
#c.SetName("dyncrystalEG_deltaEta")
#drawDRHists([effHists['newAlgDEtaHist'], effHists['UCTAlgDEtaHist']], c, 0.)
#c.SetName("dyncrystalEG_deltaPhi")
#drawDRHists([effHists['newAlgDPhiHist'], effHists['UCTAlgDPhiHist']], c, 0.)


#Place a route on a labeled Brandeis map and save the image to a file.
#Usage may be any of:
#   python Display.py
#      (defaults to use BrandeisMapLabeledCropped.jpg)
#   python Display.py jpg
#      (defaults to use BrandeisMapLabeledCropped.jpg)
#   python Display.py png
#      (defaults to use BrandeisMapLabeledCropped.png)
#   python Display.py BrandeisMapLabeled.png
#   python Display.py BrandeisMapLabeled.jpg
#   python Display.py BrandeisMapLabeledCropped.png
#   python Display.py BrandeisMapLabeledCropped.jpg

import sys
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.image as img
import matplotlib.colors as colors
import matplotlib.cm as cmx
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np
from itertools import cycle
cycol = cycle('bgrcmk').next

DATA = np.random.rand(5,5)
cmap = plt.cm.jet

cNorm  = colors.Normalize(vmin=np.min(DATA[:,4]), vmax=np.max(DATA[:,4]))

scalarMap = cmx.ScalarMappable(norm=cNorm,cmap=cmap)

MapName = 'BrandeisMapLabeledCropped.jpg'
if (len(sys.argv) > 1): MapName = sys.argv[1]
if (MapName=='jpg'): MapName='BrandeisMapLabeledCropped.jpg'
if (MapName=='png'): MapName='BrandeisMapLabeledCropped.png'

if ( (MapName != 'BrandeisMapLabeled.jpg') and \
     (MapName != 'BrandeisMapLabeled.png') and \
     (MapName != 'BrandeisMapLabeledCropped.jpg') and \
     (MapName !='BrandeisMapLabeledCropped.png') ):
     print ('\nIllegal map name - "' + MapName + '"\n')
     exit()

if ( (MapName=='BrandeisMapLabeled.png') or (MapName=='BrandeisMapLabeled.jpg') ):
   RouteData = 'Route.txt'
   RouteImage = 'Route.png'
if ( (MapName=='BrandeisMapLabeledCropped.png') or (MapName=='BrandeisMapLabeledCropped.jpg') ):
   RouteData = 'RouteCropped.txt'
   RouteImage = 'RouteCropped.png'

Brandeis = img.imread(MapName)

solution = open(RouteData).read().splitlines()

points = []
for line in solution:
    points.append(map(int, line.split()))

fig = plt.figure(figsize=(int(Brandeis.shape[1]/100),int(Brandeis.shape[0]/100)))
ax = fig.add_subplot(111, aspect='equal')
ax.imshow(Brandeis, alpha=1.0)

value = len(points);
step = 0.0;
count = 0;
for n in points:
    count = count + 1;
    # colorVal = scalarMap.to_rgba(DATA[idx,4])
    step = (step + 1.0) % 20.0
    if (step >= 19.0):
      value = value - step
    color = np.random.rand(3,1)
    color[0] = (value / len(points))
    color[1] = (1 - value / len(points))
    color[2] = (1 - value / len(points))
    # color = 'red'

    p = patches.FancyArrowPatch((n[0],n[1]), (n[2],n[3]),
      connectionstyle="arc3,rad=.2",
      ec=color,
      arrowstyle='->,head_width=10,head_length=15',
      lw=5
    )
    ax.add_patch(p)
    if (count < 10):
      ax.annotate(count, xy=(n[0],n[1]), xytext=(n[2],n[3]),fontsize=30)
    # ax.arrow(n[0],n[1],n[2]-n[0],n[3]-n[1],ec=np.random.rand(3,1), lw=4, width = 5, head_width=15,head_length=15)
_ = plt.axis('off')
# plt.show()
plt.tight_layout()
plt.savefig(RouteImage, bbox_inces='tight')

print
print('BrandeisMap.Map used = ' + MapName)
print('BrandeisMap.Route BrandeisMap.data used = ' + RouteData)
print('Output image = ' + RouteImage)
print


"""Module for Diagnosis Plots of Lineare Regression Models."""

# Extra Libs
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import statsmodels.api as sm
from statsmodels.stats.outliers_influence import summary_table, OLSInfluence
from statsmodels.nonparametric.smoothers_lowess import lowess
from scipy.stats import norm

# Global Style Variables
title_size = 15
edge_col = (0, 0, 0, 0.6)

# VERIFICATIONS
# ----------------

def verify_input(lm):
    """Check if input parameter is an linear regression model."""
    if not isinstance(lm, sm.regression.linear_model.RegressionResultsWrapper):
        raise TypeError('Input is no statsmodel OLS model!')
    return

# GATHERING VALUES
# ----------------

def get_residuals(lm):
    tbl, data, labels = summary_table(lm, alpha=0.05)
    residuals = data[:, 8]
    return residuals

def get_fitted_values(lm):
    """Return 1-D numpy array with fitted values."""
    fitted = lm.fittedvalues
    # Transform series to 1-d array, if necessary
    if isinstance(fitted, pd.Series):
        fitted = fitted.values
    return fitted

def get_standard_residuals(lm):
    vals = OLSInfluence(lm).summary_frame()
    std_resid = vals['standard_resid'].values
    return std_resid

def get_sqrt_abs_residuals(lm):
    """Return sqrt(|Standardized resiudals|)."""
    std_resid = get_standard_residuals(lm)
    sqrt_abs_res = np.sqrt(np.abs(std_resid))
    return sqrt_abs_res

def get_normalized_quantiles(lm):
    val_count = len(lm.fittedvalues)
    positions = (np.arange(1.0, val_count + 1))/(val_count + 1.0)
    norm_quantiles = norm.ppf(positions)
    return norm_quantiles

def get_cooks_d(lm):
    vals = OLSInfluence(lm).summary_frame()
    cooks_d = vals['cooks_d'].values
    return cooks_d

# DRAWING CHARTS
# ---------------

def resid_fit(lm):
    """Draw Residuals vs. Fitted Values Plot."""
    verify_input(lm)

    # Calculate values for scatter points
    fitted = get_fitted_values(lm)
    residuals = get_residuals(lm)

    # Calculate lowess for smoothing line
    grid, yhat = lowess(residuals, fitted).T

    # Get top three observations for annotation
    top_3 = np.abs(residuals).argsort()[-3:][::1]

    # Draw scatter and lowess line
    plt.plot([fitted.min(), fitted.max()], [0, 0], 'k:')
    plt.plot(grid, yhat, 'r-')
    plt.plot(fitted,
             residuals,
             'o',
             mec=edge_col,
             markeredgewidth=1,
             fillstyle='none')

    # Draw Annotations
    for point in top_3:
        plt.annotate(point,
                     xy=(fitted[point], residuals[point]),
                     color='r')

    # Set Labels
    plt.title('Residual vs. Fitted', fontsize=title_size)
    plt.xlabel('Fitted values')
    plt.ylabel('Residuals')

    return plt

def q_q(lm):
    """Draw Q-Q-Plot."""
    verify_input(lm)

    # Calulate values for scatter points
    std_resid = get_standard_residuals(lm)
    quantiles = get_normalized_quantiles(lm)

    # Sort for Q-Q plot
    std_resid_sort = np.sort(std_resid)
    quantiles_sort = np.sort(quantiles)

    # Function for fitted line
    fit = np.polyfit(quantiles_sort, std_resid_sort, deg=1)

    # Get top three observations for annotation
    # (need position of sorted for coord, and original for label)
    top_3_sorted = np.abs(std_resid_sort).argsort()[-3:][::1]
    top_3_orig = np.abs(std_resid).argsort()[-3:][::1]
    top_3 = zip(top_3_sorted, top_3_orig)

    # Draw scatter and fitted line
    plt.plot(quantiles_sort, fit[0] * quantiles_sort + fit[1], 'r:')
    plt.plot(quantiles_sort,
             std_resid_sort,
             'o',
             mec=edge_col,
             markeredgewidth=1,
             mfc='none')

    # Draw Annotations
    for point in top_3:
        plt.annotate(point[1],
                     xy=(quantiles_sort[point[0]], std_resid_sort[point[0]]),
                     color='r')

    # Set Labels
    plt.title('Normal Q-Q', fontsize=title_size)
    plt.xlabel('Theoretical Quantiles')
    plt.ylabel('Standardized residuals')

    return plt

def scale_loc(lm):
    """Draw Scale-Location Plot."""
    verify_input(lm)

    # Get Fitted Values
    fitted_vals = get_fitted_values(lm)
    sqrt_abs_res = get_sqrt_abs_residuals(lm)

    # Get top three observations for annotation
    top_3 = sqrt_abs_res.argsort()[-3:][::1]

    # Calculate lowess for smoothing line
    grid, yhat = lowess(sqrt_abs_res, fitted_vals).T

    # Draw scatter and lowess line
    plt.plot(grid, yhat, 'r-')
    plt.plot(fitted_vals,
             sqrt_abs_res,
             'o',
             mec=edge_col,
             markeredgewidth=1,
             fillstyle='none')

    # Draw Annotations
    for point in top_3:
        plt.annotate(point,
                     xy=(fitted_vals[point], sqrt_abs_res[point]),
                     color='r')

    # Set Labels
    plt.title('Scale-Location', fontsize=title_size)
    plt.xlabel('Fitted values')
    plt.ylabel(r'$\sqrt{\left|Standardized\ residuals\right|}$')

    return plt

def resid_lev(lm):
    """Draw Stanardized Residuals vs. Leverage Plot."""
    verify_input(lm)

    # Get stanardized residuals & cooks distance
    std_resid = get_standard_residuals(lm)
    cooks_d = get_cooks_d(lm)

    # Get top three observations for annotation
    top_3 = cooks_d.argsort()[-3:][::1]

    # Get Leverage
    infl = lm.get_influence()
    leverage = infl.hat_matrix_diag

    # Get Cooks Distance contour lines
    x = np.linspace(leverage.min(), leverage.max(), 100)
    params_len = len(lm.params)

    # Calculate lowess for smoothing line
    grid, yhat = lowess(std_resid, leverage).T

    # Draw cooks distance contours, scatter and lowess line
    plt.plot(x, np.sqrt((0.5 * params_len * (1 - x)) / x), 'r--')
    plt.plot(x, np.sqrt((1.0 * params_len * (1 - x)) / x), 'r--')
    plt.plot(x, np.negative(np.sqrt((0.5 * params_len * (1 - x)) / x)), 'r--')
    plt.plot(x, np.negative(np.sqrt((1.0 * params_len * (1 - x)) / x)), 'r--')
    plt.plot(grid, yhat, 'r-')
    plt.plot(leverage,
             std_resid,
             'o',
             mec=edge_col,
             markeredgewidth=1,
             fillstyle='none')

    # Limit y axis to actual values (otherwise contour lines disturb scale)
    plt.ylim(std_resid.min() * 1.1, std_resid.max() * 1.1)

    # Draw Annotations
    for point in top_3:
        plt.annotate(point, xy=(leverage[point], std_resid[point]), color='r')

    # Set Labels
    plt.title('Residuals vs. Leverage', fontsize=title_size)
    plt.xlabel('Leverage')
    plt.ylabel('Standardized residuals')

    return plt

def plot(lm):
    """Plot all 4 charts as a Matrix."""
    # Draw plot by plot
    plt.subplot(2, 2, 1)
    resid_fit(lm)

    plt.subplot(2, 2, 2)
    q_q(lm)

    plt.subplot(2, 2, 3)
    scale_loc(lm)

    plt.subplot(2, 2, 4)
    resid_lev(lm)

    # Padding between Charts
    plt.tight_layout(pad=0.5, w_pad=4, h_pad=4)

    return plt


#Exercise 1: Write a function called chop that takes a list and modifies it,
# removing the first and last elements, and returns None.
# Then write a function called middle that takes a list
# and returns a new list that contains all but the first and last elements.

def chop(t):
    del t[0] #remove first char
    del t[-1] #remove last char

def middle(t):
    new_list = t[1:] #create new list start from 2nd char (index 1)
    #print(new_list)
    del new_list[-1] #remove last char from newly created list
    return new_list

my_list = ['a', 'b', 'c', 'd']
my_list2 = ['a', 'b', 'c', 'd']

print(chop(my_list))
print(middle(my_list2))


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

from mxnet import ndarray as nd
from mxnet import autograd
from mxnet import gluon
import random
import matplotlib.pyplot as plt

num_train = 20
num_test = 100
num_inputs = 200

true_w = nd.ones((num_inputs,1)) * 0.01
true_b = 0.05

X = nd.random.normal(shape = (num_train+num_test, num_inputs))
y = nd.dot(X,true_w) + true_b
y += 0.01 * nd.random.normal(shape = y.shape)

X_train, X_test = X[:num_train, :], X[num_train:, :]
y_train, y_test = y[:num_train], y[num_train:]

batch_size = 1
dataset_train = gluon.data.ArrayDataset(X_train,y_train)
data_iter = gluon.data.DataLoader(dataset_train,batch_size,shuffle=True)

def train(weight_decay):

    net = gluon.nn.Sequential()
    with net.name_scope():
        net.add(gluon.nn.Dense(1))
    net.initialize()

    square_loss = gluon.loss.L2Loss()

    trainer = gluon.Trainer(net.collect_params(),"sgd",{'learning_rate':0.001,'wd': weight_decay})

    train_loss = []
    test_loss = []
    epochs = 50
    for e in range(epochs):
        for data, label in data_iter:
            with autograd.record():
                output = net(data)
                # print 'output', output
                loss = square_loss(output, label)
            loss.backward()
            trainer.step(batch_size)
        train_loss.append(square_loss(net(X_train),y_train).mean().asscalar())
        test_loss.append(square_loss(net(X_test), y_test).mean().asscalar())

    plt.plot(train_loss)
    plt.plot(test_loss)
    plt.legend(['train', 'test'])
    plt.show()

    print ('learned w[:10]:', net[0].weight.data(),
     'learned b:', net[0].bias.data())

train(0)
train(5)


# -- coding utf-8 --
import numpy as np
import matplotlib.pyplot as plt
import math

#parameter
g = 9.8
l = 9.8
q = 0.5
omega_d = 2.0/3.0
dt = 0.04
fd = 1.5
theta = 0.2
omega = 0
t = 0
k = 0
o_array = []
t_array = []
i=0

def reduce_theta(x):
	if x > math.pi:
		return reduce_theta(x - 2*math.pi)
	elif x < -math.pi:
		return reduce_theta(x + 2*math.pi)
	else:
		return x

while i<500000:
	if abs(t-(2*k*math.pi+math.pi/4)/omega_d)<0.02:
		o_array.append(omega)
		t_array.append(theta)
		k += 1

	omega = omega + (-g/l*math.sin(theta)-q*omega + fd*math.sin(omega_d*t))*dt
	theta += omega*dt
	theta = reduce_theta(theta)
	t += dt
	i += 1

plt.plot(t_array,o_array,'.k')
plt.text(2.5,1.2,u'$\u03C9$ versus $\u03B8$   Fd=1.5',color='black',ha='center',fontsize=20)
plt.text(2.5,1,u'$\u03c0$/4 ',color='black',ha='center',fontsize=20)
plt.title('',fontsize=28)
plt.xlabel(u'$\u03C9(omega)$',fontsize=20)
plt.ylabel(u'$\u03B8(theta)$',fontsize=20)
plt.show()


import tkinter as tk
root = tk.Tk()
from tkinter import *
import threading
import time

root.geometry("720x100")
root.title("Countdown Tester")

stop = threading.Event()

def reset():
    button["text"] = "Begin Countdown"
    label["text"] = "Countdown Finished"
    label.config(bg='dark red', fg='white')
    label2["text"] = ""
    label2.config(bg='black', fg='white')
    root.timer = root.after(1000, label.update)
    root.update_idletasks()
    stop.set()

def _5():
    label2["text"] = "5"
    label2.config(bg='orange', fg='black')
    root.update_idletasks()
    time.sleep(1)

def _4():
    label2["text"] = "4"
    label2.config(bg='orange', fg='black')
    root.update_idletasks()
    time.sleep(1)

def _3():
    label2["text"] = "3"
    label2.config(bg='orange', fg='black')
    root.update_idletasks()
    time.sleep(1)

def _2():
    label2["text"] = "2"
    label2.config(bg='orange', fg='black')
    root.update_idletasks()
    time.sleep(1)

def _1():
    label2["text"] = "1"
    label2.config(bg='orange', fg='black')
    root.update_idletasks()
    time.sleep(1)

def _0():
    label2["text"] = "0"
    label2.config(bg='orange', fg='black')
    root.update_idletasks()
    time.sleep(1)

def script():
    if not stop.isSet():
        label["text"] = "Countdown Began"
        label.config(bg='blue', fg='white')
        root.update_idletasks()
    if not stop.isSet():
        _5()
    if not stop.isSet():
        _4()
    if not stop.isSet():
        _3()
    if not stop.isSet():
        _2()
    if not stop.isSet():
        _1()
    if not stop.isSet():
        _0()
        reset()

def toggle_text():
    if button["text"] == "Begin Countdown":
        button["text"] = "Stop Countdown"
        label["text"] = "Countdown Began"
        label.config(bg='green', fg='black')
        stop.clear()
        t1 = threading.Thread(target=script)
        t1.daemon = True
        t1.start()
    else:
        button["text"] = "Begin Countdown"
        label["text"] = "Countdown Stopped"
        label.config(bg='red', fg='white')
        label2["text"] = ""
        label2.config(bg='black', fg='white')
        stop.set()
    root.update_idletasks()

def on_exit():
    root.destroy()

canvas = Canvas(width=720, height=100, bg='black')
canvas.grid(rowspan=26, columnspan=20, sticky='W,E,N,S')

button = Button(root, text="Begin Countdown", font='-weight bold', width=17, command=toggle_text)
button.grid(padx=10, pady=10, row=10, column=0, sticky='W,E,N,S')
button2 = Button(root, text="   Quit   ", font='-weight bold', command=on_exit)
button2.grid(padx=5, pady=10, row=10, column=1, sticky='W,E,N,S')

label = Label(root, text=" ", width=20, font='-weight bold', background='black')
label.grid(padx=5, pady=10, row=10, column=2, sticky='W,E,N,S')
label2 = Label(root, text=" ", width=11, font='-weight bold', background='black')
label2.grid(padx=5, pady=10, row=10, column=3, sticky='W,E,N,S')

root.resizable(width=FALSE, height=FALSE)

root.mainloop()


import tensorflow as tf
from tensorflow import keras
import numpy as np
# from pipe import get_dataset
from tensorflow_core.python.data.experimental.ops.readers import make_csv_dataset
import json

class Model(object):
    def __init__(
        self,
        config_json="data/config.json",
        vocab2id_json="data/vocab.json",
        labels_json="data/labels.json",
    ):
        self.config = json.load(open(config_json, 'r', encoding="utf8"))
        vocab2id = json.load(open(vocab2id_json, 'r', encoding='utf8'))
        self.embeddingSize = len(vocab2id.keys())
        self.embeddingDim = 100

        self.maxseqlen = self.config["maxseqlen"]

        self.filters = 10
        self.kernelSize = [5]
        self.num_class = 14
        # self.filters_2 = 3

        self.batch_size = self.config["batch_size"]
        self.input_shape = [self.maxseqlen]

    def build(self):
        inputs = keras.layers.Input(shape=self.input_shape, name="inputs")
        self.ebdLayer = keras.layers.Embedding(self.embeddingSize,
                                               self.embeddingDim,
                                               mask_zero=True,
                                               name="embedding")(inputs)
        conv1d_layer_num = 4
        convdim = 100
        convblocks = []
        for i in range(1, conv1d_layer_num + 1):
            conv = keras.layers.Conv1D(filters=convdim,
                                       kernel_size=self.kernelSize,
                                       strides=1,
                                       activation="relu",
                                       name="conv1d" + str(i))(self.ebdLayer)
            conv = keras.layers.GlobalMaxPooling1D()(conv)
            conv = keras.layers.Dropout(rate=0.5)(conv)
            convblocks.append(conv)

        convout = keras.layers.concatenate(convblocks)
        output_layer = keras.layers.Dense(units=self.num_class,
                                          activation="relu",
                                          name="output_layer")(convout)
        self.model = keras.Model(inputs, output_layer)
        self.output = tf.argmax(output_layer, axis=-1, name="argmax")

        optimizer = keras.optimizers.Adam(learning_rate=0.001)
        self.model.compile(
            optimizer=optimizer,
            loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
            metrics=["accuracy"])

    def train(self, datasetpath, batchsize):
        # ds = make_csv_dataset(datasetpath,
        #                       batch_size=batchsize,
        #                       label_name="label",
        #                       field_delim="\t",
        #                       shuffle=True)
        # for data_batch, label_batch in ds:
        #     print(label_batch)
        #     self.model.fit(x=data_batch["text"],y=label_batch,batch_size=batch_size,epochs=)
        import pandas as pd
        import numpy as np
        data = pd.read_pickle("data/data.csv")
        self.model.summary()
        x_data = data["text"].tolist()
        y_label = data["label"].tolist()
        x_data_c = []
        y_label_c = []
        index = tf.random.shuffle(range(len(x_data)))
        for i in index:
            x_data_c.append(x_data[i])
            y_label_c.append(y_label[i])
        x_data = x_data_c
        y_label = y_label_c
        # print(y_label)
        # print(x_data)
        self.model.fit(x=x_data,
                       y=y_label,
                       batch_size=100,
                       epochs=20,
                       shuffle=True)
        out = self.model.predict(x=x_data)
        out = np.argmax(out, axis=-1)
        for i in range(len(out)):
            print(out[i], "***", y_label[i])

    def predict(self):
        pass

def main():
    MD = Model()
    MD.build()
    MD.train("data/data.csv", 5)

if __name__ == "__main__":
    main()


from __future__ import division
import numpy as np
import scipy as sc
from scipy.optimize import minimize_scalar
import functools

def XYZ2OE(XX,constants):
    XX=XX.reshape((-1,1))

    X=X0[0:3]
    V=X0[3:6]

    MU=constants['MU']

    rm = np.norm( X);
    vm = np.norm(V);

    a = 1/(2/rm - vm^2/mu)

    hbar = np.cross(X,V,axisa=0, axisb=0, axisc=0)
    cbar = np.cross(V,hbar,axisa=0, axisb=0, axisc=0)-MU*X/rm

    e = np.norm(cbar)/MU

    hm = np.norm(hbar)
    ih = hbar/hm;

    ie = cbar/(MU*e)

    ip = np.cross(ih,ie,axisa=0, axisb=0, axisc=0)

    i = np.arccos(ih[2,0])

    w = np.arctan2(ie[2,0],ip[2,0])

    Om = np.arctan2(ih[0,0],-ih[1,0])

    sig = np.dot(X.transpose(),V)/np.sqrt(MU)

    E = np.arctan2(sig/np.sqrt(a),1-rm/a);

    return np.array([a,e,E,w,i,Om]).reshape((-1,1))

def keplerSolver(M,e,constants):
    tol=constants['tol']

    # % Compute Eccentric Anomayl
    if (M > -np.pi and M < 0 or M > np.pi):
        E1 = M - e;
    elif (M < -np.pi or M > 0 and M < np.pi):
        E1 = M + e
    elif (M == 0):
        E1 = M + e

    E = E1 + (M - E1 + e*np.sin(E1))/(1 - e*np.cos(E1))

    while (abs(E - E1) > tol):
        E1 = E
        E = E1 + (M - E1 + e*sin(E1))/(1 - e*cos(E1))

    E = (E/(2*np.pi) - np.floor(E/(2*np.pi)))*2*np.pi

    f = 2*np.arctan2(np.sqrt(1+e)*np.tan(E/2),np.sqrt(1-e))

    return (E,f)

def OE2XYZ(Xorb,constants):
    tol=constants['tol']
    a,e,E,w,inc,Om=Xorb.reshape(1,-1)[0]
    M=E-e*np.sin(E)

    tol     = 1e-10

    p       = a*(1 - e^2)

    E,f = keplerSolver(M,e,constants)

    c_f     = np.cos(f);
    s_f     = np.sin(f);
    r_pqw   = np.divide(np.array([ p*c_f,p*s_f,0]) , (1 + e*c_f) ).reshape(-1,1)
    v_pqw   = np.multiply(np.array([ -s_f,(e + c_f),0]),np.sqrt(mu/p) ).reshape(-1,1)

    s_Om    = sin(Om);
    c_Om    = cos(Om);
    s_w     = sin(w);
    c_w     = cos(w);
    s_i     = sin(inc);
    c_i     = cos(inc);

    ROT  = [[c_Om*c_w-s_Om*s_w*c_i,-c_Om*s_w-s_Om*c_w*c_i,s_Om*s_i],
        [s_Om*c_w+c_Om*s_w*c_i,-s_Om*s_w+c_Om*c_w*c_i,-c_Om*s_i],
        [s_w*s_i,c_w*s_i,c_i]]

    ROT=np.array(ROT)

    r       = np.dot(ROT,r_pqw)
    v       = np.dot(ROT,v_pqw)
    X=np.vstack((r,v))

    return X

def orbit_propagate_kepler(t,X0,constants):
    X0=X0.reshape((-1,1))
    MU=constants['MU']

    OE0_mu = XYZ2OE(X0,MU);
    a0,ecc0,E0,w0,inc0,Omega0=OE0_mu.reshape(1,-1)[0]

    M0=E0-ecc0*np.sin(E0);
    # X= OE2XYZ(OE0_mu,constants);
    # Xorb0_mu=[r;v];

    n=np.sqrt(MU/a0^3);

    M=n*t + M0;
    E=keplerSolver(M,ecc0,constants);
    Xorb=np.array([a0,ecc0,E,w0,inc0,Omega0]).reshape(-1,1)
    X= OE2XYZ(Xorb,constants);
    return (X,Xorb)

def transcendentalfunc(y):
    if y>0:
        sqrty=np.sqrt(y)
        C=(1-np.cos( sqrty ))/y
        S=(np.sqrt(y)-np.sin(np.sqrt(y)))/np.sqrt(y^3)

    elif y<0:
        sqrtmy=np.sqrt(-y)
        C=(np.cosh(sqrtmy)-1)/(-y)
        S=(np.sinh(sqrtmy)-sqrtmy)/np.sqrt(-y^3)
    else:
       C=0.5
       S=0.1667
    return (C,S)

def univkepler_err(MU,alpha,sig0,normr0,t,t0,x):

    [C,S]=transcendentalfunc(alpha*x^2);
    err=sig0*x**2*C+(1-normr0*alpha)*x**3*S+normr0*x-np.sqrt(MU)*(t-t0);
    return err

def universalkepler(a,t0,X0,t,options):
    x=1
    F=functools.partial(univkepler_err,)
    x=minimize_scalar(F)

    return x

def forwardSTM(t,X0,computePHI,computeProb,probFunc0,options,constants):
    # tol=constants['tol']

    X0=X0.reshape((-1,1))
    MU=constants['MU']
    # function [Xt,Xtorb,X0,X0orb,ft,f0,PHI,p0,pt]=sim1_forwardSTM(t,X0,MU,computePHI,computeProb,probFunc0)

    r0=np.norm(X0[0:3])
    v0=np.norm(X0[3:6])

    r0vec=X0[0:3]
    v0vec=X0[3:6]

    X0orb = XYZ2OE(X0,constants)
    a0,ecc0,E0,w0,inc0,Omega0=X0orb.reshape(1,-1)[0]

    # M0=E0-ecc0*np.sin(E0)
    # X0orb=[a0,ecc0,E0,w0,inc0,Omega0]
    f0 = 2*np.arctan2(np.sqrt(1+ecc0)*np.tan(E0/2),np.sqrt(1-ecc0))

    # n=np.sqrt(MU/a0^3)

    Xt,Xtorb=orbit_propagate_kepler(t,X0,constants)
    a,ecc,E,w,inc,Omega=Xtorb.reshape(1,-1)[0]
    # M=E-ecc*np.sin(E)
    ft = 2*np.arctan2(np.sqrt(1+ecc)*np.tan(E/2),np.sqrt(1-ecc))

    if options['computePHI']==False:
        PHI=np.nan
    else:
        rtvec=Xt[0:3]
        vtvec=Xt[3:6]
        rt=np.norm(rtvec)
        vt=np.norm(vtvec)

        th=ft-f0
        sig0=np.dot(r0vec.transpose(),v0vec)/np.sqrt(MU)
        p=a*(1-ecc^2)
        r=p*r0/( r0+(p-r0)*np.cos(th)-np.sqrt(p)*sig0*np.sin(th) )

        F=1-(r/p)*(1-np.cos(th))
        G=r*r0*np.sin(th)/np.sqrt(MU*p)
        Ft=np.sqrt(MU)/(r0*p)*(sig0*(1-np.cos(th))-np.sqrt(p)*np.sin(th))
        Gt=1-f0/p*(1-np.cos(th))

        Xfg=np.vstack((F*X0[0:3]+G*X0[3:6],Ft*X0[0:3]+Gt*X0[3:6]))

        # % PHI=[F,G;Ft,Gt]
        t0=0
        x=universalkepler(a,t0,X0,t,options)
        alpha=1/a
        C,S=transcendentalfunc(alpha*x**2)

        Q=x^3/alpha*( C-3*S)-np.sqrt(MU)*(t-t0)*x**2*C
        Q=Q/np.sqrt(MU)

        R=r0/MU*(1-F)*( (rtvec-r0vec)*v0vec.transpose()-(vtvec-v0vec)*r0vec.transpose()  )+Q/MU*vtvec*v0vec.transpose()+G*np.identity(3)
        V=r0/MU*(vtvec-v0vec)*(vtvec-v0vec).transpose()+1/rt^3*( r0*(1-F)*rtvec*r0vec.transpose()-Q*rtvec*v0vec.transpose() )+Gt*np.identity(3)
        Rt=rt/MU*(vtvec-v0vec)*(vtvec-v0vec).transpose()+1/r0^3*(r0*(1-F)*rtvec*r0vec.transpose()+Q*vtvec*r0vec.transpose() )+F*np.identity(3)
        Vt=(-1/r0^2*(vtvec-v0vec)*r0vec.transpose()-1/rt^2*rtvec*(vtvec-v0vec).transpose()+Ft*( np.identity(3) - 1/rt^2*rtvec*rtvec.transpose()+
            1/(MU*rt)*(rtvec*vtvec.transpose()-vtvec*rtvec.transpose())*rtvec*(vtvec-v0vec).transpose())-MU*Q*rtvec*r0vec.transpose()/(rt^3*r0^3) )

        PHI=np.block([[Rt,R],[Vt,V]])

    if options['computeProb']==False:
        p0=np.nan
        pt=np.nan
    else:
       p0=probFunc0(X0)
       pt=p0/abs(np.linalg.det(PHI))

    return (Xt,Xtorb,X0,X0orb,ft,f0,PHI,p0,pt)


# importing the Kratos Library
import KratosMultiphysics as KM
import KratosMultiphysics.ShallowWaterApplication as SW

## Import base class file
from KratosMultiphysics.ShallowWaterApplication.stabilized_shallow_water_solver import StabilizedShallowWaterSolver

def CreateSolver(model, custom_settings):
    return FCTShallowWaterSolver(model, custom_settings)

class FCTShallowWaterSolver(StabilizedShallowWaterSolver):

    def AddVariables(self):
        super().AddVariables()
        self.main_model_part.AddNodalSolutionStepVariable(KM.INTERNAL_ENERGY)

    def Initialize(self):
        super().Initialize()
        self.main_model_part.ProcessInfo.SetValue(KM.DOMAIN_SIZE, 2)
        self.fct_utility = SW.AlgebraicFluxCorrectionUtility(self.main_model_part, self.settings["afc_parameters"])

    def InitializeSolutionStep(self):
        if self._TimeBufferIsInitialized():
            self.__execute_before_low_order_step()
            super().InitializeSolutionStep()

    def SolveSolutionStep(self):
        if self._TimeBufferIsInitialized():
            is_converged = self._GetSolutionStrategy().SolveSolutionStep()
            self.__execute_after_low_order_step()
            self.__execute_before_high_order_step()
            is_converged = self._GetSolutionStrategy().SolveSolutionStep()
            return is_converged
        else:
            return True

    def FinalizeSolutionStep(self):
        if self._TimeBufferIsInitialized():
            super().FinalizeSolutionStep()
            self.__execute_after_high_order_step()

    @classmethod
    def GetDefaultParameters(cls):
        afc_parameters = KM.Parameters("""{
            "name"               : "algebraic_flux_correction_utility",
            "limiting_variables" : ["VARIABLE_NAME"],
            "maximum_iterations" : 1,
            "rebuild_level"      : 0
        }""")
        default_parameters = super().GetDefaultParameters()
        default_parameters.AddValue("afc_parameters", afc_parameters)
        return default_parameters

    def __execute_before_low_order_step(self):
        self.main_model_part.ProcessInfo.SetValue(SW.LUMPED_MASS_FACTOR, 1.0)
        self.main_model_part.ProcessInfo.SetValue(SW.IS_MONOTONIC_CALCULATION, True)
        dry_height = self.main_model_part.ProcessInfo.GetValue(SW.DRY_HEIGHT)
        SW.ShallowWaterUtilities().ResetDryDomain(self.main_model_part, dry_height)
        SW.ShallowWaterUtilities().IdentifyWetDomain(self.main_model_part, KM.ACTIVE, dry_height)
        self.fct_utility.InitializeCorrection()

    def __execute_after_low_order_step(self):
        self.fct_utility.GetLowOrderValues()

    def __execute_before_high_order_step(self):
        self.main_model_part.ProcessInfo.SetValue(SW.LUMPED_MASS_FACTOR, 0.0)
        self.main_model_part.ProcessInfo.SetValue(SW.IS_MONOTONIC_CALCULATION, False)
        dry_height = 10 * self.main_model_part.ProcessInfo.GetValue(SW.DRY_HEIGHT)
        SW.ShallowWaterUtilities().IdentifyWetDomain(self.main_model_part, KM.ACTIVE, dry_height)

    def __execute_after_high_order_step(self):
        self.fct_utility.GetHighOrderValues()
        self.fct_utility.ApplyCorrection()


"""
Задание 2.

Доработайте пример структуры "дерево",
рассмотренный на уроке.

Предложите варианты доработки и оптимизации
(например, валидация значений узлов в соответствии с требованиями для бинарного дерева)

Поработайте с доработанной структурой, позапускайте на реальных данных - на клиентском коде.
"""

class BinaryTree:
    def __init__(self, root_obj):
        self.root = root_obj
        self.left_child = None
        self.right_child = None

    def __str__(self):
        return str(self.root)

    def insert_left(self, new_node):
        if self.root >= new_node:
            if self.left_child is None:
                self.left_child = BinaryTree(new_node)
            else:
                tree_obj = BinaryTree(new_node)
                tree_obj.left_child = self.left_child
                self.left_child = tree_obj
        else:
            print('Значение слишком велико')

    def insert_right(self, new_node):
        if self.root <= new_node:
            if self.right_child is None:
                self.right_child = BinaryTree(new_node)
            else:
                tree_obj = BinaryTree(new_node)
                tree_obj.right_child = self.right_child
                self.right_child = tree_obj
        else:
            print('Значение слишком мало')

    def get_right_child(self):
        try:
            return self.right_child
        except AttributeError:
            print('Ошибка доступа к атрибуту класса.')

    def get_left_child(self):
        try:
            return self.left_child
        except AttributeError:
            print('Ошибка доступа к атрибуту класса.')

    def set_root_val(self, obj):
        self.root = obj

    def get_root_val(self):
        try:
            return self.root
        except AttributeError:
            print('Ошибка доступа к атрибуту класса.')

r = BinaryTree(14)
print(r.get_root_val())
print(r.get_left_child())
r.insert_left(40)
r.insert_left(5)
print(r.get_left_child())
print(r.get_left_child().get_root_val())
r.insert_right(12)
r.insert_right(17)
print(r.get_right_child())
print(r.get_right_child().get_root_val())
r.get_right_child().set_root_val(16)
print(r.get_right_child().get_root_val())

Урок 8. Практическое задание/Lesson 8-2.py


#
# CommandManager.py
# Botpy
#
# Created by Ashish Ahuja on 4th September 2017.
#
#

import chatexchange as ce
import threading

class CommandManager:
    def __init__(self, commands):
        self.commands = commands
        self.running_commands = []

    def run_command(self, command):
        if command.privileges() == 0:
            command_thread = threading.Thread(target=command.run)
            self.running_commands.append([command, command_thread])
            command_thread.start()
            return

        if command.message.room.is_user_privileged(command.message.user.id, command.privileges()):
            command_thread = threading.Thread(target=command.run)
            self.running_commands.append([command, command_thread])
            command_thread.start()
            return

        command.reply("You do not have sufficient privileges to run this command.")

    def handle_command(self, message):
        try:
            message_content = message.content.lower().split()
            del message_content[0]
        except AttributeError:
            return

        for command in self.commands:
            command_usage = command.usage()

            usage_index = -1

            for usage in command_usage:
                usage_index += 1
                usage_components = usage.split()
                args = []
                match = True
                last_index = min(len(usage_components), len(message_content))

                for i in range(last_index):
                    content_component = message_content[i]
                    usage_component = usage_components[i]

                    if usage_component == '*':
                        args.append(content_component)
                    elif usage_component == '...':
                        #Everything else is arguments
                        temp_index = i
                        while temp_index < len(message_content):
                            args.append(message_content[temp_index])
                            temp_index += 1
                    elif content_component != usage_component:
                        match = False

                    min_count = (len(usage_components) - 1) if (usage_components[-1] == '...') else len(usage_components)
                    if len(message_content) < min_count:
                        match = False
                if match:
                    self.run_command(command(self, message, args, usage_index))
                    return

    def cleanup_finished_commands(self):
        for command, command_thread in self.running_commands:
            if not command_thread.isAlive():
                self.running_commands.remove([command, command_thread])


# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models

class Migration(migrations.Migration):
    dependencies = [
        ('events', '0024_speakerlist'),
    ]

    operations = [
        migrations.AddField(
            model_name='event',
            name='extra_deadline',
            field=models.DateTimeField(null=True, blank=True, verbose_name='extra anmälningsstopp',
                                       help_text='Exempelvis: Datum att anmäla sig innan för att få mat.'),
        ),
    ]


from TrackLanesClass import TrackLanes
from pandac.PandaModules import *

class Track:
	def __init__(self):
		self.track = loader.loadModel("../Models/Track.egg")
		self.track.reparentTo(render)

		self.trackLanes = TrackLanes()

		self.gravity = 1
		self.groundCol = loader.loadModel("../Models/Ground.egg")
		self.groundCol.reparentTo(render)
		mask = BitMask32.range(1,3)
		mask.clearRange(2,1)
		self.groundCol.setCollideMask(mask)