from deeplearning.scientific_articles import Article
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from keras.layers import Dense, Dropout, Activation, Flatten, Input, Embedding, LSTM
from keras.layers import Convolution2D, MaxPooling2D, GlobalMaxPooling2D
import os
import keras
from keras.models import Model
from keras.utils import np_utils
from keras.models import load_model
from keras.layers.noise import GaussianNoise, GaussianDropout
from keras.regularizers import l2
from keras.layers.normalization import BatchNormalization

def get_reshaped_embed_data(embed_data):
    """
    Reshape the data for deeplearning model input (CNN)

    Parameters
    ----------
    embed_data: the embedding data

    Return
    ------
    reshaped_embed_data: shaped as [num_sections, num_data(max_section_lengths_of_words, 1, embed_size)]
    """

    print('\nReshaping Data...\n')
    title_data = list()
    abstract_data = list()
    keywords_data = list()
    for i in range(embed_data.shape[0]):
        for j in range(embed_data.shape[1]):
            if i == 0:
                print(embed_data[i][j].shape)
            reshaped_data = embed_data[i][j].reshape(embed_data[i][j].shape[0], 1, embed_data[i][j].shape[1])
            if j == 0:
                title_data.append(reshaped_data)
            elif j == 1:
                abstract_data.append(reshaped_data)
            elif j == 2:
                keywords_data.append(reshaped_data)
        if (i + 1) % 1000 == 0:
            print('Reshaped %d Data!' % (i + 1))
    title_data = np.asarray([title_data[i] for i in range(len(title_data))])
    abstract_data = np.asarray([abstract_data[i] for i in range(len(abstract_data))])
    keywords_data = np.asarray([keywords_data[i] for i in range(len(keywords_data))])
    print('Finished Reshaping Data!')
    reshaped_embed_data = [title_data, abstract_data, keywords_data]
    return reshaped_embed_data

def get_deeplearning_model(reshaped_embed_data, num_classes):
    """
    Get the deep-learning model for scientific articles

    Parameters
    ----------
    reshaped_embed_data: the reshaped embedding data of raw texts (train or text)

    num_classes: the number of classes

    """
    print('\nBuilding Deeplearning Model...\n')

    # Load Shape & Input
    title_shape = reshaped_embed_data[0][0].shape
    abstract_shape = reshaped_embed_data[1][0].shape
    keywords_shape = reshaped_embed_data[2][0].shape

    # title_input = reshaped_embed_data[0]
    # abstract_input = reshaped_embed_data[1]
    # keywords_input = reshaped_embed_data[2]

    title_input = Input(title_shape, dtype='float32', name='title_input')
    abstract_input = Input(abstract_shape, dtype='float32', name='abstract_input')
    keywords_input = Input(keywords_shape, dtype='float32', name='keywords_input')

    # Title Part
    title_input_bn = BatchNormalization()(title_input)
    title_conv_1_1 = Convolution2D(input_shape=title_shape,
                                   filters=10,
                                   kernel_size=(2, 1),
                                   strides=(1, 1),
                                   activation='relu',
                                   data_format='channels_last')(title_input_bn)
    title_pool_1_1 = GlobalMaxPooling2D()(title_conv_1_1)
    title_conv_1_2 = Convolution2D(input_shape=title_shape,
                                   filters=10,
                                   kernel_size=(3, 1),
                                   strides=(1, 1),
                                   activation='relu',
                                   data_format='channels_last')(title_input)
    title_pool_1_2 = GlobalMaxPooling2D()(title_conv_1_2)
    title_merge = keras.layers.concatenate([title_pool_1_1, title_pool_1_2])
    title_merge_bn = BatchNormalization()(title_merge)
    title_output = Dense(64, activation='sigmoid', kernel_regularizer=l2(0.01))(title_merge_bn)

    # Abstract Part
    abstract_input_bn = BatchNormalization()(abstract_input)
    abstract_conv_1_1 = Convolution2D(input_shape=abstract_shape,
                                      filters=20,
                                      kernel_size=(20, 1),
                                      strides=(1, 1),
                                      activation='relu',
                                      data_format='channels_last')(abstract_input_bn)
    abstract_pool_1_1 = MaxPooling2D(pool_size=(2, 1))(abstract_conv_1_1)
    abstract_conv_2_1 = Convolution2D(filters=10,
                                      kernel_size=(10, 1),
                                      strides=(1, 1),
                                      activation='relu',
                                      data_format='channels_last')(abstract_pool_1_1)
    abstract_pool_2_1 = MaxPooling2D(pool_size=(3, 1))(abstract_conv_2_1)
    abstract_conv_3_1 = Convolution2D(filters=10,
                                      kernel_size=(3, 1),
                                      strides=(2, 1),
                                      activation='relu',
                                      data_format='channels_last')(abstract_pool_2_1)
    abstract_pool_3_1 = GlobalMaxPooling2D()(abstract_conv_3_1)
    abstract_conv_3_2 = Convolution2D(filters=10,
                                      kernel_size=(4, 1),
                                      strides=(2, 1),
                                      activation='relu',
                                      data_format='channels_last')(abstract_pool_2_1)
    abstract_pool_3_2 = GlobalMaxPooling2D()(abstract_conv_3_2)
    abstract_conv_3_3 = Convolution2D(filters=10,
                                      kernel_size=(5, 1),
                                      strides=(2, 1),
                                      activation='relu',
                                      data_format='channels_last')(abstract_pool_2_1)
    abstract_pool_3_3 = GlobalMaxPooling2D()(abstract_conv_3_3)
    abstract_conv_3_4 = Convolution2D(filters=10,
                                      kernel_size=(6, 1),
                                      strides=(2, 1),
                                      activation='relu',
                                      data_format='channels_last')(abstract_pool_2_1)
    abstract_pool_3_4 = GlobalMaxPooling2D()(abstract_conv_3_4)
    abstract_merge = keras.layers.concatenate([abstract_pool_3_1, abstract_pool_3_2,
                                               abstract_pool_3_3, abstract_pool_3_4])
    abstract_merge_bn = BatchNormalization()(abstract_merge)
    abstract_output = Dense(512, activation='sigmoid', kernel_regularizer=l2(0.01))(abstract_merge_bn)

    # Keywords Part
    keywords_input_bn = BatchNormalization()(keywords_input)
    keywords_flatten = Flatten()(keywords_input_bn)
    keywords_output = Dense(64, activation='sigmoid', kernel_regularizer=l2(0.01))(keywords_flatten)

    # Merge and Output
    merge_layer = keras.layers.concatenate([title_output, abstract_output, keywords_output])
    merge_layer_bn = BatchNormalization()(merge_layer)
    dense_1 = Dense(256, activation='sigmoid', kernel_regularizer=l2(0.01))(merge_layer_bn)
    dropout_1 = GaussianDropout(0.25)(dense_1)
    dense_2 = Dense(32, activation='sigmoid', kernel_regularizer=l2(0.01))(dropout_1)
    dropout_2 = GaussianDropout(0.25)(dense_2)
    total_output = Dense(num_classes, activation='softmax', kernel_regularizer=l2(0.01), name='total_output')(dropout_2)

    # Print Model Summary
    dl_model = Model(inputs=[title_input, abstract_input, keywords_input], outputs=[total_output])
    print(dl_model.summary())
    print('\nDeeplearning Model Building Completed...\n')
    return dl_model

if __name__ == '__main__':
    os.chdir('E:/HDQ/Projects/Graduation')

    _article_data_path = './output/texts_without_main_text'
    _id_label_path = './output/doc_topic-15_1124.txt'
    scientific_articles = Article(article_data_path=_article_data_path,
                                  id_label_path=_id_label_path)
    _word2vec_path = './output/paper_without_main_text_400_word2vec'
    scientific_articles_embed_data = scientific_articles.get_embed_data(400, word2vec_path=_word2vec_path)
    X_train, X_test, y_train, y_test = train_test_split(scientific_articles_embed_data,
                                                        scientific_articles.data_collection['labels'],
                                                        test_size=0.4,
                                                        random_state=1)
    X_Reshaped = get_reshaped_embed_data(scientific_articles_embed_data)
    X_train_Reshaped = get_reshaped_embed_data(X_train)
    X_test_Reshaped = get_reshaped_embed_data(X_test)
    y = scientific_articles.data_collection['labels']
    for i in range(len(y)):
        y[i] -= 1
    for i in range(len(y_train)):
        y_train[i] -= 1
    for i in range(len(y_test)):
        y_test[i] -= 1
    Y = np_utils.to_categorical(y, 15)
    Y_train = np_utils.to_categorical(y_train, 15)
    Y_test = np_utils.to_categorical(y_test, 15)

    model = get_deeplearning_model(X_Reshaped, 15)

    model.compile(loss='categorical_crossentropy',
                  optimizer='sgd',
                  metrics=['accuracy'])
    model.fit({'title_input': X_train_Reshaped[0],
               'abstract_input': X_train_Reshaped[1],
               'keywords_input': X_train_Reshaped[2]},
              {'total_output': Y_train},
              batch_size=32,
              nb_epoch=30,
              validation_data=({'title_input': X_test_Reshaped[0],
                                'abstract_input': X_test_Reshaped[1],
                                'keywords_input': X_test_Reshaped[2]},
                               {'total_output': Y_test},))

    model.save('./output/nn_model/dl_model_171215.h5')
    score = model.evaluate({'title_input': X_test_Reshaped[0],
                            'abstract_input': X_test_Reshaped[1],
                            'keywords_input': X_test_Reshaped[2]},
                           {'total_output': Y_test},)
    print('Test score:', score[0])
    print('Test accuracy:', score[1])


import requests
import logging

class BranchClient(object):

    base_url = "https://api.branch.io/v1"

    def __init__(self,
                 branch_key,
                 branch_secret,
                 upside_url='https://upsideappbeta.app.link',  # default to alpha
                 session_factory=requests.session):
        self.branch_key = branch_key
        self.branch_secret = branch_secret
        self.upside_url = upside_url

        self.session = session_factory()

        self.session.mount('http://', requests.adapters.HTTPAdapter(max_retries=3))
        self.session.mount('https://', requests.adapters.HTTPAdapter(max_retries=3))

    def get_deep_link(self, deep_link):
        # $ curl 'https://api.branch.io/v1/url?url=https%3A%2F%2Fupside.app.link/THOMAS688&branch_key=xxx' | jq

        url = '{branchUrl}/url'.format(branchUrl=self.base_url)
        query_params = {
            'url': '{upsideUrl}/{deepLink}'.format(upsideUrl=self.upside_url, deepLink=deep_link),
            'branch_key': self.branch_key
        }

        logging.info('Sending to url {} query params: {}'.format(url, query_params))

        response = self.session.get(
            url=url,
            params=query_params
        )

        logging.info('url = {}'.format(response.url))

        return response.json()

    def update_deep_link(self, deep_link, **data_map):
        """
        :param deep_link: The 'deep_link' to update data for.  In Upside lingo, this is a 'promoCode' like 'THOMAS688'
        :param kwargs: The dataMap we want to update the deep_link with.  Note that the default branch.io data that
        exists on the deep link will remain intact after an update, but any and all non-branch.io data will be
        replaced with the contents of the
        :return: The response from the server
        """
        # e.g. https://api.branch.io/v1/url?url=https%3A%2F%2Fupside.app.link/THOMAS688
        branch_url = '{branchUrl}/url?url={upsideUrl}/{deepLink}'.format(branchUrl=self.base_url,
                                                                         upsideUrl=self.upside_url,
                                                                         deepLink=deep_link)

        payload = {
            'branch_key': self.branch_key,
            'branch_secret': self.branch_secret,
            'data': data_map
        }

        logging.info('sending payload {}'.format(payload))

        response = self.session.put(
            url=branch_url,
            json=payload
        )

        logging.info('url = {}'.format(response.url))

        return response.json()


import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import sys
import pandas as pd
import pytraj as pt
import matplotlib.mlab as mlab
import matplotlib.cm as cm
import os

# python program_name ncp peng ngen filename
#python plot_scores.py 5 5 10000 100 scores1.dat

ncp = sys.argv[1] #number of chromosome print
peng = sys.argv[2] # print every n generation
ngen = sys.argv[3] #number of generation
max_x = sys.argv[4] # maxiumum number of generation u want to plot

filename = np.genfromtxt(sys.argv[5])
scores = np.genfromtxt(sys.argv[5], usecols=2) # get the scores
# but scores are printed based on ncp

# need to split  scores to give an array with ncp for each peng
scores = np.split(scores, (len(scores))/int(peng))

def plot_avg_score(scores, ncp, peng, ngen, max_x):
  Avg = [];Std = []
  for i in scores:
    sum = np.sum(i)
    std = np.std(i)
    Std.append(std)
    avg = float(sum) / int(ncp)
    Avg.append(avg)
  Avg = np.array(Avg)
  Std = np.array(Std)
  import seaborn as sns; sns.set(style="ticks")
  plt.figure()
  Yarray = Avg

  #plot my data, +ncp, because of initial scores (-1)
  x=np.arange(0, (int(ngen) + int(ncp)), int(peng))
  plt.plot(x, Yarray, 'k-', color="blue")
  plt.fill_between(x, Yarray-Std, Yarray+Std)
  plt.scatter(x, Yarray, s=15, color="black")
  #plt.axhline(y=2, color="black", linewidth=1)

  plt.ylabel("Score (AAE) ", fontsize=24)
  plt.xlabel('# of Generation', fontsize=24)
  plt.ylim(0, max(Yarray))
  #plt.yticks(np.arange(23,31,1))
  #plt.xlim(0,max(x))
  plt.xlim(0, int(max_x))
  plt.grid(b=True, which='both', color='gray', linewidth=0.13, linestyle='--')
  plt.savefig('Average_score_vs_ngen.png')

plot_avg_score(scores, ncp, peng, ngen, max_x)
#print (scores)


# python -m virtualenv -p /usr/bin/python3.8 venv
# source venv/bin/activate
# pip install ipykernel
# python -m ipykernel install --user --name=venv

# %%

import pandas as pd
import numpy as np
import tensortrade.env.default as default

import matplotlib
matplotlib.use('Agg')

from tensortrade.data.cdd import CryptoDataDownload
from tensortrade.feed.core import Stream, DataFeed
from tensortrade.oms.exchanges import Exchange
from tensortrade.oms.services.execution.simulated import execute_order
from tensortrade.oms.instruments import EUR, BTC, ETH, EUR
from tensortrade.oms.wallets import Wallet, Portfolio
from tensortrade.agents import DQNAgent
from statsmodels.tsa.stattools import adfuller

cdd = CryptoDataDownload()

data = cdd.fetch("Bitstamp", "EUR", "BTC", "1h")

# data = pd.read_csv('./data/backtest/15m.csv')
# data.columns =['unix', 'open', 'high', 'low', 'close', 'volume', 'Close time', 'Quote asset volume', 'Number of trades', 'Taker buy base asset volume', 'Taker buy quote asset volume', 'Ignore']

# data['date'] = pd.to_datetime(data["unix"], unit='ms').dt.strftime('%Y-%m-%d %H:%M:%S')

# data = data[['date', 'unix', 'open', 'high', 'low', 'close', 'volume']]
# data['unix'] = data['unix'].div(1000).astype('int64')

# data['diffed'] = data['close'] - data['close'].shift(1)
# data['logged_and_diffed'] = np.log(data['close']) - np.log(data['close'].shift(1))
# print(data)

# data['logged_and_diffed'] = np.log(data['close']) - np.log(data['close']).shift(1)
# result = adfuller(data['logged_and_diffed'].values[1:], autolag="AIC")
# print('p-value: %f' % result[1])

def rsi(price: Stream[float], period: float) -> Stream[float]:
    r = price.diff()
    upside = r.clamp_min(0).abs()
    downside = r.clamp_max(0).abs()
    rs = upside.ewm(alpha=1 / period).mean() / downside.ewm(alpha=1 / period).mean()
    return 100*(1 - (1 + rs) ** -1)

def macd(price: Stream[float], fast: float, slow: float, signal: float) -> Stream[float]:
    fm = price.ewm(span=fast, adjust=False).mean()
    sm = price.ewm(span=slow, adjust=False).mean()
    md = fm - sm
    signal = md - md.ewm(span=signal, adjust=False).mean()
    return signal

features = []
for c in data.columns[1:]:
    s = Stream.source(list(data[c]), dtype="float").rename(data[c].name)
    features += [s]

cp = Stream.select(features, lambda s: s.name == "close")

features = [
    cp.log().diff().rename("lr"),
    rsi(cp, period=20).rename("rsi"),
    # macd(cp, fast=10, slow=50, signal=5).rename("macd")
]

feed = DataFeed(features)
feed.compile()

for i in range(5):
    print(feed.next())

bitstamp = Exchange("bitstamp", service=execute_order)(
    Stream.source(list(data["close"]), dtype="float").rename("EUR-BTC")
)

portfolio = Portfolio(EUR, [
    Wallet(bitstamp, 1000 * EUR),
    Wallet(bitstamp, 0.0025 * BTC)
])

renderer_feed = DataFeed([
    Stream.source(list(data["date"])).rename("date"),
    Stream.source(list(data["open"]), dtype="float").rename("open"),
    Stream.source(list(data["high"]), dtype="float").rename("high"),
    Stream.source(list(data["low"]), dtype="float").rename("low"),
    Stream.source(list(data["close"]), dtype="float").rename("close"),
    Stream.source(list(data["volume"]), dtype="float").rename("volume")
])

env = default.create(
    portfolio=portfolio,
    action_scheme="managed-risk",
    reward_scheme="risk-adjusted",
    feed=feed,
    renderer_feed=renderer_feed,
    renderer=default.renderers.PlotlyTradingChart(),
    window_size=20
)

env.observer.feed.next()

agent = DQNAgent(env)

agent.train(n_steps=10000, n_episodes=1,
            save_path="agents/", render_interval=10000)

print('Success')


import json
import os
import sys
import random

from kbEndPoint.utils.sparql import sparqlUtils
from preprocessing import metricUtils
import pandas as pd

import nltk
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer # will do for now?

import codecs
import numpy as np
from scipy import stats

#PREFIX = "/Users/funke/Documents/ComplexWebQuestions_preprocess"
PREFIX = "/media/nbhutani/Data/textray_workspace/TextRay/datasets/ComplexWebQuestions_preprocess"

INPUT_PATH = os.path.join(PREFIX, "annotated/train.json")
CANDIDATE_MAIN_WITH_CONSTRAINTS_DEST_PATH = os.path.join(PREFIX, "cands/train/main_with_constraints")
CANDIDATE_MAIN_WITH_CONSTRAINTS_DEST_REWARDS_DEDUP_PATH = os.path.join(PREFIX, "rewards/dedup/train")
CANDIDATE_MAIN_WITH_CONSTRAINTS_DEST_REWARDS_DEDUP_LABEL_PATH = os.path.join(PREFIX, "rewards/labels/train")
CANDIDATE_MAIN_WITH_CONSTRAINTS_DEST_REWARDS_DEDUP_LABEL_RESCALED_MAX_PATH = os.path.join(PREFIX, "rewards/rescaled_max/train")
CANDIDATE_MAIN_WITH_CONSTRAINTS_DEST_REWARDS_DEDUP_LABEL_RESCALED_Z_SCORE_PATH = os.path.join(PREFIX, "rewards/rescaled_zscore/train")
CANDIDATE_MAIN_WITH_CONSTRAINTS_DEST_REWARDS_DEDUP_LABEL_RESCALED_Z_SCORE_PRIORS_PATH = os.path.join(PREFIX, "rewards/rescaled_zscore_priors/train")
CANDIDATE_MAIN_WITH_CONSTRAINTS_DEST_REWARDS_DEDUP_LABEL_RESCALED_MAX_PRIORS_PATH = os.path.join(PREFIX, "rewards/rescaled_max_priors/train")
#
# CANDIDATE_MAIN_WITH_CONSTRAINTS_DEST_REWARDS_DEDUP_LABEL_RESCALED_MAX_PRIORS_DERIVED_1_PATH = os.path.join(PREFIX, "rewards/rescaled_max_priors_derived_0.1/train")
# CANDIDATE_MAIN_WITH_CONSTRAINTS_DEST_REWARDS_DEDUP_LABEL_RESCALED_MAX_PRIORS_DERIVED_5_PATH = os.path.join(PREFIX, "rewards/rescaled_max_priors_derived_0.5/train")
# CANDIDATE_MAIN_WITH_CONSTRAINTS_DEST_REWARDS_DEDUP_LABEL_RESCALED_MAX_DERIVED_1_PATH = os.path.join(PREFIX, "rewards/rescaled_max_derived_0.1/train")
CANDIDATE_MAIN_WITH_CONSTRAINTS_DEST_REWARDS_DEDUP_LABEL_RESCALED_MAX_DERIVED_5_PATH = os.path.join(PREFIX, "rewards/rescaled_max_derived_0.5/train")
# TOPIC_PATH = os.path.join(PREFIX, "topic_entities/main/train_topic.csv")

def train_data_stats(input_path, src_dir):
    questions = json.load(codecs.open(input_path, 'r', encoding='utf-8'))
    positive_examples_ct = 0
    negative_examples_ct = 0
    for q in questions:
        ques_id = q["ID"]
        type = q["compositionality_type"]
        if type == "conjunction" or type == "composition":
            ques_path = os.path.join(src_dir, ques_id + ".json")
            if not os.path.exists(ques_path):
                continue

            #print(ques_id)
            main_entity_paths = json.load(codecs.open(ques_path, 'r', encoding='utf-8'))
            for topic in main_entity_paths:
                for path in main_entity_paths[topic]:
                    if path["derived_label"] == 0:
                        negative_examples_ct += 1
                    elif path["derived_label"] == 1:
                        positive_examples_ct += 1
    print(positive_examples_ct)
    print(negative_examples_ct)

if __name__ == '__main__':
    train_data_stats(INPUT_PATH, CANDIDATE_MAIN_WITH_CONSTRAINTS_DEST_REWARDS_DEDUP_LABEL_RESCALED_MAX_DERIVED_5_PATH)


import sqlite3
import sys
import re

#collects the info from the a string containing the time info, the person writing the text, and the text itself, and puts it into the database
def send_message(message, conn):
    year = message[0:4]
    month = message[5:7]
    day = message[8:10]
    hour = message[12:14]
    try:
        messagestart = 20+message[20:].index(":")
        minute = message[15:17]
        sender = message[20:messagestart]
        text = message[messagestart+2:]
        conn.execute("INSERT INTO MESSAGES (SENDER, MESSAGE, DAY, MONTH, YEAR, HOUR, MINUTE) VALUES (?, ?, ?, ?, ?, ?, ?)", (sender, text, day, month, year, hour, minute))
    except ValueError:
        pass

args = sys.argv

conn = sqlite3.connect(args[2])  # @UndefinedVariable
conn.execute('''CREATE TABLE messages
             (ID        INTEGER PRIMARY KEY,
              SENDER    TEXT,
              MESSAGE   TEXT,
              DAY       INT,
              MONTH     INT,
              YEAR      INT,
              HOUR      INT,
              MINUTE    INT);''')
file = open(args[1],"r")

message = ""

for line in file.readlines():

    #if the line has the date/time, it should be the a new message, so send the old message and assign the line as the new message
    #otherwise, we are still dealing with the old message, so just tack on the line to the end of it
    if not(str(type(re.match("^[0-9]{4}-[0-9]{2}-[0-9]{2}", line))) == "<class 'NoneType'>"):
        send_message(message, conn)
        message = line
    else:
        message += line;
    if not "\n" in message:
        break

send_message(message, conn)

file.close()

conn.commit()
conn.close()


# Emily Schultz, ess2183
# COMS 4241
# HW 9: Heat Equation PDE Solver
# Due: 4/15/2013

import matplotlib.pyplot as plt
import numpy as np
import math

def heat_solver(h, k, T, p, u_0, u_1):
    '''The explicit algorithm solving PDE up to T, graphing every p steps.'''
    # initialize variables
    t = 0
    pcount = 0
    sigma = k/h**2

    # total steps in x and t
    x_steps = int(1/h)
    t_steps = int(T/k)
    t_index = 0

    # array of values of u at x,t
    u = np.zeros((x_steps+1, t_steps+1), float)

    # increment t up to time T
    while t <= T:
        t_index = t_index + k
        # initial conditions: u(x,0) = f(x)
        if t == 0:
            for x_index in range(0, x_steps+1):
                x = x_index * h
                u[x_index][t_index] = f(x)
        else:
            # boundary conditions
            u[0][t_index] = u_0
            u[1][t_index] = u_1
            # explicit algorithm
            for x_index in range(1, x_steps):
                u[x_index][t_index] = sigma * u[x_index-1][t_index-1] + \
                                      sigma * u[x_index+1][t_index-1] + \
                                      (1-2*sigma) * u[x_index][t_index-1]

        # get graph every pth step
        #if pcount % p == 0:
        plt.plot(range(0,int(x_steps+1)), u[:,t_index])
        #plt.gca().set_ylim([-1,1])
        plt.title("Plot %i"%(pcount/p))
        #plt.show()
        plt.savefig("1plot%i"%(pcount))
        plt.clf()
        # update the pcount and t
        pcount += 1
        t = t + k

def f(x):
    '''The function f, for initial condition u(x,0) = f(x).'''
    # hardcoded but can be easily changed
    return math.sin(math.pi*x)

def main():
    '''Solves the PDE for vibrating string.'''

    # get the inputs
    h = 10**-1#input("h? ")        # x-width               10**-1
    k = 10**-1#input("k? ")        # t-width               0.5*10**-2
    T = 1#input("T? ")        # max time              1
    p = 10#input("p? ")        # graphing rate         10
    u_0 = 0#input("U(0,t)? ") # boundary condition    0
    u_1 = 0#input("U(1,t)? ") # boundary condition    0

    heat_solver(h, k, T, p, u_0, u_1)

main()


from pathlib import Path
from typing import Any, Dict

import tensorflow as tf

from yeahml.build.build_layers import build_hidden_block
from yeahml.build.layers.config import NOTPRESENT
from yeahml.information.write_info import write_build_information

# from yeahml.build.get_components import get_logits_and_preds
from yeahml.log.yf_logging import config_logger

# Helper to make the output "consistent"
def reset_graph_deterministic(seed: int = 42) -> None:
    # logger = logging.getLogger("build_logger")
    # logger.info("reset_graph_deterministic")
    # there is no option for deterministic behavior yet...
    # > tf issue https://github.com/tensorflow/tensorflow/issues/18096
    # os.environ["TF_CUDNN_USE_AUTOTUNE"] = "0"
    tf.random.set_seed(seed)
    tf.keras.backend.clear_session()
    # np.random.seed(seed)

def reset_graph(seed: int = 42) -> None:
    # logger = logging.getLogger("build_logger")
    # logger.info("reset_graph")
    # tf.reset_default_graph()
    tf.random.set_seed(seed)
    tf.keras.backend.clear_session()

def _configure_input(cur_name, cur_config):

    # TODO: this section should be redone to match the layer API. but for now
    # I'm going to continue on with this minimal approach
    dtype = cur_config["dtype"]
    shape = cur_config["shape"]
    if cur_config["startpoint"]:
        out = tf.keras.layers.Input(shape=shape, dtype=dtype, name=cur_name)
    else:
        # all data layers should be a startpoint
        raise ValueError(f"current data layer config:{cur_config} is not a startpoint")

    return out

def _configure_layer(cur_name, cur_config):

    # 'layer_base', 'options', 'layer_in_name'
    layer_base = cur_config["layer_base"]

    # default layer function
    layer_fn = layer_base["func"]

    # assemble layer with user values (or default if not specified)
    # another way to do this could be just overwrite the values the user
    # specifies, but I want the ability to set defaults for each layer
    # eventually (in case we would want to change them)
    layer_args = layer_base["func_args"]
    layer_defaults = layer_base["func_defaults"]
    user_values = cur_config["options"]["user_vals"]

    param_dict = {}
    for i, param_name in enumerate(layer_args):
        if user_values:
            user_val = user_values[i]
        else:
            # TODO: this logic will need to be rethought, since it's possible
            # that a user argument could be `None` (I think?). I think a
            # potential solution would be to use a custom class sentinel value
            user_val = None

        # NOTE: `if user_val`: does not work because `user_val` could be False
        # TODO: this function will need to be tested
        if user_val is not None:
            param_dict[param_name] = user_val
        else:
            def_val = layer_defaults[i]
            if isinstance(def_val, NOTPRESENT):
                raise ValueError(
                    f"param {param_name} for layer {cur_name} not specified, but is required"
                )
            else:
                param_dict[param_name] = def_val

                # overwrite name with layer name if not exist
                if param_name == "name":
                    if not def_val:
                        param_dict[param_name] = cur_name
    configured_layer = layer_fn(**param_dict)

    return configured_layer

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

def _is_tensor_or_list_of_tensors(obj):
    if isinstance(obj, tf.Tensor):
        return True
    elif isinstance(obj, list):
        for v in obj:
            if isinstance(v, tf.Tensor):
                pass
            else:
                return False
        return True
    else:
        raise ValueError(f"prev_out is neither a tensor or list. is type: {type(obj)}")

def _build_branch_from_pure_node_list(branch_seq, built_nodes):
    prev_out = None
    for sub_seq in branch_seq:
        tensors = []
        if isinstance(sub_seq, str):
            sub_seq = [sub_seq]
        elif isinstance(sub_seq, TupleOut):
            sub_seq = sub_seq.vals
        for node_name in sub_seq:
            try:
                cur_built_node = built_nodes[node_name]
            except KeyError:
                # TODO: this message will have to be expanded for huge graphs
                raise KeyError(
                    f"node ({node_name}) not found in built nodes {built_nodes.keys()}"
                )

            # get output of node
            if isinstance(cur_built_node["out"], tf.Tensor):
                # output already exists
                out = cur_built_node["out"]
            else:
                # output does not exist (func hasn't been called), get prev_out
                # to use as input
                if _is_tensor_or_list_of_tensors(prev_out):
                    try:
                        cur_func = cur_built_node["func"]
                    except KeyError:
                        raise KeyError(
                            f"node ({node_name}) function has not been built yet"
                        )

                    out = cur_func(prev_out)
                    cur_built_node["out"] = out
                else:
                    raise ValueError(
                        f"node ({node_name}) does not have an output yet and there is not previous out to use as an input"
                    )
            tensors.append(out)
        if len(tensors) == 1:
            tensors = tensors[0]
        prev_out = tensors

def _unpack_and_build_tuple(tuple_seq, built_nodes):

    # ((2, [[((2, [['n_a', ...], ['n_a', ...]]), 'ct_1'), 'ct_1', 'out'], ['n_a', 'd_1', ...]]), 'c3')
    branch_tuple = tuple_seq[0]
    num_branches = branch_tuple[0]
    branch_seqs = branch_tuple[1]
    name = tuple_seq[1]
    outer_seq_to_build = TupleOut()

    for branch_seq in branch_seqs:
        if isinstance(branch_seq, tuple):
            final_node = _unpack_and_build_tuple(branch_seq, built_nodes)
            outer_seq_to_build.vals.append(final_node)
        else:
            out_seq = _build_sequence(branch_seq, built_nodes)
            _build_sequence(out_seq, built_nodes)
            outer_seq_to_build.vals.append(out_seq[-1])

    return outer_seq_to_build

def _is_pure_node_list(seq):
    # a node list may be a list of strings or a list of strings with a list of
    # strings
    for v in seq:
        if isinstance(v, str):
            pass
        elif isinstance(v, TupleOut):
            pass
        else:
            return False
    return True

class TupleOut:
    def __init__(self, vals=None):
        if vals:
            self.vals = vals
        else:
            self.vals = []

    def __str__(self):
        return str(self.__class__) + f" @ {hex(id(self))}" + ": " + str(self.__dict__)

    def __call__(self):
        return self.vals

def _build_sequence(seq_info, built_nodes):

    outer_seq_to_build = []
    if _is_pure_node_list(seq_info):
        _build_branch_from_pure_node_list(seq_info, built_nodes)
        outer_seq_to_build.append(seq_info[-1])
    else:
        # may contain sublists or tuples
        for sub_seq in seq_info:
            if isinstance(sub_seq, str):
                outer_seq_to_build.append(sub_seq)
            elif isinstance(sub_seq, TupleOut):
                outer_seq_to_build.append(sub_seq)
            elif _is_pure_node_list(sub_seq):
                _build_branch_from_pure_node_list(sub_seq, built_nodes)
                outer_seq_to_build.append(sub_seq)
            else:
                if isinstance(sub_seq, list):
                    outer_seq = _build_sequence(sub_seq, built_nodes)
                    _build_sequence(outer_seq, built_nodes)
                    if not outer_seq:
                        outer_seq_to_build.append(sub_seq[-1])
                elif isinstance(sub_seq, tuple):
                    outer_seq = _unpack_and_build_tuple(sub_seq, built_nodes)
                    outer_seq_to_build.append(outer_seq)

    return outer_seq_to_build

def build_model(config_dict: Dict[str, Dict[str, Any]]) -> Any:

    # unpack configuration
    model_cdict: Dict[str, Any] = config_dict["model"]
    meta_cdict: Dict[str, Any] = config_dict["meta"]
    log_cdict: Dict[str, Any] = config_dict["logging"]
    data_cdict: Dict[str, Any] = config_dict["data"]
    static_cdict: Dict[str, Any] = config_dict["static"]
    subgraphs_cdict: Dict[str, Any] = config_dict["subgraphs"]
    model_io_cdict: Dict[str, Any] = config_dict["model_io"]

    full_exp_path = (
        Path(meta_cdict["yeahml_dir"])
        .joinpath(meta_cdict["data_name"])
        .joinpath(meta_cdict["experiment_name"])
    )
    logger = config_logger(full_exp_path, log_cdict, "build")
    logger.info("-> START building graph")

    try:
        reset_graph_deterministic(meta_cdict["seed"])
    except KeyError:
        reset_graph()

    full_exp_path = (
        Path(meta_cdict["yeahml_dir"])
        .joinpath(meta_cdict["data_name"])
        .joinpath(meta_cdict["experiment_name"])
    )
    g_logger = config_logger(full_exp_path, log_cdict, "graph")

    # configure/build all layers and save in lookup table
    built_nodes = {}
    for name, node in static_cdict.items():
        if node.config_location == "model":
            blueprint = model_cdict["layers"][node.name]
        elif node.config_location == "data":
            blueprint = data_cdict["in"][node.name]
        else:
            raise ValueError(f"layer {name} can't be found in {node.config_location}")

        if node.startpoint:
            if node.label:
                pass
            else:
                func = None
                out = _configure_input(name, blueprint)
        else:
            func = _configure_layer(name, blueprint)
            out = None

        # TODO: this is a quick fix. the issue is that a node that is a label,
        # does not need to be built as a layer in the graph -- it is only used
        # as a target during training and therefore does not need to be included
        # here
        if not node.label:
            built_nodes[name] = {"out": out, "func": func}

    # connect the subgraphs
    for end_name, seq_dict in subgraphs_cdict.items():

        seq_info = seq_dict["sequence"]
        out_seq = _build_sequence(seq_info, built_nodes)
        if out_seq:
            _build_branch_from_pure_node_list(out_seq, built_nodes)

    model_input_tensors = []
    for name in model_io_cdict["inputs"]:
        try:
            node_d = built_nodes[name]
        except KeyError:
            raise KeyError(f"{name} not found in built nodes when creating inputs")

        try:
            out = node_d["out"]
        except KeyError:
            raise KeyError(
                f"out was not created for {name} when creating tensor inputs"
            )
        model_input_tensors.append(out)
    if not model_input_tensors:
        raise ValueError(f"not model inputs are available")

    model_output_tensors = []
    for name in model_io_cdict["outputs"]:
        try:
            node_d = built_nodes[name]
        except KeyError:
            raise KeyError(f"{name} not found in built nodes when creating outputs")

        try:
            out = node_d["out"]
        except KeyError:
            raise KeyError(
                f"out was not created for {name} when creating tensor outputs"
            )
        model_output_tensors.append(out)
    if not model_output_tensors:
        raise ValueError(f"not model outputs are available")

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

    # TODO: inputs may be more complex than an ordered list
    # TODO: outputs could be a list
    # TODO: right now it is assumed that the last layer defined in the config is the
    # output layer -- this may not be true. named outputs would be better.
    model = tf.keras.Model(inputs=model_input_tensors, outputs=model_output_tensors)

    # write meta.json including model hash
    if write_build_information(model_cdict, meta_cdict):
        logger.info("information json file created")

    return model


# -*- encoding: utf-8 -*-

# Project :zs
# Time  :2019/7/22 下午7:32
# BY    :FormatFa

from pyecharts.charts import Bar,Line

b = Bar()
b.add_xaxis(['one','two','three'])
b.add_yaxis('s1',[1,2,3])

l = Line()
l.add_xaxis(['one','two','three','four'])
l.add_yaxis('s2',[1,2,3])

l.overlap(b)
print(b)
print(l.dump_options())

l.render('test.html')


from django.shortcuts import render

from django.http import JsonResponse

from django.core.paginator import Paginator

from django.conf import settings

from django.views.generic import DetailView

import json

import datetime

from .models import *

from .filters import *

from .utils import *

# Create your views here.

def store(request):
    context = {}

    data = cart_data(request)

    products = Product.objects.all().order_by('name')

    product_filter = ProductFilter(request.GET, queryset=products)
    # context['athlete_list'] = athlete_list
    context['product_filter'] = product_filter

    paginated_product_filter = Paginator(product_filter.qs, 4)
    page_number = request.GET.get('page')
    product_page_obj = paginated_product_filter.get_page(page_number)

    # context['products'] = products
    context['product_page_obj'] = product_page_obj
    context['total_quantity'] = data['total_quantity']

    return render(request, 'store/store.html', context=context)

def cart(request):
    context = cart_data(request)

    return render(request, 'store/cart.html', context=context)

def checkout(request):
    context = cart_data(request)

    context['barikoi_autocomplete_api_key'] = settings.BARIKOI_AUTOCOMPLETE_API_KEY

    return render(request, 'store/checkout.html', context=context)

def update_item(request):
    data = json.loads(request.body)
    product_id = data['productId']
    action = data['action']

    print('action: ', action)
    print('product_id: ', product_id)

    customer = request.user.customer
    product = Product.objects.get(id=product_id)

    order, created = Order.objects.get_or_create(customer=customer, complete=False)
    orderitem, created = OrderItem.objects.get_or_create(order=order, product=product)

    if action == 'add':
        orderitem.quantity += 1
    elif action == 'remove':
        orderitem.quantity -= 1

    orderitem.save()

    if orderitem.quantity <= 0:
        orderitem.delete()

    return JsonResponse('Item was added.', safe=False)

def process_order(request):
    transaction_id = datetime.datetime.now().timestamp()
    data = json.loads(request.body)

    if request.user.is_authenticated:
        customer = request.user.customer
        order, created = Order.objects.get_or_create(customer=customer, complete=False)
    else:
        customer, order = guest_order(request, data)

    total = float(data['form']['total'])
    order.transaction_id = transaction_id

    # validate against actual price from the Order model
    # also check if cart is empty
    if (total == order.total_price) and (len(order.orderitem_set.all()) > 0):
        order.complete = True
    order.save()

    if order.shipping == True:
        ShippingAddress.objects.create(
            customer=customer,
            order=order,
            address=data['shipping']['address'],
            city=data['shipping']['city'],
            zipcode=data['shipping']['zipcode'],
        )

    print('total: ', total)
    print('order.total_price: ', order.total_price)
    print('total == order.total_price: ', total == order.total_price)

    return JsonResponse('Payment complete.', safe=False)

class ProductDetailView(DetailView):
    model = Product
    template_name = 'store/product_detail.html'

    def get(self, request, pk) :
        context = {}
        product = Product.objects.get(id=pk)
        data = cart_data(request)
        context['total_quantity'] = data['total_quantity']
        context['product'] = product
        return render(request, self.template_name, context)


#!/usr/bin/env python3

import argparse

def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument('out')
    args = parser.parse_args()

    with open(args.out, 'w') as f:
        f.write('fn main() { println!("I prefer tarnish, actually.") }')

if __name__ == "__main__":
    main()

test cases/rust/11 generated main/gen.py


# validated: 2017-12-15 EN f9bece2ffbf7 edu/wpi/first/wpilibj/Counter.java
#----------------------------------------------------------------------------
# Copyright (c) FIRST 2008-2012. All Rights Reserved.
# Open Source Software - may be modified and shared by FRC teams. The code
# must be accompanied by the FIRST BSD license file in the root directory of
# the project.
#----------------------------------------------------------------------------

import hal
import weakref
from enum import IntEnum

from .interfaces.counterbase import CounterBase
from .interfaces.pidsource import PIDSource
from .analogtriggeroutput import AnalogTriggerOutput
from .digitalinput import DigitalInput
from .sensorbase import SensorBase
from ._impl.utils import match_arglist, HasAttribute

__all__ = ["Counter"]

def _freeCounter(counterObj):
    hal.setCounterUpdateWhenEmpty(counterObj._counter, True)

    counterObj.clearUpSource()
    counterObj.clearDownSource()

    hal.freeCounter(counterObj._counter)

    counterObj.upSource = None
    counterObj.downSource = None
    counterObj._counter = None

class Counter(SensorBase):
    """Counts the number of ticks on a :class:`.DigitalInput` channel.

    This is a general purpose class for counting repetitive events. It can return
    the number of counts, the period of the most recent cycle, and detect when
    the signal being counted has stopped by supplying a maximum cycle time.

    All counters will immediately start counting - :meth:`reset` them if you need
    them to be zeroed before use.

    .. not_implemented: initCounter
    """

    class Mode(IntEnum):
        """Mode determines how and what the counter counts"""

        #: two pulse mode
        kTwoPulse = 0

        #: semi period mode
        kSemiperiod = 1

        #: pulse length mode
        kPulseLength = 2

        #: external direction mode
        kExternalDirection = 3

    EncodingType = CounterBase.EncodingType
    PIDSourceType = PIDSource.PIDSourceType
    allocatedUpSource = False
    allocatedDownSource = False

    def __init__(self, *args, **kwargs):
        """Counter constructor.

        The counter will start counting immediately.

        Positional arguments may be either channel numbers, :class:`.DigitalSource`
        sources, or :class:`.AnalogTrigger` sources in the following order:

        A "source" is any valid single-argument input to :meth:`setUpSource` and :meth:`setDownSource`

        - (none)
        - upSource
        - upSource, down source
        - encodingType, up source, down source, inverted

        If the passed object has a
        `getPortHandleForRouting` function, it is assumed to be a DigitalSource.
        If the passed object has a `createOutput` function, it is assumed to
        be an AnalogTrigger.

        In addition, extra keyword parameters may be provided for mode, inverted,
        and encodingType.

        :param upSource: The source (channel num, DigitalInput, or AnalogTrigger)
            that should be used for up counting.
        :param downSource: The source (channel num, DigitalInput, or AnalogTrigger)
            that should be used for down counting or direction control.
        :param mode:
            How and what the counter counts (see :class:`.Mode`).  Defaults to
            `Mode.kTwoPulse` for zero or one source, and
            `Mode.kExternalDirection` for two sources.
        :param inverted:
            Flips the direction of counting.  Defaults to False if unspecified.
            Only used when two sources are specified.
        :param encodingType:
            Either k1X or k2X to indicate 1X or 2X decoding. 4X decoding
            is not supported by Counter; use `Encoder` instead.  Defaults
            to k1X if unspecified.  Only used when two sources are specified.
        :type encodingType: :class:`.Counter.EncodingType`
        """

        super().__init__()
        source_identifier = [int, HasAttribute("getPortHandleForRouting"), HasAttribute("createOutput")]

        argument_templates = [[],
                              [("upSource", source_identifier), ],
                              [("upSource", source_identifier), ("downSource", source_identifier)],
                              [("encodingType", None), ("upSource", source_identifier),
                               ("downSource", source_identifier), ("inverted", bool)], ]

        _, results = match_arglist('Counter.__init__',
                                   args, kwargs, argument_templates, allow_extra_kwargs=True)

        # extract arguments
        upSource = results.pop("upSource", None)
        downSource = results.pop("downSource", None)

        encodingType = results.pop("encodingType", None)
        inverted = results.pop("inverted", False)
        mode = results.pop("mode", None)

        if mode is None:
            #Get the mode
            if upSource is not None and downSource is not None:
                mode = self.Mode.kExternalDirection
            else:
                mode = self.Mode.kTwoPulse

        # save some variables
        self.distancePerPulse = 1.0 # distance of travel for each tick
        self.pidSource = self.PIDSourceType.kDisplacement

        # create counter
        self._counter, self.index = hal.initializeCounter(mode)
        self.__finalizer = \
            weakref.finalize(self, _freeCounter, self)

        self.setMaxPeriod(.5)

        hal.report(hal.UsageReporting.kResourceType_Counter, self.index, mode)
        self.setName("Counter", self.index)

        # Set sources
        if upSource is not None:
            self.setUpSource(upSource)
        else:
            self.upSource = None

        if downSource is not None:
            self.setDownSource(downSource)
        else:
            self.downSource = None

        # when given two sources, set edges
        if upSource is not None and downSource is not None:
            if encodingType == self.EncodingType.k1X:
                self.setUpSourceEdge(True, False)
                hal.setCounterAverageSize(self._counter, 1)
            else:
                self.setUpSourceEdge(True, True)
                hal.setCounterAverageSize(self._counter, 2)
            self.setDownSourceEdge(inverted, True)

    @property
    def counter(self):
        if not self.__finalizer.alive:
            raise ValueError("Cannot use counter after free() has been called")
        return self._counter

    def free(self):
        super().free()
        self.__finalizer()

    def getFPGAIndex(self):
        """
        :returns: The Counter's FPGA index.
        """
        return self.index

    def setUpSource(self, *args, **kwargs):
        """Set the up counting source for the counter.

        This function accepts either a digital channel index, a
        `DigitalSource`, or an `AnalogTrigger` as positional arguments:

        - source
        - channel
        - analogTrigger
        - analogTrigger, triggerType

        For positional arguments, if the passed object has a
        `getPortHandleForRouting` function, it is assumed to be a DigitalSource.
        If the passed object has a `createOutput` function, it is assumed to
        be an AnalogTrigger.

        Alternatively, sources and/or channels may be passed as keyword
        arguments.  The behavior of specifying both a source and a number
        for the same channel is undefined, as is passing both a positional
        and a keyword argument for the same channel.

        :param channel: the DIO channel to use as the up source. 0-9 are on-board, 10-25 are on the MXP
        :type channel: int
        :param source: The digital source to count
        :type source: DigitalInput
        :param analogTrigger:
            The analog trigger object that is used for the Up Source
        :type analogTrigger: AnalogTrigger
        :param triggerType:
            The analog trigger output that will trigger the counter.
            Defaults to kState if not specified.
        :type triggerType: AnalogTrigger.AnalogTriggerType
        """

        #TODO Both this and the java implementation should probably not allow setting a source if one is already set.

        argument_templates = [[("channel", int)],
                              [("source", HasAttribute("getPortHandleForRouting")), ],
                              [("analogTrigger", HasAttribute("createOutput"))],
                              [("analogTrigger", HasAttribute("createOutput")), ("triggerType", None)]]

        _, results = match_arglist('Counter.setUpSource',
                                   args, kwargs, argument_templates)

        # extract arguments
        source = results.pop("source", None)
        channel = results.pop("channel", None)
        analogTrigger = results.pop("analogTrigger", None)
        triggerType = results.pop("triggerType", AnalogTriggerOutput.AnalogTriggerType.kState)

        # If we don't have source, generate it from other arguments.
        if source is None:
            if channel is not None:
                source = DigitalInput(channel)
                self.allocatedUpSource = True
                self.addChild(source)
            elif analogTrigger is not None and triggerType is not None:
                source = analogTrigger.createOutput(triggerType)
            else:
                raise ValueError("No usable source.")

        # save and set
        self.upSource = source
        hal.setCounterUpSource(self.counter,
                               self.upSource.getPortHandleForRouting(),
                               self.upSource.getAnalogTriggerTypeForRouting())

    def setUpSourceEdge(self, risingEdge, fallingEdge):
        """Set the edge sensitivity on an up counting source. Set the up
        source to either detect rising edges or falling edges.

        :param risingEdge: True to count rising edge
        :type  risingEdge: bool
        :param fallingEdge: True to count falling edge
        :type  fallingEdge: bool
        """
        if self.upSource is None:
            raise ValueError("Up Source must be set before setting the edge")
        hal.setCounterUpSourceEdge(self.counter, risingEdge, fallingEdge)

    def clearUpSource(self):
        """Disable the up counting source to the counter."""
        if self.upSource is not None and self.allocatedUpSource:
            self.upSource.free()
            self.allocatedUpSource = False
        self.upSource = None
        hal.clearCounterUpSource(self._counter)

    def setDownSource(self, *args, **kwargs):
        """Set the down counting source for the counter.

        This function accepts either a digital channel index, a
        `DigitalSource`, or an `AnalogTrigger` as positional arguments:

        - source
        - channel
        - analogTrigger
        - analogTrigger, triggerType

        For positional arguments, if the passed object has a
        `getChannelForRouting` function, it is assumed to be a DigitalSource.
        If the passed object has a `createOutput` function, it is assumed to
        be an AnalogTrigger.

        Alternatively, sources and/or channels may be passed as keyword
        arguments.  The behavior of specifying both a source and a number
        for the same channel is undefined, as is passing both a positional
        and a keyword argument for the same channel.

        :param channel: the DIO channel to use as the down source. 0-9 are on-board, 10-25 are on the MXP
        :type channel: int
        :param source: The digital source to count
        :type source: DigitalInput
        :param analogTrigger:
            The analog trigger object that is used for the Up Source
        :type analogTrigger: AnalogTrigger
        :param triggerType:
            The analog trigger output that will trigger the counter.
            Defaults to kState if not specified.
        :type triggerType: AnalogTrigger.AnalogTriggerType
        """

        #TODO Both this and the java implementation should probably not allow setting a source if one is already set.

        argument_templates = [[("channel", int)],
                              [("source", HasAttribute("getPortHandleForRouting")), ],
                              [("analogTrigger", HasAttribute("createOutput")), ],
                              [("analogTrigger", HasAttribute("createOutput")), ("triggerType", None)]]

        _, results = match_arglist('Counter.setDownSource',
                                   args, kwargs, argument_templates)

        # extract arguments
        source = results.pop("source", None)
        channel = results.pop("channel", None)
        analogTrigger = results.pop("analogTrigger", None)
        triggerType = results.pop("triggerType", AnalogTriggerOutput.AnalogTriggerType.kState)

        # If we don't have source, generate it from other arguments.
        if source is None:
            if channel is not None:
                source = DigitalInput(channel)
                self.allocatedDownSource = True
                self.addChild(source)
            elif analogTrigger is not None and triggerType is not None:
                source = analogTrigger.createOutput(triggerType)
            else:
                raise ValueError("No usable source.")

        # save and set
        self.downSource = source
        hal.setCounterDownSource(self.counter,
                                 self.downSource.getPortHandleForRouting(),
                                 self.downSource.getAnalogTriggerTypeForRouting())

    def setDownSourceEdge(self, risingEdge, fallingEdge):
        """Set the edge sensitivity on an down counting source. Set the down
        source to either detect rising edges or falling edges.

        :param risingEdge: True to count rising edge
        :type  risingEdge: bool
        :param fallingEdge: True to count falling edge
        :type  fallingEdge: bool
        """
        if self.downSource is None:
            raise ValueError("Down Source must be set before setting the edge")

        hal.setCounterDownSourceEdge(self.counter, risingEdge, fallingEdge)

    def clearDownSource(self):
        """Disable the down counting source to the counter.
        """
        if self.downSource is not None and self.allocatedDownSource:
            self.downSource.free()
            self.allocatedDownSource = False
        self.downSource = None

        hal.clearCounterDownSource(self._counter)

    def setUpDownCounterMode(self):
        """Set standard up / down counting mode on this counter. Up and down
        counts are sourced independently from two inputs.
        """
        hal.setCounterUpDownMode(self.counter)

    def setExternalDirectionMode(self):
        """Set external direction mode on this counter. Counts are sourced on
        the Up counter input. The Down counter input represents the direction
        to count.
        """
        hal.setCounterExternalDirectionMode(self.counter)

    def setSemiPeriodMode(self, highSemiPeriod):
        """Set Semi-period mode on this counter. Counts up on both rising and
        falling edges.

        :param highSemiPeriod: True to count up on both rising and falling
        :type  highSemiPeriod: bool
        """
        hal.setCounterSemiPeriodMode(self.counter, highSemiPeriod)

    def setPulseLengthMode(self, threshold):
        """Configure the counter to count in up or down based on the length
        of the input pulse. This mode is most useful for direction sensitive
        gear tooth sensors.

        :param threshold: The pulse length beyond which the counter counts the
            opposite direction. Units are seconds.
        :type  threshold: float, int
        """
        hal.setCounterPulseLengthMode(self.counter, float(threshold))

    def get(self):
        """Read the current counter value. Read the value at this instant. It
        may still be running, so it reflects the current value. Next time it
        is read, it might have a different value.
        """
        return hal.getCounter(self.counter)

    def getDistance(self):
        """Read the current scaled counter value. Read the value at this
        instant, scaled by the distance per pulse (defaults to 1).

        :returns: Scaled value
        :rtype: float
        """
        return self.get() * self.distancePerPulse

    def reset(self):
        """Reset the Counter to zero. Set the counter value to zero. This
        doesn't effect the running state of the counter, just sets the
        current value to zero.
        """
        hal.resetCounter(self.counter)

    def setMaxPeriod(self, maxPeriod):
        """Set the maximum period where the device is still considered
        "moving".  Sets the maximum period where the device is considered
        moving. This value is used to determine the "stopped" state of the
        counter using the :func:`getStopped` method.

        :param maxPeriod: The maximum period where the counted device is
            considered moving in seconds.
        :type maxPeriod: float or int
        """
        hal.setCounterMaxPeriod(self.counter, float(maxPeriod))

    def setUpdateWhenEmpty(self, enabled):
        """Select whether you want to continue updating the event timer
        output when there are no samples captured. The output of the event
        timer has a buffer of periods that are averaged and posted to a
        register on the FPGA. When the timer detects that the event source
        has stopped (based on the MaxPeriod) the buffer of samples to be
        averaged is emptied. If you enable update when empty, you will be
        notified of the stopped source and the event time will report 0
        samples. If you disable update when empty, the most recent average
        will remain on the output until a new sample is acquired. You will
        never see 0 samples output (except when there have been no events
        since an FPGA reset) and you will likely not see the stopped bit
        become true (since it is updated at the end of an average and
        there are no samples to average).

        :param enabled: True to continue updating
        :type  enabled: bool
        """
        hal.setCounterUpdateWhenEmpty(self.counter, enabled)

    def getStopped(self):
        """Determine if the clock is stopped. Determine if the clocked input
        is stopped based on the MaxPeriod value set using the
        :func:`setMaxPeriod` method.  If the clock exceeds the MaxPeriod,
        then the device (and counter) are assumed to be stopped and it
        returns True.

        :returns: Returns True if the most recent counter period exceeds the
            MaxPeriod value set by SetMaxPeriod.
        :rtype: bool
        """
        return hal.getCounterStopped(self.counter)

    def getDirection(self):
        """The last direction the counter value changed.

        :returns: The last direction the counter value changed.
        :rtype: bool
        """
        return hal.getCounterDirection(self.counter)

    def setReverseDirection(self, reverseDirection):
        """Set the Counter to return reversed sensing on the direction. This
        allows counters to change the direction they are counting in the case
        of 1X and 2X quadrature encoding only. Any other counter mode isn't
        supported.

        :param reverseDirection: True if the value counted should be negated.
        :type  reverseDirection: bool
        """
        hal.setCounterReverseDirection(self.counter, reverseDirection)

    def getPeriod(self):
        """Get the Period of the most recent count. Returns the time interval
        of the most recent count. This can be used for velocity calculations
        to determine shaft speed.

        :returns: The period of the last two pulses in units of seconds.
        :rtype: float
        """
        return hal.getCounterPeriod(self.counter)

    def getRate(self):
        """Get the current rate of the Counter. Read the current rate of the
        counter accounting for the distance per pulse value. The default
        value for distance per pulse (1) yields units of pulses per second.

        :returns: The rate in units/sec
        :rtype: float
        """
        return self.distancePerPulse / self.getPeriod()

    def setSamplesToAverage(self, samplesToAverage):
        """Set the Samples to Average which specifies the number of samples
        of the timer to average when calculating the period. Perform averaging
        to account for mechanical imperfections or as oversampling to increase
        resolution.

        :param samplesToAverage: The number of samples to average from 1 to 127.
        :type  samplesToAverage: int
        """
        hal.setCounterSamplesToAverage(self.counter, samplesToAverage)

    def getSamplesToAverage(self):
        """Get the Samples to Average which specifies the number of samples
        of the timer to average when calculating the period. Perform averaging
        to account for mechanical imperfections or as oversampling to increase
        resolution.

        :returns: The number of samples being averaged (from 1 to 127)
        :rtype: int
        """
        return hal.getCounterSamplesToAverage(self.counter)

    def setDistancePerPulse(self, distancePerPulse):
        """Set the distance per pulse for this counter. This sets the
        multiplier used to determine the distance driven based on the count
        value from the encoder. Set this value based on the Pulses per
        Revolution and factor in any gearing reductions. This distance can be
        in any units you like, linear or angular.

        :param distancePerPulse:
            The scale factor that will be used to convert pulses to useful
            units.
        :type  distancePerPulse: float
        """
        self.distancePerPulse = distancePerPulse

    def setPIDSourceType(self, pidSource):
        """Set which parameter of the encoder you are using as a process
        control variable. The counter class supports the rate and distance
        parameters.

        :param pidSource: An enum to select the parameter.
        :type  pidSource: :class:`Counter.PIDSourceType`
        """
        if pidSource not in (self.PIDSourceType.kDisplacement,
                             self.PIDSourceType.kRate):
            raise ValueError("Invalid pidSource argument '%s'" % pidSource)
        self.pidSource = pidSource

    def getPIDSourceType(self):
        return self.pidSource

    def pidGet(self):
        if self.pidSource == self.PIDSourceType.kDisplacement:
            return self.getDistance()
        elif self.pidSource == self.PIDSourceType.kRate:
            return self.getRate()
        else:
            return 0.0

    def initSendable(self, builder):
        builder.setSmartDashboardType("Counter")
        builder.addDoubleProperty("Value", self.get, None)


import json
import scrapy

from locations.items import GeojsonPointItem
day_formats = {
    "1": "Mo",
    "2": "Tu",
    "3": "We",
    "4": "Th",
    "5": "Fr",
    "6": "Sa",
    "7": "Su"
}

class HugoBossSpider(scrapy.Spider):
    name = "hugoboss"
    item_attributes = { 'brand': "Hugo Boss" }
    allowed_domains = ["production-web-hugo.demandware.net"]
    download_delay = 0.5
    start_urls = (
        'https://production-web-hugo.demandware.net/s/US/dw/shop/v16_9/stores?client_id=871c988f-3549-4d76-b200-8e33df5b45ba&latitude=37.09024&longitude=-95.71289100000001&count=200&maxDistance=100000000&distanceUnit=mi',
    )
    count =0;

    def parse(self, response):
      data = json.loads(response.body_as_unicode())
      if "data" in data:
        for store in data['data']:
            clean_hours = ''
            if "store_hours" in store:
                open_hours = json.loads(store['store_hours'])
                for key , value in open_hours.items():
                  if isinstance(value[0], str):
                    clean_hours = clean_hours + day_formats[key] + ' ' + value[0] + '-' + value[1] + '; '
                  else:
                    clean_hours = clean_hours + day_formats[key] + ' ' + value[0][0] + '-' + value[0][1] + '; '
            if "postal_code" in store:
                postal_code = store["postal_code"]
            else:
                postal_code=""
            if "phone" in store:
                phone = store["phone"]
            else:
                phone=""
            properties = {
                "ref": store['id'],
                "name": store['name'],
                "opening_hours": clean_hours,
                "website": "https://www.hugoboss.com/us/stores",
                "addr_full": store['address1'],
                "city": store['city'],
                "postcode": postal_code,
                "country": store['country_code'],
                "lat": float(store['latitude']),
                "lon": float(store['longitude']),
                "phone": phone,
            }

            yield GeojsonPointItem(**properties)
        self.count = self.count + len(data['data'])
        yield  scrapy.Request(response.urljoin(self.start_urls[0]+'&start='+str(self.count)), callback=self.parse)


# -*- coding: utf-8 -*-
"""
Created on Wed Mar 14 14:40:38 2018

@author: Liaowei
"""

import pandas as pd
import numpy as np
import time
import datetime
import os

from utils import raw_data_path,feature_data_path,result_path,cache_pkl_path,dump_pickle,load_pickle,build_train_dataset,cal_log_loss,cal_single_log_loss
from smooth import BayesianSmoothing
import gen_smooth_features as smooth_features
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import minmax_scale
import xgboost as xgb
import operator
import matplotlib.pyplot as plt
params={
    'booster':'gbtree',
    'objective': 'binary:logistic',
    'eval_metric':'logloss',
    'max_depth':8,
    'colsample_bytree': 1,
#    'lambda':1,
#    'eta':0.2,
#    'silent':0,
#    'alpha':3,
#    'subsample':1,
}
n_round=20

if __name__ == '__main__':

    t0 = time.time()
    train_data = load_pickle(path=cache_pkl_path +'train_data_onehot')
    train_Y = load_pickle(path=cache_pkl_path +'train_Y')
    cv_data = load_pickle(path=cache_pkl_path +'cv_data_onehot')
    cv_Y = load_pickle(path=cache_pkl_path +'cv_Y')

    test_data = load_pickle(path=cache_pkl_path +'test_data_onehot')
    test_file = 'round1_ijcai_18_test_a_20180301.txt'
    test = pd.read_table(raw_data_path + test_file,delim_whitespace=True)
    test_id = test.instance_id

    drop_cols = ['is_trade','user_id','shop_id','item_id','item_brand_id']
    train_data.drop(drop_cols,axis=1,inplace=True)
    cv_data.drop(drop_cols,axis=1,inplace=True)
    test_data.drop(drop_cols,axis=1,inplace=True)

    print('train shap:',train_data.shape)
    print('cv shape', cv_data.shape)
    print('test shape', test_data.shape)

    clf = LogisticRegression(C=0.5, fit_intercept=True, max_iter=1000,class_weight={0:0.5, 1:0.5})
    clf.fit(X=train_data.values, y=np.squeeze(train_Y))

    predict_train_fir = clf.predict_proba(train_data.values)[:,1]
    predict_cv_fir = clf.predict_proba(cv_data.values)[:,1]
    predict_test_fir = clf.predict_proba(test_data.values)[:,1]

    print('训练损失:',cal_log_loss(predict_train_fir, train_Y))
    print('测试损失:',cal_log_loss(predict_cv_fir, cv_Y))

    #把全量数据拿过来训练
    train_data_all = pd.concat([train_data, cv_data],axis=0)
    train_Y_all = np.append(train_Y, cv_Y)
    clf.fit(X=train_data_all.values, y=np.squeeze(train_Y_all))
    print('训练损失:',cal_log_loss(clf.predict_proba(train_data_all.values)[:,1], train_Y_all))
    predict_test_fir = clf.predict_proba(test_data.values)[:,1]

    #保存结果
    submission = pd.DataFrame({'instance_id':test_id,'predicted_score':predict_test_fir})
    print('预测正样本比例:',len(submission.loc[submission.predicted_score>=0.5])/len(submission))
    submission.to_csv(r'../result/LR_{}.txt'.format(datetime.datetime.now().strftime('%Y%m%d_%H%M%S')),
                  index=False, sep=' ',line_terminator='\r')
    #%%
    '''
    粗筛后删除分数较低的那些样本
    '''
    #按一定比例抽取概率较低的数据，作为LR的结果
    precent_2 = 0.96
    LR_cv_len = int(len(cv_data)*precent_2)
    LR_test_len = int(len(test_data)*precent_2)

    #LR的较低的分数直接拼接到最后的结果中
    predict_cv_df = pd.DataFrame({'col_index':cv_data.index,'score':predict_cv_fir})
    predict_cv_df.sort_values(by='score',inplace=True)
    predict_test_df = pd.DataFrame({'col_index':test_data.index,'score':predict_test_fir})
    predict_test_df.sort_values(by='score',inplace=True)

    LR_cv_score = predict_cv_df.iloc[0:LR_cv_len]
    LR_test_score = predict_test_df.iloc[0:LR_test_len]

    XGB_cv_index = predict_cv_df.iloc[LR_cv_len:]['col_index'].values
    XGB_test_index = predict_test_df.iloc[LR_test_len:]['col_index'].values

    #较难训练的样本的index 采用XGBoost来训练
    precent_1 = 0.65
    drop_len = int(len(train_data)*precent_1)

    predict_train_df = pd.DataFrame({'col_index':train_data.index,'score':predict_train_fir})
    predict_train_df.sort_values(by='score',inplace=True)

    #概率排名较低的前drop_len个样本中，如果label是0，则删除
    drop_index = predict_train_df.col_index[0:drop_len].apply(lambda x:(train_Y[x]==0))
    drop_index = drop_index[drop_index==True].index

    LR_train_score = predict_train_df.loc[drop_index]
    #得到保留下来的，较难训练的样本
    predict_train_df.drop(drop_index, inplace=True)
    reserved_index  = predict_train_df.col_index.values

    #读取XGBoost特征，训练数据
    train_data = load_pickle(path=cache_pkl_path +'train_data')
    cv_data = load_pickle(path=cache_pkl_path +'cv_data')
    test_data = load_pickle(path=cache_pkl_path +'test_data')

    train_data_sec = train_data.loc[reserved_index,:]
    train_Y_sec = train_Y[reserved_index]

    train_feat = xgb.DMatrix(train_data_sec.values, label=train_Y_sec)
    cv_feat = xgb.DMatrix(cv_data.values)
    test_feat = xgb.DMatrix(test_data.values)
    clf = xgb.train(params=params, dtrain=train_feat,num_boost_round=n_round)

    #得到XGBoost训练的结果
    predict_train_sec = clf.predict(train_feat)
    predict_cv_sec = clf.predict(cv_feat)
    predict_test_sec = clf.predict(test_feat)

    #把全量数据拿过来训练
#    train_data_all = pd.concat([train_data_sec, cv_data],axis=0)
#    train_Y_all = np.append(train_Y_sec, cv_Y)
#    train_feat_all = xgb.DMatrix(train_data_all.values, label=train_Y_all)
#    clf = xgb.train(params=params, dtrain=train_feat,num_boost_round=n_round)
#    predict_test_sec= clf.predict(test_feat)

    XGB_train_score = pd.DataFrame({'col_index':train_data_sec.index,'score':predict_train_sec})
    XGB_cv_score = pd.DataFrame({'col_index':cv_data.index,'score':predict_cv_sec})
    XGB_cv_score = XGB_cv_score.loc[XGB_cv_index]
    XGB_test_score = pd.DataFrame({'col_index':test_data.index,'score':predict_test_sec})
    XGB_test_score = XGB_test_score.loc[XGB_test_index]

    #将LR和XGBoost的结果混合
    train_score_all = pd.concat([LR_train_score, XGB_train_score],axis=0)
    train_score_all.sort_values(by='col_index',inplace=True)
    cv_score_all = pd.concat([LR_cv_score, XGB_cv_score],axis=0)
    cv_score_all.sort_values(by='col_index',inplace=True)
    test_score_all = pd.concat([LR_test_score, XGB_test_score],axis=0)
    test_score_all.sort_values(by='col_index',inplace=True)

    print('训练损失:',cal_log_loss(train_score_all.score.values, train_Y))
    print('测试损失:',cal_log_loss(cv_score_all.score.values, cv_Y))
    t1 = time.time()
    print('训练用时：',t1-t0)

    submission = pd.DataFrame({'instance_id':test_id,'predicted_score':test_score_all.score.values})
    print('预测正样本比例:',len(submission.loc[submission.predicted_score>=0.5])/len(submission))
    submission.to_csv(r'../result/LR_XGB_{}.txt'.format(datetime.datetime.now().strftime('%Y%m%d_%H%M%S')),
                  index=False, sep=' ',line_terminator='\r')


#!/usr/bin/python
import tkinter as tk
import tkinter.font as font
from enum import Enum

import RPi.GPIO as GPIO
from time import sleep
from threading import Thread

enaLF = 4
enaLB = 19
enaRF = 16
enaRB = 26

dirLF = 27
dirLB = 20
dirRF = 6
dirRB = 13

pulLF = 23
pulLB = 12
pulRF = 5
pulRB = 21

class Status(Enum):
    FORWARD = 1
    STOP = 2
    BACKWARD = 3

class Window(tk.Frame):

    def __init__(self, parent):
        super(Window, self).__init__(parent)
        self.direction = Status.STOP
        self.buttonFont = font.Font(family='Helvetica', size=12, weight='bold')
        self.forwardB = tk.Button(self, text="  Forward  ", font=self.buttonFont, command=self.forward)
        self.backwardB = tk.Button(self, text="  Backward  ", font=self.buttonFont, command=self.backward)
        self.stopB = tk.Button(self, text="   Stop   ", font=self.buttonFont, command=self.stop)
        self.fastB = tk.Button(self, text="   fast   ", font=self.buttonFont, command=self.set_fast, state=tk.DISABLED)
        self.slowB = tk.Button(self, text="   slow   ", font=self.buttonFont, command=self.set_slow)
        self.statusL = tk.Label(self, text="Status: ", font=self.buttonFont)
        self.speedL = tk.Label(self, text="Speed", font=self.buttonFont)

        self.statusL.grid(row=0, column=0, columnspan=2)
        self.forwardB.grid(row=1, column=0)
        self.stopB.grid(row=1, column=1)
        self.backwardB.grid(row=1, column=2)
        self.speedL.grid(row=2, column=0)
        self.fastB.grid(row=2, column=1)
        self.slowB.grid(row=2, column=2)

        self.fast = 0.0025
        self.slow = 0.0050
        self.speed = self.fast
        self.init()

    def init(self):
        # init GPIO pins
        GPIO.setmode(GPIO.BCM)

        GPIO.setup(enaLF, GPIO.OUT)
        GPIO.setup(dirLF, GPIO.OUT)
        GPIO.setup(pulLF, GPIO.OUT)
        GPIO.setup(enaLB, GPIO.OUT)
        GPIO.setup(dirLB, GPIO.OUT)
        GPIO.setup(pulLB, GPIO.OUT)
        GPIO.setup(enaRF, GPIO.OUT)
        GPIO.setup(dirRF, GPIO.OUT)
        GPIO.setup(pulRF, GPIO.OUT)
        GPIO.setup(enaRB, GPIO.OUT)
        GPIO.setup(dirRB, GPIO.OUT)
        GPIO.setup(pulRB, GPIO.OUT)

        GPIO.output(enaLF, GPIO.LOW)
        GPIO.output(dirLF, GPIO.LOW)
        GPIO.output(pulLF, GPIO.LOW)
        GPIO.output(enaLB, GPIO.LOW)
        GPIO.output(dirLB, GPIO.LOW)
        GPIO.output(pulLB, GPIO.LOW)
        GPIO.output(enaRF, GPIO.LOW)
        GPIO.output(dirRF, GPIO.LOW)
        GPIO.output(pulRF, GPIO.LOW)
        GPIO.output(enaRB, GPIO.LOW)
        GPIO.output(dirRB, GPIO.LOW)
        GPIO.output(pulRB, GPIO.LOW)

    def __del__(self):
        GPIO.cleanup()

    def set_fast(self):
        self.speed = self.fast
        self.fastB.configure(state=tk.DISABLED)
        self.slowB.configure(state=tk.NORMAL)

    def set_slow(self):
        self.speed = self.slow
        self.fastB.configure(state=tk.NORMAL)
        self.slowB.configure(state=tk.DISABLED)

    def forward(self):
        print("move forward")

    def backward(self):
        print("move backward")

    def stop(self):
        print("move stop")

class SideWalkMotor(Window):
    def __init__(self, parent):
        super(SideWalkMotor, self).__init__(parent)
        # self.thread = Thread(target=self.move)
        # self.thread.start()

    def forward(self):
        self.set_direction(Status.FORWARD)
        self.statusL.configure(text="Status: moving forward ")
        print("forward")
        thread = Thread(target=self.move).start()

    def backward(self):
        self.set_direction(Status.BACKWARD)
        self.statusL.configure(text="Status: moving backward")
        print("backward")
        thread = Thread(target=self.move).start()

    def stop(self):
        self.direction = Status.STOP
        self.statusL.configure(text="Status: stopped")
        print("stop")
        self.disableMotor()

    def move(self):
        while(self.direction != Status.STOP):
            GPIO.output(pulLF, GPIO.HIGH)
            GPIO.output(pulLB, GPIO.HIGH)
            GPIO.output(pulRF, GPIO.HIGH)
            GPIO.output(pulRB, GPIO.HIGH)
            sleep(self.speed)
            GPIO.output(pulLF, GPIO.LOW)
            GPIO.output(pulLB, GPIO.LOW)
            GPIO.output(pulRF, GPIO.LOW)
            GPIO.output(pulRB, GPIO.LOW)
            sleep(self.speed)

    def set_direction(self, direction):
        self.direction = direction
        self.enableMotor()
        if(direction == Status.FORWARD):
            GPIO.output(dirLF, GPIO.HIGH)
            GPIO.output(dirLB, GPIO.HIGH)
            GPIO.output(dirRF, GPIO.LOW)
            GPIO.output(dirRB, GPIO.LOW)
        else:
            GPIO.output(dirLF, GPIO.LOW)
            GPIO.output(dirLB, GPIO.LOW)
            GPIO.output(dirRF, GPIO.HIGH)
            GPIO.output(dirRB, GPIO.HIGH)

    def enableMotor(self):
        GPIO.output(enaLF, GPIO.HIGH)
        GPIO.output(enaLB, GPIO.HIGH)
        GPIO.output(enaRF, GPIO.HIGH)
        GPIO.output(enaRB, GPIO.HIGH)

    def disableMotor(self):
        GPIO.output(enaLF, GPIO.LOW)
        GPIO.output(enaLB, GPIO.LOW)
        GPIO.output(enaRF, GPIO.LOW)
        GPIO.output(enaRB, GPIO.LOW)

if __name__ == "__main__":
    root = tk.Tk()
    root.title("Bureau Of Engineering - Sidewalk Slope Monitoring Motor Move")
    root.geometry("350x200")
    main = SideWalkMotor(root)
    main.pack(fill="both", expand=True)
    root.mainloop()


from django.conf.urls import url

from . import views

app_name = 'bets'

urlpatterns = [
    url(r'^$', views.IndexView.as_view(), name='front_page'),
    url(
        r'^match/(?P<pk>\d+)/(?P<team_pk>\d+)/bet/$',
        views.match_bet_form,
        name='match_bet'
    ),
]


import overnight_basket
import config

import time
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

from datetime import datetime

## 성과 출력 전 백테스트 데이터 정보 추가
def make_stategy_result_data(data):
    temp_data = data

    roll_max_strategy = temp_data['Basket'].rolling(len(temp_data.index), min_periods=1).max()
    daily_drawdown_strategy = temp_data['Basket'] / roll_max_strategy - 1.0
    max_daily_drawdown_strategy = daily_drawdown_strategy.rolling(len(temp_data.index), min_periods=1).min()
    max_draw_dowm_strategy = round(abs(pd.Series.min(max_daily_drawdown_strategy)) * 100, 2)

    return_portfolio_log = np.log(temp_data['Basket'] / temp_data['Basket'].shift(1))
    volatility_month = pd.DataFrame.rolling(return_portfolio_log, window = 20).std() * np.sqrt(20)
    volatility_year = pd.DataFrame.rolling(return_portfolio_log, window = 252).std() * np.sqrt(252)

    temp_data.insert(len(temp_data.columns), "daily_drawdown", daily_drawdown_strategy)
    temp_data.insert(len(temp_data.columns), "max_draw_down", max_daily_drawdown_strategy)
    temp_data.insert(len(temp_data.columns), "volatility_month", volatility_month)
    temp_data.insert(len(temp_data.columns), "volatility_year", volatility_year)

    return temp_data

class show_result_strategy():
    def __init__(self, trading_data):
        self.manufactured_data = make_stategy_result_data(trading_data)

    ## 주요 성과지표 출력
    def show_strategy_result(self):

        ##맨 마지막 행 삭제
        data = self.manufactured_data.drop(len(self.manufactured_data.index) - 1)

        ## 백테스트 시작, 종료일자 출력
        start_date = str(data.at[0, 'Date'])
        end_date = str(data.at[len(data.index) - 1, 'Date'])

        ## 주요 성과지표 계산
        total_return = data.at[len(data.index) - 1, 'Basket'] / data.at[0, 'Basket']
        cagr = round(((total_return)**(1/int(len(data.index)/365.0)) - 1), 4) * 100
        max_draw_down = round(abs(pd.Series.min(data["max_draw_down"])) * 100, 2)
        volatility_average_month = round(np.nanmean(data["volatility_month"]) * 100, 4)
        volatility_average_year = round(np.nanmean(data["volatility_year"]) * 100, 4)

        ## 성과지표 출력
        print('Strategy : %s' %config.strategy_name)
        print('start : %s-%s-%s' %(start_date[:4], start_date[4:6],start_date[6:8]))
        print('end   : %s-%s-%s' %(end_date[:4], end_date[4:6], end_date[6:8]))
        print('----------------------')
        print('Total Return : %.2f %%' %(total_return * 100))
        print('CAGR  : %.2f %%' %cagr)
        print('MDD   : %.2f %%' %max_draw_down)
        print('Month Volatility (20 day Average) : %.4f %%' %volatility_average_month)
        print('Year Volatility (252 day Average) : %.4f %%' %volatility_average_year)

    ## 그래프 출력
    def show_asset_growth_graph(self):
        data = self.manufactured_data

        fig = plt.figure(figsize=(16, 9))

        top_axes = plt.subplot2grid((4,4), (0,0), rowspan=3, colspan=4)
        bottom_axes = plt.subplot2grid((4,4), (3,0), rowspan=1, colspan=4)

        top_axes.plot(data.index, data['Basket'], label='Basket')

        bottom_axes.plot(data.index, data["daily_drawdown"], label="daily_drawdown")
        bottom_axes.plot(data.index, data["max_draw_down"], label="max_draw_down")

        top_axes.legend(loc='best')
        data = data.set_index('Date')
        data.to_excel("./Backtest_result.xlsx", encoding = 'euc_KR')

        plt.savefig("./graph.png", dpi=240)
        plt.title('Asset Growth, Max Draw Down Graph')
        plt.tight_layout()
        plt.show()
        plt.close(fig)


import mxnet as mx
import mxnet.ndarray as nd
import mxnet.gluon.nn as nn

class Model(object):
    def __init__(self, ctx='cpu', file=None):
        self.ctx = mx.cpu() if ctx != 'gpu' else mx.gpu()

        self.net = nn.HybridSequential()
        with self.net.name_scope():
            self.net.add(
                nn.Conv2D(64, kernel_size=(7,7), padding=(3,3), activation='relu'),
                nn.Conv2D(64, kernel_size=(1,1), activation='relu'),
                nn.Conv2D(64, kernel_size=(1,1), activation='relu'),
                nn.MaxPool2D(pool_size=(2,2), strides=(2,2)),

                nn.Conv2D(96, kernel_size=(5,5), padding=(2,2), activation='relu'),
                nn.Conv2D(96, kernel_size=(1,1), activation='relu'),
                nn.Conv2D(96, kernel_size=(1,1), activation='relu'),
                nn.MaxPool2D(pool_size=(2,2), strides=(2,2)),

                nn.Conv2D(128, kernel_size=(3,3), padding=(1,1), activation='relu'),
                nn.Conv2D(128, kernel_size=(1,1), activation='relu'),
                nn.Conv2D(128, kernel_size=(1,1), activation='relu'),
                nn.MaxPool2D(pool_size=(2,2), strides=(2,2)),

                nn.Dropout(0.5),

                nn.Conv2D(10, kernel_size=(3,3), padding=(1,1), activation='relu'),
                nn.Conv2D(10, kernel_size=(1,1), activation='relu'),
                nn.Conv2D(10, kernel_size=(1,1), activation='relu'),

                nn.GlobalAvgPool2D(),
                nn.Flatten()
            )

        if file != None:
            self.net.load_params(file, ctx=self.ctx)
        else:
            self.net.initialize(ctx=self.ctx, init=mx.init.Xavier())

        self.trainer = mx.gluon.Trainer(self.net.collect_params(), 'adam')
        self.net.hybridize()

    def train(self, batches):
        loss = mx.gluon.loss.SoftmaxCrossEntropyLoss()
        total_L = 0
        for x, y in batches:
            x, y = nd.array(x, self.ctx), nd.array(y, self.ctx)
            with mx.autograd.record():
                p = self.net(x)
                L = loss(p, y).mean()
            L.backward()
            total_L += L.asscalar() / len(batches)
            self.trainer.step(1)
        return total_L

    def predict(self, x):
        return self.net(nd.array(x, self.ctx)).argmax(axis=1).asnumpy()

    def save(self, file):
        return self.net.save_params(file)


### Get CT images from DICOM ###

import numpy as np
from pydicom import dcmread

def get_dcm_img(dcm_path):
    ds = dcmread(dcm_path)
    def get_pixels_hu(slices):
        image = slices.pixel_array
        image = image.astype(np.int16)
        image[image == -2000] = 0
        intercept = slices.RescaleIntercept  #Intercept
        slope = slices.RescaleSlope  #Rescale
        if slope != 1:
            image = slope * image.astype(np.float64)
            image = image.astype(np.int16)
        image += np.int16(intercept)
        return np.array(image, dtype=np.int16)
    pixels_hu = get_pixels_hu(ds)

    temp = pixels_hu
    temp = temp[temp>=90]
    temp = temp[temp<=110]
    counts = np.bincount(temp)

    pixels_hu[pixels_hu<-50] = -50
    pixels_hu[pixels_hu>200] = 200

    minimum = np.min(pixels_hu)
    maximum = np.max(pixels_hu)
    pixels_hu = (pixels_hu-minimum)*255./(maximum-minimum)

    pixels_hu = pixels_hu.astype(np.float32)
    return pixels_hu


# use transpose according to dimension of dataset ,it should have feature in row and no of sample in column

#import data that you needed
#Use only the dataset that is feedforwarded to last layer using trained weights  and have to use for classification
import numpy as np
import pandas as pd
import tensorflow as tf
import scipy.io as sio
##   NASA dataset 1
f0=sio.loadmat('trainedNASA.mat')
f0=sio.loadmat('TrainedMNIST.mat')
f0=sio.loadmat('CWRUTrain.mat')
f1=sio.loadmat('CWRUTest2.mat')

xtrain=f0['training_inputs']
xtest=f1['test_inputs']
ytrain=f0['training_results']
ytest=f1['test_results']

xtrain=xtrain.T
ytrain=ytrain.T
xtest=xtest.T
ytest=ytest.T

from sklearn.svm import SVC
svm_model= SVC( kernel='linear', C =5).fit(xtrain, ytrain)  # for linear
#svm_model = SVC( kernel='rbf', C =5,gamma=0.341).fit(xtrain, ytrain)  # for rbf

svm_predictions = svm_model.predict(xtest)
accuracy = svm_model.score(xtest, ytest)
accuracy
#
from sklearn.ensemble import RandomForestClassifier                     # for rf
dt_model=RandomForestClassifier(n_estimators =100).fit(xtrain,ytrain)
accuracy=dt_model.score(xtest,ytest)
accuracy