import numpy as np
from typing import Tuple, Optional, Union, Set, Dict, Any
from fractions import Fraction
import random
import pygame
import sys
import os
import json

# from misc import *
from typing import List, Tuple, Union, Dict
from genetic_algorithm.individual import Individual
from neural_network import FeedForwardNetwork, linear, sigmoid, tanh, relu, leaky_relu, ActivationFunction, get_activation_by_name
from settings import settings

BLACK = (0,0,0)
WHITE = (255,255,255)
RED = (255,0,0)
GREEN = (0,180,0)
BLUE = (50,200,255)

class Bird(Individual):
    def __init__(self,
                chromosome: Optional[Dict[str, List[np.ndarray]]] = None,
                x_pos: Optional[int] = settings['init_bird_x_pos'],
                y_pos: Optional[int] = settings['init_bird_y_pos'],
                # x_pos: Optional[int] = 100,
                # y_pos: Optional[int] = 250,
                y_speed: Optional[int] = 0,
                hidden_layer_architecture: Optional[List[int]] = [12, 20],
                hidden_activation: Optional[ActivationFunction] = 'relu',
                output_activation: Optional[ActivationFunction] = 'sigmoid'
                ):

        self.Window_Width = settings['Window_Width']
        self.Window_Height = settings['Window_Height']

        self.x_pos = x_pos
        self.y_pos = y_pos
        self.y_speed = y_speed

        self.bird_surface = pygame.image.load("assets/bluebird-midflap.png").convert_alpha()
        self.bird_surface = pygame.transform.scale2x(self.bird_surface)
        self.bird_rect = self.bird_surface.get_rect(center = (self.x_pos, self.y_pos))

        self._fitness = 0  # Overall fitness
        self.score = 0
        self.x_distance_to_next_pipe_center = 0
        self.y_distance_to_next_pipe_center = 0
        self.is_alive = True

        self.hidden_layer_architecture = hidden_layer_architecture
        self.hidden_activation = hidden_activation
        self.output_activation = output_activation

        # Setting up network architecture
        num_inputs = 5 #@TODO: how many
        self.network_architecture = [num_inputs]                          # Inputs
        self.network_architecture.extend(self.hidden_layer_architecture)  # Hidden layers
        self.network_architecture.append(2)                               # 2 output, jump or not jump
        self.network = FeedForwardNetwork(self.network_architecture,
                                          get_activation_by_name(self.hidden_activation),
                                          get_activation_by_name(self.output_activation)
        )

        # If chromosome is set, take it
        if chromosome:
            # self._chromosome = chromosome
            self.network.params = chromosome
            # self.decode_chromosome()
        else:
            # self._chromosome = {}
            # self.encode_chromosome()
            pass

    @property
    def fitness(self):
        return self._fitness

    def calculate_fitness(self):
        # Give positive minimum fitness for roulette wheel selection
        # self._fitness = (self._frames) + ((2**self.score) + (self.score**2.1)*500) - (((.25 * self._frames)**1.3) * (self.score**1.2))
        # self._fitness = (self._frames) + ((2**self.score) + (self.score**2.1)*500) - (((.25 * self._frames)) * (self.score))
        print(self.y_distance_to_next_pipe_center)
        self._fitness = (2 ** self.score + self.score * 2.1) * 200 + 1000 - (self.x_distance_to_next_pipe_center + abs(self.y_distance_to_next_pipe_center)) * 1.2
        self._fitness = max(self._fitness, .1)

    @property
    def chromosome(self):
        # return self._chromosome
        pass

    def encode_chromosome(self):
        # # L = len(self.network.params) // 2
        # L = len(self.network.layer_nodes)
        # # Encode weights and bias
        # for layer in range(1, L):
        #     l = str(layer)
        #     self._chromosome['W' + l] = self.network.params['W' + l].flatten()
        #     self._chromosome['b' + l] = self.network.params['b' + l].flatten()
        pass

    def decode_chromosome(self):
        # # L = len(self.network.params) // 2
        # L = len(self.network.layer_nodes)
        # # Decode weights and bias
        # for layer in range(1, L):
        #     l = str(layer)
        #     w_shape = (self.network_architecture[layer], self.network_architecture[layer-1])
        #     b_shape = (self.network_architecture[layer], 1)
        #     self.network.params['W' + l] = self._chromosome['W' + l].reshape(w_shape)
        #     self.network.params['b' + l] = self._chromosome['b' + l].reshape(b_shape)
        pass

    # restart the game for this individual
    def reset(self):
        self._fitness = 0
        self.score = 0
        self.x_distance_to_next_pipe_center = 0
        self.y_distance_to_next_pipe_center = 0
        self.x_pos = settings['init_bird_x_pos']
        self.y_pos = settings['init_bird_y_pos']
        self.y_speed = 0
        self.is_alive = True

    def update(self, inputs):
        self.network.feed_forward(inputs)
        if self.network.out == 0:
            # do nothing
            pass
        elif self.network.out == 1:
            # jump
            self.y_speed = settings['bird_jump_speed']

        return True

    def move(self, pipes):
        self.y_pos += self.y_speed
        self.y_speed += settings['gravity']
        self.score += 2/1.2/60
        self.check_collision(pipes)
        # self.distance_travelled += abs(self.xspeed)
        # if self.x_pos < 0:
        #     self.x_pos = 0
        # elif self.x_pos > self.board_size[0]-100:
        #     self.x_pos = self.board_size[0]-100

    def check_collision(self, pipes):
        for pipe in pipes:
            if self.bird_rect.colliderect(pipe):
                self.is_alive = False
        if self.bird_rect.top <= -100 or self.bird_rect.bottom >= self.Window_Height - 100:
            self.is_alive = False

    def rotate_bird(self, bird_surface):
        new_bird = pygame.transform.rotozoom(bird_surface, self.y_speed * -3, 1)
        return new_bird

    # Draw the bird
    def draw(self, screen, winner=False, champion=False):
        if not self.is_alive:
            return
        if champion:
            self.bird_surface = pygame.image.load("assets/redbird-midflap.png").convert_alpha()
            self.bird_surface = pygame.transform.scale2x(self.bird_surface)
        elif winner:
            self.bird_surface = pygame.image.load("assets/yellowbird-midflap.png").convert_alpha()
            self.bird_surface = pygame.transform.scale2x(self.bird_surface)

        self.bird_rect.centery = self.y_pos
        rotated_bird_surface = self.rotate_bird(self.bird_surface)
        screen.blit(rotated_bird_surface, self.bird_rect)
        # TODO: include different color for champion and winner
        # if champion:
        #     pygame.draw.rect(screen,BLACK,[self.x_pos,self.y_pos,100,20])
        #     pygame.draw.rect(screen,GREEN,[self.x_pos+2,self.y_pos+2,100-4,20-4])
        # elif winner:
        #     pygame.draw.rect(screen,BLACK,[self.x_pos,self.y_pos,100,20])
        #     pygame.draw.rect(screen,BLUE,[self.x_pos+2,self.y_pos+2,100-4,20-4])
        # else:
        #     pygame.draw.rect(screen,BLACK,[self.x_pos,self.y_pos,100,20])
        #     pygame.draw.rect(screen,WHITE,[self.x_pos+2,self.y_pos+2,100-4,20-4])

def save_bird(population_folder: str, individual_name: str, bird: Bird, settings: Dict[str, Any]) -> None:
    # Make population folder if it doesn't exist
    if not os.path.exists(population_folder):
        os.makedirs(population_folder)

    # Save off settings
    if 'settings.json' not in os.listdir(population_folder):
        f = os.path.join(population_folder, 'settings.json')
        with open(f, 'w', encoding='utf-8') as out:
            json.dump(settings, out, sort_keys=True, indent=4)

    # Make directory for the individual
    individual_dir = os.path.join(population_folder, individual_name)
    if individual_name in os.listdir(population_folder):
        shutil.rmtree(individual_dir)
    os.makedirs(individual_dir)

    # Save some constructor information for replay
    # @NOTE: No need to save chromosome since that is saved as .npy
    # @NOTE: No need to save board_size or hidden_layer_architecture
    #        since these are taken from settings
    # constructor = {}
    # constructor['start_pos'] = snake.start_pos.to_dict()
    # constructor['apple_seed'] = snake.apple_seed
    # constructor['initial_velocity'] = snake.initial_velocity
    # constructor['starting_direction'] = snake.starting_direction
    # snake_constructor_file = os.path.join(individual_dir, 'constructor_params.json')

    # # Save
    # with open(snake_constructor_file, 'w', encoding='utf-8') as out:
    #     json.dump(constructor, out, sort_keys=True, indent=4)

    L = len(bird.network.layer_nodes)
    for l in range(1, L):
        w_name = 'W' + str(l)
        b_name = 'b' + str(l)

        weights = bird.network.params[w_name]
        bias = bird.network.params[b_name]

        np.save(os.path.join(individual_dir, w_name), weights)
        np.save(os.path.join(individual_dir, b_name), bias)

# def load_snake(population_folder: str, individual_name: str, settings: Optional[Union[Dict[str, Any], str]] = None) -> Snake:
#     if not settings:
#         f = os.path.join(population_folder, 'settings.json')
#         if not os.path.exists(f):
#             raise Exception("settings needs to be passed as an argument if 'settings.json' does not exist under population folder")

#         with open(f, 'r', encoding='utf-8') as fp:
#             settings = json.load(fp)

#     elif isinstance(settings, dict):
#         settings = settings

#     elif isinstance(settings, str):
#         filepath = settings
#         with open(filepath, 'r', encoding='utf-8') as fp:
#             settings = json.load(fp)

#     params = {}
#     for fname in os.listdir(os.path.join(population_folder, individual_name)):
#         extension = fname.rsplit('.npy', 1)
#         if len(extension) == 2:
#             param = extension[0]
#             params[param] = np.load(os.path.join(population_folder, individual_name, fname))
#         else:
#             continue

#     # Load constructor params for the specific snake
#     constructor_params = {}
#     snake_constructor_file = os.path.join(population_folder, individual_name, 'constructor_params.json')
#     with open(snake_constructor_file, 'r', encoding='utf-8') as fp:
#         constructor_params = json.load(fp)

#     snake = Snake(settings['board_size'], chromosome=params,
#                   start_pos=Point.from_dict(constructor_params['start_pos']),
#                   apple_seed=constructor_params['apple_seed'],
#                   initial_velocity=constructor_params['initial_velocity'],
#                   starting_direction=constructor_params['starting_direction'],
#                   hidden_layer_architecture=settings['hidden_network_architecture'],
#                   hidden_activation=settings['hidden_layer_activation'],
#                   output_activation=settings['output_layer_activation'],
#                   lifespan=settings['lifespan'],
#                   apple_and_self_vision=settings['apple_and_self_vision']
#                   )
#     return snake


import os
import os.path
import numpy
import scipy.sparse
import torch
from download_torch_ready_data import Reddit, SmallerReddit, OneQuarterReddit, TinyReddit
from torch_geometric.data import Data

from torch_geometric.utils.convert import to_networkx
import networkx
import metis

def metis_reorder(edge_index, x, y, split, P=8):
    # a reorder with gap example:
    # parts        = 0,1,0,1,0,1
    # node_ids     = 1,2,5,6,7,8
    # will sort to = 1,5,7,2,6,8
    # e.g. the old id=5 should be loc to idx=2(which is idx of old id=2), so change id=5 to 2.
    # e.g. id=7 should be loc to idx=3(which is idx of old id=5), so change id=7 to 5.
    # node_mapping = 1:1, 5:2, 7:5, ...

    #print('edge index', edge_index)
    nxg = to_networkx(Data(x=x, edge_index=edge_index))
    #print('partition begin')
    # print('nxg', nxg.nodes)
    # print('nxg', nxg.edges)
    (edgecuts, parts) = metis.part_graph(nxg, P)
    #print('parted', edgecuts, parts)
    node_ids = list(nxg) # only works when no gap in ids
    parted_ids = [(p,old_node_id) for p, old_node_id in zip(parts, node_ids)]
    sorted_node_ids = [node_id for p, node_id in sorted(parted_ids)]
    #print('sorted node ids', sorted_node_ids)

    id_mapping = dict((old_id, new_id) for old_id, new_id in zip(sorted_node_ids, node_ids))
    #print('id mapping', id_mapping)
    old_edge_index = torch.clone(edge_index)

    edge_index[0] = torch.tensor([id_mapping[old_id.item()] for old_id in old_edge_index[0]])
    edge_index[1] = torch.tensor([id_mapping[old_id.item()] for old_id in old_edge_index[1]])
    #print('node swapped', edge_index)

    def swap_tensor(t):
        # st = torch.empty_like(t)
        st = t[sorted_node_ids].clone()
        return st

    x = swap_tensor(x)
    #print(y.size(), y)
    y = swap_tensor(y)
    #print(y.size(), y)
    #print(split.size(), split)
    split = swap_tensor(split)
    #print(split.size(), split)

    return edge_index, x, y, split

def create_parted_dataset(original_dataset):
    original_name = original_dataset.__class__.__name__
    edge_index, x, y, split = metis_reorder(*list(map(original_dataset.data_dict.get, ['edge_index', 'x', 'y', 'split'])))
    parted_data_path = os.path.join('..', 'torch_ready_data', 'Parted'+original_name, 'processed', 'data.pt')
    os.makedirs(os.path.dirname(parted_data_path), exist_ok=True)
    torch.save({"x": x, "y": y, "edge_index": edge_index, "split": split}, parted_data_path)

def main():
    #torch.set_printoptions(threshold=5000)
    # list(map(create_parted_dataset, [SmallerReddit(), OneQuarterReddit(), Reddit()]))
    create_parted_dataset(Reddit())
    # create_parted_dataset(TinyReddit())

    # Cora()
    # Amazon()
    pass

if __name__ == '__main__':
    main()


from flask import Flask, render_template,request
from flask_bootstrap import Bootstrap
import json
from main import get_stories,get_random
app = Flask(__name__)

store_id=0
@app.route("/",methods=['GET', 'POST'])
def main():
    if request.method == 'POST':
        album_id = int(request.form['album_id'])
        global store_id
        store_id=album_id
        try:
            input_image,input_text,output_image,output_text=get_stories(album_id,shuffle=True)
        except:
            return render_template('error.html')
        return render_template('index.html',sorted_image=output_image,sorted_text=output_text,shuffled_image=input_image,shuffled_text=input_text)
    else:
        input_image,input_text,output_image,output_text=get_stories(Random=True)
        return render_template('index.html',sorted_image=output_image,sorted_text=output_text,shuffled_image=input_image,shuffled_text=input_text)

@app.route("/shuffle",methods=['POST'])

def shuffle():
    album_id = store_id
    try:
        input_image,input_text,output_image,output_text=get_stories(album_id,shuffle=True)
    except:
        return render_template('error.html')
    return render_template('index.html',sorted_image=output_image,sorted_text=output_text,shuffled_image=input_image,shuffled_text=input_text)

@app.route("/arrange",methods=['POST'])

def arrange():
    album_id = store_id
    try:
        input_image,input_text,output_image,output_text=get_stories(album_id,shuffle=False)
    except:
        return render_template('error.html')
    return render_template('index.html',sorted_image=output_image,sorted_text=output_text,shuffled_image=input_image,shuffled_text=input_text)

@app.route("/example")
def example():
    input_image_list=[]
    input_text_list=[]
    output_image_list=[]
    output_text_list=[]
    index=[243,654,342,676,23]
    for i in index:
        input_image,input_text,output_image,output_text=get_random(i)
        input_image_list.append(input_image)
        input_text_list.append(input_text)
        output_image_list.append(output_image)
        output_text_list.append(output_text)
    return render_template('example.html',im1=input_image_list,text1=input_text_list,im2=output_image_list,text2=output_text_list)

@app.route("/about")
def about():
    return render_template('about.html')

if __name__ == "__main__":
    app.run(debug=True)


"""Random coefficient nested logit model(RCNL,BLP1995)"""

import pyblp
import numpy as np
import pandas as pd

pyblp.options.digits = 2
pyblp.options.verbose = False
pyblp.__version__

# data
product_data = pd.read_csv(pyblp.data.NEVO_PRODUCTS_LOCATION)
product_data.head()

X1_formulation = pyblp.Formulation('0 + prices', absorb='C(product_ids)')
X2_formulation = pyblp.Formulation('1 + prices + sugar + mushy')
product_formulations = (X1_formulation, X2_formulation)

mc_integration = pyblp.Integration('monte_carlo', size=50, seed=0)
pr_integration = pyblp.Integration('product', size=5)

mc_problem = pyblp.Problem(product_formulations, product_data, integration=mc_integration)
pr_problem = pyblp.Problem(product_formulations, product_data, integration=pr_integration)
pr_problem

bfgs = pyblp.Optimization('bfgs', {'gtol': 1e-10})
bfgs

results1 = mc_problem.solve(sigma=np.ones((4, 4)), optimization=bfgs)
print(results1)
results1.beta

mc_problem


#!/usr/bin/env python
# Copyright (c) 2015 IBM. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
_account_tests_

account module - Unit tests for CouchDB and Cloudant account classes

See configuration options for environment variables in unit_t_db_base
module docstring.

"""

import unittest
import requests
import json
import base64
import os
from datetime import datetime

from cloudant.account import Cloudant
from cloudant.errors import CloudantException

from .unit_t_db_base import UnitTestDbBase
from ... import bytes_, str_

class AccountTests(UnitTestDbBase):
    """
    CouchDB/Cloudant Account unit tests
    """

    def test_constructor_with_url(self):
        """
        Test instantiating an account object using a URL
        """
        self.assertEqual(
            self.client.cloudant_url,
            self.url
            )
        self.assertEqual(self.client.encoder, json.JSONEncoder)
        self.assertIsNone(self.client.r_session)

    def test_connect(self):
        """
        Test connect and disconnect functionality
        """
        try:
            self.client.connect()
            self.assertIsInstance(self.client.r_session, requests.Session)
            self.assertEqual(self.client.r_session.auth, (self.user, self.pwd))
        finally:
            self.client.disconnect()
            self.assertIsNone(self.client.r_session)

    def test_session(self):
        """
        Test getting session information
        """
        try:
            self.client.connect()
            session = self.client.session()
            self.assertEqual(session['userCtx']['name'], self.user)
        finally:
            self.client.disconnect()

    def test_session_cookie(self):
        """
        Test getting the session cookie
        """
        try:
            self.client.connect()
            self.assertIsNotNone(self.client.session_cookie())
        finally:
            self.client.disconnect()

    def test_basic_auth_str(self):
        """
        Test getting the basic authentication string
        """
        try:
            self.client.connect()
            expected = 'Basic {0}'.format(
                str_(base64.urlsafe_b64encode(bytes_("{0}:{1}".format(self.user, self.pwd))))
                )
            self.assertEqual(
                self.client.basic_auth_str(),
                expected
                )
        finally:
            self.client.disconnect()

    def test_all_dbs(self):
        """
        Test getting a list of all of the databases in the account
        """
        dbnames = [self.dbname() for _ in range(3)]
        try:
            self.client.connect()
            for dbname in dbnames:
                self.client.create_database(dbname)
            self.assertTrue(set(dbnames).issubset(self.client.all_dbs()))
        finally:
            for dbname in dbnames:
                self.client.delete_database(dbname)
            self.client.disconnect()

    def test_create_delete_database(self):
        """
        Test database creation and deletion
        """
        try:
            self.client.connect()
            dbname = self.dbname()
            # Create database
            db = self.client.create_database(dbname)
            self.assertTrue(db.exists())
            # Delete database
            self.assertIsNone(self.client.delete_database(dbname))
            self.assertFalse(db.exists())
        finally:
            self.client.disconnect()

    def test_create_existing_database(self):
        """
        Test creation of already existing database
        """
        dbname = self.dbname()
        try:
            self.client.connect()
            self.client.create_database(dbname)
            self.client.create_database(dbname, throw_on_exists=True)
            self.fail('Above statement should raise a CloudantException')
        except CloudantException as err:
            self.assertEqual(
                str(err),
                'Database {0} already exists'.format(dbname)
                )
        finally:
            self.client.delete_database(dbname)
            self.client.disconnect()

    def test_delete_non_existing_database(self):
        """
        Test deletion of non-existing database
        """
        try:
            self.client.connect()
            self.client.delete_database('no_such_db')
            self.fail('Above statement should raise a CloudantException')
        except CloudantException as err:
            self.assertEqual(str(err), 'Database no_such_db does not exist')
        finally:
            self.client.disconnect()

    def test_keys(self):
        """
        Test retrieving the list of database names for the given client account
        """
        try:
            self.client.connect()
            self.assertEqual(list(self.client.keys()), [])
            self.assertEqual(
                self.client.keys(remote=True),
                self.client.all_dbs()
                )
        finally:
            self.client.disconnect()

    def test_get_non_existing_db_via_getitem(self):
        """
        Test __getitem__ when retrieving a non-existing database
        """
        try:
            self.client.connect()
            db = self.client['no_such_db']
            self.fail('Above statement should raise a KeyError')
        except KeyError:
            pass
        finally:
            self.client.disconnect()

    def test_get_db_via_getitem(self):
        """
        Test __getitem__ when retrieving a database
        """
        dbname = self.dbname()
        try:
            self.client.connect()
            self.client.create_database(dbname)
            # Retrieve the database object from the server using __getitem__
            db = self.client[dbname]
            self.assertIsInstance(db, self.client._DATABASE_CLASS)
        finally:
            self.client.delete_database(dbname)
            self.client.disconnect()

    def test_delete_cached_db_object_via_delitem(self):
        """
        Test __delitem__ when removing a cached database object
        """
        dbname = self.dbname()
        try:
            self.client.connect()
            db = self.client.create_database(dbname)
            self.assertIsNotNone(self.client.get(dbname))
            del self.client[dbname]
            # Removed from local cache
            # Note: The get method returns a local db object by default
            self.assertIsNone(self.client.get(dbname))
            # Database still exists remotely
            # Note: __getitem__ returns the db object from the server
            self.assertEqual(self.client[dbname], db)
        finally:
            self.client.delete_database(dbname)
            self.client.disconnect()

    def test_delete_remote_db_via_delitem(self):
        """
        Test __delitem__ when removing a database from the account
        """
        dbname = self.dbname()
        try:
            self.client.connect()
            db = self.client.create_database(dbname)
            self.assertIsNotNone(self.client.get(dbname))
            self.client.__delitem__(dbname, remote=True)
            # Removed from local cache
            self.assertIsNone(self.client.get(dbname))
            # Database removed remotely as well
            try:
                db = self.client[dbname]
                self.fail('Above statement should raise a KeyError')
            except KeyError:
                pass
        finally:
            self.client.disconnect()

    def test_get_cached_db_object_via_get(self):
        """
        Test retrieving a database from the client database cache
        """
        dbname = self.dbname()
        try:
            self.client.connect()
            # Default returns None
            self.assertIsNone(self.client.get('no_such_db'))
            # Creates the database remotely and adds it to the
            # client database cache
            db = self.client.create_database(dbname)
            # Locally cached database object is returned
            self.assertEqual(self.client.get(dbname), db)
        finally:
            self.client.delete_database(dbname)
            self.client.disconnect()

    def test_get_remote_db_via_get(self):
        """
        Test retrieving a database from the account
        """
        dbname = self.dbname()
        try:
            self.client.connect()
            # Default returns None
            self.assertIsNone(self.client.get('no_such_db', remote=True))
            # Creates the database remotely and ensure that
            # it is not in the client database local cache
            db = self.client.create_database(dbname)
            del self.client[dbname]
            self.assertIsNone(self.client.get(dbname))
            # Retrieve the database object from the server
            self.assertEqual(self.client.get(dbname, remote=True), db)
        finally:
            self.client.delete_database(dbname)
            self.client.disconnect()

    def test_set_non_db_value_via_setitem(self):
        """
        Test raising exception when value is not a database object
        """
        try:
            self.client.connect()
            self.client['not-a-db'] = 'This is not a database object'
            self.fail('Above statement should raise a CloudantException')
        except CloudantException as err:
            self.assertEqual(str(err), 'Value must be set to a Database object')
        finally:
            self.client.disconnect()

    def test_local_set_db_value_via_setitem(self):
        """
        Test setting a database object to the local database cache
        """
        try:
            self.client.connect()
            db = self.client._DATABASE_CLASS(self.client, 'local-not-on-server')
            # Value is set in the local database cache but not on the server
            self.client['local-not-on-server'] = db
            self.assertEqual(self.client.get('local-not-on-server'), db)
            self.assertFalse(db.exists())
        finally:
            self.client.disconnect()

    def test_create_db_via_setitem(self):
        """
        Test creating a database remotely using __setitem__
        """
        dbname = self.dbname()
        try:
            self.client.connect()
            db = self.client._DATABASE_CLASS(self.client, dbname)
            self.client.__setitem__(dbname, db, remote=True)
            self.assertTrue(db.exists())
        finally:
            self.client.delete_database(dbname)
            self.client.disconnect()

@unittest.skipUnless(
    os.environ.get('RUN_CLOUDANT_TESTS') is not None,
    'Skipping Cloudant Account specific tests'
    )
class CloudantAccountTests(UnitTestDbBase):
    """
    Cloudant specific Account unit tests
    """

    def test_constructor_with_account(self):
        """
        Test instantiating an account object using an account name
        """
        # Ensure that the client is new
        del self.client
        self.client = Cloudant(self.user, self.pwd, account=self.account)
        self.assertEqual(
            self.client.cloudant_url,
            'https://{0}.cloudant.com'.format(self.account)
            )

    def test_connect_headers(self):
        """
        Test that the appropriate request headers are set
        """
        try:
            self.client.connect()
            self.assertEqual(
                self.client.r_session.headers['X-Cloudant-User'],
                self.account
                )
            agent = self.client.r_session.headers.get('User-Agent')
            self.assertTrue(agent.startswith('python-cloudant'))
        finally:
            self.client.disconnect()

    def test_billing_data(self):
        """
        Test the retrieval of billing data
        """
        try:
            self.client.connect()
            expected = [
                'data_volume',
                'total',
                'start',
                'end',
                'http_heavy',
                'http_light'
                ]
            # Test using year and month
            year = datetime.now().year
            month = datetime.now().month
            data = self.client.bill(year, month)
            self.assertTrue(all(x in expected for x in data.keys()))
            #Test without year and month arguments
            del data
            data = self.client.bill()
            self.assertTrue(all(x in expected for x in data.keys()))
        finally:
            self.client.disconnect()

    def test_volume_usage_data(self):
        """
        Test the retrieval of volume usage data
        """
        try:
            self.client.connect()
            expected = [
                'data_vol',
                'granularity',
                'start',
                'end'
                ]
            # Test using year and month
            year = datetime.now().year
            month = datetime.now().month
            data = self.client.volume_usage(year, month)
            self.assertTrue(all(x in expected for x in data.keys()))
            #Test without year and month arguments
            del data
            data = self.client.volume_usage()
            self.assertTrue(all(x in expected for x in data.keys()))
        finally:
            self.client.disconnect()

    def test_requests_usage_data(self):
        """
        Test the retrieval of requests usage data
        """
        try:
            self.client.connect()
            expected = [
                'requests',
                'granularity',
                'start',
                'end'
                ]
            # Test using year and month
            year = datetime.now().year
            month = datetime.now().month
            data = self.client.requests_usage(year, month)
            self.assertTrue(all(x in expected for x in data.keys()))
            #Test without year and month arguments
            del data
            data = self.client.requests_usage()
            self.assertTrue(all(x in expected for x in data.keys()))
        finally:
            self.client.disconnect()

    def test_shared_databases(self):
        """
        Test the retrieval of shared database list
        """
        try:
            self.client.connect()
            self.assertIsInstance(self.client.shared_databases(), list)
        finally:
            self.client.disconnect()

    def test_generate_api_key(self):
        """
        Test the generation of an API key for this account
        """
        try:
            self.client.connect()
            expected = ['key', 'password', 'ok']
            api_key = self.client.generate_api_key()
            self.assertTrue(all(x in expected for x in api_key.keys()))
            self.assertTrue(api_key['ok'])
        finally:
            self.client.disconnect()

    def test_cors_configuration(self):
        """
        Test the retrieval of the current CORS configuration for this account
        """
        try:
            self.client.connect()
            expected = ['allow_credentials', 'enable_cors', 'origins']
            cors = self.client.cors_configuration()
            self.assertTrue(all(x in expected for x in cors.keys()))
        finally:
            self.client.disconnect()

    def test_cors_origins(self):
        """
        Test the retrieval of the CORS origins list
        """
        try:
            self.client.connect()
            origins = self.client.cors_origins()
            self.assertIsInstance(origins, list)
        finally:
            self.client.disconnect()

    def test_disable_cors(self):
        """
        Test disabling CORS (assuming CORS is enabled)
        """
        try:
            self.client.connect()
            # Save original CORS settings
            save = self.client.cors_configuration()
            # Test CORS disable
            self.assertEqual(self.client.disable_cors(), {'ok': True})
            # Restore original CORS settings
            self.client.update_cors_configuration(
                save['enable_cors'],
                save['allow_credentials'],
                save['origins'],
                True
                )
        finally:
            self.client.disconnect()

    def test_update_cors_configuration(self):
        """
        Test updating CORS configuration
        """
        try:
            self.client.connect()
            # Save original CORS settings
            save = self.client.cors_configuration()
            # Test updating CORS settings, overwriting origins
            result = self.client.update_cors_configuration(
                True,
                True,
                ['https://ibm.com'],
                True)
            self.assertEqual(result, {'ok': True})
            updated_cors = self.client.cors_configuration()
            self.assertTrue(updated_cors['enable_cors'])
            self.assertTrue(updated_cors['allow_credentials'])
            expected = ['https://ibm.com']
            self.assertTrue(all(x in expected for x in updated_cors['origins']))
            # Test updating CORS settings, adding to origins
            result = self.client.update_cors_configuration(
                True,
                True,
                ['https://ibm.cloudant.com']
                )
            self.assertEqual(result, {'ok': True})
            del updated_cors
            updated_cors = self.client.cors_configuration()
            self.assertTrue(updated_cors['enable_cors'])
            self.assertTrue(updated_cors['allow_credentials'])
            expected.append('https://ibm.cloudant.com')
            self.assertTrue(all(x in expected for x in updated_cors['origins']))
            # Restore original CORS settings
            self.client.update_cors_configuration(
                save['enable_cors'],
                save['allow_credentials'],
                save['origins'],
                True
                )
        finally:
            self.client.disconnect()

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


#!/usr/bin/python3
import sys, getopt
from csv_libPlot_Profile import *

def main(argv) :

    # PATHS
    ###########

    HOME = "/Users/Arthur/"
    PATH = HOME + "/Documents/UPMC/These/Codes/multilayerSingle/example/Well-Balance/Asymptotic/Womersley/"

    # FILE :
    ###########
    dataName_Profile = "Artery_0_x_r_Ux.csv"
    dataName_Properties = "Artery_0_x_R0_A0_K_R_RmR0_A_Q_U_U0_P_gradP_E_Tw_Cf_a.csv"

    nfig = 1

    Womstr = "20"
    Kstr = "1.e4"
    for dRstr in ["1e-3","1e-2","1e-1","3e-1"] :

        if (dRstr == "1e-3") :
            dR_p = r"$10^{-3}$"
        if (dRstr == "1e-2") :
            dR_p = r"$10^{-2}$"
        if (dRstr == "1e-1") :
            dR_p = r"$10^{-1}$"
        if (dRstr == "3e-1") :
            dR_p = r"$3 \times 10^{-1}$"

        #Choose number of layers & Choose number of cells
        J = "1600"
        La = "128"

        ProFile  = PATH + "T_12/a=" + str(Womstr) + "/K=" + str(Kstr) + "/dR=" + str(dRstr) + "/L=" + str(La) + "/Raf=0/J=" +str(J) + "/" + "HRQ/Order=1/KIN_HAT/Figures/" + dataName_Profile
        PropFile = PATH + "T_12/a=" + str(Womstr) + "/K=" + str(Kstr) + "/dR=" + str(dRstr) + "/L=" + str(La) + "/Raf=0/J=" +str(J) + "/" + "HRQ/Order=1/KIN_HAT/Figures/" + dataName_Properties

        # PLOTING :
        ###########
        Store = PATH + "T_12/a=" + str(Womstr) + "/Figures/"

        it = 0 ; ix = 1 ; iR0 = 2 ; iA0 = 3 ; iK = 4 ;
        iR = 5 ; iRmR0 = 6 ; iA = 7 ;
        iQ = 8 ; iU = 9 ; iU0 = 10 ;
        iP = 11 ; igradxP = 12 ; iE = 13 ;
        iTw = 14 ; iCf = 15 ; ia = 16 ;

        lPropX = [it,ix,iR0,iR]

        xScale = 200.
        yScale = 1.
        cbScale = 1.

        lTextPos = [[0.5,0.04],[0.15,0.915],[0.43,0.915],[0.97,0.905]]
        lText = [r"$N_x$="+str(J)+r", $N_r$="+str(La),r"$\alpha$="+str(Womstr),r"$\hat{R}$="+str(dR_p),r"$t$=$0.3 T_c+11T_c$"]
        lTextAlign = ["center","center","center","right"]
        lTextColor = ["black","black","black","black"]

        nArrow = 100;
        scArrow = 1.6 ;
        Colormap = 'BlueRed'
        Umid = 0.
        liY =   [1,2,3,4,5,6,7,8,9,10,11]

        title = "Ux_Arrow_a_" + str(Womstr) + "_dR_" + str(dRstr) + ".pdf"

        xLabel=r"$x \times L^{-1}$"
        yLabel=r"$r \times R^{-1}$"
        cbLabel=r"$u_x$ $\left[\frac{cm}{s}\right]$"

        nfig = plot_csv_Streamlines_Ux_adim(
                pathStore=Store,title=title,PropFile=PropFile,ProFile=ProFile,
                lPropX=lPropX,liY=liY,
                xLabel = xLabel, yLabel = yLabel, cbLabel = cbLabel,
                xScale = xScale, yScale = yScale, cbScale = cbScale,
                nArrow=nArrow,scArrow=scArrow,
                Colormap=Colormap,Umid=Umid,
                lText=lText,lTextPos=lTextPos,lTextAlign=lTextAlign,lTextColor=lTextColor,
                nf=nfig) ;

if __name__ == "__main__":
   main(sys.argv[1:])


# Copyright (c) 2021, NVIDIA CORPORATION & AFFILIATES.  All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

import pynini
from nemo_text_processing.text_normalization.en.graph_utils import (
    NEMO_WHITE_SPACE,
    GraphFst,
    delete_extra_space,
    delete_space,
    generator_main,
)
from nemo_text_processing.text_normalization.en.taggers.punctuation import PunctuationFst
from nemo_text_processing.text_normalization.en.taggers.whitelist import WhiteListFst
from nemo_text_processing.text_normalization.en.taggers.word import WordFst
from pynini.lib import pynutil

from nemo.utils import logging

class ClassifyFst(GraphFst):
    """
    Final class that composes all other classification grammars. This class can process an entire sentence including punctuation.
    For deployment, this grammar will be compiled and exported to OpenFst Finate State Archiv (FAR) File.
    More details to deployment at NeMo/tools/text_processing_deployment.

    Args:
        input_case: accepting either "lower_cased" or "cased" input.
        deterministic: if True will provide a single transduction option,
            for False multiple options (used for audio-based normalization)
        cache_dir: path to a dir with .far grammar file. Set to None to avoid using cache.
        overwrite_cache: set to True to overwrite .far files
        whitelist: path to a file with whitelist replacements
    """

    def __init__(
        self,
        input_case: str,
        cache_dir: str = None,
        overwrite_cache: bool = False,
        deterministic: bool = True,
        whitelist: str = None,
    ):
        super().__init__(name="tokenize_and_classify", kind="classify", deterministic=deterministic)

        far_file = None
        if cache_dir is not None and cache_dir != "None":
            os.makedirs(cache_dir, exist_ok=True)
            whitelist_file = os.path.basename(whitelist) if whitelist else ""
            far_file = os.path.join(
                cache_dir, f"_{input_case}_en_tn_{deterministic}_deterministic{whitelist_file}.far"
            )
        if not overwrite_cache and far_file and os.path.exists(far_file):
            self.fst = pynini.Far(far_file, mode="r")["tokenize_and_classify"]
            logging.info(f'ClassifyFst.fst was restored from {far_file}.')
        else:
            logging.info(f"Creating ClassifyFst grammars.")

            punctuation = PunctuationFst(deterministic=deterministic)
            punct_graph = punctuation.fst
            word_graph = WordFst(deterministic=deterministic, punctuation=punctuation).fst
            whitelist_graph = WhiteListFst(input_case=input_case, deterministic=deterministic).fst

            classify = pynutil.add_weight(whitelist_graph, 1) | pynutil.add_weight(word_graph, 100)

            punct = pynutil.insert("tokens { ") + pynutil.add_weight(punct_graph, weight=2.1) + pynutil.insert(" }")
            punct = pynini.closure(
                pynini.compose(pynini.closure(NEMO_WHITE_SPACE, 1), delete_extra_space)
                | (pynutil.insert(" ") + punct),
                1,
            )
            token = pynutil.insert("tokens { ") + classify + pynutil.insert(" }")
            token_plus_punct = (
                pynini.closure(punct + pynutil.insert(" ")) + token + pynini.closure(pynutil.insert(" ") + punct)
            )

            graph = (
                token_plus_punct
                + pynini.closure(
                    (
                        pynini.compose(pynini.closure(NEMO_WHITE_SPACE, 1), delete_extra_space)
                        | (pynutil.insert(" ") + punct + pynutil.insert(" "))
                    )
                    + token_plus_punct
                ).optimize()
            )

            graph = delete_space + graph + delete_space
            graph |= punct

            self.fst = graph.optimize()

            if far_file:
                generator_main(far_file, {"tokenize_and_classify": self.fst})
                logging.info(f"ClassifyFst grammars are saved to {far_file}.")


from PyQt5.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton, QSizePolicy, QGridLayout
from PyQt5.QtCore import QRect, pyqtSlot, pyqtSignal
from QtImageViewer import QtImageViewer
import os
from functools import partial

# This widget is responsible for rendering the page where waypoints can be loaded from save files.
class PrevFilesWidget(QWidget):
	changeWidgetSignal = pyqtSignal(int)
	chosenSaveFileSignal = pyqtSignal(str)
	selectTifSignal = pyqtSignal(str,str)
	def __init__(self):
		super().__init__()
		self.savespath = os.fspath('../../saves')
		self.initUI()

	def initUI(self):
		self.l = QLabel("PrevFilesWidget")
		self.btn = QPushButton("< Back",self)

		self.qbly = QVBoxLayout()
		self.qbly.addWidget(self.btn,1)
		self.btn.clicked.connect(self.on_back_clicked)

		self.itemvlist = QVBoxLayout()
		self.createLoadButtons()
		self.setLayout(self.itemvlist)

	def createLoadButtons(self):
		self.tiflist = {}

		# Go through the saves folder and make a button for each one for loading
		# the tif along with its saved waypoints
		if os.path.exists(self.savespath):
			dirlist = os.listdir( self.savespath )
			for item in dirlist:
				if item[0] != '.':
					if item[-4:] == ".txt":
						itemhlist = QHBoxLayout()
						itembtn = QPushButton(item[:-4],self)
						fline = None
						with open( os.fspath(self.savespath + "/" + item) ) as f:
							fline = f.readline().rstrip()
						itembtn.clicked.connect( partial( self.item_button_clicked, fline ) )
						itemhlist.addWidget(itembtn)
						self.tiflist[item] = itembtn
						self.itemvlist.addLayout(itemhlist)

	def getLocations(self, save_name):
		coords = []
		save_path = self.savespath + '/' + save_name + ".txt"
		try:
			with open( os.fspath(save_path) ) as save:
				lines = [ line.rstrip('\n') for line in save ]
				fline = lines[0]
				for i in range(1, len(lines)):
					line = lines[i]
					comma = line.find(',')
					x = line[:comma]
					y = line[comma+1:]
					coords.append( (x, y) )
		except (OSError, IOError, FileNotFoundError) as e:
			return [ "error" ]
		return coords

	@pyqtSlot(str)
	def item_button_clicked(self,itemName):
		self.selectTifSignal.emit(itemName, 'prevfiles')

	# When the back button is clicked, go back to the MainWidget
	@pyqtSlot()
	def on_back_clicked(self):
		self.changeWidgetSignal.emit(0)

	# When user picks a savefile, its path/save filename will be set as a local and passed with this to the overlay.
	@pyqtSlot()
	def on_savefile_chosen(self):
		self.selectTifSignal.emit(self.saveChoice, "prevfiles")


##############################################################################
#
# Copyright (c) 2003-2020 by The University of Queensland
# http://www.uq.edu.au
#
# Primary Business: Queensland, Australia
# Licensed under the Apache License, version 2.0
# http://www.apache.org/licenses/LICENSE-2.0
#
# Development until 2012 by Earth Systems Science Computational Center (ESSCC)
# Development 2012-2013 by School of Earth Sciences
# Development from 2014 by Centre for Geoscience Computing (GeoComp)
#
##############################################################################

from __future__ import print_function, division

__copyright__="""Copyright (c) 2003-2020 by The University of Queensland
http://www.uq.edu.au
Primary Business: Queensland, Australia"""
__license__="""Licensed under the Open Software License version 3.0
http://www.opensource.org/licenses/osl-3.0.php"""
__url__="https://launchpad.net/escript-finley"

"""
Test suite for PDE solvers on finley
"""

from test_simplesolve import ComplexSolveTestCase, ComplexSolveTestCaseOrder2
import esys.escriptcore.utestselect as unittest
from esys.escriptcore.testing import *

from esys.escript import Data, Solution, Vector, hasFeature
from esys.finley import Rectangle, Brick
from esys.escript.linearPDEs import SolverOptions

HAVE_MUMPS = hasFeature('mumps')
skip_amg = True
skip_muelu_long = False #hasFeature("longindex")

# number of elements in the spatial directions
NE0=12
NE1=12
NE2=8
OPTIMIZE=True

@unittest.skipIf(not HAVE_MUMPS, "MUMPS not available")
class ComplexSolveOnMumps(ComplexSolveTestCase):
    pass
@unittest.skipIf(not HAVE_MUMPS, "MUMPS not available")
class ComplexSolveOnMumpsOrder2(ComplexSolveTestCaseOrder2):
    pass

## direct
class Test_ComplexSolveFinleyRect_Order1_Mumps_Direct(ComplexSolveOnMumps):
    def setUp(self):
        self.domain = Rectangle(NE0, NE1, 1, optimize=OPTIMIZE)
        self.package = SolverOptions.MUMPS
        self.method = SolverOptions.DIRECT

    def tearDown(self):
        del self.domain

## direct
class Test_ComplexSolveFinleyRect_Order2_Mumps_Direct(ComplexSolveOnMumpsOrder2):
    def setUp(self):
        self.domain = Rectangle(NE0, NE1, 2, optimize=OPTIMIZE)
        self.package = SolverOptions.MUMPS
        self.method = SolverOptions.DIRECT

    def tearDown(self):
        del self.domain

## direct
class Test_ComplexSolveFinleyBrick_Order1_Mumps_Direct(ComplexSolveOnMumps):
    def setUp(self):
        self.domain = Brick(NE0, NE1, NE2, 1, optimize=OPTIMIZE)
        self.package = SolverOptions.MUMPS
        self.method = SolverOptions.DIRECT

    def tearDown(self):
        del self.domain

## direct
class Test_ComplexSolveFinleyBrick_Order2_Mumps_Direct(ComplexSolveOnMumpsOrder2):
    def setUp(self):
        self.domain = Brick(NE0, NE1, NE2, 2, optimize=OPTIMIZE)
        self.package = SolverOptions.MUMPS
        self.method = SolverOptions.DIRECT

    def tearDown(self):
        del self.domain

if __name__ == '__main__':
    run_tests(__name__, exit_on_failure=True)


import sys
import pickle
with open(sys.argv[1], 'rb') as f:
	x = pickle.load(f)
	preds = x['test']['preds']

from bs4 import BeautifulSoup
import glob
import collections
import shutil
import os

dev_files = sorted(glob.glob('/home/devanshg27/semtabfact/xml/test_a/*.xml'))

SUBMISSION_DIR = './submit'

if os.path.exists(SUBMISSION_DIR) and os.path.isdir(SUBMISSION_DIR):
    shutil.rmtree(SUBMISSION_DIR)

os.makedirs(SUBMISSION_DIR)

CONST_DICT = {
	'refuted': 0,
	'entailed': 1,
	'unknown': 2,
}

REV_CONST_DICT = {
	0: 'refuted',
	1: 'entailed',
	2: 'unknown',
}

idx = 0
for name in dev_files:
	with open(name) as f:
		soup = BeautifulSoup(f, 'xml')
		statements = soup.find_all("statement")

		for statement in statements:
			statement['type'] = REV_CONST_DICT[int(preds[idx])]
			idx += 1

		f2 = open(SUBMISSION_DIR + '/' + os.path.basename(name), "w")
		f2.write(soup.prettify())
		f2.close()

assert(idx == len(preds))

shutil.make_archive('submission', 'zip', SUBMISSION_DIR)


"""
1. Проанализировать скорость и сложность одного любого алгоритма,
разработанных в рамках домашнего задания первых трех уроков.
Примечание: попробуйте написать несколько реализаций алгоритма и сравнить их.

Подсказка:
1) возьмите 2-3 задачи, реализованные ранее, сделайте замеры на разных входных данных
2) сделайте для каждой из задач оптимизацию (придумайте что можно оптимизировать)
и также выполните замеры на уже оптимизированных алгоритмах
3) опишите результаты - где, что эффективнее и почему.

ВНИМАНИЕ: ЗАДАНИЯ, В КОТОРЫХ БУДУТ ГОЛЫЕ ЦИФРЫ ЗАМЕРОВ (БЕЗ АНАЛИТИКИ)
БУДУТ ПРИНИМАТЬСЯ С ОЦЕНКОЙ УДОВЛЕТВОРИТЕЛЬНО
"""

import timeit
import cProfile
import time

'''
Анализ скорости и сложности алгоритма определения сколько из диапазона натуральных
числе [2, 100000) кратны каждому из чисел в диапазоне [2, 10)
'''

'''
Реализация №1. Вложенный цикл. Здесь имеет место квадратичная сложность # O(n^2).
'''

print(f'\nЗамер выполнения кода через вложенный цикл')
print('Метод <<timeit>>')

STR_CODE_1 = '''
from_numb = 2
to_numb = 10
from_range = 2
to_range = 100000

for i in range(from_numb, to_numb):
    flag = 0
    for j in range(from_range, to_range):
        if j % i == 0:
            flag += 1
'''

LAUNCH_LST = [1, 10, 100, 1000]  # массив запусков программы
for i in LAUNCH_LST:
    print(f'Число запусков кода = {i}. ', end='')
    print(f'Время выполнения операции = {timeit.timeit(STR_CODE_1, number=i)}')

'''
Реализация №2. Функция.
'''

print(f'\nЗамер выполнения кода через функцию')
print('Метод <<timeit>>')

def cycle_method(items_1, items_2, output_str=''):
    '''Цикл'''
    for elem_1 in items_1:
        flag = 0
        for elem_2 in items_2:
            if elem_2 % elem_1 == 0:
                flag += 1
        output_str += f'В диапазоне {items_2[0:1]} - {items_2[-1:]}: {flag} чисел кратны \'{i}\'\n'
    return output_str

for i in LAUNCH_LST:
    print(f'Число запусков кода = {i}. Время выполнения операции = ', end=' ')
    print(
        timeit.timeit(
            'cycle_method(list(range(2, 10)), list(range(2, 100000)))',
            setup='from __main__ import cycle_method',
            number=i))

print('\nМетод <<cProfile>>')
cProfile.run('cycle_method(list(range(2, 10)), list(range(2, 100000)))')

'''
На данном примере просто посмотрел, как работают модули timeit и cProfile
Рассмотрел два варианта кода:
- решение через вложенный цикл. Здесь имеет место квадратичная сложность O(n^2)
- решение через функцию. Функция аналогична фрагменту кода через вложенный цикл

Метод timeit и cProfile показали эквивалентное время выполнения кода.
С ростом числа запусков кода время выполнения растёт пропорционально,
что и логично (никакой оптимизации кода нет)
'''

'''
Реализация подсчёта времени выполнения программы вручную (просто, как пример)
'''

START = time.time()

for i in range(2, 10):
    FLAG = 0
    for j in range(2, 100000):
        if j % i == 0:
            FLAG += 1
print(f'\nВремя выполнения программы (ручной замер): {time.time() - START} ')

'''
Результаты вычислений

Замер выполнения кода через вложенный цикл
Метод <<timeit>>
Число запусков кода = 1. Время выполнения операции = 0.223464768
Число запусков кода = 10. Время выполнения операции = 2.192130005
Число запусков кода = 100. Время выполнения операции = 20.938258309
Число запусков кода = 1000. Время выполнения операции = 189.561709845

Замер выполнения кода через функцию
Метод <<timeit>>
Число запусков кода = 1. Время выполнения операции =  0.14114100699998744
Число запусков кода = 10. Время выполнения операции =  1.8089432919999808
Число запусков кода = 100. Время выполнения операции =  17.963720346000002
Число запусков кода = 1000. Время выполнения операции =  215.73111694

Метод <<cProfile>>
         4 function calls in 0.169 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.006    0.006    0.169    0.169 <string>:1(<module>)
        1    0.164    0.164    0.164    0.164 task_1.py:58(cycle_method)
        1    0.000    0.000    0.169    0.169 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

Время выполнения программы (ручной замер): 0.3460197448730469
'''

Урок 4. Практическое задание/task_1.py


from __future__ import print_function
import os
from bleualign.align import Aligner

if __name__ == '__main__':
	current_path = os.path.dirname(os.path.abspath(__file__))
	options = {
		# source and target files needed by Aligner
		# they can be filenames, arrays of strings or io objects.
		'srcfile':os.path.join(current_path, '..', 'eval', 'eval1989.de'),
		'targetfile': os.path.join(current_path, '..', 'eval', 'eval1989.fr'),
		# translations of srcfile and targetfile, not influenced by 'factored'
		# they can be filenames, arrays of strings or io objects, too.
		'srctotarget': [os.path.join(current_path, '..', 'eval', 'eval1957.europarlfull.fr')],
		'targettosrc': [],
		# passing filenames or io object for them in respectly.
		# if not passing anything or assigning None, they will use StringIO to save results.
		'output-src': None, 'output-target': None,
		# other options ...
		}
	a = Aligner(options)
	a.mainloop()
	output_src, output_target = a.results()
	# output_src, output_target is StringIO because options['output-src'] is None
	src = output_src.getvalue()  # StringIO member function
	trg = output_target.getvalue().splitlines()  # array of string
	print('output_src.getvalue()')
	print(src[:30])
	print()
	print('output_target.getvalue().splitlines()')
	print(trg[:3])


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

import os
import pytest

from giraffez.__main__ import main
from giraffez.core import MainCommand
from giraffez.errors import *
from giraffez.encrypt import *

@pytest.mark.usefixtures('tmpfiles')
class TestCommandLine(object):
    def test_print_help(self, mocker):
        # Should raise a SystemExit and cause help to print
        with pytest.raises(SystemExit):
            main()

    @pytest.mark.usefixtures('config')
    def test_cmd_error(self, mocker, tmpfiles):
        os.remove(tmpfiles.key)
        create_key_file(tmpfiles.key)
        with pytest.raises(TeradataError):
            MainCommand().run(test_args=["cmd", "select * from dbc.dbcinfo", "--conf", tmpfiles.conf, "--key", tmpfiles.key])

    def test_config_not_found(self, mocker, tmpfiles):
        mock_prompt = mocker.patch('giraffez.core.prompt_bool')
        mock_prompt.return_value = False
        mock_prompt.side_effect = ConfigNotFound("Did it!")
        with pytest.raises(ConfigNotFound):
            MainCommand().run(test_args=["cmd", "select * from dbc.dbcinfo", "--conf", tmpfiles.noconf])


input_data = ['89*1***7*',
              '527*4*8*1',
              '16*7*8***',
              '6**473*2*',
              '249*8*7*3',
              '738*****5',
              '38269***7',
              '916**7284',
              '475812396']

class Table:
    digits = set(list('123456789'))
    rows_sets = [set() for _ in range(9)]
    columns_sets = [set() for _ in range(9)]
    squares_sets = [set() for _ in range(9)]
    data = [[None for _ in range(9)] for _ in range(9)]

    def __init__(self, input_data):
        init_data = [list(row) for row in input_data]
        for x in range(9):
            for y in range(9):
                cell = (x, y)
                value = init_data[x][y]
                if value not in self.digits:
                    continue
                self.add_value(cell, value)

    def row_set(self, cell):
        x, y = cell
        row_number = x
        return self.rows_sets[row_number]

    @staticmethod
    def row_neighbors(cell):
        x, y = cell
        for i in range(9):
            yield (x, i)

    def column_set(self, cell):
        x, y = cell
        column_number = y
        return self.columns_sets[column_number]

    @staticmethod
    def column_neighbors(cell):
        x, y = cell
        for i in range(9):
            yield (i, y)

    def square_set(self, cell):
        x, y = cell
        square_number = (x//3)*3 + y//3
        return self.squares_sets[square_number]

    @staticmethod
    def square_neighbors(cell):
        x, y = cell
        for i in range(x - x % 3, x+3 - x % 3):
            for j in range(y - y % 3, y+3 - y % 3):
                yield (i, j)

    def neighbors(self, cell):
        for neighbor in self.row_neighbors(cell):
            yield neighbor
        for neighbor in self.column_neighbors(cell):
            yield neighbor
        for neighbor in self.square_neighbors(cell):
            yield neighbor

    def add_value(self, cell, value):
        x, y = cell
        self.data[x][y] = value
        self.row_set(cell).add(value)
        self.column_set(cell).add(value)
        self.square_set(cell).add(value)
        for neighbor in self.neighbors(cell):
            neighbor_values = self.possible_values(neighbor)
            nx, ny = neighbor
            if len(neighbor_values) == 1 and self.data[nx][ny] not in self.digits :
                value = list(neighbor_values)[0]
                self.add_value(neighbor, value)

    def possible_values(self, cell):
        return self.digits \
               - self.row_set(cell) \
               - self.square_set(cell) \
               - self.column_set(cell)

    def show_data(self):
        for row in self.data:
            print(*row, sep=' ')

table = Table(input_data)
table.show_data()


#!/usr/bin/python
from __future__ import absolute_import, print_function
import time
import optparse
from linkfetcher import Linkfetcher
from webcrawler import Webcrawler

def option_parser():
    """Option Parser to give various options."""
    usage = '''
             $ ./crawler -d5 <url>
                Here in this case it goes till depth of 5 and url is target URL to
                start crawling.
            '''
    version = '0.0.1'

    parser = optparse.OptionParser(usage=usage, version=version)

    parser.add_option("-l", "--links", action="store_true",
                      default=False, dest="links", help="links for target url")

    parser.add_option("-d", "--depth", action="store", type="int",
                      default=30, dest="depth", help="Maximum depth traverse")
    opts, args = parser.parse_args()

    if len(args) < 1:
        parser.print_help()
        raise SystemExit(1)

    return opts, args

def getlinks(url):
    """Get Links from the Linkfetcher class."""
    page = Linkfetcher(url)
    page.linkfetch()
    for i, url in enumerate(page):
        print("%d ==> %s" % (i, url))

def main():
    """ Main class."""
    opts, args = option_parser()
    url = args[0]

    if opts.links:
        getlinks(url)
        raise SystemExit(0)

    depth = opts.depth

    sTime = time.time()

    print("CRAWLER STARTED:")
    print("%s, will crawl upto depth %d" % (url, depth))
    print("===============================================================")
    webcrawler = Webcrawler(url, depth)
    webcrawler.crawl()
    print("\n".join(webcrawler.urls))

    eTime = time.time()
    tTime = eTime - sTime
    print("\n")
    print("Crawler Statistics")
    print("==================")
    print("No of links Found: %d" % webcrawler.links)
    print("No of follwed:     %d" % webcrawler.followed)
    print("Time Stats : Found all links  after %0.2fs" % tTime)

if __name__ == "__main__":
    main()


import boto3
import botocore

import json
import urllib2
import re

#Sample event for local testing. Remember to COMMENT OUT before committing
event = {"Records": [{"eventID": "e0993e3697aa2984eba30dfc57f1731c", "eventVersion": "1.0", "dynamodb": {"Keys": {"FeedName": {"S": "nvdcve-2.0-Modified"}}, "SizeBytes": 27, "StreamViewType": "KEYS_ONLY", "SequenceNumber": "3249000000000000791629016"}, "awsRegion": "us-east-1", "eventName": "MODIFY", "eventSourceARN": "arn:aws:dynamodb:us-east-1:459772930601:table/DEV_vulnscape_nvd_feed_list/stream/2015-12-02T14:15:49.196", "eventSource": "aws:dynamodb"}]}

'''Determine the context in which this function is operating - DEV, BETA, or PROD'''
def determine_op_env(event):
    #Determine and capture context
    if event["Records"][0]["eventSource"] == "aws:dynamodb":
        result = re.search("arn:aws:dynamodb:us-.*:[0-9]*:table/([A-Z]{1,10})_.*", event["Records"][0]["eventSourceARN"])
        return result.group(1)

    else:
        print("Unexpected error.")
        raise SystemExit

def check_bucket(s3, bucket_name, logger):
    try:
        bucket_status = s3.Bucket(bucket_name).creation_date
        print(bucket_status)
        return "ACTIVE"
    except botocore.exceptions.ClientError as e:
        if e.response['Error']['Code'] == 'NotFound':
            logger.warning("S3 Bucket does not exist.")
            return "CREATE"
        else:
            logger.critical("Unexpected error: %s" % e)
            raise SystemExit

def create_s3_bucket(op_env):

    bucket = s3.create_bucket(
    ACL='private',
    Bucket='DEV_NvdFeedGzips'
    )

'''The main function, with optional variables'''
def main(event=None, context=None):

    #Determine event operating environment (e.g., DEV, PROD, BETA)
    op_env = determine_op_env(event)
    #print(op_env)

    #Enable logging, with context
    logger = logging_handler(op_env)

    #Write status update to log
    logger.info("Main function loading.")

    #Connect to S3
    s3 = boto3.resource('s3')

    #Establish appropriate name for Bucket
    bucket_name = "%s_NvdFeedGzips" % op_env

    #Check status of DynamoDB table
    bucket_situation = check_bucket(s3, bucket_name, logger)
    print(bucket_situation)

    #React appropriately to status of DynamoDB table, i.e., to continue or to create a new table
    if bucket_situation == "ACTIVE":
        logger.info("S3 Bucket exists and is ACTIVE.")
        pass
    elif bucket_situation == "CREATE":
        logger.info("Creating new S3 Bucket")
        #create_table(dynamodb, table_name)
    else:
        logger.critical("Bucket status is indeterminate.")

    #create_s3_bucket(op_env)

# Main Program
if __name__ == "__main__":
	main(event)


#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""RBogus pipeline global settings

"""

import logging
import os
import numpy as np

DEBUG_PROCESS = True

LOG_LEVEL = logging.DEBUG

LOG_FORMAT = "[RBogus-%(levelname)s @ %(asctime)-15s] %(message)s"

CONNECTION = 'sqlite:///realO2rbogus-dev.db'

LOADER = "rbogus.load.Load"

PIPELINE_SETUP = "rbogus.pipeline.RBogus"

STEPS = ["rbogus.steps.StepCrossMatch",
         "rbogus.steps.StepSCrossMatch",
         "rbogus.steps.StepSCorrCrossMatch",
         "rbogus.steps.StepCrossMatchOIS",
         "rbogus.steps.StepCrossMatchHOT"]

ALERTS = []

# This values are autoimported when you open the shell
SHELL_LOCALS = {
    "np": np
}

try:
    from .local_settings import *  # noqa
    # print "Local settings file found"
except ImportError:
    print("Local settings not found, using settings.py defaults")
    #~ WORK_DIR = os.path.join(os.path.dirname(__file__), "data")
    #~ PAWPRINT_PATH = os.path.join(WORK_DIR, "inputdata")
    #~ DATA_PATH = os.path.join(WORK_DIR, "outputdata")


#
#    ICRAR - International Centre for Radio Astronomy Research
#    (c) UWA - The University of Western Australia, 2018
#    Copyright by UWA (in the framework of the ICRAR)
#
#    (c) Copyright 2018 CSIRO
#    Australia Telescope National Facility (ATNF)
#    Commonwealth Scientific and Industrial Research Organisation (CSIRO)
#    PO Box 76, Epping NSW 1710, Australia
#    atnf-enquiries@csiro.au
#
#    This library is free software; you can redistribute it and/or
#    modify it under the terms of the GNU Lesser General Public
#    License as published by the Free Software Foundation; either
#    version 2.1 of the License, or (at your option) any later version.
#
#    This library is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public
#    License along with this library; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
#    MA 02111-1307  USA

import dlg
import os
import subprocess
from mpi4py import MPI

import six.moves.http_client as httplib

iperf_bin = '/home/wu082/downloads/iperf-2.0.11/src/iperf'

if __name__ == '__main__':
    comm = MPI.COMM_WORLD
    rank = comm.Get_rank()
    ip = dlg.utils.get_local_ip_addr()[1][0] # use infiniband over ip
    port = 6000 + rank
    ipstr = '%s+%d' % (ip, port)
    con = httplib.HTTPConnection('sdp-dfms.ddns.net', 8096)
    con.request('GET', '/reg_receiver?ip=%s' % (ipstr))

    cmd = '%s -s -p %d -w 512K' % (iperf_bin, port)
    # Run and wait until it finishes
    process = subprocess.Popen(cmd.split(),
                               close_fds=True,
                               #stdin=stdin,
                               #stdout=stdout,
                               stderr=subprocess.PIPE,
                               env=os.environ.copy())

    print('Waiting for this to complete: %s' % cmd)
    pstdout, pstderr = process.communicate()
    #if stdout != subprocess.PIPE:
    #    pstdout = b"<piped-out>"
    pcode = process.returncode


def no_space1(x):
    list = []
    for i in x:
        if i != ' ':
            list.append(i)
    return ''.join(list)

def no_space(x):
    return x.replace(' ', '')

print(no_space('8 j 8   mBliB8g  imjB8B8  jl  B'))
print('8j8mBliB8gimjB8B8jlB')


# -*- coding: utf-8 -*-
import itertools
import os.path
import subprocess
import logging
import re

import click
import requests

_REPO_LIST_URL = 'https://api.github.com/{type}s/{account}/repos?page={page_no}'

def repositories(account, type='org', filter=None):
    for page_no in itertools.count(1):
        click.echo('Page {page_no}'.format(page_no=page_no))
        url = _REPO_LIST_URL.format(type=type,
                                    account=account,
                                    page_no=page_no,
                                    filter=filter)
        logging.info('Getting repo list from {}'.format(url))
        resp = requests.get(url)
        resp.raise_for_status()
        if not resp.json():
            raise StopIteration
        for repo in resp.json():
            if not repo['private']:
                if (not filter) or re.search(filter, repo['name']):
                    yield repo

class Repository(object):
    def __init__(self, data):
        self.__dict__.update(data)

    @classmethod
    def synch_all(cls, account, type, filter=None, *args):
        """
        Clones or updates repositories belonging to `account`.
        """
        for repo_data in repositories(account, type, filter):
            repo = cls(repo_data)
            repo.synch(*args)

    @classmethod
    def synch_present(cls, filter=None, *args):
        """
        Updates repositories already present in child directories.
        """
        for dir in os.listdir():
            if os.path.isdir(dir):
                if filter and not re.search(filter, dir):
                    logging.info('{} ignored; filtered by `{}`'.format(dir,
                                                                       filter))
                else:
                    logging.info('{} exists; pulling changes'.format(dir))
                    subprocess.run(['git', 'pull'], cwd=dir)

    def synch(self, *args):
        if os.path.exists(self.name):
            logging.info('{} exists; pulling changes'.format(self.name))
            subprocess.run(['git', 'pull'], cwd=self.name)
        else:
            logging.info('Cloning '.format(self.name))
            subprocess.run(['git', 'clone', *args, self.clone_url])


#Thomas Thorpe
#16/10/14
#Iteration Strech and Challenge Exercise 12

denary = 256
binary = ""

while denary > 255 or denary < 0:
    denary = int(input("Enter a whole number between 0 and 255: "))

for count in range(7,-1,-1):
    if denary >= 2 ** (count):
        binary = binary + "{0:<4} \t".format(1)
        denary = denary - 2 ** (count)
    else:
        binary = binary + "{0:<4} \t".format(0)
print (binary)


from xai.brain.wordbase.nouns._avail import _AVAIL

#calss header
class _AVAILING(_AVAIL, ):
	def __init__(self,):
		_AVAIL.__init__(self)
		self.name = "AVAILING"
		self.specie = 'nouns'
		self.basic = "avail"
		self.jsondata = {}


from . import ScribeModuleBaseClass
from . lib.util import format_url

import re as _re
import sys

class Cpuinfo(ScribeModuleBaseClass):

    def __init__(self, input_dict=None, module_name=None, host_name=None,
                 input_type=None, scribe_uuid=None):
        ScribeModuleBaseClass.__init__(self, module_name=module_name,
                                       input_dict=input_dict,
                                       host_name=host_name,
                                       input_type=input_type,
                                       scribe_uuid=scribe_uuid)
        if input_dict:
            self.value = self._parse(input_dict)

    def __iter__(self):
        for attr, value in self.__dict__.items():
            yield attr, value

    def _parse(self, cpu_fullinput):
        output_data = {}
        split_lines = cpu_fullinput.get("lscpu").split('\n')
        if len(split_lines) <= 1:
            print("Error occured in processing CPU Info data")
            sys.exit(1)
        for x in range(len(split_lines)):
            # There are cases where the value has leading whitespaces
            # And also removing multiple whitespaces in general
            split_lines[x] = _re.sub(r":\s\s+", ": ", split_lines[x])
            split_lines[x] = _re.sub(r"\s\s+", "", split_lines[x])
            # Flags are a special case in lscpu where we want to
            # convert it to a list. We generate that here and
            # remove the old line
            if "Flags" in split_lines[x]:
                new_flags = split_lines[x].split(' ')
                new_flags.remove('Flags:')
                split_lines.remove(split_lines[x])
        if split_lines[0] != "" and len(split_lines) >= 2:
            # Dealing with keys having whitespaces
            split_lines[0] = split_lines[0].replace(' ', '_')
            if split_lines[0] not in output_data:
                output_data[split_lines[0]] = []
            current_dict = {}
            for s in split_lines[1:]:
                if ': ' in s:
                    key_val = s.split(': ', 1)
                else:
                    continue
                if len(key_val) <= 1:
                    key_val.append("None")
                current_dict[key_val[0].replace(' ', '_')] = key_val[1]
            # We update our dictionary with the created Flags list above
            current_dict.update({'Flags': new_flags})
            output_data[split_lines[0]].append(current_dict)
        return output_data


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

"""
TEXT EXTRACTION FROM IMAGES
"""
import string
import json
import numpy as np
import pytesseract
import cv2 as cv

# Read the image
IMG = cv.imread(r"C:\Users\Internship004\.spyder-py3\orig.jpg", 1)

# Improvise RGB ratio of the image
IMG = cv.cvtColor(IMG, cv.COLOR_BGR2RGB)

def clahe_try(img_rd):
    ''' Return image after applying clahe processing '''
    img_new = cv.cvtColor(img_rd, cv.COLOR_BGR2GRAY)
    clahe = cv.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
    img_new = clahe.apply(img_new)
    return img_new

def unsharp_mask(img_rd, kernel_size=(5, 5), sigma=1.0, amount=1.0, thresh=0):
    '''  Return a sharpened version of the image, using an unsharp mask '''
    blurred = cv.GaussianBlur(img_rd, kernel_size, sigma)
    sharpened = float(amount + 1) * img_rd - float(amount) * blurred
    sharpened = np.maximum(sharpened, np.zeros(sharpened.shape))
    sharpened = np.minimum(sharpened, 255 * np.ones(sharpened.shape))
    sharpened = sharpened.round().astype(np.uint8)
    if thresh > 0:
        low_contrast_mask = np.absolute(img_rd - blurred) < thresh
        np.copyto(sharpened, img_rd, where=low_contrast_mask)
    return sharpened

# Get the width and height of the image
H, W = list(IMG.shape)[0], list(IMG.shape)[1]

# Calculate aspect ratio
ASPECTRATIO = W/H

# Fix a width for passing to tesseract
FIXEDWIDTH = 700

# Resize the read image to the fixed resolution
IMG = cv.resize(IMG, (FIXEDWIDTH, int(FIXEDWIDTH//ASPECTRATIO)))

IMG = unsharp_mask(IMG)
# functon call
IMG = clahe_try(IMG)

# show and save final image
cv.imshow('show', IMG)
cv.imwrite('output.jpg', IMG)

# Pass the image to tesseract and convert it as a dataframe
TEXT = pytesseract.image_to_data(IMG, output_type='data.frame')

# Remove the WORDS with negative CONFidence scores
TEXT = TEXT[TEXT.conf != -1]
LINES = TEXT.groupby('block_num')['text'].apply(list)
CONF = TEXT.groupby(['block_num'])['conf'].mean()

# Remove punctuations from the extracted TEXT and append
# the remaining to a new list
CONFLIST = []
for i in range(len(TEXT['conf'].values)):
    if(TEXT['conf'].values[i] >= 25 and TEXT['text'].values[i] != 'nan' and
       TEXT['text'].values[i] not in string.punctuation and
       TEXT['text'].values[i][0] not in string.whitespace):
        CONFLIST.append(TEXT['text'].values[i])
CONFLIST.append('EOF')

# Pass the image to pytesseract to extract the TEXT from the image
RESULT = pytesseract.image_to_string(IMG)

# Remove white spaces from the extracted TEXT and append the wanted TEXT
NEWLIST = []
EACHLINE = RESULT.split('\n')
for i in EACHLINE:
    NEWSTR = ''
    WORDS = i.split()
    for k in WORDS:
        if k in CONFLIST:
            NEWSTR += k+' '
    NEWLIST.append(NEWSTR[0:-1])

# remove special characters and white spaces from the list
for i in NEWLIST:
    if i in string.whitespace or i in string.punctuation:
        NEWLIST.remove(i)

# Update the dictionary with line number and the line contents
D = {}
k=0
for i in range(len(NEWLIST)):
    if NEWLIST[i] not in string.whitespace:
        k+=1
        D.update({'Line '+str(k): NEWLIST[i]})

# Open a json file and dump the dictionary into it
with open('C:/Users/Internship004/Desktop/newjson.json', 'w') as jsonfile:
    json.dump(D, jsonfile, indent=2)

# Close the json file
jsonfile.close()

# Extract the required field from the json file
CNT = 0
F = open('C:/Users/Internship004/Desktop/newjson.json', 'r', encoding='utf8')
for LINE in F:
    CNT += 1
    if 'Date of Issue' in LINE:
        break
for LINE in F:
    if 'Line '+str(CNT) in LINE:
        print(LINE)
        break

# Close the displayed image after any key is pressed
cv.waitKey()
cv.destroyAllWindows()


from .dto import ChoiceDTO, QuestionDTO, QuizDTO, AnswerDTO, AnswersDTO
from typing import List

class QuizResultService():
    def __init__(self, quiz_dto: QuizDTO, answers_dto: AnswersDTO):
        self.quiz_dto = quiz_dto
        self.answers_dto = answers_dto

    def get_result(self) -> float:
        if self.quiz_dto.uuid == self.answers_dto.quiz_uuid:  # проверка, что ответы для нужного теста
            right_answers = 0
            for answer in self.answers_dto.answers:
                for question in self.quiz_dto.questions:
                    if answer.question_uuid == question.uuid:  # нашли нужный вопрос
                        choosen_variants = answer.choices
                        needed_variants = []
                        for choice in question.choices:#смотрим все его правильные ответы
                            if choice.is_correct and answer.question_uuid + '-' in choice.uuid:
                                needed_variants.append(choice.uuid)
                        if choosen_variants == needed_variants:#наши ответы совпадают с правильными?
                            right_answers += 1
            return right_answers / len(self.quiz_dto.questions)
        else:
            return 0


import os
import math
import logging
import torch
from tensorboardX import SummaryWriter

from ..model.model import FOTSModel

def save_checkpoint(epoch, model, optimizer, lr_scheduler, best_score, folder, save_as_best):
    if not os.path.exists(folder):
        os.makedirs(folder)
    if save_as_best:
        torch.save(model.state_dict(), os.path.join(folder, 'best_model.pt'))
        print('Updated best_model')
    torch.save({
        'epoch': epoch,
        'model_state_dict': model.state_dict(),
        'optimizer_state_dict': optimizer.state_dict(),
        'lr_scheduler_state_dict': lr_scheduler.state_dict(),
        'best_score': best_score  # not current score
    }, os.path.join(folder, 'last_checkpoint.pt'))

def restore_checkpoint(folder):
    model = FOTSModel().to(torch.device("cuda"))
    optimizer = torch.optim.Adam(model.parameters(), lr=0.001, betas=(0.9, 0.999), eps=1e-08, weight_decay=1e-5,
                                 amsgrad=False)
    lr_scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer, 0.94)

    if os.path.isfile(os.path.join(folder, 'last_checkpoint.pt')):
        checkpoint = torch.load(os.path.join(folder, 'last_checkpoint.pt'))
        epoch = checkpoint['epoch'] + 1
        model.load_state_dict(checkpoint['model_state_dict'])

        optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
        lr_scheduler.load_state_dict(checkpoint['lr_scheduler_state_dict'])
        best_score = checkpoint['best_score']
        return epoch, model, optimizer, lr_scheduler, best_score
    else:
        return 0, model, optimizer, lr_scheduler, -math.inf

class BaseTrainer:
    """
    Base class for all trainers
    """
    def __init__(self, metrics, config):
        self.config = config
        self.logger = logging.getLogger(self.__class__.__name__)
        self.metrics = metrics
        self.name = config['name']
        self.epochs = config['trainer']['epochs']
        self.summyWriter = SummaryWriter()
        self.checkpoint_dir = os.path.join(config['trainer']['save_dir'], self.name)

        self.start_epoch, self.model, self.optimizer, self.lr_scheduler, self.best_score = \
            restore_checkpoint(self.checkpoint_dir)
        # self.model.summary()

        self.device = torch.device("cuda")
        self.monitor = config['trainer']['monitor']

    def train(self):
        """
        Full training logic
        """
        for epoch in range(self.start_epoch, self.epochs + 1):
            result = self._train_epoch(epoch)

            log = {'epoch': epoch}
            for key, value in result.items():
                if key == 'metrics':
                    for i, metric in enumerate(self.metrics):
                        log[metric.__name__] = result['metrics'][i]
                elif key == 'val_metrics':
                    for i, metric in enumerate(self.metrics):
                        log['val_' + metric.__name__] = result['val_metrics'][i]
                else:
                    log[key] = value

            self.lr_scheduler.step()
            self.logger.info('New Learning Rate: {:.8f}'.format(self.lr_scheduler.get_lr()[0]))

            self.summyWriter.add_scalars('Train', {'train_' + self.monitor: result[self.monitor],
                                                   'val_' + self.monitor: result[self.monitor]}, epoch)

            if self.best_score < result['hmean']:
                self.best_score = result['hmean']
                save_as_best = True
            else:
                save_as_best = False
            save_checkpoint(epoch, self.model, self.optimizer, self.lr_scheduler, self.best_score,
                            self.checkpoint_dir, save_as_best)

        self.summyWriter.close()

    def _train_epoch(self, epoch):
        """
        Training logic for an epoch

        :param epoch: Current epoch number
        """
        raise NotImplementedError


import pandas as pd
import numpy as np

def cost_fun(w, x):
    return np.dot(x, w)

def normalization(x, mean, std):
    x = np.asarray(x)
    res = (x - mean) / std
    return res

df = pd.read_csv('heart.csv')

listOfFeatures = ['age', 'sex', 'cp', 'trestbps', 'chol', 'fbs', 'restecg', 'thalach', 'exang','oldpeak', 'slope',	'ca', 'thal']
#listOfFeatures = ['cp', 'fbs', 'restecg', 'thalach', 'slope', 'oldpeak', 'ca', 'thal']
#listOfFeatures = ['cp', 'fbs', 'restecg', 'ca', 'slope', 'thalach']
best1 = listOfFeatures[0]
best2 = listOfFeatures[1]
bestAccuracy = 0
bestAlpha = 0

for k1 in range(len(listOfFeatures)):
    for k2 in range(k1 + 1, len(listOfFeatures)):
        sub_data = df[[listOfFeatures[k1], listOfFeatures[k2], 'target']]
        iterations = 5
        lamda = 1 / iterations

        train_data = sub_data.sample(frac=0.80)
        test_data = sub_data.drop(train_data.index.tolist())

        x1_train = train_data[[listOfFeatures[k1]]]
        x2_train = train_data[[listOfFeatures[k2]]]

        meanTrain_X1 = np.asarray(x1_train).mean()
        stdTrain_X1 = np.asarray(x1_train).std()

        meanTrain_X2 = np.asarray(x2_train).mean()
        stdTrain_X2 = np.asarray(x2_train).std()

        X1_train_norm = normalization(x1_train, meanTrain_X1, stdTrain_X1)
        X2_train_norm = normalization(x2_train, meanTrain_X2, stdTrain_X2)

        onesTrain = np.ones([X2_train_norm.shape[0], 1])  # create a array containing only ones
        x_train = np.concatenate([onesTrain, X1_train_norm, X2_train_norm], 1)

        y_train = np.array(train_data['target'])
        for i in range(len(y_train)):
            if y_train[i] == 0:
             y_train[i] = -1

        x1_test = test_data[[listOfFeatures[k1]]]
        x2_test = test_data[[listOfFeatures[k2]]]

        meanTest_x1_test = np.asarray(x1_test).mean()
        stdTest_x1_test = np.asarray(x1_test).std()
        meanTest_x2_test = np.asarray(x2_test).mean()
        stdTest_x2_test = np.asarray(x2_test).std()

        X1_test_norm = normalization(x1_test, meanTest_x1_test, stdTest_x1_test)
        X2_test_norm = normalization(x2_test, meanTest_x2_test, stdTest_x2_test)

        onesTest = np.ones([X1_test_norm.shape[0], 1])
        x_test = np.concatenate([onesTest, X1_test_norm, X2_test_norm], 1)

        y_test = np.array(test_data['target'])
        for i in range(len(y_test)):
            if y_test[i] == 0:
             y_test[i] = -1

        y_train = y_train.reshape(len(y_train), 1)
        y_test = y_test.reshape(len(y_test), 1)

        alpha_list = [0.0000001, 0.001, 0.01, 0.1, 1]
        for alpha in alpha_list:
            w = [[0.0], [0.0], [0.0]]
            b=0
            for i in range(iterations):
                cost_values = (cost_fun(w, x_train)+b) * y_train
                for j in range(len(cost_values)):
                    if cost_values[j][0] >= 1:
                        for k in range(len(w)):
                            w[k][0] = w[k][0] - (alpha * (2 * lamda * w[k][0]))
                        b = b - alpha * 2 * lamda * b
                    else:
                        for k in range(len(w)):
                           # print(x_train[j][k])
                            w[k][0] = w[k][0] + (alpha * ((y_train[j][0] * x_train[j][k]) - (2 * lamda * w[k][0])))
                        b = b + alpha * (y_train[j][0] - 2 * lamda * b)

            cost_values = cost_fun(w, x_test)+b
            count = 0
            mat = np.zeros((len(y_test), 1))
            i = 0
            for i in range(len(cost_values)):
                if cost_values[i][0] < 0:
                    mat[i][0] = -1
                else:
                    mat[i][0] = 1
            i = 0
            for i in range(len(y_test)):
                if mat[i][0] == y_test[i][0]:
                    count = count + 1

            #print("for alpha: ", alpha)
            accuracy = (count / len(y_test)) * 100
            # print("feature 1: ", listOfFeatures[k1])
            # print("feature 2: ", listOfFeatures[k2])
            # print("Accuracy: ", accuracy)
            # print("\n")
            if accuracy > bestAccuracy:
                bestAccuracy = accuracy
                bestAlpha = alpha
                best1 = listOfFeatures[k1]
                best2 = listOfFeatures[k2]

print("Best alpha: ", bestAlpha)
print("Best feature 1: ", best1)
print("Best feature 2: ", best2)
print("Best accuracy: ", bestAccuracy," %")


#!/usr/bin/env python
# -*- coding: utf-8 -*-

# 插入排序

'''
核心：通过构建有序序列，对于未排序序列，在已排序序列中从后向前扫描(对于单向链表则只能从前往后遍历)，找到相应位置并插入。实现上通常使用in-place排序(需用到O(1)的额外空间)

	1. 从第一个元素开始，该元素可认为已排序
	2. 取下一个元素，对已排序数组从后往前扫描
	3. 若从排序数组中取出的元素大于新元素，则移至下一位置
	4. 重复步骤3，直至找到已排序元素小于或等于新元素的位置
	5. 插入新元素至该位置
	6. 重复2~5
'''

def insertionSort(data):
    for k, v in enumerate(data):
        idx = k
        while idx > 0 and data[idx-1] > v:
            data[idx] = data[idx-1]
            idx -= 1
        data[idx] = v

    return data

if __name__ == '__main__':
    data = [10, 4, 6, 2, 1, 8]
    print(data)
    print(insertionSort(data))


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 26 15:23:03 2017

@author: venusgrape

Write a program to calculate the credit card balance after one year if a person only pays the minimum monthly
payment required by the credit card company each month.

The following variables contain values as described below:

balance - the outstanding balance on the credit card

annualInterestRate - annual interest rate as a decimal

monthlyPaymentRate - minimum monthly payment rate as a decimal

For each month, calculate statements on the monthly payment and remaining balance. At the end of 12 months,
print out the remaining balance. Be sure to print out no more than two decimal digits of accuracy - so print

Remaining balance: 813.41
instead of
Remaining balance: 813.4141998135
So your program only prints out one thing: the remaining balance at the end of the year in the format:
Remaining balance: 4784.0

A summary of the required math is found below:

Monthly interest rate= (Annual interest rate) / 12.0
Minimum monthly payment = (Minimum monthly payment rate) x (Previous balance)
Monthly unpaid balance = (Previous balance) - (Minimum monthly payment)
Updated balance each month = (Monthly unpaid balance) + (Monthly interest rate x Monthly unpaid balance)

"""

def pay_debt(b, air, mmpr):
    mir = air / 12
    mmp = mmpr * b
    mub = b - mmp
    updated_balance = mub + mir * mub
    return updated_balance

def show_balance(b, air, mmpr, month):
    if month == 0:
        return b
    else:
        while month > 0:
            b = pay_debt(b, air, mmpr)
            month -= 1
        return "{0:.2f}".format(b)

print(show_balance(5000, 0.18, 0.02, 12))


from distutils.version import StrictVersion
from inspect import ismethod

import django
from django.db import models
from django.utils.functional import cached_property

class NullType(object):
    pass

def hook(
    hook: str,
    when: str = None,
    when_any=None,
    was="*",
    is_now="*",
    has_changed: bool = None,
    is_not=NullType,
):
    if when_any is None:
        when_any = []
    assert hook in (
        "before_save",
        "after_save",
        "before_create",
        "after_create",
        "before_update",
        "after_update",
        "before_delete",
        "after_delete",
    )

    def decorator(hooked_method):
        if not hasattr(hooked_method, "_hooked"):

            def func(*args, **kwargs):
                hooked_method(*args, **kwargs)

            func._hooked = []
        else:
            func = hooked_method

        func._hooked.append(
            {
                "hook": hook,
                "when": when,
                "when_any": when_any,
                "was": was,
                "is_now": is_now,
                "has_changed": has_changed,
                "is_not": is_not,
            }
        )

        return func

    return decorator

DJANGO_RELATED_FIELD_DESCRIPTOR_CLASSES = []
if StrictVersion(django.__version__) < StrictVersion("1.9"):
    from django.db.models.fields.related import (
        SingleRelatedObjectDescriptor,
        ReverseSingleRelatedObjectDescriptor,
        ForeignRelatedObjectsDescriptor,
        ManyRelatedObjectsDescriptor,
        ReverseManyRelatedObjectsDescriptor,
    )

    DJANGO_RELATED_FIELD_DESCRIPTOR_CLASSES.extend(
        [
            SingleRelatedObjectDescriptor,
            ReverseSingleRelatedObjectDescriptor,
            ForeignRelatedObjectsDescriptor,
            ManyRelatedObjectsDescriptor,
            ReverseManyRelatedObjectsDescriptor,
        ]
    )
if StrictVersion(django.__version__) >= StrictVersion("1.9"):
    from django.db.models.fields.related_descriptors import (
        ForwardManyToOneDescriptor,
        ReverseOneToOneDescriptor,
        ReverseManyToOneDescriptor,
        ManyToManyDescriptor,
    )

    DJANGO_RELATED_FIELD_DESCRIPTOR_CLASSES.extend(
        [
            ForwardManyToOneDescriptor,
            ReverseOneToOneDescriptor,
            ReverseManyToOneDescriptor,
            ManyToManyDescriptor,
        ]
    )
if StrictVersion(django.__version__) >= StrictVersion("1.11"):
    from django.db.models.fields.related_descriptors import ForwardOneToOneDescriptor

    DJANGO_RELATED_FIELD_DESCRIPTOR_CLASSES.extend([ForwardOneToOneDescriptor])
DJANGO_RELATED_FIELD_DESCRIPTOR_CLASSES = tuple(DJANGO_RELATED_FIELD_DESCRIPTOR_CLASSES)

class LifecycleModelMixin(object):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._initial_state = self.__dict__.copy()

    @property
    def _diff_with_initial(self):
        initial = self._initial_state
        current = self.__dict__
        diffs = []

        for k, v in initial.items():
            if k in current and v != current[k]:
                diffs.append((k, (v, current[k])))

        return dict(diffs)

    def initial_value(self, field_name: str = None):
        """
        Get initial value of field when model was instantiated.
        """
        if self._meta.get_field(field_name).get_internal_type() == "ForeignKey":
            if not field_name.endswith("_id"):
                field_name = field_name + "_id"

        if field_name in self._initial_state:
            return self._initial_state[field_name]

        return None

    def has_changed(self, field_name: str = None) -> bool:
        """
        Check if a field has changed since the model was instantiated.
        """
        changed = self._diff_with_initial.keys()

        if self._meta.get_field(field_name).get_internal_type() == "ForeignKey":
            if not field_name.endswith("_id"):
                field_name = field_name + "_id"

        if field_name in changed:
            return True

        return False

    def save(self, *args, **kwargs):
        skip_hooks = kwargs.pop("skip_hooks", False)
        save = super().save

        if skip_hooks:
            save(*args, **kwargs)
            return

        is_new = self._state.adding

        if is_new:
            self._run_hooked_methods("before_create")
        else:
            self._run_hooked_methods("before_update")

        self._run_hooked_methods("before_save")
        save(*args, **kwargs)
        self._run_hooked_methods("after_save")

        if is_new:
            self._run_hooked_methods("after_create")
        else:
            self._run_hooked_methods("after_update")

        self._initial_state = self.__dict__.copy()

    def delete(self, *args, **kwargs):
        self._run_hooked_methods("before_delete")
        super().delete(*args, **kwargs)
        self._run_hooked_methods("after_delete")

    @cached_property
    def _field_names(self):
        return [field.name for field in self._meta.get_fields()]

    @cached_property
    def _property_names(self):
        """
            Gather up properties and cached_properties which may be methods
            that were decorated. Need to inspect class versions b/c doing
            getattr on them could cause unwanted side effects.
        """
        property_names = []

        for name in dir(self):
            try:
                attr = getattr(type(self), name)

                if isinstance(attr, property) or isinstance(attr, cached_property):
                    property_names.append(name)

            except AttributeError:
                pass

        return property_names

    @cached_property
    def _descriptor_names(self):
        """
        Attributes which are Django descriptors. These represent a field
        which is a one-to-many or many-to-many relationship that is
        potentially defined in another model, and doesn't otherwise appear
        as a field on this model.
        """

        descriptor_names = []

        for name in dir(self):
            try:
                attr = getattr(type(self), name)

                if isinstance(attr, DJANGO_RELATED_FIELD_DESCRIPTOR_CLASSES):
                    descriptor_names.append(name)

            except AttributeError:
                pass

        return descriptor_names

    @cached_property
    def _potentially_hooked_methods(self):
        skip = set(
            ["_potentially_hooked_methods", "_run_hooked_methods"]
            + self._field_names
            + self._property_names
            + self._descriptor_names
        )

        collected = []

        for name in dir(self):
            if name in skip:
                continue

            try:
                attr = getattr(self, name)

                if ismethod(attr) and hasattr(attr, "_hooked"):
                    collected.append(attr)
            except AttributeError:
                pass

        return collected

    def _run_hooked_methods(self, hook: str):
        """
            Iterate through decorated methods to find those that should be
            triggered by the current hook. If conditions exist, check them before
            running otherwise go ahead and run.
        """
        for method in self._potentially_hooked_methods:
            for callback_specs in method._hooked:
                if callback_specs["hook"] != hook:
                    continue

                when = callback_specs.get("when")
                when_any = callback_specs.get("when_any")

                if when:
                    if self._check_callback_conditions(callback_specs):
                        method()
                elif when_any:
                    if self._check_callback_conditions_any(callback_specs):
                        method()
                else:
                    method()

    def _check_callback_conditions_any(self, specs: dict):
        _dict = specs.copy()
        for field in specs.get("when_any"):
            _dict["when"] = field
            if self._check_callback_conditions(_dict):
                return True
        return False

    def _check_callback_conditions(self, specs: dict):
        if not self._check_has_changed(specs):
            return False

        if not self._check_value_transition(specs):
            return False

        if not self._check_is_not_condition(specs):
            return False

        return True

    def _check_has_changed(self, specs: dict):
        field_name = specs["when"]
        has_changed = specs["has_changed"]

        if has_changed is None:
            return True

        return has_changed == self.has_changed(field_name)

    def _check_value_transition(self, specs: dict):
        field_name = specs["when"]
        specs_match = 0

        if specs["was"] in (self.initial_value(field_name), "*"):
            specs_match += 1

        if specs["is_now"] in (getattr(self, field_name), "*"):
            specs_match += 1

        return specs_match == 2

    def _check_is_not_condition(self, specs: dict):
        field_name = specs["when"]
        is_not = specs["is_not"]

        if is_not is NullType:
            return True

        return getattr(self, field_name) != is_not

# For backwards compatibility and Django 1.8
if StrictVersion(django.__version__) >= StrictVersion("1.9"):

    class LifecycleModel(LifecycleModelMixin, models.Model):
        class Meta:
            abstract = True