"""Solve various problems concerning the formula
"""
from __future__ import absolute_import
import copy

try:
    from networkx import DiGraph
except ImportError:
    from .minnx import DiGraph

from .tstruct import stategen

def forallexists_pre(tsys, A):
    tmp = set()
    for s in A:
        for pre_s in tsys.G.predecessors(s):
            for envpost in tsys.envtrans[pre_s]:
                canreach = False
                for post_pre_s in tsys.G.successors(pre_s):
                    if tuple([post_pre_s[i] for i in tsys.ind_uncontrolled]) != envpost:
                        continue
                    if post_pre_s in A:
                        canreach = True
                        break
                if not canreach:
                    break
            if canreach:
                tmp.add(pre_s)
    return tmp

def get_winning_set(tsys, return_intermediates=False):
    S = set(tsys.G.nodes())
    Z = [S.copy() for i in range(tsys.num_sgoals)]
    change_among_Z = True
    while change_among_Z:
        change_among_Z = False
        Z_prev = [this_Z.copy() for this_Z in Z]
        if return_intermediates:
            Y_list = [[] for i in range(len(Z))]
            X_list = [[] for i in range(len(Z))]
        for i in range(len(Z)):
            this_Z_prev = Z[i].copy()
            Y = set()
            if return_intermediates:
                num_sublevels = 0
            goal_progress = forallexists_pre(tsys, Z_prev[(i+1) % tsys.num_sgoals])
            goal_progress &= set([s for s in S if 'SYSGOAL'+str(i) in tsys.G.nodes[s]['sat']])
            while True:
                Y_prev = Y.copy()
                if return_intermediates:
                    Y_list[i].append(Y_prev)
                    num_sublevels += 1
                Y = set()
                Y_exmodal = forallexists_pre(tsys, Y_prev)
                reach_goal_progress = goal_progress.union(Y_exmodal)
                if return_intermediates:
                    X_list[i].append([])
                for j in range(tsys.num_egoals):
                    X = S.copy()
                    while True:
                        X_prev = X.copy()
                        X = forallexists_pre(tsys, X_prev)
                        X &= set([s for s in S if 'ENVGOAL'+str(j) not in tsys.G.nodes[s]['sat']])
                        X |= reach_goal_progress
                        if X == X_prev:
                            break
                        X &= X_prev
                    if return_intermediates:
                        X_list[i][num_sublevels-1].append(X)
                    Y |= X

                if Y == Y_prev:
                    if return_intermediates:
                        num_sublevels -= 1
                        X_list[i].pop()
                    break
                Y |= Y_prev
            Z[i] = Y
            if Z[i] != this_Z_prev:
                change_among_Z = True
                Z[i] &= this_Z_prev
    if return_intermediates:
        return Z[0], Y_list, X_list
    else:
        return Z[0]

def get_initial_states(W, tsys, exprtab, init_flags='ALL_ENV_EXIST_SYS_INIT'):
    """

    If initial conditions are not satisfied on the winning set, return None.
    """
    assert init_flags.upper() == 'ALL_ENV_EXIST_SYS_INIT', 'Only the initial condition interpretation ALL_ENV_EXIST_SYS_INIT is supported.'

    evalglobals = {'__builtins__': None, 'True': True, 'False': False}
    identifiers = [v['name'] for v in tsys.symtab]

    initial_states = list()

    for state in stategen([v for v in tsys.symtab if v['uncontrolled']]):
        stated = dict(zip(identifiers, state))
        if not eval(exprtab['ENVINIT'], evalglobals, stated):
            continue
        found_match = False
        for s in W:
            if (tuple([s[i] for i in tsys.ind_uncontrolled]) == state
                and 'SYSINIT' in tsys.G.nodes[s]['sat']):
                initial_states.append(copy.deepcopy(s))
                found_match = True
                break
        if not found_match:
            return None

    return initial_states

def check_realizable(tsys, exprtab, init_flags='ALL_ENV_EXIST_SYS_INIT'):
    W = get_winning_set(tsys)
    if get_initial_states(W, tsys, exprtab, init_flags) is None:
        return False
    else:
        return True

def synthesize(tsys, exprtab, init_flags='ALL_ENV_EXIST_SYS_INIT'):
    assert init_flags.upper() == 'ALL_ENV_EXIST_SYS_INIT', 'Only the initial condition interpretation ALL_ENV_EXIST_SYS_INIT is supported.'

    W, Y_list, X_list = get_winning_set(tsys, return_intermediates=True)
    initial_states = get_initial_states(W, tsys, exprtab, init_flags)
    if initial_states is None:
        return None

    goalnames = ['SYSGOAL'+str(i) for i in range(tsys.num_sgoals)]

    for goalmode in range(tsys.num_sgoals):
        Y_list[goalmode][0] = set([s for s in W if goalnames[goalmode] in tsys.G.nodes[s]['sat']])

    strategy = DiGraph()
    next_id = len(initial_states)
    workset = list(range(next_id))
    strategy.add_nodes_from([(i, {'state': s, 'mode': 0, 'initial': True})
                             for (i,s) in enumerate(initial_states)])
    while len(workset) > 0:
        nd = workset.pop()

        j = 0
        while j < len(Y_list[strategy.nodes[nd]['mode']]):
            if strategy.nodes[nd]['state'] in Y_list[strategy.nodes[nd]['mode']][j]:
                break
            j += 1
        if j == 0:
            assert goalnames[strategy.nodes[nd]['mode']] in tsys.G.nodes[strategy.nodes[nd]['state']]['sat']
            original_mode = strategy.nodes[nd]['mode']
            while goalnames[strategy.nodes[nd]['mode']] in tsys.G.nodes[strategy.nodes[nd]['state']]['sat']:
                strategy.nodes[nd]['mode'] = (strategy.nodes[nd]['mode'] + 1) % tsys.num_sgoals
                if strategy.nodes[nd]['mode'] == original_mode:
                    break
            if strategy.nodes[nd]['mode'] != original_mode:
                repeat_found = False
                for possible_repeat, attr in list(strategy.nodes(data=True)):
                    if (possible_repeat != nd
                        and attr['mode'] == strategy.nodes[nd]['mode']
                        and attr['state'] == strategy.nodes[nd]['state']):
                        repeat_found = True
                        for (u,v) in strategy.in_edges(nd):
                            strategy.add_edge(u, possible_repeat)
                        strategy.remove_edges_from(
                            list(strategy.in_edges(nd)))
                        strategy.remove_node(nd)
                        break
                if repeat_found:
                    continue

            j = 0
            while j < len(Y_list[strategy.nodes[nd]['mode']]):
                if strategy.nodes[nd]['state'] in Y_list[strategy.nodes[nd]['mode']][j]:
                    break
                j += 1
            if j == 0:
                assert goalnames[strategy.nodes[nd]['mode']] in tsys.G.nodes[strategy.nodes[nd]['state']]['sat']

        for envpost in tsys.envtrans[strategy.nodes[nd]['state']]:
            next_state = None
            for succ_nd in tsys.G.successors(strategy.nodes[nd]['state']):
                if (tuple([succ_nd[i] for i in tsys.ind_uncontrolled]) == envpost
                    and ((j > 0 and succ_nd in Y_list[strategy.nodes[nd]['mode']][j-1])
                         or (j == 0 and succ_nd in W))):
                    next_state = succ_nd
                    break

            if next_state is None:
                assert j > 0
                if j == 0:
                    import pdb; pdb.set_trace()
                blocking_index = None
                blocking_sets = X_list[strategy.nodes[nd]['mode']][j-1]
                for k in range(len(blocking_sets)):
                    if strategy.nodes[nd]['state'] in blocking_sets[k]:
                        blocking_index = k
                        break
                assert blocking_index is not None
                for succ_nd in tsys.G.successors(strategy.nodes[nd]['state']):
                    if (tuple([succ_nd[i] for i in tsys.ind_uncontrolled]) == envpost
                        and succ_nd in blocking_sets[blocking_index]):
                        next_state = succ_nd
                        break
                assert next_state is not None

            foundmatch = False
            for candidate, cattr in strategy.nodes(data=True):
                if cattr['state'] == next_state and cattr['mode'] == strategy.nodes[nd]['mode']:
                    strategy.add_edge(nd, candidate)
                    foundmatch = True
                    break
            if not foundmatch:
                if j == 0:
                    new_mode = (strategy.nodes[nd]['mode'] + 1) % tsys.num_sgoals
                else:
                    new_mode = strategy.nodes[nd]['mode']

                workset.append(next_id)
                strategy.add_node(
                    next_id,
                    state=next_state,
                    mode=new_mode,
                    initial=False)
                strategy.add_edge(nd, next_id)
                next_id += 1

    return strategy


#!/usr/bin/env python3
import sys

def main():
    elements = (1, 2, 4)
    bitset   = 0

    # Add elements to bitset
    for i in elements:
        bitset = bitset | 1<<i

    print(bitset)

    # Test for elements in bitset
    for i in range(6):
        if (bitset>>i) & 1:
            print(i)

    for i in range(6):
        if bitset & 1<<i:
            print(i)
    # Remove elements from bitset
    for i in elements:
        bitset = bitset & ~(1<<i)

    # Print contents of bitset
    print(bitset)

if __name__ == '__main__':
    main()


import math
def gamma(T,atomicMass,u1):
    return (T/(float(atomicMass)*u1))+1
def beta(gamma):
    return math.sqrt(1-(1/(gamma**2)))

atomicMass=input("Enter the Atomic mass : ") #A
Q=input("Enter positive state : ")
q=float(Q)*(1.6e-19) #q is the charge of the particle

kineticEnergy=input("Enter the Kinetic Energy in AMeV : ")

T=float(kineticEnergy)*float(atomicMass) #Kinetic Energy T in MeV

u1=931.494 #MeV/C^2
u2=1.6666e-27 #kg

m0=float(atomicMass)*u2 #in Kg

c=3e8 #speed of light in m/s

gamma=gamma(T,atomicMass,u1)
beta=beta(gamma)
print("gamma = ",gamma)
print("beta = ",beta)

Bp=(gamma*beta*c*m0)/q #B is magnetic Field and p is bending radius

print("Bp = ",Bp," Tesla meter")


from two_sum import two_sum
import copy

def three_sum(nums, target):
    res = []
    for n in range(len(nums)):
        new_num = nums[n+1:]
        temp_res = two_sum(new_num, target - nums[n])
        # print(temp_res)
        # print([x + [nums[n]] for x in temp_res])
        res.extend([x + [nums[n]] for x in temp_res])
    return res

if __name__ == "__main__":
    print(three_sum([1,2,3,4,5,6,7,8], 12))


import os, sys
import postTomo as pt

from skimage import io
from scipy import ndimage

__author__ = 'Vitaliy Starchenko'

class CorrectCapillaryTiltXZ(pt.AbstractBaseTool):
    '''
    This tool corrects the tilt of the capillary in the direction defined
    by axis.
    '''

    def __init__(self):
        self.__toolName__ = 'correctTilt'

        # an array of parameters for current generator
        self.parameters = []

        # the format for parameters: [name, initial_value, description
        self.parameters.append(
            ['inputDir', 'fullDomain', 'directory with initial files']
        )
        self.parameters.append(
            ['outputDir', 'fullDomain_corTilt', 'final directory with files '
                                             'with corrected tilt']
        )
        self.parameters.append(
            ['rotAxis', 'y', 'the axis of rotation: x, y, z']
        )
        self.parameters.append(
            ['angle', '0.5', 'the angle to correct the tilt']
        )

    def execute(self, dictFileName):
        print('Starting {0:s} tool'.format(self.__toolName__))

        lines = self.read_dict(dictFileName)
        empty, inputDir, description = self.check_a_parameter('inputDir', lines)
        empty, outputDir, description = self.check_a_parameter('outputDir', lines)
        empty, axis, description = self.check_a_parameter('rotAxis', lines)
        empty, angle, description = self.check_a_parameter('angle', lines)

        # image orientation as usual ZYX
        if axis == "x":
            rotationAxis = (0, 1)
        elif axis == "y":
            rotationAxis = (0, 2)
        elif axis == "z":
            rotationAxis = (1, 2)
        else:
            print("Wrong value of rotational axis: {}\n"
                  "Please check the dictionary file.")
            exit(2)

        angle = float(angle)
        os.makedirs(outputDir)

        tif_files = sorted([f for f in os.listdir(inputDir)
                            if (os.path.isfile(
                os.path.join(inputDir, f)) and ".tif" in f)])

        for j, filename in enumerate(tif_files):
            print('\n*********************************************')
            print('  Processing file {}'.format(filename))
            print('*********************************************')

            filen = os.path.join(inputDir, filename)
            stack_tif = io.imread(filen, plugin='tifffile')

            stack_tif = ndimage.interpolation.rotate(stack_tif, angle,
                            axes=rotationAxis, reshape=False, output=None, order=3,
                            mode='constant', cval=0.0, prefilter=True)

            resultFilename = os.path.splitext(filename)[0]
            savef = os.path.join(outputDir, '{}_corTlt.tif'.
                                 format(resultFilename))
            io.imsave(savef, stack_tif, plugin='tifffile')

        return True


from roct.upper_bound import maximum_adversarial_accuracy

import groot.datasets as datasets_module

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

# Avoid type 3 fonts
import matplotlib
matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['ps.fonttype'] = 42

import seaborn as sns
sns.set_theme(style="whitegrid", context="paper", palette="colorblind", font_scale=1.2)

datasets = ["banknote-authentication", "blood-transfusion", "breast-cancer", "cylinder-bands", "diabetes", "haberman", "ionosphere", "wine"]
data_dir = "data/"

percentages = [0.75, 0.5, 0.25]

results = []
for dataset in datasets:
    # From the groot.datasets module, find the function with name load_<selected_dataset> then execute it
    X, y = getattr(datasets_module, f"load_{dataset.replace('-', '_')}")()[1:3]
    X = MinMaxScaler().fit_transform(X)

    for epsilon in np.linspace(0, 0.5, 51):
        Delta_l = Delta_r = np.full(X.shape[1], epsilon)
        maximum_score = maximum_adversarial_accuracy(X, y, Delta_l, Delta_r)
        results.append((dataset, epsilon, maximum_score))

results_df = pd.DataFrame(results, columns=["Dataset", "Epsilon", "Adversarial accuracy bound"])

_, ax = plt.subplots(figsize=(7.5, 5))
sns.lineplot(data=results_df, x="Epsilon", y="Adversarial accuracy bound", hue="Dataset", style="Dataset", markers=True, dashes=False, ax=ax)
plt.xlabel("$\epsilon$")
plt.legend(bbox_to_anchor=(0.5, 1.05), loc=8, borderaxespad=0., ncol=4)
plt.tight_layout()
plt.savefig("out/datasets_epsilon.png")
plt.savefig("out/datasets_epsilon.pdf")
plt.close()

epsilon_table = []
for dataset in datasets:
    dataset_df = results_df[results_df["Dataset"] == dataset]

    min_robustness = dataset_df["Adversarial accuracy bound"].min()
    max_robustness = dataset_df["Adversarial accuracy bound"].max()

    epsilons = []
    for percentage in percentages:
        find_bound = (max_robustness - min_robustness) * percentage + min_robustness
        index = (dataset_df["Adversarial accuracy bound"] - find_bound).abs().idxmin()
        epsilon = dataset_df.loc[index]["Epsilon"]
        epsilons.append(epsilon)
    epsilon_table.append((dataset, *epsilons))

epsilon_df = pd.DataFrame(epsilon_table, columns=["Dataset", "75%", "50%", "25%"])
epsilon_df.to_csv("out/epsilon_choices.csv")
epsilon_df.to_latex("out/epsilon_choices.tex")


#
# @lc app=leetcode id=347 lang=python
#
# [347] Top K Frequent Elements
#
# https://leetcode.com/problems/top-k-frequent-elements/description/
#
# algorithms
# Medium (56.06%)
# Total Accepted:    230.4K
# Total Submissions: 411K
# Testcase Example:  '[1,1,1,2,2,3]\n2'
#
# Given a non-empty array of integers, return the k most frequent elements.
#
# Example 1:
#
#
# Input: nums = [1,1,1,2,2,3], k = 2
# Output: [1,2]
#
#
#
# Example 2:
#
#
# Input: nums = [1], k = 1
# Output: [1]
#
#
# Note:
#
#
# You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
# Your algorithm's time complexity must be better than O(n log n), where n is
# the array's size.
#
#
#
from collections import OrderedDict,Counter
class Solution(object):
    def topKFrequent(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: List[int]
        """

        count = Counter(nums)
        dd = OrderedDict(sorted(count.items(), key=lambda x: -x[1]))
        ans = []
        for item in dd.items():
            ans.append(item[0])
            k -= 1
            if k == 0:
                break
        return ans


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Sep 22 17:27:21 2019

@author: ubuntu
"""

import torch
from torch.autograd import Function

"""如何在pytorch中手写一个Function类，来利用pytorch的自动求导功能。
从而可以用来实现前向计算和反向传播，也就可以用来自定义层。
参考自：叠加态的猫 https://www.cnblogs.com/hellcat/p/8453615.html
"""
class Sigmoid(Function):

    @staticmethod
    def forward(ctx, x):  # ctx类似于Function类的self, 用来在forward函数好backward函数之间共享参数，只需要把要共享的参数通过命令ctx.save_for_backward保存，就可以在backward中通过ctx.saved_tensors()获得。
        output = 1 / (1 + torch.exp(-x))  #
        ctx.save_for_backward(output)
        return output

    @staticmethod
    def backward(ctx, grad_output): # 这里grad_output就相当于accum_grad，是前一级的输出梯度
        output,  = ctx.saved_tensors  # 新版pytorch用saved_tensors替代了saved_variables
        grad_x = output * (1 - output) * grad_output
        return grad_x

# 采用数值逼近方式检验计算梯度的公式对不对
#test_input = V(torch.randn(3,4), requires_grad=True)  # 早期版本需要用Varaiable
test_input = torch.randn(3, 4, requires_grad=True)
torch.autograd.gradcheck(Sigmoid.apply, (test_input,), eps=1e-3)

def f_sigmoid(x):
    y = Sigmoid.apply(x)
    y.backward(torch.ones(x.size()))

def f_naive(x):
    y =  1/(1 + torch.exp(-x))
    y.backward(torch.ones(x.size()))

def f_th(x):
    y = torch.sigmoid(x)
    y.backward(torch.ones(x.size()))

x=torch.randn(100, 100, requires_grad=True)
#%timeit -n 100 f_sigmoid(x)
#%timeit -n 100 f_naive(x)
#%timeit -n 100 f_th(x)


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

"""Tests for Requests."""

import time
import unittest

import hawk

CREDS = {
    'foobar-1234': {
        'id': 'foobar-1234',
        'key': 'some long random string',
        'algorithm': 'sha256'
    }
}

url = 'http://example.com/bazz?buzz=fizz&mode=ala'
params = {'buzz': 'fizz', 'mode': 'ala'}

class HawkTestCase(unittest.TestCase):

    def setUp(self):
        """Create simple data set with headers."""
        pass

    def tearDown(self):
        """Teardown."""
        pass

    def test_entry_points(self):

        hawk.client.header
        hawk.client.authenticate
        hawk.Server

    def test_client_api(self):
        header = hawk.client.header(url, 'GET', { 'credentials': CREDS['foobar-1234'],
                                                  'ext': 'and welcome!',
                                                  'nonce': 'lwfuar',
                                                  'timestamp': 1367927332})

        assert header['field'] == 'Hawk id="foobar-1234", ts="1367927332", nonce="lwfuar", ext="and welcome!", mac="ZZI/y3M0gV7PWCRX1VddptkWhunWxrpQikXAsLYzblU="'
        assert header['artifacts'] == {
            'nonce': 'lwfuar', 'ext': 'and welcome!', 'host': 'example.com',
            'dlg': None, 'resource': '/bazz?buzz=fizz&mode=ala', 'hash': '',
            'app': None, 'port': 80, 'ts': 1367927332, 'method': 'GET'
        }

        resp = {
            'headers': {
                'date': 'Tue, 07 May 2013 11:54:57 GMT',
                'content-type': 'text/plain',
                'server-authorization':
'Hawk mac="okjCR+o26FMhInYoJ1QO30Fu9cl3wGIWmwqydQXND+w=", hash="y+iZjG+hr2is3SmZLFOe551/LGS3PQPMY9ZWjToaNjg=", ext="and welcome!"',
               'server': 'WSGIServer/0.1 Python/2.7.2+'
           }
        }

        assert hawk.client.authenticate(resp, CREDS['foobar-1234'], header['artifacts'], {
                'payload': 'Hello and welcome!'
                })

    def test_bewit(self):
        bewit = hawk.client.get_bewit(url, {'credentials': CREDS['foobar-1234'],
                                            'ttl_sec': 60 * 1000})
        req = {
            'method': 'GET',
            'url': '/bazz?buzz=fizz&mode=ala&bewit=' + bewit,
            'host': 'example.com',
            'port': 80,
            'headers': {}
            }

        server = hawk.Server(req, lambda cid: CREDS[cid])
        assert server.authenticate_bewit({})

    def test_server_api(self):
        url = '/bazz?buzz=fizz&mode=ala'

        req = {
            'method': 'GET',
            'url': url,
            'host': 'example.com',
            'port': 80,
            'headers': {
                'authorization': 'Hawk id="foobar-1234", ts="1367927332", nonce="lwfuar", ext="and welcome!", mac="ZZI/y3M0gV7PWCRX1VddptkWhunWxrpQikXAsLYzblU="'
                }
            }
        server = hawk.Server(req, lambda cid: CREDS[cid])
        artifacts = server.authenticate({'timestampSkewSec': self._skewed_now()})

        assert artifacts == {
            'nonce': 'lwfuar', 'ext': 'and welcome!', 'dlg': '',
            'resource': '/bazz?buzz=fizz&mode=ala', 'app': '',
            'ts': '1367927332', 'port': 80,
            'mac': 'ZZI/y3M0gV7PWCRX1VddptkWhunWxrpQikXAsLYzblU=',
            'host': 'example.com', 'id': 'foobar-1234', 'hash': '',
            'method': 'GET'
        }

        payload = 'Hello and welcome!'
        header = server.header(artifacts,
                               { 'payload': payload,
                                 'contentType': 'text/plain' })

        assert header == 'Hawk mac="okjCR+o26FMhInYoJ1QO30Fu9cl3wGIWmwqydQXND+w=", hash="y+iZjG+hr2is3SmZLFOe551/LGS3PQPMY9ZWjToaNjg=", ext="and welcome!"'

    def test_server_api_with_abs_url(self):
        url = 'http://someserver/bazz?buzz=fizz&mode=ala'

        req = {
            'method': 'GET',
            'url': url,
            'host': 'example.com',
            'port': 80,
            'headers': {
                'authorization': 'Hawk id="foobar-1234", ts="1367927332", nonce="lwfuar", ext="and welcome!", mac="ZZI/y3M0gV7PWCRX1VddptkWhunWxrpQikXAsLYzblU="'
                }
            }
        server = hawk.Server(req, lambda cid: CREDS[cid])
        artifacts = server.authenticate({'timestampSkewSec': self._skewed_now()})

        self.assertEqual(artifacts['resource'],
                         '/bazz?buzz=fizz&mode=ala')

        payload = 'Hello and welcome!'
        header = server.header(artifacts,
                               { 'payload': payload,
                                 'contentType': 'text/plain' })

        assert header == 'Hawk mac="okjCR+o26FMhInYoJ1QO30Fu9cl3wGIWmwqydQXND+w=", hash="y+iZjG+hr2is3SmZLFOe551/LGS3PQPMY9ZWjToaNjg=", ext="and welcome!"'

    def test_parsing_empty_attrs(self):
        url = '/bazz?buzz=fizz&mode=ala'

        req = {
            'method': 'GET',
            'url': url,
            'host': 'example.com',
            'port': 80,
            'headers': {
                'authorization': 'Hawk id="foobar-1234", app="", ts="1367927332", nonce="lwfuar", ext="and welcome!", mac="ZZI/y3M0gV7PWCRX1VddptkWhunWxrpQikXAsLYzblU="'
                }
            }
        server = hawk.Server(req, lambda cid: CREDS[cid])
        artifacts = server.authenticate({'timestampSkewSec': self._skewed_now()})

    def _skewed_now(self):
        # Add 100 plus the difference between now and our hardcoded timestamp
        return time.time() - 1367927332 + 100

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


import torch
from torch.nn.utils import parameters_to_vector
from utils.model_utils import LeNet
from scripts.classification_datasets.mnist.mnist_torch_dataset import get_dataloaders
from laplace_package_extensions.laplace_extension import Laplace

'''
This script tests the outputs of the Laplace approximation for a very high value of \lambda
("frozen" essentially a deterministic network) and the actual deterministic network on which the Laplace approximation is fitted.
We see that there are some differences in the outputs between the deterministic network and the "frozen" Laplace
approximation model. We located the source of the difference in the laplace_package_extensions.baselapalce_extension file
in the function _nn_posterior_predictive_samples line 691. Whenever new samples are loaded in the model for evaluation
there are some small deviations from the actual deterministic values. These deviations, we believe, accumulate and make
the predictions between the deterministic and LA "frozen" model also different.
'''

#Load data and model
dir_MNIST= '/Users/Kostas/PycharmProjects/cold-warm-posteriors/scripts/classification_datasets/mnist/dataset'

test_dataloader,train_dataloader,train_suffix_dataloader,validation_dataloader,true_dataloader,true_suffix_dataloader = \
    get_dataloaders(dir=dir_MNIST,mode='classification',suffix_size = 5000,validation_size=5000,seed_split=42)

#Define model
model = LeNet()
model.load_state_dict(torch.load('MNIST/results/MNIST_lambda_0_to_1000/model_0/prior_mean.pt',map_location=torch.device('cpu')))

#Load datasamples
X,y = next(iter(test_dataloader))

#Make predictions (the LA baseclass applies a softmax internally, the deterministic model doesn't)
preds_model = model(X)
preds_model = torch.softmax(preds_model,dim=-1)

a  = parameters_to_vector(model.parameters()).detach()

#Fit Laplace approximation
la = Laplace(model, likelihood='classification', prior_precision=1,
                  subset_of_weights='all',
                  hessian_structure='isotropic', prior_mean=0)
la.fit(train_suffix_dataloader)

#Make \lambda very large
la.temperature = 10000000000000
preds_la = la(X, pred_type='nn', mode='posterior', model_averaging=True,
                     n_samples=100)

#Estimate difference between deterministic and LA "frozen" predictions
diff = preds_model.detach()-preds_la.detach()

end = 1


###############################################################################
# Tests for RxnData and XmlParser classes.

# Class methods are used on different XML files located in xml-files directory.
# Each version of the reactions XML file tests different features / cases:
#   rxns_ideal: Contains data in correct manner (the "ideal" example file).
#   rxns_neg_A: Same as rxns_ideal except for negative A rateCoeff in first
#       reaction.
###############################################################################

from pytest import approx

from chemkin import pckg_xml_path
from chemkin.chemkin_errors import ChemKinError
from chemkin.preprocessing.parse_xml import RxnType, XmlParser

def test_parse_basic_functionality ():
    """Ensures number of reactions returned is correct and that attributes
    of the <reaction> element in the XML file are parsed correctly.
    """
    xml = XmlParser(pckg_xml_path('rxns_ideal.xml'))
    species, rxns = xml.load()

    # Correct number of reactions returned.
    err_msg = 'Expected 2 reactions but received {}.'.format(len(rxns))
    assert len(rxns) == 2, err_msg

    # Attributes of <reaction> element parsed properly.
    err_msg = 'reversible attribute not parsed properly.'
    assert rxns[0].reversible == False, err_msg
    assert rxns[1].reversible == False, err_msg

    err_msg = 'rxn_id attribute not parsed properly.'
    assert rxns[0].rxn_id == 'reaction01', err_msg
    assert rxns[1].rxn_id == 'reaction02', err_msg

    err_msg = 'type attribute not parsed properly.'
    assert rxns[0].type == RxnType.Elementary, err_msg
    assert rxns[1].type == RxnType.Elementary, err_msg

def test_parse_reactants_products ():
    """Ensures reactants and products parsed correctly."""
    xml = XmlParser(pckg_xml_path('rxns_ideal.xml'))
    species, rxns = xml.load()

    err_msg = 'reactants not parsed correctly.'
    assert rxns[0].reactants == {'H':1, 'O2':1}, err_msg
    assert rxns[1].reactants == {'H2':1, 'O':1}, err_msg

    err_msg = 'products not parsed correctly.'
    assert rxns[0].products == {'OH':1, 'O':1}, err_msg
    assert rxns[1].products == {'OH':1, 'H':1}, err_msg

def test_parse_rxn_coeff ():
    """Ensures reaction coefficients are what we expect them to be."""
    xml = XmlParser(pckg_xml_path('rxns.xml'))
    species, rxns = xml.load()

    rxn1_coeff = rxns[0].rate_coeff
    rxn2_coeff = rxns[1].rate_coeff
    rxn3_coeff = rxns[2].rate_coeff

    # reaction01
    assert rxn1_coeff[0] == approx(3.52e+10)
    assert rxn1_coeff[1] == approx(7.14e+04)

    # reaction02
    assert rxn2_coeff[0] == approx(5.06e-2)
    assert rxn2_coeff[1] == approx(2.7)
    assert rxn2_coeff[2] == approx(2.63e+04)

    # reaction03
    assert rxn3_coeff == approx(1.0e+03)

def test_rxndata_equation ():
    """Ensures equation representation of RxnData is correct."""
    xml = XmlParser(pckg_xml_path('rxns_ideal.xml'))
    species, rxns = xml.load()

    err_msg = 'equation() method result different than expected.'
    expected_0 = 'H + O2 [=] O + OH'
    print(rxns[0].equation())
    assert rxns[0].equation() == expected_0, err_msg

    err_msg = 'equation() method result different than expected.'
    expected_1 = 'H2 + O [=] H + OH'
    assert rxns[1].equation() == expected_1, err_msg

def test_badparse_negative_A_arr ():
    """Ensures ChemKinError raised when A coefficient for one of the
    reactions is negative.
    """
    xml = XmlParser(pckg_xml_path('rxns_neg_A_2'))
    try:
        rxns = xml.load()
    except ChemKinError as err:
        assert type(err) == ChemKinError
        assert str(err).find(
              'A coeff < 0 in reaction with id = reaction02') != -1

def test_badparse_negative_A_modarr ():
    """Ensures ChemKinError raised when A coefficient for one of the
    reactions is negative.
    """
    xml = XmlParser(pckg_xml_path('rxns_neg_A'))
    try:
        rxns = xml.load()
    except ChemKinError as err:
        assert type(err) == ChemKinError
        assert str(err).find(
              'A coeff < 0 in reaction with id = reaction01') != -1


import os
import torch
import  numpy as np
import random
import matplotlib.pyplot as plt

# Setting seed
def set_seed(seed):
    # os.environ['PYTHONASHSEED'] = 0 무작위화 비활성화
    os.environ['PYTHONHASHSEED'] = str(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    np.random.seed(seed)
    random.seed(seed)

# Save plt as png
def save_figure(figure_name, figure_base_path = './assets/', figure_extension='.png', resolution=300):
    # make directory
    try:
        if not os.path.exists(figure_base_path):
            os.makedirs(figure_base_path)
    except:
      print('already exists')

    figure_path = figure_base_path + figure_name + figure_extension
    print('save figure: ', figure_name)

    plt.savefig(figure_path, bbox_inches='tight', format=figure_extension[1:], dpi=resolution)

# Get cpu or gpu device for training
def check_device():
    if torch.cuda.is_available():
        DEVICE = torch.device('cuda')
    else:
        DEVICE = torch.deivce('cpu')

    print('Using Pytorch version : ',  torch.__version__, 'DEVICE : ', DEVICE)

    return DEVICE

# show image & save image
def imshow(img, label):
  img = img / 2 + 0.5  # unnormalize
  npimg = img.numpy()
  plt.figure(figsize=(5, 5))
  plt.imshow(np.transpose(npimg, (1, 2, 0)))
  plt.axis('off')
  plt.show()


from helpers import Solution

def is_palindrome(x):
    y = str(x)
    n = len(y)
    for i in range(n // 2):
        if y[i] != y[n - i - 1]:
            return False
    return True

class LargestPalindromeProduct(Solution):
    def __init__(self, n=3):
        self.N = n

    def _baby_solution(self):
        bound = sum([9 * 10**i for i in range(self.N)])
        max_palindrome = 1

        max_nums = [0, 0]

        for i in range(bound+1):
            for j in range(bound+1):
                val = i * j
                if val > max_palindrome and is_palindrome(val):
                    max_palindrome = val
                    max_nums = i, j
        print(f"Max Nums: {max_nums}")
        return max_palindrome

    def solve(self):
        self._solve(chad=False)


import unittest
import dataStore as crd
ds = crd.DataStore('fifth')
ds.create('priya',20)

class Test2(unittest.TestCase):
       def test_list(self):
            res = ds.create("!priya_v",40)
            self.assertEqual(res,"OOPs! Your Key Name is Invalid, Key name can contain numbers(0-9) and alphabets(a-z A-Z) but no special characters")
if __name__ == "__main__":
    unittest.main()


# -*- coding: utf-8 -*-
"""
Created on Tue Aug  8 17:52:10 2017

@author: acbart
"""

import json
import os
import pandas as pd
import codecs
from unidecode import unidecode
from datetime import timedelta, datetime
from datetime import date as datetime_date
from dateutil.parser import parse
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
import matplotlib
import re
import math
from collections import Counter
import numpy as np
from textblob import TextBlob
from wordcloud import WordCloud, STOPWORDS
from nltk.corpus import stopwords
from nltk import word_tokenize
from itertools import tee, islice, combinations
import string
from textstat.textstat import textstat
from stepwise import forward_selected
from states import state_names
import scipy.stats as stats
import sklearn.metrics as metrics

#%%

wordcounts = []
y_wordcounts = []
ym_words = []
indexes = []
overall = Counter()
ym_words_bigrams = []
wordcounts_bigrams = []
overall_bigrams= Counter()
for filename in os.listdir('monthly_bodies'):
    _, year, month = filename.split('_')
    month = int(month[:-4])
    year = int(year)
    if year in (1996, 2017):
        continue
    with codecs.open('monthly_bodies/'+filename, encoding='utf-8') as inp:
        stripped_words = [w.strip() for w in inp.readlines()]
        words = Counter(stripped_words)
        ym_words.append(stripped_words)
        wordcounts.append(words)
        indexes.append((year, month))
        overall.update(stripped_words)
        # Bigrams
        stripped_bigrams = [l+' '+r for l, r in
                            zip(stripped_words[:-1], stripped_words[1:])]
        words_bigrams = Counter(stripped_bigrams)
        ym_words_bigrams.append(stripped_bigrams)
        wordcounts_bigrams.append(words_bigrams)
        overall_bigrams.update(stripped_bigrams)

index = pd.MultiIndex.from_tuples(indexes, names=('Year', "Month"))
lengths = pd.Series(data=[len(w) for w in ym_words],
                    index=index)
y_lengths = lengths.sum(level=0)
lengths_bigrams = pd.Series(data=[len(w) for w in ym_words_bigrams],
                    index=index)
y_lengths_bigrams = lengths_bigrams.sum(level=0)
def counts(word):
    return pd.Series(data=[w[word] for w in wordcounts],
                     index=index)
def counts_bi(phrase):
    return pd.Series(data=[p[phrase] for p in wordcounts_bigrams],
                     index=index)
#%%
BORING_PHRASES = ('computer science', 'software engineering',
                  'assistant professor', 'computer engineering',
                  'department computer', 'invites applications',
                  'professor computer', 'state university')
pd.Series(data=[[(phrase, count)
                 for phrase, count in p.most_common(100)
                 if phrase not in BORING_PHRASES]
                for p in wordcounts_bigrams],
          index=index)
#%%
def standardize_and_show_plot(ylim):
    plt.legend(loc=(1.04,.28), framealpha=0.2)
    plt.gcf().set_size_inches(4, 2)
    plt.xlim(1997, 2016)
    #plt.yticks(np.arange(0, ylim, .001))
    plt.yticks(np.arange(max(0, round(plt.gca().get_ylim()[0], 4)-.001),
                         round(plt.gca().get_ylim()[1], 4)+.001, .001))
    plt.ylabel("Proportion")
    #plt.xlabel("Year")
    plt.show()
#%%
## Programming Languages
for keyword, language in [('java', 'Java'),
                           ('python', 'Python'),
                           ('c', 'C'),
                           ('cpp', 'C++')]:
    plt.plot((counts(keyword).sum(level=0))/y_lengths,
             label=language)
standardize_and_show_plot(.005)
#%%
## Gendered words
FEMALE_TERMS = ("women", "woman", "she", "her", "herself",
              "females", "girls", "gals",
              "lady", "ladies",
               "female", "girl", "gal")
MALE_TERMS = ("men", "man", "he", "his", "him", "himself",
                 "males", "boys", "guys",
                 "gentleman", "gentlemen",
                "male", "boy", "guy")

(sum(counts(w) for w in MALE_TERMS).sum(level=0)/len(overall)).plot(label="Male")
(sum(counts(w) for w in FEMALE_TERMS).sum(level=0)/len(overall)).plot(label="Female")
plt.xlabel("")
standardize_and_show_plot(.03)
#%%
for buzzword in ["ethics", "cheating", "cheat"]:
    counts(buzzword).sum(level=0).plot(label=buzzword)
plt.legend(loc='best', framealpha=0.2)
plt.gcf().set_size_inches(4, 3)
plt.show()
#%%
for buzzword in [":)", ":-)", ":(", ":-("]:
    counts(buzzword).sum(level=0).plot(label=buzzword)
plt.legend(loc='best', framealpha=0.2)
plt.gcf().set_size_inches(4, 3)
plt.title("Smiley Faces")
plt.show()
#%%
for buzzword in ["teaching", "research"]:
    counts(buzzword).sum(level=0).plot(label=buzzword)
    #(counts(buzzword).sum(level=0)/y_lengths).plot(label=buzzword)
plt.legend(loc='best', framealpha=0.2)
plt.gcf().set_size_inches(4, 3)
plt.title("Teaching vs. Research")
plt.show()
#%%
## PL Styles
for keyword, language in [('object-oriented', 'Object-Oriented'),
                           ('functional', 'Functional'),
                           ('imperative', 'Imperative')]:
    plt.plot((counts(keyword).sum(level=0))/y_lengths,
             label=language)
standardize_and_show_plot(.03)
#%%
for buzzword in ["graduate", "undergraduate"]:
    counts(buzzword).sum(level=0).plot(label=buzzword)
    #(counts(buzzword).sum(level=0)/y_lengths).plot(label=buzzword)
plt.legend(loc='best', framealpha=0.2)
plt.gcf().set_size_inches(4, 3)
plt.title("Graduate vs. Undergraduate")
plt.show()
#print(stats.pearsonr(*[(counts(buzzword).sum(level=0)/y_lengths)
#        for buzzword in ["graduate", "undergraduate"]]))
#%%
## pd.DataFrame(overall.most_common(500), columns=["Word", "Frequency"]).to_csv("top_words.csv", index=False)
#%%
for buzzword in ["acm", "ieee"]:
    counts(buzzword).sum(level=0).plot(label=buzzword)
    #(counts(buzzword).sum(level=0)/y_lengths).plot(label=buzzword)
plt.legend(loc='best', framealpha=0.2)
plt.gcf().set_size_inches(4, 3)
plt.title("Professional Organizations")
plt.show()
#%%
for buzzword in ["fall", "spring", "winter", "summer"]:
    counts(buzzword).sum(level=0).plot(label=buzzword)
    #(counts(buzzword).sum(level=0)/y_lengths).plot(label=buzzword)
plt.legend(loc='best', framealpha=0.2)
plt.gcf().set_size_inches(4, 3)
plt.title("Semesters")
plt.show()
#%%
for buzzword in ["hci", "ai", "gui"]:
    counts(buzzword).sum(level=0).plot(label=buzzword)
    #(counts(buzzword).sum(level=0)/y_lengths).plot(label=buzzword)
plt.legend(loc='best', framealpha=0.2)
plt.gcf().set_size_inches(4, 3)
plt.title("Topics")
plt.show()
#%%
for phrase in ["computational thinking", "data structures", "k 12"]:
    counts_bi(phrase).sum(level=0).plot(label=phrase)
    #(counts(buzzword).sum(level=0)/y_lengths).plot(label=buzzword)
plt.legend(loc='best', framealpha=0.2)
plt.gcf().set_size_inches(4, 3)
plt.title("Subjects")
plt.show()
#%% CS Levels
for keyword, language in [('cs0', 'CS0'),
                           ('cs1', 'CS1'),
                           ('cs2', 'CS2'),
                           ('cs3', 'CS3')]:
    plt.plot((counts(keyword).sum(level=0))/y_lengths,
             label=language)
standardize_and_show_plot(.03)
#%%
for phrase in ["operating systems", "artificial intelligence",
               "human-computer interaction", "computer vision",
               "game development", "data mining",
               "discrete math", "machine learning",
               "computer graphics", "parallel distributed",
               "computer architecture", "software engineering"]:
    counts_bi(phrase).sum(level=0).plot(label=phrase)
    #(counts(buzzword).sum(level=0)/y_lengths).plot(label=buzzword)
plt.legend(loc='best', framealpha=0.2)
plt.gcf().set_size_inches(6, 6)
plt.title("Subjects")
plt.show()

#%%
for phrase in ["active learning", "paired programming", "distance education",
               "peer instruction"
               ]:
    counts_bi(phrase).sum(level=0).plot(label=phrase)
    #(counts(buzzword).sum(level=0)/y_lengths).plot(label=buzzword)
plt.legend(loc='best', framealpha=0.2)
plt.gcf().set_size_inches(4, 3)
plt.title("Subjects")
plt.show()
#%%
for phrase in ["functional programming", "object-oriented programming"]:
    counts_bi(phrase).sum(level=0).plot(label=phrase)
    #(counts(buzzword).sum(level=0)/y_lengths).plot(label=buzzword)
plt.legend(loc='best', framealpha=0.2)
plt.gcf().set_size_inches(4, 3)
plt.title("Subjects")
plt.show()
#%%
for phrase in ["women computing"]:
    counts_bi(phrase).sum(level=0).plot(label=phrase)
    #(counts(buzzword).sum(level=0)/y_lengths).plot(label=buzzword)
plt.legend(loc='best', framealpha=0.2)
plt.gcf().set_size_inches(4, 3)
plt.title("Subjects")
plt.show()
#%%
for phrase in ["computer science", "software engineering"]:
    counts_bi(phrase).sum(level=0).plot(label=phrase)
    #(counts(buzzword).sum(level=0)/y_lengths).plot(label=buzzword)
plt.legend(loc='best', framealpha=0.2)
plt.gcf().set_size_inches(4, 3)
plt.title("Subjects")
plt.show()
#%%
for keyword, language in [('middle school', 'Middle School'),
                           ('high school', 'High School'),
                           ('k 12', 'K-12')]:
    plt.plot((counts_bi(keyword).sum(level=0))/y_lengths_bigrams,
             label=language)
standardize_and_show_plot(.001)
#%%

s_wordcounts = []
s_y_wordcounts = []
s_ym_words = []
s_indexes = []
s_overall = Counter()
for filename in os.listdir('monthly_subjects'):
    _, year, month = filename.split('_')
    month = int(month[:-4])
    year = int(year)
    if year in (1996, 2017):
        continue
    with codecs.open('monthly_subjects/'+filename, encoding='utf-8') as inp:
        stripped_words = [w.strip() for w in inp.readlines()]
        words = Counter(stripped_words)
        s_ym_words.append(stripped_words)
        s_wordcounts.append(words)
        s_indexes.append((year, month))
        s_overall.update(stripped_words)

s_index = pd.MultiIndex.from_tuples(s_indexes, names=('Year', "Month"))
s_lengths = pd.Series(data=[len(w) for w in s_ym_words],
                    index=s_index)
s_y_lengths = s_lengths.sum(level=0)
def s_counts(word):
    return pd.Series(data=[w[word] for w in s_wordcounts],
                     index=s_index)
#%%
BORING_PHRASES = ['faculty', 'position', 'cs', 'workshop',
                  'science', 'university', 'tenuretrack', 'sigcse']
pd.Series(data=[[(phrase, count)
                 for phrase, count in p.most_common(100)
                 if phrase not in BORING_PHRASES]
                for p in s_wordcounts],
          index=s_index).map(lambda x: ', '.join([y[0] for y in x][:10]))


import unittest

import paddle.v2.fluid.core as core

from paddle.v2.fluid.framework import Program, default_startup_program

main_program = default_startup_program()

class TestOperator(unittest.TestCase):
    def test_error_type(self):
        block = main_program.create_block()
        try:
            block.append_op()
            self.assertFail()
        except ValueError as v_err:
            self.assertEqual(
                v_err.message,
                "`type` to initilized an Operator can not be None.")
        try:
            block.append_op(type="no_such_op")
            self.assertFail()
        except ValueError as a_err:
            self.assertEqual(a_err.message,
                             "Operator \"no_such_op\" has not been registered.")

    def test_op_desc_creation(self):
        program = Program()
        block = program.current_block()
        mul_x = block.create_var(
            dtype="float32", shape=[5, 10], lod_level=0, name="mul.x")
        mul_y = block.create_var(
            dtype="float32", shape=[10, 8], lod_level=0, name="mul.y")
        mul_out = block.create_var(
            dtype="float32", shape=[5, 8], lod_level=0, name="mul.out")
        mul_op = block.append_op(
            type="mul",
            inputs={"X": [mul_x],
                    "Y": mul_y},
            outputs={"Out": [mul_out]},
            attrs={"x_num_col_dims": 1})

        self.assertNotEqual(str(mul_op), "")
        self.assertEqual(mul_op.type, "mul")
        self.assertEqual(mul_op.input_names, ["X", "Y"])
        self.assertEqual(mul_op.input("X"), ["mul.x"])
        self.assertEqual(mul_op.input("Y"), ["mul.y"])
        self.assertEqual(mul_op.output_names, ["Out"])
        self.assertEqual(mul_op.output("Out"), ["mul.out"])
        self.assertEqual(
            set(mul_op.attr_names), set(["x_num_col_dims", "y_num_col_dims"]))
        self.assertEqual(mul_op.has_attr("x_num_col_dims"), True)
        self.assertEqual(mul_op.attr_type("x_num_col_dims"), core.AttrType.INT)
        self.assertEqual(mul_op.attr("x_num_col_dims"), 1)
        self.assertEqual(mul_op.has_attr("y_num_col_dims"), True)
        self.assertEqual(mul_op.attr_type("y_num_col_dims"), core.AttrType.INT)
        self.assertEqual(mul_op.attr("y_num_col_dims"), 1)
        self.assertEqual(mul_op.idx, 0)
        self.assertEqual(mul_out.op, mul_op)

    def test_mult_input(self):
        program = Program()
        block = program.current_block()
        sum_x1 = block.create_var(
            dtype="int", shape=[3, 4], lod_level=0, name="sum.x1")
        sum_x2 = block.create_var(
            dtype="int", shape=[3, 4], lod_level=0, name="sum.x2")
        sum_x3 = block.create_var(
            dtype="int", shape=[3, 4], lod_level=0, name="sum.x3")
        sum_out = block.create_var(
            dtype="int", shape=[3, 4], lod_level=0, name="sum.out")
        sum_op = block.append_op(
            type="sum",
            inputs={"X": [sum_x1, sum_x2, sum_x3]},
            outputs={"Out": sum_out})
        self.assertEqual(sum_op.type, "sum")
        self.assertEqual(sum_op.input_names, ["X"])
        self.assertEqual(sum_op.input("X"), ["sum.x1", "sum.x2", "sum.x3"])
        self.assertEqual(sum_op.output_names, ["Out"])
        self.assertEqual(sum_op.output("Out"), ["sum.out"])
        self.assertEqual(sum_op.idx, 0)
        self.assertEqual(sum_out.op, sum_op)

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


#!/usr/bin/python3
"""Print the first State object from the database hbtn_0e_6_usa."""
import sys
from model_state import Base, State
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

if __name__ == "__main__":
    # Check for the correct number of command-line arguments
    if len(sys.argv) != 4:
        print("Usage: {} <username> <password> <database>"
              .format(sys.argv[0]))
        sys.exit(1)

    # Create the database engine
    engine = create_engine(
    'mysql+mysqldb://{}:{}@localhost:3306/{}'.format(
        sys.argv[1], sys.argv[2], sys.argv[3]
    ),
    pool_pre_ping=True
    )

    # Create a session to interact with the database
    Session = sessionmaker(bind=engine)
    session = Session()

    # Query and retrieve the first State object
    first_state = session.query(State).order_by(State.id).first()

    # Check if a State object was found
    if first_state:
        print("{}: {}".format(first_state.id, first_state.name))
    else:
        print("Nothing")

    # Close the session
    session.close()


from bin import sentinel
from lib import config
from lib import sucr_config
import time
import datetime
import sys
import os
import shutil
import string
import random
import math
import logging
import traceback

from builtins import input

try:
    from colorama import init
    from termcolor import colored
    from pynput import keyboard

    DISPLAY_AVAILABLE = True
except:
    DISPLAY_AVAILABLE = False

def random_string(n):
    rnd = random.SystemRandom()
    pool = string.ascii_lowercase + string.ascii_uppercase + string.digits
    m = len(pool) - 1

    return ''.join(pool[rnd.randint(0, m)] for _ in range(n))

def run_sentinel():
    logger = logging.getLogger('sentinel')
    print(colored('Sentinel runs every 1 minute', 'green'))

    while True:
        now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")

        print(colored('{} Running sentinel'.format(now), 'green'))
        try:
            sentinel.entrypoint()
        except Exception as e:
            print(colored('Error: {}'.format(e), 'red'))

            logger.error(str(e))
            logger.error(traceback.format_exc())
            logger.error('--------------------')

        time.sleep(60) # Wait for a minute

def fix_masternode(data_folder):
    try:
        wallet_file = os.path.join(data_folder, 'wallet.dat')

        if not os.path.isfile(wallet_file):
            print('It seems like the data folder (the one containing wallet.dat) is not the same as the folder where sucr.conf is')
            data_folder = input('Please, write the data folder path: ')
            return fix_masternode(data_folder)

        print(colored('Make a copy of "wallet.dat" and "sucr.conf" before continuing.\nThis program will try it best not to touch them, but just in case!', attrs=['bold']))
        confirm = input('Once done, press [ENTER], or write cancel + [ENTER] to exit\n')
        if confirm.lower() == "cancel":
            return

        print(colored('Removing conflicting files', 'green'))
        for filename in os.listdir(data_folder):
            if filename in ('wallet.dat', 'sucr.conf'):
                continue

            realpath = os.path.join(data_folder, filename)
            if os.path.isfile(realpath):
                os.remove(realpath)
            elif os.path.isdir(realpath):
                shutil.rmtree(realpath)

            print('\t{}'.format(filename))

        print(colored('Done removing', 'green'))
        print('')

        print(colored('Checking sucr.conf contents', 'green'))
        tokens = sucr_config.SucreConfig.tokenize(config.sucr_conf)

        # Check if needed tokens are there, set them if not
        tokens['rpcuser'] = tokens.get('rpcuser', random_string(18))
        tokens['rpcpassword'] = tokens.get('rpcpassword', random_string(18))
        tokens['rpcconnect'] = tokens.get('rpcconnect', '127.0.0.1')
        tokens['rpcport'] = tokens.get('rpcport', 9335)
        tokens['server'] = 1

        # It must have a masternode=1
        try:
            if int(tokens['masternode']) != 1:
                raise Exception
        except:
            r = None
            while r not in ('y', 'n'):
                print(colored('It seems like your sucr.conf is not setting the wallet as masternode!', 'red'))
                print(colored('If your are running a COLD wallet, this must be executed on the MN wallet, not the collateral', 'red'))
                print(colored('If you are using MNs hosts like HostMNs DO NOT continue, this should be done by them, not you!', 'red'))
                r = input(colored('Continuing will set masternode=1, don\'t do it if this is a collateral wallet, [y/n]: ', 'red', attrs=['bold']))

                if r == 'n':
                    rewrite = False
                elif r != 'y':
                    print('Please write y or n')

            if r == 'y':
                tokens['masternode'] = 1
            else:
                sys.exit(1)

        # It must have a masternodeprivkey
        if 'masternodeprivkey' not in tokens:
            print(colored('Your sucr.conf does not contain a \'masternodeprivkey\' entry, please set it up before opening your wallet', 'red'))

        # Rewrite config
        with open(config.sucr_conf, 'w') as fp:
            for k, v in tokens.items():
                fp.write('{}={}\n'.format(k, v))

        print(colored('Done checking\n', 'green'))
        print(colored('Please open your wallet (sucrd on linux) and wait for it completely load/open before continuing', attrs=['bold']))
        input('Press [ENTER] to continue')
        print('')

        print(colored('Sentinel will automatically start in 15s', 'green', attrs=['bold']))
        print(colored('Until the wallet is fully synced, sentinel will show errors related to sync status, that\'s fine!', 'green'))
        print(colored('If it says it can not connect, check your sucr.conf settings', 'yellow'))

        r = 15
        while r > 0:
            sys.stdout.write('Starting in {}s \r'.format(r))
            sys.stdout.flush()
            r -= 1
            time.sleep(1)

        print('\n')
        run_sentinel()
    except Exception as e:
        print(colored('Error: {}'.format(e), 'red'))

        logger.error(str(e))
        logger.error(traceback.format_exc())
        logger.error('--------------------')

# @https://stackoverflow.com/questions/24582233/python-flush-input-before-raw-input
def flush_input():
    try:
        import sys, termios
        termios.tcflush(sys.stdin, termios.TCIOFLUSH)
    except ImportError:
        import msvcrt
        while msvcrt.kbhit():
            msvcrt.getch()

def menu():
    args = config.get_args()

    if args.repair and args.sentinel:
        print(colored('Do not specify --repair and --sentinel at the same time', 'red'))
        sys.exit(1)
    elif args.sentinel:
        return 1
    elif args.repair:
        return 2

    print('Select an option:')
    print('\t1. Start sentinel')
    print('\t2. Fix wallet and masternode')

    global menu_option
    menu_option = 0

    # No PyInput means no auto-start if no command line option was given, sry!
    if not DISPLAY_AVAILABLE:
        while menu_option not in (1, 2):
            try:
                menu_option = int(input('Please write 1 or 2 and press [ENTER]: '))
            except:
                pass

        return menu_option

    print('')

    def on_release(key):
        global menu_option

        try:
            if key.char in ('1', '2'):
                menu_option = int(key.char)
                return False
            else:
                print(colored('Option not valid', 'red'))
        except:
            pass

    # Collect events until released
    with keyboard.Listener(on_release=on_release) as listener:
        r = 30
        while menu_option == 0 and r > 0:
            sys.stdout.write('\rPress 1 or 2. Sentinel will automatically start in {}s  '.format(int(math.ceil(r))))
            sys.stdout.flush()

            r -= 0.1
            time.sleep(0.1)

        listener.stop()

    if menu_option == 0:
        menu_option = 1

    flush_input()

    print('\n')
    return menu_option

if __name__ == '__main__':
    init()

    # Setup logging
    logger = logging.getLogger('sentinel')
    hdlr = logging.FileHandler('sentinel.log')
    formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
    hdlr.setFormatter(formatter)
    logger.addHandler(hdlr)
    logger.setLevel(logging.INFO)

    if os.path.isfile(config.sentinel_config_file):
        print(colored('Using sentinel.conf: {}'.format(config.sentinel_config_file), 'green'))

    print(colored('Using sucr.conf: {}'.format(config.sucr_conf), 'green'))

    option = menu()
    if option == 1:
        run_sentinel()
    elif option == 2:
        # Use default data folder
        data_folder = os.path.dirname(config.sucr_conf)
        fix_masternode(data_folder)


from gi.repository import Gtk

#------------------------------------------------------------------------
#
# Indicator class
#
#------------------------------------------------------------------------
class Indicator(Gtk.DrawingArea):

    def __init__(self):
        Gtk.DrawingArea.__init__(self)
        self.connect('draw', self._draw)
        self.active = False
        self.set_size_request(5, -1)

    def set_active(self, value):
        self.active = value
        self.queue_draw()

    def _draw(self, widget, cr):

        # clip area to avoid extra work
        #cr.rectangle(event.area.x, event.area.y,
                     #event.area.width, event.area.height)
        #cr.clip()

        alloc = self.get_allocation()
        if self.active:
            cr.set_source_rgba(1, 0, 0, 1)
        else:
            cr.set_source_rgba(1, 0, 0, 0)
        cr.rectangle(0, 3, alloc.width, alloc.height-6)
        cr.fill()

#------------------------------------------------------------------------
#
# EntryGrid class
#
#------------------------------------------------------------------------
class EntryGrid(Gtk.Grid):

    def __init__(self, headings=None, tooltips=None, model=None, callback=None):
        Gtk.Grid.__init__(self)

        self.headings = headings
        self.tooltips = tooltips
        self.model = model
        self.widgets = []
        self.indicators = []
        self.selected = None
        self.callback = callback

    def set_model(self, model):
        self.model = model

        model.connect('row-inserted', self.row_inserted)
        model.connect('row-deleted', self.row_deleted)
        model.connect('rows-reordered', self.rows_reordered)
        self.sig_id = model.connect('row-changed', self.row_changed)

        if len(self.model) > 0:
            self.selected = model.get_iter((0,))

    def set_columns(self, columns, tooltips):
        self.headings = columns
        self.tooltips = tooltips

    def build(self):

        for child in self.get_children():
            self.remove(child)
            child.destroy()

        self.indicators = []
        self.widgets = []

        for column, heading in enumerate(self.headings):
            label = Gtk.Label(label=heading)
            label.set_halign(Gtk.Align.START)
            label.set_valign(Gtk.Align.CENTER)
            label.set_margin_right(8)
            label.show()
            self.attach(label, column + 2, 0, 1, 1)

        for row in range(len(self.model)):
            image = Gtk.Image()
            image.set_from_icon_name('gtk-index', Gtk.IconSize.BUTTON)
            button = Gtk.Button()
            button.set_relief(Gtk.ReliefStyle.NONE)
            button.add(image)
            button.connect('clicked', self.clicked, row)
            button.set_can_focus(False)
            button.show_all()
            self.attach(button, 0, row + 1, 1, 1)
            box = Indicator()
            box.show()
            if self.model.get_path(self.selected)[0] == row:
                box.set_active(True)
            self.attach(box, 1, row + 1, 1, 1)
            self.indicators.append(box)
            entry_row = []
            for column, value in enumerate(self.model[row]):
                if column == 0:
                    continue
                entry = Gtk.Entry()
                entry.set_width_chars(5)
                if value is not None:
                    entry.set_text(value)
                    set_size(entry)
                entry.set_tooltip_text(self.tooltips[column - 1])
                entry.connect('changed', self.changed, row, column)
                entry.connect('focus-in-event', self.got_focus, row)
                entry.show()
                self.attach(entry, column + 1, row + 1, 1, 1)
                entry_row.append(entry)
            self.widgets.append(entry_row)

    def get_selected(self):
        return self.selected

    def clicked(self, button, row):
        iter_ = self.model.get_iter((row,))
        self.callback(self.model, iter_)

    def got_focus(self, entry, event, row):
        for indicator in self.indicators:
            indicator.set_active(False)
        self.selected = self.model.get_iter((row,))
        self.indicators[row].set_active(True)

    def changed(self, entry, row, column):
        set_size(entry)
        self.model.handler_block(self.sig_id)
        self.model[row][column] = entry.get_text()
        self.model.handler_unblock(self.sig_id)

    def row_inserted(self, model, path, iter_):
        self.selected = model.get_iter((len(model) - 1,))
        self.build()

    def row_changed(self, model, path, iter_):
        for column in range(1, len(self.headings) + 1):
            value = model.get_value(iter_, column)
            if value is not None:
                self.widgets[path[0]][column - 1].set_text(value)

    def row_deleted(self, model, path):
        if len(model) > 0:
            self.selected = model.get_iter((0,))
        else:
            self.selected = None
        self.build()

    def rows_reordered(self, model, path, iter_, new_order):
        self.build()

    def clean_up(self):
        self.headings = None
        self.tooltips = None
        self.model = None
        self.widgets = None
        self.indicators = None
        self.selected = None
        self.callback = None

def set_size(entry):
    layout = entry.get_layout()
    width, height = layout.get_pixel_size()
    entry.set_size_request(width + 18, -1)


def match_limit(offer_book, order):

    matching_offers = offer_book.get_offers_by_price(order.price, order.order_type)
    matching_offers.sort(key=lambda x: x.base_amount, reverse=True)
    amount_left = order.amount
    take_offers = list()

    for offer in matching_offers:

        if amount_left >= offer.base_amount:
            take_offers.append(offer)
            amount_left -= offer.base_amount

    return take_offers, amount_left


"""
Shell sort is a highly efficient sorting algorithm and is based on insertion sort algorithm.
This algorithm avoids large shifts as in case of insertion sort, if the smaller value is to the far right and has to be moved to the far left.
!Eg:                    [5, 8, 3, 2, 6, 1]->  gap=3
                        [2, 6, 1, 5, 8, 3]->  gap=1
!                       [1, 2, 3, 5, 6, 8]-> Sorted list
"""

def Shell_sort(arr):
    gap = len(arr)//2
    while gap > 0:
        for i in range(gap, len(arr)):
            curr_ele = arr[i]
            pos = i
            while pos >= gap and curr_ele < arr[pos-gap]:
                arr[pos] = arr[pos-gap]
                pos -= gap
            arr[pos] = curr_ele
        gap = gap//2
    return arr

def shellSort(lst):
    gap = len(lst)//2
    while gap > 0:
        for i in range(gap, len(lst)):
            while i >= gap and lst[i] < lst[i-gap]:
                lst[i], lst[i-gap] = lst[i-gap], lst[i]
                i -= gap
        gap //= 2
    return lst

if __name__ == "__main__":
    arr = [5, 8, 3, 2, 6, 1]
    print(Shell_sort(arr))
    print(shellSort(arr))


import wx
import files

class Toolbar(wx.ToolBar):
  def __init__(self,Frame,Panel):
    self.ASH_Frame = Frame
    wx.ToolBar.__init__(self,Panel, id=-1, style = wx.TB_HORIZONTAL | wx.TB_TEXT)
    (ID_NEW,ID_OPEN,ID_SAVE,ID_PLAY,ID_STOP,ID_RECORD,ID_DOWNLOAD) = (1,2,3,4,5,6,7)
    self.AddLabelTool(ID_NEW,'New', wx.Bitmap('icons/new.png'))
    self.AddLabelTool(ID_OPEN, 'Open', wx.Bitmap('icons/open.png'))
    self.AddLabelTool(ID_SAVE, 'Save', wx.Bitmap('icons/save.png'))
    self.AddSeparator()
    self.AddLabelTool(ID_PLAY, 'Play', wx.Bitmap('icons/play.png'))
    self.AddLabelTool(ID_STOP, 'Stop', wx.Bitmap('icons/stop.png'))
    self.AddLabelTool(ID_RECORD, 'Record', wx.Bitmap('icons/record.png'))
    self.AddSeparator()
    self.AddLabelTool(ID_DOWNLOAD, 'Save Song', wx.Bitmap('icons/download.jpeg'))
    self.AddSeparator()
    Frame.Bind(wx.EVT_TOOL, self.OnNew, id=ID_NEW)
    Frame.Bind(wx.EVT_TOOL, self.OnOpen, id=ID_OPEN)
    Frame.Bind(wx.EVT_TOOL, self.OnSave, id=ID_SAVE)
    Frame.Bind(wx.EVT_TOOL, self.OnPlay, id=ID_PLAY)
    Frame.Bind(wx.EVT_TOOL, self.OnStop, id=ID_STOP)
    Frame.Bind(wx.EVT_TOOL, self.OnRecord, id=ID_RECORD)
    Frame.Bind(wx.EVT_TOOL, self.OnSaveSong, id=ID_DOWNLOAD)
    self.Realize()
    self.Player = None

  def OnNew(self,event):
    files.OnNew(self.ASH_Frame)

  def OnOpen(self,event):
    files.OpenFile(self.ASH_Frame)

  def OnSave(self,event):
    files.SaveFile(self.ASH_Frame)

  def OnPlay(self,event):
    if self.ASH_Frame.Tune.TuneName != None:
      self.Player = wx.Sound(self.ASH_Frame.Tune.TuneName)
      self.Player.Play()

  def OnStop(self,event):
    if self.Player != None:
      self.Player.Stop()
      self.Player = None

  def OnRecord(self,event):
    import tune, Tkinter, tkSnack
    tune.recorder(self.ASH_Frame)
    self.ASH_Frame.Tune.TkRoot = Tkinter.Tk()
    tkSnack.initializeSnack(self.ASH_Frame.Tune.TkRoot)
    self.ASH_Frame.Tune.SnackSound.write(".tempfile.wav")
    self.ASH_Frame.Tune.SnackSound = tkSnack.Sound(load=".tempfile.wav")
    if tune.IS_RECORDED:
      self.ASH_Frame.Tune.TuneName = "saves/.temp.wav"
      self.ASH_Frame.Tune.SnackSound.read("saves/.temp.wav")
      T = self.ASH_Frame.Tune.SnackSound.length(unit="SECONDS")
      if T < 10 :
        Time = "Tune too short!"
      elif T > 20 :
        Time = "Tune too long!"
      else:
        Time = "Time : " + str() + "Sec"
      self.ASH_Frame.Tune.Time.SetLabel(Time)
      self.ASH_Frame.Tune.DrawGraph()

  def OnSaveSong(self, event):
    S = self.ASH_Frame.Song.SongList.GetSelections()
    if self.ASH_Frame.Song.Songs == None or S == ():
      Dialog = wx.MessageDialog(None, "No Song Selected.", 'Error!', wx.OK | wx.ICON_ERROR)
      Dialog.ShowModal()
    else:
      files.SaveSong(self.ASH_Frame)


#!/usr/bin/env python
from __future__ import absolute_import, division, print_function

import imagehash
import six
from PIL import Image
import datetime
import configparser
from multiprocessing import Pool, Semaphore, cpu_count

from dbman import Db
from hashtools import runhash, is_image, cropres

# It takes an image, hashes it, and adds it to the database
class ImageData:
    def __init__(self, img):
        db_location, _ = fetch_config(version="Default")
        self.db = Db(db_location)
        self.mode = "init"
        self.exists = self.db.check_exist(img)
        self.img = img
        self.hashed = False

    def hash_img(self):
        """
        It takes an image, runs it through a series of image hashing functions, and returns the results
        """
        self.ahash = runhash(self.img, imagehash.average_hash)
        self.phash = runhash(self.img, imagehash.phash)
        self.dhash = runhash(self.img, imagehash.dhash)
        self.haar = runhash(self.img, imagehash.whash)
        self.db4 = runhash(self.img, lambda img: imagehash.whash(img, mode="db4"))
        self.color = runhash(self.img, imagehash.colorhash)
        self.crop = cropres(self.img)
        if str(self.ahash) != "":
            self.hashed = True

    def add_to_db(self):
        """
        It takes the image, and then it hashes it using the following methods: ahash, phash, dhash, haar,
        db4, color, and crop
        """

        self.db.add_hash(
            filename=self.img,
            ahash=self.ahash,
            phash=self.phash,
            dhash=self.dhash,
            haar=self.haar,
            db4=self.db4,
            color=self.color,
            crop=self.crop,
        )

def process_image(img):
    """
    It takes an image, checks if it exists in the database, hashes it if it doesn't, and adds it to the
    database if it's been hashed

    :param img: The image to be processed
    """
    image = ImageData(img=img)
    if image.exists == False:
        image.hash_img()
        if image.hashed:
            image.add_to_db()

def worker(img):
    """
    It takes an image, checks if it exists in the database, hashes it if it doesn't, and adds it to the
    database if it's been hashed

    :param img: The image to be processed
    """

    image = ImageData(img=img)
    if image.exists == False:
        image.hash_img()
        if image.hashed:
            image.add_to_db()

def fetch_config(version, file="config.ini"):
    """
    It reads the config.ini file and returns the values of the db and source keys in the section that
    matches the version parameter

    :param version: The version of the config file to use
    :param file: the name of the config file, defaults to config.ini (optional)
    :return: the values of the variables db_location and source.
    """
    config = configparser.ConfigParser()
    config.read(file)
    config_version = version

    db_location = config[config_version]["db"]
    source = config[config_version]["source"]
    return db_location, source

if __name__ == "__main__":
    import os
    import sys

    global db

    db_location, source = fetch_config(version="Default")

    # connect to database
    db = Db(db_location)

    paths = os.listdir(source)
    start = datetime.datetime.now()

    fullfiles = []

    for p in paths:
        image_name = os.path.join(source, p)
        if is_image(image_name):
            fullfiles.append(image_name)

    poolsize = Semaphore(cpu_count())
    print(poolsize)
    p = Pool(cpu_count() - 1)
    p.map(worker, fullfiles)

    db.writedupes()
    db.cropresist_csv()
    print(datetime.datetime.now() - start)