import numpy as np
import random
from src.searchable import LocalSearcheable

class Necklace(LocalSearcheable):
    """
    This class is taken from the Necklace Splitting problem, an NP-hard problem (similar to the traveling salesman),
    which is computationally expensive to solve analytically, but can be dealt with effectively through searching
    techniques coming out of randomized algorithms and combinatorics.

    The problem is, given a necklace of finite length, with a finite number of different typse of beads on it, and some
    thieves who stole it, split the necklace in as few places as possible to generate the fairest splitting of the
    beads.

    Ex:

        beads: 0 1 1 0 0 1 1 0 1 1 0 0 1 1 0 1 1 0 0 1 1 0 1 1 0 0 1 1 0 1 1 0 0 1 1 0

          cuts: 0 1 1 | 0 0 1 1 0 1 1 | 0 0 1 1 | 0 1 1 | 0 | 0 1 1 0 | 1 1 0 | 0 1 1 0 1 1 0 | 0 1 1 0

        assign:   1           4            2         3    2      1        2           0           3

        thieves: 5

        thief beads:

            thief 0> 0 1 1 0 1 1 0

            thief 1> 0 1 1 0 1 1 0

            thief 2> 0 0 1 1 0 1 1 0

            thief 3> 0 1 1 0 1 1 0

            thief 4> 0 0 1 1 0 1 1

    This isn't a perfect split, but it is as close as possible with the number of beads and the number of thieves.
    The problem becomes much more complex as the number of beads and thieves goes up.

    A necklace object primarily contains a string of integers, the beads, an integer number of thieves, who will split
    the necklace (the list of beads), a number of cuts in the list of beads, which partition the list of beads into
    sublists, and a list of assignments that assigns each of the partitions of beads to a specific thief.

    Applications of this script in bioinformatics are most obvious in detecting GC rich regions of the genome. This can
    be done by looking at the average score the script produces run for some set time on a region: this will likely be
    able to separate these regions in a new way.
    """
    def __init__(self, beads, num_thieves, max_move=20, cuts=None, assignments=None):
        """Initialize a necklace object, randomly chooses the initial cuts and assignments if none are provided.
        :param beads: the iterable beads, convertible to a list of integers
        :param num_thieves: the number of sublists to create
        :param max_move: the maximum distance to move a cut
        :param cuts: a list of preassigned cuts (for copying or starting searching from a specific point)
        :param assignments: a list of preassigned assignments (for copying or starting searching from a specific point)
        """
        self.beads = list(beads)
        self.num_beads = len(beads)
        unique = np.unique(self.beads)
        self.bead_types = len(unique)
        self.int_beads(unique)
        self.num_thieves = num_thieves
        self.max_move = max_move
        self.num_cuts = (num_thieves - 1) * self.bead_types
        self.cuts = list(cuts) if cuts is not None else self.gen_cuts()
        self.assignments = assignments if assignments is not None else self.gen_assignments()
        self.score = 0
        self.gen_score()

    def __eq__(self, other):
        """Override the default equality test
        :param other: any other object
        :return: boolean whether the two objects are equal based on __dict__ comparisons
        """
        if isinstance(other, self.__class__):
            for k in self.__dict__:
                if not np.array_equal(self.__dict__[k], other.__dict__[k]):
                    return False
            return True
        return NotImplemented

    def __ne__(self, other):
        """Define a non-equality test
        :param other: any other object
        :return: boolean on whether the two objects are equal
        """
        if isinstance(other, self.__class__):
            return not self.__eq__(other)
        return NotImplemented

    def __hash__(self):
        """Override the default hash behavior (that returns the id or the object)
        :return: hash of the sorted items in the dict of this object, used as an identifier instead of object id
        """
        return hash(tuple(sorted(self.__dict__.items())))

    def __str__(self):
        """Override the default string behavior (that returns the id or the object)
        A multiline string in the following format:
        beads (the integer list of beads)
        c>cuts (the integer list of cuts),a>assignments (the integer list of assignments),s> score (the integer score)
        for each thief:
            thief (integer of the thief) > thief beads (the integer list of beads assigned to the thief)
        :return: the string
        """
        obj = str(self.beads) + '\nc>' + str(self.cuts) + ',a>' + str(self.assignments) + ',s>' + str(self.score) + '\n'
        thieves = '\n'.join([str(thief) + '>' + str(self.get_thief_beads(thief)) for thief in range(self.num_thieves)])
        return obj + thieves

    def int_beads(self, unique):
        """Takes any iterable object (e.g. a string or a list) and converts it to a list of integers
        :param unique: the unique items in the string
        """
        # If the beads is a list of ints, return
        if all(isinstance(item, int) for item in self.beads):
            return
        # Create a dict mapping the objects in the list to integers
        to_int = {unique[i]: i for i in range(self.bead_types)}
        # Remap the list of objects to integers
        self.beads = [to_int[bead] for bead in self.beads]

    def gen_cuts(self):
        """At random, generate the cuts that will partition the list of beads
        :return: sorted list of n (where n is the number of cuts) random samples from the range 0 to the number of beads
        """
        # Return the list of cuts
        return sorted(random.sample(list(range(0, self.num_beads)), self.num_cuts))

    def gen_assignments(self):
        """At random, generate the list of integers that assign each partition of beads to a thief
        :return: list of integers assigning each partition of beads to a thief
        """
        # Get a list of zeros for each partition in the beads
        assignments = np.zeros(self.num_cuts + 1)
        # The list of all possible assignments, equivalent to the list of thieves
        all_assignments = np.arange(0, self.num_thieves)
        # The list of possible assignments for the next position (cannot be the same as the last assignment)
        possible_assignments = np.arange(1, self.num_thieves)
        for i in range(1, self.num_cuts + 1):
            # Select a random thief to assign this partition to
            assignments[i] = random.choice(possible_assignments)
            possible_assignments = [a for a in all_assignments if assignments[i] != a]
        # Return the list of assignments
        return assignments

    def get_neighbors(self):
        """Get all of the neighboring states of a necklace object. THe neighboring states are based on the new states
        that can be acchieved by a single move.

        There are two types of valid move for a Necklace, moving a cut (up to max_move indices) and changing an
        assignment. A new cut cannot coincide with any preexisting cuts, and a new assignment cannot be the same as the
        old assignment or as the assignments on either side of the partition being assigned. Allowiing multiple
        consecutive sections to have the same assignment would be the same as removing a cut, and can make it impossible
        to find a perfect solution.

        :return: the list of objects for all neighboring states
        """
        neighbors = []
        # Generate all move cut moves
        for i in range(self.num_cuts):
            # Get the leftmost and rightmost positions the current cut could move to
            pos = self.cuts[i]
            right = pos - self.max_move
            right = right if right > 0 else 0
            left = pos + self.max_move
            left = left if left < self.num_beads else self.num_beads
            # For each position in the range, generate a new state with that position
            for pos in range(right, left):
                # If the position is already a cut, it cannot be the cut of this position
                if pos in self.cuts:
                    continue
                neighbors.append(Necklace(self.beads, self.num_thieves, self.max_move,
                                          cuts=np.array(self.cuts), assignments=np.array(self.assignments)))
                neighbors[-1].cuts[i] = pos
                neighbors[-1].gen_score()
        # Generate all change assignment moves
        for i in range(len(self.assignments)):
            prev = i if i > 0 else 0
            # The list of the previous, current and next assignments to ensure non of them is chosen as the new
            # assignment, which would break the number of cuts and could invalidate the algorithm
            cannot_assign = self.assignments[prev:prev + 3]
            for thief in range(self.num_thieves):
                if thief in cannot_assign:
                    continue
                neighbors.append(Necklace(self.beads, self.num_thieves, self.max_move,
                                          cuts=np.array(self.cuts), assignments=np.array(self.assignments)))
                neighbors[-1].assignments[i] = thief
                neighbors[-1].gen_score()
        return neighbors

    def get_thief_beads(self, thief):
        """Get the beads assigned to a particular thief
        :param thief: integer thief whose beads to return
        :return: integer list of beads
        """
        thief_assignments = self.assignments == thief
        thief_beads = []
        intervals = [0] + self.cuts + [self.num_beads]
        for i in range(len(intervals)-1):
            if thief_assignments[i]:
                thief_beads += self.beads[intervals[i]:intervals[i+1]]
        return thief_beads

    def gen_score(self):
        """Generate the score, the difference in the current state and the ideal, closer to 0 is closer to the ideal
        :return: integer score closer to 0 is closer to the ideal
        """
        score = 0
        # Get the ideal number of beads for each thief to have (it is possible this is a fraction)
        unique, counts = np.unique(self.beads, return_counts=True)
        perfect_bead_split = counts / self.num_thieves
        # For each thief, for each bead type, count the number of beads and sum the difference of this and the ideal
        # number of beads, calculated above
        for thief in range(self.num_thieves):
            thief_beads = self.get_thief_beads(thief)
            thief_unique, thief_counts = np.unique(thief_beads, return_counts=True)
            for i in range(len(unique)):
                if i not in thief_unique:
                    score += 2
                    continue
                where = np.where(thief_unique == i)[0][0]
                score += abs(perfect_bead_split[i] - thief_counts[where])
        self.score = score
        return score


class BasicHashTable:
    def __init__(self, max_size = 4096):
        self.data_list = [None] * max_size

    def insert(self,key,value):
        idx = get_index(self.data_list, key)

        self.data_list[idx] = key, value

    def find(self,key):

        idx = get_valid_index(self.data_list, key)

        kv = self.data_list[idx]
        if kv is None:
            return None
        else:
            key,value = kv
            return value

    def update(self, key, value):
        idx = get_index(self.data_list,key)
        self.data_list[idx] = key,value

    def list_all(self):
        return [kv[0] for kv in self.data_list if kv is not None]

def get_index(data_list, a_string):
    # stores result
    result = 0

    for a_character in a_string:
        # Convert the character to a number
        a_number = ord(a_character)
        #update result by adding the number
        result += a_number

    list_index = result % len(data_list)

    return list_index

def get_valid_index(data_list, key):
    # Start with the index returned by get_index
    idx = get_index(data_list,key)

    while True:
        # Get the key-value pair stored at idx
        kv = data_list[idx]

        # If it is None, return the index
        if kv[0]:
            return idx

        # If the stored key matches the given key, return the index
        k, v = kv
        if k==key:
            return idx

        # Move to the next index
        idx += 1

        # Go back to the start if you have reached the end of the array
        if idx == len(data_list):
            idx = 0


from sklearn.model_selection import train_test_split
import keras
from keras.models import Sequential
from keras.layers import Dense
import numpy as np

np.random.seed(3)

# number of wine classes
classifications = 3

# load dataset
dataset = np.loadtxt('wine.data', delimiter=",")

# split dataset into sets for testing and training
X = dataset[:,1:14]
Y = dataset[:,0:1]
x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.66, random_state=5)

# convert output values to one-hot
y_train = keras.utils.to_categorical(y_train-1, classifications)
y_test = keras.utils.to_categorical(y_test-1, classifications)

# creating model
model = Sequential()
model.add(Dense(13, input_dim=13, activation='relu'))
model.add(Dense(10, activation='relu'))
model.add(Dense(classifications, activation='softmax'))

# compile and fit model
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=['accuracy'])
model.fit(x_train, y_train, batch_size=15, epochs=2500, validation_data=(x_test, y_test))


import torch
import torchvision
from utils.params import Params

HYPERPARAMS = Params().get_params(verbose=True)[0]
CROPPER = torchvision.transforms.RandomCrop(HYPERPARAMS.patch_size)
SIGMOID = torch.nn.Sigmoid()

def train_discriminator(models,optimizers,criterions,images,scalers):
    """
    Trains the discriminator one epoch: max log(D(x)) + log(1-D(G(z)))
    Inputs:
        >> models: (dict) Contains the 'gen' and 'disc' models.
        >> optimizers: (dict) Contains the 'gen' and 'disc' optimizers.
        >> criterions: (dict) Contains the 'bce' and 'l1' criterions.
        >> images: (dict) Contains the 'real_x', 'real_y' and 'fake_y' images.
        >> scalers: (dict) Contains the 'gen' and 'disc' grad scalers.
    Outputs:
        >> loss_disc: (torch.float32) containing the value of the loss for the discriminator
        >> real_disc_output: (torch.float32) containing the value of the output for the discriminator when fed with real images.
        >> fake_disc_output: (torch.float32) containing the value of the output for the discriminator when fed with fake images.
    """
    with torch.cuda.amp.autocast():
        real_disc_output = models['disc'](CROPPER(images.real_x), CROPPER(images.real_y)).reshape(-1)
        loss_real_disc = criterions['bce'](real_disc_output, torch.ones_like(real_disc_output))

        fake_disc_output = models['disc'](CROPPER(images.real_x), CROPPER(images.fake_y).detach()).reshape(-1)
        loss_fake_disc = criterions['bce'](fake_disc_output, torch.zeros_like(fake_disc_output))
        loss_disc = ((loss_real_disc + loss_fake_disc) / 2.0)

    optimizers['disc'].zero_grad()
    if scalers['disc'] is not None:
        scalers['disc'].scale(loss_disc)
    loss_disc.backward()
    optimizers['disc'].step()
    return loss_disc, SIGMOID(real_disc_output).mean().item(), SIGMOID(fake_disc_output).mean().item()


from dotenv import load_dotenv
import os

load_dotenv()

MAPS_APIKEY = os.environ.get('MAPS_APIKEY')

import populartimes

def define_search_area(waypoint, radius=100):
    """
    Defines the bounding box for the search area with bound_lower and bound_upper.
    Returns both variables in that order.
    """
    m_in_degrees = 0.000898  # approximately 100 meters in degrees
    lat = waypoint["location"]["latitude"]
    lng = waypoint["location"]["longitude"]

    # Calculate bounding box coordinates of 200x200m box
    bound_lower = (lat - (m_in_degrees * 1), lng - (m_in_degrees * 1))
    bound_upper = (lat + (m_in_degrees * 1), lng + (m_in_degrees * 1))

    return bound_lower, bound_upper

def get_least_busy_locations(api_key, waypoints, place_type, search_time):
    """
    Supply with these values: MAPS_APIKEY, waypoints list [{"name": "", "latitude": 40, "longitude": -73}, ...],
    place_type as "cafe" or "restaurant", search_time as list of "weekday", hour ["Monday", 15].

    Returns a list containing up to three suggested locations based on the lowest popularity (quietest).
    """

    radius = 20  # in meters
    suggestions = []
    found_count = 0

    for waypoint in waypoints:
        # define the bounding box for the search area (you need to write this function)
        bound_lower, bound_upper = define_search_area(waypoint, radius)

        # retrieve place data
        # places = populartimes.get(api_key, [place_type], bound_lower, bound_upper, radius=radius)
        places = None
        # filter for least busy times and add to suggestions
        if places != None:
            for place in places:
                # get the day of the week and hour for the search time from frontend input
                # day_of_week, hour = get_day_and_hour(search_time)
                if found_count >= 3:
                    break

                # check if populartimes data is available for the place
                if "populartimes" in place and place["populartimes"] is not None:
                    # get the popular times data for the day of the week

                    day_data = next((item for item in place["populartimes"] if item["name"] == search_time[0]), None)

                    if day_data is not None:
                        # get the popular times data for the hour
                        hour_popularity = day_data["data"][search_time[1]]

                        # add the place and its popularity to the suggestions
                        suggestions.append((place, hour_popularity, waypoint))
                        found_count += 1

    # sort the suggestions by popularity (ascending) and take the top 3
    suggestions = sorted(suggestions, key=lambda x: x[1])[:3]

    # if fewer than 3 suggestions, add None for the remaining ones
    while len(suggestions) < 3:
        suggestions.append(None)

    print("suggestions:", suggestions)
    return suggestions


from socket import socket
import json
import time

host = 'sawtooth-shell'
# host = '192.168.192.4'
host = '127.0.0.1'

commands = {"set_msgs" : [{"action" : "set", "name" : "station0", "value" : 1},
                    {"action" : "set", "name" : "station1", "value" : 0},
                    {"action" : "set", "name" : "station2", "value" : 1},
                    {"action" : "set", "name" : "station3", "value" : 0}
                    ],
            "inc_msgs" : [{"action" : "inc", "name" : "station0", "value" : 1},
                    {"action" : "inc", "name" : "station1", "value" : 1},
                    {"action" : "inc", "name" : "station2", "value" : 1},
                    {"action" : "inc", "name" : "station3", "value" : 1},
                    ],
            "show_msgs" : [{"action" : "show", "name" : "station0", "value" : 1},
                    {"action" : "show", "name" : "station1", "value" : 1},
                    {"action" : "show", "name" : "station2", "value" : 1},
                    {"action" : "show", "name" : "station3", "value" : 1},
                    ],
            "list_msgs" : {"action" : "list", "name" : "station0", "value" : 1}
}

class sawtoothClient():
  """docstring for client"""
  def __init__(self, host='127.0.0.1', port=9000):
    self.host = host
    self.port = port
    self.socket = socket()
    self._connect()

  def _connect(self):
    self.socket.connect((self.host, self.port))

  def _sendMessage(self,message):
    self.socket.send(message.encode())
    feedbackResp = self.socket.recv(1024).decode()
    return feedbackResp

  def deconnect(self):
    self.socket.close()

  def initialiseValues(self):
    messages_raw = commands['set_msgs']
    for message_raw in messages_raw:
      message = json.dumps(message_raw)
      feedbackResp = self._sendMessage(message)
      time.sleep(0.1)

  def sendInc(self, stationNum, value=1):
    message_raw = commands["inc_msgs"][stationNum]
    message = json.dumps(message_raw)
    message['value'] = value
    feedbackResp = self._sendMessage(message)
    time.sleep(0.1)
    return feedbackResp

  def sendShow(self, stationNum):
    message_raw = commands["show_msgs"][stationNum]
    message = json.dumps(message_raw)
    feedbackResp = self._sendMessage(message)
    time.sleep(0.1)
    return feedbackResp

  def sendList(self):
    message_raw = commands["list_msgs"]
    message = json.dumps(message_raw)
    feedbackResp = self._sendMessage(message)
    time.sleep(0.1)
    return feedbackResp


import argparse
import asyncio
import os
from aiohttp import web
from GitHubAPI import GitHubAPI
import aiohttp_cors

routes = web.RouteTableDef()

@routes.get('/')
@routes.get('/relatedRepositories/')
async def hello(request):
    text = "USAGE: GET /relatedRepositories/:organization"
    return web.Response(text=text)

@routes.get('/relatedRepositories/{name}')
async def handle(request):
    name = request.match_info.get('name')
    repos = await api.filteredRelatedRepos(name)
    return web.json_response(repos, status=200)

@routes.get('/relatedRepositories/{name}/{repo}')
async def details(request):
    name = request.match_info.get('name')
    repoName = request.match_info.get('repo')
    repo = await api.filteredRepo(name, repoName)
    return web.json_response(repo, status=200)

#https://docs.aiohttp.org/en/stable/deployment.html
parser = argparse.ArgumentParser(description="aiohttp server test")
parser.add_argument('--port')

if __name__ == '__main__':
    GITHUB_SECRET_ACCESS_KEY = os.getenv('GITHUB_ACCESS_TOKEN')
    api = GitHubAPI(GITHUB_SECRET_ACCESS_KEY)

    app = web.Application()
    app.add_routes(routes)

    cors = aiohttp_cors.setup(app, defaults={
        "*": aiohttp_cors.ResourceOptions(
                expose_headers="*",
                allow_headers="*",
        )
    })

    # Configure CORS on all routes.
    for route in list(app.router.routes()):
        cors.add(route)
    #web.run_app(app)
    args = parser.parse_args()
    web.run_app(app, port=args.port)


# User input here
print("Please input the first number:")
n1 = float(input())
print("Please input the second number:")
n2 = float(input())
print("Please tell me the math method you want(1.+  2.-  3.*  4./ )(e.g:Input 1 as add):")
method: int = int(input())

if method > 4 or method < 1:
    print("Invalid input")
else:
    pass

if method == 1:
    result = n1 + n2
    print(n1, "+", n2, "=", result)
elif method == 2:
    result = n1 - n2
    print(n1, "-", n2, "=", result)
elif method == 3:
    result = n1 * n2
    print(n1, "*", n2, "=", result)
elif method == 4 and n2 != 0:
    result = n1 / n2
    print(n1, "/", n2, "=", result)
else:
    print("Invalid input")


#!/usr/bin/env python3

import io
import operator
import pkgutil
import xml.etree.ElementTree as xmlET

import numpy as np
from collections import OrderedDict

from typing import Dict, List, Optional, OrderedDict as OrderedDictType, Sequence, Union

from ..prms_helpers import version_info
from .ControlVariable import ControlVariable
from ..Exceptions_custom import ControlError
from ..constants import ctl_order, ctl_variable_modules, ctl_implicit_modules, VAR_DELIM

cond_check = {'=': operator.eq,
              '>': operator.gt,
              '<': operator.lt}

class Control(object):
    """
    Class object for a collection of control variables.
    """

    # Author: Parker Norton (pnorton@usgs.gov)
    # Create date: 2019-04-18

    def __init__(self, verbose: Optional[bool] = False, version: Optional[Union[str, int]] = 5):
        """Create Control object.
        """

        # Container to hold dicionary of ControlVariables
        self.__control_vars = OrderedDict()
        self.__header = None
        self.__verbose = verbose

        # First read control.xml from the library
        # This makes sure any missing variables from the control file
        # end up with default values
        xml_fh = io.StringIO(pkgutil.get_data('pyPRMS', 'xml/control.xml').decode('utf-8'))
        xml_tree = xmlET.parse(xml_fh)
        xml_root = xml_tree.getroot()

        for elem in xml_root.findall('control_param'):
            var_version = version_info(elem.attrib.get('version'))
            depr_version = version_info(elem.attrib.get('deprecated'))
            name = elem.attrib.get('name')

            if (var_version.major is not None and var_version.major > version):
                if self.__verbose:
                    print(f'{name} rejected by version')
                continue
            if (depr_version.major is not None and depr_version.major <= version):
                if self.__verbose:
                    print(f'{name} rejected by deprecation version')
                continue

            datatype = int(elem.find('type').text)

            self.add(name=name, datatype=datatype)

            # TODO: this shouldn't be here
            if name in ['start_time', 'end_time']:
                # Hack to handle PRMS approach to dates
                dt = elem.find('default').text.split('-')
                if len(dt) < 6:
                    # pad short date with zeros for hms
                    dt.extend(['0' for _ in range(6 - len(dt))])
                self.get(name).default = dt
            else:
                # print('{}: {} {}'.format(name, type(elem.find('default').text), elem.find('default').text))
                self.get(name).default = elem.find('default').text

            self.get(name).description = elem.find('desc').text

            if elem.find('force_default') is not None:
                self.get(name).force_default = elem.find('force_default').text

            # Possible valid values for variable
            outvals = {}
            for cvals in elem.findall('./values'):
                self.get(name).value_repr = cvals.attrib.get('type')

                for cv in cvals.findall('./value'):
                    outvals[cv.attrib.get('name')] = []

                    for xx in cv.text.split(','):
                        outvals[cv.attrib.get('name')].append(xx)
            self.get(name).valid_values = outvals

    def __getitem__(self, item: str) -> ControlVariable:
        """Get ControlVariable object for a variable.

        :param item: name of control file variable
        :returns: ControlVariable object
        """
        return self.get(item)

    @property
    def control_variables(self) -> OrderedDictType[str, ControlVariable]:
        """Get control variable objects.

        :returns: control variable objects
        """
        return self.__control_vars

    @property
    def dynamic_parameters(self) -> List[str]:
        """Get list parameter names for which a dynamic flag set.

        :returns: list of parameter names
        """

        dyn_params = []

        for dv in self.__control_vars.keys():
            cvar = self.get(dv)

            if cvar.value_repr == 'parameter' and (isinstance(cvar.values, np.int32) or isinstance(cvar.values, np.int64)):
                # Dynamic parameter flags should always be integers
                if cvar.values > 0:
                    dyn_params.extend(cvar.associated_values)
                    # dyn_params.append(self.get(dv).associated_values)
        return dyn_params

    @property
    def has_dynamic_parameters(self) -> bool:
        """Indicates if any dynamic parameters have been requested.

        :returns: True if dynamic parameters are required
        """
        return len(self.dynamic_parameters) > 0

    @property
    def header(self) -> Union[List[str], None]:
        """Get header information defined for a control object.

        This is typically taken from the first two lines of a control file.

        :returns: Header information from control file
        """
        return self.__header

    @header.setter
    def header(self, info: Union[Sequence[str], str]):
        """Set the header information.

        :param info: list or string of header line(s)
        """

        if isinstance(info, list):
            self.__header = info
        else:
            self.__header = [info]

    @property
    def modules(self) -> Dict[str, str]:
        """Get the modules defined in the control file.

        :returns: dictionary of control variable, module name pairs
        """

        mod_dict = {}

        for xx in ctl_variable_modules:
            if self.exists(xx):
                if xx == 'precip_module':
                    if self.get(xx).values == 'climate_hru':
                        mod_dict[xx] = 'precipitation_hru'
                    else:
                        mod_dict[xx] = str(self.get(xx).values)
                elif xx == 'temp_module':
                    if self.get(xx).values == 'climate_hru':
                        mod_dict[xx] = 'temperature_hru'
                    else:
                        mod_dict[xx] = str(self.get(xx).values)
                else:
                    mod_dict[xx] = str(self.get(xx).values)

        # Add the modules that are implicitly included
        for xx in ctl_implicit_modules:
            if xx not in mod_dict:
                mod_dict[xx] = ctl_implicit_modules[xx]

        return mod_dict

    @property
    def additional_modules(self) -> List[str]:
        """Get list of summary modules in PRMS
        """

        # TODO: module_requirements should be added to metadata?
        module_requirements = {'basin_sum': '',
                               'basin_summary': 'basinOutON_OFF > 0',
                               'map_results': 'mapOutON_OFF > 0',
                               'nhru_summary': 'nhruOutON_OFF > 0',
                               'nsegment_summary': 'nsegmentOutON_OFF > 0',
                               'nsub_summary': 'nsubOutON_OFF > 0',
                               'stream_temp': 'stream_temp_flag > 0',
                               'subbasin': 'subbasin_flag = 1'}

        active_modules = []

        for cmod, cond in module_requirements.items():
            if self._check_condition(cond):
                active_modules.append(cmod)

        return active_modules

    def _check_condition(self, cstr: str) -> bool:
        """Takes a string of the form '<control_var> <op> <value>' and checks
        if the condition is True
        """
        if len(cstr) == 0:
            return True

        var, op, value = cstr.split(' ')
        value = int(value)

        if self.exists(var):
            return cond_check[op](self.get(var).values, value)
        return False

    def add(self, name: str, datatype: int):
        """Add a control variable by name.

        :param name: Name of the control variable
        :param datatype: The datatype of the control variable

        :raises ControlError: if control variable already exists
        """

        if self.exists(name):
            raise ControlError("Control variable already exists")
        self.__control_vars[name] = ControlVariable(name=name, datatype=datatype)

    def exists(self, name: str) -> bool:
        """Checks if control variable exists.

        :param name: Name of the control variable
        :returns: True if control variable exists otherwise False
        """
        return name in self.__control_vars.keys()

    def get(self, name: str) -> ControlVariable:
        """Returns the given control variable object.

        :param name: Name of the control variable
        :returns: Control variable object

        :raises ValueError: if control variable does not exist
        """

        if self.exists(name):
            return self.__control_vars[name]
        raise ValueError(f'Control variable, {name}, does not exist.')

    def remove(self, name: str):
        """Delete a control variable if it exists.

        :param name: Name of the control variable
        """

        if self.exists(name):
            del self.__control_vars[name]

    def write(self, filename: str):
        """Write a control file.

        :param filename: Name of control file to create
        """

        outfile = open(filename, 'w')

        for hh in self.__header:
            outfile.write(f'{hh}\n')

        order = ['datatype', 'values']

        # Get set of variables in ctl_order that are missing from control_vars
        setdiff = set(self.__control_vars.keys()).difference(set(ctl_order))

        # Add missing control variables (setdiff) in ctl_order to the end of the list
        ctl_order.extend(list(setdiff))

        for kk in ctl_order:
            if self.exists(kk):
                cvar = self.get(kk)

                outfile.write(f'{VAR_DELIM}\n')
                outfile.write(f'{kk}\n')

                for item in order:
                    if item == 'datatype':
                        outfile.write(f'{cvar.size}\n')
                        outfile.write(f'{cvar.datatype}\n')
                    if item == 'values':
                        if cvar.size == 1:
                            # Single-values (e.g. int, float, str)
                            # print(type(cvar.values))
                            if isinstance(cvar.values, np.bytes_):
                                print("BYTES")
                                outfile.write(f'{cvar.values.decode()}\n')
                            else:
                                outfile.write(f'{cvar.values}\n')
                        else:
                            # Multiple-values
                            if isinstance(cvar.values, np.ndarray):
                                for cval in cvar.values:
                                    outfile.write(f'{cval}\n')

        outfile.close()

    def _read(self):
        """Abstract function for reading.
        """
        assert False, 'Control._read() must be defined by child class'

# ***** END class control()


import sys
sys.path.append("..")
import numpy as np
import argparse
import math
from train_models.mtcnn_model import P_Net, R_Net, O_Net
from prepare_data.loader import TestLoader
from Detection.detector import Detector
from Detection.fcn_detector import FcnDetector
from Detection.MtcnnDetector import MtcnnDetector
import cv2
import os

def dist_s(a1, a2, t):
	res = 0
	for i in range(10):
		res += pow(a1[i] - a2[i + 4], 2)
	if t == 1:
		res  = res / (5.0 * pow(pow(a2[4] - a2[6], 2) + pow(a2[5] - a2[7], 2), 1))
	elif t == 0:
		res = res / (5.0 * a2[2] * a2[3])
	return res

land_dir = '../data/FDDB_OUTPUT'
land_dir = '../data/FDDB_OUTPUT_ORI'
anno_dir = '../data/FDDB_annotation'

land_f = open(os.path.join(land_dir, 'FDDB-landmark-fold-all.txt').replace('\\', '/'), 'r')
anno_f = open(os.path.join(anno_dir, 'FDDB-landmark-fold-all.txt').replace('\\', '/'), 'r')
lines_land = land_f.readlines()
lines_anno = anno_f.readlines()
land_f.close()
anno_f.close()
i = 1
j = 1
faces = 0
anno_faces = 0
sum = 0
images = 0
normalize_type = 0 # 0 - face size, 1 - inter-pupil distance
while i < len(lines_land) and j < len(lines_anno):
	if images % 100 == 0:
		print('images : ', images)
		print('sum : ', sum)
	images += 1

	land_num = int(lines_land[i])
	anno_num = min(5, int(lines_anno[j]))
	anno_faces += anno_num
	if anno_num == 0:
		i += land_num + 2
		j += anno_num + 2
		continue
	faces += land_num
	temp1 = []
	temp2 = []
	for m in range(i + 1, i + 1 + land_num):
		a = lines_land[m].strip().split()
		# print(a)
		tt = [float(k) for k in a]
		temp1.append(tt)
	for n in range(j + 1, j + 1 + anno_num):
		a = lines_anno[n].strip().split()
		# print(a)
		tt = [float(k) for k in a]
		temp2.append(tt)
	temp1 = np.array(temp1)
	temp2 = np.array(temp2)
	for p in range(land_num):
		# if anno_num == 0:
			# sum += dist_s(temp1[p], np.zeros((10)))
			# continue
		min_sqr = dist_s(temp1[p], temp2[0], normalize_type)
		for q in range(1, anno_num):
			min_sqr = min(min_sqr, dist_s(temp1[p], temp2[q], normalize_type))
		sum += min_sqr

	i += land_num + 2
	j += anno_num + 2

sum = sum / faces
print('faces : ', faces)
print('anno_faces : ', anno_faces)
print('landmark mean error : ', sum)

total = 0
f = open('../data/FDDB/FDDB-folds/FDDB-fold-all-ellipseList.txt', 'r')
line = f.readline()
while line:
	if line.find('big') > 0:
		line = f.readline()
		total += int(line)
	line = f.readline()
f.close()
print('total faces : ', total)


import sys

input = sys.stdin.readline

def binary_search():
    global res, ans1, ans2
    lt, rt = i + 1, n - 1
    while lt <= rt:
        mid = (lt + rt) // 2
        temp = liquids[i] + liquids[mid]

        if abs(temp) <= res:
            res = abs(temp)
            ans1, ans2 = i, mid

            if temp == 0:
                break
        if temp < 0:
            lt = mid + 1
        else:
            rt = mid - 1

if __name__ == "__main__":
    n = int(input())
    liquids = list(map(int, input().split()))
    res = float("INF")
    ans1, ans2 = 0, 0
    for i in range(n - 1):
        binary_search()

    print(liquids[ans1], liquids[ans2])


def dimensoes(matriz):
    lin = len(matriz)
    col = len(matriz[0])
    return str(lin) + "X" + str(col)

def soma_matrizes(m1, m2):
    if dimensoes(m1) == dimensoes(m2):
        m3 = list()
        tam_lin = len(m1)
        tam_col = len(m1[0])
        for i in range(tam_lin):
            lin = [m1[i][j] + m2[i][j] for j in range(tam_col)]
            m3.append(lin)
        return m3
    else:
        return False

Universidade de São Paulo/Introdução à Ciência da Computação em Python Parte 2/semana-1/soma_matriz.py


from generalSpecialistPopulation import *
import multiprocessing
import pickle

N = 10
land_num = 500
period = 50
crowd_num = 100
aspiration_ratio = 0.5
state_num = 4
IM_type = "random"

time_interval_index = 2

# to_file
file_path = "./output_generalFramework_specialistGeneralist_{time_interval_index}".format(
    time_interval_index=time_interval_index,
)

time_interval_list = [
    [0, 50],  # crowd-sourcing
    [50],  # internal R & D
    [0, 30, 50],  # idea community
    [20, 30, 50],  # idea polishment
]

crowd_proportion_list = [
    [0, 1],  # all specialist
    [0.25, 1],
    [0.5, 1],
    [0.75, 1],
    [1, 1]
]

# total elements (16, 24, 32)
knowledge_num = [10, 6, 8]
specialist_num = [2, 6, 4]

if __name__ == '__main__':
    manager = multiprocessing.Manager()
    return_dic = manager.dict()
    print(
        """
        def simulation(
                return_dic, idx, N, k, land_num, crowd_num, period, crowd_proportion, crowd_knowledge, crowd_special_knowledge,
                 aspiration_ratio, time_interval, state_num=4, IM_type="random", in_progress_feedback=False, open_env=False
               )
        """
    )

    jobs = []

    index = 0

    for proportion_index in range(0, len(crowd_proportion_list)):
        for k in range(0, 91, 30):

            for in_progress_feedback in [False, True]:
                for open_env in [False, True]:

                    p = multiprocessing.Process(
                        target=simulation, args=(
                            return_dic, index, N, k, land_num, crowd_num, period, crowd_proportion_list[proportion_index],
                            knowledge_num, specialist_num, aspiration_ratio, time_interval_list[time_interval_index],
                            state_num, IM_type, in_progress_feedback, open_env,
                        )
                    )

                    jobs.append(p)
                    p.start()

                    index += 1

    for proc in jobs:
        proc.join()

    res_dic = {}

    for k in return_dic.keys():
        res_dic[k] = return_dic[k]

    f = open(file_path, "wb")
    pickle.dump(res_dic, f)
    f.close()


import os
os.chdir("E:\BINUS\Algorithm\get os test") #file directory

fileOpen = open("exerpt.txt","r") #Opens and reads the file
fileRead = fileOpen.read()

lowDelWord = fileRead.lower().replace(".", "") #change word into lowercase and remove punctuations.

fileSplit = len(lowDelWord.split()) #count the amount of words
fileTextLen = len(lowDelWord) #count the length of words

aveWordLen = fileTextLen / fileSplit #calculate the average

fileOpen.close()

print(f"The average word length is {aveWordLen}")
print(f"The sum of all the lengths of word tokens is {fileTextLen}") #print with text
print(f"The amount of words in the text is {fileSplit}")

# print(aveWordLen)
# print(fileTextLen)
# print(fileSplit)

11-16-2021-Programming Exercises (Files)/Questions 2-4/Question 3 - Average Word Length - Programming Exercises .py


"""
Helper functions to close (i.e. claim rewards from) all mines
of a given user
"""

from src.common.logger import logger, logTx
from src.helpers.instantMessage import sendIM
from src.common.clients import makeCrabadaWeb3Client
from src.helpers.mines import (
    fetchOpenMines,
    getNextMineToFinish,
    getRemainingTimeFormatted,
    mineIsFinished,
)
from src.models.User import User
from web3.exceptions import ContractLogicError
from src.helpers.donate import maybeDonate

def closeMines(user: User) -> int:
    """
    Close all open mining games whose end time is due; return
    the number of closed games.
    """
    client = makeCrabadaWeb3Client(
        upperLimitForBaseFeeInGwei=user.config["closeMineMaxGasInGwei"]
    )
    openGames = fetchOpenMines(user)
    finishedGames = [g for g in openGames if mineIsFinished(g)]

    # Print a useful message in case there aren't finished games
    if not finishedGames:
        message = f"No mines to close for user {str(user.address)}"
        nextGameToFinish = getNextMineToFinish(openGames)
        if nextGameToFinish:
            message += f" (next in {getRemainingTimeFormatted(nextGameToFinish)})"
        logger.info(message)
        return 0

    nClosedGames = 0

    # Close the finished games
    for g in finishedGames:

        # Close mine
        gameId = g["game_id"]
        logger.info(f"Closing mine {gameId}...")
        try:
            txHash = client.closeGame(gameId)
        except ContractLogicError as e:
            logger.warning(f"Error closing mine {gameId}: {e}")
            sendIM(f"Error closing mine {gameId}: {e}")
            continue

        # Report
        txReceipt = client.getTransactionReceipt(txHash)
        logTx(txReceipt)
        if txReceipt["status"] != 1:
            logger.error(f"Error closing mine {gameId}")
            sendIM(f"Error closing mine {gameId}")
        else:
            nClosedGames += 1
            logger.info(f"Mine {gameId} closed correctly")
            sendIM(f"Mine {gameId} closed correctly")
            maybeDonate(game=g, isMining=True)

    return nClosedGames


from tkinter import *

root = Tk()

margin = 0.23
projectedSales = 100
profit = margin * int(projectedSales)

#My function that is linked to the event of my button
def profit_calculator(event):
    print(profit)

#the structure of the window
label_pan = Label(root, text="Projected annual sales:")
label_profit = Label(root, text="Projected profit")
label_result = Label(root, text=(profit), fg="red")

entry = Entry(root)

button_calc = Button(root, text= "Calculate", command=profit_calculator(1))
button_calc.bind("<Button-1>", profit_calculator(2))

#position of the elements on the window
label_pan.grid(row=0)
entry.grid(row=0, column=1)
button_calc.grid(row=1)
label_profit.grid(row=2)
label_result.grid(row=2, column=1)

root.mainloop()


import numpy as np

def break_ranges(ranges):
    xbegin = ranges[0][0]
    xend = ranges[0][1]
    ybegin = ranges[1][0]
    yend = ranges[1][1]
    xlen = xend - xbegin
    ylen = yend - ybegin
    ranges_nw = ((xbegin, xbegin + xlen // 2), (ybegin, ybegin + ylen // 2))
    ranges_ne = ((xbegin, xbegin + xlen // 2), (ybegin + ylen // 2, yend))
    ranges_sw = ((xbegin + xlen // 2, xend), (ybegin, ybegin + ylen // 2))
    ranges_se = ((xbegin + xlen // 2, xend), (ybegin + ylen // 2, yend))
    return ranges_nw, ranges_ne,\
           ranges_sw, ranges_se

def indices_from_shape(shape):
    return tuple((np.arange(dim) for dim in shape))

def break_indices(indices):
    indices = np.asarray(indices)
    m, n = indices[0].size, indices[1].size
    return ((indices[0][:m//2], indices[1][:n//2]), (indices[0][:m//2], indices[1][n//2:])),\
            ((indices[0][m//2:], indices[1][:n//2]), (indices[0][m//2:], indices[1][n//2:]))

def indices_unveil(indices):
    indices = np.asarray(indices)
    return np.vstack([np.repeat(indices[0], indices[1].size),
               np.tile(indices[1], indices[0].size)]).T


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

import os, sys, subprocess
from datetime import datetime
from settings import debug_log, debug_cmdrun, smtp_sender, smtp_port, smtp_server, smtp_username, smtp_password, report_log_success

from smtplib import SMTP_SSL as SMTP
from email.message import EmailMessage

# print level 0
def p_lv0(msg):
    print('\n!',msg)

# print level 1
def p_lv1(msg):
    print('  +',msg)

# print level 2
def p_lv2(msg):
    print('    ',msg)

def p_lv3(msg):
    print('      ',msg)

def log_write(file=False, value=False):
    '''
    basic information of backup
    '''
    if not file or not value:
        return False

    if debug_log:
        p_lv1('log write')
        p_lv3('You are seeing this message because debug_log is True')
        p_lv3('Writing in %s' % file)

    # open file
    of = open(file, 'a')
    if of:
        of.write(value + '\n')
        of.close()
        return True
    else:
        sys.exit('- Log write error.')

def cmd_run(cmd, log=False, log_err=False):
    '''
    subprocess
    return  result to variable
    cmd:    string command bash
    '''
    p_lv1('cmd run')

    if debug_cmdrun:
        p_lv2('You are seeing this message because debug_cmdrun is True')
        p_lv2(cmd)

    pipe = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    res = pipe.communicate()
    # res = tuple (stdout, stderr)

    if pipe.returncode == 0:
        msg = u"\n+ command: %s" % (cmd)
        log_write(log, msg)
        p_lv2('+ Success')

        if res[0]:
            for line in res[0].decode(encoding='utf-8').split('\n'):
                log_write(log, line)

        if res[1]:
            for line in res[1].decode(encoding='utf-8').split('\n'):
                log_write(log, line)

    # error
    else:
        msg = u"\n+ command: %s" % (cmd)
        log_write(log_err, msg)

        if res[0]:
            p_lv2('- Error')
            for line in res[0].decode(encoding='utf-8').split('\n'):
                p_lv3(line)
                log_write(log_err, line)

        if res[1]:
            p_lv2('- Error')
            for line in res[1].decode(encoding='utf-8').split('\n'):
                p_lv3(line)
                log_write(log_err, line)

def sendmail(to, app, subject=False, attach0=False, attach1=False, smtp_test=False, log_resume=False):
    '''
    to      : array of email address
    attach0 : error log
    attach1 : sucess log
    log_resume: resume text log file
    '''
    p_lv0('Send backup report mail to admin')

    destination = []
    for x in to:
        destination.append(x)

    if not destination:
        p_lv1('Error trying send a mail. Check settings.')
        p_lv1('[to] are empty. To check config[4]')
        return False

    # new message
    msg = EmailMessage()

    if smtp_test == True:
        msg.set_content('Mr-Robot3 SMTP Test configuration', 'plain')
        msg['Subject'] = "Mr.Robot3 %s" % app
        msg['From'] = smtp_sender

    if smtp_test == False:
        # read attach
        contentattach = ''

        fp = open(log_resume,'rb')
        contentattach += '------------------ Resume begin\n'
        for l in fp.readlines():
            contentattach += u"%s" % l.decode(encoding="utf-8")
        contentattach += '------------------ Resume end'
        fp.close()

        if attach0 and not os.stat("%s" % attach0).st_size == 0:
            fp = open(attach0,'rb')
            contentattach += '\n\n------------------ Error begin\n'
            for l in fp.readlines():
                contentattach += u"%s" % l.decode(encoding="utf-8")
            contentattach += '\n------------------ Error end'
            fp.close()

        # settings
        if report_log_success:
            if attach1 and not os.stat("%s" % attach1).st_size == 0:
                fp = open(attach1,'rb')
                contentattach += '\n\n------------------ Success begin\n'
                for l in fp.readlines():
                    contentattach += u"%s" % l.decode(encoding="utf-8")
                contentattach += '\n------------------ Success end'
                fp.close()

        msg.set_content(contentattach,'plain')
        msg['Subject'] = subject
        msg['From'] = smtp_sender

    try:
        conn = SMTP(smtp_server, smtp_port)
        conn.set_debuglevel(False)
        conn.login(smtp_username, smtp_password)
        conn.sendmail(smtp_sender, destination, msg.as_string())
        conn.close()
    except Exception as e:
        p_lv1('-Error trying send a mail. Check settings.')
        p_lv1("-Error: ", e)
        return False
    else:
        p_lv1("Email sent")
        return True

# date format
def dateformat(freq, hour):
    '''
        return date format based in frequency set
    '''
    if hour == False or hour == 'False':
        if freq == 'daily':
            return "%s" % (datetime.now().strftime("%A-%d-%b-%Y").lower()) # sunday-28-dez-2014
        if freq == 'month-full':
            return "%s" % (datetime.now().strftime("%d").lower()) # 28
        if freq == 'week-full':
            return '%s' % (datetime.now().strftime("%A").lower()) # sunday
        if freq == 'once':
            return  'once'
    else:
        if freq == 'daily':
            return "%s_%s" % (datetime.now().strftime("%A-%d-%b-%Y").lower(), hour) # sunday-28-dez-2014_14h00
        if freq == 'month-full':
            return "%s-%s" % (datetime.now().strftime("%d").lower(), hour) # 28-14h00m
        if freq == 'week-full':
            return '%s-%s' % (datetime.now().strftime("%A").lower(), hour) # sunday-14h00m
        if freq == 'once':
            return  hour # 14h00m (hour-min)

def mysqlshow(log, log_err, ignore_list):
    # get ALL databases
    cmd='mysqlshow'
    r = subprocess.check_output(cmd, shell=True)
    '''
    clean string
    mysqshow output:
        +--------------------+  0
        | Database           |  1
        +--------------------+  2
        | classicmodels      |  ...
        | information_schema |
        | mysql              |
        | performance_schema |
        | my_app             |
        | sys                |
        | test               |
        | hello_world        |
        +--------------------+  last-1
    ignore line 0,1,2 and last
    create a list of databases
    '''
    ignore = [0,1,2] # positions + last
    ignore.append(len(str(r).split('\\n'))-1)

    c = 0 # postions counter
    db_list = []
    for rr in str(r).split('\\n'):
        #print line,value
        #print(c, str(rr)) # debug

        if not c in ignore:
            if rr.find('+'):
                rr = rr.replace('|','')
                dbname = rr.replace(' ','')

                # BD ignore list
                if not dbname in ignore_list:
                    db_list.append(dbname)
                    #print(c, str(rr)) # debug
        c += 1
    return db_list


#! python3
# Batch renames WTparts and CADdocuments in Windchill
# Input file (*.xlsx) should contain three columns:
#      part_number | long_name | short_name
# as processing goes by fourth column will be filled with statuses:
# ok - rename successful, fail - rename failed, empty - not yet processed.

import openpyxl
import sys
from openpyxl import load_workbook
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

if len(sys.argv) != 2:
    print("use: windchill_batch_rename.py excel_file.xlsx")
    exit()

xlsx_file = sys.argv[1]
workbook = load_workbook(filename=xlsx_file)
sheet = workbook.active
windchill_url = "http://windchill_url"
browser = webdriver.Chrome()
browser.set_page_load_timeout(5)
browser.get(windchill_url)
main_window = browser.current_window_handle
input("login and press [Enter] to continue...")

def find_part(part_number):
    find_field = WebDriverWait(browser, 5).until(
        EC.presence_of_element_located(By.ID, "gloabalSearchField")
    )
    find_field.clear()
    find_field.send_keys(part_number)
    find_field.send_keys(Keys.ENTER)

def open_rename_window(part_number):
    number_link = WebDriverWait(browser, 5).until(
        EC.presence_of_element_located(By.LINK_TEXT, part_number)
    )
    ac = ActionChains(browser)
    ac.context_click(number_link).perform()
    rename_link = WebDriverWait(browser, 2).until(
        EC.presence_of_element_located(By.LINK_TEXT, "Rename")
    )
    rename_link.click()

def handle_rename_window(new_name):
    WebDriverWait(browser, 5).until(EC.new_window_is_opened)
    browser.switch_to.window(browser.window_handles[1])  # new window
    try:
        wait = WebDriverWait(browser, 3)
        rename_field = wait.until(
            EC.presence_of_element_located(
                By.XPATH, "//input[contains(@id, 'NameInputId')]"
            )
        )
        rename_field.clear()
        rename_field.send_keys(new_name)
        ok_btn = browser.find_element_by_xpath(
            "//button[contains(@accesskey, 'o')]")  # c - cancel, o - OK
        ok_btn.click()
    except:
        raise
    finally:
        browser.close()
        browser.switch_to.window(main_window)

def rename_wtpart(part_number, new_name):
    browser.get(windchill_url)
    find_part(part_number)
    open_rename_window(part_number)
    handle_rename_window(new_name)

for row in sheet.iter_rows(max_col=4):
    try:
        rename_wtpart(row[0], row[2])
        row[3] = "OK"  # if success
    except Exception as ex:
        print("error at " + row[0])
        print(ex)
        continue
    workbook.save(xlsx_file)


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

import os
os.chdir('D:/yh_min-mfactors')
from functions import *
from address_data import *
import pandas as pd
#import statsmodels.api as sm
import numpy as np

# 某一时间截面，所有个股的收益对所有个股的各个因子进行多元回归，
# 得到某个因子在某个时间个股的残差值，数据量191*227*300，得到有效因子
# 然后对每个截面求得预测收益和实际收益的相关系数，即IC(t)值，最后得到一个时间序列的IC值
# 对IC值进行T检验

# 第一步 读取行业数据
code_HS300 = pd.read_excel(add_gene_file + 'index_stockcodes.xlsx',sheetname='HS300')
stockList = list(code_HS300['code'][:])
stockList.remove('600485.SH')
industry = pd.read_pickle\
    (add_gene_file + 'industry.pkl').drop_duplicates()
industry = industry[industry['code'].isin(stockList)]
industry.index = industry['code']
industry.drop(['code'],axis = 1,inplace = True)
industry = industry.T
industry.reset_index(inplace = True)
industry.rename(columns={'index':'date'},inplace = True)

# 第二步 读取风格因子数据
# 因子数据截止到2017-12-06日'
style_filenames = os.listdir(add_Nstyle_factors)
style_list = list(map(lambda x : x[:-4],style_filenames))
for sfilename in style_filenames:
    names = locals()
    names[sfilename[:-4]] = pd.read_csv(add_Nstyle_factors+sfilename)

# 第三步 因子值回归,得到行业和风格中性的因子残差值
standard_alpha = os.listdir(add_alpha_day_stand)
for saf in standard_alpha:
    print (saf)
    # saf = 'standard_alpha_001.csv'
    alpha_d = pd.read_csv(add_alpha_day_stand + saf)
    alpha_d = alpha_d[alpha_d['code']!=600485]
    factor_data = possess_alpha(alpha_d,saf)
    df_resid=pd.DataFrame(index=stockList,columns =factor_data['date'])
    n=0
    for date in factor_data['date']:
        print (n)
        X = industry
        Y = factor_data[factor_data['date'] == date] # 每个时间截面的因子值
        Y = Y.loc[:,stockList].T
#        Y = np.array(Y.fillna(0))
        for sfile in style_list:
            mid_sd = eval(sfile)
            X = X.append(mid_sd[mid_sd['date'] == date])
        X = X.loc[:,stockList].T
        X = np.array(X.fillna(0))
        df_resid.iloc[:,n] = resid(Y, X)
        n=n+1
    df_resid.to_csv(add_resid_value_day+saf[9:18]+'_resid.csv',index = False)

# 第三步 针对给定的预测周期，通过回归方程计算单期因子收益率；
# 个股收益率
return_data = pd.read_pickle\
    (add_gene_file + 'dailyreturn.pickle').rename(columns={'symbol':'code'})
return_data['code'] = return_data['code'].apply(lambda x:add_exchange(x))
return_data = return_data[(return_data['date']>='2016-12-30') &
                 (return_data['date']<='2017-12-06') & (return_data['code'].isin(stockList))]
return_data=return_data.pivot(index='date', columns='code', values='daily_return')

# 建立回归方程,求单因子收益率
def factor_return(daynum):
    date_list = list(return_data.index)
    resid_value = os.listdir(add_resid_value_day)
    factor_freturn=pd.DataFrame(columns =[['alpha_factors'] + \
                                         [x for x in date_list if x >='2017-01-01' ]])
    n=0
    for ar in resid_value:
#        ar = 'alpha_149_resid.csv'
        try:
            resid_val = pd.read_csv(add_resid_value_day + ar)
        except:
            n=n+1
            continue
        print (n)
        factor_freturn.loc[n,'alpha_factors'] = ar[:9]
        for date in resid_val.columns:
            X = industry
#            Y = return_data[return_data.index == date] # 每个时间截面的因子值
            before_daynum = date_list[date_list.index(date)-daynum]
            Y = return_data[return_data.index == before_daynum]
            Y = Y.loc[:,stockList].T
            Y = np.array(Y.fillna(0))
            for sfile in style_list:
                mid_sd = eval(sfile)
                X = X.append(mid_sd[mid_sd['date'] == date])
            resid_v = resid_val.iloc[:,resid_val.columns == date]
            resid_v.index = stockList
            resid_v = resid_v.T
            X = X.append(resid_v)
            X = X.loc[:,stockList].T
            X = np.array(X.fillna(0))
            factor_freturn.loc[n,date] = beta_value(Y, X)[-1]
        n=n+1
    factor_freturn.to_csv(add_factor_return+'factors_return_%s.csv'%daynum,index = False)
    return 0

for daynum in range(1,6):
    factor_return(daynum)

# 第四步 检验因子有效性
freturn_value = os.listdir(add_factor_return)
#df = pd.read_csv(r'G:\short_period_mf\alpha_day\alpha_001.csv')
# 计算年化收益率和IR
for ndays in range(1,6):
    df = pd.DataFrame(columns=['factors','return_peryear','IR'])
    f_return = pd.read_csv(add_factor_return+'factors_return_%s.csv'%ndays)
    df['factors'] = f_return['alpha_factors']
    df['return_peryear'] = f_return[f_return.columns[1:]].apply( \
                                  lambda x : ((x/ndays).mean())*252,axis=1)
    df['IR'] = f_return[f_return.columns[1:]].apply( \
                          lambda x : ((x.mean()/ndays)*np.sqrt(252))/((x/ndays).std()),axis=1)
    df.to_csv(add_factor_freturn_IR+'factors_return_IR_%s.csv'%ndays,index=False)

# 第五步 统计因子收益
factors_return_IR = os.listdir(add_factor_freturn_IR)
df = pd.DataFrame(columns=['fore_days','factors_return_mean','IR_mean'])
n=0
for frI in factors_return_IR:
    frI_return = pd.read_csv(add_factor_freturn_IR+frI)
    df.loc[n,'fore_days'] = frI[18]
    df.loc[n,'factors_return_mean'] = frI_return['return_peryear'].mean()
    df.loc[n,'IR_mean'] = frI_return['IR'].mean()
    n = n+1
# df结果如下：
#  fore_days factors_return_mean  IR_mean
#0         1             10.6764  1.65927
#1         2             5.24474  1.38974
#2         3             3.81729  1.99395
#3         4             3.11387  2.54553
#4         5             2.36323  2.54787

# 第六步 找出五个预测期效果都比较好的因子
factors_return_IR = os.listdir(add_factor_freturn_IR)
df_score = pd.DataFrame(columns=['factors'])
for frI in factors_return_IR:
    frI_return2 = pd.read_csv(add_factor_freturn_IR+frI)
    frI_return2.sort_values(by='return_peryear',axis=0,ascending=True,inplace=True)
    frI_return2['score_return'] = list(range(1,192))
    frI_return2.sort_values(by='IR',axis=0,ascending=True,inplace=True)
    frI_return2['score_IR'] = list(range(1,192))
    frI_return2['score_%s'%frI[18]] = frI_return2['score_return']+ frI_return2['score_IR']
    if frI == 'factors_return_IR_1.csv':
        df_score['factors'] = frI_return2['factors']
    frI_return2 = frI_return2[['factors','return_peryear','IR','score_%s'%frI[18]]]
    frI_return2.rename(columns={'return_peryear':'return_%s'%frI[18],\
                                'IR':'IR_%s'%frI[18]},inplace = True)
    df_score = pd.merge(df_score,frI_return2,on='factors',how='inner')
df_score['score_res'] = df_score['score_1']+df_score['score_2']+df_score['score_3']\
                            +df_score['score_4']+df_score['score_5']
df_score.sort_values(by='score_res',axis=0,ascending=False,inplace=True)
df_score.to_csv(add_effecive_factors_day+'effecive_factors_day.csv',index=False)


# -*- coding: utf-8 -*-
from celery import task
from django.core.mail import send_mail
from .models import Order

@task
def order_created(order_id):
	"""
	Tarea para enviar una notificacion
	email cuando una orden es creada
	"""
	order=Order.objects.get(id=order_id)
	subject='Orden #{}'.format(order.id)
	message='Estimado {},\n\nTu orden ha sido correctamente recibida El id de tu orden es: {}.'.format(order.first_name,order_id)
	mail_sent=send_mail(subject,message,'contacto@fixter.org',[order.email])
	return mail_sent


from arm.logicnode.arm_nodes import *

class DrawCameraTextureNode(ArmLogicTreeNode):
    """Renders the scene from the view of a specified camera and draws
    its render target to the diffuse texture of the given material.

    @input Start: Evaluate the inputs and start drawing the camera render target.
    @input Stop: Stops the rendering and drawing of the camera render target.
    @input Camera: The camera from which to render.
    @input Object: Object of which to choose the material in the `Material Slot` input.
    @input Material Slot: Index of the material slot of which the diffuse
        texture is replaced with the camera's render target.

    @output On Start: Activated after the `Start` input has been activated.
    @output On Stop: Activated after the `Stop` input has been activated.
    """
    bl_idname = 'LNDrawCameraTextureNode'
    bl_label = 'Draw Camera to Texture'
    arm_section = 'draw'
    arm_version = 1

    def arm_init(self, context):
        self.add_input('ArmNodeSocketAction', 'Start')
        self.add_input('ArmNodeSocketAction', 'Stop')
        self.add_input('ArmNodeSocketObject', 'Camera')
        self.add_input('ArmNodeSocketObject', 'Object')
        self.add_input('ArmIntSocket', 'Material Slot')

        self.add_output('ArmNodeSocketAction', 'On Start')
        self.add_output('ArmNodeSocketAction', 'On Stop')


from django.http import HttpResponse, HttpResponseBadRequest
import boto3

def speech(request):
    polly_client = boto3.Session(region_name='us-east-1').client('polly')

    # Get the text to synthesize speech from
    text = request.GET.get('content')

    # Get the voice to use
    voice = request.GET.get('voice', 'Joanna')

    # Synthesize speech from the text using the specified voice
    response = polly_client.synthesize_speech(VoiceId=voice,
                OutputFormat='mp3',
                Text=text)

    # Set the filename for the downloaded file
    filename = 'awwzapp_synthesize_voice_over.mp3'

    # Create the HTTP response with the synthesized audio data and the Content-Disposition header
    http_response = HttpResponse(response['AudioStream'].read(), content_type='audio/mpeg')
    http_response['Content-Disposition'] = f'attachment; filename="{filename}"'

    return http_response


import numpy as np
import pandas as pd

###### OUTLIER FUNCTIONS #####
### get_iqr_outlier_bounds(df,include=None,exclude=None)
### trim_iqr_outliers(df,bounds)
### calc_outliers(x,lb,ub)
### add_outlier_columns(df,bounds)
### handle_iqr_outliers(df,trim=True,include=None,exclude=None)

##### IMPROVEMENT TO MAKE #####
# handle_iqr_outliers:
#   1) Investigate why python is updating my dataframe external to the function - needed to use df = old_df.copy() to prevent this
#   2) Add in multipler and remodularize like how I did in the anomaly exercises
#   3) Add "type" parameter that can identify just upper or lower bounds or both
# get_iqr_outlier_bounds:
#   1) Add function to check if column only contains 0s and 1s.  If so, remove it from the list of columns to consider.
# NEW FUNCTION:
#   1) Returns only the outliers

def get_iqr_outlier_bounds(df,include=None,exclude=None):
    """
    Returns dataframe with list of columns and the upper and lower bounds using the IQR method:
        LB = Q1 - 1.5 * IQR
        UB = Q3 + 1.5 * IQR
    If no columns passed to include or exclude, it defaults to finding outliers for all columns.
    Function will ignore non-numeric columns. FUTURE: Columns that contain only 0s and 1s.

    Returns: Pandas Dataframe
    Parameters:
           df: dataframe in which to find outliers
      include: list of columns to find outliers for
       excude: list of columns to NOT find outliers for.  Ignored if 'include'is set.

    C88
    """
    #Get Column List
    #if include and exclude are None
    if not include and not exclude:
        columns = df.columns #returns index - iterable
    elif include:
        columns = include
    else: columns = exclude

    #Only pull out numeric columns
    columns = df[columns].select_dtypes(include='number')
#     #TO DO: check if only 0s and 1s
#     for c in columns:
#         #if series contains only zeros and ones
#         if df[c].isin([0,1]).all():

    #create df for bounds
    bounds = pd.DataFrame()
    #for each column,
    for col in columns:
        #find bounds
        q1, q3 = df[col].quantile([.25,.75])
        iqr = q3 - q1
        lb = q1 - (1.5 * iqr)
        ub = q3 + (1.5 * iqr)
        #put info in df
        bounds.loc['lb',col] = lb
        bounds.loc['ub',col] = ub
    return bounds

def trim_iqr_outliers(df,bounds):
    """
    Takes in the dataset dataframe, and dataframe of the bounds for each column to be trimmed.
    Returns: Trimmed dataframe
    """
    #loop over columns to work on
    for col in bounds.columns:
        #for each col, grab the outliers
        lb = bounds.loc['lb',col]
        ub = bounds.loc['ub',col]
        #create smaller df of only rows where column is in bounds
        df = df[(df[col] >= lb) & (df[col]<=ub)]
    return df

def calc_outliers(x,lb,ub):
    ''''
    Given a value, determines if it is between the provided upper and lower bounds.
    If b/w bounds, returns 0, else returns the distance outside of the bounds
    '''
    #if not an outlier, set to zero
    if lb <= x <= ub: return 0
    elif x < lb: return x-lb
    else: return x-ub

def add_outlier_columns(df,bounds):
    #loop over columns in bounds
    for col in bounds.columns:
        #new column name
        col_name = col + '_outlier'
        #for each column, apply the outlier calculation and store to new column
        df[col_name] = df[col].apply(calc_outliers,args=(bounds.loc['lb',col],bounds.loc['ub',col]))
    return df

def handle_iqr_outliers(old_df,trim=False,include=None,exclude=None):
    """
    Takes in a dataframe and either trims outliers or creates column identifying outliers.

    Outputs: None
    Returns: Pandas Dataframe
    Parameters:
                   df: dataframe in which to find outliers
                 trim: If True, will trim out any rows that contain any outliers.
                        If False, creates new columns to indicate if row is an outlier or not.
                        Default: False
      include/exclude: list of columns to include or exclude for this function.
                       Default is all, exclude will be ignored if include is provided.
    C88
    """
    df= old_df.copy()
    #Get bounds dataframe
    bounds = get_iqr_outlier_bounds(df,include,exclude)

    #If we want new column
    if trim:
        #Function trims row if value not w/i bounds
        df = trim_iqr_outliers(df,bounds)
    else:
        #function determines if outlier and adds new columnadds new columns
        df = add_outlier_columns(df,bounds)

    return df
##################################
##################################


import numpy as np

def plot_2_signals(fig, ax, sig1, sig2,
    fs, label1='Original', label2='Reconstructed',
    xlabel='Time (sec)', ylabel='Amplitude'):
    sig1 = np.asarray(sig1)
    sig2 = np.asarray(sig2)
    n = len(sig1)
    t = np.arange(n) * (1/fs)
    ax.plot(t, sig1, label=label1)
    ax.plot(t, sig2, label=label2)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    ax.legend(loc=1)


"""
第十五章
    生成数据
    安装步骤：p286
    https://pypi.org/project/matplotlib/#files 下载matplotilb
    python -m pip install --user filename.whl
"""

from random import matplotlib as plt

#绘制简单的折线图
"""因为系统默认从x=0出发，因此为了校准图形。我们可以同时给他传入输入值和输出值"""
squares_output = [1,4,9,16,25]
squares = [1,2,3,4,5]
#设置线条的粗细
plt.plot(squares,squares_output,linewidth=5)

#title给表格设置名字，xlabel是x坐标，ylabel是y坐标
plt.title("square number" , fontsize=24)
plt.xlabel("value",fontsize =24)#x轴的名称
plt.ylabel("square of value",fontsize = 24)#y轴的名称

#设置刻度标记的大小（途中x或作y的刻度的表示）
plt.tick_params(axis='both',labelsize =14)
plt.show()


import pandas as pd
name_map = {
    'age': 'Age',
    'gender': 'Gender',
    'cancer': 'Malignancy',                                 # comorbidities
    'liver_disease': 'Liver disease',
    'chf': 'Congestive heart failure',
    'renal_failure': 'Renal failure',
    'pleural_effusion': 'Pleural effusion',
    'orientation': 'Orientation',                                     # physical
    'temperature': 'Temperature (\degree C)',
    'heart_rate': 'Heart rate (beats per minute)',
    'respiratory_rate': 'Respiratory rate (breaths per minute)',
    'bp_systolic': 'Systolic blood pressure (mmHg)',
    'bp_diastolic': 'Diastolic blood pressure (mmHg)',
    'bp_mean_arterial': 'Mean arterial pressure (mmHg)',
    'gcs': 'Glasgow Coma Scale',
    'rbcs': 'Red blood cells (millions/$\mu$L)',                      # is this low?
    'wbc': 'White blood cells (thousands/$\mu$L)',
    'platelets': 'Platelets (thousands/$\mu$L)',
    'hct': 'Hematocrit (%)',                                          # is this low?
    'rdw': 'Red blood cell dist. width (%)',
    'mcv': 'Mean corpuscular volume (fL)',
    'mch': 'Mean corpuscular hemoglobin/ MCH (pg)',
    'mchc': 'MCH concentration (g/dL)',
    'neutrophils': 'Neutrophils (%)',                                 # is this higher than normal?
    'lymphocytes': 'Lymphocytes (%)',                                 # is this lower than normal?
    'monocytes': 'Monocytes (%)',
    'eosinophils': 'Eosinophils (%)',
    'basophils': 'Basophils (%)',
    'bands': 'Band cells (%)',                                        # is this low?
    'sodium': 'Sodium (mmol/L)',
    'potassium': 'Potassium (mmol/L)',
    'chloride': 'Chloride (mmol/L)',
    'bicarbonate': 'Bicarbonate (mmol/L)',
    'bun': 'Blood urea nitrogen (mg/dL)',
    'creatinine': 'Creatinine (mg/dL)',
    'glucose': 'Glucose (mg/dL)',
    'ast': 'Aspartate aminotransferase (units/L)',
    'alt': 'Alanine aminotransferase (units/L)',
    'alkaline_phosphatase': 'Alkaline phosphatase (units/L)',
    'crp': 'C-reactive protein (mg/L)',
    'direct_bilirubin': 'Direct bilirubin (mg/L)',
    'total_bilirubin': 'Total bilirubin (mg/L)',
    'total_protein': 'Total protein (g/dL)',
    'calcium': 'Calcium (mg/dL)',
    'albumin': 'Albumin (g/dL)',
    'troponin': 'Troponin (ng/mL)',
    'pt': 'Prothrombin time (sec)',
    'ptt': 'Partial thromboplastin time (sec)',
    'ph': 'pH',
    'pao2': 'Partial pressure of oxygen (mmHg)',
    'sao2': 'Arterial oxygen saturation (mmHg)',
    'deceased_indicator': 'Deceased',
    'vasopressor_indicator': 'Vasopressors administered',
    'ventilator_indicator': 'Ventilator used'
  }

def get_mean_std_str(df, col, return_range=False, tabs=False):
  dcol = df[col]
  s = col
  if len(col) < 8:
    s += '\t'
  if len(col) < 16:
    s += '\t'
  if len(col) < 24:
    s += '\t'

  if return_range:
    dlower = dcol.quantile(0.25)  # change to iqr
    dupper = dcol.quantile(0.75)
    if col == 'ph':
      s += '\t{0:.2f} ({1:.2f}-{2:.2f})'.format(dcol.median(), dlower, dupper)
    else:
      s += '\t{0:.1f} ({1:.1f}-{2:.1f})'.format(dcol.median(), dlower, dupper)
    # s += '\t{0:.1f} ({1:.1f})'.format(dcol.median(), dupper - dlower)
  else:
    s += '\t{0:.1f} ({1:.1f})'.format(dcol.mean(), dcol.std())

  if not tabs:
    s = s.replace('\t', '').replace(col, '')
  return s

def get_binary_var_str(df, col, missingness=False, tabs=False):
  if col is not None:
    dcol = df[col]
  else:
    dcol = df

  ct = int(dcol.sum())
  prop = ct / float(len(df))
  missing = dcol.isna().sum() / float(len(df))

  s = ''
  if col is not None:
    s += col
    if len(col) < 8:
      s += '\t'
    if len(col) < 16:
      s += '\t'
    if len(col) < 24:
      s += '\t'
  s += '\t{0:d} ({1:.1%})'.format(ct, prop)

  if missingness:
    s += '\t(missingness: {0:.1%})'.format(missing)

  if not tabs:
    s = s.replace('\t', '')
    if col is not None:
      s = s.replace(col, '')
  return s

def make_table1(df, df_name):
  global name_map

  N = len(df)
  indent = '\hspace{5mm}'

  names = ['']
  vals = ['(n = {})'.format(N)]

  def add_pair(key, val):
    names.append(key)
    vals.append(val)

  def add_header(header):
    names.append('\\textbf{' + header + '}')
    vals.append('')

  ## Demographics
  add_header('Demographics')

  # age
  age = df['age']
  r1 = len(df[(df['age'] < 30)])
  r2 = len(df[(df['age'] >= 30) & (df['age'] < 40)])
  r3 = len(df[(df['age'] >= 40) & (df['age'] < 50)])
  r4 = len(df[(df['age'] >= 50) & (df['age'] < 60)])
  r5 = len(df[(df['age'] >= 60)])

  add_pair('Age, years', get_mean_std_str(df, 'age', return_range=True))
  add_pair('Age range, years', '')
  add_pair(indent + '$<$ 30', '{0:d} ({1:.1%})'.format(r1, r1 / float(N)))
  add_pair(indent + '30-39', '{0:d} ({1:.1%})'.format(r2, r2 / float(N)))
  add_pair(indent + '40-49', '{0:d} ({1:.1%})'.format(r3, r3 / float(N)))
  add_pair(indent + '50-59', '{0:d} ({1:.1%})'.format(r4, r4 / float(N)))
  add_pair(indent + '$\\leq$ 60', '{0:d} ({1:.1%})'.format(r5, r5 / float(N)))

  # gender
  gender = df['gender']
  g1 = sum(gender == 'Male') + sum(gender == 'gender:m')
  g2 = sum(gender == 'Female') + sum(gender == 'gender:f')

  add_pair('Gender', '')
  add_pair(indent + 'Male', '{0:d} ({1:.1%})'.format(g1, g1 / float(N)))
  add_pair(indent + 'Female', '{0:d} ({1:.1%})'.format(g2, g2 / float(N)))

  ## Comorbidities
  add_pair('', '')
  add_header('Comorbidities')

  cvars = [
    'pleural_effusion'
  ]
  for col in cvars:
    val = get_binary_var_str(df, col)
    add_pair(name_map[col], val)

  ## Physicals
  add_pair('', '')
  add_header('Physical exam findings')

  # orientation
  orientation = df['orientation']
  o1 = (orientation >= 4).astype(int)
  o2 = (orientation < 4).astype(int)

  add_pair('Orientation', '')
  add_pair(indent + 'oriented', get_binary_var_str(o1, None))
  add_pair(indent + 'confused', get_binary_var_str(o2, None))

  # other physical measurements
  pvars = [
    'temperature', 'heart_rate', 'respiratory_rate',
    'bp_systolic', 'bp_diastolic', 'bp_mean_arterial', 'gcs'
  ]
  for col in pvars:
    val = get_mean_std_str(df, col, return_range=True)
    add_pair(name_map[col], val)

  ## Laboratory findings
  add_pair('', '')
  add_header('Laboratory findings')

  hema_vars = [
    'rbcs', 'wbc', 'platelets', 'hct', 'rdw', 'mcv', 'mch', 'mchc',
    'neutrophils', 'lymphocytes', 'monocytes', 'eosinophils', 'basophils', 'bands',
  ]
  chem_vars = [
    'sodium', 'potassium', 'chloride', 'bicarbonate',
    'bun', 'creatinine', 'glucose',
    'ast', 'alt', 'alkaline_phosphatase', 'crp',
    'direct_bilirubin', 'total_bilirubin', 'total_protein',
    'calcium', 'albumin', 'troponin'
  ]
  coag_vars = ['pt', 'ptt']
  bgas_vars = ['ph', 'pao2', 'sao2']

  add_pair('Hemotology', '')
  for col in hema_vars:
    try:
      add_pair(indent + name_map[col], get_mean_std_str(df, col, return_range=True))
    except Exception as e:
      print(e)
      import pdb;
      pdb.set_trace()

  add_pair('Chemistry', '')
  for col in chem_vars:
    add_pair(indent + name_map[col], get_mean_std_str(df, col, return_range=True))

  add_pair('Coagulation', '')
  for col in coag_vars:
    add_pair(indent + name_map[col], get_mean_std_str(df, col, return_range=True))

  add_pair('Blood gas', '')
  for col in bgas_vars:
    add_pair(indent + name_map[col], get_mean_std_str(df, col, return_range=True))

  ## Outcomes
  add_pair('', '')
  add_header('Outcomes')
  out_vars = [
    'deceased_indicator',
    'vasopressor_indicator',
    'ventilator_indicator'
  ]
  for col in out_vars:
    add_pair(name_map[col], get_binary_var_str(df, col))

  table_df = pd.DataFrame({'Variable': names, df_name: vals})
  return table_df

def print_table1(df, df_name):
  print('==================== TABLE FOR {} ================='.format(df_name))
  N = len(df)
  print('Patients (n = {})\n'.format(N))

  age = df['age']
  print('Age missingness*:\t{0:d} ({1:.1%})'.format(age.isna().sum(), round(age.isna().sum() / float(N), 3)))
  print('Age, years\t\t{0:.1f} ({1:.1f})'.format(age.mean(), age.std()))
  print('Age range, years')
  r = len(df[(df['age'] < 30)])
  print('\t<30\t\t{0:d} ({1:.1%})'.format(r, r / float(N)))
  r = len(df[(df['age'] >= 30) & (df['age'] < 40)])
  print('\t30-39\t\t{0:d} ({1:.1%})'.format(r, r / float(N)))
  r = len(df[(df['age'] >= 40) & (df['age'] < 50)])
  print('\t40-49\t\t{0:d} ({1:.1%})'.format(r, r / float(N)))
  r = len(df[(df['age'] >= 50) & (df['age'] < 60)])
  print('\t50-59\t\t{0:d} ({1:.1%})'.format(r, r / float(N)))
  r = len(df[(df['age'] >= 60)])
  print('\t>= 60\t\t{0:d} ({1:.1%})'.format(r, r / float(N)))

  gender = df['gender']
  print('\nGender missingness*: \t{0:d} ({1:.1%})'.format(gender.isna().sum(), round(gender.isna().sum() / float(N), 3)))
  print('Gender')
  g = sum(gender == 'Male')
  print('\tMale\t\t{0:d} ({1:.1%})'.format(g, g / float(N)))
  g = sum(gender == 'Female')
  print('\tFemale\t\t{0:d} ({1:.1%})'.format(g, g / float(N)))

  print('\nLab values')
  print('(compare w/ washington state)')
  lvars = [  # compare w/ washington state
    'wbc', 'lymphocytes', 'hemoglobin', 'platelets', 'sodium',
    'creatinine', 'total_bilirubin',
    'alkaline_phosphatase', 'ast', 'alt', 'troponin'
  ]
  for col in lvars:
    print(get_mean_std_str(df, col))
  print('\n(remaining lab values)')
  lvars = [  # compare w/ washington state
    'bun', 'temperature',
    'rbcs', 'hct', 'rdw', 'mcv', 'mch', 'mchc',
    'neutrophils', 'monocytes', 'eosinophils', 'basophils',
    'ph', 'glucose', 'pao2', 'fio2', 'crp',
    'direct_bilirubin', 'total_protein', 'albumin',
    'ferritin', 'pt', 'ptt', 'fibrinogen',
    'bands', 'bicarbonate', 'calcium',
    'chloride', 'potassium', 'heart_rate', 'sao2', 'gcs', 'respiratory_rate',
    'bp_systolic', 'bp_diastolic', 'bp_mean_arterial',
  ]
  for col in lvars:
    print(get_mean_std_str(df, col, return_range=True))

  print('\nComorbidities')
  cvars = [
    'smoking', 'cancer', 'liver_disease', 'chf',
    'renal_failure', 'pleural_effusion'
  ]
  for col in cvars:
    print(get_binary_var_str(df, col))

  print('\nOrientation')
  print(get_mean_std_str(df, 'orientation'))

  print('\nOutcomes')
  outvars = [
    'censor_or_deceased_days',
    'censor_or_vasopressor_days',
    'censor_or_ventilator_days',
    'deceased_indicator',
    'vasopressor_indicator',
    'ventilator_indicator'
  ]
  for col in outvars:
    if 'indicator' in col:
      print(get_binary_var_str(df, col))
    else:
      print(get_mean_std_str(df, col))

  import pdb; pdb.set_trace()
  #
  #
  # df[['gender']]
  #
  # labs = ['wbc', ]
  #
  # tab = df[]
  #
  # print('Values are n (%) or mean (SD) unless otherwise specified.')

"""Full list of variables:
'rbcs', 'wbc', 'platelets',
'hemoglobin', 'hct', 'rdw', 'mcv', 'mch', 'mchc', 'neutrophils',
'lymphocytes', 'monocytes', 'eosinophils', 'basophils', 'bun',
'temperature', 'ph', 'sodium', 'glucose', 'pao2', 'fio2', 'ldh', 'crp',
'direct_bilirubin', 'total_bilirubin', 'total_protein', 'albumin',
'ferritin', 'pt', 'ptt', 'fibrinogen', 'ast', 'alt', 'creatinine',
'troponin', 'alkaline_phosphatase', 'bands', 'bicarbonate', 'calcium',
'chloride', 'potassium', 'gender', 'age', 'ethnicity',
'observation_offset', 'heart_rate', 'sao2', 'gcs', 'respiratory_rate',
'bp_systolic', 'bp_diastolic', 'bp_mean_arterial', 'smoking', 'cancer',
'liver_disease', 'chf', 'renal_failure', 'pleural_effusion',
'orientation', 'censor_or_deceased_days', 'deceased_indicator',
'censor_or_vasopressor_days', 'vasopressor_indicator',
'censor_or_ventilator_days', 'ventilator_indicator'
"""

eicu_template = '../data/eicu/eicu_cleaned/eicu_anypna_{}_days_post_inicu.csv'
mimic_template = '../data/mimic/mimic_cleaned/cleaner_mimic_anypna_timeline_flfv_{}_days_post_inicu.csv'

# make anypna table
table_cols = [
  # (eicu_template.format(0), 'eICU Day 0'),
  # (eicu_template.format(1), 'eICU Day 1'),
  (eicu_template.format(2), 'eICU Day 2'),
  # (mimic_template.format(0), 'MIMIC Day 0'),
  # (mimic_template.format(1), 'MIMIC Day 1'),
  (mimic_template.format(2), 'MIMIC Day 2'),
]

tte_names = ['Time to death (days)',
             'Time to administering vasopressors (days)',
             'Time to ventilation (days)']
median_ttes = {
  'eICU Day 0': ['58.7', 'NA', '21.0'],
  'eICU Day 1': ['57.7', 'NA', '14.5'],
  'eICU Day 2': ['56.7', 'NA', 'NA'],
  'MIMIC Day 0': ['NA', '12.7', '1.78'],
  'MIMIC Day 1': ['NA', '24.3', '3.71'],
  'MIMIC Day 2': ['NA', '28.6', '3.87'],
}

# iterate through columns and format rows of latex table
table_df = None
filter_neg_outcomes = True
missingness_df = None
for fpath, name in table_cols:
  if 'mimic' in name.lower():
    df = pd.read_csv(fpath, '|')
    if filter_neg_outcomes:
      df = df[df['censor_or_deceased_days'] > 0]
      df = df[df['age'] >= 18]
  else:
    df = pd.read_csv(fpath)
    if filter_neg_outcomes:
      df = df[df['censor_or_deceased_days'] > 0]

  # make missingness df
  vars1 = ['age', 'gender', 'pleural_effusion', 'orientation']
  pvars = [
    'temperature', 'heart_rate', 'respiratory_rate',
    'bp_systolic', 'bp_diastolic', 'bp_mean_arterial', 'gcs'
  ]
  hema_vars = [
    'rbcs', 'wbc', 'platelets', 'hct', 'rdw', 'mcv', 'mch', 'mchc',
    'neutrophils', 'lymphocytes', 'monocytes', 'eosinophils', 'basophils', 'bands',
  ]
  chem_vars = [
    'sodium', 'potassium', 'chloride', 'bicarbonate',
    'bun', 'creatinine', 'glucose',
    'ast', 'alt', 'alkaline_phosphatase', 'crp',
    'direct_bilirubin', 'total_bilirubin', 'total_protein',
    'calcium', 'albumin', 'troponin'
  ]
  coag_vars = ['pt', 'ptt']
  bgas_vars = ['ph', 'pao2', 'sao2']
  out_vars = [
    'deceased_indicator',
    'vasopressor_indicator',
    'ventilator_indicator'
  ]
  all_vars = vars1 + pvars + hema_vars + chem_vars + coag_vars + bgas_vars + out_vars

  if missingness_df is None:
    print(name)
    miss_frac = (df[all_vars].isna().sum() / len(df)).tolist()
    miss_cts = (df[all_vars].isna().sum()).tolist()
    miss_summ = list(['{} ({})'.format(round(f, 3), round(c, 3)) for (f, c) in zip(miss_frac, miss_cts)])
    missingness_df = {'Variable': [name_map[v] for v in all_vars]}
    missingness_df[name + ' (n = {})'.format(len(df))] = miss_summ
  else:
    print(name)
    miss_frac = (df[all_vars].isna().sum() / len(df)).tolist()
    miss_cts = (df[all_vars].isna().sum()).tolist()
    miss_summ = list(['{} ({})'.format(round(f, 3), round(c, 3)) for (f, c) in zip(miss_frac, miss_cts)])
    missingness_df[name + ' (n = {})'.format(len(df))] = miss_summ
    missingness_df = pd.DataFrame(missingness_df)

  # make table 1
  table = make_table1(df, name)
  if table_df is None:
    table_df = table
  else:  # iteratively add column to table
    assert(table_df['Variable'].tolist() == table['Variable'].tolist())
    table = table.drop('Variable', axis=1)
    table_df = pd.concat((table_df, table), axis=1)

# add median time to event values
tte_rows = {'Variable': tte_names}
for col in table_df.columns:
  table_df[col] = table_df[col].str.replace('%', '\\%')  # escape %
  if col in median_ttes:
    tte_rows[col] = tte_rows.get(col, []) + median_ttes[col]
tte_df = pd.DataFrame(tte_rows)
table_df = table_df.append(tte_df)

print('======================= table 1 ===============================')
print(table_df.to_latex(index=False, escape=False))
import pdb; pdb.set_trace()
print('======================= missingness ===============================')
print(missingness_df.to_latex(index=False))


import json

def load_json_data(file_path):
    """
    Returns the json data from given json file path.
    """

    try:
        with open(file_path) as f:
            data = json.load(f)
    except:
        return None

    return data


import collections
import numpy as np
from sklearn.externals import joblib
from news_scraper_update.utils.relevance_score import get_tags, preprocess

def tag_documents(doc, bad_tags=[]):
    word2tag, _ = get_tags()
    tag = ''
    c = collections.Counter()
    for word in word2tag:
        if word2tag[word] in bad_tags:
            continue
        if (len(word) <= 3 and ' ' + word + ' ' in doc) or (len(word) >= 4 and ' ' + word in doc):
            c[word2tag[word]] += 1
    if len(c) > 0:
        tag = max(c, key=c.get)
    else:
        tag = 'undefined'
    return tag

def get_genres(key_event, tfidf, tagger_model):
    doc = key_event['title'] + ' ' + key_event['description']
    small_topics = ['Cloud Computing', 'Automotive', 'Machine Learning', 'Intellectual Property', 'Startup', 'Trade',
                    'Corruption', 'Taxation', 'Credit Market', 'Fraud', 'Blockchain', 'Recession', 'Economic data',
                    'Fixed Income', 'Emerging market', 'Technical Analysis', 'Antitrust', 'Tariff']
    best_tag = tag_documents(doc)
    vec = tfidf.transform([preprocess(doc)]).toarray()
    probs = tagger_model.predict_proba(vec).flatten()
    l1 = np.argsort(probs)
    l = [ i for i, prob in enumerate(probs) if prob >= 0.19 ]
    if len(l) == 0 and l1[-1] > 0.091:
        l = [l1[-1]]
        if probs[l1[-1]] <= .2 and probs[l1[-2]] > .1:
            l.append(l1[-2])
    if len(l) == 1 and probs[l1[-1]] >= .43 and probs[l1[-2]] >= .1:
        l.append(l1[-2])
        if probs[l1[-3]] >= .095:
            l.append(l1[-3])
    doctags = list(tagger_model.classes_[l])
    if best_tag in small_topics and best_tag not in doctags:
        doctags.append(best_tag)
    return doctags


import wifiLIB
import DHT2G

from machine import Timer

tim = Timer(-1)
tim.init(period=5000, mode=Timer.ONE_SHOT, callback=lambda t:print(1))
# tim.init(period=5000, mode=Timer.PERIODIC, callback=lambda t:repeat5())

import time
start = time.ticks_ms() # get millisecond counter
delta = time.ticks_diff(time.ticks_ms(), start) # compute time difference
start = time.ticks_ms()
while False:
    TS = int(time.ticks_diff(time.ticks_ms(), start)/1000)
    print(TS)
    time.sleep(2)

def repeat5():
    print(5)


from lib.booking import Booking
from datetime import timedelta

class BookingRepository:
    def __init__(self, connection):
        self.connection = connection

    def all(self):
        rows = self.connection.execute('SELECT * FROM bookings')
        bookings = []
        for row in rows:
            bookings.append(Booking(row["id"], row["start_date"], row["end_date"], row["property_id"], row["user_id"]))
        return  bookings

    def find(self, id):
        row = self.connection.execute('SELECT * FROM bookings WHERE id=%s', [id])
        row = row[0]
        return Booking(row["id"], row["start_date"], row["end_date"], row["property_id"], row["user_id"])

    def create(self,booking):
        row = self.connection.execute('INSERT INTO bookings(start_date, end_date, property_id, user_id) VALUES (%s,%s,%s,%s) RETURNING id',[
            booking.start_date, booking.end_date, booking.property_id, booking.user_id])
        booking.id = row[0]["id"]
        return booking

    def find_by_property_id(self, property_id):
        rows = self.connection.execute('SELECT * FROM bookings WHERE property_id=%s', [property_id])
        bookings_for_property_id = []
        for row in rows:
            bookings_for_property_id.append(Booking(row['id'], row["start_date"], row["end_date"], row["property_id"], row["user_id"]))
        return bookings_for_property_id

    def date_range_maker(self, start_date, end_date):
        dates = []
        delta = timedelta(days=1)

        while start_date <= end_date:
            dates.append(start_date)
            start_date += delta
        return dates

    def availability_checker(self, booking_to_check):
        bookings = self.find_by_property_id(booking_to_check.property_id)
        booked_dates = []
        for booking in bookings:
            booked_dates.extend(self.date_range_maker(booking.start_date, booking.end_date))
        dates_to_check = self.date_range_maker(booking_to_check.start_date, booking_to_check.end_date)
        for date in dates_to_check:
            if date in booked_dates:
                return False
        return True

    def find_bookings_with_property_name_and_booking_dates_by_user_id(self, user_id):
        bookings = []
        rows = self.connection.execute(
            "SELECT properties.name, bookings.start_date, bookings.end_date " \
        "FROM bookings JOIN properties ON bookings.property_id=properties.id " \
        "WHERE bookings.user_id=%s",[user_id])
        for row in rows:
            bookings.append(f"{row['name']} - {row['start_date']} - {row['end_date']}")
        return bookings


from time import sleep
from random import randint
import numpy

snake_body_color = [0xff, 0x00, 0x00]
snake_head_color = [0x63, 0x03, 0x03] #dark red
wall_color = [0x00, 0x00, 0xff] #blue
food_color = [0xe0, 0xb1, 0x16] #orange

WIDTH = 16
HEIGHT = 10

snake = [] # Initial snake co-ordinates
wall = []
food = [3,3]

KEY_UP=1
KEY_DOWN=2
KEY_LEFT=3
KEY_RIGHT=4

neighbors = [(0, 1), (0, -1), (1, 0), (-1, 0)]

def init():
	global snake
	global wall
	snake = [[4,10], [4,9], [4,8], [4,7]]
	wall = []
	for i in range(HEIGHT):
		wall.append([wall_color] * WIDTH)

def display(wall):
	frame = fromListToLedframe(wall)
	printLedWall(frame)

def printLedWall(Byteframe):

	file = open("/tmp/lwfb", "wb")
	# print("file opend")
	file.write(Byteframe)
	file.close()
	# print("file closed")
	return True

def fromListToLedframe(wall):
	wall_format = []
	for i in range(HEIGHT):
		for j in range(WIDTH):
			wall_format.append(wall[i][j][0])
			wall_format.append(wall[i][j][1])
			wall_format.append(wall[i][j][2])

	b_wall_format = bytearray(wall_format)

	return b_wall_format

def displaySnake(snake, wall):
	i = 0
	for value in snake:
		if (i == 0):
			wall[value[0]][value[1]] = snake_head_color
		else:
			wall[value[0]][value[1]] = snake_body_color
		i+=1

def displayFood(food, wall):
	wall[food[0]][food[1]] = food_color

def getObstaclesGrid():
	global snake
	grid = []
	for i in range(HEIGHT):
		grid.append([0] * WIDTH)
	for pos in snake:
		grid[pos[0]][pos[1]] = 1
	return grid

def h(a, b):
    """Return distance between 2 points"""
    return (b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2

def find_path(start, end, grid):
    """Return path between 2 points
    example :
    grid = [
        [0, 0, 0, 0],
        [0, 1, 0, 0],
        [0, 0, 1, 0],
        [0, 0, 0, 0],
    ]
    path = find_path((3,3), (0, 0), grid)
    """
    array = numpy.array(grid)

    open_list = []
    close_list = []
    came_from = {}

    # Add F score in open_list
    gscore = {start:0}
    fscore = {start:h(start, end)}
    open_list.append((fscore[start], start))

    while open_list:

        # Get min F value in open_list
        minf = min(open_list, key=lambda o: o[0])
        current = minf[1]
        open_list.remove(minf)

        # Return path
        if current == end:
            data = []
            while current in came_from:
                data.append(list(current))
                current = came_from[current]
            return data[::-1]

        # Add current node in close_list
        close_list.append(current)

        # For neighbors
        for i, j in neighbors:
            neighbor = current[0] + i, current[1] + j
            tentative_g_score = gscore[current] + h(current, neighbor)

            # Check neighbor value
            if 0 <= neighbor[0] < array.shape[0]:
                if 0 <= neighbor[1] < array.shape[1]:
                    if array[neighbor[0]][neighbor[1]] == 1 and (neighbor[0], neighbor[1]) != end:
                        continue
                else:
                    continue
            else:
                continue

            # Continue if neighbor is in close list
            if neighbor in close_list and tentative_g_score >= gscore.get(neighbor, 0):
                continue

            # If neighbor is not in open_list
            if tentative_g_score < gscore.get(neighbor, 0) or neighbor not in [i[1]for i in open_list]:
                came_from[neighbor] = current
                gscore[neighbor] = tentative_g_score
                fscore[neighbor] = tentative_g_score + h(neighbor, end)
                open_list.append((fscore[neighbor], neighbor))

    return []

init()
grid = [
	[0, 0, 0, 0],
	[0, 1, 0, 0],
	[0, 0, 1, 0],
	[0, 0, 0, 0],
]

path = []

while(True):

	newPos = [-1, -1]

	path = find_path(tuple(snake[0]), tuple(food), getObstaclesGrid())
	if (len(path) > 0):
		newPos = path[0]
		path.pop(0)
	else:
		print("random !!")
		key = 1
		failed = 0
		while True:
			key = randint(1, 4)
			newPos[0] = snake[0][0] + (key == KEY_DOWN and 1) + (key == KEY_UP and -1)
			newPos[1] = snake[0][1] + (key == KEY_LEFT and -1) + (key == KEY_RIGHT and 1)

			if (newPos in snake or newPos[0] < 0 or newPos[0] > HEIGHT-1 or newPos[1] < 0 or newPos[1] > WIDTH-1
			# or food == newPos
			):
				failed+=1
				if (failed > 20):
					init()
					break
			else:
				break

		if (failed > 20):
			continue

	snake.insert(0, newPos)

	# WALL[LIGNE][COLONNE]

	if snake[0] == food:   # When snake eats the food
		food = []
		while food == []:
			food = [randint(0, HEIGHT-1), randint(0, WIDTH-1)]   # Calculating next food's coordinates
			if food in snake: food = []
	else:
		last = snake.pop()
		wall[last[0]][last[1]] = wall_color

	displayFood(food, wall)
	displaySnake(snake, wall)
	display(wall)

	sleep(0.2)