import argparse
import os
import logging
import sys
import itertools

import torch
from torch.utils.data import DataLoader, ConcatDataset
from torch.optim.lr_scheduler import CosineAnnealingLR, MultiStepLR

from vision.utils.misc import str2bool, Timer, freeze_net_layers, store_labels
from vision.ssd.ssd import MatchPrior
from vision.ssd.vgg_ssd import create_vgg_ssd
from vision.ssd.mobilenetv1_ssd import create_mobilenetv1_ssd
from vision.ssd.mobilenetv1_ssd_lite import create_mobilenetv1_ssd_lite
from vision.ssd.mobilenet_v2_ssd_lite import create_mobilenetv2_ssd_lite
from vision.ssd.mobilenetv3_ssd_lite import create_mobilenetv3_large_ssd_lite, create_mobilenetv3_small_ssd_lite
from vision.ssd.squeezenet_ssd_lite import create_squeezenet_ssd_lite
from vision.datasets.voc_dataset import VOCDataset
from vision.datasets.open_images import OpenImagesDataset,  OpenImagesDataset3
from vision.nn.multibox_loss import MultiboxLoss
from vision.ssd.config import vgg_ssd_config
from vision.ssd.config import mobilenetv1_ssd_config
from vision.ssd.config import squeezenet_ssd_config
from vision.ssd.data_preprocessing import TrainAugmentation, TestTransform

import torchvision.transforms.functional as FT
import torchvision.transforms as T
from thop import profile, clever_format
from flopth import flopth
import time
from collections import OrderedDict
import onnx
from onnx import shape_inference

parser = argparse.ArgumentParser(
    description='Single Shot MultiBox Detector Training With Pytorch')

parser.add_argument("--dataset_type", default="voc", type=str,
                    help='Specify dataset type. Currently support voc and open_images.')

parser.add_argument('--datasets', nargs='+', help='Dataset directory path')
parser.add_argument('--validation_dataset', help='Dataset directory path')
parser.add_argument('--balance_data', action='store_true',
                    help="Balance training data by down-sampling more frequent labels.")

parser.add_argument('--net', default="vgg16-ssd",
                    help="The network architecture, it can be mb1-ssd, mb1-lite-ssd, mb2-ssd-lite, mb3-large-ssd-lite, mb3-small-ssd-lite or vgg16-ssd.")
parser.add_argument('--freeze_base_net', action='store_true',
                    help="Freeze base net layers.")
parser.add_argument('--freeze_net', action='store_true',
                    help="Freeze all the layers except the prediction head.")

parser.add_argument('--mb2_width_mult', default=1.0, type=float,
                    help='Width Multiplifier for MobilenetV2')

# Params for SGD
parser.add_argument('--lr', '--learning-rate', default=1e-3, type=float,
                    help='initial learning rate')
parser.add_argument('--momentum', default=0.9, type=float,
                    help='Momentum value for optim')
parser.add_argument('--weight_decay', default=5e-4, type=float,
                    help='Weight decay for SGD')
parser.add_argument('--gamma', default=0.1, type=float,
                    help='Gamma update for SGD')
parser.add_argument('--base_net_lr', default=None, type=float,
                    help='initial learning rate for base net.')
parser.add_argument('--extra_layers_lr', default=None, type=float,
                    help='initial learning rate for the layers not in base net and prediction heads.')

# Params for loading pretrained basenet or checkpoints.
parser.add_argument('--base_net',
                    help='Pretrained base model')
parser.add_argument('--pretrained_ssd', help='Pre-trained base model')
parser.add_argument('--resume', default=None, type=str,
                    help='Checkpoint state_dict file to resume training from')

# Scheduler
parser.add_argument('--scheduler', default="multi-step", type=str,
                    help="Scheduler for SGD. It can one of multi-step and cosine")

# Params for Multi-step Scheduler
parser.add_argument('--milestones', default="80,100", type=str,
                    help="milestones for MultiStepLR")

# Params for Cosine Annealing
parser.add_argument('--t_max', default=120, type=float,
                    help='T_max value for Cosine Annealing Scheduler.')

# Train params
parser.add_argument('--batch_size', default=32, type=int,
                    help='Batch size for training')
parser.add_argument('--num_epochs', default=120, type=int,
                    help='the number epochs')
parser.add_argument('--num_workers', default=0, type=int,
                    help='Number of workers used in dataloading')
parser.add_argument('--validation_epochs', default=5, type=int,
                    help='the number epochs')
parser.add_argument('--debug_steps', default=100, type=int,
                    help='Set the debug log output frequency.')
parser.add_argument('--use_cuda', default=True, type=str2bool,
                    help='Use CUDA to train model')

parser.add_argument('--checkpoint_folder', default='models/',
                    help='Directory for saving checkpoint models')

logging.basicConfig(stream=sys.stdout, level=logging.INFO,
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
args = parser.parse_args()
DEVICE = torch.device("cuda:0" if torch.cuda.is_available() and args.use_cuda else "cpu")

if args.use_cuda and torch.cuda.is_available():
    torch.backends.cudnn.benchmark = True
    logging.info("Use Cuda.")

def build_model():

        class MySimpleNet(torch.nn.Module):
            def __init__(self):
                super().__init__()

                self.conv1 = torch.nn.Conv2d(3,12,3,1,1)
                self.bn1 = torch.nn.BatchNorm2d(12)
                self.act1 = torch.nn.ReLU()
                self.pool1 = torch.nn.MaxPool2d(2,2)
                self.conv2 = torch.nn.Conv2d(12,24,3,1,1)
                self.bn2 = torch.nn.BatchNorm2d(24)
                self.act2 = torch.nn.ReLU()
                self.pool2 = torch.nn.MaxPool2d(2,2)
                self.dense1 = torch.nn.Linear(1176,100)
                self.dense2 = torch.nn.Linear(100,10)
                self.dense3 = torch.nn.Linear(10,1)
                self.sigmoid = torch.nn.Sigmoid()

            def forward(self,X:torch.Tensor):
                shape = X.shape
                X = self.pool1(self.act1(self.bn1(self.conv1(X))))
                X = self.pool2(self.act2(self.bn2(self.conv2(X))))
                X = X.reshape(shape[0],-1)
                X = self.sigmoid(self.dense3(self.dense2(self.dense1(X))))

                return X
        return MySimpleNet()

def add_infershape(onnx_path):
    onnx.save(shape_inference.infer_shapes(onnx.load(onnx_path)),onnx_path)

if __name__ == '__main__':
    timer = Timer()

    class_names  = ['background', "qrcode", "barcode", "mpcode", "pdf417", "dmtx"]
    class_dict = {"qrcode":1, "barcode":2, "mpcode":3, "pdf417":4, "dmtx":5, "background": 0}
    label2class_dict = {1:"qrcode", 2:"barcode", 3:"mpcode", 4:"pdf417", 5:"dmtx", 0:"background"}
    args.net = 'mb3-small-ssd-lite'
    args.net = 'mb2-ssd-lite'
    args.net = 'mb1-ssd'
    args.net = 'mb2-ssd600-lite-c1'
    args.net = 'mb2-ssd600-lite'

    args.checkpoint_folder = os.path.join(os.getcwd(),'checkpoint')
    args.dataset_type = 'xcode'
    args.datasets = [os.path.join(os.getcwd(),'jsons')]
    # model_path = r"C:\kwoncy\projects\xcode-detection\pytorch-ssd\checkpoint\mb3-small-ssd-lite-Epoch-50-Loss-6.65807689583105.pth"

    pretrained_model_path = r"C:\kwoncy\projects\xcode-detection\pytorch-ssd\eval_required\mb2-ssd600-lite-c1-best-E1610-L0.9222-map81.5.pth"
    pretrained_model_path = None
    # pretrained_model_path = r"C:\kwoncy\projects\xcode-detection\pytorch-ssd\checkpoint\mb2-ssd600-lite-best-E120-L2.1105-mAP-0.5883.pth"
    # args.scheduler = None
    # args.resume = r"C:\kwoncy\projects\xcode-detection\pytorch-ssd\checkpoint\mb3-small-ssd-lite-Epoch-50-Loss-6.65807689583105.pth"

    # args.net = 'mb3-small-ssd-lite'
    # args.checkpoint_folder = os.path.join(os.getcwd(),'checkpoint')
    # args.dataset_type = 'voc'
    # args.datasets = ['D:\datas\VOCdevkit\VOC2007','D:\datas\VOCdevkit\VOC2012']
    # args.validation_dataset = 'D:\datas\VOCdevkit\VOC2007-test'
    # args.t_max = 200
    # args.validation_epochs = 5
    # args.num_epochs = 200
    # args.scheduler = 'cosine'
    # args.lr = 0.01

    logging.info(args)
    if args.net == 'vgg16-ssd':
        net = create_vgg_ssd(len(class_names), is_test=True)
    elif args.net == 'mb1-ssd':
        net = create_mobilenetv1_ssd(len(class_names), is_test=True)
    elif args.net == 'mb1-ssd-lite':
        net = create_mobilenetv1_ssd_lite(len(class_names), is_test=True)
    elif args.net == 'sq-ssd-lite':
        net = create_squeezenet_ssd_lite(len(class_names), is_test=True)
    elif args.net == 'mb2-ssd-lite':
        net = create_mobilenetv2_ssd_lite(len(class_names), width_mult=args.mb2_width_mult, is_test=True)
    elif args.net == 'mb3-large-ssd-lite':
        net = create_mobilenetv3_large_ssd_lite(len(class_names), is_test=True)
    elif args.net == 'mb3-small-ssd-lite':
        net = create_mobilenetv3_small_ssd_lite(len(class_names), is_test=True)
    elif args.net == 'mb2-ssd600-lite':
        net =  create_mobilenetv2_ssd_lite(len(class_names), is_test=True, ssd600=True)
    elif args.net == 'mb2-ssd600-lite-c1':
        net =  create_mobilenetv2_ssd_lite(len(class_names), is_test=True, ssd600=True,c=1)
    else:
        logging.fatal("The net type is wrong. It should be one of vgg16-ssd, mb1-ssd and mb1-ssd-lite.")
        parser.print_help(sys.stderr)
        sys.exit(1)

    timer.start("Load Model")
    if pretrained_model_path:
        # net.load(args.trained_model)
        net.load(pretrained_model_path)

    # net.load_state_dict(torch.load(r"C:\kwoncy\projects\xcode-detection\pytorch-ssd\checkpoint\mb3-small-ssd-lite-Epoch-15-Loss-9.15076301574707.pth"))
    # net.load_state_dict(torch.load(r"C:\kwoncy\projects\xcode-detection\pytorch-ssd\checkpoint\mb2-ssd-lite-Epoch-65-Loss-3.078031623363495.pth"))
    net.to(DEVICE)

    from pytorch_model_summary import summary
    # dummy_input= torch.randn(1,3,300,300).to(DEVICE)
    dummy_input= torch.randn(1,3,600,600).to(DEVICE)
    # dummy_input= torch.randn(1,1,600,600).to(DEVICE)

    torch.onnx.export(net, dummy_input,f'{args.net}.onnx',verbose=True, opset_version=11)
    # torch.onnx.export(net, dummy_input,'mysimplenet.onnx',verbose=True, opset_version=11)

    print(summary(net, dummy_input, show_input=True))
    macs, params = profile(net, inputs=(dummy_input,))
    macs, params = clever_format([macs,params], "%.3f")
    print('thop macs: ',macs)
    print('thop params: ', params)
    flops, params = flopth(net, inputs=(dummy_input,))
    print('flopth flops: ', flops)
    print('flopth params: ', params)

    start_time = time.time()
    _ = net(dummy_input)
    print('time: ', time.time()-start_time)

    base_net_lr = args.base_net_lr if args.base_net_lr is not None else args.lr
    extra_layers_lr = args.extra_layers_lr if args.extra_layers_lr is not None else args.lr
    if args.freeze_base_net:
        logging.info("Freeze base net.")
        freeze_net_layers(net.base_net)
        params = itertools.chain(net.source_layer_add_ons.parameters(), net.extras.parameters(),
                                 net.regression_headers.parameters(), net.classification_headers.parameters())
        params = [
            {'params': itertools.chain(
                net.source_layer_add_ons.parameters(),
                net.extras.parameters()
            ), 'lr': extra_layers_lr},
            {'params': itertools.chain(
                net.regression_headers.parameters(),
                net.classification_headers.parameters()
            )}
        ]
    elif args.freeze_net:
        freeze_net_layers(net.base_net)
        freeze_net_layers(net.source_layer_add_ons)
        freeze_net_layers(net.extras)
        params = itertools.chain(net.regression_headers.parameters(), net.classification_headers.parameters())
        logging.info("Freeze all the layers except prediction heads.")
    else:
        params = [
            {'params': net.base_net.parameters(), 'lr': base_net_lr},
            {'params': itertools.chain(
                net.source_layer_add_ons.parameters(),
                net.extras.parameters()
            ), 'lr': extra_layers_lr},
            {'params': itertools.chain(
                net.regression_headers.parameters(),
                net.classification_headers.parameters()
            )}
        ]


class Solution:
    def merge(self, intervals: List[List[int]]) -> List[List[int]]:
        if len(intervals)<2:
            return intervals
        intervals.sort()
        ans=[intervals[0]]
        for x,y in intervals[1:]:
            if x>ans[-1][1]:
                ans.append([x,y])
            elif y>ans[-1][1]:
                ans[-1][1]=y
        return ans


from fastapi.testclient import TestClient
from requests import Session
from sqlalchemy import func

from recipies.models import Ingredient, CreateIngredient, Dish, IngredientItem

def test_create_ingredient(
    client: TestClient,
    ingredient: Ingredient,
):
    response = client.post("/ingredients/", json={"name": "carrot"})
    data = response.json()
    data.pop("id")
    assert response.status_code == 201
    assert data == ingredient

def test_get_ingredient(session: Session, client: TestClient, ingredient: Ingredient):
    db_ingredient = Ingredient.from_orm(CreateIngredient(name="carrot"))
    session.add(db_ingredient)
    session.commit()
    response = client.get("/ingredients/1")
    data = response.json()
    data.pop("id")
    assert response.status_code == 200
    assert data == ingredient

def test_get_ingredient_does_not_exist(client: TestClient):
    response = client.get("/ingredients/1")
    data = response.json()
    assert data["detail"] == "Ingredient not found"
    assert response.status_code == 404

def test_list_ingredients(session: Session, client: TestClient, ingredient: Ingredient):
    db_ingredient = Ingredient.from_orm(CreateIngredient(name="carrot"))
    db_ingredient1 = Ingredient.from_orm(CreateIngredient(name="broccoli"))
    session.add(db_ingredient)
    session.add(db_ingredient1)
    session.commit()
    response = client.get("/ingredients/")
    data = response.json()
    assert response.status_code == 200
    assert len(data) == 2

def test_delete_ingredient(session: Session, client: TestClient):
    db_ingredient = Ingredient.from_orm(CreateIngredient(name="carrot"))
    session.add(db_ingredient)
    session.commit()
    response = client.delete("/ingredients/1")
    assert response.status_code == 204

def test_delete_ingredient_does_not_exist(client: TestClient):
    response = client.delete("/ingredients/1")
    assert response.status_code == 404

def test_create_dish(session: Session, client: TestClient, create_dish):
    response = client.post("/ingredients/dish/", json=create_dish)
    assert session.query(func.count(Dish.id)).scalar() == 1
    assert session.query(func.count(IngredientItem.id)).scalar() == 2
    assert response.status_code == 201

def test_list_dishes(session: Session, client: TestClient, create_dish):
    client.post("/ingredients/dish/", json=create_dish)
    response = client.get("/ingredients/dish/")
    assert len(response.json()) == 1
    assert response.status_code == 200

def test_delete_dish(session: Session, client: TestClient, create_dish):
    client.post("/ingredients/dish/", json=create_dish)
    response = client.delete("/ingredients/dish/1")
    assert response.status_code == 204
    assert response.json() == {"message": "Dish 1 deleted"}

def test_delete_dish_does_not_exist(session: Session, client: TestClient, create_dish):
    response = client.delete("/ingredients/dish/1")
    assert response.status_code == 404

def test_dish_detail(session: Session, client: TestClient, create_dish):
    client.post("/ingredients/dish/", json=create_dish)
    response = client.get("/ingredients/dish/1")
    assert response.status_code == 200

def test_dish_detail_does_not_exist(session: Session, client: TestClient, create_dish):
    response = client.get("/ingredients/dish/1")
    assert response.status_code == 404


# -*- coding: utf-8 -*-
"""
Created on Tue Nov  3 15:07:16 2017

@author: Suhas Somnath
"""
from __future__ import division, print_function, unicode_literals, \
    absolute_import
import unittest
import sys
import os
sys.path.append("../../sidpy/")
from sidpy.sid import Reader

class TestCanReadNotImplemented(unittest.TestCase):

    def setUp(self):

        class DummyReader(Reader):

            def read(self):
                pass

        self.reader_class = DummyReader

    def test_bad_init_obj(self):
        with self.assertRaises(TypeError):
            _ = self.reader_class(['haha.txt'])

    def test_file_does_not_exist(self):
        with self.assertRaises(FileNotFoundError):
            _ = self.reader_class('haha.txt')

    def test_valid_file(self):

        file_path = os.path.abspath('blah.txt')
        with open(file_path, mode='w') as file_handle:
            file_handle.write('Nothing')

        reader = self.reader_class(file_path)
        with self.assertWarns(DeprecationWarning):
            reader.can_read()

        os.remove(file_path)

    def test_invalid_file_object(self):
        with self.assertRaises(TypeError):
            _ = self.reader_class(1.23234)

        with self.assertRaises(TypeError):
            _ = self.reader_class({'fdfdfd': 33})

        with self.assertRaises(TypeError):
            _ = self.reader_class([1, '2343434', False])

    def test_empty_file_path(self):
        with self.assertRaises(ValueError):
            _ = self.reader_class('    ')

    def test_space_in_file_path(self):
        file_path = './blah.txt'
        with open(file_path, mode='w') as file_handle:
            file_handle.write('Nothing')

        reader = self.reader_class('    ' + file_path + '    ')

        self.assertEqual(reader._input_file_path, file_path)

        os.remove(file_path)

if __name__ == '__main__':
    unittest.main()


import numpy as np

def distances(n=100000, d=10):
    X = np.array([np.random.uniform(size=n) for i in range(d)])
    Y = np.array([np.random.uniform(size=n) for j in range(d)])
    Z = (X - Y) ** 2
    R = np.sqrt(np.sum(Z, axis=0))
    R_sd = np.std(R)
    R_mean = np.mean(R)
    print(f"d = {d}, sqrt(d) = {np.sqrt(d):.3f}, [{R_mean - R_sd:.3f}, {R_mean + R_sd:.3f}], R_max: {np.max(R):.3f}")

def analytic_distances(d=10):
    E = 1/6 * d
    Var = 7/180 * d
    SD = np.sqrt(Var)
    print(f"d = {d} [{E - SD:.3f}, {E + SD:.3f}]")

def main():
    low, high = 1, 6
    dims = []
    for i in range(low - 1, high):
        for k in range(5):
            dims.append((2*k+1) * 10 ** i)
    for d in dims:
        distances(d=d)

if __name__ == "__main__":
    main()


try:
    from ipware.ip import get_ip, get_real_ip
except ImportError:
    from django.core.exceptions import ImproperlyConfigured
    raise ImproperlyConfigured('django-crispy-contact-form application requires django-ipware package')

def get_user_ip(request):
    """Return user ip

    :param request: Django request object
    :return: user ip
    """
    ip = get_real_ip(request)
    if ip is None:
        ip = get_ip(request)
        if ip is None:
            ip = '127.0.0.1'
    return ip


#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 自己写得双指针,结合三数之和的双指针思想, 效率比较低 但是好像别人写的也没好多少,反正都是O(N**2)

# 执行用时：152 ms, 在所有 Python3 提交中击败了32.48%的用户
# 内存消耗：13.8 MB, 在所有 Python3 提交中击败了7.17%的用户
class Solution:
    def threeSumClosest(self, nums, target):
        nums.sort()
        res = nums[0] + nums[1] + nums[2]
        mymin = abs(target - nums[0] - nums[1] - nums[2])
        for i in range(len(nums)-2):
            if i > 0 and nums[i] == nums[i-1]:
                continue
            l=i+1
            r = len(nums) - 1
            while l < r:
                if target - (nums[i] + nums[l] + nums[r]) == 0:
                    res = target
                    return res

                elif target - (nums[i] + nums[l] + nums[r]) > 0:
                    if mymin > abs(target - nums[i] - nums[l] - nums[r]):
                        mymin = abs(target - nums[i] - nums[l] - nums[r])
                        res = nums[i] + nums[l] + nums[r]
                    l+=1
                else:
                    if mymin > abs(target - nums[i] - nums[l] - nums[r]):
                        mymin = abs(target - nums[i] - nums[l] - nums[r])
                        res = nums[i] + nums[l] + nums[r]
                    r-=1

        return res

#不过可以进行如下优化，优化的细节是在双指针前加入当前这一轮，也就是first固定下来后，的最大值和最小值与target的关系。
# 如果最大的数都小于等于target，那直接下一轮，first往右也许能更大；如果最小的数都大于等于target，那压根别找了，因为不可能再有更小的数了，直接Break掉返回结果即可。

a=Solution().threeSumClosest([-1,2,1,-4],1)
print(a)

16. 最接近的三数之和.py


'''
Data Scaling

@author: jame phankosol
'''
import numpy as np

# Find the min and max values for each column
def dataset_minmax(dataset):
    '''
    retuen list of tuple (min, max) for each column
    :param dataset:
    :return:
    '''
    minmax = []
    for i in range(len(dataset[0])):
        col_values = [row[i] for row in dataset]
        value_min = min(col_values)
        value_max = max(col_values)
        minmax.append((value_min, value_max))
    return minmax

# Rescale dataset columns to the any range [desired_min, desired_max]
def normalize_dataset(dataset, minmax, desired_min=-1, desired_max=1):
    '''
    rescale data's value
    :param dataset:
    :param minmax:
    :param desired_min:
    :param desired_max:
    :return:
    '''
    desired_range = desired_max - desired_min
    for i in range(0, len(dataset[0])):
        orginal_min, original_max = minmax[i][0], minmax[i][1]
        orginal_range = original_max - orginal_min
        for row in dataset:
            row[i] = (desired_range * ((row[i] - orginal_min) / orginal_range)) + desired_min

# calculate column means
def column_means(dataset):
    means = []
    for i in range(0, len(dataset[0])):
        sum = 0
        for row in dataset:
            sum += row[i]
        mean = sum / float(len(dataset))
        means.append(mean)
    return means

# calculate column standard deviations (SD)
def column_stdevs(dataset, means):
    stdevs = []
    for i in range(0, len(dataset[0])):
        variance = 0
        for row in dataset:
            variance += (row[i] - means[i]) ** 2
        stdevs.append(np.sqrt(variance / float(len(dataset) - 1)))
    return stdevs

# standardize dataset
def standardize_dataset(dataset, means, stdevs):
    '''
    converting data to center
    :param dataset:
    :param means:
    :param stdevs:
    :return:
    '''
    for row in dataset:
        for i in range(0, len(dataset[0])):
            row[i] = (row[i] - means[i]) / stdevs[i]


import json
import zipfile
from io import BytesIO

import numpy as np
from PIL import Image, ImageOps
from django.db.models import Q, QuerySet
from django.http import HttpResponse
from django.utils import timezone

from lsdb.models import WorkOrder, Unit, ProcedureResult
from lsdb.utils import ExcelFile

def create_download_file(work_orders: QuerySet[WorkOrder], tsd_ids, unit_ids,
                         procedure_ids, adjust_images=False):
    mem_zip = BytesIO()

    with zipfile.ZipFile(mem_zip, mode='w', compression=zipfile.ZIP_DEFLATED) as zf:
        for work_order in work_orders:
            sheets = []

            excel_file_units = ExcelFile()
            unit_sheet = excel_file_units.workbook.active
            unit_sheet.append(["Manufacturer", "Technology", "Model",
                               "Serial Number", "Calibration Device Used",
                               "Maximum Voltage[V]", "Width [mm]", "Height [mm]",
                               "Pmax [W]", "VOC [V]", "VMP[V]",
                               "ISC [A]", "IMP [A]"])
            sheets.append(unit_sheet)

            excel_file_vi = ExcelFile()
            vi_sheet = excel_file_vi.workbook.active
            vi_sheet.append(["Serial Number", "TSD", "LEG", "Defect", "Category", "Notes", "Images:"])
            sheets.append(vi_sheet)

            excel_file_el = ExcelFile()
            el_sheet = excel_file_el.workbook.active
            el_sheet.append(
                ["Serial Number", "TSD", "LEG", "Aperture", "ISO", "Exposure Count", "Injection Current",
                 "Exposure Time"])
            sheets.append(el_sheet)

            excel_file_wl = ExcelFile()
            wl_sheet = excel_file_wl.workbook.active
            wl_sheet.append(["Serial Number", "TSD", "LEG", "Insulation Resistance", "Passed?", "Test Voltage",
                             "Leakage Current", "Current Trip Setpoint", "Water Temperature"])
            sheets.append(wl_sheet)

            excel_file_dt = ExcelFile()
            dt_sheet = excel_file_dt.workbook.active
            dt_sheet.append(["Serial Number", "TSD", "LEG", "Forward Voltage", "Reverse Voltage", "Pass?"])
            sheets.append(dt_sheet)

            excel_file_flash = ExcelFile()
            flash_sheet = excel_file_flash.workbook.active
            flash_sheet.append(
                ["Serial Number", "TSD", "LEG", "Pmp", "Voc", "Vmp", "Isc", "Imp", "Irradiance", "Temperature"])
            sheets.append(flash_sheet)

            excel_file_color = ExcelFile()
            color_book = excel_file_color.workbook

            if tsd_ids:
                units = Unit.objects.filter(Q(procedureresult__work_order=work_order) & Q(
                    procedureresult__test_sequence_definition__id__in=tsd_ids)).distinct()
            elif unit_ids:
                units = Unit.objects.filter(
                    Q(procedureresult__work_order=work_order) & Q(id__in=unit_ids)).distinct()
            else:
                units = Unit.objects.filter(procedureresult__work_order=work_order).distinct()

            for unit in units:
                unit_sheet.append([
                    unit.unit_type.manufacturer.name,
                    # unit.unit_type.module_property.module_technology,
                    unit.unit_type.model,
                    unit.serial_number,
                    None,
                    unit.unit_type.module_property.system_voltage,
                    unit.unit_type.module_property.module_width,
                    unit.unit_type.module_property.module_height,
                    unit.unit_type.module_property.nameplate_pmax,
                    unit.unit_type.module_property.voc,
                    unit.unit_type.module_property.vmp,
                    unit.unit_type.module_property.isc,
                    unit.unit_type.module_property.imp
                ])

                if procedure_ids:
                    procedure_query = ProcedureResult.objects.filter(
                        Q(unit=unit) & Q(procedure_definition__id__in=procedure_ids)).order_by(
                        'test_sequence_definition', 'linear_execution_group').select_related(
                        'unit').prefetch_related('stepresult_set__measurementresult_set').distinct()
                else:
                    procedure_query = ProcedureResult.objects.filter(unit=unit).order_by(
                        'test_sequence_definition', 'linear_execution_group').select_related(
                        'unit').prefetch_related('stepresult_set__measurementresult_set')

                for test in procedure_query:
                    if "I-V" in test.procedure_definition.name:
                        for step_result in test.stepresult_set.all().exclude(archived=True):
                            data = [unit.serial_number, test.test_sequence_definition.name, test.name]
                            for measurement in step_result.measurementresult_set.all().order_by('report_order'):
                                if measurement.measurement_result_type.name == 'result_files':
                                    if measurement.result_files.all().count():
                                        has_result = step_result.measurementresult_set.filter(
                                            result_files__name__icontains="Results")
                                        for azurefile in measurement.result_files.all():
                                            if has_result and "Results" in azurefile.file.name:
                                                path = "Flash Data/"
                                                if "200" in step_result.name:
                                                    path += "200W/"
                                                elif "Rear" in test.procedure_definition.name:
                                                    path += "Rear/"
                                                path += "FLASH/{}/{}".format(
                                                    test.test_sequence_definition.name,
                                                    test.name
                                                )
                                                bytes = azurefile.file.file.read()
                                                zf.writestr('{}/{}/{}/{}/{}'.format(
                                                    work_order.project.number,
                                                    work_order.name,
                                                    "Flash Data",
                                                    path,
                                                    azurefile.file.name,
                                                ),
                                                    bytes)
                                            elif not has_result:
                                                path = "Flash Data/"
                                                if "200" in step_result.name:
                                                    path += "200W/"
                                                elif "Rear" in test.procedure_definition.name:
                                                    path += "Rear/"
                                                path += "FLASH/{}/{}".format(
                                                    test.test_sequence_definition.name,
                                                    test.name
                                                )
                                                bytes = azurefile.file.file.read()
                                                zf.writestr('{}/{}/{}/{}/{}'.format(
                                                    work_order.project.number,
                                                    work_order.name,
                                                    "Flash Data",
                                                    path,
                                                    azurefile.file.name,
                                                ),
                                                    bytes)
                                            else:
                                                continue
                                elif measurement.measurement_result_type.name == 'result_datetime':
                                    continue
                                else:
                                    data.append(getattr(measurement, measurement.measurement_result_type.name))
                            flash_sheet.append(data)
                    elif "Wet Leakage" in test.procedure_definition.name:
                        for step_result in test.stepresult_set.all().exclude(archived=True):
                            data = [unit.serial_number, test.test_sequence_definition.name, test.name]
                            for measurement in step_result.measurementresult_set.all().order_by('report_order'):
                                data.append(getattr(measurement, measurement.measurement_result_type.name))
                            wl_sheet.append(data)
                    elif "Visual Inspection" in test.procedure_definition.name:
                        skip = False
                        for step_result in test.stepresult_set.all().exclude(archived=True):
                            data = [unit.serial_number, test.test_sequence_definition.name, test.name]
                            for measurement in step_result.measurementresult_set.all().order_by('report_order'):
                                if step_result.name == "Inspect Module":
                                    if getattr(measurement, measurement.measurement_result_type.name):
                                        skip = True
                                        data.append("No Defects Observed")
                                        vi_sheet.append(data)
                                elif skip:
                                    break
                                else:
                                    if measurement.measurement_result_type.name == 'result_files':
                                        if measurement.result_files.all().count():
                                            for azurefile in measurement.result_files.all():
                                                bytes = azurefile.file.file.read()
                                                zf.writestr('{}/{}/{}/{}'.format(
                                                    work_order.project.number,
                                                    work_order.name,
                                                    "VI Images",
                                                    azurefile.file.name,
                                                ),
                                                    bytes)
                                                data.append(azurefile.file.name)
                                    else:
                                        if measurement.result_defect != None and measurement.measurement_result_type.name == "result_defect":
                                            print(test, step_result, measurement)
                                            print(measurement.result_defect)
                                            data.append(measurement.result_defect.short_name)
                                            data.append(measurement.result_defect.category)
                                        else:
                                            data.append(
                                                getattr(measurement, measurement.measurement_result_type.name))
                            if skip:
                                break
                            vi_sheet.append(data)
                    elif "EL Image" in test.procedure_definition.name:
                        for step_result in test.stepresult_set.all().exclude(archived=True):
                            data = [unit.serial_number, test.test_sequence_definition.name, test.name]
                            for measurement in step_result.measurementresult_set.all().order_by('report_order'):
                                if measurement.measurement_result_type.name == 'result_files':
                                    if measurement.result_files.all().count():
                                        for azurefile in measurement.result_files.all():
                                            filetype = ''
                                            if "RAW" in azurefile.file.name:
                                                continue

                                            if azurefile.file.name.lower().endswith(
                                                    ('xls', 'xlsx', 'txt', 'csv')):
                                                filetype = 'DataFiles'
                                            else:
                                                filetype = 'ImageFiles'

                                            if filetype == 'ImageFiles':
                                                temp = azurefile.file.name.split(".")
                                                name = temp[0]
                                                for sbstr in range(1, len(temp) - 1):
                                                    name = name + "." + temp[sbstr]

                                                name = "{}-{}-{}.{}".format(name,
                                                                            test.test_sequence_definition.name,
                                                                            test.name, temp[len(temp) - 1])

                                            bytes = BytesIO(azurefile.file.file.read())
                                            bytes.seek(0)

                                            tempImage = Image.open(bytes)
                                            try:
                                                if adjust_images == True:
                                                    width, height = tempImage.size
                                                    tempImage = tempImage.rotate(-90, expand=True)
                                                    tempImage = tempImage.resize((height, width))
                                                    tempImage = ImageOps.grayscale(tempImage)
                                                    tempImage = ImageOps.autocontrast(tempImage)
                                                image_bytes = BytesIO()
                                                tempImage.save(image_bytes, format='jpeg', quality=75)
                                                tempImage.close()
                                                content = image_bytes.getvalue()
                                                image_bytes.close()
                                            except:
                                                bytes.seek(0)
                                                content = bytes.getvalue()
                                            bytes.close()

                                            zf.writestr('{}/{}/{}/{}'.format(
                                                    work_order.project.number,
                                                    work_order.name,
                                                    "EL Images",
                                                    name,
                                                ),
                                                content)
                                else:
                                    data.append(getattr(measurement, measurement.measurement_result_type.name))
                        el_sheet.append(data)
                    elif "Colorimeter" in test.procedure_definition.name:
                        if "{}".format(unit.serial_number) not in color_book.sheetnames:
                            color_sheet = color_book.create_sheet(title="{}".format(unit.serial_number))
                        for step_result in test.stepresult_set.all().exclude(archived=True):
                            data = [unit.serial_number, test.test_sequence_definition.name, test.name]
                            for measurement in step_result.measurementresult_set.all().order_by('report_order'):
                                if step_result.name == "Measure Color" and measurement.measurement_result_type.name == "result_string":
                                    if measurement.result_string == None:
                                        pass
                                        # csv[result.name][result.procedure_definition.name]['body'].append('N/A')
                                    else:
                                        # Convert meassurement string to data values
                                        data = json.loads(measurement.result_string)
                                        data = data["values"]

                                        # Header Rows
                                        color_sheet.append([
                                            "Position",
                                            "L*",
                                            "A*",
                                            "B*"
                                        ])

                                        # Append values to sheet, create columns as we go
                                        l_val, a_val, b_val = [], [], []

                                        color_sheet.append(["Serial Number:", unit.serial_number, "TSD:",
                                                            test.test_sequence_definition.name, "LEG:",
                                                            test.name])
                                        for value in data:
                                            row = [value["position"], value["l_value"], value["a_value"],
                                                   value["b_value"]]
                                            l_val.append(value["l_value"])
                                            a_val.append(value["a_value"])
                                            b_val.append(value["b_value"])
                                            color_sheet.append(row)

                                        color_sheet.append(["Average", np.average(l_val), np.average(a_val),
                                                            np.average(b_val)])

                                        color_sheet.append(["STD", np.std(l_val), np.std(a_val), np.std(b_val)])
                                        color_sheet.append(["", "", "", ""])
                            # color_sheet.append(data)
                    elif "Diode Test" in test.procedure_definition.name:
                        for step_result in test.stepresult_set.all().exclude(archived=True):
                            data = [unit.serial_number, test.test_sequence_definition.name, test.name]
                            for measurement in step_result.measurementresult_set.all().order_by('report_order'):
                                data.append(getattr(measurement, measurement.measurement_result_type.name))
                            dt_sheet.append(data)

            for sheet in sheets:
                dims = {}
                for row in sheet.rows:
                    for cell in row:
                        if cell.value:
                            dims[cell.column_letter] = max(
                                (dims.get(cell.column_letter, 0), len(str(cell.value))))
                for col, value in dims.items():
                    sheet.column_dimensions[col].width = value

            zf.writestr('{}/{}/{}'.format(work_order.project.number, work_order.name, "VI.xlsx"),
                        excel_file_vi.get_memory_file().read())

            zf.writestr('{}/{}/{}'.format(work_order.project.number, work_order.name, "EL.xlsx"),
                        excel_file_el.get_memory_file().read())

            zf.writestr('{}/{}/{}'.format(work_order.project.number, work_order.name, "WL.xlsx"),
                        excel_file_wl.get_memory_file().read())

            zf.writestr('{}/{}/{}'.format(work_order.project.number, work_order.name, "DT.xlsx"),
                        excel_file_dt.get_memory_file().read())

            zf.writestr('{}/{}/{}'.format(work_order.project.number, work_order.name, "Flash.xlsx"),
                        excel_file_flash.get_memory_file().read())

            zf.writestr('{}/{}/{}'.format(work_order.project.number, work_order.name, "Colorimeter.xlsx"),
                        excel_file_color.get_memory_file().read())

            zf.writestr('{}/{}/{}'.format(work_order.project.number, work_order.name, "Units.xlsx"),
                        excel_file_units.get_memory_file().read())

    filename = timezone.now().strftime('%b-%d-%Y-%H%M%S')
    response = HttpResponse(mem_zip.getvalue(), content_type='application/x-zip-compressed')
    response['Content-Disposition'] = f'attachment; filename={filename}.zip'
    return response


from PIL import Image
import numpy as np
import os
from natsort import natsorted

def apply_runlength(arr, filename):
    current_clr = True  # true is white, false is black
    runlength = 0
    newarr = np.array([], dtype='i')

    pixel_num = 1

    for pixel in np.nditer(arr):
        if pixel_num % 2 == 0:
            pixel_num += 1
            continue

        if pixel == current_clr:
            runlength += 1
            pixel_num += 1
        else:
            newarr = np.append(newarr, runlength)

            # set runlength to 1 and not 0, because we should count this pixel
            # for the next run
            runlength = 1
            current_clr = not current_clr
            pixel_num += 1

    # account for the last run
    newarr = np.append(newarr, runlength)

    return newarr

def reverse_runlength(arr, dimensions):
    newarr = np.array([], dtype='b')

    current = True

    # convert runs back into binary 1s and 0s
    for run in arr:
        newarr = np.append(newarr, [current, 0] * run)
        current = not current

    # reshape to original dimensions
    newarr = np.reshape(newarr, dimensions)

    return newarr.astype(bool)

def compress(directory, filename, save_filename):
    # open a file
    img = Image.open(directory + filename, 'r')
    data = np.array(img)

    # runlength compress the file
    compressed = apply_runlength(data, filename)

    # save compressed information to new file
    with open(save_filename, mode='a') as file:
        np.savetxt(file, compressed, fmt='%s', newline=' ')

def decompress(filename):
    # load compressed data
    compressed = np.loadtxt(filename).astype(int)

    # reverse runlength encoding
    decompressed = reverse_runlength(compressed, (90, 119))

    # save new array to image
    img = Image.fromarray(decompressed)
    img.save("decompressed.png")

def main():
    choice = input("Do you want to compress or decompress? ")

    if choice == "compress":
        directory = input("Please enter directory where frames are: ") + "/"
        save_filename = input("Please enter filename for compressed data: ").strip()

        framelist = os.listdir(directory)
        framelist = natsorted(framelist)

        num = 0
        for frame_filename in framelist:
            compress(directory, frame_filename, save_filename)
            num += 1
            print(f"Compressed {num} out of {len(framelist)}")

    elif choice == "decompress":
        filename = input("Filename: ")
        decompress(filename)

if __name__ == "__main__":
    main()


"""Function that deletes some files from the directory."""

import os

def delete_file(file_path):
    """
    It's a function that determines if a directory exists and deletes it.

    Parameters
    ----------
    file_path : The desired directory.

    Returns
    -------
    None.

    """
    if os.path.isfile(file_path):
        os.remove(file_path)
        print("File "+file_path+" has been deleted")
    else:
        print("File "+file_path+" does not exist")

def delete_files():
    """
    It's a function that deletes some specified files that ain't useful.

    Returns
    -------
    None.

    """
    delete_file('Wing_Geometry_Generation.tcl')
    delete_file('HM_Files/command1.tcl')
    delete_file('HM_Files/wing.hm')
    delete_file('batchmesher_config_2019.cfg')
    delete_file('batchmesher_config.cfg')
    delete_file('command1.tcl')
    delete_file('command2.tcl')
    delete_file('hmmenu.set')


import json
import multiprocessing
import os
from pathlib import Path

import pandas as pd
import polars as pl
import webdataset as wds
from tqdm import tqdm

from graphnet.data.constants import FEATURES, TRUTH

meta_data_path = Path("../../input/icecube/icecube-neutrinos-in-deep-ice/train_meta.parquet")
geometry_path = Path("../../input/icecube/icecube-neutrinos-in-deep-ice/sensor_geometry.csv")
input_data_path = "../../input/icecube/icecube-neutrinos-in-deep-ice/train/batch_{batch_id}.parquet"
shard_dir = Path("../../input/webdatasets")
shard_dir.mkdir(exist_ok=True)

shard_filename = "../../input/webdatasets/batch-%03d.tar"

from webdataset import TarWriter

def write_one_batch(batch_id, meta_data, geometry_table,):
    pattern = "../../input/webdatasets/batch-%03d.tar"
    print(f"Start saving batch {batch_id}")

    meta_data_batch = meta_data[meta_data.batch_id == batch_id]
    event_ids = meta_data_batch["event_id"].unique()

    df_batch = pd.read_parquet(input_data_path.format(batch_id=batch_id))
    df_batch[["x", "y", "z"]] = geometry_table.loc[df_batch.sensor_id.values, ["x", "y", "z"]].values

    fname = pattern % batch_id
    stream = TarWriter(fname)

    for event_id in tqdm(event_ids, desc=f"Batch {batch_id}"):
        df_event = df_batch[df_batch.index == event_id].copy()
        write_samples_into_single_shard(stream, meta_data_batch, event_id, df_event)
    print(f"Finished saving batch {batch_id}")

    stream.close()

def write_samples_into_single_shard(stream, meta_data_batch, event_id, df_batch):
    truth = meta_data_batch[meta_data_batch.event_id == event_id][TRUTH.KAGGLE].values
    features = df_batch.loc[event_id, FEATURES.KAGGLE].values

    data = {
            "__key__": str(event_id),
            "pickle": (
                features, truth,
            )
        }

    size = stream.write(data)
    return size

batch_ids = range(51, 101)

import joblib

meta_data = pd.read_parquet(meta_data_path)
geometry_table = pd.read_csv(geometry_path) # dtypes={"sensor_id": pl.Int16}
geometry_table = geometry_table.set_index("sensor_id")
joblib.Parallel(n_jobs=4, verbose=11)(joblib.delayed(write_one_batch)(batch_id, meta_data, geometry_table) for batch_id in batch_ids)


# 사용자 입력 : input()
string = input("인사말을 입력하세요 > ")
print(string)
print(type(string))

string = input("입력 > ")
# print("입력 + 100", string+100) input()함수로 입력받은 자료는 모두 문자열로 저장되므로 문자열과 숫자는 더할 수 없어서 에러가 발생

# 문자열을 숫자로 바꾸기
stringA = input("입력 A : ")
intA = int(stringA)

stringB = input("입력 B : ")
intB = int(stringB)

print("문자열 자료 : ", stringA+stringB)
print("숫자 자료 : ", intA+intB)


import heapq
from collections import defaultdict

class Solution:
    def partitionString(self, s: str) -> int:
        letters = set()
        result = 1
        for i in range(len(s)):
            if s[i] not in letters:
                letters.add(s[i])
            else:
                result += 1
                letters = {s[i]}
        return result


# -*- coding: utf-8 -*-
"""
Created on Sat Jan 25 11:20:59 2020

@author: surface
"""
import tensorflow as tf
import math

length=50#帧长
input=39#MFCC特征维数
###########输入数据
x = tf.placeholder(tf.float32,[None,length,input])#输入数据

def self_attention(x,hidden_layer):
    x=tf.layers.conv1d(x,hidden_layer*3,1,strides=1, padding='same')
    Q,K,V=tf.split(x, 3, axis=2)
    K=tf.transpose(K,[0,2,1])
    result=tf.reduce_sum(tf.matmul(Q,K)/math.sqrt(hidden_layer),axis=1)
    result=tf.reshape(result,[-1,50,1])
    result=tf.nn.softmax(result)
    V=V*result
    return V

def multi_head_attention(x,head,output_channel):
    xn=tf.split(x,head,axis=2)
    print(xn)
    V1=xn[0]
    print(V1)
    V1=self_attention(V1,32)
    for a in xn[1:]:
        V=self_attention(a,32)
        V1=tf.concat([V1,V],axis=2)
    print(V1)
    V1=tf.layers.conv1d(V1,output_channel,1,strides=1, padding='same')
    return V1
x=tf.layers.conv1d(x,100,1,strides=1, padding='same')
multi_head_attention(x,5,100)


import re
import logging
import warnings
import json
from math import sqrt
import numpy as np
from .base import string_types
from .ndarray import NDArray, load
from . import random
from . import registry
from . import ndarray
from . util import is_np_array
from . import numpy as _mx_np  # pylint: disable=reimported

# inherit str for backward compatibility
class InitDesc(str):
    """
    Descriptor for the initialization pattern.

    Parameters
    ----------
    name : str
        Name of variable.
    attrs : dict of str to str
        Attributes of this variable taken from ``Symbol.attr_dict``.
    global_init : Initializer
        Global initializer to fallback to.
    """
    def __new__(cls, name, attrs=None, global_init=None):
        ret = super(InitDesc, cls).__new__(cls, name)
        ret.attrs = attrs or {}
        ret.global_init = global_init
        return ret

class Initializer(object):
    """The base class of an initializer."""
    def __init__(self, **kwargs):
        self._kwargs = kwargs
        self._verbose = False
        self._print_func = None

    def set_verbosity(self, verbose=False, print_func=None):
        """Switch on/off verbose mode

        Parameters
        ----------
        verbose : bool
            switch on/off verbose mode
        print_func : function
            A function that computes statistics of initialized arrays.
            Takes an `NDArray` and returns an `str`. Defaults to mean
            absolute value str((abs(x)/size(x)).asscalar()).
        """
        self._verbose = verbose
        if print_func is None:
            def asum_stat(x):
                """returns |x|/size(x), async execution."""
                return str((ndarray.norm(x)/sqrt(x.size)).asscalar())
            print_func = asum_stat
        self._print_func = print_func
        return self

    def _verbose_print(self, desc, init, arr):
        """Internal verbose print function

        Parameters
        ----------
        desc : InitDesc or str
            name of the array
        init : str
            initializer pattern
        arr : NDArray
            initialized array
        """
        if self._verbose and self._print_func:
            logging.info('Initialized %s as %s: %s', desc, init, self._print_func(arr))

    def dumps(self):
        """Saves the initializer to string

        Returns
        -------
        str
            JSON formatted string that describes the initializer.

        Examples
        --------
        >>> # Create initializer and retrieve its parameters
        ...
        >>> init = mx.init.Normal(0.5)
        >>> init.dumps()
        '["normal", {"sigma": 0.5}]'
        >>> init = mx.init.Xavier(factor_type="in", magnitude=2.34)
        >>> init.dumps()
        '["xavier", {"rnd_type": "uniform", "magnitude": 2.34, "factor_type": "in"}]'
        """
        return json.dumps([self.__class__.__name__.lower(), self._kwargs])

    def __call__(self, desc, arr):
        """Initialize an array

        Parameters
        ----------
        desc : InitDesc
            Initialization pattern descriptor.

        arr : NDArray
            The array to be initialized.
        """
        if not isinstance(desc, InitDesc):
            self._legacy_init(desc, arr)
            return

        if desc.global_init is None:
            desc.global_init = self
        init = desc.attrs.get('__init__', "")

        if init:
            # when calling Variable initializer
            create(init)._init_weight(desc, arr)
            self._verbose_print(desc, init, arr)
        else:
            # register nnvm::FSetInputVariableAttrs in the backend for new patterns
            # don't add new cases here.
            if desc.endswith('weight'):
                self._init_weight(desc, arr)
                self._verbose_print(desc, 'weight', arr)
            elif desc.endswith('bias'):
                self._init_bias(desc, arr)
                self._verbose_print(desc, 'bias', arr)
            elif desc.endswith('gamma'):
                self._init_gamma(desc, arr)
                self._verbose_print(desc, 'gamma', arr)
            elif desc.endswith('beta'):
                self._init_beta(desc, arr)
                self._verbose_print(desc, 'beta', arr)
            elif desc.endswith('min'):
                self._init_zero(desc, arr)
                self._verbose_print(desc, 'min', arr)
            elif desc.endswith('max'):
                self._init_one(desc, arr)
                self._verbose_print(desc, 'max', arr)
            elif desc.endswith('weight_quantize'):
                self._init_quantized_weight(desc, arr)
                self._verbose_print(desc, 'weight_quantize', arr)
            elif desc.endswith('bias_quantize'):
                self._init_quantized_bias(desc, arr)
                self._verbose_print(desc, 'bias_quantize', arr)
            else:
                self._init_default(desc, arr)

    def _legacy_init(self, name, arr):
        """Legacy initialization method.

        Parameters
        ----------
        name : str
            Name of corresponding NDArray.

        arr : NDArray
            NDArray to be initialized.
        """
        warnings.warn(
            "\033[91mCalling initializer with init(str, NDArray) has been deprecated." \
            "please use init(mx.init.InitDesc(...), NDArray) instead.\033[0m",
            DeprecationWarning, stacklevel=3)
        if not isinstance(name, string_types):
            raise TypeError('name must be string')
        if not isinstance(arr, NDArray):
            raise TypeError('arr must be NDArray')
        if name.startswith('upsampling'):
            self._init_bilinear(name, arr)
        elif name.startswith('stn_loc') and name.endswith('weight'):
            self._init_zero(name, arr)
        elif name.startswith('stn_loc') and name.endswith('bias'):
            self._init_loc_bias(name, arr)
        elif name.endswith('bias'):
            self._init_bias(name, arr)
        elif name.endswith('gamma'):
            self._init_gamma(name, arr)
        elif name.endswith('beta'):
            self._init_beta(name, arr)
        elif name.endswith('weight'):
            self._init_weight(name, arr)
        elif name.endswith("moving_mean"):
            self._init_zero(name, arr)
        elif name.endswith("moving_var"):
            self._init_one(name, arr)
        elif name.endswith("moving_inv_var"):
            self._init_zero(name, arr)
        elif name.endswith("moving_avg"):
            self._init_zero(name, arr)
        elif name.endswith('min'):
            self._init_zero(name, arr)
        elif name.endswith('max'):
            self._init_one(name, arr)
        else:
            self._init_default(name, arr)

    def _init_bilinear(self, _, arr):
        weight = np.zeros(np.prod(arr.shape), dtype='float32')
        shape = arr.shape
        f = np.ceil(shape[3] / 2.)
        c = (2 * f - 1 - f % 2) / (2. * f)
        for i in range(np.prod(shape)):
            x = i % shape[3]
            y = (i // shape[3]) % shape[2]
            weight[i] = (1 - abs(x / f - c)) * (1 - abs(y / f - c))
        arr[:] = weight.reshape(shape)

    def _init_loc_bias(self, _, arr):
        shape = arr.shape
        assert(shape[0] == 6)
        arr[:] = np.array([1.0, 0, 0, 0, 1.0, 0])

    def _init_zero(self, _, arr):
        arr[:] = 0.0

    def _init_one(self, _, arr):
        arr[:] = 1.0

    def _init_bias(self, _, arr):
        arr[:] = 0.0

    def _init_quantized_bias(self, _, arr):
        arr[:] = 0

    def _init_gamma(self, _, arr):
        arr[:] = 1.0

    def _init_beta(self, _, arr):
        arr[:] = 0.0

    def _init_weight(self, name, arr):
        """Abstract method to Initialize weight."""
        raise NotImplementedError("Must override it")

    def _init_quantized_weight(self, _, arr):
        _arr = random.randint(-127, 127, dtype='int32').asnumpy()
        arr[:] = np.int8(_arr)

    def _init_default(self, name, _):
        raise ValueError(
            f'Unknown initialization pattern for {name}. ' \
            'Default initialization is now limited to '\
            '"weight", "bias", "gamma" (1.0), and "beta" (0.0).' \
            'Please use mx.sym.Variable(init=mx.init.*) to set initialization pattern')

    def __eq__(self, other):
        if not isinstance(other, Initializer):
            return NotImplemented
        # pylint: disable=unidiomatic-typecheck
        return type(self) is type(other) and self._kwargs == other._kwargs

# pylint: disable=invalid-name
_register = registry.get_register_func(Initializer, 'initializer')
alias = registry.get_alias_func(Initializer, 'initializer')
create = registry.get_create_func(Initializer, 'initializer')
# pylint: enable=invalid-name

def register(klass):
    """Registers a custom initializer.

    Custom initializers can be created by extending `mx.init.Initializer` and implementing the
    required functions like `_init_weight` and `_init_bias`. The created initializer must be
    registered using `mx.init.register` before it can be called by name.

    Parameters
    ----------
    klass : class
        A subclass of `mx.init.Initializer` that needs to be registered as a custom initializer.

    Example
    -------
    >>> # Create and register a custom initializer that
    ... # initializes weights to 0.1 and biases to 1.
    ...
    >>> @mx.init.register
    ... @alias('myinit')
    ... class CustomInit(mx.init.Initializer):
    ...   def __init__(self):
    ...     super(CustomInit, self).__init__()
    ...   def _init_weight(self, _, arr):
    ...     arr[:] = 0.1
    ...   def _init_bias(self, _, arr):
    ...     arr[:] = 1
    ...
    >>> # block is an instance of 'mxnet.gluon.Block'
    ...
    >>> block.initialize(CustomInit())
    """
    return _register(klass)

class Load(object):
    """Initializes variables by loading data from file or dict.

    **Note** Load will drop ``arg:`` or ``aux:`` from name and
    initialize the variables that match with the prefix dropped.

    Parameters
    ----------
    param: str or dict of str->`NDArray`
        Parameter file or dict mapping name to NDArray.
    default_init: Initializer
        Default initializer when name is not found in `param`.
    verbose: bool
        Flag for enabling logging of source when initializing.

    """
    def __init__(self, param, default_init=None, verbose=False):
        if isinstance(param, str):
            param = load(param)
        assert isinstance(param, dict)
        self.param = {}
        for name, arr in param.items():
            if name.startswith('arg:') or name.startswith('aux:'):
                self.param[name[4:]] = arr
            else:
                self.param[name] = arr
        self.default_init = default_init
        self.verbose = verbose

    def __call__(self, name, arr):
        if name in self.param:
            assert arr.shape == self.param[name].shape, \
                f'Parameter {name} cannot be initialized from loading. ' + \
                f'Shape mismatch, target {str(arr.shape)} vs loaded {self.param[name].shape}'
            arr[:] = self.param[name]
            if self.verbose:
                logging.info('Initialized %s by loading', name)
        else:
            assert self.default_init is not None, \
                f"Cannot Initialize {name}. Not found in loaded param " + \
                "and no default Initializer is provided."
            self.default_init(name, arr)
            if self.verbose:
                logging.info('Initialized %s by default', name)

class Mixed(object):
    """Initialize parameters using multiple initializers.

    Parameters
    ----------
    patterns: list of str
        List of regular expressions matching parameter names.
    initializers: list of Initializer
        List of initializers corresponding to `patterns`.

    Example
    -------
    >>> # Given 'block', an instance of 'mxnet.gluon.Block', initialize biases to zero
    ... # and every other parameter to random values with uniform distribution.
    ...
    >>> init = mx.initializer.Mixed(['bias', '.*'], [mx.init.Zero(), mx.init.Uniform(0.1)])
    >>> block.initialize(init)
    >>>
    >>> for dictionary in module.get_params():
    ...     for key in dictionary:
    ...         print(key)
    ...         print(dictionary[key].asnumpy())
    ...
    fullyconnected1_weight
    [[ 0.0097627   0.01856892  0.04303787]]
    fullyconnected1_bias
    [ 0.]

    """
    def __init__(self, patterns, initializers):
        assert len(patterns) == len(initializers)
        self.map = list(zip([re.compile(p) for p in patterns], initializers))

    def __call__(self, name, arr):
        for prog, init in self.map:
            if prog.match(name):
                init(name, arr)
                return
        raise ValueError('Parameter name %s did not match any pattern. Consider' +
                         'add a ".*" pattern at the and with default Initializer.')

@register
@alias("zeros")
class Zero(Initializer):
    """Initializes weights to zero.

    Example
    -------
    >>> # Given 'block', an instance of 'mxnet.gluon.Block', initialize weights to zero.
    ...
    >>> init = mx.initializer.Zero()
    >>> module.initialize(init)
    >>> for dictionary in module.get_params():
    ...     for key in dictionary:
    ...         print(key)
    ...         print(dictionary[key].asnumpy())
    ...
    fullyconnected0_weight
    [[ 0.  0.  0.]]
    """
    def __init__(self):
        super(Zero, self).__init__()

    def _init_weight(self, _, arr):
        arr[:] = 0

@register
@alias("ones")
class One(Initializer):
    """Initializes weights to one.

    Example
    -------
    >>> # Given 'block', an instance of 'mxnet.gluon.Block', initialize weights to one.
    ...
    >>> init = mx.initializer.One()
    >>> module.initialize(init)
    >>> for dictionary in module.get_params():
    ...     for key in dictionary:
    ...         print(key)
    ...         print(dictionary[key].asnumpy())
    ...
    fullyconnected0_weight
    [[ 1.  1.  1.]]
    """
    def __init__(self):
        super(One, self).__init__()

    def _init_weight(self, _, arr):
        arr[:] = 1

@register
class Constant(Initializer):
    """Initializes the weights to a given value.
    The value passed in can be a scalar or a NDarray that matches the shape
    of the parameter to be set.

    Parameters
    ----------
    value : float, NDArray
        Value to set.
    """
    def __init__(self, value):
        super(Constant, self).__init__(value=value)
        self.value = value

    def _init_weight(self, _, arr):
        arr[:] = self.value

    def dumps(self):
        val = self._kwargs['value']
        if not np.isscalar(val):
            self._kwargs['value'] = val.tolist() if isinstance(val, np.ndarray) else val.asnumpy().tolist()
        return json.dumps([self.__class__.__name__.lower(), self._kwargs])

@register
class Uniform(Initializer):
    """Initializes weights with random values uniformly sampled from a given range.

    Parameters
    ----------
    scale : float, optional
        The bound on the range of the generated random values.
        Values are generated from the range [-`scale`, `scale`].
        Default scale is 0.07.

    Example
    -------
    >>> # Given 'block', an instance of 'mxnet.gluon.Block', initialize weights
    >>> # to random values uniformly sampled between -0.1 and 0.1.
    ...
    >>> init = mx.init.Uniform(0.1)
    >>> module.initialize(init)
    >>> for dictionary in module.get_params():
    ...     for key in dictionary:
    ...         print(key)
    ...         print(dictionary[key].asnumpy())
    ...
    fullyconnected0_weight
    [[ 0.01360891 -0.02144304  0.08511933]]
    """
    def __init__(self, scale=0.07):
        super(Uniform, self).__init__(scale=scale)
        self.scale = scale

    def _init_weight(self, _, arr):
        uniform_fn = _mx_np.random.uniform if is_np_array() else random.uniform
        uniform_fn(-self.scale, self.scale, arr.shape, dtype=arr.dtype, out=arr)

@register
class Normal(Initializer):
    """Initializes weights with random values sampled from a normal distribution
    with a mean of zero and standard deviation of `sigma`.

    Parameters
    ----------
    sigma : float, optional
        Standard deviation of the normal distribution.
        Default standard deviation is 0.01.

    Example
    -------
    >>> # Given 'block', an instance of 'mxnet.gluon.Block', initialize weights
    >>> # to random values sampled from a normal distribution.
    ...
    >>> init = mx.init.Normal(0.5)
    >>> module.initialize(init)
    >>> for dictionary in module.get_params():
    ...     for key in dictionary:
    ...         print(key)
    ...         print(dictionary[key].asnumpy())
    ...
    fullyconnected0_weight
    [[-0.3214761  -0.12660924  0.53789419]]
    """
    def __init__(self, sigma=0.01):
        super(Normal, self).__init__(sigma=sigma)
        self.sigma = sigma

    def _init_weight(self, _, arr):
        normal_fn = _mx_np.random.normal if is_np_array() else random.normal
        normal_fn(0, self.sigma, arr.shape, dtype=arr.dtype, out=arr)

@register
class Orthogonal(Initializer):
    """Initialize weight as orthogonal matrix.

    This initializer implements *Exact solutions to the nonlinear dynamics of
    learning in deep linear neural networks*, available at
    https://arxiv.org/abs/1312.6120.

    Parameters
    ----------
    scale : float optional
        Scaling factor of weight.

    rand_type: string optional
        Use "uniform" or "normal" random number to initialize weight.

    """
    def __init__(self, scale=1.414, rand_type="uniform"):
        super(Orthogonal, self).__init__(scale=scale, rand_type=rand_type)
        self.scale = scale
        self.rand_type = rand_type

    def _init_weight(self, _, arr):
        nout = arr.shape[0]
        nin = np.prod(arr.shape[1:])
        if self.rand_type == "uniform":
            tmp = random.uniform(-1.0, 1.0, shape=(nout, nin)).asnumpy()
        elif self.rand_type == "normal":
            tmp = random.normal(0.0, 1.0, shape=(nout, nin)).asnumpy()
        u, _, v = np.linalg.svd(tmp, full_matrices=False) # pylint: disable=invalid-name
        if u.shape == tmp.shape:
            res = u
        else:
            res = v
        res = self.scale * res.reshape(arr.shape)
        arr[:] = res

@register
class Xavier(Initializer):
    """Returns an initializer performing "Xavier" initialization for weights.

    This initializer is designed to keep the scale of gradients roughly the same
    in all layers.

    By default, `rnd_type` is ``'uniform'`` and `factor_type` is ``'avg'``,
    the initializer fills the weights with random numbers in the range
    of :math:`[-c, c]`, where :math:`c = \\sqrt{\\frac{3.}{0.5 * (n_{in} + n_{out})}}`.
    :math:`n_{in}` is the number of neurons feeding into weights, and :math:`n_{out}` is
    the number of neurons the result is fed to.

    If `rnd_type` is ``'uniform'`` and `factor_type` is ``'in'``,
    the :math:`c = \\sqrt{\\frac{3.}{n_{in}}}`.
    Similarly when `factor_type` is ``'out'``, the :math:`c = \\sqrt{\\frac{3.}{n_{out}}}`.

    If `rnd_type` is ``'gaussian'`` and `factor_type` is ``'avg'``,
    the initializer fills the weights with numbers from normal distribution with
    a standard deviation of :math:`\\sqrt{\\frac{3.}{0.5 * (n_{in} + n_{out})}}`.

    Parameters
    ----------
    rnd_type: str, optional
        Random generator type, can be ``'gaussian'`` or ``'uniform'``.

    factor_type: str, optional
        Can be ``'avg'``, ``'in'``, or ``'out'``.

    magnitude: float, optional
        Scale of random number.
    """
    def __init__(self, rnd_type="uniform", factor_type="avg", magnitude=3):
        super(Xavier, self).__init__(rnd_type=rnd_type, factor_type=factor_type,
                                     magnitude=magnitude)
        self.rnd_type = rnd_type
        self.factor_type = factor_type
        self.magnitude = float(magnitude)

    def _init_weight(self, name, arr):
        shape = arr.shape
        hw_scale = 1.
        if len(shape) < 2:
            raise ValueError('Xavier initializer cannot be applied to vector {0}. It requires at'
                             ' least 2D.'.format(name))
        if len(shape) > 2:
            hw_scale = np.prod(shape[2:])
        fan_in, fan_out = shape[1] * hw_scale, shape[0] * hw_scale
        factor = 1.
        if self.factor_type == "avg":
            factor = (fan_in + fan_out) / 2.0
        elif self.factor_type == "in":
            factor = fan_in
        elif self.factor_type == "out":
            factor = fan_out
        else:
            raise ValueError("Incorrect factor type")
        scale = np.sqrt(self.magnitude / factor)
        if self.rnd_type == "uniform":
            uniform_fn = _mx_np.random.uniform if is_np_array() else random.uniform
            uniform_fn(-scale, scale, arr.shape, dtype=arr.dtype, out=arr)
        elif self.rnd_type == "gaussian":
            normal_fn = _mx_np.random.normal if is_np_array() else random.normal
            normal_fn(0, scale, arr.shape, dtype=arr.dtype, out=arr)
        else:
            raise ValueError("Unknown random type")

@register
class MSRAPrelu(Xavier):
    """Initialize the weight according to a MSRA paper.

    This initializer implements *Delving Deep into Rectifiers: Surpassing
    Human-Level Performance on ImageNet Classification*, available at
    https://arxiv.org/abs/1502.01852.

    This initializer is proposed for initialization related to ReLu activation,
    it makes some changes on top of Xavier method.

    Parameters
    ----------
    factor_type: str, optional
        Can be ``'avg'``, ``'in'``, or ``'out'``.

    slope: float, optional
        initial slope of any PReLU (or similar) nonlinearities.
    """
    def __init__(self, factor_type="avg", slope=0.25):
        magnitude = 2. / (1 + slope ** 2)
        super(MSRAPrelu, self).__init__("gaussian", factor_type, magnitude)
        self._kwargs = {'factor_type': factor_type, 'slope': slope}

@register
class Bilinear(Initializer):
    """Initialize weight for upsampling layers."""
    def __init__(self):
        super(Bilinear, self).__init__()

    def _init_weight(self, _, arr):
        weight = np.zeros(np.prod(arr.shape), dtype='float32')
        shape = arr.shape
        f = np.ceil(shape[3] / 2.)
        c = (2 * f - 1 - f % 2) / (2. * f)
        for i in range(np.prod(shape)):
            x = i % shape[3]
            y = (i // shape[3]) % shape[2]
            weight[i] = (1 - abs(x / f - c)) * (1 - abs(y / f - c))
        arr[:] = weight.reshape(shape)

@register
class LSTMBias(Initializer):
    """Initialize all biases of an LSTMCell to 0.0 except for
    the forget gate whose bias is set to custom value.

    Parameters
    ----------
    forget_bias: float, default 1.0
        bias for the forget gate. Jozefowicz et al. 2015 recommends
        setting this to 1.0.
    """
    def __init__(self, forget_bias=1.0):
        super(LSTMBias, self).__init__(forget_bias=forget_bias)
        self.forget_bias = forget_bias

    def _init_weight(self, name, arr):
        arr[:] = 0.0
        # in the case of LSTMCell the forget gate is the second
        # gate of the 4 LSTM gates, we modify the according values.
        num_hidden = int(arr.shape[0] / 4)
        arr[num_hidden:2*num_hidden] = self.forget_bias

@register
class RNNFused(Initializer):
    """Initialize RNN fused parameter with bias part initialized to 0.0 and
    weight initialized with random values uniformly sampled from a given range.

    Parameters
    ----------
    mode : {'gru', 'lstm', 'rnn_relu', 'rnn_tanh'}, required
        the type of RNN to compute
    num_layers : int (non-negative), required
        number of stacked layers
    state_size : int (non-negative), required
        size of the state for each layer
    bidirectional : boolean, optional, default=0
        whether to use bidirectional recurrent layers
    projection_size : int or None, optional, default='None'
        size of project size
    scale : float, optional
        The bound on the range of the generated random values for weights.
        Values are generated from the range [-`scale`, `scale`].
        Default scale is 0.07.
    """
    def __init__(self, mode, num_layers, state_size, bidirectional=False,
                 projection_size=None, i2h_weight_initializer=None,
                 h2h_weight_initializer=None, i2h_bias_initializer=None,
                 h2h_bias_initializer=None, h2r_weight_initializer=None):
        super(RNNFused, self).__init__(mode=mode, num_layers=num_layers,
                                       state_size=state_size,
                                       bidirectional=bidirectional,
                                       projection_size=projection_size,
                                       i2h_weight_initializer=i2h_weight_initializer,
                                       h2h_weight_initializer=h2h_weight_initializer,
                                       i2h_bias_initializer=i2h_bias_initializer,
                                       h2h_bias_initializer=h2h_bias_initializer,
                                       h2r_weight_initializer=h2r_weight_initializer)
        self.gates = {'rnn_relu': 1, 'rnn_tanh': 1, 'lstm': 4, 'gru': 3}[mode]
        self.num_layers = num_layers
        self.num_hidden = state_size
        self.dir = 2 if bidirectional else 1
        self.projection_size = projection_size
        self._i2h_weight_initializer = i2h_weight_initializer
        self._h2h_weight_initializer = h2h_weight_initializer
        self._i2h_bias_initializer = i2h_bias_initializer
        self._h2h_bias_initializer = h2h_bias_initializer
        self._h2r_weight_initializer = h2r_weight_initializer

    # pylint: disable=too-many-nested-blocks
    def _init_weight(self, name, arr):
        arr_len = arr.shape[0]
        size = self.num_hidden * self.dir * self.gates
        if not self.projection_size:
            # second layer size
            size2 = (self.num_hidden * self.dir + self.num_hidden + 2) * size
            input_size = (arr_len - (self.num_layers - 1) * size2) // \
                size - 2 - self.num_hidden
        else:
            # second layer size
            size2 = (self.projection_size * self.dir + self.projection_size + 2) * size
            size_projection = self.projection_size * self.num_hidden * self.num_layers * self.dir
            input_size = (arr_len - size_projection - (self.num_layers - 1) * size2) // \
                size - 2 - self.projection_size
        begin = 0
        if not self.projection_size:
            for param in ['weight', 'bias']:
                for layer_num in range(self.num_layers):
                    for _ in range(self.dir):
                        for connect in ['i2h', 'h2h']:
                            num_inputs = input_size
                            if layer_num != 0:
                                num_inputs = self.num_hidden * self.dir
                            if connect == 'h2h':
                                num_inputs = self.num_hidden
                            shape0 = self.gates * self.num_hidden
                            if param == 'weight':
                                cur_len = shape0 * num_inputs
                            else:
                                cur_len = shape0
                            self._init_util(param, connect, arr[begin:begin+cur_len])
                            begin += cur_len
        else:
            for param in ['weight', 'bias']:
                for layer_num in range(self.num_layers):
                    for _ in range(self.dir):
                        for connect in ['i2h', 'h2h', 'h2r']:
                            if connect != 'h2r' or param != 'bias':
                                if connect == 'h2r':
                                    cur_len = self.projection_size * self.num_hidden
                                else:
                                    num_inputs = input_size
                                    if layer_num != 0:
                                        num_inputs = self.projection_size * self.dir
                                    if connect == 'h2h':
                                        num_inputs = self.projection_size
                                    shape0 = self.gates * self.num_hidden
                                    if param == 'weight':
                                        cur_len = shape0 * num_inputs
                                    else:
                                        cur_len = shape0
                                self._init_util(param, connect, arr[begin:begin+cur_len])
                                begin += cur_len

    def _init_util(self, param, connect, arr):
        name = "_{}_{}_initializer".format(connect, param)
        init = getattr(self, name)
        create(init)(InitDesc(name, {'__init__': init}), arr)

    def set_initializer(self, init):
        self._i2h_weight_initializer = \
            init if not self._i2h_weight_initializer else 'uniform'
        self._h2h_weight_initializer = \
            init if not self._h2h_weight_initializer else 'uniform'
        self._i2h_bias_initializer = \
            init if not self._i2h_bias_initializer else 'zero'
        self._h2h_bias_initializer = \
            init if not self._i2h_bias_initializer else 'zero'
        self._h2r_weight_initializer = \
            init if not self._h2r_weight_initializer else 'uniform'


import json
import os

def get_configuration():
    with open("./process-energy-estimation/configuration.json") as json_file:
            configuration = json.load(json_file)

    command = configuration['COMMAND']
    powerlog_filename = configuration['POWERLOG_FILENAME']
    process_filename = configuration['PROCESS_FILENAME']
    nvidia_smi_filename = configuration['NVIDIA_SMI_FILENAME']
    total_process_data = configuration['TOTAL_PROCESS_DATA']
    interval = configuration['INTERVAL']
    cpu_sockets = configuration['PHYSICAL_CPU_SOCKETS']

    return [command,powerlog_filename,process_filename,nvidia_smi_filename,total_process_data,interval,cpu_sockets]

def initialize_files():
    """Checks if the files with the given names exist, if this is true deletes them."""
    reports_path = get_reports_path()
    base_reports_path = get_base_reports_path()
    powerlog_filename = get_powerlog_filename()
    process_filename = get_process_filename()
    nvidia_smi_filename = get_nvidia_smi_filename()

    base_powerlog_filename = get_base_powerlog_filename()
    base_nvidia_smi_filename = get_base_nvidia_smi_filename()

    if not os.path.exists(reports_path):
        os.mkdir(reports_path)
    if not os.path.exists(base_reports_path):
        os.mkdir(base_reports_path)
    if os.path.exists(base_powerlog_filename):
        os.remove(base_powerlog_filename)
    if os.path.exists(base_nvidia_smi_filename):
        os.remove(base_nvidia_smi_filename)
    if os.path.exists(powerlog_filename):
        os.remove(powerlog_filename)
    if os.path.exists(process_filename):
        os.remove(process_filename)
    if os.path.exists(nvidia_smi_filename):
        os.remove(nvidia_smi_filename)

def get_base_check_seconds():
    with open("./process-energy-estimation/configuration.json") as json_file:
            configuration = json.load(json_file)
    return configuration['BASE_CHECK_SECONDS']

def get_reports_path():
    with open("./process-energy-estimation/configuration.json") as json_file:
            configuration = json.load(json_file)
    return configuration['REPORTS_PATH']

def get_base_reports_path():
    with open("./process-energy-estimation/configuration.json") as json_file:
            configuration = json.load(json_file)

    reports_path = configuration['REPORTS_PATH']
    return os.path.join(reports_path, "base/")

def get_base_powerlog_filename():
    with open("./process-energy-estimation/configuration.json") as json_file:
            configuration = json.load(json_file)

    reports_path = configuration['REPORTS_PATH']
    powerlog_filename = configuration['POWERLOG_FILENAME']
    return os.path.join(reports_path, "base/", powerlog_filename)

def get_base_nvidia_smi_filename():
    with open("./process-energy-estimation/configuration.json") as json_file:
            configuration = json.load(json_file)

    reports_path = configuration['REPORTS_PATH']
    nvidia_smi_filename = configuration['NVIDIA_SMI_FILENAME']
    return os.path.join(reports_path, "base/", nvidia_smi_filename)

def get_powerlog_filename():
    with open("./process-energy-estimation/configuration.json") as json_file:
            configuration = json.load(json_file)
    reports_path = configuration['REPORTS_PATH']
    powerlog_filename = configuration['POWERLOG_FILENAME']
    return os.path.join(reports_path, powerlog_filename)

def get_process_filename():
    with open("./process-energy-estimation/configuration.json") as json_file:
            configuration = json.load(json_file)

    reports_path = configuration['REPORTS_PATH']
    process_filename = configuration['PROCESS_FILENAME']
    return os.path.join(reports_path, process_filename)

def get_nvidia_smi_filename():
    with open("./process-energy-estimation/configuration.json") as json_file:
            configuration = json.load(json_file)

    reports_path = configuration['REPORTS_PATH']
    nvidia_smi_filename = configuration['NVIDIA_SMI_FILENAME']
    return os.path.join(reports_path, nvidia_smi_filename)

def get_total_process_data_filename():
    with open("./process-energy-estimation/configuration.json") as json_file:
            configuration = json.load(json_file)
    reports_path = configuration['REPORTS_PATH']
    total_process_data_filename = configuration['TOTAL_PROCESS_DATA']
    return os.path.join(reports_path, total_process_data_filename)

def get_command():
    with open("./process-energy-estimation/configuration.json") as json_file:
            configuration = json.load(json_file)
    return configuration['COMMAND']

def get_auxiliary_command():
    with open("./process-energy-estimation/configuration.json") as json_file:
            configuration = json.load(json_file)
    return configuration['AUXILIARY_COMMAND']

def get_interval():
    with open("./process-energy-estimation/configuration.json") as json_file:
            configuration = json.load(json_file)
    return configuration['INTERVAL']

def get_physical_cpu_sockets():
    with open("./process-energy-estimation/configuration.json") as json_file:
            configuration = json.load(json_file)
    return configuration['PHYSICAL_CPU_SOCKETS']

def get_cpu_on():
    with open("./process-energy-estimation/configuration.json") as json_file:
            configuration = json.load(json_file)
    return configuration['CPU']

def get_gpu_on():
    with open("./process-energy-estimation/configuration.json") as json_file:
            configuration = json.load(json_file)
    return configuration['GPU']

def get_dram_on():
    with open("./process-energy-estimation/configuration.json") as json_file:
            configuration = json.load(json_file)
    return configuration['DRAM']

def get_cpu_usage_collector():
    with open("./process-energy-estimation/configuration.json") as json_file:
            configuration = json.load(json_file)
    return configuration['CPU_USAGE_COLLECTOR']

def has_soc_gpu():
    with open("./process-energy-estimation/configuration.json") as json_file:
            configuration = json.load(json_file)
    return configuration['SOC_GPU']


def cutTheSticks(arr):
    s = list(set(arr))
    s.sort()
    ar1 = [len(arr)]
    temp = len(arr)
    for i in range(len(s)-1):
        temp -= arr.count(s[i])
        ar1.append(temp)

    return ar1

print(cutTheSticks([5, 4, 4, 2, 2, 8]))


# import Levenshtein as lev
from fuzzywuzzy import fuzz

# function to match any general string
def match_name(text, name):
    name_ratio = fuzz.partial_ratio(name.lower(), text.lower())
    return name_ratio

# function to match aadhar number
def match_aadhar_no(text, id_no):
    id_match = ""
    count = 0
    for i in id_no:
        if i.isnumeric():
            id_match += i
            count += 1
            if count % 4 == 0 and count != 12:
                id_match += " "
        elif i == ' ':
            continue
        else:
            return 0
    # print("ID MATCH = ", id_match)
    if count == 12:
        id_ratio = fuzz.partial_ratio(id_match, text)
        return id_ratio
    else:
        return 0

# function to match DL number
def match_dl_no(text, id_no):
    # print(text)
    # id_match = ''
    count = 0
    # print('id =', id_no)

    # pre-processing
    for i in id_no:
        if (count < 2 and i.isalpha()) or (count < 15 and i.isnumeric()):
            # id_match += i
            count += 1
        elif i == ' ' or i == '-':
            continue
        else:
            return 0
    # print('id_match =', id_match)
    id_ratio = fuzz.partial_ratio(id_no, text)
    return id_ratio

# match passport id
def match_passport_id(text, id_no):
    id_ratio = fuzz.partial_ratio(id_no, text)
    return id_ratio

# function to pre-process DOB and match it according to the separator
def match_dob(text, dob, sep):
    temp = ''
    count = 0
    date = ['', '', '']
    dob += '/'
    for i in dob:
        # print(i)
        if i.isnumeric():
            temp += i
        elif i == '.' or i == '-' or i == '/':
            if count < 3:
                date[count] = temp
                temp = ''
                count += 1
            else:
                return 0
        else:
            return 0
    date_match = date[0] + sep + date[1] + sep + date[2]
    date_ratio = fuzz.partial_ratio(date_match, text)
    # print(date_ratio)
    return date_ratio


import re
import os
import Core
import SUSE

META_CLASS = "SLE"
META_CATEGORY = "DM"
META_COMPONENT = "MPIO"
PATTERN_ID = os.path.basename(__file__)
PRIMARY_LINK = "META_LINK_TID"
OVERALL = Core.TEMP
OVERALL_INFO = "NOT SET"
OTHER_LINKS = "META_LINK_TID=https://www.suse.com/support/kb/doc/?id=000019884|META_LINK_BUG=https://bugzilla.suse.com/show_bug.cgi?id=1182917"

Core.init(META_CLASS, META_CATEGORY, META_COMPONENT, PATTERN_ID, PRIMARY_LINK, OVERALL, OVERALL_INFO, OTHER_LINKS)

##############################################################################
# Local Function Definitions
##############################################################################

def mpioPmemErrors():
	fileOpen = "mpio.txt"
	section = "bin/multipath -ll"
	content = []
	CONFIRMED = re.compile("pmem.*HDIO_GETGEO failed with 25", re.IGNORECASE)
	if Core.isFileActive(fileOpen):
		if Core.getRegExSection(fileOpen, section, content):
			for line in content:
				if CONFIRMED.search(line):
					return True
	return False

##############################################################################
# Main Program Execution
##############################################################################

IN_SCOPE = False
RPM_NAME = 'multipath-tools'
RPM_VERSION_FIXED = ''

SERVER = SUSE.getHostInfo()
if( SERVER['DistroVersion'] == 15 ):
	if( SERVER['DistroPatchLevel'] == 2 ):
		RPM_VERSION_FIXED = '0.8.2+166.7501a27-4.9.1'
		IN_SCOPE = True

if( IN_SCOPE ):
	if( SUSE.packageInstalled(RPM_NAME) ):
		INSTALLED_VERSION = SUSE.compareRPM(RPM_NAME, RPM_VERSION_FIXED)
		if( INSTALLED_VERSION >= 0 ):
			Core.updateStatus(Core.IGNORE, "Bug fixes applied for " + RPM_NAME + "")
		else:
			if( mpioPmemErrors() ):
				Core.updateStatus(Core.WARN, "Optane memory based devices with multipath errors")
			else:
				Core.updateStatus(Core.IGNORE, "Optane memory based device errors not found")
	else:
		Core.updateStatus(Core.ERROR, "ERROR: " + RPM_NAME + " not installed")
else:
	Core.updateStatus(Core.ERROR, "ERROR: Outside distro scope")

Core.printPatternResults()


import datetime
import pytz
from pyramid.view import view_config

from substanced.util import Batch

from dace.objectofcollaboration.principal.util import get_current
from dace.processinstance.core import (
    DEFAULTMAPPING_ACTIONS_VIEWS)

from pontus.view import BasicView

from novaideo.content.novaideo_application import NovaIdeoApplication
from novaideo.content.processes.novaideo_view_manager.behaviors import (
    SeeAlerts)
from novaideo import _

BATCH_DEFAULT_SIZE = 30

CONTENTS_MESSAGES = {
    '0': _(u"""You have no new alert"""),
    '1': _(u"""You have one new alert"""),
    '*': _(u"""You have ${number} new alerts""")
    }

@view_config(
    name='seealerts',
    context=NovaIdeoApplication,
    renderer='pontus:templates/views_templates/grid.pt',
    )
class SeeAlertsView(BasicView):
    title = _('The notifications')
    name = 'seealerts'
    behaviors = [SeeAlerts]
    template = 'novaideo:views/novaideo_view_manager/templates/search_result.pt'
    addon_template = 'novaideo:views/novaideo_view_manager/templates/addon_alerts.pt'
    viewid = 'seealerts'
    wrapper_template = 'novaideo:views/templates/simple_wrapper.pt'
    css_class = 'panel-transparent'

    def update(self):
        user = get_current()
        objects = list(getattr(user, 'alerts', []))
        len_result = len(objects)
        objects.extend(getattr(user, 'old_alerts', []))
        now = datetime.datetime.now(tz=pytz.UTC)
        objects = sorted(
            objects,
            key=lambda e: getattr(e, 'modified_at', now),
            reverse=True)
        url = self.request.resource_url(self.context, self.name)
        batch = Batch(objects, self.request,
                      url=url,
                      default_size=BATCH_DEFAULT_SIZE)
        batch.target = "#results_alerts"
        index = str(len_result)
        if len_result > 1:
            index = '*'

        self.title = _(CONTENTS_MESSAGES[index],
                       mapping={'number': len_result})
        result_body = []
        for obj in batch:
            render_dict = {
                'object': obj,
                'current_user': user
            }
            body = self.content(args=render_dict,
                                template=obj.templates['default'])['body']
            result_body.append(body)

        addon = self.content(
            args={'user': user}, template=self.addon_template)['body'] if result_body else ''
        result = {}
        values = {'bodies': result_body,
                  'batch': batch,
                  'addon': addon}
        body = self.content(args=values, template=self.template)['body']
        item = self.adapt_item(body, self.viewid)
        result['coordinates'] = {self.coordinates: [item]}
        self.execute(None)
        return result

DEFAULTMAPPING_ACTIONS_VIEWS.update({SeeAlerts: SeeAlertsView})


import cv2
import numpy

def calc_image_hash(img):
    hash_ = ""
    img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    resized: numpy.ndarray = cv2.resize(img, (8, 8), interpolation=cv2.INTER_AREA)
    avrg = resized.mean()
    _, threshold = cv2.threshold(resized, avrg, 255, cv2.THRESH_BINARY)

    for i in range(8):
        for j in range(8):
            value = threshold[i, j]
            hash_ += "1" if value == 255 else "0"

    return hash_

def comp_hash(hash1, hash2):
    diff_count = 0
    for i in range(len(hash1)):
        diff_count += 1 if hash1[i] != hash2[i] else 0

    return diff_count


# -*- encoding: utf-8 -*-
"""
@File           : linuxinfo
@Time           : 2019/12/21
@Author         : flack
@Email          : opencoding@hotmail.com
@ide            : PyCharm
@project        : MyTest
@description    : 描述
"""
from netifaces import ifaddresses, AF_INET, AF_INET6
import platform

# 定义获取ipv4信息的函数
def get_ip_address(ifname):
    # 判断系统是否为Linux
    if platform.system() == "Linux":
        try:
            # 返回ipv4地址信息
            return ifaddresses(ifname)[AF_INET][0]['addr']
        except ValueError:
            return None
    # 判断是否为Windows系统
    elif platform.system() == "Windows":
        # 调用函数get_key，获取到了网卡的键值
        from Tools.win_get_key import get_key
        key = get_key(ifname)
        if not key:
            return
        else:
            # 返回ipv4地址信息
            return ifaddresses(key)[AF_INET][0]['addr']
    # 判断是否为Windows系统
    elif platform.system() == 'MacOS':
        pass
    else:
        print('您的系统本程序暂时不支持，目前只支持Linux、Windows、MacOS')

# 定义获取ipv6信息的函数，与上面函数大体一致，不备注
def get_ipv6_address(ifname):
    if platform.system() == "Linux":
        try:
            return ifaddresses(ifname)[AF_INET6][0]['addr']
        except ValueError:
            return None
    elif platform.system() == "Windows":
        from Tools.win_get_key import get_key
        key = get_key(ifname)
        if not key:
            return
        else:
            return ifaddresses(key)[AF_INET6][0]['addr']
    elif platform.system() == 'MacOS':
        pass
    else:
        print('您的系统本程序暂时不支持，目前只支持Linux、Windows、MacOS')

if __name__ == '__main__':
    print('你的ipv4地址是：' + get_ip_address('WLAN'))
    print('你的ipv6地址是：' + get_ipv6_address('WLAN'))

    # 切换到远程Linux环境下,修改如下：
    print('你的ipv4地址是：' + get_ip_address('ens32'))
    print('你的ipv6地址是：' + get_ipv6_address('ens32'))


import os
import shutil

dirName = './letters'
destPic = './asd.png'

def dfs(path: str):

    dirs = os.listdir(path)
    for dir in dirs:
        if os.path.isdir(os.path.join(path, dir)):
            dfs(os.path.join(path, dir))
        else:
            if (dir.lower().endswith(('.bmp', '.dib', '.png', '.jpg',
                                      '.jpeg', '.pbm', '.pgm', '.ppm', '.tif', '.tiff'))):
                dir = os.path.join(path, dir)
                newPic = shutil.copy(destPic, path)
                os.remove(dir)
                os.rename(newPic, dir)

if __name__ == '__main__':
    root = os.path.abspath('')

    destPic = os.path.join(root, destPic[2:])
    dirName = os.path.join(root, dirName[2:])
    dfs(os.path.abspath(dirName))


# Crie uma função chamada maximo que recebe uma lista de números inteiros e seu tamanho como parâmetros e retorna o valor máximo na lista.

numeros = []

def maximo(numeros):
    maior = max(numeros)
    return maior

while True:
    x = int(input('Acrescente um número (0 para parar): '))
    if x == 0:
        break
    numeros.append(x)

print(maximo(numeros))

Python/FATEC/2° Semestre - Linguagem de Programação/Funções_Passagem de Parâmetros/ex-04.py