from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import sys
import os
import pickle
import time
import logging
import util.parse_opt as parse_opt
import argparse

def parse_args(args_list):
    """
     #Parses the command line input.
    """
    parser = parse_opt.MyArgumentParser(description='this train tool',
            fromfile_prefix_chars='@')

    parser.add_argument('--ps-hosts',dest='ps_hosts',
            type=str, default=None,
            help='parameter server')

    parser.add_argument('--worker-hosts',dest='worker_hosts',
            type=str, default=None,
            help='worker hosts')

    parser.add_argument('--job-name',dest='job_name',
            type=str, default=None,
            help='job name (ps/worker)')

    parser.add_argument('--task-index',dest='task_index',
            type=int, default=None,
            help='task index')
    parser.add_argument('--num-threads', dest='num_threads', type=int, default=1,
            help='number threads (int, default = 1)')

    parser.add_argument('--tf-save-path', dest='tf_save_path',
            type=str, default=None,
            help='TensorFlow save path name for the run (allow multiples run with the same output path)'
            '(str, default = None)')

    parser.add_argument('--criterion', dest='criterion',
            type=str, default='ctc',
            help='TensorFlow loss criterion '
            '(str, default = ctc)')

    parser.add_argument('--max-epoch', dest='max_epoch', type=int, default=None,
            help='Max epoch to train (no limitation if not provided)' '(int, default = None)')

    parser.add_argument('--Debug', dest='Debug', type=bool, default=False,
            help='debug or not'
            '(bool, default = False)')

    parser.add_argument('--use-config-file-if-checkpoint-exists', dest='use_config_file_if_checkpoint_exists',type=bool,
            default=False,
            help='use config file train model'
            '(bool, default = False)')

    parser.add_argument('--checkpoint-dir', dest='checkpoint_dir', type=str,
            default='outdir',
            help='save dir' '(str, default = outdir)')

    parser.add_argument('--reset-global-step', dest='reset_global_step',type=bool,
            default=False,
            help='reset global step = 0 '
            '(bool, default = False)')

    parser.add_argument('--queue-cache', dest='queue_cache', type=int,
            default=100,
            help='input data cache' '(int, default = 100)')

    parser.add_argument('--label-dim', dest='label_dim', type=int, default=-1,
            help='nnet out dim(int, default = -1)')

    parser.add_argument('--den-fst', dest='den_fst', type=str, default=None,
            help='denominator fst file(int, default = None)')

    # features parameters
    parser.add_argument('--io-thread-num', dest='io_thread_num', type=int, default=1,
            help='io threads number(int, default = 1)')

    parser.add_argument('--max-egs-kind', dest='max_egs_kind', type=int, default=1,
            help='max egs kind(int, default = 5)')

    parser.add_argument('--tdnn-start-frames', dest='tdnn_start_frames',type=int,
            default=0,
            help='tdnn start add frames' '(int, default = 0)')

    parser.add_argument('--tdnn-end-frames', dest='tdnn_end_frames',type=int,
            default=0,
            help='tdnn end add frames' '(int, default = 0)')

    parser.add_argument('--max-input-seq-length', dest='max_input_seq_length',type=int,
            default=1500,
            help='allow input max input length' '(int, default = 1500)')

    parser.add_argument('--max-target-seq-length', dest='max_target_seq_length',type=int,
            default=1500,
            help='allow target max input length' '(int, default = 1500)')

    parser.add_argument('--tr-scp', dest='tr_scp', type=str,
            default=None,
            help='train scp file' '(str, default = None)')

    parser.add_argument('--tr-label', dest='tr_label', type=str,
            default=None,
            help='train label file' '(str, default= None)')

    parser.add_argument('--lat-scp-file', dest='lat_scp_file', type=str,
            default=None,
            help='train lattice scp file' '(str, default= None)')

    parser.add_argument('--ali-map-file', dest='ali_map_file', type=str,
            default=None,
            help='lattice ali map file' '(str, default= None)')
    parser.add_argument('--silence-phones', dest='silence_phones', type=str,
            default=None,
            help='smbr and mfpe train silence phone list' '(str, default= None)')
    parser.add_argument('--class-frame-counts', dest='class_frame_counts', type=str,
            default=None,
            help='Vector with frame-counts of pdfs to compute log-priors.' '(str, default= None)')

#    parser.add_argument('--cv-scp', dest='cv_scp', type=str,
#            default=None,
#            help='cv scp file' '(str, default = None)')

#    parser.add_argument('--cv-label', dest='cv_label', type=str,
#            default=None,
#            help='cv label file' '(str, default= None)')

#    parser.add_argument('--restore-training', dest='restore_training', type=bool,
#            default=False,
#            help='restore training' '(bool, default = False)')

    parser.add_argument('--shuffle', dest='shuffle', type=bool,
            default=False,
            help='shuffle data' '(bool, default = False)')

    parser.add_argument('--log-file', dest='log_file', type=str,
            default='log',
            help='log file' '(str, default = log)')

    parser.add_argument('--log-level', dest='log_level', type=str,
            default='INFO',
            help='log level' '(str, default = INFO)')

    '''
        ##add feature option
    '''
    feature_opt = parser.add_argument_group(title='feature_opt',
            description='feature option relation parameters')

    feature_opt.add_argument('--feature-transfile', dest='feature_transfile',
            type=str, default=None,
            help='Feature transform in front of main network (in nnet format)'
            ' (str, defalut = None)')

    feature_opt.add_argument('--skip-frames', dest='skip_frame',
            type=int, default=1,
            help='skip frame number'
            ' (int, default = 1)')

    feature_opt.add_argument('--start-frames', dest='start_frames',
            type=int, default=0,
            help='start frame ,it must be lt skip-frames'
            ' (int, default = 0)')

    '''
       #add train common option
    '''
    train_common_opt = parser.add_argument_group(title='train_common_opt',
            description='training common option relation parameters')

    train_common_opt.add_argument('--steps-per-checkpoint', dest='steps_per_checkpoint',
            type=int, default=1000,
            help='save frequency'
            '(int, default = 1000)')

    train_common_opt.add_argument('--learning-rate', dest='learning_rate',
            type=float, default=1.0,
            help='learning rate for NN training'
            ' (float, default = 1.0)')

    train_common_opt.add_argument('--l2-scale', dest='l2_scale',
            type=float, default=0.00005,
            help='l2 scale for NN training'
            ' (float, default = 0.00005)')

    train_common_opt.add_argument('--learning-rate-decay-steps', dest='learning_rate_decay_steps',
            type=int, default=10000,
            help='learning rate decay step for NN training'
            ' (int, default = 10000)')

    train_common_opt.add_argument('--learning-rate-decay-rate', dest='learning_rate_decay_rate',
            type=float, default=0.96,
            help='learning rate decay rate for NN training'
            ' (float, default = 0.96)')

    train_common_opt.add_argument('--batch-size', dest='batch_size', type=int,
            default=1,
            help='Number of streams in the Multi-stream training'
            '(int, default = 1)')

#    train_common_opt.add_argument('--num-streams', dest='nstreams', type=int,
#            default=1,
#            help='Number of streams in the Multi-stream training'
#            '(int, default = 1)')

    train_common_opt.add_argument('--num-frames-batch',
            dest='num_frames_batch',
            type=int, default=20,
            help='Length of \'one stream\' in the Multi-stream training'
            '(int, default = 20)')

    train_common_opt.add_argument('--overlap',
            dest='overlap',
            type=int, default=0,
            help='This parameter for lc lstm add, it\'s feature slice overlap length.'
            '(int, default = 0)')

    train_common_opt.add_argument('--init-scale', dest='init_scale', type=float,
            default=0.01,
            help='bound of the range of random values to generate'
            '(float, default = 0.01)')

#    train_common_opt.add_argument('--lr-decay-factor', dest='lr_decay_factor', type=float,
#            default = 0.5,
#            help='learn rate decay factor'
#            '(float, default = 0.5)')

    train_common_opt.add_argument('--grad-clip', dest='grad_clip', type=float,
            default = 5.0,
            help='Clipping the accumulated gradients (per-updates)'
            '(float, default = 5.0)')

    train_common_opt.add_argument('--optimizer', dest='optimizer',
            type=str, default='GD',
            help='optimizer : GD|Adam|Adadelta|AdagradDA|Adagrad'
            '(string, default = ""GD)')

    train_common_opt.add_argument('--use-sgd', dest='use_sgd', type=bool,
            default=False,
            help='train parameter way'
            '(bool, default = False)')

    train_common_opt.add_argument('--use-normal', dest='use_normal', type=bool,
            default=False,
            help='train parameter way'
            '(bool, default = False)')

    train_common_opt.add_argument('--use-sync', dest='use_sync', type=bool,
            default=False,
            help='train parameter way'
            '(bool, default = False)')

    train_common_opt.add_argument('--use-clip', dest='use_clip', type=bool,
            default=False,
            help='train parameter way'
            '(bool, default = False)')

    train_common_opt.set_defaults(cross_validate=False)
    train_common_opt.add_argument('--cross-validate', dest='cross_validate',
            action='store_true',
            help='Perform cross-validation (don\'t back-propagate)'
            ' (bool, default = false)')

    train_common_opt.add_argument('--momentum', type=float, default=0.0,
            help='Momentum'
            ' (float, default = 0.0)')

    train_common_opt.add_argument('--objective-function', dest='objective_function',
            type=str, default='ctc',
            help='Objective function : ctc|xent|mse'
            '(string, default = "ctc")')

    train_common_opt.add_argument('--report-step', dest='report_step', type=int,
            default=100,
            help='Step (number of sequences) for status reporting'
            ' (int, default = 100)')

    train_common_opt.add_argument('--time-major', dest='time_major', type=bool,
            default=False,
            help='time major'
            '(bool, default = True)')

    # no use
    train_common_opt.add_argument('--forward-only', dest='forward_only', type=bool,
            default=False,
            help='only calculate forward'
            '(bool, default = False)')

    train_common_opt.add_argument('--print-trainable-variables', dest='print_trainable_variables',
            type=bool, default=False,
            help='print trainable variables'
            '(bool, default = False)')

    '''
       #add lstm train relation option
    '''
    train_lstm_opt = parser.add_argument_group(title='train_lstm_opt',
            description='training lstm option relation parameters')

#    train_lstm_opt.add_argument('--state-is-tuple',dest='state_is_tuple',
#            type=bool, default=True,
#            help='Lstm state is tuple'
#            ' (bool, default = True)')

    train_lstm_opt.add_argument('--nnet-conf',dest='nnet_conf',
            type=str, default=None,
            help='It\'s save nnet configure parameter.'
            ' (bool, default = None)')

    # no need
    train_lstm_opt.add_argument('--use-gridlstm',dest='use_gridlstm',
            type=bool, default=False,
            help='First layer wether use gridlstm'
            ' (bool, default = False)')

    # no need
    train_lstm_opt.add_argument('--dropout-input-keep-prob', dest='dropout_input_keep_prob', type=float,
            default=1.0,
            help='dropout input keep prob'
            '(float, default = 1.0)')
    # use
    train_lstm_opt.add_argument('--dropout-output-keep-prob', dest='output_keep_prob', type=float,
            default=1.0,
            help='dropout output keep prob'
            '(float, default = 1.0)')
    # no need
    train_lstm_opt.add_argument('--rnn-state-reset-ratio', dest='rnn_state_reset_ratio', type=float,
            default=0.0,
            help='rnn state reset ratio'
            '(bool, default = 0.0)')

    '''
       #add mutually exclusive group 4 choise 1
    '''
#    group = parser.add_mutually_exclusive_group(required=True)
#    group.set_defaults(train=False)
#    group.set_defaults(file=None)
#    group.set_defaults(record=False)
#    group.set_defaults(evaluate=False)
#    group.add_argument('--train', dest='train', action='store_true', help='Train the network')
#    group.add_argument('--file', type=str, help='Path to a wav file to process')
#    group.add_argument('--record', dest='record', action='store_true', help='Record and write result on the fly')
#    group.add_argument('--evaluate', dest='evaluate', action='store_true', help='Evaluate WER against the test_set')

    '''
        #dict choise
    '''
    args = parser.parse_args(args_list)
    if args.start_frames >= args.skip_frame and args.start_frames != 0:
        raise 'error feature option'

    if args.tr_scp is None:
        raise 'input scp file it\'s None'

    if args.tr_label is None:
        logging.info('input label it\'s None')

    return args.__dict__

if __name__ == "__main__":
    args = parse_args(sys.argv[1:])
    print(args)
    for k,v in args.iteritems():
        print(k+':\t'+str(v))


from detectron2.layers import batched_nms
import pdb

import torch

def ml_nms(boxlist, nms_thresh, max_proposals=-1,
           score_field="scores", label_field="labels"):
    """
    Performs non-maximum suppression on a boxlist, with scores specified
    in a boxlist field via score_field.
    Arguments:
        boxlist(BoxList)
        nms_thresh (float)
        max_proposals (int): if > 0, then only the top max_proposals are kept
            after non-maximum suppression
        score_field (str)
    """
    if nms_thresh <= 0:
        return boxlist
    if boxlist.has('pred_boxes'):
        boxes = boxlist.pred_boxes.tensor
        labels = boxlist.pred_classes
    else:
        boxes = boxlist.proposal_boxes.tensor
        labels = boxlist.proposal_boxes.tensor.new_zeros(
            len(boxlist.proposal_boxes.tensor))
    scores = boxlist.scores

    # pdb.set_trace()
    # print(boxes.size())
    # print(scores.size())
    # print('prepare')

    # keep = batched_nms(boxes, scores, labels, nms_thresh)
    nmsed_boxes, nmsed_scores, nmsed_classes, _ = batched_nms(boxes, scores, labels, nms_thresh)
    # if max_proposals > 0:
    #     keep = keep[: max_proposals]
    keep = torch.tensor(range(0, 400))
    boxlist = boxlist[keep]
    # pdb.set_trace()
    # print(keep.size())
    boxlist.pred_boxes.tensor = torch.squeeze(nmsed_boxes)
    boxlist.scores =  torch.squeeze(nmsed_scores)
    boxlist.pred_classes.tensor = torch.squeeze(nmsed_classes)
    return boxlist


#!/usr/bin/python

import re
import time
import requests
import argparse
import urllib2
from pprint import pprint

import os
from sys import exit
from prometheus_client import start_http_server, Summary
from prometheus_client.core import GaugeMetricFamily, REGISTRY

DEBUG = int(os.environ.get('DEBUG', '0'))

COLLECTION_TIME = Summary('jenkins_collector_collect_seconds', 'Time spent to collect metrics from Jenkins')

class JenkinsCollector(object):
    # The build statuses we want to export about.
    statuses = ["lastBuild", "lastCompletedBuild", "lastFailedBuild",
                "lastStableBuild", "lastSuccessfulBuild", "lastUnstableBuild",
                "lastUnsuccessfulBuild"]

    def __init__(self, target):
        self._target = target.rstrip("/")

    # Desired initial metric:
    # users_logged_in{name="foo",channel="big_game_hunters"}
    def collect(self):
        """
        The collect method is special and must exist for the Python Prometheus
        exporter library to actually do anything.
        """
        start = time.time()

        # Request data from Jenkins
        # jobs = self._request_data()

        # self._setup_empty_prometheus_metrics()

        # ts3_user_login_count{user_name="foo", channel="blah"}

        metric = GaugeMetricFamily(
            'ts3_user_login_count',
            'Teamspeak 3 user logged in',
            labels=["user_name", "channel"])

        metric.add_metric(["foo", "blah"], 1)   # 1 user is logged in... I guess...
        metric.add_metric(["bar", "blah"], 1)
        metric.add_metric(["baz", "afk"], 1)

        yield metric

        # for status in self.statuses:
        #     for metric in self._prometheus_metrics[status].values():
        #         yield metric

        duration = time.time() - start
        COLLECTION_TIME.observe(duration)

    def _request_data(self):
        # Request exactly the information we need from Shivtr
        url = self._target
        jobs = "[fullName,number,timestamp,duration,actions[queuingDurationMillis,totalDurationMillis," \
               "skipCount,failCount,totalCount,passCount]]"
        tree = 'jobs[fullName,url,{0}]'.format(','.join([s + jobs for s in self.statuses]))

        params = {
            'tree': tree,
        }

        def parsejobs(myurl):
            # params = tree: jobs[name,lastBuild[number,timestamp,duration,actions[queuingDurationMillis...
            response = requests.get(myurl, params=params, verify=(not self._insecure))
            if DEBUG:
                pprint(response.text)
            if response.status_code != requests.codes.ok:
                raise Exception("Call to url %s failed with status: %s" % (myurl, response.status_code))
            result = response.json()
            if DEBUG:
                pprint(result)

            jobs = []
            for job in result['jobs']:
                if job['_class'] == 'com.cloudbees.hudson.plugins.folder.Folder' or \
                   job['_class'] == 'jenkins.branch.OrganizationFolder' or \
                   job['_class'] == 'org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject':
                    jobs += parsejobs(job['url'] + '/api/json')
                else:
                    jobs.append(job)
            return jobs

        return parsejobs(url)

    def _setup_empty_prometheus_metrics(self):
        # The metrics we want to export.
        self._prometheus_metrics = {}
        for status in self.statuses:
            snake_case = re.sub('([A-Z])', '_\\1', status).lower()
            self._prometheus_metrics[status] = {
                'number':
                    GaugeMetricFamily('jenkins_job_{0}'.format(snake_case),
                                      'Jenkins build number for {0}'.format(status), labels=["jobname"]),
                'duration':
                    GaugeMetricFamily('jenkins_job_{0}_duration_seconds'.format(snake_case),
                                      'Jenkins build duration in seconds for {0}'.format(status), labels=["jobname"]),
                'timestamp':
                    GaugeMetricFamily('jenkins_job_{0}_timestamp_seconds'.format(snake_case),
                                      'Jenkins build timestamp in unixtime for {0}'.format(status), labels=["jobname"]),
                'queuingDurationMillis':
                    GaugeMetricFamily('jenkins_job_{0}_queuing_duration_seconds'.format(snake_case),
                                      'Jenkins build queuing duration in seconds for {0}'.format(status),
                                      labels=["jobname"]),
                'totalDurationMillis':
                    GaugeMetricFamily('jenkins_job_{0}_total_duration_seconds'.format(snake_case),
                                      'Jenkins build total duration in seconds for {0}'.format(status), labels=["jobname"]),
                'skipCount':
                    GaugeMetricFamily('jenkins_job_{0}_skip_count'.format(snake_case),
                                      'Jenkins build skip counts for {0}'.format(status), labels=["jobname"]),
                'failCount':
                    GaugeMetricFamily('jenkins_job_{0}_fail_count'.format(snake_case),
                                      'Jenkins build fail counts for {0}'.format(status), labels=["jobname"]),
                'totalCount':
                    GaugeMetricFamily('jenkins_job_{0}_total_count'.format(snake_case),
                                      'Jenkins build total counts for {0}'.format(status), labels=["jobname"]),
                'passCount':
                    GaugeMetricFamily('jenkins_job_{0}_pass_count'.format(snake_case),
                                      'Jenkins build pass counts for {0}'.format(status), labels=["jobname"]),
            }

    def _get_metrics(self, name, job):
        for status in self.statuses:
            if status in job.keys():
                status_data = job[status] or {}
                self._add_data_to_prometheus_structure(status, status_data, job, name)

    def _add_data_to_prometheus_structure(self, status, status_data, job, name):
        # If there's a null result, we want to pass.
        if status_data.get('duration', 0):
            self._prometheus_metrics[status]['duration'].add_metric([name], status_data.get('duration') / 1000.0)
        if status_data.get('timestamp', 0):
            self._prometheus_metrics[status]['timestamp'].add_metric([name], status_data.get('timestamp') / 1000.0)
        if status_data.get('number', 0):
            self._prometheus_metrics[status]['number'].add_metric([name], status_data.get('number'))
        actions_metrics = status_data.get('actions', [{}])
        for metric in actions_metrics:
            if metric.get('queuingDurationMillis', False):
                self._prometheus_metrics[status]['queuingDurationMillis'].add_metric([name], metric.get('queuingDurationMillis') / 1000.0)
            if metric.get('totalDurationMillis', False):
                self._prometheus_metrics[status]['totalDurationMillis'].add_metric([name], metric.get('totalDurationMillis') / 1000.0)
            if metric.get('skipCount', False):
                self._prometheus_metrics[status]['skipCount'].add_metric([name], metric.get('skipCount'))
            if metric.get('failCount', False):
                self._prometheus_metrics[status]['failCount'].add_metric([name], metric.get('failCount'))
            if metric.get('totalCount', False):
                self._prometheus_metrics[status]['totalCount'].add_metric([name], metric.get('totalCount'))
                # Calculate passCount by subtracting fails and skips from totalCount
                passcount = metric.get('totalCount') - metric.get('failCount') - metric.get('skipCount')
                self._prometheus_metrics[status]['passCount'].add_metric([name], passcount)

def parse_args():
    parser = argparse.ArgumentParser(
        description='jenkins exporter args jenkins address and port'
    )
    parser.add_argument(
        '-u', '--url',
        metavar='url',
        required=False,
        help='shivtr ts3 url that returns the ts3 data',
        default=os.environ.get('TS3_SHIVTR_URL', 'http://jenkins:8080')
    )
    parser.add_argument(
        '-p', '--port',
        metavar='port',
        required=False,
        type=int,
        help='Listen to this port',
        default=int(os.environ.get('VIRTUAL_PORT', '5050'))
    )
    return parser.parse_args()

def main():
    try:
        args = parse_args()
        port = int(args.port)
        REGISTRY.register(JenkinsCollector(args.url))
        start_http_server(port)
        print("Polling {}. Serving at port: {}".format(args.url, port))
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        print(" Interrupted")
        exit(0)

if __name__ == "__main__":
    main()


'''

2017 IFN680 Assignment

Instructions:
    - You should implement the class PatternPosePopulation

'''

import numpy as np
import matplotlib.pyplot as plt

import pattern_utils
import population_search
import os
import logging
#------------------------------------------------------------------------------

class PatternPosePopulation(population_search.Population):
    '''

    '''
    def __init__(self, W, pat):
        '''
        Constructor. Simply pass the initial population to the parent
        class constructor.
        @param
          W : initial population
        '''
        self.pat = pat
        super().__init__(W)

    def evaluate(self):
        '''
        Evaluate the cost of each individual.
        Store the result in self.C
        That is, self.C[i] is the cost of the ith individual.
        Keep track of the best individual seen so far in
            self.best_w
            self.best_cost
        @return
           best cost of this generation

        '''
        self.best_w = self.W[0].copy()
        for i in range(len(self.C)):#for every element in the hight of the matrix
            self.C[i], temp = self.pat.evaluate(self.distance_image,self.W[i,:])  # Evalueate pattern with params W against distance image to get value C[i]
            if self.best_cost > self.C[i]: # If a new best cost is found
                self.best_cost = self.C[i]
                self.best_w = self.W[i].copy()
            #print(temp)

        return self.best_cost
        	# INSERT YOUR CODE HERE

    def mutate(self):
        '''
        Mutate each individual.
        The x and y coords should be mutated by adding with equal probability
        -1, 0 or +1. That is, with probability 1/3 x is unchanged, with probability
        1/3 it is decremented by 1 and with the same probability it is
        incremented by 1.
        The angle should be mutated by adding the equivalent of 1 degree in radians.
        The mutation for the scale coefficient is the same as for the x and y coords.
        @post:
          self.W has been mutated.
        '''
        #generate array of random -1 to 1, in shape of W
#        mutations = np.random.choice([-1., 0., 1.], self.W.shape)
        mutations = np.random.choice([-1., 0., 1.], self.W.shape, p=[1/3, 1/3, 1/3])
        # convert theta column to radians
        mutations[:,2] *= np.pi/180
        # Add mutation values to W
        self.W += mutations

    def set_distance_image(self, distance_image):
        self.distance_image = distance_image

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

def initial_population(region, scale = 10, pop_size=20):
    '''

    '''
    # initial population: exploit info from region
    rmx, rMx, rmy, rMy = region
    W = np.concatenate( (
                 np.random.uniform(low=rmx,high=rMx, size=(pop_size,1)) ,
                 np.random.uniform(low=rmy,high=rMy, size=(pop_size,1)) ,
                 np.random.uniform(low=-np.pi,high=np.pi, size=(pop_size,1)) ,
                 np.ones((pop_size,1))*scale
                 #np.random.uniform(low=scale*0.9, high= scale*1.1, size=(pop_size,1))
                        ), axis=1)
    return W

#------------------------------------------------------------------------------
def test_particle_filter_search(generation,individuals, IndexPattern, times):
    '''
    Run the particle filter search on test image 1 or image 2of the pattern_utils module

    '''
    testname = str(times) +':'+ str(IndexPattern) + ':'+ str(generation) + ' x ' + str(individuals)
    Testlogfile = str(times) +'-'+ str(IndexPattern) + '-'+ str(generation) + '_x_' + str(individuals)
    print(testname)

    if os.path.exists('log') is False:
        os.mkdir('log')

    logfile =  str(IndexPattern) + '-'+ str(generation) + '_x_' + str(individuals)+'.log'
    log_path = os.path.join('log',logfile)
    log_level = logging.DEBUG
    log_format = '%(message)s'
    logger = logging.root
    logger.basicConfig = logging.basicConfig(format=log_format, filename=log_path, level=log_level)
    logger.debug(testname)

    if True:
        # use image 1
        imf, imd , pat_list, pose_list = pattern_utils.make_test_image_1(True)
        ipat = IndexPattern # index of the pattern to target
    else:
        # use image 2
        imf, imd , pat_list, pose_list = pattern_utils.make_test_image_2(True)
        ipat = 0 # index of the pattern to target

    # Narrow the initial search region
    pat = pat_list[ipat] #  (100,30, np.pi/3,40),
    #    print(pat)
    xs, ys = pose_list[ipat][:2]
    region = (xs-20, xs+20, ys-20, ys+20)
    scale = pose_list[ipat][3]

    pop_size=individuals
    W = initial_population(region, scale , pop_size)

    pop = PatternPosePopulation(W, pat)
    pop.set_distance_image(imd)

    pop.temperature = 5

    #Dictionary to convert pattern value to string
    patDict = {0:"Small Square",1:"Large Square",2:"Large Triangle",3:"Small Triangle"}

    # Create Paths for outputs
    # Creaet Output Folder
    if os.path.exists('out') is False:
        os.mkdir('out')

    # Create 'Pat' Folder
    if os.path.exists('out\\'+patDict[IndexPattern]) is False:
        os.mkdir('out\\'+patDict[IndexPattern])

    # Create Gen vs Pop folder
    if os.path.exists('out\\'+patDict[IndexPattern]+'\\'+str(generation)+"x"+str(individuals)) is False:
        os.mkdir('out\\'+patDict[IndexPattern]+'\\'+str(generation)+"x"+str(individuals))

    Lw, Lc = pop.particle_filter_search(generation,log=True)

    plt.plot(Lc)
    plt.title('Cost vs generation index')
    plt.savefig(r'out\\'+patDict[IndexPattern]+'\\'+str(generation)+'x'+str(individuals)+'\\'+str(times)+'_'+str(generation) +'x'+str(individuals)+'Cost_Vs_Generation.png')
   #plt.show()

    #print(pop.best_w)
    #print(pop.best_cost)
    logger.debug(pop.best_w)
    logger.debug(pop.best_cost)

    pattern_utils.display_solution(pat_list,
                      pose_list,
                      pat,
                      pop.best_w)

#    with open('out'+str(generation)+'x'+str(individuals)+'.csv', 'r+') as out:
#        out.write('times, Pattern, generation, population, Best Cost, Best W \n')

    with open(FILENAME+'.csv', 'a') as out: #+str(generation)+'x'+str(individuals)+'
        out.write(str(times)+','+str(IndexPattern)+','+str(generation)+','+str(individuals)+','+str(pop.best_cost)+','+str(pop.best_w)+'\n')

#    pattern_utils.replay_search(pat_list,
#                      pose_list,
#                      pat,
#                      Lw)

   # os.rename('out', str(times)+'_'+ str(IndexPattern)+'_'+str(generation)+'_'+str(individuals))

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

# Allow FILENAME to be accessed globally
FILENAME = 'output'

if __name__=='__main__':

    Reverse_flag = False

   # for i  in range(1, 3):
   #     print (str(i) + ':' + str(IndexPattern) + ':'+ str(population) + 'x' + str(generation))
#test_particle_filter_search(40,30, 1,0)

    pop = [40]#,50,60,70] # Table of Populations to compare
    gen = [40]#,50,60,70] # Table of Generations to compare

    #Iterate through filenames to prevent fileoverwriting
    fileIndex = 0
    while os.path.exists(FILENAME+'_%s.csv'%fileIndex):
        fileIndex+=1

    # Set Filename to clear filename
    FILENAME +='_'+str(fileIndex)

    #create new file to write to or clears old file
    with open(FILENAME+'.csv', 'w') as out:
        out.write('')

#############################################################################################
    for pi in range(4):
        for ipop in range (len(pop)):
            for igen in range(len(gen)):
                for i  in range(1, 11):
                    print (str(i) + ':' + str(pi) + ':'+ str(pop[ipop]) + 'x' + str(gen[igen]))
                    test_particle_filter_search(gen[igen],pop[ipop], pi,i)

#############################################################################################

# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#                               CODE CEMETARY

#
#    def test_2():
#        '''
#        Run the particle filter search on test image 2 of the pattern_utils module
#
#        '''
#        imf, imd , pat_list, pose_list = pattern_utils.make_test_image_2(False)
#        pat = pat_list[0]
#
#        #region = (100,150,40,60)
#        xs, ys = pose_list[0][:2]
#        region = (xs-20, xs+20, ys-20, ys+20)
#
#        W = initial_population_2(region, scale = 30, pop_size=40)
#
#        pop = PatternPosePopulation(W, pat)
#        pop.set_distance_image(imd)
#
#        pop.temperature = 5
#
#        Lw, Lc = pop.particle_filter_search(40,log=True)
#
#        plt.plot(Lc)
#        plt.title('Cost vs generation index')
#        plt.show()
#
#        print(pop.best_w)
#        print(pop.best_cost)
#
#
#
#        pattern_utils.display_solution(pat_list,
#                          pose_list,
#                          pat,
#                          pop.best_w)
#
#        pattern_utils.replay_search(pat_list,
#                          pose_list,
#                          pat,
#                          Lw)
#
#    #------------------------------------------------------------------------------
#

IFN 680 - Assessment 1 - Current Best Answer/my_submission.py


from pathlib import Path
from uuid import uuid4 as uuid
from pyfakefs import fake_filesystem_unittest

from stores.template_store import template_store
from models.template import Template
from config.config import config
from db.db_connection import database
from utils.filesystem import file_system

class TemplateStoreTest(fake_filesystem_unittest.TestCase):
    #
    #   Setup
    #

    @classmethod
    def setUpClass(cls):
        config.open_config(use_default=True)

    def setUp(self):
        database.connect(':memory:')
        self.setUpPyfakefs()

    def tearDown(self):
        database.close()

    def test_create_creates_file(self):
        template = Template(
            'test_template', 'test_template.tex', '~/.texviper/templates')
        template_store.create(template, '')

        self.assertTrue(
            Path('~/.texviper/templates/test_template.tex').expanduser().exists()
        )

    def test_create_inserts_to_database(self):
        template = Template(
            'test_template', 'test_template.tex', '~/.texviper/templates')
        template_store.create(template, '')

        database.execute(
            '''select * from Templates where name="test_template"'''
        )
        result = database.fetch_one()

        self.assertEqual(len(result), 4)

    def test_delete_removes_file(self):
        template = Template(
            'test_template', 'test_template.tex', '~/.texviper/templates')
        template_store.create(template, '')

        template_store.delete_one(template.template_id)
        self.assertFalse(file_system.file_exists(
            Path('~/.texviper/templates/test_template.tex').expanduser()
        ))


import maya.cmds as cmds
from PySide2 import QtWidgets, QtCore, QtGui
import maya.OpenMayaUI as OpenMayaUI # for maya_main_window()
import shiboken2 #used to integrate C++ programs to Python

def maya_main_window():
    # To make our main window a child to maya so we can make it hover above maya
	main_window_ptr=OpenMayaUI.MQtUtil.mainWindow()
	return shiboken2.wrapInstance(int(main_window_ptr), QtWidgets.QWidget)

def deleteUI(my_ui):
    if cmds.window(my_ui, exists=1):
        cmds.deleteUI(my_ui)
    if cmds.windowPref(my_ui, exists=1):
        cmds.windowPref(my_ui, remove=1)

scroll_style = """
    QScrollBar:vertical {
        background: rgb(10,10,10);
        width: 5px;
        margin: 0px 0 0px 0;
        }

    QScrollBar::handle:vertical {
        border: 1px rgb(0, 0, 0);
        background: rgb(128, 123, 171);
        }
"""

class SelectionWidget(QtWidgets.QWidget):
    #create a selection set
    buttonSignal = QtCore.Signal(str)  # create a static attribute
    selectionClicked = QtCore.Signal(list)   # Create a custom signal to select the set

    def __init__(self, objects, count, ):
        super(SelectionWidget, self).__init__()

        self.objects = objects
        self.count = count
        self.state = True

        self.selection = []
        self.get_selection()

        self.object_path = self.get_selection()
        self.display_name = [obj.split("|")[-1] for obj in self.selection] #to display a short name

        self.setup_ui()

    def setup_ui(self):
        '''
        setting up UI for selection widget
        '''
        self.setMinimumSize(228,90)
        self.setMaximumHeight(90)
        self.setAutoFillBackground(True) #to set color we need this
        self.set_background() #set color

        #layout
        self.main_layout = QtWidgets.QHBoxLayout()
        self.setContentsMargins(5, 0, 5, 0)
        self.setLayout(self.main_layout)

        #text on the widget
        self.short_names_label = QtWidgets.QLineEdit()
        self.short_names_label.textChanged.connect(lambda x: self.short_names_label.displayText())
        self.short_names_label.setReadOnly(True)  # Set the QLineEdit as read-only initially

        self.short_names_label.setStyleSheet(
            "QLineEdit { "
            "border: 0px;"
            "background-color: rgb(60, 60, 60); "
            "color: rgb(128, 123, 171);"
            "font-size: 16px;  "
            "}")
        self.update_short_names_label()  # Update the label text with the short names
        self.main_layout.addWidget(self.short_names_label)
        self.short_names_label.setAlignment(QtCore.Qt.AlignCenter)

    def get_selection(self):
        '''
        With that one we create selection set
        '''
        self.selection = cmds.ls(sl=True, l=True)

    def update_short_names_label(self):
        '''
        Creates a name based on all the controllers that you selected.
        Convert the list of short names to a single string with comma separator.
        '''
        names_str = ", ".join(self.display_name)
        self.short_names_label.setText(names_str)

    def select_objects_back(self):
        cmds.select(self.objects)

    def set_background(self, r=60, g=60, b=60):
        self.p = QtGui.QPalette()
        self.color = QtGui.QColor(r, g, b)
        self.p.setColor(self.backgroundRole(), self.color)
        self.setPalette(self.p)

    def mouseReleaseEvent(self, event):
        '''
        selecting the set by releasing the button with function select_objects_back()
        '''
        self.p = self.palette()
        self.p.setColor(self.backgroundRole(), QtGui.QColor(90, 90, 90))
        self.setPalette(self.p)

        if self.state == True:
            self.select_objects_back()

    def enterEvent(self, event):
        self.setCursor(QtCore.Qt.PointingHandCursor)
        self.set_background(80, 80, 80)

    def leaveEvent(self, event):
        self.setCursor(QtCore.Qt.ArrowCursor)
        self.set_background(60, 60, 60)

    def mousePressEvent(self, event):
        '''
        we are calling pop menu on this event
        '''
        self.p = self.palette()
        self.p.setColor(self.backgroundRole(), QtGui.QColor(100, 100, 100))
        self.setPalette(self.p)

        if event.button() == QtCore.Qt.LeftButton:
            self.state = True

        elif event.button() == QtCore.Qt.RightButton:
            self.state = False  # we are blocking the button's pressing

            self.create_context_menu()  # instead we are creating pop-up context menu
            self.pop_menu.exec_(self.mapToGlobal(event.pos()))

    def separator_line(self):
        '''
        separator for create_context_menu()
        '''
        self.separator = QtWidgets.QAction(self)
        self.separator.setSeparator(True)
        self.pop_menu.addAction(self.separator)

    def create_context_menu(self):
        '''
        create a pop-up context menu with five new options for the selection set
        '''
        self.pop_menu = QtWidgets.QMenu(self)

        self.pop_menu_add = QtWidgets.QAction('Add Selection', self)  # QAction is a menu type
        self.pop_menu.addAction(self.pop_menu_add)
        self.pop_menu_add.triggered.connect(self.add_selection)

        self.pop_menu_remove = QtWidgets.QAction('Remove Selection', self)
        self.pop_menu.addAction(self.pop_menu_remove)
        self.pop_menu_remove.triggered.connect(self.remove_selection)

        self.separator_line()

        self.pop_menu_remove = QtWidgets.QAction('Rename', self)
        self.pop_menu.addAction(self.pop_menu_remove)
        self.pop_menu_remove.triggered.connect(self.rename_selection)

        self.pop_menu_delete = QtWidgets.QAction('Delete', self)
        self.pop_menu.addAction(self.pop_menu_delete)
        self.pop_menu_delete.triggered.connect(self.delete_selection)

        self.separator_line()

        self.pop_menu_close = QtWidgets.QAction('Close window', self)
        self.pop_menu.addAction(self.pop_menu_close)
        self.pop_menu_close.triggered.connect(self.close_window)

    def add_selection(self):
        '''
        'Add Selection'
        '''
        self.selection = cmds.ls(sl=True, l=True)
        for obj in self.selection:
            if obj not in self.objects:
                self.objects.append(obj)

    def remove_selection(self):
        '''
        'Remove Selection' on pop menu
        '''
        self.selection = cmds.ls(sl=True, l=True)
        for obj in self.selection:
            if obj in self.objects:
                self.objects.remove(obj)

    def rename_selection(self):
        '''
        'Rename' on pop menu
        '''
        self.short_names_label.setFocus()
        self.short_names_label.setReadOnly(False)
        self.short_names_label.editingFinished.connect(self.finish_editing)

    def finish_editing(self):
        '''
        This one we call in rename selection, it helps us to finish editing and reset focus
        '''
        self.short_names_label.setReadOnly(True)
        self.setFocus()

    def delete_selection(self):
        '''
        'Delete' on pop menu
        '''
        self.deleteLater()  # to delete widget

    def close_window(self):
        '''
        'Close window' on pop menu
        '''
        my_ui.close()  # close window

class PlusSelectionWidget(QtWidgets.QWidget):
    #create "+" widget for adding new sets of controls
    clicked = QtCore.Signal() # Define a custom clicked signal

    def __init__(self, icon=None, sub=False):
        super(PlusSelectionWidget, self).__init__()
        self.state = False
        self.sub = sub
        self.setup_ui()

    def setup_ui(self):
        '''
        Setting up UI for "+" widget
        '''
        self.setMinimumSize(228, 90)
        self.setMaximumHeight(90)
        self.setAutoFillBackground(True)  # to set color we need this
        self.set_background()  # set color
        self.main_layout = QtWidgets.QHBoxLayout()
        self.setContentsMargins(5, 0, 5, 0)
        self.setLayout(self.main_layout)

        # text on the widget
        self.plus_label = QtWidgets.QLabel("+")
        self.main_layout.addWidget(self.plus_label)
        self.plus_label.setAlignment(QtCore.Qt.AlignCenter)
        self.plus_label.setStyleSheet(''' font-size: 24px; ''')

    def mouseReleaseEvent(self, event):
        '''
        This one we use only for changing style on button released
        '''
        self.p = self.palette()
        self.p.setColor(self.backgroundRole(), QtGui.QColor(80, 80, 80))
        self.setPalette(self.p)

    def mousePressEvent(self, event):
        '''
        Style for the button and
        '''
        self.p = self.palette()
        self.p.setColor(self.backgroundRole(), QtGui.QColor(100, 100, 100))
        self.setPalette(self.p)

        # that's pretty important for mouseReleaseEvent on 44rd string
        if event.button() == QtCore.Qt.LeftButton:
            self.state = True
            self.on_widget_clicked(event)

        elif event.button() == QtCore.Qt.RightButton:
            self.state = False  # we are blocking the button's pressing

    def on_widget_clicked(self, event):
        # Emit the custom clicked signal when the widget is clicked
        self.clicked.emit()

    def set_background(self, r=60, g=60, b=60):
        # set background
        self.p = QtGui.QPalette()
        self.color = QtGui.QColor(r, g, b)
        self.p.setColor(self.backgroundRole(), self.color)
        self.setPalette(self.p)

    def enterEvent(self, event):
        self.setCursor(QtCore.Qt.PointingHandCursor)
        self.set_background(80, 80, 80)

    def leaveEvent(self, event):
        self.setCursor(QtCore.Qt.ArrowCursor)
        self.set_background(60, 60, 60)

class SelectionToolWindow(QtWidgets.QDialog):

    def __init__(self, parent=maya_main_window()):
        #creates window for a work with selection sets
        super(SelectionToolWindow, self).__init__(parent=parent)  # super is important to call the main class

        #to create a selection set
        self.selection = []
        self.get_selection()
        self.create_selection_list = {}
        self.num = 1

        #here we create translucent window but for real just removing the top part of the window
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.Tool)
        self.setSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
        self.offset = QtCore.QPoint(0, 0)
        self.pressed = False
        self.background_color = QtGui.QColor(45, 44, 44, 200)
        self.setAutoFillBackground(True)

        self.p = self.palette()
        self.p.setColor(self.backgroundRole(), QtGui.QColor(45, 44, 44))
        self.setPalette(self.p)

        #that will help us to frag the window
        self.setAcceptDrops(True)
        self.setMouseTracking(True)  # Track mouse movements
        self.draggable = False
        self.offset = None

        self.setup_ui() #main window and layouts
    def setup_ui(self):
        self.setWindowTitle("Custom Bunny UI")
        self.setObjectName("MyCustomWidgetBunny")

        self.setMinimumSize(250,250)
        self.setMaximumSize(250, 900)

        self.main_layout = QtWidgets.QVBoxLayout()
        self.main_layout.setAlignment(QtCore.Qt.AlignTop)
        self.main_layout.setContentsMargins(0, 0, 0, 0) #if you don't want any margins
        self.main_layout.setSpacing(3) #margins between buttons
        self.setLayout(self.main_layout)

        #* --------------------scroll area----------------------------- *#
        self.scroll_area = QtWidgets.QScrollArea()
        self.scroll_area.setMinimumHeight(400)
        self.scroll_area.setWidgetResizable(True)
        self.scroll_area.setMinimumWidth(250)
        self.scroll_area.setMaximumWidth(250)
        self.scroll_area.setFocusPolicy(QtCore.Qt.NoFocus)

        self.scrollbar = QtWidgets.QScrollBar()
        self.scrollbar.setStyleSheet(scroll_style)
        self.scroll_area.setVerticalScrollBar(self.scrollbar)

        self.scroll_area_widget = QtWidgets.QWidget()
        self.scroll_area.setWidget(self.scroll_area_widget)

        self.scroll_layout = QtWidgets.QVBoxLayout()
        self.scroll_layout.setAlignment(QtCore.Qt.AlignTop)
        self.scroll_layout.setContentsMargins(5, 0, 5, 0)
        self.scroll_layout.setSpacing(5) #layout
        self.scroll_area_widget.setLayout(self.scroll_layout)

        self.main_layout.addWidget(self.scroll_area)
        #* -------------------------------------------------------------- *#

        #adding plus widget
        self.plus_button = PlusSelectionWidget()
        self.main_layout.addWidget(self.plus_button)
        self.plus_button.clicked.connect(self.on_widget_clicked)

    def mousePressEvent(self, event):
        '''
        This one either drags window LB, either open a pop menu RB
        '''
        if event.button() == QtCore.Qt.LeftButton and event.y() <= 400:
            self.draggable = True
            self.offset = event.pos()

        elif event.button() == QtCore.Qt.RightButton:
            self.state = False  # we are blocking the button's pressing

            self.create_main_context_menu()  # instead we are creating pop-up context menu
            self.pop_main_menu.exec_(self.mapToGlobal(event.pos()))

    def mouseReleaseEvent(self, event):
        '''
        Need to support the window moving
        '''
        if event.button() == QtCore.Qt.LeftButton:
            self.draggable = False
            self.offset = None

    def mouseMoveEvent(self, event):
        '''
        Need to support the window moving
        '''
        if self.draggable and self.offset is not None:
            # Calculate the new window position based on the mouse movement
            new_pos = event.globalPos() - self.offset
            self.move(new_pos)

    def paintEvent(self, event):
        '''
        Painting boarders? not sure
        '''
        super(SelectionToolWindow, self).paintEvent(event)
        painter = QtGui.QPainter(self)
        painter.begin(self)
        painter.fillRect(0, 0, self.width(), self.height(), QtGui.QColor(68, 68, 68, 150))
        painter.setCompositionMode(QtGui.QPainter.CompositionMode_Source)
        painter.end()

    def on_widget_clicked(self):
        '''
        What's happening when you click "+" widget
        '''
        self.get_selection()
        self.add_selection_widget()

    def get_selection(self):
        self.selection = cmds.ls(sl=1, l=1) #duh

    def add_selection_widget(self):
        '''
        This one creates selection set by using SelectionWidget class and create_selection_list var
        '''
        self.selection_widget = SelectionWidget(self.selection, self.num)
        self.create_selection_list[self.num] = self.selection_widget
        self.num =+ 1

        self.scroll_layout.addWidget(self.selection_widget)

    def create_main_context_menu(self):
        '''
        create a pop-up context menu with a close option
        '''
        self.pop_main_menu = QtWidgets.QMenu(self)

        self.pop_main_menu_close = QtWidgets.QAction('Close window', self)
        self.pop_main_menu.addAction(self.pop_main_menu_close)
        self.pop_main_menu_close.triggered.connect(self.close_main_window)

    def close_main_window(self):
        '''
        To close window from pop up menu in the main window
        '''
        my_ui.close()

#---------------------------------THE-END------------------------------------------

deleteUI("MyCustomWidgetBunny")
global dialog
#also creating var for close_window() and close_main_window()
my_ui = SelectionToolWindow()
my_ui.show()


import numpy as np
import matplotlib.pyplot as plt

s = 1.0
ms = 0.001 * s

V = 1.0
mV = 0.001 * V

Ohm = 1.0
MOhm = 1000000 * Ohm

A = 1.0
nA = 0.000000001

tau_m = 10*ms
E_l = -70*mV
V_th = -40*mV
R_m = 10 * MOhm
I_e = 3.1*nA
delta_t = 0.25*ms

V = -70*mV

vs = []
ts = []

for i in np.arange(0, 1*s, 0.25*ms):
    V = V + delta_t * (E_l-V+R_m*I_e)/tau_m
    if V >= V_th:
        # spike
        V = E_l
    vs.append(V)
    ts.append(i)

print(vs)
plt.plot(ts, vs)
plt.show()


import base64

import requests
from django.http import JsonResponse, HttpResponse
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from rest_framework.authentication import SessionAuthentication, BasicAuthentication, TokenAuthentication
from rest_framework.permissions import IsAuthenticated

from altcoin.address.models import Address, AddressImpl
from altcoin.address.serializers import AddressSerializer
import json
from altcoin.errors import ValidationError, AuthenticationError
from altcoin.settings.base import get_env_var
from rest_framework.views import APIView
from altcoin.utils import Util

from altcoin.settings.local import db_uri
from bitcoinlib import keys
from bitcoinlib.wallets import HDWallet
from altcoin.address.serializers import QRSerializer
# import the logging library
import logging

# Get an instance of a logger
logger = logging.getLogger(__name__)

class NewAddressView(APIView):

    def get(self,request,account_id, wallet_id):
        try:
            logger.debug('Started ', self.__class__.__name__, ' get method')
            amount = request.GET.get('amount')
            qr = request.GET.get('qr')
            print('Started ', self.__class__.__name__, ' wallet_id=%s,  account_id=%s, qr=%s, amount=%s ' % (str(wallet_id), str(account_id), bool(qr), str(amount)))
            if account_id and wallet_id:
                addressimpl = AddressImpl()
                address = addressimpl.getNewAddress(account_id,wallet_id)
                if address :
                    response_dict = {'address_id' : address.address_id, 'status' : 'success' }
                    if qr :
                        text = address.network_name+':'+address.address_id+' amoount:' + str(amount)
                        output = Util.generate_qr(text)
                        print('received for  response generate_qr %s' % (output))
                        image = QRSerializer(output).data
                        response_dict['file_type'] = image['file_type']
                        response_dict['image_base64'] = image['image_base64']

                return JsonResponse(response_dict, safe=False)
            else:
                raise ValidationError(get_env_var('exception.validation.address.no_account_or_no_wallet'))
        except AuthenticationError as e:
            raise e
        except Exception as e:
            raise e

class AddressView(APIView):
    @method_decorator(cache_page(60 * 1))
    def get(self,request):

        print('Started ' + self.__class__.__name__ + ' get method')
        try:

            wallet_id = request.GET.get('wallet_id')
            just_address = request.GET.get('just_address')
            print('Started ',self.__class__.__name__ ,'  wallet_id=%s, just_address=%s ' %(str(wallet_id),
                  bool(str(just_address))))

            if wallet_id :
                wallet = HDWallet(wallet_id, db_uri=db_uri)
                keys = wallet.keys(include_private=True)
                keys = [x.__dict__ for x in keys]
                address_list = []
                if bool(just_address) == True:
                    for key in keys:
                        address_list.append(key['address'])
                else:
                    for key in keys:
                        address_list.append(
                            {'address_id': key['address'], 'private_key': key['private'], 'public_key': key['public']})
                return JsonResponse(address_list, safe=False)
            else:
                raise ValidationError(get_env_var('exception.validation.address.no_wallet'))
        except Exception as e:
            raise e


import sys
from collections import deque
class Node:
    def __init__(self, data, **kwargs):
        self.data = data
        self.left_child = kwargs.get('left_child', None)
        self.right_child = kwargs.get('right_child', None)

def search(root, data):
    current = root
    print('current is: ',current.data)

    while current.data != data:
        if current:
            print(' searching at current is: ', current.data)
        else:
            print('No data found')
            return

        if current.data > data:
            current = current.left_child

        if current.data < data:
            current = current.right_child
    return current.data

def insert(root,data):
    '''
    :param root: pointer to the root of the BST
    :param data: data of the new node
    :return: pointer to the root of new BST
    '''
    new_node = Node(int(data), left_child=None, right_child=None)
    if root is None:
        return new_node
    current = root
    while True:
        parent = current
        if parent.data > data:
            current = current.left_child
            if current is None:
                parent.left_child = new_node
                return root
        else:
            current = current.right_child
            if current is None:
                parent.right_child = new_node
                return root

def delete_node(root, data):
    '''
    :param root: pointer to the head or the BST
    :param data: delete the node with data
    :return: pointer to the head of a new BST
    '''
    if root is None:
        return root
    if root.data == data:
        return None
    if root.data > data:
        root.left_child = delete_node(root.left_child, data)
#TODO: Print tree
def pre_order(root):
    if root is None:
        return
    print(root.data, end=' ')
    pre_order(root.left_child)
    pre_order(root.right_child)

def post_order(root):
    if root is None:
        return
    post_order(root.left_child)
    post_order(root.right_child)
    print(root.data, end=' ')

def in_order(root):
    if root is None:
        return
    in_order(root.left_child)
    print(root.data, end=' ')
    in_order(root.right_child)

def height_of_tree(root):
    if root is None:
        return -1
    left_height = 1 + height_of_tree(root.left_child)
    right_height = 1 + height_of_tree(root.right_child)
    return max(left_height, right_height)

def top_view(root):
    if hasattr(top_view, 'counter') is False:
        top_view.counter = 0
    if root.left_child and top_view.counter >= 0:
        top_view.counter += 1
        top_view(root.left_child)
    top_view.counter -= 1
    print(root.data, end=' ')

    if root.right_child and top_view.counter < 0:
        top_view.counter -= 1
        top_view(root.right_child)

def topView(root, order = 0):
    if root is None:
        return
    if order <= 0:
        topView(root.left_child, -1)
    print(root.data, end=' ')
    if order >= 0:
        topView(root.right_child, 1)

#TODO: Breadth-first search (BFS) is an algorithm
def level_order(root):
    if root is None:
        return
    queue = deque()
    queue.append(root)
    while len(queue) != 0:
        current = queue.popleft()
        print(current.data, end=' ')
        if current.left_child:
            queue.append(current.left_child)
        if current.right_child:
            queue.append(current.right_child)

def main():
    first_root = Node(9)
    # insert(first_root, 11)
    # insert(first_root, 7)
    # insert(first_root, 14)
    # insert(first_root, 12)
    # insert(first_root, 13)
    # insert(first_root, 15)
    insert(first_root, 8)
    insert(first_root, 6)
    insert(first_root, 7)
    insert(first_root, 3)
    insert(first_root, 1)
    print('post order')
    post_order(first_root)
    print()
    print('pre order')
    pre_order(first_root)
    print()
    print('in order')
    in_order(first_root)
    print()
    print('height of tree: ', height_of_tree(first_root))
    print()
    print('top view')
    top_view(first_root)
    print()
    print('topView ')
    topView(first_root)
    print()
    print('level order')
    level_order(first_root)

if __name__ == '__main__':
    main()


import json
from flask import jsonify
from datetime import date

class Customer:
    def __init__(self, cpf, nome, email, dataDeNascimento, sexo, rendaMensal):
        self.id = 0
        self.cpf = cpf
        self.nome = nome
        self.email = email
        self.dataDeNascimento = dataDeNascimento
        self.sexo = sexo
        self.rendaMensal = rendaMensal

    def saveCustomer(customer):
        CCustomer = Customer(customer["cpf"], customer["nome"], customer["email"], customer["dataDeNascimento"], customer["sexo"], customer["rendaMensal"])

        with open("/db_jason/customers.json", "r+") as file:
            data = json.load(file)

            for p in data["customers"]:
                if p["cpf"] == CCustomer.cpf:
                    return "CPF já cadastrado!"

            CCustomer.id = len(data["customers"]) + 1
            CCustomer = CCustomer.__dict__
            data["customers"].append(CCustomer)

            file.seek(0)
            json.dump(data, file, indent=4)

            for person in data["customers"]:
                if person["id"] == CCustomer["id"]:
                    return jsonify("id: " + str(person["id"]))

        return "Storage Problem"

class Product:
    def __init__(self, nome, susep, expiracaoDeVenda, valorMinimoAporteInicial, valorMinimoAporteExtra, idadeDeEntrada, idadeDeSaida, carenciaInicialDeResgate, carenciaEntreResgates):
        self.id = 0
        self.nome = nome
        self.susep = susep
        self.expiracaoDeVenda = expiracaoDeVenda
        self.valorMinimoAporteInicial = valorMinimoAporteInicial
        self.valorMinimoAporteExtra = valorMinimoAporteExtra
        self.idadeDeEntrada = idadeDeEntrada
        self.idadeDeSaida = idadeDeSaida
        self.carenciaInicialDeResgate = carenciaInicialDeResgate
        self.carenciaEntreResgates = carenciaEntreResgates

    def saveProduct(product):
        PProduct = Product(product["nome"], product["susep"], product["expiracaoDeVenda"], product["valorMinimoAporteInicial"], product["valorMinimoAporteExtra"], product["idadeDeEntrada"], product["idadeDeSaida"], product["carenciaInicialDeResgate"], product["carenciaEntreResgates"])

        with open("db_json/products.json", "r+") as file:
            data = json.load(file)

            PProduct.id = len(data["products"]) + 1
            PProduct = PProduct.__dict__
            data["products"].append(PProduct)

            file.seek(0)
            json.dump(data, file, indent=4)

            for p in data["products"]:
                if p["id"] == PProduct["id"]:
                    return jsonify("id: " + str(p["id"]))

        return "Storage Problem"

class Plan:
    def __init__(self, customerId, productId, aporte, hiringDate):
        self.id = 0
        self.customerId = customerId
        self.productId = productId
        self.aporte = aporte
        self.hiringDate = hiringDate

    def savePlan(plan):
        PPlan = Plan(plan["customerId"], plan["productId"], plan["aporte"], plan["hiringDate"])
        with open("db_json/plans.json", "r+") as file:
            data = json.load(file)

            PPlan.id = len(data["plans"]) + 1

            #rules

            produto = getItemById(PPlan.productId, "products")
            if produto == False:
                return "Product not found"

            exp = produto["expiracaoDeVenda"]
            delta = date(int(exp[0:4]), int(exp[5:7]), int(exp[8:10])) - date.today()

            if delta.days < 0:
                    return "Product expired"

            customer = getItemById(PPlan.customerId, "customers")
            if customer == False:
                return "Customer not found"

            exp = customer["dataDeNascimento"]
            delta =  (date.today() - date(int(exp[0:4]), int(exp[5:7]), int(exp[8:10])))/365

            if delta.days < produto["idadeDeEntrada"] or delta.days > produto["idadeDeSaida"]:
                return "Customer not allowed"

            if plan["aporte"] < produto["valorMinimoAporteInicial"]:
                return "Aporte not allowed"

            #end rules
            PPlan = PPlan.__dict__
            data["plans"].append(PPlan)

            file.seek(0)
            json.dump(data, file, indent=4)

            for p in data["plans"]:
                if p["id"] == PPlan["id"]:
                    return jsonify("id: " + str(p["id"]))

        return "Storage Problem"

class AporteExtra:
    def __init__(self, customerId, productId, aporte):
        self.id = 0
        self.customerId = customerId
        self.productId = productId
        self.aporte = aporte

    def saveAporteExtra(aporte):
        AAporte = AporteExtra(aporte["customerId"], aporte["productId"], aporte["aporte"])
        with open("db_json/aporteextra.json", "r+") as file:
            data = json.load(file)

            AAporte.id = len(data["aporteextra"]) + 1

            #rules
            produto = Product.getProduct(AAporte.productId)
            if AAporte.aporte < produto["valorMinimoAporteExtra"]:
                return "Aporte not allowed"
            #end rules

            AAporte = AAporte.__dict__
            data["aporteextra"].append(AAporte)

            file.seek(0)
            json.dump(data, file, indent=4)

            for p in data["aporteextra"]:
                if p["id"] == AAporte["id"]:
                    return jsonify("id: " + str(p["id"]))

        return "Storage Problem"

class Resgate:
    def __init__(self, planId, resgateValue):
        self.id = 0
        self.planId = planId
        self.resgateValue = resgateValue
        self.resgateDate = str(date.today())

    def saveResgate(resgate):
        RResgate = Resgate(resgate["planId"], resgate["resgateValue"])
        aporte = 0
        data = {}
        with open("db_json/resgates.json", "r+") as file:
            data = json.load(file)

            RResgate.id = len(data["resgates"]) + 1
            #rules
            plano = getItemById(RResgate.planId, "plans")
            if plano == False:
                return "Plan not found"

            if plano["aporte"] < RResgate.resgateValue:
                return "Not enough founds"
            aporte = plano["aporte"] - RResgate.resgateValue

            exp = plano["hiringDate"]
            dt = date.today() - date(int(exp[0:4]), int(exp[5:7]), int(exp[8:10]))
            if int(dt.days) < 60:
                return "You need at least 60 days to resgate"

            for p in data["resgates"]:
                dt = date.today() - date(int(p["resgateDate"][0:4]), int(p["resgateDate"][5:7]), int(p["resgateDate"][8:10]))
                if int(dt.days) < 30:
                    return "You need at least 30 days to resgate again"

            #end rules
            RResgate = RResgate.__dict__
            data["resgates"].append(RResgate)

            file.seek(0)
            json.dump(data, file, indent=4)

        with open("db_json/plans.json", "r+") as file2:
            data1 = json.load(file2)
            for p in data1["plans"]:
                if p["id"] == RResgate["planId"]:
                    p["aporte"] = aporte
            file2.seek(0)
            json.dump(data1, file2, indent=4)

        for p in data["resgates"]:
            if p["id"] == len(data["resgates"]):
                return jsonify("id: " + str(p["id"]))

        return "Storage Problem"

# id = id do objeto a ser buscado
# item = nome do arquivo
def getItemById(id, item):
    with open(f"db_json/{item}.json", "r+") as file:
        data = json.load(file)
        for p in data[item]:
            if p["id"] == id:
                return p
        return False


import random
import art
import game_data
from replit import clear

def higher_lower ():
  print(art.logo)
  end = False
  score = 0
  while(not end):
    a=random.choice(game_data.data)
    b=random.choice(game_data.data)
    print(f"Compare A : {a['name']}, {a['description']}, {a['country']}")
    print(art.vs)
    print(f"Compare B : {b['name']}, {b['description']}, {b['country']}")
    opt = input("who has more following? Type 'A' or 'B' : ")
    if (opt == 'A' and a['follower_count']>b['follower_count']):
      score +=1
      clear()
      print(art.logo)
      print(f"You are right current score {score}")
    elif (opt == 'B' and a['follower_count']<b['follower_count']):
      score +=1
      clear()
      print(art.logo)
      print(f"You are right current score {score}")
    else:
      print("sorry")
      end = True

higher_lower()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 18 16:42:18 2018

@author: hajime

"""

import numpy as np
from matplotlib import pyplot as plt, cm as cm, mlab as mlab

x = np.linspace(-4,4,num=2000)
#x = np.random.normal(2.0,2.0,2000)
#x = 7-np.random.exponential(1.0,2000)
#x = np.random.triangular(0.,4.,4.,2000)
x_lin = np.linspace(x.min(),x.max(),num=2000)

n, bins, patches = plt.hist(x, 100,  facecolor='blue', alpha=0.5)
plt.title('x')
plt.show()
plt.close()

L1=1.
k1=1.
x_start1=-1.#-1.
#x_start1=2.
y1 = L1/(1.+np.exp(-k1*(x-x_start1 )))
y1_lin = L1/(1.+np.exp(-k1*(x_lin-x_start1 )))

plt.plot(x_lin,y1_lin)
plt.title('First CC growth')
plt.show()
plt.close()

n, bins, patches = plt.hist(y1, 100,  facecolor='blue', alpha=0.5)
plt.title('CC1')
plt.show()
plt.close()

L2=1.
k2=1.
x_start2=6.0
#x_start2=9.0
y2 = L2/(1.+np.exp(-k2*(x-x_start2 )))
y2_lin = L2/(1.+np.exp(-k2*(x_lin-x_start2 )))
#y2 = np.exp(.3*x)

plt.plot(x_lin,y2_lin)
plt.title('Second CC growth')
plt.show()
plt.close()

n, bins, patches = plt.hist(y2, 100,  facecolor='blue', alpha=0.5)
plt.title('CC2')
plt.show()
plt.close()

Y=.5*y1+.5*y2
Y_lin=.5*y1_lin+.5*y2_lin
plt.plot(x_lin,Y_lin)
plt.show()
plt.close()

n, bins, patches = plt.hist(Y, 100,  facecolor='blue', alpha=0.5)
plt.title('Two CC combined')
plt.show()
plt.close()


import time
import random
import itertools
import numpy as np
import matplotlib.pyplot as plt

from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn.metrics import cohen_kappa_score
from sklearn.metrics import classification_report
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import RandomForestClassifier

import rpy2.robjects as robjects
from rpy2.robjects.packages import importr
RRF = importr("RRF")
from rpy2.robjects import numpy2ri

numpy2ri.activate()

def diff(first, second):
    second = set(second)
    return [item for item in first if item not in second]

def ind_VfoldCross(data, selec):
    cls = np.unique(data)
    arr_train = []

    for i in cls:
        # get the indexes for each
        ind = np.where(data == i)

        if len(ind[0]) <= selec:
            sel = random.sample(range(len(ind[0])), round(len(ind[0])*2/3))
            arr_train.extend(ind[0][sel])
        else:
            sel = random.sample(range(len(ind[0])), selec)
            arr_train.extend(ind[0][sel])

    return arr_train

def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title, fontsize=16)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45, fontsize=12)
    plt.yticks(tick_marks, classes, fontsize=12)

    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix')

    # print(cm)

    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, cm[i, j],
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")

    # plt.tight_layout()
    plt.ylabel('True label', fontsize=13)
    plt.xlabel('Predicted label', fontsize=13)

def split_data(data, labels, Ntrain, Nval, method):

    if method.find('class') >= 0:
        list_of_train = ind_VfoldCross(labels, Ntrain)
    else:
        list_of_train = random.sample(range(data.shape[0]),Ntrain)
    list_of_rest = diff(range(len(data)), list_of_train)
    X_sub = data[list_of_rest, :]
    Y_sub = labels[list_of_rest]
    if method.find('class')>=0:
        list_of_val = ind_VfoldCross(Y_sub, Nval)
    else:
        list_of_val = random.sample(range(X_sub.shape[0]),Nval)
    list_of_test = diff(range(len(X_sub)), list_of_val)

    data_tr = data[list_of_train, :]
    data_tr = np.array(data_tr, dtype="float64")
    labels_tr = labels[list_of_train]
    if method.find('class')>=0:
        labels_tr = [str(labels_tr[t][0]) for t in range(0, len(labels_tr))]
    else:
        labels_tr = [str(labels_tr[t]) for t in range(0, len(labels_tr))]

    data_vl = X_sub[list_of_val, :]
    data_vl = np.array(data_vl, dtype="float64")
    labels_vl = Y_sub[list_of_val]
    if method.find('class')>=0:
        labels_vl = [labels_vl[t][0] for t in range(0, len(labels_vl))]
    else:
        labels_vl = [labels_vl[t] for t in range(0, len(labels_vl))]

    data_ts = X_sub[list_of_test, :]
    data_ts = np.array(data_ts, dtype="float64")
    labels_ts = Y_sub[list_of_test]
    if method.find('class')>=0:
        labels_ts = [labels_ts[t][0] for t in range(0, len(labels_ts))]
    else:
        labels_ts = [labels_ts[t] for t in range(0, len(labels_ts))]

    return data_tr, labels_tr, data_vl, labels_vl, data_ts, labels_ts

def RandomForestFI(data_tr, labels_tr, method):

    t = time.time()
    if method.find('class')>=0:
        rf = RandomForestClassifier(n_estimators=200, n_jobs=-1)
    else:
        rf = RandomForestRegressor(n_estimators=200, n_jobs=-1)
    rf = rf.fit(data_tr, labels_tr)
    print('RF done!')
    importances = rf.feature_importances_
    importRF_all = importances / max(importances)
    indicesRF = np.argsort(importRF_all, axis=None)[::-1]
    elapsed = time.time() - t
    print('time of RF using all features: ', elapsed)
    return rf, importRF_all, indicesRF

def GRRFoptimization(lambda0, gamma, importRF_all, indicesRF,
                     data_tr, labels_tr, data_vl, labels_vl, Niter, method):
    summary = []
    for lam in lambda0:
        for gam in gamma:
            if gam != 0 or lam != 0:
                if method.find('class')>=0:
                    OAgrrf = []
                    kgrrf = []
                else:
                    RMSEgrrf = []
                    Rgrrf = []

                BestcoefReg = (1 - gam) * lam + gam * importRF_all
                if method.find('class')>=0:
                    grrf = RRF.RRF(robjects.r.matrix(data_tr, nrow=data_tr.shape[0],
                                                 ncol=data_tr.shape[1]),
                               robjects.vectors.FactorVector(labels_tr),
                               flagReg=1, coefReg=BestcoefReg)
                else:
                    grrf = RRF.RRF(robjects.r.matrix(data_tr, nrow=data_tr.shape[0],
                                                     ncol=data_tr.shape[1]),
                                   robjects.vectors.FloatVector(labels_tr),
                                   flagReg=1, coefReg=BestcoefReg)
                selected_features = grrf.rx2('feaSet')
                selected_features = np.array([x - 1 for x in selected_features])

                col = selected_features
                colRF = indicesRF[0:len(col)]

                for N2 in range(Niter):
                    X_train1 = data_tr[:, col]
                    X_val1 = data_vl[:, col]

                    if method.find('class')>=0:
                        grrf_F = RandomForestClassifier(n_estimators=200, n_jobs=-1)
                    else:
                        grrf_F = RandomForestRegressor(n_estimators=200, n_jobs=-1)
                    grrf_F = grrf_F.fit(X_train1, labels_tr)

                    Ypred_GRRF = grrf_F.predict(X_val1)

                    if method.find('class')>=0:
                        Ypred_GRRF = [int(i) for i in Ypred_GRRF]
                        OA_GRRF = accuracy_score(labels_vl, Ypred_GRRF)
                        kappa_GRRF = cohen_kappa_score(labels_vl, Ypred_GRRF)
                    else:
                        rmse_grrf =  np.sqrt(np.mean((labels_vl-Ypred_GRRF) ** 2))
                        r_grrf = np.corrcoef(labels_vl,Ypred_GRRF)[1, 0]

                    X_train1 = data_tr[:, colRF]
                    X_val1 = data_vl[:, colRF]

                    if method.find('class')>=0:
                        rf_F = RandomForestClassifier(n_estimators=200, n_jobs=-1)
                    else:
                        rf_F = RandomForestRegressor(n_estimators=200, n_jobs=-1)

                    rf_F = rf_F.fit(X_train1, labels_tr)
                    Ypred_RF = rf_F.predict(X_val1)

                    if method.find('class')>=0:
                        Ypred_RF = [int(i) for i in Ypred_RF]
                        OA_RF = accuracy_score(labels_vl, Ypred_RF)
                        print(['gamma: ' + str(gam) + ' and lambda ' + str(lam) + ' select '
                               + str(len(colRF))+' features:' + ' GRRF OA= ' + str(OA_GRRF*100)
                               + ' and RFsel OA= ' + str(OA_RF*100)])

                        OAgrrf.append(OA_GRRF)
                        kgrrf.append(kappa_GRRF)
                    else:
                        rmse_rf = np.sqrt(np.mean((labels_vl-Ypred_RF) ** 2))

                        print(['gamma: ' + str(gam) + ' and lambda ' + str(lam) + ' select '
                               + str(len(colRF))+' features:' + ' GRRF RMSE= ' + str(rmse_grrf)
                               + ' and RFsel RMSE= ' + str(rmse_rf)])
                        RMSEgrrf.append(rmse_grrf)
                        Rgrrf.append(r_grrf)

                if method.find('class')>=0:
                    OA1 = np.mean(OAgrrf)
                    K1 = np.mean(kgrrf)
                    results = [gam, lam, OA1, K1]
                else:
                    RMSE1 = np.mean(RMSEgrrf)
                    R1 = np.mean(Rgrrf)
                    results = [gam, lam, RMSE1, R1]
                summary.append(results)

    return summary

def SelectedGRRFfeatures(BestGamma, BestLambda, importRF_all, data_tr, labels_tr,
                         data_vl, labels_vl, method):
    BestcoefReg = (1 - BestGamma) * BestLambda + BestGamma * importRF_all
    X_train = np.concatenate((data_tr, data_vl), axis=0)
    Y_train = np.concatenate((labels_tr, labels_vl), axis=0)

    if method.find('class')>=0:
        final_grrf = RRF.RRF(robjects.r.matrix(X_train, nrow=X_train.shape[0], ncol=X_train.shape[1]),
                         robjects.vectors.FactorVector(Y_train),
                         flagReg=1, coefReg=BestcoefReg)
    else:
        final_grrf = RRF.RRF(robjects.r.matrix(X_train, nrow=X_train.shape[0],
                                                     ncol=X_train.shape[1]),
                                   robjects.vectors.FloatVector(Y_train),
                                   flagReg=1, coefReg=BestcoefReg)

    selected_features = final_grrf.rx2('feaSet')
    importances = final_grrf.rx2('importance')
    # Indices python start 0 and in R in 1:
    selected_features = np.array([x - 1 for x in selected_features])

    importances = importances[selected_features]
    importances = importances / np.max(importances)
    indices = np.argsort(importances, axis=None)[::-1]

    return indices, importances, selected_features

def prediction(data_tr, labels_tr, data_ts, labels_ts, classes_names, method, selected_features=None):

    if selected_features is not None:
        X_train = data_tr[:, selected_features]
        X_ts = data_ts[:, selected_features]
    else:
        X_train = data_tr
        X_ts = data_ts

    if method.find('class')>=0:
        final = RandomForestClassifier(n_estimators=200, n_jobs=-1)
    else:
        final = RandomForestRegressor(n_estimators=200, n_jobs=-1)
    final = final.fit(X_train, labels_tr)

    Ypred = final.predict(X_ts)
    if method.find('class')>=0:
        Ypred = [int(i) for i in Ypred]

        output1 = accuracy_score(labels_ts, Ypred)
        output3 = cohen_kappa_score(labels_ts, Ypred)
        output2 = confusion_matrix(labels_ts, Ypred)
        output4 = classification_report(labels_ts, Ypred, target_names=classes_names)
    else:
        output1 = np.sqrt(np.mean((labels_ts - Ypred) ** 2))
        output2 = np.mean(labels_ts-Ypred)
        output3 = np.mean(np.abs(labels_ts-Ypred))
        output4 = np.corrcoef(labels_ts,Ypred)[1, 0]

    return final, output1, output2, output3, output4

def mapping(final, data_total, labels_total, selected_features, nr, nc, method):

    if len(selected_features) > 0:
        X_total = data_total[:, selected_features]
    else:
        X_total = data_total
    Yimage = final.predict(X_total)
    if method.find('class')>=0:
        Yimage = np.array([int(i) for i in Yimage]).reshape(nr * nc, 1)
        Yimage[labels_total == 0] = 0
    Yimage = Yimage.reshape((nr, nc))
    return Yimage


import time

from selenium import webdriver
from selenium.webdriver.common.by import By

URL = "https://the-internet.herokuapp.com/checkboxes"

def checkboxes(check1, check2):

    driver = webdriver.Chrome()
    try:
        driver.get(URL)
        checks = driver.find_elements(By.CSS_SELECTOR,"#checkboxes > input[type=checkbox]")
        chk_check1= checks[0]
        chk_check2= checks[1]

        chk_check1_checked =chk_check1.get_property("checked")
        chk_check2_checked =chk_check2.get_property("checked")

        if (chk_check1_checked and not check1) or (not chk_check1_checked and check1):
            chk_check1.click()
        if (chk_check2_checked and not check2) or (not chk_check2_checked and check2):
            chk_check2.click()

    finally:
        time.sleep(3)
        driver.close()

checkboxes(True, True)
checkboxes(True, False)
checkboxes(False, False)
checkboxes(False, True)


import commands
import argparse
from utils import get_list_from_file, makefile, get_file_list_from_dir, check_pattern_exist, makedir

run_darknet = "/home/sap/da_yolo/darknet"
base = "/home/sap/da_yolo/da_yolo"
train_base = "sim10k"
val_base = "cityscapes1_val"
plot_base = "plot"
pretrain_path = "/data1/sap/backup/darknet53.conv.74"
gpus = "0"
data_name = "obj.data"
cfg_name = "obj.cfg"
loss_name = "loss.log"

parser = argparse.ArgumentParser()
parser.add_argument("--base", default=base)
parser.add_argument("--train_base", default=train_base)
parser.add_argument("--val_base", default=val_base)
parser.add_argument("--plot_base", default=plot_base)
parser.add_argument("--pretrain_path", default=pretrain_path)
parser.add_argument("--gpus", default=gpus)
parser.add_argument("--data_name", default=data_name)
parser.add_argument("--cfg_name", default=cfg_name)
parser.add_argument("--loss_name", default=loss_name)
args = parser.parse_args()

base = args.base
train_base = args.train_base
val_base = args.val_base
plot_base = args.plot_base
pretrain_path = args.pretrain_path
gpus = args.gpus
data_name = args.data_name
cfg_name = args.cfg_name
loss_name = args.loss_name

def get_backup_path_from_train_data(train_data_pah):
    content_list = get_list_from_file(train_data_pah)
    for content in content_list :
        a, b = content.split("=")
        if a.strip() == "backup":
            return b.strip() + '/'

def darknet_map(darknet, data, cfg, wgt, log):
    makefile(log)
    cmd_write_name = "echo \"#\" " + wgt + " >> " + log
    _, _ = commands.getstatusoutput(cmd_write_name)
    cmd_map = darknet + " detector map " + data + " " + cfg + " " + wgt + " >> " +  log
    print(cmd_map)
    _, output = commands.getstatusoutput(cmd_map)

def darknet_multiple_map(darknet, data, cfg, wgt_dir, log, is_dont_100 = True, is_dont_final = True):
    wgt_list = get_file_list_from_dir(wgt_dir)
    if(is_dont_100) :
        pattern = '.*_100.weights'
        wgt_list = [wgt for wgt in wgt_list if not check_pattern_exist(wgt, pattern)]
    if(is_dont_final) :
        pattern = '.*_final.weights'
        wgt_list = [wgt for wgt in wgt_list if not check_pattern_exist(wgt, pattern)]
    size = len(wgt_list)
    for i, wgt in enumerate(wgt_list) :
        print(i, '/', size)
        darknet_map(darknet, data, cfg, wgt, log)

if __name__ == "__main__" :
    train_data_path = base + "/" + train_base + "/" + data_name
    train_cfg_path = base + "/" + train_base + "/" + cfg_name
    train_loss_path = base + "/" + train_base + "/" + loss_name
    train_backup_path = get_backup_path_from_train_data(train_data_path)
    makedir(train_backup_path)

    val_data_path = base + "/" + val_base + "/" + data_name
    val_backup_path = train_backup_path
    val_map_path = base + "/" + val_base + "/map_" + val_base + "_with_" + train_base + ".log"

    plot_save_path = base + "/" + plot_base + "/map_" + val_base + "_with_" + train_base + ".png"

    train_yolo = run_darknet + " detector train " + train_data_path + " " + train_cfg_path + " " + pretrain_path + " -dont_show -gpus " + gpus + " > " + train_loss_path
    plot_yolo = "python /home/sap/script_da/yolo/plot_yolo_log.py " + train_loss_path + " " + val_map_path + " " + plot_save_path

    #print(train_yolo)
    #_, output = commands.getstatusoutput(train_yolo)
    #print(output)
    #darknet_multiple_map(run_darknet, val_data_path, train_cfg_path, val_backup_path, val_map_path)
    print(plot_yolo)
    #_, output = commands.getstatusoutput(plot_yolo)
    #print(output)


# -*- coding: utf-8 -*-
"""
This script gets you started with the GerryChain package.
"""

### https://gerrychain.readthedocs.io/en/latest/user/recom.html
### https://gerrychain.readthedocs.io/en/latest/user/quickstart.html

## Import needed functions from library
from gerrychain import Graph, Partition, Election
from gerrychain.updaters import Tally, cut_edges
from gerrychain import MarkovChain
from gerrychain.constraints import single_flip_contiguous
from gerrychain.proposals import propose_random_flip
from gerrychain.accept import always_accept
import pandas as pd
import matplotlib.pyplot as plt
from gerrychain import (GeographicPartition, proposals, updaters, constraints, accept)
from gerrychain.tree_proposals import recom
from functools import partial
import tqdm

## Parameters
pennDataPathPrefix = ""
shpFileSuffix = "PA_VTD.shp"
jsonFileSuffix = "PA_VTD.json"

#### Import Data

#### The Graph.from_file() classmethod creates a Graph of the precincts in our
#### shapefile. By default, this method copies all of the data columns from the
#### shapefile’s attribute table to the graph object as node attributes.
#### The contents of this particular shapefile’s attribute table are summarized
#### in the mggg-states/PA-shapefiles GitHub repo.

#### Depending on the size of the state, the process of generating an adjacency
#### graph can take a bit of time. To avoid having to repeat this process in
#### the future, we call graph.to_json() to save the graph in the NetworkX
#### json_graph format under the name "PA_VTD.json.

graph = Graph.from_file(pennDataPathPrefix+shpFileSuffix)

graph.to_json(pennDataPathPrefix+jsonFileSuffix)

## Simple Example

#### In order to run a Markov chain, we need an adjacency Graph of our VTD
#### geometries and Partition of our adjacency graph into districts.
#### This Partition will be the initial state of our Markov chain.

#### We configure an Election object representing the 2012 Senate
#### election, using the USS12D and USS12R vote total columns from our
#### shapefile. The first argument is a name for the election ("SEN12"), and
#### the second argument is a dictionary matching political parties to their
#### vote total columns in our shapefile. This will let us compute hypothetical
#### election results for each districting plan in the ensemble.

election = Election("SEN12", {"Dem": "USS12D", "Rep": "USS12R"})

#### Finally, we create a Partition of the graph. This will be the starting
#### point for our Markov chain.

initial_partition = Partition(
    graph,
    assignment="2011_PLA_1",
    updaters={
        "cut_edges": cut_edges,
        "population": Tally("TOT_POP", alias="population"),
        "SEN12": election
    }
)

#### With the "population" updater configured, we can see the total population
#### in each of our congressional districts. In an interactive Python session,
#### we can print out the populations like this:

for district, pop in initial_partition["population"].items():
    print("District {}: {}".format(district, pop))

#### Notice that partition["population"] is a dictionary mapping the ID of each
#### district to its total population (that’s why we can call the .items()
#### method on it). Most updaters output values in this dictionary format.

#### For more information on updaters, see the gerrychain.updaters documentation.

#### Running a chain
#### Now that we have our initial partition, we can configure and run a Markov
#### chain. Let’s configure a short Markov chain to make sure everything works
#### properly.

chain = MarkovChain(
    proposal=propose_random_flip,
    constraints=[single_flip_contiguous],
    accept=always_accept,
    initial_state=initial_partition,
    total_steps=1000
)

#### The above code configures a Markov chain called chain, but does not run it
#### yet. We run the chain by iterating through all of the states using a for
#### loop. As an example, let’s iterate through this chain and print out the
#### sorted vector of Democratic vote percentages in each district for each
#### step in the chain.

for partition in chain:
    print(sorted(partition["SEN12"].percents("Dem")))

#### That’s all: you’ve run a Markov chain!

#### To analyze the Republican vote percentages for each districting plan in
#### our ensemble, we’ll want to actually collect the data, and not just print
#### it out. We can use a list comprehension to store these vote percentages,
#### and then convert it into a pandas DataFrame.

d_percents = [sorted(partition["SEN12"].percents("Dem")) for partition in chain]

data = pd.DataFrame(d_percents)

#### This code will collect data from a different ensemble than our for loop
#### above. Each time we iterate through the chain object, we run a fresh new
#### Markov chain (using the same configuration that we defined when
#### instantiating chain).

#### The pandas DataFrame object has many helpful methods for analyzing and
#### plotting data. For example, we can produce a boxplot of our ensemble’s
#### Democratic vote percentage vectors, with the initial 2011 districting plan
#### plotted in red, in just a few lines of code:
ax = data.boxplot()
data.iloc[0].plot(style="ro", ax=ax)

plt.show()

#### (Before you over-analyze this data, keep in mind that this is a toy
#### ensemble of just one thousand plans created by single flips.)

#### Recon Sample
#### This document shows how to run a chain using the ReCom proposal used in
#### MGGG’s 2018 Virginia House of Delegates report.

#### Our goal is to use ReCom to generate an ensemble of districting plans for
#### Pennsylvania, and then make a box plot comparing the Democratic vote
#### shares for plans in our ensemble to the 2011 districting plan that the
#### Pennsylvania Supreme Court found to be a Republican-favoring partisan
#### gerrymander.

#### Setting up the initial districting plan
#### We configure Election objects representing some of the election data from
#### our shapefile.

elections = [
    Election("SEN10", {"Democratic": "SEN10D", "Republican": "SEN10R"}),
    Election("SEN12", {"Democratic": "USS12D", "Republican": "USS12R"}),
    Election("SEN16", {"Democratic": "T16SEND", "Republican": "T16SENR"}),
    Election("PRES12", {"Democratic": "PRES12D", "Republican": "PRES12R"}),
    Election("PRES16", {"Democratic": "T16PRESD", "Republican": "T16PRESR"})
]

#### Configuring our updaters
#### We want to set up updaters for everything we want to compute for each plan
#### in the ensemble.

# Population updater, for computing how close to equality the district
# populations are. "TOT_POP" is the population column from our shapefile.
my_updaters = {"population": updaters.Tally("TOT_POP", alias="population")}

# Election updaters, for computing election results using the vote totals
# from our shapefile.
election_updaters = {election.name: election for election in elections}
my_updaters.update(election_updaters)

#### Instantiating the partition
#### We can now instantiate the initial state of our Markov chain, using the 2011 districting plan:

initial_partition = GeographicPartition(graph,
                                        assignment="2011_PLA_1",
                                        updaters=my_updaters)

#### GeographicPartition comes with built-in area and perimeter updaters. We do
#### not use them here, but they would allow us to compute compactness scores
#### like Polsby-Popper that depend on these measurements.

#### Setting up the Markov chain
#### Proposal
#### First we’ll set up the ReCom proposal. We need to fix some parameters
#### using functools.partial before we can use it as our proposal function.

# The ReCom proposal needs to know the ideal population for the districts so that
# we can improve speed by bailing early on unbalanced partitions.

ideal_population = sum(initial_partition["population"].values()) / len(initial_partition)

# We use functools.partial to bind the extra parameters (pop_col, pop_target, epsilon, node_repeats)
# of the recom proposal.
proposal = partial(recom,
                   pop_col="TOT_POP",
                   pop_target=ideal_population,
                   epsilon=0.02,
                   node_repeats=2
                  )

#### Constraints
#### To keep districts about as compact as the original plan, we bound the
#### number of cut edges at 2 times the number of cut edges in the initial
#### plan.

compactness_bound = constraints.UpperBound(
    lambda p: len(p["cut_edges"]),
    2*len(initial_partition["cut_edges"])
)

pop_constraint = constraints.within_percent_of_ideal_population(initial_partition, 0.02)

#### Configuring the Markov chain
chain = MarkovChain(
    proposal=proposal,
    constraints=[
        pop_constraint,
        compactness_bound
    ],
    accept=accept.always_accept,
    initial_state=initial_partition,
    total_steps=1000
)

#### Running the chain
#### Now we’ll run the chain, putting the sorted Democratic vote percentages
#### directly into a pandas DataFrame for analysis and plotting. The DataFrame
#### will have a row for each state of the chain. The first column of the
#### DataFrame will hold the lowest Democratic vote share among the districts
#### in each partition in the chain, the second column will hold the
#### second-lowest Democratic vote shares, and so on.

# This will take about 10 minutes.

#data = pd.DataFrame(
#    sorted(partition["SEN12"].percents("Democratic"))
#    for partition in chain
#)

#### If you install the tqdm package, you can see a progress bar as the chain
#### runs by running this code instead:

data = pd.DataFrame(
   sorted(partition["SEN12"].percents("Democratic"))
   for partition in chain.with_progress_bar()
)

#### Create a plot
#### Now we’ll create a box plot similar to those appearing the Virginia report.

fig, ax = plt.subplots(figsize=(8, 6))

# Draw 50% line
ax.axhline(0.5, color="#cccccc")

# Draw boxplot
data.boxplot(ax=ax, positions=range(len(data.columns)))

# Draw initial plan's Democratic vote %s (.iloc[0] gives the first row)
data.iloc[0].plot(style="ro", ax=ax)

# Annotate
ax.set_title("Comparing the 2011 plan to an ensemble")
ax.set_ylabel("Democratic vote % (Senate 2012)")
ax.set_xlabel("Sorted districts")
ax.set_ylim(0, 1)
ax.set_yticks([0, 0.25, 0.5, 0.75, 1])

plt.show()

#### There you go! To build on this, here are some possible next steps:

#### Add, remove, or tweak the constraints
#### Use a different proposal from GerryChain, or create your own
#### Perform a similar analysis on a different districting plan for Pennsylvania
#### Perform a similar analysis on a different state
#### Compute partisan symmetry scores like Efficiency Gap or Mean-Median, and create a histogram of the scores of the ensemble.
#### Perform the same analysis using a different election than the 2012 Senate election
#### Collect Democratic vote percentages for _all_ the elections we set up, instead of just the 2012 Senate election.


import pygame as pg
import numpy as np
import objct
import cam
import matrixs

def gen_fig(size, height):
    points = [[0,height,0]]
    l = np.sqrt(size*size / 0.5857)
    pn = np.array([l,0,0,1])
    p = [pn[0],pn[1],pn[2]]
    points.append(p)
    rotate = matrixs.rotate_y(np.pi/4)
    for i in range(7):
        pn = pn @ rotate
        p = [pn[0],pn[1],pn[2]]
        points.append(p)
    faces = [[1,2,0],[2,3,0],[3,4,0],[4,5,0],[5,6,0],[6,7,0],[7,8,0],[8,1,0],[8,7,6,5,4,3,2,1]]
    return objct.Object(points,faces)

class scale_bar:
    def __init__(self,screen,posx,posy,width,height):
        self.x = posx
        self.y = posy
        self.width = width
        self.height = height
        self.pos = 1
        self.screen = screen

    def check_res(self):
        if pg.mouse.get_pressed()[0] != 0:
            mouse_pos = pg.mouse.get_pos()
            if(mouse_pos[0] > self.x and mouse_pos[0] < self.x + self.width and mouse_pos[1] > self.y and mouse_pos[1] < self.y + self.height):
                self.pos = mouse_pos[0] - self.x
        pg.draw.rect(self.screen,'black',pg.Rect(self.x,self.y + self.height/2-3,self.width,3))
        pg.draw.rect(self.screen,'red',pg.Rect(self.x + self.pos, self.y,10,self.height))
        return self.pos/self.width

class Program:
    def __init__(self):
        pg.init()
        self.RES =  self.WIDTH, self.HEIGHT = 1600, 900
        self.FPS = 60
        self.screen = pg.display.set_mode(self.RES)
        self.clock = pg.time.Clock()
        self.camera = cam.Camera(self.screen,self.HEIGHT,self.WIDTH)
        pg.display.set_caption('Main')
        pg.font.init()
        self.font = pg.font.SysFont('Hack', 24)

    def draw(self):
        self.screen.fill('azure3')

    def run(self):

        scale1 = scale_bar(self.screen,self.WIDTH - 230,30,200,30)
        scale2 = scale_bar(self.screen,self.WIDTH - 230,70,200,30)
        textsurface1 = self.font.render('a', False, (0, 0, 0))
        textsurface2 = self.font.render('h', False, (0, 0, 0))
        a = scale1.check_res() * 20
        h = scale2.check_res() * 20
        while True:
            ob1 = gen_fig(a,h)
            self.camera.Control()
            self.draw()
            self.camera.Draw_obj(ob1)
            a = scale1.check_res() * 10
            h = scale2.check_res() * 20
            [exit() for i in pg.event.get() if i.type == pg.QUIT]
            self.screen.blit(textsurface1,(self.WIDTH - 250,30))
            self.screen.blit(textsurface2,(self.WIDTH - 250,70))
            pg.display.flip()
            self.clock.tick(self.FPS)

if __name__ == '__main__':
    pr = Program()
    pr.run()