import sys
import os
import argparse
import torch
import torch.nn as nn
from thop import profile
from train import test
from utils.registry import network_handler, dataset_handler
from utils.graph import *

def trace_add_node(add_node):
    assert(len(add_node.prev_nodes) > 0)
    for node in add_node.prev_nodes:
        if isinstance(node, AddNode):
            non_add_node = trace_add_node(node)
        else:
            non_add_node = node
            break
    return non_add_node

def compute_out_idx_from_mask(mask):
    if mask.dim() == 4:
        output_idx = torch.sum(mask, (1, 2, 3)).nonzero().squeeze(-1)
    elif mask.dim() == 2:
        output_idx = torch.sum(mask, 1).nonzero().squeeze(-1)
    elif mask.dim() == 1:
        output_idx = mask.nonzero().squeeze(-1)
    else:
        raise NotImplementedError(
            'Unexpected mask dimensions: `{}`'.format(mask.dim()))
    return output_idx

def parse_arguments():
    parser = argparse.ArgumentParser(description='Evalaute the pruned networks')
    parser.add_argument('--network_name', type=str, required=True,
                        help='Network name for evaluation.')
    parser.add_argument('--dataset_name', type=str, required=True,
                        help='Dataset name for evaluation.')
    parser.add_argument('--target_mode', type=str, required=True,
                        help='Target mode of pruning.')
    parser.add_argument('--prune_ratio', type=float, required=True,
                        help='Prune ratio of the pruned network.')
    args = parser.parse_args()
    return args

def main(*args, **kwargs):

    # ----------------------------------------
    # General settings
    # ----------------------------------------
    args = parse_arguments()
    network_name = args.network_name
    dataset_name = args.dataset_name
    target_mode = args.target_mode
    prune_ratio = args.prune_ratio
    assert(prune_ratio > 0.0), 'Only evaluate networks with prune_ratios > 0.0'

    image_size  = (224, 224)
    batch_size  = 128
    num_classes = 1000
    chkpt_path  = os.path.join('CHECKPOINTS/{}_{}/{}/ratio_{}.pth'.format(
        network_name, dataset_name, target_mode, prune_ratio))

    # ----------------------------------------
    # Model settings
    # ----------------------------------------
    origin_model = network_handler[network_name](num_classes=num_classes)
    pruned_model = network_handler[network_name](num_classes=num_classes, channel_ratio=1.0-prune_ratio)
    chkpt_content = torch.load(chkpt_path)
    origin_model.load_state_dict(chkpt_content['state_dict'])
    masks = chkpt_content['masks']
    graph = origin_model.build_graph()
    origin_state_dict = origin_model.state_dict()

    for name, params in pruned_model.named_parameters():
        device = params.device
        module_name = name[:name.rfind('.')]
        curr_node = graph.node_dict[module_name]

        assert(len(curr_node.prev_nodes) in [0, 1])
        prev_node = None if (len(curr_node.prev_nodes) == 0) else curr_node.prev_nodes[0]

        if isinstance(prev_node, AddNode):
            prev_node = trace_add_node(prev_node)

        if curr_node.module_type == 'conv':
            assert(params.dim() in [1, 4])
            out_chs = compute_out_idx_from_mask(masks[curr_node.module_name]).to(device)
            if params.dim() == 4:  # weight
                if prev_node is not None:
                    inp_chs = compute_out_idx_from_mask(masks[prev_node.module_name]).to(device)

                pruned_params = origin_state_dict[name].index_select(0, out_chs)
                if (prev_node is not None) and (curr_node.info['groups'] == 1):  # Non-First Layer and Non-Depthwise Conv
                    pruned_params = pruned_params.index_select(1, inp_chs)

            else:                  # bias
                pruned_params = origin_state_dict[name].index_select(0, out_chs)

        elif curr_node.module_type == 'bn':
            assert(params.dim() == 1)
            out_chs = compute_out_idx_from_mask(masks[curr_node.module_name]).to(device)
            pruned_params = origin_state_dict[name].index_select(0, out_chs)

        elif curr_node.module_type == 'fc':
            assert(params.dim() in [1, 2])
            out_chs = compute_out_idx_from_mask(masks[curr_node.module_name]).to(device)
            if params.dim() == 2:  # weight
                if prev_node is not None:
                    inp_chs = compute_out_idx_from_mask(masks[prev_node.module_name]).to(device)

                pruned_params = origin_state_dict[name].index_select(0, out_chs)
                if prev_node is not None:
                    pruned_params = pruned_params.index_select(1, inp_chs)

            else:                  # bias
                pruned_params = origin_state_dict[name].index_select(0, out_chs)

        else:
            raise NotImplementedError('Unexpected module_type `{}`'.format(
                curr_node.module_type))

        params.data.copy_(pruned_params)

    for name, buffers in pruned_model.named_buffers():
        device = params.device
        module_name = name[:name.rfind('.')]
        curr_node = graph.node_dict[module_name]

        assert(curr_node.module_type == 'bn')
        if 'num_batches_tracked' in name:
            buffers.data.copy_(origin_state_dict[name])
        else:
            out_chs = compute_out_idx_from_mask(masks[curr_node.module_name]).to(device)
            pruned_buffers = origin_state_dict[name].index_select(0, out_chs)
            buffers.data.copy_(pruned_buffers)

    # ----------------------------------------
    # Evaluate the origin and pruned models
    # ----------------------------------------
    device = 'cuda'
    origin_model.to(device)
    pruned_model.to(device)
    origin_model.eval()
    pruned_model.eval()
    _, test_loader, num_classes = dataset_handler[dataset_name](
            batch_size, batch_size, image_size[0], num_workers=24, pin_memory=True)

    criterion = nn.CrossEntropyLoss()
    test_loss, test_acc = test(pruned_model, device, test_loader, criterion, -1)
    print('Test Acc@1: {:.4f}, Test Acc@5: {:.4f}'.format(*test_acc))

    images = torch.randn(*((1, 3)+image_size)).to(device)
    flops1, params1 = profile(origin_model, inputs=(images,), verbose=False)
    flops2, params2 = profile(pruned_model, inputs=(images,), verbose=False)

    print('Full Network FLOPs: {:.4f} M, Params: {:.4f} M'.format(flops1/(1000**2), params1/(1000**2)))
    print('Pruned Network FLOPs: {:.4f} M, Params: {:.4f} M'.format(flops2/(1000**2), params2/(1000**2)))
    return

if __name__ == '__main__':
    main()


from swigibpy import EWrapper
import time
from swigibpy import EPosixClientSocket, ExecutionFilter
from ibutils.IButils import bs_resolve, action_ib_fill

FILL_CODE = 1234
MEANINGLESS_NUMBER = 999999
MAX_WAIT = 99999

from core import config
globalconf = config.GlobalConfig()
log = globalconf.log

def return_IB_connection_info():
    """
    Returns the tuple host, port, clientID required by eConnect
    """
    host = ""
    port = ""
    clientid = ""
    return (host, port, clientid)

class IBWrapper(EWrapper):
    """
    Callback object passed to TWS, these functions will be called directly by TWS.
    """
    def __init__(self):
        self.nextValidId = 1

    def init_error(self):
        setattr(self, "flag_iserror", False)
        setattr(self, "error_msg", "")

    def init_time(self):
        setattr(self, "data_the_time_now_is", None)

    def init_fill_data(self):
        setattr(self, "data_fill_data", {})
        setattr(self, "data_comm_data", {})
        setattr(self, "flag_fill_data_finished", False)

    def error(self, id, errorCode, errorString):
        """
        This event is called when there is an error with the communication or when TWS wants to send a message to the client.
        """
        errors_to_trigger=globalconfig.get_list_errors_to_trigger_ib()
        if errorCode in errors_to_trigger:
            errormsg="IB error id %d errorcode %d string %s" %(id, errorCode, errorString)
            print (errormsg)
            setattr(self, "flag_iserror", True)
            setattr(self, "error_msg", errormsg)

    def currentTime(self, time_from_server):
        """
        This function receives the current system time on the server side.
        """
        setattr(self, "data_the_time_now_is", time_from_server)

    def nextValidId(self, orderId):
        """
        This function is called after a successful connection to TWS.
        """
        self.nextValidId=orderId

    def managedAccounts(self, openOrderEnd):
        """
        This function is called when a successful connection is made to an account.
        It is also called when the reqManagedAccts() function is invoked.
        """
        pass

    def orderStatus(self, reqid, status, filled, remaining, avgFillPrice, permId,
            parentId, lastFilledPrice, clientId, whyHeld):
        """
        This event is called whenever the status of an order changes. It is also fired after reconnecting
        to TWS if the client has any open orders.
        """
        pass

    def add_fill_data(self, reqId, execdetails):
        if "data_fill_data" not in dir(self):
            filldata={}
        else:
            filldata=self.data_fill_data
        if reqId not in filldata.keys():
            filldata[reqId]={}
        execid=execdetails['orderid']
        filldata[reqId][execid]=execdetails
        setattr(self, "data_fill_data", filldata)

    def add_comm_data(self, execId, commdetails):
        if "data_comm_data" not in dir(self):
            commdata={}
        else:
            commdata=self.data_comm_data
        if execId not in commdata.keys():
            commdata[execId]={}
        execid=commdetails['execId']
        commdata[execId][execId]=commdetails
        setattr(self, "data_comm_data", commdata)

    def commissionReport(self, commissionReport):
        print("CommisionReport is being called ...")
        commision=commissionReport.commision
        currency=commissionReport.currency
        execId=commissionReport.execId
        realizedPNL=commissionReport.realizedPNL
        thisyield=0 # ERROR PY yield is reserved word commissionReport.yield
        yieldRedemptionDate=commissionReport.yieldRedemptionDate
        commdetails=dict(commision=str(commision),currency=str(currency),
        execId=str(execId),realizedPNL=str(realizedPNL),theyield=str(thisyield),
        yieldRedemptionDate=str(yieldRedemptionDate))
        ## This is just execution data we've asked for
        self.add_comm_data(execId, commdetails)

    def execDetails(self, reqId, contract, execution):
        print("execDetails is being called ...")
        """
        This is called if
        a) we have submitted an order and a fill has come back
        b) We have asked for recent fills to be given to us
        We populate the filldata object and also call action_ib_fill in case we need to do something with the
          fill data
        See API docs, C++, SocketClient Properties, Contract and Execution for more details
        """
        reqId=int(reqId)
        execid=execution.execId
        exectime=execution.time
        thisorderid=int(execution.orderId)
        account=execution.acctNumber
        exchange=execution.exchange
        permid=execution.permId
        avgprice=execution.price
        cumQty=execution.cumQty
        clientid=execution.clientId
        symbol=contract.symbol
        expiry=contract.expiry
        side=execution.side
        execdetails=dict(side=str(side), times=str(exectime), orderid=str(thisorderid), qty=int(cumQty),
        price=float(avgprice), symbol=str(symbol), expiry=str(expiry), clientid=str(clientid),
        execid=str(execid), account=str(account), exchange=str(exchange), permid=int(permid))

        if reqId==FILL_CODE:
            ## This is a fill from a trade we've just done
            action_ib_fill(execdetails)

        else:
            ## This is just execution data we've asked for
            self.add_fill_data(reqId, execdetails)

    def execDetailsEnd(self, reqId):
        """
        No more orders to look at if execution details requested
        """
        setattr(self, "flag_fill_data_finished", True)

class IBclient(object):
    def __init__(self, callback,accountid="DU242089"):
        tws = EPosixClientSocket(callback)
        (host, port, clientid)=return_IB_connection_info()
        tws.eConnect(host, port, clientid)
        self.tws=tws
        self.accountid=accountid
        self.cb=callback

    def get_fills(self, reqId=MEANINGLESS_NUMBER):
        print("Getting fills...")
        assert type(reqId) is int

        self.cb.init_fill_data()
        self.cb.init_error()
        self.cb.init_time()

        self.tws.reqExecutions(reqId,ExecutionFilter())

        start_time=time.time()
        iserror=False
        finished=False

        while not finished and not iserror:
            finished=self.cb.flag_fill_data_finished
            iserror=self.cb.flag_iserror
            if (time.time() - start_time) > MAX_WAIT:
                finished=True
            pass

        if iserror:
            print ("Error happened")
            print (self.cb.error_msg)

        execlist=self.cb.data_fill_data[reqId]
        commlist=1 #self.cb.data_comm_data[reqId]
        return (execlist,commlist)

    def speaking_clock(self):
        print ("Getting the time... ")

        self.tws.reqCurrentTime()

        start_time=time.time()

        self.cb.init_error()
        self.cb.init_time()

        iserror=False
        not_finished=True

        while not_finished and not iserror:
            """
            The issue we have here is that the IB API is very much set up as an event driven process.
             So not like normal sequential code like function A calling function B and function A
             returning the answer back. No instead we have function A just kind of hanging around
             waiting for function B and then somehow by magic function B just happens.

            That is not the way I do stuff. I don't have to - I am running fairly slow trading
            systems not intraday high frequency stuff that needs to react to every tick for which
            an event driven system makes sense. Also it goes against my ethical beliefs. Why should
            function A have to wait for function B to run? What makes function B so special? Its
            just rudeness.

            So what we have to do is make the client-server relationship appear sequential, at
            least to anything sitting outside the wrapper module. That also means we need to handle
            the conditions of the thing not finishing in a reasonable time and finishing with an error.
            """
            not_finished=self.cb.data_the_time_now_is is None
            iserror=self.cb.flag_iserror

            if (time.time() - start_time) > MAX_WAIT:
                not_finished=False

            if iserror:
                not_finished=False

        if iserror:
            print ("Error happened")
            print (self.cb.error_msg)

        return self.cb.data_the_time_now_is


import sys

n, m = map(int, input().split())
cant = [sys.stdin.readline().rstrip('\n') for i in range(n+m)]
cant.sort()

count = 0
cantHearSee = []

for i in range(n+m-1):
    if cant[i] == cant[i+1]:
        count += 1
        cantHearSee.append(cant[i])

print(count)
for x in cantHearSee:
    print(x)


from __future__ import division, print_function
from braviz.utilities import set_pyqt_api_2
set_pyqt_api_2()

import PyQt4.QtGui as QtGui
import PyQt4.QtCore as QtCore

from braviz.interaction.qt_guis.parallel_coordinates import Ui_parallel_coordinates
from braviz.interaction.qt_models import VarListModel
from braviz.interaction.qt_dialogs import SelectOneVariableWithFilter
from braviz.interaction.sample_select import SampleLoadDialog
from braviz.readAndFilter.config_file import get_config

import braviz.readAndFilter.tabular_data as braviz_tab_data

import logging

__author__ = 'da.angulo39'

class ParallelCoordinatesApp(QtGui.QMainWindow):

    def __init__(self, scenario, broadcast, receive):

        super(ParallelCoordinatesApp, self).__init__()
        self.ui = None
        self.url = None
        self.broadcast = broadcast
        self.receive = receive

        config = get_config(__file__)
        def_vars = config.get_default_variables()
        def_var_codes = map(def_vars.get, ("nom2", "ratio1", "ratio2"))
        def_var_codes = map(braviz_tab_data.get_var_idx, def_var_codes)
        def_var_codes = filter(lambda x: x is not None, def_var_codes)
        self.cathegorical_var = braviz_tab_data.get_var_idx(def_vars["nom1"])

        self.attributes = def_var_codes
        self.sample_id = None
        self.vars_model = VarListModel(checkeable=True)
        self.vars_model.select_items(self.attributes)

        self.generate_url()
        self.setup_ui()

    def setup_ui(self):
        self.ui = Ui_parallel_coordinates()
        self.ui.setupUi(self)

        self.ui.vars_list.setModel(self.vars_model)
        self.vars_model.CheckedChanged.connect(self.vars_changed)
        self.ui.search_box.returnPressed.connect(self.filter_list)

        self.ui.cathegory_combo.addItem(
            braviz_tab_data.get_var_name(self.cathegorical_var))
        self.ui.cathegory_combo.insertSeparator(
            self.ui.cathegory_combo.count())
        self.ui.cathegory_combo.addItem("<Select cathegory>")
        self.ui.cathegory_combo.activated.connect(self.change_cathegory)
        self.ui.cathegory_combo.setCurrentIndex(0)
        self.ui.actionSelect_Sample.triggered.connect(self.set_sample)
        self.ui.clear_button.clicked.connect(self.clear_selection)

        web_view = self.ui.webView
        set=web_view.settings()
        set.setAttribute(set.DeveloperExtrasEnabled,True)

        self.refresh_web_view()

    def refresh_web_view(self):
        self.ui.webView.load(QtCore.QUrl(self.url))
        link = 'open in web browser: %s' % (
            self.url, self.url)
        self.ui.url_label.setText(link)

    def generate_url(self):
        all_vars = [self.cathegorical_var] + self.attributes
        str_vars = ",".join(map(str, all_vars))
        url = "http://127.0.0.1:8100/parallel?vars=%s" % str_vars
        if self.sample_id is not None:
            url += "&sample=%s" % self.sample_id
        self.url = url
        return url

    def vars_changed(self):
        self.attributes = [
            braviz_tab_data.get_var_idx(v) for v in sorted(self.vars_model.checked_set)]
        self.generate_url()
        self.refresh_web_view()

    def filter_list(self):
        mask = "%%%s%%" % self.ui.search_box.text()
        self.vars_model.update_list(mask)

    def resizeEvent(self, *args, **kwargs):
        # super(ParallelCoordinatesApp,self).resizeEvent(*args,**kwargs)
        self.refresh_web_view()

    def change_cathegory(self):
        if self.ui.cathegory_combo.currentIndex() == self.ui.cathegory_combo.count() - 1:
            # print "dispatching dialog"
            params = {}
            dialog = SelectOneVariableWithFilter(
                params, accept_real=False, accept_nominal=True)
            selection = dialog.exec_()
            logger = logging.getLogger(__name__)
            logger.info("Cathegories selection %s", params)
            if selection > 0:
                var = params["selected_outcome"]
                self.ui.cathegory_combo.insertItem(0, var)
                self.ui.cathegory_combo.setCurrentIndex(0)
            else:
                return
        else:
            var = self.ui.cathegory_combo.itemText(
                self.ui.cathegory_combo.currentIndex())
        self.cathegorical_var = braviz_tab_data.get_var_idx(var)
        self.generate_url()
        self.refresh_web_view()

    def set_sample(self):
        dialog = SampleLoadDialog()
        res = dialog.exec_()
        if res == dialog.Accepted:
            new_sample = dialog.current_sample_idx
            self.sample_id = new_sample
        self.generate_url()
        self.refresh_web_view()

    def clear_selection(self):
        self.vars_model.clear_selection()

if __name__ == "__main__":
    import sys

    scenario = None
    broadcast = None
    receive = None
    if len(sys.argv) > 1:
        scenario = sys.argv[1]
    if len(sys.argv) > 3:
        broadcast = sys.argv[2]
        receive = sys.argv[3]

    app = QtGui.QApplication([])
    main_window = ParallelCoordinatesApp(scenario, broadcast, receive)
    main_window.show()
    app.exec_()


from pathlib import Path

from synthesizer.hparams import hparams
from synthesizer.train import train
from utils.argutils import print_args
import argparse

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("run_id", type=str, help= \
        "Name for this model. By default, training outputs will be stored to saved_models/<run_id>/. If a model state "
        "from the same run ID was previously saved, the training will restart from there. Pass -f to overwrite saved "
        "states and restart from scratch.")
    parser.add_argument("syn_dir", type=Path, help= \
        "Path to the synthesizer directory that contains the ground truth mel spectrograms, "
        "the wavs and the embeds.")
    parser.add_argument("-m", "--models_dir", type=Path, default="saved_models", help=\
        "Path to the output directory that will contain the saved model weights and the logs.")
    parser.add_argument("-s", "--save_every", type=int, default=1000, help= \
        "Number of steps between updates of the model on the disk. Set to 0 to never save the "
        "model.")
    parser.add_argument("-b", "--backup_every", type=int, default=25000, help= \
        "Number of steps between backups of the model. Set to 0 to never make backups of the "
        "model.")
    parser.add_argument("-f", "--force_restart", action="store_true", help= \
        "Do not load any saved model and restart from scratch.")
    parser.add_argument("--hparams", default="", help=\
        "Hyperparameter overrides as a comma-separated list of name=value pairs")
    args = parser.parse_args()
    print_args(args, parser)

    args.hparams = hparams.parse(args.hparams)

    # Run the training
    train(**vars(args))


import os

def read_input(input_filename):
    with open(input_filename) as input_file:
        input_split_lines = [item.rstrip() for item in input_file.readlines()]
    return input_split_lines

def main(input_lines):

    counters = [0]*len(input_lines[0])
    total_lines = len(input_lines)
    # count the 1s in each position. We know the number of zeros
    # will be (total_lines - counters[i])
    for item in input_lines:
        for char_pos, char in enumerate(item):
            if char == '1':
                counters[char_pos] += 1

    # Build strings from '0' and '1' chars depending on the values in counters
    gamma_str = ''
    epsilon_str = ''
    for count in counters:
        if count > total_lines/2:
            # 1 is more frequent, so set 1 in gamma, 0 in epsilon
            gamma_str += '1'
            epsilon_str += '0'
        else:
            gamma_str += '0'
            epsilon_str += '1'

    # Convert strings to bytearrays, then convert those from base 2 to base 10 ints
    return int(bytearray(gamma_str, 'utf-8'), 2) * \
        int(bytearray(epsilon_str, 'utf-8'), 2)

def functional(input_lines):

    counters = [0]*len(input_lines[0])
    total_lines = len(input_lines)
    # count the 1s in each position. We know the number of zeros
    # will be (total_lines - counters[i])
    for item in input_lines:
        for char_pos, char in enumerate(item):
            if char == '1':
                counters[char_pos] += 1

    # return True if 1s more frequent than 0s, else False
    one_more_freq = list(map(lambda count: count > total_lines/2, counters))

    gamma_str = ''.join(map(lambda x: '1' if x else '0', one_more_freq))
    epsilon_str = ''.join(map(lambda x: '0' if x else '1', one_more_freq))

    # Convert strings to bytearrays, then convert those from base 2 to base 10 ints
    return int(bytearray(gamma_str, 'utf-8'), 2) * \
        int(bytearray(epsilon_str, 'utf-8'), 2)

if __name__ == "__main__":
    input_split_by_lines = read_input(os.path.join(os.path.dirname(__file__), 'input.txt'))
    print(main(input_lines=input_split_by_lines))
    print(functional(input_lines=input_split_by_lines))


#!/usr/bin/env python

# Designed to allow a recursive copy from the localhost to the remote
# host in Paramiko SFTP.

from __future__ import print_function # for testing only
from __future__ import with_statement
from constants import *

# For testing only, paramiko:
import paramiko
import stat
import tempfile
import shutil

# Wrapper to allow use of with and good tear-down practice (adapted
# from http://effbot.org/zone/python-with-statement.htm).
class ParamikoTransportWrapper:
    def __init__(self, host, port):
        self.host = host
        self.port = port
        self.transport = None

    def __enter__(self):
        self.transport = paramiko.Transport((self.host, self.port))
        return self.transport

    def __exit__(self, exc_type, exc_value, traceback):
        if self.transport is not None:
            self.transport.close()
        return False # allows any exception to be raised

# Wrapper to allow use of with and good tear-down practice (adapted
# from http://effbot.org/zone/python-with-statement.htm).
class ParamikoSFTPWrapper:
    def __init__(self, transport):
        self.transport = transport
        self.sftp = None

    def __enter__(self):
        self.sftp = paramiko.SFTPClient.from_transport(self.transport)
        return self.sftp

    def __exit__(self, exc_type, exc_value, traceback):
        if self.sftp is not None:
            self.sftp.close()
        return False # allows any exception to be raised

# recursive_walk adapted from the response by Jerub to
# http://stackoverflow.com/questions/120656/directory-listing-in-python
import os

# Recursively walks top, performing file_operation on each file and
# dir_operation on each directory.  Performs dir_operation on a
# directory just before performing file_operation on each file in that
# directory.  First call to dir_operation is on top.  Subdirectories
# are called in an order defined by os.walk.
#
# For details of onerror and followlinks (and more details on the
# recursive walk), see os.walk.
def recursive_walk(top, file_operation, dir_operation, onerror=None, followlinks=False):
    for dirpath, dirnames, filenames in os.walk(top, True, onerror, followlinks):
        # Perform dir_operation on the directory itself first.
        dir_operation(dirpath)

        # Perform file_operation on each file.
        for filename in filenames:
            file_operation(os.path.join(dirpath, filename))

        # for subdirname in dirnames:
        #     dir_operation(os.path.join(dirpath, subdirname))

        # # Advanced usage:
        # # editing the 'dirnames' list will stop os.walk() from recursing into there.
        # if '.git' in dirnames:
        #     # don't go into any .git directories.
        #     dirnames.remove('.git')

# For testing only, SFTP a directory using paramiko:
def sftp_testing(controller_top):
    host = "ec2-54-242-199-187.compute-1.amazonaws.com"
    port = 22
    username = "ec2-user"
    sshkeyfile = "/home/c/cs221x/dora/ssh-keys/steve-kuba-pair.pem"
    mykey = paramiko.RSAKey.from_private_key_file(sshkeyfile)

    # Note: using abspath and then basename should ensure that we get
    # just the directory's name (and no more of the path) out.  This
    # works whether controller_top is something like 'foo' or 'foo/'.
    controller_path = os.path.abspath(controller_top)
    web_server_path = '.'

    with ParamikoTransportWrapper(host,port) as transport:
        transport.use_compression()
        transport.connect(username = username, pkey = mykey)
        with ParamikoSFTPWrapper(transport) as sftp:
            # Note: will want to use functions not just lambdas to add
            # handy stuff like catching and ignoring IOErrors.. which
            # is perhaps what we want to do.  (Maybe log rather than
            # ignore!)
            recursive_walk(controller_path,
                           #lambda filename: print("sftp.put", filename, os.path.join(web_server_path, os.path.relpath(filename, os.path.dirname(controller_path)))),
                           #lambda dirname: print("sftp.mkdir", os.path.join(web_server_path, os.path.relpath(dirname, os.path.dirname(controller_path)))))
                           #lambda filename: sftp.put(filename, os.path.join(web_server_path, os.path.relpath(filename, os.path.dirname(controller_path)))),
                           #lambda dirname: sftp.mkdir(os.path.join(web_server_path, os.path.relpath(dirname, os.path.dirname(controller_path)))))
                           lambda filename: print("sftp.put", filename, os.path.join(web_server_path, os.path.relpath(filename, os.path.dirname(controller_path))), sftp.put(filename, os.path.join(web_server_path, os.path.relpath(filename, os.path.dirname(controller_path))))),
                           lambda dirname: print("sftp.mkdir", os.path.join(web_server_path, os.path.relpath(dirname, os.path.dirname(controller_path))), sftp.mkdir(os.path.join(web_server_path, os.path.relpath(dirname, os.path.dirname(controller_path))))))
            print("Recursive delete on: %s" % os.path.join(web_server_path, os.path.basename(controller_path)))
            recursive_walk_remote(sftp, os.path.join(web_server_path, os.path.basename(controller_path)),
                                  lambda filename: print("sftp.remove", filename, sftp.remove(filename)),
                                  lambda dirname: print("sftp.rmdir", dirname, sftp.rmdir(dirname)))
    # sftp.close()
    # transport.close()

# Recursively walks top, performing file_operation on each file and
# dir_operation on each directory.  Performs dir_operation on a
# directory just before performing file_operation on each file in that
# directory.  First call to dir_operation is on top.  Subdirectories
# are called in an order defined by os.walk.
#
# For details of onerror and followlinks (and more details on the
# recursive walk), see os.walk.
def recursive_walk_remote(sftp, top, file_operation, dir_operation, topdown=False, onerror=None, followlinks=False):
    try:
        sftpattrib = None
        if followlinks:
            sftpattrib = sftp.stat(top)
        else:
            sftpattrib = sftp.lstat(top)

        if not hasattr(sftpattrib, 'st_mode'):
            raise "Unable to proceed with recursive_walk_remote b/c SFTP does not return the st_mode field used to tell whether items are files or directories on %s" % top

        if stat.S_ISDIR(sftpattrib.st_mode):
            # It's a directory.  Depending on pre vs. post order
            # selection, either operate on it first or recursively
            # operate on its children first.

            if topdown:
                dir_operation(top)

            # Question: could there be a problem if by this time top
            # is NO LONGER a directory?  Security hole?  Exception
            # issue?  Or, will exceptions on the far side handle this
            # correctly?
            for f in sftp.listdir(top):
                pathname = os.path.join(top, f)
                recursive_walk_remote(sftp, pathname, file_operation, dir_operation, topdown, onerror, followlinks)

            if not topdown:
                dir_operation(top)
        elif stat.S_ISREG(sftpattrib.st_mode) or stat.S_ISLNK(sftpattrib.st_mode):
            # It's a file (or a link).  Operate on it.
            file_operation(top)
        else:
            # Unknown file type.  What to do?
            print('Skipping %s' % top)
    except IOError as e:
        if onerror is None:
            pass
        else:
            onerror(e)

def recursive_walk_test_with_file_structure(locn = None):
    class TemporaryDirectory:
        def __init__(self, suffix, prefix, dir):
            self.suffix = suffix
            self.prefix = prefix
            self.dir = dir
            self.tempdir = None

        def __enter__(self):
            self.tempdir = tempfile.mkdtemp(self.suffix, self.prefix, self.dir)
            return self.tempdir

        def __exit__(self, exc_type, exc_value, traceback):
            if self.tempdir is not None:
                # Not too painful if the Temporary Directory stays around.
                # Just do due diligence to try to destroy it.
                shutil.rmtree(self.tempdir, True)
            # Allow any errors to propagate.
            return False

    # Create a temporary directory:
    with TemporaryDirectory('rec_walk_test', 'tmp', locn) as tempdir:
        # and fill it with:
        # Some files
        with open(os.path.join(tempdir, 'test1'), 'w') as f:
            f.write('test1\n')
        with open(os.path.join(tempdir, 'test2'), 'w') as f:
            f.write('test2\n')

        # One empty subdirectory;
        os.makedirs(os.path.join(tempdir, 'emptydir'))

        # One subdirectory with some files and a subdirectory (with a file) inside of it:
        os.makedirs(os.path.join(tempdir, 'bigdir', 'subdir'))
        with open(os.path.join(tempdir, 'bigdir', 'test3'), 'w') as f:
            f.write('test3\n')
        with open(os.path.join(tempdir, 'bigdir', 'test4'), 'w') as f:
            f.write('test4\n')
        with open(os.path.join(tempdir, 'bigdir', 'test5'), 'w') as f:
            f.write('test5\n')
        with open(os.path.join(tempdir, 'bigdir', 'subdir', 'test6'), 'w') as f:
            f.write('test6\n')

        # A link to the file in the sub-sub-directory and a link to the sub-sub-directory (for followlink testing).
        os.symlink(os.path.join(tempdir, 'bigdir', 'subdir', 'test6'),
                   os.path.join(tempdir, 'link1'))
        os.symlink(os.path.join(tempdir, 'bigdir', 'subdir'),
                   os.path.join(tempdir, 'link2'))

        # Note: CANNOT get unreadable file to actually be unreadable :(
        # # A file with no user read access (for error testing).
        # with open(os.path.join(tempdir, 'unreadable'), 'w') as f:
        #     f.write('unreadable\n')
        # try:
        #     os.chmod(os.path.join(tempdir, 'unreadable'), 0)
        #     results = []
        #     recursive_walk(tempdir,
        #                    lambda f: results.append("File: %s" % f),
        #                    lambda d: results.append("Dir: %s" % d),
        #                    lambda e: results.append("Error: %s" % e.filename),
        #                    False)
        #     print(results)
        #     results = []
        #     recursive_walk(tempdir,
        #                    lambda f: results.append("File: %s" % f),
        #                    lambda d: results.append("Dir: %s" % d),
        #                    lambda e: results.append("Error: %s" % e.filename),
        #                    True)
        #     print(results)
        # finally:
        #     os.chmod(os.path.join(tempdir, 'unreadable'), stat.S_IRUSR | stat.S_IWUSR)

        # TODO: automated testing/checking code.
        results = []
        recursive_walk(tempdir,
                       lambda f: results.append("File: %s" % f),
                       lambda d: results.append("Dir: %s" % d),
                       lambda e: results.append("Error: %s" % e.filename),
                       False)
        print(results)
        results = []
        recursive_walk(tempdir,
                       lambda f: results.append("File: %s" % f),
                       lambda d: results.append("Dir: %s" % d),
                       lambda e: results.append("Error: %s" % e.filename),
                       True)
        print(results)

        with ParamikoTransportWrapper('deas.ugrad.cs.ubc.ca', 22) as transport:
            transport.use_compression()
            transport.connect(username='cs221x',
                              pkey=paramiko.DSSKey.from_private_key_file('/home/c/cs221x/.ssh/id_dsa_localhost'))
            with ParamikoSFTPWrapper(transport) as sftp:
                results = []
                recursive_walk_remote(sftp, tempdir,
                                      lambda f: results.append("File: %s" % f),
                                      lambda d: results.append("Dir: %s" % d))
                print(results)

    # Maybe the same for testing FTP but open an FTP connection to self??


__author__ = "Sergio Gonzalez Diaz"
__copyright__ = "Social Robots Group. Robotics Lab. University Carlos III of Madrid"
__credits__ = ["Sergio Gonzalez Diaz"]
__license__ = "LEUC3M v1.0"
__version__ = "0.0.0"
__maintainer__ = "Sergio Gonzalez Diaz"
__email__ = "sergigon@ing.uc3m.es"
__status__ = "Development"

# ROS libraries
import rospy
import rospkg

# Libraries
import paramiko
from scp import SCPClient, SCPException
import socket
import ctypes
from threading import Timer
import threading # https://www.geeksforgeeks.org/python-different-ways-to-kill-a-thread/
                 # Fix thread exception: https://stackoverflow.com/questions/38667755/killing-a-thread-in-python-simple-program-does-not-seem-to-work
import yaml
import os
import subprocess

# Exceptions
class NoKey(Exception):
    pass
class NoTabletParams(Exception):
    pass

## @brief Thread with exception
#  Class made to manage methods with exceptions
class PcTabletSyncThread(threading.Thread):

    # Constants
    SUCCESS, ERROR, CON_ERROR = 0, -1, -2
    TIME_WAIT = 5 # sec
    PKG_NAME = 'tablet_data_connections'

    ## @brief Init method
    #  @param name: Name of the method
    #  @param ssh: SSH object
    #  @param arg1: First argument
    #  @param arg2: Second argument
    #  @param recursive: Transfer files and directories recursively
    def __init__(self, name):
        # Thread initialization
        threading.Thread.__init__(self)
        # Local paths
        rospack = rospkg.RosPack()
        pkg_path = rospack.get_path(self.PKG_NAME) + '/' # Package path
        self.path_data = pkg_path + 'data/' # Data path
        self.path_alz = self.path_data + os.environ['HOSTNAME'] + '/' # Alz path
        self.tablet_params_filepath = self.path_alz + 'tablet_ssh_params.yaml' # Tablet SSH params filepath
        self.key_filepath = "/home/user/.ssh/id_tablet.pub" # SSH key filepath
        # Arguments initializarion
        self.name = name
        # Connection variables
        self._server, self._port, self._user = None, None, None
        self.key = None
        # Variables initialization
        self.timer = None
        self.filename_prev, self.sent_prev = '', 0
        # Result variables
        self.result = self.CON_ERROR
        self.info = None

    ## @brief Start the timer
    def start_timer(self):
        self.timer = Timer(self.TIME_WAIT, self.raise_exception)
        self.timer.start()

    ## @brief Cancel the timer
    def cancel_timer(self):
        # Canel the timer
        if(self.timer!=None):
            self.timer.cancel()

    ## @brief Target function of the thread class
    #  Executes the methods specified and check the errors
    def run(self):
        self.read_tablet_params()
        # Methods
        try:
            # rsync pull
            if(self.name == 'rsync_pull'):
                self.rsync_pull()
            elif(self.name == 'rsync_push'):
                self.rsync_push()
            # Wrong name
            else:
                rospy.logerr('Wrong name; %s. Closing thread' % self.name)
                self.result = self.ERROR
        # Exceptions
        except NoKey as e:
            rospy.logerr('NoKey: %s' % e)
            self.result = self.ERROR
        except NoTabletParams as e:
            rospy.logerr('NoTabletParams: %s' % e)
            self.result = self.ERROR
        except OSError as e: # No PC file
            rospy.logerr('OSError: %s ' % e)
            self.result = self.ERROR
        except IOError as e: # No PC directory (scp_get)
                             # Directory no exists (sftp_listdir)
                             # Directory already exists or directory root no exists (sftp_mkdir)
                             # Newpath is a folder or something goes wrong (sftp_rename)
                             # Bad file descriptor (SSH client badly created)
            rospy.logerr('IOError: %s ' % e)
            if(e=='[Errno 9] Bad file descriptor'):
                self.result = self.CON_ERROR
            else:
                self.result = self.ERROR
        except EOFError as e: # EOFError transfer
            rospy.logerr ('EOFError: %s ' % e)
            self.result = self.CON_ERROR
        except paramiko.transport as e:
            rospy.logerr ('transport: %s ' % e)
            self.result = self.CON_ERROR
        except: # Timeout or error
            raise
            rospy.logerr('Timeout or Error')
            self.result = self.CON_ERROR
        else: # Executes if no exceptions
            self.result = self.SUCCESS
        finally: # Executes always
            # Cancel the timer
            self.cancel_timer()

    def read_tablet_params(self):
        try:
            rospy.logdebug('Reading: ' + self.tablet_params_filepath)
            stream = open(self.tablet_params_filepath, 'r')
            params_dict = yaml.load(stream) # Loads value
            self._ip, self._port, self._user = params_dict['tablet_IP'], params_dict['tablet_port'], params_dict['tablet_user']
        except IOError as e: # Reading file error
            rospy.logerr(e)
            self._ip, self._port, self._user = None, None, None # Reset value
            result = self.ERROR
        except KeyError as e: # Reading file error
            rospy.logerr(e)
            self._ip, self._port, self._user = None, None, None # Reset value
            result = self.ERROR
        else: # No errors
            result = self.SUCCESS
        return result

    ## @brief Progress callback that prints the current percentage
    #    completed for the file sending
    #  @param filename: Path of the sent file
    #  @param size: Size of the sent file
    #  @param sent: Current size sent of the sent file.
    def progress(self, filename, size, sent):
        rospy.logdebug("%s\'s progress: %.2f%%   \r" % (filename, float(sent)/float(size)*100) )
        # Checks if it is stuck
        if(self.filename_prev!=filename or self.sent_prev!=sent): # It is not stuck
            self.cancel_timer()
            self.start_timer()
        # Fill prev variables
        self.filename_prev, self.sent_prev = filename, sent

    ########################## Methods ##########################

    def rsync_pull(self):
        self.start_timer()
        rospy.logdebug('rsync pulling')
        cmd = "rsync -av --delete -O -e 'ssh -p " + str(self._port) + "' " + self._user + "@" + self._ip + ":/sdcard/multimedia/testimg/ ../Pictures/"
        os.system(cmd)

    def rsync_push(self):
        self.start_timer()
        rospy.logdebug('rsync pushing')
        cmd = "rsync -av --delete -O -e 'ssh -p " + str(self._port) + "' ../Pictures/ " + self._user + "@" + self._ip + ":/sdcard/multimedia/testimg"
        os.system(cmd)

    ## @brief scp get method
    def scp_get(self, remote_path, local_path, recursive):
        # Start timer
        self.start_timer()
        rospy.logdebug('Establish rsync connection')
        #cmd = "rsync -av -e 'ssh -p 8022' " + full_remote_path + " " + local_path
        #full_remote_path = tablet_user + '@' + tablet_IP + ':' + remote_path
        os.system("rsync -av -e 'ssh -p 8022' u0_a121@192.168.1.106:/sdcard/multimedia/testimg/ Pictures/")
        #subprocess.call(['rsync', '-av', '-e', '"ssh -p 8022"', 'u0_a121@192.168.1.106:/sdcard/Pictures/', 'Pictures/' ])

    ##################### Thread functions ######################

    ## @brief Returns id of the respective thread
      #  @return id: id of the respective thread
    def get_id(self):
        if hasattr(self, '_thread_id'):
            return self._thread_id
        for id, thread in threading._active.items():
            if thread is self:
                return id

    ## @brief Raise an exception in the active thread
    def raise_exception(self):
        thread_id = self.get_id()
        res = ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(thread_id),
              ctypes.py_object(SystemExit))
        if res > 1:
            ctypes.pythonapi.PyThreadState_SetAsyncExc(thread_id, 0)
            rospy.logerr('Exception raise failure')

    ## @brief Returns thread result
      #  @return result: thread result
    def return_result(self):
        return self.result

    ## @brief Returns thread info
      #  @return info: thread info
    def return_info(self):
        return self.info


import pandas as pd
from sklearn.svm import LinearSVC
from sklearn.model_selection import train_test_split
from pymongo import MongoClient
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.model_selection import RandomizedSearchCV
from sklearn import datasets, linear_model, metrics
from sklearn.ensemble import RandomForestRegressor
from sklearn import ensemble
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
import numpy as np

def main(file_name,algorithm_name,pred_type,imp_features,target,inputType,feature_values):
    client = MongoClient("mongodb://127.0.0.1:27017") #host uri
    db = client["datasets"]
    col = db[file_name]
    data = pd.DataFrame(list(col.find()))
    data.drop(data.columns[[0]], axis = 1, inplace = True)
    df=data
    df=df[imp_features]
    cat_features=[]

    test=pd.DataFrame(columns=imp_features,index=[0])
    test.loc[0]=feature_values

    sc= StandardScaler()
    pca = PCA()

    X = pd.DataFrame()
    y = pd.DataFrame()

    X = df

    y[target] = data[target]

    X_train, X_test, y_train, y_test = train_test_split(X, y,test_size=0.3, random_state = 0)

    #Applying StandardScaler on it
    X_train = sc.fit_transform(X_train)
    X_test = sc.transform(X_test)

    #Applying PCA on it
    X_train = pca.fit_transform(X_train)
    X_test = pca.transform(X_test)

    y =  np.log1p(y)
    for col in X.columns:
        if np.abs(X[col].skew()) > 0.3:
            X[col] = np.log1p(X[col])

    if(algorithm_name=="Linear_SVM"):
        #Linear Support Vector Classifier
        print("Linear Support Vector Classifier")
        svc = LinearSVC(max_iter= 1000, C = 70)
        svc.fit(X_train,y_train)
        print("Test set score: {:.2f}".format(svc.score(X_test, y_test)))
        result=svc.predict(test)
        print(result)

    elif(algorithm_name=="RandomForest" and pred_type=="Classification"):
        #RandomForest Classifier
        print("RandomForest Classifier")
        rf = RandomForestClassifier(n_estimators= 20, criterion = 'entropy', random_state = 0)
        rf.fit(X_train,y_train)
        print("Test set score: {:.2f}".format(rf.score(X_test, y_test)))
        result=rf.predict(test)
        print(result)

    elif(algorithm_name=="DecisionTree"):
        #DecisionTree Classifier
        print("DecisionTree Classifier")
        clf_gini = DecisionTreeClassifier(criterion = "gini", random_state = 100,max_depth=3, min_samples_leaf=5)
        clf_gini.fit(X_train, y_train)
        print("Test set score: {:.2f}".format(clf_gini.score(X_test, y_test)))
        result=clf_gini.predict(test)
        print(result)

    elif(algorithm_name=="Adaptive_GB"):
        #Adaptive Gradient Boosting Classifier
        print("Adaptive Gradient Boosting Classifier")
        abc = AdaBoostClassifier(n_estimators=50,learning_rate=1)
        model = abc.fit(X_train, y_train)
        print("Test set score: {:.2f}".format(model.score(X_test, y_test)))
        result=model.predict(test)
        print(result)

    elif(algorithm_name=="Linear_Regression"):
        #Linear Regression
        print("Linear regression")
        reg = linear_model.LinearRegression()
        reg.fit(X_train, y_train)
        print("Test set score: {:.2f}".format(reg.score(X_test, y_test)))
        result=reg.predict(test)
        print(result)

    elif(algorithm_name=="RandomForest" and pred_type=="Regression"):
        #RandomForest Regressor
        print("RandomForest Classifier")
        regressor = RandomForestRegressor(n_estimators = 20, random_state = 0)
        regressor.fit(X_train,y_train)
        print("Test set score: {:.2f}".format(regressor.score(X_test, y_test)))
        result=regressor.predict(test)
        print(result)

    elif(algorithm_name=="GradientBoosting"):
        #Gradient Boosting Regression
        print("Gradient Boosting Regressor")
        params = {'n_estimators': 500, 'max_depth': 4, 'min_samples_split': 2,'learning_rate': 0.01, 'loss': 'ls'}
        model = ensemble.GradientBoostingRegressor(**params)
        model.fit(X_train,y_train)
        print("Test set score: {:.2f}".format(model.score(X_test, y_test)))
        result=model.predict(test)
        print(result)

    return result


import sys
import numpy as np
import math
filename1 = sys.argv[1]
f1 = open(filename1, "r")
x = f1.read().splitlines()
x = x[1:]
data1 = []
t = []
X = []
for term in x:
    data1.append(term.split(','))
for i in range(len(data1)):
    data1[i][0] = int(float(data1[i][0]))
    data1[i][1] = float(data1[i][1])
for i in range(len(data1)):
    for j in range(len(data1[0])):
        if data1[i][j] == 'yes':
            data1[i][j] = 1
        if data1[i][j] == 'no':
            data1[i][j] = 0
for i in range(len(data1)):
    t.append(data1[i][4])
for i in range(len(data1)):
    X.append(data1[i][:4])
X = np.array(X)
#regularization
mean = np.mean(X, axis=0)
std = np.std(X, axis=0)
X = (X - mean)/std

epsi = 0.12
w1 = np.random.rand(4,4)*2*epsi - epsi
w2 = np.random.rand(1,4)*2*epsi - epsi

t = np.array(t)
t = np.reshape(t, (100, 1))

a1 = []
a2 = []
error = 10000
error1 = 10000
error2 = 1
step = 0
while (error1 - error2) > 0.00000001:
    # a1 = hidden output 100*4, a2 = output 100*1
    a1 = np.dot(X, np.transpose(w1))
    a1 = 1.0 / (1 + np.exp(-a1))
    a2 = np.dot(a1, np.transpose(w2))
    a2 = 1.0 / (1 + np.exp(-a2))
    error1 = error
    error = np.sum((t-a2)**2)/float(2)
    if error < error1:
        sys.stdout.write("%s\n" % error)
    else: break
    error2 = error
    # deltk = output error, delth = hidden error
    row1 = len(a2)
    col1 = len(a2[0])
    # deltk = 100*1
    deltk = a2 * (np.ones((row1, col1)) - a2) * (t - a2)

    row2 = len(a1)
    col2 = len(a1[0])
    # delth = 100*4
    delth = a1 * (np.ones((row2, col2)) - a1) * np.dot(deltk, w2)
    # deltw2 = 1*4, deltw1 = 4*4
    deltw2 = 0.05 * np.dot(np.transpose(deltk), a1)
    deltw1 = 0.05 * np.dot(np.transpose(delth), X)

    # w1 = 4*4, w2 = 1*4
    w1 += deltw1
    w2 += deltw2
    step += 1
    if step > 300000: break
sys.stdout.write("TRAINING COMPLETED! NOW PREDICTING.\n")

filename2 = sys.argv[2]
f2 = open(filename2, "r")
y = f2.read().splitlines()
y = y[1:]
data2 = []
X = []
for term in y:
    data2.append(term.split(','))
for i in range(len(data2)):
    data2[i][0] = int(float(data2[i][0]))
    data2[i][1] = float(data2[i][1])
for i in range(len(data2)):
    for j in range(len(data2[0])):
        if data2[i][j] == 'yes':
            data2[i][j] = 1
        if data2[i][j] == 'no':
            data2[i][j] = 0
for i in range(len(data2)):
    X.append(data2[i][:4])
np.array(X)
mean = np.mean(X, axis=0)
std = np.std(X, axis=0)
X = (X - mean)/std

a1 = []
a2 = []
a1 = np.dot(X, np.transpose(w1))
a1 = 1.0 / (1 + np.exp(-a1))
a2 = np.dot(a1, np.transpose(w2))
a2 = 1.0 / (1 + np.exp(-a2))
for term in a2:
    if term >0.5: sys.stdout.write("yes\n")
    else: sys.stdout.write("no\n")


# import numpy

# a = numpy.array([1,2,3])
# a.max() #maior elemento da matriz
# a.min() #menor elemento da matirz
# a.sum() #soma dos elementos da matriz
# a.mean() #media da soma dos elementos da matriz
# a.std() #desvio padrao dos elementos da matriz
# print(a)

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

import pandas

# teste = pandas.read_excel('Planilha data science.xlsx')
# tst = pandas.read_csv('Planilha data science - Página1.csv')

# print(tst.head()) #primeiras X (padrao 5) linhas

data = pandas.DataFrame({'nome':['luis','joao','paula','mario'],
                        'idade':[12,14,15,10],
                        'sexo':['m','m','f','m']}) #interno tem um dicionario
# print(data)
# print(data.shape) #retorna qnt linhas e colunas
# print(data.describe()) #traz operacoes matematicas e estatisticas
# print(data['nome']) #filtrar coluna nome
# print(data.loc[[0,2]]) #filtrando linha zero e dois
# print(data.loc[0:2]) #filtrando da linha zero ate a dois
# print(data.loc[data['nome']=='luis']) #query filtrando a linha onde na coluna nome corresponde a 'luis'
# print(data['nome'].loc[2]) #trazendo apenas a linha 2 da coluna nome
# print(data.size) #retorna quantidade de elementos
# print(data.loc[data['idade']>=12]) #retorna as linhas onde a idade eh maior ou igual a 12
data = data.rename(columns={'nome':'Nome','idade':'age'}) #mudando nomes das colunas
print(data)
print(data['age'].value_counts()) #contabilizando repeticoes de um valor em uma coluna
data.drop('nome',axis=1) #excluindo a coluna nome, axis 0 eh linha axis 1 eh coluna


from numbers import Real
from typing import Any, Callable, Dict, Iterator, Optional, Sequence, Tuple

import numpy as np

from ._base import OptimizerBase, T_WrapperMode, T_SequenceReal

class DE(OptimizerBase):
    def __init__(self, func: Callable, ndim: int = None, lb: T_SequenceReal = None, ub: T_SequenceReal = None,
                 func_wrapper_mode: T_WrapperMode = 'common', func_wrapper_options: Optional[Dict[str, Any]] = None,
                 log_iter_history: bool = True, log_func_call_history: bool = True,
                 pop_size: int = 40, max_iter: int = 150, f: Real = 0.5, cr: Real = 0.3):
        super().__init__(func, ndim, lb, ub, func_wrapper_mode, func_wrapper_options, log_iter_history,
                         log_func_call_history)
        self.pop_size = pop_size
        self.max_iter = max_iter
        self.f = f
        self.cr = cr

        self.positions: Optional[np.ndarray] = None
        self.values: Optional[np.ndarray] = None
        self.previous_values: Optional[np.ndarray] = None

        self.v: Optional[np.ndarray] = None
        self.u: Optional[np.ndarray] = None
        self.fitnesses: Optional[np.ndarray] = None
        self.previous_fitnesses: Optional[np.ndarray] = None

    def init_population(self):
        self.positions = np.random.randint(low=self.lb, high=self.ub, size=(self.pop_size, self.ndim))
        self.u = self.positions
        self.update_values()
        self.ranking()

    def mutation(self):
        r1, r2, r3 = (np.random.randint(0, self.pop_size, size=self.pop_size) for _ in range(3))
        self.v = self.positions[r1, :] + self.f * (self.positions[r2, :] - self.positions[r3, :])
        self.v = np.where((self.lb < self.v) & (self.v < self.ub),
                          self.v,
                          np.random.uniform(low=self.lb, high=self.ub, size=(self.pop_size, self.ndim)))

    def crossover(self):
        self.u = np.where(np.random.rand(self.pop_size, self.ndim) < self.cr,
                          self.v,
                          self.positions)

    def update_values(self):
        self.previous_values = self.values
        self.values = self.func(self.u)

    def ranking(self):
        self.previous_fitnesses = self.fitnesses
        self.fitnesses = -self.values

    def selection(self):
        self.positions = np.where((self.fitnesses > self.previous_fitnesses).reshape(-1, 1),
                                  self.u,
                                  self.positions)

    def update_generation_best(self):
        generation_best_index = self.values.argmin()
        self.best_value = self.values[generation_best_index]
        self.best_position = self.positions[generation_best_index, :]

    def run_iter(self, *args, **kwargs) -> Iterator[Tuple[Real, Sequence[Real]]]:
        self.init_population()
        for i in range(self.max_iter):
            self.mutation()
            self.crossover()
            self.update_values()
            self.ranking()
            self.selection()
            self.update_generation_best()
            self.log_history()
            yield self.best_value, self.best_position


import numpy as np
import sympy as sp
from logger import Logger
from scipy.optimize import minimize
from util import to_np_mat, to_np_vec, sigmoid, rotate, gaussian
from simulation import SimulationReporter
from delay import DelayArray

# ------------------------------------------------------------------------
# PID
# ------------------------------------------------------------------------

class PD:
    """Proportional Derivative Controller.

    a = ad + Kp (xd - x) + Kd (ud - u)

    """

    def __init__(self, Kp, Kd):
        """
        Parameters
        ----------

        Kp: proportional gain

        Kd: derivative gain

        """

        self.Kp = Kp
        self.Kd = Kd
        self.t = 0

    def compute(self, x, u, xd, ud, ad):
        """
        Parameters
        ----------

        x: current position
        u: current velocity
        xd: desired position
        ud: desired velocity
        ad: desired acceleration

        Returns
        -------

        the requred acceleration

        """

        return ad + self.Kp * (xd - x) + self.Kd * (ud - u)

class PID:
    """
    An implementation of Proportional Integral Derivative controller.

    e = xd - x
    u = Kp e + Ki \int_0^t e d\tau + Kd de/dt

    """

    def __init__(self, Kp, Ki, Kd):
        """
        Parameters
        ----------

        Kp: proportional gain
        Ki: integral gain
        Kd: derivative gain

        """

        self.Kp = Kp
        self.Ki = Ki
        self.Kd = Kd
        self.t = 0
        self.error_sum = 0

    def compute(self, t, x, u, xd, ud):
        """
        Parameters
        ----------

        x: current position
        u: current velocity
        xd: desired position
        ud: desired velocity

        Returns
        -------

        the requred acceleration

        Note: TODO if the numerical integration goes backward in time then this
        may cause instabilities

        """

        dt = np.abs(self.t - t)

        error = xd - x
        error_d = ud - u
        self.error_sum = self.error_sum + error * dt
        self.t = t

        return self.Kp * error + self.Kd * error_d + self.Ki * self.error_sum

# ------------------------------------------------------------------------
# JointSpaceController
# ------------------------------------------------------------------------

class JointSpaceController:
    """Simple joint space tracking controller.

    """

    def __init__(self, model):
        """ Constructor.

        Parameters
        ----------

        model: reference to the model

        """
        self.logger = Logger('JointSpaceController')
        self.model = model
        self.pid = PID(10, 2, 2)
        self.reporter = SimulationReporter(model)

    def controller(self, x, t, x0):
        """Default simple joint space PD controller.

        Parameters
        ----------

        x: state
        t: time
        x0: initial state

        """
        # self.logger.debug('Time: ' + str(t))
        n = self.model.nd
        q = np.array(x[:n])
        u = np.array(x[n:])
        qd = np.array(self.target(x, t, x0))
        ud = np.zeros((n))
        tau = np.array(self.pid.compute(t, q, u, qd, ud))

        # record
        self.reporter.t.append(t)
        self.reporter.q.append(q)
        self.reporter.qd.append(qd)
        self.reporter.tau.append(tau)
        # self.fs_reporter.record(t, q, u, tau.reshape((n, 1)))

        return tau

    def target(self, x, t, x0):
        """Default joint space target function.

        Parameters
        ----------

        x: state
        t: time
        x0: initial state

        """
        return [np.deg2rad(130), np.deg2rad(60), np.deg2rad(95)]

# ------------------------------------------------------------------------
# TaskSpaceController
# ------------------------------------------------------------------------

class TaskSpaceController:
    """A task space controller that moves the task goal in a direction.

    """

    def __init__(self, model, task, angle=0, evaluate_muscle_forces=False):
        """Constructor.

        Parameters
        ----------

        model: Model
            a reference to the model

        task: TaskSpace
            a reference to TaskSpace

        angle: rad (default=0)
            direction to move the task

        evaluate_muscle_forces: Boolean (default=False)
            compute muscle forces that satisfy the task constraint

        """
        self.logger = Logger('TaskSpaceController')
        self.model = model
        self.task = task
        self.angle = angle
        self.evaluate_muscle_forces = evaluate_muscle_forces
        self.pd = PD(50, 5)
        self.reporter = SimulationReporter(model)

    def controller(self, x, t, x0):
        """Controller function.

        Parameters
        ----------

        x: state
        t: time
        x0: initial state

        """
        self.logger.debug('Time: ' + str(t))
        n = self.model.nd
        q = np.array(x[:n])
        u = np.array(x[n:])
        pose = self.model.model_parameters(q=q, u=u)

        # task variables
        xc = to_np_mat(self.task.x(pose))
        uc = to_np_mat(self.task.u(pose, u))
        xd, ud, ad = self.target(x, t, x0)
        ad = sp.Matrix(self.pd.compute(xc, uc, xd, ud, ad))

        # forces
        tau, ft = self.task.calculate_force(ad, pose)
        ft = to_np_vec(ft)
        tau = to_np_vec(tau)

        # solve static optimization
        fm = None
        if self.evaluate_muscle_forces:
            m = self.model.md
            R = to_np_mat(self.model.R.subs(pose))
            RT = R.transpose()

            def objective(x):
                return np.sum(x**2)

            def inequality_constraint(x):
                return np.array(tau + RT * (x.reshape(-1, 1))).reshape(-1,)

            x0 = np.zeros(m)
            bounds = tuple([(0, self.model.Fmax[i, i]) for i in range(0, m)])
            constraints = ({'type': 'ineq', 'fun': inequality_constraint})
            sol = minimize(objective, x0,  method='SLSQP',
                           bounds=bounds,
                           constraints=constraints)
            if sol.success is False:
                raise Exception('Static optimization failed at: ' + t)

            fm = sol.x.reshape(-1,)

        # record
        self.reporter.t.append(t)
        self.reporter.q.append(q)
        self.reporter.u.append(u)
        self.reporter.x.append(np.array(xc).reshape(-1,))
        self.reporter.xd.append(np.array(xd).reshape(-1,))
        self.reporter.tau.append(tau)
        self.reporter.ft.append(ft)
        self.reporter.fm.append(fm)
        # self.fs_reporter.record(t, q, u, tau)

        return tau

    def target(self, x, t, x0):
        """ A directed sigmoid function target.

        Parameters
        ----------

        x: state
        t: time
        x0: initial state

        Returns
        -------

        (x, u, a)

        """
        pose0 = self.model.model_parameters(q=x0[:self.model.nd])
        xt0 = self.task.x(pose0)

        t0 = 1
        A = 0.3
        B = 4
        o = np.asmatrix([[0], [0]])
        xd, ud, ad = sigmoid(t, t0, A, B)

        xd = rotate(o, np.asmatrix([[xd], [0]]), self.angle)
        ud = rotate(o, np.asmatrix([[ud], [0]]), self.angle)
        ad = rotate(o, np.asmatrix([[ad], [0]]), self.angle)

        return (np.asmatrix(xt0 + xd),
                np.asmatrix(ud),
                np.asmatrix(ad))

# ------------------------------------------------------------------------
# MuscleSpaceControllerJS
# ------------------------------------------------------------------------

class MuscleSpaceControllerJS:
    """This controller uses the muscle space EoM to driving a model from a
    reference pose to a desired pose in joint space.

    """

    def __init__(self, model, musclespace):
        """Constructor.

        Parameters
        ----------

        model: a reference to the model
        musclespace: a reference to MuscleSpace

        """
        self.logger = Logger('MsucleSpaceControllerJS')
        self.model = model
        self.musclespace = musclespace
        self.pid = PID(10, 0, 10)
        self.reporter = SimulationReporter(model)

    def controller(self, x, t, x0):
        """Default simple joint space PD controller.

        Parameters
        ----------

        x: state
        t: time
        x0: initial state

        """
        self.logger.debug('Time: ' + str(t))
        n = self.model.nd
        m = self.model.md
        q = np.array(x[:n])
        u = np.array(x[n:])
        qd = np.array(self.target(x, t, x0))
        ud = np.zeros((n))
        qddot = np.array(self.pid.compute(t, q, u, qd, ud))

        # evalutate desired lmdd_d
        pose = self.model.model_parameters(q=q, u=u)
        RDotQDot = self.model.RDotQDot.subs(pose)
        RQDDot = self.model.R.subs(pose) * sp.Matrix(qddot)
        lmdd_d = sp.Matrix(RDotQDot + RQDDot)
        lmd = to_np_vec(self.model.lmd.subs(pose))
        lmd_d = to_np_vec(self.model.R.subs(pose) * sp.Matrix(u))

        tau = self.musclespace.calculate_force(lmdd_d, pose)[0]
        tau = to_np_vec(tau)

        # store record
        self.reporter.t.append(t)
        self.reporter.q.append(q)
        self.reporter.u.append(u)
        self.reporter.qd.append(qd)
        self.reporter.lmd.append(lmd)
        self.reporter.lmd_d.append(lmd_d)
        self.reporter.tau.append(tau)

        return tau

    def target(self, x, t, x0):
        """Default joint space target function.

        Parameters
        ----------

        x: state
        t: time
        x0: initial state

        """
        return [np.deg2rad(130), np.deg2rad(60), np.deg2rad(95)]

# ------------------------------------------------------------------------
# PosturalMuscleSpaceController
# ------------------------------------------------------------------------

class PosturalMuscleSpaceController:
    """This is a posture controller in muscle space. The system is disturbed and a
    muscle length controller is responsible for restoring it to its initial
    pose.

    """

    def __init__(self, model, musclespace, kp, kd, delay, a, t0, sigma, gamma):
        """ Constructor.

        Parameters
        ----------

        model: a reference to ToyModel

        muscle: a reference to TaskSpace

        kp: proportional gain

        kd: derivative gain

        delay: the delay of the reflex loops

        a: Gaussian aptitude (disturbance)

        t0: time of application (disturbance)

        sigma: outspread (disturbance)

        gamma: direction of disturbance

        """
        self.logger = Logger('PosturalMsucleSpaceController')
        self.model = model
        self.musclespace = musclespace
        self.pd = PD(kp, kd)  # (10, 10) -> full controller, (0, 10) -> reflex
        self.delay = delay
        self.a = a
        self.t0 = t0
        self.sigma = sigma
        self.gamma = gamma
        self.reporter = SimulationReporter(model)
        self.__initialize_delay_components()
        self.__calculate_task()

    def __calculate_task(self):
        """Calculate model's end effector Jacobian transpose.

        """
        xt = sp.Matrix(self.model.ee)
        Jt = xt.jacobian(self.model.Q())
        self.JtT = Jt.transpose()

    def __add_in_distrurbance(self, t, tau, pose):
        """Adds distrubance to end effector.

        """
        angle = self.gamma
        o = np.asmatrix([[0], [0]])
        fd = np.asmatrix([[gaussian(t, self.a, self.t0, self.sigma)], [0]])
        fd = rotate(o, fd, angle)
        return tau + self.JtT.subs(pose) * fd

    def __initialize_delay_components(self):
        """Initializes the delay components with the default muscle length (state(0))
        and zero length velocities.

        """
        n = self.model.nd
        m = self.model.md

        state0 = self.model.state0
        q = state0[:n]
        u = state0[n:]
        pose = self.model.model_parameters(q=q, u=u)
        self.lm0 = self.model.lm.subs(pose)
        self.lm0 = to_np_vec(self.lm0)

        delay = np.full(m, self.delay)

        self.lm_del = DelayArray(m, delay, self.lm0)
        self.lmd_del = DelayArray(m, delay, np.zeros(m))

    def controller(self, x, t, x0):
        """ Controller.

        Parameters
        ----------

        x: state
        t: time
        x0: initial state

        """
        self.logger.debug('Time: ' + str(t))
        m = self.model.md
        n = self.model.nd
        q = np.array(x[:n])
        u = np.array(x[n:])

        # compute current muscle length and derivative
        pose = self.model.model_parameters(q=q, u=u)
        lm = self.model.lm.subs(pose)
        lm = to_np_vec(lm)
        lmd = self.model.lmd.subs(pose)
        lmd = to_np_vec(lmd)

        # compute target
        lm_des = self.target(x, t, x0)
        lmd_des = np.zeros(m)
        lmdd_des = np.zeros(m)

        # update delayed values and get current (must update first)
        self.lm_del.add(t, lm)
        self.lmd_del.add(t, lmd)

        lm_del = np.array(self.lm_del.get_delayed())
        lmd_del = np.array(self.lmd_del.get_delayed())

        lmdd_des = sp.Matrix(self.pd.compute(
            lm_del, lmd_del, lm_des, lmd_des, lmdd_des))

        tau, fm = self.musclespace.calculate_force(lmdd_des, pose)
        tau = self.__add_in_distrurbance(t, tau, pose)
        tau = to_np_vec(tau)

        # record
        self.reporter.t.append(t)
        self.reporter.q.append(q)
        self.reporter.u.append(u)
        self.reporter.lm.append(lm)
        self.reporter.lm_d.append(lm_des)
        self.reporter.fm.append(fm)
        self.reporter.tau.append(tau)

        return tau

    def target(self, x, t, x0):
        """Default muscle space target function.

        Parameters
        ----------

        x: state
        t: time
        x0: initial state

        """
        return self.lm0


import time

from selenium.webdriver.common.by import By
from utilities.BaseClass import Baseclass

# @pytest.mark.usefixtures("setup")
class Testone(Baseclass):
    def test_e2e(self):

        self.driver.find_element(By.LINK_TEXT, "Shop").click()
        time.sleep(2)
        self.driver.execute_script("window.scrollBy(0, 500)")

        products = self.driver.find_elements(By.XPATH, "//div[@class='card h-100']")

        for product in products:
            productName = product.find_element(By.XPATH, "div/h4/a").text
            if productName == "Blackberry":
                product.find_element(By.XPATH, "div/button").click()

        time.sleep(2)

        self.driver.find_element(By.XPATH, "//a[@class='nav-link btn btn-primary']").click()
        time.sleep(2)
        # driver.close()


import tkinter as tk
from random import randrange

window = tk.Tk()
window.title("OOP2 - Guessing Game")
# Game labels.
lblInst = tk.Label(window, text = "Guess a number from 0 to 9")
lblLine0 = tk.Label(window, text = "*********************************************************************")
lblNoGuess = tk.Label(window, text = "No of Guesses: 0")
lblMaxGuess = tk.Label(window, text = "Max Guess: 3")
lblLine1 = tk.Label(window, text = "*********************************************************************")
lblLogs = tk.Label(window, text="Game Stats")
lblLine2 = tk.Label(window, text = "*********************************************************************")

# Create the buttons
buttons = []
for index in range(0, 10):
    # Here we assign all the buttons from 0, 10 using lambda functions. We disable the buttons by default before starting the game.
    button = tk.Button(window, text=index, command=lambda index=index : process(index), state=tk.DISABLED)
    buttons.append(button)

btnStartGameList = []
for index in range(0, 1):
    btnStartGame = tk.Button(window, text="Start Game", command=lambda : startgame(index))
    btnStartGameList.append(btnStartGame)

# append elements to grid
lblInst.grid(row=0, column=0, columnspan=5)
lblLine0.grid(row=1, column=0, columnspan=5)
lblNoGuess.grid(row=2, column=0, columnspan=3)
lblMaxGuess.grid(row=2, column=3, columnspan=2)
lblLine1.grid(row=3, column=0, columnspan=5)
lblLogs.grid(row=4, column=0, columnspan=5)  # row 4 - 8 is reserved for showing logs

lblLine2.grid(row=9, column=0, columnspan=5)

for row in range(0, 2):
    for col in range(0, 5):
        i = row * 5 + col  # convert 2d index to 1d. 5= total number of columns
        buttons[i].grid(row=row+10, column=col)

btnStartGameList[0].grid(row=13, column=0, columnspan=5)

# Main game logic

guess = 0
totalNumberOfGuesses = 0
totalPoints = 20
secretNumber = randrange(10)
print(secretNumber)
lblLogs = []
guess_row = 4

# reset all variables
def init():
    global buttons, guess, totalNumberOfGuesses, secretNumber, lblNoGuess, lblLogs, guess_row
    guess = 0
    totalNumberOfGuesses = 0
    secretNumber = randrange(10)
    print(secretNumber)
    lblNoGuess["text"] = "Number of Guesses: 0"
    guess_row = 4

    # remove all logs on init - So as to have better troubleshooting
    for lblLog in lblLogs:
        lblLog.grid_forget()
    lblLogs = []

# Starting the game.
def process(i):
    global totalNumberOfGuesses, buttons, guess_row, totalPoints
    guess = i

    totalNumberOfGuesses += 1
    lblNoGuess["text"] = "Number of Guesses: " + str(totalNumberOfGuesses)

    # Check if guess match secret number
    if guess == secretNumber:
        lbl = tk.Label(window, text="Your guess was right. You won! :) and got %d Points" % totalPoints, fg="green")
        lbl.grid(row=guess_row, column=0, columnspan=5)
        lblLogs.append(lbl)
        guess_row += 1

        # Disabling the buttons that are
        for b in buttons:
            b["state"] = tk.DISABLED
    else:
        # Give player some hints
        if guess > secretNumber:
            lbl = tk.Label(window, text="Secret number is less than your current guess :)", fg="red")
            lbl.grid(row=guess_row, column=0, columnspan=5)
            lblLogs.append(lbl)
            guess_row += 1

        # elif guess // Checking if the guess is divisible by the number.
        else:
            lbl = tk.Label(window, text="Secret number is greater than your current guess :)", fg="red")
            lbl.grid(row=guess_row, column=0, columnspan=5)
            lblLogs.append(lbl)
            guess_row += 1

    # game is over when max no of guesses is reached
    if totalNumberOfGuesses == 3:
        if guess != secretNumber:
            totalPoints = 0
            lbl = tk.Label(window, text="Max guesses reached. You lost :( and got %d points." % totalPoints, fg="red")
            lbl.grid(row=guess_row, column=0, columnspan=5)
            lblLogs.append(lbl)
            guess_row += 1

        for b in buttons:
            b["state"] = tk.DISABLED

    buttons[i]["state"] = tk.DISABLED

status = "none"

def startgame(i):
    global status
    for b in buttons:
        b["state"] = tk.NORMAL
    # Changing the status of the game to started. Users can restart the game at any time.
    if status == "none":
        status = "started"
        btnStartGameList[i]["text"] = "Restart Game"
    else:
        status = "restarted"
        init()
    print("Game started")

window.mainloop()


from django.shortcuts import render
from ws4redis.redis_store import RedisMessage
from ws4redis.publisher import RedisPublisher
from django.shortcuts import HttpResponse
from rewardingroads.forms import *
from rewardingroads.decorators import *
from django.views.decorators.csrf import csrf_exempt
import json
from django.conf import settings

@user_session_set
def index(request):
	user = Traveller.objects.get(name=request.session['username'])
	total_reports = Report.objects.filter(reporter=user).exclude(trigger=3)
	verified_reports = Report.objects.filter(reporter=user).filter(processed=1)
	context = {
		'user' : user,
		'total_reports' : total_reports,
		'verified_reports' : verified_reports
	}
	return render(request,'rewardingroads/home.html',context)

def govt(request):
	operators = Operator.objects.order_by('penalty')
	pendingReports = Report.objects.filter(processed=0).count()
	pendingInformation = Information.objects.filter(status=0).count()
	totalPenalty = 0
	for operator in operators:
		totalPenalty += operator.penalty
	travellerCount = Traveller.objects.all().count()
	context = {
		'operators' : operators,
		'pendingReports' : pendingReports,
		'pendingInformation' : pendingInformation,
		'travellerCount' : travellerCount,
		'totalPenalty' : totalPenalty
	}
	return render(request,'rewardingroads/govt.html',context)

@user_session_set
def drive(request):
	user = Traveller.objects.get(name=request.session['username'])
	roads = Road.objects.all()
	unchecked = Information.objects.filter(status=0).filter(trust__lte=0.90)
	complete = []
	for ind,info in enumerate(unchecked):
		temp = [float(info.latitude),float(info.longitude),ind+1]
		complete.append(temp)
	final = json.dumps(complete)
	context = {
		'user' : user,
		'roads' : roads,
		'toBeChecked' : final,
		'unchecked' : unchecked,
		'YOUR_API_KEY' : settings.YOUR_API_KEY
	}
	return render(request,'rewardingroads/drive.html',context)

def login(request):
	request.session.pop('username',None)
	if request.method == "POST":
		print(request.POST)
		username = request.POST.get('name','nikhil96sher')
		request.session['username'] = username
		return HttpResponseRedirect('/roads/')
	return render(request,'rewardingroads/login.html')

revmap = {
	'Decelaration' : 1,
	'Lane Switching' : 2,
	'Confirmation' : 3
}

@csrf_exempt
def report(request):
	try:
		data = json.loads(request.body)
		rows = data['myrows']
		road = Road.objects.get(pk = int(data['road']))
		user = Traveller.objects.get(name = request.session['username'])
		for report in rows:
			# print(report)
			# Getting Request Parameters
			latitude = float(report['Latitude'])
			longitude = float(report['Longitude'])
			trigger = revmap[report['Trigger Type']]
			severity = int(report['Severity'])
			time = report['Incident Time']

			#Create Report Instance
			d = Report()
			d.reporting_time = time
			d.reporter = user
			d.latitude = latitude
			d.longitude = longitude
			d.road = road
			d.trigger = trigger
			d.severity = severity

			# Create User Instance
			user.last_latitude = latitude
			user.last_longitude = longitude
			user.save()

			# Computing Information Instance
			delta = 0.01
			lat = round(latitude,4)
			lon = round(longitude,4)
			infos = Information.objects.filter(latitude__lte=lat+delta, latitude__gte=lat-delta).filter(longitude__lte=lon+delta, longitude__gte=lon-delta)

			if(infos.count() != 0):
				info = infos[0]
				if trigger == 3 and severity <= 2:
					print("YO CASE")
					info.status = 1
					info.save()
				info.last_report_time = d.reporting_time
				info.first_report_time = info.last_report_time
				info.trust = ((info.report_count * info.trust) + (user.trust))/(info.report_count+1)
				info.avg_severity = (info.avg_severity*info.report_count)/(info.report_count+1)
				info.report_count += 1
				info.save()
			else:
				info = Information()
				info.latitude = lat
				info.longitude = lon
				info.trust = user.trust
				info.road = road
				info.last_report_time = d.reporting_time
				info.first_report_time = info.last_report_time
				info.avg_severity = severity
				info.save()
			d.information = info
			d.save()
		return HttpResponse("success")
	except Exception as e:
		print(e)
		return HttpResponse("error")


"""Panda package management for Windows.

Application core constant objects.
"""
from pathlib import Path

# Package's installation config files

PKG_CFG_DNAME = 'conf'

# Package info descriptor file
PKG_INFO_FPATH = 'pkginfo.json'
# Package extra installation options descriptor file
PKG_EXTCFG_FEXT = 'extra'
PKG_EXTCFG_FNAME = '.'.join(['{pkg_name}', PKG_EXTCFG_FEXT, 'j2'])
PKG_EXTCFG_FPATH = str(Path(PKG_CFG_DNAME, PKG_EXTCFG_FNAME))
# Package service options descriptor file
PKG_SVCCFG_FEXT = 'nssm'
PKG_SVCCFG_FNAME = '.'.join(['{pkg_name}', PKG_SVCCFG_FEXT, 'j2'])
PKG_SVCCFG_FPATH = str(Path(PKG_CFG_DNAME, PKG_SVCCFG_FNAME))

# List of package installation config file extensions
PKG_INSTCFG_FEXTS = [PKG_EXTCFG_FEXT, PKG_SVCCFG_FEXT]


import risar
from PyQt5.QtWidgets import QMessageBox
from random import randint
from math import *

class Zoga:
    def __init__(self, bool):
        if bool:
            self.x = risar.miska[0]
            self.y = risar.miska[1]
            self.r = 30
            self.barva = risar.bela
        else:
            self.r = 10
            self.x, self.y = risar.nakljucne_koordinate()
            if self.x < 10:
                self.x += 10
            elif self.x > risar.maxX - self.r:
                self.x -= 10
            if self.y < 10:
                self.y += 10
            elif self.y > risar.maxY - self.r:
                self.y -= 10
            self.barva = risar.nakljucna_barva()
            self.hitrostX = randint(-5, 5)
            self.hitrostY = sqrt(25 - self.hitrostX ** 2)
        self.eksplodirana = False
        self.active = True
        self.time_to_live = 4
        self.obj = risar.krog(self.x, self.y, self.r, self.barva, 3)

def razdalja(x, y, x1, y1):                      #vrne razdaljo med dvema točkama v 2-d ravnini
    return sqrt((x - x1) ** 2 + (y - y1) ** 2)

zoge = []
for i in range(30):               #kreira 30 objektov Zoga
    zoge.append(Zoga(False))
zoga_miska = Zoga(True)

eksplodirane = []
count_eksplodiranih = -1       #da ne šteje miska.klik

risar.obnavljaj = False

timer = 0
while not risar.klik or eksplodirane:      #izvaja se dokler nismo še kliknili z miško ali dokler je še kakšna eksplodirana žoga na zaslonu
    for el in zoge:
        el.obj.setPos(el.x, el.y)          #nastavi novo pozicijo za vsako žogo
    risar.cakaj(0.02)
    timer += 0.02
    for el in zoge:
        if el.active:
            if el.x > risar.maxX - el.r or el.x < el.r:                  #preverja odboje
                el.hitrostX = -el.hitrostX
            if el.y > risar.maxY - el.r or el.y < el.r:
                el.hitrostY = -el.hitrostY
            if not el in eksplodirane:                        #izvede premik, če žoga ni še eksplodirana
                el.x += el.hitrostX
                el.y += el.hitrostY
            for eksp in eksplodirane:       #preverja razdaljo med trenutno žogo in vsemi ostalimi eksplodiranimi žogami
                if razdalja(el.x, el.y, eksp.x, eksp.y) < 40 and el not in eksplodirane:
                    eksplodirane.append(el)
                    c = el.obj.pen().color().lighter()
                    c.setAlpha(192)
                    el.obj.setBrush(c)

    if not risar.klik:                                                    #če še ni prišlo do klika premika večji krog glede na koordinate miške
        zoga_miska.obj.setPos(risar.miska[0], risar.miska[1])
        zoga_miska.x = risar.miska[0]
        zoga_miska.y = risar.miska[1]
    elif zoga_miska not in eksplodirane and zoga_miska.active:
        eksplodirane.append(zoga_miska)

    for el in eksplodirane:                                #po 4 sekundah skrije posamezno eksplodirano žogo
        el.obj.setRect(-30, -30, 60, 60)
        el.time_to_live -= 0.02
        if el.time_to_live <= 0:
            eksplodirane.pop(eksplodirane.index(el))
            el.obj.hide()
            count_eksplodiranih += 1
            el.active = False

    risar.obnovi()
str = "Razstrelil si {} žog!".format(count_eksplodiranih)                      #izpis končnega števila razstreljenih žog
QMessageBox.information(None, "Konec", str)
risar.stoj()


import os
import shutil

inputdir= 'NoFilterModeDataSet/'
outputdir='hand_dataset/'
listing = os.listdir(inputdir)
listing.sort()
os.mkdir(outputdir)
l=['Aboard', 'All_Gone', 'Baby', 'Beside', 'Book', 'Bowl', 'Bridge', 'Camp', 'Cartridge', 'Eight', 'Five', 'Fond', 'Four', 'Friend', 'Glove', 'Hang', 'High', 'House', 'How_Many', 'IorMe', 'Man', 'Marry', 'Meat', 'Medal', 'Mid_Day', 'Middle', 'Money', 'Moon', 'Mother', 'Nine', 'One', 'Opposite', 'Prisoner', 'Ring', 'Rose', 'See', 'Seven', 'Short', 'Six', 'Superior', 'Ten', 'Thick', 'Thin', 'Three', 'Tobacco', 'Two', 'Up', 'Watch', 'Write', 'You']
for i in l:
    os.mkdir(outputdir+i)
    for j in listing:
        if i in j:
            shutil.copy(inputdir+j, outputdir+i)


"""2021 aug 25

Revision ID: 5cf83e20d692
Revises: c43b11d1301d
Create Date: 2021-08-25 16:54:58.014713

"""
from alembic import op
import sqlalchemy as sa
import dbo_models

# revision identifiers, used by Alembic.
revision = '5cf83e20d692'
down_revision = 'c43b11d1301d'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('extrapercentageall',
    sa.Column('id', dbo_models._helper.GUID(), nullable=False),
    sa.Column('promotiontype_id', dbo_models._helper.GUID(), nullable=False),
    sa.Column('percent_off', sa.Float(), nullable=False),
    sa.Column('updated_time', sa.DateTime(timezone=True), nullable=False),
    sa.ForeignKeyConstraint(['promotiontype_id'], ['promotiontype.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_extrapercentageall_promotiontype_id'), 'extrapercentageall', ['promotiontype_id'], unique=False)
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_extrapercentageall_promotiontype_id'), table_name='extrapercentageall')
    op.drop_table('extrapercentageall')
    # ### end Alembic commands ###


import pygplates
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
#import time
#import pygmt

#matplotlib inline

#input parameters
rotation_filename = 'Global_EarthByte_230-0Ma_GK07_AREPS.rot'
input_topology_filename = 'Global_EarthByte_230-0Ma_GK07_AREPS_PlateBoundaries.gpml'
topology_features = pygplates.FeatureCollection(input_topology_filename)
rotation_model = pygplates.RotationModel(rotation_filename)

time_step = 10.
oldest_seed_time = 100.
start_time=100
end_time=40

#### load the gmt file which has the points
File_data = np.loadtxt("India.txt", dtype=int)

# Empty array for storing Long/Lat of
point_longitude = []
point_latitude = []
time_list = np.arange(oldest_seed_time,time_step,-time_step)

for seed_time in time_list:

    # Location of seed point for Kerguelenc
    #seed_geometry = pygplates.PointOnSphere(-50, 80)

    # Seed point for Hawaii
    seed_geometry = pygplates.MultiPointOnSphere([(lat,lon) for lat, lon in File_data])

    #seed_geometry = pygplates.MultiPointOnSphere(
    #[
     #   (42,-114),
      #  (37,-110),
      #  (54,-116)
    #])

    for time in np.arange(seed_time,end_time,-time_step):
        #print max_time, time

        # Get the plate polygons for this time
        resolved_topologies = []
        pygplates.resolve_topologies(topology_features, rotation_model, resolved_topologies, time)

        # make plate partitioner from polygons
        plate_partitioner = pygplates.PlatePartitioner(resolved_topologies, rotation_model)

        # Find the plate id of the polygon that contains the point
        partitioned_inside_geometries = []
        plate_partitioner.partition_geometry(seed_geometry, partitioned_inside_geometries)
        #PlateID = partitioned_inside_geometries[0][0].get_feature().get_reconstruction_plate_id()
        PlateID = 550

        #print PlateID

        # Get the stage rotation that will move the point from where it is at the current time
        # to its location at the next time step, based on the plate id that contains the point at the
        # current time
        stage_rotation = rotation_model.get_rotation(time-time_step, PlateID, time, anchor_plate_id=3)

        # use the stage rotation to reconstruct the tracked point from position at current time
        # to position at the next time step
        seed_geometry = stage_rotation * seed_geometry

    #print('seed time = %d, plume is within plate %i' % (seed_time, PlateID))

    #point_longitude.append(seed_geometry.to_lat_lon_list().get_longitude())
    point_latitude.append(seed_geometry.to_lat_lon_list())

a=np.array(point_latitude)

##### define function to isolate lat & lon
def lat_lon(coord, no_of_time_steps,no_of_points):

    for x in range(no_of_points):
        globals()['lons%s' %x] = [coord[i][x][1] for i in range(no_of_time_steps)]
        globals()['lats%s' %x] = [coord[i][x][0] for i in range(no_of_time_steps)]

    #@ This was used previously in the early stage and is suitable while using only one data file
    #for x in range(no_of_points):
        #coordinates=[[]]
        #or j in range(no_of_time_steps):
        #coordinates=eval("lons" + str(x)), eval("lats" + str(x))
        #coordinates=[[eval("lons" + str(x)), eval("lats" + str(x))]]
        #return(['lons%s' %x], ['lats%s' %x] )

    #@ This is now used as this meant to do fo multiple data files
    coordinates=[[eval("lons" + str(x)), eval("lats" + str(x))] for x in range (no_of_points)]
    return(coordinates)

total_time_steps=int((start_time-end_time)/time_step + 1)
no_of_data_points=len(seed_geometry)
ages=np.arange(start=end_time,stop=start_time+time_step,step=time_step,dtype=int)
ages1=np.flip(ages)

### pass the co-oridnates, total_time_steps, & no of data points  to the fucntion lon_lat
b = lat_lon(a, total_time_steps, no_of_data_points)

#### create figure
import pygmt
import geopandas as gpd
fig=pygmt.Figure()
pygmt.makecpt(cmap="seis", series=[ages1.min(), ages1.max(), time_step])
#fig.basemap(region = [220, 300, 10, 60], projection="M8c", frame=True)
fig.basemap(region="g", projection="W270/12c", frame=True)
fig.coast(shorelines="0.8p,white")

##### loop over the data points
for i in range(no_of_data_points):

    #@ This was used previously in the early stages and is suitable while using only one data file
    #fig.plot(x=eval("lons" + str(i)), y=eval("lats" + str(i)),

    #@ This is now used and is easy to plot while using more than one data files
    fig.plot(x=b[i][0], y=b[i][1],
    fill=ages1,
    style="c0.1c",
    cmap=True,
    pen="black",
    )
rgi = gpd.read_file('reconstructed_40.00Ma.shp')
fig.plot(data=rgi, pen="0.5p,black")
fig.colorbar(frame='af+l"Age (Ma)"')
fig.show()


import argparse
import json
import sys
import os
from tqdm import tqdm

from .v5_client import LuminosoClient
from .v5_constants import URL_BASE

DESCRIPTION = 'Download documents from a Luminoso project via the command line.'
DOCS_PER_BATCH = 1000

# The fields we want for "concise" or "expanded" downloads
CONCISE_FIELDS = ['title', 'text', 'metadata']
EXPANDED_FIELDS = CONCISE_FIELDS + ['terms', 'fragments', 'vector']

def _sanitize_filename(filename):
    """
    Get a filename that lacks the / character (so it doesn't express a path by
    accident) and also lacks spaces (just for tab-completion convenience).
    """
    return filename.replace('/', '_').replace(' ', '_')

def iterate_docs(client, expanded=False, progress=False):
    """
    Yield each document in a Luminoso project in turn. Requires a client whose
    URL points to a project.

    If expanded=True, it will include additional fields that Luminoso added in
    its analysis, such as 'terms' and 'vector'.

    Otherwise, it will contain only the fields necessary to reconstruct the
    document: 'title', 'text', and 'metadata'.

    Shows a progress bar if progress=True.
    """
    # Get total number of docs from the project record
    num_docs = client.get()['document_count']
    progress_bar = None
    try:
        if progress:
            progress_bar = tqdm(desc='Downloading documents', total=num_docs)

        for offset in range(0, num_docs, DOCS_PER_BATCH):
            response = client.get(
                'docs', offset=offset, limit=DOCS_PER_BATCH,
                fields=EXPANDED_FIELDS if expanded else CONCISE_FIELDS
            )
            docs = response['result']
            for doc in docs:
                if progress:
                    progress_bar.update()
                yield doc

    finally:
        if progress:
            progress_bar.close()

def download_docs(client, output_filename=None, expanded=False):
    """
    Given a LuminosoClient pointing to a project and a filename to write to,
    retrieve all its documents in batches, and write them to a JSON lines
    (.jsons) file with one document per line.
    """
    if output_filename is None:
        # Find a default filename to download to, based on the project name.
        projname = _sanitize_filename(client.get()['name'])
        output_filename = '{}.jsons'.format(projname)

        # If the file already exists, add .1, .2, ..., after the project name
        # to unobtrusively get a unique filename.
        counter = 0
        while os.access(output_filename, os.F_OK):
            counter += 1
            output_filename = '{}.{}.jsons'.format(projname, counter)

        print('Downloading project to {!r}'.format(output_filename))

    with open(output_filename, 'w', encoding='utf-8') as out:
        for doc in iterate_docs(client, expanded=expanded, progress=True):
            print(json.dumps(doc, ensure_ascii=False), file=out)

def _main(argv):
    """
    Handle arguments for the 'lumi-download' command.
    """
    parser = argparse.ArgumentParser(
        description=DESCRIPTION,
        formatter_class=argparse.RawDescriptionHelpFormatter
    )
    parser.add_argument(
        '-b',
        '--base-url',
        default=URL_BASE,
        help='API root url, default: %s' % URL_BASE,
    )
    parser.add_argument('-f', '--token-file',
                        help='file where an API token was saved')
    parser.add_argument(
        '-e', '--expanded',
        help="Include Luminoso's analysis of each document, such as terms and"
             ' document vectors',
        action='store_true',
    )
    parser.add_argument(
        'project_id', help='The ID of the project in the Daylight API'
    )
    parser.add_argument(
        'output_file', nargs='?', default=None,
        help='The JSON lines (.jsons) file to write to'
    )
    args = parser.parse_args(argv)

    client = LuminosoClient.connect(
        url=args.base_url, token_file=args.token_file,
        user_agent_suffix='lumi-download'
    )
    proj_client = client.client_for_path('projects/{}'.format(args.project_id))
    download_docs(proj_client, args.output_file, args.expanded)

def main():
    """
    The setuptools entry point.
    """
    _main(sys.argv[1:])


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

from django.db import models, migrations
import django.core.validators

class Migration(migrations.Migration):

    dependencies = [
        ('website', '0037_auto_20150722_0325'),
    ]

    operations = [
        migrations.AlterField(
            model_name='application',
            name='contact_number',
            field=models.CharField(default=None, validators=[django.core.validators.RegexValidator(regex=b'^([0-9]{7}|[0-9]{11})$', message=b'Telephone and Mobile Numbers are only allowed')], max_length=15, blank=True, null=True, verbose_name=b'Contact Number'),
        ),
        migrations.AlterField(
            model_name='application',
            name='time_indicator',
            field=models.ForeignKey(default=1, verbose_name=b'Time Indicator', to='website.TimeIndicator'),
        ),
        migrations.AlterField(
            model_name='application',
            name='vessel_type',
            field=models.ForeignKey(verbose_name=b'Vessel Type', to='website.VType'),
        ),
    ]