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

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import StratifiedKFold
import time
import zipfile
from PIL import Image
import torch
import torch.nn as nn
import torchvision
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torchvision.transforms as transforms
from torch.optim.lr_scheduler import StepLR
from torch.utils.model_zoo import load_url as load_state_dict_from_url
from typing import Type, Any, Callable, Union, List, Optional
from torch.optim import lr_scheduler
import torchvision.models as models
from torchvision import *
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader
from tqdm import trange
import warnings
import argparse

warnings.filterwarnings('ignore')
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

## data
train_dir = "training_images.zip"
test_dir  = "testing_images.zip"
train_image_file = "training_labels.txt"
test_image_file  = "testing_img_order.txt"
class_file = "classes.txt"

##  preprocessing
parser = argparse.ArgumentParser()
parser.add_argument('--local_rank', default=-1, type=int,
                    help='node rank for distributed training')

parser.add_argument("--folds", type=int, default=5)
parser.add_argument("--seed", type=int, default=0)
parser.add_argument("--num_workers", type=int, default=0)
parser.add_argument("--n_classes", type=int, default=200)
parser.add_argument("--label_smoothing", type=float, default=0.125)
parser.add_argument("--grad_clip", type=int, default=1)
parser.add_argument("--test", action='store_true', default=True)
parser.add_argument("--batch_size", type=int, default=16)
parser.add_argument("--epochs1", type=int, default=20)
parser.add_argument("--epochs2", type=int, default=20)
parser.add_argument("--lr", type=float, default=1e-3)
parser.add_argument("--max_lr1", type=float, default=1e-3)
parser.add_argument("--max_lr2", type=float, default=1e-4)
parser.add_argument("--weight_decay", type=float, default=1e-6)
args = parser.parse_args(args=[])

class AddGaussianNoise(object):
    def __init__(self, mean=0., std=1.):
        self.std = std
        self.mean = mean

    def __call__(self, tensor):
        return tensor + torch.randn(tensor.size()) * self.std + self.mean

    def __repr__(self):
        return self.__class__.__name__ + '(mean={0}, std={1})'.format(self.mean, self.std)

train_transform = transforms.Compose([transforms.Resize([256,256]),
                                      transforms.RandomCrop([224,224], padding=4, padding_mode='reflect'),
                                      transforms.RandomHorizontalFlip(),
                                      transforms.RandomRotation(10),
                                      transforms.ToTensor(),
                                      transforms.Normalize([0.485, 0.456, 0.406],
                                                           [0.229, 0.224, 0.225]),
                                      AddGaussianNoise(0.1, 0.08),
                                      transforms.RandomErasing(inplace=True)
                                      # transforms.ToPILImage()
                                      ])
valid_transform = transforms.Compose([transforms.Resize([224,224]),
                                      transforms.ToTensor(),
                                      transforms.Normalize([0.485, 0.456, 0.406],
                                                           [0.229, 0.224, 0.225])
                                      ])

class Bird(Dataset):
    def __init__(self, data_dir, dataframe, test = False, transform = None):
        """
        Args:
            data_dir: path to image directory.
            dataframe : the dataframe containing images with corresponding labels.
            test: (bool) – If True, image_list_file is for test.
            transform: optional transform to be applied on a sample.
        """
        self.dir = data_dir
        self.df = dataframe
        self.test = test
        self.transform = transform

    def __getitem__(self, index):
        """
        Args:
            index: the index of item
        Returns:
            image and its labels
        """
        if not self.test:
            img, target = self.df.iloc[index, [0,2]].values
            with zipfile.ZipFile(self.dir, "r") as z:
                file_in_zip = z.namelist()
                if img in file_in_zip:
                    filelikeobject = z.open(img, 'r')
                    image = Image.open(filelikeobject).convert('RGB')
                else:
                    print("Not found " + img + "in " + self.dir)

            if self.transform is not None:
                image = self.transform(image)
            return image, torch.tensor(target)

        else:
            img = self.df.iloc[index][0]
            with zipfile.ZipFile(self.dir, "r") as z:
                file_in_zip = z.namelist()
                if img in file_in_zip:
                    filelikeobject = z.open(img, 'r')
                    image = Image.open(filelikeobject).convert('RGB')
                else:
                    print("Not found " + img + "in " + self.dir)

            if self.transform is not None:
                image = self.transform(image)
            return image

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

class ResNet50(nn.Module):
    """Model modified.
    The architecture of our model is the same as standard ResNet50
    except the classifier layer which has an additional sigmoid function.
    """
    def __init__(self, out_size, grad = False):
        super(ResNet50, self).__init__()
        self.network = models.resnet50(pretrained=True)
        num_ftrs = self.network.fc.in_features
        self.network.fc = nn.Sequential(nn.BatchNorm1d(num_ftrs),
                                        nn.ReLU(inplace=True),
                                        nn.Dropout(0.3),
                                        nn.Linear(num_ftrs, 512),
                                        nn.BatchNorm1d(512),
                                        nn.ReLU(inplace=True),
                                        nn.Dropout(0.3),
                                        nn.Linear(512, out_size)
                                        )

    def forward(self, x):
        x = self.network(x)
        return x

    def freeze(self):
        for param in self.network.parameters():
            param.require_grad = False
        for param in self.network.fc.parameters():
            param.require_grad = True

    def unfreeze(self):
        for param in self.network.parameters():
            param.require_grad = True

def draw_chart(chart_data, Fold):
    fig, ax = plt.subplots(1, 2, figsize=(10, 5))
    ax[0].plot(chart_data['epoch'],chart_data['train_loss'],label='train_loss')
    ax[0].plot(chart_data['epoch'],chart_data['val_loss'],label='val_loss')
    ax[0].grid(True,axis="y",ls='--')
    ax[0].legend(loc= 'best')
    ax[0].set_title('Loss on Training and Validation Data', fontsize=10)
    ax[0].set_xlabel('epoch',fontsize=10)
    ax[0].set_ylabel('Loss',fontsize=10)
    ax[1].plot(chart_data['epoch'],chart_data['val_acc'],label='val_acc')
    ax[1].plot(chart_data['epoch'],chart_data['train_acc'],label='train_acc')
    ax[1].grid(True,axis="y",ls='--')
    ax[1].legend(loc= 'best')
    ax[1].set_title('Accuracy  on Training and Validation Data',fontsize=10)
    ax[1].set_xlabel('epoch',fontsize=10)
    ax[1].set_ylabel('Accuracy',fontsize=10)

    plt.suptitle('Fold :{}'.format(Fold))
    plt.tight_layout()

def fit(model, dataloader, optimizer, criterion, scheduler, chart_data, acc_list, path_save, freeze = True):
    if freeze == True:
        epochs = args.epochs1
    else:
        epochs = args.epochs2
    total_epochs = args.epochs1 + args.epochs2

    for epoch in trange(epochs, desc="Epochs"):
        if freeze == True:
            chart_data['epoch'].append(epoch)
            print(f'Starting epoch {epoch+1}')
        else:
            chart_data['epoch'].append(epoch+args.epochs1)
            print(f'Starting epoch {epoch+args.epochs1+1}')
        for phase in ['train', 'val']:
            if phase == "train":
                model.train()
            else:
                model.eval()

            running_loss = 0.0
            running_accuracy = 0.0

            for i, (data , target) in enumerate(dataloader[phase]):
                data , target = data.to(device), target.to(device)
                if phase == 'train':
                    optimizer.zero_grad()

                output = model(data)
                _, preds = torch.max(output, dim=1)
                loss = criterion(output, target)

                if phase == 'train':
                    loss.backward()
                    if args.grad_clip:
                        nn.utils.clip_grad_value_(model.parameters(), args.grad_clip)
                    optimizer.step()

                running_loss += loss.item()
                running_accuracy += preds.eq(target).sum().item()
            epoch_loss = running_loss / len(dataloader[phase].dataset)
            epoch_acc = running_accuracy / len(dataloader[phase].dataset)

            if phase == 'train':
                chart_data['train_loss'].append((epoch_loss))
                chart_data['train_acc'].append(epoch_acc)
                curr_lr = optimizer.param_groups[0]['lr']
                print(f'LR:{curr_lr}')
                scheduler.step()

            if phase == 'val':
                chart_data['val_loss'].append((epoch_loss))
                chart_data['val_acc'].append(epoch_acc)
                acc_list.append(epoch_acc)
            if freeze == True:
                print('------------------------------------------------------------------------')
                print('Epoch [%d/%d]:%s Loss of the model:  %.4f %%' % (epoch+1, total_epochs, phase, 100 * epoch_loss))
                print('Epoch [%d/%d]:%s Accuracy of the model: %.4f %%' % (epoch+1, total_epochs,phase, 100 * epoch_acc))
                print('------------------------------------------------------------------------')
            else:
                print('------------------------------------------------------------------------')
                print('Epoch [%d/%d]:%s Loss of the model:  %.4f %%' % (epoch+args.epochs1+1, total_epochs, phase, 100 * epoch_loss))
                print('Epoch [%d/%d]:%s Accuracy of the model: %.4f %%' % (epoch+args.epochs1+1, total_epochs,phase, 100 * epoch_acc))
                print('------------------------------------------------------------------------')

        if freeze == False:
            torch.save({'model_state_dict':model.state_dict()},
                       os.path.join(path_save,str(epoch+args.epochs1+1)+'_'+str(epoch_acc)+'.pth'))
    return chart_data, acc_list

def training():
    since = time.time()
    df = pd.read_table(train_image_file, sep = "\s", header=None,
                       names = ["Images", "Class_names"])
    df['labels'] = df.groupby(['Class_names']).ngroup()

    # kfolds
    kfolds = StratifiedKFold(n_splits=args.folds, shuffle=True, random_state=args.seed)
    for fold in range(args.folds):
        train_idx, valid_idx = list(kfolds.split(np.arange(len(df)), df['labels']))[fold]
        train_set = BirdDataSet(train_dir, df.iloc[train_idx, :], transform = train_transform)
        valid_set = BirdDataSet(train_dir, df.iloc[valid_idx, :], transform = valid_transform)
        print('='*70)
        print(f'FOLD {fold} for model')
        print('train_size: {}, valid_size: {}'.format(len(train_idx), len(valid_idx)))
        print('='*70)

        dataloader  = {"train" : DataLoader(dataset = train_set, batch_size = args.batch_size,
                                            shuffle = True, num_workers = args.num_workers),
                        "val"  : DataLoader(dataset = valid_set, batch_size = args.batch_size,
                                            shuffle = False, num_workers = args.num_workers)}
        since = time.time()
        path_save = f"resnet50/fold_{fold}"
        if not os.path.exists(path_save):
            os.makedirs(path_save)
        chart_data={"train_loss":[],"val_loss":[], "val_acc":[],"train_acc":[],"epoch":[]}

        model = ResNet50(args.n_classes).to(device)
        if torch.cuda.device_count() > 1:
            model = nn.DataParallel(model, device_ids=[0,1,2,3])
        criterion = nn.CrossEntropyLoss()
        optimizer = optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay)

        acc_list = []
        print('model freeze:')
        model.freeze()
        if torch.cuda.device_count() > 1:
            model1 = nn.DataParallel(model, device_ids=[0,1,2,3])
        scheduler = lr_scheduler.OneCycleLR(optimizer, args.max_lr1, epochs = args.epochs1,
                                            steps_per_epoch=len(dataloader['train']))
        chart_data, acc_list = fit(model, dataloader, optimizer, criterion,
                                   scheduler, chart_data, acc_list, path_save)

        print('model unfreeze:')
        model.unfreeze()
        if torch.cuda.device_count() > 1:
            model2 = nn.DataParallel(model, device_ids=[0,1,2,3])
        scheduler = lr_scheduler.OneCycleLR(optimizer, args.max_lr2, epochs = args.epochs2,
                                            steps_per_epoch=len(dataloader['train']))
        chart_data, acc_list = fit(model, dataloader, optimizer, criterion,
                                   scheduler, chart_data, acc_list, path_save, freeze = False)

        time_elapsed = time.time() - since
        print('Training complete in {:.0f}m {:.0f}s'.format(
            time_elapsed // 60, time_elapsed % 60))
        print('Best val Acc for epoch {}: {:.4f}%'.format(np.argmax(acc_list)+1, max(acc_list) * 100))
        draw_chart(chart_data, fold)

if __name__ == "__main__":
    if args.test:
        testing()
    else:
        training()
    torch.cuda.empty_cache()


from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.views.generic import TemplateView
from django.views import defaults as default_views
from rest_framework_jwt.views import obtain_jwt_token
from space_manager import views

urlpatterns = [
    # Django Admin, use {% url 'admin:index' %}
    url(settings.ADMIN_URL, admin.site.urls),

    # User management
    url(r'^rest-auth/', include('rest_auth.urls')),
    url(r'^rest-auth/registration/', include('rest_auth.registration.urls')),
    url(r'^seats/', include('space_manager.seats.urls', namespace='seats')),
    url(r'^rooms/', include('space_manager.rooms.urls', namespace='rooms')),
    url(r'^users/', include('space_manager.users.urls', namespace='users')),
    url(r'^cabinets/',
        include('space_manager.cabinets.urls', namespace='cabinets')),
    url(r'^membership/',
        include('space_manager.membership.urls', namespace='membership')),
    url(r'^branch/',
        include('space_manager.branches.urls', namespace='branches')),
    url(r'^payment/',
        include('space_manager.payment.urls', namespace='payments')),
    url(r'^qrcodes/', include(
        'space_manager.qrcodes.urls', namespace='qrcodes')),
    # url(r'^accounts/', include('allauth.urls')),
    # url(r'^', views.ReactAppView.as_view()),

    # Your stuff: custom urls includes go here
] + static(
    settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

# urlpatterns += [
#     url(r'^', views.ReactAppView.as_view()),
# ]
if settings.DEBUG:
    # This allows the error pages to be debugged during development, just visit
    # these url in browser to see how these error pages look like.
    urlpatterns += [
        url(r'^400/$',
            default_views.bad_request,
            kwargs={
                'exception': Exception('Bad Request!')
            }),
        url(r'^403/$',
            default_views.permission_denied,
            kwargs={
                'exception': Exception('Permission Denied')
            }),
        url(r'^404/$',
            default_views.page_not_found,
            kwargs={
                'exception': Exception('Page not Found')
            }),
        url(r'^500/$', default_views.server_error),
    ]
    if 'debug_toolbar' in settings.INSTALLED_APPS:
        import debug_toolbar
        urlpatterns = [
            url(r'^__debug__/', include(debug_toolbar.urls)),
        ] + urlpatterns


from nltk.tree import *
from fastapi import FastAPI
from itertools import *

def flatten_nested_list(lst):
    if not any((type(i) == list) for i in lst):
        return [lst]

    result = []
    for position, item in enumerate(lst):
        if type(item) == list:
            for nested_item in item:
                new_lst = lst[:position] + [nested_item] + lst[position + 1:]
                result += (flatten_nested_list(new_lst))
            break

    return result

# this function relies on the fact, that positions[0] is a list of normal positions of elements we want to combine
def make_combinations(lst, positions):  # [[0, 2], [2, 0]]
    result = []
    for pos in positions:
        new_lst = lst.copy()
        normal_positions = positions[0]
        for i in range(len(pos)):
            new_lst[pos[i]] = lst[normal_positions[i]]
        result.append(new_lst)
    return result

def paraphrases(tree, change_labels=('NP',), conjunctions=('CC', ','), limit=20):
    def paraphrase_children(children):
        child_result = []
        for child in children:
            child_result.append(paraphrases(child))
        return flatten_nested_list(child_result)

    # case - leaf
    if len(tree) == 1:
        return tree

    # case - "NP"
    if tree.label() in change_labels:
        positions_to_change = []
        length = len(tree)
        position = 0
        for position, child in enumerate(tree):
            if child.label() in change_labels:
                positions_to_change.append(position)
            elif child.label() not in conjunctions:
                break

        # any other element is encountered
        if position < length - 1:
            to_add = paraphrase_children(tree)
            result = []
            for comb in to_add:
                if limit != -1 and len(result) >= limit:
                    return result
                result.append(Tree(tree.label(), comb))

        # only elements from change_labels and conjunctions are encountered
        else:
            lst_combinations = make_combinations(tree, list(permutations(positions_to_change)))
            result = []
            for tree_comb in lst_combinations:
                if limit != -1 and len(result) >= limit:
                    return result
                to_add = paraphrase_children(tree_comb)
                for comb in to_add:
                    result.append(Tree(tree.label(), comb))

    else:
        to_add = paraphrase_children(tree)
        result = []
        for combination in to_add:
            if limit != -1 and len(result) >= limit:
                return result
            result.append(Tree(tree.label(), combination))

    return result

def console_test(test_str, test_limit):
    tree = Tree.fromstring(test_str)
    lst_paraphrases = paraphrases(tree, limit=test_limit)
    for iterator in lst_paraphrases:
        print(iterator.leaves())

#console_test('(NP(NP I)(, ,)(NP her)(CC or)(NP him))', 4)

app = FastAPI()

@app.get("/paraphrase")
def read_tree(tree: str, limit: int = 20):
    lst = paraphrases(Tree.fromstring(tree), limit=limit)
    data = [{"tree": var.__str__().replace('\n', ' ')} for var in lst]
    return {"paraphrases": data}


#指先を検出するプログラム
import cv2
import numpy as np
import sys
from scipy.spatial import distance
import time

def main():
    # 入力画像の取得
    t1 = time.time()
    im = cv2.imread("%s"%param[1])
    height,width = im.shape[:2]
    gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
    cv2.rectangle(gray, (0, height-200), (width-1, height-1), (0, 0, 0), -1)

    label = cv2.connectedComponentsWithStats(gray)
    # ブロブ情報を項目別に抽出
    n = label[0] - 1
    data = np.delete(label[2], 0, 0)
    center = np.delete(label[3], 0, 0)
    max_index = np.argsort(data[:,4])[::-1][0] + 1
    #print("最大面積のラベル番号", max_index)
    x_center = int(center[max_index -1][0])
    y_center = int(center[max_index -1][1])

    dst = label[1]
    list = []
    list2 = []
    #a = np.array(center[max_index -1])
    a = np.array([y_center,x_center])
    print("1a")

    for y in range(height):
        for x in range(width):
            if dst[y][x] == max_index:
                b = np.array([y,x])
                list.append(distance.euclidean(a,b))

    #ave = np.mean(list)
    ave = np.average(list)
    print("1b")

    for y in range(height):
        for x in range(width):
            if dst[y][x] == max_index:
                c = np.array([y,x])
                #dis = np.linalg.norm(a-c)
                dis = distance.euclidean(a,c)
                list2.append(dis - ave)
                #print(dis - ave)
                if (dis - ave) < 1000:
                    im[y][x] = (255,255,0)

    print("1c")
    ave2 = np.average(list2)
    su = np.sum(list2)

    print("中心との距離の平均",ave)
    print("偏差の平均",ave2)
    print("偏差の和",su)

    """
    # ラベルの個数nだけ色を用意
    print("ブロブの個数:", n)
    print("各ブロブの外接矩形の左上x座標", data[:,0])
    print("各ブロブの外接矩形の左上y座標", data[:,1])
    print("各ブロブの外接矩形の幅", data[:,2])
    print("各ブロブの外接矩形の高さ", data[:,3])
    print("各ブロブの面積", data[:,4])
    print("中心座標:\n",center[max_index - 1])
    """

    cv2.circle(im,(x_center,y_center), 20,(0,0,255),-1)
    cv2.imwrite("yubisaki.jpg", im)
    t2 = time.time()
    elapsed_time = t2-t1
    print(f"経過時間：{elapsed_time}")

if __name__ == '__main__':
    param = sys.argv
    main()


from opensearchpy import OpenSearch

host = 'localhost'
port = 9200
auth = ('admin', 'admin') # For testing only. Don't store credentials in code.
# ca_certs_path = '/full/path/to/root-ca.pem' # Provide a CA bundle if you use intermediate CAs with your root CA.

# Optional client certificates if you don't want to use HTTP basic authentication.
# client_cert_path = '/full/path/to/client.pem'
# client_key_path = '/full/path/to/client-key.pem'

# Create the client with SSL/TLS enabled, but hostname verification disabled.
client = OpenSearch(
    hosts = [{'host': host, 'port': port}],
    http_compress = True, # enables gzip compression for request bodies
    http_auth = auth,
    # client_cert = client_cert_path,
    # client_key = client_key_path,
    use_ssl = True,
    verify_certs = False,
    ssl_assert_hostname = False,
    ssl_show_warn = False,
    # ca_certs = ca_certs_path
)

# Create an index with non-default settings.
index_name = 'qa_index_384'

# document = {
#   'Q_text':'abc',
#   'Q_vec':[1,2,3,4],
#   'Ans':'ABC',
# }

from sentence_transformers import SentenceTransformer, util
model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
embedding = model.encode("朋友圈信息流", convert_to_tensor=True)
Q_vec = embedding.tolist()
document = {
  'Q_text':"朋友圈信息流",
  'Q_vec':Q_vec,
  'Ans':'ABC',
}
print(len(Q_vec))
id = '20'

response = client.index(
    index = index_name,
    body = document,
    id = id,
    refresh = True
)

print('\nAdding document:')
print(response)


"""

Filename: pca_tryme

Purpose: Test the pca_tools module

Author: John Weaver
Date created: 02.05.18
Possible problems:

"""
# ------------------------------------------------------------------------------
# Standard Packages
# ------------------------------------------------------------------------------
import numpy as np
import matplotlib.pyplot as plt

# ------------------------------------------------------------------------------
# Additional Packages
# ------------------------------------------------------------------------------
import pygappy.pca_projection as pca
import pygappy.visualization as pcaplot

# ------------------------------------------------------------------------------
# Parameters
# ------------------------------------------------------------------------------

# Number of eigenspectra to use ( >2 & <<440)
Nspec = 10

# Number of test spectra to generate
Ntry = 100

# Verbosity
verbose = True

# Noise
noise = True

# Norm factor
appnorm = 1.

# PCs to be shown
Nshow = 0

# Display error windows
windows = True
windowsize = 10 # in pixels

# Number of gaps
Ngappy = 10

# Seed
np.random.seed(0)

# ------------------------------------------------------------------------------
# Declarations and Functions
# ------------------------------------------------------------------------------

# Get eigensystem
ewave, espec, emean, evar = pca.eigenbasis()
espec = espec[:Nspec]

# Generate test spectra
test_pcs = np.random.normal(0, 0.5, (Ntry, Nspec))
test_pcs[:, 0] = np.random.normal(-6, 2, Ntry)
test_pcs[:, 1] = np.random.normal(0, 2, Ntry)
test_spec = test_pcs @ espec

# Generate error array
error = np.ones(440)
for i in np.random.randint(0, 440, Ngappy):
    start = i
    end = i + windowsize
    if end >= len(error):
        end = len(error) - 1
    error[start: end] = 0.

# Create figure
fig1, ax1 = plt.subplots(ncols=1, nrows=2)

# ------------------------------------------------------------------------------
# Main Program
# ------------------------------------------------------------------------------

# Loop over test spectra
for i, spec in enumerate(test_spec):

    # Prepare data
    data = emean + spec
    if noise:
        data = data + np.random.normal(0, 0.02, 440)
    data = appnorm * (data / np.mean(data))

    if verbose:
        print(f'\n{i+1}/{Ntry} -----------------------------------')
        x, y, z = test_pcs[i, :3]
        print(f'    first 3 PCs: {x:2.1f} {y:2.1f} {z:2.1f}')

    # Get GappyPCA results
    gpcs, cov = pca.gappy(
        data, error, espec, emean, cov=True, verbose=verbose)

    # Check them
    if verbose:
        x, y, z = gpcs[:3]
        print(
            f'    GappyPCA: {x:2.1f} {-y:2.1f} {z:2.1f}'
        )

    # Plot them
    pcaplot.model(data, ewave, gpcs, espec, ax=ax1[0], error=error, mean=emean,
                   nshow=Nshow, windows=windows)

    # Get NormGappyPCA results
    npcs, norm, cov = pca.normgappy(
        data, error, espec, emean, cov=True, verbose=verbose)

    # Check them
    if verbose:
        x, y, z = npcs[:3]
        print(
            f'    NormGappyPCA: {x:2.1f} {-y:2.1f} {z:2.1f} | N={norm:2.1f}'
        )

    # Plot them
    pcaplot.model(data, ewave, npcs, espec, ax=ax1[1], error=error, mean=emean,
                   norm=norm, nshow=Nshow, windows=windows)

    # ID the axes
    ax1[0].text(
        0.1, 0.1, 'GappyPCA', color='darkred', transform=ax1[0].transAxes)
    ax1[1].text(
        0.1,
        0.1,
        'NormGappyPCA',
        color='royalblue',
        transform=ax1[1].transAxes)

    # Next
    plt.pause(0.001)
    if (i + 1) != Ntry:
        input('\nNext? ')
        ax1[0].clear()
        ax1[1].clear()
    else:
        print('\nEnd.')

# End Script


from django.db import models
from django.conf import settings
from django.contrib.auth.models import User
from django.utils import timezone
from django.urls import reverse

# Create your models here.
#Quiz Model
class Quiz(models.Model):
    title = models.CharField(max_length=100, verbose_name="Quiz Title")
    slug = models.SlugField(max_length=200, unique=True, null=True)
    created = models.DateTimeField(auto_now_add=True, null=True)
    date = models.DateTimeField(null=True, blank=True)
    duration = models.PositiveIntegerField(verbose_name="Quiz Duration (mins)", null=True, blank=True)
    active = models.BooleanField(default=True)

    class Meta:
        ordering = ['title']

    def __str__(self):
        return self.title

    def get_group_detail(self):
        return reverse('quiz_detail', args=[self.slug])

#Question Model
class Question(models.Model):
    quiz = models.ForeignKey(Quiz, on_delete=models.CASCADE, related_name="questions", verbose_name="Quiz", null=True)
    order = models.PositiveIntegerField(verbose_name="Question Order", default=1)
    question = models.TextField(null=True, blank=True, verbose_name="Question")
    image = models.ImageField(upload_to='questions/image/', blank=True, null=True, verbose_name='Question image')
    optionA = models.CharField(max_length=200, null=True, blank=True, verbose_name="Option A")
    optionB = models.CharField(max_length=200, null=True, blank=True, verbose_name="Option B")
    optionC = models.CharField(max_length=200, null=True, blank=True, verbose_name="Option C")
    optionD = models.CharField(max_length=200, null=True, blank=True, verbose_name="Option D")
    cat = (('optionA', 'optionA'), ('optionB', 'optionB'), ('optionC', 'optionC'), ('optionD', 'optionD'))
    answer = models.CharField(max_length=200, choices=cat)

    class Meta:
        ordering = ['order']

    def __str__(self):
        return f'Question {self.order}'

# Groups Model
GROUP_CHOICES = (
    ('Public', 'Public'),
    ('Private', 'Private')
)
class Group(models.Model):
    title = models.CharField(max_length=100, verbose_name="Group Title", null=True)
    description = models.TextField(verbose_name="Group Description", null=True, blank=True)
    type = models.CharField(max_length=100, choices=GROUP_CHOICES, verbose_name="Group Type", default='Public')
    slug = models.SlugField(max_length=200, unique=True, null=True)
    image = models.ImageField(upload_to='groups/image/', blank=True, null=True, verbose_name='Group image')
    admins = models.ManyToManyField(User, related_name="group_admin", blank=True)
    members = models.ManyToManyField(User, related_name="group_member",blank=True)
    quizzes = models.ManyToManyField(Quiz, related_name="group", blank=True)

    def get_group_detail(self):
        return reverse('group_detail', args=[self.slug])

    class Meta:
        ordering = ['title']

    def __str__(self):
        return self.title

#Profile Model
class Profile(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="profile")
    f_name = models.CharField(max_length=100, verbose_name="First Name", null=True)
    l_name = models.CharField(max_length=100, verbose_name="Last Name", null=True)
    photo = models.ImageField(upload_to='users/photo/', blank=True, null=True, verbose_name='Profile Pic')
    about = models.TextField(verbose_name="About", null=True, blank=True)
    school = models.CharField(max_length=200, verbose_name="Institution", null=True, blank=True)
    course = models.CharField(max_length=200, verbose_name="Course Studying", null=True, blank=True)
    quiz = models.ManyToManyField(Quiz, related_name="creator", blank=True)

    def get_user_profile(self):
        return reverse('user_profile', args=[self.user.username])

    def __str__(self):
        return f'{self.f_name} {self.l_name}'

    class Meta:
        ordering = ['l_name']

# Score Model
REMARK_CHOICES = (
    ('Gold', 'Gold'),
    ('Silver', 'Silver'),
    ('Bronze', 'Bronze'),
    ('Copper', 'Copper'),
)
class Score(models.Model):
    student = models.ForeignKey(Profile, on_delete=models.CASCADE, related_name="scores")
    name = models.CharField(max_length=100, null=True, blank=True, verbose_name="name")
    quiz = models.ForeignKey(Quiz, on_delete=models.CASCADE, related_name="score")
    score = models.FloatField(verbose_name="Total Score")
    remark = models.CharField(max_length=100, choices=REMARK_CHOICES, null=True, blank=True, verbose_name="Remark")
    created = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f'{self.student}'

    class Meta:
        ordering = ['-score']

class Chat(models.Model):
    sender = models.CharField(max_length=100, null=True, verbose_name="sender")
    receiver = models.CharField(max_length=100, null=True, verbose_name="receiver")
    message = models.TextField(verbose_name="message")
    date = models.DateTimeField(auto_now_add=True, null=True)
    seen = models.BooleanField(default=False, null=True)

    def __str__(self):
        return f'{self.sender}'

    class Meta:
        ordering = ['date']

class Developer(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="developer", null=True)
    nickname = models.CharField(max_length=100, verbose_name="Nickname", null=True)
    f_name = models.CharField(max_length=100, verbose_name="First Name", null=True)
    l_name = models.CharField(max_length=100, verbose_name="Last Name", null=True)
    photo = models.ImageField(upload_to='devs/photo/', blank=True, null=True, verbose_name='Profile Pic')
    about = models.TextField(verbose_name="About", null=True, blank=True)
    organization = models.CharField(max_length=200, verbose_name="Organization", null=True, blank=True)
    location = models.CharField(max_length=200, verbose_name="Location", null=True, blank=True)
    skill = models.CharField(max_length=200, verbose_name="Skills", null=True, blank=True)
    email = models.EmailField(max_length=200, verbose_name="Email", null=True, blank=True)
    phone_no = models.CharField(max_length=200, verbose_name="Phone Number", null=True, blank=True)
    website = models.URLField(max_length=200, verbose_name="website", null=True, blank=True)
    facebook = models.URLField(max_length=200, verbose_name="facebook", null=True, blank=True)
    twitter = models.URLField(max_length=200, verbose_name="twitter", null=True, blank=True)
    github = models.URLField(max_length=200, verbose_name="github", null=True, blank=True)
    whatsapp = models.URLField(max_length=200, verbose_name="whatsapp", null=True, blank=True)

    def __str__(self):
        return f'{self.nickname}'


import json
from datetime import datetime

import pymysql
import requests
from dotenv import dotenv_values
from fastapi import HTTPException
from pymysql import MySQLError

config_env = dotenv_values(".env")

def replace_none_with_empty_string(data):
    if isinstance(data, list):
        return [replace_none_with_empty_string(item) for item in data]
    elif isinstance(data, dict):
        return {key: replace_none_with_empty_string(value) if value is not None else "" for key, value in data.items()}
    else:
        return data if data is not None else ""

async def call_api_send_async(db):
    url = config_env['URL_SEND_DDC']
    query = "SELECT " \
            "p.hoscode, " \
            "p.hosname, " \
            "p.vn, " \
            "p.cid, " \
            "p.passport_no, " \
            "p.prefix, " \
            "p.first_name, " \
            "p.last_name, " \
            "p.nationality, " \
            "p.gender, " \
            "p.birth_date, " \
            "p.age_y, " \
            "p.age_m, " \
            "p.age_d, " \
            "p.marital_status_id, " \
            "p.address, " \
            "p.moo, " \
            "p.road, " \
            "p.chw_code, " \
            "p.amp_code, " \
            "p.tmb_code, " \
            "p.mobile_phone, " \
            "p.occupation, " \
            "e.epidem_report_guid, " \
            "e.epidem_report_group_code, " \
            "p.hoscode as treated_hospital_code, " \
            "e.report_datetime, " \
            "e.onset_date, " \
            "e.treated_date, " \
            "e.diagnosis_date, " \
            "e.death_date, " \
            "e.Cdeath, " \
            "e.informer_name, " \
            "e.diagnosis_icd10, " \
            "e.diagnosis_icd10_list, " \
            "e.organism, " \
            "e.complication, " \
            "e.epidem_person_status_id, " \
            "e.epidem_symptom_type_id, " \
            "e.respirator_status, " \
            "e.vaccinated_status, " \
            "'3' as municipal,  " \
            "e.epidem_address, " \
            "e.epidem_moo, " \
            "e.epidem_road, " \
            "e.epidem_chw_code, " \
            "e.epidem_amp_code, " \
            "e.epidem_tmb_code, " \
            "e.location_gis_latitude, " \
            "e.location_gis_longitude, " \
            "'50' as isolate_chw_code, " \
            "e.patient_type, " \
            "e.active_case_finding, " \
            "e.epidem_cluster_type_id, " \
            "e.cluster_latitude, " \
            "e.cluster_longitude, " \
            "e.`comment`, " \
            "l.epidem_lab_confirm_type_id, " \
            "l.lab_report_date, " \
            "l.lab_report_result, " \
            "l.specimen_date, " \
            "l.specimen_place_id, " \
            "l.lab_his_ref_code, " \
            "l.lab_his_ref_name, " \
            "l.tmlt_code ," \
            "p.message_from_ddc, " \
            "p.send_ddc_moph, " \
            "user_moph.token " \
            " FROM	ddc_person AS p " \
            "INNER JOIN	ddc_epidem_report AS e	ON p.hoscode = e.hoscode AND p.vn = e.vn AND p.hn = e.hn " \
            "LEFT JOIN	ddc_lab_report AS l	ON e.hoscode = l.hoscode AND e.vn = l.vn AND e.hn = l.hn " \
            "INNER JOIN user_moph on user_moph.hoscode = p.hoscode and user_moph.active = 1 " \
            "WHERE (p.message_from_ddc <> 'OK' OR p.message_from_ddc IS NULL) " \
			"AND e.diagnosis_icd10 in ('U071','U072') " \
			"AND e.hoscode = '10713' " \
            " GROUP BY e.hoscode, e.vn, e.hn "

    try:
        with db.cursor(pymysql.cursors.DictCursor) as cursor:
            cursor.execute(query)
            results = cursor.fetchall()

            # datetime now
            print("start time = " + datetime.now().strftime('%Y-%m-%d %H:%M:%S'))

            for row in results:
                # Assuming 'birth_date' is the column containing datetime objects
                row['birth_date'] = row['birth_date'].strftime('%Y-%m-%d')

                json_data = {
                    "hospital": {
                        "hospital_code": row['hoscode'],
                        "hospital_name": row['hosname'],
                        "his_identifier": "ihims | epidem-cm"
                    },
                    "person": {
                        "cid": row['cid'],
                        "passport_no": row['passport_no'],
                        "prefix": row['prefix'],
                        "first_name": row['first_name'],
                        "last_name": row['last_name'],
                        "nationality": row['nationality'],
                        "gender": row['gender'],
                        "birth_date": row['birth_date'],
                        "age_y": row['age_y'],
                        "age_m": row['age_m'],
                        "age_d": row['age_d'],
                        "marital_status_id": row['marital_status_id'],
                        "address": row['address'],
                        "moo": row['moo'],
                        "road": row['road'],
                        "chw_code": row['chw_code'],
                        "amp_code": row['amp_code'],
                        "tmb_code": row['tmb_code'],
                        "mobile_phone": row['mobile_phone'],
                        "occupation": row['occupation'],
                    },
                    "epidem_report": {
                        "epidem_report_guid": row['epidem_report_guid'],
                        "epidem_report_group_code": row['epidem_report_group_code'],
                        "treated_hospital_code": row['treated_hospital_code'],
                        "report_datetime": row['report_datetime'],
                        "onset_date": row['onset_date'],
                        "treated_date": row['treated_date'],
                        "diagnosis_date": row['diagnosis_date'],
                        "death_date": row["death_date"],
                        "organism": row["organism"],
                        "complication": row["complication"],
                        "cdeath": row["Cdeath"],
                        "informer_name": row["informer_name"],
                        "principal_diagnosis_icd10": row["diagnosis_icd10"],
                        "diagnosis_icd10_list": row["diagnosis_icd10_list"],
                        "epidem_person_status_id": row["epidem_person_status_id"],
                        "epidem_symptom_type_id": row["epidem_symptom_type_id"],
                        "municipal": row["municipal"],
                        "respirator_status": row["respirator_status"],
                        "vaccinated_status": row["vaccinated_status"],
                        "epidem_address": row["epidem_address"],
                        "epidem_moo": row["epidem_moo"],
                        "epidem_road": row["epidem_road"],
                        "epidem_chw_code": row["epidem_chw_code"],
                        "epidem_amp_code": row["epidem_amp_code"],
                        "epidem_tmb_code": row["epidem_tmb_code"],
                        "location_gis_latitude": row["location_gis_latitude"],
                        "location_gis_longitude": row["location_gis_longitude"],
                        "isolate_chw_code": row["isolate_chw_code"],
                        "patient_type": row["patient_type"],
                        "active_case_finding": row["active_case_finding"],
                        "epidem_cluster_type_id": row["epidem_cluster_type_id"],
                        "cluster_latitude": row["cluster_latitude"],
                        "cluster_longitude": row["cluster_longitude"],
                        "comment": row["comment"]
                    },
                    "lab_report": {
                        "epidem_lab_confirm_type_id": row["epidem_lab_confirm_type_id"],
                        "specimen_date": row["specimen_date"],
                        "specimen_place_id": row["specimen_place_id"],
                        # short if
                        "lab_report_date": row["lab_report_date"],
                        "lab_report_result": row["lab_report_result"],
                        "lab_his_ref_code": row["lab_his_ref_code"],
                        "lab_his_ref_name": row["lab_his_ref_name"],
                        "tmlt_code": row["tmlt_code"]
                    },
                }

                headers = {
                    'Content-type': 'Application/json',
                    'Authorization': 'Bearer ' + row['token']
                }

                modified_json_data = replace_none_with_empty_string(json_data)
                # print("this is payload = " + json.dumps(modified_json_data))
                response = requests.request("POST", url, headers=headers, data=json.dumps(modified_json_data))
                # print("this is payload = " + json.dumps(modified_json_data))

                # all_json_data.append(modified_json_data)
                rows_count = cursor.rowcount
                print(response.text)

                json_response = json.loads(response.text)
                message = json_response['Message']

                sql = "UPDATE ddc_person " \
                      "SET send_ddc_moph = '1' , message_from_ddc = %s" \
                      "WHERE ddc_person.hoscode = %s AND ddc_person.vn = %s"
                #         not in with
                cursor.execute(sql, (message, row['hoscode'], row['vn']))
                db.commit()

        # print(f"insert {rows_count} record")
        print("end time = " + datetime.now().strftime('%Y-%m-%d %H:%M:%S'))

    except MySQLError as e:
        print(e)
        # raise HTTPException(status_code=500, detail="Database error")


#!/usr/bin/env python

# WS server example that synchronizes state across clients

import asyncio
import json
import logging
import websockets
from definitions import WEBSOCKET_IP, WEBSOCKET_PORT

logging.basicConfig()

STATE = {"task": None,
         "parameters": None}

USERS = set()

def parameters_event():
    if STATE['parameters'] is None:
        p = json.dumps({"type": "parameters", "has_data": False, "parameters": ""})
    else:
        p = json.dumps({"type": "parameters", "has_data": True, "parameters": json.dumps(STATE['parameters'])})
    return p

def specific_parameter_event(name):
    if STATE['parameters'] is None:
        p = json.dumps({"type": "iparam", "has_data": False, "value": ""})
    else:
        p = json.dumps({"type": "iparam", "has_data": True, "value": json.dumps(STATE['parameters'][name])})
    return p

def task_event():
    if STATE['task'] is None:
        p = json.dumps({"type": "task", "has_data": False, "task": ""})
    else:
        p = json.dumps({"type": "task", "has_data": True, "task": STATE['task']})
    return p

def users_event():
    return json.dumps({"type": "users", "count": len(USERS)})

async def notify_parameters():
    if USERS:  # asyncio.wait doesn't accept an empty list
        message = parameters_event()
        await asyncio.wait([user.send(message) for user in USERS])

async def notify_specific_parameter(name):
    if USERS:
        message = specific_parameter_event(name)
        await asyncio.wait([user.send(message) for user in USERS])

async def notify_task():
    if USERS:  # asyncio.wait doesn't accept an empty list
        message = task_event()
        await asyncio.wait([user.send(message) for user in USERS])

async def notify_users():
    if USERS:  # asyncio.wait doesn't accept an empty list
        message = users_event()
        await asyncio.wait([user.send(message) for user in USERS])

async def register(websocket):
    USERS.add(websocket)
    await notify_users()

async def unregister(websocket):
    USERS.remove(websocket)
    await notify_users()

async def serve_parameters(websocket, path):
    # register(websocket) sends user_event() to websocket
    try:
        await register(websocket)
        async for message in websocket:
            data = json.loads(message)
            print(data)
            if data["type"] == "set_parameters":
                STATE["parameters"] = json.loads(data["parameters"])
                await notify_parameters()
            elif data["type"] == "set_task":
                STATE["task"] = data["task"]
                await notify_task()
                await notify_parameters()
            elif data["type"] == "get_parameter":
                await notify_specific_parameter(data["name"])
    except Exception:
        print("Websocket connection closed.")
    finally:
        await unregister(websocket)

start_server = websockets.serve(serve_parameters, WEBSOCKET_IP, WEBSOCKET_PORT)
loop = asyncio.get_event_loop()
loop.run_until_complete(start_server)
loop.run_forever()


# createmasks stage

import argparse
from functools import partial
from pathlib import Path
from typing import Iterable, Union

import psutil
from pygeos.set_operations import union

import geopandas as gpd
import numpy as np
import pandas as pd
import rioxarray
import xarray as xr
from shapely.geometry import Polygon
from tqdm.contrib.concurrent import process_map

def make_poly(coords: pd.Series) -> Polygon:
    """Create Shapely polygon (a tile boundary) from x1,y1 and x2,y2 coordinates"""
    xs = [coords[v] for v in "x1,x1,x2,x2,x1".split(",")]
    ys = [coords[v] for v in "y1,y2,y2,y1,y1".split(",")]
    return Polygon(zip(xs, ys))

def _identify_empty(tile: Union[Path, str]) -> bool:
    """Helper func for exclude_nodata_tiles"""

    with xr.open_rasterio(tile).sel(band=1) as t:
        # original check
        # status = True if t.max().values - t.min().values > 0 else False
        # check 2 (edge tiles with all white/ black are also detected)
        return False if np.isin(t, [0, 255]).all() else True

def exclude_nodata_tiles(
    path: Iterable[Union[Path, str]],
    tiles_df: gpd.GeoDataFrame,
    workers: int,
) -> gpd.GeoDataFrame:
    """Identify tiles that only contain NoData (in parallel)"""

    print(f"WORKERS: {workers}")

    tile_names = sorted([Path(p) if isinstance(p, str) else p for p in path])

    results = process_map(_identify_empty, tile_names, max_workers=workers, chunksize=1)

    valid_d = dict([(t.name, r) for t, r in zip(tile_names, results)])

    tiles_df["status"] = tiles_df.filename.map(valid_d)
    # limit tiles to those with actual data (and delete status column afterwards)
    return tiles_df[tiles_df.status == 1].drop("status", axis=1)

def create_tile_grid_gdf(path: Union[Path, str], crs: str) -> gpd.GeoDataFrame:
    """Convert gdal_tile split info file into geopandas dataframe"""

    tiles_df = pd.read_csv(path, sep=";", header=None)
    tiles_df.columns = ["filename", "x1", "x2", "y1", "y2"]
    tiles_df["geometry"] = tiles_df.apply(make_poly, axis=1)
    tiles_df = tiles_df.drop(["x1", "x2", "y1", "y2"], axis=1)
    tiles_gpd = gpd.GeoDataFrame(tiles_df, crs=crs, geometry=tiles_df.geometry)
    return tiles_gpd

def split_groundtruth_data_by_tiles(
    groundtruth: gpd.GeoDataFrame, tiles_df: gpd.GeoDataFrame
) -> gpd.GeoDataFrame:
    """Split the oberserved dead tree areas into tile segments for faster downstream processing"""

    union_gpd = gpd.overlay(tiles_df, groundtruth, how="intersection")

    train_files = list(sorted(union_gpd.filename.value_counts().keys()))

    tiles_with_groundtruth = tiles_df[tiles_df.filename.isin(train_files)]
    return tiles_with_groundtruth, union_gpd  # union_gpd[union_gpd.id == 2]

def _mask_tile(
    tile_filename: str,
    *,
    groundtruth_df: gpd.GeoDataFrame,
    crs: str,
    inpath: Path,
    outpath: Path,
    simple: bool = False,
) -> float:

    image_tile_path = inpath / tile_filename
    mask_tile_path = outpath / tile_filename

    with rioxarray.open_rasterio(
        image_tile_path, chunks={"band": 4, "x": 256, "y": 256}
    ) as tile:
        mask_orig = xr.ones_like(tile.load().sel(band=1, drop=True), dtype="uint8")
        mask_orig.rio.set_crs(crs)
        selection = groundtruth_df.loc[groundtruth_df.filename == tile_filename]

        if simple:
            # just use the geometry for clipping (single-class)
            mask = mask_orig.rio.clip(
                selection.geometry,
                crs,
                drop=False,
                invert=False,
                all_touched=True,
                from_disk=True,
            )

        else:
            # use type col from shapefile to create (multi-)classification masks

            classes = [0, 1, 2]  # 0: non-class, 1: coniferous, 2: broadleaf

            selection.loc[:, "type"] = pd.to_numeric(selection["type"])
            masks = [mask_orig * 0]
            for c in classes[1:]:
                gdf = selection.loc[selection["type"] == c, :]

                if len(gdf) > 0:
                    mask = mask_orig.rio.clip(
                        gdf.geometry,
                        crs,
                        drop=False,
                        invert=False,
                        all_touched=True,
                        from_disk=True,
                    )
                else:
                    mask = mask_orig * 0
                masks.append(mask)
            mask = xr.concat(masks, pd.Index(classes, name="classes")).argmax(
                dim="classes"
            )

        mask.astype("uint8").rio.to_raster(mask_tile_path, tiled=True)
        mask_sum = np.count_nonzero(mask.values)  # just for checks
    return mask_sum

def create_tile_mask_geotiffs(
    tiles_df_train: gpd.GeoDataFrame, workers: int, **kwargs
) -> None:
    """Create binary mask geotiffs"""
    process_map(
        partial(_mask_tile, **kwargs),
        tiles_df_train.filename.values,
        max_workers=workers,
        chunksize=1,
    )

def create_masks(
    indir: Path,
    outdir: Path,
    shpfile: Path,
    workers: int,
    simple: bool,
) -> None:
    """
    Stage 1: produce masks for training tiles
    """

    # load domain shape files and use its crs for the entire script
    groundtruth = gpd.read_file(shpfile).explode()
    if "Type" in list(groundtruth.columns.values):
        groundtruth = groundtruth.rename(columns={"Type": "type"})

    crs = groundtruth.crs  # reference crs

    tiles_df = create_tile_grid_gdf(indir / "locations.csv", crs)
    tiles_df = exclude_nodata_tiles(
        sorted(indir.glob("*.tif")),
        tiles_df,
        workers,
    )
    print(f"len2: {len(tiles_df)}")
    tiles_df.to_file("locations.shp")

    tiles_df_train, groundtruth_df = split_groundtruth_data_by_tiles(
        groundtruth, tiles_df
    )

    create_tile_mask_geotiffs(
        tiles_df_train,
        workers,
        groundtruth_df=groundtruth_df,
        crs=crs,
        inpath=indir,
        outpath=outdir,
        simple=simple,
    )

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("indir", type=Path)
    parser.add_argument("outdir", type=Path)
    parser.add_argument("shpfile", type=Path)

    num_cores = psutil.cpu_count(logical=False)

    parser.add_argument(
        "--workers",
        dest="workers",
        type=int,
        default=num_cores,
        help="number of workers for parallel execution [def: %(default)s]",
    )

    parser.add_argument(
        "--simple",
        dest="simple",
        default=False,
        action="store_true",
        help="use just the geometry of the shapefile (no classes)",
    )

    args = parser.parse_args()

    Path(args.outdir).mkdir(parents=True, exist_ok=True)

    create_masks(
        args.indir,
        args.outdir,
        args.shpfile,
        args.workers,
        args.simple,
    )

if __name__ == "__main__":
    main()


from PyQt5 import QtCore, QtGui, QtWidgets
from module import Ui_module
import glob
from pathlib import Path
import os

__copyright__ = "Copyright 2019, RISE Research Institutes of Sweden"
__author__ = "Naveed Anwar Bhatti and Martina Brachmann"

class mainmodule(QtWidgets.QDialog):
    def __init__(self, parent=None):
        QtWidgets.QDialog.__init__(self, parent)
        self.parent = parent
        self.ui = Ui_module()
        self.ui.setupUi(self)
        self.selected_module = ""

        modules = glob.glob(os.path.join("Modules", "*.py"))  # Add all python files in Module directory as items

        for module in modules:
            item = QtWidgets.QListWidgetItem(module)  # create an item with a caption
            self.ui.listWidget.addItem(item)
            # item.setCheckable(True)  # add a checkbox to it
            # model.appendRow(item)  # Add the item to the model

        # self.listWidget.setModel(model)  # Apply the model to the list view
        self.ui.listWidget.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
        self.ui.saveModules.clicked.connect(self.return_modules)

    def return_modules(self):
        items = self.ui.listWidget.selectedItems()
        x = []
        for i in range(len(items)):
            x.append(str(self.ui.listWidget.selectedItems()[i].text()))
        if len(items)>0:
            if "/" in x[0]:
               self.selected_module = str(x[0]).split("/")[1].split(".")[0]
               print(x[0])
            else:
                self.selected_module = str(x[0]).split("\\")[1].split(".")[0]
                print(x[0])
        self.parent.selected_module=self.selected_module
        self.close()


from pymatgen.analysis.adsorption import AdsorbateSiteFinder
from pymatgen.core.structure import Structure
from pymatgen.core.surface import Slab, SlabGenerator
import numpy as np

# struct = Structure.from_file("POSCAR")
# asf = AdsorbateSiteFinder(struct)

def slab_from_structure(miller, structure):
    return Slab(lattice=structure.lattice,
                species=structure.species_and_occu,
                coords=structure.frac_coords,
                miller_index=miller,
                oriented_unit_cell=structure,
                shift=0,
                scale_factor=np.eye(3, dtype=np.int),
                site_properties=structure.site_properties)

from pymatgen.ext.matproj import MPRester

with MPRester() as m:
    struct = m.get_structure_by_material_id('mp-2', conventional_unit_cell=True)

miller_index = (1, 1, 1)
sg = SlabGenerator(initial_structure=struct,
                   miller_index=miller_index,
                   min_slab_size=10,
                   min_vacuum_size=10,
                   max_normal_search=max([abs(i) for i in miller_index]),
                   primitive=True,
                   lll_reduce=True,
                   in_unit_planes=True,
                   center_slab=True)

slab = sg.get_slabs()[0]
slab.make_supercell([2, 2, 1])

# struct = Structure.from_file('POSCAR_Pd_79.vasp')
# slab = slab_from_structure((1, 1, 1), struct)
asf = AdsorbateSiteFinder(slab)
sites = asf.find_adsorption_sites(symm_reduce=False)
from pymatgen.core.structure import Molecule
site_coords = list(sites.values())
site_coords = [a for b in site_coords for a in b]
mol = Molecule(['H'], [[0, 0, 0]])
# for site in site_coords:
#     asf = AdsorbateSiteFinder(slab)
#     slab = asf.add_adsorbate(molecule=mol, ads_coord=site)
# slab.to('poscar', 'pd_slab.vasp')


from django.conf.urls import url
from . import views

# mysite/urls.py 에서 url(r'', include('blog.urls')) 했어서
# Django will redirect everything that comes into htp://127.0.0.1:8000/
# to blog.urls and look for further instructions there.

urlpatterns = [
    url(r'^$', views.home, name='home'),
    url(r'^category/([0-9]+)/$', views.category, name='category'),
    url(r'^posts/$', views.post_list, name='post_list'),
    url(r'^posts/([0-9]+)/$', views.post_view, name='post_view'),
    url(r'^write/$', views.write_post, name='write_post'),
]


import csv
import datetime
import os
import re

import titlecase
from beancount.core import amount
from beancount.core import data
from beancount.core import flags
from beancount.core import number as beancount_number
from beancount.ingest import importer

_COLUMN_DATE = 'Posting Date'
_COLUMN_PAYEE = 'Description'
_COLUMN_AMOUNT = 'Amount'

_FILENAME_PATTERN = re.compile(r'Chase(\d{4})_Activity_[\d_]{8}.*\.CSV',
                               re.IGNORECASE)

class CheckingImporter(importer.ImporterProtocol):

    def __init__(self,
                 account,
                 lastfour=None,
                 currency='USD',
                 account_patterns=None):
        self._account = account
        self._last_four_account_digits = lastfour
        self._currency = currency
        self._account_patterns = []
        if account_patterns:
            for pattern, account_name in account_patterns:
                self._account_patterns.append(
                    (re.compile(pattern, flags=re.IGNORECASE), account_name))

    def _parse_amount(self, amount_raw):
        return amount.Amount(beancount_number.D(amount_raw), self._currency)

    def file_date(self, file):
        return max(map(lambda x: x.date, self.extract(file)))

    def file_account(self, _):
        return self._account

    def identify(self, file):
        match = _FILENAME_PATTERN.match(os.path.basename(file.name))
        if not match:
            return False
        return self._last_four_account_digits == match.group(1)

    def extract(self, f):
        transactions = []

        with open(f.name, encoding='utf-8') as csv_file:
            for index, row in enumerate(csv.DictReader(csv_file)):
                metadata = data.new_metadata(f.name, index)
                transaction = self._extract_transaction_from_row(row, metadata)
                if not transaction:
                    continue
                transactions.append(transaction)

        return transactions

    def _extract_transaction_from_row(self, row, metadata):
        transaction_date = datetime.datetime.strptime(row[_COLUMN_DATE],
                                                      '%m/%d/%Y').date()
        payee, transaction_description = _parse_description(row[_COLUMN_PAYEE])
        if payee:
            payee = titlecase.titlecase(payee)
        else:
            raise ValueError(f'failed to parse {row[_COLUMN_PAYEE]}')
        if transaction_description:
            narration = titlecase.titlecase(transaction_description)
        if row[_COLUMN_AMOUNT]:
            transaction_amount = self._parse_amount(row[_COLUMN_AMOUNT])
        else:
            return None  # 0 dollar transaction

        if transaction_amount == amount.Amount(beancount_number.D(0),
                                               self._currency):
            return None

        postings = [
            data.Posting(account=self._account,
                         units=transaction_amount,
                         cost=None,
                         price=None,
                         flag=None,
                         meta=None)
        ]
        for pattern, account_name in self._account_patterns:
            if pattern.search(payee) or pattern.search(
                    narration) or pattern.search(payee + narration):
                postings.append(
                    data.Posting(account=account_name,
                                 units=-transaction_amount,
                                 cost=None,
                                 price=None,
                                 flag=None,
                                 meta=None))
                break

        # For some reason, pylint thinks data.Transactions is not callable.
        # pylint: disable=not-callable
        return data.Transaction(
            meta=metadata,
            date=transaction_date,
            flag=flags.FLAG_OKAY,
            payee=payee,
            narration=narration,
            tags=data.EMPTY_SET,
            links=data.EMPTY_SET,
            postings=postings,
        )

_DESCRIPTION_PATTERN = re.compile(
    # pylint: disable=line-too-long
    r'ORIG CO NAME:(.+?)\s*ORIG ID:.*DESC DATE:.*CO ENTRY DESCR:(.+?)\s*SEC:.*TRACE#:.*EED:.*',
    re.IGNORECASE)

_OUTBOUND_TRANSFER_PATTERN = re.compile(
    r'Online Transfer \d+ to (.+?)\s*transaction #', re.IGNORECASE)

_INBOUND_TRANSFER_PATTERN = re.compile(
    r'Online Transfer \d+ from (.+?)\s*transaction #', re.IGNORECASE)

def _parse_description(description):
    match = _DESCRIPTION_PATTERN.search(description)
    if match:
        return match.group(1), match.group(2)
    match = _OUTBOUND_TRANSFER_PATTERN.search(description)
    if match:
        return match.group(1), description
    match = _INBOUND_TRANSFER_PATTERN.search(description)
    if match:
        return match.group(1), description
    return None, None


from modules.menu import Menu
from modules.pizza import Pizza

menu = Menu()

def saveMenu(file_path, menu):
    ##sauvegarder le menu dans le fichier file_path
    fichier = open(file_path, 'w')
    fichier.write("menu :\n")
    fichier.write(str(menu))
    fichier.close()

def getMenuFromFile(file_path):
    ##recuperer le menu du fichier file_path
    print("getMenu")

def helpAdmin():
    print(""" Help :
				- addPizza pizzaName (ajouter une pizza au menu)
				- addIngredient pizzaName ingredient (ajouter un ingrédient à une pizza)
				- addPrix pizzaName prix (ajouter un prix à une pizza)
				- addQte pizzaName qte (ajouter une qte à une pizza)
				- exit (sortir du menu admin)
		""")

def addPizza(option):
    global menu
    _, pizzaName = option.split(" ")
    menu.addPizza(Pizza(pizzaName))

def addIngredient(option):
    global menu
    _, pizzaname, ingredient = option.split(" ")
    menu.getPizza(pizzaname).addIngredient(ingredient)

def addPrix(option):
    global menu
    _, pizzaname, prix = option.split(" ")
    menu.getPizza(pizzaname).fixerPrix(int(prix))

def addQte(option):
    global menu
    _, pizzaname, quantite = option.split(" ")
    menu.getPizza(pizzaname).addQuantite(int(quantite))

def helpClient():
    print(""" Help :
				- menu (afficher le menu)
				- manger pizzaName (manger une pizza)
				- exit (sortir du restaurant)
			""")

def displayMenu():
    global menu
    print(menu)

def manger(action):
    global menu
    _, pizza = action.split(" ")
    if menu.getPizza(pizza).getQuantite()>0:
        print("bonap")
        menu.getPizza(pizza).addQuantite(-1)
    else:
        print("pizza non disponible")

while True:
    login = input("login : ")
    if login == "exit":
        break
    if login == "admin":
        while True:
            option = input("choisir une option : ")
            if option == "exit":
                break
            if option == "help":
                helpAdmin()
            # Traiter les commandes admin
            if option.startswith("addPizza"):
                addPizza(option)
            if option.startswith("addIngredient"):
                addIngredient(option)
            if option.startswith("addPrix"):
                addPrix(option)
            if option.startswith("addQte"):
                addQte(option)
            if option.startswith("saveMenu"):
                saveMenu("./menu.txt", menu)

    if login == "client":
        while True:
            action = input("que voulez vous faire : ")
            if action == "exit":
                print("Au revoir")
                break
            if action == "help":
                helpClient()
            if action == "menu":
                displayMenu()
            if action.startswith("manger"):
                manger(action)

        # Traiter les commandes client


from typing import Optional

import tokenizers

# toks from https://github.com/facebookresearch/esm/blob/main/esm/constants.py#L7
# prepend/append toks from https://github.com/facebookresearch/esm/blob/main/esm/data.py#L153
PROTEINSEQ_TOKS = {
    "toks": [
        "L",
        "A",
        "G",
        "V",
        "S",
        "E",
        "R",
        "T",
        "I",
        "D",
        "P",
        "K",
        "Q",
        "N",
        "F",
        "Y",
        "M",
        "H",
        "W",
        "C",
        "X",
        "B",
        "U",
        "Z",
        "O",
        ".",
        "-",
    ],
    "prepend_toks": ["<cls>", "<pad>", "<eos>", "<unk>"],
    "append_toks": ["<null_1>", "<mask>"],
}

def protein_tokenizer(
    pad_to_multiple_of: Optional[int] = None, max_length: Optional[int] = 1024
) -> tokenizers.Tokenizer:
    """Returns the default protein tokenizer.

    Args:
        pad_to_multiple_of (Optional[int]): If specified, pads all sequences to
            nearest (higher) multiple of value. Defaults to None.
        max_length (Optional[int]): Maximum length to truncate to. Defaults to 1024.

    Returns:
        tokenizers.Tokenizer: HuggingFace Tokenizer to tokenize proteins.
    """

    # Just repurpose the WordLevel tokenizer, since the vocabulary is fully known.
    tokenizer = tokenizers.Tokenizer(
        tokenizers.models.WordLevel(
            {
                key: idx
                for (idx, key) in enumerate(
                    PROTEINSEQ_TOKS["prepend_toks"]
                    + PROTEINSEQ_TOKS["toks"]
                    + PROTEINSEQ_TOKS["append_toks"]
                )
            },
            unk_token="<unk>",
        )
    )

    # Use regex to strip out the capital letters + <special> tokens.
    tokenizer.pre_tokenizer = tokenizers.pre_tokenizers.Split(
        tokenizers.Regex("[A-Z]|[<][a-z]*[>]"), behavior="removed", invert=True
    )

    # Specify the special tokens; makes decoding + printing neater.
    tokenizer.add_special_tokens(
        PROTEINSEQ_TOKS["prepend_toks"] + PROTEINSEQ_TOKS["append_toks"]
    )

    # Add template, to add <cls> to start and <eos> to end of all sequences.
    tokenizer.post_processor = tokenizers.processors.TemplateProcessing(
        single="<cls> $A <eos>",
        pair=None,
        special_tokens=[("<cls>", 0), ("<eos>", 2)],
    )

    # If padding is requested, enable padding.
    if pad_to_multiple_of:
        tokenizer.enable_padding(
            pad_id=1, pad_token="<pad>", pad_to_multiple_of=pad_to_multiple_of
        )

    # Enable truncation to specified max length.
    tokenizer.enable_truncation(max_length=max_length)

    return tokenizer


from data import windows, signal, WINDOW_SIZE, fs
from matplotlib import pyplot as plt
from matplotlib import transforms
from matplotlib.gridspec import GridSpec
from numpy import fft
from waterfall_plot import plot_waterfall
import numpy as np

spectral_content = fft.rfft(windows, axis=0)
normed_spectral_content = np.abs(spectral_content)
sample_size = (WINDOW_SIZE // 2) + ((WINDOW_SIZE + 1) % 2)
freq_scaling = np.linspace(0, fs / 2, normed_spectral_content.shape[0])
freq_scaling = freq_scaling.reshape((sample_size, 1))

pl = np.log(freq_scaling * normed_spectral_content + 0.0001)

# Summarize data
# Our buckets will be roughly:
# 0 - 100 Hz
# 101 - 2 Khz
# 2Khz - 20Khz
div1 = int(sample_size * (100 / (fs * 2)))
div2 = int(sample_size * (2000 / (fs * 2)))

SEG_START = 100
SEG_END = 300

def sumarize(start, stop):
    # Select the right part of the array
    tr = np.sum(pl[start:stop,SEG_START:SEG_END], axis=0)
    # Compute a moving average (window size = 16)
    # to smooth out measurements
    MA_SIZE = 3
    tr = np.cumsum(tr)
    tr[MA_SIZE:] = tr[MA_SIZE:] - tr[:-MA_SIZE]
    tr = tr[MA_SIZE-1:] / MA_SIZE
    # Normalize the moving average
    mi = np.min(tr)
    ma = np.max(tr)
    # Normalize TR
    tr = (tr - mi) / (ma - mi)
    return tr

sum0 = sumarize(0, div1)
sum1 = sumarize(div1, div2)
sum2 = sumarize(div2, -1)

fig = plt.figure(figsize=(8,16))

subplots = fig.subplots(ncols=1, nrows=4, sharex='col')

p0 = subplots[0].plot(sum0)
subplots[0].set_title('lows')
subplots[0].axhline(y=0.75)
p1 = subplots[1].plot(sum1)
subplots[1].set_title('mids')
subplots[1].axhline(y=0.8)
p2 = subplots[2].plot(sum2)
subplots[2].set_title('his')
subplots[2].axhline(y=0.6)

subplots[3].imshow(pl[:,SEG_START:SEG_END], aspect='auto')

plt.savefig('img/beats.png', bbox_inches='tight', dpi=fig.dpi)


from datetime import datetime
import sqlite3 as sql
con = sql.connect("bank_data.db")
cur = con.cursor()
import traceback

global account_no
global cust_id

def change_addr():
    address = input("Enter address : ")
    cur.execute('UPDATE customer SET address = (?) WHERE cust_id = (?);',(address, cust_id))
    print("Address successfully changed\n")
    con.commit()

def open_account():
    try:
        li=["Savings Account","Current Account","Fixed Deposit Account"]
        for n,val in enumerate(li):
            print(n+1,val)
        amount = int(input("Enter Amount to be deposited in the FD : "))
        if amount > 0 and not amount%100:
            pass
        else:
            print("Enter amount in multiples of 1000")
            exit()
        term = int(input("Enter the term for the FD(in terms of years): "))
        if (not term > 1) :
            print("Enter term period greater than 12 months")
            exit()
        cur.execute('INSERT INTO FD_table(account_no,amount,term) VALUES(?,?,?);',(account_no,amount,term))
        con.commit()
        value = cur.execute("SELECT fd_no FROM FD_table order by fd_no desc limit 1;")
        for row in value:
            fd_no = row[0]
            print("Your fixed deposit number is :", fd_no)

        value=cur.execute("SELECT * FROM FD_table WHERE account_no=(?);",(account_no,))
        for row in value:
            print("FD number: ",row[0])
            print("Customer ID: ",row[1])
            print("Amount: ", row[2])
            print("Term: ", row[3])

    except Exception as e:
        print(e)
        traceback.print_exc()

def deposit():
    amount = int(input("Enter Amount to be deposited : "))
    value = cur.execute('SELECT balance FROM accounts WHERE account_no = (?);', (account_no,))
    for row in value:
        bal=row[0]

    bal = bal + amount
    print("New balance is : ",bal)
    cur.execute('UPDATE accounts SET balance = (?) WHERE account_no = (?);', (bal, account_no))
    con.commit()
    cur.execute('INSERT INTO logTable(cust_id,account_no,deposit,withdraw,transfer,balance) VALUES (?,?,?,?,?,?);', (account_no,account_no,amount,'NULL','NULL', bal))
    con.commit()
    value = cur.execute('SELECT transaction_id FROM logTable ORDER BY transaction_id DESC limit 1;')
    for row in value:
        print("Your transaction ID is : ", row[0])

def withdraw():
    amount = int(input("Enter Amount to be withdrawl : "))
    value = cur.execute('SELECT balance FROM accounts WHERE account_no = (?);', (account_no,))
    for row in value:
        bal = row[0]

    if (bal - amount < 0):
        print('Not enough balance')
        exit()
    else:
        bal = bal-amount
        print("New balance is : ",bal)
        cur.execute('UPDATE accounts SET balance = (?) WHERE account_no = (?);', (bal, account_no))
        cur.execute('INSERT INTO logTable(cust_id,account_no,deposit,withdraw,transfer,balance) VALUES (?,?,?,?,?,?);',(account_no, account_no,"NULL", amount,"NULL", bal))
        con.commit()
        value = cur.execute('SELECT transaction_id FROM logTable ORDER BY transaction_id DESC limit 1;')
        for row in value:
            print("Your transaction ID is : ", row[0])

def get_statement():
    value = cur.execute('SELECT account_no, transaction_id, deposit, withdraw, transfer, balance FROM logTable WHERE account_no = (?);',(account_no,))
    for row in value:
        print("Account_no",row[0])
        print("Transaction_id",row[1])
        print("Deposit", row[2])
        print("Withdraw", row[3])
        print("Transfer", row[4])
        print("Balance", row[5])

def transfer_money():
    acc2 = int(input('Enter Account No. to transfer money : '))
    a = cur.execute('SELECT account_no FROM accounts WHERE account_no = (?);', (acc2,))
    value = cur.execute('SELECT balance FROM accounts WHERE account_no = (?);', (acc2,))
    for row in value:
        bal = row[0]
        print(bal)
    if(a):
        amount = int(input('Enter amount to Transfer : '))
        value = cur.execute('SELECT balance FROM accounts WHERE account_no = (?);', (account_no,))
        for row in value:
            my = row[0]

        if ((my - amount) < 0):
            print('Not enough balance')
        else:
            my = my - amount
            bal = bal + amount
            cur.execute('UPDATE accounts SET balance = (?) WHERE account_no = (?);', (my, account_no))
            cur.execute('INSERT INTO logTable(cust_id,account_no,deposit,withdraw,transfer,balance) VALUES (?,?,?,?,?,?);',(account_no,account_no,"NULL","NULL",amount,my))
            cur.execute('UPDATE accounts SET balance = (?) WHERE account_no = (?);', (bal, acc2))
            print("Transfer Complete")
            con.commit()
            value = cur.execute('SELECT transaction_id FROM logTable ORDER BY transaction_id DESC limit:1;')
            for row in value:
                print("Your trabsaction ID is : ", row[0])
    else:
        exit()

def close_account():
    cur.execute('UPDATE accounts SET account_status=(?), account_closing_date =CURRENT_TIMESTAMP WHERE account_no = (?);', ("closed", account_no,))
    con.commit()
    print("Account closed")

def get_loan():
    amount = int(input("Enter Amount of loan to be issued : "))
    if(amount>0 and not amount%100):
        pass
    else:
        print("Enter amount in multiples of 1000")
        exit()
    term = int(input("Enter the term of the loan(in terms of years): "))
    cur.execute('INSERT INTO loan_table(account_no,amount,term) VALUES(?,?,?);',(account_no,amount,term))
    con.commit()
    value = cur.execute("SELECT loan_id FROM loan_table order by loan_id desc limit 1;")
    for row in value:
        loan_id = row[0]
        print("Your Loan ID is :", loan_id)
    value = cur.execute("SELECT * FROM loan_table WHERE account_no=(?);",(account_no,))
    for row in value:
        print("Loan number: ",row[0])
        print("Customer ID: ",row[1])
        print("Amount: ", row[2])
        print("Term: ", row[3])

def logout():
    print("logging out")
    exit()

def profile_func(acc):
    global account_no
    account_no = acc
    global cust_id
    cust_id = acc
    li = ["Address Change","Open New Acccount", "Money Deposit","Money Withdrawal", "Transfer Money","Print Statement","Account Closure","Avail Loan","Customer Logout"]
    for n,val in enumerate(li):
        print(n+1,val)
    choice_dict={
                    1:change_addr,
                    2:open_account,
                    3:deposit,
                    4:withdraw,
                    5:transfer_money,
                    6:get_statement,
                    7:close_account,
                    8:get_loan,
                    9:logout
                }
    choice = int(input("Enter your choice : "))
    choice_dict[choice]()


import argparse
import sys

from absl import app

from google.protobuf import message
from google.protobuf import text_format
from tensorflow.core.framework import graph_pb2
from tensorflow.core.protobuf import config_pb2
from tensorflow.core.protobuf import meta_graph_pb2
from tensorflow.core.protobuf import saved_model_pb2
from tensorflow.python.framework import importer
from tensorflow.python.framework import ops
from tensorflow.python.grappler import cost_analyzer
from tensorflow.python.grappler import tf_optimizer
from tensorflow.python.platform import gfile
from tensorflow.python.training import saver

def get_metagraph():
  """Constructs and returns a MetaGraphDef from the input file."""
  with gfile.GFile(FLAGS.input) as input_file:
    input_data = input_file.read()
    try:
      saved_model = saved_model_pb2.SavedModel()
      text_format.Merge(input_data, saved_model)
      meta_graph = saved_model.meta_graphs[0]
    except text_format.ParseError:
      try:
        saved_model.ParseFromString(input_data)
        meta_graph = saved_model.meta_graphs[0]
      except message.DecodeError:
        try:
          meta_graph = meta_graph_pb2.MetaGraphDef()
          text_format.Merge(input_data, meta_graph)
        except text_format.ParseError:
          try:
            meta_graph.ParseFromString(input_data)
          except message.DecodeError:
            try:
              graph_def = graph_pb2.GraphDef()
              text_format.Merge(input_data, graph_def)
            except text_format.ParseError:
              try:
                graph_def.ParseFromString(input_data)
              except message.DecodeError:
                raise ValueError(f"Invalid input file: {FLAGS.input}.")
            importer.import_graph_def(graph_def, name="")
            graph = ops.get_default_graph()
            meta_graph = saver.export_meta_graph(
                graph_def=graph.as_graph_def(), graph=graph)
  if FLAGS.fetch is not None:
    fetch_collection = meta_graph_pb2.CollectionDef()
    for fetch in FLAGS.fetch.split(","):
      fetch_collection.node_list.value.append(fetch)
    meta_graph.collection_def["train_op"].CopyFrom(fetch_collection)
  return meta_graph

def main(_):
  metagraph = get_metagraph()
  config = config_pb2.ConfigProto()
  if FLAGS.rewriter_config is not None:
    text_format.Merge(FLAGS.rewriter_config,
                      config.graph_options.rewrite_options)
  optimized_graph = tf_optimizer.OptimizeGraph(config, metagraph)
  metagraph.graph_def.CopyFrom(optimized_graph)

  report = cost_analyzer.GenerateCostReport(metagraph, FLAGS.per_node_report,
                                            FLAGS.verbose)
  print(report)
  if FLAGS.memory_report:
    report = cost_analyzer.GenerateMemoryReport(metagraph)
    print(report)

if __name__ == "__main__":
  parser = argparse.ArgumentParser()
  parser.add_argument(
      "--input",
      type=str,
      default=None,
      help="Input file path. Accept SavedModel, MetaGraphDef, and GraphDef in "
      "either binary or text format.")
  parser.add_argument(
      "--fetch",
      type=str,
      default=None,
      help="The names of the fetch node delimited by comma.")
  parser.add_argument(
      "--rewriter_config",
      type=str,
      default=None,
      help="Configuration for the grappler optimizers, described as a "
      "RewriterConfig protocol buffer. Usage example 1: "
      "--rewriter_config='optimize_tensor_layout: true "
      "disable_model_pruning: true'. Usage example 2: "
      "--rewriter_config='optimizers: \"constfold\" optimizers: \"layout\"'")
  parser.add_argument(
      "--per_node_report",
      action="store_true",
      help="Generate per-node report. By default the report contains stats "
      "aggregated on a per op type basis, per_node_report adds results "
      "for each individual node to the report.")
  parser.add_argument(
      "--memory_report",
      action="store_true",
      help="Generate memory usage report.")
  parser.add_argument(
      "--verbose",
      action="store_true",
      help="Generate verbose reports. By default, succinct reports are used.")
  FLAGS, unparsed = parser.parse_known_args()
  app.run(main=main, argv=[sys.argv[0]] + unparsed)


import platform
import json
from flask import Flask
from flask import render_template
from cpuinfo import get_cpu_info
import os
import socket

class Machine:
    # constructor where i get all system information
    def __init__(self):
        cpu_data = get_cpu_info()

        self.type = platform.machine()
        self.name = platform.node()
        self.system = platform.system()
        self.release = platform.release()
        self.cpu_version = cpu_data["cpuinfo_version_string"]
        self.cpu_bits = cpu_data["bits"]
        self.cpu_frequence = cpu_data["hz_advertised_friendly"]
        self.cpu_brand = cpu_data["brand_raw"]

    # return the instance as a dictonary
    def getDico(self):
        return {
            'Systeme': self.system,
            'Release': self.release,
            'Type': self.type,
            'name': self.name,
            'Version du CPU': self.cpu_version,
            'Bits du CPU': self.cpu_bits,
            'Marque du CPU': self.cpu_brand,
            'Frequence du CPU': self.cpu_frequence
        }

    # start flask
    def start(self):
        app = Flask(__name__)

        @app.route("/")
        def MyMachine():
            return render_template("index.html", maMachine = self.getDico())

        app.run()

tmp = Machine()
tmp.start()


import sys
import traceback
import logging

from surprise.model_selection import cross_validate
from auto_surprise.algorithms.base import AlgorithmBase
from auto_surprise.constants import (
    DEFAULT_TARGET_METRIC,
    CV_N_JOBS,
    DEFAULT_HPO_ALGO,
)
from auto_surprise.context.limits import TimeoutException
from auto_surprise.context.result_logging_manager import ResultLoggingManager
import auto_surprise.context.limits as limits

class Trainer(object):
    def __init__(
        self,
        tmp_dir,
        algo=None,
        data=None,
        target_metric=DEFAULT_TARGET_METRIC,
        hpo_algo=DEFAULT_HPO_ALGO,
        verbose=False,
        random_state=None,
        baseline_loss=None,
    ):
        """
        Initialize new trainer
        """
        self.__logger = logging.getLogger(__name__)
        self.verbose = verbose
        self.tmp_dir = tmp_dir
        self.algo_name = algo

        # Dynamically instantiate algorithm
        self.algo_base = AlgorithmBase(
            data=data,
            algo_name=self.algo_name,
            metric=target_metric,
            cv_n_jobs=CV_N_JOBS,
            hpo_algo=hpo_algo,
            verbose=verbose,
            random_state=random_state,
            baseline_loss=baseline_loss,
        )

    def start(self, max_evals):
        """
        Start with no limits
        """
        try:
            with ResultLoggingManager(self.tmp_dir, self.algo_name) as result_logger:
                self.algo_base.set_result_logger(result_logger)

                best, trials = self.algo_base.best_hyperparams(max_evals)

                best_trial = None
                # Sort best trial based on loss value
                if best:
                    best_trial = sorted(
                        trials.results, key=lambda x: x["loss"], reverse=False
                    )[0]
                else:
                    best_trial = trials

                return best, best_trial
        except Exception as e:
            self.__logger.error("Exception : ", e)
            self.__logger.error(traceback.format_exc())

            return False, False

    def start_with_limits(self, max_evals, time_limit, tasks):
        """
        Start the trainer with a time limit
        """
        with ResultLoggingManager(self.tmp_dir, self.algo_name) as result_logger:
            try:
                # Although hyperopt.fmin does have a timout parameter, it doesnt seem to work with multiprocessing
                with limits.run_with_enforced_limits(time_limit=time_limit):
                    self.algo_base.set_result_logger(result_logger)

                    _, best_trial = self.algo_base.best_hyperparams(max_evals)

                    if self.algo_base.trials.results:
                        best_trial = sorted(
                            self.algo_base.trials.results,
                            key=lambda x: x["loss"],
                            reverse=False,
                        )[0]

                    tasks[self.algo_name] = {
                        **best_trial,
                        "exception": False,
                        "trials": self.algo_base.trials,
                    }

            except TimeoutException:
                # Handle timeout when enforced cpu time limit is reached
                trials = self.algo_base.trials

                if trials.results:
                    best_trial = sorted(
                        trials.results, key=lambda x: x["loss"], reverse=False
                    )[0]
                    # A timeout exception is not considered an algorithm exception
                    tasks[self.algo_name] = {
                        **best_trial,
                        "exception": False,
                        "trials": trials,
                    }
                else:
                    # When no trials were completed before the job timed out
                    tasks[self.algo_name] = {
                        "loss": None,
                        "hyperparams": None,
                        "exception": False,
                        "trials": trials,
                    }

            except Exception:
                self.__logger.error("Exception for algo {0}".format(self.algo_name))
                self.__logger.error(traceback.format_exc())

                tasks[self.algo_name] = {
                    "loss": None,
                    "hyperparams": None,
                    "exception": True,
                    "trials": self.algo_base.trials,
                }


from typing import TypeAlias

from foundry import data_dir
from foundry.game.Data import Mario3Level
from smb3parse.util import hex_int

LevelAddress: TypeAlias = int
EnemyItemAddress: TypeAlias = int

ObjectData: TypeAlias = tuple[LevelAddress, bytearray]
EnemyItemData: TypeAlias = tuple[EnemyItemAddress, bytearray]
LevelByteData: TypeAlias = tuple[ObjectData, EnemyItemData]

EMPTY_OBJECT_DATA: ObjectData = (-1, bytearray())
EMPTY_ENEMY_DATA: EnemyItemData = (-1, bytearray())

def _load_level_offsets() -> tuple[list[Mario3Level], list[int]]:
    offsets = [Mario3Level(0, 0, 0, 0, 0, "Placeholder")]
    world_indexes = [0]

    with open(data_dir.joinpath("levels.dat"), "r") as level_data:
        for line_no, line in enumerate(level_data.readlines()):
            data = line.rstrip("\n").split(",")

            numbers = map(hex_int, data[0:5])
            level_name = data[5]

            (
                game_world,
                level_in_world,
                rom_level_offset,
                enemy_offset,
                real_obj_set,
            ) = numbers

            level = Mario3Level(
                game_world,
                level_in_world,
                rom_level_offset,
                enemy_offset,
                real_obj_set,
                level_name,
            )

            offsets.append(level)

            if level.game_world > 0 and level.level_in_world == 1:
                world_indexes.append(line_no)

    return offsets, world_indexes


import asyncio

import Time
from db import db
from loader import dp, bot
from aiogram import types
from aiogram.types import Message
from aiogram.dispatcher.filters import Command, Text
from aiogram.dispatcher import FSMContext

from misc.throttling import rate_limit
from states import States
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.dispatcher.filters.state import StatesGroup, State
import keyboards
import messages
import datetime
from Time import switchWeekday
import create_message

@rate_limit
@dp.message_handler(Command("start"))
async def start(message: types.Message):
    if db.user_exists(message.from_user.id):
        await message.answer(messages.WELCOME_MESSAGE, reply_markup=keyboards.start)
    else:
        await message.answer(messages.WELCOME_MESSAGE)
        await message.answer(messages.REGISTRATION_MESSAGE)
        States.Registration.set()

@rate_limit
@dp.message_handler(state=States.Registration)
async def registration(message: types.Message, state: FSMContext):
    group_number = message.text
    while True:

        if message.text in messages.groups:
            db.add_user(0, message.from_user.id, message.from_user.full_name, group_number)
            await message.answer(messages.end_registration, reply_markup=keyboards.start)
            break
        else:
            await message.answer(messages.group_number_fall)

    await state.update_data(answer1=group_number)

    await state.finish()

@rate_limit
@dp.message_handler(Text("О нас"))
async def about_us(message: types.Message):
    await message.answer(messages.ABOUT_US_MESSAGE)

@rate_limit
@dp.message_handler(Text("Функции"))
async def functions(message: types.Message):
    await message.answer(messages.FUNCTION_MESSAGE, reply_markup=keyboards.functions)

@rate_limit
@dp.message_handler(Text("Свое расписание"), state=None)
async def student_tt(message: types.Message):
    await message.answer(messages.what_tt, reply_markup=keyboards.what_tt)
    await States.Ques_What_tt.set()

@rate_limit
@dp.message_handler(state=States.Ques_What_tt)
async def student_ques_what_tt(message: types.Message, state: FSMContext):
    answer = message.text
    group_number = await db.get_user_group(message.from_user.id)
    await state.update_data(answer1=answer)

    if answer == "На завтра":
        await message.answer(messages.get_tt)
        await message.answer(db.get_tt_tomorrow(group_number,
                             switchWeekday(datetime.datetime.today().weekday() + 1)),
                             reply_markup=keyboards.functions)

    elif answer == "На сегодня":
        await message.answer(messages.get_tt)
        await message.answer(db.get_tt_today(group_number, switchWeekday(datetime.datetime.today().weekday())),
                             reply_markup=keyboards.functions)

    elif answer == "На неделю":
        await message.answer(messages.get_tt)
        await message.answer(db.get_tt_week(group_number),
                             reply_markup=keyboards.functions)

    await state.finish()

@rate_limit
@dp.message_handler(Text("Расписание звонков"))
async def get_tt_calls(message: types.Message):
    await message.answer(messages.TT_cals)
    await message.answer(Time.tt_Time())

@rate_limit
@dp.message_handler(Text("Расписание преподавателя"), state=None)
async def get_tt_Lec(message: types.Message):
    await message.answer(messages.SELECT_NAME_LECTURER, reply_markup=keyboards.back)
    await States.Get_Lecturer_tt.set()

@rate_limit
@dp.message_handler(Text("Числитель / Знаменатель"))
async def get_tipe_weel(message: types.Message):
    await message.answer(Time.get_ch_zn())

@rate_limit
@dp.message_handler(Text("Назад"))
async def back(message: types.Message):
    await message.answer(messages.FUNCTION_MESSAGE, reply_markup=keyboards.functions)

@rate_limit
@dp.message_handler(state=States.Confirm)
async def confirm(message: types.Message, state: FSMContext):
    if message.text == "Да":
        with await state.proxy() as data:
            db.change_user_group(message.from_user.id, data['group'])
            await message.answer(create_message.change_group_successful(data['group']))
        await state.finish()
    else:
        await message.answer("Окей, отменил", reply_markup=keyboards.functions)

@rate_limit
@dp.message_handler(Text, state=States.Get_Lecturer_tt)
async def get_what_Lec_tt(message: types.Message, state: FSMContext):
    if message.text.lower() not in messages.lecturers_lastnames:
        await message.answer(messages.WRONG_SELECT_NAME_LEC)
    else:
        await message.answer(db.get_tt_lecther(message), reply_markup=keyboards.functions)
        await state.finish()

@rate_limit
@dp.message_handler(Text, state=None)
async def change_group_number(message: types.Message, state: FSMContext):
    if message.text in messages.groups:

        await States.Confirm().set()
        async with state.proxy() as data:
            data['group'] = message.text

# @dp.message_handler(state=None)
# async def another(message: types.Message, state: FSMContext):