from aita.aita_dataset import AitaDataset
import datetime
from keras.preprocessing.sequence import pad_sequences
import numpy as np
import os
import pandas as pd
import pyarrow as pa
import pyarrow.parquet as pq
from transformers import BertTokenizer, BertForSequenceClassification, get_linear_schedule_with_warmup, AdamW
import random
from sklearn.model_selection import train_test_split
import tensorflow as tf
import time
import torch
from torch.utils.data import DataLoader, random_split, SequentialSampler, RandomSampler

def format_time(elapsed):
    '''
    Takes a time in seconds and returns a string hh:mm:ss
    '''
    # Round to the nearest second.
    elapsed_rounded = int(round((elapsed)))

    # Format as hh:mm:ss
    return str(datetime.timedelta(seconds=elapsed_rounded))

def flat_accuracy(preds, labels):
    pred_flat = np.argmax(preds, axis=1).flatten()
    labels_flat = labels.flatten()
    return np.sum(pred_flat == labels_flat) / len(labels_flat)

def pad_data(tokenizer, input_ids):
    # We'll borrow the `pad_sequences` utility function to do this.

    MAX_LEN = 512

    print('\nPadding/truncating all sentences to %d values...' % MAX_LEN)

    print('\nPadding token: "{:}", ID: {:}'.format(tokenizer.pad_token, tokenizer.pad_token_id))

    # Pad our input tokens with value 0.
    # "post" indicates that we want to pad and truncate at the end of the sequence,
    # as opposed to the beginning.
    input_ids = pad_sequences(
        input_ids,
        maxlen=MAX_LEN,
        dtype="long",
        value=0,
        truncating="post",
        padding="post"
    )
    print('\nDone')
    return input_ids

def set_up_datasets():
     # Load the BERT tokenizer.
    print('Loading BERT tokenizer...')
    tokenizer = BertTokenizer.from_pretrained('bert-base-uncased', do_lower_case=True)

    aita_dataset = AitaDataset(tokenizer, max_length=512)

    train_size = int(np.floor(len(aita_dataset) * 0.9))
    test_size = len(aita_dataset) - train_size
    batch_size = 32

    # Create the DataLoader for our training set.
    train_data, validation_data = random_split(aita_dataset, [train_size, test_size])
    train_sampler = RandomSampler(train_data)
    train_dataloader = DataLoader(train_data, sampler=train_sampler, batch_size=batch_size, num_workers=4)

    # Create the DataLoader for our validation set.
    validation_sampler = SequentialSampler(validation_data)
    validation_dataloader = DataLoader(validation_data, sampler=validation_sampler, batch_size=batch_size)

    return train_dataloader, validation_dataloader

def train(train_dataloader, epochs=4):
    device = torch.device('cpu')

    print("Loading model")
    model = BertForSequenceClassification.from_pretrained(
        "bert-base-uncased", # Use the 12-layer BERT model, with an uncased vocab.
        num_labels = 1, # The number of output labels--2 for binary classification.
                    # You can increase this for multi-class tasks.
        output_attentions = False, # Whether the model returns attentions weights.
        output_hidden_states = False, # Whether the model returns all hidden-states.
        max_position_embeddings=512
    )

    # Total number of training steps is number of batches * number of epochs.
    total_steps = len(train_dataloader) * epochs

    # Note: AdamW is a class from the huggingface library (as opposed to pytorch)
    # I believe the 'W' stands for 'Weight Decay fix"
    optimizer = AdamW(
        model.parameters(),
        lr = 2e-5, # args.learning_rate - default is 5e-5, our notebook had 2e-5
        eps = 1e-8 # args.adam_epsilon  - default is 1e-8.
    )

    # Create the learning rate scheduler.
    scheduler = get_linear_schedule_with_warmup(
        optimizer,
        num_warmup_steps=0, # Default value in run_glue.py
        num_training_steps=total_steps
    )

    # This training code is based on the `run_glue.py` script here:
    # https://github.com/huggingface/transformers/blob/5bfcd0485ece086ebcbed2d008813037968a9e58/examples/run_glue.py#L128

    # Set the seed value all over the place to make this reproducible.
    # seed_val = 42

    # random.seed(seed_val)
    # np.random.seed(seed_val)
    # torch.manual_seed(seed_val)

    # Store the average loss after each epoch so we can plot them.
    loss_values = []

    # For each epoch...
    for epoch_i in range(0, epochs):

        # ========================================
        #               Training
        # ========================================

        # Perform one full pass over the training set.

        print("")
        print('======== Epoch {:} / {:} ========'.format(epoch_i + 1, epochs))
        print('Training...')

        # Measure how long the training epoch takes.
        t0 = time.time()

        # Reset the total loss for this epoch.
        total_loss = 0

        # Put the model into training mode. Don't be mislead--the call to
        # `train` just changes the *mode*, it doesn't *perform* the training.
        # `dropout` and `batchnorm` layers behave differently during training
        # vs. test (source: https://stackoverflow.com/questions/51433378/what-does-model-train-do-in-pytorch)
        model.train()

        # For each batch of training data...
        for step, batch in enumerate(train_dataloader):

            # Progress update every 40 batches.
            if step % 40 == 0 and not step == 0:
                # Calculate elapsed time in minutes.
                elapsed = format_time(time.time() - t0)

                # Report progress.
                print('  Batch {:>5,}  of  {:>5,}.    Elapsed: {:}.'.format(step, len(train_dataloader), elapsed))

            if step > 1:
                break

            # Unpack this training batch from our dataloader.
            #
            # As we unpack the batch, we'll also copy each tensor to the GPU using the
            # `to` method.
            #
            # `batch` contains three pytorch tensors:
            #   [0]: input ids
            #   [1]: attention masks
            #   [2]: labels

            b_input_ids = batch[0].to(device)
            b_input_mask = batch[1].to(device)
            b_labels = batch[2].to(device)

            # Always clear any previously calculated gradients before performing a
            # backward pass. PyTorch doesn't do this automatically because
            # accumulating the gradients is "convenient while training RNNs".
            # (source: https://stackoverflow.com/questions/48001598/why-do-we-need-to-call-zero-grad-in-pytorch)
            model.zero_grad()

            # Perform a forward pass (evaluate the model on this training batch).
            # This will return the loss (rather than the model output) because we
            # have provided the `labels`.
            # The documentation for this `model` function is here:
            # https://huggingface.co/transformers/v2.2.0/model_doc/bert.html#transformers.BertForSequenceClassification
            outputs = model(
                b_input_ids,
                # token_type_ids=None,
                attention_mask=b_input_mask,
                labels=b_labels
            )

            # The call to `model` always returns a tuple, so we need to pull the
            # loss value out of the tuple.
            loss = outputs[0]

            # Accumulate the training loss over all of the batches so that we can
            # calculate the average loss at the end. `loss` is a Tensor containing a
            # single value; the `.item()` function just returns the Python value
            # from the tensor.
            total_loss += loss.item()

            # Perform a backward pass to calculate the gradients.
            loss.backward()

            # Clip the norm of the gradients to 1.0.
            # This is to help prevent the "exploding gradients" problem.
            torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)

            # Update parameters and take a step using the computed gradient.
            # The optimizer dictates the "update rule"--how the parameters are
            # modified based on their gradients, the learning rate, etc.
            optimizer.step()

            # Update the learning rate.
            scheduler.step()

            # Calculate the average loss over the training data.
            avg_train_loss = total_loss / len(train_dataloader)

            # Store the loss value for plotting the learning curve.
            loss_values.append(avg_train_loss)

        print("")
        print("\tAverage training loss: {0:.2f}".format(avg_train_loss))
        print("\tTraining epoch took: {:}".format(format_time(time.time() - t0)))
    print("")
    print("Training complete!")

    return model

def validate(validation_dataloader, model):
    # ========================================
        #               Validation
        # ========================================
        # After the completion of each training epoch, measure our performance on
        # our validation set.

        print("")
        print("Running Validation...")

        t0 = time.time()

        # Put the model in evaluation mode--the dropout layers behave differently
        # during evaluation.
        model.eval()

        # Tracking variables
        eval_loss, eval_accuracy = 0, 0
        nb_eval_steps, nb_eval_examples = 0, 0

        # Evaluate data for one epoch
        for i, batch in enumerate(validation_dataloader):

            if i > 2:
                break
            # Unpack the inputs from our dataloader
            b_input_ids, b_input_mask, b_labels = batch

            # Telling the model not to compute or store gradients, saving memory and
            # speeding up validation
            with torch.no_grad():

                # Forward pass, calculate logit predictions.
                # This will return the logits rather than the loss because we have
                # not provided labels.
                # token_type_ids is the same as the "segment ids", which
                # differentiates sentence 1 and 2 in 2-sentence tasks.
                # The documentation for this `model` function is here:
                # https://huggingface.co/transformers/v2.2.0/model_doc/bert.html#transformers.BertForSequenceClassification
                outputs = model(
                    b_input_ids,
                    token_type_ids=None,
                    attention_mask=b_input_mask
                )

            # Get the "logits" output by the model. The "logits" are the output
            # values prior to applying an activation function like the softmax.
            logits = outputs[0]

            # Move logits and labels to CPU
            logits = logits.numpy()
            label_ids = b_labels.numpy()

            # Calculate the accuracy for this batch of test sentences.
            tmp_eval_accuracy = flat_accuracy(logits, label_ids)

            # Accumulate the total accuracy.
            eval_accuracy += tmp_eval_accuracy

            # Track the number of batches
            nb_eval_steps += 1

        # Report the final accuracy for this validation run.
        print("\tAccuracy: {0:.2f}".format(eval_accuracy/nb_eval_steps))
        print("\tValidation took: {:}".format(format_time(time.time() - t0)))

def save_model(model, path):
    model.save_pretrained(path)

def main():
    train_loader, valid_loader = set_up_datasets()

    model = train(train_loader, epochs=1)

    validate(valid_loader, model)

    save_model(model, '/Users/dashiell/workspace/aita/models')

if __name__ == '__main__':
    main()


# the_plain_bottle.py

select_gas_model(model='ideal gas', species=['air'])
initial = FlowCondition(p=5955.0,  u=0.0,    v=0.0, T=304.0)

# Create the nodes that define key points for our geometry.
A = Node(-0.07, 0.0, label="A"); B = Node(-0.05, 0.0, label="B")
C = Node(0.0, 0.0, label="C"); D = Node(0.005, 0.012, label="D")
E = Node(0.1, 0.03, label="E"); F = Node(0.202, 0.03, label="F")
G = Node(0.207, 0.0, label="G"); H = Node(0.3, 0.0, label="H")
I = Node(-0.07, 0.1, label="I"); J = Node(-0.05, 0.1, label="J")
K = Node(0.1, 0.1, label="K"); L = Node(0.202, 0.1, label="L")
M = Node(0.3, 0.1, label="M"); N = Node(0.3, 0.03, label="N")

# Some interior Bezier control points
CD_b1 = Node(0.0, 0.006, label="CD-b1")
DJ_b1 = Node(-0.008, 0.075, label="DJ-b1")
GF_b1 = Node(0.207, 0.027, label="GF-b1")
DE_b1 = Node(0.0064, 0.012, label="DE-b1")
DE_b2 = Node(0.0658, 0.0164, label="DE-b2")
DE_b3 = Node(0.0727, 0.0173, label="DE-b3")

# Now, we join our nodes to create lines that will be used to form our blocks.
AB = Line(A, B); BC = Line(B, C); GH = Line(G,H) # lower boundary along x-axis
CD = Bezier([C, CD_b1, D]) # top of bottle
DE = Bezier([D, DE_b1, DE_b2, DE_b3, E]) # neck of bottle
EF = Line(E, F) # side of bottle
GF = Bezier([G, GF_b1, F],"GF",0.0,1.0,1) # bottom, with arc-length parameterization
# Upper boundary of domain
IJ = Line(I, J); JK = Line(J, K); KL = Line(K, L); LM = Line(L, M)
# Lines to divide the gas flow domain into blocks.
AI = Line(A, I); BJ = Line(B, J); DJ = Bezier([D, DJ_b1, J])
JD = DJ.copy(direction=-1); EK = Line(E, K); FL = Line(F, L);
NM = Line(N, M); HN = Line(H, N); FN = Line(F, N)

# Define the blocks, boundary conditions and set the discretisation.
n0 = 10; n1 = 4; n2 = 20; n3 = 20; n4 = 20; n5 = 12; n6 = 8

BL_0 = Block2D(make_patch(IJ, BJ, AB, AI), nni=n1, nnj=n0,
               fill_condition=initial, label="[0]")
BL_1 = Block2D(make_patch(JD, CD, BC, BJ), nni=n2, nnj=n0,
               fill_condition=initial, label="[1]")
BL_2 = Block2D(make_patch(JK, EK, DE, DJ), nni=n3, nnj=n2,
               fill_condition=initial, label="[2]")
BL_3 = Block2D(make_patch(KL, FL, EF, EK), nni=n4, nnj=n2,
               fill_condition=initial, label="[3]")
BL_4 = Block2D(make_patch(LM, NM, FN, FL), nni=n5, nnj=n2,
               fill_condition=initial, label="[4]")
BL_5 = Block2D(make_patch(FN, HN, GH, GF), nni=n5, nnj=n6,
               fill_condition=initial, label="[5]")
identify_block_connections()

# Make a nicely-scaled SVG file at the end.
sketch.xaxis(-0.1, 0.3, 0.05, -0.05)
sketch.yaxis(0.0, 0.10, 0.05, -0.01)
sketch.window(-0.1, 0.0, 0.3, 0.4, 0.02, 0.05, 0.20, 0.23)


# -*- coding: utf-8 -*-
"""
Jsconnect is a lightweight library aimed to help you connect django appliction
with javascript frameworks.

Available support for:
    ExtJs: ExtDirect, Storage via ExtDirect (partial), ExtForms

SEE:
    SVN (main source): http://code.google.com/p/jsconnect
    GIT (svn mirror): https://github.com/rumatoest/jsconnect
    PYPI: http://pypi.python.org/pypi/jsconnect
"""
__version__ = "0.1.0"

from django.conf import settings
from django.core.serializers.json import DjangoJSONEncoder
from django.http import HttpResponse
from django.utils.safestring import SafeData

import os.path

def js_connector(request):
    """
    Django view that generate javascript file with some utiles for your web application
    Use Javascript pattern: static/django-extdirect/connector.js

    Example:
        # In your urls.py add
        url(r'^js/extdirect-connector.js$', 'extdirect.js_connector', name="extdirect-connector"),

        # Then somwere in template add
        <script type="text/javascript" src="{% url 'extdirect-connector' %}" ></script>
    """
    from django.middleware import csrf

    file_path = os.path.join(os.path.dirname(__file__), 'static', 'django-extdirect', 'connector.js')
    file_path = os.path.realpath(file_path)

    file_content = u"//Required file \"%s\" does not exists!" % file_path
    if os.path.exists(file_path):
        f = open(file_path , 'r')
        file_content = f.read()

    config = dict(
        debug=settings.DEBUG,
        media_url=str(settings.MEDIA_URL).rstrip('/'),
        static_url=str(settings.STATIC_URL).rstrip('/'),
        csrf_token=csrf.get_token(request),
    )

    return HttpResponse(file_content + u"\ndjangoExtDirect.config = %s;" % DjangoJSONEncoder(ensure_ascii=False).encode(config), content_type="text/javascript")

class JsonEncoder(DjangoJSONEncoder):
    """
    Just DjangoJSONEncoder with some additional stuff you should not know about.
    """

    def default(self, o):

#        from datetime import date, datetime
#        # http://www.w3.org/TR/NOTE-datetime
#        if isinstance(o, datetime):
#            return  o.strftime("%Y-%m-%d %H:%M:%S")
#        if isinstance(o, date):
#            return  o.strftime("%Y-%m-%d")

        if isinstance(o, JsonContainer):
            return  o.get_json()
        return super(self.__class__, self).default(o)

    @staticmethod
    def js_string(string_as_js):
        """
        Mark string to be output in JSON as Java Script.
        Used django native mark_safe() method
        """
        from django.utils.safestring import mark_safe
        return mark_safe(string_as_js)

class JsonContainer(dict):
    """
    Just a dictionary for JSON data. Now it is not very functional and
    I may remove it in future vertions.
    """

    def __str__(self):
        """
        see: get_json()
        """
        return self.get_json()

    def __unicode__(self):
        return self.get_json()

    def get_json(self):
        """
        Convert self to JSON
        """
        return JsonEncoder().encode(self)

# This is awful python lib monkey patching
# Yes I know it is very bad, but I have no choice :(
# Actually I have a choice, but I don't want to create my own json encoder.
#----------------------------------------------------------------------------------------
import json.encoder
# See: http://svn.python.org/view/python/branches/release27-maint/Lib/json/encoder.py?view=markup

def _encode_string_wrapper(encode_fn):
    """
    In order to return safe string as JS code we must wrap next python functions:
    encode_basestring(s) и py_encode_basestring_ascii(s)
    with this wrapper
    """
    def wrapper(str):
        encoded = encode_fn(str)
        if isinstance(str, SafeData):
            if len(encoded) > 2 and encoded.startswith('"') or encoded.startswith('\''):
                return encoded[1:-1]
        return encoded
    return wrapper

# Wrapping itself
json.encoder.encode_basestring = _encode_string_wrapper(json.encoder.encode_basestring)
json.encoder.py_encode_basestring_ascii = _encode_string_wrapper(json.encoder.py_encode_basestring_ascii)


import random

class Node:
    def __init__(self, index, attr={}):
        self.index = index
        self.attr = attr  # label

    def __str__(self):
        return self.index

class Edge:
    def __init__(self, index, node_from='', node_to='', attr={}):
        self.index = index
        self.node_from = node_from
        self.node_to = node_to
        self.attr = attr

    def __str__(self):
        return self.index

class Graph:
    def __init__(self, nodes, edges):
        self.nodes = nodes
        self.edges = edges
        self.incidence_matrix = self._create_incidence_matrix()
        self.edge = self._create_edge_dict() # adjacent matrix
        self.nodes2, self.nodes_attr = self._create_nodes()
        self.nodes3 = self.nodes2
        self.index1 = list(range(len(self.nodes2)))

    def __len__(self):
        return len(self.nodes2)
    def _create_nodes(self):
        nodes =  [_.index for _ in self.nodes]
        #random.shuffle(nodes)
        attr = {_.index : _.attr for _ in self.nodes}
        return nodes, attr

    def _shuffle_nodes(self):
        index = list(range(len(self.nodes2)))
        random.shuffle(index)
        index2 = [0 for i in range(len(index))]
        for i in range(len(index)):
            index2[index[i]]=i

        self.index1 = index
        self.index2 = index2
        self.nodes2 = [self.nodes3[_] for _ in index]

    def print_shuffle(self):
        print(self.index1)
        #print(self.index2)

    def _create_edge_dict(self):
        all_edge = {}

        for node in self.nodes:
            all_edge[node.index] = {}

        for edge in self.edges:
            node_from = edge.node_from
            node_to = edge.node_to
            all_edge[node_from][node_to] = edge.attr
            all_edge[node_to][node_from] = edge.attr

        return all_edge

    def _create_incidence_matrix(self):
        """
        Constructs incidence matrix of graph.
        """
        matrix = [
            [0 for _ in range(len(self.edges))] for _ in range(len(self.nodes))
        ]

        for node_id, node in enumerate(self.nodes):
            for edge_id, edge in enumerate(self.edges):
                if edge.node_from == node.index:
                    matrix[node_id][edge_id] = 1
                if edge.node_to == node.index:
                    matrix[node_id][edge_id] = -1

        return matrix


__author__ = 'Joe Linn'

import unittest
import pylastica
from tests.base import Base

class StatusTest(unittest.TestCase, Base):
    def test_get_response(self):
        index = self._create_index()
        status = pylastica.Status(index.client)
        self.assertIsInstance(status.response, pylastica.Response)
        index.delete()

    def test_get_index_statuses(self):
        index = self._create_index()
        status = pylastica.Status(index.client)
        statuses = status.index_statuses

        self.assertIsInstance(statuses, list)

        for index_status in statuses:
            self.assertIsInstance(index_status, pylastica.index.Status)

        index.delete()

    def test_get_index_names(self):
        index_name = 'test'
        client = self._get_client()
        index = client.get_index(index_name)
        index.create(options=True)
        status = pylastica.Status(client)
        names = status.index_names

        self.assertIsInstance(names, list)
        self.assertTrue(index.name in names)

        for name in names:
            self.assertIsInstance(name, unicode)
        index.delete()

    def test_index_exists(self):
        index_name = 'pylastica_test'
        client = self._get_client()
        index = client.get_index(index_name)
        try:
            index.delete()
        except pylastica.exception.ResponseException as e:
            pass

        status = pylastica.Status(client)

        self.assertFalse(status.index_exists(index_name))

        index.create()
        status.refresh()

        self.assertTrue(status.index_exists(index_name))
        index.delete()

    def test_alias_exists(self):
        alias_name = 'pylastica_test-alias'
        index = self._create_index()
        status = pylastica.Status(index.client)

        for tmp_index in status.get_indices_with_alias(alias_name):
            tmp_index.remove_alias(alias_name)

        self.assertFalse(status.alias_exists(alias_name))

        index.add_alias(alias_name)
        status.refresh()

        self.assertTrue(status.alias_exists(alias_name))
        index.delete()

    def test_server_status(self):
        status = self._get_client().status
        server_status = status.server_status

        self.assertIsInstance(server_status, dict)
        self.assertTrue('status' in server_status)
        self.assertTrue(server_status['status'] == 200)
        self.assertTrue('version' in server_status)

        version_info = server_status['version']
        self.assertTrue('number' in version_info)

    def test_get_indices_with_alias(self):
        index_name1 = 'test1'
        index_name2 = 'test2'
        alias_name = 'test-alias'
        client = self._get_client()
        index1 = pylastica.index.Index(client, index_name1)
        index1.create(options=True)
        index2 = pylastica.index.Index(client, index_name2)
        index2.create(options=True)
        index1.add_alias(alias_name)
        index2.add_alias(alias_name)

        status = client.status
        indices = status.get_indices_with_alias(alias_name)

        self.assertEqual(indices[0].name, index1.name)
        self.assertEqual(indices[1].name, index2.name)

        index1.delete()
        index2.delete()

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


class Settings():
    def __init__(self):
        """初始化游戏静态设置"""
        self.screen_width=1200
        self.screen_height=700
        self.bg_color=(230,230,230)

        #bullet
        self.bullet_width=300
        self.bullet_height=15
        self.bullet_color=60,60,60
        self.bullets_allowed=3

        #外星人
        self.alien_speed_factor=1#水平速度
        self.fleet_drop_speed=1#垂直速度
        self.fleet_direction=1#1为右移

        #飞船
        self.ship_limit=3

        #加速游戏节奏
        self.speedup_scale=1.1
        #外星人点数的提高速度
        self.score_scale=1.5

        self.initialize_dynamic_settings()

    def initialize_dynamic_settings(self):
        self.ship_speed_factor=1.5
        self.bullet_speed_factor=3
        self.alien_speed_factor=1
        self.fleet_direction=1
        #计分
        self.alien_point=50

    def increase_speed(self):
        self.ship_speed_factor *= self.speedup_scale
        self.bullet_speed_factor *= self.speedup_scale
        self.alien_speed_factor *= self.speedup_scale

        self.alien_points=int(self.alien_points*self.score_scale)


import numpy as np
from six import BytesIO
from PIL import Image

import tensorflow as tf
import json
from object_detection.utils import ops as utils_ops
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util
import os
import pathlib

def load_image_into_numpy_array(path):
    """Load an image from file into a numpy array.

    Puts image into numpy array to feed into tensorflow graph.
    Note that by convention we put it into a numpy array with shape
    (height, width, channels), where channels=3 for RGB.

    Args:
      path: the file path to the image

    Returns:
      uint8 numpy array with shape (img_height, img_width, 3)
    """
    img_data = tf.io.gfile.GFile(path, 'rb').read()
    image = Image.open(BytesIO(img_data))
    (im_width, im_height) = image.size
    return np.array(image.getdata()).reshape(
        (im_height, im_width, 3)).astype(np.uint8)

def get_keypoint_tuples(eval_config):
    """Return a tuple list of keypoint edges from the eval config.

    Args:
      eval_config: an eval config containing the keypoint edges

    Returns:
      a list of edge tuples, each in the format (start, end)
    """
    tuple_list = []
    kp_list = eval_config.keypoint_edge
    for edge in kp_list:
        tuple_list.append((edge.start, edge.end))
    return tuple_list

def get_model_detection_function(model):
    """Get a tf.function for detection."""

    @tf.function
    def detect_fn(image):
        """Detect objects in image."""

        image, shapes = model.preprocess(image)
        prediction_dict = model.predict(image, shapes)
        detections = model.postprocess(prediction_dict, shapes)

        return detections, prediction_dict, tf.reshape(shapes, [-1])

    return detect_fn

def run_inference_for_single_image(model, image):
    image = np.asarray(image)
    # The input needs to be a tensor, convert it using `tf.convert_to_tensor`.
    input_tensor = tf.convert_to_tensor(image)
    # The model expects a batch of images, so add an axis with `tf.newaxis`.
    input_tensor = input_tensor[tf.newaxis,...]

    # Run inference
    model_fn = model.signatures['serving_default']
    output_dict = model_fn(input_tensor)

    # All outputs are batches tensors.
    # Convert to numpy arrays, and take index [0] to remove the batch dimension.
    # We're only interested in the first num_detections.
    num_detections = int(output_dict.pop('num_detections'))
    output_dict = {key:value[0, :num_detections].numpy()
                   for key,value in output_dict.items()}
    output_dict['num_detections'] = num_detections

    # detection_classes should be ints.
    output_dict['detection_classes'] = output_dict['detection_classes'].astype(np.int64)

    # Handle models with masks:
    if 'detection_masks' in output_dict:
        # Reframe the the bbox mask to the image size.
        detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
            output_dict['detection_masks'], output_dict['detection_boxes'],
            image.shape[0], image.shape[1])
        detection_masks_reframed = tf.cast(detection_masks_reframed > 0.5,
                                           tf.uint8)
        output_dict['detection_masks_reframed'] = detection_masks_reframed.numpy()

    return output_dict

def show_inference(model, image_path, category_index, output_path):
    # the array based representation of the image will be used later in order to prepare the
    # result image with boxes and labels on it.
    image_np = np.array(Image.open(image_path))
    # Actual detection.
    file_name = os.path.basename(image_path)
    output_dict = run_inference_for_single_image(model, image_np)
    # Visualization of the results of a detection.
    vis_util.visualize_boxes_and_labels_on_image_array(
        image_np,
        output_dict['detection_boxes'],
        output_dict['detection_classes'],
        output_dict['detection_scores'],
        category_index,
        instance_masks=output_dict.get('detection_masks_reframed', None),
        use_normalized_coordinates=True,
        line_thickness=4,
        max_boxes_to_draw=1000,)

    image = Image.fromarray(image_np)
    image.save(os.path.join(output_path, file_name))

def show_gt(image_path, category_index, coors, tags, output_path):
    # the array based representation of the image will be used later in order to prepare the
    # result image with boxes and labels on it.
    image_np = np.array(Image.open(image_path))

    # Actual detection.
    file_name = os.path.basename(image_path)
    # Visualization of the results of a detection.
    vis_util.visualize_boxes_and_labels_on_image_array(
        image_np,
        coors,
        tags,
        np.ones_like(tags),
        category_index,
        instance_masks=None,
        use_normalized_coordinates=True,
        line_thickness=4,
        max_boxes_to_draw=1000,)

    image = Image.fromarray(image_np)
    image.save(os.path.join(output_path, file_name))

def main():
    assets_dict = {}
    with open('/home/kimberly/projects/ui_data_v13/ui/test_vott/overlapped.vott', "r") as fp:
        data = json.load(fp)
        assets = data['assets']
        for a in assets:
            assets_dict[assets[a]['name']] = a
    base_dir = '/home/kimberly/projects/ui_data_v13/ui/'
    pbtxt = os.path.join(base_dir, 'ui.pbtxt')

    model_dir = '/home/kimberly/projects/ui_data_v13/ui/model'

    detection_model = tf.saved_model.load(model_dir)
    PATH_TO_LABELS = pbtxt
    category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
    category_dict = {}
    for cat in category_index.values():
        category_dict[cat['name']] = cat['id']
    PATH_TO_TEST_IMAGES_DIR = pathlib.Path(base_dir+'/test')
    TEST_IMAGE_PATHS = sorted(list(PATH_TO_TEST_IMAGES_DIR.glob("*")))
    for image_path in TEST_IMAGE_PATHS:
        # show_inference(detection_model, image_path, category_index)
        image_name = os.path.basename(image_path)
        asset_key = assets_dict[image_name]
        with open('/home/kimberly/projects/ui_data_v13/ui/test_vott/'+asset_key+ '-asset.json', "r") as fp:
            asset_data = json.load(fp)
            regions = asset_data['regions']
            im_width = asset_data['asset']['size']['width']
            im_height = asset_data['asset']['size']['height']
            coors = []
            tags = []
            for r in regions:
                height = r['boundingBox']['height']
                width = r['boundingBox']['width']
                left = r['boundingBox']['left']
                top = r['boundingBox']['top']
                # coor = [left / width, top / height, (left + width) / width, (top + height) / height]
                coor = [top / im_height, left / im_width, (top + height) / im_height, (left + width) / im_width]

                coors.append(coor)
                tag = r['tags'][0]
                tag = tag.lower()
                tag_id = category_dict[tag]
                tags.append(tag_id)
            coors = np.asarray(coors)
            tags = np.asarray(tags)
        show_inference(detection_model, image_path, category_index, '/home/kimberly/projects/ui_data_v13/ui/test_detected')
        show_gt(image_path, category_index, coors, tags, '/home/kimberly/projects/ui_data_v13/ui/gt')

if __name__ == '__main__':
    main()


"""RestartPolicyChecker

Checks the docker stack file content to make sure that restart
policies are in effect and adhere to certain demands"""

__author__ = 'tinglev@kth.se'

from modules.steps.base_pipeline_step import BasePipelineStep
from modules.util import data_defs, pipeline_data_utils, ci_status
from modules.util.exceptions import DeploymentError

class RestartPolicyChecker(BasePipelineStep):

    CI_STATUS_MISSING_POLICY = 'Missing policy'
    CI_STATUS_BAD_POLICY = 'Bad policy'

    def __init__(self):
        BasePipelineStep.__init__(self)

    def get_required_env_variables(self):
        return []

    def get_required_data_keys(self):
        return [data_defs.STACK_FILE_PARSED_CONTENT]

    def run_step(self, pipeline_data):
        for _, service in pipeline_data_utils.get_parsed_services(pipeline_data):
            try:
                self.has_restart_policy(service)
            except:
                ci_status.post_restart_policy_check(pipeline_data, self.CI_STATUS_MISSING_POLICY, 10)
                raise
            try:
                self.verify_restart_policy(service['deploy']['restart_policy'])
            except:
                ci_status.post_restart_policy_check(pipeline_data, self.CI_STATUS_BAD_POLICY, 10)
                raise
        ci_status.post_restart_policy_check(pipeline_data, ci_status.STATUS_OK, 0)
        return pipeline_data

    def has_restart_policy(self, service):
        if not 'deploy' in service or not 'restart_policy' in service['deploy']:
            raise DeploymentError('docker-stack.yml missing restart policy')

    def verify_restart_policy(self, policy_struct):
        if not ('condition' in policy_struct and
                'delay' in policy_struct and
                'max_attempts' in policy_struct):
            raise DeploymentError('docker-stack.yml has bad restart policy')


'''
    来源：力扣（LeetCode）
    链接：https://leetcode-cn.com/problems/container-with-most-water
    著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
'''
from typing import List

class Solution:
    '''
    暴力破解法，时间复杂度O(n^2)，会遍历所有结果
    '''
    capacity = 0

    def maxArea(self, height: List[int]) -> int:
        if len(height) < 2:
            return 0
        for index, value in enumerate(height):
            for i in range(index + 1, len(height)):
                length = i - index
                min_height = min(height[index], height[i])
                self.capacity = max(self.capacity, length * min_height)
        return self.capacity

class Solution2:
    '''
    双指针法
    '''
    capacity = 0

    def maxArea(self, height: List[int]) -> int:
        i, j = 0, len(height) - 1
        while i < j:
            if height[i] <= height[j]:
                self.capacity = max(self.capacity, height[i] * (j - i))
                i += 1
            else:
                self.capacity = max(self.capacity, height[j] * (j - i))
                j -= 1
        return self.capacity

if __name__ == "__main__":
    _solution = Solution()
    capacity = _solution.maxArea([1, 8, 6, 2, 5, 4, 8, 3, 7])
    print(capacity)


#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
#from __future__ import unicode_literals

import os.path
from unittest import SkipTest

import numpy as np
from numpy.testing import assert_allclose, assert_equal

import pytest
import itertools

from freediscovery.text import FeatureVectorizer
from freediscovery.categorization import Categorizer
from freediscovery.dupdet import DuplicateDetection
from freediscovery.cluster import Clustering
from freediscovery.lsi import LSI
from freediscovery.io import parse_ground_truth_file
from freediscovery.utils import categorization_score
from freediscovery.exceptions import OptionalDependencyMissing
from .run_suite import check_cache

basename = os.path.dirname(__file__)
data_dir = os.path.join(basename, "..", "data", "ds_001", "raw")

@pytest.mark.parametrize('use_hashing, method', itertools.product([False, True],
                                                ['Categorization', 'LSI',
                                                 'DuplicateDetection', 'Clustering']))
def test_features_hashing(use_hashing, method):
    # check that models work both with and without hashing

    cache_dir = check_cache()

    n_features = 20000

    fe = FeatureVectorizer(cache_dir=cache_dir)
    uuid = fe.preprocess(data_dir, file_pattern='.*\d.txt', n_features=n_features,
                         use_hashing=use_hashing)
    uuid, filenames  = fe.transform()

    ground_truth = parse_ground_truth_file(
                            os.path.join(data_dir, "..", "ground_truth_file.txt"))

    if method == 'Categorization':
        cat = Categorizer(cache_dir=cache_dir, dsid=uuid, cv_n_folds=2)
        index = cat.fe.search(ground_truth.index.values)

        try:
            coefs, Y_train = cat.train(
                                    index,
                                    ground_truth.is_relevant.values,
                                    )
        except OptionalDependencyMissing:
            raise SkipTest

        Y_pred = cat.predict()
        X_pred = np.arange(cat.fe.n_samples_, dtype='int')
        idx_gt = cat.fe.search(ground_truth.index.values)

        scores = categorization_score(idx_gt,
                            ground_truth.is_relevant.values,
                            X_pred, Y_pred)
        assert_allclose(scores['precision'], 1, rtol=0.5)
        assert_allclose(scores['recall'], 1, rtol=0.5)
        cat.delete()
    elif method == 'LSI':
        lsi = LSI(cache_dir=cache_dir, dsid=uuid)
        lsi_res, exp_var = lsi.transform(n_components=100)  # TODO unused variables
        lsi_id = lsi.mid
        assert lsi.get_dsid(fe.cache_dir, lsi_id) == uuid
        assert lsi.get_path(lsi_id) is not None
        assert lsi._load_pars(lsi_id) is not None
        lsi.load(lsi_id)

        idx_gt = lsi.fe.search(ground_truth.index.values)
        idx_all = np.arange(lsi.fe.n_samples_, dtype='int')

        for accumulate in ['nearest-max', 'centroid-max']:
                            #'nearest-diff', 'nearest-combine', 'stacking']:
            _, Y_train, Y_pred, ND_train = lsi.predict(
                                    idx_gt,
                                    ground_truth.is_relevant.values,
                                    accumulate=accumulate)
            scores = categorization_score(idx_gt,
                                ground_truth.is_relevant.values,
                                idx_all, Y_pred)
            assert_allclose(scores['precision'], 1, rtol=0.5)
            assert_allclose(scores['recall'], 1, rtol=0.3)
    elif method == 'DuplicateDetection':
        dd = DuplicateDetection(cache_dir=cache_dir, dsid=uuid)
        try:
            dd.fit()
        except ImportError:
            raise SkipTest
        cluster_id = dd.query(distance=10)
    elif method =='Clustering':
        if not use_hashing:
            cat = Clustering(cache_dir=cache_dir, dsid=uuid)
            cm = getattr(cat,'k_means')
            labels, htree = cm(2, lsi_components=20)

            terms = cat.compute_labels(n_top_words=10)
        else:
            with pytest.raises(NotImplementedError):
                Clustering(cache_dir=cache_dir, dsid=uuid)

    else:
        raise ValueError


import torch
from ....torchio import DATA
from ....utils import is_image_dict
from .. import RandomTransform

class RandomFlip(RandomTransform):
    def __init__(
            self,
            axes=(0,),
            flip_probability=0.5,
            seed=None,
            verbose=False,
            ):
        super().__init__(seed=seed, verbose=verbose)
        self.axes = axes
        assert flip_probability > 0
        assert flip_probability <= 1
        self.flip_probability = flip_probability

    def apply_transform(self, sample):
        axes_to_flip_hot = self.get_params(self.axes, self.flip_probability)
        sample['random_flip'] = axes_to_flip_hot
        for image_dict in sample.values():
            if not is_image_dict(image_dict):
                continue
            tensor = image_dict[DATA]
            dims = []
            for dim, flip_this in enumerate(axes_to_flip_hot):
                if not flip_this:
                    continue
                actual_dim = dim + 1  # images are 4D
                dims.append(actual_dim)
            tensor = torch.flip(tensor, dims=dims)
            image_dict[DATA] = tensor
        return sample

    @staticmethod
    def get_params(axes, probability):
        axes_hot = [False, False, False]
        for axis in axes:
            random_number = torch.rand(1)
            flip_this = bool(probability > random_number)
            axes_hot[axis] = flip_this
        return axes_hot


import unicodedata
from difflib import Differ

def remove_french_specific_letters(word):
    # transforme the strink into unicode with the normal form KD (NFKD), replace all compatibility characters with their equivalents.
    word_nfkd = unicodedata.normalize('NFKD', word)
    word_ascii = word_nfkd.encode('ASCII', 'ignore').decode()
    return word_ascii

def compare(wrong, right):
    # normalize both strings to make sure equivalent (but different) unicode characters are canonicalized
    wrong = unicodedata.normalize('NFKC', wrong)
    right = unicodedata.normalize('NFKC', right)

    num_diffs = 0
    index = 0
    differences = list(Differ().compare(wrong, right))
    while True:
        try:
            diff = differences[index]
        except IndexError:
            break
        op = diff[0]
        char = diff[-1]

        # if the character isn't equal in both strings, increase the difference counter
        if op != ' ':
            num_diffs += 1

        # if a character is wrong, there will be two operations: one
        # "+" and one "-" operation
        # we want to count this as a single mistake, not as two mistakes
        if op in '+-':
            try:
                next_diff = differences[index+1]
            except IndexError:
                num_diffs -= 0.25
                pass
            else:
                next_op = next_diff[0]
                if next_op in '+-' and next_op != op:
                    # skip the next operation, we don't want to count
                    # it as another mistake
                    index += 1

                    # Limit the difference score if it's only a special character
                    next_char = next_diff[-1]
                    if remove_french_specific_letters(char) == remove_french_specific_letters(next_char):
                        num_diffs -= 0.5
                if next_op == ' ':
                    print("oui")
                    index += 1
                    # verify if there was a permutation of letters
                    try:
                        next_diff = differences[index+1]
                    except IndexError:
                        pass
                    else:
                        next_op = next_diff[0]
                        next_char = next_diff[-1]
                        if next_op in '+-' and next_op != op and  next_char == char:
                            index += 1
                            num_diffs -= 0.25

        index += 1
    #return num_diffs / max(len(wrong), len(right))
    return num_diffs

for w, r in (('ab','ac'),
            ('àb','ab'),
            ('ab','ba'),
            ('ba','ab'),
            ('ça','ca'),
            ('ca','ça'),
            ('être','etre'),
            ('clala','claal'),
            ('très','trés'),
            ('blob','blobo'),
            ):
    #print('"{}" and "{}": {}% difference'.format(w, r, compare(w, r)*100))
    print('"{}" and "{}": {} difference'.format(w, r, compare(w, r)))


aggressive_1, calm_1, aggressive_2, calm_2 = list(map(int, input().split()))
p, m, g = list(map(int, input().split()))

dog_1 = (aggressive_1, calm_1)
dog_2 = (aggressive_2, calm_2)

def is_aggressive(dog, time):
    if time > (dog[0] + dog[1]):
        if time % (dog[0] + dog[1]) <= dog[0]:
            return True
        else:
            return False
    else:
        if time <= dog[0]:
            return True
        else:
            return False

def counter(person):
    n = 0
    if is_aggressive(dog_1, person):
        n += 1
    if is_aggressive(dog_2, person):
        n += 1
    return n

def output(person):
    if counter(person) == 0 :
        print('none')
    elif counter(person) == 1:
        print('one')
    else:
        print('both')

output(p)
output(m)
output(g)


import socket
import sys
import os
import ssl
import asyncore

def main():
    HOST = '127.0.0.1'    # The remote host
    PORT = 1234              # The same port as used by the server
    s = None
    for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM):
        af, socktype, proto, canonname, sa = res
        try:
            s = socket.socket(af, socktype, proto)
        except OSError as msg:
            s = None
            continue
        try:
            s.connect(sa)
        except OSError as msg:
            s.close()
            s = None
            continue
        break
    if s is None:
        print('could not open socket')
        sys.exit(1)
    with s:
        s.sendall(b'Hello, world')
        data = s.recv(1024)
    print('Received', repr(data))

#ssl
#Client socket example with default context and IPv4/IPv6 dual stack
def main1():
    hostname = 'www.baidu.com'
    context = ssl.create_default_context()

    with socket.create_connection((hostname, 443)) as sock:
        with context.wrap_socket(sock, server_hostname=hostname) as ssock:
            print(ssock.version())

#Client socket example with custom context and IPv4:
def main2():
    hostname = 'www.python.org'
    # PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
    context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
    context.load_verify_locations('path/to/cabundle.pem')

    with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as sock:
        with context.wrap_socket(sock, server_hostname=hostname) as ssock:
            print(ssock.version())

class HTTPClient(asyncore.dispatcher):
    def __init__(self, host, path):
        asyncore.dispatcher.__init__(self)
        self.create_socket()
        self.connect( (host, 80) )
        self.buffer = bytes('GET %s HTTP/1.0\r\nHost: %s\r\n\r\n' %
                            (path, host), 'ascii')

    def handle_connect(self):
        pass

    def handle_close(self):
        self.close()

    def handle_read(self):
        print(self.recv(8192))

    def writable(self):
        return (len(self.buffer) > 0)

    def handle_write(self):
        sent = self.send(self.buffer)
        self.buffer = self.buffer[sent:]

def main3():
    client = HTTPClient('www.python.org', '/')
    asyncore.loop()

if (__name__=="__main__"):
    main3()


import win32com.client
import sys
itunes = win32com.client.Dispatch("iTunes.Application")
library = itunes.LibraryPlaylist

command = str(sys.argv[1])
searchQueryString = str(sys.argv[2])

if command == 'play' :
    itunes.Play()
elif command == 'pause' :
    itunes.Pause()
elif command == 'next' :
    itunes.NextTrack()
elif command == 'previous' :
    itunes.PreviousTrack()
elif command == 'getInfo' :
    pass
elif command == "search" and searchQueryString != 'undefined':
    result = library.Search(searchQueryString,0)
    result.Item(1).Play() if result != None else print()
elif int(command) >= 0 or int(command) <= 100:
    itunes.SoundVolume = int(command)

itunes.CurrentTrack.Artwork(1).SaveArtworkToFile("D:/Development/pyTunes/public/art-work.png")

print(str(itunes.CurrentTrack.Artist) + " - " + str(itunes.CurrentTrack.Name)+";"+str(itunes.GetPlayerButtonsState()[1]))


#'gouhaowang,chazidian,jihaoba,beyaiw,chazidian,xingming,pianziweb,\\\
#guisd,gududai,138023,bole,04338888,shouji,rmbhuilv,xysjk,juzhao,\\\
#sjhcx,mshaoma,sojkan,taohaoba,fpsjk,aiqzu,taohaoma,38hp,ip138,kuaichala,\\\
#tiaohao,135cha,tiaohao,51so,yenwoop,baishicha,shoujicha,chahaoba,qeayer,aiqzu,jctnsy,\\\
#gdhaoma,danhaoma,yenwoop,stl56,bixinshui,bixinshui,\\\
#8684,tiantianxie,herowind,xy1989,135130,chanumber,jihaoba'

#white_list = [['\n        anquan.baidu.com    ']
#    , ['gd.tiaohao.com/others/...\xa0']
#    , ['https://www.gouhaowang.com/hao...\xa0']
#    , ['https://www.chazidian.com/haom...\xa0']
#    , ['https://m.jihaoba.com/haoduan/...\xa0']
#    , ['https://www.jihaoba.com/shouji...\xa0']
#    , ['https://m.jihaoba.com/haoduan/...\xa0']
#    , ['beyaiw.com/aiwdianhua/...\xa0']
#    , ['https://mip.chazidian.com/haom...\xa0']
#    , ['www.xingming.net/gujia...\xa0']
#    , ['https://www.pianziweb.com/mobi...\xa0']]
#white_list1 = ['www.guisd.com',
#               'gududai.com/c',
#               'www.138023.co',
#               'www.bole.name',
#               'www.04338888.',
#               'shouji.jiweix',
#               'www.rmbhuilv.',
#               'tool.baiwanzh',
#               'www.xysjk.com',
#               'www.juzhao.ne',
#               'www.sjhcx.com',
#               'www.mshaoma.c',
#               'sojkan.com/so',
#               'www.taohaoba.',
#               'www.fpsjk.cn/',
#               'm.aiqzu.net/m',
#               'aiqzu.net/mob',
#               'www.taohaoma.',
#               'www.38hp.com/',
#               'jx.ip138.com/',
#               'www.kuaichala',
#               'gd.tiaohao.co',
#               'www.135cha.co',
#               'www.tiaohao.c',
#               'www.51so.cc/m',
#               'yenwoop.com/h',
#               'www.baishicha',
#               'www.shoujicha',
#               'www.131313131',
#               'www.13677777.',
#               'www.chahaoba.',
#               'qeayer.com/di',
#               'www.38hp.com/',
#               'aiqzu.net/mob',
#               'm.jctnsy.com/',
#               'www.gdhaoma.c',
#               'www.danhaoma.',
#               'cn.amp.chahao',
#               'www.yenwoop.c',
#               'www.stl56.com',
#               'www.bixinshui',
#               'www.8684.cn/s',
#               'm.tiantianxie',
#               'www.herowind.',
#               'www.xy1989.co',
#               'www.135130.co',
#               'chanumber.cn/',
#               'm.jihaoba.com']
white_list=['www','m','mip','jx','gd','ads','search','ws','noads','call','so','fj','bj','en']
white_list1= ['gouhaowang',
               'kuz8',
             'chazidian',
             'jihaoba',
             'beyaiw',
             'chazidian',
             'xingming',
             'pianziweb',
			 'sjgsd',
             'guisd',
             'gududai',
             '138023',
             'bole',
             '04338888',
             'shouji',
             'rmbhuilv',
             'xysjk',
             'juzhao',
             'sjhcx',
             'mshaoma',
             'sojkan',
             'taohaoba',
             'fpsjk',
             'aiqzu',
             'taohaoma',
			 'haoma',
             '38hp',
             'ip138',
             'kuaichala',
             'tiaohao',
             '135cha',
             'tiaohao',
             '51so',
             'yenwoop',
             'baishicha',
             'shoujicha',
             'chahaoba',
             'qeayer',
             'jctnsy',
             'gdhaoma',
             'danhaoma',
             'yenwoop',
             'stl56',
             'bixinshui',
             'bixinshui',
             '8684',
             'tiantianxie',
             'herowind',
             'xy1989',
             '135130',
             'chanumber',
             'jihaoba',
             '51hao',
             'shoujichahao',
			 'rrsms',
			 'phone001',
			 'tiantianxieye',
			 '13677777',
			 'hmjx',
			 'tongmengguo',
			 'haomais',
			 'qqdna',
             'reg007',
             'chachahaoma',
             'xxruiyi',
             'wzyzlm',
             'verysource',
             'mobizc',
             '10865',
             'wzsswlkj','57cha','cqcounter','haolingba','026hao','vinwan','hn','99haoling','anxin360','mai','hepan'
			 ,'cryptofresh','13131313131','2dianying','dom','wncx','ekaing','computers-online','0551lhw','verysource']


import ctypes
from random import sample
from pprint import pformat

test_conditions = ['Saline', 'CNO', 'Baseline']

mice = {
    mouse: sample(test_conditions, len(test_conditions))
    for mouse in range(1, 11)
}

pretty_mice = ' ' + pformat(mice)[1:-1]

def Mbox(title, text, style):
    return ctypes.windll.user32.MessageBoxW(0, text, title, style)

Mbox('Random Mice Test Condition Results', pretty_mice, 1)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 11 22:16:20 2020

@author: sahebsingh
"""

from util import *
import numpy as np
from scipy.linalg import block_diag

def buildGadget(l, n):
    # the secret vector [s] is an (n-1)-dimensional vector,
    # the secret key [t] is -s‖1, an n-dimensional vector
    #
    # the error vector [e] is an m-dimensional vector
    #
    # the matrix [A] is an (n-1)×m matrix (n-1 rows, m = n×l columns) #
    # the public key [B] is ( A ) which is an n×m matrix
    # ( sA+e )
    #
    g = 2**np.arange(l)
    return block_diag(*[g for null in range(n)])

def encrypt(keys, message):
    stat("Encrypting message")
    #
    # the gadget matrix [G] is an n×m matrix (n rows, m = n×l columns)
    #
    # the matrix R is an m×m matrix (n×l rows, n×l columns)
    #
    # the ciphertext is (n×m)⋅(m×m) *> an n×m matrix
    #
    R = np.random.randint(2, size=(keys.m, keys.m), dtype=np.int64).astype(keys.datatype)
    G = buildGadget(keys.l, keys.n)
    return (np.dot(keys.PK, R) + message*G)%keys.q


'''
Si definiscono divisori propri di un numero tutti i suoi divisori tranne l'uno e il numero stesso.
Scrivere una funzione modi(ls,k) che, presa una lista ls di interi  ed un intero
non negativo k:
    1) cancella  dalla lista ls gli interi che non hanno esattamente k divisori propri
    2) restituisce una seconda lista che contiene i soli numeri primi di ls.
NOTA: un numero maggiore di 1 e' primo se ha 0 divisori propri.

ad esempio per ls = [121, 4, 37, 441, 7, 16]
modi(ls,3) restituisce la lista con i numeri primi [37,7] mentre al termine della funzione si avra' che la lista ls=[16]

Per altri  esempi vedere il file grade.txt

ATTENZIONE: NON USATE LETTERE ACCENTATE.
ATTENZIONE: Se il grader non termina entro 30 secondi il punteggio dell'esercizio e' zero.
'''

import math
def modi(ls,k):
    lsp=[]
    count=0

    for i in range(len(ls)-1, -1, -1):
        for h in range(2,int(math.sqrt(ls[i]))+1):
            if(ls[i]%h==0):
                count+=2
        #print(count)
        #print(ls[i])

        if(count==0):
            lsp.append(ls[i])
        if(k!=count):
            del ls[i]

        count=0
    return lsp[::-1] #reverse solo perche' il grader non dava giusto il risultato poiche' invertito

ls=[1234579,1234604,1234613,1234641,1234684,1234687,1234793,1234836,1234837,1234847]
print(modi(ls,6))
print(ls)


import numpy as np
import matplotlib.pyplot as plt

class LogisticRegression(object):
    '''
    A logistic regression classifier.
    '''

    def __init__(self):
        self.W = None

    def train(self, X_train, y_train, num_batch = 0, learning_rate = 0.1, num_iters = 1000):
        '''
        Train the model and store weights in self.X.

        :param num_batch: If num_batch == 0 using gradient descent to train the model,
            else using stochastic gradient descent to train the model.
        :param X_train: A numpy array of shape (num_train, D) containing
            training data, where num_train is the number of training examples
            and D is the dimension of each sample.
        :param y_train: A numpy array of shape (num_train,) containing the
            labels of training data, where y_train[i] is the label of X_train[i].

        :return: None
        '''
        num_train = X_train.shape[0]
        if num_batch > num_train:
            raise Exception('The number of batch is greater than the number of training examples.')
        elif num_batch == 0:
            self.__gradient_descent(X_train, y_train, learning_rate, num_iters)
        else:
            indices_batch = np.random.choice(num_train, num_batch, replace = False)
            X_batch = X_train[indices_batch]
            y_batch = y_train[indices_batch]
            self.__gradient_descent(X_batch, y_batch, learning_rate, num_iters)

    def __gradient_descent(self, X_train, y_train, learning_rate, num_iters):
        '''
        Train the model using gradient descent.

        Parameters and return result are same as train.
        '''
        num_train, D = X_train.shape
        weights = np.ones(D)
        for i in range(num_iters):
            grad = X_train.T.dot(self.__sigmoid(X_train.dot(weights)) - y_train) / num_train
            weights -= learning_rate * grad
        self.W = weights

    def predict(self, X_test):
        '''
        Predict the labels of test data.

        :param X_test: A numpy array of shape (num_test, D) containing
            the test data.
        :return y_pred: A numpy array of shape (num_test,) containing the
            labels predicted where y_pred[i] is the predicted label of X_test[i].
        '''
        scores = self.__sigmoid(X_test.dot(self.W))
        y_pred = np.array([1 if x >= 0.5 else 0 for x in scores])
        return y_pred

    def test(self, X_test, y_test):
        '''
        Test according the trained weights.
        Print the number of test cases, the number of test cases predicted error
            and the error rate.

        :param X_test: A numpy array of shape (num_test, D) containing test data.
        :param y_test: A numpy array of shape (num_test,) containing the labels of test data.
        :return: None
        '''
        y_pred = self.predict(X_test)
        num_test = X_test.shape[0]
        num_error = 0
        for i in range(num_test):
            if y_test[i] == y_pred[i]:
                #print('Test case', i + 1, 'predict correct.')
                pass
            else:
                #print('Test case', i + 1, 'predict error.')
                num_error += 1
        print('test: %d, error: %d, error rate: %f %%' % (num_test, num_error, 100.0 * num_error / num_test))

    def __sigmoid(self, x):
        return 1.0 / (1.0 + np.exp(-x))


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2020 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Ebuild function generator to migrate src_install into GN.

Default values follow the default values of ebuild (see manpage of ebuild).
https://dev.gentoo.org/~zmedico/portage/doc/man/ebuild.5.html
"""

from __future__ import print_function

import os

VALID_INSTALL_TYPES = ('bin', 'ins', 'lib.a', 'lib.so', 'sbin')

class EbuildFunctionError(Exception):
  """The base exception for ebuild_function."""

class InvalidInstallTypeError(EbuildFunctionError):
  """Invalid type exception that is raised when install type is invalid."""
  def __init__(self):
    message = f'install_type must be {", ".join(VALID_INSTALL_TYPES)}'
    super().__init__(message)

def generate(sources, install_path=None, outputs=None, symlinks=None,
             recursive=False, options=None, command_type=None):
  """Generates commandlines for installing files using a ebuild function.

  Args:
    sources: A list of source files to be installed.
    install_path: A string of path to install into. When both install_path and
      symlinks are specified, it joins a install_path to symlinks.
      When command_type is "executable", "shared_library" or
      "static_library", install_path must end with "bin", "sbin" or "lib".
    outputs: A list of new file names to be installed as. If not specified,
      original file names are used.
    symlinks: A list of new symbolic links to be created. If specified,
      installation command becomes "dosym" and args except for sources are
      ignored.
    recursive: A boolean if you install them recursively. This is only
      available when command_type and symlinks are not specified.
    options: A string to be passed to xxopts. This is only available when
      command_type and symlinks are not specified.
    command_type: A string of where is the config defined.
      "executable", "shared_library", "static_library" and None are only
      allowed to be specified.
      The generated command depends on command_type.
        executable: dobin, dosbin, newbin, newsbin
        shared_library: dolib.so, newlib.so
        static_library: dolib.a, newlib.a
        None: doins, newins, dosym

  Returns:
    A list of commandlines correspond to given args.
    It can generate "doins", "dobin", "dosbin", "dolib.a", "dolib.so" and
    "dosym". When "outputs" is specified, it generates new-command of those
    except for "dosym".
    doins:
    [
      ['insinto', 'path/to/install'],
      ['insopts', '-m0644'],
      ['doins', 'sources[0]', 'sources[1]', ...],
    ]
    dobin):
    [
      ['into', 'path/to/install'],
      ['dobin', 'sources[0]', 'sources[1]', ...],
    ]
    dosym:
    [
      ['dosym', 'sources[0]', 'path/to/symlink[0]'],
      ['dosym', 'sources[1]', 'path/to/symlink[1]'],
      ...
    ]
    dolib.a and dolib.so is the same as dobin.
    When "outputs" are specified, installation commands change to multiple
    commands of "newxxx" like dosym.
  """
  if not command_type:
    if not symlinks:
      install_type = 'ins'
    else:
      install_type = 'sym'
      if install_path:
        outputs = [os.path.join(install_path, symlink) for symlink in symlinks]
      else:
        outputs = symlinks

  elif command_type == 'executable':
    install_path, install_type = os.path.split(install_path)
    assert install_type in ('bin', 'sbin'), ('install_path must end in bin'
                                             ' or sbin in an executable target')

  elif command_type == 'shared_library':
    install_path, lib = os.path.split(install_path)
    assert lib == 'lib', ('install_path must end in lib in a shared_library'
                          ' target')
    install_type = 'lib.so'

  elif command_type == 'static_library':
    install_path, lib = os.path.split(install_path)
    assert lib == 'lib', ('install_path must end in lib in a static_library'
                          ' target')
    install_type = 'lib.a'
  else:
    raise AssertionError('unknown type. type must be executable,'
                         ' shared_library or static_library')
  cmd_list = option_cmd(install_type, install_path, options)
  cmd_list += install(install_type, sources, outputs, recursive)
  return cmd_list

def sym_install(sources, symlinks):
  """Generates "dosym" commandlines.

  Args:
    sources: A list of source files of symbolic links.
    symlinks: A list of symbolic links to be created.

  Returns:
    A list of commandlines of "dosym".
    [
      ['dosym', 'sources[0]', 'symlinks[0]'],
      ['dosym', 'sources[1]', 'symlinks[1]'],
      ...
    ]
  """
  assert len(sources) == len(symlinks), ('the number of symlinks must be the'
                                         ' same as sources')
  return [['dosym', source, symlink]
          for source, symlink in zip(sources, symlinks)]

def option_cmd(install_type, install_path='', options=None):
  """Generates commandlines of options appropriate for the |install_type|.

  Args:
    install_type: A string of a suffix of an installation command.
    install_path: A string of path to be installed into. This is passed to
      "xxxinto" commands.
    options: A string of options of installation. This is available only when
      install_type == "ins". This is passed to "xxxopts".

  Returns:
    A list of commandlines for specifying options.
    doins options (install_type == "ins"):
    [
      ['insinto', 'path/to/install'],
      ['insopts', '-m0644'],
    ]
    dobin, dosbin, dolib.so, dolib.a options:
    [
      ['into', 'path/to/install']
    ]
  """
  if install_type == 'ins':
    return [
        ['insinto', install_path or '/'],
        ['insopts', options or '-m0644'],
    ]
  if install_type in VALID_INSTALL_TYPES:
    return [['into', install_path or '/usr']]
  return []

def install(install_type, sources, outputs=None, recursive=False):
  """Generates commandlines for installation.

  When |outputs| is specified, it generates new command.

  Args:
    install_type: A string of a suffix of an installation command.
    sources: A list of source files to be installed.
    outputs: A list of new file names to be installed as. If not specified,
      original file names are used.
    recursive: A boolean if you install them recursively. This is available
      only when install_type == "ins" and outputs are not specified.

  Returns:
    A list of commandlines for installation.
  """
  if install_type == 'sym':
    return sym_install(sources, outputs)
  if not outputs:
    return do_command(install_type, sources, recursive)
  return new_command(install_type, sources, outputs)

def do_command(install_type, sources, recursive=False):
  """Generates commandlines of do-command.

  Args:
    install_type: A string of a suffix of an installation command.
      "ins", "bin", "sbin", "lib.so" and "lib.a" are allowed.
    sources: A list of source files to be installed.
    recursive: A boolean if you install them recursively. This is available
      only when install_type == "ins".

  Returns:
    A list of commandlines for installation.
    [
      ['dobin', 'sources[0]', 'sources[1]', ...]
    ]

    Especially, when install_type == "ins" and recursive == true:
    [
      ['doins', '-r', 'sources[0]', 'sources[1]', ...]
    ]
  """
  if install_type not in VALID_INSTALL_TYPES:
    raise InvalidInstallTypeError()
  recursive_opts = []
  if install_type == 'ins' and recursive:
    recursive_opts = ['-r']
  return [['do%s' % install_type] + recursive_opts + sources]

def new_command(install_type, sources, outputs):
  """Generates commandlines of new-command.

  Args:
    install_type: A string of a suffix of an installation command.
      "ins", "bin", "sbin", "lib.so" and "lib.a" are allowed.
    sources: A list of source files to be installed.
    outputs: A list of new file names to be installed as.

  Returns:
    A list of commandlines for installation.
    [
      ['newins', 'sources[0]', 'outputs[0]'],
      ['newins', 'sources[1]', 'outputs[1]'],
      ...
    ]
  """
  if install_type not in VALID_INSTALL_TYPES:
    raise InvalidInstallTypeError()
  assert len(sources) == len(outputs), ('the number of outputs must be the same'
                                        ' as sources')
  return [['new%s' % install_type, source, output]
          for source, output in zip(sources, outputs)]


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 20 18:35:13 2017

@author: ajaver
"""
import tables
import numpy as np
import pandas as pd
import os
from scipy.io import loadmat

from tierpsy.analysis.stage_aligment.alignStageMotion import _h_get_stage_inv

tab_path = os.path.join(os.path.dirname(__file__), 'conversion_table.csv')
feats_conv = pd.read_csv(tab_path).dropna()
FEATS_MAT_MAP = {row['feat_name_tierpsy']:row['feat_name_segworm'] for ii, row in feats_conv.iterrows()}
FEATS_OW_MAP = {row['feat_name_tierpsy']:row['feat_name_openworm'] for ii, row in feats_conv.iterrows()}

def test_mismatching():
    for x in FEATS_MAT_MAP:
        mat = FEATS_MAT_MAP[x]
        ow = FEATS_OW_MAP[x]

        mat_r = mat.replace('/worm/', '').replace('/','.')
        if mat_r != ow:
            d1 = mat_r.split('.')
            d2 = ow.split('.')

            missed = [x for x in d1 if not x in d2]

            print(missed, ow, mat)

class FeatsReader():
    def __init__(self, feat_file):
        self.feat_file = feat_file

    @property
    def features_events(self):
        try:
            return self._features_events
        except:
            with tables.File(self.feat_file, 'r') as fid:

                features_events = {}
                node = fid.get_node('/features_events')
                for worn_n in node._v_children.keys():
                    worm_node = fid.get_node('/features_events/' + worn_n)

                    for feat in worm_node._v_children.keys():
                        if not feat in features_events:
                            features_events[feat] = {}
                        dat = fid.get_node(worm_node._v_pathname, feat)[:]
                        features_events[feat][worn_n] = dat

            self._features_events = features_events
            return self._features_events

    @property
    def features_events_plate(self):
        def dict2array(dd):
            return np.concatenate([val for val in dd.values()])
        return {feat:dict2array(val) for feat, val in self.features_events.items()}

    @property
    def features_timeseries(self):
        try:
            return self._features_timeseries
        except:
            with pd.HDFStore(self.feat_file, 'r') as fid:
                self._features_timeseries = fid['/features_timeseries']
            return self._features_timeseries

    def read_plate_features(self):
        dd = {x:self.features_timeseries[x].values for x in self.features_timeseries}
        worm_features_dict = {**dd, **self.features_events_plate}
        return worm_features_dict

    def get_worm_coord(self, worm_index, field_name):
        good_rows = self.features_timeseries['worm_index'] == worm_index
        rows_indexes = good_rows.index.values[good_rows.values]

        with tables.File(self.feat_file, 'r') as fid:
            skeletons = fid.get_node('/coordinates/' + field_name)[rows_indexes, : , :]

        #shift to correct using the timestamp
        inds = self.features_timeseries.loc[rows_indexes, 'timestamp'].astype(np.int)
        tot = inds.max() + 1

        #import pdb
        #pdb.set_trace()

        if tot != skeletons.shape[0]:
            skeletons_ts = np.full((tot, *skeletons.shape[1:]), np.nan)
            skeletons_ts[inds] = skeletons
            return skeletons_ts
        else:
            return skeletons

class FeatsReaderComp(FeatsReader):
    def __init__(self, feat_file, segworm_feat_file='', skel_file=''):
        if not segworm_feat_file:
            segworm_feat_file = feat_file.replace('.hdf5', '.mat')

        if not skel_file:
            skel_file = feat_file.replace('_features.hdf5', '_skeletons.hdf5')

        self.skel_file = skel_file
        self.segworm_feat_file = segworm_feat_file
        super().__init__(feat_file)

    def _read_feats_segworm_hdf5(self):
        with tables.File(self.segworm_feat_file, 'r') as fid:
            feats_segworm = {}
            for name_tierpsy, name_segworm in FEATS_MAT_MAP.items():
                if name_segworm in fid:
                    if not 'eigenProjection' in name_segworm:
                        dd = fid.get_node(name_segworm)
                        if dd != np.dtype('O'):
                            feats_segworm[name_tierpsy] = dd[:]
                        else:
                            if len(dd) == 1:
                                dd = dd[0]
                            feats_segworm[name_tierpsy]=np.array([x[0][0,0] for x in dd])

                    else:
                        ii = int(name_tierpsy.replace('eigen_projection_', '')) - 1
                        feats_segworm[name_tierpsy] = fid.get_node(name_segworm)[:, ii]
                else:
                    feats_segworm[name_tierpsy] = np.array([np.nan])

            for key, val in feats_segworm.items():
                feats_segworm[key] = np.squeeze(val)

        return feats_segworm

    def _read_feats_segworm_mat(self):
        dat  = loadmat(self.segworm_feat_file)
        feats_segworm = {}
        for name_tierpsy, name_segworm in FEATS_MAT_MAP.items():
            prev = dat
            for field in name_segworm.split('/'):

                if isinstance(prev, (np.ndarray, np.void)):

                    ff = prev.dtype.names
                    if not ff is None:
                        has_field = field in ff
                    else:
                        has_field = False
                else:
                    has_field = field in prev

                if has_field:
                    prev = prev[field]
                    if prev.size == 1 and prev.shape == (1,1):
                       prev = prev[0,0]

#                if name_tierpsy == 'inter_backward_distance':
#                    print(field)
#                    import pdb
#                    pdb.set_trace()

            if 'eigen_projection_' in name_tierpsy:
                ii = int(name_tierpsy.replace('eigen_projection_', '')) - 1
                prev = prev[ii, :]

            if isinstance(prev, (np.float64, float)):
                prev = np.atleast_1d(np.array(prev))
            elif isinstance(prev, np.ndarray):
                prev = np.squeeze(prev)
                if prev.size == 0:
                    prev = np.nan
                elif prev.dtype == np.dtype('O'):
                    prev = prev.astype(np.float)
                else:
                    if prev.size < 100:
                        raise ValueError

            feats_segworm[name_tierpsy] = prev

        return feats_segworm

    def read_feats_segworm(self):
        try:
            feats_segworm = self._read_feats_segworm_hdf5()
        except:
            feats_segworm = self._read_feats_segworm_mat()

        return feats_segworm

    def _read_skels_segworm_hdf5(self):
        with tables.File(self.segworm_feat_file, 'r') as fid:
            segworm_x = fid.get_node('/worm/posture/skeleton/x')[:]
            segworm_y = fid.get_node('/worm/posture/skeleton/y')[:]
        return segworm_x, segworm_y

    def read_skels_segworm_mat(self):
        dat  = loadmat(self.segworm_feat_file)
        segworm_x = dat['worm'][0,0]['posture'][0,0]['skeleton'][0,0]['x']
        segworm_y = dat['worm'][0,0]['posture'][0,0]['skeleton'][0,0]['y']

        return segworm_x.T, segworm_y.T

    @property
    def skels_segworm(self):
        try:
            return self._skels_segworm
        except:
            #load segworm data
            try:
                segworm_x, segworm_y = self._read_skels_segworm_hdf5()
            except:
                segworm_x, segworm_y = self.read_skels_segworm_mat()

            skel_segworm = np.stack((-segworm_x,-segworm_y), axis=2)
            skel_segworm = np.rollaxis(skel_segworm, 0, skel_segworm.ndim)
            skel_segworm = np.asfortranarray(skel_segworm)

            self._skels_segworm = np.rollaxis(skel_segworm, 2, 0)

            return self._skels_segworm

    @property
    def skeletons(self):
        try:
            return self._skeletons
        except:
            #load segworm data
            self._skeletons = self.get_worm_coord(1, 'skeletons')
            return self._skeletons

    def read_skeletons(self):
        return self._align_skeletons(self.skel_file, self.skeletons, self.skels_segworm)

    def _align_skeletons(self, skel_file, skeletons, skel_segworm):
            #load rotation matrix to compare with the segworm
            with tables.File(skel_file, 'r') as fid:
                rotation_matrix = fid.get_node('/stage_movement')._v_attrs['rotation_matrix']

                microns_per_pixel_scale = fid.get_node(
                        '/stage_movement')._v_attrs['microns_per_pixel_scale']

            # rotate skeleton to compensate for camera movement
            dd = np.sign(microns_per_pixel_scale)
            rotation_matrix_inv = np.dot(
                rotation_matrix * [(1, -1), (-1, 1)], [(dd[0], 0), (0, dd[1])])
            for tt in range(skel_segworm.shape[0]):
                skel_segworm[tt] = np.dot(rotation_matrix_inv, skel_segworm[tt].T).T

            max_n_skel = min(skel_segworm.shape[0], skeletons.shape[0])
            #shift the skeletons coordinate system to one that diminushes the errors the most.
            dskel = skeletons[:max_n_skel]-skel_segworm[:max_n_skel]
            seg_shift = np.nanmedian(dskel, axis = (0,1))
            skel_segworm += seg_shift

            return skeletons, skel_segworm

    @property
    def stage_movement(self):
        try:
            return self._stage_movement
        except:
            timestamp = np.arange(self._skeletons.shape[0])
            self._stage_movement, _ = _h_get_stage_inv(self.skel_file, timestamp)
            return self._stage_movement


"""add_timestamp_related column in ProductReleaseInfo

Revision ID: bab26a6e0ae2
Revises: 3057c19a2f4f
Create Date: 2021-04-22 05:45:14.676499

"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = 'bab26a6e0ae2'
down_revision = '3057c19a2f4f'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('product_release_info', sa.Column('created_at', sa.TIMESTAMP(),
                  server_default=sa.func.current_timestamp(), nullable=False))
    op.add_column('product_release_info', sa.Column('updated_at', sa.TIMESTAMP(),
                  server_default=sa.func.current_timestamp(), nullable=False))
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('product_release_info', 'updated_at')
    op.drop_column('product_release_info', 'created_at')
    # ### end Alembic commands ###


"""
Take a list, say for example this one:

      a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
      and write a program that prints out all the elements of the list that
      are less than 5.

      Extras:

          1. Instead of printing the elements one by one, make a new list that
          has all the elements less than 5 from this list in it and print out
          this new list.
          2. Write this in one line of Python.
          3. Ask the user for a number and return a list that contains only
          elements from the original list a that are smaller than that number
          given by the user.
"""

a_list = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
new_list = []

user_num = int(raw_input('Enter a number: '))

for num in a_list:
    if num < user_num:
        new_list.append(num)

print(new_list)

# Writing the same in one line
my_list = [num for num in a_list if num < user_num]
print(my_list)


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

import numpy as np
import pandas as pd
import xlwings as xw
import os

wb = xw.Book('result.xlsm')
sht = wb.sheets('业绩数据')
start_date = sht.range('C1').value
end_date = sht.range('C2').value

dir_path = os.path.dirname(wb.fullname)
data = pd.read_excel(dir_path + '\\weekly_return.xlsx', header=0)

def get_month_rtn(df):
    sub_data = data[data['fund_name'] == df.fund_name.min()]
    nav1 = df[df['date'] == df['date'].min()]['last_nav'].max()
    if df['date'].min() > sub_data['date'].min():
        date1 = df['date'].min() - pd.Timedelta(days=7)
        while sub_data[sub_data['date'] == date1].size == 0:
            date1 = date1 - pd.Timedelta(days=7)
        nav1 = sub_data[sub_data['date'] == date1]['nav'].max()

    nav2 = df[df['date'] == df['date'].max()]['nav'].max()
    return nav2/nav1 - 1

def get_std_a(df):
    return np.std(df['weekly_return'])*np.sqrt(52)

def get_max_retra(df, start_date):
    nav_list = list(df['nav'])
    if (df['date'].min() - pd.Timedelta(days=7)) >= start_date:
        nav_list.insert(0, df['last_nav'][df.index[0]])

    peak_list = [max(nav_list[:i+1]) for i in range(len(nav_list))]
    return ((pd.Series(nav_list) / pd.Series(peak_list)) - 1).min()

def main_fun():
    sub_data = data[(data['date'] >= start_date) & (data['date'] < end_date)]
    # filter funds with less than 3 records in a month
    grouped = sub_data.groupby('fund_name')
    name_list = (grouped.apply(len) >= 3)[(grouped.apply(len) >= 3)].index
    sub_data = sub_data[sub_data['fund_name'].isin(name_list)]
    # calculate annual return, annual std, sharpe ratio, max retracement
    grouped = sub_data.groupby('fund_name')

    cate = grouped['type'].apply(min)
    rtn_m = grouped.apply(lambda x: get_month_rtn(x))
    rtn_a = rtn_m.apply(lambda x: (1+x)**(12) - 1)
    std_a = grouped.apply(get_std_a)
    sharpe = (rtn_a - 0.015)/std_a
    max_retra = grouped.apply(lambda x: get_max_retra(x, start_date))
    result = pd.DataFrame({'type':cate, 'annual_return':rtn_a, 'annual_std':std_a, 'sharpe':sharpe, 'max_retra':max_retra, 'month_return':rtn_m},
                      index = rtn_a.index)
    print(result)
    sht.range('A3').expand().clear_contents()
    sht.range('A3').value = result

main_fun()

code for work in CICC/week_to_all_time.py


# -*- coding: utf-8 -*-
"""
Created on Tue Jul 31 12:29:01 2018

@author: Student
"""
from __future__ import division
#function to find mean
def find_mean(mylist):
    s = sum(mylist) #finding sum of list
    print('Mean')
    m = s/len(mylist)
    print(m)
#function to find mode
def mode(l):
    if len(l)>1:
        d = {}
    for value in l:
        if value not in d:
            d[value] = 1
        else:
            d[value] += 1
    if len(d) == 1:
        print(value)
    else:
        i = 0

        for key,value in d.items():
            if i < value:
                i = value

        print('Modes are')
        #If more than one mode is present
        for key,value in d.items():
            if i == value:
                print(key)
#function to find median
def find_median(l):
    #sort the list
    l = sorted(l)
    n = len(l)
    mid = n//2 #to get integer value as mid as it will be index to the list
    if n%2 == 0:
        return(l[mid] + l[mid-1])/2
    else:
        return(l[mid])
l = [20, 22, 22, 56, 56,79] #declaring list
#function calls
mode(l)
find_mean(l)
med = find_median(l)
print('Median',med)


__license__ = 'Nathalie (c) EPITA'
__docformat__ = 'reStructuredText'
__revision__ = '$Id: prefixTrees.py 2018-02-14'

"""
Prefix Trees homework
2018
@author: pierrick.made
"""

from algopy import tree

##NOTES for me : ...
## 1. Use binary search instead of linear ! if time is available for it <3
## binsearch(L)
## 2. possibility of using [1:] instead of word and position !

###############################################################################
## MEASURES
def _binsearch(T, x):
    l, r = 0, T.nbchildren
    while l < r:
        pivot = l + (r-l)/2
        if T.children[pivot] == x:
            return pivot
        elif T.children[pivot] > x:
            l = pivot
        else:
            r = pivot - 1
    return r

def countwords(T):
    """ count words in dictionnary T

    :param T: The prefix tree
    :rtype: int
    :Example:
    >>> countwords(Tree1)
    11
    """
    nbWords = 0
    for child in T.children:
        nbWords += countwords(child)
    return nbWords + T.key[1]

def longestwordlength(T):
    """ longest word length

    :param T: The prefix tree
    :rtype: int
    :Example:
    >>> longestwordlength(Tree1)
    6
    """
    lengthmax = -1;
    for child in T.children:
        lengthmax = max(lengthmax, longestwordlength(child))
    return lengthmax+1

def _averagelength(T, h):
    sumLength, nbWords = 0, 0
    for child in T.children:
        s, n = _averagelength(child, h+1)
        sumLength += s
        nbWords += n
    if T.key[1]:
        sumLength += h
        nbWords += 1
    return sumLength, nbWords

def averagelength(T):
    """ average word length

    :param T: The prefix tree
    :rtype: float
    :Example:
    >>> averagelength(Tree1)
    4.636363636363637
    """
    s, n = _averagelength(T,0)
    return s/n if n else 0

###############################################################################
## Researches
def _searchword(T, word, position, length):
    for child in T.children:
        if child.key[0] == word[position]:
            if position==length-1: #last char
                return child.key[1]
            return _searchword(child, word, position+1, length)
        if child.key[0] > word[position]: #supposing the tree sorted (a->z)
            return False
    return False

def searchword(T, word):
    """ search for a word in dictionary

    :param T: The prefix tree
    :param word: The word to search for
    :rtype: bool
    :examples:
    >>> searchword(Tree1, "fabulous")
    False
    >>> searchword(Tree1, "Famous")
    True
    """
    if word=="":
        return T.key == ('', True)
    return _searchword(T, word.lower(), 0, len(word))

###############################################################################
## Lists

def wordlist(T):
    """ generate the word list

    :param T: The prefix tree
    :rtype: list
    :example:
    >>> print(wordlist(Tree1))
    ['case', 'cast', 'castle', 'circle', 'city', 'come',
    'could', 'fame', 'famous', 'fan', 'fancy']
    """
    List = []
    if T.key[1]:
        List.append(T.key[0])
    for child in T.children:
        wordsOfChild = wordlist(child)
        for wordOfChild in wordsOfChild:
            List.append(T.key[0] + wordOfChild)
    return List

def longestwords(T):
    """ search for the longest words in dictionary

    :param T: The prefix tree
    :rtype: list
    :examples: # order in result does not matter
    >>> longestwords(Tree1)
    ['castle', 'circle', 'famous']
    """
    longestwords = []
    lengthLongest = 0
    for word in wordlist(T):
        wordLength = len(word)
        if wordLength == lengthLongest:
            longestwords.append(word)
        elif wordLength > lengthLongest:
            longestwords = [word]
            lengthLongest = wordLength
    return longestwords

def _searchPrefixTree(T, word, position, length):
    for child in T.children:
        if child.key[0] == word[position]:
            if position==length-1: #last char
                return child
            return _searchPrefixTree(child, word, position+1, length)
        if child.key[0] > word[position]: #supposing the tree sorted (a->z)
            return None
    return None

def completion(T, prefix):
    """ generate the list of words with a common prefix

    :param T: The prefix tree
    :param pref: the prefix
    :rtype: list
    :examples: # result elements can have different case...
    >>> completion(Tree1, "Fan")
    ['Fan', 'Fancy']
    >>> completion(Tree1, "CI")
    ['Circle', 'City']
    >>> completion(Tree1, "what")
    []
    """
    prefix = prefix.lower()
    if prefix=="":
        return wordlist(T)
    T = _searchPrefixTree(T, prefix, 0, len(prefix))
    L = []
    if T is not None:
        for word in wordlist(T):
            L.append(prefix[:len(prefix)-1] + word)
    return L

def treetofile(T, filename):
    """ save the dictionary in a file

    :param T: The prefix tree
    :param filename: the file name
    :example:
    >>> treeToFile(Tree1, "test.txt")
    # give the same file as "textFiles/wordList1.txt" but in alphabetic order
    """
    f = open(filename, "w")
    for word in wordlist(T):
        f.write(word + "\n")
    f.close()

###############################################################################
## Build Tree

def addword(T, word):
    """ add a word in dictionary

    :param T: The prefix tree
    :param word: The word to add
    """
    if T.key[0] == '' and word == "": # want to insert the null word as a word
        T.key = ('',True)
    if not word=="":
        isLastChar = (len(word) == 1)
        i = 0
        while i < T.nbchildren and T.children[i].key[0] < word[0]:
            i += 1
        if i == T.nbchildren:
            newTree = tree.Tree((word[0], isLastChar))
            addword(newTree, word[1:])
            T.children.append(newTree)
        elif T.children[i].key[0] == word[0]:
            addword(T.children[i], word[1:])
            T.children[i].key[1] == isLastChar
        else:
            newTree = tree.Tree((word[0], isLastChar))
            addword(newTree, word[1:])
            T.children[i:i] = [newTree] # insertion of newTree at index i

def treefromfile(filename):
    """ build the prefix tree from a file of words

    :param filename: The file name
    :rtype: Tree
    """
    T = tree.Tree(('',False))
    f = open(filename, "r")
    for word in f.readlines():
        word = word.lower().strip()
        addword(T, word)
    f.close()
    return T


"""This module implements a simple spindown model for an isolated pulsar.
"""
# spindown.py
# Defines Spindown timing model class
import numpy
import astropy.units as u
try:
    from astropy.erfa import DAYSEC as SECS_PER_DAY
except ImportError:
    from astropy._erfa import DAYSEC as SECS_PER_DAY
import parameter as p
from .timing_model import TimingModel, MissingParameter
from ..phase import *
from ..utils import time_from_mjd_string, time_to_longdouble, str2longdouble, taylor_horner,\
                    time_from_longdouble

class Spindown(TimingModel):
    """This class provides a simple timing model for an isolated pulsar."""
    def __init__(self):
        super(Spindown, self).__init__()

        # The number of terms in the taylor exapansion of spin freq (F0...FN)
        #self.num_spin_terms = maxderivs

        self.add_param(p.floatParameter(name="F0", value=0.0, units="Hz",
                       description="Spin-frequency", long_double=True))

        self.add_param(p.prefixParameter(name="F1", value=0.0, units='Hz/s^1',
                       description="Spindown-rate",
                       unitTplt=self.F_unit,
                       descriptionTplt=self.F_description,
                       type_match='float',long_double=True))

        self.add_param(p.MJDParameter(name="TZRMJD",
                       description="Reference epoch for phase = 0.0",
                       time_scale='tdb'))

        self.add_param(p.MJDParameter(name="PEPOCH",
                       description="Reference epoch for spin-down",
                       time_scale='tdb'))

        self.phase_funcs += [self.spindown_phase,]

    def setup(self):
        super(Spindown, self).setup()
        # Check for required params
        for p in ("F0",):
            if getattr(self, p).value is None:
                raise MissingParameter("Spindown", p)

        # Check continuity
        F_terms = self.get_prefix_mapping('F').keys()
        F_terms.sort()
        F_in_order = range(1,max(F_terms)+1)
        if not F_terms == F_in_order:
            diff = list(set(F_in_order) - set(F_terms))
            raise MissingParameter("Spindown", "F%d"%diff[0])
        # If F1 is set, we need PEPOCH
        if self.F1.value != 0.0:
            if self.PEPOCH.value is None:
                raise MissingParameter("Spindown", "PEPOCH",
                        "PEPOCH is required if F1 or higher are set")

        self.num_spin_terms = len(self.get_prefix_mapping('F')) + 1

    def F_description(self, x):
        """Template function for description"""
        if x <1:
            return "Spin-frequency"
        else:
            return "Spin-frequency %d derivative"%x

    def F_unit(self,x):
        """Template function for unit"""
        if x <1:
            return "Hz"
        else:
            return "Hz/s^%d"%x

    def get_spin_terms(self):
        """Return a list of the spin term values in the model: [F0, F1, ..., FN]
        """
        return [getattr(self, "F%d"%ii).value for ii in range(self.num_spin_terms)]

    def spindown_phase(self, toas, delay):
        """Spindown phase function.

        delay is the time delay from the TOA to time of pulse emission
          at the pulsar, in seconds.

        This routine should implement Eq 120 of the Tempo2 Paper II (2006, MNRAS 372, 1549)

        returns an array of phases in long double
        """
        # If TZRMJD is not defined, use the first time as phase reference
        # NOTE, all of this ignores TZRSITE and TZRFRQ for the time being.
        # TODO: TZRMJD should be set by default somewhere in a standard place,
        #       after the TOAs are loaded (RvH -- June 2, 2015)
        # NOTE: Should we be using barycentric arrival times, instead of TDB?
        if self.TZRMJD.value is None:
            self.TZRMJD.value = toas['tdb'][0] - delay[0]*u.s
        # Warning(paulr): This looks wrong.  You need to use the
        # TZRFREQ and TZRSITE to compute a proper TDB reference time.
        if not hasattr(self, "TZRMJDld"):
            self.TZRMJDld = time_to_longdouble(self.TZRMJD.value)

        # Add the [0.0] because that is the constant phase term
        fterms = [0.0] + self.get_spin_terms()

        dt_tzrmjd = (toas['tdbld'] - self.TZRMJDld) * SECS_PER_DAY - delay
        # TODO: what timescale should we use for pepoch calculation? Does this even matter?
        dt_pepoch = (time_to_longdouble(self.PEPOCH.value) - self.TZRMJDld) * SECS_PER_DAY

        phs_tzrmjd = taylor_horner(dt_tzrmjd-dt_pepoch, fterms)
        phs_pepoch = taylor_horner(-dt_pepoch, fterms)
        return phs_tzrmjd - phs_pepoch

    def d_phase_d_F0(self, toas):
        """Calculate the derivative wrt F0"""
        # NOTE: Should we be using barycentric arrival times, instead of TDB?
        # TODO: toas should have units from the table
        tdb = toas['tdbld'].quantity * u.day
        dt_pepoch = time_to_longdouble(self.PEPOCH.value) * u.day
        delay = self.delay(toas) * u.s
        dpdF0 = -(tdb - dt_pepoch) - delay
        return dpdF0.decompose()

    def d_phase_d_F1(self, toas):
        """Calculate the derivative wrt F1"""
        # NOTE: Should we be using barycentric arrival times, instead of TDB?
        # TODO: what timescale should we use for pepoch calculation? Does this even matter?
        tdb = toas['tdbld'] * u.day
        delay = self.delay(toas) * u.s
        dt_pepoch = time_to_longdouble(self.PEPOCH.value) * u.day
        dt = tdb - dt_pepoch - delay
        dpdF1 = -0.5 * dt ** 2
        return dpdF1.decompose()


#!/usr/bin/env python

if __name__ == '__main__':
    import sys
    import unittest

    loader = unittest.TestLoader()
    runner = unittest.runner.TextTestRunner()

    args = sys.argv[1:]
    tests = None

    if args:
        if 'TestCase' in args[0]:
            tests = loader.loadTestsFromNames(args)
        else:
            start_dir = args[0]
    else:
        start_dir = 'tests'

    if tests is None:
        tests = loader.discover(start_dir, 'test_*.py', top_level_dir='tests')

    result = runner.run(tests)

    if result.errors or result.failures:
        sys.exit(1)