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))


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Mar  6 14:03:24 2022

@author: felipearrospide
"""

import geopandas as gpd
from matplotlib import pyplot as plt

# geopandas trabaja con archivos .shp, .shx, .geojson

regiones = gpd.read_file('REGIONES_2020.shp')

fig = plt.figure()
ax = fig.add_subplot(111)
regiones.plot(ax=ax, fc='none',ec='black')
plt.title('Regiones de Chile')
ax.set_xlabel('Longitud')
ax.set_ylabel('Latitud')
plt.show()


import numpy as np
import matplotlib.pyplot as plt
from pyunicorn import climate
#import xarray as xr

DATA_FILENAME = 'data/_Clim_Pred_LRF_New_RF25_IMD0p251982.nc'
#DATA_FILENAME = xr.open_mfdataset(PATH)

#  Type of data file ("NetCDF" indicates a NetCDF file with data on a regular
#  lat-lon grid, "iNetCDF" allows for arbitrary grids - > see documentation).
#  For example, the "NetCDF" FILE_TYPE is compatible with data from the IPCC
#  AR4 model ensemble or the reanalysis data provided by NCEP/NCAR.
FILE_TYPE = "NetCDF"

#  Indicate data source (optional)
DATA_SOURCE = "Rajeevan_Dataset_1999"

#  Name of observable in NetCDF file ("air" indicates surface air temperature)
#  in NCEP/NCAR reanalysis data)
OBSERVABLE_NAME = "RAINFALL"

#  Select a subset in time and space from the data (e.g., a particular region
#  or a particular time window, or both)
#WINDOW = {"time_min": 0., "time_max": 0., "lat_min": 0, "lon_min": 0,
 #         "lat_max": 30, "lon_max": 0}  # selects the whole data set

#  Indicate the length of the annual cycle in the data (e.g., 12 for monthly
#  data). This is used for calculating climatological anomaly values
#  correctly.
TIME_CYCLE = 12

#  Related to climate network construction

#  For setting fixed threshold
THRESHOLD = 0.5

#  For setting fixed link density
LINK_DENSITY = 0.005

#  Indicates whether to use only data from winter months (DJF) for calculating
#  correlations
WINTER_ONLY = False

#
#  Print script title
#

print("\n")
print("Tutorial on how to use climate")
print("-------------------------------")
print("\n")

#
#  Create a ClimateData object containing the data and print information
#

data = climate.ClimateData.Load(
    file_name=DATA_FILENAME, observable_name=OBSERVABLE_NAME,
    data_source=DATA_SOURCE, file_type=FILE_TYPE,
    window=None, time_cycle=TIME_CYCLE)

#  Print some information on the data set
print(data)

map_plots = climate.MapPlots(data.grid, DATA_SOURCE)

#build network using mutual information
net = climate.MutualInfoClimateNetwork(
   data, threshold=THRESHOLD, winter_only=WINTER_ONLY)

print("Link density:", net.link_density)

#  Get degree
degree = net.degree()
#  Get closeness
closeness = net.closeness()
#  Get betweenness
betweenness = net.betweenness()
#  Get local clustering coefficient
clustering = net.local_clustering()
#  Get average link distance
ald = net.average_link_distance()
#  Get maximum link distance
mld = net.max_link_distance()

#
#  Save results to text file
#

#  Save the grid (mainly vertex coordinates) to text files
data.grid.save_txt(filename="1982_0.25x0.25_grid.txt")

#  Save the degree sequence. Other measures may be saved similarly.
np.savetxt("1982_0.25x0.25_degree.txt", degree)

#
#  Plotting
#

#  Comment everything below if you are not using pyngl for plotting!

#  Add network measures to the plotting queue
map_plots.add_dataset("Degree", degree)
map_plots.add_dataset("Closeness", closeness)
map_plots.add_dataset("Betweenness (log10)", np.log10(betweenness + 1))
map_plots.add_dataset("Clustering", clustering)
map_plots.add_dataset("Average link distance", ald)
map_plots.add_dataset("Maximum link distance", mld)

#  Change the map projection
map_plots.resources.mpProjection = "Robinson"
map_plots.resources.mpCenterLonF = 0

#  Change the levels of contouring
map_plots.resources.cnLevelSelectionMode = "EqualSpacedLevels"
map_plots.resources.cnMaxLevelCount = 20

# map_plots.resources.cnRasterSmoothingOn = True
# map_plots.resources.cnFillMode = "AreaFill"

map_plots.generate_map_plots(file_name="1982_0.25x0.25_climate_network_measures",
                             title_on=False, labels_on=True)


# 滤波器
import torch
import torch.nn as nn
import torch.nn.functional as F
# a = torch.Tensor([[2,1,6]])
# # softmax
# print(F.softmax(a,dim=1))
#
# # logsoftmax
# logsoftmax = nn.LogSoftmax()
# print(logsoftmax(a))
# # 负对数似然损失
# target1=torch.Tensor([0]).long()
# target2 = torch.Tensor([1]).long()
# target3 = torch.Tensor([2]).long()
# print(target1,target2,target3)
# # 测试   取出a中对应target位置的值并取负号
# n1 = F.nll_loss(a,target1)
# print(n1)
# n2 = F.nll_loss(a,target2)
# print(n2)
# n3 = F.nll_loss(a,target3)
# print(n3)
#
# # # nn.CrossEntropy()是nn.LogSoftmax()和nn.NLLLoss的结合
# n4 = F.cross_entropy(a,target3)
# print(n4)
#分类正确的是正样本，分类错误的是负样本。用来增加模型的健壮性

import torch
import torch.nn as nn
import torch.nn.functional as F
class focal_loss(nn.Module):
    def __init__(self, alpha=0.25, gamma=2, num_classes = 5, size_average=True):

        super(focal_loss,self).__init__()
        self.size_average = size_average
        if isinstance(alpha,list):
            assert len(alpha)==num_classes   # α可以以list方式输入,size:[num_classes] 用于对不同类别精细地赋予权重
            print("Focal_loss alpha = {}, 将对每一类权重进行精细化赋值".format(alpha))
            self.alpha = torch.Tensor(alpha)
        else:
            assert alpha<1   #如果α为一个常数,则降低第一类的影响,在目标检测中为第一类
            print(" --- Focal_loss alpha = {} ,将对背景类进行衰减,请在目标检测任务中使用 --- ".format(alpha))
            self.alpha = torch.zeros(num_classes)
            self.alpha[0] += alpha
            self.alpha[1:] += (1-alpha) # α 最终为 [ α, 1-α, 1-α, 1-α, 1-α, ...] size:[num_classes]
        self.gamma = gamma

    def forward(self, preds, labels):
        """
        focal_loss损失计算
        :param preds:   预测类别. size:[B,N,C] or [B,C]    分别对应与检测与分类任务, B 批次, N检测框数, C类别数
        :param labels:  实际类别. size:[B,N] or [B]
        :return:
        """
        # assert preds.dim()==2 and labels.dim()==1
        preds = preds.view(-1,preds.size(-1))
        self.alpha = self.alpha.to(preds.device)
        preds_softmax = F.softmax(preds, dim=1) # 这里并没有直接使用log_softmax, 因为后面会用到softmax的结果(当然你也可以使用log_softmax,然后进行exp操作)
        preds_logsoft = torch.log(preds_softmax)
        preds_softmax = preds_softmax.gather(1,labels.view(-1,1))   # 这部分实现nll_loss ( crossempty = log_softmax + nll )
        preds_logsoft = preds_logsoft.gather(1,labels.view(-1,1))
        self.alpha = self.alpha.gather(0,labels.view(-1))
        loss = -torch.mul(torch.pow((1-preds_softmax), self.gamma), preds_logsoft)  # torch.pow((1-preds_softmax), self.gamma) 为focal loss中 (1-pt)**γ
        loss = torch.mul(self.alpha, loss.t())
        if self.size_average:
            loss = loss.mean()
        else:
            loss = loss.sum()
        return loss
class CrossEntropyLoss(torch.nn.Module):
    def __init__(self, reduction='mean'):
        super(CrossEntropyLoss, self).__init__()
        self.reduction = reduction
    def forward(self, logits, target):
        # logits: [N, C, H, W], target: [N, H, W]
        # loss = sum(-y_i * log(c_i))
        if logits.dim() > 2:
            logits = logits.view(logits.size(0), logits.size(1), -1)  # [N, C, HW]
            logits = logits.transpose(1, 2)   # [N, HW, C]
            logits = logits.contiguous().view(-1, logits.size(2))    # [NHW, C]
        target = target.view(-1, 1)    # [NHW，1]

        logits = F.log_softmax(logits, 1)
        print(logits)
        logits = logits.gather(1, target)   # [NHW, 1]
        loss = -1 * logits

        if self.reduction == 'mean':
            loss = loss.mean()
        elif self.reduction == 'sum':
            loss = loss.sum()
        return loss
class CrossEntropyFocalLoss(nn.Module):
    def __init__(self, alpha=0.25, gamma=2, num_classes = 5,reduction='mean'):
        super(CrossEntropyFocalLoss, self).__init__()
        self.reduction = reduction
        if isinstance(alpha, list):
            assert len(alpha) == num_classes  # α可以以list方式输入,size:[num_classes] 用于对不同类别精细地赋予权重
            print("Focal_loss alpha = {}, 将对每一类权重进行精细化赋值".format(alpha))
            self.alpha = torch.Tensor(alpha)
        else:
            assert alpha < 1  # 如果α为一个常数,则降低第一类的影响,在目标检测中为第一类
            print(" --- Focal_loss alpha = {} ,将对背景类进行衰减,请在目标检测任务中使用 --- ".format(alpha))
            self.alpha = torch.zeros(num_classes)
            self.alpha[0] += alpha
            self.alpha[1:] += (1 - alpha)  # α 最终为 [ α, 1-α, 1-α, 1-α, 1-α, ...] size:[num_classes]
        self.gamma = gamma

    def forward(self, logits, target):
        # logits: [N, C, H, W], target: [N, H, W]
        # loss = sum(-y_i * log(c_i))
        if logits.dim() > 2:
            logits = logits.view(logits.size(0), logits.size(1), -1)  # [N, C, HW]
            logits = logits.transpose(1, 2)  # [N, HW, C]
            logits = logits.contiguous().view(-1, logits.size(2))  # [NHW, C]
        target = target.view(-1, 1)  # [NHW，1]

        pt = F.softmax(logits, 1)
        pt = pt.gather(1, target).view(-1)  # [NHW]
        log_gt = torch.log(pt)

        if self.alpha is not None:
            # alpha: [C]
            alpha = self.alpha.gather(0, target.view(-1))  # [NHW]
            log_gt = log_gt * alpha

        loss = -1 * (1 - pt) ** self.gamma * log_gt

        if self.reduction == 'mean':
            loss = loss.mean()
        elif self.reduction == 'sum':
            loss = loss.sum()
        return loss
# input is of size N x C = 3 x 5
input = torch.randn(3,5,requires_grad=True)
target = torch.tensor([1,0,4])
print(input)
print(target)
# softmax = F.softmax(input,dim=1)
# print("softmax",softmax)
# m = F.log_softmax(input,dim=1)
# print("logsoftmax",m)
# loss = nn.NLLLoss()
# output = loss(m,target)
# print(output)
# print(F.cross_entropy(input,target))
# print(F.nll_loss(input,target))
# print(F.cross_entropy(input, target))
# loss_function = CrossEntropyLoss()
# loss= loss_function(input,target)
# print(loss)
# loss_focal = CrossEntropyFocalLoss()
# focal_loss = loss_focal(input,target)
# print(focal_loss)
import math
# print(math.e ** (0) * 2)
import wave
import numpy as np
audio_list = ["data/0/data_CH1_1.wav","data/1/data_CH1.wav","data/2/5511300F.wav",
              "data/3/5702100A.wav","data/4/7202100Q.wav"]
file = wave.open(audio_list[0], 'r')
params = file.getparams()
# 声道，采样宽度，帧速率，帧数，唯一标识，无损
nchannels, sampwidth, framerate, nframes = params[:4]
# print(nchannels,sampwidth, framerate, nframes)
str_data = file.readframes(nframes)
file.close()
str_data = np.frombuffer(str_data, dtype=np.short)
temp_data = str_data.T
print(str_data[:10])
print(temp_data[:10])
    # 1 2 17067 4266751
    # 1 2 17067 5120100
    # 1 2 14900 354172
    # 1 2 30000 35987
    # 1 2 10240 29040
import os
import glob
file_path = "data"
cate = [file_path + "/" + x for x in os.listdir(file_path) if os.path.isdir(file_path + "/" + x)]
labels = []
audio_name = []
for idx, folder in enumerate(cate):
    # print(folder) data/0
    for audio in glob.glob(folder + '/*.wav'):
        audio_name.append(os.path.join(audio,"\n"))
        x = os.path.basename(folder)
        labels.append(os.path.join(x,"\n"))
with open("labels.txt","w") as f:
    f.write(''.join(labels))


T= int(input())

def Paper(N):                                   # 재귀함수를 만들어 푼다.
    if N==10:
        return 1                                # 길이가 10이면 당연히 한 번
    elif N==20:
        return 3                                # 길이가 20이면 좌우,상하,정사각형으로 3개
    elif N>=30:
        return 1*Paper(N-10)+2*Paper(N-20)      # 10을 뺐을 때와 20을 뺐을 때에는 사실 중복이 하나

for t in range(1, T+1):
    N=int(input())
    print(f'#{t} {Paper(N)}')


import requests
import json
from .logger import log

def create_wlan(ssid: str, psk: str, site_id: str, token: str, vlan: int, duration: int) -> dict:
    """
    Creates a wireless network at a specified Mist site.

    :param str ssid: The name of the wireless network to be created.
    :param str psk: The preshared key to be used on the wireless network.
    :param str site_id: The unique site ID where the wireless network will be created.
    :param str token: Mist API token with network admin or super user rights.
    :param int vlan: VLAN ID for the wireless network.
    :param int duration: Days before the wireless network expires.

    :return dict: A dictionary containing the JSON response from the Mist API.
    """
    url = f"https://api.mist.com/api/v1/sites/{site_id}/wlans"
    log.info(f"Creating new wireless network '{ssid}' with the PSK '{psk}'.")
    headers = {
        "Content-type": "application/json",
        "Authorization": f"Token {token}"
    }
    wlan = {
        "ssid": ssid,
        "enabled": True,
        "auth": {
            "type": "psk",
            "psk": psk
        },
        "roam_mode": "11r",
        "vlan_enabled": True,
        "vlan_id": vlan
    }
    res = requests.post(url=url, data=json.dumps(wlan), headers=headers)
    data = res.json()
    if res.status_code == 200:
        log.info(f"New wireless network '{ssid}' with a duration of {duration} created!")
        return data
    else:
        log.error("Failed to create new wireless network!")
        log.error(f"Response: {res.json()}")
        return {"error": "failed"}


import socket
import os
import os.path as osp
import sys
import struct
import threading
import re
import argparse
from cryptography.exceptions import InvalidSignature
from utils import verify_cert, gen_session_key_and_RSAEnc_by_certkey, AES_decrypt_and_verify, AES_encrypt_and_digest

def socket_service(serverIP, serverPort, root):
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        s.bind((serverIP, serverPort))
        s.listen(10)
    except socket.error as msg:
        print(msg)
        sys.exit(1)
    print('Waiting connection...')

    while 1:
        conn, addr = s.accept()
        print(f'Accept new connection from {addr}')
        conn.send('Hi, Welcome to the BlackBoard!'.encode('utf-8'))
        t = threading.Thread(target=run, args=(conn, root))
        t.start()

def sendMsg(conn, msg):
    conn.send(msg.encode('utf-8'))

def receiveMsg(conn):
    print(conn.recv(1024).decode('utf-8'))

def receiveFile(conn, root):
    new_filename = 'exit' ## used to close current thread safely
    while 1:
        fileinfo_size = struct.calcsize('256sl')
        buf = conn.recv(fileinfo_size)
        if buf:
            filename, filesize = struct.unpack('256sl', buf)
            # fn = filename
            fn = filename.decode('utf-8').strip('\00')
            os.makedirs(os.path.join(f'./{root}', fn.split('.')[0]), exist_ok=True)
            new_filename = os.path.join(f'./{root}', fn.split('.')[0], fn)
            # print('file new name is {0}, filesize if {1}'.format(new_filename,
            #                                                      filesize))

            recvd_size = 0
            fp = open(new_filename, 'wb')
            # print('start receiving...')

            while not recvd_size == filesize:
                if filesize - recvd_size > 1024:
                    data = conn.recv(1024)
                    recvd_size += len(data)
                else:
                    data = conn.recv(filesize - recvd_size)
                    recvd_size = filesize
                fp.write(data)
            fp.close()
            # print('end receive...')
        # conn.close()
        break
    return new_filename

def sendFile(conn, filepath):
    while 1:
        if os.path.isfile(filepath):
            fileinfo_size = struct.calcsize('256sl')
            fhead = struct.pack('256sl', os.path.basename(filepath).encode('utf-8'),
                                os.stat(filepath).st_size)
            conn.send(fhead)
            # print('client filepath: {0}'.format(filepath))

            fp = open(filepath, 'rb')
            while 1:
                data = fp.read(1024)
                if not data:
                    # print('{0} file send over...'.format(filepath))
                    break
                conn.send(data)
        break
def communication(conn, root, session_key):
    while 1:
        filename = receiveFile(conn, root)
        if filename == 'exit':
            print('Close one connection')
            break
        ### decrypt the msg file
        decryptMsg = AES_decrypt_and_verify(filename, session_key)
        decryptMsg = decryptMsg.decode('utf-8')
        ###
        sid = os.path.basename(os.path.dirname(filename))
        print(f'{sid}: {decryptMsg}')

def run(conn, root):
    stu_cert_path = receiveFile(conn, root)
    sid = os.path.basename(os.path.dirname(stu_cert_path))
    print(f'Received Certificant from sid: {sid}')
    valid = True
    try:
        verify_cert(cli_cert_path=stu_cert_path, ca_cert_path=osp.join('cuhk', 'cuhk.cert.pem')) ### be relaced by verify the cert and encrpypt
    except InvalidSignature:
        print('Certificate is invalid')
        valid = False
    if valid:
        print('Certificate is valid')
        sendMsg(conn, msg='Certificate is valid')
    else:
        sendMsg(conn, msg='Certificate is invalid')
    session_key = gen_session_key_and_RSAEnc_by_certkey(root, sid)
    print('Generated Session key')
    sendFile(conn, osp.join(root, sid, f'{sid}.enc_session_key.pem'))
    print('Sent out Session key')
    communication(conn, root, session_key)
    conn.close()

if __name__ == '__main__':
    os.makedirs('blackboard', exist_ok=True)
    root = 'blackboard'
    clientIP = '127.0.0.1'
    serverIP = '127.0.0.1'
    serverPort = 12346

    # 1. gererate key pairs 2.generate ca cert
    socket_service(serverIP, serverPort, root)


from django import template
import datetime, humanfriendly

register = template.Library()

@register.filter(name='get_occurence')
def get_occurence(value, total):
    percentage = (value / total) * 100
    # print(percentage)
    if percentage <= 25:
        return "info"
    elif percentage > 65:
        return "warning"
    else:
        return "dark"

@register.filter(name='get_duration')
def get_duration(start, end):
    if end != None:
        total = end - start
        hours, remainder = divmod(total.total_seconds(), 3600)
        minutes, seconds = divmod(remainder, 60)
        duration = 'for '
        if int(hours) != 0:
            duration += str(int(hours)) + 'h '
        if int(minutes) != 0:
            duration += str(int(minutes)) + 'm '
        if int(seconds) != 0:
            duration += str(int(seconds)) + 's'

        return duration
    else:
        return ''


import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import sys
import pickle
import os

# expects as an argument a .npy file with nxpx2 data
# where n is the number of frames, p is the number of points and 2 is x,y

def update_line(num, data, line, annotationStuff):
    # line.set_data(data[..., :num])
    # return np.random.randint(1, 30, 30), np.random.randint(1, 30, 30)
    line.set_data(data[num, :,0], data[num,:,1])
    if(annotationStuff is not None):
      annotation, plt, annotationLocation = annotationStuff
      print(annotationLocation)
      print(annotation[num])
      plt.title(str(annotation[num]))
      # ax.annotate(s=str(annotation[num]), xy=annotationLocation,  xytext=annotationLocation)
    return line

def animateFromData(videoName, data, fps, annotation=None):
  fig1 = plt.figure()
  l, = plt.plot([], [], "o")
  pad = 5
  x_vars = data[:,:,0]
  x_vars = x_vars.reshape(x_vars.size)
  y_vars = data[:,:,1]
  y_vars = y_vars.reshape(y_vars.size)
  data[:, :, 1] = -data[:, :, 1]
  y_min, y_max = (np.min(y_vars) - pad, np.max(y_vars) + pad)
  plt.xlim(np.min(x_vars) - pad, np.max(x_vars) + pad)
  plt.ylim(y_min, y_max)

  annotationStuff = None
  if annotation is not None:
    ax = fig1.add_subplot(1,1,1)
    annotationLocation = (y_min + 30, np.min(x_vars)+50)
    annotationStuff = (annotation, plt, annotationLocation)
  line_ani = animation.FuncAnimation(fig1, update_line, data.shape[0], fargs=(data, l, annotationStuff),
                                     interval=(1000/fps))
  line_ani.save(videoName, fps=fps)

def extractFramesFromFile(fileName):
  a = pickle.load(open( fileName, "rb" ))
  data = a['data']
  fps = a['fps']
  videoName = fileName[:-2] + '.mp4'
  animateFromData(videoName, data, fps)

def forAllFilesInDir(path):
    i = 0
    numSuccess = 0
    for f in os.listdir(path):
        if f.endswith(".p"):
            outFileName = os.path.join(path, f[:-2]) + ".p"  # excluding .mp4 endign
            # if(outFileName in os.listdir(path)): # later , probably make faster, like convert to map first
            #   continue
            print(f) # print which video we are processing
            extractFramesFromFile(outFileName)

# Expects one argument which is the path to directory of .p files"
# containing object with fields 'data':nxpx2 matrix, and 'fps' - frames "
# n is the number of frames, p is the number of points and 2 is x,y"
# result is saving corresponding animations in current directory
if __name__ == '__main__':
  if len(sys.argv) != 2:
      print(
       "Expects one argument which is the path to directory of .p files"
       " containing object with fields 'data':nxpx2 matrix, and 'fps' - frames "
       " n is the number of frames, p is the number of points and 2 is x,y"
      )
      exit()
  forAllFilesInDir(sys.argv[1])


import numpy as np
import os
import numbers
import palpy

from lsst.utils import getPackageDir
from lsst.sims.utils import ZernikePolynomialGenerator
from lsst.sims.coordUtils import lsst_camera
from lsst.sims.coordUtils import DMtoCameraPixelTransformer
from lsst.afw.cameraGeom import PIXELS, FOCAL_PLANE, FIELD_ANGLE
from lsst.afw.cameraGeom import DetectorType
import lsst.geom as geom
from lsst.sims.utils.CodeUtilities import _validate_inputs

__all__ = ["LsstZernikeFitter"]

def _rawPupilCoordsFromObserved(ra_obs, dec_obs, ra0, dec0, rotSkyPos):
    """
    Convert Observed RA, Dec into pupil coordinates

    Parameters
    ----------
    ra_obs is the observed RA in radians

    dec_obs is the observed Dec in radians

    ra0 is the RA of the boresite in radians

    dec0 is the Dec of the boresite in radians

    rotSkyPos is in radians

    Returns
    --------
    A numpy array whose first row is the x coordinate on the pupil in
    radians and whose second row is the y coordinate in radians
    """

    are_arrays = _validate_inputs([ra_obs, dec_obs], ['ra_obs', 'dec_obs'],
                                  "pupilCoordsFromObserved")

    theta = -1.0*rotSkyPos

    ra_pointing = ra0
    dec_pointing = dec0

    # palpy.ds2tp performs the gnomonic projection on ra_in and dec_in
    # with a tangent point at (pointingRA, pointingDec)
    #
    if not are_arrays:
        try:
            x, y = palpy.ds2tp(ra_obs, dec_obs, ra_pointing, dec_pointing)
        except:
            x = np.NaN
            y = np.NaN
    else:
        try:
            x, y = palpy.ds2tpVector(ra_obs, dec_obs, ra_pointing, dec_pointing)
        except:
            # apparently, one of your ra/dec values was improper; we will have to do this
            # element-wise, putting NaN in the place of the bad values
            x = []
            y = []
            for rr, dd in zip(ra_obs, dec_obs):
                try:
                    xx, yy = palpy.ds2tp(rr, dd, ra_pointing, dec_pointing)
                except:
                    xx = np.NaN
                    yy = np.NaN
                x.append(xx)
                y.append(yy)
            x = np.array(x)
            y = np.array(y)

    # rotate the result by rotskypos (rotskypos being "the angle of the sky relative to
    # camera coordinates" according to phoSim documentation) to account for
    # the rotation of the focal plane about the telescope pointing

    x_out = x*np.cos(theta) - y*np.sin(theta)
    y_out = x*np.sin(theta) + y*np.cos(theta)

    return np.array([x_out, y_out])

class LsstZernikeFitter(object):
    """
    This class will fit and then apply the Zernike polynomials needed
    to correct the FIELD_ANGLE to FOCAL_PLANE transformation for the
    filter-dependent part.
    """

    def __init__(self):
        self._camera = lsst_camera()
        self._pixel_transformer = DMtoCameraPixelTransformer()
        self._z_gen = ZernikePolynomialGenerator()

        self._rr = 500.0  # radius in mm of circle containing LSST focal plane;
                          # make it a little bigger to accommodate any slop in
                          # the conversion from focal plane coordinates back to
                          # pupil coordinates (in case the optical distortions
                          # cause points to cross over the actual boundary of
                          # the focal plane)

        self._band_to_int = {}
        self._band_to_int['u'] = 0
        self._band_to_int['g'] = 1
        self._band_to_int['r'] = 2
        self._band_to_int['i'] = 3
        self._band_to_int['z'] = 4
        self._band_to_int['y'] = 5

        self._int_to_band = 'ugrizy'

        self._n_grid = []
        self._m_grid = []

        # 2018 May 8
        # During development, I found that there was negligible
        # improvement in the fit when allowing n>4, so I am
        # hard-coding the limit of n=4 here.
        for n in range(4):
            for m in range(-n, n+1, 2):
                self._n_grid.append(n)
                self._m_grid.append(m)

        self._build_transformations()

    def _get_coeffs(self, x_in, y_in, x_out, y_out):
        """
        Get the coefficients of the best fit Zernike Polynomial
        expansion that transforms from x_in, y_in to x_out, y_out.

        Returns numpy arrays of the Zernike Polynomial expansion
        coefficients in x and y.  Zernike Polynomials correspond
        to the radial and angular orders stored in self._n_grid
        and self._m_grid.
        """

        polynomials = {}
        for n, m in zip(self._n_grid, self._m_grid):
            values = self._z_gen.evaluate_xy(x_in/self._rr, y_in/self._rr, n, m)
            polynomials[(n,m)] = values

        poly_keys = list(polynomials.keys())

        dx = x_out - x_in
        dy = y_out - y_in

        b = np.array([(dx*polynomials[k]).sum() for k in poly_keys])
        m = np.array([[(polynomials[k1]*polynomials[k2]).sum() for k1 in poly_keys]
                      for k2 in poly_keys])

        alpha_x_ = np.linalg.solve(m, b)
        alpha_x = {}
        for ii, kk in enumerate(poly_keys):
            alpha_x[kk] = alpha_x_[ii]

        b = np.array([(dy*polynomials[k]).sum() for k in poly_keys])
        m = np.array([[(polynomials[k1]*polynomials[k2]).sum() for k1 in poly_keys]
                      for k2 in poly_keys])

        alpha_y_ = np.linalg.solve(m, b)
        alpha_y = {}
        for ii, kk in enumerate(poly_keys):
            alpha_y[kk] = alpha_y_[ii]

        return alpha_x, alpha_y

    def _build_transformations(self):
        """
        Solve for and store the coefficients of the Zernike
        polynomial expansion of the difference between the
        naive and the bandpass-dependent optical distortions
        in the LSST camera.
        """
        catsim_dir = os.path.join(getPackageDir('sims_data'),
                                  'FocalPlaneData',
                                  'CatSimData')

        phosim_dir = os.path.join(getPackageDir('sims_data'),
                                  'FocalPlaneData',
                                  'PhoSimData')

        # the file which contains the input sky positions of the objects
        # that were given to PhoSim
        catsim_catalog = os.path.join(catsim_dir,'predicted_positions.txt')

        with open(catsim_catalog, 'r') as input_file:
            header = input_file.readline()
        params = header.strip().split()
        ra0 = np.radians(float(params[2]))
        dec0 = np.radians(float(params[4]))
        rotSkyPos = np.radians(float(params[6]))

        catsim_dtype = np.dtype([('id', int), ('xmm_old', float), ('ymm_old', float),
                                 ('xpup', float), ('ypup', float),
                                 ('raObs', float), ('decObs', float)])

        catsim_data = np.genfromtxt(catsim_catalog, dtype=catsim_dtype)

        sorted_dex = np.argsort(catsim_data['id'])
        catsim_data=catsim_data[sorted_dex]

        # convert from RA, Dec to pupil coors/FIELD_ANGLE
        x_field, y_field = _rawPupilCoordsFromObserved(np.radians(catsim_data['raObs']),
                                                       np.radians(catsim_data['decObs']),
                                                       ra0, dec0, rotSkyPos)

        # convert from FIELD_ANGLE to FOCAL_PLANE without attempting to model
        # the optical distortions in the telescope
        field_to_focal = self._camera.getTransform(FIELD_ANGLE, FOCAL_PLANE)
        catsim_xmm = np.zeros(len(x_field), dtype=float)
        catsim_ymm = np.zeros(len(y_field), dtype=float)
        for ii, (xx, yy) in enumerate(zip(x_field, y_field)):
            focal_pt = field_to_focal.applyForward(geom.Point2D(xx, yy))
            catsim_xmm[ii] = focal_pt.getX()
            catsim_ymm[ii] = focal_pt.getY()

        phosim_dtype = np.dtype([('id', int), ('phot', float),
                                 ('xpix', float), ('ypix', float)])

        self._pupil_to_focal = {}
        self._focal_to_pupil = {}

        for i_filter in range(6):
            self._pupil_to_focal[self._int_to_band[i_filter]] = {}
            self._focal_to_pupil[self._int_to_band[i_filter]] = {}
            phosim_xmm = np.zeros(len(catsim_data['ypup']), dtype=float)
            phosim_ymm = np.zeros(len(catsim_data['ypup']), dtype=float)

            for det in self._camera:
                if det.getType() != DetectorType.SCIENCE:
                    continue
                pixels_to_focal = det.getTransform(PIXELS, FOCAL_PLANE)
                det_name = det.getName()
                bbox = det.getBBox()
                det_name_m = det_name.replace(':','').replace(',','').replace(' ','_')

                # read in the actual pixel positions of the sources as realized
                # by PhoSim
                centroid_name = 'centroid_lsst_e_2_f%d_%s_E000.txt' % (i_filter, det_name_m)
                full_name = os.path.join(phosim_dir, centroid_name)
                phosim_data = np.genfromtxt(full_name, dtype=phosim_dtype, skip_header=1)

                # make sure that the data we are fitting to is not too close
                # to the edge of the detector
                assert phosim_data['xpix'].min() > bbox.getMinY() + 50.0
                assert phosim_data['xpix'].max() < bbox.getMaxY() - 50.0
                assert phosim_data['ypix'].min() > bbox.getMinX() + 50.0
                assert phosim_data['ypix'].max() < bbox.getMaxX() - 50.0

                xpix, ypix = self._pixel_transformer.dmPixFromCameraPix(phosim_data['xpix'],
                                                                        phosim_data['ypix'],
                                                                        det_name)
                xmm = np.zeros(len(xpix), dtype=float)
                ymm = np.zeros(len(ypix), dtype=float)
                for ii in range(len(xpix)):
                    focal_pt = pixels_to_focal.applyForward(geom.Point2D(xpix[ii], ypix[ii]))
                    xmm[ii] = focal_pt.getX()
                    ymm[ii] = focal_pt.getY()
                phosim_xmm[phosim_data['id']-1] = xmm
                phosim_ymm[phosim_data['id']-1] = ymm

            # solve for the coefficients of the Zernike expansions
            # necessary to model the optical transformations and go
            # from the naive focal plane positions (catsim_xmm, catsim_ymm)
            # to the PhoSim realized focal plane positions
            alpha_x, alpha_y = self._get_coeffs(catsim_xmm, catsim_ymm,
                                                phosim_xmm, phosim_ymm)

            self._pupil_to_focal[self._int_to_band[i_filter]]['x'] = alpha_x
            self._pupil_to_focal[self._int_to_band[i_filter]]['y'] = alpha_y

            # solve for the coefficients to the Zernike expansions
            # necessary to go back from the PhoSim realized focal plane
            # positions to the naive CatSim predicted focal plane
            # positions
            alpha_x, alpha_y = self._get_coeffs(phosim_xmm, phosim_ymm,
                                                catsim_xmm, catsim_ymm)

            self._focal_to_pupil[self._int_to_band[i_filter]]['x'] = alpha_x
            self._focal_to_pupil[self._int_to_band[i_filter]]['y'] = alpha_y

    def _apply_transformation(self, transformation_dict, xmm, ymm, band):
        """
        Parameters
        ----------
        tranformation_dict -- a dict containing the coefficients
        of the Zernike decomposition to be applied

        xmm -- the input x position in mm

        ymm -- the input y position in mm

        band -- the filter in which we are operating
        (can be either a string or an int; 0=u, 1=g, 2=r, etc.)

        Returns
        -------
        dx -- the x offset resulting from the transformation

        dy -- the y offset resulting from the transformation
        """
        if isinstance(band, int):
            band = self._int_to_band[band]

        if isinstance(xmm, numbers.Number):
            dx = 0.0
            dy = 0.0
        else:
            dx = np.zeros(len(xmm), dtype=float)
            dy = np.zeros(len(ymm), dtype=float)

        for kk in self._pupil_to_focal[band]['x']:
            values = self._z_gen.evaluate_xy(xmm/self._rr, ymm/self._rr, kk[0], kk[1])
            dx += transformation_dict[band]['x'][kk]*values
            dy += transformation_dict[band]['y'][kk]*values

        return dx, dy

    def dxdy(self, xmm, ymm, band):
        """
        Apply the transformation necessary when going from pupil
        coordinates to focal plane coordinates.

        The recipe to correctly use this method is

        xf0, yf0 = focalPlaneCoordsFromPupilCoords(xpupil, ypupil,
                                                   camera=lsst_camera())

        dx, dy = LsstZernikeFitter().dxdy(xf0, yf0, band=band)

        xf = xf0 + dx
        yf = yf0 + dy

        xf and yf are now the actual position in millimeters on the
        LSST focal plane corresponding to xpupil, ypupil

        Parameters
        ----------
        xmm -- the naive x focal plane position in mm

        ymm -- the naive y focal plane position in mm

        band -- the filter in which we are operating
        (can be either a string or an int; 0=u, 1=g, 2=r, etc.)

        Returns
        -------
        dx -- the offset in the x focal plane position in mm

        dy -- the offset in the y focal plane position in mm
        """
        return self._apply_transformation(self._pupil_to_focal, xmm, ymm, band)

    def dxdy_inverse(self, xmm, ymm, band):
        """
        Apply the transformation necessary when going from focal
        plane coordinates to pupil coordinates.

        The recipe to correctly use this method is

        dx, dy = LsstZernikeFitter().dxdy_inverse(xf, yf, band=band)

        xp, yp = pupilCoordsFromFocalPlaneCoords(xf+dx,
                                                 yf+dy,
                                                 camera=lsst_camera())

        xp and yp are now the actual position in radians on the pupil
        corresponding to the focal plane coordinates xf, yf

        Parameters
        ----------
        xmm -- the naive x focal plane position in mm

        ymm -- the naive y focal plane position in mm

        band -- the filter in which we are operating
        (can be either a string or an int; 0=u, 1=g, 2=r, etc.)

        Returns
        -------
        dx -- the offset in the x focal plane position in mm

        dy -- the offset in the y focal plane position in mm
        """
        return self._apply_transformation(self._focal_to_pupil, xmm, ymm, band)


import os
import sys
import json
import requests

from config import URL, CHANNEL_ID

def get_url(url):
    response = requests.get(url)
    content = response.content.decode("utf8")
    return content

def send_message(text):
    url = URL + "sendMessage?text={}&chat_id={}&parse_mode={}".format(text, CHANNEL_ID, "markdown")
    get_url(url)

path, dirs, files = next(os.walk(os.getcwd()))
f=open('photos.txt', 'r')

data = f.read()
f.close()
f=open('photos.txt', 'w')
# file_count = len(files)-25
#f.seek(0)
new=int(data)+1
f.write(str(new))
f.close()
if int(data) == 175 or int(data) == 105:
  send_message("`Poze facute:` "+str(data))
  send_message("SCHIMBA ROLA!")
if int(data) % 25 == 0:
  send_message("`Poze facute:` "+str(data))
  os.system('rm -rf *.jpg')


# wget http://www.minethatdata.com/Kevin_Hillstrom_MineThatData_E-MailAnalytics_DataMiningChallenge_2008.03.20.csv
from sklearn.linear_model import LogisticRegression, LinearRegression
import numpy as np
import networkx as nx
from networkx.algorithms.bipartite.matching import hopcroft_karp_matching
from tqdm import tqdm
from load_male_data import load_biased_male_data

biased_male_data = load_biased_male_data()

# 3.2.1
# まずは傾向スコアを求める
model = LogisticRegression()
model.fit([[e['history'], e['recency']] + e['binarized_channel'] for e in biased_male_data],
          [e['treatment'] for e in biased_male_data])

propensity_scores = model.predict_proba([[e['history'], e['recency']] + e['binarized_channel'] for e in biased_male_data])[:, 1]

# どうせだし biased_male_data に propensity_scores を付与しましょう
for pos, e in enumerate(biased_male_data):
    e['ps'] = propensity_scores[pos]

# 群に分割
treatment_data = [e for e in biased_male_data if e['treatment'] == 1]
controll_data = [e for e in biased_male_data if e['treatment'] == 0]

# # 最大重みマッチングとして解きましょう
# I = range(len(treatment_data))
# J = range(len(controll_data))
# g = nx.Graph()
# weighted_edges = []
# for i in tqdm(I, ascii=True):
#     for j in J:
#         # 通常は diff^2 が距離なのでそのまま最小で使いたいですが
#         # 最大で解くので 1.0 から引きます
#         w = 1.0 - (treatment_data[i]['ps'] - controll_data[j]['ps']) ** 2
#         weighted_edges.append((f'i_{i}', f'j_{j}', w))

# g.add_weighted_edges_from(weighted_edges)

# ret = hopcroft_karp_matching(g)
# # マッチング結果を matched_data に入れる
# matched_data = []
# for k, v in ret.items():
#     _type, v1 = k.split('_')
#     v1 = int(v1)
#     v2 = int(v.split('_')[1])
#     if _type == 'i':
#         matched_data.append(treatment_data[v1])
#         matched_data.append(controll_data[v2])
#     else:
#         matched_data.append(treatment_data[v2])
#         matched_data.append(controll_data[v1])

# # treatment:0.843491048593346
# # recency:0.4778351631443985 -> 0.4801651891393515
# # history:0.2716497558095329 -> 0.2719059723736311
# # 全然改善しませんね

# greedy に解きましょう
# propensity score で昇順にソート ( treatment < controll だから ps が大きいものは余らせたい)
treatment_data = sorted(treatment_data, key=lambda x: x['ps'])
n_match = min(len(treatment_data), len(controll_data))
matched_data = []
for i in tqdm(range(n_match), ascii=True):
    e_pos = treatment_data[i]
    # 一番近いものを探す
    j = sorted([(j, (e['ps'] - e_pos['ps']) ** 2) for j, e in enumerate(controll_data)], key=lambda x: x[1])[0][0]
    e_neg = controll_data.pop(j)
    matched_data.append(e_pos)
    matched_data.append(e_neg)

# マッチが終わったので効果を計算する
model = LinearRegression()
model.fit([[e['treatment']]  for e in matched_data],
          [e['spend'] for e in matched_data])
print(f'treatment:{model.coef_[0]}')
# result
# treatment:0.8451103782474086

# 3.2.3
# マッチング前後で共変量の分布がどう変わったか確認しましょう
# 各共変量に対して Average Standardized Absolute Mean distance (ASAM) を計算しましょう
# ASAM は平均の差をその標準誤差で割った値です

# 本文の「その標準偏差」の「その」が何を指すのかわからない
# 検索したところ， Cohen の d がこれに相当するのではないか?
# 定義は |E[x_1] - E[x_2]| / s_c
# s_c = ((n_1 * (s_1 ** 2) + n_2 * (s_2 ** 2)) / (n_1 + n_2)) ** 0.5
# s_1 = Var[x_1] ** 0.5, s_2 = Var[x_2] ** 0.5
# 参考 : https://bellcurve.jp/statistics/course/12765.html
# figure 3.5 の `distance` が何を意味するのかよく分かっていない
# ひとまず ps で計算してみましょう

x_1_unadj = [x['ps'] for x in biased_male_data if x['treatment'] == 1]
x_0_unadj = [x['ps'] for x in biased_male_data if x['treatment'] == 0]
s_c_unadj = len(x_1_unadj) * (np.std(x_1_unadj) ** 2) + len(x_0_unadj) * (np.std(x_0_unadj) ** 2)
s_c_unadj /= (len(x_1_unadj) + len(x_0_unadj))
d_unadj = abs(np.average(x_1_unadj) - np.average(x_0_unadj)) / (s_c_unadj ** 0.5)

x_1_adj = [x['ps'] for x in matched_data if x['treatment'] == 1]
x_0_adj = [x['ps'] for x in matched_data if x['treatment'] == 0]
s_c_adj = len(x_1_adj) * (np.std(x_1_adj) ** 2) + len(x_0_adj) * (np.std(x_0_adj) ** 2)
s_c_adj /= (len(x_1_adj) + len(x_0_adj))
d_adj = abs(np.average(x_1_adj) - np.average(x_0_adj)) / (s_c_adj ** 0.5)
print(f'ps:{d_unadj} -> {d_adj}')

for k in ['recency', 'history']:
    x_1_unadj = [x[k] for x in biased_male_data if x['treatment'] == 1]
    x_0_unadj = [x[k] for x in biased_male_data if x['treatment'] == 0]
    s_c_unadj = len(x_1_unadj) * (np.std(x_1_unadj) ** 2) + len(x_0_unadj) * (np.std(x_0_unadj) ** 2)
    s_c_unadj /= (len(x_1_unadj) + len(x_0_unadj))
    d_unadj = abs(np.average(x_1_unadj) - np.average(x_0_unadj)) / (s_c_unadj ** 0.5)

    x_1_adj = [x[k] for x in matched_data if x['treatment'] == 1]
    x_0_adj = [x[k] for x in matched_data if x['treatment'] == 0]
    s_c_adj = len(x_1_adj) * (np.std(x_1_adj) ** 2) + len(x_0_adj) * (np.std(x_0_adj) ** 2)
    s_c_adj /= (len(x_1_adj) + len(x_0_adj))
    d_adj = abs(np.average(x_1_adj) - np.average(x_0_adj)) / (s_c_adj ** 0.5)

    print(f'{k}:{d_unadj} -> {d_adj}')

for pos in range(3):
    x_1_unadj = [x['binarized_channel'][pos] for x in biased_male_data if x['treatment'] == 1]
    x_0_unadj = [x['binarized_channel'][pos] for x in biased_male_data if x['treatment'] == 0]
    s_c_unadj = len(x_1_unadj) * (np.std(x_1_unadj) ** 2) + len(x_0_unadj) * (np.std(x_0_unadj) ** 2)
    s_c_unadj /= (len(x_1_unadj) + len(x_0_unadj))
    d_unadj = abs(np.average(x_1_unadj) - np.average(x_0_unadj)) / (s_c_unadj ** 0.5)

    x_1_adj = [x['binarized_channel'][pos] for x in matched_data if x['treatment'] == 1]
    x_0_adj = [x['binarized_channel'][pos] for x in matched_data if x['treatment'] == 0]
    s_c_adj = len(x_1_adj) * (np.std(x_1_adj) ** 2) + len(x_0_adj) * (np.std(x_0_adj) ** 2)
    s_c_adj /= (len(x_1_adj) + len(x_0_adj))
    d_adj = abs(np.average(x_1_adj) - np.average(x_0_adj)) / (s_c_adj ** 0.5)

    print(f'channel_{pos}:{d_unadj} -> {d_adj}')

# result
# ps:0.5235731806205225 -> 0.3186743724862133
# recency:0.4778351631443985 -> 0.3321028493729235
# history:0.2716497558095329 -> 0.01041296968179367
# channel_0:0.18216784523404742 -> 0.007067847374114813
# channel_1:0.07359552251750925 -> 0.004461431671379047
# channel_2:0.04637906531038619 -> 0.008512051815361875


# Server.py file

import socket
from threading import Thread
import pickle as p

# Global Variables
ClientCount = 0
Messages = ["<Server>: Hello", "<Server>: World"]
Clients  = []
Threads  = []

# Threads
def ClientConnected(clientSocket, address): # New thread instance created for each connected client
   try:
      global Messages

      print("Got a connection from " + str(address))
      clientSocket.send(p.dumps(Messages)) # Send list of all previous messages to client
      ListenForPackets(clientSocket)

   except Exception as e:
      print(str(e))

def ListenForPackets(clientSocket):
   global Messages
   while True:
         response = p.loads(clientSocket.recv(1024)) # Wait for message from new client
         Messages.append(response)
         SendToClients(response)

def SendToClients(message):
   global Clients

   for client in Clients:
      client.send(p.dumps(message))

def __main__():
   # Create a socket object
   serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

   # Get local machine name
   host = socket.gethostname()
   port = 9999

   # Bind to the port
   serverSocket.bind((host, port))

   # Queue up to 5 requests
   serverSocket.listen(5)
   print("\nListening for connections...")

   while True:
      # Waiting for connections
      clientSocket, clientAddress = serverSocket.accept()
      Clients.append(clientSocket)
      newThread = Thread(target=ClientConnected, args=(clientSocket, clientAddress))
      newThread.start()
      Threads.append(newThread)

if __name__ == "__main__":
   __main__()


import random
import sys

nb_mys = random.randint(1, 100)

left_try = 5
print("*** Le jeu du nombre mystère ***")
while left_try > 0 :
    try_text = print(f"Il vous reste {left_try} essai{'s' if left_try > 1 else ''} 🤞")
    trial = input('👉  Devinez le nombre mystère : ')
    if trial.isdigit() :
        trial = int(trial)
        if trial == nb_mys:
            print('Bravo ! 🏆 Vous avez trouvé le nombre mystère ! 🎉🎉🎉')
            print(f"Vous avez trouvé en {6-left_try} essai{'s' if left_try > 1 else ''} 😎")
            print('Fin du jeu.')
            sys.exit()
        else:
            left_try -= 1
            if left_try == 0:
                print('👎 Vous avez perdu ! 😢')
                print(f'Le nombre mystère était : {nb_mys}')
                print('Fin du jeu.')
                sys.exit()
            if trial > nb_mys:
                print(f'C\'est moins que {trial} !')
            else:
                print(f'C\'est plus que {trial} !')
    else :
        print('Veuillez entrer un nombre valide. 😉')


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):


import paho.mqtt.client as paho

def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))

client = paho.Client("testclient", clean_session=False, protocol=paho.MQTTv31)
client.on_message = on_message
client.connect("localhost", 1883)
client.subscribe("rpi/useage", qos=1)
client.loop_forever()


class Solution:
    def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:
        l = 0
        start = head
        stack = []
        while l < left - 1:
            start = start.next
            l += 1
        temp = start
        l_ = l

        while l < right:
            stack.append(start.val)
            start = start.next
            l += 1

        while l_ < right:
            temp.val = stack.pop()
            temp = temp.next
            l_ += 1

        return head


# https://leetcode.com/problems/word-break/

def wordBreak(s, wordDict):
    st = set(wordDict)
    return dfs(s, st)

def dfs(s, st):
    if s in st:
        return True
    for i in range(1, len(s)+1):
        if s[:i] in st:
            res = dfs(s[i:], st)
            if res:
                return True
    return False

def wordBreak(s, wordDict):
    dp = [False] * (len(s) + 1)
    dp[0] = True
    for i in range(1, len(s)+1):
        for word in wordDict:
            if i - len(word) >= 0:
                prev = s[i - len(word):i]
                if word == prev and dp[i - len(word)] == True:
                    dp[i] = True
    return dp[-1]

print(wordBreak(s="leetcode", wordDict=["leet", "code"]))
print(wordBreak(s="applepenapple", wordDict=["apple", "pen"]))
print(wordBreak(s="catsandog", wordDict=["cats", "dog", "sand", "and", "cat"]))


import re
n = int(input())
pattern = r'<a\shref=\"(.+?)\".*?>([^<]*?)</'
#.+? means 1to any indexe . bste pare ? mane specially first and laste
for i in range(n):
    st=input()
    match= re.findall(pattern,st)
    print(match)
    for l,t in match:
        print(f'{l},{t.strip()}')

#https://stackoverflow.com/questions/3075130/what-is-the-difference-between-and-regular-expressions


"""Write a function, `tag_count`, that takes as its argument a list
of strings. It should return a count of how many of those strings
are XML tags. You can tell if a string is an XML tag if it begins
with a left angle bracket "<" and ends with a right angle bracket ">".
"""

#  Define the tag_count function
def tag_count(xml_count):
    count = 0
    for xml in xml_count:
        if xml[0] == '<' and xml[-1] == '>':
            count += 1
    return count

#or use this

def tag_count(tokens):
    count = 0
    for token in tokens:
        if token[0] == '<' and token[-1] == '>':
            count += 1
    return count

# Test for the tag_count function:
list1 = ['<greeting>', 'Hello World!', '</greeting>']
count = tag_count(list1)
print("Expected result: 2, Actual result: {}".format(count))


### Shelly Dimmer 2 Plotter
### Temperature / Brightness over Time
### SpawnTerror 2021

import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.animation as animation

from urllib.request import urlopen
from datetime import datetime
import json
import time

# Create figure for plotting / variables
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
xs = []
ys = []
ys2 = []

# Shelly IP / update interval
update_interval = 10000
url = "http://192.168.1.230/status"

# Gui info
fig.text(0.5, 0.92, "Shelly Dimmer 2 Plotter", ha="center", va="bottom", size="medium",color="black")
fig.text(0.40, 0.89, "Temperature", ha="center", va="bottom", size="medium",color="red")
fig.text(0.5, 0.89, " & ", ha="center", va="bottom", size="medium")
fig.text(0.60,0.89,"Brightness", ha="center", va="bottom", size="medium",color="blue")
fig.text(0.13,0.15,'URL: ' + url + '.' , ha="left", va="bottom", size="medium",color="black")
fig.text(0.13,0.12,'Start time: ' + dt.datetime.now().strftime('%H:%M:%S') + '.' , ha="left", va="bottom", size="medium",color="black")
fig.text(0.13,0.09,'Update interval: ' + str(update_interval)[0:2] + 's.' , ha="left", va="bottom", size="medium",color="black")

# Animate function
def animate(i, xs, ys):

    # Read temperature (Celsius) from TMP102
    response = urlopen(url)
    data = json.loads(response.read())
    tempe_values = data['tmp']
    temperature = tempe_values['tC']
    light_values = data['lights']
    light_read = light_values[0]
    brightness = light_read['brightness']
    plt.pause(0.001)

    # Add x and y to lists
    xs.append(dt.datetime.now().strftime('%M:%S'))
    ys.append(temperature)
    ys2.append(brightness)

    # Draw x and y lists
    ax.plot(xs, ys, color='red')
    ax.plot(xs, ys2, color='blue')

    # Format plot
    plt.xticks(rotation=45, ha='right')
    plt.subplots_adjust(bottom=0.30)
    plt.ylabel('Temperature (C)')

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


import base64
import json
import os
import random
from io import BytesIO
from ntpath import basename

from OCR.main import ocr
from NLP.MahrousNLP import main as nlp

from PIL import Image
from flask import Flask, jsonify, request
from flask_cors import CORS

from HELP.file_tool import get_file_from_folder
from constants import CONSTANTS

app = Flask(__name__, static_folder='static', static_url_path="/static")
CORS(app)

def save_file(name, text, path):
    with open(f'{path}{name}', "w+") as file:
        file.write(text)

        pass

@app.route("/problems", methods=['GET'])
def get_problems():
    def read_file(path="", _type="txt"):
        file = open(path, "r", encoding="utf8")
        data = file.read()
        file.close()
        return data

    image_path_list = get_file_from_folder('./static')

    result = {}
    for image in image_path_list:
        image_name = basename(image)
        image_name_without_addition = image_name.split('.')[0]
        image_path = f'http://localhost:3001/static/{image_name}'
        image_ocr_text_file_path = f'./Data/ocr_text/{image_name_without_addition}.txt'
        image_correct_text_file_path = f'./Data/correct_text/{image_name_without_addition}.txt'
        image_nlp_mahad_ocr_text_file_path = f'./Data/nlp_ocr_text/mahad/{image_name_without_addition}.txt'
        image_nlp_mahad_correct_text_file_path = f'./Data/nlp_correct_text/mahad/{image_name_without_addition}.txt'
        image_nlp_mahrous_ocr_text_file_path = f'./Data/nlp_ocr_text/mahrous/{image_name_without_addition}.txt'
        image_nlp_mahrous_correct_text_file_path = f'./Data/nlp_correct_text/mahrous/{image_name_without_addition}.txt'
        image_solve_file_path = f'./Data/solve/{image_name_without_addition}.txt'
        ocr_text = read_file(image_ocr_text_file_path)
        correct_text = read_file(image_correct_text_file_path)
        nlp_mahad_ocr_text = json.loads(read_file(image_nlp_mahad_ocr_text_file_path))
        nlp_mahad_correct_text = json.loads(read_file(image_nlp_mahad_correct_text_file_path))
        nlp_mahrous_ocr_text = json.loads(read_file(image_nlp_mahrous_ocr_text_file_path))
        nlp_mahrous_correct_text = json.loads(read_file(image_nlp_mahrous_correct_text_file_path))
        solve = json.loads(read_file(image_solve_file_path))

        result[image_name_without_addition] = {
            "correct_text": correct_text,
            "ocr_text": ocr_text,
            "image_path": image_path,
            "image_name": image_name,
            "nlp_mahad_ocr_text": nlp_mahad_ocr_text,
            "nlp_mahad_correct_text": nlp_mahad_correct_text,
            "nlp_mahrous_ocr_text": nlp_mahrous_ocr_text,
            "nlp_mahrous_correct_text": nlp_mahrous_correct_text,
            "solve": solve
        }
    return jsonify(result)

    pass

@app.route('/problems', methods=['POST'])
def set_problems():
    data = request.json['body']

    image_name_without_addition = data["image_name"].split('.')[0]
    image_correct_text_file_path = f'./Data/correct_text/{image_name_without_addition}.txt'
    with open(image_correct_text_file_path, 'w', encoding="utf8") as file:
        file.write(data['correct_text'])

    return jsonify(data)

@app.route("/keywords", methods=["GET"])
def get_keywords():
    pass

@app.route("/keywords", methods=["POST"])
def set_keywords():
    pass

@app.route('/', methods=['POST'])
def cameraapi():
    cwd = os.path.abspath(os.getcwd())
#     solution = {}
    image_data = request.json['image']
    name = str(f'{random.randint(0, 100000000000)}.png')
    img = Image.open(BytesIO(base64.b64decode(image_data.split(',')[1])))

    img_path = f'{cwd}/imageProcciessing/{name}'
    img.save(img_path)

    text = ocr(img_path)
    save_file(name=name[:-4]+'.txt', text=text, path=f"{cwd}/imageProcciessing/")
    nlp_result = nlp(text)
    quantitites = [item for data in nlp_result['data'] for item in data['symbols']]
    nlp_result['quantitites']=quantitites

    return jsonify(nlp_result)

@app.route("/solve")
def solve():

    new_quantitites = request.json['new_quantitites']
    obj = request.json['obj']

    return body

@app.route('/text', methods=['GET', 'POST'])
def textapi():
    solution = {}
    if request.method == 'POST':
        text = request.json["text"]
        grammar = request.json["grammar"]
        print(text, grammar)
        if (grammar == "Finder"):
            solution['result'] = tense_parser.find_tense_simple_form_str(text)
        elif (grammar == "Conjunction"):
            text, explain = GrammerSolver(text)
            solution['result'] = text
            solution['result way'] = explain
        else:
            solution['result'] = QuestionGenerater(f'''{text}.''')
        print(solution)
        return solution

    elif request.method == 'GET':
        return 'Hello'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=CONSTANTS['PORT'])


#!/usr/bin/env python3
import json
import random
import sys

def breed(dict1, dict2):
    offspring = {}
    offspring['head'] = random.choice([dict1['head'], dict2['head']])
    offspring['body'] = random.choice([dict1['body'], dict2['body']])
    offspring['arms'] = random.choice([dict1['arms'], dict2['arms']])
    offspring['legs'] = random.choice([dict1['legs'], dict2['legs']])
    offspring['tails'] = offspring['legs'] + offspring['arms']
    return(offspring)

def main():

    with open(sys.argv[1], 'r') as f:
        dict_animals = json.load(f)

    print("Random animal: ", random.choice(dict_animals['animals']))

    dict1 = random.choice(dict_animals['animals'])
    print("Parent 1: ", dict1)
    dict2 = random.choice(dict_animals['animals'])
    print("Parent 2: ", dict2)

    dict_animals['offspring'] = []
    offspring = breed(dict1, dict2)
    dict_animals['offspring'].append(offspring)
    print("Offspring of Parents 1 and 2: ", dict_animals['offspring'])

if __name__ == "__main__":
    main()


import numpy as np

import symforce.symbolic as sf
from symforce.test_util import TestCase
from symforce.test_util.cam_test_mixin import CamTestMixin

class CameraTest(CamTestMixin, TestCase):
    """
    Test the Camera class
    """

    @classmethod
    def element(cls) -> sf.Camera:
        return sf.Camera(
            calibration=sf.LinearCameraCal(focal_length=(440, 400), principal_point=(320, 240)),
            image_size=(640, 480),
        )

    def test_image_size(self) -> None:
        """
        Tests:
            Camera.in_view
            Camera.pixel_from_camera_point
            Camera.camera_ray_from_pixel
        """
        camera = self.element()
        assert camera.image_size is not None

        # Check that is_valid is set to 0 when point is outside FOV
        point_in_FOV = sf.V3(0, 0, 1)
        pixel_in_FOV, point_in_FOV_valid = camera.pixel_from_camera_point(point_in_FOV)
        self.assertTrue(point_in_FOV_valid == 1)
        self.assertTrue(camera.in_view(pixel_in_FOV, camera.image_size) == 1)

        point_outside_FOV = sf.V3(100, 0, 1)
        pixel_outside_FOV, point_outside_FOV_valid = camera.pixel_from_camera_point(
            point_outside_FOV
        )
        self.assertTrue(point_outside_FOV_valid == 0)
        self.assertTrue(camera.in_view(pixel_outside_FOV, camera.image_size) == 0)

class PosedCameraTest(CamTestMixin, TestCase):
    """
    Test the PosedCamera class
    """

    @classmethod
    def element(cls) -> sf.PosedCamera:
        return sf.PosedCamera(
            pose=sf.Pose3(R=sf.Rot3.from_yaw_pitch_roll(0.0, np.pi / 2.0, 0.0), t=sf.V3(0, 0, 100)),
            calibration=sf.LinearCameraCal(focal_length=(440, 400), principal_point=(320, 240)),
            image_size=(640, 480),
        )

    def test_posed_camera(self) -> None:
        """
        Tests:
            PosedCamera.pixel_from_global_point
            PosedCamera.global_point_from_pixel
        """
        posed_cam = self.element()

        # Transform some points to pixel coordinates and back
        for _ in range(100):
            global_point = (
                sf.V3(np.random.uniform(low=-1.0, high=1.0, size=(3,))) + posed_cam.pose.t
            )
            range_to_point = (global_point - posed_cam.pose.t).norm(epsilon=1e-9)
            pixel, is_valid = posed_cam.pixel_from_global_point(global_point)
            if is_valid == 1:
                # Make sure we reproject in the right direction
                global_point_reprojected, _ = posed_cam.global_point_from_pixel(
                    pixel, range_to_point=range_to_point
                )
                self.assertStorageNear(global_point, global_point_reprojected)

            # Transform pixel to global coordinates and back
            pixel = sf.V2(np.random.uniform(low=0, high=1000, size=(2,)))
            global_point, _ = posed_cam.global_point_from_pixel(pixel, range_to_point=1)
            pixel_reprojected, _ = posed_cam.pixel_from_global_point(global_point)
            self.assertStorageNear(pixel, pixel_reprojected)

    def test_warp_pixel(self) -> None:
        """
        Tests:
            PosedCamera.warp_pixel
        """
        # Create two cameras whose optical axes intersect
        posed_cam_1 = sf.PosedCamera(
            pose=sf.Pose3(
                R=sf.Rot3.from_yaw_pitch_roll(0.0, np.pi / 2.0, 0.0), t=sf.V3(0.0, 2.0, 0.0)
            ),
            calibration=sf.LinearCameraCal(focal_length=(440, 400), principal_point=(320, 240)),
        )
        posed_cam_2 = sf.PosedCamera(
            pose=sf.Pose3(
                R=sf.Rot3.from_yaw_pitch_roll(0.0, 0.0, -np.pi / 2.0), t=sf.V3(2.0, 0.0, 0.0)
            ),
            calibration=sf.LinearCameraCal(focal_length=(440, 400), principal_point=(320, 240)),
        )
        point_on_optical_axes = sf.V3(2.0, 2.0, 0.0)
        inverse_range = 0.5
        pixel_1, _ = posed_cam_1.pixel_from_global_point(point_on_optical_axes)
        pixel_2, is_valid_warp_into_2 = posed_cam_1.warp_pixel(
            pixel=pixel_1, inverse_range=inverse_range, target_cam=posed_cam_2
        )
        self.assertEqual(is_valid_warp_into_2, 1)
        self.assertStorageNear(pixel_1, pixel_2)

        # Try with a camera posed such that the point is invalid
        posed_cam_3 = sf.PosedCamera(
            pose=sf.Pose3(R=sf.Rot3(), t=sf.V3(0.0, 0.0, 1.0)),
            calibration=sf.LinearCameraCal(focal_length=(440, 400), principal_point=(320, 240)),
        )
        _, is_valid_warp_into_3 = posed_cam_1.warp_pixel(
            pixel=pixel_1, inverse_range=inverse_range, target_cam=posed_cam_3
        )
        self.assertEqual(is_valid_warp_into_3, 0)

        # Check that we don't get NaNs when using symbolic inverse_range and nonzero epsilon
        symbolic_inverse_range = sf.Symbol("inv_range")
        cam_1_ray = sf.V3(0.5, 1, 1)
        pixel_inf_1, _ = posed_cam_1.pixel_from_camera_point(cam_1_ray)
        pixel_inf_2, is_valid_inf = posed_cam_1.warp_pixel(
            pixel=pixel_inf_1,
            inverse_range=symbolic_inverse_range,
            target_cam=posed_cam_2,
            epsilon=self.EPSILON,
        )
        cam_2_ray = posed_cam_2.pose.R.inverse() * (posed_cam_1.pose.R * cam_1_ray)
        pixel_inf_2_rot_only, _ = posed_cam_2.pixel_from_camera_point(cam_2_ray)
        self.assertEqual(is_valid_inf.subs(symbolic_inverse_range, 0), 1)  # type: ignore
        self.assertStorageNear(
            pixel_inf_2.subs(symbolic_inverse_range, 0), pixel_inf_2_rot_only, places=4
        )

        # Check that when inverse_range = 0 and epsilon = 0 we exactly recover rotation-only math
        pixel_inf_2_exact, is_valid_inf = posed_cam_1.warp_pixel(
            pixel=pixel_inf_1, inverse_range=0, target_cam=posed_cam_2
        )
        self.assertEqual(is_valid_inf, 1)
        self.assertStorageNear(pixel_inf_2_exact, pixel_inf_2_rot_only, places=9)

if __name__ == "__main__":
    TestCase.main()


#!/usr/bin/env -S python

import asyncio
import can
import logging

log = logging.getLogger(__name__)

def rx_loop_gen(rx_bus, tx_busses):
    async def loop():
        reader = can.AsyncBufferedReader()
        loop = asyncio.get_running_loop()
        notifier = can.Notifier(rx_bus, [reader], loop=loop)
        async for msg in reader:
            for bus in tx_busses:
                bus.send(msg)

    return loop

async def run(configs):
    if len(configs) < 2:
        log.critical(f"Not enough devices specified ({len(configs)}), minimum 2 needed")
        return

    busses = []
    for cfg in configs:
        busses.append(can.Bus(**cfg))

    coroutines = []
    for i in range(0, len(busses)):
        l = rx_loop_gen(busses[i], [bus for ii, bus in enumerate(busses) if ii != i])
        coroutines.append(l())

    await asyncio.gather(*coroutines)

if __name__ == "__main__":
    import argparse
    import textwrap
    import json

    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description=textwrap.dedent(
            """\
        CAN gateway using python-can

        The config file (JSON) is expected to be an array of
        dictionaries - each of which represents one python-can
        bus. Each dictionary will be passed to the Bus
        constructor like so: can.Bus(**dict) .
        """
        ),
    )
    parser.add_argument("config")
    args = parser.parse_args()

    configs = None
    with open(args.config, "r") as f:
        configs = json.load(f)

    try:
        log.info("Starting Gateway")
        asyncio.run(run(configs))
    except KeyboardInterrupt:
        pass


from tkinter import *
import winsound

def lancer():
    #Jouer le son:
    winsound.PlaySound('coran.mp3', winsound.SND_LOOP | winsound.SND_ASYNC)
    """
    Pour jouer un fichier .wav, on utilise le fonction PlaySound  qui on
    transmet le nom du fichhier et en deuxieme argument, on place des flags , ici
    un flag pour jouer en boucle et un flag pour que le flux audio ne bloque pas
    le programme principal.
    """

def couper():
    #Couper le son :
    winsound.PlaySound(None, winsound.SND_PURGE)
    """
    Pour couper(provisoirement) le son, on utlise encore le fonction Playsound
    mais avec un argument valant None
    """

def close_sound():
    couper()
    app.destroy()

app= Tk()
app.title("Audio sour Tkinter avec Winsound")

app.protocol("WM_DELETE_WINDOW",close_sound)

Button(app, text="Activer", command=lancer, width=40).pack(pady=20)
Button(app, text="Désactiver", command=couper, width=40).pack(pady=20)

app.mainloop()

Tkinter/I - Information générales et transversales/4 - Quelques téchniques générales/9 - Audio sour Tkinter avec Winsound/index.py


#Day 2 - 2/12/19

#part one
def getIntsList():
    f = open("puzzle_input/day2.txt", "r")
    ints = [int(x.strip()) for x in f.readline().split(',')]
    f.close()
    return ints

def intCode(ints):
    for i in range(0, len(ints), 4):
        n = ints[i]
        #opcode 1 - add the two next digits and store in position given
        if(n == 1):
            a = ints[ints[i+1]]
            b = ints[ints[i+2]]
            ints[ints[i+3]] = a + b
        #opcode 2 - multiply the two next digits and store in position given
        elif(n == 2):
            a = ints[ints[i+1]]
            b = ints[ints[i+2]]
            ints[ints[i+3]] = a * b
        #opcode 99 - stop the program
        elif (n==99):
            break

    #return the diagnostic code at position 0 in list
    return ints[0]

#part two
def findNounAndVerb(noun=50, verb=0):
    #reset ints list
    ints = getIntsList()

    #insert noun and verb into position 1 and 2 in the program memory
    ints[1] = noun
    ints[2] = verb

    #run the intCode program
    result = intCode(ints)

    #check whether the output equals the target (19690720)
    if(result == 19690720):
        return 100*noun + verb

    #since verb is only added at the end, only change it if the result is within 19690720 +/- 99
    #starting verb at 0, we can just check within range 19690720 - 99
    elif(result >= 19690720 - 99):
        return findNounAndVerb(noun, verb+1)

    #otherwise, alter the noun
    elif(result > 19690720):
        return findNounAndVerb(noun-1, verb)
    elif(result < 19690720):
        return findNounAndVerb(noun+1, verb)
    else:
        print("oops")
        return

#main routine
ints = getIntsList()
ints[1] = 12
ints[2] = 2
print(intCode(ints))
print(findNounAndVerb())


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 22 18:03:08 2018

@author: sadra
"""

import numpy as np

import matplotlib
#matplotlib.use('Agg')

from scipy.spatial import ConvexHull
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.collections import PatchCollection

from main.auxilary_methods import vertices_cube

def visualize_funnel(s,iterations,xmin=-1,xmax=1,ymin=-1,ymax=1,xlabel='x_1',ylabel='x_2',title="interesting plot"):
    fig,ax1 = plt.subplots()
    #fig=plt.figure(figsize=(20,20),dpi=80, facecolor='w', edgecolor='k')
    ax1.set_xlabel(xlabel)
    ax1.set_ylabel(ylabel)
    ax1.set_xlim([xmin,xmax])
    ax1.set_ylim([ymin,ymax])
    p_list=[]
    list_of_states=s.X[0:s.tree_size[iterations]]
    for state in list_of_states:
        if state.mode==state.successor[0].mode:
            p_list.append(patches.Polygon(vertices_hull_eps(s,[state]+[state.successor[0]]), True))
        else:
            p_list.append(patches.Polygon(vertices_hull_eps(s,[state]), True))
    p=PatchCollection(p_list,color=(0.4,0.4,0.4))
    ax1.add_collection(p)
    ax1.grid(color=(0,0,0), linestyle='--', linewidth=0.3)
    ax1.set_title(title)
    ax1.plot([0,0],[-5,5],'black',linewidth=0.5)
    fig.savefig('figures/bouncing_ball_funnel_%d.png'%iterations, dpi=100)
    return fig

def vertices_hull_eps(s,list_of_states):
    """
    Convex hull computation of list of states
    """
    PV=np.empty((0,s.n))
    for p in list_of_states:
        PV=np.vstack((PV,p.vertices_eps+p.x.T))
    hull=ConvexHull(PV)
    return PV[hull.vertices,:]

def show_ball(s,t):
    x=0
    y=s.traj[t][0]
    p=[]
    p1=[]
    p2=[]
    fig,ax1 = plt.subplots()
    r=0.1
    ax1.set_aspect('equal')
    # Ground
    p1.append(patches.Polygon(np.array([[-0.5,-0.5,0.5,0.5],[0,-0.5,-0.5,0]]).T, True))
    p1=PatchCollection(p1,color=(0.5,0.7,1))
    # Goal
    p2.append(patches.Polygon(np.array([[-0.2,-0.2,0.2,0.2],[1.2,1,1,1.2]]).T, True))
    p2=PatchCollection(p2,color=(0.5,1,0.5))
    # Ball
    circle = patches.Circle([x,y], r)
    #fig=plt.figure(figsize=(10,10),dpi=80, facecolor='w', edgecolor='k')
    ax1.set_xlim([-0.5,0.5])
    ax1.set_ylim([-0.5,1.2])
    p.append(circle)
    p=PatchCollection(p,color=(0.99,0.5,0.5))
    ax1.add_collection(p2)
    ax1.add_collection(p1)
    ax1.add_collection(p)
    ax1.set_facecolor((0, 0.0, 0.0))
    ax1.set_title("",fontsize=20)
    fig.savefig('figures/ball_%d.png'%t, dpi=100)
    return fig

if False:
    for iteration in range(1,s.tree_iterations):
        fig=visualize_funnel(s,iteration,xmin=-0.1,xmax=1.2,ymin=-5,ymax=5,xlabel=r'Height',ylabel=r'Velocity',title="Bouncing Ball Iteration - %d"%iteration)

if True:
    for t in range(len(s.traj)):
        fig=show_ball(s,t)