import django
import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "COVID_19_API.settings.local")
django.setup()

from bs4 import BeautifulSoup
import requests

from api.models import *

def main():
	url = 'https://www.worldometers.info/coronavirus/'
	r = requests.get(url)
	soup = BeautifulSoup(r.text)
	table = soup.find('table', id='main_table_countries_today')
	table_body = table.find('tbody')
	rows = table_body.find_all('tr')
	data = []

	for row in rows:
		cols = row.find_all('td')
		cols = [ele.text.strip() for ele in cols]
		obj = CoronaCounrty.objects.get_or_create(
			country=cols[0],
		)
		CoronaCounrty.objects.filter(id=obj[0].id).update(
			total_cases=cols[1].replace(",", "").replace("+", ""),
			new_cases=cols[2].replace(",", "").replace("+", ""),
			total_deaths=cols[3].replace(",", "").replace("+", ""),
			new_deaths=cols[4].replace(",", "").replace("+", ""),
			total_recovered=cols[5].replace(",", "").replace("+", ""),
			active_cases=cols[6].replace(",", "").replace("+", ""),
			serious_cases=cols[7].replace(",", "").replace("+", ""),
			tot_cases=cols[8].replace(",", "").replace("+", "")
		)
		data.append(cols)

	print(data)

if __name__ == "__main__":
	main()


# 本代码目的：统计每天市场各股票涨跌幅的偏度数据并绘图
# 平台：Mindgo

import pandas as pd
import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

###############################################################################
# part1. 基本信息赋值
begin = datetime.date(2007, 1, 1)                  # 设定起始时间
end = datetime.date(2018, 9, 7)                    # 设定终止时间

Tdays = get_trade_days(begin, end)

# threshold = ['10%','15%','20%']
# result = pd.DataFrame(index = threshold, columns = Tdays)

# 初始化一个以日期为索引，存储偏度数据的dataframe
skewness_df = pd.DataFrame()

###############################################################################
# part2. 计算
for Today in Tdays:
    print(Today.strftime("%Y-%m-%d"))
    universe = list(get_all_securities('stock', Today).index)  # 全A股
    # universe = get_index_stocks('000300.SH', Today)  # 沪深300
    # 获取该天的开盘收盘价数据，返回值为字典，键为股票代码、值为以日期为行索引、
    # 以数据名为列索引的dataframe
    stock_dic = get_candle_stick(universe, end_date=Today.strftime("%Y%m%d"),
                                 fre_step='1d', fields=['open', 'close'],
                                 skip_paused=False, fq='pre', bar_count=1, is_panel=0)

    # 初始化一个以股票代码为索引，存储涨跌幅数据的dataframe，用于后续计算偏度
    all_stock_data = pd.DataFrame()
    # 将数据存入all_stock_data中
    for stock in list(stock_dic.keys()):
        stock_data = stock_dic[stock]
        try:
            # 添加一列为涨跌幅，并计算出结果
            stock_data["change"] = stock_data["close"] / stock_data["open"] - 1
            # 将数据存入all_stock_data中
            all_stock_data = all_stock_data.append(pd.DataFrame(
                {"change": stock_data.iloc[0]["change"]}, index=[stock]
            ))
        except Exception:
            pass

    # 计算偏度
    skewness = all_stock_data.skew(axis=0, skipna=True).iloc[0]
    # 将该日期的偏度存入skewness_df中
    skewness_df = skewness_df.append(pd.DataFrame(
        {'skewness': skewness}, index=[Today]
    ))

###############################################################################
# part3. 绘图
# 设定图的大小
plt.figure(figsize=(24, 6))
# 日期序列
x = skewness_df.index
# 偏度序列
y = skewness_df['skewness']
# 設置x軸主刻度顯示格式（日期）
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))
# 設置x軸主刻度間距
plt.gca().xaxis.set_major_locator(mdates.MonthLocator(interval=12))
# 绘图
plt.plot(x, y, color='black')
# 设定图的名字，并设定字体大小
plt.title("Skewness Over Time", fontsize=16)
# 出图
plt.show()


#!/usr/bin/env python

import datetime
import re

from sqlalchemy import Column, MetaData, Table
from sqlalchemy import Boolean, Date, DateTime, Float, Integer, String, Time
from sqlalchemy.schema import CreateTable

DIALECTS = {
    'access': 'access.base',
    'firebird': 'firebird.kinterbasdb',
    'informix': 'informix.informixdb',
    'maxdb': 'maxdb.sapdb',
    'mssql': 'mssql.pyodbc',
    'mysql': 'mysql.mysqlconnector',
    'oracle': 'oracle.cx_oracle',
    'postgresql': 'postgresql.psycopg2',
    'sqlite': 'sqlite.pysqlite',
    'sybase': 'sybase.pyodbc'
}

NULL_COLUMN_MAX_LENGTH = 32

def make_column(column):
    """
    Creates a sqlalchemy column from a csvkit Column.
    """
    sql_column_kwargs = {'nullable': False}
    sql_type_kwargs = {}

    column_types = {
        bool: Boolean,
        int: Integer,
        float: Float,
        datetime.datetime: DateTime,
        datetime.date: Date,
        datetime.time: Time,
        None: String,
        unicode: String
        }

    max_lengths = {
        None: NULL_COLUMN_MAX_LENGTH,
        unicode: column.max_length
        }

    if column.type in column_types:
        sql_column_type = column_types[column.type]
    else:
        raise ValueError('Unexpected normalized column type: %s' % column.type)

    if column.type in max_lengths:
        sql_type_kwargs['length'] = max_lengths[column.type]

    sql_column_kwargs['nullable'] = column.nullable

    return Column(column.name, sql_column_type(**sql_type_kwargs), **sql_column_kwargs)

def make_table(csv_table, name='table_name'):
    """
    Creates a sqlalchemy table from a csvkit Table.
    """
    metadata = MetaData()
    sql_table = Table(csv_table.name, metadata)

    for column in csv_table:
        sql_table.append_column(make_column(column))

    return sql_table

def make_create_table_statement(sql_table, dialect=None):
    """
    Generates a CREATE TABLE statement for a sqlalchemy table.
    """
    if dialect:
        module = __import__('sqlalchemy.dialects.%s' % DIALECTS[dialect], fromlist=['dialect'])
        sql_dialect = module.dialect()
    else:
        sql_dialect = None

    return unicode(CreateTable(sql_table).compile(dialect=sql_dialect)).strip() + ';'

def de_parameterize_insert_query(statement, values, dialect=None):
    """
    Insert row values in a parameterized insert statement generated by sqlalchemy.

    Note: all the hard-coded processing in this function sucks, but sqlalchemy doesn't expose a way of doing this as part of the dialect-specific query processing.
    """
    # All dialects use VALUES to set off the start of the parameter clause
    start_params = statement.index('VALUES (') + 7
    params_clause = statement[start_params:]

    # Get an iterator for the values (which are likely a tuple)
    values = iter(values)

    # Use probably ill-advised closure magic to format values in way SQL understands
    def value_formatter(*args):
        v = values.next()

        if v is None:
            return 'NULL'
        elif type(v) == bool:
            return unicode(v).upper()
        elif type(v) == unicode:
            return u'\'%s\'' % v
        else:
            return unicode(v)

    # ':column_name' syntax
    if dialect in ['access', 'firebird', 'maxdb', 'mssql', 'oracle', 'sybase', None]:
        params_clause = re.sub('(:"?.+?"?)(?=[,)])', value_formatter, params_clause)
    # '?' syntax
    elif dialect in ['informix', 'sqlite']:
        params_clause = re.sub('(\?)(?=[,)])', value_formatter, params_clause)
    # '%s' syntax
    elif dialect in ['mysql']:
        params_clause = re.sub('(%s)(?=[,)])', value_formatter, params_clause)
    # '%(column_name)s' syntax
    elif dialect in ['postgresql']:
        params_clause = re.sub('(%("?.+?"?)s)(?=[,)])', value_formatter, params_clause)

    return statement[:start_params] + params_clause

def make_insert_statement(sql_table, values, dialect=None):
    """
    Generates INSERT statements for data in a Table using a sqlalchemy dialect.
    """
    if dialect:
        module = __import__('sqlalchemy.dialects.%s' % DIALECTS[dialect], fromlist=['dialect'])
        sql_dialect = module.dialect()
    else:
        sql_dialect = None

    insert = unicode(sql_table.insert().compile(dialect=sql_dialect))
    statement = de_parameterize_insert_query(insert, values, dialect=dialect)

    return statement.strip() + ';'


import random

import numpy as np
import criticalnet as cnet
from scipy.misc import face
from PIL import Image
import networkx as nx
import pickle
from criticalnet.data.sample import tanmay_images
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import matplotlib.pylab as plt

images, image_name_list = tanmay_images()
clutch_dict = dict()
for image_name in image_name_list:
    image_name_length = len(image_name)
    clutch = image_name[:image_name_length - 16]
    egg_name = image_name[image_name_length - 16:]
    if clutch not in clutch_dict:
        clutch_dict[clutch] = [egg_name]
    else:
        clutch_dict[clutch].append(egg_name)

f = open("move_extra_eggs.sh", "w")

for clutch, eggs in clutch_dict.items():
    clutch_size = len(eggs)
    if clutch_size > 2:
        num_to_remove = clutch_size - 2
        chosen_num = []
        for i in range(num_to_remove):
            random_num = random.randint(0, clutch_size - 1)
            while random_num in chosen_num:
                random_num = random.randint(0, clutch_size - 1)
            chosen_num.append(random_num)
            f.write(f"mv {clutch}{eggs[random_num]} ../extra_eggs\n")

f.close()
print("here")

def get_cn():
    images, image_name_list = tanmay_images()
    # sorted_images = [img for _, img in sorted(zip(image_name_list, images))]
    graphs = []
    for i, im in enumerate(images):
        net0 = cnet.CriticalNet(image=im, ktimes=110, lap_mode='DOG')
        net0.compute(beta=8, extrema_dist=30, draw=False)
        G = net0.G
        graphs.append(G)
        print("%d image finished." % i)
    pickle_out = open("full_graph.pickle", "wb")
    pickle.dump(graphs, pickle_out)
    pickle_out.close()

# get_cn()

def draw_cn():
    images, image_name_list = tanmay_images()
    pickle_in = open("graph.pickle", "rb")
    graphs = pickle.load(pickle_in)
    # for im in images:
    net0 = cnet.CriticalNet(image=images[1], ktimes=110, lap_mode='DOG', G=graphs[1])
    net0.draw()
    net0 = cnet.CriticalNet(image=images[5], ktimes=110, lap_mode='DOG', G=graphs[5])
    net0.draw()

# draw_cn()

def draw_heatmap():
    path_to_csv = "edited_num_match_edges_matrix.csv"
    df = pd.read_csv(path_to_csv, index_col=0)
    # plt.imshow(df,cmap='hot',interpolation='nearest')
    # plt.show()
    ax = sns.heatmap(df, cmap="YlGnBu", square=True)
    plt.show()

# draw_heatmap()


#!/usr/bin/env python
#
# Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
#

# This script parses outputs of HLTMPPU ROS hit simulation, converts them
# to JSON and appends to extra-results.json

import logging
import re
import sys
import os.path
import json
from collections import OrderedDict
from enum import IntEnum

class LastUpdatedOrderedDict(OrderedDict):
    'Store items in the order the keys were last added'

    def __setitem__(self, key, value):
        if key in self:
            del self[key]
        OrderedDict.__setitem__(self, key, value)

class LookUpDict(IntEnum):
    'Defines columns in hitstats file'
    ROS_NAME = 0
    NUM_ROBS = 1
    TOTAL_HITS_PER_EVT = 2
    TOTAL_ROB_FRAC_PER_EVT = 3
    TOTAL_BYTES_PER_EVT = 4
    NOBLD_HITS_PER_EVT = 5
    NOBLD_ROB_FRAC_PER_EVT = 6
    NOBLD_BYTES_PER_EVT = 7
    EVBLD_HITS_PER_EVT = 8
    EVBLD_ROB_FRAC_PER_EVT = 9
    EVBLD_BYTES_PER_EVT = 10

def get_lines(pattern, filename):
    if not os.path.isfile(filename):
        logging.warning("Cannot open file {}".format(filename))
        return None
    with open(filename) as logfile:
        lines = re.findall("{}.*$".format(pattern),
                           logfile.read(), re.MULTILINE)
        if len(lines) == 0:
            logging.warning("Could not find pattern \"{}\" in file {}".format(pattern, filename))
            return None
        return lines

def split_lines(pattern, lines):
    result = []
    for line in lines:
        split_result = []
        for token in re.split(pattern, line):
            token = token.replace(' ', '')
            if not token == '':
                split_result.append(token)
        result.append(split_result)
    return result

def main():
    logging.basicConfig(stream=sys.stdout,
                        format='%(levelname)-8s %(message)s',
                        level=logging.INFO)

    ros_stats = LastUpdatedOrderedDict()

    lines = get_lines(r'^\s*ROS-', 'ros_hitstats_reject.txt')
    table = split_lines(r'\||,', lines)

    for row in table:
        name = row[LookUpDict.ROS_NAME]
        hit_rate = row[LookUpDict.TOTAL_ROB_FRAC_PER_EVT]
        data_rate = row[LookUpDict.TOTAL_BYTES_PER_EVT]
        ros_stats[name] = LastUpdatedOrderedDict({
            'hits-per-evt': hit_rate,
            'kbytes-per-evt': float(data_rate)/1000
        })

    data = LastUpdatedOrderedDict()
    output_file = 'extra-results.json'
    if os.path.isfile(output_file):
        with open(output_file) as f:
            data.update(json.load(f, object_pairs_hook=LastUpdatedOrderedDict))

    data.update({'ros-stats': ros_stats})

    with open(output_file, 'w') as f:
        json.dump(data, f, indent=4)

if "__main__" in __name__:
    sys.exit(main())


import re
from requests.packages.urllib3.exceptions import InsecureRequestWarning
from bs4 import BeautifulSoup

def getData(content):
    soup = BeautifulSoup(content, 'html.parser')
    content = soup.find(id="article-detail-content")
    text = content.get_text()

    lines = []
    xwords = ["googletag.cmd.push", "<!--", "-->", "\/\/", "OA_show"]
    pattern = r"|".join(xwords)

    for line in text.splitlines():
        if len(line.strip()) > 0:
            if re.search(pattern, str(line)) == None:
                lines.append(line)

    text = " ".join(lines)
    text = re.sub(u'\xa0', "", text).lower()
    text = re.sub(r'\,', "", text)
    text = re.sub(r'\.', " ", text)
    text = re.sub(r'\t|\"|\'', "", text).lower()
    title = soup.find("h1").get_text()
    words = text.split(" ")

    return[title, len(words)]


import math
from typing import List, Callable

import torch
from torch import nn as nn, Tensor
from torch.nn import Parameter

class GraphConvolutionLayer(nn.Module):
    """
    Simple GCN layer, similar to https://arxiv.org/abs/1609.02907
    """

    def __init__(self, in_features, out_features, bias=True, add_identity: bool=True):
        super().__init__()
        self.in_features = in_features
        self.out_features = out_features
        self.add_identity = add_identity
        self.weight = Parameter(torch.FloatTensor(in_features, out_features))
        if bias:
            self.bias = Parameter(torch.FloatTensor(out_features))
        else:
            self.register_parameter('bias', None)
        self.reset_parameters()

    def reset_parameters(self):
        stdv = 1. / math.sqrt(self.weight.size(1))
        self.weight.data.uniform_(-stdv, stdv)
        if self.bias is not None:
            self.bias.data.uniform_(-stdv, stdv)

    def forward(self, input: Tensor, adj: Tensor):
        """
        :param input: shape=(node_num, feature_dim)
        :param adj: shape=(node_num, node_num)
        :return:
        """
        node_num = input.shape[0]
        if self.add_identity:
            adj = adj + torch.eye(node_num, dtype=adj.dtype, device=adj.device)
        degrees_sqrt_reciprocal = torch.diag(torch.reciprocal(torch.sqrt(torch.sum(adj, dim=1))))
        support = torch.mm(input, self.weight)
        adj_normalized = torch.mm(torch.mm(degrees_sqrt_reciprocal, adj), degrees_sqrt_reciprocal)
        output = torch.mm(adj_normalized, support)
        if self.bias is not None:
            return output + self.bias
        else:
            return output

    def __repr__(self):
        return self.__class__.__name__ + ' (' \
               + str(self.in_features) + ' -> ' \
               + str(self.out_features) + ')'

class MultiLayerGcn(nn.Module):
    def __init__(self, dims: List[int], dropout: float, bias: bool=True, add_identity: bool=True, activation: Callable=torch.relu):
        super().__init__()
        self.gcns = nn.ModuleList(
            [GraphConvolutionLayer(in_dim, out_dim, bias, add_identity)
             for in_dim, out_dim in zip(dims[:-1], dims[1:])]
        )
        self.dropout = nn.Dropout(dropout)
        self.activation = activation

    def forward(self, input: Tensor, adj: Tensor):
        x = input
        for layer in self.gcns:
            x = self.activation(self.dropout(layer(x, adj)))
        return x


"""
Mask R-CNN
Train on the toy Balloon dataset and implement color splash effect.

Copyright (c) 2018 Matterport, Inc.
Licensed under the MIT License (see LICENSE for details)
Written by Waleed Abdulla

"""

import os
import sys
import json
import datetime
import numpy as np
import skimage.draw

# Root directory of the project
ROOT_DIR = os.getcwd()

# Import Mask RCNN
sys.path.append(ROOT_DIR)
from config import Config
import utils
import model as modellib

# Directory to save logs and model checkpoints, if not provided
# through the command line argument --logs
DEFAULT_LOGS_DIR = os.path.join(ROOT_DIR, "logs")

############################################################
#  Configurations
############################################################

class globConfig(Config):
    """Configuration for training on the toy  dataset.
    Derives from the base Config class and overrides some values.
    """
    # Give the configuration a recognizable name
    NAME = "glob"

    # We use a GPU with 12GB memory, which can fit two images.
    # Adjust down if you use a smaller GPU.
    IMAGES_PER_GPU = 1

    # Number of classes (including background)
    NUM_CLASSES = 1 + 1  # Background + baloon

    # Number of training steps per epoch
    STEPS_PER_EPOCH = 100

    # Skip detections with < 90% confidence
    DETECTION_MIN_CONFIDENCE = 0.9

############################################################
#  Dataset
############################################################

class globDataset(utils.Dataset):

    def load_dataset(self, dataset_dir, json_path):
        """Load a subset of the Balloon dataset.
        dataset_dir: Root directory of the dataset.
        subset: Subset to load: train or val
        """
        # Add classes. We have only one class to add.
        self.add_class("glomerulus", 1, "glomerulus")

        # Load annotations
        # VGG Image Annotator saves each image in the form:
        # { 'filename': '28503151_5b5b7ec140_b.jpg',
        #   'regions': {
        #       '0': {
        #           'region_attributes': {},
        #           'shape_attributes': {
        #               'all_points_x': [...],
        #               'all_points_y': [...],
        #               'name': 'polygon'}},
        #       ... more regions ...
        #   },
        #   'size': 100202
        # }
        # We mostly care about the x and y coordinates of each region
        json_path="../../data/hubmap/train/aaa6a05cc.json"
    annotations = json.load(open(json_path))
    #print(type(annotations))
    #print(annotations)
    annotations = [a["geometry"] for a in annotations]

    # Add images
    for a in annotations:
        # Get the x, y coordinates of points of the polygons that make up
        # the outline of each object instance, stored in the geometry of each shape dictionary
        polygons = [r for r in a['coordinates']]

        image_path = "../../data/hubmap/train/aaa6a05cc.tiff"
        image = io.imread(image_path)
        height, width = image.shape[:2]

        self.add_image(
            "glomerulus",
            image_id="aaa6a05cc.tiff",  # use file name as a unique image id
            path=image_path,
            width=width, height=height,
            polygons=polygons)

    def load_mask(self, image_id):
        """Generate instance masks for an image.
       Returns:
        masks: A bool array of shape [height, width, instance count] with
            one mask per instance.
        class_ids: a 1D array of class IDs of the instance masks.
        """
        # If not a balloon dataset image, delegate to parent class.
        image_info = self.image_info[image_id]
        if image_info["source"] != "glomerulus":
            return super(self.__class__, self).load_mask(image_id)

        # Convert polygons to a bitmap mask of shape
        # [height, width, instance_count]
        info = self.image_info[image_id]
        mask = np.zeros([info["height"], info["width"], len(info["polygons"])],
                        dtype=np.uint8)
        for i, p in enumerate(info["polygons"]):
            # Get indexes of pixels inside the polygon and set them to 1
            rr, cc = skimage.draw.polygon(p['all_points_y'], p['all_points_x'])
            mask[rr, cc, i] = 1

        # Return mask, and array of class IDs of each instance. Since we have
        # one class ID only, we return an array of 1s
        return mask, np.ones([mask.shape[-1]], dtype=np.int32)

    def image_reference(self, image_id):
        """Return the path of the image."""
        info = self.image_info[image_id]
        if info["source"] == "balloon":
            return info["path"]
        else:
            super(self.__class__, self).image_reference(image_id)

def train(model):
    """Train the model."""
    # Training dataset.
    dataset_train = globDataset()
    dataset_train.load_dataset(args.dataset, "train")
    dataset_train.prepare()

    # Validation dataset
    dataset_val = globDataset()
    dataset_val.load_data(args.dataset, "val")
    dataset_val.prepare()

    # *** This training schedule is an example. Update to your needs ***
    # Since we're using a very small dataset, and starting from
    # COCO trained weights, we don't need to train too long. Also,
    # no need to train all layers, just the heads should do it.
    print("Training network heads")
    model.train(dataset_train, dataset_val,
                learning_rate=config.LEARNING_RATE,
                epochs=30,
                layers='heads')

############################################################
#  Training
############################################################

# Configurations
if args.command == "train":
    config = BalloonConfig()
else:
    class InferenceConfig(BalloonConfig):
            # Set batch size to 1 since we'll be running inference on
            # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
        GPU_COUNT = 1
        IMAGES_PER_GPU = 1
    config = InferenceConfig()
config.display()

# Create model
if args.command == "train":
    model = modellib.MaskRCNN(mode="training", config=config,
                                  model_dir=args.logs)
else:
    model = modellib.MaskRCNN(mode="inference", config=config,
                                  model_dir=args.logs)

# Select weights file to load
if args.weights.lower() == "coco":
    weights_path = COCO_WEIGHTS_PATH
# Download weights file
    if not os.path.exists(weights_path):
        utils.download_trained_weights(weights_path)
elif args.weights.lower() == "last":
      # Find last trained weights
    weights_path = model.find_last()[1]
elif args.weights.lower() == "imagenet":
        # Start from ImageNet trained weights
    weights_path = model.get_imagenet_weights()
else:
    weights_path = args.weights

# Load weights
print("Loading weights ", weights_path)
if args.weights.lower() == "coco":
        # Exclude the last layers because they require a matching
        # number of classes
    model.load_weights(weights_path, by_name=True, exclude=[
            "mrcnn_class_logits", "mrcnn_bbox_fc",
            "mrcnn_bbox", "mrcnn_mask"])
else:
    model.load_weights(weights_path, by_name=True)

    # Train or evaluate
if args.command == "train":
    train(model)
elif args.command == "splash":
    detect_and_color_splash(model, image_path=args.image,
                                video_path=args.video)
else:
    print("'{}' is not recognized. "
              "Use 'train' or 'splash'".format(args.command))


from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
class Vovel(Button):
    def __init__(self, position = (0,0,0)):
        super().__init__(
            parent=scene,
            position=position,
            model='cube',
            origin_y = 0.5,
            texture= 'white_cube',
            color= color.white,
            highlight_color = color.lime,
        )
    def Input(self, key):
        if self.hovered:
            if key == 'left mouse down':
                vovel = Vovel(position= self.position + mouse.normal)
            if key == 'right mouse down':
                destroy(self)
app = Ursina()
for z in range(8):
    for x in range(8):
        vovel = Vovel(position=  (x,0,z))
player = FirstPersonController()
app.run()


# -*- coding: utf-8 -*-
"""
collective.pullrequestreview

Licensed under the GPL license, see LICENCE.txt for more details.
"""

from pyramid.events import subscriber
from pyramid.view import render_view
from githubevent.events import PullRequest
from .gitclone import GitClone
from .interfaces import IPullRequestValidation

@subscriber(PullRequest)
class PullRequestSubscriber(object):

    def __init__(self, request):
        self.request = request

    @property
    def validators(self):
        registry = self.request.registry
        for validator in registry.subscribers((self.request,), IPullRequestValidation):
            yield validator

    def validate(self, repository):
        validationResults = []
        for validator in self.validators:
            validationResults.append(validator.validate(repository))
        return validationResults

    def __call__(self):
        with GitClone(self.request.base_repo_name, self.request.base_repo_url) as repo:
            validationResults = self.validate(repo)
            repo.merge(self.request.head_repo_url)
            validationResutlsAfter = self.validate(repo)
            validationView = render_view((validationResults, validationResutlsAfter),
                    self.request, 'view')
            # ADD VIEW.render TO COMMENT


# -*- coding: utf-8 -*-

from urlparse import parse_qs
from django.template import Library

# --------------------------------------------------------------------------- #

register = Library()

# --------------------------------------------------------------------------- #

@register.inclusion_tag('admin/profile/claim/filter.html')
def admin_list_filter(cl, spec):
    choices, options = list(spec.choices(cl)), list()
    field_name = spec.field.name

    for i in choices:
        parsed = parse_qs(i['query_string'].strip('?'))
        key = '%s__exact' % field_name

        i['parsed_query'] = parsed

        if key in parsed:
            i['option_value'] = parsed[key].pop()

    return dict(
        title      = spec.title(),
        field_name = field_name,
        choices    = choices,
        options    = options,
    )


from collections import deque
import torch as T
import numpy as np

class DDPGExperienceBuffer:
    def __init__(self, size, bs, threshold, device):
        self.size = size
        self.bs = bs
        self.threshold_v = threshold
        self.device = device

        self.states = deque(maxlen=self.size)
        self.actions = deque(maxlen=self.size)
        self.rewards = deque(maxlen=self.size)
        self.dones = deque(maxlen=self.size)
        self.next_states = deque(maxlen=self.size)

    def add(self, state, action, reward, done, next_state):

        self.states.append(state)
        self.actions.append(action)
        self.rewards.append(reward)
        self.dones.append(done)
        self.next_states.append(next_state)

    def draw(self):
        random = np.random.permutation(np.arange(len(self)))[:self.bs]
        return [T.stack([x[i] for i in random]).to(self.device) for x in [self.states, self.actions, self.rewards, self.dones, self.next_states]]

    @property
    def threshold(self):
        return True if (self.threshold_v < len(self.states) / self.size) else False

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


from collections import defaultdict
import operator
from nltk.sentiment.vader import SentimentIntensityAnalyzer
import nltk

def classification(predicted_model, test_pairscore, test_bilinear_score, test_question, question, review):
    total_no = 0
    correct = 0
    incorrect = 0
    c , i = 0, 0
    relevant_reviews = defaultdict(lambda: defaultdict())
    for prod_key in test_bilinear_score:
        question_review_score = defaultdict(lambda: defaultdict())
        for each_question in test_bilinear_score[prod_key]:
            # total_no += 5
            review_score, dummy = defaultdict(), defaultdict()
            for each_review in test_bilinear_score[prod_key][each_question]:
                score = defaultdict()
                weight_list = predicted_model[prod_key][each_question]
                sum = weight_list[0] * test_pairscore[prod_key][each_question][each_review]['ROGUE'] + \
                      weight_list[1] * test_pairscore[prod_key][each_question][each_review]['BM25+'] + \
                      weight_list[2] * test_bilinear_score[prod_key][each_question][each_review]
                abs_sum = abs(sum)
                score['score'] = sum
                score['abs_score'] = abs_sum
                for x in review[prod_key]:
                    if x['ID'] == each_review:
                        score['actual_sen'] = x['actual_sen']
                        break
                review_score[each_review] = sum
                dummy[each_review] = score

            sorted_list = sorted(review_score.items(), key=lambda x: x[1], reverse=True)
            # sorted_list = list(reversed(sorted(review_score.iteritems(), key=operator.itemgetter(1))))
            i = 0
            # nltk.download()
            yes, no = 0, 0
            sid = SentimentIntensityAnalyzer()
            for x in sorted_list:
                sentence = dummy[x[0]]['actual_sen']
                #print sentence
                ss = sid.polarity_scores(sentence)
                if ss["compound"] < 0:
                    no += 1
                else:
                    yes += 1

                i += 1
                if i == 5:
                    break
            if yes >= no:
                if test_question[prod_key][0]['A'] == True:
                    c += 1
                else:
                    i += 1
            else:
                if test_question[prod_key][0]['A'] == False:
                    c += 1
                else:
                    i +=  1
    accuracy =  float(c) / float(c + i)
    print ("accuracy: %s" %accuracy)
    return c, i


import argparse
import codecs
import json
import matplotlib.pyplot as plt
import numpy as np
import os
import requests
import sys
import yaml

def run_test(endpoint="https://cosmicrays.amentum.space/parma/differential_intensity",
    saving=False):
    """
    Reads published data from file.
    Fetchs model data from the API.
    Produces a PNG image.
    TODO compares against expected output and returns True if passed
    """

    # Set up the figures and labels
    fig = plt.figure()
    ax = fig.add_subplot(111)
    energy_label = r'$E \, (MeV)$'
    ax.set_xlabel(energy_label)
    diff_intensity_label = r"$\frac{d \varphi}{d E} \, (cm^{-2} s^{-1} sr^{-1} MeV^{-1})$"
    ax.set_ylabel(diff_intensity_label);
    colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k',  'navy', 'orange']

    # The request oackage lets you pass query string params as a dictionary, kewl
    payload = {
        'year' : '1998',
        'month' : '5',
        'day' : '28',
        'latitude' : '34.4',
        'longitude' : '-107.6',
        'particle' : 'proton',
        'angle' : '1.0'
    }

    print(f"Loading published data ")
    # Read the experimental data from YAML file
    with open('./protons_mocchiuti.yaml') as f :
        doc = yaml.load(f, Loader=yaml.FullLoader)
    f.close()

    print(f"Loading API data ")

    # For each atmospheric depth in the experimental data, in reverse order
    for i,plot in enumerate(doc['plots'][::-1]):

        data = np.array(plot['data'])
        kes = data.T[2] # GeV
        intensities = data.T[4] # /m2/s/sr/GeV
        errors = data.T[5] # /m2/s/sr/GeV
        multipliers = data.T[6] #

        # scale the values by the multiplers to separate the plots in y
        intensities *= multipliers
        errors *= multipliers

        atm_depth = plot['atm_depth']['value']
        this_label = np.str(atm_depth) + plot['atm_depth']['units']

        print("atmospheric depth ", this_label)

        this_color = colors[i]

        ax.errorbar(kes*1000.0 #GeV-> MeV
            , intensities*1e-7  #/m2/s/sr/GeV -> /cm2/s/sr/MeV
            , yerr = errors*1e-7
            , linestyle = "None"
            , marker = "."
            , label = this_label
            , color = this_color)

        # Over-write the atmospheric depth parameter for this dataset
        payload['atmospheric_depth'] = str(atm_depth)

        # Hit the Cosmic Ray API and fetch the effective dose rate
        try:
            response = requests.get(endpoint, params=payload)
        except requests.exceptions.RequestException as e:
            print(e)

        json_payload = response.json()

        # Extract the energy differential flux distribution
        kes = json_payload["energies"]["data"] # MeV
        flux = json_payload["intensities"]["data"] # /cm2/s/sr/MeV

        ax.plot(kes # MeV
            , flux  #
            , linestyle = "-"
            , marker = "None"
            , color = this_color)

        # save correct values, or compare against loaded
        if saving :
            np.save(f"{i}", flux)
        else :
            correct_flux = np.load(f"{i}.npy")
            if np.count_nonzero(flux-correct_flux):
                print(flux,correct_flux)
                raise ValueError("Incorrect flux value")
                sys.exit(1)

    # Finalise formatting and save the plot
    ax.loglog()
    ax.grid()
    ax.set_xlim(left = 1e2, right =1e5)
    ax.legend(loc="upper right", fontsize = 'small', numpoints = 1)
    plt.tight_layout()
    plt.savefig("protons_mocchiuti.png")

    return

if __name__ == '__main__':
    """
    If running file as script, just call the analysis function
    """

    # parse argument to over-ride endpoint name when testing

    parser = argparse.ArgumentParser()

    parser.add_argument('--endpoint', dest='endpoint',
        help='over-ride default endpoint for testing', required=False)

    parser.add_argument('--saving', dest='saving', action='store_true',
        required=False)

    args = parser.parse_args()

    # can either specify alternative endpoint
    if args.endpoint is not None:
        run_test(args.endpoint)
    elif args.saving:
        run_test(saving=args.saving)
        print("correct values saved")
    else :
        run_test()
    print("test passed")


from typing import List
from collections import defaultdict

class Solution:
    def numBusesToDestination(self, routes: List[List[int]], S: int, T: int) -> int:

        stop2route = defaultdict(list, [])

        for i, r in enumerate(routes):
            for stop in r:
                stop2route[stop].append(i)

        stop2remove = []
        for stop, route in stop2route.items():
            if len(route) == 1 and stop != S and stop != T:
                stop2remove.append( (stop, route[0]) )
        for s, r in stop2remove:
            stop2route.pop(s)
            routes[r].remove(s)

        del stop2remove

        print(stop2route)

        # initialize the queue with all possible stops from the bus originated from the stop S
        queue = []
#        queue = [ stop for stop in routes[r] for r in stop2route[S] ]
        for r in stop2route[S]:
            queue.extend([ (1, stop) for stop in routes[r] if stop != S])
        # keep a record of buses taken
        #taken = set(stop2route[S])
        #
        visited = set([S])

        while queue:
            print(queue)
            k, curr = queue.pop(0)
            if curr == T:
                return k
            #visited.add(curr)
            for r in stop2route[curr]:
                #if r not in taken:
                #    queue.extend([(k+1, stop) for stop in routes[r] if stop != curr and stop not in visited])
                queue.extend([(k+1, stop) for stop in routes[r] if stop != curr and stop not in visited])

        return -1

if __name__ == '__main__':
    sol = Solution()

    input_list = [
        {"routes": [[1, 2, 7], [3, 6, 7]], 'S': 1, 'T': 6},
    ]

    for inp in input_list:
        result = sol.numBusesToDestination(**inp)
        print(result)


# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

import pandas as pd
import numpy as np
import bs4
import nltk
import urllib
import requests

request = "books.txt"

columns = ['title', 'author', 'rating']

with open(request) as f:
    #read and load books
    text = f.read()
    books = [s for s in text.splitlines()]

url = "https://www.commonsensemedia.org/book-reviews/pride-and-prejudice"

url_get = requests.get(url)
soup = bs4.BeautifulSoup(url_get.content, 'html.parser')

ed_value = soup.find("div", id="content-grid-item-educational")

result = re.sub('[^0-9]','', a)

pos_msg = None
pos_RM = None
violence = None
sex = None
language = None
consumerism = None
Drinking_drug_smoke = None

print(soup.contents)


# -*- coding: utf-8 -*-
#
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Wrappers for protocol buffer enum types."""

import enum

class CompressionType(enum.IntEnum):
    """
    The ways in which threat entry sets can be compressed.

    Attributes:
      COMPRESSION_TYPE_UNSPECIFIED (int): Unknown.
      RAW (int): Raw, uncompressed data.
      RICE (int): Rice-Golomb encoded data.
    """

    COMPRESSION_TYPE_UNSPECIFIED = 0
    RAW = 1
    RICE = 2

class ThreatType(enum.IntEnum):
    """
    The type of threat. This maps dirrectly to the threat list a threat may
    belong to.

    Attributes:
      THREAT_TYPE_UNSPECIFIED (int): Unknown.
      MALWARE (int): Malware targeting any platform.
      SOCIAL_ENGINEERING (int): Social engineering targeting any platform.
      UNWANTED_SOFTWARE (int): Unwanted software targeting any platform.
    """

    THREAT_TYPE_UNSPECIFIED = 0
    MALWARE = 1
    SOCIAL_ENGINEERING = 2
    UNWANTED_SOFTWARE = 3

class ComputeThreatListDiffResponse(object):
    class ResponseType(enum.IntEnum):
        """
        The type of response sent to the client.

        Attributes:
          RESPONSE_TYPE_UNSPECIFIED (int): Unknown.
          DIFF (int): Partial updates are applied to the client's existing local database.
          RESET (int): Full updates resets the client's entire local database. This means
          that either the client had no state, was seriously out-of-date,
          or the client is believed to be corrupt.
        """

        RESPONSE_TYPE_UNSPECIFIED = 0
        DIFF = 1
        RESET = 2


from doit.tools import title_with_actions

from doitcmd import BaseCommand

class Copy(BaseCommand):
    cmd_template = 'install {opts} {source} {dest}'
    base_options = {'D': True} # -D / create all leading folders

    def __call__(self, source, dest, **kwargs):
        """return task dict"""
        opts = self.opt_str(self.options, kwargs)

        cmd = self.cmd_template.format(opts=opts, source=source, dest=dest)
        return {
            'name': '{}'.format(dest),
            'actions': [self.action(cmd)],
            'file_dep': [source],
            'targets': [dest],
            'title': title_with_actions,
            }


import bpy
from bpy.types import Operator

# Set the pivot point of the active object
# to the center and add a mirror modifier
class FC_MirrorOperator(Operator):
    bl_idname = "object.mirror"
    bl_label = "Center Origin & Mirror"
    bl_description = "Mirror selected object"
    bl_options = {'REGISTER', 'UNDO'}

    @classmethod
    def poll(cls, context):

        mode = context.active_object.mode
        return len(context.selected_objects) == 1 and mode == "OBJECT"

    def execute(self, context):

        cursor_location = bpy.context.space_data.cursor_location.copy()

        bpy.context.scene.cursor_location = (0.0, 0.0, 0.0)
        bpy.ops.object.origin_set(type='ORIGIN_CURSOR')

        bpy.ops.object.modifier_add(type='MIRROR')

        bpy.context.scene.cursor_location = cursor_location

        return {'FINISHED'}


from pathlib import Path
from typing import Optional

import typer

admin_app = typer.Typer()

@admin_app.command("initialize-database")
def initialize_database(database_uri: str):
    """
    Initialize a SQL database for use by Tiled.
    """
    from sqlalchemy import create_engine

    from ..database.core import (
        REQUIRED_REVISION,
        UninitializedDatabase,
        check_database,
        initialize_database,
    )

    engine = create_engine(database_uri)
    redacted_url = engine.url._replace(password="[redacted]")
    try:
        check_database(engine)
    except UninitializedDatabase:
        # Create tables and stamp (alembic) revision.
        typer.echo(
            f"Database {redacted_url} is new. Creating tables and marking revision {REQUIRED_REVISION}.",
            err=True,
        )
        initialize_database(engine)
        typer.echo("Database initialized.", err=True)
    else:
        typer.echo(f"Database at {redacted_url} is already initialized.", err=True)
        raise typer.Abort()

@admin_app.command("upgrade-database")
def upgrade_database(
    database_uri: str,
    revision: Optional[str] = typer.Argument(
        None,
        help="The ID of a revision to upgrade to. By default, upgrade to the latest one.",
    ),
):
    """
    Upgrade the database schema to the latest version.
    """
    from sqlalchemy import create_engine

    from ..database.core import get_current_revision, upgrade

    engine = create_engine(database_uri)
    redacted_url = engine.url._replace(password="[redacted]")
    current_revision = get_current_revision(engine)
    if current_revision is None:
        # Create tables and stamp (alembic) revision.
        typer.echo(
            f"Database {redacted_url} has not been initialized. Use `tiled admin initialize-database`.",
            err=True,
        )
        raise typer.Abort()
    upgrade(engine, revision or "head")

@admin_app.command("downgrade-database")
def downgrade_database(
    database_uri: str,
    revision: str = typer.Argument(..., help="The ID of a revision to downgrade to."),
):
    """
    Upgrade the database schema to the latest version.
    """
    from sqlalchemy import create_engine

    from ..database.core import downgrade, get_current_revision

    engine = create_engine(database_uri)
    redacted_url = engine.url._replace(password="[redacted]")
    current_revision = get_current_revision(engine)
    if current_revision is None:
        # Create tables and stamp (alembic) revision.
        typer.echo(
            f"Database {redacted_url} has not been initialized. Use `tiled admin initialize-database`.",
            err=True,
        )
        raise typer.Abort()
    downgrade(engine, revision)

@admin_app.command("check-config")
def check_config(
    config_path: Path = typer.Argument(
        None,
        help=(
            "Path to a config file or directory of config files. "
            "If None, check environment variable TILED_CONFIG. "
            "If that is unset, try default location ./config.yml."
        ),
    ),
):
    "Check configuration file for syntax and validation errors."
    import os

    from ..config import parse_configs

    config_path = config_path or os.getenv("TILED_CONFIG", "config.yml")
    try:
        parse_configs(config_path)
    except Exception as err:
        typer.echo(str(err), err=True)
        raise typer.Exit(1)
    typer.echo("No errors found in configuration.")


import numpy as np
from scipy.fftpack import fft, ifft
from scipy import hamming
from scipy.io import wavfile
from scipy.signal import resample
import scipy.linalg as LA
import pylab as pl
import matplotlib.pyplot as plt

def resample_signal(signal, fs, fs_resample):
    n = int(len(signal) * fs_resample / fs)
    return resample(signal, n)

### for STFT ###

def stft(signal, fftSize=1024, shiftSize=512, window='hamming'):
    """
    Parameters
    ----------
    signal: input signal
    fftSize: frame length
    shiftSize: frame shift
    window: window function

    Returns
    -------
    S: spectrogram of input signal (fftSize/2+1 x frame x ch)
    window: used window function (fftSize x 1)
    """
    signal = np.array(signal)

    if window == 'hamming':
        # todo 色々対応
        window = hamming(fftSize+1)[:fftSize]

    zeroPadSize = fftSize - shiftSize
    length = signal.shape[0]
    frames = int(np.floor((length + fftSize - 1) / shiftSize))
    I = int(fftSize/2 + 1)

    if len(signal.shape) == 1:
        # monoral
        signal = np.concatenate([np.zeros(zeroPadSize), signal, np.zeros(fftSize)])
        S = np.zeros([I, frames], dtype=np.complex128)

        for j in range(frames):
            sp = j * shiftSize
            spectrum = fft(signal[sp: sp+fftSize] * window)
            S[:, j] = spectrum[:I]

        return S, window

    elif len(signal.shape) == 2:
        nch = signal.shape[1]
        signal = np.concatenate([np.zeros([zeroPadSize, nch]), signal, np.zeros([fftSize,nch])])
        S = np.zeros([I, frames, nch], dtype=np.complex128)

        for ch in range(nch):
            for j in range(frames):
                sp = j * shiftSize
                spectrum = fft(signal[sp: sp+fftSize, ch] * window)
                S[:, j, ch] = spectrum[:I]

        return S, window

    else:
        raise ValueError('illegal signal dimension')

def whitening(X, dnum=2):
    # M == dnumで固定 (todo)
    I, J, M = X.shape
    Y = np.zeros(X.shape, dtype=np.complex128)

    def _whitening(Xi):
        V = Xi @ Xi.T.conjugate() / J  # covariance matrix (M, M)
        eig_val, P = LA.eig(V)
        D = np.diag(eig_val)

        idx = np.argsort(eig_val)
        D = D[idx, idx]
        P = P[:, idx]

        # D2 = D[M - dnum:, M - dnum:]
        # P2 = P[:, M-dnum:]

        return (np.diag(D ** (-0.5)) @ P.T.conjugate() @ Xi).T  # (M, M) * (M, M) * (M, J)

    for i in range(I):
        Y[i] = _whitening(X[i].T)

    return Y

def power(S):
    return np.real(S) ** 2 + np.imag(S) ** 2

def spectrogram(S, output_path=None, sampling_rate=8000, fftSize=None, shiftSize=None, **kwargs):
    I, J = S.shape

    if fftSize is None:
        fftSize = (I - 1) * 2

    if shiftSize is None:
        shiftSize = fftSize // 2

    X, Y = pl.meshgrid(
        (pl.arange(J + 1) - 0.5) * shiftSize / sampling_rate,  # sec
        (pl.arange(I + 1) - 0.5) / fftSize * sampling_rate  # Hz
    )

    pl.pcolormesh(X, Y, np.log(S + 1e-5), **kwargs)
    pl.xlabel('time [s]', fontsize='x-large')
    pl.ylabel('frequency [Hz]', fontsize='x-large')
    pl.xticks(fontsize='large')
    pl.yticks(fontsize='large')
    pl.subplots_adjust(left=0.15, right=0.85, top=0.95, bottom=0.15)

    if output_path is None:
        pl.show()
    else:
        pl.savefig(output_path)
    pl.clf()

def histgram(S, output_path=None, **kwargs):
    # plt.clf()
    plt.hist(S.flatten(), **kwargs)
    if output_path is None:
        pl.show()
    else:
        pl.savefig(output_path)

### for ISTFT ###

def optSynWnd(analysisWindow, shiftSize):
    fftSize = analysisWindow.shape[0]
    synthesizedWindow = np.zeros(fftSize)
    for i in range(shiftSize):
        amp = 0
        for j in range(1, int(fftSize / shiftSize) + 1):
            amp += analysisWindow[i + (j-1) * shiftSize] ** 2
        for j in range(1, int(fftSize / shiftSize) + 1):
            synthesizedWindow[i + (j-1) * shiftSize] = analysisWindow[i + (j-1) * shiftSize] / amp

    return synthesizedWindow

def istft(S, shiftSize, window=None, length=None):
    """
    % [inputs]
    %           S: STFT of input signal (fftSize/2+1 x frames x nch)
    %   shiftSize: frame shift (default: fftSize/2)
    %      window: window function used in STFT (fftSize x 1) or choose used
    %              function from below.
    %              'hamming'    : Hamming window (default)
    %              'hann'       : von Hann window
    %              'rectangular': rectangular window
    %              'blackman'   : Blackman window
    %              'sine'       : sine window
    %      length: length of original signal (before STFT)
    %
    % [outputs]
    %   waveform: time-domain waveform of the input spectrogram (length x nch)
    %
    """
    if window is None:
        fftSize = shiftSize * 2
        window = hamming(fftSize+1)[:fftSize]

    if S.ndim == 2:
        freq, frames = S.shape
        fftSize = (freq-1) * 2
        invWindow = optSynWnd(window, shiftSize)
        spectrum = np.zeros(fftSize, dtype=np.complex128)

        tmpSignal = np.zeros([(frames - 1) * shiftSize + fftSize])
        for j in range(frames):
            spectrum[:int(fftSize / 2) + 1] = S[:, j]
            spectrum[0] /= 2
            spectrum[int(fftSize / 2)] /= 2
            sp = j * shiftSize
            tmpSignal[sp: sp + fftSize] += (np.real(ifft(spectrum, fftSize) * invWindow) * 2)

        waveform = tmpSignal[fftSize - shiftSize: (frames - 1) * shiftSize + fftSize]

    elif S.ndim == 3:
        freq, frames, nch = S.shape
        fftSize = (freq-1) * 2
        invWindow = optSynWnd(window, shiftSize)
        spectrum = np.zeros(fftSize, dtype=np.complex128)

        tmpSignal = np.zeros([(frames - 1) * shiftSize + fftSize, nch])
        for ch in range(nch):
            for j in range(frames):
                spectrum[:int(fftSize / 2) + 1] = S[:, j, ch]
                spectrum[0] /= 2
                spectrum[int(fftSize / 2)] /= 2
                sp = j * shiftSize
                tmpSignal[sp: sp + fftSize, ch] += (np.real(ifft(spectrum, fftSize) * invWindow) * 2)

        waveform = tmpSignal[fftSize - shiftSize: (frames - 1) * shiftSize + fftSize]

    if length:
        waveform = waveform[:length]

    return waveform

def main(input_path, output_path):
    """
    input_path : wav
    output_path : png
    """
    fs, signal = wavfile.read(input_path)
    S, window = stft(signal, fftSize=4096, shiftSize=2048)
    S = power(S)
    spectrogram(S[:, :, 0], output_path)

if __name__ == '__main__':
    # exit(main('input/synth_res.wav', 'test.png'))

    fs, signal1 = wavfile.read('input/guitar_res.wav')
    signal1 = signal1 / 32768.0
    assert fs == 16000
    fs, signal2 = wavfile.read('input/synth_res.wav')
    signal2 = signal2 / 32768.0
    assert fs == 16000
    nch = 2

    mix = np.zeros([len(signal1), nch])

    for ch in range(nch):
        mix[:, ch] = signal1[:, ch] + signal2[:, ch]

    S, window = stft(mix, fftSize=4096, shiftSize=2048)
    mix2 = istft(S, 2048, window, mix.shape[0])
    wavfile.write('est.wav', 16000, mix2)


from typing import Optional, Iterable

from .base import BaseFunction, SyncFunctionMixin
from .request import Request

__all__ = (
    'BaseKeyPair',
    'KeyPair',
)

class BaseKeyPair(BaseFunction):

    @classmethod
    def _create(cls, user_id: int,
                is_active: bool=True,
                is_admin: bool=False,
                resource_policy: Optional[str]=None,
                rate_limit: Optional[int]=None,
                concurrency_limit: Optional[int]=None,
                fields: Optional[Iterable[str]]=None):
        if fields is None:
            fields = ('access_key', 'secret_key')
        q = 'mutation($user_id: Int!, $input: KeyPairInput!) {' \
            '  create_keypair(user_id: $user_id, props: $input) {' \
            '    ok msg keypair { $fields }' \
            '  }' \
            '}'
        q = q.replace('$fields', ' '.join(fields))
        vars = {
            'user_id': user_id,
            'input': {
                'is_active': is_active,
                'is_admin': is_admin,
                'resource_policy': resource_policy,
                'rate_limit': rate_limit,
                'concurrency_limit': concurrency_limit,
            },
        }
        resp = yield Request('POST', '/admin/graphql', {
            'query': q,
            'variables': vars,
        })
        data = resp.json()
        return data['create_keypair']

    @classmethod
    def _list(cls, user_id: int,
              is_active: Optional[bool]=None,
              fields: Optional[Iterable[str]]=None):
        if fields is None:
            fields = (
                'access_key', 'secret_key',
                'is_active', 'is_admin',
            )
        q = 'query($user_id: Int!, $is_active: Boolean) {' \
            '  keypairs(user_id: $user_id, is_active: $is_active) {' \
            '    $fields' \
            '  }' \
            '}'
        q = q.replace('$fields', ' '.join(fields))
        vars = {
            'user_id': user_id,
            'is_active': is_active,
        }
        resp = yield Request('POST', '/admin/graphql', {
            'query': q,
            'variables': vars,
        })
        data = resp.json()
        return data['keypairs']

    @classmethod
    def activate(cls, access_key: str):
        raise NotImplementedError

    @classmethod
    def deactivate(cls, access_key: str):
        raise NotImplementedError

    def __init_subclass__(cls):
        cls.create = cls._call_base_clsmethod(cls._create)
        cls.list = cls._call_base_clsmethod(cls._list)

class KeyPair(SyncFunctionMixin, BaseKeyPair):
    pass


# coding=utf-8
'日志，全局统一用logger'

from logging.handlers import TimedRotatingFileHandler

__author__ = 'Jiateng Liang'
from bootstrap import app

import logging

logger = logging.getLogger(app.config['LOG_NAME'])

fh = TimedRotatingFileHandler(filename=app.config['LOG_PATH'], when="D", interval=1)

if app.config['LOG_LEVEL'] == 'DEBUG':
    logger.setLevel(logging.DEBUG)
    fh.setLevel(logging.DEBUG)
elif app.config['LOG_LEVEL'] == 'INFO':
    logger.setLevel(logging.INFO)
    fh.setLevel(logging.INFO)
elif app.config['LOG_LEVEL'] == 'WARN':
    logger.setLevel(logging.WARN)
    fh.setLevel(logging.WARN)
elif app.config['LOG_LEVEL'] == 'ERROR':
    logger.setLevel(logging.ERROR)
    fh.setLevel(logging.ERROR)

formatter = logging.Formatter(app.config['LOG_FORMAT'], app.config['LOG_DATE_FORMAT'])

fh.setFormatter(formatter)
logger.addHandler(fh)

if app.config['LOG_CONSOLE']:
    console = logging.StreamHandler()
    console.setLevel(app.config['LOG_LEVEL'])
    console.setFormatter(formatter)
    logger.addHandler(console)


"""
Main interface module to use pyEPR.

Contains code to conenct to Ansys and to analyze HFSS files using the EPR method.

This module handles the micowave part of the analysis and conenction to

Further contains code to be able to do autogenerated reports,

Copyright Zlatko Minev, Zaki Leghtas, and the pyEPR team
2015, 2016, 2017, 2018, 2019, 2020
"""

from __future__ import print_function  # Python 2.7 and 3 compatibility

import sys
from pathlib import Path

import pandas as pd

from . import Dict, ansys, config, logger
from .toolbox.pythonic import get_instance_vars

class ProjectInfo(object):
    """
    Class containing options and information about the manipulation and analysis in HFSS.

    Junction info:
    -----------------------
        self.junctions : OrderedDict()

        A Josephson tunnel junction has to have its parameters specified here for the analysis.
        Each junction is given a name and is specified by a dictionary.
        It has the following properties:

            `Lj_variable`: Name of HFSS variable that specifies junction inductance Lj defined
                        on the boundary condition in HFSS. DO NOT USE Global names that start
                        with $.
            `rect`:     Name of HFSS rectangle on which lumped boundary condition is specified.
            `line`:     Name of HFSS polyline which spans the length of the recntalge.
                        Used to define the voltage across the junction.
                        Used to define the current orientation for each junction.
                        Used to define sign of ZPF.
            `length`:   Length in HFSS of the junction rectangle and line
                        (specified in meters). You can use epr.parse_units('100um')
            `Cj_variable` : Experimental. Optional.
                        Name of HFSS variable that specifies junction inductance Cj defined
                        on the boundary condition in HFSS. DO NOT USE Global names that start
                        with $.

        Example definition:

        ..code-block python

            # Define a single junction
            pinfo = ProjectInfo('')
            pinfo.junctions['j1'] = {'Lj_variable' : 'Lj1',
                         'rect'        : 'JJrect1',
                         'line'        : 'JJline1',
                         'length'      : parse_units('50um')} # Length is in meters

            # Specify multiple junctions in HFSS model
            n_junctions = 5
            for i in range(1, 1+n_junctions):
                pinfo.junctions[f'j{i}'] = {'Lj_variable' : f'Lj{i}',
                                            'rect'        : f'JJrect{i}',
                                            'line'        : f'JJline{i}',
                                            'length'      : parse_units('50um')}

    HFSS app connection settings
    -----------------------
        project_path  : str
            Directory path to the hfss project file. Should be the directory, not the file.
            default = None: Assumes the project is open, and thus gets the project based
            on `project_name`
        project_name  : str,  None
            Name of the project within the project_path. "None" will get the current active one.
        design_name   : str,  None
            Name of the design within the project. "None" will get the current active one.
        setup_name    : str,  None
            Name of the setup within the design. "None" will get the current active one.

    Additional init setting:
    -----------------------
        do_connect : True by default. Connect to HFSS

    HFSS desgin settings
    -----------------------
    describe junction parameters
        junc_rects    = None
            Name of junction rectangles in HFSS
        junc_lines    = None
            Name of lines in HFSS used to define the current orientation for each junction
        junc_LJ_names = None
            Name of junction inductance variables in HFSS.
            Note, DO NOT USE Global names that start with $.
        junc_lens     = None
            Junciton rect. length, measured in meters.

    """

    class _Dissipative:
        # TODO: remove and turn to dict

        def __init__(self):
            self.dielectrics_bulk = None
            self.dielectric_surfaces = None
            self.dielectric_MA_surfaces = None
            self.dielectric_MS_surfaces = None
            self.dielectric_SA_surfaces = None
            self.resistive_surfaces = None
            self.seams = None

    def __init__(self, project_path=None, project_name=None, design_name=None,
                 setup_name=None, do_connect=True):

        # Path: format path correctly to system convention
        self.project_path = str(Path(project_path)) \
            if not (project_path is None) else None
        self.project_name = project_name
        self.design_name = design_name
        self.setup_name = setup_name

        # HFSS desgin: describe junction parameters
        # TODO: introduce modal labels
        self.junctions = Dict()  # See above for help
        self.ports = Dict()

        # Dissipative HFSS volumes and surfaces
        self.dissipative = self._Dissipative()
        self.options = config.ansys

        # Conected to HFSS variable
        self.app = None
        self.desktop = None
        self.project = None
        self.design = None
        self.setup = None

        if do_connect:
            self.connect()

    _Forbidden = ['app', 'design', 'desktop', 'project',
                  'dissipative', 'setup', '_Forbidden', 'junctions']

    def save(self):
        '''
        Return a dicitonary to save
        '''
        return dict(
            pinfo=pd.Series(get_instance_vars(self, self._Forbidden)),
            dissip=pd.Series(get_instance_vars(self.dissipative)),
            options=pd.Series(get_instance_vars(self.options)),
            junctions=pd.DataFrame(self.junctions),
            ports=pd.DataFrame(self.ports),
        )

    def connect(self):
        """
        Connect to Ansys Desktop.
        """
        logger.info('Connecting to Ansys Desktop API...')

        self.app, self.desktop, self.project = ansys.load_ansys_project(
            self.project_name, self.project_path)
        self.project_name = self.project.name
        self.project_path = self.project.get_path()

        # Design
        if self.design_name is None:
            self.design = self.project.get_active_design()
            self.design_name = self.design.name
            logger.info(f'\tOpened active design\n\
\tDesign:    {self.design_name} [Solution type: {self.design.solution_type}]')
        else:

            try:
                self.design = self.project.get_design(self.design_name)
                logger.info(f'\tOpened active design\n\
\tDesign:    {self.design_name} [Solution type: {self.design.solution_type}]')

            except Exception as e:
                _traceback = sys.exc_info()[2]
                logger.error(f"Original error \N{loudly crying face}: {e}\n")
                raise(Exception(' Did you provide the correct design name?\
                    Failed to pull up design. \N{loudly crying face}').with_traceback(_traceback))

        # Setup
        try:
            setup_names = self.design.get_setup_names()

            if len(setup_names) == 0:
                logger.warning('\tNo design setup detected.')
                if self.design.solution_type == 'Eigenmode':
                    logger.warning('\tCreating eigenmode default setup one.')
                    setup = self.design.create_em_setup()
                    self.setup_name = setup.name
                elif self.design.solution_type == 'DrivenModal':
                    setup = self.design.create_dm_setup()  # adding a driven modal design
                    self.setup_name = setup.name
            else:
                if self.setup_name not in setup_names:
                    logger.warning('\tUser specified design setup does not exist. Utilizing the default satup.')
                    self.setup_name = setup_names[0]

            # get the actual setup if there is one
            self.get_setup(self.setup_name)

        except Exception as e:

            _traceback = sys.exc_info()[2]
            logger.error(f"Original error \N{loudly crying face}: {e}\n")
            raise Exception(' Did you provide the correct setup name?\
                        Failed to pull up setup. \N{loudly crying face}').with_traceback(_traceback)

        # Finalize
        self.project_name = self.project.name
        self.design_name = self.design.name

        logger.info(
            '\tConnection to Ansys established successfully. \N{grinning face} \n')

        return self

    def get_setup(self, name):
        """
        Connects to a specific setup for the design.
        Sets  self.setup and self.setup_name.
        If Name is
        """
        if name is None:
            return None
        else:
            self.setup = self.design.get_setup(name=self.setup_name)
            if self.setup is None:
                logger.error(f"Could not retrieve setup: {self.setup_name}\n \
                               Did you give the right name? Does it exist?")

            self.setup_name = self.setup.name
            logger.info(
                f'\tOpened setup `{self.setup_name}`  ({type(self.setup)})')
            return self.setup

    def check_connected(self):
        """Checks if fully connected including setup
        """
        return\
            (self.setup is not None) and\
            (self.design is not None) and\
            (self.project is not None) and\
            (self.desktop is not None) and\
            (self.app is not None)

    def disconnect(self):
        '''
        Disconnect from existing HFSS design.
        '''
        assert self.check_connected() is True,\
            "It does not appear that you have connected to HFSS yet.\
            Use the connect()  method. \N{nauseated face}"
        self.project.release()
        self.desktop.release()
        self.app.release()
        ansys.release()

    # UTILITY FUNCTIONS

    def get_dm(self):
        '''
        Utility shortcut function to get the design and modeler.

        .. code-block:: python
            oDesign, oModeler = projec.get_dm()
        '''
        return self.design, self.design.modeler

    def get_all_variables_names(self):
        """Returns array of all project and local design names."""
        return self.project.get_variable_names() + self.design.get_variable_names()

    def get_all_object_names(self):
        """Returns array of strings"""
        o_objects = []
        for s in ["Non Model", "Solids", "Unclassified", "Sheets", "Lines"]:
            o_objects += self.design.modeler.get_objects_in_group(s)
        return o_objects

    def validate_junction_info(self):
        """ Validate that the user has put in the junction info correctly.
        Do no also forget to check the length of the rectangles/line of
        the junction if you change it.
        """

        all_variables_names = self.get_all_variables_names()
        all_object_names = self.get_all_object_names()

        for jjnm, jj in self.junctions.items():

            assert jj['Lj_variable'] in all_variables_names,\
                """pyEPR ProjectInfo user error found \N{face with medical mask}:
                Seems like for junction `%s` you specified a design or project
                variable for `Lj_variable` that does not exist in HFSS by the name:
                 `%s` """ % (jjnm, jj['Lj_variable'])

            for name in ['rect', 'line']:

                assert jj[name] in all_object_names, \
                    """pyEPR ProjectInfo user error found \N{face with medical mask}:
                    Seems like for junction `%s` you specified a %s that does not exist
                    in HFSS by the name: `%s` """ % (jjnm, name, jj[name])

        # TODO: Check the length of the rectangle


import os
import sys
import HTSeq
import re
import subprocess
import pysam
from site_cluster import site_cluster

def complement(s):
    basecomplement = {'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A', 'N': 'N',
                      'H': 'D'}
    letters = list(s)
    letters = [basecomplement[base] for base in letters]
    return ''.join(letters)

def rc(s):
    s = s[::-1]
    s = complement(s)
    return s

def merge_clusters(cluster_ranges):
    merged = {}
    merged_ranges = {}
    ranges = cluster_ranges
    overlapping_count = 0
    if(len(ranges) > 0):
        saved = list(ranges[0])
    else:
        saved = []
    for st, en, strand in sorted([sorted(t) for t in ranges]):
        if st <= saved[1]:
            overlapping_count += 1
            saved[1] = max(saved[1], en)
        else:
            yield tuple(saved)
            saved[0] = st
            saved[1] = en
    yield tuple(saved)

def add_signal(clusters):
    for a_clust in clusters:
        a_clust.signal = read_region(a_clust.chrm, a_clust.start,
                                      a_clust.end, a_clust.strand)

def read_region(chrm, start, end, strand,
                bamfile='/home/dp/Desktop/bams/rna_seq_srr652960.bam'):
    start = max(1, start)
    bamfile_obj = pysam.AlignmentFile(bamfile, 'rb')
    s = bamfile_obj.fetch(chrm, int(start), int(end))
    reads = 0
    for r in s:
        if(strand == "+" and not r.is_reverse):
            reads += 1
        if(strand == "-" and r.is_reverse):
            reads += 1
    return reads

def get_total_reads(bam_filename):
    cmdl = ["samtools", "flagstat", str(bam_filename)]
    stats = subprocess.Popen(cmdl, stdout=subprocess.PIPE)
    stp = re.compile(r'\\n(\d+) \+ (\d+) mapped(.+)')
    parseout = stp.search(str(stats.communicate()))
    if parseout:
        total_reads = int(parseout.group(1)) + int(parseout.group(2))
        return total_reads
    return False


"""
Acted spider created on the top of ATSSpider

scrapy crawl acted -a mining_job_id=9999 -a iteration=1 -a extract=1 -a url="http://www.acted.org/en/our-job-offers"

Sample URL:
    http://www.acted.org/en/our-job-offers
"""

from hashlib import sha1
from scrapy.http import Request
from scrapy.selector import Selector
from urlparse import urljoin

from brightcorp.base.atsspiders import ATSSpider
from brightcorp.items import BrightcorpItemLoader
from brightcorp.processors import HtmlFormatter, NormalizedJoin, Prefix

class Acted(ATSSpider):

    name = 'acted'

    def parse(self, response):
        sel = Selector(response)

        for href in sel.xpath(
            '//div[contains(@class, "view-listing-job")]/div[@class="view-content"]/div[contains(@class, "views-row")]//h2[@class="node-title"]/a/@href'
        ).extract():
            yield Request(
                callback=self.parse_job_callback(),
                url=urljoin(response.url, href)
            )

        # pagination
        next_page = sel.xpath(
            '//ul[@class="pager"]/li[@class="pager-next"]/a/@href'
        ).extract()
        if next_page:
            yield Request(
                callback=self.parse,
                url=urljoin(response.url, next_page[0])
            )

    def parse_job(self, response):
        """
        Extract all required information.
        """
        sel = Selector(response)

        loader = BrightcorpItemLoader(selector=sel)

        loader.add_xpath(
            'title',
            '//div[@class="content"]/h1[@class="page-title"]/text()'
        )
        loader.add_xpath(
            'location',
            [
                '//div[@class="content"]/div[@class="jobdetails"]//p/strong[contains(text(), "City:")]/../text()',
                '//div[@class="content"]/div[@class="jobdetails"]//p/strong[contains(text(), "Country:")]/following-sibling::*[1]/text()'
            ],
            NormalizedJoin(', ')
        )
        loader.add_value(
            'referencenumber',
            sha1(response.url).hexdigest(),
            Prefix('%s-' % self.name)
        )
        loader.add_value('url', response.url)
        loader.add_xpath(
            'description',
            '//div[@class="content"]/div[@class="jobprofil"]',
            HtmlFormatter()
        )
        loader.add_xpath(
            'qualifications',
            '//div[@class="content"]/div[@class="jobqualif"]',
            HtmlFormatter()
        )
        loader.add_xpath(
            'company_description',
            '//div[@class="content"]/div[@class="job-acted"]',
            HtmlFormatter()
        )
        loader.add_xpath(
            'jobtype',
            '//div[@class="jobdetails"]//p/strong[contains(text(), "Contract:")]/../text()'
        )
        loader.add_xpath(
            'jobcategory',
            '//div[@class="jobdetails"]//p/strong[contains(text(), "Department:")]/../text()'
        )
        loader.add_xpath(
            'duration',
            '//div[@class="content"]/div[@class="jobdetails"]//p/strong[contains(text(), "Duration")]/../text()'
        )
        loader.add_value('apply_url', response.url)

        yield loader.load_item()


import datetime
from django.db.transaction import atomic

from SocialApp.models import Slide, Friend
from UserApp.models import User
from UserApp.models import Profile
from common import keys
from common import errors
from libs.cache import rds
from Tantan import config

def rcmd_from_queue(uid):
    '''从推荐列表进行推荐'''
    # 结果是2进制列表,取20个
    uid_list = rds.lrange(keys.FIRST_RCMD_Q % uid, 0, 19)
    # 转换成int类型
    uid_list = [int(uid) for uid in uid_list]

    users = User.objects.filter(id__in=uid_list)
    return users

def rcmd_from_db(uid, num=20):
    '''从数据库中推荐用户'''
    profile = Profile.objects.get(id=uid)

    # 计算出生日期范围
    today = datetime.date.today()
    earliest_birth = today - datetime.timedelta(days=profile.max_dating_age * 365)
    latest_birth = today - datetime.timedelta(days=profile.min_dating_age * 365)

    # 仅仅获取已经划过的人的sid
    sid_list = Slide.objects.filter(uid=uid).values_list('sid', flat=True)

    users = User.objects.filter(
        location=profile.dating_location,
        gender=profile.dating_gender,
        birthday__range=[earliest_birth, latest_birth]
    ).exclude(id__in=sid_list)[:num]

    return users

def rcmd(uid):
    '''推荐用户'''
    first_users = rcmd_from_queue(uid)
    remain = 20 - len(first_users)
    if remain > 0:
        second_users = rcmd_from_db(uid, num=remain)
        return set(first_users) | set(second_users)
    else:
        return first_users

# 将函数放在事务中执行，出错会自动回滚，成功自动提交

@atomic
def like_someone(uid, sid):
    '''喜欢的人'''
    # 1.添加滑动记录
    Slide.slide(uid=uid, sid=sid, slide_type='like')

    # 2.强制删除优先推荐队列里的id
    rds.lrem(keys.FIRST_RCMD_Q % uid, count=0, value=sid)

    # 给被滑动者添加积分
    rds.zincrby(keys.HOT_RANK, config.SLIDE_SCORE['like'], sid)

    # 3.检查对方是否喜欢自己，如果喜欢那么建立好友关系
    is_like = Slide.is_liked(uid=sid, sid=uid)
    if is_like is True:
        Friend.make_friend(uid, sid)
        return True
    else:
        return False

@atomic
def superlike_someone(uid, sid):
    '''超级喜欢'''
    # 1.添加滑动记录
    Slide.slide(uid=uid, sid=sid, slide_type='superlike')

    # 2.强制删除优先推荐队列里的id
    rds.lrem(keys.FIRST_RCMD_Q % uid, count=0, value=sid)

    # 给被滑动者添加积分
    rds.zincrby(keys.HOT_RANK, config.SLIDE_SCORE['superlike'], sid)

    # 3.检查对方是否喜欢自己，如果喜欢那么建立好友关系
    is_like = Slide.is_liked(uid=sid, sid=uid)
    if is_like is True:
        Friend.make_friend(uid, sid)
        return True
    elif is_like is False:
        return False
    else:
        # 对方没有滑到自己，将自己添加到对方的优先推荐队列
        rds.lpush(keys.FIRST_RCMD_Q % sid, uid)
        return False

def dislike_someone(uid, sid):
    '''不喜欢'''
    # 1.添加滑动记录
    Slide.slide(uid=uid, sid=sid, slide_type='dislike')

    # 2.强制删除优先推荐队列里的id
    rds.lrem(keys.FIRST_RCMD_Q % uid, count=0, value=sid)

    # 给被滑动者添加积分
    rds.zincrby(keys.HOT_RANK, config.SLIDE_SCORE['dislike'], sid)

def rewind_slide(uid):
    '''反悔(每天3次，5分钟内)'''
    now_time = datetime.datetime.now()
    today = now_time.date()
    rewind_key = keys.REWIND_K % (today, uid)

    # 检查是否已经3次
    rewind_num = rds.get(rewind_key, 0)
    if rewind_num >= config.REWIND_TIMES:
        raise errors.RewindLimit

    # 检查最后一次滑动是否在5分钟内
    last_slide = Slide.objects.filter(uid=uid).latest('slide_time')

    if now_time > last_slide.slide_time + datetime.timedelta(minutes=config.REWIND_TIMEOUT):
        raise errors.RewindTimeout

    # 将多次数据修改在事务中执行
    with atomic():

        # 检查是否是好友
        if last_slide.slide_type in ['like', 'superlike']:
            Friend.broken(uid, last_slide.sid)

            if last_slide.slide_type == 'superlike':
                # 从对方的推荐列表里删除我的id
                rds.lrem(keys.FIRST_RCMD_Q % last_slide.sid, count=0, value=uid)

        # 撤回被滑动者的积分
        score = config.SLIDE_SCORE[last_slide.slide_type]
        rds.zincrby(keys.HOT_RANK, -score, last_slide.sid)

        # 删除最后一次滑动
        last_slide.delete()

        # 设置缓存，反悔次数加一
        rds.set(rewind_key, rewind_num + 1, 86400)

def get_top_n(RANK_NUM):
    '''获取排行榜前n名的用户数据'''
    # 从redis里取出原始数据
    origin_data = rds.zrevrange(keys.HOT_RANK, 0, RANK_NUM - 1, withscores=True)
    # 原始数据转换成int类型
    cleaned_data = [[int(uid), int(score)] for uid, score in origin_data]

    # 用户的id列表
    uid_list = [item[0] for item in cleaned_data]

    # 数据库取数据
    users = User.objects.filter(id__in=uid_list)
    users = sorted(users, key=lambda user: uid_list.index(user.id))

    # 整理数据
    rank_data = []
    for index, (uid, score) in enumerate(cleaned_data):
        rank = index + 1
        user = users[index]
        user_data = user.to_dict(exclude=['phonenum', 'birthday', 'location',
                                          'vip_id', 'vip_end'])
        user_data['rank'] = rank
        user_data['score'] = score
        rank_data.append(user_data)

    return rank_data


# -*- coding:utf-8 -*-
'''
__author__:liubin

'''
from django.conf.urls import url

from . import views

urlpatterns = [

    url(r'^index/$',views.index),
    url(r'^login/$', views.login),

    url(r'^login_handle/$',views.login_handle),
]


from __future__ import print_function
import tensorflow as tf
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle

import re
import numpy as np
import os
import time
import json.encoder
from glob import glob
from PIL import Image
import pickle

def download_data():
    annotation_zip = tf.keras.utils.get_file('captions.zip',
                                          cache_subdir=os.path.abspath('.'),
                                          origin = 'http://images.cocodataset.org/annotations/annotations_trainval2014.zip',
                                          extract = True)
    annotation_file = os.path.dirname(annotation_zip)+'/annotations/captions_train2014.json'

    name_of_zip = 'train2014.zip'
    if not os.path.exists(os.path.abspath('.') + '/' + name_of_zip):
        image_zip = tf.keras.utils.get_file(name_of_zip,
                                        cache_subdir=os.path.abspath('.'),
                                        origin = 'http://images.cocodataset.org/zips/train2014.zip',
                                        extract = True)
        PATH = os.path.dirname(image_zip)+'/train2014/'
    else:
        PATH = os.path.abspath('.')+'/train2014/'

    return annotation_file

def prepaer_data(annotation_file):
    with open(annotation_file, 'r') as f:
        annotations = json.load(f)

        # Store captions and image names in vectors
        all_captions = []
        all_img_name_vector = []

        for annot in annotations['annotations']:
            caption = '<start> ' + annot['caption'] + ' <end>'
            image_id = annot['image_id']
            full_coco_image_path = PATH + 'COCO_train2014_' + '%012d.jpg' % (image_id)

            all_img_name_vector.append(full_coco_image_path)
            all_captions.append(caption)

        # Shuffle captions and image_names together
        # Set a random state
        train_captions, img_name_vector = shuffle(all_captions,
                                                all_img_name_vector,
                                                random_state=1)

        # Select the first 30000 captions from the shuffled set
        num_examples = 30000
        train_captions = train_captions[:num_examples]
        img_name_vector = img_name_vector[:num_examples]

def load_image(image_path):
    img = tf.io.read_file(image_path)
    img = tf.image.decode_jpeg(img, channels=3)
    img = tf.image.resize(img, (299, 299))
    img = tf.keras.applications.inception_v3.preprocess_input(img)
    return img, image_path


from itertools import combinations_with_replacement

import scipy
from scipy.constants import Boltzmann, Avogadro

import numpy as np

import mdtraj as md

kb = 10**-3 * Boltzmann * Avogadro

__all__ = ['GNMA']

class GNMA(object):
    """A class for Gaussian Network Model Analysis (GNMA) ([IB97]_, [TH97]_)
    using MDTraj.

    See example :ref:`gnm`.

    .. [IB97] Bahar I, Atilgan AR, Erman B. Direct evaluation of thermal
       fluctuations in protein using a single parameter harmonic potential.
       *Folding & Design* **1997** 2:173-181.

    .. [TH97] Haliloglu T, Bahar I, Erman B. Gaussian dynamics of folded
       proteins. *Phys. Rev. Lett.* **1997** 79:3090-3093.
    """

    def __init__(self, mode=0, k=1., nb_cutoff=0.5, steps=50, temp=300.,
                 selection='not element H'):
        self.mode = mode
        self.k = k
        self.nb_cutoff = nb_cutoff
        self.steps = steps
        self.temp = temp
        self.selection = selection
        self.dirty = True

    def _define_interactions(self):
        self.bonded = [(i.index, j.index) for i, j in self.top.bonds]
        self.bonded += [(j.index, i.index) for i, j in self.top.bonds]
        combos = combinations_with_replacement(self.top.atoms, 2)
        self.non_bonded = [(i.index, j.index) for i, j in combos
                           if i != j and (i.index, j.index) not in self.bonded]

    def _solve(self, K):

        vals, vecs = scipy.linalg.eigh(K, turbo=True, eigvals=None)
        ind = np.argsort(vals)[1:]
        self.eigenvalues_, self.eigenvectors_ = vals[ind], vecs[:, ind]

    def _reweight(self):
        self.frequencies_ = np.sqrt(np.abs(self.eigenvalues_)) / (2. * np.pi)
        self.weighted_eigenvectors_ = (self.eigenvectors_ /
                                       np.sqrt(self.masses *
                                               np.ones_like(self.eigenvectors_
                                                            ).T
                                               ).T
                                       )
        self.amplitudes_ = np.sqrt(
            2 * self.temp * kb) / (2 * np.pi * self.frequencies_)
        self.weighted_eigenvectors_ *= self.amplitudes_

    def _animate(self, mode):
        mid = int(self.steps / 2.)
        xyz = np.zeros((self.steps + 1, self.top.n_atoms, 3))
        xyz[mid] = self.xyz[0]

        if isinstance(mode, int):
            mode = [mode]

        grad = np.atleast_2d(self.weighted_eigenvectors_[:, mode].dot(
            self.frequencies_[mode])).T
        grad *= np.ones((self.top.n_atoms, 3))

        for i in range(mid):
            xyz[i + mid + 1] = xyz[i + mid] + grad
            xyz[mid - i - 1] = xyz[mid - i] - grad

        return md.Trajectory(xyz[:-1], self.top)

    def transform(self, X):
        if self.dirty:
            self.fit(X)
            self.dirty = False

        return self._animate(self.mode)

    def _nb_force(self, x):
        return 1E-6 / (x - self.nb_cutoff).clip(1E-3, np.inf)**2.

    def fit(self, X):
        self.ind = X.top.select(self.selection)
        self._structure = X[0].atom_slice(self.ind)
        self.top = self._structure.top
        self.xyz = self._structure.xyz

        # Extract Masses
        self.masses = np.array([i.element.mass for i in self.top.atoms])
        M = np.sqrt(np.outer(self.masses, self.masses))

        # Define Non-Bonded and Bonded Interactions
        self._define_interactions()

        # Initialize Kirchoff Matrix
        self.kirchkoff_ = np.zeros((self.top.n_atoms, self.top.n_atoms))

        # Add Non-Bonded Interactions to Kirchoff Matrix
        d = md.compute_distances(self._structure,
                                 atom_pairs=self.non_bonded).ravel()
        self.kirchkoff_[list(zip(*self.non_bonded))] = - (self.k *
                                                          self._nb_force(d))
        self.kirchkoff_ += self.kirchkoff_.T

        # Add Bonded Interactions to Kirchoff Matrix
        if self.bonded:
            self.kirchkoff_[list(zip(*self.bonded))] = - 1E2 * self.k

        # Set Diagonals
        dind = np.diag_indices_from(self.kirchkoff_)
        self.kirchkoff_[dind] -= self.kirchkoff_.sum(axis=1)

        # Solve Eigenvalue Problem
        self._solve(self.kirchkoff_ / M)

        # Reweight Eigenvalues and Eigenvectors
        self._reweight()

        self.dirty = False

        return self

    def fit_transform(self, X):
        self.dirty = True
        return self.transform(X)


import argparse
import os
import sys
import yaml

from jinja2 import Environment, FileSystemLoader

def renderTemplate(env, template_file, context):
	return env.get_template(template_file).render(context)

def createPrototxtFile(env, template_file, context, prototxt_file):
	with open(prototxt_file, "w") as f:
		f.write(renderTemplate(env, template_file, context))

def readYamlFile(yaml_file):
	with open(yaml_file, "r") as f:
		return yaml.load(f)

def parseValue(string):
	try:
		return int(string)
	except ValueError:
		pass

	try:
		return float(string)
	except ValueError:
		return string

def run(args):
	env = Environment(
		autoescape  = False,
		loader      = FileSystemLoader("templates"),
		trim_blocks = False
	)

	context = {}
	if args.yaml_files:
		for y in args.yaml_files:
			try:
				context.update(readYamlFile(y))
			except IOError:
				print("Unable to open parameters file '%s'." % y)
				sys.exit(-1)

	if args.parameters:
		for p in args.parameters:
			name, sep, value = p.partition("=")
			if sep == "=": context[name] = parseValue(value)

	createPrototxtFile(env, args.template_file, context, args.prototxt_file)
	print("Generated prototxt file: '%s'" % args.prototxt_file)

if __name__ == "__main__":
	parser = argparse.ArgumentParser(description = "Generate train/test prototxt files for the MNC network.")
	parser.add_argument("template_file",      help = "path to the template file which stores the network architecture")
	parser.add_argument("prototxt_file",      help = "path to the output prototxt file")
	parser.add_argument("-y", "--yaml_files", help = "path to the YAML files which stores the parameters",       action = "append")
	parser.add_argument("-p", "--parameters", help = "overrides certain parameters specified in the YAML files", action = "append")
	args = parser.parse_args()

	if not os.path.exists(os.path.join(os.getcwd(), "templates")):
		print("Please run this script from the MNC working directory.")
		sys.exit(-1)

	run(args)


# -*- coding: utf-8 -*-
# © 2016 Alessandro Fernandes Martini, Trustcode
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from jinja2 import Environment, FileSystemLoader
import unicodedata
from lxml import etree
from lxml import objectify
from builtins import str as text

HOMOLOGACAO = 1
PRODUCAO = 2

URLS = {
    HOMOLOGACAO: {
        'CalcularFretePrazo': 'http://ws.correios.com.br/calculador/CalcPrecoPrazo.asmx?WSDL',  #noqa
        'SIGEPWeb': 'https://apphom.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente?wsdl',  #noqa
        'BuscaEventos': 'http://webservice.correios.com.br/service/rastro'
    },
    PRODUCAO: {
        'CalcularFretePrazo': 'http://ws.correios.com.br/calculador/CalcPrecoPrazo.asmx?WSDL',  #noqa
        'SIGEPWeb': 'https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente?wsdl',  #noqa
        'BuscaEventos': 'http://webservice.correios.com.br/service/rastro'
    },
}

def render_xml(path, template_name, usuario, validation_schema=None):
    env = Environment(loader=FileSystemLoader(path),
                      extensions=['jinja2.ext.with_'])
    template = env.get_template(template_name)
    xml = template.render(usuario)
    parser = etree.XMLParser(
        remove_blank_text=True,
        remove_comments=True,
        strip_cdata=False,
        schema=validation_schema
    )
    root = etree.fromstring(xml, parser=parser)
    return etree.tostring(root)

def sanitize_response(response):
    response = text(response)
    response = unicodedata.normalize('NFKD', response).encode('ascii',
                                                              'ignore')
    tree = etree.fromstring(response)
    # Remove namespaces inuteis na resposta
    for elem in tree.getiterator():
        if not hasattr(elem.tag, 'find'):
            continue
        i = elem.tag.find('}')
        if i >= 0:
            elem.tag = elem.tag[i+1:]
    objectify.deannotate(tree, cleanup_namespaces=True)
    return response, objectify.fromstring(etree.tostring(tree))

def _valida(metodo, api, kwargs):
    if api == 'SIGEPWeb':
        ambiente_nao_obrigatorio = ['cep_consulta',
                                    'digito_verificador_etiqueta']
        if metodo not in ambiente_nao_obrigatorio and 'ambiente' not in kwargs:
            raise Exception('O ambiente é obrigatório neste método')


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('application', '0015_auto_20160216_0102'),
    ]

    operations = [
        migrations.AlterField(
            model_name='students',
            name='e_mail',
            field=models.CharField(max_length=30, null=True, verbose_name='e-mail', blank=True),
        ),
        migrations.AlterField(
            model_name='students',
            name='phone',
            field=models.CharField(max_length=20, verbose_name='\u0422\u0435\u043b\u0435\u0444\u043e\u043d'),
        ),
    ]