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

from django.db import models, migrations
import versatileimagefield.fields

class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='DownloadSource',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('page_hash', models.CharField(db_index=True, max_length=255, blank=True)),
                ('page_url', models.CharField(max_length=1024, blank=True)),
                ('page_title', models.CharField(max_length=1024, blank=True)),
                ('thumbnail', versatileimagefield.fields.VersatileImageField(upload_to=b'images/download_source_thumbnail/', verbose_name=b'Thumbnail')),
                ('thumbnail_url', models.CharField(default=b'', max_length=1024, null=True, blank=True)),
                ('domain', models.CharField(max_length=255, blank=True)),
                ('create_time', models.DateTimeField(auto_now_add=True)),
                ('last_modify_time', models.DateTimeField(auto_now=True)),
                ('lock_stats', models.IntegerField(default=0, choices=[(0, b'Default'), (1, b'Deleted'), (2, b'Expired')])),
            ],
        ),
        migrations.CreateModel(
            name='DownloadSourceItems',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('file_url', models.CharField(max_length=1024, blank=True)),
                ('file_name', models.CharField(max_length=255, blank=True)),
                ('is_multi_part', models.BooleanField(default=False)),
                ('is_direct', models.BigIntegerField(default=True)),
                ('file_size', models.IntegerField(default=0)),
                ('content_type', models.CharField(max_length=255, null=True, blank=True)),
                ('create_time', models.DateTimeField(auto_now_add=True)),
                ('last_modify_time', models.DateTimeField(auto_now=True)),
                ('source', models.ForeignKey(blank=True, to='vidfans.DownloadSource', null=True)),
            ],
        ),
        migrations.CreateModel(
            name='Episodes',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('title', models.CharField(max_length=1024)),
                ('desc', models.TextField(null=True, blank=True)),
                ('air_date', models.DateField(null=True, blank=True)),
                ('episode_number', models.IntegerField(default=0)),
            ],
        ),
        migrations.CreateModel(
            name='Movie',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('content_type', models.CharField(default=b'M', max_length=1, db_index=True, choices=[(b'M', b'Movie'), (b'T', b'Tv'), (b'A', b'Animate')])),
                ('title', models.CharField(max_length=255, db_index=True)),
                ('year', models.CharField(db_index=True, max_length=255, blank=True)),
                ('imdb_id', models.CharField(db_index=True, max_length=255, blank=True)),
                ('tmdb_id', models.CharField(db_index=True, max_length=255, blank=True)),
                ('poster', versatileimagefield.fields.VersatileImageField(upload_to=b'images/movie_poster/', verbose_name=b'poster_url')),
                ('poster_url', models.CharField(default=b'', max_length=1024, null=True, blank=True)),
                ('backdrop', versatileimagefield.fields.VersatileImageField(upload_to=b'images/movie_backdrop/', verbose_name=b'backdrop')),
                ('backdrop_url', models.CharField(default=b'', max_length=1024, null=True, blank=True)),
                ('genres', models.CharField(max_length=255, null=True, blank=True)),
                ('director', models.CharField(max_length=255, null=True, blank=True)),
                ('writers', models.CharField(max_length=1024, null=True, blank=True)),
                ('cast', models.CharField(max_length=1024, null=True, blank=True)),
                ('desc', models.TextField(blank=True)),
                ('alternative_titles', models.CharField(max_length=1024, null=True, blank=True)),
                ('country', models.CharField(max_length=255, null=True, blank=True)),
                ('language', models.CharField(max_length=255, null=True, blank=True)),
                ('also_known_as', models.CharField(max_length=255, null=True, blank=True)),
                ('runtime', models.CharField(max_length=255, null=True, blank=True)),
                ('plot_keywords', models.CharField(max_length=1024, null=True, blank=True)),
                ('trailer', models.CharField(max_length=1024, blank=True)),
                ('rating', models.FloatField(default=0.0)),
                ('popularity', models.FloatField(default=0.0)),
                ('trending', models.FloatField(default=0.0)),
                ('num_of_view', models.IntegerField(default=0)),
                ('num_of_play', models.IntegerField(default=0)),
                ('released_time', models.DateTimeField(null=True, blank=True)),
                ('create_time', models.DateTimeField(auto_now_add=True)),
                ('last_modify_time', models.DateTimeField(auto_now=True)),
            ],
        ),
        migrations.CreateModel(
            name='Seasons',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('title', models.CharField(db_index=True, max_length=256, null=True, blank=True)),
                ('year', models.CharField(max_length=256, db_index=True)),
                ('season_num', models.CharField(max_length=256, blank=True)),
                ('movie', models.ForeignKey(blank=True, to='vidfans.Movie', null=True)),
            ],
        ),
        migrations.AddField(
            model_name='episodes',
            name='season',
            field=models.ForeignKey(blank=True, to='vidfans.Seasons', null=True),
        ),
        migrations.AddField(
            model_name='downloadsource',
            name='episode',
            field=models.ForeignKey(blank=True, to='vidfans.Episodes', null=True),
        ),
        migrations.AddField(
            model_name='downloadsource',
            name='movie',
            field=models.ForeignKey(blank=True, to='vidfans.Movie', null=True),
        ),
    ]


# -*- coding: utf-8 -*-
import constants
import json
import nlp_base
import youtokentome as yttm

class ContinueOuterLoop(Exception):
    pass

vocab_size = 22000

bpe_tokenizer = yttm.BPE(constants.BPE_MODEL_FILENAME+'_'+str(vocab_size))
CHUNK_SIZE = 200

cur_index = 0
with_answer_in_text = 0
without_answer_in_text = 0

try:
    with open(file='../../wiki/all-full-name-pages-json', mode='r', encoding='utf-8') as f:
        with open(file='nn_data/all_nn_data'+'_'+str(vocab_size), mode='w', encoding='utf-8') as resf:
            for _ in range(cur_index):
                next(f)

            for line in f:
                cur_index += 1
                cur_json = json.loads(line)

                page_title = cur_json['title']
                page_text = cur_json['text']

                entity_to_search = nlp_base.get_search_strings(page_title)

                if not entity_to_search:
                    without_answer_in_text += 1
                    continue

                try:
                    tokenized_chunks, token_positions = nlp_base.get_nn_data(bpe_tokenizer, entity_to_search, page_text, CHUNK_SIZE)
                except Exception as e:
                    print('GET NN DATA EXCEPTION!!!!!')
                    print(e)
                    print(f'Line {cur_index} failed!!!!!')
                    print(f'Page Title - {page_title}')
                    print(f'Page Text - {page_text}')
                    print('-------------------------')
                    continue

                test_input = tokenized_chunks
                test_answer = token_positions

                try:
                    for chunk in test_answer:
                        if not any(chunk):
                            without_answer_in_text += 1
                            raise ContinueOuterLoop
                except ContinueOuterLoop:
                    continue

                json.dump({'input': test_input, 'answer': test_answer}, resf)
                resf.write('\n')

                with_answer_in_text += 1

                if with_answer_in_text % 1000 == 0:
                    print(f'With answer - {with_answer_in_text}')

except Exception as e:
    print(f'Cycle exception!')
    print(e)
    print(f'Line {cur_index} failed!!!!!')
    print(f'Page Title - {page_title}')
    print(f'Page Text - {page_text}')

print(f'With answer - {with_answer_in_text}')
print(f'Without answer - {without_answer_in_text}')


"""
algorithm test harnesses

A test harness is used to evaluate an algorithm on a dataset.

needs:
- the resampling method to split up the dataset
- the algorithm under evaluation
- the measure of performance
"""

"""
train-test test harness

Algorithm trained on training data and makes predictions on test data.
"""
from random import seed
from random import randrange
from csv import reader

# CSV file load
def load_csv(filename):
    dataset = list()
    with open(filename, 'r') as file:
        csv_reader = reader(file)
        for row in csv_reader:
            if not row:
                continue
            dataset.append(row)
    return dataset

# string to float
def str_column_to_float(dataset, column):
    for row in dataset:
        row[column] = float(row[column].strip())

# train/test split
def train_test_split(dataset, split):
    train = list()
    # e.g. train on 60% of the data
    train_size = split * len(dataset)
    dataset_copy = list(dataset)
    while len(train) < train_size:
        index = randrange(len(dataset_copy))
        # append random selections from dataset copy to training data
        train.append(dataset_copy.pop(index))
    return train, dataset_copy

# measure accuracy
def accuracy_metric(actual, predicted):
    correct = 0
    for i in range(len(actual)):
        if actual[i] == predicted[i]:
            correct += 1
    return correct / float(len(actual)) * 100.0

# evaluate a given algorithm on a train/test split
def evaluate_algorithm(dataset, algorithm, split, *args):
    train, test = train_test_split(dataset, split)
    test_set = list()
    for row in test:
        row_copy = list(row)
        row_copy[-1] = None
        test_set.append(row_copy)
    predicted = algorithm(train, test_set, *args)
    actual = [row[-1] for row in test]
    accuracy = accuracy_metric(actual, predicted)
    return accuracy

# let's evaluate a zero rule classification algorithm
def zero_rule_classification(train, test):
    output_values = [row[-1] for row in train]
    prediction = max(set(output_values), key=output_values.count)
    predicted = [prediction for i in range(len(test))]
    return predicted

# example
seed(1)
filename = 'data.csv'
dataset = load_csv(filename)
for i in range(len(dataset[0])):
    str_column_to_float(dataset, i)
# train on 60% of data
split = 0.6
accuracy = evaluate_algorithm(dataset, zero_rule_classification, split)
print('Accuracy: %.3f%%' % (accuracy))
# Accuracy: 67.427%

"""
cross-validation test harness

- need to evaluate on k models on different subsets of the data
- gold standard for evaluation
- each fold/group has a turn to be held out of training
"""

# cross validation
def cross_validation_split(dataset, n_folds):
    dataset_split = list()
    dataset_copy = list(dataset)
    fold_size = int(len(dataset) / n_folds)
    for _ in range(n_folds):
        fold = list()
        while len(fold) < fold_size:
            index = randrange(len(dataset_copy))
            fold.append(dataset_copy.pop(index))
            dataset_split.append(fold)
        return dataset_split

def evaluate_algorithm_cross_validation(dataset, algorithm, n_folds, *args):
    folds = cross_validation_split(dataset, n_folds)
    scores = list()
    for fold in folds:
        train_set = list(folds)
        train_set.remove(fold)
        # the list of folds is flattened into a list of rows
        # to match the algorithm's expectation of a training dataset
        train_set = sum(train_set, [])
        test_set = list()
        for row in fold:
            row_copy = list(row)
            test_set.append(row_copy)
            row_copy[-1] = None
        predicted = algorithm(train_set, test_set, *args)
        actual = [row[-1] for row in fold]
        accuracy = accuracy_metric(actual, predicted)
        scores.append(accuracy)
    return scores

# example
seed(1)
filename = 'data.csv'
dataset = load_csv(filename)
for i in range(len(dataset[0])):
    str_column_to_float(dataset, i)
n_folds = 5
scores = evaluate_algorithm(dataset, zero_rule_classification, n_folds)
print('Scores: %s' % scores)
print('Mean Accuracy: %.3f%%' % (sum(scores)/len(scores)))
# Scores: [62.091503267973856, 64.70588235294117, 64.70588235294117, 64.70588235294117,
# 69.28104575163398]
# Mean Accuracy: 65.098%


'''
Not working
I don't think the data set exists anymore
'''
# Imports
from __future__ import absolute_import, division, print_function

import pathlib

import pandas as pd
import seaborn as sns

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

# Getting data
dataset_path = keras.utils.get_file("auto-mpg.csv", "https://www.kaggle.com/uciml/autompg-dataset")
column_names = ['MPG', 'Cylinders', 'Displacement', 'Horsepower', 'Weight',
                'Acceleration', 'Model Year', 'Origin']
raw_dataset = pd.read_csv(dataset_path, names=column_names,
                          na_values="?", comment='\t',
                          sep=" ", skipinitialspace=True)
dataset = raw_dataset.copy()
dataset.tail()

# Pre-processing data
dataset = dataset.dropna()  # Removing empty elements
origin = dataset.pop('Origin')  # Changing Origin column into 3 one-hots for each origin
dataset['USA'] = (origin == 1) * 1.0
dataset['Europe'] = (origin == 2) * 1.0
dataset['Japan'] = (origin == 3) * 1.0
dataset.tail()

# Splitting data into train and test
train_dataset = dataset.sample(frac=0.8, random_state=0)
test_dataset = dataset.drop(train_dataset.index)

sns.pairplot(train_dataset[["MPG", "Cylinders", "Displacement", "Weight"]], diag_kind="kde")


from logging import getLogger
from typing import Any, Dict, List, Set

from gnosis.eth import EthereumClient

from ..models import EthereumTxCallType, EthereumTxType, InternalTx
from .transaction_scan_service import TransactionScanService

logger = getLogger(__name__)

class InternalTxServiceProvider:
    def __new__(cls):
        if not hasattr(cls, 'instance'):
            from django.conf import settings
            cls.instance = InternalTxService(EthereumClient(settings.ETHEREUM_TRACING_NODE_URL),
                                             block_process_limit=settings.INTERNAL_TXS_BLOCK_PROCESS_LIMIT)
        return cls.instance

    @classmethod
    def del_singleton(cls):
        if hasattr(cls, "instance"):
            del cls.instance

class InternalTxService(TransactionScanService):
    @property
    def database_field(self):
        return 'tx_block_number'

    def find_relevant_tx_hashes(self, safe_addresses: List[str], from_block_number: int,
                                to_block_number: int) -> Set[str]:
        """
        Search for tx hashes with internal txs (in and out) of a `safe_address`
        :param safe_addresses:
        :param from_block_number: Starting block number
        :param to_block_number: Ending block number
        :return: Tx hashes of txs with internal txs relevant for the `safe_addresses`
        """
        logger.debug('Searching for internal txs from block-number=%d to block-number=%d - Safes=%s',
                     from_block_number, to_block_number, safe_addresses)

        to_traces = self.ethereum_client.parity.trace_filter(from_block=from_block_number,
                                                             to_block=to_block_number,
                                                             to_address=safe_addresses)

        from_traces = self.ethereum_client.parity.trace_filter(from_block=from_block_number,
                                                               to_block=to_block_number,
                                                               from_address=safe_addresses)

        # Log INFO if traces found, DEBUG if not
        log_fn = logger.info if to_traces + from_traces else logger.debug
        log_fn('Found %d relevant txs between block-number=%d and block-number=%d. Safes=%s',
               len(to_traces + from_traces), from_block_number, to_block_number, safe_addresses)

        return set([trace['transactionHash'] for trace in (to_traces + from_traces)])

    def process_tx_hash(self, tx_hash: str) -> List[InternalTx]:
        """
        Search on Ethereum and store internal txs for provided `tx_hash`
        :param tx_hash:
        :return: List of `InternalTx` already stored in database
        """
        return self._process_traces(self.ethereum_client.parity.trace_transaction(tx_hash))

    def _process_trace(self, trace: Dict[str, Any]) -> InternalTx:
        ethereum_tx = self.create_or_update_ethereum_tx(trace['transactionHash'])
        return InternalTx.objects.get_or_create_from_trace(trace, ethereum_tx)

    def _process_traces(self, traces: List[Dict[str, Any]]) -> List[InternalTx]:
        return [self._process_trace(trace) for trace in traces]


import os
from shutil import copy2
from pathlib import Path

#not working

def copyFile (srcname,dstname,myfile):
    try:

        my_file = Path(dstname+myfile)
        if my_file.is_file():
            # file exists then remove it
            try:
                os.remove(dst_file)
            except PermissionError as exc:
                os.chmod(dst_file, stat.S_IWUSR)
                os.remove(dst_file)
        # Copy file
        my_folder = Path(dstname)
        if my_folder.is_dir():
            result = copy2(os.path.join(srcname+myfile), dstname)

        else:
            result = "folder not foud"
        return result

    except OSError as why:
        return "Error"

source_folder = "/Users/anandihalli/Documents/01_Work/anglofrench/MasterPSD/MasterFile_Onboarding-assets"
basename = "load_xfe_anglofrench_GALive.jpg"
srcname = source_folder

dstname = "/Users/anandihalli/Documents/Scripting/Pyhton/temp"
copyFile(srcname,dstname,basename)

Pyhton/Tests and genral scripts/test_copy_files_2.py


class Person:
  def __init__(self,name):
    self.name=name

  def show_name(self):
    return f'My name is {self.name}'

def strange_func(*args):
  for arg in args:
    try:
      print(arg.show_name())
    except:
      print("У данного аргумента нет имени")

john = Person('John')
alice = Person('Alice')
charlie = Person('Charlie')

strange_func(john,alice,charlie)


# -*- coding: UTF-8 -*-
import numpy as np
import sys
import caffe
import logging
import time
from google.protobuf import text_format
from caffe.proto import caffe_pb2
from PIL import Image
caffe_root = '/home/sh/workplace/objectDetection/ssd/caffe/'
sys.path.insert(0, caffe_root + 'python')

np.set_printoptions(threshold='nan')
logging.basicConfig(filename='./logger.log', level=logging.INFO)
# Make sure that caffe is on the python path:
# caffe_root = '../'  # this file is expected to be in {caffe_root}/examples
caffe_root = '/home/sh/workplace/objectDetection/ssd/caffe/'
# load PASCAL VOC labels
labelmap_file = caffe_root + 'data/VOC0712/labelmap_voc.prototxt'
file = open(labelmap_file, 'r')
labelmap = caffe_pb2.LabelMap()
text_format.Merge(str(file.read()), labelmap)

def get_labelname(labelmap, labels):
    num_labels = len(labelmap.item)
    labelnames = []
    if type(labels) is not list:
        labels = [labels]
    for label in labels:
        found = False
        for i in range(0, num_labels):
            if label == labelmap.item[i].label:
                found = True
                labelnames.append(labelmap.item[i].display_name)
                break
        assert found is True
    return labelnames

def init_net(model_def, model_weights):
    caffe.set_device(1)  # if we have multiple GPUs, pick the first one
    caffe.set_mode_gpu()
    net = caffe.Net(model_def,      # defines the structure of the model
                    model_weights,  # contains the trained weights
                    caffe.TEST)     # use test mode
    return net

def init_imageNet():
    model_def = caffe_root + 'models/bvlc_reference_caffenet/deploy.prototxt'
    model_weights = caffe_root \
        + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'
    return init_net(model_def, model_weights)

def init_ssdNet():
    model_def = caffe_root \
        + 'models/VGGNet/VOC0712/SSD_300x300/deploy.prototxt'
    model_weights = caffe_root \
        + 'models/VGGNet/VOC0712/SSD_300x300/VGG_VOC0712_SSD_300x300_iter_120000.caffemodel'
    return init_net(model_def, model_weights)

def get_transformer(net, mu):
    # create transformer for the input called 'data'
    transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
    # move image channels to outermost dimension
    transformer.set_transpose('data', (2, 0, 1))
    # subtract the dataset-mean value in each channel
    transformer.set_mean('data', mu)
    # rescale from [0, 1] to [0, 255]
    transformer.set_raw_scale('data', 255)
    # swap channels from RGB to BGR
    transformer.set_channel_swap('data', (2, 1, 0))
    return transformer

def feature_extraction_imageNet(image_path, net):
    # load the mean ImageNet image (as distributed with Caffe) for subtraction
    mu = np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy')
    # average over pixels to obtain the mean (BGR) pixel values
    mu = mu.mean(1).mean(1)
    transformer = get_transformer(net, mu)
    # set the size of the input (we can skip this if we're happy
    # with the default; we can also change it later, e.g.,
    # for different batch sizes)
    net.blobs['data'].reshape(1,        # batch size
                              3,         # 3-channel (BGR) images
                              227, 227)  # image size is 227x227
    image = caffe.io.load_image(image_path)
    transformed_image = transformer.preprocess('data', image)
    # plt.imshow(image)
    net.blobs['data'].data[...] = transformed_image
    net.forward()
    feature = net.blobs['fc7'].data[0]
    # feature = net.blobs['fc6'].data[0]
    return feature

def feature_extraction_SSD(image_path, net):
    image = caffe.io.load_image(image_path)
    mu = np.array([104, 117, 123])
    transformer = get_transformer(net, mu)
    # set net to batch size of 1
    image_resize = 300
    net.blobs['data'].reshape(1, 3, image_resize, image_resize)
    transformed_image = transformer.preprocess('data', image)
    net.blobs['data'].data[...] = transformed_image
    # Forward pass.
    net.forward()
    feature = net.blobs['fc7'].data[0]
    return feature

def object_detection_SSD(image_path, net):
    image = caffe.io.load_image(image_path)
    mu = np.array([104, 117, 123])
    transformer = get_transformer(net, mu)
    # set net to batch size of 1
    image_resize = 300
    net.blobs['data'].reshape(1, 3, image_resize, image_resize)
    transformed_image = transformer.preprocess('data', image)
    net.blobs['data'].data[...] = transformed_image
    # Forward pass.
    detections = net.forward()['detection_out']
    return detections

def get_image_info(image_path, imageNet, ssdNet):
    detections = object_detection_SSD(image_path, ssdNet)
    # Parse the outputs.
    det_label = detections[0, 0, :, 1]
    det_conf = detections[0, 0, :, 2]
    det_xmin = detections[0, 0, :, 3]
    det_ymin = detections[0, 0, :, 4]
    det_xmax = detections[0, 0, :, 5]
    det_ymax = detections[0, 0, :, 6]

    # Get detections with confidence higher than 0.6.
    top_indices = [i for i, conf in enumerate(det_conf) if conf >= 0.6]

    top_conf = det_conf[top_indices]
    top_label_indices = det_label[top_indices].tolist()
    top_labels = get_labelname(labelmap, top_label_indices)
    top_xmin = det_xmin[top_indices]
    top_ymin = det_ymin[top_indices]
    top_xmax = det_xmax[top_indices]
    top_ymax = det_ymax[top_indices]

    image = caffe.io.load_image(image_path)
    img = Image.open(image_path)
    img_result = []
    for i in range(top_conf.shape[0]):
        object_info = {
            'category': None,
            'location': None,
            'feature': None,
            'hash': None,
            'score': 0
        }
        xmin = int(round(top_xmin[i] * image.shape[1])) \
            if (int(round(top_xmin[i] * image.shape[1]))) > 0 else 0
        ymin = int(round(top_ymin[i] * image.shape[0])) \
            if (int(round(top_ymin[i] * image.shape[0]))) > 0 else 0
        xmax = int(round(top_xmax[i] * image.shape[1])) \
            if (int(round(top_xmax[i] * image.shape[1]))) > 0 else 0
        ymax = int(round(top_ymax[i] * image.shape[0])) \
            if (int(round(top_ymax[i] * image.shape[0]))) > 0 else 0
        score = top_conf[i]
        category = top_labels[i]
        img_cut = img.crop((xmin, ymin, xmax, ymax))
        save_path = caffe_root + 'tmp/' + category \
            + time.strftime("%Y-%m-%d_%H:%M:%S", time.localtime())+'.jpg'
        img_cut.save(save_path)
        feature = feature_extraction_imageNet(save_path, imageNet)
        # feature = feature_extraction_SSD(save_path, ssdNet)
        hash = "TODO"
        object_info['feature'] = feature.tolist()
        object_info['location'] = str([xmin, ymin, xmax, ymax])[1:-1]
        object_info['category'] = category.encode('utf8')
        object_info['hash'] = hash
        object_info['score'] = score
        img_result.append(object_info)
    return img_result


from django import forms
from .models import Livestream, LivestreamGroup
from django.contrib.auth.models import User
from . import twitchAPI as TwitchAPI

class LivestreamForm(forms.ModelForm):
    class Meta:
        model = Livestream
        exclude = ['user', 'twitchUserId']
        labels = {
            'twitchUsername': 'Twitch Username'
        }
    # verifies livestream form fields are unique to user
    # have to override because we don't ask for user in form which normally
    # excludes user from validation
    def validate_unique(self):
        exclude = self._get_validation_exclusions()
        exclude.remove('user') # allow checking against the missing attribute

        try:
            self.instance.validate_unique(exclude=exclude)
        except forms.ValidationError as e:
            self._update_errors(e.message_dict)
    # verifies twitch username is valid
    #def clean_twitchUsername(self):
    #    cleaned_twitchUsername = self.cleaned_data['twitchUsername']

     #   if not TwitchAPI.userValid(cleaned_twitchUsername):
     #       raise forms.ValidationError('Twitch username not found.')
     #   return cleaned_twitchUsername

class LivestreamGroupForm(forms.ModelForm):
    class Meta:
        model = LivestreamGroup
        exclude = ['user']

    # verifies livestreamGroup form fields are unique to user
    # have to override because we don't ask for user in form which normally
    # excludes user from validation
    def validate_unique(self):
        exclude = self._get_validation_exclusions()
        exclude.remove('user') # allow checking against the missing attribute

        try:
            self.instance.validate_unique(exclude=exclude)
        except forms.ValidationError as e:
            self._update_errors(e.message_dict)


import asyncio
import websockets

async def server(ws, path):
    print("path", path)
    message = await ws.recv()
    print(f"command: {message}")

    if message == "ping":
        await ws.send("pong")
    elif message == "timeout":
        await asyncio.sleep(3)
        await ws.send("timeout pong")
    else:
        await ws.send("unknown command")

start_server = websockets.serve(server, 'localhost', 8765)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()


# Using dictionary to store the subtraction result,
# Search in dictionary is O(1)
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        result = {}
        for i in range(len(nums)):
            diff = target - nums[i]
            if diff in result:
                return [i, result[diff]]
            else:
                result[nums[i]] = i

'''
Javascript Code:

var twoSum = function(nums, target) {
    var result = {};
    var length = nums.length;
    for (var i = 0; i < length; i++) {
        var diff = target - nums[i];
        if (result.hasOwnProperty(diff)){
            return [i, result[diff]];
        } else{
            result[nums[i]] = i;
        }
    }
};
'''


import mxnet as mx
from mxnet import nd, autograd, gluon
import numpy as np
import scipy
import pickle
import sys
from time import process_time

import inspect

# Parameters
CTX = mx.gpu(0)

NUM_LAYER = 100
NUM_UNITS = 500
LEARNING_RATE = 1e-4
INPUT_STD = 1
OUTPUT_STD = 1
ACT = 'tanh'
NUM_LAYER_LIST = [500]
LEARNING_RATE_LIST = [LEARNING_RATE]
NUM_HOUSEHOLDER_V = 20

n_epoch = 100
trial_times = 100
plot_batch = 1

def dump(data, filename):
    with open(filename, 'wb') as f:
        pickle.dump(data, f)

def Rsq(J):
    K = J@J.T
    sum_a = np.diag(K@K).sum()
    sum_b = np.diag(K).sum()
    return sum_a/sum_b**2, sum_a, sum_b

def eye(F, N):
    return F.one_hot(F.arange(N, ctx=CTX), N)

class Householder(mx.gluon.HybridBlock):
    def __init__(self, N, use_bias=True, dtype='float32', bias_initializer='zeros', **kwargs):
        super(Householder, self).__init__(**kwargs)
        with self.name_scope():
            self.N = N
            self.num_v = NUM_HOUSEHOLDER_V
            # Number of Householder vectors in a layer
            self.v_concat = self.params.get('v_concat',
                shape=(N, self.num_v), init=mx.initializer.Normal(), dtype=dtype)
            if use_bias:
                self.bias = self.params.get('bias', shape=(N,), init=bias_initializer, dtype=dtype,
                                            allow_deferred_init=True)
            else:
                self.bias = None

    def hybrid_forward(self, F, x, v_concat, bias=None):
        I = eye(F, self.N)
        for iV in range(self.num_v):
            v = F.slice(v_concat, begin=(None, iV), end=(None, iV+1))
            vv = F.broadcast_div(F.dot(v, F.transpose(v)), F.dot(F.transpose(v), v))
            H = I - 2*vv
            x = F.FullyConnected(x, H, None, no_bias=True, num_hidden=self.N, flatten=True, name='fwd%d'%iV)
        x = x + bias
        return x

    def __repr__(self):
        s = '{name}({N}, linear)'
        return s.format(name=self.__class__.__name__,
                        N=str(self.N))

class MLP(gluon.Block):
    def __init__(self, num_layer=10, num_units=500, **kwargs):
        super(MLP, self).__init__(**kwargs)
        with self.name_scope():
            data = mx.sym.var('input')
            self.dense_list = [mx.gluon.nn.Dense(num_units, use_bias=True)]
            for iL in range(num_layer-1):
                self.dense_list.append(
                    Householder(num_units, use_bias=True)
                )
            self.dense_list.append(mx.gluon.nn.Dense(10, use_bias=True))
        for dense in self.dense_list:
            self.register_child(dense)
        self.input = None
        self.hidden_list = []

    def forward(self, x):
        self.input = x
        self.input.attach_grad()
        self.hidden_list = []
        for dense in self.dense_list[0:-1]:
            # x = dense(x)
            x = dense(x)
            # x.attach_grad()
            self.hidden_list.append(x)
            if ACT == 'relu':
                x = nd.relu(x)
            elif ACT == 'tanh':
                x = nd.tanh(x)
            elif ACT == 'hard-tanh':
                x = 2*nd.hard_sigmoid(x, alpha=0.5, beta=0.5)-1
        x = self.dense_list[-1](x)
        # x.attach_grad()
        self.hidden_list.append(x)
        return x

# Data
def data_xform(data):
    return nd.moveaxis(data, 2, 0).astype('float32') / 255
mnist = mx.test_utils.get_mnist()
train_data = mx.gluon.data.vision.MNIST(train=True).transform_first(data_xform)
val_data = mx.gluon.data.vision.MNIST(train=False).transform_first(data_xform)
train_loader = mx.gluon.data.DataLoader(train_data, shuffle=True, batch_size=100)
val_loader = mx.gluon.data.DataLoader(val_data, shuffle=False, batch_size=100)
all_loader = mx.gluon.data.DataLoader(val_data, shuffle=False, batch_size=50000)

metric = mx.metric.create([mx.metric.Accuracy(), mx.metric.CrossEntropy()])
loss_function = gluon.loss.SoftmaxCrossEntropyLoss()

# Record for different activation
ln_net = MLP(num_layer=NUM_LAYER, num_units=NUM_UNITS)
n_seed = 1
n_lr = 1
lr_range = [LEARNING_RATE]
batch_size = 100
rsq_period = 100

rsq_array = np.zeros((n_seed, n_lr, 600*n_epoch//rsq_period))
BRA = {}
start = process_time()

for seed in range(0, n_seed):
    print('Seed %d'%seed)
    # weight_container = np.zeros((n_lr, n_epoch*600//rsq_period, NUM_LAYER-2, NUM_UNITS, NUM_UNITS))
    for iA, lr in enumerate(lr_range):
        print('Learning rate: %2E'%lr)
        mx.random.seed(seed)
        np.random.seed(seed)
        ln_net.collect_params().initialize(
            mx.init.Xavier(rnd_type='gaussian', magnitude=2 if ACT == 'relu' else 1),ctx=CTX, force_reinit=True)
        opt = mx.optimizer.SGD(learning_rate=lr)
        trainer = gluon.Trainer(ln_net.collect_params(), opt)

        for e in range(n_epoch):
            print('Epoch %d' %e)
            sys.stdout.flush()
            b = 0
            for inputs, labels in train_loader:
                inputs = inputs.as_in_context(CTX)
                labels = labels.as_in_context(CTX)

                with autograd.record():
                    outputs = ln_net(inputs)
                    loss = loss_function(outputs, labels)

                loss.backward()
                trainer.step(batch_size=inputs.shape[0])

                if b % rsq_period == 0:
                    metric.reset()
                    val_output_list = []
                    for inputs, labels in val_loader:
                        inputs = inputs.as_in_context(CTX)
                        labels = labels.as_in_context(CTX)
                        outputs = ln_net(inputs)
                        metric.update(labels, mx.nd.softmax(outputs))
                        val_output_list.append(outputs.asnumpy())
                    te_metric = metric.get()[1]
                    Y = np.concatenate(val_output_list, axis=0)
                    Y = (Y-np.mean(Y, axis=0)).T
                    rsq_y = Rsq(Y)[0]
                    rsq_array[seed, iA, (e*600+b)//rsq_period] = rsq_y
                    print('Batch: %d, Rsq: %.6f, Acc: %.3f' %(b, rsq_y, te_metric[0]))
                    BRA = {'Batch':e*600+b,'Rsq':rsq_y,'Acc':te_metric[0]}
                    file = str(str(seed) + '_' + str(e) + '_' + str(b) + '.pickle')
                    dump(BRA, file)
                    sys.stdout.flush()

                    for layer_index in range(1, NUM_LAYER-1):
                        # weight_container[seed, iA, (e*600+b)//rsq_period, layer_index-1, :, :] = ln_net.dense_list[layer_index].weight.data().asnumpy()
                        pass
                b += 1
            # pickle.dump(weight_container, open('weight_seed%d.pickle'%seed, 'wb'))

    print('\nNetwork width = %d\nNetwork depth = %d\nNonlinear type = %s\nLearning rate = %.2E\nBatch size = %d'
         %(NUM_UNITS, NUM_LAYER, "Tanh(SGD)", LEARNING_RATE, batch_size))

end = process_time()
time = abs(start - end)
print("time:", time)


# Kennesaw State University
# College of Computer and Software Engineering
# CS 4308, Concepts of Programming Languages, SN: 01
# Julia Parser In Python
# Leonard Hayes : lhayes32@students.kennesaw.edu
# 12/4/2019
# File Description: This file is used to decode Statements within blocks of the parser

from Parser.Decode_Expression import Expression

class Statement:
    line_index = 0

    # Begins with creating setting up local variables, then will break down the Expressions within each statement
    def __init__(self, line, etype):
        self.expressions = []
        self.print_tokens = []
        self.line = line
        self.etype = etype
        self.blocks = []

        if etype == "ASSIGNMENT":
            if len(line) == 3:
                self.expressions.append(Expression("ASSIGNMENT", line))
            else:
                self.expressions.append(Expression("ASSIGNMENT", line[0:2]))
                self.expressions.append(Expression("ARITHMETIC", line[2:5]))

        elif etype == "WHILE":
            self.expressions.append(Expression("BOOLEAN", line[1:4]))

        elif etype == "IF":
            self.expressions.append(Expression("BOOLEAN", line[1:4]))

        elif etype == "PRINT":
            if len(line) == 4:
                self.print_tokens.append(line[2])
            else:
                self.expressions.append(Expression("ARITHMETIC", line[2:5]))

        elif etype == "FOR":
            self.expressions.append(Expression("ASSIGNMENT", line[1:4]))
            self.expressions.append(Expression("ITER", line[4:6]))

    # Prints the Assignment along with its expression
    def print_encoded_statement(self):
        if self.etype == "ASSIGNMENT":
            if len(self.line) == 3:
                print("<assignment_statement> -> <assignment_expression>")
                self.expressions[0].print_encoded_expression()
            else:
                print("<assignment_statement> -> <assignment_expression> <arithmetic_expression>")
                for expression in self.expressions:
                    expression.print_encoded_expression()

        elif self.etype == "WHILE":
            print("<while_statement> -> while <boolean_expression> then <block>")
            self.expressions[0].print_encoded_expression()
            self.blocks[0].print_encoded_block()

        elif self.etype == "IF":
            print("<conditional_statement> -> if <boolean_expression> then <block> else <block>")
            self.expressions[0].print_encoded_expression()
            self.blocks[0].print_encoded_block()

        elif self.etype == "ELSE":
            self.blocks[0].print_encoded_block()

        elif self.etype == "PRINT":
            if len(self.print_tokens) != 0:
                print("<print_statement> -> print ( <" + self.print_tokens[0].type + " id:" + str(self.print_tokens[0].id) + "> )")
                self.print_tokens[0].print_token()
            else:
                print("<print_statement> -> print ( <arithmetic_expression> )")
                self.expressions[0].print_encoded_expression()

        elif self.etype == "FOR":
            print("<for_statement> -> for id = <iter> <block> end")
            for expression in self.expressions:
                expression.print_encoded_expression()


from statistics import mean

res = []
N = int(input())
for i in range(N):
    lit = list(map(int, input().split()))
    li = []
    if len(lit) == 1:
        li = list(map(int, input().split()))
    if not li:
        li = lit[1:]
    total = lit[0]
    count = 0
    avg = mean(li)
    for l in li:
        if l > avg:
            count += 1
    res.append(round((count/total) * 100, 3))

for r in res:
    print('{:.3f}%'.format(r))
    # print(f"{r:.3f}%")


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ==============================================================================
#          \file   augument.py
#        \author   chenghuige
#          \date   2020-10-03 12:31:58.983404
#   \Description
# ==============================================================================

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import sys
import os

import random

import tensorflow as tf
import tensorflow_addons as tfa
# from tf_image.core.colors import rgb_shift, channel_drop

from gezi import logging
import melt as mt
from .config import *

## TODO 通用augment迁移到 melt.image.augment
## 修改需要tpu验证测试单独 特别tf.function可能有问题 尽量其他函数不加 主augment有

# https://github.com/matterport/Mask_RCNN/issues/230
def random_crop_resize(img,
                       mask,
                       crop_shape=(228, 228),
                       resize_shape=None,
                       aug_mask=True):
  height, width = crop_shape
  assert img.shape[0] >= height
  assert img.shape[1] >= width
  assert img.shape[0] == mask.shape[0]
  assert img.shape[1] == mask.shape[1]
  img_shape = (img.shape[0],
               img.shape[1]) if not resize_shape else resize_shape
  mask_shape = mask.shape
  x = tf.random.uniform(shape=[], maxval=img.shape[1] - width, dtype=tf.int32)
  y = tf.random.uniform(shape=[], maxval=img.shape[0] - height, dtype=tf.int32)
  img = img[y:y + height, x:x + width]
  img = tf.image.resize(img, img_shape)
  if aug_mask:
    mask = mask[y:y + height, x:x + width]
    mask = tf.image.resize(mask,
                           img_shape,
                           antialias=False,
                           method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)

  img = tf.reshape(img, [*img_shape, img.shape[-1]])
  mask = tf.reshape(mask, mask_shape)
  return img, mask

# TODO 似乎tpu不能接受这里 @tf.function 连接失败
def apply2(func, image, mask, p=0.5, aug_mask=True):
  val = tf.random.uniform(())
  if val <= p:
    image = func(image)
    if aug_mask:
      mask = func(mask)

  return image, mask

def augment_rot_oneof(image, mask, p=0.75, aug_mask=True):
  def _aug(image, mask, aug_mask=True):
    p = tf.random.uniform(())
    if p <= (1 / 3.):
      image = tf.image.rot90(image, k=1)
      if aug_mask:
        mask = tf.image.rot90(mask, k=1)
    elif p <= (2 / 3.):
      image = tf.image.rot90(image, k=2)
      if aug_mask:
        mask = tf.image.rot90(mask, k=2)
    else:
      image = tf.image.rot90(image, k=3)
      if aug_mask:
        mask = tf.image.rot90(mask, k=3)

    return image, mask

  val = tf.random.uniform(())
  if val < p:
    image, mask = _aug(image, mask, aug_mask=aug_mask)
  return image, mask

# 加强空间aug 增加rot90
def augment_spatial(image, mask, aug_mask=True):
  image, mask = apply2(tf.image.flip_left_right,
                       image,
                       mask,
                       aug_mask=aug_mask)
  image, mask = apply2(tf.image.flip_up_down, image, mask, aug_mask=aug_mask)
  rotate_rate = FLAGS.rotate_rate or 0.5
  image, mask = apply2(tf.image.rot90,
                       image,
                       mask,
                       p=rotate_rate,
                       aug_mask=aug_mask)

  return image, mask

def augment_spatial2(image, mask, p, aug_mask=True):
  image, mask = apply2(tf.image.flip_left_right,
                       image,
                       mask,
                       aug_mask=aug_mask)
  image, mask = apply2(tf.image.flip_up_down, image, mask, aug_mask=aug_mask)
  image, mask = augment_rot_oneof(image, mask, p=p, aug_mask=aug_mask)
  return image, mask

def augment_color_oneof(image):
  def _aug(image):
    n_ch = image.shape[-1]
    if n_ch > 3:
      image, other = tf.split(image, [3, n_ch - 3], axis=-1)
    p = tf.random.uniform(())
    if p <= (1 / 3.):
      image = tf.image.random_saturation(image, 0.7, 1.3)
    elif p <= (2 / 3.):
      image = tf.image.random_contrast(image, 0.8, 1.2)
    else:
      image = tf.image.random_brightness(image, 0.1)
    if n_ch > 3:
      image = tf.concat([image, other], -1)

    return image

  color_rate = FLAGS.color_rate or 0.15
  return mt.Apply(_aug, color_rate)(image)

def augment_color_compose(image):
  def _aug(image):
    n_ch = image.shape[-1]
    if n_ch > 3:
      image, other = tf.split(image, [3, n_ch - 3], axis=-1)
    image = tf.image.random_saturation(image, 0.7, 1.3)
    image = tf.image.random_contrast(image, 0.8, 1.2)
    image = tf.image.random_brightness(image, 0.1)
    if n_ch > 3:
      image = tf.concat([image, other], -1)
    return image

  color_rate = FLAGS.color_rate or 0.15
  return mt.Apply(_aug, color_rate)(image)

def augment_sharpness(image, sharpen_rate=0.1, blur_rate=0.1):
  if not sharpen_rate and not blur_rate:
    return image

  assert sharpen_rate + blur_rate < 1

  n_ch = image.shape[-1]
  if n_ch > 3:
    image, other = tf.split(image, [3, n_ch - 3], axis=-1)

  p = tf.random.uniform(())
  if p <= sharpen_rate:
    rate = tf.random.uniform((), 1, 2)
    image = tfa.image.sharpness(image, rate)
  elif blur_rate and p <= sharpen_rate + blur_rate:
    rate = tf.random.uniform((), 0.1, 1)
    image = tfa.image.sharpness(image, rate)

  if n_ch > 3:
    image = tf.concat([image, other], -1)

  return image

def mosaic_mix(image, mask, p=1.0):
  n_ch = image.shape[-1]
  DIM = mt.get_shape(image, 1)
  AUG_BATCH = mt.get_shape(image, 0)

  index = int(DIM / 2)

  imgs = []
  masks = []

  def _deal(image, j, k, l, m):
    img1 = image[j, 0:index, 0:index, :]
    img2 = image[k, 0:index, index:, :]
    img3 = image[l, index:, 0:index, :]
    img4 = image[m, index:, index:, :]

    one = tf.concat([img1, img2], axis=1)
    two = tf.concat([img3, img4], axis=1)
    img = tf.concat([one, two], axis=0)
    return img

  for j in range(AUG_BATCH):
    # DO CUTMIX WITH PROBABILITY DEFINED ABOVE
    prob = tf.random.uniform(())
    k = tf.random.uniform([], 0, AUG_BATCH, dtype=tf.int32)
    l = tf.random.uniform([], 0, AUG_BATCH, dtype=tf.int32)
    m = tf.random.uniform([], 0, AUG_BATCH, dtype=tf.int32)

    # MAKE CUTMIX IMAGE
    img = _deal(image, j, k, l, m)
    imgs.append(img)
    mask_ = _deal(mask, j, k, l, m)
    masks.append(mask_)

  image2 = tf.stack(imgs, axis=0)
  mask2 = tf.stack(masks, axis=0)
  # RESHAPE HACK SO TPU COMPILER KNOWS SHAPE OF OUTPUT TENSOR (maybe use Python typing instead?)
  image2 = tf.reshape(image2, (AUG_BATCH, DIM, DIM, n_ch))
  mask2 = tf.reshape(mask2, (AUG_BATCH, DIM, DIM, 1))

  return image2, mask2

# https://www.kaggle.com/cdeotte/cutmix-and-mixup-on-gpu-tpu
# TODO 如果蒸馏 需要 也对软标签 pred做cutmix
# TODO 实验增加cutmix + scale缩放
def cutmix(image, mask, pred=None, p=1.0):
  n_ch = image.shape[-1]
  # input image - is a batch of images of size [n,dim,dim,3] not a single image of [dim,dim,3]
  # output - a batch of images with cutmix applied
  DIM = mt.get_shape(image, 1)
  AUG_BATCH = mt.get_shape(image, 0)

  # imgs = []
  # for j in range(AUG_BATCH):
  #   prob = tf.random.uniform(())
  ## TODO FIXME 很奇怪 这里必须用tf.cond 不能用prob < p if else 。。。 否则stack的时候
  ## inaccessibleTensorError: The tensor 'Tensor("cond/strided_slice:0", shape=(256, 256, 3), dtype=float32)' cannot be accessed here: it is defined in another function or code block. Use return values,
  ## explicit Python locals or TensorFlow collections to access it. Defined in: FuncGraph(name=cond_true_7099, id=140047006505040); accessed from: FuncGraph(name=Dataset_map_Dataset.post_decode, id=140047007789200).
  #   # if prob < p:
  #   #   imgs.append(image[j])
  #   # else:
  #   #   imgs.append(image[0])

  #   img = tf.cond(
  #     tf.less(prob, p),
  #     lambda: image[j],
  #     lambda: image[0])

  #   imgs.append(img)

  # image = tf.stack(imgs)

  # return image, mask

  def _deal(image, j, k, xa, xb, ya, yb):
    one = image[j, ya:yb, 0:xa, :]
    two = image[k, ya:yb, xa:xb, :]
    three = image[j, ya:yb, xb:DIM, :]
    middle = tf.concat([one, two, three], axis=1)
    img = tf.concat([image[j, 0:ya, :, :], middle, image[j, yb:DIM, :, :]],
                    axis=0)
    # img = tf.reshape(img, (DIM, DIM, 3))
    return img

  imgs = []
  masks = []
  if pred is not None:
    preds = []

  for j in range(AUG_BATCH):
    # DO CUTMIX WITH PROBABILITY DEFINED ABOVE
    prob = tf.random.uniform(())
    k = tf.random.uniform([], 0, AUG_BATCH, dtype=tf.int32)
    # CHOOSE RANDOM LOCATION
    x = tf.random.uniform([], 0, DIM, dtype=tf.int32)
    y = tf.random.uniform([], 0, DIM, dtype=tf.int32)
    b = tf.random.uniform([], 0, 1)  # this is beta dist with alpha=1.0

    P = tf.cast(prob <= p, tf.int32)
    WIDTH = tf.cast(DIM * tf.math.sqrt(1 - b), tf.int32) * P
    ya = tf.math.maximum(0, y - WIDTH // 2)
    yb = tf.math.minimum(DIM, y + WIDTH // 2)
    xa = tf.math.maximum(0, x - WIDTH // 2)
    xb = tf.math.minimum(DIM, x + WIDTH // 2)
    # MAKE CUTMIX IMAGE
    img = _deal(image, j, k, xa, xb, ya, yb)
    imgs.append(img)
    mask_ = _deal(mask, j, k, xa, xb, ya, yb)
    masks.append(mask_)

    if pred is not None:
      pred_ = _deal(pred, j, k, xa, xb, ya, yb)
      preds.append(pred_)

  image2 = tf.stack(imgs, axis=0)
  mask2 = tf.stack(masks, axis=0)
  # RESHAPE HACK SO TPU COMPILER KNOWS SHAPE OF OUTPUT TENSOR (maybe use Python typing instead?)
  image2 = tf.reshape(image2, (AUG_BATCH, DIM, DIM, n_ch))
  mask2 = tf.reshape(mask2, (AUG_BATCH, DIM, DIM, 1))

  if pred is not None:
    pred2 = tf.stack(preds, axis=0)
    pred2 = tf.reshape(pred2, (AUG_BATCH, DIM, DIM, FLAGS.NUM_CLASSES))
    return image2, mask2, pred2

  return image2, mask2

def scaled_cutmix(image, mask, pred=None, p=1.0):
  n_ch = image.shape[-1]
  # input image - is a batch of images of size [n,dim,dim,3] not a single image of [dim,dim,3]
  # output - a batch of images with cutmix applied
  DIM = mt.get_shape(image, 1)
  AUG_BATCH = mt.get_shape(image, 0)

  def _deal(image,
            j,
            k,
            xa,
            xb,
            ya,
            yb,
            xa2,
            xb2,
            ya2,
            yb2,
            method='bilinear'):
    one = image[j, ya:yb, 0:xa, :]
    two = image[k, ya2:yb2, xa2:xb2, :]
    two = mt.image.resize(two, [yb - ya, xb - xa], method=method)
    three = image[j, ya:yb, xb:DIM, :]
    middle = tf.concat([one, two, three], axis=1)
    img = tf.concat([image[j, 0:ya, :, :], middle, image[j, yb:DIM, :, :]],
                    axis=0)
    # img = tf.reshape(img, (DIM, DIM, 3))
    return img

  imgs = []
  masks = []
  if pred is not None:
    preds = []

  for j in range(AUG_BATCH):
    # DO CUTMIX WITH PROBABILITY DEFINED ABOVE
    prob = tf.random.uniform(())
    k = tf.random.uniform([], 0, AUG_BATCH, dtype=tf.int32)
    # CHOOSE RANDOM LOCATION
    x = tf.random.uniform([], 0, DIM, dtype=tf.int32)
    y = tf.random.uniform([], 0, DIM, dtype=tf.int32)
    b = tf.random.uniform([], 0, 1)  # this is beta dist with alpha=1.0

    P = tf.cast(prob <= p, tf.int32)
    WIDTH = tf.cast(DIM * tf.math.sqrt(1 - b), tf.int32) * P
    WIDTH = tf.math.maximum(WIDTH, 2)
    ya = tf.math.maximum(0, y - WIDTH // 2)
    yb = tf.math.minimum(DIM, y + WIDTH // 2)
    xa = tf.math.maximum(0, x - WIDTH // 2)
    xb = tf.math.minimum(DIM, x + WIDTH // 2)

    lo, hi = FLAGS.cutmix_range[0], FLAGS.cutmix_range[1]
    scale = tf.random.uniform([], lo, hi)
    WIDTH2 = tf.cast(tf.cast(WIDTH, tf.float32) * scale, tf.int32)
    WIDTH2 = tf.math.maximum(WIDTH2, 2)

    masking = tf.cast(
        tf.random.uniform([]) <= FLAGS.cutmix_scale_rate, tf.int32)
    WIDTH2 = WIDTH2 * masking + WIDTH * (1 - masking)

    ya2 = tf.math.maximum(0, y - WIDTH2 // 2)
    yb2 = tf.math.minimum(DIM, y + WIDTH2 // 2)
    xa2 = tf.math.maximum(0, x - WIDTH2 // 2)
    xb2 = tf.math.minimum(DIM, x + WIDTH2 // 2)

    # MAKE CUTMIX IMAGE
    img = _deal(image, j, k, xa, xb, ya, yb, xa2, xb2, ya2, yb2)
    imgs.append(img)
    mask_ = _deal(mask,
                  j,
                  k,
                  xa,
                  xb,
                  ya,
                  yb,
                  xa2,
                  xb2,
                  ya2,
                  yb2,
                  method='nearest')
    masks.append(mask_)

    if pred is not None:
      pred_ = _deal(pred, j, k, xa, xb, ya, yb, xa2, xb2, ya2, yb2)
      preds.append(pred_)

  image2 = tf.stack(imgs, axis=0)
  mask2 = tf.stack(masks, axis=0)
  # RESHAPE HACK SO TPU COMPILER KNOWS SHAPE OF OUTPUT TENSOR (maybe use Python typing instead?)
  image2 = tf.reshape(image2, (AUG_BATCH, DIM, DIM, n_ch))
  mask2 = tf.reshape(mask2, (AUG_BATCH, DIM, DIM, 1))

  if pred is not None:
    pred2 = tf.stack(preds, axis=0)
    pred2 = tf.reshape(pred2, (AUG_BATCH, DIM, DIM, FLAGS.NUM_CLASSES))
    return image2, mask2, pred2

  return image2, mask2

def mixup(image, mask, p=1.0):
  # input image - is a batch of images of size [n,dim,dim,3] not a single image of [dim,dim,3]
  # output - a batch of images with mixup applied
  n_ch = image.shape[-1]
  # input image - is a batch of images of size [n,dim,dim,3] not a single image of [dim,dim,3]
  # output - a batch of images with cutmix applied
  DIM = mt.get_shape(image, 1)
  AUG_BATCH = mt.get_shape(image, 0)

  imgs = []
  masks = []
  probs = []
  for j in range(AUG_BATCH):
    # DO MIXUP WITH PROBABILITY DEFINED ABOVE
    P = tf.cast(tf.random.uniform([], 0, 1) <= p, tf.float32)
    # CHOOSE RANDOM
    k = tf.cast(tf.random.uniform([], 0, AUG_BATCH), tf.int32)
    a = tf.random.uniform([], 0, 1) * P  # this is beta dist with alpha=1.0
    # MAKE MIXUP IMAGE
    img1 = image[j]
    img2 = image[k]
    a = tf.cast(a, img1.dtype)
    imgs.append((1 - a) * img1 + a * img2)
    # MAKE CUTMIX LABEL
    probs.append(a)
    mask_j = mask[j]
    mask_k = mask[k]
    mask_ = tf.concat([mask_j, mask_k], -1)
    masks.append(mask_)

  # RESHAPE HACK SO TPU COMPILER KNOWS SHAPE OF OUTPUT TENSOR (maybe use Python typing instead?)
  image2 = tf.reshape(tf.stack(imgs), (AUG_BATCH, DIM, DIM, n_ch))
  mask2 = tf.reshape(tf.stack(masks), (AUG_BATCH, DIM, DIM, 2))
  prob2 = tf.reshape(tf.stack(probs), (AUG_BATCH, 1))
  return image2, mask2, prob2

def mixup_transform(image, mask):
  n_ch = image.shape[-1]
  # input image - is a batch of images of size [n,dim,dim,3] not a single image of [dim,dim,3]
  # output - a batch of images with cutmix applied
  DIM = mt.get_shape(image, 1)
  AUG_BATCH = mt.get_shape(image, 0)

  # THIS FUNCTION APPLIES BOTH CUTMIX AND MIXUP
  SWITCH = FLAGS.mixup_switch
  CUTMIX_PROB = FLAGS.cutmix_rate
  cutmix_fn = cutmix if not FLAGS.cutmix_range else scaled_cutmix
  MIXUP_PROB = FLAGS.mixup_rate
  # FOR SWITCH PERCENT OF TIME WE DO CUTMIX AND (1-SWITCH) WE DO MIXUP
  image2, mask2 = cutmix_fn(image, mask, p=CUTMIX_PROB)
  mask2 = tf.concat([mask2, mask2], -1)
  image3, mask3, prob3 = mixup(image, mask, p=MIXUP_PROB)
  prob2 = tf.ones_like(prob3) * 0.5
  imgs = []
  masks = []
  probs = []
  for j in range(AUG_BATCH):
    P = tf.cast(tf.random.uniform([], 0, 1) <= SWITCH, image2[0].dtype)
    imgs.append(P * image2[j] + (1 - P) * image3[j])
    masks.append(P * mask2[j] + (1 - P) * mask3[j])
    probs.append(P * prob2[j] + (1 - P) * prob3[j])
  # RESHAPE HACK SO TPU COMPILER KNOWS SHAPE OF OUTPUT TENSOR (maybe use Python typing instead?)
  image4 = tf.reshape(tf.stack(imgs), (AUG_BATCH, DIM, DIM, n_ch))
  mask4 = tf.reshape(tf.stack(masks), (AUG_BATCH, DIM, DIM, 2))
  prob4 = tf.reshape(tf.stack(probs), (AUG_BATCH, 1))
  return image4, mask4, prob4

def mosaic_transform(image, mask):
  n_ch = image.shape[-1]
  # input image - is a batch of images of size [n,dim,dim,3] not a single image of [dim,dim,3]
  # output - a batch of images with cutmix applied
  DIM = mt.get_shape(image, 1)
  AUG_BATCH = mt.get_shape(image, 0)

  # THIS FUNCTION APPLIES BOTH CUTMIX AND MIXUP
  SWITCH = FLAGS.mixup_switch
  CUTMIX_PROB = FLAGS.cutmix_rate
  cutmix_fn = cutmix if not FLAGS.cutmix_range else scaled_cutmix
  MIXUP_PROB = FLAGS.mosaic_rate
  # FOR SWITCH PERCENT OF TIME WE DO CUTMIX AND (1-SWITCH) WE DO MIXUP
  image2, mask2 = cutmix_fn(image, mask, p=CUTMIX_PROB)
  image3, mask3 = mosaic_mix(image, mask, p=MIXUP_PROB)
  imgs = []
  masks = []
  for j in range(AUG_BATCH):
    P = tf.cast(tf.random.uniform([], 0, 1) <= SWITCH, image2[0].dtype)
    imgs.append(P * image2[j] + (1 - P) * image3[j])
    masks.append(P * mask2[j] + (1 - P) * mask3[j])
  # RESHAPE HACK SO TPU COMPILER KNOWS SHAPE OF OUTPUT TENSOR (maybe use Python typing instead?)
  image4 = tf.reshape(tf.stack(imgs), (AUG_BATCH, DIM, DIM, n_ch))
  mask4 = tf.reshape(tf.stack(masks), (AUG_BATCH, DIM, DIM, 1))
  return image4, mask4

## TODO gaussian blur hue 等等变换
## 暂时这个也去掉tf.function colab tpu之前也能跑 terminal也可以但是 local环境jupter这里function会报错 诡异 TODO
# @tf.function
def augment(image, mask=None, aug_mask=True):
  '''
  image: [B, H, W]
  mask:[B, H, 1]
  '''

  if mask is None:
    aug_mask = False
    mask = tf.zeros_like(image[:, :, 0])

  # < 0 means tested not good and depreciated
  if FLAGS.augment_level == 0:
    image, mask = apply2(tf.image.flip_left_right,
                         image,
                         mask,
                         p=FLAGS.hflip_rate,
                         aug_mask=aug_mask)
    image, mask = apply2(tf.image.flip_up_down,
                         image,
                         mask,
                         p=FLAGS.hflip_rate,
                         aug_mask=aug_mask)
  elif FLAGS.augment_level == 1:
    image, mask = augment_spatial(image, mask, aug_mask)
  elif FLAGS.augment_level == 2:
    image, mask = augment_spatial(image, mask, aug_mask)
    image = augment_color_oneof(image)
  elif FLAGS.augment_level == 3:  # diff with 4 will not do sharpness
    image, mask = augment_spatial(image, mask, aug_mask)
    image = augment_color_compose(image)
  elif FLAGS.augment_level == 4:  # diff with 3, by default will do sharpness
    image, mask = augment_spatial(image, mask, aug_mask)
    image = augment_color_compose(image)
    FLAGS.sharpen_rate = FLAGS.sharpen_rate or 0.1
    FLAGS.blur_rate = FLAGS.blur_rate or 0.1
    image = augment_sharpness(image, FLAGS.sharpen_rate, FLAGS.blur_rate)
  elif FLAGS.augment_level == 5:
    image, mask = augment_spatial2(image, mask, 0.75, aug_mask)
    image = augment_color_compose(image)
    image = augment_sharpness(image, FLAGS.sharpen_rate, FLAGS.blur_rate)
  elif FLAGS.augment_level == 6:
    image, mask = augment_spatial2(image, mask, 0.75, aug_mask)
    image = augment_color_compose(image)
  elif FLAGS.augment_level == 7:  # 当前最佳
    image, mask = augment_spatial2(image, mask, 0.5, aug_mask)
    image = augment_color_compose(image)
  elif FLAGS.augment_level == 8:
    image, mask = augment_spatial2(image, mask, 0.5, aug_mask)
    image = augment_color_compose(image)
    FLAGS.sharpen_rate = FLAGS.sharpen_rate or 0.1
    FLAGS.blur_rate = FLAGS.blur_rate or 0.1
    image = augment_sharpness(image, FLAGS.sharpen_rate, FLAGS.blur_rate)
  elif FLAGS.augment_level == -4:
    FLAGS.rotate_rate = 0.5
    image, mask = augment_spatial(image, mask, aug_mask)
    FLAGS.color_rate = 1.
    image = augment_color_compose(image)
  else:
    raise ValueError(FLAGS.augment_level)

  return image, mask

# 注意不要@tf.function 否则不能save graph.. TODO
# @tf.function
def pred_augment(image, tta_fns, intersect=False, reverse=False):
  # fns = [getattr(tf.image,  tta_fn) for tta_fn in tta_fns]
  def _getaug(tta_fn):
    # TODO sharp not work..
    if tta_fn.startswith('sharpness'):
      rate = float(tta_fn.split('_')[-1])
      return lambda x: tfa.image.sharpness(x, rate)

    if tta_fn.startswith('rot90'):
      k = 1
      if '_' in tta_fn:
        k = int(tta_fn.split('_')[-1])
      return lambda x: tf.image.rot90(x, k)

    if tta_fn.startswith('size'):
      size_ = int(tta_fn[len('size'):])
      return lambda x: tf.image.resize(x, [size_, size_])

    return getattr(tf.image, tta_fn)

  fns = []
  for tta_fn in tta_fns:
    if not '-' in tta_fn:
      fns.append(_getaug(tta_fn))
    else:

      def fn(x):
        tta_fns_ = tta_fn.split('-')
        for tta_fn_ in tta_fns_:
          x = _getaug(tta_fn_)(x)
        return x

      fns.append(fn)

  if intersect:
    fns_ = []
    for fn in fns:
      fns__ = []
      for i in range(len(fns_)):
        fns__.append(fns_[i])
        fns__.append([*fns_[i], fn])
      fns__.append([fn])
      fns_ = fns__

    fns = []
    for i in range(len(fns_)):

      def fn(x):
        for j in range(len(fns_[i])):
          x = fns_[i][j](x)
        return x

      fns.append(fn)

  if reverse:
    assert len(tta_fns) == len(fns)
    for i in range(len(fns)):
      if tta_fns[i].startswith('sharpness'):
        fns[i] = lambda x: x
      elif tta_fns[i].startswith('rot90'):
        k = 1
        if '_' in tta_fns[i]:
          k = int(tta_fns[i].split('_')[-1])
        fns[i] = lambda x: tf.image.rot90(x, k=4 - k)
      elif tta_fn.startswith('size'):
        fns[i] = lambda x: tf.image.resize(x, FLAGS.ori_image_size)

  return mt.image.pred_augment(image, fns)


import cv2
from flask import Flask, request, render_template, Response
from flask_restful import Resource, Api
import pymysql
import json
import testDetect

app = Flask(__name__)
api = Api(app)

#  Connect database
db = pymysql.connect("localhost", "root", "87654321", "bank")
cursor = db.cursor()

class customer(Resource):
    def get(self, id):
        cursor.execute("Select * from customers where id = %d" %int(id))
        data = cursor.fetchone()
        print(data)
        return json.dumps(data, ensure_ascii=False)
# @app.route('/')
# def hello_world():
#     return 'Hello, World!'
api.add_resource(customer, '/<int:id>')

@app.route('/')
def index():
    return Response(open('src/static/FormAddInfor.html').read(), mimetype="text/html")

@app.route('/getvd', methods=['POST'])
def get():
    file = request.files['file']
    print(file.name)
    print(file)
    file.save('./vd.webm')
    testDetect.main('./vd.webm')
    return "Getted"

if __name__ == '__main__':
    app.run()#host="192.168.61.15", port=5001)


# uncompyle6 version 3.7.4
# Python bytecode 2.5 (62131)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: build/bdist.macosx-10.6-i386/egg/typhoonae/capability_stub.py
# Compiled at: 2010-12-12 04:36:57
"""TyphoonAE's stub of the capability service API."""
import google.appengine.api.apiproxy_stub, google.appengine.api.capabilities
IsEnabledRequest = google.appengine.api.capabilities.IsEnabledRequest
IsEnabledResponse = google.appengine.api.capabilities.IsEnabledResponse
CapabilityConfig = google.appengine.api.capabilities.CapabilityConfig

class CapabilityServiceStub(google.appengine.api.apiproxy_stub.APIProxyStub):
    """Capability service stub."""

    def __init__(self, service_name='capability_service'):
        """Constructor.

        Args:
            service_name: Service name expected for all calls.
        """
        super(CapabilityServiceStub, self).__init__(service_name)

    def _Dynamic_IsEnabled(self, request, response):
        """Implementation of CapabilityService::IsEnabled().

        Args:
            request: An IsEnabledRequest.
            response: An IsEnabledResponse.
        """
        response.set_summary_status(IsEnabledResponse.ENABLED)
        default_config = response.add_config()
        default_config.set_package('')
        default_config.set_capability('')
        default_config.set_status(CapabilityConfig.ENABLED)


#!/usr/bin/env python3

import argparse
import hashlib
import os
import random
import sys

__VERSION__ = '0.1.1'
CHARACTERS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

def get_parser():
    parser = argparse.ArgumentParser(
        description='a cli tool used to change the MD5 of files',
    )

    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument(
        '-f', '--file',
        metavar='FILE',
        type=str,
        help='the file you want to change md5',
    )
    group.add_argument(
        '-d', '--dir',
        metavar='DIR',
        type=str,
        help='change the md5 value for each of the files in a given directory',
    )
    parser.add_argument(
        '-s', '--show', action='store_true',
        help='show the md5 value'
    )
    parser.add_argument('-v', '--version',
                        action='version', version=__VERSION__)
    return parser

def get_md5(file):
    if os.path.exists(file):
        with open(file, 'rb') as f:
            md5 = hashlib.md5()
            while True:
                buffer = f.read(4096)
                if not buffer:
                    break
                md5.update(buffer)
            return md5.hexdigest()

def handle_single_file(file, show_md5=None):
    random_str = ''.join(random.choice(CHARACTERS))

    old = ''
    if show_md5:
        old = get_md5(file)

    with open(file, 'ab') as f:
        f.write(random_str.encode('utf-8'))

    new = get_md5(file)

    if show_md5:
        if old != new:
            print('[Success]The md5 value {0} has been changed from {1} to {2}'.format(file, old, new))
        else:
            print("[Failed]The md5 of file {0} before and after is the same,please check".format(file))
    else:
        if old != new:
            print("[Success]The md5 of file {0} has been changed.".format(file))
        else:
            print("[Failed]The md5 of file {0} before and after is the same,please check".format(file))

def change_the_md5(file_path=None, dir_name=None, show_md5=False):
    if file_path and os.path.exists(file_path):
        handle_single_file(file_path, show_md5)

    if dir_name and os.path.exists(dir_name):
        print("Found directory:", dir_name)
        for root, dirs, files in os.walk(dir_name):
            for f in files:
                file_path = os.path.join(root, f)
                handle_single_file(file_path, show_md5)

def cli():
    args = vars(get_parser().parse_args())
    file_path = args.get('file', None)
    dir_name = args.get('dir', None)
    show_md5 = args.get('show', None)

    change_the_md5(file_path=file_path, dir_name=dir_name, show_md5=show_md5)

if __name__ == '__main__':
    cli()


# ---------------------------------------------------------#
#   astroNN.NN.train_tools: Tools to train models
# ---------------------------------------------------------#

import numpy as np
import random

def load_batch(num_train, batch_size, indx, mu_std, spectra, y):
    # Generate list of random indices (within the relevant partition of the main data file, e.g. the
    # training set) to be used to index into data_file
    indices = random.sample(range(indx, indx + num_train), batch_size)
    indices = np.sort(indices)

    mean_labels = mu_std[0]
    std_labels = mu_std[1]

    # load data
    spectra = spectra[indices, :]
    y = y[:][indices]

    # Normalize labels
    normed_y = (y - mean_labels) / std_labels

    # Reshape X data for compatibility with CNN
    spectra = spectra.reshape(len(spectra), spectra.shape[1], 1)

    return spectra, normed_y

def generate_train_batch(num_objects, batch_size, indx, mu_std, spectra, y):
    while True:
        x_batch, y_batch = load_batch(num_objects, batch_size, indx, mu_std, spectra, y)
        yield (x_batch, y_batch)

def generate_cv_batch(num_objects, batch_size, indx, mu_std, spectra, y):
    while True:
        x_batch, y_batch = load_batch(num_objects, batch_size, indx, mu_std, spectra, y)
        yield (x_batch, y_batch)


from .exc2 import Bank

class TestBank:

    def test_create_bank(self):
        bank = Bank()
        assert bank.amount == 0
        assert isinstance(bank, Bank)

    def test_deposit(self):
        #given
        bank = Bank()

        #when
        bank.deposit(100)

        #then
        assert bank.amount == 100

    def test_deposit_twice(self):
        #given
        bank = Bank()

        #when
        bank.deposit(100)
        bank.deposit(100)

        #then
        assert bank.amount == 200

    def test_withdraw(self):
        #given
        bank = Bank()
        bank.deposit(100)
        #when
        withdraw = bank.withdraw(100)

        #then
        assert withdraw == 100
        assert bank.amount == 0


"""empty message

Revision ID: cee99f2e3104
Revises: b9b9c1d4e4d8
Create Date: 2019-04-08 17:07:41.212425

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'cee99f2e3104'
down_revision = 'b9b9c1d4e4d8'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('symptom', schema=None) as batch_op:
        batch_op.add_column(sa.Column('user_id', sa.Integer(), nullable=True))
        batch_op.drop_constraint('fk_symptom_admin_id_user', type_='foreignkey')
        batch_op.create_foreign_key(batch_op.f('fk_symptom_user_id_user'), 'user', ['user_id'], ['id'])
        batch_op.drop_column('admin_id')

    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('symptom', schema=None) as batch_op:
        batch_op.add_column(sa.Column('admin_id', sa.INTEGER(), nullable=True))
        batch_op.drop_constraint(batch_op.f('fk_symptom_user_id_user'), type_='foreignkey')
        batch_op.create_foreign_key('fk_symptom_admin_id_user', 'user', ['admin_id'], ['id'])
        batch_op.drop_column('user_id')

    # ### end Alembic commands ###


# List Operations

# Q-8: Quiz (Len Quiz)

# What is the value of len(p) after running the following code:
p = [1, 2]
p.append(3)
p = p + [4, 5]
len(p)
# Ans: 5
print(len(p))

# Q-9: Quiz

# What is the value of len(p) after running:
p = [1, 2]
q = [3, 4]
p.append(q)
# len(p) → ?
print(len(p))

Introduction_To_Python/Unit 3/Quizzes/5. List Operations.py


from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.shortcuts import render
from .models import Character, Fighter, Mage, Cleric, Thief

def index(request):
    return HttpResponse("Character Creator App!")

def view_all_characters(request):
    characters = Character.objects.all()
    context = {'characters': characters}
    return render(request, 'characters/index.html', context)

def view_character(request, character_id):
    character = Character.objects.get(pk=character_id)
    try:
        charClass = character.fighter.__class__.__name__
    except:
        try:
            charClass = character.mage.__class__.__name__
        except:
            try:
                charClass = character.thief.__class__.__name__
            except:
                try:
                    charClass = character.cleric.__class__.__name__
                except:
                    pass
    context = {'character': character, 'charClass': charClass}
    return render(request, 'character/index.html', context)


#  Scraper for Kansas Appeals Court
# CourtID: kanctapp
# Court Short Name: kanctapp
# Author: Andrei Chelaru
# Reviewer: mlr
# Date created: 25 July 2014

from juriscraper.opinions.united_states.state import kan

class Site(kan.Site):
    def __init__(self, *args, **kwargs):
        super(Site, self).__init__(*args, **kwargs)
        self.court_id = self.__module__
        self.court_index = 2


import torch
from torch import nn, optim
from models.classifierVGG import ClassifierVGG
# from models.classifier import Classifier
from models.dataloader import DataLoader
from env import *

CAT = SIGN_CAT
# CLF_CKPT_PATH = NOTHING_CLF_CKPT_PATH
CLF_CKPT_PATH = SIGN_CLF_CKPT_PATH

def main():
    train_loader = DataLoader(train=True, noise=True, flip=True, batch_size=BATCH_SIZE)
    valid_loader = DataLoader(train=False, noise=False, flip=False, batch_size=BATCH_SIZE)

    model = ClassifierVGG(CAT).cuda()
    model.load(CLF_CKPT_PATH)

    criterion = nn.NLLLoss()
    optimizer = optim.Adam(model.parameters(), lr=ETA, weight_decay=1e-3)

    top_valid_acc = 0.0

    for e in range(EPOCHS):
        train_loss = 0.0
        train_acc = 0.0
        valid_loss = 0.0
        valid_acc = 0.0

        for x, y in train_loader.next_batch():
            x = torch.FloatTensor(x).cuda()
            y = torch.LongTensor(y).cuda()

            logps = model(x)
            loss = criterion(logps, y)

            with torch.no_grad():
                ps = torch.exp(logps)
                ps_k, top_k = ps.topk(1, dim=1)
                equal = top_k == y.view(*top_k.size())

                train_acc += torch.mean(equal.type(torch.FloatTensor))
                train_loss += loss.item()

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

        with torch.no_grad():
            model.eval()

            for x, y in valid_loader.next_batch():
                x = torch.FloatTensor(x).cuda()
                y = torch.LongTensor(y).cuda()

                logps = model(x)
                loss = criterion(logps, y)

                ps = torch.exp(logps)
                ps_k, top_k = ps.topk(1, dim=1)
                equal = top_k == y.view(*top_k.size())

                valid_acc += torch.mean(equal.type(torch.FloatTensor))
                valid_loss += loss.item()

            train_loss /= len(train_loader)
            train_acc /= len(train_loader)
            valid_loss /= len(valid_loader)
            valid_acc /= len(valid_loader)

            print(f"Epoch {e+1}/{EPOCHS}")
            print(f"Train loss: {train_loss:.8f}")
            print(f"Train acc: {train_acc:.4f}")
            print(f"Valid loss: {valid_loss:.8f}")
            print(f"Valid acc: {valid_acc:.4f}")

            if top_valid_acc <= valid_acc:
                top_valid_acc = valid_acc
                model.save(CLF_CKPT_PATH, top_valid_acc)

            model.train()

if __name__ == "__main__":
    main()


# coding: utf-8

import json
import codecs
import re

def findTitleFromJson(title):
    f=codecs.open("jawiki-country.json","r","utf-8")
    for line in f:
        article=json.loads(line)
        if article["title"]==title:
            return article["text"]

    return ""

lines = findTitleFromJson("イギリス").split("\n")

for line in lines :
    if "Category" in line :
        category_line = re.search("^\[\[Category:(.+?)(|\|.*)\]\]$",line)
        if category_line != None :
            print (category_line.group(1))


import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn import datasets
import matplotlib.pylab as plt

boston = datasets.load_boston()

# print(boston.feature_names)

X_train = boston.data[0:30]

# print(X_train)

y_train = boston.target[0:30]

print( y_train)

y_train_binary = []

for price in y_train:
    y_train_binary.append(price >= 20)

# plt.plot(X_train, y_train_binary)
# plt.show()

X_test = boston.data[28]
y_test = boston.target[28]

model = LogisticRegression(C=1e5, solver='lbfgs', multi_class='multinomial')

model.fit(X_train, y_train_binary)

print(model.coef_)
print(model.predict([X_test]))
print(model.predict_proba([X_test]))

print(y_test)


#Minseo_Kwak
#CS 1301-C02
#mkwak7@gatech.edu
# "I worked on the homework assignment alone, using only this semester's course materials."

from Myro import *

init("COM10")

def SeeingRed():
    p = takePicture()
    for i in getPixels(p):
        setRed(i,245)

    #show(p)
    savePicture(p,"Seeing Red.jpg")

#SeeingRed()
def Fade():
    aList = []

    for x in range(0,6):
        p = takePicture()
        for i in getPixels(p):
            r=getRed(i)
            g=getGreen(i)
            b=getBlue(i)
            setRed(i,(r-(r*0.2*x)))
            setGreen(i,(g-(g*0.2*x)))
            setBlue(i,(b-(b*0.2*x)))
        savePicture(p,"pic"+str(x)+".jpg")
        turnLeft(1,1)
    p0= makePicture("pic0.jpg")
    p1= makePicture("pic1.jpg")
    p2= makePicture("pic2.jpg")
    p3= makePicture("pic3.jpg")
    p4= makePicture("pic4.jpg")
    p5= makePicture("pic5.jpg")
    aList= [p0,p1,p2,p3,p4,p5]
    savePicture(aList,"Fade.gif")
Fade()

def Overlay():
    p = takePicture()

    for y in range(150,450):
        for x in range(100,150):
            pix = getPixel(p,x,y)
            setRed(pix,0)
            setGreen(pix,255)
            setBlue(pix,0)
    for y in range(120,150):
        for x in range(150,650):
            pix = getPixel(p,x,y)
            setRed(pix,0)
            setGreen(pix,255)
            setBlue(pix,0)
    #show(p)
    savePicture(p,"Overlay.jpg")

#Overlay()

def MultipleExposure():
    p1= takePicture()
    p2= takePicture()
    p3= takePicture()
    p4= takePicture()
    w=getWidth(p1)
    h=getHeight(p1)
    for x in range(0,w):
        for y in range(0,h):
            pix1 = getPixel(p1,x,y)
            pix2 = getPixel(p2,x,y)
            pix3 = getPixel(p3,x,y)
            pix4 = getPixel(p4,x,y)
            setRed(pix1, int((getRed(pix1)+getRed(pix2)+getRed(pix3)+getRed(pix4))/4))
            setGreen(pix1, int((getGreen(pix1)+getGreen(pix2)+getGreen(pix3)+getGreen(pix4))/4))
            setBlue(pix1, int((getBlue(pix1)+getBlue(pix2)+getBlue(pix3)+getBlue(pix4))/4))
    #show(p1)
    savePicture(p1,"Multiple Exposure.jpg")

#MultipleExposure()

def CrossFade():
    p= takePicture()
    p0= takePicture()
    p1= takePicture()
    turnLeft(1,1)
    p2= takePicture()
    p3= takePicture()
    p4= takePicture()
    aList = []
    aList.append(p)
    aList.append(p0)
    aList.append(p1)
    w=getWidth(p1)
    h=getHeight(p1)
    for x in range(0,w):
        for y in range(0,h):
            pix1 = getPixel(p1,x,y)
            pix2 = getPixel(p2,x,y)

            setRed(pix1, int((getRed(pix1)+getRed(pix2))/2))
            setGreen(pix1, int((getGreen(pix1)+getGreen(pix2))/2))
            setBlue(pix1, int((getBlue(pix1)+getBlue(pix2))/2))
    aList.append(p1)
    aList.append(p2)
    aList.append(p3)
    aList.append(p4)
    savePicture(aList,"CrossFade.gif",10,0)

#CrossFade()

def SplitScreen():
    p1 = takePicture()
    turnLeft(1,1)
    p2 = takePicture()
    w=int((getWidth(p1))/2)
    h=int((getHeight(p1))/2)
    for x in range(0,(w*2)):
        for y in range(0,h):
            pix1 = getPixel(p1,x,y)
            pix2 = getPixel(p2,x,y)
            setRed(pix1,getRed(pix2))
            setGreen(pix1,getGreen(pix2))
            setBlue(pix1,getBlue(pix2))
    #show(p1)
    savePicture(p1,"Split Screen.jpg")

#SplitScreen()

def ScreenShake():
    p=takePicture()
    p2=copyPicture(p)
    w=getWidth(p)
    h=getHeight(p)
    aList=[]
    aList.append(p)

    for y in range(0,h):
        for x in range(0,w-20):
            pix= getPixel(p,x,y)
            pix1= getPixel(p2,x+20,y)
            setRed(pix1,getRed(pix))
            setGreen(pix1,getGreen(pix))
            setBlue(pix1,getBlue(pix))

    for y in range(0,h):
        for x in range(0,20):
            pix1= getPixel(p2,x,y)
            setRed(pix1,0)
            setGreen(pix1,0)
            setBlue(pix1,0)

    aList.append(p2)
    aList.append(p2)
    aList.append(p2)

    for y in range(0,h-20):
        for x in range(0,w):
            pix= getPixel(p,x,y)
            pix1= getPixel(p2,x,y+20)
            setRed(pix1,getRed(pix))
            setGreen(pix1,getGreen(pix))
            setBlue(pix1,getBlue(pix))

    for y in range(0,20):
        for x in range(0,w):
            pix1= getPixel(p2,x,y)
            setRed(pix1,0)
            setGreen(pix1,0)
            setBlue(pix1,0)

    aList.append(p2)
    aList.append(p2)
    aList.append(p2)

    for y in range(0,h):
        for x in range(20,w):
            pix= getPixel(p,x,y)
            pix1= getPixel(p2,x-20,y)
            setRed(pix1,getRed(pix))
            setGreen(pix1,getGreen(pix))
            setBlue(pix1,getBlue(pix))

    for y in range(0,h):
        for x in range(w-20,w):
            pix1= getPixel(p2,x,y)
            setRed(pix1,0)
            setGreen(pix1,0)
            setBlue(pix1,0)

    aList.append(p2)
    aList.append(p2)
    aList.append(p2)

    for y in range(20,h):
        for x in range(0,w):
            pix= getPixel(p,x,y)
            pix1= getPixel(p2,x,y-20)
            setRed(pix1,getRed(pix))
            setGreen(pix1,getGreen(pix))
            setBlue(pix1,getBlue(pix))

    for y in range(h-20,h):
        for x in range(0,w):
            pix1= getPixel(p2,x,y)
            setRed(pix1,0)
            setGreen(pix1,0)
            setBlue(pix1,0)

    aList.append(p2)
    aList.append(p2)
    aList.append(p2)
    savePicture(aList,"ScreenShake.gif",15,0)

#ScreenShake()


import os
import cv2
import numpy

#i dati del cornell sono immagini JPEG e file con array numpy
#le immagini jpeg sono 200*200
#ogni rettangolo ha top-left width height orientation
#sono contenuti in due cartelle diverse

def generator_cornell(folder_path_data,folder_path_labels,batch_size):
    current_size = 0

    #leggo numero immmagini
    CORNELL_data_names = [name for name in os.listdir(folder_path_data) if os.path.isfile(folder_path_data+"/"+name)]
    CORNELL_labels_names = [name for name in os.listdir(folder_path_labels) if os.path.isfile(folder_path_labels+"/"+name)]
    assert len(CORNELL_data_names) == len(CORNELL_labels_names)
    total_size = len(CORNELL_data_names)

    row = 200
    col = 200

    yield [total_size,batch_size]
    while True:
        if current_size == total_size:
            current_size = 0

        # massimo numero di elementi che posso leggere ancora prima di riniziare
        read_size = min(batch_size, total_size - current_size)

        res_data = numpy.zeros((read_size, 200*200))
        res_label = numpy.zeros((read_size, 5))

        for i in xrange(0, read_size):
            res_data[i, :] = cv2.imread(folder_path_data+"/"+CORNELL_data_names[current_size+i],cv2.IMREAD_GRAYSCALE)
            # carico tutti i rettangolo e ne estraggo uno
            rects = numpy.load(folder_path_labels+"/"+CORNELL_labels_names[current_size+i],'r')
            res_label[i, :] = numpy.random.permutation(rects)[0]

        current_size += read_size
        yield [res_data,res_label,current_size == total_size]


"""
Jython functions to be run in the imsim_deep_pipeline.xml scripts.
"""
from java.util import HashMap

def submit_visits():
    """
    Loop over obsHistIds in the VISIT_LIST_FILE and submit the
    associated visit_task.
    """
    job_vars = HashMap()
    visit_file = open(VISIT_LIST_FILE)
    vmin = int(VISIT_NUM_MIN)
    vmax = int(VISIT_NUM_MAX)
    for line in visit_file.readlines()[vmin:vmax]:
        obsHistID = int(line.strip())
        job_vars.put('OBSHISTID', obsHistID)
        pipeline.createSubstream("visit_task", obsHistID, job_vars)
    visit_file.close()

def submit_imsim_jobs():
    """
    Loop over the sensors and submit the imsim_tasks.
    """
    job_vars = HashMap()
    try:
        # If make_instcat sets the number of sensor-visits to submit,
        # use that value; otherwise, use the global default.
        make_instcat = pipeline.getProcessInstance("make_instcat")
        num_sensors = int(make_instcat.getVariable("NUM_SENSOR_VISITS"))
    except:
        num_sensors = int(NUM_SENSORS)
    for sensor_number in range(num_sensors):
        job_vars.put('SENSOR_NUMBER', sensor_number)
        pipeline.createSubstream('imsim_task', sensor_number, job_vars)

def register_imsim_files():
    """
    Register the imsim.py data products with the Data Catalog.
    """
    pass


'''
Created on 9 Mar 2017

@author: hp
'''
import urllib.request

def readURLtoList(URL):
        data = urllib.request.urlopen(URL)
        for line in data:
# earth.wall(x, y)
            print(line)

# with open("walls.txt") as f:
#     lines = f.readlines()
#     lines = [line.rstrip('\n') for line in open('walls,txt')]

from robotparts import *
from datastructures.world import *

WorldFile=open('C:\\Users\\hp\\DSA\\DSA1\\walls.txt', 'r')
x=0
for i in WorldFile:
    if x==0:
        print(i[:-1])
    print(i[2:-1],"cat")
    x+=1


from requests_oauthlib import OAuth2Session

class SpreadsheetAPIError(Exception):
    pass

class SpreadsheetAPI:
    def __init__(self, api_key: str):
        self.api_key = api_key
        self.session = OAuth2Session()

    def terminate(self):
        self.session.close()

    def request(self, url):
        with self.session.get(url) as resp:
            if resp.status_code != 200:
                raise SpreadsheetAPIError(resp)
            return resp.json()

    def spreadsheet_info(self, spreadsheet_id: str):
        url = (
            f'https://sheets.googleapis.com/v4/'
            f'spreadsheets/{spreadsheet_id}/?key={self.api_key}')
        body = self.request(url)
        return body

    def spreadsheet_values(self, spreadsheet_id: str, range: str):
        url = (
            f'https://sheets.googleapis.com/v4/'
            f'spreadsheets/{spreadsheet_id}/values/{range}?key={self.api_key}')
        body = self.request(url)
        return body['values']


import os
import sys
import select
import pygame

WIDTH = 640
HEIGHT = 480

pygame.init()

fpsClock = pygame.time.Clock()

window = pygame.display.set_mode((WIDTH, HEIGHT))

black = pygame.Color(0, 0, 0)
gray = pygame.Color(128, 128, 128)
white = pygame.Color(255, 255, 255)
green = pygame.Color(0, 255, 0)
red = pygame.Color(255, 0, 0)
blue = pygame.Color(0, 0, 255)
pink = pygame.Color(255, 0, 255)
darkGreen = pygame.Color(0, 128, 0)
darkRed = pygame.Color(128, 0, 0)
darkBlue = pygame.Color(0, 0, 128)
darkPink = pygame.Color(128, 0, 128)

intervalColors = [
    blue,   # octave
    red,    # 2nd m
    pink,   # 2nd M
    green,  # 3rd m
    green,  # 3rd M
    blue,   # 4th P
    red,    # 4th A/5th D
    blue,   # 5th P
    green,  # 6th m
    green,  # 6th M
    pink,   # 7th m
    red,    # 7th M
]

inactiveColors = [
    darkBlue,   # octave
    darkRed,    # 2nd m
    darkPink,   # 2nd M
    darkGreen,  # 3rd m
    darkGreen,  # 3rd M
    darkBlue,   # 4th P
    darkRed,    # 4th A/5th D
    darkBlue,   # 5th P
    darkGreen,  # 6th m
    darkGreen,  # 6th M
    darkPink,   # 7th m
    darkRed,    # 7th M
]

fd = os.open('/dev/midi1', os.O_RDONLY)
poll = select.poll()

poll.register(fd, select.POLLIN)

step = 0
notes = []

def add_intervals(new_note):
    for n in notes:
        if n.active:
            n.intervals.append(new_note)

class Note(object):
    def __init__(self, step, pitch):
        self.pitch = pitch
        self.step = step
        self.active = True
        self.intervals = []

    def x(self):
        return WIDTH - (step - self.step)

    def y(self):
        return HEIGHT - 4 * self.pitch

while True:
    step = step + 1
    data = poll.poll(0)
    while data:
        code = ord(os.read(fd, 1))
        if code in (144, 128):
            pitch, force = [ord(x) for x in os.read(fd, 2)]
            if code == 144:
                new_note = Note(step, pitch)
                notes.append(new_note)
                add_intervals(new_note)
            else:
                # !!Use a dict for pitches
                for n in notes:
                    if n.pitch == pitch:
                        n.active = False
        data = poll.poll(0)
    notes = [x for x in notes if x.step > step - WIDTH]
    window.fill(black)
    for n in notes:
        color = white if n.active else gray
        pygame.draw.circle(window, color, (n.x(), n.y()), 2)
        for other in n.intervals:
            if n.active and other.active:
                colorArray = intervalColors
            else:
                colorArray = inactiveColors
            color = colorArray[(other.pitch - n.pitch) % 12]
            pygame.draw.line(window, color, (n.x(), n.y()),
                             (other.x(), other.y()), 1)
    pygame.display.update()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    fpsClock.tick(30)


import pandas as pd
import numpy as np
from QT_Assignment.toolbox import moduleTemplates

class DataLoader(moduleTemplates.Dataloader_module):
    def __init__(self):
        super().__init__()
        self.info['name'] = 'Iris Loader'
        self.info['default'] = 0

    def load(self, path, splitAccelFiles):
        df = pd.DataFrame([])
        df = pd.read_csv(path, sep=" ", header=None)
        df = np.ravel(df)
        df = pd.to_numeric(df, errors='coerce')
        df = df.reshape(int(len(df)), 1).T

        properties = {'ts': df, 'Sampling Rate': 1, 'params': []}
        return properties


"""Module for validating user input values."""
import numpy as np

def validate_user_input(microscope, settings):
    application_files = [
        settings["system"]["application_file_rectangle"],
        settings["system"]["application_file_cleaning_cross_section"],
    ]
    _validate_application_files(microscope, application_files)
    scanning_resolutions = [
        settings["imaging"]["resolution"],
        settings["fiducial"]["reduced_area_resolution"],
    ]
    _validate_scanning_rotation(microscope)
    dwell_times = [settings["imaging"]["dwell_time"]]
    _validate_dwell_time(microscope, dwell_times)
    _validate_scanning_resolutions(microscope, scanning_resolutions)
    # TODO
    # ion_beam_currents = [
    #     settings["fiducial"]["fiducial_milling_current"],
    #     settings["lamella"]["milling_current"],
    # ]
    # _validate_ion_beam_currents(microscope, ion_beam_currents)

def _validate_application_files(microscope, application_files):
    """Check that the user supplied application files exist on this system.

    Parameters
    ----------
    microscope : Connected Autoscrpt microscope instance.
    application_files : list
        List of application files, eg: ['Si', 'Si_small']

    Raises
    ------
    ValueError
        Application file name not found in list of available application files.
    """
    available_files = microscope.patterning.list_all_application_files()
    for app_file in application_files:
        if not app_file in available_files:
            raise ValueError(
                "{} not found ".format(app_file)
                + "in list of available application files!\n"
                "Please choose one from the list: \n"
                "{}".format(available_files)
            )

def _validate_dwell_time(microscope, dwell_times):
    """Check that the user specified dwell times are within the limits.

    Parameters
    ----------
    microscope : Connected Autoscrpt microscope instance.
    dwell_times : list
        List of dwell times, eg: [1e-7, 1e-6]

    Raises
    ------
    ValueError
        Dwell time is smaller than the minimum limit.
    ValueError
        Dwell time is larger than the maximum limit.
    """
    dwell_limits = microscope.beams.ion_beam.scanning.dwell_time.limits
    for dwell in dwell_times:
        if not isinstance(dwell, (int, float)):
            raise ValueError(
                "Dwell time must be a number!\n".format(dwell)
                + "Please choose a value between the limits: \n"
                "{}".format(dwell_limits)
            )
        if dwell < dwell_limits.min:
            raise ValueError(
                "{} dwell time is too small!\n".format(dwell)
                + "Please choose a value between the limits: \n"
                "{}".format(dwell_limits)
            )
        elif dwell > dwell_limits.max:
            raise ValueError(
                "{} dwell time is too large!\n".format(dwell)
                + "Please choose a value between the limits: \n"
                "{}".format(dwell_limits)
            )
        else:
            if dwell is np.nan:
                raise ValueError(
                    "{} dwell time ".format(dwell) + "is not a number!\n"
                    "Please choose a value between the limits:\n"
                    "{}".format(dwell_limits)
                )

def _validate_ion_beam_currents(microscope, ion_beam_currents):
    """Check that the user supplied ion beam current values are valid.

    Parameters
    ----------
    microscope : Connected Autoscrpt microscope instance.
    ion_beam_currents : list
        List of ion beam currents, eg: [ 3e-10, 1e-09]

    Raises
    ------
    ValueError
        Beam current not found in list of available ion beam currents.
    """
    available_ion_beam_currents = (
        microscope.beams.ion_beam.beam_current.available_values
    )
    for beam_current in ion_beam_currents:
        if not beam_current in available_ion_beam_currents:
            raise ValueError(
                "{} not found ".format(beam_current)
                + "in list of available ion beam currents!\n"
                "Please choose one from the list: \n"
                "{}".format(available_ion_beam_currents)
            )

def _validate_horizontal_field_width(microscope, horizontal_field_widths):
    """Check that the ion beam horizontal field width is within the limits.

    Parameters
    ----------
    microscope : Connected Autoscrpt microscope instance.
    horizontal_field_widths : list
        List of ion beam horizontal field widths, eg: [50e-6, 100e-6]

    Raises
    ------
    ValueError
        Ion beam horizontal field width is smaller than the minimum limit.
    ValueError
        Ion beam horizontal field width is larger than the maximum limit.
    """
    hfw_limits = microscope.beams.ion_beam.horizontal_field_width.limits
    for hfw in horizontal_field_widths:
        if not isinstance(hfw, (int, float)):
            raise ValueError(
                "Horizontal field width must be a number!\n"
                "Please choose a value between the limits: \n"
                "{}".format(hfw_limits)
            )
        if hfw < hfw_limits.min:
            raise ValueError(
                "{} ".format(hfw) + "horizontal field width is too small!\n"
                "Please choose a value between the limits: \n"
                "{}".format(hfw_limits)
            )
        elif hfw > hfw_limits.max:
            raise ValueError(
                "{} ".format(hfw) + "horizontal field width is too large!\n"
                "Please choose a value between the limits: \n"
                "{}".format(hfw_limits)
            )
        else:
            if hfw is np.nan:
                raise ValueError(
                    "{} dwell time ".format(hfw) + "is not a number!\n"
                    "Please choose a value between the limits: \n"
                    "{}".format(hfw_limits)
                )

def _validate_scanning_resolutions(microscope, scanning_resolutions):
    """Check that the user supplied scanning resolution values are valid.

    Parameters
    ----------
    microscope : Connected Autoscrpt microscope instance.
    scanning_resolutions : list
        List of scanning resolutions, eg: ['1536x1024', '3072x2048']

    Raises
    ------
    ValueError
        Resolution not found in list of available scanning resolutions.
    """
    available_resolutions = (
        microscope.beams.ion_beam.scanning.resolution.available_values
    )
    microscope.beams.ion_beam.beam_current.available_values
    for resolution in scanning_resolutions:
        if not resolution in available_resolutions:
            raise ValueError(
                "{} not found ".format(resolution)
                + "in list of available scanning resolutions!\n"
                "Please choose one from the list: \n"
                "{}".format(available_resolutions)
            )

def _validate_scanning_rotation(microscope):
    rotation = microscope.beams.ion_beam.scanning.rotation.value
    if rotation is None:
        microscope.beams.ion_beam.scanning.rotation.value = 0
        rotation = microscope.beams.ion_beam.scanning.rotation.value
    if not np.isclose(rotation, 0.):
        raise ValueError(
            "Ion beam scanning rotation must be either 0 or 180 degrees!"
            "\nPlease change your system settings and try again."
            "\nCurrent rotation value is {}".format(rotation)
        )