import math
import random
from Tkinter import *
from PIL import Image, ImageTk

class ShopHolo(object):
    @staticmethod
    def init(data):
        ShopHolo.img1 = Image.open("images/shopholo1.png")
        ShopHolo.img2 = Image.open("images/shopholo2.png")
        ShopHolo.img3 = Image.open("images/shopholo3.png")
        ShopHolo.imgs = dict()
        ShopHolo.imgs[1] = ShopHolo.img1
        ShopHolo.imgs[2] = ShopHolo.img2
        ShopHolo.imgs[3] = ShopHolo.img3

        w = h = int(data.height*2/3.)
        for key in ShopHolo.imgs:
            img = ShopHolo.imgs[key]
            img = img.resize((w,h), Image.ANTIALIAS)
            ShopHolo.imgs[key] = img

    def __init__(self, image):
        PILimg = baseImg = ShopHolo.imgs[image]
        self.image = [PILimg, baseImg, ImageTk.PhotoImage(PILimg)]
        self.angle = 0
        self.angleSpeed = 2 * image
        if image == 2:
            self.angleSpeed *= -1

    def update(self):
        # rotate holo
        self.angle += self.angleSpeed
        PILimg = self.image[0]
        baseImg = self.image[1]
        PILimg = baseImg.rotate(self.angle)
        self.image = [PILimg, baseImg, ImageTk.PhotoImage(PILimg)]

    def draw(self, canvas, data):
        canvas.create_image(data.width/2.,data.height/2.,\
                                image = self.image[2])


"""

    线程同步解决方式：
        事件（event）

    event事件是线程通信机制中最简单的实现，使用一个内部的标记flag
    ，通过flag的True或False的变化来进行操作

"""

"""
    问题：
        工人和老板问题

"""

import  threading
import logging
import time

logging.basicConfig(level=logging.INFO,format="%(message)s")
root = logging.getLogger()
root.setLevel(logging.INFO)
n = 0

def boss():
    global n
    while True:
        c = logging.getLogger("c")
        c.info("{}{}".format("boss来查看杯子：", n))
        if n == 10:
            log = logging.getLogger("a")
            log.info("{}{}".format("boss:","得到10个杯子"))
            break

def worker():
    global n
    time.sleep(2)
    n += 1
    w = logging.getLogger("b")
    w.info("{}{}".format("工人生产出",n))

threading.Thread(target=boss,daemon=True).start()

while True:
    time.sleep(2)
    worker()


from discord.ext import commands
import json

class Cog(commands.Cog, name="config"):
    def __init__(self, bot):
        self.bot = bot
        self.data = {}
        self.load_json()
        self.sections = []

    def get_config(self, section):
        if section in self.data:
            data = self.data[section]
        else:
            data = {}
        section = ConfigSection(self, section, data)
        self.sections.append(section)
        return section

    def set_data(self, section, data):
        self.data[section] = data
        self.save_json()

    def load_json(self):
        with open("config.json", "r") as f:
            self.data = json.load(f)

    def save_json(self):
        with open("config.json", "w") as f:
            json.dump(self.data, f, indent="\t")

    @commands.command()
    async def reload_config(self, ctx):
        self.load_json()
        for i in self.sections:
            i.__init__(self, i.section, self.data[i.section])
        await ctx.send("Reloaded config")

class ConfigSection:
    def __init__(self, parent, section, data):
        self.parent = parent
        self.section = section
        self.data = data

    def __getitem__(self, item):
        return self.data[item]

    def __setitem__(self, item, value):
        self.data[item] = value
        self.parent.set_data(self.section, self.data)

    def set_default(self, key, value):
        if key not in self.data:
            self.__setitem__(key, value)

async def setup(bot):
    await bot.add_cog(Cog(bot))


from iot.DB import DBSingleton
db= DBSingleton.get_instance()
db.connectDB()

from iot.models import User, Room
from iot.serializers import RoomSerializer
from rest_framework.response import  Response
from rest_framework import status
from rest_framework.decorators import APIView

class RoomViewSet(APIView):
    def get(self, request):
        rooms = Room.objects.all()
        rooms_serializer = RoomSerializer(rooms, many=True)
        return Response(rooms_serializer.data)

    def post(self, request):
        try:
            serializer = RoomSerializer(data=request.data)
            if serializer.is_valid():
                serializer.save()
                nRoom= Room.objects.get(Id= serializer.validated_data["Id"])
                admins= User.objects(isAdmin= True)
                print(serializer.validated_data)
                for admin in admins:
                    admin.update(add_to_set__room= nRoom.id)
                    nRoom.update(add_to_set__users= admin.id)
                return Response(serializer.data, status.HTTP_201_CREATED)

            return Response(serializer.errors, status.HTTP_400_BAD_REQUEST)
        except:
            return Response(status.HTTP_409_CONFLICT)

class RoomDetailViewSet(APIView):
    def get_object(self, Id):
        try:
            room= Room.objects.get(Id=Id)

            return room
        except Room.DoesNotExist:
            return Response(status.HTTP_404_NOT_FOUND)

    def get(self, request, Id):
        try:
            room= self.get_object(Id)
            room_serializer = RoomSerializer(room)

            return Response(room_serializer.data)
        except:
            return Response(status.HTTP_404_NOT_FOUND)

    def put(self, request, Id):
        try:
            room = self.get_object(Id)
            serializer = RoomSerializer(room, data=request.data)
            if serializer.is_valid():
                serializer.save()
                return Response(serializer.data)

            return Response(serializer.errors, status.HTTP_400_BAD_REQUEST)
        except:
            return Response(serializer.errors, status.HTTP_403_FORBIDDEN)

    def delete(self, request, Id):
        room = self.get_object(Id)
        count= 0
        admins= []
        for user in room.users:
            if user.isAdmin == True:
                admins.append(user)
        if len(admins) == len(room.users):
            print("yes")
            for admin in admins:
                admin.update(pull__room= room.id)
            room.delete()
            return Response(status.HTTP_204_NO_CONTENT)
        else:
            return Response(status.HTTP_400_BAD_REQUEST)


from django.urls import path
from .views import *
urlpatterns = [
    path('',inicio_view, name = 'inicio_view'),
    path('about/',about_view, name ='about_view'),
    path('contacto/',contacto_view, name='contacto_view'),
    path('productos/',productos_view, name='productos_view'),
    path('marcas/',marcas_view, name = 'marcas_view'),
    path('categorias/',categorias_view, name= 'categorias_view'),

    path('agregar_producto/',agregar_producto_view, name = 'agregar_producto'),
    path('agregar_marca/',agregar_marca_view, name = 'agregar_marca'),
    path('agregar_categoria/',agregar_categoria_view, name = 'agregar_categoria'),

    path('ver_producto/<int:id_prod>',ver_producto_view, name = 'ver_producto_view'),
    path('eliminar_producto/<int:id_prod>',eliminar_producto_view, name = 'eliminar_producto_view'),
    path('editar_producto/<int:id_prod>',editar_producto_view, name = 'editar_producto_view'),

    path('ver_marca/<int:id_marca>',ver_marca_view, name = 'ver_marca_view'),
    path('eliminar_marca/<int:id_marca>',eliminar_marca_view, name = 'eliminar_marca_view'),
    path('editar_marca/<int:id_marca>',editar_marca_view, name = 'editar_marca_view'),

    path('ver_categoria/<int:id_categoria>',ver_categoria_view, name = 'ver_categoria_view'),
    path('eliminar_categoria/<int:id_categoria>',eliminar_categoria_view, name = 'eliminar_categoria_view'),
    path('editar_categoria/<int:id_categoria>',editar_categoria_view, name = 'editar_categoria_view'),

    path('login/',login_view,name='login_view'),
    path('logout/',logout_view,name='logout_view'),
]


from flix_web_app.domainmodel.actor import Actor
from flix_web_app.domainmodel.director import Director
from flix_web_app.domainmodel.genre import Genre
from flix_web_app.domainmodel.movie import Movie
from flix_web_app.domainmodel.user import User

import pytest

from flix_web_app.home import services as home_serv
from flix_web_app.logging_in import services as log_serv
from flix_web_app.search import services as search_serv
from flix_web_app.logging_in.services import ValidationException

def test_get_movies_for_browse(populated_repo):
    movie = Movie('Picnic', 2016)
    movie.id = 1001
    populated_repo.add_movie(movie)
    movie2 = Movie('Panadol', 2011)
    movie2.id = 1002
    populated_repo.add_movie(movie2)
    movie3 = Movie('Banana', 2009)
    movie3.id = 1003
    populated_repo.add_movie(movie3)
    assert home_serv.get_movies([1001, 1002, 1003], populated_repo) == [movie, movie2, movie3]

def test_get_ids_of_movies(populated_repo):
    li = [i for i in range(1, 1001)]
    assert home_serv.get_ids_of_movies(populated_repo) == li

def test_get_movie_ids_by_x(populated_repo):
    movie = Movie('Picnic', 2016)
    movie.id = 1001
    movie.add_genre(Genre('Blarg'))
    movie.add_actor(Actor('Glue Stick'))
    movie.director = Director('George Smith')
    populated_repo.add_movie(movie)
    lis = [1001]
    assert search_serv.get_movies_ids_by_title('Picnic', populated_repo) == lis
    assert search_serv.get_movies_ids_by_actor('Glue Stick', populated_repo) == lis
    assert search_serv.get_movies_ids_by_genre('Blarg', populated_repo) == lis
    assert search_serv.get_movies_ids_by_director('George Smith', populated_repo) == lis
    assert search_serv.get_movies_ids_by_title('LOL', populated_repo) is None
    assert search_serv.get_movies_ids_by_actor('LOL', populated_repo) is None
    assert search_serv.get_movies_ids_by_genre('LOL', populated_repo) is None
    assert search_serv.get_movies_ids_by_director('LOL', populated_repo) is None

def test_get_movies_by_x(populated_repo):
    movie = Movie('Picnic', 2016)
    movie.id = 1001
    movie.add_genre(Genre('Blarg'))
    movie.add_actor(Actor('Glue Stick'))
    movie.director = Director('George Smith')
    populated_repo.add_movie(movie)
    lis = [movie]
    assert search_serv.get_movies_by_title([1001], populated_repo) == lis
    assert search_serv.get_movies_by_actor([1001], populated_repo) == lis
    assert search_serv.get_movies_by_genre([1001], populated_repo) == lis
    assert search_serv.get_movies_by_director([1001], populated_repo) == lis
    assert search_serv.get_movies_by_title([1010], populated_repo) == []
    assert search_serv.get_movies_by_actor([1010], populated_repo) == []
    assert search_serv.get_movies_by_genre([1010], populated_repo) == []
    assert search_serv.get_movies_by_director([1010], populated_repo) == []

def test_add_user(populated_repo):
    # add new user
    user = User('Hazel', '12345')
    log_serv.add_user('Hazel', '12345', populated_repo)
    assert log_serv.get_user('Hazel', populated_repo) == user
    # user doesnt exist
    with pytest.raises(log_serv.UnknownUserException):
        log_serv.get_user('Error', populated_repo)
    # username taken
    with pytest.raises(log_serv.NameNotUniqueException):
        log_serv.add_user('Hazel', '12345', populated_repo)
    # invalid password
    with pytest.raises(log_serv.ValidationException):
        log_serv.validate_user('Hazel', "123456", populated_repo)
    #valid password
    try:
        log_serv.validate_user("Hazel", "12345", populated_repo)
    except ValidationException:
        assert False


import numpy as np

import torch
from torch import nn
from torch.utils.data import DataLoader
from torch.functional import F

MTU = 1514

def session_2d_histogram(ts, sizes, path, plot=False):
    # ts_norm = map(int, ((np.array(ts) - ts[0]) / (ts[-1] - ts[0])) * MTU)
    if len(ts) == 1:
        ts_norm = np.array([0.0])
    else:
        ts_norm = ((np.array(ts) - ts[0]) / (ts[-1] - ts[0])) * MTU
    H, xedges, yedges = np.histogram2d(sizes, ts_norm, bins=(range(0, MTU + 1, 1), range(0, MTU + 1, 1)))

    if plot:
        plt.set_cmap('plasma')
        plt.pcolormesh(xedges, yedges, H)
        plt.xlim(0, MTU + 1)
        plt.ylim(0, MTU + 1)
        plt.xticks([])
        plt.yticks([])
        plt.savefig(path)
    return torch.Tensor(H.astype(np.int32)).unsqueeze(0)

class TraceDataset(torch.utils.data.IterableDataset):
    def __init__(self, flow_times, flow_packets, flow_sizes, indexes, em):
        self.flow_times = flow_times
        self.flow_packets = flow_packets
        self.flow_sizes = flow_sizes
        self.indexes = indexes
        self.em = em

    def __iter__(self):
        for idx in self.indexes:
            c = 1 if self.flow_sizes[idx + 1] > self.em else 0
            yield session_2d_histogram(self.flow_times[idx], self.flow_packets[idx], '', False), c

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

class CNNModel(nn.Module):
    def __init__(self, filters=(10, 20)):
        super().__init__()
        self.filters = filters

        self.conv1 = nn.Conv2d(1, out_channels=self.filters[0], kernel_size=5, stride=2)
        self.bn1 = nn.BatchNorm2d(filters[0])
        self.conv2 = nn.Conv2d(filters[0], out_channels=filters[1], kernel_size=5, stride=2)
        self.bn2 = nn.BatchNorm2d(filters[1])
        self.d2 = nn.Dropout(p=0.25)
        self.linear1 = nn.Linear(filters[1] * 93 * 93, 64)
        self.d3 = nn.Dropout(p=0.5)
        self.linear2 = nn.Linear(64, 1)

    def forward(self, x):
        x = self.conv1(x)
        x = F.relu(x)
        x = F.max_pool2d(x, 2)
        x = self.bn1(x)

        x = self.conv2(x)
        x = F.relu(x)
        x = F.max_pool2d(x, 2)
        x = self.bn2(x)

        x = self.d2(x)

        x = x.view(-1, self.filters[-1] * 93 * 93)

        x = self.linear1(x)
        x = F.relu(x)

        x = self.d3(x)

        x = self.linear2(x)

        x = F.sigmoid(x)

        return x


import numpy as np
import torch

def cg_solve(f_Ax, b, x_0=None, cg_iters=10, residual_tol=1e-10, callback=None, verbose=False):
    """
    Goal: Solve Ax=b equivalent to minimizing f(x) = 1/2 x^T A x - x^T b
    Assumption: A is PSD, no damping term is used here (must be damped externally in f_Ax)
    Verbose mode works only with numpy
    """

    if type(b) == torch.Tensor:
        x = torch.zeros(b.shape[0]) if x_0 is None else x_0
        x = x.to(b.device)
        if b.dtype == torch.float16:
            x = x.half()
        r = b - f_Ax(x)
        p = r.clone()
    elif type(b) == np.ndarray:
        x = np.zeros_like(b) if x_0 is None else x_0
        r = b - f_Ax(x)
        p = r.copy()
    else:
        print("Type error in cg")

    fmtstr = "%10i %10.3g %10.3g %10.3g"
    titlestr = "%10s %10s %10s %10s"
    if verbose: print(titlestr % ("iter", "residual norm", "soln norm", "obj fn"))

    for i in range(cg_iters):
        if callback is not None:
            callback(x)
        if verbose:
            obj_fn = 0.5*x.dot(f_Ax(x)) - 0.5*b.dot(x)
            norm_x = torch.norm(x) if type(x) == torch.Tensor else np.linalg.norm(x)
            print(fmtstr % (i, r.dot(r), norm_x, obj_fn))

        rdotr = r.dot(r)
        Ap = f_Ax(p)
        alpha = rdotr/(p.dot(Ap))
        x = x + alpha * p
        r = r - alpha * Ap
        newrdotr = r.dot(r)
        beta = newrdotr/rdotr
        p = r + beta * p

        if newrdotr < residual_tol:
            # Early CG termination because the residual is small
            break

    if callback is not None:
        callback(x)
    if verbose:
        obj_fn = 0.5*x.dot(f_Ax(x)) - 0.5*b.dot(x)
        norm_x = torch.norm(x) if type(x) == torch.Tensor else np.linalg.norm(x)
        print(fmtstr % (i, r.dot(r), norm_x, obj_fn))
    return x


import pickle
from fastfeature.plotting.plotter import cool_plotting
from fastsklearnfeature.candidates.CandidateFeature import CandidateFeature
from fastsklearnfeature.candidates.RawFeature import RawFeature
from typing import List, Dict, Any
import numpy as np

#file = "/tmp/chart.p"
#file = '/home/felix/phd/fastfeature_logs/charts/chart_all_23_11.p'
#file = '/home/felix/phd/fastfeature_logs/charts/chart_all_fold20_no_hyper_opt_32min.p'
#file = '/home/felix/phd/fastfeature_logs/charts/chart_all_sorted_by_complexity_fold20_hyper_opt_1045min.p'

#heart
#file = '/home/felix/phd/fastfeature_logs/newest_28_11/chart_hyper_10_all.p'
#my_range = (0.72, 0.88)
# heart also raw features
file = '/home/felix/phd/fastfeatures/results/cluster_good_cv_fixed_group/all_data.p'
my_range = (0.50, 0.88)

#diabetes
#file = '/home/felix/phd/fastfeatures/results/diabetes/chart.p'
#my_range = (0.72, 0.78)

all_data = pickle.load(open(file, "rb"))

names = [str(r['candidate']) for r in all_data]
scores = [r['score'] for r in all_data]
runtimes = [r['time'] for r in all_data]
candidates: List[CandidateFeature] = [r['candidate'] for r in all_data]

group_by_transformation = {}
for i in range(len(candidates)):
    c = candidates[i]
    if not isinstance(c, RawFeature):
        if not c.transformation.name in group_by_transformation:
            group_by_transformation[c.transformation.name] = []
        group_by_transformation[c.transformation.name].append(scores[i])

keys = []
avg_score = []
for k,v in group_by_transformation.items():
    keys.append(k)
    avg_score.append(np.mean(v))
    print(k + ': mean ' + str(np.mean(v)) + " max " + str(np.max(v)))


# enhanced Stokes drag on spherical particle in cylindrical tube

# r ... particle radius
# R ... cylinder radius

# drag ... reduced stokes drag

# dimensionless parameters:
# lam = r/R in [0,1]
# mu  = r/(R-r) in [0,inf]
# rho = (R-r)/r = 1/lam - 1 in [0,inf]

# empirical law:
# drag = exp(2*r/(R-r)) = exp(2*lam/(1-lam)) = exp(2*mu) = exp(2/rho)

# get experimental data from csv file
from numpy import genfromtxt, exp
TT = genfromtxt('stokes_drag_table_rdivR.csv', delimiter=',')
lam = TT[:,0] # lam = r/R
drag0 = TT[:,1]
drag = [exp(2.*x/(1.-x)) for x in lam]

D0 = [1./x for x in drag0]
D = [1./x for x in drag]

rho = [1./x - 1. for x in lam[::-1]]
drag0rho = drag0[::-1]
dragrho = [exp(2./x) for x in rho]

import matplotlib.pyplot as plt
'''
#fname = "IV_c0.1_Ry1000nm.eps"
plt.plot(lam,drag0, 's-', label="Paine, Scherr")
plt.plot(lam,drag, 'x-', label="Mitscha-Baude")
plt.xlabel("r/R")
plt.ylabel("drag/drag0")
plt.legend(loc='lower right')

#plt.savefig(fname, bbox_inches='tight')
plt.show()
'''
#fname = "IV_c0.1_Ry1000nm.eps"
plt.plot(lam,D0, 's-', label="Paine, Scherr")
plt.plot(lam,D, 'x-', label="Mitscha-Baude")
plt.xlabel("r/R")
plt.ylabel("D/D0")
plt.legend(loc='upper right')
'''
#fname = "IV_c0.1_Ry1000nm.eps"
plt.loglog(rho,drag0rho, 's-', label="Paine, Scherr")
plt.loglog(rho,dragrho, 'x-', label="Mitscha-Baude")
plt.xlabel("(R-r)/r")
plt.ylabel("drag/drag0")
plt.legend(loc='upper right')
'''
#plt.savefig(fname, bbox_inches='tight')
plt.show()


from pkg.decorators import setup_file_required
from utils import CapturingOutput
from test_commands import HERE
import unittest

class DecoratorTest(unittest.TestCase):
    def test_setup_file_required(self):
        func = setup_file_required(lambda: print('Function Done!'))

        with CapturingOutput() as output:
            (HERE / 'setup.cfg').touch(exist_ok=True)
            func()
            (HERE / 'setup.cfg').unlink(missing_ok=True)
            func()

        self.assertEqual(output[0], 'Function Done!')
        self.assertEqual(output[1], 'Error: The "setup.cfg" file is required!!')

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


# 두 리스트의 길이 차이가 1이므로 두 리스트 정렬하여 순서를 똑같이 만든 후 요소가 달라지는 부분에서 완주 못한 1명을 찾아내는 방법을 사용하였다.
def solution(participant, completion):
    participant.sort()     # participant 참가자 이름 알파벳 순서대로 정렬
    completion.sort()     # completion 참가자 이름 알파벳 순서대로 정렬
    for i in range(len(completion)):     # i는 인덱스
        if participant[i] != completion[i]:     # participant[i]와 completion[i]가 달라지는 부분은 participant[i]가 완주를 하지 못했다는 의미이므로
            return participant[i]     # participant[i]를 return하고 끝낸다.
    else:     # 인덱스 i는 participant보다 길이가 1 짧은 completion의 길이 이전까지의 범위이므로 위 for문을 그냥 통과한 경우
        return participant[-1]     # participant의 마지막 참가자가 완주를 하지 못한 것이므로 participant[-1]을 return하고 끝낸다.

print(solution(["mislav", "stanko", "mislav", "ana"], ["stanko", "ana", "mislav"]))

# 효율성 테스트 통과해야 하므로 아래의 코드는 불가!
# def solution(participant, completion):
#     while len(participant) != 1:
#         for p in participant:
#             if p in completion:
#                 participant.remove(p)
#                 completion.remove(p)
#     return participant[0]

# 다른 풀이 1 (Collection 모듈 사용하기)
# Counter 함수에 중복된 데이터가 저장된 배열을 인자로 넘기면 각 원소가 몇 번씩 나오는지가 저장된 객체를 얻게 된다.
import collections

def solution(participant, completion):
    answer = collections.Counter(participant) - collections.Counter(completion)     # 연산 가능!
    # print(collections.Counter(participant))     --> Counter({'mislav': 2, 'stanko': 1, 'ana': 1})
    # print(collections.Counter(completion))     --> Counter({'stanko': 1, 'ana': 1, 'mislav': 1})
    # print(answer)     --> Counter({'mislav': 1})
    return list(answer.keys())[0]

# 다른 풀이 2 (dictionary 이용)
# python에서는 Dictionary라는 자료구조를 통해 해시를 구현할 수 있다.
# List에 비해 Dictionary가 매우 빠른 시간복잡도를 가져 원소를 넣거나 삭제, 찾는 일이 많을 때에는 딕셔너리를 사용하는 것이 좋다.
# hash() 함수 : 개체에 해시 값이 있는 경우 해당 개체의 해시 값을 반환, dictionary를 보면서 빠르게 dictionary 키를 비교하는 데 사용되는 '정수'
# but 이 방법은 찾아야 하는 사람이 1명인 경우에만 사용 가능 (해시 충돌 때문에)
def solution(participant, completion):
    answer = ''
    temp = 0
    dic = {}
    for part in participant:
        dic[hash(part)] = part     # 문자열 part(participant 요소 하나하나)의 hash 값이 키가 되고 part가 값이 되어 dic의 원소로 추가됨.
        temp += int(hash(part))     # temp에는 dic의 키 값인 해시 값을 하나씩 모두 더한다.
    for com in completion:
        temp -= hash(com)     # completion에 있는 요소들(문자열, 참가자)의 해시 값을 temp에서 하나씩 뺀다. (같은 문자열일 경우, 해시값이 같다.)
    answer = dic[temp]     # 마지막에는 완주에 실패한 사람의 키인 해시값 하나만 남게 되는데 그것으로 정답을 구한다.

    return answer
print(solution(["mislav", "stanko", "mislav", "ana"], ["stanko", "ana", "mislav"]))

python_programmers_school/level 1/완주하지 못한 선수.py

완주하지 못한 선수.py


import os
import sys
from pathlib import Path

sys.path.insert(0, '../')
from script_generator import *

DATASET = ["sift10M"]
SEARCH_L = [""]
TYPE = ["distributed"]
NUM_SHARD = ["1", "2", "4", "8", "16"]

gen = ScriptGenerator(f"num_shard.sh")

gen.options = {"DATASET": DATASET, "SEARCH_L": SEARCH_L, "TYPE": TYPE, "NUM_SHARD": NUM_SHARD}

experiments, scripts = gen.generate()

for i, (exp, sc) in enumerate(zip(experiments, scripts)):
    SCRIPT_DIR = f"/root/git/guitar/workload/script-generator/scripts/distributed/"
    Path(SCRIPT_DIR).mkdir(parents=True, exist_ok=True)

    file_name = f"{exp['DATASET']}_{exp['SEARCH_L']}_{exp['TYPE']}_{exp['NUM_SHARD']}.sh"
    filepath = os.path.join(SCRIPT_DIR, file_name)
    print(filepath)

    with open(filepath, "w") as f:
        f.write(sc)

print(f"generated {len(experiments)} experiment scripts")


# -*- coding: utf-8 -*-
# @Author  : Junru_Lu
# @File    : util.py
# @Software: PyCharm
# @Environment : Python 3.6+
# @Reference : https://github.com/likejazz/Siamese-LSTM

# 基础包
from keras import backend as K
from keras.layers import Layer
from keras.preprocessing.sequence import pad_sequences
import numpy as np
import itertools

'''
本配置文件提供了一系列预定义函数
'''

# ------------------自定义函数------------------ #

def text_to_word_list(flag, text):  # 文本分词
    text = str(text)
    text = text.lower()

    if flag == 'cn':
        pass
    else:
        # 英文文本下的文本清理规则
        import re
        text = re.sub(r"[^A-Za-z0-9^,!.\/'+-=]", " ", text)
        text = re.sub(r"what's", "what is ", text)
        text = re.sub(r"\'s", " ", text)
        text = re.sub(r"\'ve", " have ", text)
        text = re.sub(r"can't", "cannot ", text)
        text = re.sub(r"n't", " not ", text)
        text = re.sub(r"i'm", "i am ", text)
        text = re.sub(r"\'re", " are ", text)
        text = re.sub(r"\'d", " would ", text)
        text = re.sub(r"\'ll", " will ", text)
        text = re.sub(r",", " ", text)
        text = re.sub(r"\.", " ", text)
        text = re.sub(r"!", " ! ", text)
        text = re.sub(r"\/", " ", text)
        text = re.sub(r"\^", " ^ ", text)
        text = re.sub(r"\+", " + ", text)
        text = re.sub(r"\-", " - ", text)
        text = re.sub(r"\=", " = ", text)
        text = re.sub(r"'", " ", text)
        text = re.sub(r"(\d+)(k)", r"\g<1>000", text)
        text = re.sub(r":", " : ", text)
        text = re.sub(r" e g ", " eg ", text)
        text = re.sub(r" b g ", " bg ", text)
        text = re.sub(r" u s ", " american ", text)
        text = re.sub(r"\0s", "0", text)
        text = re.sub(r" 9 11 ", "911", text)
        text = re.sub(r"e - mail", "email", text)
        text = re.sub(r"j k", "jk", text)
        text = re.sub(r"\s{2,}", " ", text)

    text = text.split()

    return text

def make_w2v_embeddings(flag, word2vec, df, embedding_dim):  # 将词转化为词向量
    vocabs = {}  # 词序号
    vocabs_cnt = 0  # 词个数计数器

    vocabs_not_w2v = {}  # 无法用词向量表示的词
    vocabs_not_w2v_cnt = 0  # 无法用词向量表示的词个数计数器

    # 停用词
    # stops = set(open('data/stopwords.txt').read().strip().split('\n'))

    for index, row in df.iterrows():
        # 打印处理进度
        if index != 0 and index % 1000 == 0:
            print(str(index) + " sentences embedded.")

        for question in ['question1', 'question2']:
            q2n = []  # q2n -> question to numbers representation
            words = text_to_word_list(flag, row[question])

            for word in words:
                # if word in stops:  # 去停用词
                    # continue
                if word not in word2vec and word not in vocabs_not_w2v:  # OOV的词放入不能用词向量表示的字典中，value为1
                    vocabs_not_w2v_cnt += 1
                    vocabs_not_w2v[word] = 1
                if word not in vocabs:  # 非OOV词，提取出对应的id
                    vocabs_cnt += 1
                    vocabs[word] = vocabs_cnt
                    q2n.append(vocabs_cnt)
                else:
                    q2n.append(vocabs[word])
            df.at[index, question + '_n'] = q2n

    embeddings = 1 * np.random.randn(len(vocabs) + 1, embedding_dim)  # 随机初始化一个形状为[全部词个数，词向量维度]的矩阵
    '''
    词1 [a1, a2, a3, ..., a60]
    词2 [b1, b2, b3, ..., b60]
    词3 [c1, c2, c3, ..., c60]
    '''
    embeddings[0] = 0  # 第一行用0填充，因为不存在index为0的词

    for index in vocabs:
        vocab_word = vocabs[index]
        if vocab_word in word2vec:
            embeddings[vocab_word] = word2vec[vocab_word]
    del word2vec

    return df, embeddings

def split_and_zero_padding(df, max_seq_length):  # 调整tokens长度

    # 训练集矩阵转换成字典
    X = {'left': df['question1_n'], 'right': df['question2_n']}

    # 调整到规定长度
    for dataset, side in itertools.product([X], ['left', 'right']):
        dataset[side] = pad_sequences(dataset[side], padding='pre', truncating='post', maxlen=max_seq_length)

    return dataset

class ManDist(Layer):  # 封装成keras层的曼哈顿距离计算

    # 初始化ManDist层，此时不需要任何参数输入
    def __init__(self, **kwargs):
        self.result = None
        super(ManDist, self).__init__(**kwargs)

    # 自动建立ManDist层
    def build(self, input_shape):
        super(ManDist, self).build(input_shape)

    # 计算曼哈顿距离
    def call(self, x, **kwargs):
        self.result = K.exp(-K.sum(K.abs(x[0] - x[1]), axis=1, keepdims=True))
        return self.result

    # 返回结果
    def compute_output_shape(self, input_shape):
        return K.int_shape(self.result)


from django.conf import settings
from django.conf.urls.static import static
from django.urls import path

from . import views

app_name = "recognition"

urlpatterns = [
    path('', views.index, name = 'index'),
    path('history/', views.history, name = 'history'),
    path('history/<int:id>/',views.detail, name = 'detail'),
    path('sendhistory/',views.sendhistory, name='sendhistory'),
    path('sendhistory/<int:id>/',views.detailsend, name='detailsend'),
    path('yandexkit/', views.yandexkit, name = 'yandexkit')
]

if settings.DEBUG:
    urlpatterns += static(
        settings.MEDIA_URL,
        document_root = settings.MEDIA_ROOT
    )


import os
import warnings
from PIL import Image
import numpy as np
from sklearn.externals import joblib
from test_model.config import *

def tes1t1_data(file_path):
    img = Image.open(file_path)
    data = img.getdata()
    data = np.matrix(data) / 255
    data1 = data.tolist()
    data2 = []
    for i in data1:
        data2.extend(i)
    return [data2]

def together_path(dir_list):           # 遍历一个文件夹的所有函数
    all_file_path = []
    for i in dir_list:
        file_list = os.listdir('./{0}'.format(i))
        for j in file_list:
            file_path = '{0}/{1}'.format(i, j)
            all_file_path.append(file_path)
    return all_file_path

def reconginize_img(img_path):
    warnings.filterwarnings("ignore")
    m = tes1t1_data(img_path)
    svc_3 = joblib.load(model_path_3)
    a = svc_3.predict(m)
    print(a)

if __name__ == '__main__':
    img_path_list = together_path(img_path)
    for file_path in img_path_list:
        reconginize_img(file_path)


import pandas as pd
import numpy as np
import numpy.random
import pandas as pd
from astropy.convolution import convolve
import matplotlib.pyplot as plt
from astropy.convolution.kernels import Gaussian2DKernel
import scipy.ndimage as spnd
from matplotlib.colors import LogNorm

ldv_x = []
ldv_y = []
ldv_z = []
ldv = pd.read_csv('./differenced_ldv.csv')
for index, row in ldv.iterrows():
    ldv_x.append(row['x_bucket'])
    ldv_y.append(row['y_bucket'])
    ldv_z.append(row['difference_PM2.5'])

heatmap_ldv, xedges_ldv, yedges_ldv = np.histogram2d(ldv_x, ldv_y, bins=500, weights=[w*100+40 for w in ldv_z])
extent_ldv = [yedges_ldv[0], yedges_ldv[-1], xedges_ldv[0], xedges_ldv[-1]]

im = plt.imshow(convolve(heatmap_ldv, Gaussian2DKernel(stddev=2)), extent=extent_ldv, cmap='inferno', vmin=0, vmax=100)

plt.colorbar()

plt.show()

bus_x = []
bus_y = []
bus_z = []
bus = pd.read_csv('./differenced_bus.csv')
for index, row in bus.iterrows():
    bus_x.append(row['x_bucket'])
    bus_y.append(row['y_bucket'])
    bus_z.append(row['difference_PM2.5'])
heatmap_bus, xedges_bus, yedges_bus = np.histogram2d(bus_x, bus_y, bins=500, weights=[w*100+40 for w in bus_z])
extent_bus = [yedges_bus[0], yedges_bus[-1], xedges_bus[0], xedges_bus[-1]]

taxi_x = []
taxi_y = []
taxi_z = []
taxi = pd.read_csv('./differenced_taxi.csv')
for index, row in taxi.iterrows():
    taxi_x.append(row['x_bucket'])
    taxi_y.append(row['y_bucket'])
    taxi_z.append(row['difference_PM2.5'])
heatmap_taxi, xedges_taxi, yedges_taxi = np.histogram2d(taxi_x, taxi_y, bins=500, weights=[w*100+40 for w in taxi_z])
extent_taxi = [yedges_taxi[0], yedges_taxi[-1], xedges_taxi[0], xedges_taxi[-1]]

heatmaps=[heatmap_taxi, heatmap_taxi, heatmap_bus, heatmap_ldv]

im = plt.imshow(convolve(heatmap_taxi, Gaussian2DKernel(stddev=2)), extent=extent_ldv, cmap='inferno', vmin=0, vmax=100)

plt.colorbar()

plt.show()

im = plt.imshow(convolve(heatmap_bus, Gaussian2DKernel(stddev=2)), extent=extent_ldv, cmap='inferno', vmin=0, vmax=100)

plt.colorbar()

plt.show()

'''fig, ax = plt.subplots(2, 1)

pcm = ax[0].pcolor(x, y, z,
                   norm=colors.LogNorm(vmin=min(z), vmax=max(z)),
                   cmap='Greys')
fig.colorbar(pcm, ax=ax[0], extend='max')

pcm = ax[1].pcolor(x, y, z, cmap='Greys')
fig.colorbar(pcm, ax=ax[1], extend='max')
fig.show()'''

'''from astropy.convolution.kernels import Gaussian2DKernel
plt.pcolor(convolve(heatmap, Gaussian2DKernel(stddev=2)), cmap='inferno')
plt.colorbar()

plt.show()'''


from collections import deque

def melt(i, j):
    q = deque([(i, j)])
    while q:
        y, x = q.popleft()
        for d in range(4):
            nr, nc = y + dr[d], x + dc[d]
            if nr < 0 or nr >= N or nc < 0 or nc >= M:
                continue
            if visited[nr][nc]:
                continue

            if MAP[nr][nc]:
                visited[nr][nc] = 1
                q.append((nr, nc))
            elif MAP[y][x]:
                MAP[y][x] -= 1
    return

def year(y):
    global keep
    res = y
    iceberg = 0
    for r in range(N):
        for c in range(M):
            if MAP[r][c] and not visited[r][c]:
                if iceberg > 0:
                    keep = False
                    return res
                visited[r][c] = 1
                melt(r, c)
                iceberg += 1

    if not iceberg:
        keep = False
        return 0

    return res + 1

dr = [-1, 1, 0, 0]
dc = [0, 0, -1, 1]

N, M = map(int, input().split())
MAP = [list(map(int, input().split())) for _ in range(N)]

ans = 0
keep = True
while keep:
    visited = [[0] * M for _ in range(N)]
    ans = year(ans)

print(ans)


import numpy
import cv2

def main():

    camaraWeb()

    #cargarVideo()

    #guardarVideo()

    return

def camaraWeb():

    cap = cv2.VideoCapture(0)

    while(True):
        # Captura frame por frame
        ret,frame = cap.read()

        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        cv2.imshow('frame',frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    # When everything done, release the capture
    cap.release()
    cv2.destroyAllWindows()

# def cargarVideo():

#     cap = cv2.VideoCapture("videos/saludo.avi")

#     while(cap.isOpened()):

#         ret, frame = cap.read()

#         gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

#         cv2.imshow('frame',gray)

#         if cv2.waitKey(1) & 0xFF == ord('q'):
#             break

#     cap.release()
#     cv2.destroyAllWindows()

# def guardarVideo():

#     cap = cv2.VideoCapture(0)

#     # Definimos el codec a usar
#     fourcc = cv2.VideoWriter_fourcc(*'XVID')

#     out = cv2.VideoWriter('videos/output.avi',fourcc, 20.0, (640,480))

#     while(cap.isOpened()):

#         ret, frame = cap.read()
#         if ret==True:
#             #frame = cv2.flip(frame,0)

#             out.write(frame)

#             cv2.imshow('frame',frame)
#             if cv2.waitKey(1) & 0xFF == ord('q'):
#                 break
#         else:
#             break

#     # Cerramos la conexion
#     cap.release()
#     out.release()
#     cv2.destroyAllWindows()

if __name__ == '__main__':
    main()


#!/usr/bin/python3
"""
Script that sends a request to the URL with the letter as a parameter
and displays the body of the response.
"""
import requests
from sys import argv

if __name__ == "__main__":

    url = "http://0.0.0.0:5000/search_user"
    data = {"q": ""}

    if len(argv) > 1:
        data["q"] = argv[1]

    req = requests.post(url, data=data)
    try:
        res_dict = req.json()

        if (len(res_dict) == 0):
            print("No result")
        else:
            print("[{}] {}".format(res_dict.get("id"), res_dict.get("name")))
    except Exception:
        print("Not a valid JSON")


import argparse
import logging
from datetime import datetime
from time import sleep

from tqdm import tqdm

from app import db
from app.models import WatchlistItem, Title
from lib.providers import Providers

LOGGER = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)

# Create argument parser
parser = argparse.ArgumentParser()
parser.add_argument('--config', '-c', default='config')
args = parser.parse_args()

i = 0
providers = Providers(config_path=args.config)
LOGGER.info('Update all watchlist items with outdated providers list')

query = db.session.query(WatchlistItem, Title.title, Title.id) \
    .select_from(WatchlistItem).join(Title)

for item, title, tmdb_id in tqdm(query.all()):
    updated_providers = [provider for provider in providers.get_names(title, tmdb_id)]
    if set(updated_providers) != set(item.providers):
        item.providers = updated_providers
        item.update_datetime_utc = datetime.utcnow()
        # Commit straight away to avoid IdleInTransactionSessionTimeout
        db.session.commit()
        i += 1
    sleep(0.1)

LOGGER.info(f'Applied {i} changes to the database')

LOGGER.info('Done')


# -*- coding: utf-8 -*-
"""
Created on Sun Jan 19 16:52:22 2020

@author: Kokkinos
"""

"""Example program to show how to read a multi-channel time series from LSL."""
import numpy as np
import scipy.io as sio
from pylsl import StreamInlet, resolve_stream
from tkinter import *

from graphics import Graphics

class data_rec(Graphics):

    def __init__(self):

        Graphics.__init__(self)

        with open("visual_cues.txt") as f:
            content = f.readlines()
            content = [line.rstrip('\n') for line in content]

        self.Fs = int(content[0]) # Sampling Frequency
        self.Rdur = int(content[1]) # Rest visual cue duration
        self.IMdur = int(content[2]) # Imaginary Movement visual cue duration
        self.Prdur = int(content[3])

        del content[0:4]
        self.content = np.array(content)

        self.vcN = len(self.content) # number of visual cues
        idxs = np.where(self.content == 'REST')
        self.RN = len(idxs[0]) # number of REST visual cues
        idxs = np.where(self.content == 'PREPARE')
        self.PRN = len(idxs[0])
        self.IMN = len(self.content) - self.RN - self.PRN # number of Imaginary Movements visual cues
        self.recdur = self.RN * self.Rdur * self.Fs + self.IMN * self.IMdur * self.Fs + self.PRN * self.Prdur * self.Fs # duration of the recording

    def begin_stream(self):

        print("looking for an EEG stream...")
        self.streams = resolve_stream('type', 'EEG')

        # create a new inlet to read from the stream
        self.inlet = StreamInlet(self.streams[0])

    def record_loop(self):

        cSTR = 0
        cVC = 0
        cdur = 0
        dur = self.Prdur
        buffer = []

        while cSTR < self.recdur:

            sample, timestamp = self.inlet.pull_sample()
            buffer += [sample,]

            if cdur % (dur * self.Fs) == 0:

                if self.content[cVC] == 'REST':
                    print("REST")
                    self.delete_all()
                    cdur = 0
                    dur = self.Rdur
                    cVC = cVC+1

                elif self.content[cVC] == 'LEFT':
                    print("LEFT")
                    self.left_arrow()
                    cdur = 0
                    dur = self.IMdur
                    cVC = cVC+1

                elif self.content[cVC] == 'RIGHT':
                    print("RIGHT")
                    self.right_arrow()
                    cdur = 0
                    dur = self.IMdur
                    cVC = cVC+1

                elif self.content[cVC]=='PREPARE':
                    self.Concentration_Cross()
                    cdur = 0
                    dur = self.Prdur
                    cVC = cVC+1

            cSTR = cSTR + 1
            cdur = cdur + 1

        return buffer

    def save_recording(self, buffer):

        LABELS = []
        trig = []
        offset = self.IMdur * self.Fs
        IM_interval = 2* self.IMdur * self.Fs

        idxs=np.where(self.content=='REST')
        self.content = np.delete(self.content,idxs)
        idxs=np.where(self.content=='PREPARE')
        self.content = np.delete(self.content,idxs)

        for i, visual_cue in enumerate(self.content):

            if visual_cue == 'LEFT':
                LABELS += [1,]
                trig += [offset + i * IM_interval,]
            else:
                LABELS += [2,]
                trig += [offset + i * IM_interval,]

        trig = np.array(trig)
        LABELS = np.array(LABELS)
        buffer = np.array(buffer)

        # create matlab files
        sio.savemat('trig.mat', {'trig':trig})
        sio.savemat('rec.mat', {'rec':buffer})
        sio.savemat('LABELS.mat', {'LABELS':LABELS})

rec = data_rec()
rec.begin_stream()
buffer = rec.record_loop()
rec.save_recording(buffer)


import cv2
import numpy as np

img = cv2.imread("../school_blurry.jpg", cv2.IMREAD_GRAYSCALE)
cv2.imshow("original",img)
kernel25x25 = np.ones((25,25), np.uint8)

dilate = cv2.dilate(img,kernel25x25,iterations=1)
erode = cv2.erode(img,kernel25x25,iterations=1)
prom = cv2.addWeighted(dilate,0.5,erode,0.5,0)
cv2.imshow("Promedio",prom)

width,height = img.shape[:2]
for i in xrange(width):
    for j in xrange(height):
        if (img[i,j] > prom[i,j]):
            img[i,j]= dilate[i,j]
        else:
            img[i, j] = erode[i, j]

cv2.imshow("Restaurado",img)

kernel5x5 = np.ones((7,7), np.uint8)
TopHat = cv2.morphologyEx(img,cv2.MORPH_TOPHAT,kernel5x5)
cv2.imshow("Restaurado - TopHat",img - TopHat)

cv2.waitKey(0)


import os
from typing import Optional
from pydantic import BaseModel
from fastapi import FastAPI, File, UploadFile, Body
from fastapi.responses import FileResponse
from fastapi.middleware.cors import CORSMiddleware

# Alec's basemodel
from basemodel_v1 import RHRAD_online, resultsProcesser

TEMP_PATH = os.environ.get("DATA_PATH", default="data_cache")
# dictionary to hold display messages
display_strings = {
    "low": "You may not be infected with COVID-19, though you may be asymptomatic. Maintain normal testing routine, and continue social distancing.",
    "medium": "Your resting heart rate has been elevated for a few hours. Monitor your symptoms closely and consider isolation.",
    "high": "Your resting heart rate has been elevated for many hours. You may have COVID-19 or another serious condition. Consider isolation and contact your healthcare provider if symptoms worsen.",
    "unknown": "Something went wrong - please try again. Make sure that both files are properly formatted.",
}

# Project-level imports
# from utils import make_prediction_on_data_with_appropriate_model, load_appropriate_model, extract_data_from_file

# from app.api.routes import router as api_router

# to run server, uvicorn api:app --reload
def get_application():
    app = FastAPI(title="Phresh", version="1.0.0")
    app.add_middleware(
        CORSMiddleware,
        allow_origins=["*"],  # *
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )
    # app.include_router(api_router, prefix="/api")
    return app

app = get_application()

class Item(BaseModel):
    name: str
    price: float
    is_offer: Optional[bool] = None

# an example definition of a post operation which will take in a file
@app.post("/analyze")  # /{option}
async def analyze_data(
    heart_file: UploadFile = File(...),
    step_file: UploadFile = Body(...),
):
    # default response - will be modified and returned
    response = {
        "threat_level": "unknown",
        "disp_str": "Please upload a file to recieve an analysis",
    }
    # check for filetype
    if heart_file.content_type != "text/csv" or step_file.content_type != "text/csv":
        response["disp_str"] = "Please be sure to upload a CSV file."
    else:
        # save the uploaded files to temporary files
        hr_path, step_path = os.path.join(TEMP_PATH, "hr.csv"), os.path.join(
            TEMP_PATH, "step.csv"
        )
        anomalies_path, alerts_path = (
            os.path.join(TEMP_PATH, "out_anomalies.csv"),
            os.path.join(TEMP_PATH, "out_alerts"),
        )
        with open(hr_path, "wb") as hr_file_tmp:
            hr_file_tmp.write(heart_file.file.read())
        with open(step_path, "wb") as step_file_tmp:
            step_file_tmp.write(step_file.file.read())
        try:
            # load the model with the appropriate files
            model1 = RHRAD_online(
                hr=hr_path,  # path to heart rate csv
                steps=step_path,  # path to steps csv
                myphd_id_anomalies=anomalies_path,  # where to put anomalies csv
                myphd_id_alerts=alerts_path,  # "results/IndividualRed_alerts.csv",  # where to put alerts csv
            )
            # using results paths from model1
            resultsModel = resultsProcesser(
                anomaliesCSV=anomalies_path,  # "results/IndividualRed_anomalies.csv",
                alertsCSV=alerts_path,  # "results/IndividualRed_alerts.csv",
            )
            alertLevel = (
                resultsModel.getAlertLevel()
            )  # returns a string: low, medium, or high
            anomalousHours = (
                resultsModel.getAnomalousHours()
            )  # returns a list of strings
            hours_msg = (
                "During these hours you had a high resting heart rate, relative to your baseline, which triggered the alert: "
                + ", ".join(anomalousHours)
            )
            print("RESULTS: ", alertLevel)

            response = {
                "threat_level": alertLevel,
                "disp_str": display_strings[alertLevel],
                "hours_msg": hours_msg,
            }
        except:
            response["disp_str"] = "Something was wrong with the files you uploaded."
        # alternative return statement
        # return {"threat_level":alertLevel, "disp_str":display_strings[alertLevel], "hours_msg":hours_msg}
    return response

@app.get("/get_operations")
def get_operations():
    return {"operations": ["analyze", "something else"]}


"""Define the command line parser for each utility (pipeline, iotools, etc.)."""

import abc
from argparse import ArgumentParser
from os import getcwd
from os.path import expanduser, join

PIPELINE_CATEGORIES = {
    "CLINICA_COMPULSORY": "Clinica mandatory arguments",
    "OPTIONAL": "Pipeline options",
    "CLINICA_OPTIONAL": "Clinica standard options",
    "ADVANCED": "Pipelines advanced options",
    "IOTOOLS_OPTIONS": "Optional arguments",
}

class CmdParser:
    """Abstract class to extend in order to create your command line parser.

    For pipelines, please use the 'clinica generate template' command.
    For converters, see clinica/iotools/converters/*/*_cli.py
    For iotools, see clinica/iotools/utils/data_handling_cli.py
    """

    __metaclass__ = abc.ABCMeta

    def __init__(self):
        self.reset()
        self.build()

    def build(self):
        self.define_name()
        self.define_description()
        self.set_content()
        self.define_options()

    def reset(self):
        self._args = ArgumentParser()
        self._name = None

    def set_content(self):
        self._args._positionals.title = "Mandatory arguments"
        self._args._optionals.title = "Optional arguments"
        if not self._description:
            self._description = self._name
            self._args.description = (
                "If you are not familiar with Clinica, see:\n"
                "https://aramislab.paris.inria.fr/clinica/docs/public/latest/InteractingWithClinica/"
            )
        else:
            self._args.description = (
                f"{self._description}\n\nIf you are not familiar with Clinica, see:\n"
                "https://aramislab.paris.inria.fr/clinica/docs/public/latest/InteractingWithClinica/"
            )

    @property
    def options(self):
        return self._args

    @options.setter
    def options(self, x):
        self._args = x

    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, x):
        self._name = x

    @property
    def description(self):
        return self._description

    @description.setter
    def description(self, x):
        self._description = x

    @abc.abstractmethod
    def define_name(self):
        pass

    def define_description(self):
        self._description = None

    @abc.abstractmethod
    def define_options(self):
        pass

    def add_clinica_standard_arguments(
        self,
        add_tsv_flag=True,
        add_wd_flag=True,
        add_nprocs_flag=True,
        add_overwrite_flag=False,
        add_yes_flag=False,
    ):
        clinica_standard_options = self._args.add_argument_group(
            PIPELINE_CATEGORIES["CLINICA_OPTIONAL"]
        )
        if add_tsv_flag:
            clinica_standard_options.add_argument(
                "-tsv",
                "--subjects_sessions_tsv",
                help="TSV file containing a list of subjects with their sessions.",
            )
        if add_wd_flag:
            clinica_standard_options.add_argument(
                "-wd",
                "--working_directory",
                help="Temporary directory to store pipelines intermediate results.",
            )
        if add_nprocs_flag:
            clinica_standard_options.add_argument(
                "-np",
                "--n_procs",
                metavar="N",
                type=int,
                help="Number of cores used to run in parallel.",
            )
        if add_overwrite_flag:
            clinica_standard_options.add_argument(
                "-overwrite",
                "--overwrite_outputs",
                action="store_true",
                default=False,
                help="Force overwrite of output files in CAPS folder.",
            )

        if add_yes_flag:
            clinica_standard_options.add_argument(
                "-y",
                "--yes",
                action="store_true",
                default=False,
                help="Execute the pipeline even if input images are not centered without "
                "asking for more user input.",
            )

        return clinica_standard_options

    @abc.abstractmethod
    def run_command(self, args):
        pass

    @staticmethod
    def list_to_string(list):
        """Convert list (e.g. [8, 8, 8]) to string (e.g. '8 8 8')."""
        string_without_commas_and_brackets = " ".join(str(item) for item in list)
        return string_without_commas_and_brackets

    @staticmethod
    def absolute_path(arg):
        if not arg:
            return None
        elif arg[:1] == "~":
            return expanduser(arg)
        elif arg[:2] == "./":
            return join(getcwd(), arg[2:])
        else:
            return join(getcwd(), arg)

def init_cmdparser_objects(root_parser, parser, objects):
    """Init all derived CmdParser instances with specific data.

    Args:
        root_parser: The root parser
        parser: The ArgParser node (e.g. 'run' or 'convert')
        objects: All CmdParser instances of this file
    """

    def silent_help():
        pass

    def error_message(p):
        def error(x):
            p.print_help()
            root_parser.print_help = silent_help
            exit(-1)

        return error

    def init(x):
        import argparse

        x.options = parser.add_parser(
            x.name,
            add_help=False,
            help=x.description,
            formatter_class=argparse.RawDescriptionHelpFormatter,
        )
        x.options.error = error_message(x.options)
        x.options.set_defaults(func=x.run_command)
        x.build()

    for x in objects:
        try:
            init(x)
        except BaseException:
            pass


import random
from datetime import datetime

def sleeps(i:int):

    for n in range(i):
        horario_atual = datetime.now()
        segundos = horario_atual.second
        horario_atual = datetime.now()
        segundos2 = horario_atual.second
        while segundos2==segundos:
            horario_atual = datetime.now()
            segundos2 = horario_atual.second

def Strings(s:str,i:int):
  ss=s
  while i>=len(ss):
    ss=ss+ss
  return ss[0:i]

def arraysb(x:int,y:int,value:str)->str:
  s=""
  ss=Strings(value,x)+"\n"
  for i in range(y):
      s=s+ss
  return s

def setarray(w:int,h:int,value:str,x:int,y:int,arrayss:str):
  a=arrayss
  a=arrayss[0:y*(w+1)+x]
  a=a+value
  a=a+arrayss[y*(w+1)+x+1:]
  return a

def report(xx:int,yy:int,s:str,ss:str,xxx,yyy):
    a1=s
    sss=s
    for n in range(10):
        sss=a1
        a1=setarray(xx,yy,ss,xxx[n],yyy[n],sss)
    print("\x1b[1;1d"+a1)

xx=16
yy=11
print("\x1bc\x1b[43;30m")
a1:str=arraysb(xx,yy,"*")
x=[]
y=[]
for n in range(10):
    x.append(random.randint(0,xx-1))
    y.append(random.randint(0,yy-1))

for n in range(200):
    report(xx,yy,a1," ",x,y)
    sleeps(1)
    report(xx,yy,a1,"*",x,y)
    sleeps(1)


from flask import g
from datetime import datetime, timedelta
from flask_apscheduler import APScheduler
from acdumo import create_app
from acdumo.email import send_notification_email
from acdumo.models import get_db, User
from acdumo.acdumo import (
    TICKERS, BONDS, download_historical_price_data,
    compute_signals, decide_strategy, plot_prices
)

scheduler = APScheduler()

REPORT = """Date
====
{date}

Signals
=======
| Ticker | Signal |
| ------ | ------ |
{signals}

Strategy
========
{strategy}
"""

@scheduler.task(
    'cron',
    id='send_notification_email',
    day_of_week='mon,tue,wed,thu,fri',
    hour='7',
    misfire_grace_time=900
)
def notification_email():
    date = datetime.today()
    hpd_yesterday = download_historical_price_data(date - timedelta(days=1), *TICKERS, freq='weekly')
    signals_yesterday = compute_signals(hpd_yesterday, freq='weekly')
    strategy_yesterday = decide_strategy(signals_yesterday, bonds=BONDS)
    hpd = download_historical_price_data(date, *TICKERS, freq='weekly')
    signals = compute_signals(hpd, freq='weekly')
    strategy = decide_strategy(signals, bonds=BONDS)
    formatted_date = datetime.today().strftime('%Y-%m-%d')
    signals_sans_bonds = dict((t, s) for t, s in signals.items() if t != BONDS)
    alert = None
    if strategy != strategy_yesterday:
        alert = 'A strategy change has occurred recently.'
    elif any(abs(signals[BONDS] - signals[s]) < 0.1 for s in signals_sans_bonds.keys()):
        alert = (
            'A stocks signal is within 10% of the bonds signal, so a '
            'strategy change may occur soon.'
        )
    if alert is not None:
        app = create_app(configure_scheduler=False)
        with app.app_context():
            db = get_db()
            for user in User.query.all():
                if user.subscribed:
                    send_notification_email(
                        user,
                        formatted_date,
                        REPORT.format(
                            date=formatted_date,
                            signals='\n'.join(
                                f'|    {t} | {s*100:.4}% |' for t, s in signals.items()
                            ),
                            strategy=strategy
                        ),
                        alert=alert
                    )


import os
import requests
import datetime
from dataflows import Flow

SCHEMAS = {
    'groups': {'fields': [{'name': n, 'type': t} for n, t in {
        'users': 'array',
        'display_name': 'string',
        'description': 'string',
        'image_display_url': 'string',
        'type': 'boolean',
        'created': 'datetime',
        'name': 'string',
        'is_organization': 'boolean',
        'state': 'string',
        'extras': 'array',
        'image_url': 'string',
        'title': 'string',
        'revision_id': 'string',
        'num_followers': 'integer',
        'id': 'string'
    }.items()]},
    'users': {'fields': [{'name': n, 'type': t} for n, t in {
        'display_name': 'string',
        'name': 'string',
        'created': 'datetime',
        'id': 'string',
        'sysadmin': 'boolean',
        'state': 'string',
        'fullname': 'string',
        'email': 'string',
        'number_created_packages': 'integer',
        'number_of_edits': 'integer'
    }.items()]}
}

PAGINATION_LIMITS = {
    'groups': 500
}

INITIAL_REQUEST_DATA = {
    'groups': {'all_fields': True, 'include_dataset_count': False, 'include_extras': True, 'include_users': True},
    'users': {'all_fields': True}
}

ACTION_URL_PATH = {
    'groups': '/api/3/action/group_list',
    'users': '/api/3/action/user_list',
}

def get_requests_session(CKAN_API_KEY):
    CKAN_AUTH_HEADERS = {'Authorization': CKAN_API_KEY}
    session = requests.session()
    session.headers.update(CKAN_AUTH_HEADERS)
    return session

def parse_ckan_row(row, schema):
    output_row = {}
    for field in schema['fields']:
        value = row[field['name']]
        value = {
            'string': str,
            'integer': int,
            'datetime': lambda v: datetime.datetime.strptime(v, '%Y-%m-%dT%H:%M:%S.%f'),
            'boolean': bool,
            'array': lambda v: v
        }[field['type']](value)
        output_row[field['name']] = value
    return output_row

def get(session, url, obj_type):
    schema = SCHEMAS[obj_type]
    limit = PAGINATION_LIMITS.get(obj_type)
    request_data = INITIAL_REQUEST_DATA[obj_type]
    url_path = ACTION_URL_PATH[obj_type]

    def __get_objs_page():
        groups_res = session.post(f'{url}{url_path}', data=request_data).json()
        assert groups_res and groups_res['success']
        return groups_res['result']

    def __get():
        if limit:
            offset = 0
            while True:
                request_data.update(limit=limit, offset=offset)
                objs = __get_objs_page()
                num_objs = 0
                for num_objs, obj in enumerate(objs, 1):
                    yield parse_ckan_row(obj, schema)
                if num_objs == 0:
                    break
                offset += num_objs
        else:
            for obj in __get_objs_page():
                yield parse_ckan_row(obj, schema)

    def _get(package):
        package.pkg.add_resource({'name': f'ckan_{obj_type}', 'path': f'ckan_{obj_type}.csv', 'schema': schema})
        yield package.pkg
        yield from package
        yield __get()

    return _get

def _ckan(action, obj_type, session, url):
    if action == 'get':
        if obj_type in ['group', 'groups']:
            return get(session, url, 'groups')
        elif obj_type in ['user', 'users']:
            return get(session, url, 'users')
    raise NotImplementedError

def ckan(action, obj_type, CKAN_API_KEY=None, CKAN_URL=None):
    if not CKAN_API_KEY:
        CKAN_API_KEY = os.environ.get('CKAN_API_KEY')
    if not CKAN_URL:
        CKAN_URL = os.environ.get('CKAN_URL')
    assert CKAN_API_KEY and CKAN_URL
    return _ckan(action, obj_type, get_requests_session(CKAN_API_KEY), CKAN_URL)


"""
A binary heap can be a minheap or a maxheap.
Following are some standard operations on minheap.
    1. get_min(): O(1). Get the smallest element
    2. extract_min(): O(log(n)). Pop the min element out.
    3. update_value(): O(log(n)). Update the value of a node
    4. insert/delete(): O(log(n))
    5. min_heapify(): O(log(n)). Heapify a node in the tree
"""
from typing import Optional

class MinHeap:
    def __init__(self):
        self.arr: list = []
        self.size = 0

    @staticmethod
    def parent_index(index: int) -> int:
        return int((index - 1) / 2)

    @staticmethod
    def right_child_index(index: int) -> int:
        return 2 * (index + 1)

    @staticmethod
    def left_child_index(index: int) -> int:
        return 2 * index + 1

    def exchange_nodes(self, index_1, index_2):
        self.arr[index_1], self.arr[index_2] = self.arr[index_2], self.arr[index_1]

    def move_up(self, index):
        """
        time complexity: O(log(n))
        """
        while index >= 1:
            parent_index = self.parent_index(index)

            if self.arr[index] >= self.arr[parent_index]:
                break

            self.exchange_nodes(index, parent_index)
            index = parent_index

    def min_heapify(self, index):
        """
        Heapify single node of an already heapified tree
        time complexity: O(log(n))
        """
        while index < self.size - 1:
            left_child_index = self.left_child_index(index)
            right_child_index = self.right_child_index(index)

            temp = index

            if (
                self.size > left_child_index
                and self.arr[left_child_index] < self.arr[index]
            ):
                temp = left_child_index
            if (
                self.size > right_child_index
                and self.arr[right_child_index] < self.arr[temp]
            ):
                temp = right_child_index

            if temp == index:
                break

            self.exchange_nodes(index, temp)
            index = temp

    def get_min(self) -> Optional[int]:
        return self.arr[0] if self.arr else None

    def insert(self, data: int):
        self.arr.append(data)
        self.size += 1
        self.move_up(self.size - 1)

    def extract_min(self) -> Optional[int]:
        data = self.get_min()

        if data is not None:
            self.arr[0] = self.arr[self.size - 1]
            self.min_heapify(0)
            self.size -= 1
        return data

    def update_value(self, index, new_value: int):
        if self.size <= index or index < 0:
            raise AssertionError("Invalid index")

        previous_value = self.arr[index]
        self.arr[index] = new_value

        if new_value < previous_value:
            self.move_up(index)
        elif new_value > previous_value:
            self.min_heapify(index)

    def delete(self, index: int):
        if index >= self.size or index < 0:
            raise AssertionError("Invalid index")

        self.arr[index] = -10 ** 10
        self.move_up(index)
        self.extract_min()

if __name__ == "__main__":
    min_heap = MinHeap()
    min_heap.insert(3)
    min_heap.insert(2)
    min_heap.delete(1)
    min_heap.insert(15)
    min_heap.insert(5)
    min_heap.insert(4)
    min_heap.insert(45)

    print("heap", min_heap.arr)
    print("extract min:", min_heap.extract_min())
    print("get min:", min_heap.get_min())
    min_heap.update_value(2, 1)
    print("get min", min_heap.get_min())
    print("heap", min_heap.arr)


import json
import os
import argparse

import requests
import re
import time

from tools import try_parse_float

# parse args from command line -- scan will just query, not download
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--download')
parser.add_argument('-s', '--scan')
parser.add_argument('-l', '--limit')
args = parser.parse_args()

if args.download:
    genus = args.download
elif args.scan:
    genus = args.scan

# using API available at https://www.xeno-canto.org/article/153
request_url = "https://www.xeno-canto.org/api/2/recordings"
pre_query = "?query="
query = f"gen:{genus.title()}%20type:call"

# create download destination
IMAGE_PATH = os.getcwd() + f'/images/{genus.title()}/'

print(f"Query: {query}\nDownload destination: {IMAGE_PATH}\n")

start_time = time.time()

r = requests.get(request_url + pre_query + query)

if r.status_code == 200:
    content = json.loads(r.text)
else:
    print("Error: status ", r.status_code)
    quit()

# prepare for downloading
num_recordings = int(content['numRecordings'])
num_species = int(content['numSpecies'])
recordings = content['recordings']

print(f"Payload with {num_recordings} recordings with {num_species} species after {(time.time() - start_time):0.2f} sec.")
start_time = time.time()

if args.download and num_recordings > 0:
    os.makedirs(IMAGE_PATH, exist_ok=True)

count = 0

# start downloading
for i in range(num_recordings):
    current_rec = recordings[i]

    rec_url = current_rec['url']
    rec_id = int(current_rec['id'])
    rec_genus = current_rec['gen']
    rec_species = current_rec['sp']
    req = requests.get(rec_url)
    rec_text = req.text

    print(f'Loaded id {rec_id} after {(time.time() - start_time):0.2f} sec. '
          f'Genus: {rec_genus}; Species: {rec_species}')
    start_time = time.time()

    if not args.download:
        continue

    # super hacky way to check if recording length is less than 10 sec
    # format of rec_length is e.g. td>Length	20.5 (s)
    # WARNING: this is not guaranteed to remain stable! works as of 7/21/19
    rec_length_str = re.findall('td.*\(s\)', rec_text)[0]
    rec_length_str = rec_length_str.split()[0]
    rec_length_index = rec_length_str.rfind('>') + 1
    rec_length = try_parse_float(rec_length_str[rec_length_index:])

    if rec_length is None or rec_length < 10:
        print(f"Too short: {rec_length} sec. Skipped!\n")
        continue

    # download sonogram
    # search the page corresponding to rec_id for a url ending in `large.png'. This is a sonogram (i.e. spectrograph)
    sonogram_url = re.findall('www.*large.png', rec_text)[0]
    f = open(IMAGE_PATH + str(rec_id) + '-' + rec_species + '.png', 'wb')
    f.write(requests.get('http://' + sonogram_url).content)
    f.close()
    count += 1

    print(f'Downloaded {sonogram_url} after {(time.time() - start_time):0.2f} sec.\n')
    start_time = time.time()

    if args.limit and count >= int(args.limit):
        break;

print(f"\n{count} files downloaded.\n")


# -*- coding: utf-8 -*-
"""
Created on Sat Aug 25 21:37:03 2018

@author: Okaleb
"""

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
#import random as rnd
from skimage.filters import gaussian
#from skimage.measure import label
from skimage.filters import threshold_otsu as otsu
from skimage.filters.rank import median

def prom_desvEstandar(image_box,pintar=True):
    #Definición de arreglos que contendrán las imágenes resultantes
    promedio = np.zeros((image_box.shape[0],image_box.shape[1]),dtype="float64")
    desvEstandar = np.zeros((image_box.shape[0],image_box.shape[1]),dtype="float64")
    prom_masDesvEstandar = np.zeros((image_box.shape[0],image_box.shape[1]),dtype="float64")
    prom_menosDesvEstandar = np.zeros((image_box.shape[0],image_box.shape[1]),dtype="float64")
    num_imagenes = image_box.shape[2]
    num_pixeles = image_box.shape[0] * image_box.shape[1]
    """
    Obtiene las imagenes y hace la sumatoria para calcular el promedio
    """
    promedio = np.sum(image_box,axis = 2) / num_imagenes
    """
    Calcula la desviación estándar
    """
    for i in range(num_imagenes):
        desvEstandar += (image_box[:,:,i] - promedio) ** 2

    desvEstandar = np.sqrt(desvEstandar / (num_imagenes - 1))
    """Es una forma de calcular la variable n tomando en cuenta factores de la imagen"""
    n = 4 * (np.sum(desvEstandar) / (num_pixeles - pixeles_0(desvEstandar)))
    desvEstandar += n
    """Aproxima los pixeles de las imágenes a los puntos de inflexión de la campana Gaussiana"""
    prom_masDesvEstandar = promedio + desvEstandar
    prom_menosDesvEstandar = promedio - desvEstandar
    """
    histogramaDesv = np.zeros((256,1),dtype = np.double)
    filas,columnas = desvEstandar.shape
    for i in range(filas):
        for j in range(columnas):
            histogramaDesv[int(desvEstandar[i,j])][0] += 1
    plt.plot(histogramaDesv)
    plt.axis([0,50,0,40000])
    plt.show()
    """
    if(pintar == True):
        plt.title('Promedio')
        plt.imshow(promedio,cmap='gray')
        plt.savefig("imProm_autos.jpg")
        plt.show()

        plt.title('Desviación Estándar')
        plt.imshow(desvEstandar,cmap='gray')
        plt.savefig("imDesv_autos.jpg")
        plt.show()

        plt.title('Promedio + Desviación Estándar')
        plt.imshow(prom_masDesvEstandar,cmap='gray')
        plt.savefig("imMas_autos.jpg")
        plt.show()

        plt.title('Promedio - Desviación Estándar')
        plt.imshow(prom_menosDesvEstandar,cmap='gray')
        plt.savefig("imMenos_autos.jpg")
        plt.show()

    return prom_masDesvEstandar,prom_menosDesvEstandar
def pixeles_0(imagen):
    cont = 0
    for i in range(imagen.shape[0]):
        for j in range(imagen.shape[1]):
            if(imagen[i][j] == 0):
                cont += 1
    return cont
"""Funcion que abre el video y realiza el segmentado"""
def imagen_auto_segmentada(prom_masDesvEstandar,prom_menosDesvEstandar,pintar=True):
    imagen_auto1 = np.zeros((220,360),dtype="float64")
    imagen_auto2 = np.zeros((220,360),dtype="float64")
    nombre = 0#randint(1,339)
    detect = []
    for indice in range(100):
        nombre = indice + 1
        imagen = cv.imread("Autos/segmentacion/imagenes/%d.jpg"%nombre,cv.IMREAD_GRAYSCALE)

        imagen_auto1 = imagen < prom_menosDesvEstandar
        imagen_auto2 = imagen > prom_masDesvEstandar
        imagen_or = np.bitwise_or(imagen_auto1,imagen_auto2)   #func_or(imagen_auto1,imagen_auto2)

        s = 10
        w = 5
        t = (((w-1)/2)-0.5)/s
        imagen_filtrada = gaussian(imagen_or,sigma = s,truncate = t)

        imagen_filtrada = median(imagen_or,np.ones((10,10)))
        umbral = otsu(imagen_filtrada)

        plt.title("Imagen_or_umbralada")
        plt.imshow(imagen_filtrada,cmap="gray")
        plt.savefig("Autos/segmentacion/programa/%d.jpg"%nombre)
        plt.show()
        imagen_filtrada = imagen_filtrada > umbral
        #print(imagen_filtrada.shape)
        region1 = imagen_filtrada[0:60,0:360]
        region2 = imagen_filtrada[61:120,0:360]
        region3 = imagen_filtrada[121:220,0:360]
        pix_1 = np.sum(region1)
        pix_2 = np.sum(region2)
        pix_3 = np.sum(region3)
        #print(num_pixeles)
        num_1 = pix_1 / 1600
        num_2 = pix_2 / 3000
        num_3 = pix_3 / 5400
        num_autos = num_1 + num_2 + num_3
        redondeo = round(num_autos)
        detect.append(redondeo)
        print("pix_1 = {pixeles} Num autos = {autos}".format(pixeles = pix_1,autos=num_1))
        print("pix_2 = {pixeles} Num autos = {autos}".format(pixeles = pix_2,autos=num_2))
        print("pix_3 = {pixeles} Num autos = {autos}".format(pixeles = pix_3,autos=num_3))
        print("Num_autos = {autos} redondeo = {red}".format(autos=num_autos,red=redondeo))

        """
        umbral_rango = umbral * 255
        rango_imagen_filtrada = imagen_filtrada * 255
        rango_imagen_filtrada = np.array(rango_imagen_filtrada,dtype=np.uint8)

        histograma = np.zeros((256,1),dtype = np.double)
        filas,columnas = rango_imagen_filtrada.shape
        for i in range(filas):
            for j in range(columnas):
                histograma[rango_imagen_filtrada[i,j]][0] += 1

        linea = np.zeros((256,1),dtype = np.double)
        linea[int(umbral_rango)] = 600
        """
        if(pintar == True):
            plt.title('Imagen Auto leida %d'%nombre)
            plt.imshow(imagen,cmap='gray')
            plt.show()

            plt.title('Imagen Auto < prom_menosDesvEstandar')
            plt.imshow(imagen_auto1,cmap='gray')
            plt.savefig("imMenor_autos.jpg")
            plt.show()

            plt.title('Imagen Auto > prom_masDesvEstandar')
            plt.imshow(imagen_auto2,cmap='gray')
            plt.savefig("imMayor_autos.jpg")
            plt.show()

            plt.title("Imagen_or_umbralada")
            plt.imshow(imagen_filtrada,cmap="gray")
            plt.savefig("Autos/segmentacion/programa/%d.jpg"%nombre)
            plt.show()
            imagen_filtrada = imagen_filtrada > umbral
            num_pixeles = np.sum(imagen_filtrada)
            num_autos = num_pixeles / 4300
            redondeo = round(num_autos)
            print("Num pixeles = {pixeles} Num autos = {autos} redondeo={red}".format(pixeles = num_pixeles,autos=num_autos, red=redondeo))

            plt.title("Imagen_or")
            plt.imshow(imagen_or,cmap="gray")
            plt.savefig("imOr_autos.jpg")
            plt.show()
            """
            plt.title('Histograma')
            plt.plot(histograma)
            plt.plot(linea)
            plt.text(int(umbral_rango),500,u'umbral_otsu',fontsize = 10,horizontalalignment = 'center')
            plt.axis([0,255,0,600])
            plt.show()
            """
    imagen_resultante = imagen * imagen_filtrada

    return imagen_resultante, detect

"""Lee las imágenes en la ruta especificada y las guarda en un arreglo de imágenes"""
def leer_imagenes(filas,columnas,num_imagenes,ruta):
    image_box = np.zeros((filas,columnas,num_imagenes),dtype="float64")
    for i in range(num_imagenes):
        indice = i + 1
        image_box[:,:,i] = cv.imread(ruta%indice,cv.IMREAD_GRAYSCALE)

    return image_box

if __name__ == '__main__':
    filas = 220
    columnas = 360
    num_imagenes = 63
    ruta = "Autos/no_autos/%d.jpg"

    image_box = leer_imagenes(filas,columnas,num_imagenes,ruta)
    prom_masDesvEstandar, prom_menosDesvEstandar = prom_desvEstandar(image_box,pintar=False)
    imagen_auto,detect = imagen_auto_segmentada(prom_masDesvEstandar,prom_menosDesvEstandar,pintar=False)
    """
    plt.title('Imagen Auto Segmentada')
    plt.imshow(imagen_auto,cmap='gray')
    plt.savefig("imSegmentada_autos.jpg")
    plt.show()
    """


import numpy as np
import prizmatoid as pzt
import data as da
import read_vna_csv as cs
from scipy import interpolate
from scipy import signal
from scipy import ndimage
import healpy as hp
from pygsm2016 import GlobalSkyModel2016

#ANALYSIS FUNCTIONS

def sky_model(GSM, A, minutes, poly, low, high):
    bin1 = round(1440/minutes)
    bins = np.where(A.any(axis=1))[0].shape[0]
    Gavg = np.nansum(GSM[:,:], axis=0)/bins
    Tdif = GSM - Gavg
    Pavg = np.nansum(A[:,:], axis=0)/bins
    Pdiff = A - Pavg
    pdifsq = Pdiff*Pdiff
    B = np.nansum(pdifsq[:,:], axis=0)
    ptdif = Pdiff*Tdif
    C = np.nansum(ptdif[:,:], axis=0)
    k = C/B
    obs = k*Pavg
    v = []
    for j in range(low, high+2, 2):
        v.append(j)
    v=np.array(v)
    fit = np.polyfit(np.log10(v/100), np.log10(np.abs(obs)), poly)
    power = np.zeros(int((high-low)/2)+1)
    i = 0
    while i <= poly:
        power = power + fit[poly-i]*(np.log10(v/100))**i
        i += 1
    y = 10**(power)
    return y, obs, k, fit

def find_efficiency1(ant_s11, xsmooth):
    """ Finds the antenna efficiency from the antenna s11 only. """

    #read the sll files
    s11=cs.read_vna_file(ant_s11)
    s11freqs = s11[:,0]

    #convert the s11 from dB to linear
    s11_eff =[0 for x in range((len(s11[:,0])))]
    for i in range(len(s11[:,0])):
        s11_eff[i]= 10**(s11[i,1]/20)

    #interpolate and smooth efficiency (to match frequencies used in data)
    sos = signal.butter(1, xsmooth, btype='lp', output='sos')
    lin=interpolate.interp1d(s11freqs, signal.sosfilt(sos, s11_eff), kind='slinear',fill_value="extrapolate")
    xnew= np.linspace(0,250000000, num = 4096, endpoint= True)
    eff=1-((lin(xnew))**2)

    return eff

def find_efficiency2(ant_s11, frontend_s11, xsmooth):
    """ Finds the antenna efficiency from the antenna and front end s11 data. """

    #read the sll files
    s11=cs.read_vna_file(ant_s11)
    j6=cs.read_vna_file(frontend_s11)
    s11freqs = s11[:,0]

    #convert the angles to radians and amplitudes from dB to linear
    s11_ang = [0 for x in range((len(s11[:,0])))]
    j6_ang = [0 for x in range((len(s11[:,0])))]
    s11_eff =[0 for x in range((len(s11[:,0])))]
    j6_eff =[0 for x in range((len(s11[:,0])))]
    for i in range(len(s11[:,0])):
        s11_ang[i] = s11[i,2]*(np.pi/180)
        j6_ang[i] = j6[i,2]*(np.pi/180)
        s11_eff[i]= 10**(s11[i,1]/20)
        j6_eff[i]= 10**(j6[i,1]/20)

    #find the real and imaginary parts of the s11 data
    s11_real = [0 for x in range((len(s11[:,0])))]
    s11_imag = [0 for x in range((len(s11[:,0])))]
    j6_real = [0 for x in range((len(s11[:,0])))]
    j6_imag = [0 for x in range((len(s11[:,0])))]
    for i in range(len(s11[:,0])):
        s11_real[i]= s11_eff[i]*np.cos(s11_ang[i])
        s11_imag[i]= s11_eff[i]*np.sin(s11_ang[i])
        j6_real[i]= j6_eff[i]*np.cos(j6_ang[i])
        j6_imag[i]= j6_eff[i]*np.sin(j6_ang[i])

    #convert the reflection coefficients (s11) to impedences (Z)
    Z_s11_real = [0 for x in range((len(s11[:,0])))]
    Z_s11_imag = [0 for x in range((len(s11[:,0])))]
    Z_j6_real = [0 for x in range((len(s11[:,0])))]
    Z_j6_imag = [0 for x in range((len(s11[:,0])))]
    for i in range(len(s11[:,0])):
        Z_s11_imag[i] =(((50*s11_imag[i])*(1-s11_real[i]))-((50+(50*s11_real[i]))*(0-s11_imag[i])))/(((1-s11_real[i])**2)+((0-s11_imag[i])**2))
        Z_s11_real[i] =(((50+(50*s11_real[i]))*(1-s11_real[i])) + ((50*s11_imag[i])*(0-s11_imag[i])))/(((1-s11_real[i])**2)+((0-s11_imag[i])**2))
        Z_j6_real[i] = (((50+(50*j6_real[i]))*(1-j6_real[i])) + ((50*j6_imag[i])*(0-j6_imag[i])))/(((1-j6_real[i])**2)+((0-j6_imag[i])**2))
        Z_j6_imag[i] = (((50*j6_imag[i])*(1-j6_real[i]))-((50+(50*j6_real[i]))*(0-j6_imag[i])))/(((1-j6_real[i])**2)+((0-j6_imag[i])**2))

   #find the top and bottom of the total reflection co-efficent (gamma) after combining the antenna and front end
    topreal = [0 for x in range((len(s11[:,0])))]
    botreal = [0 for x in range((len(s11[:,0])))]
    topimag = [0 for x in range((len(s11[:,0])))]
    botimag = [0 for x in range((len(s11[:,0])))]
    for i in range(len(s11[:,0])):
        topreal[i] = Z_s11_real[i] - Z_j6_real[i]
        botreal[i] = Z_s11_real[i] + Z_j6_real[i]
        topimag[i] = Z_s11_imag[i] + Z_j6_imag[i]
        botimag[i] = Z_s11_imag[i] + Z_j6_imag[i]

    #find the real and imaginary parts of gamma
    gammareal = [0 for x in range((len(s11[:,0])))]
    gammaimag = [0 for x in range((len(s11[:,0])))]
    for i in range(len(s11[:,0])):
        gammareal[i] = ((topreal[i]*botreal[i])+(topimag[i]*botimag[i]))/((botreal[i]**2)+(botimag[i]**2))
        gammaimag[i] = ((topimag[i]*botreal[i])-(topreal[i]*botimag[i]))/((botreal[i]**2)+(botimag[i]**2))

    mag = [0 for x in range((len(s11[:,0])))]
    for i in range(len(s11[:,0])):
        mag[i] = np.sqrt((gammareal[i])**2 + (gammaimag[i])**2)

    #interpolate and smooth gamma to match frequencies used in data
    sos = signal.butter(1, xsmooth, btype='lp', output='sos')
    lingamma=interpolate.interp1d(s11freqs, signal.sosfilt(sos, mag), kind='slinear',fill_value="extrapolate")
    xnew= np.linspace(0,250000000, num = 4096, endpoint= True)
    eff=1-((lingamma(xnew))**2)

    return eff

def find_short(shorton, prizm_data, newlist_antend, antenna, polarization):

    somelist1 = shorton
    newlist_shortend = []

    for i in range(len(somelist1)-1):
        if somelist1[i+1] != somelist1[i] + 1 :
            newlist_shortend.append(somelist1[i])

    x=len(somelist1)-1
    newlist_shortend.append(somelist1[x])

    newlist_shortstart = []

    for i in range(1, len(somelist1)):
        if somelist1[i-1] != somelist1[i] - 1:
            newlist_shortstart.append(somelist1[i])

    newlist_shortstart.insert(0,somelist1[0])

    print(len(newlist_shortstart))

    short_lengths = list(np.array(newlist_shortend) - np.array(newlist_shortstart))
    short = []
    for i in range(0, len(newlist_shortend)):
        sum = np.zeros(4096)
        for j in range(0, short_lengths[i]+1):
            sum = sum + prizm_data[antenna][polarization][newlist_shortstart[i]+j]
        short.append(sum/(short_lengths[i]+1))

    dif=[]
    for i in range(0, len(newlist_shortend)-1):
        dif.append(newlist_shortend[i+1] - newlist_shortend[i])
    base = np.average(dif)

    for i in range(0, len(dif)):
        if dif[i] > base + base/2:
            short.insert(i+1, pzt.interpolate_short(prizm_data, antenna, polarization)[i])

    if newlist_shortend[-1] < newlist_antend[-1]:
        short.append(pzt.interpolate_short(prizm_data, antenna, polarization)[-1])

    return short


import sqlite3
import os

# #
# 本模块用来对路径为 "path_db" 数据库进行简单操作，一般步骤如下
# 1. 实例化DbManger，查看该数据库包含的所有表。可对数据库进行：
#       查看已有表，表的列； 新建表，重命名表，备份表，删除列。
# 2. 实例化TableManger，可对表进行：
#       查看列，查看行和id，显示并返回行，行的查找； 插入新行，删除行，修改行。
#  注意：所有表内部数据以隐藏的列 "id" 进行传递，id列会自动生成。
# #

__Author__ = "J"

class DbManager:
    def __init__(self, path_db):
        self.conn = sqlite3.connect(path_db)
        self.c = self.conn.cursor()
        self.__tables = self.getAllTables()

    def __del__(self):
        try:
            self.c.close()
            self.conn.close()
            print("The database closed.")
        except:
            print("关闭数据库异常!!!")

    def createNewTable(self, table_name, columns):
        # columns是一个tuple或者list数据
        if table_name not in self.__tables:
            sql = "CREATE TABLE IF NOT EXISTS %s(id integer PRIMARY KEY AUTOINCREMENT" % table_name + (",%s text"*len(columns)) % columns + ")"
            self.c.execute(sql)
            self.__tables = self.getAllTables()
        else:
            print("The table %r has already existed!" % table_name)

    def dropTable(self, table_name):
        if table_name in self.__tables:
            sql = "DROP TABLE IF EXISTS %s" % table_name
            self.c.execute(sql)
            self.__tables = self.getAllTables()
        else:
            print("There is no table named %r" % table_name)

    def renameTable(self, old_table_name, new_table_name):
        if old_table_name in self.__tables and new_table_name not in self.__tables:
            sql = "ALTER TABLE %s RENAME TO %s" % (old_table_name, new_table_name)
            self.c.execute(sql)
            self.conn.commit()
        elif old_table_name not in self.__tables:
            print("There is no table named %r", old_table_name)
        else:
            print("The new name %r has already existed in this database" % new_table_name)

    def backupTable(self, original_table_name, new_table_name=None):
        # 将表复制到同一个数据库，每行的id不会发生变化
        if original_table_name in self.__tables:
            if new_table_name == None:
                new_table_name = original_table_name + "_backup"
            while new_table_name in self.__tables:
                new_table_name = new_table_name + "_副本"
            print(new_table_name)

            columns = self.getTableColumns(original_table_name)
            sql1 = "CREATE TABLE IF NOT EXISTS %s(id integer PRIMARY KEY AUTOINCREMENT" % new_table_name + (",%s text"*len(columns)) % columns + ")"
            sql2 = "INSERT INTO %s" % new_table_name + " SELECT * FROM %s" % original_table_name
            self.c.execute(sql1)
            self.c.execute(sql2)
            self.conn.commit()
        else:
            print("There is no table named %s", original_table_name)

    def copyTableToOtherDb(self, table_name, path_other_database, Reorder=False):
        # Reorder为True，将表重新整理，移除空余的id，并复制到新的数据库中。例如原表有id为(1, 2, 4)，新的表中id为(1, 2, 3)
        table_names = []
        other_db_conn = sqlite3.connect(path_other_database)
        other_db_cursor = other_db_conn.cursor()
        other_db_cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
        temp = other_db_cursor.fetchall()
        for table in temp:
            table_names.append(table[0])
        if table_name in table_names:
            print("A table named %s has already exsited in database %s" % (table_name, os.path.basename(path_other_database)))
            return table_name
        columns = self.getTableColumns(table_name)
        self.c.execute("SELECT * FROM %s" % table_name)
        datas = self.c.fetchall()
        sql1 = "CREATE TABLE IF NOT EXISTS %s(id integer PRIMARY KEY AUTOINCREMENT" % table_name + (",%s text" * len(columns)) % columns + ")"
        other_db_cursor.execute(sql1)
        for data in datas:
            if Reorder:
                sql2 = "INSERT INTO %s(" % table_name + (((",%s"*len(columns)).strip(',')) % columns) + ") VALUES(" + (",?"*len(columns)).strip(',') + ")"
                other_db_cursor.execute(sql2, tuple(data[1:]))
            else:
                sql2 = "INSERT INTO %s " % table_name + "VALUES(" + (",?"*len(data)).strip(',') + ")"
                other_db_cursor.execute(sql2, tuple(data))
        other_db_conn.commit()
        return table_name

    def deleteTableColumns(self, table_name, *columns_to_del, Reorder=False):
        # Reorder为True，将表重新整理，移除空余的id，。例如原表有id为(1, 2, 4)，新的表中id为(1, 2, 3)
        columns_original = list(self.getTableColumns(table_name))
        temp_table = "temp_table"
        for column in columns_to_del:
            if column not in columns_original:
                print("Columns need to be deleted are not in table %s, the table is not modified!" % table_name)
                return
            else:
                columns_original.pop(columns_original.index(column))
        new_columns = tuple(columns_original)
        sql1 = "CREATE TABLE IF NOT EXISTS %s(id integer PRIMARY KEY AUTOINCREMENT" % temp_table + (",%s text" * len(new_columns)) % new_columns + ")"
        if Reorder:
            sql2 = "INSERT INTO %s(" % temp_table + ((",%s"*len(new_columns)) % new_columns).strip(",") + ")" + " SELECT " + ((",%s"*len(new_columns)) % new_columns).strip(",") + " FROM %s" % table_name
        else:
            sql2 = "INSERT INTO %s" % temp_table + " SELECT id" + (",%s"*len(new_columns)) % new_columns + " FROM %s" % table_name
        self.c.execute(sql1)
        self.c.execute(sql2)
        self.conn.commit()
        self.dropTable(table_name)
        self.renameTable(temp_table, table_name)

    def addTableColumns(self, table_name, *columns_to_add):
        columns_original = self.getTableColumns(table_name)
        for column in columns_to_add:
            if column in columns_original:
                print("Columns need to be add have already existed in table %s, the table is not modified!" % table_name)
                return
        for column in columns_to_add:
            self.c.execute("ALTER TABLE %s ADD COLUMN %s text" % (table_name, column))
        self.conn.commit()

    def renameTableColumns(self, table_name, **columns_to_rename):
        columns_original = list(self.getTableColumns(table_name))
        for k, v in columns_to_rename.items():
            if k not in columns_original:
                print("Columns need to be renamed are not in table %s, the table is not modified!" % table_name)
                return
            else:
                columns_original[columns_original.index(k)] = v
        new_columns = tuple(columns_original)
        temp_table = "temp_table"
        sql1 = "CREATE TABLE IF NOT EXISTS %s(id integer PRIMARY KEY AUTOINCREMENT" % temp_table + (",%s text" * len(new_columns)) % new_columns + ")"
        sql2 = "INSERT INTO %s" % temp_table + " SELECT * FROM %s" % table_name
        self.c.execute(sql1)
        self.c.execute(sql2)
        self.c.execute("DROP TABLE IF EXISTS %s" % table_name)
        self.c.execute("ALTER TABLE %s RENAME TO %s" % (temp_table, table_name))
        self.conn.commit()

    # ###################### 以下所有为查询数据相关函数 ######################
    def getAllTables(self):
        table_names = []
        self.c.execute("SELECT name FROM sqlite_master WHERE type='table'")
        temp = self.c.fetchall()
        for table in temp:
            table_names.append(table[0])
        table_names = tuple(table_names)
        return table_names

    def getTableColumns(self, table_name):
        # 获取表中列的名字
        self.c.execute("PRAGMA table_info(%s)" % table_name)
        temp = self.c.fetchall()
        columns = []
        for i in temp:
            columns.append(i[1])
        columns = tuple(columns)
        return columns[1:]

    def execSQL(self, sql):
        self.c.execute(sql)
        return self.c.fetchall()

class TableManager:
    def __init__(self, table_name, path_db, columns4init=('如果不是创建新表，不需要填写该参数',)):
        # 修改表内容后自动保存
        self.auto_commit = True
        self.table = table_name
        self.columns = columns4init

        self.conn = sqlite3.connect(path_db)
        self.c = self.conn.cursor()
        sql = ("CREATE TABLE IF NOT EXISTS %s(id integer PRIMARY KEY AUTOINCREMENT" + ",%s text" * len(self.columns) + ")") % ((self.table,) + self.columns)
        self.c.execute(sql)
        self.columns = self._getColumns()

    def __del__(self):
        try:
            self.c.close()
            self.conn.close()
        except:
            print("Fatal Error: The database: %s can't be closed!!!" % self.table)

    def appendLine(self, **columns):
        # 表末尾添加一条新行
        col = []
        val = []
        for k, v in columns.items():
            col.append(k)
            val.append(v)
        sql = "INSERT INTO %s(" % self.table + ((",%s" * len(col)) % tuple(col)).strip(',') + ") VALUES(" + (",?" * len(col)).strip(',') + ")"
        self.c.execute(sql, tuple(val))
        if self.auto_commit:
            self.conn.commit()

    def deleteLine(self, id):
        # 删除id对应的某一行
        self.c.execute("DELETE FROM %s WHERE id=?" % self.table, (str(id),))
        if self.auto_commit:
            self.conn.commit()

    def _deleteLines(self, ids):
        # 删除tuple内所有id对应的行
        for id in ids:
            self.c.execute("DELETE FROM %s WHERE id=?" % self.table, (str(id),))
        if self.auto_commit:
            self.conn.commit()

    def modifyLine(self, id, **columns):
        for k, v in columns.items():
            self.c.execute("UPDATE %s SET %s=? WHERE id=?" % (self.table, str(k)), (str(v), str(id)))
        if self.auto_commit:
            self.conn.commit()

    def commitData(self):
        # 手动提交数据
        self.conn.commit()

    def rollbackData(self):
        # 手动回滚数据操作
        self.conn.rollback()

    #  ###################### 以下所有为查询数据相关函数 ######################

    def _getColumns(self):
        # 获取表中列的名字
        self.c.execute("PRAGMA table_info(%s)" % self.table)
        temp = self.c.fetchall()
        columns = []
        for i in temp:
            columns.append(i[1])
        columns = tuple(columns)
        return columns[1:]

    def getColumns(self):
        return self.columns

    def countLines(self):
        sql = "SELECT count(*) FROM %s" % self.table
        self.c.execute(sql)
        result = self.c.fetchone()
        return result[0]

    def getAllId(self, orderby=None, desc=False):
        # 返回所有行id，例如(1, 2, 3, 4, ...)
        # 按column对所有id进行升序排列，当desc为True时，降序排列
        ids = []
        if orderby is None or orderby not in self.columns:
            sql = 'SELECT id FROM %s' % self.table
        else:
            if desc:
                sql = 'SELECT id FROM %s ORDER BY %s DESC' % (self.table, orderby)
            else:
                sql = 'SELECT id FROM %s ORDER BY %s' % (self.table, orderby)
        self.c.execute(sql)
        result = self.c.fetchall()
        for i in result:
            ids.append(i[0])
        ids = tuple(ids)
        return ids

    def displayBriefData(self, id, *columns):
        if len(columns) == 0:
            columns = (self.columns[0],)
        sql = "SELECT " + (("%s,"*len(columns)) % columns).strip(',') + " FROM %s WHERE id=?" % self.table
        self.c.execute(sql, (str(id),))
        line = self.c.fetchone()
        return line

    def displayBriefDatas(self, ids, *columns):
        lines = []
        for id in ids:
            lines.append(self.displayBriefData(id, *columns))
        lines = tuple(lines)
        return lines

    def displayDetailedData(self, id):
        if id not in self.getAllId():
            return
        self.c.execute("SELECT * FROM %s WHERE id=?" % self.table, (str(id), ))
        line = self.c.fetchone()
        # return line[1:]
        temp_cols = list(self.columns)
        temp_cols.insert(0, "id")
        return dict(zip(temp_cols, line))

    def searchDataByKey(self, **column):
        # 模糊匹配，返回id，例如参数为title="bug"，结果返回(1, 2, 3,...)
        ids = []
        key = []
        var = []
        mix = []
        if len(column) == 0:
            print("没有进行匹配，请输入至少1个列名及对应关键字")
            return ()
        for k, v in column.items():
            key.append(str(k))
            var.append(str(v))
            mix.append('%'+str(v)+'%')
        # 把key中的每项依次间隔插入到mix中，比如[1,3,5]插入到[2,4,6]后得到[1,2,3,4,5,6]
        for n in range(len(key)):
            mix.insert(n*2, key[n])
        sql = "SELECT id FROM %s WHERE " % self.table + "%s LIKE %r" % tuple(mix[:2]) + " AND %s LIKE %r" * (len(key) - 1) % tuple(mix[2:])
        self.c.execute(sql)
        result = self.c.fetchall()
        for item in result:
            ids.append(item[0])
        '''
        # 打印各个列表关键字匹配的结果
        dict_ids = {}
        for k, v in column.items():
            ids.clear()
            sql = "SELECT id FROM %s WHERE %s LIKE %r" % (self.__table_name, str(k), '%'+str(v)+'%')
            self.c.execute(sql)
            result = self.c.fetchall()
            for item in result:
                ids.append(item[0])
            dict_ids[k] = tuple(ids)
        print(dict_ids)
        '''
        ids = tuple(ids)
        return ids

    def searchDataInTable(self, keyword, *exclude):
        ids = set()
        target_cols = tuple(col for col in self.columns if col not in exclude)
        for col in target_cols:
            sql = 'SELECT id FROM %s WHERE ' % self.table + "%s LIKE %r" % (col, '%'+keyword + '%')
            self.c.execute(sql)
            result = self.c.fetchall()
            for item in result:
                ids.add(item[0])
        return tuple(ids)

if __name__ == '__main__':
    import re
    import time

    # 多表模糊匹配查询速度测试
    time_mark = time.time()
    t_vers = {
        'V01': TableManager('t_V01', './libfiles/soft.db'),
        'V02': TableManager('t_V02', './libfiles/soft.db'),
        'V03V04': TableManager('t_V03V04', './libfiles/soft.db'),
        'V05': TableManager('t_V05', './libfiles/soft.db'),
        'T05': TableManager('t_T05', './libfiles/soft.db')
    }

    string = '华 麦 邵 阀门'
    ret_data = {'itemsNum': 0, 'items': {}}
    keywords = re.split(r'\s+', string)
    key = 0
    for soft_type in t_vers:
        ids_for_each_keys = []

        for keyword in keywords:
            ids_for_each_keys.append(t_vers[soft_type].searchDataInTable(keyword, 'path'))
        ids = ids_for_each_keys[0]
        for i in range(1, len(ids_for_each_keys)):
            ids = tuple(id for id in ids if id in ids_for_each_keys[i])
        ret_data['itemsNum'] += len(ids)
        for soft_id in ids:
            ret_data['items'][key] = t_vers[soft_type].displayDetailedData(soft_id)
            key += 1
    print(ret_data)
    print('一共耗时: ', time.time() - time_mark)