from enum import Enum
from telemetrix import telemetrix
from fes import FES
import logging

CtrlState = Enum('CtrlState', 'off statecheck charging discharging ')
BtryMainSwitchState = Enum('BtryMainSwitchState', 'off on conn_failed')

#off:           the battery controller is switched off
#statecheck:
#charging
#discharging

class BatteryControl(object):
    def __init__(self, name, board, chargerPin, disChargerPin, mainSwitchServoPin, usbCtrlCable, usbCableSpeed, mockBattery=False):
        self.name=name
        self.batteryId=None
        self.chargerPin=chargerPin
        self.disChargerPin=disChargerPin
        self.mainSwitchServoPin=mainSwitchServoPin
        self.ctrlstate=CtrlState.off
        self.btrystate=BtryMainSwitchState.off
        self.switchServoPos=0
        self.targetSOC=50
        self.currentSOC=-1
        self.socFailCount=0
        self.currentValue={}
        self.valueReadFailCount={}
        self.operationExceptions={}
        self.mockBattery=mockBattery
        self.usbCtrlCable=usbCtrlCable
        self.usbCableSpeed=usbCableSpeed
#        sel

        self.board=board
        self.battery=None
        self.initPin()
        self.initBoard()

    def __repr__(self):
        return f"BatteryControl: name: {self.name}, ctrlstate: {self.ctrlstate}, btrystate: {self.btrystate}, targetSOC: {self.targetSOC}, currentSOC: {self.currentSOC}"

    def getState(self):
        state={'ctrlstate': str(self.ctrlstate), 'btrystate': str(self.btrystate), 'targetSOC': self.targetSOC, 'currentSOC': self.currentSOC, 'socFailCount': self.socFailCount, 'valueReadFailCount': self.valueReadFailCount, 'operationExceptions': self.operationExceptions}
        return state

    def getTelemetric(self):
        if (self.btrystate == BtryMainSwitchState.on and self.battery != None):
            self.getCurrentTmin(True)
            self.getCurrentTmax(True)
            self.getCurrentCmin(True)
            self.getCurrentCmax(True)
            self.getCurrentMinh(True)
            self.getCurrentMaxh(True)

        if (self.battery != None):
            telemetric={
                'name': str(self.batteryId),
                'tmin': str(self.currentValue.get(self.battery.tmin.__name__)),
                'tmax': str(self.currentValue.get(self.battery.tmax.__name__)),
                'cmin': str(self.currentValue.get(self.battery.cmin.__name__)),
                'cmax': str(self.currentValue.get(self.battery.cmax.__name__)),
                'minh': str(self.currentValue.get(self.battery.minh.__name__)),
                'maxh': str(self.currentValue.get(self.battery.maxh.__name__)),
                'tbal': str(self.currentValue.get(self.battery.tbal.__name__))}
        else:
            telemetric={}

        return telemetric

    def shortInfo(self):
        return f"{self.name} "+("off" if (self.btrystate==BtryMainSwitchState.off) else f"t:{self.targetSOC}, c:{self.currentSOC}")

    def initPin(self):
        self.board.set_pin_mode_digital_output(self.chargerPin)
        self.board.set_pin_mode_digital_output(self.disChargerPin)
        self.board.set_pin_mode_servo(self.mainSwitchServoPin)

    def initBoard(self):
        self.board.digital_write(self.chargerPin, 1)
        self.board.digital_write(self.disChargerPin, 1)
        self.board.servo_write(self.mainSwitchServoPin, 0)

        self.ctrlstate=CtrlState.off
        self.btrystate=BtryMainSwitchState.off
        self.switchServoPos=0

    def setTargetSoc(self,targetSOC=50):
        self.targetSOC=targetSOC

    def getCurrentTmin(self, refresh=False):
        return self.getValueByName(self.battery.tmin, refresh, float)

    def getCurrentTmax(self, refresh=False):
        return self.getValueByName(self.battery.tmax, refresh, float)

    def getCurrentCmin(self, refresh=False):
        return self.getValueByName(self.battery.cmin, refresh, float)

    def getCurrentCmax(self, refresh=False):
        return self.getValueByName(self.battery.cmax, refresh, float)

    def getCurrentMinh(self, refresh=False):
        return self.getValueByName(self.battery.minh, refresh, float)

    def getCurrentMaxh(self, refresh=False):
        return self.getValueByName(self.battery.maxh, refresh, float)
#
#    def getCurrentSOC(self, refresh=False):
#        return self.getValueByName(self.battery.soc, refresh)

    def getValueByName(self, method, refresh, conversion=None):
        if (refresh and self.mockBattery == False):
            try:
                if (not(method.__name__ in self.valueReadFailCount)):
                    self.valueReadFailCount[method.__name__]=0

                value=method()

                if (conversion != None):
                    self.currentValue[method.__name__]=conversion(value)
                else:
                    self.currentValue[method.__name__]=value

                self.valueReadFailCount[method.__name__]=0
            except IndexError as e:
                self.valueReadFailCount[method.__name__]=self.valueReadFailCount[method.__name__]+1
                logging.warn('could not read %s, keeping the previous state %s, failCount %s',
                        method.__name__, self.currentValue.get(method.__name__), self.valueReadFailCount.get(method.__name__))
                self.storeExceptions(method.__name__, e)
            except Exception as e:
                self.valueReadFailCount[method.__name__]=self.valueReadFailCount[method.__name__]+1
                logging.warn('could not read %s, keeping the previous state %s, failCount %s',
                        method.__name__, self.currentValue.get(method.__name__), self.valueReadFailCount.get(method.__name__))
                self.storeExceptions(method.__name__, e)
        elif (refresh and self.mockBattery == True):
            self.currentValue[method.__name__]=self.currentValue[method.__name__]+1

        return self.currentValue.get(method.__name__)

    def getCurrentSOC(self, refresh=False):
        if (refresh and self.mockBattery == False):
            try:
                self.currentSOC=round(self.battery.soc(), 2)
                self.socFailCount=0
            except IndexError as e:
                logging.info('could not read soc, keeping the previous state %s, failCount %s', self.currentSOC, self.socFailCount)
                self.socFailCount=self.socFailCount+1
                self.storeExceptions('soc', e)
            except Exception as e:
                logging.info('could not read soc, keeping the previous state %s, failCount %s', self.currentSOC, self.socFailCount)
                self.socFailCount=self.socFailCount+1
                self.storeExceptions('soc', e)
        elif (refresh and self.mockBattery == True):
            self.currentSOC=self.currentSOC+1

        return self.currentSOC

    def switchBatteryOn(self):
        if (self.btrystate != BtryMainSwitchState.on):
            self.board.servo_write(self.mainSwitchServoPin, 90)
            if (self.mockBattery == False):
                logging.info('opening battery %s with speed %s', self.usbCtrlCable, self.usbCableSpeed)
                try:
                    self.battery=FES(self.usbCtrlCable, self.usbCableSpeed)
                    self.battery.open()
                    self.batteryId=self.battery.connect()

#                    while (not(self.battery.isConnected())):
#                        self.battery.connect()

                    self.battery.password()
                    self.btrystate=BtryMainSwitchState.on
                except Exception as e:
                    self.btrystate=BtryMainSwitchState.conn_failed
                    logging.error('battery switch on failed: '+str(e))
                    self.storeExceptions('connect', e)
            else:
                self.btrystate=BtryMainSwitchState.on
            logging.info('battery switched on')
        else:
            logging.warn('the battery is in %s state, can not switch on again', self.btrystate)

    def switchBatteryOff(self):
        if (self.btrystate == BtryMainSwitchState.on):
            self.btrystate=BtryMainSwitchState.off
            self.board.servo_write(self.mainSwitchServoPin, 0)
            logging.info('battery switched off')
        else:
            logging.warn('the battery is in %s state, can not switch off again', self.btrystate)

    def switchOffChargeDisCharge(self):
        if (self.ctrlstate != CtrlState.off):
            self.board.digital_write(self.chargerPin, 1)
            self.board.digital_write(self.disChargerPin, 1)
            self.ctrlstate = CtrlState.off
            logging.info('controller switched off')
        else:
            logging.warn('the controller is in %s state, please switch on it before', self.ctrlstate)

    def shutdown(self):
        self.switchOffChargeDisCharge()
        self.switchBatteryOff()

    def charge(self):
        self.switchBatteryOn()
        if (self.btrystate != BtryMainSwitchState.on):
            logging.warn('the battery is in %s state, please switch on it before', self.btrystate)
            return

        if (self.ctrlstate == CtrlState.off):
            self.board.digital_write(self.chargerPin, 0)
            self.ctrlstate = CtrlState.charging
            logging.info('charging')
        else:
            logging.warn('the controller is in %s state, please switch off it before', self.ctrlstate)

    def disCharge(self):
        self.switchBatteryOn()
        if (self.btrystate != BtryMainSwitchState.on):
            logging.warn('the battery is in %s state, please switch on it before', self.btrystate)
            return

        if (self.ctrlstate == CtrlState.off):
            self.board.digital_write(self.disChargerPin, 0)
            self.ctrlstate = CtrlState.discharging
            logging.info('discharging')
        else:
            logging.warn('the controller is in %s state, please switch off it before', self.ctrlstate)

    def doManagement(self):
        if (self.btrystate != BtryMainSwitchState.on):
            logging.debug('the battery is in %s state, please switch on it before', self.btrystate)
            return False

        if (self.ctrlstate == CtrlState.off):
            logging.debug('the controller is in %s state, please switch off it before', self.ctrlstate)
            return False

        #get current SOC
        self.getCurrentSOC(True)

        if (self.ctrlstate == CtrlState.charging and self.currentSOC >= self.targetSOC):
            logging.info('target SOC %s reached %s during %s', self.targetSOC, self.currentSOC, self.ctrlstate)
            self.switchOffChargeDisCharge()
            return True
        elif (self.ctrlstate == CtrlState.discharging and self.currentSOC <= self.targetSOC):
            logging.info('target SOC %s reached %s during %s', self.targetSOC, self.currentSOC, self.ctrlstate)
            self.switchOffChargeDisCharge()
            return True
        else:
            logging.info('target SOC %s not reached %s during %s', self.targetSOC, self.currentSOC, self.ctrlstate)
        return False

    def storeExceptions(self, operation, e):
        if (not(operation in self.operationExceptions)):
            self.operationExceptions[operation]=[]
        self.operationExceptions[operation].append(str(e))

#    def doStateCheck(self):


# -*- coding: utf-8 -*-
import sys
import numpy as np
import json

def get_lines(path):
    with open(path) as f:
        content = f.readlines()
    for line in content:
        if line == "": continue
        yield line.replace('\n', '').replace(' ', '\t').split('\t')

range  = (500, 9000)
limits = (500, 4000)
sample = get_lines(sys.argv[1])
header = next(sample)

values = []
for line in sample:
    obj, val = dict(zip(header, line)), float(line[3])
    if 'fft' in obj: val = float(obj['fft'])
    if 'FFT_dF' in obj: val = float(obj['FFT_dF'])
    if val < limits[0] or val > limits[1]: continue
    values.append(val)

hist = np.histogram(values, bins=400, range=range, density=True)[0]

with open('gim.py') as f:
    content = f.read().split('# TRAINED\n')
    content[1]  = "trained_mean = %f\n" % np.mean(values)
    content[1] += "trained = np.array(%s)\n" % json.dumps(hist.tolist())

with open('gim.py', 'w') as f:
    f.write("# TRAINED\n".join(content))

print("Done!")


import shutil
import cv2
import keras
from keras import backend as K, Sequential
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing import image
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D, Dropout
from keras.applications import MobileNet, VGG16
from keras.applications.mobilenet import preprocess_input
import numpy as np
import matplotlib.pyplot as plt
from google_images_download import google_images_download
from sklearn.preprocessing import LabelBinarizer
from tensorflow.python.keras.models import load_model
from keras.layers import Input
from keras.layers.pooling import AveragePooling2D
from keras.layers.core import Flatten
from Utilities.Utilities import Utilities
import os
from PIL import Image
import split_folders

class Train_MobileNet:

    def __init__(self):
        os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
        self.mobile = keras.applications.mobilenet.MobileNet()
        self.util = Utilities()

        self.w = 224
        self.h = 224

    def prepare_image(self, file):
        img_path = ''
        img = image.load_img(img_path + file, target_size=(self.w, self.h, 3))
        img_array = image.img_to_array(img)
        img_array_expanded_dims = np.expand_dims(img_array, axis=0)
        return keras.applications.mobilenet.preprocess_input(img_array_expanded_dims)

    def load_image(self, img_path, show=False):
        img = image.load_img(img_path, target_size=(self.w, self.h))
        img_tensor = image.img_to_array(img)  # (height, width, channels)
        img_tensor = np.expand_dims(img_tensor,
                                    axis=0)  # (1, height, width, channels), add a dimension because the model expects this shape: (batch_size, height, width, channels)
        img_tensor /= 255.  # imshow expects values in the range [0, 1]

        if show:
            plt.imshow(img_tensor[0])
            plt.axis('off')
            plt.show()

        return img_tensor

    def convert_h5_to_pb(self, modelo, tf=None):
        frozen_graph = self.util.freeze_session(K.get_session(), output_names=[out.op.name for out in modelo.outputs])
        tf.train.write_graph(frozen_graph, "Models/", "my_model.pb", as_text=False)

    def download_data_set(self, class_names, training_paht):
        response = google_images_download.googleimagesdownload()

        for i_class_names in class_names:
            arguments = {"keywords": i_class_names, "output_directory": training_paht, "limit": 100, "print_urls": False,
                         "format": "png", "size": "medium",
                         "o": training_paht}

            paths = response.download(arguments)

    def resize_image(self, image_path):
        for dirname in os.listdir(image_path):
            path = image_path + "/" + dirname
            for i, filename in enumerate(os.listdir(path)):
                oriimg = image.load_img(path + "\\" + filename)
                img = oriimg.resize((self.w, self.h), Image.ANTIALIAS)
                # print("Old Size: " + str(np.array(oriimg).shape) + " New Size: " + str(np.array(img).shape))
                img.save(image_path + "\\" + dirname + "\\" + dirname + "_" + str(i) + ".jpg")

    def raname_images(self, image_path):
        for dirname in os.listdir(image_path):
            path = image_path + "/" + dirname
            for i, filename in enumerate(os.listdir(path)):
                try:
                    os.rename(image_path + "/" + dirname + "/" + filename,
                              image_path + "/" + dirname + "/" + dirname + "_" + str(i) + ".jpg")
                except:
                    break

    def remove_corrupted_image(self, image_path):
        img_dir = image_path
        for filename in os.listdir(img_dir):
            filepath = os.path.join(img_dir, filename)
            print('file folder >>>>>' + filepath)
            if filename == ".DS_Store":
                print('DS folder -----' + filepath)
                os.remove(filepath)
            else:
                for imagename in os.listdir(filepath):
                    imagepath = os.path.join(filepath, imagename)
                    try:
                        with Image.open(imagepath) as im:
                            print('Ok')
                    except:
                        print(imagepath + " -----> Removed")
                        os.remove(imagepath)

    def remove_old_dataset(self, dataset_path):
        for filename in os.listdir(dataset_path):
            try:
                foldet_to_remove = dataset_path + "/" + filename
                print(foldet_to_remove)
                shutil.rmtree(foldet_to_remove)
            except:
                print("Not Folder in the path")
                break

    def remove_files(self, files_paht):
        for filename in os.listdir(files_paht):
            try:
                print(files_paht + "/" + filename)
                os.remove(files_paht + "/" + filename)
            except:
                print("Not Files in the folder")
                break

    def remove_old_models(self, model_path):
        for folder in os.listdir(model_path):
            for i in os.listdir(model_path + "/" + folder):
                try:
                    print(model_path + "/" + folder + "/" + i)
                    os.remove(model_path + "/" + folder + "/" + i)
                except:
                    print("Not Files in the folder")
                    break

    def copy_image_to_all_class(self, src_path, dst_path):
        for folder in os.listdir(src_path):
            for i in os.listdir(src_path + "\\" + folder):
                src_dir = src_path + "\\" + folder + "\\" + i

                dst_dir = dst_path + "\\" + i

                print(src_dir)
                print(dst_dir)

                shutil.copy(src_dir, dst_dir)

    def create_mobile_net(self, type_net=0, n_class=0, alpha=0.75, depth_multiplier=1, pooling='avg', weights="imagenet",
                          include_top=False, dropout=1e-3, dropout_rate=0.001):
        if type_net == 0:
            base_model = keras.applications.mobilenet.MobileNet(input_shape=(self.w, self.h, 3), alpha=alpha,
                                                                depth_multiplier=depth_multiplier,
                                                                dropout=dropout,
                                                                pooling=pooling,
                                                                include_top=include_top,
                                                                weights=weights,
                                                                classes=n_class)

            x = base_model.output
            print(x)
            x = Dropout(dropout_rate, name='dropout')(x)  # drop=0.001
            preds = Dense(n_class, activation='softmax')(x)  # final layer with softmax activation
            model = Model(inputs=base_model.input, outputs=preds)
            return model

        elif type_net == 1:
            base_model = MobileNet(input_shape=(self.w, self.h, 3), alpha=alpha, depth_multiplier=depth_multiplier,
                                   dropout=dropout, pooling=pooling, include_top=include_top,
                                   weights=weights, classes=n_class)
            x = base_model.output
            print(x)
            x = Dropout(dropout_rate, name='dropout')(x)  # drop=0.001
            preds = Dense(n_class, activation='softmax')(x)  # final layer with softmax activation
            model = Model(inputs=base_model.input, outputs=preds)
            return model

        elif type_net == 2:
            base_model = MobileNet(weights=weights)
            x = base_model.output
            print(x)
            x = Dropout(dropout_rate, name='dropout')(x)  # drop=0.001
            preds = Dense(n_class, activation='softmax')(x)  # final layer with softmax activation
            model = Model(inputs=base_model.input, outputs=preds)
            return model
        elif type_net == 3:

            base_model = MobileNet(input_shape=(self.w, self.h, 3), alpha=alpha, depth_multiplier=depth_multiplier,
                                   dropout=dropout, pooling=pooling, include_top=include_top,
                                   weights=weights, classes=n_class)

            head_model = base_model.output
            head_model = AveragePooling2D(pool_size=(7, 7))(head_model)
            head_model = Flatten(name="flatten")(head_model)
            head_model = Dense(128, activation="relu")(head_model)
            head_model = Dropout(0.5)(head_model)
            head_model = Dense(n_class, activation="softmax")(head_model)

            # place the head FC model on top of the base model (this will become
            # the actual model we will train)
            model = Model(inputs=base_model.input, outputs=head_model)

            return model

        elif type_net == 4:
            baseModel = VGG16(weights="imagenet", include_top=False,
                              input_tensor=Input(shape=(self.w, self.h, 3)))
            # show a summary of the base model
            print("[INFO] summary for base model...")
            print(baseModel.summary())

            head_model = baseModel.output
            head_model = AveragePooling2D(pool_size=(7, 7))(head_model)
            head_model = Flatten(name="flatten")(head_model)
            head_model = Dense(128, activation="relu")(head_model)
            head_model = Dropout(0.5)(head_model)
            head_model = Dense(n_class, activation="softmax")(head_model)

            # place the head FC model on top of the base model (this will become
            # the actual model we will train)
            model = Model(inputs=baseModel.input, outputs=head_model)

            return model

        elif type_net == 5:
            base_model = keras.applications.mobilenet.MobileNet(input_shape=(224, 224, 3), alpha=0.75, depth_multiplier=1,
                                                                dropout=0.001, pooling='avg', include_top=False,
                                                                weights="imagenet", classes=1000)
            x = base_model.output
            x = Dropout(0.001, name='dropout')(x)  # drop=0.001
            preds = Dense(1000, activation='softmax')(x)  # final layer with softmax activation
            model = Model(inputs=base_model.input, outputs=preds)
            return model

        elif type_net == 6:
            # Build the model.
            model = Sequential([
                Dense(64, activation='relu', input_shape=(224, 224, 3)),
                Dense(64, activation='relu'),
                Dense(10, activation='softmax'),
            ])
            return model

    def print_model_estructure(self, model):
        for i, layer in enumerate(model.layers):
            print(i, layer.name)

        for layer in model.layers:
            layer.trainable = False
        # or if we want to set the first 20 layers of the network to be non-trainable
        for layer in model.layers[:20]:
            layer.trainable = False
        for layer in model.layers[20:]:
            layer.trainable = True

    def train_model(self, model, train_path="", epochs=50, batch_size=64, class_mode='categorical', shuffle=True):
        train_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)  # included in our dependencies

        train_generator = train_datagen.flow_from_directory(train_path,
                                                            target_size=(self.w, self.h),
                                                            color_mode='rgb',
                                                            batch_size=batch_size,
                                                            class_mode=class_mode,
                                                            shuffle=shuffle)

        model.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['accuracy'])
        # Adam optimizer
        # loss function will be categorical cross entropy
        # evaluation metric will be accuracy

        step_size_train = 50  # train_generator.n / train_generator.batch_size
        history = model.fit_generator(generator=train_generator,
                                      steps_per_epoch=step_size_train,
                                      epochs=epochs)

        return history

    def save_model(self, path_stored_model, model):
        json_string = model.to_json()
        open(path_stored_model + 'model.json', 'w').write(json_string)

        # self.model.save_weights('Deep_Learning/Deep_Model/Face_model_weights.h5')
        model.save(path_stored_model + 'model_1.h5')

    def predic_class(self, model, test_image):
        new_image = self.load_image(test_image)
        pred = model.predict(new_image)
        return pred

    def convert_h5_to_tflite(self):
        keras_model_path = "Keras_Models\model_1.h5"
        tflite_path = "Tflite_Model\model.tflite"
        cmd = "tflite_convert --keras_model_file " + keras_model_path + " --output_file " + tflite_path
        os.system(cmd)

    def convert_tflite_to_kmodel(self):
        path_all_dataset = "All_IMG_Dataset"
        tflite_path = "Tflite_Model/model.tflite"
        kmodel_path = "K_Models/model.kmodel"
        cmd = "tools/ncc.exe -i tflite -o k210model --dataset " + path_all_dataset + " " + tflite_path + " " + kmodel_path
        os.system(cmd)
        # os.startfile(cmd)

    def draw_training_loss(self, history):
        print(history.history.keys())
        # summarize history for accuracy
        plt.plot(history.history['acc'])
        plt.plot(history.history['val_acc'])
        plt.title('model accuracy')
        plt.ylabel('accuracy')
        plt.xlabel('epoch')
        plt.legend(['train', 'valid'], loc='upper left')
        plt.show()
        # summarize history for loss
        plt.plot(history.history['loss'])
        plt.plot(history.history['val_loss'])
        plt.title('model loss')
        plt.ylabel('loss')
        plt.xlabel('epoch')
        plt.legend(['train', 'valid'], loc='upper left')
        plt.show()

def main():

    train_mobilenet = Train_MobileNet()

    image_training_paht = 'dataset'
    ttv_path = 'ttv_path'  # Train-Test-Validation Path
    image_test_path = 'dataset/valid'
    all_data_set_path = 'dataset-all'
    path_stored_model = 'Keras_Models/'
    test_image = 'ttv_path/test'
    model_path = "Models"

    print("Rename Image...")
    raname_images(image_training_paht)

    print("Resize Image...")
    resize_image(image_training_paht)

    split_folders.ratio(image_training_paht, output=ttv_path, seed=1337, ratio=(.8, .1, .1))

    print("Resize Train Image...")
    resize_image(ttv_path + '\\train')
    print("Resize Test Image...")
    resize_image(ttv_path + '\\test')
    print("Resize Validation Image...")
    resize_image(ttv_path + '\\val')

    class_names = os.listdir(ttv_path + '\\train')
    print(class_names)
    encoder = LabelBinarizer()
    categorical_class_name = encoder.fit_transform(class_names)
    print(categorical_class_name)

    epochs = 6
    train_or_test = True

    print("Creating Model...")
    model = train_mobilenet.create_mobile_net(type_net=0, n_class=len(class_names), dropout_rate=0.001)

    if train_or_test:
        train_mobilenet.remove_old_models(model_path)

        train_mobilenet.remove_old_dataset(image_training_paht)
        train_mobilenet.remove_old_dataset(image_test_path)
        train_mobilenet.remove_files(all_data_set_path)
        train_mobilenet.download_data_set(class_names, image_training_paht)

        train_mobilenet.remove_corrupted_image(image_training_paht)
        train_mobilenet.raname_images(image_training_paht)

        train_mobilenet.resize_image(image_training_paht)

        print("Copy Image to all folder...")
        train_mobilenet.copy_image_to_all_class(image_training_paht, all_data_set_path)

        train_mobilenet.print_model_estructure(model)

        print("Training Model...")
        history = train_mobilenet.train_model(model, ttv_path + "\\train", epochs=epochs)

        print("Save Model...")
        train_mobilenet.save_model(path_stored_model, model)

        print("Convert H5 to tflite...")
        train_mobilenet.convert_h5_to_tflite()

        print("Convert tflite to Kmodel...")
        train_mobilenet.convert_tflite_to_kmodel()

        # draw_training_loss(history)
    else:
        model = load_model('E:\Dropbox\Cognitive_Service_Project\Edge_AI_Server\Models\Keras_Models\model_1.h5')
        model.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['accuracy'])

        for i in os.listdir(test_image):
            path_0 = test_image + "\\" + i
            for j in os.listdir(path_0):
                path = path_0 + "\\" + j
                prediction = train_mobilenet.predic_class(model, path)
                max_provavility = np.argmax(prediction[0])
                print(
                    "File Name: [ " + i + " ] " + " Predicted ConfigFile Name: [ " + class_names[
                        max_provavility] + " ] " + str(
                        prediction))


from typing import Optional, Callable, Tuple, Union, Dict, Any
import os
from datetime import datetime as dt
import pandas as pd
import numpy as np
import pickle

from flax.training import train_state

def get_y_trues(
    data:pd.core.frame.DataFrame,
    sessionkey:Optional[str]="userId",
    itemkey:Optional[str]="itemId"
) -> pd.core.frame.DataFrame:
    """
        Preprocess the data : first col=userId, second col=itemIds(it has all interaction history for user.)
    """
    return data.groupby(sessionkey)[itemkey].unique().reset_index(name="itemIds")

def train_test_split(
    data: pd.core.frame.DataFrame,
    split_date: Union[int, dt]
) -> Tuple[pd.core.frame.DataFrame]:
    """
        the data split by timestamp
    """

    return data[data["timestamp"] <= split_date], data[data["timestamp"] > split_date]

def save_params(
    params:Dict[str, Any],
    name:Optional[str]="model",
    path : Optional[str]= "~/work/recs/params/"
):
    save_path = os.path.join(path, name)
    print("=====saving states=====")
    with open(save_path, mode="wb") as f:
        pickle.dump(params, f)
    print("=====done=====")

def get_batchs(batch):
    return batch["state"], batch["action"], batch["n_state"], batch["reward"]


"""empty message

Revision ID: 45afaf9abb24
Revises: 9b3ce75f508e
Create Date: 2019-11-03 14:18:42.936851

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '45afaf9abb24'
down_revision = '9b3ce75f508e'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('patient',
    sa.Column('patient_id', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('first_name', sa.String(length=50), nullable=True),
    sa.Column('last_name', sa.String(length=50), nullable=True),
    sa.Column('address', sa.String(length=128), nullable=True),
    sa.Column('city', sa.String(length=50), nullable=True),
    sa.Column('province', sa.String(length=50), nullable=True),
    sa.Column('state', sa.String(length=50), nullable=True),
    sa.PrimaryKeyConstraint('patient_id')
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('patient')
    # ### end Alembic commands ###


from rest_framework.views import APIView
from rest_framework.response import Response

class EnumView(APIView):
    permission_classes = []
    fields = ('i18n', )

    def get(self, *args, **kwargs):
        enums = self.enum_class.get_as_tuple_list()
        context = []

        for enum in enums:
            _id = enum[1]
            enum_context = {'id': _id}

            for field in self.fields:
                enum_context[field] = getattr(self.enum_class, field)[_id]

            context.append(enum_context)

        return Response(context)


from doctest import OutputChecker
import pathlib
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input

# creating a simple keras model
x = [-1, 0, 1, 2, 3, 4]
y = [-3, -1, 1, 3, 5, 7]

model = tf.keras.models.Sequential([tf.keras.layers.Dense(units=1, input_shape=[1])])
model.compile(optimizer='sgd', loss='mean_squared_error')
model.fit(x, y, epochs=200, verbose=1)
# The TensorFlow library was compiled to use AVX instructions, but these aren't available on your machine.

# generate a saved model
export_dir = '/tmp/saved_model/'
tf.saved.model.save(model, export_dir)

# convert the saved model to tflite
converter = tf.lite.TFLiteConverter.from_saved_model(export_dir)
tflite_model = converter.convert()

tflite_model_file = pathlib.Path('/tmp/saved_model/saved_model.tflite')
tflite_model_file.write_bytes(tflite_model)

# initalize the TF Lite interpreter to try it out
interpreter = tf.lite.Interpreter(model_content=tflite_model)
interpreter.allocate_tensors()

input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# test the TF Lite model on random input data
input_shape = input_details[0]['shape']
inputs, outputs = [], []

for _ in range(100):
    input_data = np.array(np.random.random_sample(input_shape), dtype=np.float32)
    interpreter.set_tensor(input_details[0]['index'], input_data)

    interpreter.invoke()
    tflite_results = interpreter.get_tensor(output_details[0]['index'])

    # test TF model on random input data
    tf_results = model(tf.constant(input_data))
    output_data = np.array(tf_results)

    inputs.append(input_data[0][0])
    outputs.append(output_data[0][0])

# vizualize the results
plt.plot(inputs, outputs, 'r')
plt.show()

"""
# download the model
try:
    from google.colab import files
    files.download(tflite_model_file)
except:
    pass
"""


import matplotlib.pyplot as plt
import matplotlib.animation as animation
import random
import serial
import time
import numpy as np
import scipy.io as sio
from serial_handler import read_serial, goPos, serial_setup
from mpl_toolkits import mplot3d

# Create figure for plotting
fig = plt.figure()
ax = fig.add_subplot(1,1,1)

xs = []
ys = []
zs = []

# Initialize communication with magnetometer
sensor, controller = serial_setup()

# This function is called periodically from FuncAnimation
def animate(i, xs, ys, zs):

    # Read temperature (Celsius) from TMP102
    data = read_serial(sensor,False)

    # Add x and y to lists
    if len(data)==3 and any(data):
        print(data)
        xs.append(data[0])
        ys.append(data[1])
        zs.append(data[2])

    # Limit x and y lists to 20 items
        xs = xs[-20:]
        ys = ys[-20:]
        zs = zs[-20:]

    # Draw x and y lists
    ax.clear()
    ax.set_xlim([-500,500])
    ax.set_ylim([-500,500])
    ax.scatter(xs, ys)

    # Format plot
    plt.subplots_adjust(bottom=0.30)
    plt.title('Magnetometer Data Over Time')

# Set up plot to call animate() function periodically
ani = animation.FuncAnimation(fig, animate, fargs=(xs, ys, zs), interval=20)
plt.show()


from twilio.rest import TwilioRestClient
from twilio import twiml
from datetime import date

# To find these visit https://www.twilio.com/user/account
ACCOUNT_SID = "ACf0757674cd3d5092237f0ee7afca722e"
AUTH_TOKEN = "YYYYYYYYYYYYYYYYYY"

client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)

message = client.messages.create(
    body=input('Enter message to be sent\n'),  # Message body, if any #input('Enter message to be sent\n')
    to=input('Enter your phone number\n'),
    from_="+13023856046",
    media_url="http://www.hdwallpapers.in/walls/pawan_kalyan-wide.jpg"
)

'''
media_url=[  # List of media URLs, if any
        "http://example.com/image1.jpg",
        "http://example.com/image2.jpg",

'''
print(message.sid)

for message in client.messages.list():
    print(message.body)

messages = client.messages.list(
    to="+16692261830",
    date_sent=date(2015,4,18),
)
#input('Enter the phone number\n'),"+16692261830","+13015260890"

for message in messages:
    print(message.body)

'''

Redacting or Deleting Message Records

To protect your users’ privacy and/or comply with legal requirements, Twilio allows you to redact your Message bodies or delete the records outright.

from twilio.rest import TwilioRestClient

# To find these visit https://www.twilio.com/user/account
ACCOUNT_SID = "ACXXXXXXXXXXXXXXXXX"
AUTH_TOKEN = "YYYYYYYYYYYYYYYYYY"

client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)
message_sid = "MM123"

client.messages.redact(message_sid)
message = client.messages.get(message_sid)
print message.body  # Will be an empty string

client.messages.delete(message_sid)  # Deletes record entirely, subsequent requests will return 404

'''


from configparser import ConfigParser
from fantastic.exercises.choose.choose_class import Choose

class VraiFaux(Choose):
    """See Exercise class documentation to understand the different parameters"""

    def __init__(self, json_path: str, config: ConfigParser) -> None:
        super().__init__(
            json_path,
            config,
            output_folder_name="vrai-faux",
        )

    def find_choices(self):
        self.choices = [["vrai", "faux"]]
        return self.choices


import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import pandas as pd
#Load data
data_2018 =pd.read_csv('data/player_category_sg_2018', header=[0, 1], index_col=[0,1])

#Player dictionary
player_options = pd.DataFrame()

player_options['label'] = data_2018.index.get_level_values(level = 1)
player_options['value'] = data_2018.index.get_level_values(level = 0)

player_options = player_options.to_dict('records')

def sort_index(string):
    return int(string.replace('=', '-').partition("-")[2])

def getSortValue(category):
    return distance_categories.index(category)

#Sort categories in correct order
distance_categories = sorted(data_2018.columns.levels[0], key= sort_index )

#Stylesheet
external_stylesheets = [
    'https://codepen.io/chriddyp/pen/bWLwgP.css',
    {
        'href': 'https://codepen.io/chriddyp/pen/bWLwgP.css',
        'rel': 'stylesheet',
    }
]

#Define max and min player based on darta
max_player = pd.DataFrame( columns=['cat', 'player_name', 'adj_sg'])

min_player = pd.DataFrame( columns=['cat', 'player_name', 'adj_sg'])

for cat in distance_categories:
    min_df = data_2018[cat][data_2018[cat]['shot_count'] > 30]
    min_row = min_df.loc[[min_df['adj_sg'].idxmin(axis=0)]].reset_index()[['player_name', 'adj_sg']]
    min_row['cat'] = cat
    min_player = min_player.append(min_row, ignore_index=True, sort=False)

    max_df = data_2018[cat][data_2018[cat]['shot_count'] > 30]
    max_row = max_df.loc[[max_df['adj_sg'].idxmax(axis=0)]].reset_index()[['player_name', 'adj_sg']]
    max_row['cat'] = cat
    max_player = max_player.append(max_row, ignore_index=True, sort=False)
#Create app
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
server = app.server
#Layout
app.layout = html.Div(children=[
    html.H2(
        children='Strokes Gained - Distance Categories',
        style={
            'textAlign': 'center'
        }),
    html.H4(
        children='Data source: PGA Tour Shotlink',
        style={
            'textAlign': 'center'
        }
        ),
    dcc.Dropdown(
        id="player_selection",
        options=player_options,
        multi=True,
        value = ['8793']
    ),
    dcc.Checklist(
    id = "max_min",
    options=[
        {'label': 'Show max', 'value': 'ShowMax'},
        {'label': 'Show min', 'value': 'ShowMin'},
        {'label': 'Show 95% confidence interval', 'value': 'Show95'},

    ],
    labelClassName='checkable',
    values=[],
    ),
    dcc.Graph(
        id='chart',
    )
])
#Callback to update chart
@app.callback(
    dash.dependencies.Output('chart', 'figure'),
    [dash.dependencies.Input('player_selection', 'value'),
    dash.dependencies.Input('max_min', 'values')])
def update_graph(player_selection, max_min):
    plots = []
    for player in player_selection:
        playerdf = data_2018[data_2018.index.get_level_values('PlayerNr') == int(player)]
        if(len(playerdf) > 0):
            player_name = playerdf.index.get_level_values('player_name')[0]
            playerdf = playerdf.transpose().unstack(level=1)
            playerdf.columns =  [ '95_up', '95_low', 'adj_sg', 'shot_count', 'std', 'std_err']
            playerdf['cat_ind'] = pd.Series(playerdf.index.values).apply(getSortValue).values
            playerdf = playerdf.sort_values('cat_ind')
            #Show confidence interval
            if 'Show95' in max_min:
                pplot = go.Scatter (
                    x = playerdf.index.values,
                    y = playerdf['adj_sg'].round(3),
                    name = player_name,
                    text =  "Nr of shots: " + playerdf['shot_count'].astype(int).astype(str),
                    error_y=dict(
                    type='data',
                    symmetric=True,
                    array=1.96*playerdf['std_err'],
                    )
                )
            else:
                    pplot = go.Scatter (
                    x = playerdf.index.values,
                    y = playerdf['adj_sg'].round(3),
                    name = player_name,
                    text =  "Nr of shots: " + playerdf['shot_count'].astype(int).astype(str),

                )
            plots.append(pplot)

    #Checkbox settings
    if "ShowMax" in max_min:
        plots.append( go.Scatter( x = max_player['cat'], y = max_player['adj_sg'].round(3), name = "Max", text = max_player['player_name']) )

    if "ShowMin" in max_min:
        plots.append( go.Scatter( x = min_player['cat'], y = min_player['adj_sg'].round(3), name = "Min", text = min_player['player_name'] ) )

    layout = go.Layout(
            title="Average strokes gained per shot in category",
            xaxis=dict(
                title='Category (yards from hole)',
                titlefont=dict(
                    family='Courier New, monospace',
                    size=18,
                    color='#7f7f7f'
                     )
                ),
            yaxis=dict(
                title='Strokes gained per shot',
                titlefont=dict(
                    family='Courier New, monospace',
                    size=18,
                    color='#7f7f7f'
                    )
                )
            )
    return {"data":plots, "layout": layout }

if __name__ == '__main__':
    app.run_server(debug=True)


import numpy as np
import matplotlib.pyplot as plt
import hngd.hngd
import hngd.default_model_parameters

"""

Reproduce Fig 3 from Passelaigue F., Simon, P. A. and Motta, A. T., 'Predicting
the hydride rim by improving the solubility limits in the Hydride
Nucleation-Growth-Dissolution (HNGD) model', Journal of Nuclear Materials 558,
pp. 153363 (2022).

"""

model_parameters = hngd.default_model_parameters.model_parameters

tau = 1e4
t_0 = 0.3

temperature_stamps = np.array([20, 650, 300, 700, 700])
time_stamps = np.array([0, 0.05, 0.10, 0.3, 3])*1e4

time = np.linspace(time_stamps[0], time_stamps[-1], 1000)
temperature = np.interp(time, time_stamps, temperature_stamps)

TSSd0 = model_parameters['TSSd0']
Q_TSSd = model_parameters['Q_TSSd']
TSSp0 = model_parameters['TSSp0']
Q_TSSp = model_parameters['Q_TSSp']

temp = np.zeros(2)
TSSp = np.zeros(len(time))
TSSd = np.zeros(len(time))
TSSp_eff = np.zeros(len(time))

t_0 = 0

temperature_change_flag = True

for i, t in enumerate(time):
    temp[1] = temperature[i]

    TSSp[i], TSSd[i] = hngd.hngd.calc_terminal_solid_solubility(TSSp0, Q_TSSp, TSSd0, Q_TSSd, temp[1])
    TSSp_eff[i] = TSSp[i]

    diff_temp = temp[1] - temp[0]

    if diff_temp == 0 and temperature_change_flag:
        temperature_change_flag = False
        t_0 = t

    if diff_temp == 0:
        TSSp_eff[i] = TSSd[i] + (TSSp[i]-TSSd[i])*np.exp(-(t-t_0)/tau)
    else:
        temperature_change_flag = True
        t_0 = 0

    temp[0] = temp[1]

fig, ax1 = plt.subplots(figsize=(6,4))

color = 'tab:red'
ax1.plot(time, TSSd, color=color, label='TSSd', ls=':')
ax1.plot(time, TSSp, color=color, label='TSSp', ls='--')
ax1.plot(time, TSSp_eff, color=color, label='TSSp$_{eff}$')
ax1.set_ylim([0, 450])
ax1.set_xlabel('Time [s]')
ax1.set_ylabel('Hydrogen content [wt.ppm]', color=color)
ax1.tick_params(axis='y', labelcolor=color)
ax1.legend()
ax1.grid()

ax2 = ax1.twinx()

color = 'tab:blue'
ax2.plot(time, temperature,color=color, label='Temperature')
ax2.set_ylabel('Temperature [K]', color=color)
ax2.tick_params(axis='y', labelcolor=color)

ax2.set_xlim([0, time_stamps[-1]])
ax2.set_ylim([0, 900])
ax2.legend()

fig.tight_layout()

plt.savefig('test_TSSp_eff_mHNGD.png', bbox_inches='tight')

plt.show()


from pprint import pprint

import pymongo

class database:

    def __init__(self):
        #Change this to where ever you are hosting the database
        connected = False
        while connected == False:
            loc = input("Please enter the database loacation or leave empty for localhost: ")

            if loc == "":
                try:
                    self.client = pymongo.MongoClient("mongodb://localhost:27017/",serverSelectionTimeoutMS=1500)
                    self.client.server_info()
                except pymongo.errors.ServerSelectionTimeoutError as err:

                    print("Connection Failed")
                else:
                    connected = True
            else:

                try:
                    self.client = pymongo.MongoClient(loc,serverSelectionTimeoutMS=1500)
                    self.client.server_info()
                except pymongo.errors.ServerSelectionTimeoutError as err:

                        print("Connection Failed")
                else:
                    connected = True

        self.db = self.client.queuesystem

        self.collection = self.db.line

    def addtoqueue(self,item):
        client = {"_id": item.userID, "name:": item.name, "reason:":item.reason, "position:": item.position, "uniqueID:": item.uniqueID, "serviceData:": item.serviceData }
        x = self.collection.insert_one(client)

    def removefromqueue(self,item):
        client = {"_id": item.userID, "name:": item.name, "reason:":item.reason, "position:": item.position,"uniqueID:": item.uniqueID,"serviceData:": item.serviceData}

        y = {"_id": item.userID}

        self.collection.delete_one(y)
    def updatePositions(self):
        for x in self.collection.find():
            print(x["position:"])
            self.collection.find_one_and_update({"_id": x["_id"]},{'$inc': {"position:": -1}})

    def callUp(self):
        x = self.collection.find_one_and_delete({"position:" : 1})
        self.updatePositions()
        return x

    def isEmpty(self):
        return self.collection.count({}) == 0

    def updateDisplay(self):
        pass

    def changePosition(self,id,spot):
        ## CHECK FOR VALID INPUTS
        if int(spot) > self.findLastPosition() - 1:
            return "Out of bounds position please try again"

        current = self.collection.find_one({"_id" : int(id)})

        if int(spot) > current["position:"]:
            for x in self.collection.find():
                if (int(spot) >= x["position:"] and x["_id"] != int(id)) and (x["position:"] > current["position:"]):
                    self.collection.update_one({"_id": x["_id"]}, {"$inc": {"position:": -1 }})
        elif int(spot) < current["position:"]:
            for x in self.collection.find():
                if (int(spot) <= x["position:"] and x["_id"] != int(id))and (x["position:"] < current["position:"]):
                    self.collection.update_one({"_id": x["_id"]}, {"$inc": {"position:": 1}})
        self.collection.update_one({"_id": int(id)}, {"$set": {"position:": int(spot)}})

    def toList(self):
        return self.collection.find()

    def findLastPosition(self):
        position = 1
        for x in self.collection.find():
            if position <= x["position:"]:
                position = x["position:"] + 1
        return position

    def viewDataBase(self):
        cursor = self.collection.find({})
        for document in cursor:
            pprint(document)


from colorama import Fore, Style

from Player import Player

class InputService:

    def __init__(self):
        self.game_board = ['#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
        self.available_tiles = list(range(1, 10))

    @staticmethod
    def get_player_names():
        player_1_name_input = input("Player 1 - Please enter you name\n")
        player_2_name_input = input("Player 2 - Please enter you name\n")
        return player_1_name_input.title(), player_2_name_input.title()

    @staticmethod
    def get_player_move_choice(player_1):
        input_styles = ['X', 'O']
        player_style_input = input(f"{player_1}, please pick a marker 'X' or 'O'\n")

        while player_style_input.upper() not in input_styles:
            print(Fore.RED + f'Invalid input : {player_style_input}' + Style.RESET_ALL)
            player_style_input = input(
                f"{player_1}, please choose a marker only between" + Fore.GREEN + f" {input_styles[0]} " + Style.RESET_ALL +
                "or " + Fore.GREEN + f"{input_styles[1]}" + Style.RESET_ALL)

        player1_choice = player_style_input.upper()
        input_styles.pop(input_styles.index(player_style_input.upper()))
        player2_choice = input_styles[0]

        return player1_choice, player2_choice

    def create_players(self):
        player_1_name_input, player_2_name_input = self.get_player_names()
        player_1_move, player_2_move = self.get_player_move_choice(player_1_name_input)
        player_1 = Player.player_from_input(player_1_name_input, player_1_move)
        player_2 = Player.player_from_input(player_2_name_input, player_2_move)

        return player_1, player_2

    def play_moves(self, selected_player):
        print(selected_player.color)
        player_tile_input = input(f'{selected_player.name}, please choose a tile to play.\n')
        print(Style.RESET_ALL)

        while not player_tile_input.isdigit() or int(player_tile_input) not in self.available_tiles:
            print(Fore.RED + f'Invalid input : {player_tile_input}' + Style.RESET_ALL)
            print(
                'Please choose a tile to play among values : ' + Fore.YELLOW + f'{self.available_tiles} \n' + Style.RESET_ALL)
            player_tile_input = input()

        print(f'Player Tile input is : {player_tile_input}')
        self.game_board[int(player_tile_input)] = selected_player.move_style
        self.available_tiles.pop(self.available_tiles.index(int(player_tile_input)))

    @staticmethod
    def ask_play_again():
        options = ('Y', 'N')
        while True:
            play_again_input = input('Do You want to play another game ? Y/N\n')
            if play_again_input.isdigit() or play_again_input.upper() not in options:
                print(f'Incorrect input, please choose an option from : {options}')
                continue
            else:
                break

        return play_again_input

    def reset_board(self):
        self.game_board = ['#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
        self.available_tiles = list(range(1, 10))


import os
from twisted.internet import protocol, reactor
from twisted.internet.protocol import Protocol
from twisted.internet.defer import succeed
from twisted.internet.error import ProcessDone
from twisted.python import log
from gi.repository import Gtk
from gridtogo.client.ui.dialog import *
from twisted.web.client import Agent
from twisted.web.http_headers import Headers
from twisted.web.iweb import IBodyProducer
import urllib
import xml.dom.minidom
from zope.interface import implements

class PostProducer(object):
	implements(IBodyProducer)

	def __init__(self, values):
		self.body = urllib.urlencode(values)
		self.length = len(self.body)

	def startProducing(self, consumer):
		consumer.write(self.body)
		return succeed(None)

	def pauseProducing(self):
		pass

	def stopProducing(self):
		pass

class ConsoleProtocol(protocol.ProcessProtocol):
	def __init__(self, name, logFile, opensimdir, consolePort, externalhost, callOnEnd=None, callOnOutput=None):
		self.name = name
		self.logFile = logFile
		self.callOnEnd = callOnEnd
		self.callOnOutput = callOnOutput
		self.consolePort = consolePort
		self.opensimdir = opensimdir
		self.externalhost = externalhost

		self._buffer = ''

	def connectionMade(self):
		log.msg("Connection Established to child process " + self.name)
		self.pid = self.transport.pid

	def childDataReceived(self, fd, data):
		# data is not a complete line, this buffers and only passes complete lines
		if self.callOnOutput:
			data = self._buffer + data
			self._buffer = ''
			for line in data.splitlines(True):
				if line.endswith(('\n', '\r\n')):
					self.callOnOutput(self.name, line.strip())
				else:
					self._buffer = line

	def processEnded(self, reason):
		log.msg("Process " + self.name + " has ended. Reason: " + str(reason))

		#TODO: Prevent this dialog from showing if the user initiated the kill
		# Maybe move this dialog stuff somewhere else
		if reason.type is ProcessDone:
			showModalDialog(None, Gtk.MessageType.INFO, 'Process %s exited cleanly.' % self.name)
		else:
			showModalDialog(
				None,
				Gtk.MessageType.ERROR,
				'Process "%s" has exited uncleanly,\nrefer to the logfile %s for details.'
				% (self.name, self.logFile))

		if self.callOnEnd:
			self.callOnEnd(self.name, reason)

	def sendCommand(self, command, callback=None):
		"""Sends a command to the REST console of this process."""
		log.msg("[REST] Sending command: /" + url)
		if not hasattr(self, '_sessionid'):
			log.msg("[REST] [SESSION] Fetching Session ID")
			agent = Agent(reactor)
			d = agent.request(
				'POST',
				'http://%s:%d/StartSession/' % (str(self.externalhost), self.consolePort),
				Headers({"Content-Type": ["application/x-www-form-urlencoded"]}),
				PostProducer({
					"USER": "gridtogo",
					"PASS": "gridtogopass"
				}))

			def request(response):
				response.deliverBody(CommandProtocol(response.length, done))

			def err(response):
				log.msg("[REST] [SESSION] [ERROR] " + str(response))

			def done(protocol):
				def getText(nodelist):
					rc = []
					for node in nodelist:
						if node.nodeType == node.TEXT_NODE:
							rc.append(node.data)
					return ''.join(rc)

				xmldata = protocol.alldata
				xmlstr = str(bytearray(xmldata))
				log.msg("[REST] [SESSION] Received: " + xmlstr)
				dom = xml.dom.minidom.parseString(xmlstr)
				self.sessionid = getText(dom.getElementsByTagName("ConsoleSession")[0].getElementsByTagName("SessionID")[0].childNodes)
				log.msg("[REST] [SESSION] Session ID = " + self.sessionid)

				attrs = { "ID": self._sessionid, "COMMAND": command }
				self.sendCommand2("SessionCommand", attrs, callback)
				self.sendCommand2(url, attrs, callback)

			d.addCallback(request)
			d.addErrback(err)
		else:
			attrs = { "ID": self._sessionid, "COMMAND": command }
			self.sendCommand2("SessionCommand", attrs, callback)

	def sendCommand2(self, url, attrs, callback=None):
		agent = Agent(reactor)
		d = agent.request(
			'POST',
			('http://%s:%d/' % (str(self.externalhost), self.consolePort)) + url,
			Headers({"Content-Type": ["application/x-www-form-urlencoded"]}),
			PostProducer(attrs))

		def request(response):
			log.msg("[REST] Received response")
			callback(response)

		def err(response):
			log.msg("[REST] [ERROR] " + str(response))

		if not callback is None:
			d.addCallback(request)

		d.addErrback(err)

class CommandProtocol(Protocol):
	def __init__(self, size, callback):
		self.size = size
		self.alldata = []
		self.callback = callback

	def dataReceived(self, data):
		self.alldata += data

	def connectionLost(self, reason):
		log.msg("[REST] Connection Lost. Reason: " + str(reason))
		self.callback(self)

#TODO: Remove hard-coded path separators and use path.join

def spawnRobustProcess(opensimdir, externalhost, callOnEnd=None, callOnOutput=None):
	log.msg("Starting ROBUST")

	try:
		os.unlink(opensimdir + '/bin/Robust.log')
	except OSError:
		pass

	p = ConsoleProtocol("ROBUST", opensimdir + '/bin/Robust.log', opensimdir, 18000, externalhost, callOnEnd, callOnOutput)
	spawnMonoProcess(
		p,
		opensimdir + "/bin/" + "Robust.exe",
		['-console', 'rest'],
		opensimdir + "/bin")
	log.msg("Started Robust")
	return p

def spawnRegionProcess(opensimdir, region, consolePort, externalhost, callOnEnd=None, callOnOutput=None):
	log.msg("Starting Region: " + region)

	try:
		os.unlink(opensimdir + '/bin/OpenSim.log')
	except OSError:
		pass

	p = ConsoleProtocol(region, opensimdir + '/bin/OpenSim.log', opensimdir, consolePort, externalhost, callOnEnd, callOnOutput)
	spawnMonoProcess(p, opensimdir + "/bin/" + "OpenSim.exe", [
		'-inimaster="' + opensimdir + '/bin/OpenSim.ini"',
		'-inifile="' + opensimdir + '/bin/Regions/' + region + '.ini"',
		"-console=rest",
		'-name="'+ region + '"'
	], opensimdir + "/bin")
	log.msg("Started region: " + region)
	return p

def spawnMonoProcess(protocol_, name, args, p):
	if os.name == 'nt':
		raise NotImplementedError('Running on Windows is not yet supported.')
	else:
		return spawnProcess(
			'mono',
			["mono", "--debug", name] + args,
			p,
			protocol_)

def spawnProcess(executable, args, workingDir, protocol_=protocol.ProcessProtocol()):
	dump = '[PROCESS] Starting: '
	for arg in args:
		dump += arg + ' '
	log.msg(dump)

	return reactor.spawnProcess(
		protocol_,
		executable,
		args,
		os.environ,
		workingDir)


import telebot

bot = telebot.TeleBot('5626502754:AAG4vkxqLA4WBe8LUEpxSTQ79UZq4G9N-AE')

first = ''
second = ''
result = 0

@bot.message_handler(commands=['start'])
def start(message):
    name = f'Hello, {message.from_user.first_name} I am calculator bot! \n' \
           f'Send me /add, /subtract, /multiply or /divide'
    bot.send_message(message.chat.id, name, parse_mode='html')

def first_num(message):  # asking for the input
    global first
    first = message.text
    bot.send_message(message.from_user.id, f'Please send the first number: ', parse_mode='html')
    bot.register_next_step_handler(message, second_num)

def second_num(message):  # asking for the input
    global second
    second = message.text
    bot.send_message(message.from_user.id, f'Please send the second number: ', parse_mode='html')
    bot.register_next_step_handler(message, operations)

@bot.message_handler(commands=['add'])
def add(message):
    first_num(message)
    global result
    if message == '/add':
        result = int(first) + int(second)
        bot.send_message(message.from_user.id, f'Answer: {result}', parse_mode='html')

@bot.message_handler(commands=['subtract'])
def subtract(message):
    first_num(message)
    global result
    if message == '/subtract':
        result = int(first) - int(second)
        bot.send_message(message.from_user.id, f'Answer: {result}', parse_mode='html')

@bot.message_handler(commands=['multiply'])
def multiply(message):
    first_num(message)
    global result
    if message == '/multiply':
        result = int(first) * int(second)
        bot.send_message(message.from_user.id, f'Answer: {result}', parse_mode='html')

@bot.message_handler(commands=['divide'])
def divide(message):
    first_num(message)
    global result
    if message == '/divide':
        result = int(first) // int(second)
        bot.send_message(message.from_user.id, f'Answer: {result}', parse_mode='html')

@bot.message_handler(commands=['add', 'subtract', 'multiply', 'divide'])
def operations(message):
    if message.text == '/add':
        bot.send_message(message.from_user.id, add(message), parse_mode='html')
    elif message.text == '/subtract':
        bot.send_message(message.from_user.id, subtract(message), parse_mode='html')
    elif message.text == '/multiply':
        bot.send_message(message.from_user.id, multiply(message), parse_mode='html')
    elif message.text == '/divide':
        bot.send_message(message.from_user.id, divide(message), parse_mode='html')
    else:
        bot.send_message(message.from_user.id, 'Please enter the correct operation', parse_mode='html')

bot.polling(none_stop=True)


import sys
import json

writer_data = json.loads(sys.stdin.read())

def write():
     path = writer_data.get('path')
     mimetype = writer_data.get('mimetype', '')
     properties = writer_data.get('properties', '')

     output = open(path, 'w')
     output.write(json.dumps(properties))

     return_status = {}
     return_status['status'] = 'OK'
     return_status['error'] = ''

     print(json.dumps(return_status))

if __name__ == "__main__":
    write()


import tempfile
from django.core.exceptions import ValidationError
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import TestCase
from app_shop.models import Category, Product, PropertyName, PropertyValue, Property
from app_review.models import Review
from app_users.models import User
from app_settings.models import SiteSettings
from app_orders.models import Order, OrderItem

class CategoryTest(TestCase):

    @classmethod
    def setUpTestData(cls):
        category = Category.objects.create(name='test_category')
        category.save()
        product = Product.objects.create(name='test_product', id=1, category=category, price=10, stock=1,
                                         manufacturer='test_manufacturer')
        product.save()
        product2 = Product.objects.create(name='test_product2', id=2, category=category, price=100, stock=1,
                                          manufacturer='test_manufacturer')
        product2.save()

    def test_name_label(self):
        category = Category.objects.get(id=1)
        field_label = category._meta.get_field('name').verbose_name
        self.assertEquals(field_label, 'Название')

    def test_slug_label(self):
        category = Category.objects.get(id=1)
        field_label = category._meta.get_field('slug').verbose_name
        self.assertEquals(field_label, 'Псевдоним для url')

    def test_icon_label(self):
        category = Category.objects.get(id=1)
        field_label = category._meta.get_field('icon').verbose_name
        self.assertEquals(field_label, 'Иконка')

    def test_parent_label(self):
        category = Category.objects.get(id=1)
        field_label = category._meta.get_field('parent').verbose_name
        self.assertEquals(field_label, 'Родительская категория')

    def test_image_label(self):
        category = Category.objects.get(id=1)
        field_label = category._meta.get_field('image').verbose_name
        self.assertEquals(field_label, 'Изображение')

    def test_name_max_length(self):
        category = Category.objects.get(id=1)
        max_length = category._meta.get_field('name').max_length
        self.assertEquals(max_length, 200)

    def test_slug_max_length(self):
        category = Category.objects.get(id=1)
        max_length = category._meta.get_field('slug').max_length
        self.assertEquals(max_length, 200)

    def test_icon_validator_svg(self):
        image = SimpleUploadedFile(tempfile.NamedTemporaryFile(suffix=".jpg").name, b"file_content",
                                   content_type="image/jpeg")
        category = Category.objects.create(name='test_category2', icon=image)
        with self.assertRaises(ValidationError, msg='Файл не svg'):
            category.full_clean()

    def test_get_min(self):
        category = Category.objects.get(id=1)
        self.assertEquals(category.get_min(), 10)

class ProductTest(TestCase):

    @classmethod
    def setUpTestData(cls):
        user = User.objects.create(email='test@test.ru')
        user.set_password('12345')
        user.save()
        category = Category.objects.create(name='test_category')
        category.save()
        settings = SiteSettings.objects.create(cost_express=500, edge_for_free_delivery=2000,
                                               cost_usual_delivery=200,
                                               root_category=category)
        settings.save()
        product = Product.objects.create(name='test_product', id=1, category=category, price=10, stock=1,
                                         manufacturer='test_manufacturer')
        product.save()
        review = Review.objects.create(user=user, product=product, text='test_review')
        review.save()
        order = Order.objects.create(user=user, address='test_address', city='test_city', card_number=888888888)
        order.save()
        order_item = OrderItem.objects.create(order=order, product=product, price=10, quantity=10)
        order_item.save()

    def test_category_label(self):
        product = Product.objects.get(id=1)
        field_label = product._meta.get_field('category').verbose_name
        self.assertEquals(field_label, 'Категория')

    def test_name_label(self):
        product = Product.objects.get(id=1)
        field_label = product._meta.get_field('name').verbose_name
        self.assertEquals(field_label, 'Название')

    def test_slug_label(self):
        product = Product.objects.get(id=1)
        field_label = product._meta.get_field('slug').verbose_name
        self.assertEquals(field_label, 'Псевдоним для url')

    def test_description_label(self):
        product = Product.objects.get(id=1)
        field_label = product._meta.get_field('description').verbose_name
        self.assertEquals(field_label, 'Описание')

    def test_price_label(self):
        product = Product.objects.get(id=1)
        field_label = product._meta.get_field('price').verbose_name
        self.assertEquals(field_label, 'Цена')

    def test_stock_label(self):
        product = Product.objects.get(id=1)
        field_label = product._meta.get_field('stock').verbose_name
        self.assertEquals(field_label, 'Остаток')

    def test_available_label(self):
        product = Product.objects.get(id=1)
        field_label = product._meta.get_field('available').verbose_name
        self.assertEquals(field_label, 'Активен')

    def test_created_label(self):
        product = Product.objects.get(id=1)
        field_label = product._meta.get_field('created').verbose_name
        self.assertEquals(field_label, 'Создан')

    def test_updated_label(self):
        product = Product.objects.get(id=1)
        field_label = product._meta.get_field('updated').verbose_name
        self.assertEquals(field_label, 'Обновлен')

    def test_manufacturer_label(self):
        product = Product.objects.get(id=1)
        field_label = product._meta.get_field('manufacturer').verbose_name
        self.assertEquals(field_label, 'Производитель')

    def test_limited_label(self):
        product = Product.objects.get(id=1)
        field_label = product._meta.get_field('limited').verbose_name
        self.assertEquals(field_label, 'Ограниченная серия')

    def test_name_max_length(self):
        product = Product.objects.get(id=1)
        max_length = product._meta.get_field('name').max_length
        self.assertEquals(max_length, 200)

    def test_slug_max_length(self):
        product = Product.objects.get(id=1)
        max_length = product._meta.get_field('slug').max_length
        self.assertEquals(max_length, 200)

    def test_price_max_digits(self):
        product = Product.objects.get(id=1)
        max_digits = product._meta.get_field('price').max_digits
        self.assertEquals(max_digits, 10)

    def test_price_decimal_places(self):
        product = Product.objects.get(id=1)
        decimal_places = product._meta.get_field('price').decimal_places
        self.assertEqual(decimal_places, 2)

    def test_manufacturer_max_length(self):
        product = Product.objects.get(id=1)
        max_length = product._meta.get_field('manufacturer').max_length
        self.assertEquals(max_length, 50)

    def test_in_stock(self):
        product = Product.objects.get(id=1)
        self.assertTrue(product.in_stock)

    def test_total_review(self):
        product = Product.objects.get(id=1)
        self.assertEqual(1, product.total_review)

    def test_free_delivery(self):
        product = Product.objects.get(id=1)
        self.assertFalse(product.free_delivery)

    def test_total_sale(self):
        product = Product.objects.get(id=1)
        self.assertEqual(10, product.total_sale)

    def test_str(self):
        product = Product.objects.get(id=1)
        self.assertEqual(str(product), product.name)

class PropertyNameTest(TestCase):

    @classmethod
    def setUpTestData(cls):
        category = Category.objects.create(name='test_category')
        category.save()
        product = Product.objects.create(name='test_product', id=1, category=category, price=10, stock=1,
                                         manufacturer='test_manufacturer')
        product.save()
        property = PropertyName.objects.create(name='test_property')
        property.save()

    def test_name_label(self):
        property = PropertyName.objects.get(id=1)
        field_label = property._meta.get_field('name').verbose_name
        self.assertEquals(field_label, 'характеристика')

    def test_name_max_length(self):
        property = PropertyName.objects.get(id=1)
        max_length = property._meta.get_field('name').max_length
        self.assertEquals(max_length, 255)

class PropertyValueTest(TestCase):

    @classmethod
    def setUpTestData(cls):
        category = Category.objects.create(name='test_category')
        category.save()
        product = Product.objects.create(name='test_product', id=1, category=category, price=10, stock=1,
                                         manufacturer='test_manufacturer')
        product.save()
        value = PropertyValue.objects.create(value='test_value')
        value.save()

    def test_value_label(self):
        value = PropertyValue.objects.get(id=1)
        field_label = value._meta.get_field('value').verbose_name
        self.assertEquals(field_label, 'значение')

    def test_value_max_length(self):
        value = PropertyValue.objects.get(id=1)
        max_length = value._meta.get_field('value').max_length
        self.assertEquals(max_length, 255)

class PropertyTest(TestCase):

    @classmethod
    def setUpTestData(cls):
        category = Category.objects.create(name='test_category')
        category.save()
        product = Product.objects.create(name='test_product', id=1, category=category, price=10, stock=1,
                                         manufacturer='test_manufacturer')
        product.save()
        value = PropertyValue.objects.create(value='test_value')
        value.save()
        property_name = PropertyName.objects.create(name='test_property')
        property_name.save()
        property = Property.objects.create(property=property_name, product=product, value=value)
        property.save()

    def test_property_label(self):
        property = Property.objects.get(id=1)
        field_label = property._meta.get_field('property').verbose_name
        self.assertEquals(field_label, 'название')

    def test_product_label(self):
        property = Property.objects.get(id=1)
        field_label = property._meta.get_field('product').verbose_name
        self.assertEquals(field_label, 'товар')

    def test_value_label(self):
        property = Property.objects.get(id=1)
        field_label = property._meta.get_field('value').verbose_name
        self.assertEquals(field_label, 'значение')

    def test_str(self):
        property = Property.objects.get(id=1)
        self.assertEqual(str(property), f'{property.property.name}: {property.value.value}')


import codecs
import re

from .compat import IS_TYPE_CHECKING, to_text

if IS_TYPE_CHECKING:
    from typing import (  # noqa:F401
        IO, Iterator, Match, NamedTuple, Optional, Pattern, Sequence, Text,
        Tuple
    )

def make_regex(string, extra_flags=0):
    # type: (str, int) -> Pattern[Text]
    return re.compile(to_text(string), re.UNICODE | extra_flags)

_newline = make_regex(r"(\r\n|\n|\r)")
_multiline_whitespace = make_regex(r"\s*", extra_flags=re.MULTILINE)
_whitespace = make_regex(r"[^\S\r\n]*")
_export = make_regex(r"(?:export[^\S\r\n]+)?")
_single_quoted_key = make_regex(r"'([^']+)'")
_unquoted_key = make_regex(r"([^=\#\s]+)")
_equal_sign = make_regex(r"(=[^\S\r\n]*)")
_single_quoted_value = make_regex(r"'((?:\\'|[^'])*)'")
_double_quoted_value = make_regex(r'"((?:\\"|[^"])*)"')
_unquoted_value_part = make_regex(r"([^ \r\n]*)")
_comment = make_regex(r"(?:[^\S\r\n]*#[^\r\n]*)?")
_end_of_line = make_regex(r"[^\S\r\n]*(?:\r\n|\n|\r|$)")
_rest_of_line = make_regex(r"[^\r\n]*(?:\r|\n|\r\n)?")
_double_quote_escapes = make_regex(r"\\[\\'\"abfnrtv]")
_single_quote_escapes = make_regex(r"\\[\\']")

try:
    # this is necessary because we only import these from typing
    # when we are type checking, and the linter is upset if we
    # re-import
    import typing

    Original = typing.NamedTuple(
        "Original",
        [
            ("string", typing.Text),
            ("line", int),
        ],
    )

    Binding = typing.NamedTuple(
        "Binding",
        [
            ("key", typing.Optional[typing.Text]),
            ("value", typing.Optional[typing.Text]),
            ("original", Original),
            ("error", bool),
        ],
    )
except ImportError:
    from collections import namedtuple
    Original = namedtuple(  # type: ignore
        "Original",
        [
            "string",
            "line",
        ],
    )
    Binding = namedtuple(  # type: ignore
        "Binding",
        [
            "key",
            "value",
            "original",
            "error",
        ],
    )

class Position:
    def __init__(self, chars, line):
        # type: (int, int) -> None
        self.chars = chars
        self.line = line

    @classmethod
    def start(cls):
        # type: () -> Position
        return cls(chars=0, line=1)

    def set(self, other):
        # type: (Position) -> None
        self.chars = other.chars
        self.line = other.line

    def advance(self, string):
        # type: (Text) -> None
        self.chars += len(string)
        self.line += len(re.findall(_newline, string))

class Error(Exception):
    pass

class Reader:
    def __init__(self, stream):
        # type: (IO[Text]) -> None
        self.string = stream.read()
        self.position = Position.start()
        self.mark = Position.start()

    def has_next(self):
        # type: () -> bool
        return self.position.chars < len(self.string)

    def set_mark(self):
        # type: () -> None
        self.mark.set(self.position)

    def get_marked(self):
        # type: () -> Original
        return Original(
            string=self.string[self.mark.chars:self.position.chars],
            line=self.mark.line,
        )

    def peek(self, count):
        # type: (int) -> Text
        return self.string[self.position.chars:self.position.chars + count]

    def read(self, count):
        # type: (int) -> Text
        result = self.string[self.position.chars:self.position.chars + count]
        if len(result) < count:
            raise Error("read: End of string")
        self.position.advance(result)
        return result

    def read_regex(self, regex):
        # type: (Pattern[Text]) -> Sequence[Text]
        match = regex.match(self.string, self.position.chars)
        if match is None:
            raise Error("read_regex: Pattern not found")
        self.position.advance(self.string[match.start():match.end()])
        return match.groups()

def decode_escapes(regex, string):
    # type: (Pattern[Text], Text) -> Text
    def decode_match(match):
        # type: (Match[Text]) -> Text
        return codecs.decode(match.group(0), 'unicode-escape')  # type: ignore

    return regex.sub(decode_match, string)

def parse_key(reader):
    # type: (Reader) -> Optional[Text]
    char = reader.peek(1)
    if char == "#":
        return None
    elif char == "'":
        (key,) = reader.read_regex(_single_quoted_key)
    else:
        (key,) = reader.read_regex(_unquoted_key)
    return key

def parse_unquoted_value(reader):
    # type: (Reader) -> Text
    value = u""
    while True:
        (part,) = reader.read_regex(_unquoted_value_part)
        value += part
        after = reader.peek(2)
        if len(after) < 2 or after[0] in u"\r\n" or after[1] in u" #\r\n":
            return value
        value += reader.read(2)

def parse_value(reader):
    # type: (Reader) -> Text
    char = reader.peek(1)
    if char == u"'":
        (value,) = reader.read_regex(_single_quoted_value)
        return decode_escapes(_single_quote_escapes, value)
    elif char == u'"':
        (value,) = reader.read_regex(_double_quoted_value)
        return decode_escapes(_double_quote_escapes, value)
    elif char in (u"", u"\n", u"\r"):
        return u""
    else:
        return parse_unquoted_value(reader)

def parse_binding(reader):
    # type: (Reader) -> Binding
    reader.set_mark()
    try:
        reader.read_regex(_multiline_whitespace)
        if not reader.has_next():
            return Binding(
                key=None,
                value=None,
                original=reader.get_marked(),
                error=False,
            )
        reader.read_regex(_export)
        key = parse_key(reader)
        reader.read_regex(_whitespace)
        if reader.peek(1) == "=":
            reader.read_regex(_equal_sign)
            value = parse_value(reader)  # type: Optional[Text]
        else:
            value = None
        reader.read_regex(_comment)
        reader.read_regex(_end_of_line)
        return Binding(
            key=key,
            value=value,
            original=reader.get_marked(),
            error=False,
        )
    except Error:
        reader.read_regex(_rest_of_line)
        return Binding(
            key=None,
            value=None,
            original=reader.get_marked(),
            error=True,
        )

def parse_stream(stream):
    # type: (IO[Text]) -> Iterator[Binding]
    reader = Reader(stream)
    while reader.has_next():
        yield parse_binding(reader)


from itertools import product

import numpy as np
import pandas as pd

# map JRC/our sectors to hotmaps sector, where mapping exist
sector_mapping = {
    "Electric arc": "Iron and steel",
    "Integrated steelworks": "Iron and steel",
    "DRI + Electric arc": "Iron and steel",
    "Ammonia": "Chemical industry",
    "Basic chemicals (without ammonia)": "Chemical industry",
    "Other chemicals": "Chemical industry",
    "Pharmaceutical products etc.": "Chemical industry",
    "Cement": "Cement",
    "Ceramics & other NMM": "Non-metallic mineral products",
    "Glass production": "Glass",
    "Pulp production": "Paper and printing",
    "Paper production": "Paper and printing",
    "Printing and media reproduction": "Paper and printing",
    "Alumina production": "Non-ferrous metals",
    "Aluminium - primary production": "Non-ferrous metals",
    "Aluminium - secondary production": "Non-ferrous metals",
    "Other non-ferrous metals": "Non-ferrous metals",
}

def build_nodal_industrial_energy_demand():
    fn = snakemake.input.industrial_energy_demand_per_country_today
    industrial_demand = pd.read_csv(fn, header=[0, 1], index_col=0)

    fn = snakemake.input.industrial_distribution_key
    keys = pd.read_csv(fn, index_col=0)
    keys["country"] = keys.index.str[:2]

    nodal_demand = pd.DataFrame(
        0.0, dtype=float, index=keys.index, columns=industrial_demand.index
    )

    countries = keys.country.unique()
    sectors = industrial_demand.columns.levels[1]

    for country, sector in product(countries, sectors):
        buses = keys.index[keys.country == country]
        mapping = sector_mapping.get(sector, "population")

        key = keys.loc[buses, mapping]
        demand = industrial_demand[country, sector]

        outer = pd.DataFrame(
            np.outer(key, demand), index=key.index, columns=demand.index
        )

        nodal_demand.loc[buses] += outer

    nodal_demand.index.name = "TWh/a"

    nodal_demand.to_csv(snakemake.output.industrial_energy_demand_per_node_today)

if __name__ == "__main__":
    if "snakemake" not in globals():
        from _helpers import mock_snakemake

        snakemake = mock_snakemake(
            "build_industrial_energy_demand_per_node_today",
            simpl="",
            clusters=48,
        )

    build_nodal_industrial_energy_demand()


__author__ = "Will Hannon"
__copyright__ = "Copyright 2020 Will Hannon"
__email__ = "wwh22@uw.edu"
__license__ = "MIT"

## === Import Libraries === ##
import pysam #count variant alleles from BAM
import pandas as pd #data frames
import numpy as np #arrays
import os #interacting with files
from Bio import SeqIO #reading fasta format
import re #regular expressions

## === Functions === ##

def check_read(read):
    """
    Helper function to decide what reads should
    be keep when parsing alignment file with `pysam`.

    Parameters
    ----------
    read : AlignedSegment
        read from alignment file parsed with `pysam`.

    Returns
    -------
    bool
        True/False if read should be included

    """
    # Exclude Quality Failures
    if read.is_qcfail:
        return False
    # Exclude Secondary Mappings
    if read.is_secondary:
        return False
    # Exclude Unmapped Reads
    if read.is_unmapped:
        return False
    else:
        return True

def get_day(path):
    """
    If the samples are downloaded from the SRA, use the
    SRA accession to get the day the sample was collected on.

    Parameters
    ----------
    path : str
        path to the sample

    Returns
    -------
    int
        the day the sample was collected

    """

    day_mapping = {
    'SRR13160722':152,
    'SRR13160723':146,
    'SRR13160724':143,
    'SRR13160725':130,
    'SRR13160726':128,
    'SRR13160727':81,
    'SRR13160728':75,
    'SRR13160729':25,
    'SRR13160730':18
    }

    basename = os.path.basename(path)

    regex = re.compile("(?P<run>SRR\d+)\.BWA")

    m = regex.match(basename)

    return day_mapping[str(m.group('run'))]

def phase_variants(aa_change_list, joined_spike_count_df, filepath, contig = "NC_045512.2"):
    """
    Uses a list of variant alleles, a data-frame of SNPs corresponding to BAM, and an alignment file to make a N (SNPs) X P (Reads)
    dataframe with the read support for every obseved phase of varaints.

    Parameters
    ----------
    joined_spike_count_df : Pandas.DataFrame
        Dataframe with SNPs summarized over a region.

    filepath : str
        path to the bam file to be parsed

    aa_change_list : list
        a list of alleles to phase

    conitg : str
        name of the chromosome, in this case the whole reference.

    Returns
    -------
    Pandas.DataFrame
       A Pandas.DataFrame of 0, 1, or NaN, describing read support for a given phase.

    """

    ## ===== Format inputs and get SNP list ===== ##

    # Get the SNPs to query over specific day
    SNPs_df = joined_spike_count_df[joined_spike_count_df.AA_CHANGE.isin(aa_change_list)]
    SNPs_set = set(pair for pair in zip(SNPs_df.POS, SNPs_df.ALT))

    ## ===== Move through the BAM file and get haplotypes ===== ##

    # Save the haplotypes in a dictionary
    haplotype_dict = {}

    # Get the start and stop
    start = sorted([pos for pos, alt in SNPs_set])[0]
    stop = sorted([pos for pos, alt in SNPs_set])[-1]

    # Open alignment with pysam
    with pysam.AlignmentFile(filepath, "rb") as bamfile:

        # Get the pileup column for a specific region
        for pileupcolumn in bamfile.pileup(contig, start = start, stop = stop, stepper = 'nofilter'):

            # Check the if the position has a target SNP (converted to 0-indexed)
            if pileupcolumn.pos in [pos-1 for pos, alt in SNPs_set]:

                # Iterate over every alignment in the pileup column.
                for pileupread in pileupcolumn.pileups:

                    # Check if the read is valid and can be parsed
                    if check_read(pileupread.alignment) and not pileupread.is_del and not pileupread.is_refskip:

                        # Save the query name
                        qname = pileupread.alignment.query_name

                        # Save the 1-indexed position
                        pos = pileupcolumn.pos + 1

                        # Save the base at that position in the read
                        alt = pileupread.alignment.query_sequence[pileupread.query_position]

                        # Check if this read has the SNP or not
                        if (pos, alt) in SNPs_set:
                            phase = 1 # The read has the SNP

                        else:
                            phase = 0 # The read doesn't have the SNP

                        # Add the readname to the dictionary
                        if qname in haplotype_dict.keys():
                            haplotype_dict[qname].append((pos, phase, alt))
                        else:
                            haplotype_dict[qname] = [(pos, phase, alt)]

    ## ===== Convert the haplotype dictionary to a dataframe filling in missings ===== ##

    # Save the haplotypes with missing values (not covered by reads) filled
    completed_haplotype_dict = {}

    # Iterate over the haplotype dictionary created above
    for qname, alleles in haplotype_dict.items():

        # Make a dictionary to fill with observed SNPs for each read
        aa_dict = {tup:"-" for tup in SNPs_set}

        # Iterate over all observed alleles
        for allele in alleles:

            # If it's 1, then an allele was observed
            if allele[1] == 1:
                # Add this observation based on the key
                aa_dict[(allele[0], allele[2])] = 1
            # If it's a wild type allele
            elif allele[1] == 0:
                # Check every possible wt allele
                for key in aa_dict.keys():
                    # Check by position
                    if key[0] == allele[0]:
                        # If it's the same positon add a 0
                        aa_dict[key] = 0

        # Add this populated dictionary to the haplotype dictionary
        completed_haplotype_dict[qname] = aa_dict

    # Convert this dicitonary into a dataframe and take the transpose
    haplotype_df = pd.DataFrame(completed_haplotype_dict).T

    ## ===== Format the dataframe ===== ##

    # * FIX THE BUG WITH ALLELES IN SAME BASE *

    # Dict to format the column names
    aa_conversion = {pair:aa for pair, aa in zip(zip(SNPs_df.POS, SNPs_df.ALT),SNPs_df.AA_CHANGE)}

    # Flatten the column indicies
    haplotype_df.columns = [aa_conversion[(pos,alt)] for pos,alt in haplotype_df.columns.values]

    # Replace the '-' with 'NaN'
    haplotype_df = haplotype_df.replace('-', np.nan)

    # Return the phased SNP df
    return haplotype_df

## === Main === ##

# Input path to SNP df
inputpath_csv = str(snakemake.input.csv)
inputpath_bam = str(snakemake.input.bam)

# Output path to csv file
outpath = str(snakemake.output)

# Name of sample
accession = os.path.basename(inputpath_bam)

# Get the day of the sample
if accession.startswith("Day"):

    regex = re.compile("Day(?P<Day>\d+)_")
    m = regex.match(accession)
    day = int(m.group('Day'))

else:

   day = get_day(inputpath)

# Read in the join_calling df
input_snp_df = pd.read_csv(inputpath_csv)

# List of SNPs to phase
snp_list = ["N440D", "E484A", "F486I", "F486L", "Y489H", "Q493K"]

# Phase the SNPs
haplotypes_df = phase_variants(snp_list, input_snp_df, inputpath_bam, contig = "NC_045512.2")

# Add accession and day to data frame
haplotypes_df.insert(0, 'ACCESSION', accession)
haplotypes_df.insert(1, 'DAY', day)

# Write out the results
haplotypes_df.to_csv(outpath, index=False)


# -*- encoding: utf-8 -*-
import pyaudio
import threading
from google.cloud import speech
from voicebot.utils.gcp_utils import get_credentials

CHUNK_SIZE = 1024
RATE = 16000
pyaudio_conf = {
    'format': pyaudio.paInt16,
    'channels': 1,
    'rate': RATE,
    'frames_per_buffer': CHUNK_SIZE,
    'input': True
}

def _init_stt_context():
    p = None
    def stt_init():
        nonlocal p
        p = pyaudio.PyAudio()
    def stt_stop():
        nonlocal p
        p.terminate()
    def recording_stream():
        nonlocal p
        return p.open(**pyaudio_conf)
    return (stt_init, stt_stop, recording_stream)

init, stop, get_recording_stream = _init_stt_context()

def is_enter_pressed(msg):
    t = None
    def _myinput():
        input(msg)
    def _is_enter_pressed():
        nonlocal t
        if t:
            if t.is_alive():
                return False
            else:
                t = None
                return True
        else:
            t = threading.Thread(target=_myinput)
            t.start()
            return False

    return _is_enter_pressed

def recording_message(is_recording):
    if is_recording:
        print('\nRecording started')
    else:
        print('\nRecording stopped')

def ready_message(is_prompting):
    if is_prompting:
        print('\nReady to start recording...', end='')

def record_voice(start_recording=is_enter_pressed('Press <Enter> to start...'), stop_recording=is_enter_pressed('Press <Enter> to stop...'), ready_prompt=ready_message, recording_prompt=recording_message):
    """
    The function to get the recorded voice.

    Arguments:
    start_recording -- the non-blocking function that returns True if the recorder can be started.
    stop_recording -- the non-blocking function that returns True if the recorder should be stopped.
    ready_prompt -- the function that prints the message when it is ready to record voice (when True)
    recording_prompt -- The function to show prompt message/indicator when recording (True) or when not recording (False)
    """
    ready_prompt(True)
    while not start_recording():
        pass
    ready_prompt(False)
    recording_prompt(True)
    stream = get_recording_stream()
    frames = []
    while not stop_recording():
        data = stream.read(CHUNK_SIZE)
        frames.append(data)
    stream.stop_stream()
    stream.close()
    recording_prompt(False)

    return b''.join(frames)

def google_stt(voice_data, lang='zh-tw'):
    audio = speech.RecognitionAudio(content=voice_data)
    config = speech.RecognitionConfig(
        encoding = speech.RecognitionConfig.AudioEncoding.LINEAR16,
        sample_rate_hertz = RATE,
        language_code = lang,
        enable_automatic_punctuation = True
    )
    client = speech.SpeechClient(credentials=get_credentials('google-stt.json'))
    response = client.recognize(config=config, audio=audio)
    if len(response.results)==0:
        return ''
    else:
        return response.results[0].alternatives[0].transcript


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

"""
    Provide command line analysis and verification
"""

import argparse
import os

def read_args(args):
    """ Read cli argument """

    parser = __create_parser()
    arg = vars(parser.parse_args(args))

    return arg

def __create_parser():
    """ Create the args parser """

    parser = argparse.ArgumentParser(
        usage       = "./%(prog)s [-h] [--example] [--c '#'] [-b '@export'] [-m '<TAG2INCLUDE>'] [--tagname '@name'] [--tagdesc '@title'] -i DIRECTORY \n",
        description = "Automatic update of documentation for R scripts",
        formatter_class = argparse.ArgumentDefaultsHelpFormatter
    )

    parser.add_argument("--example", action = "store_true", help = "Run example")
    parser.add_argument("--comment", default = "#\'", help = "define comment symbol")
    parser.add_argument("-i", type = __isdir, required = True,
        help = "Directory where the files to update are located")
    parser.add_argument("-b", "--block", default = "@export",
        help = "tag for filtering comment blocks to insert")
    parser.add_argument("-m", "--main", default = "<TAG2INCLUDE>",
        help = "tag for finding the main comment block where the --blocks will be inserted")
    parser.add_argument("--tagname", default = "@name",
        help = "in a bloc, tag to identify function name")
    parser.add_argument("--tagdesc", default = "@title",
        help = "in a bloc, tag to identify function desc")
    return parser

def __isfile(val):
    """Check  if the path is leading to a file."""
    val = str(val)

    if not os.path.isfile(val):
        raise argparse.ArgumentTypeError(val + " is not a file")

    return val

def __isdir(val):
    """Check  if the path is leading to a directory."""
    val = str(val)

    if not os.path.isdir(val):
        raise argparse.ArgumentTypeError(val + " is not a directory")

    return val


# 10001st prime
# Problem 7
# By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

# What is the 10 001st prime number?

# BRUTE FORCE
def is_prime(n):
    for i in range(2, n):
        if n % i == 0:
            return False
            break
    return True

c = 0
for i in range(2, 120000):
    if is_prime(i):
        c += 1
        print(c, ': ', i)
        if c == 10001:
            print(c, ':', i)
            break

#ELEGENT

import itertools
def erat2( ):
    D = {  }
    yield 2
    for q in itertools.islice(itertools.count(3), 0, None, 2):
        p = D.pop(q, None)
        if p is None:
            D[q*q] = q
            yield q
        else:
            x = p + q
            while x in D or not (x&1):
                x += p
            D[x] = p

def get_primes_erat(n):
  return list(itertools.takewhile(lambda p: p<n, erat2()))

res = get_primes_erat(104749)
print(len(res))


from flask import Flask
from .s3_obj_upload.basic.views import basic
from .s3_obj_upload.form_data.views import form_data
from .s3_obj_upload.pre_signed_url.views import pre_signed_url

app = Flask(__name__)

app.register_blueprint(basic)
app.register_blueprint(form_data)
app.register_blueprint(pre_signed_url)

@app.before_request
def before_request():
    return

@app.after_request
def after_request(response):
    print(f'response: {response}')
    response.headers['Access-Control-Allow-Origin'] = '*'
    response.headers['Access-Control-Allow-Methods'] = ['POST']
    return response

@app.route('/', methods=['GET'])
def main():
    return 'main'


import cv2
import numpy as np
from config import *

img1 = cv2.imread(PATH_ACTUAL)
img2 = cv2.imread(PATH_PLANOGRAM)

img1 = cv2.resize(img1, (1920, 1440))
img2 = cv2.resize(img2, (1920, 1440))

#ORB Detector
orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(img1, None)
kp2, des2 = orb.detectAndCompute(img2, None)

#Brute Force
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck = True)
matches = bf.match(des1, des2)
matches = sorted(matches, key=lambda x:x.distance)

matching_result = cv2.drawMatches(img1, kp1, img2, kp2, matches[:50], None)

# cv2.imshow("img1",img1)
# cv2.imshow("img2",img2)
cv2.imshow("matching result", cv2.resize(matching_result, None, fx=0.4, fy=0.4))
cv2.imwrite("../output/ORB_TOP_50_MATCHES.png", matching_result)
cv2.waitKey(0)


import json
import boto3
from boto3.dynamodb.types import TypeDeserializer, TypeSerializer

dynamodb = boto3.client('dynamodb')

# Define a custom deserializer to convert DynamoDB items to Python dictionaries
deserializer = TypeDeserializer()

# Define a custom serializer to convert Python dictionaries to DynamoDB items
serializer = TypeSerializer()

def create_conversation(conversation):
    response = dynamodb.put_item(
        TableName='chats',
        Item={
            'conversation_id': serializer.serialize(conversation['conversation_id']),
            'messages': serializer.serialize(conversation['messages'])
        }
    )
    return response

def update_conversation(conversation):
    response = dynamodb.update_item(
        TableName='chats',
        Key={
            'conversation_id': serializer.serialize(conversation['conversation_id'])
        },
        UpdateExpression='SET messages = :messages',
        ExpressionAttributeValues={
            ':messages': serializer.serialize(conversation['messages'])
        }
    )
    return response

def get_conversation(conversation_id):
    response = dynamodb.get_item(
        TableName='chats',
        Key={
            'conversation_id': serializer.serialize(conversation_id)
        }
    )
    item = response.get('Item')
    if item:
        return {
            'conversation_id': deserializer.deserialize(item['conversation_id']),
            'messages': deserializer.deserialize(item['messages'])
        }
    return None