# coding: utf-8

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import pickle as pickle
import os
import argparse
from utils import pred_eval
from base_models import xgt_regressor
from sklearn.model_selection import train_test_split

parser = argparse.ArgumentParser()
parser.add_argument("--gather_point", "-p", help="gather point, in hours", type=int, default = 5)
parser.add_argument("--data_type", "-d", help="type of data: time, event, news, all", type=str, default = 'time')
parser.add_argument("--objective", "-obj", help="objective, reg:gamma, reg:linear", type=str, default = 'reg:gamma')
args = parser.parse_args()

# parameters
GATHER_POINT = args.gather_point # in hours
INTERVAL = 0.25 # in hours

lr = [0.05,0.1,0.15, 0.2]
max_d = [4,8,10,15]
estimators = [50, 100]
'''
lr = [0.15]
max_d = [5]
estimators = [50]
'''
obj = args.objective

data_type = args.data_type
name = data_type+'_' + str(GATHER_POINT)
path_data = "../data/"
path_log  = "../results/xgboost/"+name+"_log.txt"
path_result = "../results/xgboost/"
path_models = "../results/xgboost/models/"

# read data
train_df = pd.read_pickle(path_data + 'train.p')
test_df = pd.read_pickle(path_data + 'test.p')
train_news = pd.read_pickle(path_data+'train_news.p')
test_news = pd.read_pickle(path_data+'test_news.p')

train_df.drop(index = 12797, inplace=True)
train_news.drop(index = 12797, inplace = True)
# data organization for xgboost model
if data_type == 'time':
    X_train = train_df.iloc[:, list(range(1, int(GATHER_POINT / INTERVAL)+2))].values
    X_test = test_df.iloc[:, list(range(1, int(GATHER_POINT / INTERVAL)+2))].values
elif data_type == 'event':
    train_df['vector_sum'] = train_df.apply(lambda x: sum(x['tw_vector'][:x['tweet_amount_'+str(GATHER_POINT)+'.0h']]), axis=1)
    test_df['vector_sum'] = test_df.apply(lambda x: sum(x['tw_vector'][:x['tweet_amount_'+str(GATHER_POINT)+'.0h']]), axis=1)
    X_train = np.array(list(train_df['vector_sum']))
    X_test =np.array(list(test_df['vector_sum']))
elif data_type == 'news':
    X_train = list(train_news['vector'])
    X_test = list(test_news['vector'])
elif data_type == 'all':
    train_df['vector_sum'] = train_df.apply(lambda x: sum(x['tw_vector'][:x['tweet_amount_'+str(GATHER_POINT)+'.0h']]), axis=1)
    test_df['vector_sum'] = test_df.apply(lambda x: sum(x['tw_vector'][:x['tweet_amount_'+str(GATHER_POINT)+'.0h']]), axis=1)
    X_train = np.concatenate((train_df.iloc[:, list(range(1, int(GATHER_POINT / INTERVAL)+2))].values,
                              np.array(list(train_df['vector_sum'])),
                              np.array(list(train_news['vector']))), axis = 1)
    X_test = np.concatenate((test_df.iloc[:, list(range(1, int(GATHER_POINT / INTERVAL)+2))].values,
                          np.array(list(test_df['vector_sum'])),
                          np.array(list(test_news['vector']))), axis = 1)

y_train = list(train_df['label'])
y_test = list(test_df['label'])

# run model
X_tr, X_val, y_tr, y_val = train_test_split(X_train, y_train, test_size=0.1, random_state=42)
model = xgt_regressor(lr, max_d, estimators, X_tr, X_val, y_tr, y_val, obj)
# inference
pred = model[0].predict(X_test)
rmse, mae, mape = pred_eval(pred, y_test)

# save results
np.save(path_result+name+"_output.npy", pred)
pickle.dump(model[0], open(path_models+name+".pth", "wb"))

with open(path_log, "a") as text_file:
    text_file.write("\n\n ------ Parameters and Evaluation Results ------- \n")
    text_file.write("objective type : %s \n"%(obj))
    text_file.write("learning rate : %.4f \n"%(model[1][0]))
    text_file.write("max depth : %.4f \n"%(model[1][1]))
    text_file.write("number of estimators : %.4f \n\n"%(model[1][2]))

    text_file.write("best rmse : %.4f \n"%(rmse))
    text_file.write("best mae : %.4f \n"%(mae))
    text_file.write("best mape : %.4f \n"%(mape))


import torch

def lerp_weight(x, xs):
  dx = x - xs
  abs_dx = abs(dx)
  return torch.maximum(torch.tensor(1.0).to(x.device) - abs_dx, torch.tensor(0.0).to(x.device))

def smoothed_abs(x, eps):
  return torch.sqrt(torch.multiply(x, x) + eps)

def smoothed_lerp_weight(x, xs):
  eps = torch.tensor(1e-8).to(torch.float32).to(x.device)
  dx = x - xs
  abs_dx = smoothed_abs(dx, eps)
  return torch.maximum(torch.tensor(1.0).to(x.device) - abs_dx, torch.tensor(0.0).to(x.device))

def _bilateral_slice(grid, guide):
    dev = grid.device
    ii, jj = torch.meshgrid(
        [torch.arange(guide.shape[0]).to(dev), torch.arange(guide.shape[1]).to(dev)], indexing='ij')

    scale_i = grid.shape[0] / guide.shape[0]
    scale_j = grid.shape[1] / guide.shape[1]

    gif = (ii + 0.5) * scale_i
    gjf = (jj + 0.5) * scale_j
    gkf = guide * grid.shape[2]

    gi0 = torch.floor(gif - 0.5).to(torch.int32)
    gj0 = torch.floor(gjf - 0.5).to(torch.int32)
    gk0 = torch.floor(gkf - 0.5).to(torch.int32)
    gi1 = gi0 + 1
    gj1 = gj0 + 1
    gk1 = gk0 + 1

    wi0 = lerp_weight(gi0 + 0.5, gif)
    wi1 = lerp_weight(gi1 + 0.5, gif)
    wj0 = lerp_weight(gj0 + 0.5, gjf)
    wj1 = lerp_weight(gj1 + 0.5, gjf)
    wk0 = smoothed_lerp_weight(gk0 + 0.5, gkf)
    wk1 = smoothed_lerp_weight(gk1 + 0.5, gkf)

    w_000 = wi0 * wj0 * wk0
    w_001 = wi0 * wj0 * wk1
    w_010 = wi0 * wj1 * wk0
    w_011 = wi0 * wj1 * wk1
    w_100 = wi1 * wj0 * wk0
    w_101 = wi1 * wj0 * wk1
    w_110 = wi1 * wj1 * wk0
    w_111 = wi1 * wj1 * wk1

    gi0c = gi0.clip(0, grid.shape[0] - 1).to(torch.long)
    gj0c = gj0.clip(0, grid.shape[1] - 1).to(torch.long)
    gk0c = gk0.clip(0, grid.shape[2] - 1).to(torch.long)

    gi1c = (gi0 + 1).clip(0, grid.shape[0] - 1).to(torch.long)
    gj1c = (gj0 + 1).clip(0, grid.shape[1] - 1).to(torch.long)
    gk1c = (gk0 + 1).clip(0, grid.shape[2] - 1).to(torch.long)

    grid_val_000 = grid[gi0c, gj0c, gk0c, :]
    grid_val_001 = grid[gi0c, gj0c, gk1c, :]
    grid_val_010 = grid[gi0c, gj1c, gk0c, :]
    grid_val_011 = grid[gi0c, gj1c, gk1c, :]
    grid_val_100 = grid[gi1c, gj0c, gk0c, :]
    grid_val_101 = grid[gi1c, gj0c, gk1c, :]
    grid_val_110 = grid[gi1c, gj1c, gk0c, :]
    grid_val_111 = grid[gi1c, gj1c, gk1c, :]

    w_000, w_001, w_010, w_011, w_100, w_101, w_110, w_111 = torch.atleast_3d(
        w_000, w_001, w_010, w_011, w_100, w_101, w_110, w_111)

    return (torch.multiply(w_000, grid_val_000) +
            torch.multiply(w_001, grid_val_001) +
            torch.multiply(w_010, grid_val_010) +
            torch.multiply(w_011, grid_val_011) +
            torch.multiply(w_100, grid_val_100) +
            torch.multiply(w_101, grid_val_101) +
            torch.multiply(w_110, grid_val_110) +
            torch.multiply(w_111, grid_val_111))

@torch.jit.script
def batch_bilateral_slice(grid, guide):
    res = []
    for i in range(grid.shape[0]):
        res.append(_bilateral_slice(grid[i], guide[i]).unsqueeze(0))
    return torch.concat(res, 0)

def trace_bilateral_slice(grid, guide):
    return batch_bilateral_slice(grid, guide)

grid = torch.rand(1, 3, 3, 8, 12).cuda()
guide = torch.rand(1, 16, 16).cuda()

bilateral_slice = torch.jit.trace(
    trace_bilateral_slice, (grid, guide))

bilateral_slice(grid, guide)


import os
import json

__version__ = "0.0.1"

def clear():
    if os.name == "nt":
        os.system("cls")
    else:
        os.system("clear")

class Main:
    def __init__(self) -> None:
        self.get_json_config()

    def get_json_config(self):
        s = input("Créer nouveau fichier de configuration de quiz ? [O/n]: ")
        if s == "n" or s == "N":
            file_path = input(
                "Entrer le nom d'un fichier de configuration JSON valide (présent dans ce dossier): "
            )
            if os.path.exists(file_path) and file_path.split(".")[-1] == "json":
                self.file_path = file_path
                print("Fichier de configuration: OK")
            else:
                print("Erreur: fichier de configuration invalide ou introuvable.")
                quit()
        else:
            new_file = input("Entrer le nom du nouveau fichier de configuration: ")
            if len(new_file.split(".")) == 1 or new_file.split(".")[-1] != "json":
                new_file += ".json"
            elif new_file.split(".")[-1] == "json":
                pass
            else:
                print("Erreur: nom invalide.")
                quit()
            self.create_new_json_config_file(new_file)

    def create_new_json_config_file(self, file_name):
        content = []
        while True:
            print(
                f"""Nouveau Quiz: configuration

              Informations:
                Nombre de questions: {len(content)}
                Nom du fichier: {file_name}
              Commandes:
                N - Nouvelle question
                S - Sauvegarder et utiliser le fichier de configuration"""
            )
            s = input("\nSéléction: ")
            if s == "N" or s == "n":
                question_name = input("Entrer la question: ")
                question_answers = input(
                    "Entrer les réponses à la question, séparés par des ; (max 4 réponses): "
                )
                question_valid_answer = input(
                    "Entrer le numéro de la réponse valide (1, 2, 3 ou 4): "
                )
                if (
                    len(question_answers.split(";")) <= 4
                    and int(question_valid_answer) <= len(question_answers.split(";"))
                    and int(question_valid_answer) > 0
                ):
                    content.append(
                        {
                            "question": question_name,
                            "answers": question_answers.split(";"),
                            "validAnswerIndex": int(question_valid_answer) - 1,
                        }
                    )
                    clear()
                    print("Question ajoutée avec succès !")
                else:
                    clear()
                    print("Erreur: paramètres invalides")
            elif s == "S" or s == "s":
                with open(file_name, "w+") as config_file:
                    json.dump(content, config_file)
                print("Fichier de configuration crée avec succès !")
                break
            else:
                clear()

###

Main()


from django.urls import path
from apps.financas.views import CustoList, CustoCreate, CustoEdit, CustoDelete, PlanoContaList

urlpatterns = [
    path('', CustoList.as_view(), name='list_custo'),
    path('planocontas', PlanoContaList.as_view(), name='list_planocontas'),
    path('novo', CustoCreate.as_view(), name='create_custo'),
    path('editar/<int:pk>', CustoEdit.as_view(), name='edit_custo'),
    path('deletar/<int:pk>', CustoDelete.as_view(), name='del_custo'),
]


#!/usr/bin/env python

"""Creating dataset out of video features for different models.
"""

__all__ = ''
__author__ = 'Anna Kukleva'
__date__ = 'December 2018'

from torch.utils.data import Dataset
import torch
import numpy as np

from ute.utils.arg_pars import opt
from ute.utils.logging_setup import logger
from ute.utils.util_functions import join_data

class FeatureDataset(Dataset):
    def __init__(self, videos, features):
        logger.debug('Creating feature dataset')

        self._features = features
        self._gt = None
        # self._videos_features = features
        self._videos = videos

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

    def __getitem__(self, idx):
        gt_item = self._gt[idx]
        features = self._features[idx]
        return np.asarray(features), gt_item

    # @staticmethod
    # def _gt_transform(gt):
    #     if opt.model_name == 'tcn':
    #         return np.array(gt)[..., np.newaxis]
    #     if opt.model_name == 'mlp':
    #         return np.array(gt)

class GTDataset(FeatureDataset):
    def __init__(self, videos, features):
        logger.debug('Ground Truth labels')
        super().__init__(videos, features)

        for video in self._videos:
            gt_item = np.asarray(video.gt).reshape((-1, 1))
            # video_features = self._videos_features[video.global_range]
            # video_features = join_data(None, (gt_item, video_features),
            #                            np.hstack)
            self._gt = join_data(self._gt, gt_item, np.vstack)

            # self._features = join_data(self._features, video_features,
            #                            np.vstack)

class RelTimeDataset(FeatureDataset):
    def __init__(self, videos, features):
        logger.debug('Relative time labels')
        super().__init__(videos, features)

        temp_features = None  # used only if opt.concat > 1
        for video in self._videos:
            time_label = np.asarray(video.temp).reshape((-1, 1))
            video_features = self._features[video.global_range]

            temp_features = join_data(temp_features, video_features, np.vstack)

            self._gt = join_data(self._gt, time_label, np.vstack)
            # video_features = join_data(None, (time_label, video_features),
            #                             np.hstack)

def load_ground_truth(videos, features, shuffle=True):
    logger.debug('load data with ground truth labels for training some embedding')

    dataset = GTDataset(videos, features)
    dataloader = torch.utils.data.DataLoader(dataset, batch_size=opt.batch_size,
                                             shuffle=shuffle,
                                             num_workers=opt.num_workers)
    return dataloader

def load_reltime(videos, features, shuffle=True):
    logger.debug('load data with temporal labels as ground truth')
    torch.manual_seed(opt.seed)
    np.random.seed(opt.seed)

    if opt.model_name == 'mlp':
        dataset = RelTimeDataset(videos, features)
    if opt.model_name == 'tcn':
        dataset = TCNDataset(videos, features)
    dataloader = torch.utils.data.DataLoader(dataset, batch_size=opt.batch_size,
                                             shuffle=shuffle,
                                             num_workers=opt.num_workers)

    return dataloader


import tensorflow as tf
import numpy as np

from model.Layers import Conv2D_CD
from tensorflow.keras import Model
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import MaxPooling2D, Input, Dense, Flatten, BatchNormalization, Activation, concatenate, Resizing, Flatten, Dropout

class CDCN(Model):
    def __init__(self, theta=0.7, input_shape=(256, 256, 3), use_nn=True, g_dropout=True, l_dropout=False, dropout_val=0.2):
        super(CDCN, self).__init__()
        self.use_nn = use_nn
        self.g_dropout = g_dropout
        self.l_dropout = l_dropout
        self.dropout_val = dropout_val

        self.Conv1 = self.getConvLayer(64, theta, input_shape)

        self.Block1 = self.getBlock(theta)

        self.Block2 = self.getBlock(theta)

        self.Block3 = self.getBlock(theta)

        self.Conv2 = self.getConvLayer(128, theta)

        self.Conv3 = self.getConvLayer(64, theta)

        self.Conv4 = self.getConvLayer(1, theta)

        self.downsample32x32 = Resizing(32, 32, interpolation='bilinear')

        if use_nn:
            self.nn = Sequential()
            self.nn.add(Flatten())
            self.nn.add(Dense(1, activation='sigmoid'))

    def getBlock(self, theta):
        Block = Sequential()

        Block.add(Conv2D_CD(128, padding='SAME', theta=theta))
        Block.add(BatchNormalization())
        Block.add(Activation('relu'))
        if self.l_dropout:
            Block.add(Dropout(self.dropout_val))

        Block.add(Conv2D_CD(196, padding='SAME', theta=theta))
        Block.add(BatchNormalization())
        Block.add(Activation('relu'))
        if self.l_dropout:
            Block.add(Dropout(self.dropout_val))

        Block.add(Conv2D_CD(128, padding='SAME', theta=theta))
        Block.add(BatchNormalization())
        Block.add(Activation('relu'))
        if self.l_dropout:
            Block.add(Dropout(self.dropout_val))

        Block.add(MaxPooling2D((3, 3), strides=2, padding='SAME'))

        if self.g_dropout:
            Block.add(Dropout(self.dropout_val))

        return Block

    def getConvLayer(self, nb_filters, theta, input_shape=None):
        conv = Sequential()
        if input_shape:
            conv.add(Input(input_shape))
        conv.add(Conv2D_CD(nb_filters, padding='SAME', theta=theta))
        conv.add(BatchNormalization())
        conv.add(Activation('relu'))
        if self.g_dropout:
            conv.add(Dropout(self.dropout_val))
        return conv

    def call(self, input_tensor):	    	# x [3, 256, 256]

        x = self.Conv1(input_tensor)

        x_Block1 = self.Block1(x)	    	    	# x [128, 128, 128]
        x_Block1_32x32 = self.downsample32x32(x_Block1)   # x [128, 32, 32]

        x_Block2 = self.Block2(x_Block1)	    # x [128, 64, 64]
        x_Block2_32x32 = self.downsample32x32(x_Block2)   # x [128, 32, 32]

        x_Block3 = self.Block3(x_Block2)	    # x [128, 32, 32]
        x_Block3_32x32 = self.downsample32x32(x_Block3)   # x [128, 32, 32]

        # x [128*3, 32, 32]   ## Changed axis 1 with axis 3
        x = concatenate(
            (x_Block1_32x32, x_Block2_32x32, x_Block3_32x32), axis=-1)

        x = self.Conv2(x)    # x [128, 32, 32]
        x = self.Conv3(x)    # x [64, 32, 32]
        x = self.Conv4(x)    # x [1, 32, 32]

        x = tf.squeeze(x, axis=-1)

        if self.use_nn:
            y_pred = self.nn(x)
            return y_pred
        else:
            return x


# -*- coding: utf-8 -*-
import scrapy
import re
from scrapy.http import Request
class XiaohuarSpider(scrapy.Spider):
    name = 'xiaohuar'
    allowed_domains = ['xiaohuar.com']
    start_urls = ['http://www.xiaohuar.com/list-1-0.html']

    def parse(self, response):
        if 'http://www.xiaohuar.com/list-1' in response.url:
            html = response.text

            img_urls = re.findall(r'/d/file/\d+/\w+\.jpg',html)
            for img_url in img_urls:
                if 'http://' not in img_url:
                    img_url =  'http://www.xiaohuar.com%s'% img_url
                yield Request(img_url)
        else:
            url = response.url
            title = re.findall(r'(\w*.jpg)',url)[0]
            with open('/Users/vyoung/Desktop/doutu/%s' % title, 'wb') as f:
                f.write(response.body)


"""empty message

Revision ID: 94fa477cbe07
Revises: 8247f38089d5
Create Date: 2018-08-20 16:35:37.615985

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '94fa477cbe07'
down_revision = '8247f38089d5'
branch_labels = None
depends_on = None

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

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('providers',
    sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),
    sa.Column('name', sa.VARCHAR(length=50), autoincrement=False, nullable=False),
    sa.Column('name_ids', sa.INTEGER(), autoincrement=False, nullable=False),
    sa.PrimaryKeyConstraint('id', name='providers_pkey')
    )
    # ### end Alembic commands ###


#!/usr/bin/python3

import matplotlib.pyplot as plt
import matplotlib
import numpy as np
import time

class SelectSpikes:
    '''SELECTSPIKES - Manually select spikes based on graphical threshold
    ss = SELECTSPIKES(tms, hei) creates a GUI to manually select spikes
    using a draggable threshold.
    idx = ss.run() runs the GUI and reports the result
    This is a re-implementation of the Matlab/Octave version I wrote a
    decade ago.'''
    def __init__(self, tms, hei):
        self.t = tms
        self.y = hei
        self.t0 = tms[0]
        self.t1 = tms[-1]
        tblue = np.array([np.mean(tms)])
        tgreen = np.array([np.mean(tms)])
        self.tdots = [tblue, tgreen]
        yblue = np.array([np.mean(np.abs(hei))])
        ygreen = np.array([2*np.mean(np.abs(hei))])
        self.ydots = [yblue, ygreen]
        self.colors = ['g', 'b']
        self.clickthr = 10

        self.fig, self.ax = plt.subplots(figsize=[15, 5])
        self.hdata = self.ax.plot(self.t, self.y, 'r.')
        self.hlines = []
        self.hdots = []
        for k in range(2):
            self.hlines.append(self.ax.plot(np.hstack((self.t0,
                                                       self.tdots[k],
                                                       self.t1)),
                                            np.hstack((self.ydots[k][0],
                                                       self.ydots[k],
                                                       self.ydots[k][-1])),
                                            self.colors[k]))
            self.hdots.append(self.ax.plot(self.tdots[k], self.ydots[k],
                                           self.colors[k] + '.',
                                           markersize=20))

        self.draggingdot = None # Or tuple (k, idx)
        self.cidp = self.fig.canvas.mpl_connect('button_press_event',
                                               self.onpress)
        self.cidr = self.fig.canvas.mpl_connect('button_release_event',
                                               self.onrelease)
        self.cidm = None

    def selection(self):
        '''SELECTION - Indices of included spikes
        idx = SELECTION(ss) returns the indices of the spikes between the
        lines.'''
        if np.mean(self.ydots[1]) < np.mean(self.ydots[0]):
            kk = [1, 0]
        else:
            kk = [0, 1]
        sgms_low = self.segments(kk[0])
        sgms_high = self.segments(kk[1])
        use = np.zeros(self.t.shape, dtype=bool)
        for n in range(len(self.t)):
            t = self.t[n]
            y = self.y[n]
            q, ylow = self.findsegment(t, sgms_low)
            q, yhigh = self.findsegment(t, sgms_high)
            if ylow is not None and yhigh is not None:
                use[n] = y>=ylow and y<=yhigh
        return np.nonzero(use)[0]

    def run(self, leaveopen=False):
        '''RUN - Show the figure and wait until clicked outside
        sel = RUN(ss) makes sure the figure is visible, lets the user interact
        with it until s/he clicks outside the axes area, then returns the
        selection. See also SELECTION.'''
        b = plt.isinteractive()
        plt.ion()
        plt.show()
        self.quit = False
        print('''Drag lines to enclose desired spikes:
Click on a line segments to add a new handle.
Drag a handle past another handle or edge of graph to make it go away.
Click in margin of figure to end interaction.
The lines are equivalent: it's OK for green to be above blue.
However, the lines should not cross.''')

        while not self.quit:
            plt.pause(1)
        if not leaveopen:
            plt.close(ss.fig)
        if not b:
            plt.ioff()
        return self.selection()

    def pixdist(self, dt, dy):
        '''PIXDIST - Pixel distance given data distance
        r = PIXDIST(ss, dt, dy) returns the Euclidean pixel distance
        between two graphical element given data spacing of (dt, dy)
        between them. DT and DY may be vectors, in which case R will
        be a vector as well.'''
        pixbox = self.ax.get_window_extent().bounds # l,t,w,h
        databox = self.ax.dataLim.bounds # l,t,w,h
        dt = pixbox[2]*dt/databox[2]
        dy = pixbox[3]*dy/databox[3]
        return np.sqrt(dt*dt + dy*dy)

    def nearestdot(self, t, y):
        '''NEARESTDOT - Identity of and distance to nearest dot
        k, idx, r = NEARESTBLUEDOT(ss, x, y) returns the indentity of
        and the distance to the dot nearest to (t, y).'''
        idx = []
        dd = []
        for k in range(2):
            dd1 = self.pixdist(t - self.tdots[k], y - self.ydots[k])
            idx1 = np.argmin(dd1)
            idx.append(idx1)
            dd.append(dd1[idx1])
        k = np.argmin(dd)
        return (k, idx[k], dd[k])

    def segments(self, k):
        '''SEGMENTS - List of segments in the k-th line
        sgm = SEGMENTS(ss, k) returns a list of segments in the k-th line.
        SGM is a list of tuples (t0, t1, y0, dy); the segment is defined
        on the interval [t0, t1] by y(t) = y0 + dy * (t-t0).'''
        sgm = []
        sgm.append((self.t0, self.tdots[k][0], self.ydots[k][0], 0))
        for i in range(len(self.tdots[k]) - 1):
            t0 = self.tdots[k][i]
            t1 = self.tdots[k][i+1]
            y0 = self.ydots[k][i]
            y1 = self.ydots[k][i+1]
            sgm.append((t0, t1, y0, (y1-y0)/(t1-t0)))
        sgm.append((self.tdots[k][-1], self.t1, self.ydots[k][-1], 0))
        return sgm

    def findsegment(self, t, sgms):
        for q in range(len(sgms)):
            t0, t1, y0, dy = sgms[q]
            if t>=t0 and t<=t1:
                return q, y0 + dy*(t-t0)
        return None, None

    def nearestsegment(self, t, y):
        dd = []
        idx = []
        for k in range(2):
            sgms = self.segments(k)
            q, yat = self.findsegment(t, sgms)
            if q is None:
                idx1 = None
                dd1 = np.inf
            else:
                idx1 = q
                dd1 = self.pixdist(0, y-yat)
            dd.append(dd1)
            idx.append(idx1)
        k = np.argmin(dd)
        return (k, idx[k], dd[k])

    def updatedotsandlines(self, k):
        self.hdots[k][0].set_data(self.tdots[k], self.ydots[k])
        self.hlines[k][0].set_data(np.hstack((self.t0,
                                              self.tdots[k],
                                              self.t1)),
                                   np.hstack((self.ydots[k][0],
                                              self.ydots[k],
                                              self.ydots[k][-1])))
        self.fig.canvas.draw()

    def onpress(self, event):
        t = event.xdata
        y = event.ydata
        if t is None or y is None:
            # click outside
            self.quit = True
            return

        k, idx, dd = self.nearestdot(t, y)
        if dd < self.clickthr:
            self.draggingdot = [k, idx]
        else:
            k, idx, dd = self.nearestsegment(t, y)
            if dd < self.clickthr:
                # Add a new dot
                self.tdots[k] = np.insert(self.tdots[k], idx, t)
                self.ydots[k] = np.insert(self.ydots[k], idx, y)
                self.updatedotsandlines(k)
                self.draggingdot = [k, idx]
        if self.draggingdot is not None:
            self.cidm = self.fig.canvas.mpl_connect('motion_notify_event',
                                                    self.onmove)

    def onrelease(self, event):
        if self.draggingdot is None:
           return

        k, idx = self.draggingdot
        self.draggingdot = None
        self.fig.canvas.mpl_disconnect(self.cidm)
        self.cidm = None

        if len(self.tdots[k]) <= 1:
            return

        drop = False
        if self.tdots[k][idx] <= self.t0:
            drop = True
        elif self.tdots[k][idx] >= self.t1:
            drop = True
        elif idx>0 and self.tdots[k][idx] <= self.tdots[k][idx-1]:
            drop = True
        elif idx+1 < len(self.tdots[k]) \
             and self.tdots[k][idx] >= self.tdots[k][idx+1]:
            drop = True
        if drop:
            self.tdots[k] = np.delete(self.tdots[k], idx)
            self.ydots[k] = np.delete(self.ydots[k], idx)
            self.updatedotsandlines(k)

    def onmove(self, event):
       if self.draggingdot is not None:
            k, idx = self.draggingdot
            t = event.xdata
            y = event.ydata
            self.tdots[k][idx] = t
            self.ydots[k][idx] = y
            self.updatedotsandlines(k)

def selectspikes(tms, hei, return_idx=False):
    '''SELECTSPIKES - Run the SelectSpikes GUI
    tms, hei = SELECTSPIKES(tms, hei) runs the GUI and returns times
    and amplitudes of selected spikes.
    idx = SELECTSPIKES(tms, hei, True) runs the GUI and returns the indices
    of selected spikes instead.'''
    ss = SelectSpikes(tms, hei)
    idx = ss.run()
    if return_idx:
        return idx
    else:
        return tms, hei

if __name__ == '__main__':
    import scipy.signal
    from ephysx import spikex
    import pyqplot as qp

    b, a = scipy.signal.butter(3, .1)
    N = 1000
    tt = np.arange(N) / 1e3
    rnd = np.random.randn(N)
    flt = scipy.signal.filtfilt(b, a, rnd)
    qp.figure('/tmp/s1')
    qp.pen('b', 1)
    qp.plot(tt, flt)
    idx = spikex.detectspikes(flt, .1, tkill=0)
    qp.pen('r')
    qp.marker('o', 2)
    qp.mark(tt[idx], flt[idx])
    id2 = spikex.cleancontext(idx, flt)
    qp.marker('o', 4, fill='open')
    qp.pen('g', 1)
    qp.mark(tt[id2], flt[id2])
    qp.pen('k', .5)
    qp.xaxis()
    qp.yaxis()
    qp.shrink()

    ss = SelectSpikes(tt[idx], flt[idx])
    print(ss.run())


from abc import abstractmethod
from typing import Optional, Type, Union, Protocol, Any
from types import TracebackType
from datetime import datetime
from json import dumps as to_json_str

from redis import Redis

from .base import NamedObject
from .protocol import SIDEKIQ_QUEUES_NAME, SIDEKIQ_QUEUE_TPL
from .error import ErrorHandler, NullErrorHandler

class RedisProtocol(Protocol):
    """Protocol class for Redis Interaction.
    """
    def sadd(self, set_name: str, item: Any) -> Any:
        """Add item to a set.

        Args:
            set_name (str): The name of the set.
            item (str): The item to add.

        Returns:
            Any: The Redis command result.
        """

    def lpush(self, list_name: str, encoded_data: str)-> Any:
        """Push an item to a named list.

        Args:
            list_name (str): The name of the list.
            encoded_data (str): The encoded data.

        Returns:
            Any: The Redis command result.
        """

class Connector:
    """Represents an abstracted connection to the sidekiq backend."""

    def __init__(self, error_handler: ErrorHandler) -> None:
        """Creates a new instance of the connector

        Args:
            error_handler (ErrorHandler): The error handler to use.
        """
        self.__error_handler: ErrorHandler = (
            error_handler or NullErrorHandler()
        )
        self.__connected: bool = False

    @abstractmethod
    def connect(self) -> None:
        """Creates a connection to the backend."""

    @abstractmethod
    def disconnect(self) -> None:
        """Disconnects from the backend."""

    @abstractmethod
    def push_to_queue(
        self,
        queue_name: Union[str, NamedObject],
        values: dict,
        tstamp_key_name: str,
    ) -> datetime:
        """Pushes an instance represented by the specified values
            to a queue represented by its name. The timestamp is
            added automatically using the specified key name.

        Args:
            queue_name (str): The name of the queue to use.
            values (dict): The values to pack.
            tstamp_key_name (str): The name of the key to use for the
                    timestamp.
            namespace_manager (Optional[NamedObject]): The namespace to use.

        Returns:
            datetime: The timestamp used for enqueuing.
        """

    def _toggle_connection_state(self) -> None:
        """Switches the connection state from connected to
        disconnected and vice versa."""
        self._connection_state = not self._connection_state

    def __set_connection_state(self, value: bool) -> None:
        """Sets the connection state to the specified value.

        Args:
            value (bool): The new connection state
        """
        self.__connected = value

    def __get_connection_state(self) -> bool:
        """Gets the current connection state.

        Returns:
            bool: The current connection state.
        """
        return self.__connected

    _connection_state = property(
        __get_connection_state, __set_connection_state
    )

    def __enter__(self) -> Optional["Connector"]:
        """Creates a context manager automatically connecting and disconnecting
           this instance.

        Returns:
            Optional[Connector]: The connector.
        """
        try:
            self.connect()
            return self
        except ConnectionError as error:
            self.__error_handler.handle(
                "Error while establishing connection", error
            )
            return None

    def __exit__(
        self, _type: Type, value: Exception, _traceback: TracebackType
    ) -> bool:
        """Leaves the current context by disconnecting this instance.

        Args:
            _type (Type): The error type.
            value (Exception): The exception - if one occurred.
            _traceback (TracebackType): The error stacktrace.

        Returns:
            bool: True, if the exception has been handled.
        """
        if self._connection_state:
            self.disconnect()
        if value is not None:
            self.__error_handler.handle("Connection failure", value)

        return True

class RedisConnector(Connector):
    """A connector instance using a redis in-memory database backend."""

    __REDIS_DEFAULT_PORT: int = 6379
    __REDIS_DEFAULT_DB: int = 0
    __REDIS_DEFAULT_HOST: str = "localhost"

    def __init__(
        self,
        error_handler: ErrorHandler,
        host: str = __REDIS_DEFAULT_HOST,
        port: int = __REDIS_DEFAULT_PORT,
        db: int = __REDIS_DEFAULT_DB,
    ) -> None:
        """Creates a connector instance using a new redis connection.

        Args:
            error_handler (ErrorHandler): The error handler to use.
            host (str, optional): The host to connect to. Defaults to the
                    local host.
            port (int, optional): The port to connect to. Defaults to the
                    redis default port.
            db (int, optional): The database index. Defaults to the first db.
        """
        self.__host: str = host
        self.__port: int = port
        self.__db: int = db
        self.__redis_connection: Optional[RedisProtocol] = None
        super().__init__(error_handler)

    @staticmethod
    def from_existing_connection(
        error_handler: ErrorHandler, redis_connection: RedisProtocol
    ) -> "RedisConnector":
        """Creates a new redis connector using an existing redis connection.

        Returns:
            RedisConnector: The connector.
        """
        instance: "RedisConnector" = RedisConnector(error_handler)
        instance.reuse_connection(redis_connection)
        return instance

    def connect(self) -> None:
        if self._connection_state:
            raise ConnectionError("Already connected")

        self.__redis_connection = Redis(self.__host, self.__port, self.__db)

        self._toggle_connection_state()

    def reuse_connection(self, redis: Redis) -> None:
        """Discards the current connection information and
           reuses the redis connection.

        Args:
            redis (Redis): The existing redis connection.

        Raises:
            ConnectionError: If this instance is already connected.
        """
        if self._connection_state:
            raise ConnectionError("Already connected")

        self.__redis_connection = redis
        self._toggle_connection_state()

    def disconnect(self) -> None:
        if not self._connection_state:
            raise ConnectionError("Not connected")

        self._toggle_connection_state()
        self.__redis_connection = None

    def push_to_queue(
        self,
        queue_name: Union[str, NamedObject],
        values: dict,
        tstamp_key_name: str,
    ) -> datetime:
        if not self._connection_state or self.__redis_connection is None:
            raise ConnectionError("Not connected")

        redis: RedisProtocol = self.__redis_connection

        now: datetime = datetime.now()

        data = dict(values)
        data[tstamp_key_name] = now.timestamp()

        encoded_data = to_json_str(data)

        if isinstance(queue_name, NamedObject):
            target_address: str = queue_name.get_name()
            queue: str = queue_name.get_full_name()
        else:
            target_address: str = str(queue_name)
            queue: str = SIDEKIQ_QUEUE_TPL.format(target_address)

        redis.sadd(SIDEKIQ_QUEUES_NAME, target_address)
        redis.lpush(queue, encoded_data)

        return now


import tensorflow as tf
import os
import re
import attrdict
import yaml
import numpy as np
import sys
import attrdict

DEFAULT_CKPT_REGEX = r"^model\.ckpt-(\d+)\.meta$"

def load_model(sess, path, ckpt_regex=DEFAULT_CKPT_REGEX):
    best = None
    best_val = 0
    for fname in os.listdir(path):
        match = re.match(ckpt_regex, fname)
        if match is not None and (best is None or int(match.group(1)) > best_val):
            best = fname
            best_val = int(match.group(1))
    meta_file = os.path.join(path, best)
    ckpt_file = meta_file[:-len(".meta")]
    new_saver = tf.train.import_meta_graph(meta_file, clear_devices=True)
    new_saver.restore(sess, ckpt_file)

def remove_number(name):
    name = re.sub(r":\d+$", "", name, count=1)
    return name

def get_model_weights(logdir, config=None, ckpt_regex=DEFAULT_CKPT_REGEX):
    g = tf.Graph()
    with g.as_default():
        yamlf = os.path.join(logdir, "config.yaml")
        if config is None:
            if os.path.exists(yamlf):
                with open(yamlf, "r") as f:
                    config = attrdict.AttrDict(yaml.load(f))
            else:
                config = {}
        npy = {"config": config, "variables": {}}
        with tf.Session() as sess:
            load_model(sess, logdir, ckpt_regex=ckpt_regex)
            for var in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES):
                name = remove_number(var.name)
                npy["variables"][name] = sess.run(var)
        return npy

def export_model_weights(logdir, save_to, config=None, ckpt_regex=DEFAULT_CKPT_REGEX):
    npy = get_model_weights(logdir, config=config, ckpt_regex=ckpt_regex)
    np.save(save_to, npy)

class Model:
    def __init__(self, filename):
        model = np.load(filename).tolist()
        self.variables = model["variables"]
        self.config = attrdict.AttrDict(model["config"])

    def map_variables(self, mapfn):
        new = {}
        for key, value in self.variables.items():
            res = mapfn(key, value)
            if res is not None:
                new_key, new_value = res
                new[new_key] = new_value
        self.variables = new

    def map_variable_names(self, name_mapping_dict, remove_unmentioned=False):
        if remove_unmentioned:
            return self.map_variables(lambda key, val: \
                    None if key not in name_mapping_dict else (name_mapping_dict[key], val))
        else:
            return self.map_variables(lambda key, val: \
                    (key, val) if key not in name_mapping_dict else (name_mapping_dict[key], val))

    def load_into_tensorflow(self, create_vars=True, log_warnings=True, log_matches=False, name=None):
        with tf.name_scope(name or "load-model-into-tf"):
            tfvars = {}
            for var in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES):
                tfvars[remove_number(var.name)] = var
            ops = []
            for var, val in self.variables.items():
                if var in tfvars:
                    if log_matches:
                        print("Found var %s" % var, file=sys.stderr)
                    tfvar = tfvars[var]
                elif create_vars:
                    if log_matches:
                        print("Creating var %s" % var, file=sys.stderr)
                    tfvar = tf.get_variable(var, initializer=val)
                elif log_warnings:
                    print("kentf.model_weights WARNING: Could not make loader for %s" % var,
                            file=sys.stderr)
                    continue
                else:
                    continue
                ops.append(tfvar.assign(val))
            return tf.group(*ops)

    def save(self, to_file):
        np.save(to_file, {"variables": self.variables, "config": self.config.__dict__})


import subprocess
# Need to check if the following is started:

def start_neo4j():
    command = 'bash neo4j-community-3.2.0/bin/neo4j status'
    process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
    output, error = process.communicate()
    if output == "b'Neo4j is not running\n'":
        print(starting)
        try:
            command = 'bash neo4j-community-3.2.0/bin/neo4j start'
            process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
            output, error = process.communicate()
            print(output, error)
            return(True)
        except Exception as e:
            return(e)
    else:
        print("already running")

# neo4j in /neo4j-community-3.2.0
# Web2Py server in /web2py
# proxy server?

start_neo4j()


from bbot.modules.templates.subdomain_enum import subdomain_enum

class sitedossier(subdomain_enum):
    flags = ["subdomain-enum", "passive", "safe"]
    watched_events = ["DNS_NAME"]
    produced_events = ["DNS_NAME"]
    meta = {"description": "Query sitedossier.com for subdomains"}

    base_url = "http://www.sitedossier.com/parentdomain"
    max_pages = 10

    async def handle_event(self, event):
        query = self.make_query(event)
        async for hostname in self.query(query):
            try:
                hostname = self.helpers.validators.validate_host(hostname)
            except ValueError as e:
                self.verbose(e)
                continue
            if hostname and hostname.endswith(f".{query}") and not hostname == event.data:
                await self.emit_event_wait(hostname, "DNS_NAME", event, abort_if=self.abort_if)

    async def query(self, query, parse_fn=None, request_fn=None):
        results = set()
        base_url = f"{self.base_url}/{self.helpers.quote(query)}"
        url = str(base_url)
        for i, page in enumerate(range(1, 100 * self.max_pages + 2, 100)):
            self.verbose(f"Fetching page #{i+1} for {query}")
            if page > 1:
                url = f"{base_url}/{page}"
            response = await self.helpers.request(url)
            if response is None:
                self.info(f'Query "{query}" failed (no response)')
                break
            if response.status_code == 302:
                self.verbose("Hit rate limit captcha")
                break
            for regex in self.scan.dns_regexes:
                for match in regex.finditer(response.text):
                    hostname = match.group().lower()
                    if hostname and hostname not in results:
                        results.add(hostname)
                        yield hostname
            if '<a href="/parentdomain/' not in response.text:
                self.verbose(f"Next page not found in {response.text}")
                break


import grequests
import json
import time
from lxml import etree
import os

headers={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113'}
path=os.getcwd()
with open(path+"\CityCode.txt",'r',encoding='utf-8')as f:
    CityCode=f.read()
    CityCode=json.loads(CityCode)
def ConvertBirthday(birthday):
    # 利用localtime()函数将时间戳转化成时间数组
    localtime = time.localtime(birthday)
    #print(localtime)
    # 利用strftime()函数重新格式化时间
    dt = time.strftime('%Y:%m:%d %H:%M:%S',localtime)
    return dt
    #print(dt)

def GetUserInfo(text):
    try:

        result=json.loads(text)
        UserId=result['userPoint']['userId']
        level=result['level']
        birthday=result['profile']['birthday']
        province=result['profile']['province']
        city=result['profile']['city']
        gender=result['profile']['gender']#1为男2为女0为未知
        followed=result['profile']['followeds']
        follows=result['profile']['follows']
        nickname=result['profile']['nickname']
        listenSongs=result['listenSongs']
        if birthday>0:
            birthday=ConvertBirthday(birthday/1000)
            age=2020-int(birthday[:4])
        else:
            age=0
        #path=os.getcwd()
        # with open(path+"\CityCode.txt",'r',encoding='utf-8')as f:
        #     CityCode=f.read()
        #     CityCode=json.loads(CityCode)
        #print(type(CityCode))
        if str(city) in CityCode:
            city=CityCode[str(city)]
            province=CityCode[str(province)]
        else:
            city='海外'
            province='海外'
        user_url='https://music.163.com/user/home?id='+str(UserId)
        # print(province)
        # print(city)
        # print(gender)
        # print(level,nickname,listenSongs)
        # print(followed,follows)
        #print(age)
        return(UserId,nickname,level,gender,age,province,city,followed,follows,listenSongs,user_url)
    except:
        return 0
def getText(UserIds):
    reqs=[]
    for i in UserIds:
        url='https://music.163.com/api/v1/user/detail/'+str(i)
        reqs.append(grequests.get(url,headers=headers))

    resultInfos=[]
    res_list=grequests.map(reqs)
    res_text_list=[ret.text for ret in res_list if ret and ret.status_code == 200]
    return res_text_list


import cv2

from extraction.FormatModel import UtilFunctionsExtraction, UtilDebug
from api import engine

class RawValue:
    def __init__(self, value, countItems=1, parser=None, nameParser='None', singleParser=None,
                 nameSingleParser='None'):

        if countItems >=1:
            self.position = value
            self.position[0]=(self.position[0][0],self.position[0][1])
            self.position[1] = (self.position[1][0], self.position[1][1])
            self.countItems = countItems
            self.parser = parser
            self.singleParser = singleParser
            self.nameParser = nameParser
            self.nameSingleParser = nameSingleParser
            self.predictedValue = None
            self.arrayOfImages = None

        else:
            if nameSingleParser is not None and nameSingleParser == 'digitPredictor':
                self.predictedValue =[]
                EngineDigit = engine.UniqueEngineDigit()
                for v in value:
                    if v is not None:
                        self.predictedValue.append(chr(EngineDigit.pred[v] + ord('0')))
                    else:
                        self.predictedValue.append(' ')
            elif nameSingleParser is not None and nameSingleParser == 'letterPredictor':
                self.predictedValue = []
                EngineLetter = engine.UniqueEngineLetter()
                for v in value:

                    if v is not None:
                        self.predictedValue.append(chr(EngineLetter.pred[v] + ord('A')))
                    else:
                        self.predictedValue.append(' ')
            else:
                self.predictedValue = value

            self.nameParser = nameParser
            self.nameSingleParser = nameSingleParser
            self.countItems = -1

    def convert2ParsedValues(self):
        if self.nameParser == 'parserImage2ArrayChar':
            if self.nameSingleParser == 'letterPredictor':
                return ArrayPredictedChar(self.predictedValue)
            elif self.nameSingleParser == 'digitPredictor':
                return ArrayPredictedNumber(self.predictedValue)
        elif self.nameParser == 'parserImage2Categoric':
            return PredictedCategoric(self.predictedValue)
        print(self.nameParser)
        if self.nameSingleParser is not None:
            print(self.nameSingleParser)
        else:
            print('nameSingleParser is None')
        raise Exception('bad arguments or not implemented parser')

    def jsonDefault(object):
        return object.__dict__

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

    def getPosition(self):
        return self.position

    def parse(self, arg):
        if self.parser is None:
            raise Exception('no parser given')

        return self.parser(arg)

    def drawPosition(self, img):
        TL = self.position[0]
        BR = self.position[1]
        # print('TL ', TL)
        # TL = (TL[0],TL[1])
        # BR = (BR[0], BR[1])
        # print('BR ', BR)
        cv2.rectangle(img, TL, BR, (0, 255, 0), 2)

    def parserImage2ArrayChar(self, arg):
        if self.singleParser == self.letterPredictor:
            charArray_timer = UtilDebug.ArrayLetterTimer()
        else:
            charArray_timer = UtilDebug.ArrayDigitTimer()

        charArray_timer.startTimer(1) #self.count
        img = arg

        TL = self.position[0]
        BR = self.position[1]
        count = self.countItems
        arrayOfImages = UtilFunctionsExtraction.extractCharacters(img, TL, BR, count)
        arrayResult = []
        for singleImage in arrayOfImages:
            if singleImage is None:
                predicted = None
            else:
                predicted = self.singleParser(singleImage)

            arrayResult.append(predicted)
        #UtilFunctionsExtraction.plotImagesWithPrediction(arrayResult,arrayOfImages)
        self.predictedValue = arrayResult
        self.arrayOfImages = arrayOfImages
        charArray_timer.endTimer()
        return self.predictedValue

    def calcCuadroArrayChar(self, arg):
        if self.singleParser == self.letterPredictor:
            charArray_timer = UtilDebug.ArrayLetterTimer()
        else:
            charArray_timer = UtilDebug.ArrayDigitTimer()

        charArray_timer.startTimer(1) #self.count
        img = arg

        TL = self.position[0]
        BR = self.position[1]
        count = self.countItems
        UtilFunctionsExtraction.predictCuadros(img, TL, BR, count)
        charArray_timer.endTimer()

    def getFinalValue(self, arg):
        return self.predictedValue

    def parserImage2Categoric(self,arg):
        if self.singleParser is not None:
            categoric_timer = UtilDebug.CategoryTimer()
            categoric_timer.startTimer(1)
            ret = self.singleParser(arg)
            categoric_timer.endTimer()
        else:
            self.arrayOfImages = None
            self.predictedValue = ['unknow']
            return self.predictedValue

    def parserCategoricLabelsInside(self, arg):
        img = arg
        TL = self.position[0]
        BR = self.position[1]
        labels = self.position[2]
        self.countItems = len(labels)
        self.arrayOfImages = UtilFunctionsExtraction.extractCategory_extractColumnLabelsInside(img, TL, BR, len(labels))
        results = UtilFunctionsExtraction.predictValuesCategory_labelsInside(self.arrayOfImages, labels)
        self.predictedValue = []
        for r in results:
            self.predictedValue.append(r)
        return self.predictedValue
    def parserCategoricLabelsLeft(self, arg):
        img = arg
        TL = self.position[0]
        BR = self.position[1]
        labels = self.position[2]
        self.countItems = len(labels)
        self.arrayOfImages = UtilFunctionsExtraction.extractCategory_extractColumnLabelsLeft(img, TL, BR, len(labels), len(labels[0]))
        results = UtilFunctionsExtraction.predictValuesCategory_labelsLeft(self.arrayOfImages, labels)
        self.predictedValue = []
        for r in results:
            self.predictedValue.append(r)
        return self.predictedValue
    def parserCategoricLabelsSex(self, arg):
        img = arg
        TL = self.position[0]
        BR = self.position[1]
        labels = self.position[2]
        self.countItems = len(labels)
        self.arrayOfImages = UtilFunctionsExtraction.extractCategory_extractColumnLabelsSex(img, TL, BR, len(labels))
        results = UtilFunctionsExtraction.predictValuesCategory_labelsSex(self.arrayOfImages, labels)
        self.predictedValue = []
        for r in results:
            self.predictedValue.append(r)
        return self.predictedValue

    def parserCategoricLabelsDocumento(self, arg):
        img = arg
        TL = self.position[0]
        BR = self.position[1]
        labels = self.position[2]
        self.countItems = len(labels)
        self.arrayOfImages = UtilFunctionsExtraction.extractCategory_extractColumnLabelsDocumento(img, TL, BR, len(labels))
        results = UtilFunctionsExtraction.predictValuesCategory_labelsDocumento(self.arrayOfImages, labels)
        self.predictedValue = []
        for r in results:
            self.predictedValue.append(r)
        return self.predictedValue
    def parserCategoricLabelsTipoSuministro(self, arg):
        img = arg
        TL = self.position[0]
        BR = self.position[1]
        labels = self.position[2]
        self.countItems = len(labels)
        self.arrayOfImages = UtilFunctionsExtraction.extractCategory_extractColumnLabelsTipoSuministro(img, TL, BR, len(labels))
        results = UtilFunctionsExtraction.predictValuesCategory_labelsSingleButtons(self.arrayOfImages, labels)
        self.predictedValue = []
        for r in results:
            self.predictedValue.append(r)
        return self.predictedValue

    def parserCategoricLabelsTipoVia(self, arg):
        img = arg
        TL = self.position[0]
        BR = self.position[1]
        labels = self.position[2]
        self.countItems = len(labels)
        self.arrayOfImages = UtilFunctionsExtraction.extractCategory_extractColumnLabelsTipoVia(img, TL, BR, len(labels))
        results = UtilFunctionsExtraction.predictValuesCategory_labelsSingleButtons(self.arrayOfImages, labels)
        self.predictedValue = []
        for r in results:
            self.predictedValue.append(r)
        return self.predictedValue

    def parserCategoricLabelsSiNo(self, arg):
        img = arg
        TL = self.position[0]
        BR = self.position[1]
        labels = self.position[2]
        self.countItems = len(labels)
        self.arrayOfImages = UtilFunctionsExtraction.extractCategory_extractColumnLabelsTipoSiNo(img, TL, BR, len(labels))
        results = UtilFunctionsExtraction.predictValuesCategory_labelsSingleButtons(self.arrayOfImages, labels)
        self.predictedValue = []
        for r in results:
            self.predictedValue.append(r)
        return self.predictedValue

    def letterPredictor(self, img):
        indx = engine.predictImage(img)
        return indx#chr(pred_label + ord('A'))
        #return 'A'

    def digitPredictor(self, img):
        indx = engine.predictImageDigit(img)
        return indx#chr(pred_label + ord('0'))
        #return '0'

class ArrayImageNumber(RawValue):
    def __init__(self, position, count):
        super().__init__(position, count, self.parserImage2ArrayChar, 'parserImage2ArrayChar',
                         self.digitPredictor, 'digitPredictor')
    def __str__(self):
        return 'ArrayImageNumber: ' + str(self.countItems)

class ArrayImageChar(RawValue):
    def __init__(self, position, count):
        super().__init__(position, count, self.parserImage2ArrayChar, 'parserImage2ArrayChar',
                         self.letterPredictor, 'letterPredictor')
    def __str__(self):
        return 'ArrayImageChar: ' + str(self.countItems)

class ImageCategoric(RawValue):
    def __init__(self, position, count):
        if count == 1:
            super().__init__(position, 1, self.parserImage2Categoric, 'parserImage2Categoric', None, None)
        else:
            raise Exception('Categoric values always should have count = 1')
    def __str__(self):
        return 'ImageCategoric  : ' + str(self.countItems)

class ImageCategoricLabelsInside(RawValue):
    def __init__(self, position, count):
        if count == 1:
            super().__init__(position, 1, self.parserImage2Categoric, 'parserImage2Categoric',
                             self.parserCategoricLabelsInside, 'parserCategoricLabelsInside')
        else:
            raise Exception('Categoric values always should have count = 1')
    def __str__(self):
        return 'ImageCategoric  : ' + str(self.countItems)
class ImageCategoricLabelsLeft(RawValue):
    def __init__(self, position, count):
        if count == 1:
            super().__init__(position, 1, self.parserImage2Categoric, 'parserImage2Categoric',
                             self.parserCategoricLabelsLeft, 'parserCategoricLabelsLeft')
        else:
            raise Exception('Categoric values always should have count = 1')

    def __str__(self):
        return 'ImageCategoric  : ' + str(self.countItems)

class ImageCategoricLabelsSex(RawValue):
    def __init__(self, position, count):
        if count == 1:
            super().__init__(position, 1, self.parserImage2Categoric, 'parserImage2Categoric',
                             self.parserCategoricLabelsSex, 'parserCategoricLabelsSex')
        else:
            raise Exception('Categoric values always should have count = 1')

    def __str__(self):
        return 'ImageCategoric  : ' + str(self.countItems)

class ImageCategoricLabelsSiNo(RawValue):
    def __init__(self, position, count):
        if count == 1:
            super().__init__(position, 1, self.parserImage2Categoric, 'parserImage2Categoric',
                             self.parserCategoricLabelsSiNo, 'parserCategoricLabelsSiNo')
        else:
            raise Exception('Categoric values always should have count = 1')

    def __str__(self):
        return 'ImageCategoric  : ' + str(self.countItems)

class ImageCategoricLabelsTipoVia(RawValue):
    def __init__(self, position, count):
        if count == 1:
            super().__init__(position, 1, self.parserImage2Categoric, 'parserImage2Categoric',
                             self.parserCategoricLabelsTipoVia, 'parserCategoricLabelsTipoVia')
        else:
            raise Exception('Categoric values always should have count = 1')

    def __str__(self):
        return 'ImageCategoric  : ' + str(self.countItems)

class ImageCategoricLabelsDocumento(RawValue):
    def __init__(self, position, count):
        if count == 1:
            super().__init__(position, 1, self.parserImage2Categoric, 'parserImage2Categoric',
                             self.parserCategoricLabelsDocumento, 'parserCategoricLabelsDocumento')
        else:
            raise Exception('Categoric values always should have count = 1')

    def __str__(self):
        return 'ImageCategoric  : ' + str(self.countItems)
class ImageCategoricLabelsTipoSuministro(RawValue):
    def __init__(self, position, count):
        if count == 1:
            super().__init__(position, 1, self.parserImage2Categoric, 'parserImage2Categoric',
                             self.parserCategoricLabelsTipoSuministro, 'parserCategoricLabelsTipoSuministro')
        else:
            raise Exception('Categoric values always should have count = 1')

    def __str__(self):
        return 'ImageCategoric  : ' + str(self.countItems)
class ArrayPredictedNumber(RawValue):
    def __init__(self, value):
        super().__init__(value, -1, None, 'parserImage2ArrayChar',None, 'digitPredictor')

    def __str__(self):
        return 'ArrayPredictedNumber: ' + str(self.countItems)

class ArrayPredictedChar(RawValue):
    def __init__(self, value):
        super().__init__(value, -1, None, 'parserImage2ArrayChar', None, 'letterPredictor')
    def __str__(self):
        return 'ArrayPredictedChar: ' + str(self.countItems)

class PredictedCategoric(RawValue):
    def __init__(self, value):
        super().__init__(value, -1, None, 'parserImage2Categoric', None, None)

    def __str__(self):
        return 'PredictedCategoric  : ' + str(self.countItems)


# -*- coding: utf-8 -*-
"""
Created on Tue Mar  1 11:20:37 2022

@author: Emeline
"""
import numpy as np

# matrice G
G = np.zeros([60, 60])

# Room1
# wall (ext)
# convection
G[0, 0] = Gcv_LR_w[1]  # convection wall concrete out
# conduction
G[1, 1] = G[2, 2] = Gcd_LR_w_c  # conduction wall concrete
G[3, 3] = G[4, 4] = Gcd_LR_w_i  # conduction wall insulation
G[5, 5] = G[6, 6] = Gcd_LR_w_p  # conduction wall plaster
# convection
G[7, 7] = Gcv_LR_w[0]  # convection wall plaster inside

# floor
# convection
G[8, 8] = Gcv_LR_f[0]  # convection floor temp fix
# conduction
G[9, 9] = G[10, 10] = Gcd_LR_f  # conduction floor
# convection
G[11, 11] = Gcv_LR_f[0]  # convection floor inside

# door
# convection
G[12, 12] = Gcv_LR_d[0]  # convection door temp fix
# conduction
G[13, 13] = G[14, 14] = Gcd_LR_d  # conduction door
# convection
G[15, 15] = Gcv_LR_d[0]  # convection door inside

# dividing wall
# convection
G[16, 16] = Gcv_LR_dw[0]  # convection d-wall plaster fix
# conduction
G[17, 17] = G[18, 18] = Gcd_LR_dw_p  # conduction d-wall plaster
G[19, 19] = G[20, 20] = Gcd_LR_dw_i  # conduction d-wall insulation
G[21, 21] = G[22, 22] = Gcd_LR_dw_p  # conduction d-wall plaster
# convection
G[23, 23] = Gcv_LR_dw[0]  # convection wall plaster inside

# window + ventilation
# conduction
G[24, 24] = Gw_LR + Gv_LR  # conduction w + venti

# renouvellement air
# conduction
G[25, 25] = Gr_LR  # renouv air LR

# partition wall
# convection
G[26, 26] = Gcv_LRBR_dw[0]  # convection d-wall plaster in
# conduction
G[27, 27] = G[28, 28] = Gcd_LRBR_dw_p  # conduction d-wall plaster
G[29, 29] = G[30, 30] = Gcd_LRBR_dw_i  # conduction d-wall insulation
G[31, 31] = G[32, 32] = Gcd_LRBR_dw_p  # conduction d-wall plaster
# convection
G[33, 33] = Gcv_LRBR_dw[0]  # convection wall plaster inside

# door
# convection
G[34, 34] = Gcv_LRBR_d[0]  # convection door temp fix
# conduction
G[35, 35] = G[36, 36] = Gcd_LRBR_d  # conduction door
# convection
G[37, 37] = Gcv_LRBR_d[0]  # convection door inside

# Room2
# wall (ext)
# convection
G[45, 45] = Gcv_BR_w[1]  # convection wall concrete out
# conduction
G[44, 44] = G[43, 43] = Gcd_BR_w_c  # conduction wall concrete
G[42, 42] = G[41, 41] = Gcd_BR_w_i  # conduction wall insulation
G[40, 40] = G[39, 39] = Gcd_BR_w_p  # conduction wall plaster
# convection
G[38, 38] = Gcv_BR_w[1]  # convection wall plaster inside

# floor
# convection
G[49, 49] = Gcv_BR_f[0]  # convection floor temp fix
# conduction
G[48, 48] = G[47, 47] = Gcd_BR_f  # conduction floor
# convection
G[46, 46] = Gcv_BR_f[0]  # convection floor inside

# dividing wall
# convection
G[57, 57] = Gcv_BR_dw[0]  # convection d-wall plaster fix
# conduction
G[56, 56] = G[55, 55] = Gcd_BR_dw_p  # conduction d-wall plaster
G[54, 54] = G[53, 53] = Gcd_BR_dw_i  # conduction d-wall insulation
G[52, 52] = G[51, 51] = Gcd_BR_dw_p  # conduction d-wall plaster
# convection
G[50, 50] = Gcv_BR_dw[0]  # convection wall plaster inside

# window + ventilation
# conduction
G[59, 59] = Gw_BR + Gv_BR  # conduction w + venti

# renouvellement air
# conduction
G[58, 58] = Gr_BR  # renouv air BR

np.set_printoptions(suppress=False)
print(G)


import numpy as np
import matplotlib.pyplot as plt

time = np.loadtxt("dados_time.txt")
N = len(time)
time.sort(kind='quicksort')
#probabilidade de cada indice
y = np.arange(N) / float(N) #cdf
y = 1- y #ccdf
# plotting
plt.xlabel('time')
plt.ylabel('CCDF')

plt.title('CCDF of the time')

plt.plot(time, y)
plt.show()


# Strings used in the Google Analytics account to customize that account for BenarNews

# definitions
DEFS = {}

# dimension code definitions (to prevent gibberish strings like 'dimension16' from being used elsewhere):
DEFS['dimensionname'] = {
    'ga:dimension1': 'Content Type',
    'ga:dimension2': 'Language',
    'ga:dimension3': 'Section',
    'ga:dimension4': 'Categories',
    'ga:dimension5': 'Featured Story',
    'ga:dimension6': 'Publication Date',
    'ga:dimension7': 'Page Title',
    'ga:dimension8': 'Page Template',
    'ga:dimension9': 'Theme',
    'ga:dimension10': 'Topic',
    'ga:dimension11': 'Objective',
    'ga:dimension12': 'Word Count',
    'ga:dimension13': 'Article UID',
    'ga:dimension14': 'Headline',
    'ga:dimension15': 'Short Headline',
    'ga:dimension16': 'Article CMS ID',
}

DEFS['dimensioncode'] = {
    DEFS['dimensionname']['ga:dimension1']: 'ga:dimension1',
    DEFS['dimensionname']['ga:dimension2']: 'ga:dimension2',
    DEFS['dimensionname']['ga:dimension3']: 'ga:dimension3',
    DEFS['dimensionname']['ga:dimension4']: 'ga:dimension4',
    DEFS['dimensionname']['ga:dimension5']: 'ga:dimension5',
    DEFS['dimensionname']['ga:dimension6']: 'ga:dimension6',
    DEFS['dimensionname']['ga:dimension7']: 'ga:dimension7',
    DEFS['dimensionname']['ga:dimension8']: 'ga:dimension8',
    DEFS['dimensionname']['ga:dimension9']: 'ga:dimension9',
    DEFS['dimensionname']['ga:dimension10']: 'ga:dimension10',
    DEFS['dimensionname']['ga:dimension11']: 'ga:dimension11',
    DEFS['dimensionname']['ga:dimension12']: 'ga:dimension12',
    DEFS['dimensionname']['ga:dimension13']: 'ga:dimension13',
    DEFS['dimensionname']['ga:dimension14']: 'ga:dimension14',
    DEFS['dimensionname']['ga:dimension15']: 'ga:dimension15',
    DEFS['dimensionname']['ga:dimension16']: 'ga:dimension16',
}

# metric code definitions
DEFS['metricname'] = {
    'ga:metric1': 'Comments',
    'ga:metric2': 'Newsletter Subscriptions',
    'ga:metric3': 'Emails Sent',
    'ga:metric4': 'News Submissions',
}

DEFS['metriccode'] = {
    DEFS['metricname']['ga:metric1']: 'ga:metric1',
    DEFS['metricname']['ga:metric2']: 'ga:metric2',
    DEFS['metricname']['ga:metric3']: 'ga:metric3',
    DEFS['metricname']['ga:metric4']: 'ga:metric4',
}

# dimension constants (redundant, but more readable than DEFS['dimensioncode']['Dimension Name']):
CONTENT_TYPE = DEFS['dimensioncode']['Content Type']
LANGUAGE = DEFS['dimensioncode']['Language']
SECTION = DEFS['dimensioncode']['Section']
CATEGORIES = DEFS['dimensioncode']['Categories']
FEATURED_STORY = DEFS['dimensioncode']['Featured Story']
PUBLICATION_DATE = DEFS['dimensioncode']['Publication Date']
PAGE_TITLE = DEFS['dimensioncode']['Page Title']
PAGE_TEMPLATE = DEFS['dimensioncode']['Page Template']
THEME = DEFS['dimensioncode']['Theme']
TOPIC = DEFS['dimensioncode']['Topic']
OBJECTIVE = DEFS['dimensioncode']['Objective']
WORD_COUNT = DEFS['dimensioncode']['Word Count']
ARTICLE_UID = DEFS['dimensioncode']['Article UID']
HEADLINE = DEFS['dimensioncode']['Headline']
SHORT_HEADLINE = DEFS['dimensioncode']['Short Headline']
ARTICLE_CMSID = DEFS['dimensioncode']['Article CMS ID']

# a duplicate of the dictionary in core.vocabulary.py, added as a workaround for circular imports in other files
WEBPROPERTIES = {
    'UA-61666183-15': {
        'enviro': 'prod',
        'language': 'Totals',
        'total': True,
        'report_order': 5,
    },
    'UA-61666183-6': {
        'enviro': 'prod',
        'language': 'English',
        'total': False,
        'report_order': 0,
    },
    'UA-61666183-10': {
        'enviro': 'prod',
        'language': 'Indonesian',
        'total': False,
        'report_order': 1,
    },
    'UA-61666183-9': {
        'enviro': 'prod',
        'language': 'Malaysian',
        'total': False,
        'report_order': 2,
    },
    'UA-61666183-8': {
        'enviro': 'prod',
        'language': 'Thai',
        'total': False,
        'report_order': 3,
    },
    'UA-61666183-7': {
        'enviro': 'prod',
        'language': 'Bengali',
        'total': False,
        'report_order': 4,
    },
}

# a duplicate of the function in core.vocabulary.py, added as a workaround for circular imports in other files
def get_webproperty_by_language(language):
    for key, value in WEBPROPERTIES.items():
        if language == value['language']:
            return key
    raise NotInVocabularyError("A web property for language '%s'" % language)


#!/usr/bin/python3

import sys, os
from PyQt5 import QtCore

# create the settings file Example-1.conf in ~/.config/QSettingsExample
settings = QtCore.QSettings('QSettingsExample', 'Example-1')
print(settings.fileName())
# settings.setValue('Section/Item', Value)
settings.setValue('nags/firmware', True)
settings.setValue('john/theme_selection', 'Dark')
settings.setValue('apple/license', 'XXXXX-XXXX-XXXXX-XXXX')
#settings.setValue('banna/firmware', True)
settings.setValue('banana/firmware', True)
# if you don't specify a section it goes in General
settings.setValue('firmware', True)
#settings.remove('license')

# force the value to be a python bool
print(settings.value('firmware', None, type=bool))

keys = settings.allKeys()

print(keys)

'''
~/.config/QSettingsExample/Example-1.conf

Note: if you change a section name the old section is not removed
unless you explicity remove it with settings.remove('section/value')

[General]
firmware=true

[apple]
license=XXXXX-XXXX-XXXXX-XXXX

[banana]
firmware=true

[banna]
firmware=true

[john]
theme_selection=Dark

[nags]
firmware=true'''


import enum
from typing import Union
import collections
import numpy as np
from myosuite.utils.quat_math import euler2quat
import glob
import pickle

# Time precision to use. Avoids rounding/resolution errors during comparisons
_TIME_PRECISION = 4

# Reference structure
ReferenceStruct = collections.namedtuple('ReferenceStruct',
        ['time',        # float(N)
         'robot',       # shape(N, n_robot_jnt) ==> robot trajectory
         'robot_vel',   # shape(N, n_robot_jnt) ==> robot velocity
         'object',      # shape(M, n_objects_jnt) ==> object trajectory
         'robot_init',  # shape(n_objects_jnt) ==> initial robot pose (can be different from robot(0,n_robot_jnt))
         'object_init'  # shape(n_objects_jnt) ==> initial object (can be different from object(0,n_object_jnt))
         ])

# Reference type
class ReferenceType(enum.Enum):
    """Reference types"""
    FIXED = 0
    RANDOM = 1
    TRACK = 2

# Reference motion
class ReferenceMotion():
    def __init__(self, reference_data:Union[str, dict],
                 motion_extrapolation:bool=False,    # zero order hold if the motion is over
                 random_generator=None
                 ):
        """
        Reference Type
            Fixed  :: N==1, M==1  :: input is <Fixed target dict>
            Random :: N==2, M==2  :: input is <Randomization range dict> :: ind0:low_limit, ind1:high_limit
            Track  :: N>2 or M>2  :: input as <Motion file to be tracked>
        """

        self.motion_extrapolation = motion_extrapolation
        self.np_random = random_generator if random_generator!=None else np.random

        # load reference
        self.reference = self.load(reference_data)

        # check reference for format
        self.check_format(self.reference)
        self.reference['time'] = np.around(self.reference['time'], _TIME_PRECISION) # round to help with comparisons
        robot_shape = self.reference['robot'].shape if self.reference['robot'] is not None else (0,0)
        object_shape = self.reference['object'].shape if self.reference['object'] is not None else (0,0)
        self.robot_dim = robot_shape[1]
        self.object_dim = object_shape[1]

        # identify type
        if robot_shape[0] > 2 or object_shape[0] > 2:
            self.type = ReferenceType.TRACK
        elif robot_shape[0] == 2 or object_shape[0] == 2:
            self.type = ReferenceType.RANDOM
        elif robot_shape[0] == 1 or object_shape[0] == 1:
            self.type = ReferenceType.FIXED
        else:
            raise ValueError("Reference values not per specs")

        # identify length
        self.robot_horizon = robot_shape[0]
        self.object_horizon = object_shape[0]
        self.horizon = max(robot_shape[0], object_shape[0])

        # Add missing values
        if self.type == ReferenceType.RANDOM:
            # use mean postures from the randomization range
            if 'robot_init' not in self.reference.keys():
                self.reference['robot_init'] = np.mean(self.reference['robot'])
            if 'object_init' not in self.reference.keys():
                self.reference['object_init'] = np.mean(self.reference['object'][0])
        else:
            # use first timeset
            if 'robot_init' not in self.reference.keys():
                self.reference['robot_init'] = self.reference['robot'][0]
            if 'object_init' not in self.reference.keys():
                self.reference['object_init'] = self.reference['object'][0]

        # cache to help with finding index
        self.index_cache = 0

    def check_format(self, reference):
        """
        Check reference format
        """
        if reference['robot'] is not None:
            assert reference['robot'].ndim == 2, "Check robot reference, must be shape(N, n_robot_jnt)"
        if reference['object'] is not None:
            assert reference['object'].ndim == 2, "Check object reference, must be shape(N, n_object_jnt)"
        if reference['robot_init'] is not None:
            assert reference['robot_init'].ndim == 1, "Check robot_init reference, must be shape(n_robot_jnt)"
        if reference['robot'] is not None and reference['robot_init'] is not None:
            assert reference['robot_init'].shape[0] == reference['robot'].shape[1], "n_robot_jnt different between motion and init "
        if reference['object_init'] is not None:
            assert reference['object_init'].ndim == 1, "Check object_init reference, must be shape(n_object_jnt)"
        if reference['object_init'] is not None and reference['object'] is not None:
            assert reference['object_init'].shape[0] == reference['object'].shape[1], "n_object_jnt different between motion and init "

    def load(self, reference_data):
        """
        Load reference motion
        """

        # Load data
        if isinstance(reference_data, str):
            if reference_data.split('.')[-1]=="npz":
                reference = {k:v for k, v in np.load(reference_data).items()}
            elif reference_data.split('.')[-1]=="pkl" or reference_data.split('.')[-1]=="pickle":
                with open(reference_data, 'rb') as data:
                    reference = pickle.load(data)
            # if 'robot_vel' not in self.reference.keys():
                # self.reference['robot_vel'] = np.zeros(self.reference['robot'].shape)
        elif isinstance(reference_data, dict):
            reference = reference_data.copy()
        else:
            raise TypeError("Unknown reference type")

        # resolve values
        assert 'time' in reference.keys(), "Missing key (time) in reference"
        if 'robot_init' not in reference.keys():
            reference['robot_init'] = reference['robot'][0] if 'robot' in reference.keys() else None
        if 'object_init' not in reference.keys():
            reference['object_init'] = reference['object'][0] if 'object' in reference.keys() else None

        # build reference
        ref = ReferenceStruct(
            time = reference['time'], # Must have
            robot = reference['robot'] if 'robot' in reference.keys() else None,
            robot_vel = reference['robot_vel'] if 'robot_vel' in reference.keys() else None,
            object = reference['object'] if 'object' in reference.keys() else None,
            robot_init = reference['robot_init'],
            object_init = reference['object_init'],
        )
        return ref._asdict()

    def find_timeslot_in_reference(self, time):
        """
        Find the timeslot interval for the provided time in the reference motion
        INPUT:  Time: Specified time
        OUTPUT: Timeslot index tuple(ind_prev, ind_next)
                - where reference['time'][ind_prev] <= time <= reference['time'][ind_next]
                - ind_prev == ind_next if exact time is found in reference['time']
        """

        time = np.around(time, _TIME_PRECISION) # round to help with comparisons
        if self.type == ReferenceType.FIXED:
            return (0,0)
        if self.motion_extrapolation and time>=self.reference['time'][-1]:
            return (self.horizon-1,self.horizon-1)
        else:
            assert time<=self.reference['time'][-1], f"Trying to access time (={time}) beyond max reference duration (={self.reference['time'][-1]}) "

        # search locally for index
        if time == self.reference['time'][self.index_cache]:
            # print(f"curr match: {time}")
            return (self.index_cache, self.index_cache)

        elif self.index_cache<(self.horizon-1):
            if time == self.reference['time'][self.index_cache+1]:
                # print(f"next match: {time}")
                self.index_cache += 1
                return (self.index_cache, self.index_cache)

            elif time > self.reference['time'][self.index_cache] and time < self.reference['time'][self.index_cache+1]:
                # print(f"interval match: {time}")
                return (self.index_cache, self.index_cache+1)
            else:
                print(f"No result using hueristic search. Attempting sort match: {time}")
                self.index_cache = np.searchsorted(self.reference['time'], time, side="right") - 1
                if time == self.reference['time'][self.index_cache]:
                    return (self.index_cache, self.index_cache)
                elif time > self.reference['time'][self.index_cache] and time < self.reference['time'][self.index_cache+1]:
                    return (self.index_cache, self.index_cache+1)
                else:
                    raise ValueError("We shouldn't be in this condition")
        else:
            raise ValueError("We shouldn't be in this condition")

    def reset(self):
        """
        Reset the cache to point back to the beginning
        """
        self.index_cache = 0

    def get_init(self):
        """
        return the initial posture of the robot and the object
        """
        return self.reference['robot_init'], self.reference['object_init']

    def get_reference(self, time):
        """
        Return the reference at the given time. Linearly interpolate if there is no exact match
        """
        if self.type == ReferenceType.FIXED:
            robot_ref = self.reference['robot'][0]
            robot_vel_ref = self.reference['robot_vel'][0]
            object_ref = self.reference['object'][0]
        elif self.type == ReferenceType.RANDOM:
            robot_ref     = self.np_random.uniform(low=self.reference['robot'][0,:], high=self.reference['robot'][1,:])
            robot_vel_ref = self.np_random.uniform(low=self.reference['robot_vel'][0,:], high=self.reference['robot_vel'][1,:])
            object_ref    = self.np_random.uniform(low=self.reference['object'][0,:], high=self.reference['object'][1,:])
        elif self.type == ReferenceType.TRACK:
            ind, ind_next = self.find_timeslot_in_reference(time=time)
            if ind == ind_next:
                # Exact frame[time] found for reference
                robot_ref     = self.reference['robot'][ind] if self.robot_horizon>1 else self.reference['robot'][0]
                if self.reference['robot_vel'] is None:
                    robot_vel_ref = None
                else:
                    robot_vel_ref = self.reference['robot_vel'][ind] if self.robot_horizon>1 else self.reference['robot_vel'][0]
                if self.reference['object'] is None:
                    object_ref = None
                else:
                    object_ref = self.reference['object'][ind] if self.object_horizon>1 else self.reference['object'][0]
            else:
                # Linearly interpolate between frames to get references
                # ref[time] = blend(ref[ind] + (1-blend)*ref[ind_next])
                print(f"Direct frame reference not found at {time} sec. Attempting linear blend between two frames [{ind},{ind_next}]")
                blend = time - self.reference['time'][ind]/(self.reference['time'][ind_next]-self.reference['time'][ind])

                # robot motion
                if self.robot_horizon>1:
                    robot_ref = (1.0-blend)**self.reference['robot'][ind]+blend*self.reference['robot'][ind_next]
                    if self.reference['robot_vel'] is None:
                        robot_vel_ref = None
                    else:
                        robot_vel_ref = (1.0-blend)**self.reference['robot_vel'][ind]+blend*self.reference['robot_vel'][ind_next]
                else:
                    robot_ref = self.reference['robot'][0]
                    robot_vel_ref = None if self.reference['robot_vel'] is None else self.reference['robot_vel'][0]

                # object motion
                if self.reference['object'] is None:
                    object_ref = None
                elif self.object_horizon>1:
                    object_ref = (1.0-blend)*self.reference['object'][ind]+blend*self.reference['object'][ind_next]
                else:
                    object_ref = self.reference['object'][0]

        return ReferenceStruct(time = time,
            robot = robot_ref,
            robot_vel = robot_vel_ref,
            object = object_ref,
            robot_init = self.reference['robot_init'],
            object_init = self.reference['object_init'],
            )

    def __repr__(self) -> str:
        return self.reference.__repr__()


import numpy as np
import torch.backends.cudnn as cudnn

import sys
import os
import argparse
import pprint

import utils
import logging
import time
import glob
import shutil
from mmcv import Config

import architecture_code
import models
from dataset import dataset_entry
from attack import *

Debug = False

parser = argparse.ArgumentParser()
parser.add_argument('--config', '-c', type=str, default='./experiments/RobNet_cifar10_experiments/config_adversarial_test.py',
                    help='location of the config file')
parser.set_defaults(augment=True)
args = parser.parse_args()

def main():
    global cfg

    cfg = Config.fromfile(args.config)

    cfg.save = '{}/{}-{}-{}'.format(cfg.save_path, cfg.model, cfg.dataset,
                                    time.strftime("%Y%m%d-%H%M%S"))
    utils.create_exp_dir(cfg.save)

    logger = utils.create_logger('global_logger', cfg.save + '/log.txt')

    if not torch.cuda.is_available():
        logger.info('no gpu device available')
        sys.exit(1)

    # Set cuda device & seed
    torch.cuda.set_device(cfg.gpu)
    np.random.seed(cfg.seed)
    cudnn.benchmark = True
    torch.manual_seed(cfg.seed)
    cudnn.enabled = True
    torch.cuda.manual_seed(cfg.seed)

    # Model
    print('==> Building model..')
    net = models.model_search_entry(cfg)
    net = net.cuda()
    net_adv = AttackPGD(net, cfg.attack_param)
    # Load checkpoint.
    if not Debug:
        print('==> Resuming from checkpoint...')
        net_adv.load_state_dict(torch.load(cfg.resume_path))

    # Specify specific genotype_list
    # genotype_list = [3,3,3,3,3,1,3,1,2,3,1,1,1,3]
    genotype_list = [1,1,2,3,0,1,2,0,0,3,3,0,3,1]
    cfg.netpara = sum(p.numel() for p in net.parameters()) / 1e6
    logger.info("Genotypes: {}".format(genotype_list))
    logger.info('config: {}'.format(pprint.pformat(cfg)))

    # Data
    print('==> Preparing data..')

    testloader = dataset_entry(cfg)
    criterion = nn.CrossEntropyLoss()

    print('==> Testing on Data..')
    test(net_adv, testloader, criterion, True, genotype_list)

def test(net, testloader, criterion, adv=False, genotype_list=None):
    losses = utils.AverageMeter(0)
    top1 = utils.AverageMeter(0)
    top5 = utils.AverageMeter(0)

    logger = logging.getLogger('global_logger')

    net.eval()

    with torch.no_grad():
        for batch_idx, (inputs, targets) in enumerate(testloader):
            inputs, targets = inputs.cuda(), targets.cuda()

            if not adv:
                outputs = net(inputs, genotype_list=genotype_list)
            else:
                outputs, inputs_adv = net(inputs, targets, genotype_list)

            loss = criterion(outputs, targets)
            prec1, prec5 = utils.accuracy(outputs.data, targets, topk=(1, 5))

            num = inputs.size(0)
            losses.update(loss.item(), num)
            top1.update(prec1.item(), num)
            top5.update(prec5.item(), num)

            if batch_idx % cfg.report_freq == 0:
                logger.info(
                    'Test: [{0}/{1}]\t'
                    'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
                    'Prec@1 {top1.val:.3f} ({top1.avg:.3f})\t'
                    'Prec@5 {top5.val:.3f} ({top5.avg:.3f})\t'
                    .format(batch_idx, len(testloader), loss=losses, top1=top1, top5=top5))

    final_loss = losses.avg
    final_top1 = top1.avg
    final_top5 = top5.avg

    logger.info(' * Prec@1 {:.3f}\tPrec@5 {:.3f}\tLoss {:.3f}\t'.format(final_top1, final_top5, final_loss))

    return final_top1

if __name__ == '__main__':
    main()


#!/usr/bin/env python

import rospy
from opcua import Client, ua
from machines.msg import Pneumatic
import sys
import time

# <---------------------------------- error handling
def error_handling(block):
    return 'Retrying {}. {}. {}, line: {}'.format(block, sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2].tb_lineno)
# ------------------------------------------>

def callback(data):
    client = Client("opc.tcp://10.226.52.227:4994")
    client.connect()
    cmdLockingUp = client.get_node("ns=3;s=[PLC]HMI_Manual_LockingCylinder_Up")
    cmdLockingDown = client.get_node("ns=3;s=[PLC]HMI_Manual_Pallet_Locking_Down")

    if data.extended:
        cmdLockingDown.set_value(False)
        cmdLockingUp.set_value(True)
        print("locking extend")

    if data.retracted:
        cmdLockingUp.set_value(False)
        cmdLockingDown.set_value(True)
        print("locking retract")

    client.disconnect()

def main():

    status = Pneumatic()
    stn1url = "opc.tcp://10.226.52.227:4994"
    client1 = Client(stn1url)
    client1.connect()

    PalletLockingCylinder1_Down = client1.get_node("ns=3;s=::[PLC]aentr:1:I.6")
    pub = rospy.Publisher('status/locking', Pneumatic, queue_size=10)
    rospy.init_node('locking', anonymous=True)
    rospy.Subscriber('control/locking', Pneumatic, callback)
    rate = rospy.Rate(1)

    while not rospy.is_shutdown():

        status.header.frame_id = 'lockingStatus'
        status.header.stamp = rospy.Time.now()
        status.retracted = PalletLockingCylinder1_Down.get_value()
        status.extended = not status.retracted
        pub.publish(status)
        rate.sleep()

if __name__ == '__main__':

    try:
        main()

    except KeyboardInterrupt:
        print('exiting ...')


from flask import Blueprint, url_for, redirect, flash, request, abort
from chargingbooth.main.utils import start_route, is_registered

main = Blueprint('main', __name__)

# This is the function that will be called so that no other device
# On the network can access the routes
@main.before_app_request
def ip_check():
	# Check if it is not localhost
	if request.remote_addr != "127.0.0.1":
		abort(403)

@main.route("/")
@main.route("/home")
def home():
	start_route()

	try:
		if is_registered():
			return redirect(url_for('system_admin_account.login'))
		else:
			return redirect(url_for('register.home'))
	except:
		flash("Unable to Connect to Server!", "danger")
		return redirect(url_for('error.register'))

	return redirect(url_for('register.home'))


import numpy as np
import sys

data  = np.loadtxt(sys.argv[1])
kappa = data[:,0]
P1 = 1.00*6.022*1E-2
P0 = data[:,1]/1E5*6.022*1E-2
#print(P0,P1)
#print(-(P1 - P0)*2*kappa*8.33)
a1=np.sqrt(-(P1 - P0)*2*kappa*8.33)
#a1=(P1 - P0)

#print(kappa,a1)

fp=open('klm_a.dat','w')

for i in range(len(a1)):
    fp.write('%f %f\n'%(data[i,0],a1[i]))
fp.close()


from __future__ import annotations

import datetime
import typing

from psycopg2.extras import RealDictCursor

from src import data
from src.adapter.ds.dst_ds.pg import PgDstDs

__all__ = ("create",)

def create(
    *,
    api: data.API,
    cur: typing.Any,
    dst_db_name: str,
    dst_schema_name: str | None,
    dst_table_name: str,
    src_table: data.Table,
    batch_ts: datetime.datetime,
    after: dict[str, datetime.date],
) -> data.DstDs:
    if api == data.API.PSYCOPG2:
        return PgDstDs(
            cur=typing.cast(RealDictCursor, cur),
            dst_db_name=dst_db_name,
            dst_schema_name=dst_schema_name,
            dst_table_name=dst_table_name,
            src_table=src_table,
            batch_ts=batch_ts,
            after=after,
        )

    raise NotImplementedError(f"The api specified, {api!s}, does not have an DstDs implementation.")


# Построить сегментацию изображения на основании вычисления площади поверхности сегмента.

from PIL import Image, ImageDraw
import numpy as np
from matplotlib import pyplot as plt

CELL_SIZE = 20
delta = [1, 2]

def get_A(cell, ds):
    length, width = cell.size[0], cell.size[1]
    u = np.zeros((len(ds) + 1, length + 1, width + 1))
    b = np.zeros((len(ds) + 1, length + 1, width + 1))
    v = np.zeros((len(ds) + 1))
    A = np.zeros((len(ds) + 1))

    for i in range(0, length):
        for j in range(0, width):
            u[0][i][j] = cell.getpixel((i, j))
            b[0][i][j] = cell.getpixel((i, j))

            for d in ds:
                u[d][i][j] = max(u[d - 1][i][j] + 1, max(u[d - 1][i + 1][j + 1], u[d - 1][i - 1][j + 1], u[d - 1][i + 1][j - 1], u[d - 1][i - 1][j - 1]))
                b[d][i][j] = min(b[d - 1][i][j] - 1, min(b[d - 1][i + 1][j + 1], b[d - 1][i - 1][j + 1], b[d - 1][i + 1][j - 1], b[d - 1][i - 1][j - 1]))

    for d in ds:
        v[d] = 0
        for i in range(0, length):
            for j in range(0, width):
                v[d] += u[d][i][j] - b[d][i][j]

    for d in ds:
        A[d] = (v[d] - v[d-1]) / 2

    return A[delta[-1]]

def build_segmentation(image, save_file_name):
    length, width = image.size[0], image.size[1]
    cell_length, cell_width = length//CELL_SIZE, width//CELL_SIZE
    A = np.zeros((cell_length, cell_width))
    for i in range(0, cell_length):
        for j in range(0, cell_width):
            area = (i*CELL_SIZE, j*CELL_SIZE, (i+1)*CELL_SIZE, (j+1)*CELL_SIZE)
            cell = image.crop(area)
            A[i][j] = get_A(cell, delta)

    mean = np.mean(A)

    new_image = Image.new("L", (cell_length*CELL_SIZE, cell_width*CELL_SIZE))
    img1 = ImageDraw.Draw(new_image)

    for i in range(0, cell_length):
        for j in range(0, cell_width):
            if A[i][j] < mean:
                img1.rectangle([(i*CELL_SIZE, j*CELL_SIZE), ((i+1)*CELL_SIZE, (j+1)*CELL_SIZE)], fill = "white", outline="white")
            else:
                img1.rectangle([(i*CELL_SIZE, j*CELL_SIZE), ((i+1)*CELL_SIZE, (j+1)*CELL_SIZE)], fill = "black", outline="black")
    new_image.save(save_file_name)

def getBlackAndWhiteImage(image):
    black_and_white = image.convert('1')
    return black_and_white

if __name__ == '__main__':
        name = 'image_text.png'
        try:
            im = Image.open(name)

            im = getBlackAndWhiteImage(im)
            segmentated_name = "segmentated_" + name
            build_segmentation(im, segmentated_name)
            im_result = getBlackAndWhiteImage(Image.open('segmentated_image.jpg'))

            plt.figure(figsize=(5, 5))
            plt.imshow(im_result)
            plt.show()

        except Exception as e:
            print(e)
            print("Could not find the file. Please try again.")

Методы фрактального анализа цифровых изображений/labs/lab7.py


from fastapi import APIRouter, Depends, UploadFile

from ....controllers import PropertiesController
from ....schemas.query_params import PropertyQuery
from ....schemas.request import (
    RentalCreate,
    PropertyCreate,
    PropertyUpdate,
    ReviewCreate,
    ReviewUpdate,
)
from ....utils.jwt import ADMIN_AUTH, AUTH

router = APIRouter()
controller = PropertiesController()

@router.get("")
async def get_properties(filters: PropertyQuery = Depends()):
    return await controller.get_properties(filters=filters)

@router.get("/{property_id}")
async def get_property(property_id: int):
    return await controller.get_property(property_id=property_id)

@router.post("", dependencies=[Depends(ADMIN_AUTH)])
async def create_property(data: PropertyCreate):
    return await controller.create_property(data_in=data)

@router.post("/{property_id}/images", dependencies=[Depends(ADMIN_AUTH)])
async def upload_image(property_id: int, image: UploadFile):
    return await controller.upload_image(property_id=property_id, image=image)

@router.delete("/{property_id}/images/{image_id}", dependencies=[Depends(ADMIN_AUTH)])
async def delete_image(property_id: int, image_id: int):
    return await controller.remove_image(property_id=property_id, image_id=image_id)

@router.put("/{property_id}", dependencies=[Depends(ADMIN_AUTH)])
async def update_property(property_id: int, data: PropertyUpdate):
    return await controller.update_property(property_id=property_id, data=data)

@router.delete("/{property_id}", dependencies=[Depends(ADMIN_AUTH)])
async def delete_property(property_id: int):
    return await controller.delete_property(property_id=property_id)

@router.get("/{property_id}/reviews")
async def get_reviews(property_id: int):
    return await controller.get_reviews(property_id=property_id)

@router.post("/{property_id}/reviews")
async def create_review(property_id: int, data: ReviewCreate, user=Depends(AUTH)):
    return await controller.add_review(
        property_id=property_id,
        user_id=user.id,
        data=data,
    )

@router.put("/{property_id}/reviews/{review_id}")
async def update_review(
    property_id: int,
    review_id: int,
    data: ReviewUpdate,
    user=Depends(AUTH),
):
    return await controller.update_review(
        property_id=property_id,
        review_id=review_id,
        user_id=user.id,
        data=data,
    )

@router.get("/{property_id}/rentals")
async def get_rentals(property_id: int):
    return await controller.get_rentals(property_id=property_id)

@router.post("/{property_id}/rentals")
async def create_rental(property_id: int, data: RentalCreate, user=Depends(AUTH)):
    return await controller.book_property(
        property_id=property_id,
        user_id=user.id,
        data=data,
    )

@router.post("/rentals/{rental_id}/accept", dependencies=[Depends(ADMIN_AUTH)])
async def accept_rental(rental_id: int):
    return await controller.accept_rental(
        rental_id=rental_id,
    )

@router.post("/rentals/{rental_id}/decline", dependencies=[Depends(ADMIN_AUTH)])
async def decline_rental(rental_id: int):
    return await controller.decline_rental(
        rental_id=rental_id,
    )

@router.get("/{property_id}/tenants")
async def get_tenants(property_id: int):
    return await controller.get_tenants(property_id=property_id)

@router.post("/{property_id}/tenants", dependencies=[Depends(ADMIN_AUTH)])
async def add_tenant(property_id: int, user_id: int):
    return await controller.add_tenant(property_id=property_id, user_id=user_id)

@router.delete("/{property_id}/tenants/{tenant_id}", dependencies=[Depends(ADMIN_AUTH)])
async def remove_tenant(property_id: int, tenant_id: int):
    return await controller.remove_tenant(property_id=property_id, tenant_id=tenant_id)