__copyright__ = 'Copyright(c) Gordon Elliott 2014'

"""
    The Wallet class takes care of identifying a wallet and moving funds
    to another wallet.
"""
import logging

from decimal import Decimal
from time import time, sleep

from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException

LOG = logging.getLogger(__name__)
MINIMUM_CONFIRMATIONS = 1

class Wallet(object):
    """ Wallet with one account
    """

    def __init__(self, uri, account_name, fee):
        self._connection = AuthServiceProxy(uri)
        self._account_name = account_name

        self._connection.settxfee(fee)

    @property
    def account_name(self):
        return self._account_name

    def get_address(self):
        return self._connection.getnewaddress(self._account_name)

    def get_accounts(self):
        return self._connection.listaccounts(MINIMUM_CONFIRMATIONS)

    @property
    def balance(self):
        return self._connection.getbalance(self._account_name, MINIMUM_CONFIRMATIONS)

    def move_to_account(self, source_account, destination_account, amount, comment=None):
        if amount > Decimal('0.0'):
            self._connection.move(source_account, destination_account, amount, MINIMUM_CONFIRMATIONS, comment)
        elif amount < Decimal('0.0'):
            self._connection.move(destination_account, source_account, -amount, MINIMUM_CONFIRMATIONS, comment)
        # just fall through if amount == 0.0 as there is nothing to do

    def transfer_to(self, destination, amount):
        """ Create an address for each wallet and transfer the funds between them
        """
        to_address = destination.get_address()

        validation = self._connection.validateaddress(to_address)
        if validation['isvalid']:
            try:
                transaction_id = self._connection.sendfrom(self._account_name, to_address, amount, MINIMUM_CONFIRMATIONS, 'out', 'in')
                if transaction_id is not None:
                    transaction_details = self._connection.gettransaction(transaction_id)
                    fee = transaction_details.get(u'fee')
                    # LOG.info('transaction_id %r, fee %r', transaction_id, fee)
                    return transaction_id, fee
                else:
                    return None, None
            except JSONRPCException as json_ex:
                LOG.exception('JSON Exception calling sendfrom(%s,_,%d) %r' % (self._account_name, amount, json_ex.error))
                raise
        else:
            return None, None

    def find_transaction(self, including_transaction, confirmations=MINIMUM_CONFIRMATIONS):
        for transaction in self._connection.listtransactions():
            if transaction.get(u'txid') == including_transaction:
                if transaction[u'confirmations'] >= confirmations:
                    return True
        return False

    def wait_for_transaction(
        self, including_transaction, confirmations=MINIMUM_CONFIRMATIONS, max_wait=60, secs_between_checks=0.1
    ):
        current = time()
        timeout = current + max_wait
        while current < timeout and not self.find_transaction(including_transaction, confirmations):
            sleep(secs_between_checks)
            current = time()
        return current < timeout

    def __repr__(self):
        """ Provide a string representation
        """
        return "{}('{}')".format(self.__class__.__name__, self._account_name)

"""
>>> bitcoinrpc.connect_to_remote('admin1', '123', host='localhost', port=19001)
>>> conn = bitcoinrpc.connect_to_remote('admin1', '123', host='localhost', port=19001)
>>> conn.getbalance()
>>> pay_to = conn.getnewaddress()
>>> pay_to
>>> rv = conn.validateaddress(pay_to)
>>> rv
>>> conn.getinfo()
>>> conn.move('test0', 'test1', 10.0)
>>> conn.getinfo()
>>> rv = conn.validateaddress(pay_to)
>>> altconn = bitcoinrpc.connect_to_local('2/bitcoin.conf')
"""


from model import Model

class Brick(Model):

  def __init__(self):
    super(Brick, self).__init__()

    self.clazz = 'Brick'
    self.type = 'logic_and'

    self.meta = {
      'imageurl'    :{'index':0, 'type':'url',    'display':'image',  'group':'info', 'label':'Image'},
      'name'        :{'index':1, 'type':'string', 'display':'text',   'group':'info', 'label':'Name'},
      'description' :{'index':2, 'type':'string', 'display':'text',   'group':'info', 'label':'Description'},
      'inputA'      :{'index':3, 'type':'boolean', 'display':'toggle', 'group':'control', 'label':'inputA' },
      'inputB'      :{'index':4, 'type':'boolean', 'display':'toggle', 'group':'control', 'label':'inputB' },
      'output'      :{'index':5, 'type':'boolean', 'display':'state', 'group':'control', 'label':'output' },
    }

    self.props = {
      'imageurl':'wireup-logo.png',
      'name':'Logic AND',
      'description':'if inputA AND inputB then output',
      'inputA': False,
      'inputB': False,
      'output': False
    }

  def start(self):
    self.on('inputA',self.evaluate)
    self.on('inputB',self.evaluate)

  def stop(self):
    self.commit('output', False)

  def evaluate(self, model, prop, value):
    if self.props['inputA'] and self.props['inputB']:
      self.commit('output', True)
    else:
      self.commit('output', False)

  def toDescription(self):
    return [self.clazz,self.type,self.props['imageurl'],self.props['name'],self.props['description']]


def add(self, constraint, check=False):
        """
        Add a constraint to the set

        :param constraint: The constraint to add to the set.
        :param check: Currently unused.
        :return:
        """
        if isinstance(constraint, bool):
            constraint = BoolConstant(constraint)
        assert isinstance(constraint, Bool)
        constraint = simplify(constraint)
        # If self._child is not None this constraint set has been forked and a
        # a derived constraintset may be using this. So we can't add any more
        # constraints to this one. After the child constraintSet is deleted
        # we regain the ability to add constraints.
        if self._child is not None:
            raise Exception('ConstraintSet is frozen')

        if isinstance(constraint, BoolConstant):
            if not constraint.value:
                logger.info("Adding an impossible constant constraint")
                self._constraints = [constraint]
            else:
                return

        self._constraints.append(constraint)

        if check:
            from ...core.smtlib import solver
            if not solver.check(self):
                raise ValueError("Added an impossible constraint")


# DIRECTORY = r'C:\Users\Professional\Documents\2021\Texts'
DIRECTORY = r"P:\Users\Professional\Documents\2021"
FIND_STRING = 'стандарт'
COUNT_ADDING_SYMBOLS = 30
import os

def find(find_directory, find_string):
    for root, dirs, files in os.walk(find_directory):  # рекурсивная функция
        for name in files:
            find_string_in_file(os.path.join(root, name), find_string)

def find_string_in_file(file, find_string):
    f = open(file, 'r', encoding='utf-8', errors='ignore')
    content = f.read()
    # print(content)
    index = content.find(find_string)
    f.close()
    if index != -1:
        print_find_info(file, content, find_string)

def print_find_info(file, content, find_string):
    index = content.find(find_string)
    start_index = index - COUNT_ADDING_SYMBOLS if index >= COUNT_ADDING_SYMBOLS else 0
    end_index = index + COUNT_ADDING_SYMBOLS + len(find_string)
    content = content[start_index:end_index]
    content = content.replace(find_string, "\x1b[32;1m" + find_string + "\x1b[0m")
    # print (f'{file}:\n...{content[start_index:end_index]}...')
    print()
    print(file)
    print()
    print(content)
    print()

if __name__ == "__main__":
    find(DIRECTORY, FIND_STRING)


import pandas as pd
import numpy as np
import torch
from torch import optim
from torch import nn
from sklearn.model_selection import train_test_split
from torch.utils.data import DataLoader, TensorDataset

excel_file = "血常规数据整合.xlsx"
data = pd.read_excel(excel_file)

X = data[['WBC', 'LY','GR','MO','RBC','Hgb','HCT','MCV','MCH','RDW','PLT','PCT','MPV','PDW']].values
Y = data['result'].values

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2)

X_train=torch.tensor(X_train)
Y_train=torch.tensor(Y_train)
train_dataset = TensorDataset(X_train,Y_train)
train_loader = DataLoader(train_dataset,batch_size=60,shuffle=True)
#X_train=X_train.unsqueeze(-1)
#X_train=X_train.unsqueeze(-1)
#X_train=X_train.repeat(1,1,8,8)
#Y_train=Y_train.unsqueeze(-1)
#Y_train=Y_train.unsqueeze(-1)
#Y_train=Y_train.repeat(1,1,8,8)
#print(X_train.shape)
#shape=[239,14,8,8]

X_test=torch.tensor(X_test)
Y_test=torch.tensor(Y_test)
test_dataset = TensorDataset(X_test,Y_test)
test_loader = DataLoader(test_dataset,batch_size=60,shuffle=False)
#X_test=X_test.unsqueeze(-1)
#X_test=X_test.unsqueeze(-1)
#X_test=X_test.repeat(1,1,8,8)
#Y_test=Y_test.unsqueeze(-1)
#Y_test=Y_test.unsqueeze(-1)
#Y_test=Y_test.repeat(1,1,8,8)

class SimpleCNN(nn.Module):
	def __init__(self,num_classes=3):
		#num_classes即最终输出的类别数（默认10）
		super(SimpleCNN,self).__init__()
		self.cv1=nn.Conv2d(in_channels=14,out_channels=32,kernel_size=3,stride=1,padding=1)
		#输入14，输出32，卷积核为3*3（后面调试）
		#padding=1:保证输入输出大小一致
		self.relu1=nn.ReLU()
		self.p1=nn.MaxPool2d(kernel_size=5,stride=1)
		#窗口越大步长越小指向全局而忽略细节，反之同理

		self.cv2=nn.Conv2d(in_channels=32,out_channels=64,kernel_size=3,stride=1,padding=1)
		self.relu2=nn.ReLU()
		self.p2=nn.MaxPool2d(kernel_size=5,stride=1)

		self.input=nn.Linear(1024,128)
		#3(上层卷积输出)*9*9（池化数据，后续调试）
		self.relu3=nn.ReLU()
		self.output=nn.Linear(128,num_classes)

	def forward(self,x):
		x=self.p1(self.relu1(self.cv1(x)))
		x=self.p2(self.relu2(self.cv2(x)))
		#print(x.shape)
		x=x.view(-1,1024)
		x=self.input(x)
		x=self.relu3(x)
		x=self.output(x)
		#x=x.repeat(60,1)
		return x

model = SimpleCNN()

#print(model)

criterion = nn.CrossEntropyLoss()
#损失函数min差距，计算损失更新权重
optimizer = optim.Adam(model.parameters(),lr=0.01)
#Adam优化算法，0.01学习率

for epoch in range(10):#进行10个周期
	for inputs,labels in train_loader:
		inputs = inputs.to(torch.float32)
		inputs=inputs.unsqueeze(-1)
		inputs=inputs.unsqueeze(-1)
		inputs=inputs.repeat(1,1,8,8)
		#print(inputs.shape)
		#print(labels.shape)
		optimizer.zero_grad()
		#优化器清零（默认相加，故在循环中每个周期开始时需要return0）
		outputs = model(inputs)
		#print(outputs.shape)
		loss = criterion(outputs,labels)
		loss.backward()
		#回传数据，更新参数
		optimizer.step()
		#依据以上更新参数，每次循环进行迭代

total = 0
correct = 0

with torch.no_grad():#喜报，把backward禁了
	for inputs,labels in test_loader:
		inputs = inputs.to(torch.float32)
		inputs=inputs.unsqueeze(-1)
		inputs=inputs.unsqueeze(-1)
		inputs=inputs.repeat(1,1,8,8)
		outputs = model(inputs)
		#print(inputs)
		#print(outputs)
		_,predicted = torch.max(outputs.data,1)
		total += labels.size(0)
		correct += int((predicted == labels).sum())
		#print(type(predicted))
		#<class 'torch.Tensor'>
		arrpre = predicted.int()
		#转换作numpy数组/int
		#strpre = np.array_str(arrpre)
		#numpy至str
		#print(strpre)

length = len(arrpre)
for i in arrpre:
	#print(type(i))
	if i == 1:
		print('贫血')
	elif i == 0:
		print('病毒感染')
	elif i == 2:
		print('细菌感染')

print(f"准确率：{100 * correct / total}%")

神经网络（cnn）.py


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

"""Folder plugin."""

import numbers

from gl.folder_list import FolderList as glancesFolderList
from gl.plugins.glances_plugin import GlancesPlugin

class Plugin(GlancesPlugin):

    """Glances folder plugin."""

    def __init__(self, args=None):
        """Init the plugin."""
        super(Plugin, self).__init__(args=args)

        # We want to display the stat in the curse interface
        self.display_curse = True

        # Init stats
        self.glances_folders = None
        self.reset()

    def get_key(self):
        """Return the key of the list."""
        return 'path'

    def reset(self):
        """Reset/init the stats."""
        self.stats = []

    def load_limits(self, config):
        """Load the foldered list from the config file, if it exists."""
        self.glances_folders = glancesFolderList(config)

    def update(self):
        """Update the foldered list."""
        # Reset the list
        self.reset()

        if self.input_method == 'local':
            # Folder list only available in a full Glances environment
            # Check if the glances_folder instance is init
            if self.glances_folders is None:
                return self.stats

            # Update the foldered list (result of command)
            self.glances_folders.update()

            # Put it on the stats var
            self.stats = self.glances_folders.get()
        else:
            pass

        return self.stats

    def get_alert(self, stat):
        """Manage limits of the folder list"""

        if not isinstance(stat['size'], numbers.Number):
            return 'DEFAULT'
        else:
            ret = 'OK'

        if stat['critical'] is not None and stat['size'] > int(stat['critical']) * 1000000:
            ret = 'CRITICAL'
        elif stat['warning'] is not None and stat['size'] > int(stat['warning']) * 1000000:
            ret = 'WARNING'
        elif stat['careful'] is not None and stat['size'] > int(stat['careful']) * 1000000:
            ret = 'CAREFUL'

        return ret

    def msg_curse(self, args=None):
        """Return the dict to display in the curse interface."""
        # Init the return message
        ret = []

        # Only process if stats exist and display plugin enable...
        if not self.stats or args.disable_folder:
            return ret

        # Build the string message
        # Header
        msg = '{}'.format('FOLDERS')
        ret.append(self.curse_add_line(msg, "TITLE"))

        # Data
        for i in self.stats:
            ret.append(self.curse_new_line())
            if len(i['path']) > 15:
                # Cut path if it is too long
                path = '_' + i['path'][-15 + 1:]
            else:
                path = i['path']
            msg = '{:<16} '.format(path)
            ret.append(self.curse_add_line(msg))
            try:
                msg = '{:>6}'.format(self.auto_unit(i['size']))
            except (TypeError, ValueError):
                msg = '{:>6}'.format(i['size'])
            ret.append(self.curse_add_line(msg, self.get_alert(i)))

        return ret


import numpy as np
import matplotlib.pyplot as plt

data = np.load('mnist.npz')

X_train = data['X_train']  # shape = (60000, 28, 28)
y_train = data['y_train']  # shape = (60000,)

S = np.ndarray(shape=(100, 28, 28))

for i in range(10):
    S[i * 10:(i + 1) * 10] = X_train[y_train == i][:10]

pixels = S.reshape((-1, 28, 28))

pixels = np.insert(pixels, pixels.shape[1], 255, 1)
pixels = np.insert(pixels, pixels.shape[2], 255, 2)

pixels = pixels.reshape((10, 10, 29, 29)).transpose((0, 2, 1, 3)).reshape((-1, 29, 29))

plt.imshow(pixels.reshape((-1, 290)), cmap='gray')

plt.show()


import numpy as np
import pandas as pd
from scipy.stats import triang
from sklearn.linear_model import Ridge
import matplotlib.pyplot as plt
import os
import multiprocessing

def rv_generator(low_cost: float, high_cost: float, high_end: int, num: int)->np.ndarray:
    """
    Generate random variables using a triangular distribution.

    Args:
        low_cost (float): Lower limit of triangular distribution.
        high_cost (float): Upper limit of triangular distribution.
        high_end (int): Mode of triangular distribution expressed by a Scale Index between 1 and 10.
        num (int): Number of random numbers to be returned.

    Returns:
        np.ndarray: Array of random numbers generated from the triangular distribution.
    """
    # Calculate parameters for the triangular distribution
    a = low_cost
    b = high_cost
    c = a + (high_end - 1) * (b - a) / 9

    # Create a triangular distribution object
    triangular_dist = triang(c=(c - a) / (b - a), loc=a, scale=(b - a))

    # Generate random samples from the triangular distribution
    samples = triangular_dist.rvs(size=num)

    return samples

def boostraping(X, y, features_to_predict):
    indices = np.random.choice(len(X), len(X), replace=True)
    X_sample = X.iloc[indices]
    y_sample = y.iloc[indices]

    clf = Ridge(alpha=1.0)
    clf.fit(X_sample, y_sample)
    return clf.predict(features_to_predict)

def pseudo_data(data, high_end: int):
    result_rows = []

    # Iterate through each row in the original DataFrame
    for idx, row in data.iterrows():
        samples = rv_generator(row['low cost'], row['high cost'], high_end, 10)
        for sample in samples:
            result_rows.append({
                'bedrooms': row['bedrooms'],
                'bathrooms': row['bathrooms'],
                'kitchen': row['kitchen'],
                'living room': row['living room'],
                'detached': row['detached'],
                'modified sqft': row['modified sqft'],
                'additional sqft': row['additional sqft'],
                '2nd story': row['2nd story'],
                'cost': sample
            })

    # Create a DataFrame from the list of dictionaries
    return pd.DataFrame(result_rows)

def cost_distribution_estimate(user_input,high_end, num_samples = 1000):

    def generate_predicted_values(seed):
        np.random.seed(seed)
        return boostraping(X, y,[user_input])

    script_dir = os.path.dirname(os.path.abspath(__file__))
    csv_file_path = os.path.join(script_dir, 'price_data.csv')
    raw_price_data = pd.read_csv(csv_file_path)

    # generate pseudo data with given high_end
    df = pseudo_data(raw_price_data, high_end=high_end)
    X = df.drop(['cost'], axis=1)
    y = df['cost']

    # Initialize an array to store the predicted values
    predicted_values = np.zeros((num_samples))

    # Use multiprocessing to generate predicted values in parallel
    with multiprocessing.Pool() as pool:
        results = pool.map(generate_predicted_values, range(num_samples))
        predicted_values = np.array(results)

    mean_predicted_value = np.mean(predicted_values)
    return mean_predicted_value, predicted_values

def cost_distribution_estimate_non_mp_version(user_input,high_end, num_samples = 1000):
    script_dir = os.path.dirname(os.path.abspath(__file__))
    csv_file_path = os.path.join(script_dir, 'price_data.csv')
    raw_price_data = pd.read_csv(csv_file_path)

    # generate pseudo data with given high_end
    df = pseudo_data(raw_price_data, high_end=high_end)
    X = df.drop(['cost'], axis=1)
    y = df['cost']

    predicted_values = np.array([boostraping(X,y,[user_input]) for _ in range(num_samples)])
    mean_predicted_value = np.mean(predicted_values)

    return mean_predicted_value, predicted_values


from BlackjackPackage.card import Card
from BlackjackPackage.hand import Hand
from BlackjackPackage.deck import Deck
from BlackjackPackage.chip import Chip

LINES = 31

SUITS = ["Spades", "Hearts", "Clubs", "Diamonds"]
RANKS = ["Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", \
         "Ten", "Jack", "Queen", "King", "Ace"]
VALUES = {"Two":2, "Three":3, "Four":4, "Five":5, "Six":6, "Seven":7, "Eight":8, "Nine":9, \
          "Ten":10, "Jack":10, "Queen":10, "King":10, "Ace":11}

def print_hands(player, dealer, chips):
    print("-" * LINES)
    print(f"Your bet: ${chips.bet}")
    print("Your cards:")
    print(f"{player}")
    print(f"Value: {player.value}\n")

    print("Dealer cards:")
    print(f"{dealer}")
    if dealer.cards[1].face_down:
        print(f"Value: {dealer.value - dealer.cards[1].value}")
    else:
        print(f"Value: {dealer.value}")
    print("-" * LINES)

def place_bet(chips):
    while True:
        try:
            chips.bet = int(input("Place a bet: $"))
        except ValueError:
            print("Please bet a numerical value!")
        else:
            if chips.bet > chips.total:
                print("You don't have enough money!")
                continue
            break

def get_input():
    while True:
        move = input("'hit' or 'stay'? (q to exit): ").lower()

        if move in ("hit", "stay", "q"):
            print()
            return move

        print("Not a valid choice")

def play_again():
    while True:
        choice = input("Play again? ('yes' or 'no'): ").lower()

        if choice in ("yes", "no"):
            print()
            return choice

        print("Not a valid choice")

def initialize(chips):
    print("\n" * 5 + "=" * LINES)
    print("Welcome to blackjack in python!\n")
    print(f"Players chip count is: ${chips.total}")

    cards = []

    for suit in SUITS:
        for rank in RANKS:
            cards.append(Card(suit, rank, VALUES.get(rank)))

    card_deck = Deck(cards)
    player = Hand()
    dealer = Hand()

    card_deck.shuffle()

    place_bet(chips)

    return card_deck, player, dealer, chips

def make_move(card_deck, player, dealer, chips):
    while True:
        print_hands(player, dealer, chips)
        move = get_input()

        if move == "q":
            print("Goodbye")
            return "quit"

        if move == "hit":
            dealt_card = card_deck.deal()
            player.hit(dealt_card)
            print(f"Hit: {dealt_card}")
            if player.check_bust():
                print(f"Bust! You lost ${chips.bet}!")
                chips.lose()
                if chips.total < 1:
                    print("You've lost all your money!")
                    return "over"
                return "game"
            continue

        if move == "stay":
            dealer.cards[1].face_down = False
            while dealer.value < 17:
                dealer.hit(card_deck.deal())

            if player.is_winner(dealer):
                if dealer.value != player.value:
                    print(f"You Win ${chips.bet}!")
                    chips.win()
                    return "game"
            else:
                print(f"Dealer wins! You lost ${chips.bet}")
                chips.lose()
                if chips.total < 1:
                    print("You've lost all your money!")
                    return "over"
            print("Push! It's a tie!")
            return "game"

def play(setup):
    card_deck = setup[0]
    player = setup[1]
    dealer = setup[2]
    chips = setup[3]

    print("Dealing cards...")
    temp_card = card_deck.deal()
    temp_card.face_down = True
    dealer.hit(card_deck.deal(), temp_card)
    player.hit(card_deck.deal(), card_deck.deal())

    outcome = make_move(card_deck, player, dealer, chips)

    if outcome != "quit":
        print_hands(player, dealer, chips)
        if outcome != "over":
            return play_again()

    return outcome

def main():
    chips = Chip()
    while play(initialize(chips)) == "yes":
        pass

    print(f"Thanks for playing. You took home ${chips.total}\n")

main()


import matplotlib
matplotlib.rcParams['text.usetex'] = True
import matplotlib.pyplot as plt  #to implement plots
import csv                       #to read and process CSV files
import numpy as np               #to format numbers
from matplotlib.ticker import (MultipleLocator,FormatStrFormatter,AutoMinorLocator)

expt = []    #y-axis data
eesto = []   #x-axis data 1
qeomsto = [] #x-axis data 2
vqdsto = []  #x-axis data 3
eedz = []    #x-axis data 4
qeomdz = []  #x-axis data 5
vqddz = []   #x-axis data 6

#open CSV file
with open('/Users/gvnjones/Projects/gavinjones/mccoled/docs/npjcompmat/fig4d.csv') as csvDataFile:
    csvReader = csv.reader(csvDataFile)    #file handler
    for row in csvReader:                  #loop through file handler
        expt.append(row[1])                #load up second column element
        eesto.append(row[2])               #load up third column element
        qeomsto.append(row[3])
        vqdsto.append(row[4])
        eedz.append(row[5])
        qeomdz.append(row[6])
        vqddz.append(row[7])
fig, ax = plt.subplots()

exptFloat=list(map(float, expt))
eestoFloat=list(map(float, eesto))
qeomstoFloat=list(map(float, qeomsto))
vqdstoFloat=list(map(float, vqdsto))
eedzFloat=list(map(float, eedz))
qeomdzFloat=list(map(float, qeomdz))
vqddzFloat=list(map(float, vqddz))

print(exptFloat)

ax.plot(eestoFloat,exptFloat, label="Exact (STO-3G)", color="green", linestyle="dotted", marker="d")
ax.plot(qeomstoFloat,exptFloat, label="qEOM-VQE (STO-3G)", color="red", linestyle=(0, (1,10)), marker="d")
ax.plot(vqdstoFloat,exptFloat, label="VQD (STO-3G)", color="blue", linestyle=(0, (5,10)), marker="d")
ax.plot(eedzFloat,exptFloat, label="Exact (6-31G(d))", color="green", linestyle="dotted", marker="o")
ax.plot(qeomdzFloat,exptFloat, label="qEOM-VQE (6-31G(d))", color="red", linestyle=(0, (1,10)), marker="o")
ax.plot(vqddzFloat,exptFloat, label="VQD (6-31G(d))", color="blue", linestyle=(0, (5,10)), marker="o")

ax.set_xlabel(r'Experimental $ \Delta E_{ST}$', fontsize=16)
ax.set_ylabel(r'Calculated $ \Delta E_{ST}$', fontsize=16)
plt.xticks(fontsize=15)
plt.yticks(fontsize=15)
ax.get_xaxis().set_major_formatter(FormatStrFormatter('% 1.1f'))
ax.get_yaxis().set_major_formatter(FormatStrFormatter('% 1.1f'))
plt.legend(fontsize=12)                #displays legend
plt.tight_layout()          #auto adjust plot for better viewing
plt.savefig('/Users/gvnjones/Projects/gavinjones/mccoled/docs/npjcompmat/fig4d.pdf', dpi=900)
plt.show()                  #show the plot


fees = [180, 5000, 10, 600]
records = ["05:34 5961 IN", "06:00 0000 IN", "06:34 0000 OUT", "07:59 5961 OUT", "07:59 0148 IN",
           "18:59 0000 IN", "19:09 0148 OUT", "22:59 5961 IN", "23:00 5961 OUT"]  # [14600, 34400, 5000]
# fees = [120, 0, 60, 591]
# records = ["16:00 3961 IN","16:00 0202 IN","18:00 3961 OUT","18:00 0202 OUT","23:58 3961 IN"]  # [0, 591]
# fees = [1, 461, 1, 10]
# records = ["00:00 1234 IN"]  # [14841]
import math

def changeToMinute(first, second):
    h1, m1 = map(int, first.split(':'))
    h2, m2 = map(int, second.split(':'))
    total1, total2 = h1 * 60 + m1, h2 * 60 + m2

    return total2 - total1;

def solution(fees, records):
    dt, df, ut, uf = fees
    check = {}
    check_time = {}
    for record in records:
        when, car, inout = record.split()
        if inout == "IN":
            check[car] = when
        else:
            if car not in check_time:
                check_time[car] = changeToMinute(check[car], when)
            else:
                check_time[car] += changeToMinute(check[car], when)
            check[car] = "0"
    print(check)
    print(check_time)
    for key, value in check.items():
        print(key, value)
        if value != "0":
            if key in check_time:
                check_time[key] += changeToMinute(value, "23:59")
            else:
                check_time[key] = changeToMinute(value, "23:59")  # 테스트3번에서 check_time에 아예 아무것도 안들어가있을 수도 있음

    check_time = sorted(check_time.items())

    answer = []
    for car, total_time in check_time:
        if total_time <= dt:
            answer.append(df)
        else:
            answer.append(df + math.ceil((total_time - dt) / ut) * uf)

    return answer


"""
Definition of the main component of the training of the networks, the `TrainingLoop` class.
"""

import gc
from random import choice
from numpy import linspace
from torch.utils.data import DataLoader, random_split
from torchvision import transforms
from torchsummary import summary
from settings import (
    SHOW_NETWORK_SUMMARY, PRINT_BATCH_EVERY_X_ITERATIONS, BASE_DIM,
    SAVE_EVERY_X_ITERATIONS, RESOLUTIONS, RES_BATCH_SIZE, ALPHA_STEPS,
    N_SAMPLES_RES, COLLECT_GARBAGE_EVERY_X_ITERATIONS,
    WRITE_BATCH_EVERY_X_ITERATIONS, SUMMARIES_DIR, DATASET_CHANNELS
)
from training.train_batch import train_batch
from training.save import save_checkpoint
from device import device
from mywriter import MyWriter, TrainingBatchInfo

class Resolution:
    """
    Contains information about a training resolution. Used by `TrainingLoop`.

    Attributes
    ----------
    res: int
        Numerical resolution
    resolution_step: int
        Number of batches that have trained the networks during this resolution
    save_every_x_iterations: int
        Frequency of saving a checkpoint. It varies depending on the numerical resolution.
    batch_size: int
        Size of the batches used to train the networks. It varies depending on
        the numerical resolution.
    start_alpha: float
        Starting point for the alpha steps on this resolution. It's zero if the
        networks have not been trained on this resolution.
    alpha_steps_completed: int
        Number of discrete alpha steps completed on the training of this resolution
        so far.

    Methods
    -------
    get_dataset_transform()
        Returns a dataset transform that among other things, resizes images to `res`.
    """
    def __init__(self, res, init_training_vars=None):
        """
        Parameters
        ----------
        res: int
            Numerical resolution
        init_training_vars: dict, optional
            Dict containing variables used to initialize attributes. If its None
            then certain class attributes will be initialized to a default value.

        Raises
        ------
        ValueError
            When the starting resolution specified in init_training_vars is not in
            the `RESOLUTIONS` list.
        """
        if res in RESOLUTIONS:
            self.res = res
            self.resolution_step = 0
            self.save_every_x_iterations = SAVE_EVERY_X_ITERATIONS[str(self.res)]
            self.batch_size = RES_BATCH_SIZE[str(self.res)]
            if init_training_vars:
                self.start_alpha = init_training_vars['start_alpha']
                self.alpha_steps_completed = init_training_vars['alpha_steps_completed']
            else:
                self.start_alpha = 0.001
                self.alpha_steps_completed = 0
        else:
            raise ValueError('Resolution not valid. See settings.')

    def get_dataset_transform(self):
        """
        Returns a dataset transform that among other things, resizes tensor images to `res`.

        Returns
        -------
        transform
            A dataset transform to resize tensor images to `res`.
        """
        transform = transforms.Compose([
            transforms.ToTensor(),
            transforms.Resize(self.res),  # Resize to the same size
            transforms.CenterCrop(self.res),  # Crop to get square area
            transforms.RandomHorizontalFlip(),  # Increase number of samples
            transforms.Normalize([0.5] * DATASET_CHANNELS,
                                 [0.5] * DATASET_CHANNELS,)])
        return transform

class TrainingLoop:
    """
    Starts or continues a training session.

    The training scheme used here has three main loops. First is the progressive
    growing of the training images, second is the alpha used for the smooth introduction
    of blocks that goes from 0 to 1 and third is the dataloader that iterates through
    batches.

    If settings.py specify that a checkpoint is to be used as a starting point
    then the training will continue from that point.

    Many settings that control the behavior of the training and thus of this class
    are defined in settings.py. Some of behaviors are described further down this section.

    This training uses a progressive growing approach, meaning that after training the
    networks on a small initial resolution, the resolution of the training images will
    increase after certain number of batches. The resolutions the training is going to
    train on is defined on `RESOLUTIONS`.

    A new training id is assigned to the training session if the session is not
    initialized from a checkpoint, the training id retreived from the checkpoint is used
    if otherwise.

    Attributes
    ----------
    stgan: StyleGAN
        The generator network.
    disc: Discriminator
        The discriminator network.
    g_optim: Optimizer
        The optimizer of the generator.
    d_optim: Optimizer
        The optimizer of the discriminator.
    dataset: Dataset
        The training dataset.
    dataset_split:
        A split of the dataset into various subsets.
    run_id: string
        The id of the training.
    start_res: int
        The resolution where the training will begin.
    summary_dir: string
        A directory were tensorboard summaries will be written.
    global_step: int
        Number of the current training batch.
    log_filename_suffix: string
        A suffix for the summary name.
    writer: MyWriter
        A helper class to easily write information to the board.
    write_batch_every_x_iterations: int
        Variable to control how often information is written to the board.
    resolutions_list: list
        The resolutions (in order) the networks will be trained on. The first
        resolution not always is the first resolution in `RESOLUTIONS`.
    res_idx: int
        The id (based on `resolutions_list`) of the current resolution.
    resolution: Resolution
        A helper class that contains information about a training resolution.

    Methods
    -------
    start()
        Starts the training process.
    finish()
        Finalizes the training process.
    """
    def __init__(self, init_training_vars, stgan, disc, g_optim, d_optim, dataset):
        """
        Parameters
        ----------
        init_training_vars: dict
            Dict containing necessary variables to start or restart the training.
        stgan: StyleGAN
            Instance of the generator.
        disc: Discriminator
            Discriminator network.
        g_optim: Optimizer
            Optimizer for the generator.
        d_optim: Optimizer
            Optimizer for the discriminator.
        dataset: Dataset
            Training dataset.

        Raises
        ------
        ValueError
            When the starting resolution specified in init_training_vars is not in
            the `RESOLUTIONS` list.
        """
        self.stgan = stgan
        self.disc = disc
        self.g_optim = g_optim
        self.d_optim = d_optim
        self.dataset = dataset
        self.dataset_split = self._split_dataset()
        self.stgan.train()
        self.disc.train()
        self.run_id = init_training_vars['run_id']
        self.start_res = init_training_vars['start_res']
        self.summary_dir = '{}/{}'.format(SUMMARIES_DIR, self.run_id)
        self.global_step = init_training_vars['global_step']
        self.log_filename_suffix = init_training_vars['log_filename_suffix']
        self.writer = MyWriter(
            self.summary_dir, self.log_filename_suffix, self.disc, self.stgan
        )
        if self.start_res not in RESOLUTIONS:
            raise ValueError('Resolution not valid. See settings.')
        self.write_batch_every_x_iterations = PRINT_BATCH_EVERY_X_ITERATIONS * 2
        self.resolutions_list = self._get_resolutions_list()
        self.res_idx = 0
        self.resolution = Resolution(self.start_res, init_training_vars)

    def start(self):
        """
        Starts the training process.

        During the execution of this function, information about the batches is printed
        to the standard output as well as written to a tensorboard instance; to access said
        isntance is necessary to open a web browser and go to the specified location and port.

        The constant `COLLECT_GARBAGE_EVERY_X_ITERATIONS` controls the frequency of garbage
        collection.

        Checkpoints are saved every certain number of batches and it varies depending on the
        current resolution of the training, the constant that controls it is the dict
        `SAVE_EVERY_X_ITERATIONS`.

        """
        if SHOW_NETWORK_SUMMARY:
            self._show_network_summary()
        while self.res_idx < len(self.resolutions_list):
            print(f'Beginning of res: {self.resolution.res}')
            self.dataset.transform = self.resolution.get_dataset_transform()
            self.disc.start_at_resolution = self.resolution.res
            self.stgan.stop_at_resolution = self.resolution.res
            alpha_steps_left = ALPHA_STEPS - self.resolution.alpha_steps_completed
            for alpha in linspace(self.resolution.start_alpha, 1, num=alpha_steps_left):
                self.stgan.alpha = alpha
                self.disc.alpha = alpha
                dataset_chunk = choice(self.dataset_split)
                batch_size = min(len(dataset_chunk), self.resolution.batch_size)
                dataloader = DataLoader(
                    dataset_chunk,
                    batch_size=batch_size,
                    shuffle=True
                )
                for real_images in dataloader:
                    if self.global_step % COLLECT_GARBAGE_EVERY_X_ITERATIONS:
                        gc.collect()  # collect garbage
                    real_images = real_images[0]  # excluding labels
                    real_images = real_images.to(device)
                    train_batch_result = train_batch(
                        self.stgan,
                        self.disc,
                        self.g_optim,
                        self.d_optim,
                        real_images
                    )
                    fake_images, gen_loss, disc_loss, real_pred, fake_pred = train_batch_result
                    training_batch_info = TrainingBatchInfo(
                        fake_images,
                        real_images,
                        gen_loss,
                        disc_loss,
                        real_pred,
                        fake_pred,
                        self.global_step
                    )
                    if (
                        self.global_step %
                        self.resolution.save_every_x_iterations == 0
                    ) and self.resolution.resolution_step > 0:
                        save_checkpoint(
                            self.stgan.state_dict(),
                            self.disc.state_dict(),
                            self.g_optim.state_dict(),
                            self.d_optim.state_dict(),
                            self.resolution.res,
                            self.run_id,
                            self.global_step,
                            self.resolution.alpha_steps_completed
                        )
                    if self.global_step % PRINT_BATCH_EVERY_X_ITERATIONS == 0:
                        self._log_batch(alpha, training_batch_info)
                    if (
                        self.global_step % WRITE_BATCH_EVERY_X_ITERATIONS == 0 or
                        (
                            self.global_step %
                            self.resolution.save_every_x_iterations == 0 and
                            self.global_step > 0
                        )  # writing on save too
                    ):
                        self.writer.write_batch(training_batch_info)

                    self.global_step += 1
                    self.resolution.resolution_step += 1
                self.resolution.alpha_steps_completed += 1
            self._end_resolution()
        self.stop()

    def finish(self):
        self.writer.close()

    def _log_batch(self, alpha, training_batch_info):
        """
        Prints to the stdout information about the current batch defined in `training_batch_info`.

        Parameters
        ----------
        alpha: float
            The alpha step of the training.
        training_batch_info: TrainingBatchInfo
            An object containing information about the current batch.
        """
        print(
            'res: {}. alpha: {}. global batch: {}. gen loss: {}. disc loss: {}'
            .format(
                self.resolution.res,
                alpha,
                self.global_step,
                training_batch_info.gen_loss.mean(),
                training_batch_info.disc_loss.mean()
            )
        )

    def _end_resolution(self):
        """
        Ends the training process for the current resolution.

        If a larger resolution is defined in `RESOLUTIONS`, then it prepares to start training
        the networks with image of that resolution.
        """
        print(f'End of res {self.resolution.res}')
        self.res_idx += 1
        if self.res_idx < len(self.resolutions_list):
            next_res = self.resolutions_list[self.res_idx]
            self.resolution = Resolution(next_res)

    def _show_network_summary(self):
        """
        Shows a summary of the networks.

        Prints a dissection of the two main networks, showing its layers, activations,
        number of trainable parameters, etc.
        """
        self.disc.start_at_resolution = 8  # needed for the summary
        print('--- Discriminator Summary ---')
        print(summary(self.disc, input_size=(3, 8, 8)))
        print('--- Stylegan Summary ---')
        print(summary(self.stgan, input_size=[(BASE_DIM,), (BASE_DIM,)]))

    def _split_dataset(self):
        """
        Divides `dataset` into `split_dataset_in` parts. All of them are of equal
        size except for the last one, which is equal or larger in size than the rest.

        Returns
        -------
        list-like
            A random split of the dataset.
        """
        lendataset = len(self.dataset)
        split_dataset_in = ALPHA_STEPS * lendataset // N_SAMPLES_RES
        dataset_split_lengths = [lendataset // split_dataset_in] * (split_dataset_in - 1)
        dataset_split_lengths.append(lendataset - sum(dataset_split_lengths))
        dataset_split = random_split(self.dataset, dataset_split_lengths)
        print(
            '''Dataset splat in chunks of len: {}.
                This split is controled by the constants:
                ALPHA_STEPS and N_SAMPLES_RES
            '''.format(' '.join(map(lambda x: str(len(x)), dataset_split)))
        )
        return dataset_split

    def _get_resolutions_list(self):
        """
        Returns the ordered resolutions the training session is going to go through.

        If a training session is continued from a checkpoint then the starting resolution
        could be any of the valid resolutions defined in `RESOLUTIONS` (see settings.py).

        Returns
        -------
        list
            A list of valid resolutions that are greater or equal to `start_res`.
        """
        new_resolutions = list()
        for res in RESOLUTIONS:
            if res >= self.start_res:
                new_resolutions.append(res)
        return new_resolutions


from setuptools import setup, find_packages

with open("README.md", encoding="utf-8") as readme_file:
    readme = readme_file.read()

with open("requirements.txt", encoding="utf-8") as requirements_file:
    requirements = [_.strip() for _ in requirements_file.readlines()]

setup(
    name="stx",
    version="0.1.0",
    description="StreamXfer is a powerful tool for streaming data from SQL Server to object storage for seamless transfer using UNIX pipe, supporting various general data formats(CSV, TSV, JSON).",
    long_description=readme,
    packages=find_packages(),
    include_package_data=True,
    install_requires=requirements,
    python_requires=">=3.9",
    entry_points={
        "console_scripts": [
            "stx = streamxfer.cli.main:cli",
            "stx-mssql-csv-escape = streamxfer.cli.mssql_csv_escape:cli",
            "stx-mssql-json-escape = streamxfer.cli.mssql_json_escape:cli",
            "stx-redshift-escape = streamxfer.cli.redshift_escape:cli",
        ],
    },
)


"""empty message

Revision ID: ebab1388de9d
Revises: 11bd9e5e267e
Create Date: 2019-06-13 18:05:04.408172

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'ebab1388de9d'
down_revision = '11bd9e5e267e'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('map_register', sa.Column('modified_by', sa.String(), nullable=True))
    op.create_foreign_key(None, 'map_register', 'users', ['modified_by'], ['id'])
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'map_register', type_='foreignkey')
    op.drop_column('map_register', 'modified_by')
    # ### end Alembic commands ###


import pprint as pr
import numpy as np

class PLA:

    def __init__(self, filename, dimensions, numIterations):
        self.data = self.parse(filename)
        self.weights = np.zeros(dimensions + 1)
        for _ in range(numIterations):
            self.learn()

        print(self.checkAccuracy())
        pr.pp(self.weights)

    def checkAccuracy(self):
        length = len(self.data)
        correct = 0
        for row in self.data:
            dotP = np.dot(self.weights, row["point"])
            val = 1 if dotP > 0 else -1
            if val == row["label"]:
                correct += 1
        accuracy = float(correct)/length
        return accuracy

    def parse(self, filename):
        f = open(filename, 'r')
        fl = f.readlines()
        data = []
        for line in fl:
            d = {}
            l = line.strip().split(",")
            d["point"] = np.array([float(1.0) ,float(l[0]), float(l[1]), float(l[2])])
            d["label"] = int(l[3])
            data.append(d)
        return data

    def learn(self):
        for d in self.data:
            point = d["point"]
            label = d["label"]
            dotP = np.dot(self.weights, point)
            if dotP <= 0 and label == 1:
                self.weights = np.add(self.weights, point)
            elif dotP > 0 and label == -1:
                self.weights = np.subtract(self.weights, point)

pla = PLA("classification.txt", 3, 20)


from __future__ import absolute_import

import os
import io
import re
import ast
import posixpath

from os.path import getmtime, isfile
from itertools import chain
from collections import defaultdict

from acrylamid.templates import AbstractEnvironment, AbstractTemplate
from mako.lookup import TemplateLookup
from mako import exceptions, runtime

try:
    from acrylamid.assets.web import Mixin
except ImportError:
    from acrylamid.assets.fallback import Mixin

class CallVisitor(ast.NodeVisitor):

    def __init__(self, callback):
        self.callback = callback
        super(CallVisitor, self).__init__()

    def visit_Call(self, node):
        if isinstance(node.func, ast.Name):
            self.callback(node)

def unast(node):
    if isinstance(node, ast.Str):
        return node.s
    elif isinstance(node, ast.List):
        return [unast(item) for item in node.elts]
    raise NotImplementedError(node)

def find_assets(tt):
    """
    Parse AST from Mako template and yield *args, **kwargs from any
    `compile` call.
    """
    rv = []

    def collect(node):
        if node.func.id != "compile":
            return

        args = list(unast(x) for x in node.args)
        kwargs = dict((x.arg, unast(x.value)) for x in node.keywords)

        rv.append((args, kwargs))

    CallVisitor(collect).visit(ast.parse(tt.code))

    for args, kwargs in rv:
        yield args, kwargs

class ExtendedLookup(TemplateLookup):
    """
    Custom Mako template lookup that records dependencies, mtime and referenced
    web assets.
    """

    inherits = re.compile(r'<%inherit file="([^"]+)" />')
    includes = re.compile(r'<%namespace file="([^"]+)" import="[^"]+" />')

    def __init__(self, *args, **kwargs):
        # remember already resolved templates -> modified state
        # TODO don't assume macros.html never changes
        self.modified = {'macros.html': False}

        # requested template -> parents as flat list
        self.resolved = defaultdict(set)

        # assets in the form of theme/base.html -> (*args, **kwargs)
        self.assets = defaultdict(list)

        super(ExtendedLookup, self).__init__(*args, **kwargs)

    def get_template(self, uri):
        """This is stolen and truncated from mako.lookup:TemplateLookup."""

        u = re.sub(r'^/+', '', uri)
        for dir in self.directories:
            filename = posixpath.normpath(posixpath.join(dir, u))
            if os.path.isfile(filename):
                return self._load(filename, uri)
        else:
            raise exceptions.TopLevelLookupException(
                                "Cant locate template for uri %r" % uri)

    def _load(self, filename, uri):

        deps = [uri, ]
        while len(deps) > 0:

            child = deps.pop()
            if child in self.modified:
                continue

            for directory in self.directories:
                filename = posixpath.normpath(posixpath.join(directory, child))
                if isfile(filename):
                    break

            p = self.modulename_callable(filename, child)

            try:
                modified = getmtime(filename) > getmtime(p)
            except OSError:
                modified = True

            self.modified[child] = modified

            with io.open(filename, encoding='utf-8') as fp:
                source = fp.read()

            parents = chain(self.inherits.finditer(source), self.includes.finditer(source))
            for match in parents:
                self.resolved[child].add(match.group(1))
                deps.append(match.group(1))

            # TODO: definitely an ugly way (= side effect) to get the byte code
            tt = super(ExtendedLookup, self)._load(filename, child)
            for args, kwargs in find_assets(tt):
                self.assets[uri].append((args, kwargs))

        # already cached due side effect above
        return self._collection[uri]

class Environment(AbstractEnvironment):

    extension = ['.html', '.mako']

    def __init__(self, layoutdirs, cachedir):
        self._mako = ExtendedLookup(
            directories=layoutdirs,
            module_directory=cachedir,
            # similar to mako.template.Template.__init__ but with
            # leading cache_ for the acrylamid cache
            modulename_callable=lambda filename, uri:\
                os.path.join(os.path.abspath(cachedir), 'cache_' +
                    os.path.normpath(uri.lstrip('/')) + '.py'),
            input_encoding='utf-8')

        self.filters = {}

    def register(self, name, func):
        self.filters[name] = func

    def fromfile(self, env, path):
        return Template(env, path, self._mako.get_template(path))

    def extend(self, path):
        self._mako.directories.append(path)

    @property
    def loader(self):
        return self._mako

class Template(AbstractTemplate, Mixin):

    def render(self, **kw):
        # we inject the filter functions as top-level objects into the template,
        # that's probably the only way that works with Mako
        kw.update(self.engine.filters)
        buf = io.StringIO()
        ctx = runtime.Context(buf, **kw)
        self.template.render_context(ctx)
        return buf
        # For debugging template compilation:
        # TODO: Integrate this with acrylamid somehow
        #from mako import exceptions as mako_exceptions
        #try:
        #    return self.template.render(**kw)
        #except:
        #    print mako_exceptions.text_error_template().render()
        #    return unicode(mako_exceptions.html_error_template().render())


import wx
import cv2
import numpy as np
from ImageViewPanel import *
from IdentifiedPoint import *

#Panel for a given orthographic perspective of the body
class BodyViewPanel(ImageViewPanel):
    def __init__(self, bodyView, parent):
        image = bodyView.get_smoothed_image()
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        ImageViewPanel.__init__(self, image, parent)

        self.bodyView = bodyView
        name = bodyView.getName()

    #Given an identified 3d point, try to add a projected
    #2d version of the point, and update the display
    def tryAddProjectedPoint(self, idedPoint):
        point = idedPoint.getPoint()
        newPoint = self.bodyView.get_smoothed_image_pos(point)
        if (newPoint is None):
            return
        #Link the id
        newIdedPoint = idedPoint.createLinked(newPoint)
        #Add and refresh
        self.addPoint(newIdedPoint)

    #Given a 2d [x, y] point in image index space,
    #returns the 3d [x, y, z] world coordinate it corresponds to
    #or None if not present
    def get_world_pos(self, point):
        return self.bodyView.get_smoothed_world_pos(point)


from __future__ import absolute_import, division, print_function
import threading
from six.moves import cPickle as pickle
from . import SocketServer
import sys
import traceback
import os
import socket
import time

from . import FileClient
from . import FileSocket

# Socket Server

class FileServer:
  def __init__(self, port='', handler=''):
    sys.setcheckinterval(0)

    local = socket.getfqdn()
    server_address = (local, port)

    if handler:
      ddbs = _FileServer(server_address, handler)
    else:
      ddbs = _FileServer(server_address, FileRequestHandler)

    # now serve requests forever
    ddbs.keep_alive = 1
    while ddbs.keep_alive:
      ddbs.handle_request()

class _FileServer(SocketServer.ThreadingTCPServer):

  def server_bind(self):
    """Override server_bind to store the server name."""
    SocketServer.ThreadingTCPServer.server_bind(self)
    host, port = self.socket.getsockname()
    if not host or host == '0.0.0.0':
      host = socket.getfqdn()

##############################################################################
# Attributes accessable using self.server.server_name for example
##############################################################################
    self.server_name = host
    self.server_port = port
    self.main_thread = threading.enumerate()[0]
    self.ServerLock = threading.Lock()
    self.pid = os.getpid()

    self.FileLockDictionary = {}

class FileRequestHandler(SocketServer.StreamRequestHandler):

#------------------------------------------------------------------------------
# File functions
#------------------------------------------------------------------------------

  def _LockFile(self, file, id):
    DICT = self.server.FileLockDictionary
    rc = 1
    self.server.ServerLock.acquire()
    try:
      if id in DICT.keys():
        rc = 0
      if file in DICT.values():
        rc = 0
      if rc:
        DICT[id] = file
    finally:
      self.server.ServerLock.release()
    return rc

  def LockFile(self, file, id):
    rc = self._LockFile(file, id)
    while not rc:
      time.sleep(.01)
      rc = self._LockFile(file, id)

  def ReadPickleFile(self, file):
    try:
      f = open(file, 'rb')
      obj = pickle.load(f)
      f.close()
    except Exception:
      print('failed to load',file)
      obj = None
    return obj

  def WritePickleFile(self, obj, file):
    try:
      f = open(file, 'wb')
      pickle.dump(obj, f)
      f.close()
    except Exception:
      print('failed to dump',file)

  def ReadFile(self, file):
    try:
      f = open(file, 'rb')
      lines = f.read()
      f.close()
    except Exception:
      print('failed to read',file)
      lines = ''
    return lines

  def WriteFile(self, lines, file):
    try:
      f = open(file, 'wb')
      f.write(lines)
      f.close()
    except Exception:
      print('failed to write',file)

  def _UnlockFile(self, file, id):
    DICT = self.server.FileLockDictionary
    rc = 1
    self.server.ServerLock.acquire()
    try:
      if not id in DICT.keys():
        rc = 0
      if not file in DICT.values():
        rc = 0
      if rc:
        del DICT[id]
    finally:
      self.server.ServerLock.release()
    return rc

  def UnlockFile(self, file, id):
    rc = self._UnlockFile(file, id)
    while not rc:
      time.sleep(.01)
      rc = self._UnlockFile(file, id)

  def FileExists(self, file):
    return os.path.exists(file)

#------------------------------------------------------------------------------
# Server information
#------------------------------------------------------------------------------
  def GetServerDetails(self):
    #user = self.GetUser()
    server_sc = self.GetSocketConnection()
    #return user, server_sc
    return server_sc

  #def GetUser(self):
  #  return GetUser()

  def GetSocketConnection(self):
    """
    Returns the socket connection data
    """
    s = FileSocket.SocketConnection(self.server.server_name,
                                      os.getcwd(),
                                      self.server.server_port,
                                      os.getpid()
                                      )
    return s

  def GetHost(self):
    return self.server.server_name

  def GetPort(self):
    return self.server.server_port

#------------------------------------------------------------------------------
# Client tester
#------------------------------------------------------------------------------
  def _tester(self, host, port):
    client = FileClient.FileClient(host = host,
                                     port = port)
    try:
      client.tester()
      self.event.set()
    except Exception:
      pass

  def ClientTest(self, host, port, timeout=2):
    self.event = threading.Event()
    self.event.clear()

    t = threading.Thread(target=self._tester,
                         args=(host,port))
    t.setDaemon(1)
    t.start()

    self.event.wait(timeout) # timed wait may be too long

    if self.event.isSet():
      return 1
    return None

#------------------------------------------------------------------------------
# Misc
#------------------------------------------------------------------------------
  def getpid(self):
    return os.getpid()

  def shutdown(self):
    self.server.ServerLock.acquire()
    try:
      self.server.keep_alive = 0
    finally:
      self.server.ServerLock.release()

  def tester(self):
    """
    Called by client to test if server is alive
    """
    return " Hello from\n\t%s\n on %s %s" \
           % (str(self), str(self.server.server_name), sys.platform)

#------------------------------------------------------------------------------
# handler
#------------------------------------------------------------------------------
  def handle(self):
    """
    This method handles the arguments of a remote call
    """
    try:

      while self.server.keep_alive: # still alive?
        # get method name from client
        try:
          method = pickle.load(self.rfile) # use pickle to avoid blocking
        except Exception:
          #print 'failed to load method'
          #self.wfile.flush() #doesn't fixed socket problem!!!!!!!!!!
          break
        #except EOFError, socket.error: # socket closed?
        #  break

        if method == 'shutdown':
          self.server.keep_alive = 0

        # get arguments from client
        try:
          args = pickle.load(self.rfile)
        except Exception:
          args = ()
          print('Method',method)
          print('error in args ',args)
        try:
          kw = pickle.load(self.rfile)
        except Exception:
          kw = {}
          print('error in kw ',kw)

        #print method
        #print args
        #print kw

        # get method pointer
        meth_obj = getattr(self,method)
        #print meth_obj

        # call method
        try:
          result = meth_obj(*args, **kw)
          pickle.dump(result,self.wfile,1) # binary by default
        except Exception:
          try:
            pickle.dump('0\n',self.wfile,1)
          except Exception:
            pass
          print('Method',meth_obj)
          print('Args  ',args)
          print('Kw    ',kw)
          raise
        try:
          self.wfile.flush()
        except Exception:
          pass

    except Exception:
      print(self.__class__.__name__+'.handle: ')
      traceback.print_exc()
      pass

    sys.stdout.flush()

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

def StartServer():
  s = FileSocket.GetSocket()
  port = (s.getsockname()[1])
  t = threading.Thread(target=FileServer,
                       args=(port,))
  #t.setDaemon(1)
  return (t,port)

def BindClient(host, port):
  nobind = 1
  while nobind:
    try:
      client = FileClient.FileClient(host = host,
                                     port = port
                                     )
      client.tester()
      nobind = 0
    except Exception:
      time.sleep(.2)
      nobind += 1
      if nobind > 10:
        return None

  return client

def ReadServerFile():
  host = None
  port = None
  try:
    f = open('server.port', 'rb')
    host, port = pickle.load(f)
    f.close()
  except Exception:
    print('failed to read server port file')
  return host, port

def WriteServerFile(port):
  try:
    f = open('server.port', 'wb')
    pickle.dump((socket.getfqdn(),
                 port),
                f
                )
    f.close()
  except Exception:
    print('failed to write server port file')

def GetServerClient():
  host, port = ReadServerFile()
  client = None
  if host:
    client = BindClient(host, port)

  return client


import sys
from collections import Counter

def dist(a, b):
    return abs(a[0] - b[0]) + abs(a[1] - b[1])

def parse(text):
    res = dict()
    for row in text.strip().split('\n'):
        p = row.split()
        sensor = int(p[2][2:-1]), int(p[3][2:-1])
        beacon = int(p[-2][2:-1]), int(p[-1][2:])
        res[sensor] = dist(sensor, beacon)
    return res

def part1(by_radius, max_side=4_000_000):
    intervals = []
    for (x, y), radius in by_radius.items():
        arm = radius - abs(max_side // 2 - y)
        if arm < 0:
            continue
        intervals.append((x - arm, x + arm))
    intervals.sort()
    return max(right for _, right in intervals) - intervals[0][0]

def part2(by_radius, max_side=4_000_000):
    positive, negative = Counter(), Counter()
    for ((x, y), r) in by_radius.items():
        positive[y - x + r + 1] += 1
        positive[y - x - r - 1] += 1
        negative[y + x + r + 1] += 1
        negative[y + x - r - 1] += 1
    positive, negative = [set(k for k, v in d.items() if v > 1) for d in [positive, negative]]
    for a in positive:
        for b in negative:
            pos = (b - a) // 2, (a + b) // 2
            if all(0 < c < max_side for c in pos):
                if all(dist(pos, scanner) > radius for scanner, radius in by_radius.items()):
                    return 4_000_000 * pos[0] + pos[1]

text = open(sys.argv[-1], 'r').read()
parsed = parse(text)
print(part1(parsed))
print(part2(parsed))


__author__ = 'yoyomyo'

import pdb

class HoughArray:

    def __init__(self, neighborhood):
        self.data = []  # data points to vote for
        self.vote = []  # vote count for each data
        self.size = 0   # number of data
        self.neighborhood = neighborhood  #a point may fall into the neighborhood around a data entry

    def add(self, toAdd):
        # if toAdd already belongs to an entry's neighborhood,
        # increment the vote of that entry
        # else create data's own entry and insert it
        if self.size == 0:
            self.data.append(toAdd)
            self.vote.append(1)
            self.size = 1
        else:
            # self.__add_helper__(toAdd, 0, self.size-1)

            # find the data entry closest to toAdd
            diffs = [abs(toAdd-x) for x in self.data]
            min_diff, min_idx = min((val, idx) for (idx, val) in enumerate(diffs))

            if min_diff < self.neighborhood:
                self.vote[min_idx] += 1
            else:
                # create a new data entry
                self.data.append(toAdd)
                self.vote.append(1)
                self.size += 1

    def get_high_votes(self):
        # return a subset of data that received a lot of votes
        # the threshold is 0.5 * (total vote/size)
        total_vote = sum(self.vote)
        filter_result =  filter(lambda x: x[0] >= 0.5* total_vote / (self.size+0.0), zip(self.vote, self.data))
        return map(lambda x: x[1], filter_result)

# ha = HoughArray(10)
# ha.__add__(20)
# ha.__add__(15)
# ha.__add__(30)
# ha.__add__(50)
# pdb.set_trace()


import  numpy as np
import pandas as pd
from UsefulFunction import cleandata
from nltk.corpus import stopwords
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import GridSearchCV
from  UsefulFunction import cleandata,  tokenizer_porter, tokenizer

if __name__ == '__main__':
    df = pd.read_csv('movie_data.csv', encoding="ISO-8859-1")
    # print(df.head(3))
    df['review'] = df['review'].apply(cleandata)

    X_train = df.loc[:25000, 'review'].values
    y_train = df.loc[:25000, 'sentiment'].values
    X_test = df.loc[25000:, 'review'].values
    y_test = df.loc[25000:, 'sentiment'].values

    stop = stopwords.words('english')

    tfidf = TfidfVectorizer(strip_accents=None,
                            lowercase=False,
                            preprocessor=None)

    param_grid = [{'vect__ngram_range': [(1, 1)],
                   'vect__stop_words': [stop, None],
                   'vect__tokenizer': [tokenizer, tokenizer_porter],
                   'clf__penalty': ['l1', 'l2'],
                   'clf__C': [1.0, 10.0, 100.0]},
                  {'vect__ngram_range': [(1, 1)],
                   'vect__stop_words': [stop, None],
                   'vect__tokenizer': [tokenizer, tokenizer_porter],
                   'vect__use_idf':[False],
                   'vect__norm':[None],
                   'clf__penalty': ['l1', 'l2'],
                   'clf__C': [1.0, 10.0, 100.0]},
                  ]

    lr_tfidf = Pipeline([('vect', tfidf),
                         ('clf', LogisticRegression(random_state=0))])

    gs_lr_tfidf = GridSearchCV(lr_tfidf, param_grid,
                               scoring='accuracy',
                               cv=5,
                               verbose=1,
                               n_jobs=-1)

    gs_lr_tfidf.fit(X_train, y_train)

    print('Best parameter set: %s ' % gs_lr_tfidf.best_params_)
    print('CV Accuracy: %.3f' % gs_lr_tfidf.best_score_)

    clf = gs_lr_tfidf.best_estimator_
    print('Test Accuracy: %.3f' % clf.score(X_test, y_test))


from rest_framework import serializers
from .models import Categoria

class CategoriaSerializer(serializers.Serializer):
	pk = serializers.IntegerField(read_only=True)
	categoria = serializers.CharField()
	descripcion = serializers.CharField()

	def create(self, validated_data):
		return Categoria.objects.create(**validated_data)

	def update (self, instance, validated_data):
		instance.categoria = validated_data.get('categoria', instance.categoria)
		instance.descripcion = validated_data.get('descripcion', instance.descripcion)
		instance.save()
		return instance

class CategoriaModelSerializer(serializers.ModelSerializer):
	class Meta:
		model = Categoria
		fields = ('id','categoria','descripcion')


#data_preprocessing
import pandas as pd
import numpy as np

df = pd.read_csv("BTC_input.csv", parse_dates=["trade_date"])
nupl = pd.read_json(
    "net-unrealized-profit-loss-nupl-btc-24h.json", orient='records')

nupl["t"] = pd.to_datetime(nupl["t"], infer_datetime_format=True)

df2 = pd.merge(df, nupl, left_on="trade_date", right_on='t')
df2 = df2.drop(["BTC_price_change_1_day", "t", "days", "Day",
                "months", "weeks", "years", "Month", "Week", "Year", "DIndex", "GL_USDx100"], axis=1)
df = df2.rename(columns={"trade_date": "date",
                         "USD_price_change_1_day": "dprice", "v": "nupl"})
print(df)
df.to_csv("data.csv")


import numpy as np
import os, scipy
import scipy.io
from scipy import interpolate
###############################################
############# Wave Data Import ################
###############################################
try:
    X_wave = np.array(scipy.io.loadmat(os.getcwd()+"\\"+"Environment"+"\\"+"Wave_Data"+"\\" +"DX_SALV_REV1.mat")['DX'])
    Y_wave = np.array(scipy.io.loadmat(os.getcwd()+"\\"+"Environment"+"\\"+"Wave_Data"+"\\" +"DY_SALV_REV1.mat")['DY'])
    N_wave = np.array(scipy.io.loadmat(os.getcwd()+"\\"+"Environment"+"\\"+"Wave_Data"+"\\" +"DN_SALV_REV1.mat")['DN'])
except:
    X_wave = np.array(scipy.io.loadmat(os.getcwd()+"\\"+"Wave_Data"+"\\" +"DX_SALV_REV1.mat")['DX'])
    Y_wave = np.array(scipy.io.loadmat(os.getcwd()+"\\"+"Wave_Data"+"\\" +"DY_SALV_REV1.mat")['DY'])
    N_wave = np.array(scipy.io.loadmat(os.getcwd()+"\\"+"Wave_Data"+"\\" +"DN_SALV_REV1.mat")['DN'])

###############################################
############### MMG Model Group ###############
###############################################
global rps_m, delta_m

def MMG_Time_Derivative(u,v,r,x,y,psi,delta,rps,WHA,w_flag):
    """
    ----------
    ip : current state of the ship [u,v,r,x,y,psi]
    delta : commanded rudder angle
    rpm : commanded propeller speed

    Returns
    -------
    Time derivative of ship state[u_dot,v_dot,r_dot,U,V,r]

    """
    ### Assertion ###
    if u == 0 or u < 0:
        u = 0.00001
    if rps == 0 or rps < 0:
        rps = 1
    ### ###
    U             = np.sqrt((u**2)+(v**2))   # Resultant speed
    beta          = np.arctan(-v/u)          # Hull drift angle at midship
    Np            = rps                      #just for notation

    ################################################
    ###### Priniciple Particulars of the ship ########
    ################################################
    Lpp     = 2.902        # length between perpenticular
    d       = 0.189        # ship draft
    xG      = 0.102        # Longitudinal coordinate of center of gravity of ship
    rho     = 1025         # Water density
    Volume  = 0.235        # Displacement volume of ship
    Dp      = 0.090        # Propeller diameter
    HR      = 0.144        # Rudder span length
    AR      = 0.00928      # Profile area of movable part of mariner rudder

    ######################################################
    #### Mass, Added Mass and Added Moment of inertia ####
    ######################################################
    mx,my,jz = 0.022,0.223,0.011

    ### Non DiMensionalizing(ndm) parameters ###
    ndm_force   = 0.5*rho*Lpp*d*(U**2)
    ndm_moment  = 0.5*rho*(Lpp**2)*d*(U**2)
    ndm_mass    = 0.5*rho*d*(Lpp**2)
    ndm_massMoI = 0.5*rho*d*(Lpp**4)

    ### Dimensional mass ###
    mx = mx*ndm_mass
    my = my*ndm_mass
    jz = jz*ndm_massMoI
    m  = Volume*rho
    IzG = m * ((0.25*Lpp)**2) #Moment of inertia of ship around center of gravity
    #####################################################
    ####### Governing Equation (Equation 4) #############
    #####################################################

    ### Mass Matrix ### (u_dot,v_dot,r_dot)
    M = np.array([[(m+mx),0,0],[0,(m+my),m*xG],[0,m*xG,(jz+((xG**2)*m)+IzG)]])

    ### LHS ### (Other than u_dot,v_dot,r_dot) #which should be added on right side
    LHS_r = np.array([[-(m+my)*v*r-xG*m*(r**2)], [(m+mx)*u*r], [m*xG*u*r]])

    #####################################################
    ####### Hydrodynamic Force on Hull(F_hull)  #########
    #####################################################
    ### Non Dimensional Velocity ###
    v_ndm = v/U
    r_ndm = r*Lpp/U

    R0 = 0.022 # Ship resistance coefficient in straight moving

    ### Non Dimensional Hydrodynamic Derivatives ###
    X_vv, X_vr ,X_rr ,X_vvvv = -0.04, 0.002, 0.011, 0.771
    Y_v, Y_R, Y_vvv, Y_vvr, Y_vrr, Y_rrr = -0.315, 0.083, -1.607, 0.379, -0.391, 0.008
    N_v, N_R, N_vvv, N_vvr, N_vrr, N_rrr = -0.137, -0.049, -0.03, -0.294, 0.055, -0.013

    ### Force and Moment ###
    X_hull =  -R0 + (X_vv*(v_ndm**2)) + (X_vr*v_ndm*r_ndm) + (X_rr*(r_ndm**2)) + (X_vvvv*(v_ndm**4))

    Y_hull =  (Y_v*v_ndm) + (Y_R*r_ndm) + (Y_vvv*(v_ndm**3)) + (Y_vvr*(v_ndm**2)*r_ndm) + (Y_vrr*v_ndm*(r_ndm**2)) + (Y_rrr*(r_ndm**3))

    N_hull = (N_v*v_ndm) + (N_R*r_ndm) + (N_vvv*(v_ndm**3)) + (N_vvr*(v_ndm**2)*r_ndm) + (N_vrr*v_ndm*(r_ndm**2)) + (N_rrr*(r_ndm**3))

    ### Equation 6 ###
    X_hull = ndm_force * X_hull
    Y_hull = ndm_force * Y_hull
    N_hull = ndm_moment* N_hull

    ### Hull Force ###
    F_hull = np.array([[X_hull],[Y_hull],[N_hull]])

    #####################################################
    ##### Propellar Force on Ship(F_propellar)  #########
    #####################################################
    tp          = 0.220                    # Thrust deduction factor
    wp0         = 0.40                     # Effective wake in straight moving
    k0,k1,k2    = 0.2931, -0.2753, -0.1385 # 2nd order polynomial function coefficient
    xP          = -0.48                    # Longitudinal coordinate of propellerposition
    beta_P      = beta - (xP * r_ndm)      # geometrical inflow angle

    # C1,C2       = 2, 1.6 if beta_P > 0 else 1.2 # wake  change characteristic coefficents
    # wp          = (1 - wp0)* (1 + ((1 - np.exp(-C1*abs(beta_P)))*(C2 - 1)))
    #                                             Equation 16
    wp          =  wp0*np.exp(-4*(beta_P**2))   # Equation 12
    Jp          =  u*(1-wp)/(Np*Dp)             # Equation 11
    KT          =  k0 + k1*Jp + k2*(Jp**2)      # Equation 10
    T           =  rho*(Np**2)*(Dp**4)*KT       # Equation 9
    X_propellar =  (1 - tp)*T                   # Equation 8

    ### Propellar Force ###
    F_propellar = np.array([[X_propellar],[0],[0]])

    #####################################################
    ######### Rudder Force on Ship(F_rudder)  ###########
    #####################################################

    ### Equation 46 ###
    epsilon    = 1.09                   # Ratio of wake fraction
    k          = 0.5                    # An experimental constant forexpressing uR
    eta        = Dp/HR                  # Ratio of propeller diameter to rudderspan
    uP         = (1 - wp)*u             # propeller inflow velocity

    uR1   = np.sqrt(1 + ((8*KT)/(np.pi*(np.square(Jp)))))
    uR2   = np.square(1 + k*(uR1 -1))
    uR    = epsilon * uP * np.sqrt( (eta*uR2) + (1-eta))

    ### Equation 23 ###
    lR          = -0.710                        # Effective longitudinal coordinate
    beta_R      =  beta -(lR*r_ndm)             # Effective inflow angle to rudder in maneuvering motions
    gamma_r     = 0.396 if beta_R < 0 else 0.64 # Flow straightening coefficient

    vR    =  U * gamma_r * beta_R

    ### Equation 19 ###
    f_alpha     = 2.747                     # Rudder lift gradient coefficient
    alpha_R     = delta - np.arctan(vR/uR)  # Effective inflow angle to rudder
    Ur          = np.sqrt((uR**2)+(vR**2))  # Resultant inflow velocity to rudder

    F_normal = 0.5 * rho * AR * (Ur**2) * f_alpha * np.sin(alpha_R)

    ### Equation 18 ###
    tR         = 0.387                      # Steering resistance deduction factor
    aH         = 0.312                      # Rudder force increase factor
    xH         = -0.464   # -0.45* Lpp      # Longitudinal coordinate of acting point of the additional lateral force
    xR         = -0.5 * Lpp                 # Longitudinal coordinate of rudder position

    X_rudder =  -(1 - tR) * F_normal * np.sin(delta)
    Y_rudder =  -(1 + aH) * F_normal * np.cos(delta)
    N_rudder =  -(xR + (aH*xH)) * F_normal * np.cos(delta)

    ### Rudder Force ###
    F_rudder = np.array([[X_rudder],[Y_rudder],[N_rudder]])
    #####################################################
    ####### Wave Drift Force on Ship(F_Drift)  ##########
    #####################################################
    if w_flag == True:
        Lamda              = 320/Lpp
        FS_force           = Lamda**3
        FS_moment          = Lamda**4

        Amplitude          = 3.2
        wave_frequency     = 0.439*(Lamda)**0.5 #0.3584
        heading_angle      = WHA

        A1  =  np.real(X_wave[::,1:]) * (Amplitude**2)/2
        A2  = -np.real(Y_wave[::,1:]) * (Amplitude**2)/2
        A3  = -np.real(N_wave[::,1:]) * (Amplitude**2)/2

        wave_drift     = np.real(2*np.pi*X_wave[::,0])*(Lamda)**0.5
        hangle_drift   = -np.arange(-np.pi,np.pi+0.01,np.deg2rad(15))
        hangle_desire1  = heading_angle - psi

        hangle_desire = (hangle_desire1 + np.pi) % (2 * np.pi) - np.pi

        fA1 = interpolate.interp2d(hangle_drift, wave_drift,A1) # kind='cubic'(generally linear)
        fA2 = interpolate.interp2d(hangle_drift, wave_drift,A2) # kind='cubic'(generally linear)
        fA3 = interpolate.interp2d(hangle_drift, wave_drift,A3) # kind='cubic'(generally linear)

        X_Drift =  fA1(hangle_desire,wave_frequency)  / FS_force
        Y_Drift =   fA2(hangle_desire,wave_frequency) / FS_force
        N_Drift =   fA3(hangle_desire,wave_frequency) / FS_moment

        F_Drift = np.array([[X_Drift[0]],[Y_Drift[0]],[N_Drift[0]]])

    elif w_flag == False :
        F_Drift = np.array([[0.0],[0.0],[0.0]])
    #####################################################
    ##### Solution of Governing Equation (A.X = b)  #####
    #####################################################
    A_inverse       = np.linalg.inv(M)
    b               = F_hull + F_propellar + F_rudder - LHS_r + F_Drift

    ### u_dot,v_dot,r_dot ###
    X   = A_inverse.dot(b)
    #####################################################
    ############## Kinetics of the Ship   ###############
    #####################################################
    ### Rotation Matrix ###
    R = np.array( [[np.cos(psi), -np.sin(psi), 0], [np.sin(psi),  np.cos(psi), 0], [0, 0, 1]])
    ### Velocity and Moment ###
    Vel_Mom = R.dot(np.array([u,v,r]))

    ### time derivatives ###
    TD = [X[0].item(), X[1].item(), X[2].item(),Vel_Mom[0].item(), Vel_Mom[1].item(), Vel_Mom[2].item(),]

    return np.array(TD)

def activate(ip,delta_d,delta_rps,W_flag,WHA,RuPos,PropPos):
    """
    ----------
    ip      : current state of the ship [u,v,r,x,y,psi]
    RuPos   : Rudder Position
    rps_c   : propeller current speed
    delta_r : commanded rudder angle
    rps_r   : commanded propeller speed
    WHA     : Wave Head Angle
    W_Flag  : Wave Force Inclusion
    Returns
    -------
    Time derivative of ship state[u_dot,v_dot,r_dot,U,V,r], Rudder_Position, Propeller_speed

    """

    h = 1 # time step as increment
    u,v,r,x,y,psi = ip[0],ip[1],ip[2],ip[3],ip[4],ip[5]

    delta      = RuPos + delta_d
    if abs(delta) > np.deg2rad(35):
        delta    = np.sign(delta)*np.deg2rad(35) # Assertion of maximum rudder angle

    rps        = PropPos + delta_rps
    if rps < 5:
        rps = 5.0
    if rps > 25:
        rps = 25
    #########################################
    RuPos_new = delta
    rps_new   = rps
    ###############################
    ### RK4's K1,K2,K3,K4 Value ###
    ###############################
    K   = np.zeros(8)
    K0  = np.array([u,v,r,x,y,psi])

    K1_ = MMG_Time_Derivative(u,v,r,x,y,psi,delta,rps,WHA,W_flag)
    K1  = h*K1_[:3]
    k1  = h*K0[:3]

    K2_ = MMG_Time_Derivative(u+(K1[0]/2), v+(K1[1]/2), r+(K1[2]/2), x+(k1[0]/2), y+(k1[1]/2), psi+(k1[2]/2),delta,rps,WHA,W_flag)
    K2  = h*K2_[:3]
    k2  = h*(K0[:3] + 0.5*K1)

    K3_ = MMG_Time_Derivative(u+(K2[0]/2), v+(K2[1]/2), r+(K2[2]/2),x+(k2[0]/2), y+(k2[1]/2), psi+(k2[2]/2),delta,rps,WHA,W_flag)
    K3  = h*K3_[:3]
    k3  = h*(K0[:3] + 0.5*K2)

    K4_ = MMG_Time_Derivative(u+(K3[0]), v+(K3[1]), r+(K3[2]), x+(k3[0]), y+(k3[1]), psi+(k3[2]),delta,rps,WHA,W_flag)
    K4  = h*K4_[:3]
    k4  = h*(K0[:3] + 0.5*K3)

    K[:3]  =  (1/6) * (K1 + (2*K2) + (2*K3) + K4)

    R      = np.array( [[np.cos(psi), -np.sin(psi), 0], [np.sin(psi),  np.cos(psi), 0], [0, 0, 1]])
    del_k  =  ((1/6) * (k1 + (2*k2) + (2*k3) + k4)).reshape(3,1)
    K[3:6] = (R.dot(del_k)).reshape(1,3)

    nxt_state = np.array(ip) + K

    return nxt_state.tolist(), RuPos_new, rps_new

#####################################################
############# To check ##############################
#####################################################
# import matplotlib
# matplotlib.rc('xtick', labelsize=12)
# matplotlib.rc('ytick', labelsize=12)
# font = {'family' : 'normal',
#         'size'   : 12}
# matplotlib.rc('font', **font)
# import matplotlib.pyplot as plt

# Lpp   = 2.902
# rps   = 18
# N     = 100
# WHA   = np.pi
# W_F    =  False # True

# ip1         = [0.77,0,0,0,0,0,0,0]
# ip2         = [0.77,0,0,0,0,0,0,0]

# print(ip1)
# data   = list()
# data1  = list()
# RuPos  = np.deg2rad(0)
# RPS    = 25
# RuPos1 = -np.deg2rad(0)
# RPS1   = 25
# data.append(ip1)
# data1.append(ip2)

# x1,y1 = [ip1[2]],[ip1[3]]
# x2,y2 = [ip1[2]],[ip1[3]]
# RRR =[]
# delta = np.deg2rad(0)
# for i in range(N):
#     temp,RuPos_New,_ = activate(data[-1],-delta,0.0,W_F,WHA,0.0,RPS) # ip,delta_d,W_flag,WHA,RuPos,rps
#     RuPos           = RuPos_New
#     data.append(temp)
#     x1.append(temp[3]/Lpp)
#     y1.append(temp[4]/Lpp)

# for j in range(N):
#     #delta = np.random.rand()*np.deg2rad(20)
#     temp1,RuPos_New,_  = activate(data1[-1],delta,0.0,W_F,WHA,0.0,RPS)
#     RuPos1           = RuPos_New
#     data1.append(temp1)
#     x2.append(temp1[3]/Lpp)
#     y2.append(temp1[4]/Lpp)
#     RRR.append(temp1[0])

# plt.figure(figsize=(9,6))
# plt.plot(y1,x1,'green',linewidth=3,label = " Numerical Model ")
# plt.plot(y2,x2,'green',linewidth=3,)
# plt.scatter(0,0,marker="P",label = "starting point ")
# plt.xlabel("Transfer (x/L)")
# plt.ylabel("Advance (x/L)")
# plt.axhline(y=0,color="red",alpha=0.5)
# plt.title("L3 : KVLCC2 simulation for 120 seconds for n= 18.2 rps with u_0 = 0.78 m/s ")
# plt.legend(loc="best")
# plt.grid()
# plt.show()
# plt.plot(RRR)
# plt.axhline(y = 0.9)
# print(max(x1))
# print(max(y1))
######################################################
######################################################


def start(self, priority_queue, resource_queue):

        self._kill_event = threading.Event()
        self._priority_queue_pull_thread = threading.Thread(target=self._migrate_logs_to_internal,
                                                            args=(
                                                                priority_queue, 'priority', self._kill_event,)
                                                            )
        self._priority_queue_pull_thread.start()

        self._resource_queue_pull_thread = threading.Thread(target=self._migrate_logs_to_internal,
                                                            args=(
                                                                resource_queue, 'resource', self._kill_event,)
                                                            )
        self._resource_queue_pull_thread.start()

        """
        maintain a set to track the tasks that are already INSERTED into database
        to prevent race condition that the first resource message (indicate 'running' state)
        arrives before the first task message.
        If race condition happens, add to left_messages and operate them later

        """
        inserted_tasks = set()
        left_messages = {}

        while (not self._kill_event.is_set() or
               self.pending_priority_queue.qsize() != 0 or self.pending_resource_queue.qsize() != 0 or
               priority_queue.qsize() != 0 or resource_queue.qsize() != 0):

            """
            WORKFLOW_INFO and TASK_INFO messages

            """
            self.logger.debug("""Checking STOP conditions: {}, {}, {}, {}, {}""".format(
                              self._kill_event.is_set(),
                              self.pending_priority_queue.qsize() != 0, self.pending_resource_queue.qsize() != 0,
                              priority_queue.qsize() != 0, resource_queue.qsize() != 0))

            # This is the list of first resource messages indicating that task starts running
            first_messages = []

            # Get a batch of priority messages
            messages = self._get_messages_in_batch(self.pending_priority_queue,
                                                   interval=self.batching_interval,
                                                   threshold=self.batching_threshold)
            if messages:
                self.logger.debug(
                    "Got {} messages from priority queue".format(len(messages)))
                update_messages, insert_messages, all_messages = [], [], []
                for msg_type, msg in messages:
                    if msg_type.value == MessageType.WORKFLOW_INFO.value:
                        if "python_version" in msg:   # workflow start message
                            self.logger.debug(
                                "Inserting workflow start info to WORKFLOW table")
                            self._insert(table=WORKFLOW, messages=[msg])
                        else:                         # workflow end message
                            self.logger.debug(
                                "Updating workflow end info to WORKFLOW table")
                            self._update(table=WORKFLOW,
                                         columns=['run_id', 'tasks_failed_count',
                                                  'tasks_completed_count', 'time_completed',
                                                  'workflow_duration'],
                                         messages=[msg])
                    else:                             # TASK_INFO message
                        all_messages.append(msg)
                        if msg['task_time_returned'] is not None:
                            update_messages.append(msg)
                        else:
                            inserted_tasks.add(msg['task_id'])
                            insert_messages.append(msg)

                            # check if there is an left_message for this task
                            if msg['task_id'] in left_messages:
                                first_messages.append(
                                    left_messages.pop(msg['task_id']))

                self.logger.debug(
                    "Updating and inserting TASK_INFO to all tables")
                self._update(table=WORKFLOW,
                             columns=['run_id', 'tasks_failed_count',
                                      'tasks_completed_count'],
                             messages=update_messages)

                if insert_messages:
                    self._insert(table=TASK, messages=insert_messages)
                    self.logger.debug(
                        "There are {} inserted task records".format(len(inserted_tasks)))
                if update_messages:
                    self._update(table=TASK,
                                 columns=['task_time_returned',
                                          'task_elapsed_time', 'run_id', 'task_id'],
                                 messages=update_messages)
                self._insert(table=STATUS, messages=all_messages)

            """
            RESOURCE_INFO messages

            """
            messages = self._get_messages_in_batch(self.pending_resource_queue,
                                                   interval=self.batching_interval,
                                                   threshold=self.batching_threshold)

            if messages or first_messages:
                self.logger.debug(
                    "Got {} messages from resource queue".format(len(messages)))
                self._insert(table=RESOURCE, messages=messages)
                for msg in messages:
                    if msg['first_msg']:
                        msg['task_status_name'] = States.running.name
                        msg['task_time_running'] = msg['timestamp']
                        if msg['task_id'] in inserted_tasks:
                            first_messages.append(msg)
                        else:
                            left_messages[msg['task_id']] = msg
                if first_messages:
                    self._insert(table=STATUS, messages=first_messages)
                    self._update(table=TASK,
                                 columns=['task_time_running',
                                          'run_id', 'task_id'],
                                 messages=first_messages)


import argparse
import os
import time

from stable_baselines3.common.running_mean_std import RunningMeanStd
import numpy as np
from gym.wrappers.clip_action import ClipAction
from aliengo_env.env import AliengoEnv

from train import get_params

parser = argparse.ArgumentParser(description='Script for visualizing trained models')
parser.add_argument('--config', default='defaults', type=str)
# parser.add_argument('--save-vid', default=False, action='store_true')
# parser.add_argument('--deterministic', action='store_true', default=False, help='whether to use a deterministic policy')
parser.add_argument('--vis', default=False, action='store_true', help='enable diagnostic visualization of aliengo')
parser.add_argument('--seed', type=int, default=1, help='random seed (default: 1)')
parser.add_argument('--ws', type=int, default=-1)
parser.add_argument('--speed', default=1.0, type=float)
parser.add_argument('--device', default='cpu', help='device to load the trained model into')
args = parser.parse_args()

filename = os.path.join('./trained_models', args.config + str(args.seed) + '.npz')
if args.ws != -1:
    import paramiko
    ssh_client = paramiko.SSHClient()
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ws_ip = ['143.215.128.18',
             '143.215.131.33',
             '143.215.184.72',
             '143.215.128.16',
             '143.215.131.25',
             '143.215.131.23',
             '130.207.124.148']  # last one is sky1.cc.gatech.edu
    print('\n\nOpening Remote SSH Client...\n\n')
    if args.ws <= 6:
        ssh_client.connect(ws_ip[args.ws - 1], 22, 'jcoholich')
    else:
        ssh_client.connect(ws_ip[args.ws - 1], 22, 'jcoholich3')
    print('Connected!\n\n')
    # ssh_client.exec_command('cd hutter_kostrikov; cd trained_models')
    sftp_client = ssh_client.open_sftp()
    filename = sftp_client.open(os.path.join('aliengo_baseline/', filename), 'rb')
temp = np.load(filename)
mean_std = RunningMeanStd()
policy = temp['arr_0']
mean_std.mean = temp['arr_1']
mean_std.var = temp['arr_2']
print('Agent Loaded\n\n')

yaml_args = get_params(args.config)

# yaml_args.render = True
yaml_args.env_params['render'] = True
yaml_args.env_params['vis'] = args.vis

if yaml_args.env_name == "aliengo":
    env = AliengoEnv(**yaml_args.env_params)
    env = ClipAction(env)
else:
    raise NotImplementedError

obs = env.reset()
while True:
    time.sleep(1/240. * env.action_repeat * 1.0/args.speed)
    norm_obs = (obs - mean_std.mean) / np.sqrt(mean_std.var)
    action = policy @ np.expand_dims(norm_obs, 1)
    obs, rew, done, _ = env.step(action.flatten())
    if done:
        obs = env.reset()