'''Various configuration parameters and functions
'''

import logging
import os

# ----------------------------------------------------------------------
# Default parameters for use with matplotlib.pyplot.rcParams.update

_MPL_CONFIG = {
    "figure.figsize": [6.0, 4.5],
    "grid.linewidth": 1.2,
    "grid.linestyle": "--",
    "axes.linewidth": 1.2,
    "lines.linewidth": 1.2,
    "figure.dpi": 80,
    "savefig.dpi": 200,
    "font.size": 12,
    "legend.fontsize": 12,
    "figure.titlesize": 12,
    "patch.force_edgecolor": "True",
    "patch.facecolor": "b",
    "font.sans-serif": "Times New Roman",
    "font.family": "sans-serif"
    }

# ----------------------------------------------------------------------
# Default parameters for special plots

_SETTLING_BAND_STYLES = dict(
    color='g',
    linestyle='--'
    )

_SETTING_TIME_STYLES = dict(
    color='r',
    linestyle='--'
    )

_RISE_SETTLE_TEXT_STYLES = dict(
    horizontalalignment='center',
    verticalalignment='center',
    bbox=dict(facecolor='white', alpha=0.8)
    )

# ----------------------------------------------------------------------
# Default folder structure and functions relative to specified root /

PSSPLOT_FOLDERS = {
    'out_files': r'data\out_files',
    'excel_files': r'data\excel',
    'output_folder': r'output',
    'plot_folder': r'plot_files'
    }

# ----------------------------------------------------------------------
# Logger initialisation

def init_logger(name):
    'Create logger with set parameters'
    _logger = logging.getLogger(name)
    _format = '[%(asctime)s]:%(levelname)s:%(name)s:%(funcName)s:%(message)s'
    logging.basicConfig(
        level=logging.INFO,
        format=_format,
        datefmt='%Y-%m-%d %H:%M:%S'
        )
    return _logger


from node import Node

class LinkedList:

    def __init__(self):
        self.head = None

    def insert_node(self, value, index=None):
        new_node = Node(value)
        if self.head is None:
            self.head = new_node
        elif value <= self.head.value:
            new_node.next = self.head
            self.head = new_node
        else:
            previous = self.head
            runner = self.head.next

            while (runner is not None) and (value > runner.value):
                previous = runner
                runner = runner.next

            new_node.next = runner
            previous.next = new_node


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

# ExportDemexTradesAndTransfersToKoinly
# Copyright © 2021 Eporediese for Switcheo / MaiTePora <3
# Licenced under GPL

# Creates CSV files in a format the the tax software Koinly understands, from Demex account trades and transfers for any Demex addresses
# The CSV files are written in the location that this script is executed
# This is not tax advice. The user accepts that the output of this tool should be reviewed for accuracy and completeness

# LIBRARYS
# ------------------------------------------------------------------------------
# Tradehub Python API: https://mai-te-pora.github.io/tradehub-python/index.html
import tradehub
from tradehub.wallet import Wallet
from tradehub.transactions import Transactions
from tradehub.authenticated_client import AuthenticatedClient
from tradehub.types import CreateOrderMessage

# Date manipulation
import datetime;
import time;
from datetime import date

# Regular expressions
import re

# PARAMETERS
# ------------------------------------------------------------------------------
# Provide the mneumonic of any wallet (can be an empty wallet)
myMnemonic = 'these are twelve words that used together allow access to your wallet' #Tradehub mnemonic (example here: 12 word TradeHub mnemonic)

# Provide the Demex addresses that you want to pull trades and transfers for
demexAddresses = [
    'swth1primary', # Primary
    'swth1secondary' # Secondary
]

# CONSTANTS
# ------------------------------------------------------------------------------
demPK = Wallet(myMnemonic, network='mainnet')
clientDem = AuthenticatedClient(demPK, network='mainnet',trusted_ips=None, trusted_uris=['http://54.255.5.46:5001', 'http://175.41.151.35:5001'])

# MAIN
# ------------------------------------------------------------------------------
print(f'Connected To Tradehub --- {demPK.address}')

transDem = Transactions(demPK,
             trusted_ips=None,
             trusted_uris=['http://54.255.5.46:5001', 'http://175.41.151.35:5001'],
             network="mainnet")

# Iterate over Demex adresses

for address in demexAddresses:

    #print (address)

    # Get external transfers for Demex address
    transfers = transDem.get_external_transfers(address)
    #print (transfers)

    # Get trades (note: can set date range if needed)
    trades = transDem.get_address_trades('','',address) # Limited to 200 trades?
    #print (trades)

    # ct stores current time
    ct = datetime.datetime.now()
    #print("current time:-", ct)

    # Convert to a format that can be used in a filename
    fileDate = str(ct.month) + str(ct.day) + str(ct.year) + str(ct.hour) + str(ct.minute) + str(ct.second)
    #print (fileDate)

    # Open the output Koinly transfer history CSV file
    transferFilename = "koinly_transfer_history_for_" + address + "_at_" + str(fileDate) + ".csv"
    #print (transferFilename)
    transferFile = open(transferFilename, "w")

    # Open the output Koinly trade history CSV file
    tradeFilename = "koinly_trade_history_for_" + address + "_at_" + str(fileDate) + ".csv"
    #print (transferFilename)
    tradeFile = open(tradeFilename, "w")

    # Write out CSV files in Koinly file format
    # Trasfer tmplate is here: https://docs.google.com/spreadsheets/d/13H9kREcrNJmCNxkpYK-AaRlkWh8PSshbpPBmewmDX4o/edit#gid=0
    # Trade template is here: https://docs.google.com/spreadsheets/d/1MEVrdiS941S-s0igBZKh-pbW5TqJb4mpfkmnDzDkCcg/edit#gid=0
    # All date fields must be formatted like this: YYYY-MM-DD HH:mm:ss.
    # For ex. if you want to enter 5th Jan 2019 the date will be 2019-01-05.
    # All dates should be in UTC.

    transferHeader = "Koinly Date,Amount,Currency,Label,TxHash\n"
    #print (header)
    transferFile.write(transferHeader)

    tradeHeader = "Koinly Date,Pair,Side,Amount,Total,Fee Amount,Fee Currency,Order ID,Trade ID\n"
    tradeFile.write(tradeHeader)

# Iterate over transfers
# Koinly Date	Amount	Currency	Label	TxHash

for transfer in transfers:

    #print (transfer)

    # Extract timestamp
    transferEpoch = transfer['timestamp']
    #print (transferEpoch)

    # Convert Epoch timestamp format to YYYY-MM-DD HH:mm:ss format
    # In UTC
    koinlyTimestamp = datetime.datetime.utcfromtimestamp(transferEpoch).strftime('%Y-%m-%d %H:%M:%S')

    # Extract amount
    transferAmount = transfer['amount']

    # Extract currency
    transferSymbol = transfer['symbol']

    # Convert Demex to Koinly symbols
    if (transferSymbol == "SWTHN"):
        koinlySymbol = "SWTH"
    else:
        koinlySymbol = transferSymbol

    # Extract Transaction type
    transferType = transfer['transfer_type']

    # Extract Transaction hash to use as label
    transferHash = transfer['transaction_hash']

    # Extract Transaction status
    transferStatus = transfer['status']

    if (transferStatus == 'success'):
        transferFile.write(koinlyTimestamp + "," + transferAmount + "," + koinlySymbol.upper() + "," + transferType + "," + transferHash + "\n")

# Iterate over trades
# Koinly Date	Pair	Side	Amount	Total	Fee Amount	Fee Currency	Order ID	Trade ID

for trade in trades:

    #print (trade)

    # Extract timestamp
    tradeEpoch = trade['block_created_at']
    #print (tradeEpoch)

    # Convert block timestamp format to YYYY-MM-DD HH:mm:ss format

    s = re.findall('(.+)T(.+)\.', tradeEpoch)
    #print (s)
    #print (s[0][0])
    #print (s[0][1])
    koinlyTimestamp = s[0][0] + " " + s[0][1]
    #print (koinlyTimestamp)

    # Extract amount
    tradeAmount = trade['quantity']

    # Construct Buy|Sell side
    if (trade['side'] == 'sell'):
        tradeSide = 'Sell'
    else:
        tradeSide = 'Buy'

    # Extract trade pair
    tradePairExtracted = trade['market']
    #print (tradePairExtracted)
    # Convert to Koinly format and known coins
    s = re.findall('(.+[^\d])[\d]*_(.+[^\d])[\d]*', tradePairExtracted)
    fromCurrencyExtracted = s[0][0].upper()
    toCurrencyExtracted = s[0][1].upper()
    tradePair = s[0][0].upper() + "-" + s[0][1].upper()
    #print (tradePair)

    # Extract trade price and use to calculate the total
    tradePrice = trade['price']
    #print (tradePrice)
    #print (tradeAmount)
    tradeTotal = float(tradePrice) * float(tradeAmount)
    #print (str(tradeTotal))

    # Extract fee amount
    tradeFeeAmount = str(trade['fee_amount'])

    # Extract fee total
    tradeFeeCurrencyExtracted = trade['fee_denom']
    #print (tradeFeeCurrencyExtracted)
    # Convert to Koinly format and known coins
    s = re.findall('(.+[^\d])[\d]*', tradeFeeCurrencyExtracted)
    tradeFeeCurrency = s[0].upper()
    #print (tradeFeeCurrency)

    # Extract order ID
    tradeOrderID = str(trade['order_id'])

    # Extract trade ID
    tradeTradeID = str(trade['id'])

    tradeFile.write(koinlyTimestamp + "," + tradePair + "," + tradeSide + "," + str(tradeAmount) + "," + str(tradeTotal) + "," + tradeFeeAmount + "," + tradeFeeCurrency + "," + tradeOrderID + "," + tradeTradeID + "\n")

transferFile.close()
tradeFile.close()


import os

import numpy as np
import tensorflow as tf
from absl import app, flags
from skimage import io
from tensorflow.compat.v1 import ConfigProto, InteractiveSession

from core.dataset import CreateDataset
from core.unet import Unet

# Setup dynamic gpu memory growth.
config = ConfigProto()
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)

FLAGS = flags.FLAGS

flags.DEFINE_string('logdir', None,
                    'the dir of weights file saved by model.save_weights().')
flags.DEFINE_string('label_file_path', None,
                    'Path of testing dataset label file.')
flags.DEFINE_integer('batch_size', 1, 'Global batch size.')
flags.DEFINE_integer('image_size', 1024, 'Size of each image side to use.')
flags.DEFINE_boolean('preprocess', False, 'Whether to use preprocessed image.')
flags.DEFINE_integer('num_classes', 2, 'Number of classes.')
flags.DEFINE_boolean('transpose_conv', False,
                     'Whether to use Conv2DTranspose as upsample layer.')
flags.DEFINE_boolean('visulize', False, 'Whether to visulize predicts.')

def visulize_predicts(predicts, images, labels, masks, logdir, idx):
  """ Visulizing predicts with different color:
    error -> red, right -> green, miss -> blue.

  Args:
    predicts: numpy array, shape = [batch, h, w].
    images: numpy array, shape = [batch, h, w, 1 or 3].
    labels: numpy array, shape = [batch, h, w, 1].
    masks: numpy array, shape = [batch, h, w, 1].
    logdir: str, to stores the floder of visualization results.
    idx: int, use to name result image, images[i] -> str(idx+i).jpg.
  """
  visulize_dir = os.path.join(logdir, 'visulize')
  if not os.path.exists(visulize_dir): os.mkdir(visulize_dir)

  if images.shape[-1] == 1:
    images = np.tile(images, [1, 1, 1, 3])

  for i in range(predicts.shape[0]):
    right = predicts[i][:, :, np.newaxis] * labels[i] * masks[i]
    error = predicts[i][:, :, np.newaxis] - right
    miss = labels[i] - right

    vis = np.concatenate([error, right, miss], axis=-1)
    vis = np.concatenate([images[i], vis * 255], axis=0)

    vis_path = os.path.join(visulize_dir, str(idx+i)+'.png')
    io.imsave(vis_path, vis.astype(np.uint8))

def main(argv):
  # Load datasets.
  test_dataset = CreateDataset(
    file_path=FLAGS.label_file_path,
    image_size=FLAGS.image_size,
    batch_size=FLAGS.batch_size,
    preprocess=FLAGS.preprocess,
    augmentation=False,
    repeat=False)

  model = Unet(
      data_format='channels_last',
      classes=FLAGS.num_classes,
      transpose_conv=FLAGS.transpose_conv)
  weights_path = os.path.join(FLAGS.logdir, 'unet_weights')
  model.load_weights(weights_path)

  # Setup metric.
  mIoU = tf.keras.metrics.MeanIoU(num_classes=2)

  count = 0
  for idx, (images, labels, masks) in enumerate(test_dataset):
    logits = model.build_call(images, training=False)
    predicts = np.argmax(logits.numpy(), axis=-1)

    # optinal.
    if FLAGS.visulize:
      visulize_predicts(predicts, images, labels, masks, FLAGS.logdir, count)
      count += predicts.shape[0]

    mIoU.update_state(labels, predicts, masks)

  cm = mIoU.total_cm
  print('confuse mat:\n', cm.numpy().astype(np.int32))
  print('IoU: ', (cm[1, 1] / (cm[0, 1] + cm[1, 0] + cm[1, 1])).numpy())
  print('mIoU: ', mIoU.result().numpy())

if __name__ == '__main__':
    app.run(main)


import streamlit as st
import json
import requests
import pandas as pd
import numpy as np
import re
from datetime import datetime as dt
from datetime import time
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib

st.set_page_config(layout="centered",
                   page_title="DataCracy Dashboard",
                   page_icon = '💙')

#SLACK_BEARER_TOKEN = os.environ.get('SLACK_BEARER_TOKEN') ## Get in setting of Streamlit Share
SLACK_BEARER_TOKEN = st.secrets["TOKEN"]
DTC_GROUPS_URL = ('https://raw.githubusercontent.com/anhdanggit/atom-assignments/main/data/datacracy_groups.csv')
#st.write(json_data['SLACK_BEARER_TOKEN'])

@st.cache
def load_users_df():
    # Slack API User Data
    endpoint = "https://slack.com/api/users.list"
    headers = {"Authorization": "Bearer {}".format(SLACK_BEARER_TOKEN)}
    response_json = requests.post(endpoint, headers=headers).json()
    user_dat = response_json['members']

    # Convert to CSV
    user_dict = {'user_id':[],'name':[],'display_name':[],'real_name':[],'title':[],'is_bot':[]}
    for i in range(len(user_dat)):
      user_dict['user_id'].append(user_dat[i]['id'])
      user_dict['name'].append(user_dat[i]['name'])
      user_dict['display_name'].append(user_dat[i]['profile']['display_name'])
      user_dict['real_name'].append(user_dat[i]['profile']['real_name_normalized'])
      user_dict['title'].append(user_dat[i]['profile']['title'])
      user_dict['is_bot'].append(int(user_dat[i]['is_bot']))
    user_df = pd.DataFrame(user_dict)
    # Read dtc_group hosted in github
    dtc_groups = pd.read_csv(DTC_GROUPS_URL)
    user_df = user_df.merge(dtc_groups, how='left', on='name')
    return user_df

@st.cache
def load_channel_df():
    endpoint2 = "https://slack.com/api/conversations.list"
    data = {'types': 'public_channel,private_channel'} # -> CHECK: API Docs https://api.slack.com/methods/conversations.list/test
    headers = {"Authorization": "Bearer {}".format(SLACK_BEARER_TOKEN)}
    response_json = requests.post(endpoint2, headers=headers, data=data).json()
    channel_dat = response_json['channels']
    channel_dict = {'channel_id':[], 'channel_name':[], 'is_channel':[],'creator':[],'created_at':[],'topics':[],'purpose':[],'num_members':[]}
    for i in range(len(channel_dat)):
        channel_dict['channel_id'].append(channel_dat[i]['id'])
        channel_dict['channel_name'].append(channel_dat[i]['name'])
        channel_dict['is_channel'].append(channel_dat[i]['is_channel'])
        channel_dict['creator'].append(channel_dat[i]['creator'])
        channel_dict['created_at'].append(dt.fromtimestamp(float(channel_dat[i]['created'])))
        channel_dict['topics'].append(channel_dat[i]['topic']['value'])
        channel_dict['purpose'].append(channel_dat[i]['purpose']['value'])
        channel_dict['num_members'].append(channel_dat[i]['num_members'])
    channel_df = pd.DataFrame(channel_dict)
    return channel_df

@st.cache(allow_output_mutation=True)
def load_msg_dict():
    endpoint3 = "https://slack.com/api/conversations.history"
    headers = {"Authorization": "Bearer {}".format(SLACK_BEARER_TOKEN)}
    msg_dict = {'channel_id':[],'msg_id':[], 'msg_ts':[], 'user_id':[], 'latest_reply':[],'reply_user_count':[],'reply_users':[],'github_link':[],'text':[]}
    for channel_id, channel_name in zip(channel_df['channel_id'], channel_df['channel_name']):
        print('Channel ID: {} - Channel Name: {}'.format(channel_id, channel_name))
        try:
            data = {"channel": channel_id}
            response_json = requests.post(endpoint3, data=data, headers=headers).json()
            msg_ls = response_json['messages']
            for i in range(len(msg_ls)):
                if 'client_msg_id' in msg_ls[i].keys():
                    msg_dict['channel_id'].append(channel_id)
                    msg_dict['msg_id'].append(msg_ls[i]['client_msg_id'])
                    msg_dict['msg_ts'].append(dt.fromtimestamp(float(msg_ls[i]['ts'])))
                    msg_dict['latest_reply'].append(dt.fromtimestamp(float(msg_ls[i]['latest_reply'] if 'latest_reply' in msg_ls[i].keys() else 0))) ## -> No reply: 1970-01-01
                    msg_dict['user_id'].append(msg_ls[i]['user'])
                    msg_dict['reply_user_count'].append(msg_ls[i]['reply_users_count'] if 'reply_users_count' in msg_ls[i].keys() else 0)
                    msg_dict['reply_users'].append(msg_ls[i]['reply_users'] if 'reply_users' in msg_ls[i].keys() else 0)
                    msg_dict['text'].append(msg_ls[i]['text'] if 'text' in msg_ls[i].keys() else 0)
                    ## -> Censor message contains tokens
                    text = msg_ls[i]['text']
                    github_link = re.findall('(?:https?://)?(?:www[.])?github[.]com/[\w-]+/?', text)
                    msg_dict['github_link'].append(github_link[0] if len(github_link) > 0 else None)
        except:
            print('====> '+ str(response_json))
    msg_df = pd.DataFrame(msg_dict)
    return msg_df

def process_msg_data(msg_df, user_df, channel_df):
    ## Extract 2 reply_users
    msg_df['reply_user1'] = msg_df['reply_users'].apply(lambda x: x[0] if x != 0 else '')
    msg_df['reply_user2'] = msg_df['reply_users'].apply(lambda x: x[1] if x != 0 and len(x) > 1 else '')
    ## Merge to have a nice name displayed
    msg_df = msg_df.merge(user_df[['user_id','name','DataCracy_role']].rename(columns={'name':'submit_name'}), \
        how='left',on='user_id')
    msg_df = msg_df.merge(user_df[['user_id','name']].rename(columns={'name':'reply1_name','user_id':'reply1_id'}), \
        how='left', left_on='reply_user1', right_on='reply1_id')
    msg_df = msg_df.merge(user_df[['user_id','name']].rename(columns={'name':'reply2_name','user_id':'reply2_id'}), \
        how='left', left_on='reply_user2', right_on='reply2_id')
    ## Merge for nice channel name
    msg_df = msg_df.merge(channel_df[['channel_id','channel_name','created_at']], how='left',on='channel_id')
    ## Format datetime cols
    msg_df['created_at'] = msg_df['created_at'].dt.strftime('%Y-%m-%d')
    msg_df['msg_date'] = msg_df['msg_ts'].dt.strftime('%Y-%m-%d')
    msg_df['msg_time'] = msg_df['msg_ts'].dt.strftime('%H:%M')
    msg_df['wordcount'] = msg_df.text.apply(lambda s: len(s.split()))
    return msg_df

def convert_date(s):
  if s == 0:
    d = 'Mon'
  elif s == 1:
    d = 'Tue'
  elif s == 2:
    d = 'Wed'
  elif s ==3:
    d = 'Thu'
  elif s == 4:
    d = 'Fri'
  elif s == 5:
    d = 'Sat'
  elif s == 6:
    d = 'Sun'
  return d
user_df = load_users_df()
channel_df = load_channel_df()
msg_df = load_msg_dict()
msg_df = process_msg_data(msg_df, user_df, channel_df)

submit_df = msg_df[msg_df.channel_name.str.contains('assignment')]
submit_df = submit_df[submit_df.DataCracy_role.str.contains('Learner')]
latest_ts = submit_df.groupby(['channel_name', 'user_id']).msg_ts.idxmax() ## -> Latest ts
submit_df = submit_df.loc[latest_ts]

len_submit = len(submit_df)
len_review = len(submit_df[submit_df['reply_users']!= 0])

submit_gr1_df = submit_df.groupby('DataCracy_role').get_group('Learner_Gr1')

len_submit_1 = len(submit_gr1_df)
len_review_1 = len(submit_gr1_df[submit_gr1_df['reply_users']!= 0])

submit_gr2_df = submit_df.groupby('DataCracy_role').get_group('Learner_Gr2')

len_submit_2 = len(submit_gr2_df)
len_review_2 = len(submit_gr2_df[submit_gr2_df['reply_users']!= 0])

submit_gr3_df = submit_df.groupby('DataCracy_role').get_group('Learner_Gr3')

len_submit_3 = len(submit_gr3_df)
len_review_3 = len(submit_gr3_df[submit_gr3_df['reply_users']!= 0])

submit_gr4_df = submit_df.groupby('DataCracy_role').get_group('Learner_Gr4')

len_submit_4 = len(submit_gr4_df)
len_review_4 = len(submit_gr4_df[submit_gr4_df['reply_users']!= 0])

submit_df['msg_date'] = pd.to_datetime(submit_df['msg_date'])
submit_date = submit_df['msg_date'].dt.weekday
submit_date = submit_date.apply(convert_date)
submit_date = submit_date.value_counts().reindex(['Mon','Tue','Wed','Thu','Fri','Sat','Sun'])

channel_submit = submit_df.groupby('channel_name').size()
channel_submit = channel_submit.reset_index().rename(columns={0:'count'})
st.title("DataCracy Submit Dashboard")

matplotlib.rcParams['font.family'] = "sans-serif"
matplotlib.rcParams['font.sans-serif'] = "Open Sans"
plt.rcParams['patch.edgecolor'] = 'black'

select = list(pd.unique(submit_df['DataCracy_role']))
select.append('All Group')
option = st.selectbox(
    'Choose submit by group',
    sorted(select))

if option == 'Learner_Gr1':
    st.write(f'Group 1 submited {len_submit_1} assignments')
    st.write(f'Group 1 have {len_review_1} submit be reviewed')
    sizes_all =np.array([len_review, len_submit-len_review])
    sizes_gr1 =np.array([len_review_1, len_submit_1-len_review_1])
    def func(pct, allvals):
        absolute = int(round(pct/100.*np.sum(allvals)))
        return "{:.1f}%\n({:d})".format(pct, absolute)
    # sizes =[len_submit, len_submit-len_review]

    pie, (ax1,ax2) = plt.subplots(1,2)
    ax1.pie(sizes_all,autopct=lambda pct: func(pct, sizes_all), colors=['#ffffff','#e6e6e6'],textprops={'fontsize': 7})
    ax1.set_title('All group submit', fontdict={'fontsize': 10})

    ax2.pie(sizes_gr1,autopct=lambda pct: func(pct, sizes_gr1), colors=['#ffffff','#e6e6e6'],textprops={'fontsize': 7})
    ax2.set_title('Group 1 submit',fontdict={'fontsize': 10})

    pie.legend(['reviewed', 'non reviewed'])

    st.pyplot(pie)
elif option == 'Learner_Gr2':
    st.write(f'Group 2 submited {len_submit_2} assignments')
    st.write(f'Group 2 have {len_review_2} submit be reviewed')
    sizes_all =np.array([len_review, len_submit-len_review])
    sizes_gr2 =np.array([len_review_2, len_submit_2-len_review_2])
    def func(pct, allvals):
        absolute = int(round(pct/100.*np.sum(allvals)))
        return "{:.1f}%\n({:d})".format(pct, absolute)
    # sizes =[len_submit, len_submit-len_review]

    pie, (ax1,ax2) = plt.subplots(1,2)
    ax1.pie(sizes_all,autopct=lambda pct: func(pct, sizes_all), colors=['#ffffff','#e6e6e6'],textprops={'fontsize': 7})
    ax1.set_title('All group submit', fontdict={'fontsize': 10})

    ax2.pie(sizes_gr2,autopct=lambda pct: func(pct, sizes_gr2), colors=['#ffffff','#e6e6e6'],textprops={'fontsize': 7})
    ax2.set_title('Group 2 submit',fontdict={'fontsize': 10})

    pie.legend(['reviewed', 'non reviewed'])

    st.pyplot(pie)
elif option == 'Learner_Gr3':
    st.write(f'Group 3 submited {len_submit_3} assignments')
    st.write(f'Group 3 have {len_review_3} submit be reviewed')
    sizes_all =np.array([len_review, len_submit-len_review])
    sizes_gr3 =np.array([len_review_3, len_submit_3-len_review_3])
    def func(pct, allvals):
        absolute = int(round(pct/100.*np.sum(allvals)))
        return "{:.1f}%\n({:d})".format(pct, absolute)
    # sizes =[len_submit, len_submit-len_review]

    pie, (ax1,ax2) = plt.subplots(1,2)
    ax1.pie(sizes_all,autopct=lambda pct: func(pct, sizes_all), colors=['#ffffff','#e6e6e6'],textprops={'fontsize': 7})
    ax1.set_title('All group submit', fontdict={'fontsize': 10})

    ax2.pie(sizes_gr3,autopct=lambda pct: func(pct, sizes_gr3), colors=['#ffffff','#e6e6e6'],textprops={'fontsize': 7})
    ax2.set_title('Group 2 submit',fontdict={'fontsize': 10})

    pie.legend(['reviewed', 'non reviewed'])

    st.pyplot(pie)
elif option == 'Learner_Gr4':
    st.write(f'Group 4 submit {len_submit_4} assignments')
    st.write(f'Group 4 have {len_review_4} submit be reviewed')
    sizes_all =np.array([len_review, len_submit-len_review])
    sizes_gr4 =np.array([len_review_4, len_submit_4-len_review_4])
    def func(pct, allvals):
        absolute = int(round(pct/100.*np.sum(allvals)))
        return "{:.1f}%\n({:d})".format(pct, absolute)
    # sizes =[len_submit, len_submit-len_review]

    pie, (ax1,ax2) = plt.subplots(1,2)
    ax1.pie(sizes_all,autopct=lambda pct: func(pct, sizes_all), colors=['#ffffff','#e6e6e6'],textprops={'fontsize': 7})
    ax1.set_title('All group submit', fontdict={'fontsize': 10})

    ax2.pie(sizes_gr4,autopct=lambda pct: func(pct, sizes_gr4), colors=['#ffffff','#e6e6e6'],textprops={'fontsize': 7})
    ax2.set_title('Group 2 submit',fontdict={'fontsize': 10})

    pie.legend(['reviewed', 'non reviewed'])

    st.pyplot(pie)
elif option == 'All Group':
    st.write(f'All learners submited {len_submit} assignments')
    st.write(f'Have {len_review} submit be reviewed ')
    sizes_all =np.array([len_review, len_submit-len_review])
    def func(pct, allvals):
        absolute = int(round(pct/100.*np.sum(allvals)))
        return "{:.1f}%\n({:d} )".format(pct, absolute)
    # sizes =[len_submit, len_submit-len_review]

    pie, ax1 = plt.subplots()
    ax1.pie(sizes_all,autopct=lambda pct: func(pct, sizes_all), colors=['#ffffff','#e6e6e6'],textprops={'fontsize': 7})
    ax1.set_title('All group submit', fontdict={'fontsize': 10})

    pie.legend(['reviewed', 'non reviewed'])

    st.pyplot(pie)

period = st.slider(
    'Select a range of hours submit',
    0,23,(0,23))
period = list(period)
time_submit = submit_df['msg_time'].apply(lambda x: x.split(':')[0]).value_counts().sort_index()[period[0]:period[1]+1]

sns.set_style("ticks")
bar = plt.figure()
sns.barplot(time_submit.index,time_submit.values,facecolor=(1, 1, 1, 0),edgecolor=".2")
plt.title('Time submit distribution',fontsize=15)
plt.xlabel('Hour')
plt.ylabel('Submit')
st.pyplot(bar)

option1 = st.selectbox(
    'Submit by',
    ['Week','Weekday'])
if option1 == 'Week':
    submit_channel = plt.figure()
    ax1 = sns.barplot(channel_submit.index+1,channel_submit['count'],facecolor=(1, 1, 1, 0),edgecolor=".2")
    for p in ax1.patches:
        ax1.annotate(format(p.get_height(), '.1f'),
                   (p.get_x() + p.get_width() / 2., p.get_height()),
                   ha = 'center', va = 'center',
                   size=10,
                   xytext = (0, -12),
                   textcoords = 'offset points')
    plt.title('Submit distribution by week',fontsize=15)
    plt.xlabel('Assignment week')
    plt.ylabel('Submit')
    st.pyplot(submit_channel)
elif option1 == 'Weekday':
    bar_date = plt.figure()

    ax = sns.barplot(submit_date.index,submit_date.values,facecolor=(1, 1, 1, 0),edgecolor=".2", order= submit_date.index)
    for p in ax.patches:
        ax.annotate(format(p.get_height(), '.1f'),
                    (p.get_x() + p.get_width() / 2., p.get_height()),
                    ha = 'center', va = 'center',
                    size=10,
                    xytext = (0, -12),
                    textcoords = 'offset points')
    plt.title('Submit distribution by weekday',fontsize=15)
    plt.xlabel('Days of week')
    plt.ylabel('Submit')
    st.pyplot(bar_date)


#class Solution:
#    def beautySum(self, s: str) -> int:

def beautySum(s):
    if len(s) <= 2:
        return 0
    # Now assured that len(s) >= 3.
    somme = 0
    for head in range(len(s) - 2):
        statistics = {s[head]: 1}
        if s[head+1] in statistics:
            statistics[s[head+1]] += 1
        else:
            statistics[s[head+1]] = 1
        for tail in range(head+2, len(s)):
            if s[tail] in statistics:
                statistics[s[tail]] += 1
            else:
                statistics[s[tail]] = 1
            somme += max(statistics.values()) - min(statistics.values())
    return somme

def test_case(k, s, expected):
    print(f"Test case {k:02d}")
    ans = beautySum(s)
    if ans == expected:
        print("Correct.")
    else:
        print(f"Incorrect. ans = {ans}, expected = {expected}")

if __name__ == "__main__":
    test_case(1, "aabcb", 5)
    test_case(2, "aabcbaa", 17)


from sklearn import svm
import numpy as np
from sklearn.neighbors import KNeighborsClassifier

###########################################
#Choose the K value in the KNN
###########################################

k = 7

###########################################
#Training the clf (classifier)
###########################################

def create_labeled_data():
    minlist = list()
    xlist = list()
    ylist = list()
    with open(("Data/train-io.txt"), "r", encoding="utf-8") as f:
        for i, line in enumerate(f):
            if i <= 300000:
                for word in line.split():
                    minlist.append(word)
                for x, word in enumerate(minlist):
                    if x == 12:
                        ylist.append(word)
                        minlist.remove(word)
                xlist.append(minlist)
                minlist = list()
    return xlist, ylist

x, Y = create_labeled_data()
# convert x into a numpy array
X = np.array(x)
# create instance of svm classifier
clf = KNeighborsClassifier(n_neighbors = k)
# fit the 12-D input to the correct output of 1 or 0
clf.fit(X, Y)

##############################################################
#Read in test-in and predict labels
##############################################################

def test_data():
    minlist = list()
    queries = list()
    with open(("Data/test-in.txt"), "r", encoding="utf-8") as f:
         for i, line in enumerate(f):
            if i <= 10000:
                for word in line.split():
                    minlist.append(word)
                queries.append(minlist)
                minlist = list()
    return queries

qs = test_data()
# predict either 1 or 0 for each input
predicted_ans = clf.predict(qs)
print(predicted_ans)

##############################################################
#Output result to test-out.txt file
##############################################################

def output_file (predicted_ans):
    with open(("Data/test-out.txt"), "w", encoding="utf-8") as f:
        for label in predicted_ans:
            f.write(label)
            f.write("\n")

output_file(predicted_ans)


from flask import Flask, request, abort, render_template, flash, redirect, url_for
from linebot import (LineBotApi, WebhookHandler)
from linebot.exceptions import (InvalidSignatureError)
from linebot.models import *
from datetime import datetime
import configparser

# LINE 聊天機器人的基本資料
config = configparser.ConfigParser()
config.read('config.ini')
#YOUR_CHANNEL_ACCESS_TOKEN #需改成自己的 #TOP=>股市小子=>圖片辨識機械人=> Messaging API
line_bot_api = LineBotApi(config.get('line-bot', 'channel_access_token'))
#YOUR_CHANNEL_SECRET #需改成自己的
handler = WebhookHandler(config.get('line-bot', 'channel_secret'))

app = Flask(__name__)

#直接回應圖片訊息   #需在imgur上傳圖片
@app.route("/sendImage/")
def sendImage(event):
    try:
        message = ImageSendMessage(
            original_content_url = "https://i.imgur.com/5PNBSaO.png",
            preview_image_url = "https://i.imgur.com/5PNBSaO.png"
        )
        line_bot_api.reply_message(event.reply_token, message)
    except:
        line_bot_api.reply_message(event.reply_token, TextSendMessage(text='系統還在準備中...'))


from django import template
from django.conf import settings
from django.core.urlresolvers import reverse

register = template.Library()

@register.inclusion_tag('multiuploader/multiuploader_main.html')
def multiuploader(create_view=None, list_view=None, create_url=None, list_url=None, include_container=True, **view_kwargs):
    context = {
        'static_url': settings.STATIC_URL,
        'include_container': include_container
    }

    if create_url is not None:
        context['create_url'] = create_url
    elif create_view is not None:
        context['create_url'] = reverse(create_view, kwargs=view_kwargs)

    if list_url is not None:
        context['list_url'] = list_url
    elif list_view is not None:
        context['list_url'] = reverse(list_view, kwargs=view_kwargs)

    return context


#
# @lc app=leetcode id=70 lang=python3
#
# [70] Climbing Stairs
#

# @lc code=start

class Solution:
    def climbStairs(self, n: int) -> int:
        far, close = 1, 1
        for i in range(1, n):
            far, close = close, far + close
        return close

# @lc code=end

# 45/45 cases passed (20 ms)
# Your runtime beats 98.3 % of python3 submissions
# Your memory usage beats 100 % of python3 submissions (12.7 MB)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 20 12:03:02 2018

Detecta y devuelve la region del LED del contador de UTE

@author: nicolas
"""
import cv2
from detect import detect
import numpy as np

def extraer_region(img_color):

    img_gray = cv2.cvtColor(img_color, cv2.COLOR_BGR2GRAY)
    img_blur = cv2.medianBlur(img_gray, 55)
    #Aplico umbral para tener imagen binaria
    ret, thresh = cv2.threshold(img_blur, 127, 255, 0)
    #Hallo contornos
    (_, contours, _) = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    #Hallo contornos que sean rectangulos y los ploteo
    rectangles = []
    for contour in contours:
        shape = detect(contour)
        if shape == "rectangle":
            rect = cv2.boundingRect(contour)
            rectangles.append(rect)

    total_area = thresh.size
    max_allowed = total_area*0.3
    rectangles = [rectangle for rectangle in rectangles if rectangle[2] * rectangle[3] < max_allowed]
    ganamos = np.argmax([rectangle[2] * rectangle[3] for rectangle in rectangles])

    rect = rectangles[ganamos]
    region = img_color[rect[1]:rect[1]+rect[3], rect[0]:rect[0]+rect[2], :]

    return region


#!/usr/bin/env python
# -*- coding:utf-8 -*-
#author: wu.zheng midday.me

import os
import tensorflow as tf
import numpy as np
from dataset import data_factory
import model
import argparse

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('--name',  help='an integer for the accumulator', )
parser.add_argument('--max_step', default=500000, help='max step for train')

def train():
  NUM_CLASS = data_factory.get_data_config(args.name)['num_class']
  CHECK_POINTS_PATH = data_factory.get_data_config(args.name)['check_points_path']
  train_model = model.UnetModel(NUM_CLASS, is_training=True )
  global_step = tf.train.get_or_create_global_step()
  update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
  saver = tf.train.Saver()
  with tf.control_dependencies(update_ops):
    loss_op = tf.train.AdamOptimizer(learning_rate=0.00001).minimize(train_model.loss, global_step=global_step)
    train_op = tf.group([loss_op, update_ops])
  tf.summary.scalar(name='seg_loss', tensor=train_model.seg_loss)
  tf.summary.scalar(name='aux_loss', tensor=train_model.aux_loss)
  tf.summary.scalar(name="loss", tensor=train_model.loss)
  tf.summary.scalar(name="acc", tensor=train_model.acc)
  merge_summary_op = tf.summary.merge_all()
  with tf.Session() as sess:
    ckpt = tf.train.latest_checkpoint(CHECK_POINTS_PATH)
    init = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
    sess.run(init)
    if ckpt:
      st = int(ckpt.split("-")[-1])
      saver.restore(sess, ckpt)
      sess.run(global_step.assign(st))
      print("restore from %s"%(ckpt))
    summary_writer = tf.summary.FileWriter(CHECK_POINTS_PATH)
    summary_writer.add_graph(sess.graph)
    for batch_data in data_factory.get_data_iterator(args.name):
      if batch_data is None:
        print("Warning: batch_data is None")
        continue
      labels = np.array(batch_data[1])
      labels = np.expand_dims(labels, -1)
      images = np.array(batch_data[0])
      _, loss, _, acc, s, summary = sess.run([train_op, train_model.loss, train_model.acc_op, train_model.acc, global_step, merge_summary_op],
          feed_dict={train_model.image: images, train_model.label: labels})
      print("step:%d loss : %f acc: %f"%(s, loss , acc ))
      if s % 100 == 0:
        summary_writer.add_summary(summary=summary, global_step=s)
        saver.save(sess=sess, save_path=os.path.join(CHECK_POINTS_PATH, 'model'), global_step=s)
      if s > args.max_step:
        print("train finish")
        break

if __name__ == '__main__':
  args = parser.parse_args()
  train()


import numpy as np
import pygame as pg
import pandas as pd
import pylab as plt
import itertools as it
import math
import os
import pathos.multiprocessing as mp

from anytree import AnyNode as Node
from anytree import RenderTree
from collections import OrderedDict

# Local import
from algorithms.stochasticPW import ScoreChild, SelectAction, SelectNextState, InitializeChildren, Expand, ExpandNextState, \
        PWidening, RollOut, backup, OutputAction, PWMultipleTrees

from simple1DEnv import TransitionFunction, RewardFunction, Terminal
from visualize import draw
import stochasticAgentsMotionSimulationByAccerelationActionBurnTimeWithProbabilityDistractor as ag
import Attention
import calPosterior
import stochasticBeliefAndAttentionSimulationBurnTimeUpdateIdentityMax as ba
import envWithProbabilityDistractor as env
import reward
import trajectoriesSaveLoad as tsl

class MakeDiffSimulationRoot():
    def __init__(self, isTerminal, updatePhysicalStateByBelief):
        self.isTerminal = isTerminal
        self.updatePhysicalStateByBelief = updatePhysicalStateByBelief
    def __call__(self, action, state):
        stateForSimulationRoot = self.updatePhysicalStateByBelief(state)
        while self.isTerminal(stateForSimulationRoot):
            stateForSimulationRoot = self.updatePhysicalStateByBelief(state)
        rootNode = Node(id={action: stateForSimulationRoot}, numVisited=0, sumValue=0, is_expanded = False)
        return rootNode

class RunMCTSTrjactory:
    def __init__(self, maxRunningSteps, numTree, planFrequency, actionUpdateFrequecy, transitionFunctionInPlay, isTerminal, makeDiffSimulationRoot, render):
        self.maxRunningSteps = maxRunningSteps
        self.numTree = numTree
        self.planFrequency = planFrequency
        self.actionUpdateFrequecy = actionUpdateFrequecy
        self.transitionFunctionInPlay = transitionFunctionInPlay
        self.isTerminal = isTerminal
        self.makeDiffSimulationRoot = makeDiffSimulationRoot
        self.render = render

    def __call__(self, mcts):
        state, action = None, None
        currState = self.transitionFunctionInPlay(state, action)
        trajectory = []
        for runningStep in range(self.maxRunningSteps):

            if self.isTerminal(currState):
                trajectory.append([currState, None])
                break
            if runningStep % (self.planFrequency * self.actionUpdateFrequecy) == 0:
                rootNodes = [self.makeDiffSimulationRoot(action, currState) for treeIndex in range(self.numTree)]
                actions = mcts(rootNodes)
                #actionSpace = [(np.cos(degreeInPolar), np.sin(degreeInPolar)) for degreeInPolar in np.arange(0, 360, 8)/180 * math.pi]
                #actions = [actionSpace[np.random.choice(range(len(actionSpace)))]]
            action = actions[int(runningStep/self.actionUpdateFrequecy) % self.planFrequency]
            physicalState, beliefAndAttention = currState
            hypothesisInformation, positionOldTimeDF = beliefAndAttention
            probabilityOnHypothesisAttention = np.exp(hypothesisInformation['logP'])
            posteriorOnHypothesisAttention = probabilityOnHypothesisAttention/probabilityOnHypothesisAttention.sum()
            probabilityOnAttentionSlotByGroupbySum = posteriorOnHypothesisAttention.groupby(['wolfIdentity','sheepIdentity']).sum().values
            posterior = probabilityOnAttentionSlotByGroupbySum/np.sum(probabilityOnAttentionSlotByGroupbySum)
            attentionStatusOnJointSpace = hypothesisInformation['attentionStatus']
            attentionStatus = attentionStatusOnJointSpace.groupby(['wolfIdentity', 'sheepIdentity']).mean().values

            stateToRecord = [physicalState, posterior, attentionStatus, [np.array(list(rootNode.id.values())[0])[0][3] for rootNode in rootNodes]]
            trajectory.append([stateToRecord, action])
            nextState = self.transitionFunctionInPlay(currState, action)
            #print('***', physicalState[0][0], action)
            #print('***', np.linalg.norm(physicalState[1][0]), np.linalg.norm(physicalState[1][10]))
            currState = nextState
        return trajectory

class RunOneCondition:
    def __init__(self, getTrajectorySavePathByCondition, getCSVSavePathByCondition):
        self.getTrajectorySavePathByCondition = getTrajectorySavePathByCondition
        self.getCSVSavePathByCondition = getCSVSavePathByCondition

    def __call__(self, condition):

        getSavePath = self.getTrajectorySavePathByCondition(condition)
        getCSVSavePath = self.getCSVSavePathByCondition(condition)
        attentionType = condition['attentionType']
        alpha = condition['alphaForStateWidening']
        C = condition['CForStateWidening']
        minAttentionDistance = condition['minAttentionDistance']
        rangeAttention = condition['rangeAttention']
        numTree = condition['numTrees']
        numSimulations = condition['numSimulationTimes']
        actionRatio = condition['actionRatio']
        cBase = condition['cBase']
        burnTime = condition['burnTime']

        numSub = 2
        allResultsIdentity = []
        allResultsAttention = []
        allResultsIdentitySampled = []
        possibleTrialSubtleties = [11.0, 3.3]#[500.0, 11.0, 3.3, 1.83, 0.92, 0.31, 0.001]
        for subIndex in range(numSub):
            meanIdentityPerceptionOnConditions = {}
            meanAttentionPerceptionOnConditions = {}
            meanIdentitySampledOnConditions = {}
            for chasingSubtlety in possibleTrialSubtleties:

                print(numTree, chasingSubtlety, numSimulations, attentionType)
                numAgent = 25
                sheepId = 0
                suspectorIds = list(range(1, numAgent))

                resetWolfIdAndSubtlety = ag.ResetWolfIdAndSubtlety(suspectorIds, [chasingSubtlety])
                distanceToVisualDegreeRatio = 20
                minInitSheepWolfDistance = 9 * distanceToVisualDegreeRatio
                minInitSheepDistractorDistance = 2.5 * distanceToVisualDegreeRatio  # no distractor in killzone when init
                isLegalInitPositions = ag.IsLegalInitPositions(sheepId, minInitSheepWolfDistance, minInitSheepDistractorDistance)
                xBoundary = [0, 640]
                yBoundary = [0, 480]
                resetAgentPositions = ag.ResetAgentPositions(xBoundary, yBoundary, numAgent, isLegalInitPositions)
                resetPhysicalState = ag.ResetPhysicalState(sheepId, numAgent, resetAgentPositions, resetWolfIdAndSubtlety)

                numFramePerSecond = 40
                numMDPTimeStepPerSecond = 5
                numFrameWithoutActionChange = int(numFramePerSecond/numMDPTimeStepPerSecond)

                sheepActionUpdateFrequency = 1
                minSheepSpeed = int(17.4 * distanceToVisualDegreeRatio/numFramePerSecond)
                maxSheepSpeed = int(23.2 * distanceToVisualDegreeRatio/numFramePerSecond)
                warmUpTimeSteps = int(10 * numMDPTimeStepPerSecond)
                sheepPolicy = ag.SheepPolicy(sheepActionUpdateFrequency, minSheepSpeed, maxSheepSpeed, warmUpTimeSteps, burnTime)

                wolfActionUpdateFrequency = int(0.2 * numMDPTimeStepPerSecond)
                minWolfSpeed = int(8.7 * distanceToVisualDegreeRatio/numFramePerSecond)
                maxWolfSpeed = int(14.5 * distanceToVisualDegreeRatio/numFramePerSecond)
                wolfPolicy = ag.WolfPolicy(wolfActionUpdateFrequency, minWolfSpeed, maxWolfSpeed, warmUpTimeSteps)
                distractorActionUpdateFrequency = int(0.2 * numMDPTimeStepPerSecond)
                minDistractorSpeed = int(8.7 * distanceToVisualDegreeRatio/numFramePerSecond)
                maxDistractorSpeed = int(14.5 * distanceToVisualDegreeRatio/numFramePerSecond)
                distractorPolicy = ag.DistractorPolicy(distractorActionUpdateFrequency, minDistractorSpeed, maxDistractorSpeed, warmUpTimeSteps)
                preparePolicy = ag.PreparePolicy(sheepId, numAgent, sheepPolicy, wolfPolicy, distractorPolicy)
                updatePhysicalState = ag.UpdatePhysicalState(sheepId, numAgent, preparePolicy)

                xBoundary = [0, 640]
                yBoundary = [0, 480]
                checkBoundaryAndAdjust = ag.CheckBoundaryAndAdjust(xBoundary, yBoundary)
                transiteMultiAgentMotion = ag.TransiteMultiAgentMotion(checkBoundaryAndAdjust)

                minDistance = 0.0 * distanceToVisualDegreeRatio
                isTerminal = env.IsTerminal(sheepId, minDistance)
               # screen = pg.display.set_mode([xBoundary[1], yBoundary[1]])
               # screenColor = np.array([0, 0, 0])
               # sheepColor = np.array([0, 255, 0])
               # wolfColor = np.array([255, 0, 0])
               # circleSize = 10
               # saveImage = True
               # saveImageFile = 'image3'
               # render = env.Render(numAgent, screen, xBoundary[1], yBoundary[1], screenColor, sheepColor, wolfColor, circleSize, saveImage, saveImageFile, isTerminal)
                render = None
                renderOnInSimulation = False
                transiteStateWithoutActionChangeInSimulation = env.TransiteStateWithoutActionChange(numFrameWithoutActionChange, isTerminal, transiteMultiAgentMotion, render, renderOnInSimulation)
                renderOnInPlay = False
                transiteStateWithoutActionChangeInPlay = env.TransiteStateWithoutActionChange(numFrameWithoutActionChange, isTerminal, transiteMultiAgentMotion, render, renderOnInPlay)

                if attentionType == 'idealObserver':
                    attentionLimitation= 4
                    precisionPerSlot=500.0
                    precisionForUntracked=500.0
                    memoryratePerSlot=1.0
                    memoryrateForUntracked=1.0
                if attentionType == 'preAttention':
                    attentionLimitation= 4
                    precisionPerSlot=2.5
                    precisionForUntracked=2.5
                    memoryratePerSlot=0.45
                    memoryrateForUntracked=0.45
                if attentionType == 'attention3':
                    attentionLimitation= 3
                    precisionPerSlot=8.0
                    precisionForUntracked=0.01
                    memoryratePerSlot=0.7
                    memoryrateForUntracked=0.01
                if attentionType == 'hybrid3':
                    attentionLimitation= 3
                    precisionPerSlot=8.0
                    precisionForUntracked=2.5
                    memoryratePerSlot=0.7
                    memoryrateForUntracked=0.45
                if attentionType == 'attention4':
                    attentionLimitation= 4
                    precisionPerSlot=8.0
                    precisionForUntracked=0.01
                    memoryratePerSlot=0.7
                    memoryrateForUntracked=0.01
                if attentionType == 'hybrid4':
                    attentionLimitation= 4
                    precisionPerSlot=8.0
                    precisionForUntracked=2.5
                    memoryratePerSlot=0.7
                    memoryrateForUntracked=0.45

                if attentionType == 'preAttentionMem0.25':
                    attentionLimitation= 4
                    precisionPerSlot=2.5
                    precisionForUntracked=2.5
                    memoryratePerSlot=0.25
                    memoryrateForUntracked=0.25
                if attentionType == 'preAttentionMem0.65':
                    attentionLimitation= 4
                    precisionPerSlot=2.5
                    precisionForUntracked=2.5
                    memoryratePerSlot=0.65
                    memoryrateForUntracked=0.65
                if attentionType == 'preAttentionPre0.5':
                    attentionLimitation= 4
                    precisionPerSlot=0.5
                    precisionForUntracked=0.5
                    memoryratePerSlot=0.45
                    memoryrateForUntracked=0.45
                if attentionType == 'preAttentionPre4.5':
                    attentionLimitation= 4
                    precisionPerSlot=4.5
                    precisionForUntracked=4.5
                    memoryratePerSlot=0.45
                    memoryrateForUntracked=0.45

                attention = Attention.AttentionToPrecisionAndDecay(precisionPerSlot, precisionForUntracked, memoryratePerSlot, memoryrateForUntracked)
                transferMultiAgentStatesToPositionDF = ba.TransferMultiAgentStatesToPositionDF(numAgent)
                possibleSubtleties = [500.0, 11.0, 3.3, 1.83, 0.92, 0.31, 0.001]
                resetBeliefAndAttention = ba.ResetBeliefAndAttention(sheepId, suspectorIds, possibleSubtleties, attentionLimitation, transferMultiAgentStatesToPositionDF, attention)

                maxAttentionDistance = minAttentionDistance + rangeAttention
                attentionMinDistance = minAttentionDistance * distanceToVisualDegreeRatio
                attentionMaxDistance = maxAttentionDistance * distanceToVisualDegreeRatio
                numStandardErrorInDistanceRange = 4
                calDistancePriorOnAttentionSlot = Attention.CalDistancePriorOnAttentionSlot(attentionMinDistance, attentionMaxDistance, numStandardErrorInDistanceRange)
                attentionSwitch = Attention.AttentionSwitch(attentionLimitation, calDistancePriorOnAttentionSlot)
                computePosterior = calPosterior.CalPosteriorLog(minDistance)

                print(attentionLimitation, attentionMinDistance/distanceToVisualDegreeRatio, attentionMaxDistance/distanceToVisualDegreeRatio)

                attentionSwitchFrequencyInSimulation = np.inf
                beliefUpdateFrequencyInSimulation = np.inf
                updateBeliefAndAttentionInSimulation = ba.UpdateBeliefAndAttentionState(attention, computePosterior, attentionSwitch, transferMultiAgentStatesToPositionDF,
                        attentionSwitchFrequencyInSimulation, beliefUpdateFrequencyInSimulation, burnTime)

                attentionSwitchFrequencyInPlay = int(0.6 * numMDPTimeStepPerSecond)
                beliefUpdateFrequencyInPlay = int(0.2 * numMDPTimeStepPerSecond)
                updateBeliefAndAttentionInPlay = ba.UpdateBeliefAndAttentionState(attention, computePosterior, attentionSwitch, transferMultiAgentStatesToPositionDF,
                        attentionSwitchFrequencyInPlay, beliefUpdateFrequencyInPlay, burnTime)

                updatePhysicalStateByBeliefFrequencyInSimulationRoot = int(0.2 * numMDPTimeStepPerSecond)
                updatePhysicalStateByBeliefInSimulationRoot = ba.UpdatePhysicalStateImagedByBelief(updatePhysicalStateByBeliefFrequencyInSimulationRoot)
                updatePhysicalStateByBeliefFrequencyInSimulation = np.inf
                updatePhysicalStateByBeliefInSimulation = ba.UpdatePhysicalStateImagedByBelief(updatePhysicalStateByBeliefFrequencyInSimulation)

                updatePhysicalStateByBeliefFrequencyInPlay = np.inf
                updatePhysicalStateByBeliefInPlay = ba.UpdatePhysicalStateImagedByBelief(updatePhysicalStateByBeliefFrequencyInPlay)

                transitionFunctionInSimulation = env.TransitionFunction(resetPhysicalState, resetBeliefAndAttention, updatePhysicalState, transiteStateWithoutActionChangeInSimulation,
                        updateBeliefAndAttentionInSimulation, updatePhysicalStateByBeliefInSimulation)

                transitionFunctionInPlay = env.TransitionFunction(resetPhysicalState, resetBeliefAndAttention, updatePhysicalState, transiteStateWithoutActionChangeInPlay,
                        updateBeliefAndAttentionInPlay, updatePhysicalStateByBeliefInPlay)

                maxRollOutSteps = 5
                aliveBouns = 1/maxRollOutSteps
                deathPenalty = -1
                rewardFunction = reward.RewardFunctionTerminalPenalty(sheepId, aliveBouns, deathPenalty, isTerminal)
                rewardRollout = lambda state, action, nextState: rewardFunction(state, action)

                numActionSpace = 8
                actionInterval = int(360/(numActionSpace))
                actionMagnitude = actionRatio * minSheepSpeed
                actionSpace = [(np.cos(degreeInPolar) * actionMagnitude, np.sin(degreeInPolar) * actionMagnitude) for degreeInPolar in np.arange(0, 360, actionInterval)/180 * math.pi]
                getActionPrior = lambda state : {action: 1/len(actionSpace) for action in actionSpace}

                cInit = 1
                #cBase = 50
                scoreChild = ScoreChild(cInit, cBase)
                selectAction = SelectAction(scoreChild)
                selectNextState = SelectNextState(selectAction)

                initializeChildren = InitializeChildren(actionSpace, transitionFunctionInSimulation, getActionPrior)
                expand = Expand(isTerminal, initializeChildren)
                pWidening = PWidening(alpha, C)
                expandNewState = ExpandNextState(transitionFunctionInSimulation, pWidening)

                rolloutPolicy = lambda state: actionSpace[np.random.choice(range(numActionSpace))]
                rolloutHeuristic = lambda state: 0
                estimateValue = RollOut(rolloutPolicy, maxRollOutSteps, transitionFunctionInSimulation, rewardRollout, isTerminal, rolloutHeuristic)

                numActionPlaned = 1
                outputAction = OutputAction(numActionPlaned, actionSpace)
                #numSimulations = int(numTotalSimulationTimes/numTree)

                #sheepColorInMcts = np.array([0, 255, 0])
                #wolfColorInMcts = np.array([255, 0, 0])
                #distractorColorInMcts = np.array([255, 255, 255])
                #saveImageMCTS = True
                #mctsRender = env.MctsRender(numAgent, screen, xBoundary[1], yBoundary[1], screenColor, sheepColorInMcts, wolfColorInMcts, distractorColorInMcts, circleSize, saveImageMCTS, saveImageFile)
                #mctsRenderOn = False
                #mctsRender = None
                #pg.init()
                #mcts = MCTS(numSimulations, selectChild, expand, rollout, backup, selectAction, mctsRender, mctsRenderOn)
                pwMultipleTrees = PWMultipleTrees(numSimulations, selectAction, selectNextState, expand, expandNewState, estimateValue, backup, outputAction)

                maxRunningSteps = int(25 * numMDPTimeStepPerSecond)
                makeDiffSimulationRoot = MakeDiffSimulationRoot(isTerminal, updatePhysicalStateByBeliefInSimulationRoot)
                runMCTSTrjactory = RunMCTSTrjactory(maxRunningSteps, numTree, numActionPlaned, sheepActionUpdateFrequency, transitionFunctionInPlay, isTerminal, makeDiffSimulationRoot, render)

                rootAction = actionSpace[np.random.choice(range(numActionSpace))]
                numTrial = 10
                trajectories = [runMCTSTrjactory(pwMultipleTrees) for trial in range(numTrial)]

                savePath = getSavePath({'chasingSubtlety': chasingSubtlety, 'subIndex': subIndex})
                #tsl.saveToPickle(trajectories, savePath)
                #def getTrueWolfIndentityAcc(trajectory):
                #    AccTrial = []
                #    for timeStepIndex in range(len(trajectory)):
                #        timeStep = trajectory[timeStepIndex]
                #        wolfId = trajectory[0][0][0][3][0]
                #        wolfIdInEach = timeStep[0][0][3][0]
                #        #print(wolfId, '**', wolfIdInEach)
                #        if (timeStepIndex % 3 == 0) and timeStepIndex >= 11:
                #            AccTrial.append(timeStep[0][1][int(wolfIdInEach) - 1])
                #    meanIdentityAcc = np.mean(AccTrial)
                #    #meanIdentityAcc = np.mean(np.array([timeStep[0][1][int(timeStep[0][0][3][0] - 1)] for timeStep in trajectory])[11:])
                #    return meanIdentityAcc
                getTrueWolfIndentityAcc = lambda trajectory: np.array([timeStep[0][1][int(timeStep[0][0][3][0] - 1)] for timeStep in trajectory])[:]
                identityPerceptionTimeSeries = np.mean([getTrueWolfIndentityAcc(trajectory) for trajectory in trajectories], axis = 0)
                resultsTimeSeries = pd.DataFrame([identityPerceptionTimeSeries], columns = list(range(len(identityPerceptionTimeSeries))))
                savePathIdentitySeries = getCSVSavePath({'chasingSubtlety': chasingSubtlety, 'measure': 'identity'})
                if subIndex == 0:
                    resultsTimeSeries.to_csv(savePathIdentitySeries, mode='a')
                else:
                    resultsTimeSeries.to_csv(savePathIdentitySeries, mode='a', header=False)
                meanIdentityPerception = np.mean([getTrueWolfIndentityAcc(trajectory) for trajectory in trajectories])
                meanIdentityPerceptionOnConditions.update({chasingSubtlety: meanIdentityPerception})
                print(meanIdentityPerceptionOnConditions)

                getTrueWolfAttentionNumber = lambda trajectory: np.array([timeStep[0][2][int(timeStep[0][0][3][0] - 1)] for timeStep in trajectory])[:]
                attentionNumberTimeSeries = np.mean([getTrueWolfAttentionNumber(trajectory) for trajectory in trajectories], axis = 0)
                resultsAttentionTimeSeries = pd.DataFrame([attentionNumberTimeSeries], columns = list(range(len(attentionNumberTimeSeries))))
                savePathAttentionSeries = getCSVSavePath({'chasingSubtlety': chasingSubtlety, 'measure': 'attentionNumber'})
                if subIndex == 0:
                    resultsAttentionTimeSeries.to_csv(savePathAttentionSeries, mode='a')
                else:
                    resultsAttentionTimeSeries.to_csv(savePathAttentionSeries, mode='a', header=False)
                meanAttentionPerception = np.mean([getTrueWolfAttentionNumber(trajectory) for trajectory in trajectories])
                meanAttentionPerceptionOnConditions.update({chasingSubtlety: meanAttentionPerception})

                getSampledWolfIndentityAcc = lambda trajectory: np.array([int(int(timeStep[0][3][0][0]) == int(timeStep[0][0][3][0]))
                    for timeStep in trajectory])[:]
                identitySampledTimeSeries = np.mean([getSampledWolfIndentityAcc(trajectory) for trajectory in trajectories], axis = 0)
                resultsSampledTimeSeries = pd.DataFrame([identitySampledTimeSeries], columns = list(range(len(identitySampledTimeSeries))))
                savePathIdentitySampledSeries = getCSVSavePath({'chasingSubtlety': chasingSubtlety, 'measure': 'identitySampled'})
                if subIndex == 0:
                    resultsSampledTimeSeries.to_csv(savePathIdentitySampledSeries, mode='a')
                else:
                    resultsSampledTimeSeries.to_csv(savePathIdentitySampledSeries, mode='a', header=False)
                meanIdentitySampled = np.mean([getSampledWolfIndentityAcc(trajectory) for trajectory in trajectories])
                meanIdentitySampledOnConditions.update({chasingSubtlety: meanIdentitySampled})

            allResultsIdentity.append(meanIdentityPerceptionOnConditions)
            resultsIdentity = pd.DataFrame(allResultsIdentity)
            csvSavePathIdentity = getCSVSavePath({'measure': 'identity'})
            resultsIdentity.to_csv(csvSavePathIdentity)

            allResultsAttention.append(meanAttentionPerceptionOnConditions)
            resultsAttention = pd.DataFrame(allResultsAttention)
            csvSavePathAttention = getCSVSavePath({'measure': 'attentionNumber'})
            resultsAttention.to_csv(csvSavePathAttention)

            allResultsIdentitySampled.append(meanIdentitySampledOnConditions)
            resultsIdentitySampled = pd.DataFrame(allResultsIdentitySampled)
            csvSavePathIdentitySampled = getCSVSavePath({'measure': 'identitySampled'})
            resultsIdentitySampled.to_csv(csvSavePathIdentitySampled)

def drawPerformanceline(dataDf, axForDraw):
    plotDf = dataDf.reset_index()
    plotDf.plot(x = "subtleties", y = "escapeRate", ax = axForDraw)

def main():
    manipulatedVariables = OrderedDict()
    manipulatedVariables['alphaForStateWidening'] = [0.25]
    #manipulatedVariables['attentionType'] = ['idealObserver', 'hybrid4']
    manipulatedVariables['attentionType'] = ['hybrid4']
    #manipulatedVariables['attentionType'] = ['preAttention']
    #manipulatedVariables['attentionType'] = ['idealObserver', 'preAttention', 'attention4', 'hybrid4']
    #manipulatedVariables['attentionType'] = ['preAttentionMem0.65', 'preAttentionMem0.25', 'preAttentionPre0.5', 'preAttentionPre4.5']
    manipulatedVariables['CForStateWidening'] = [2]
    manipulatedVariables['minAttentionDistance'] = [10.0]#[5.0, 10.0, 20.0, 40.0]
    manipulatedVariables['rangeAttention'] = [10.0]#[5.0, 10.0, 20.0, 40.0]
    manipulatedVariables['cBase'] = [44]
    manipulatedVariables['numTrees'] = [1]
    manipulatedVariables['numSimulationTimes'] = [1]
    manipulatedVariables['actionRatio'] = [0.2]
    manipulatedVariables['burnTime'] = [0]

    productedValues = it.product(*[[(key, value) for value in values] for key, values in manipulatedVariables.items()])
    parametersAllCondtion = [dict(list(specificValueParameter)) for specificValueParameter in productedValues]

    DIRNAME = os.path.dirname(__file__)
    trajectoryDirectory = os.path.join(DIRNAME, '..', 'data', 'mcts',
                                'trajectories')
    if not os.path.exists(trajectoryDirectory):
        os.makedirs(trajectoryDirectory)

    trajectoryExtension = '.pickle'
    getTrajectorySavePathByCondition = lambda condition: tsl.GetSavePath(trajectoryDirectory, trajectoryExtension, condition)
    measurementEscapeExtension = '.csv'
    getCSVSavePathByCondition = lambda condition: tsl.GetSavePath(trajectoryDirectory, measurementEscapeExtension, condition)
    runOneCondition = RunOneCondition(getTrajectorySavePathByCondition, getCSVSavePathByCondition)

    #runOneCondition(parametersAllCondtion[0])
    numCpuCores = os.cpu_count()
    numCpuToUse = int(numCpuCores)
    runPool = mp.Pool(numCpuToUse)
    runPool.map(runOneCondition, parametersAllCondtion)

    precisionToSubtletyDict={500:0,50:5,11:30,3.3:60,1.83:90,0.92:120,0.31:150,0.001: 180}

    #levelValues = [numTotalSimulationTimes, numTrees, subtleties]
    #levelNames = ["numTotalSimulationTimes", "numTrees", "subtleties"]

    #modelIndex = pd.MultiIndex.from_product(levelValues, names=levelNames)

    #toSplitFrame = pd.DataFrame(index = modelIndex)

    #modelResultDf = toSplitFrame.groupby(levelNames).apply(evaluateMCTSInEscape)
    #fig = plt.figure()
    #plotLevels =levelNames [:1]
    #plotRowNum = len(levelValues[0])
    #plotColNum = len(levelValues[1])
    #plotCounter = 1

    #for (key, dataDf) in modelResultDf.groupby(["numTotalSimulationTimes", "numTrees"]):
    #    axForDraw = fig.add_subplot(plotRowNum, plotColNum, plotCounter)
    #    drawPerformanceline(dataDf, axForDraw)
    #    plotCounter+=1

    #plt.show()
    #fig.savefig('escapeRateActUpdateSheep2Wolf3Att95Tree3Sime36.png')
    #print("Finished evaluating")

if __name__ == "__main__":
    main()


#Libraries

from crypto_trading import *
from pandas import read_csv
import matplotlib.pyplot as plt
import sys
import multiprocessing as mp
import numpy as np
import itertools
import pickle
from classifier_all import get_data

#Genetic
#from GA_optimization import *

coin_name = "eth"
df_price = read_csv('./backtesting/%s_price.csv'%(coin_name), header=0, index_col=0)
df_output = read_csv('./backtesting/%s_output.csv'%(coin_name), header=0, index_col=0)
start_row = 4970
end_row = 5000

# Start
start_investment = 100000
port = launch_backtesting(df_price,df_output,Portfolio(start_investment),0.01,100)
port.historical_positions['netto_profit'].sum()

##### PLOT

portfolio1 = port
all_signal = True

df_price.index = pd.to_datetime(df_price.index,unit='s')
portfolio1.signals_all.index = pd.to_datetime(portfolio1.signals_all.index,unit='s')
portfolio1.signals_taken.index = pd.to_datetime(portfolio1.signals_taken.index,unit='s')

if all_signal:
  #All signals
  buy_signal = portfolio1.signals_all[portfolio1.signals_all['buy'] == 1]['buy']
  sell_signal = portfolio1.signals_all[portfolio1.signals_all['sell'] == -1]['sell']
else:
  #Taken signals
  buy_signal = portfolio1.signals_taken[portfolio1.signals_all['buy'] == 1]['buy']
  sell_signal = portfolio1.signals_taken[portfolio1.signals_all['sell'] == -1]['sell']

plt.plot(buy_signal.index, df_price.ix[buy_signal.index]['Close'], '^', markersize=10, color='g',label='Buy')
plt.plot(sell_signal.index, df_price.ix[sell_signal.index]['Close'], 'v', markersize=10, color='r',label='Sell')

  #Price
plt.plot(df_price[24:48].index, df_price[24:48]['Close'], label='Bitcoin')

plt.xlabel('Date')
plt.ylabel('Price (USD)')
plt.legend(loc=0)
plt.show()

##### END PLOT


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

import io
import mock
import os

import charms_openstack.test_utils as test_utils

import charms.ovn_charm as ovn_charm

class TestOVNConfigProperties(test_utils.PatchHelper):

    def test_ovn_key(self):
        self.assertEquals(ovn_charm.ovn_key(None),
                          os.path.join(ovn_charm.OVS_ETCDIR, 'key_host'))

    def test_ovn_cert(self):
        self.assertEquals(ovn_charm.ovn_cert(None),
                          os.path.join(ovn_charm.OVS_ETCDIR, 'cert_host'))

    def test_ovn_ca_cert(self):
        cls = mock.MagicMock()
        cls.charm_instance.name = mock.PropertyMock().return_value = 'name'
        self.assertEquals(ovn_charm.ovn_ca_cert(cls),
                          os.path.join(ovn_charm.OVS_ETCDIR, 'name.crt'))

class Helper(test_utils.PatchHelper):

    def setUp(self):
        super().setUp()
        self.patch_release(ovn_charm.BaseOVNChassisCharm.release)
        self.patch_object(ovn_charm.reactive, 'is_flag_set',
                          return_value=False)
        self.patch_object(
            ovn_charm.charms_openstack.adapters, '_custom_config_properties')
        self._custom_config_properties.side_effect = {}
        self.target = ovn_charm.BaseOVNChassisCharm()
        # remove the 'is_flag_set' patch so the tests can use it
        self._patches['is_flag_set'].stop()
        setattr(self, 'is_flag_set', None)
        del(self._patches['is_flag_set'])
        del(self._patches_start['is_flag_set'])

    def patch_target(self, attr, return_value=None):
        mocked = mock.patch.object(self.target, attr)
        self._patches[attr] = mocked
        started = mocked.start()
        started.return_value = return_value
        self._patches_start[attr] = started
        setattr(self, attr, started)

class TestOVNChassisCharm(Helper):

    def test_optional_openstack_metadata(self):
        self.assertEquals(self.target.packages, ['ovn-host'])
        self.assertEquals(self.target.services, ['ovn-host'])
        self.patch_object(
            ovn_charm.charms_openstack.adapters, '_custom_config_properties')
        self._custom_config_properties.side_effect = {}
        self.patch_object(ovn_charm.reactive, 'is_flag_set',
                          return_value=True)
        c = ovn_charm.BaseOVNChassisCharm()
        self.assertEquals(c.packages, [
            'ovn-host', 'networking-ovn-metadata-agent', 'haproxy'
        ])
        self.assertEquals(c.services, [
            'ovn-host', 'networking-ovn-metadata-agent'])

    def test_run(self):
        self.patch_object(ovn_charm.subprocess, 'run')
        self.patch_object(ovn_charm.ch_core.hookenv, 'log')
        self.target.run('some', 'args')
        self.run.assert_called_once_with(
            ('some', 'args'),
            stdout=ovn_charm.subprocess.PIPE,
            stderr=ovn_charm.subprocess.STDOUT,
            check=True,
            universal_newlines=True)

    def test_configure_tls(self):
        self.patch_target('get_certs_and_keys')
        self.get_certs_and_keys.return_value = [{
            'cert': 'fakecert',
            'key': 'fakekey',
            'cn': 'fakecn',
            'ca': 'fakeca',
            'chain': 'fakechain',
        }]
        with mock.patch('builtins.open', create=True) as mocked_open:
            mocked_file = mock.MagicMock(spec=io.FileIO)
            mocked_open.return_value = mocked_file
            self.target.configure_cert = mock.MagicMock()
            self.target.run = mock.MagicMock()
            self.target.configure_tls()
            mocked_open.assert_called_once_with(
                '/etc/openvswitch/charmname.crt', 'w')
            mocked_file.__enter__().write.assert_called_once_with(
                'fakeca\nfakechain')
            self.target.configure_cert.assert_called_once_with(
                ovn_charm.OVS_ETCDIR,
                'fakecert',
                'fakekey',
                cn='host')

    def test_configure_ovs(self):
        self.patch_target('run')
        self.patch_target('restart_all')
        self.patch_object(ovn_charm, 'ovn_key')
        self.patch_object(ovn_charm, 'ovn_cert')
        self.patch_object(ovn_charm, 'ovn_ca_cert')
        ovsdb_interface = mock.MagicMock()
        db_sb_connection_strs = mock.PropertyMock().return_value = ['dbsbconn']
        ovsdb_interface.db_sb_connection_strs = db_sb_connection_strs
        cluster_local_addr = mock.PropertyMock().return_value = (
            'cluster_local_addr')
        ovsdb_interface.cluster_local_addr = cluster_local_addr
        self.target.configure_ovs(ovsdb_interface)
        self.run.assert_has_calls([
            mock.call('ovs-vsctl', 'set-ssl', mock.ANY, mock.ANY, mock.ANY),
            mock.call('ovs-vsctl', 'set', 'open', '.',
                      'external-ids:ovn-encap-type=geneve', '--',
                      'set', 'open', '.',
                      'external-ids:ovn-encap-ip=cluster_local_addr', '--',
                      'set', 'open', '.',
                      'external-ids:system-id=cluster_local_addr'),
            mock.call('ovs-vsctl', 'set', 'open', '.',
                      'external-ids:ovn-remote=dbsbconn'),
        ])
        self.run.reset_mock()
        self.target.enable_openstack = True
        self.patch_object(ovn_charm.ovn, 'SimpleOVSDB')
        managers = mock.MagicMock()
        self.SimpleOVSDB.return_value = managers
        self.target.configure_ovs(ovsdb_interface)
        managers.find.assert_called_once_with(
            'target="ptcp:6640:127.0.0.1"')
        self.run.assert_has_calls([
            mock.call('ovs-vsctl', 'set-ssl', mock.ANY, mock.ANY, mock.ANY),
            mock.call('ovs-vsctl', 'set', 'open', '.',
                      'external-ids:ovn-encap-type=geneve', '--',
                      'set', 'open', '.',
                      'external-ids:ovn-encap-ip=cluster_local_addr', '--',
                      'set', 'open', '.',
                      'external-ids:system-id=cluster_local_addr'),
            mock.call('ovs-vsctl', 'set', 'open', '.',
                      'external-ids:ovn-remote=dbsbconn'),
            mock.call('ovs-vsctl', '--id', '@manager',
                      'create', 'Manager', 'target="ptcp:6640:127.0.0.1"',
                      '--', 'add', 'Open_vSwitch', '.', 'manager_options',
                      '@manager'),
        ])

    def test_configure_bridges(self):
        self.patch_object(ovn_charm.os_context, 'NeutronPortContext')
        npc = mock.MagicMock()

        def _fake_resolve_ports(mac_or_if):
            result = []
            for entry in mac_or_if:
                if ':' in entry:
                    result.append('eth0')
                    continue
                result.append(entry)
            return result

        npc.resolve_ports.side_effect = _fake_resolve_ports
        self.NeutronPortContext.return_value = npc
        self.patch_target('config')
        self.config.__getitem__.side_effect = [
            '00:01:02:03:04:05:br-provider eth5:br-other',
            'provider:br-provider other:br-other']
        self.patch_object(ovn_charm.ovn, 'SimpleOVSDB')
        bridges = mock.MagicMock()
        bridges.find.side_effect = [
            [
                {'name': 'delete-bridge'},
                {'name': 'br-other'}
            ],
            StopIteration,
        ]
        ports = mock.MagicMock()
        ports.find.side_effect = [[{'name': 'delete-port'}]]
        opvs = mock.MagicMock()
        self.SimpleOVSDB.side_effect = [bridges, ports, opvs]
        self.patch_object(ovn_charm.ovn, 'del_br')
        self.patch_object(ovn_charm.ovn, 'del_port')
        self.patch_object(ovn_charm.ovn, 'add_br')
        self.patch_object(ovn_charm.ovn, 'list_ports')
        self.list_ports().__iter__.return_value = []
        self.patch_object(ovn_charm.ovn, 'add_port')
        self.target.configure_bridges()
        npc.resolve_ports.assert_has_calls([
            mock.call(['00:01:02:03:04:05']),
            mock.call(['eth5']),
        ], any_order=True)
        bridges.find.assert_has_calls([
            mock.call('name=br-provider'),
            mock.call('name=br-other'),
        ], any_order=True)
        self.del_br.assert_called_once_with('delete-bridge')
        self.del_port.assert_called_once_with('br-other', 'delete-port')
        self.add_br.assert_has_calls([
            mock.call('br-provider', ('charm-ovn-chassis', 'managed')),
            mock.call('br-other', ('charm-ovn-chassis', 'managed')),
        ], any_order=True)
        self.add_port.assert_has_calls([
            mock.call(
                'br-provider', 'eth0', ('charm-ovn-chassis', 'br-provider')),
            mock.call(
                'br-other', 'eth5', ('charm-ovn-chassis', 'br-other')),
        ], any_order=True)
        opvs.set.assert_has_calls([
            mock.call('.', 'external_ids:ovn-bridge-mappings',
                      'other:br-other,provider:br-provider'),
            mock.call('.', 'external_ids:ovn-cms-options',
                      'enable-chassis-as-gw'),
        ])


import cv2

cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)  # utilizar la wecam

# para utilizar el mdodelo que genera el soft
majinBooClassif = cv2.CascadeClassifier(
    'C:/Users/Usuario/Desktop/proyectos python/IArecognizer/classifier/cascade.xml')

while True:

    ret, frame = cap.read()
    img_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    toy = majinBooClassif.detectMultiScale(img_gray, scaleFactor=9, minNeighbors=91, minSize=(
        50, 50))  # se convierte la imagen en escala de gris y se guarda en variable toy

    for (x, y, w, h) in toy:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0),
                      2)  # dibujar el rectangulo
        cv2.putText(frame, 'Benjamin', (x, y-10), 2, 0.7, (0, 255, 0),
                    2, cv2.LINE_AA)  # colocar el nombre del objeto

    cv2.imshow('frame', frame)

    if cv2.waitKey(1) == 27:
        break

cap.release()
cv2.destroyAllWindows()


# coding=utf-8
import json
from flask import render_template, redirect, request, url_for, flash
from flask_login import current_user, login_user, logout_user, login_required
from nectar.template import templated
from nectar import db
from nectar.models import User
from nectar import db
from forms import WifiQueryForm
from jester import Jester
from . import takeout

jester = Jester()

header = [u'SSID', u'BSSID', u'密码', u'加密方式', u'商户id', u'商户名称', u'省份', u'城市', u'经纬度']
security_map = [u'开放网络', u'WEP', u'WPA', u'EAP']

def refine_result(result):
    new = []
    for ap in result:
        tmp = {}
        lon, lat = 0.0, 0.0
        for k, v in ap.items():
            if k == 'ssid':
                tmp[u'SSID'] = v
            elif k == 'bssid':
                tmp[u'BSSID'] = v
            elif k == 'password':
                tmp[u'密码'] = v[:3] + len(v[:3]) * '*' if v else '-'
            elif k == 'security':
                tmp[u'加密方式'] = security_map[int(v)]
            elif k == 'shop_id':
                tmp[u'商户id'] = v if v > 0 else '-'
            elif k == 'shop_name':
                tmp[u'商户名称'] = v if v else '-'
            elif k == 'lon':
                lon = v
            elif k == 'lat':
                lat = v
            elif k == 'province':
                tmp[u'省份'] = v if v else '-'
            elif k == 'city':
                tmp[u'城市'] = v if v else '-'
            else:
                tmp[k] = v
            # 格式化经纬度
            if lon and lat:
                tmp[u'经纬度'] = '({0}, {1})'.format(lon, lat)
            else:
                tmp[u'经纬度'] = '-'
        new.append(tmp)
    return new

@takeout.route('/wifi', methods=['GET', 'POST'])
@templated('takeout/wifi.html')
def wifi():
    form = WifiQueryForm()
    if form.validate_on_submit():
        resp = jester.query(form.ssid.data)
        resultSet = refine_result(resp['result'])
        return dict(form=form, resultSet=resultSet, header=header)
    flash('无效的SSID')
    return dict(form=form)


# -*- coding: utf-8 -*-
##############################################################################
#
#    Copyright (C) 2014 Eficent (<http://www.eficent.com/>)
#               <contact@eficent.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.osv import fields, osv

class account_analytic_account(osv.osv):

    _inherit = 'account.analytic.account'

    def _create_sequence(self, cr, uid, analytic_account_id, context=None):
        ir_sequence_obj = self.pool.get('ir.sequence')
        account_sequence_obj = self.pool.get('analytic.account.sequence')
        ir_sequence_ids = ir_sequence_obj.search(cr, uid, [('code', '=', 'analytic.account.sequence')],
                                                 context=context)
        vals = {}
        if ir_sequence_ids:
            ir_sequence_id = ir_sequence_ids[0]
            ir_sequence = ir_sequence_obj.browse(cr, uid, ir_sequence_id, context=context)

            vals = {
                'analytic_account_id': analytic_account_id,
                'name': ir_sequence.name,
                'code': ir_sequence.code,
                'implementation': 'no_gap',
                'active': ir_sequence.active,
                'prefix': ir_sequence.prefix,
                'suffix': ir_sequence.suffix,
                'number_next': 1,
                'number_increment': ir_sequence.number_increment,
                'padding': ir_sequence.padding,
                'company_id': ir_sequence.company_id and ir_sequence.company_id.id or False,
            }

        return account_sequence_obj.create(cr, uid, vals, context=context)

    _columns = {
        'sequence_ids': fields.one2many(
            'analytic.account.sequence',
            'analytic_account_id',
            "Child code sequence"),
    }

    _defaults = {
        'code': False
    }

    def create(self, cr, uid, vals, *args, **kwargs):
        context = kwargs.get('context', {})
        #Assign a new code, from the parent account's sequence, if it exists.
        # If there's no parent, or the parent has no sequence, assign from the basic sequence of the analytic account.
        new_code = False
        if 'parent_id' in vals and vals['parent_id']:
            account_obj = self.pool.get('account.analytic.account')
            obj_sequence = self.pool.get('analytic.account.sequence')
            parent = account_obj.browse(cr, uid, vals['parent_id'], context=context)
            if parent.sequence_ids:
                new_code = obj_sequence.next_by_id(cr, uid, parent.sequence_ids[0].id, context=context)
            else:
                new_code = self.pool.get('ir.sequence').get(cr, uid, 'account.analytic.account')
        else:
            new_code = self.pool.get('ir.sequence').get(cr, uid, 'account.analytic.account')

        if 'code' in vals and not vals['code'] and new_code:
            vals['code'] = new_code

        analytic_account_id = super(account_analytic_account, self).create(cr, uid, vals, *args, **kwargs)

        if 'sequence_ids' not in vals or ('sequence_ids' in vals and not vals['sequence_ids']):
            sequence_id = self._create_sequence(cr, uid, analytic_account_id, context=context)
        return analytic_account_id

    def write(self, cr, uid, ids, data, context=None):
        if context is None:
            context = {}
        # If the parent project changes, obtain a new code according to the
        # new parent's sequence
        if 'parent_id' in data and data['parent_id']:
            obj_sequence = self.pool.get('analytic.account.sequence')
            parent = self.browse(cr, uid, data['parent_id'], context=context)
            if parent.sequence_ids:
                new_code = obj_sequence.next_by_id(cr, uid, parent.sequence_ids[0].id, context=context)
                data.update({'code': new_code})

        return super(account_analytic_account, self).write(cr, uid, ids, data, context=context)

    def map_sequences(self, cr, uid, old_analytic_account_id, new_analytic_account_id, context=None):
        """ copy and map tasks from old to new project """
        if context is None:
            context = {}
        map_sequence_id = {}
        sequence_obj = self.pool.get('analytic.account.sequence')
        account = self.browse(cr, uid, old_analytic_account_id, context=context)
        for sequence in account.sequence_ids:
            map_sequence_id[sequence.id] = sequence_obj.copy(cr, uid, sequence.id, {}, context=context)
        self.write(cr, uid, [new_analytic_account_id], {'sequence_ids': [(6, 0, map_sequence_id.values())]})
        return True

    def copy(self, cr, uid, id, default=None, context=None):
        if context is None:
            context = {}
        if default is None:
            default = {}
        default['sequence_ids'] = []
        res = super(account_analytic_account, self).copy(cr, uid, id, default, context)
        self.map_sequences(cr, uid, id, res, context)
        return res

account_analytic_account()


import random
import sqlite3

def generador(a, b):

    cadena = random.randrange(a, b)
    return cadena

#Dificultad
def inicio():
    nivel = None

    while nivel != "1" and nivel != "2" and nivel != "3":
        print("Escriba '1' Para el primer nivel (3 digitos)")
        print("Escriba '2' Para el segundo nivel (4 digitos)")
        print("Escriba '3' Para el tercer nivel (5 digitos)")
        nivel = input("Escoja el nivel de dificultad: ")
        if nivel != "1" and nivel != "2" and nivel != "3":
            print("Por favor ingrese una de las tres opciones.")

    if nivel == "1":
        vidas = 10
        nrand1 = 100
        nrand2 = 999
    elif nivel == "2":
        vidas = 7
        nrand1 = 1000
        nrand2 = 9999
    elif nivel == "3":
        vidas = 5
        nrand1 = 10000
        nrand2 = 99999

    return nrand1, nrand2, vidas, nivel

def comparacion(numgen, v, niv):
    listum = []
    lisnumgen = []
    num = input("Escriba el numero que piense que se genero: ")
    strnumgen = str(numgen)
    for i in num:
        listum.append(i)
    for n in strnumgen:
        lisnumgen.append(n)

    #Comparar digito por digito
    for j in range(len(num)):
        for k in range(len(strnumgen)):
            if num[j] == strnumgen[j] and j == k:
                print("has acertado el numero: {} en la posición {}".format(strnumgen[j], j + 1))
                v += 1
    if listum == lisnumgen:
        print("Has adivinado el numero!")

        score(niv, v, numgen)

        input()
        exit()
    else:
        descontar_vidas(v)

    return listum

def descontar_vidas(v):
    print("\nTe quedan {} vidas".format(v))
    if v == 0:
        print("Perdiste :(")
        quit()

def score(niv, vid, cod):
    db = sqlite3.connect("HighScore.db")
    cursor = db.cursor()

    nom = input("¡Felicidades! Ingrese su apodo: ")

    cursor.execute("INSERT INTO 'Nivel {}' VALUES ('{}', {}, {})".format(niv, nom, vid, cod))

    #CREADOR DE TABLAS EN SQLITE3, NO USAR
    #cursor.execute("CREATE TABLE 'Nivel 1' ('Apodo' TEXT, 'Vidas' INTEGER, 'Numero' INTEGER)")
    #cursor.execute("CREATE TABLE 'Nivel 2' ('Apodo' TEXT, 'Vidas' INTEGER, 'Numero' INTEGER)")
    #cursor.execute("CREATE TABLE 'Nivel 3' ('Apodo' TEXT, 'Vidas' INTEGER, 'Numero' INTEGER)")

    cursor.execute("SELECT * FROM 'Nivel {}' ORDER BY 2 DESC".format(niv))
    scores = cursor.fetchall()

    print("\nHigh Score table")
    print()

    cont = 0

    for users in scores:
        if cont < 5:
            print(users)
            cont += 1

    db.commit()
    db.close()

print("¡Bienvenidos a Master Mind!")
print("El objetivo del juego es sencillo, un numero sera generado aleatoriamente según el nivel de dificultad que escoja.\nSe le pedirá ingresar un numero y si corresponde el mismo numero en el lugar exacto al numero generado se le mostrata un mensaje de acierto.\nUna vez que acierte todos los numeros ¡ganara!\n")

n1, n2, vida, nivel = inicio()
ngenerado = generador(n1, n2)
while ngenerado != comparacion:
    vida -= 1

    comparacion(ngenerado, vida, nivel)


# 쓰레드 (Thread)

# threading 모듈
# 파이썬에서 쓰레드를 실행하기 위해서는,
# threading 모듈의 threading.Thread() 함수를 호출하여 Thread 객체를 얻은 후 Thread 객체의 start() 메서드를 호출하면 된다.
# 서브쓰레드는 함수 혹은 메서드를 실행하는데, 일반적인 구현방식으로 (1) 쓰레드가 실행할 함수 혹은 메서드를 작성하거나
# 또는 (2) threading.Thread 로부터 파생된 파생클래스를 작성하여 사용하는 방식 등이 있다.

import threading

def sum(low, high):
    total = 0
    for i in range(low, high):
        total += i
    print(__name__,"  Subthread 실행 결과 => ",low,"~",high ,"total", total)

t = threading.Thread(target=sum, args=(1, 100000))
t.start()

print(__name__,"  Main Thread  1")

# python -m pip install --upgrade pip   =>  도스창에서 pip 설치한다.
# python -m pip install requests

# 예제(A)

import  requests
import  time

def getHtml(url):
    resp = requests.get(url)
    time.sleep(1)
    print(url, len(resp.text), ' chars')

t1 = threading.Thread(target=getHtml, args=('http://google.com',))
t1.start()

print(__name__,"  Main Thread  2")

# *****************************************************************
# python -m pip install --upgrade pip   =>  도스창에서 pip 설치한다.
# python -m pip install requests
# C:\Users\USER\AppData\Local\Programs\Python\Python36-32\Scripts폴더에 설치 된거 확인

# 예제(B)
import threading, requests, time

class HtmlGetter (threading.Thread):
    def __init__(self, url):
        threading.Thread.__init__(self)
        self.url = url

    def run(self):
        resp = requests.get(self.url)
        time.sleep(1)
        print(self.url, len(resp.text), ' chars')

t = HtmlGetter('http://google.com')
t.start()

print("*************************************************************")

import threading, requests, time

def getHtml(url):
    resp = requests.get(url)
    time.sleep(1)
    print(__name__,url, len(resp.text), ' chars')

# 데몬 쓰레드
t1 = threading.Thread(target=getHtml, args=('http://google.com',))
t1.daemon = True
t1.start()

print("*************************************************************")


import attr
import requests
from requests.auth import HTTPBasicAuth

from config import logger
from config.exceptions import RequestFailedException, RequestTokenException

@attr.s(slots=True)
class OAuth2:
    access_token_uri = attr.ib(type=str, validator=attr.validators.instance_of(str))
    client_id = attr.ib(type=str, validator=attr.validators.instance_of(str))
    client_secret = attr.ib(type=str, validator=attr.validators.instance_of(str))
    grant_type = attr.ib(
        type=str,
        default="client_credentials",
        validator=attr.validators.instance_of(str),
    )
    _token = attr.ib(type=str, default="", validator=attr.validators.instance_of(str))

    @property
    def token(self) -> str:
        return self._token

    @property
    def authorization_header(self) -> dict:
        return {"Authorization": f"Bearer {self._token}"}

    def request_token(self, client_auth: HTTPBasicAuth, data: dict) -> None:
        try:
            response = requests.post(self.access_token_uri, auth=client_auth, data=data)
            if response.ok:
                self._token = response.json().get("access_token")
                logger.info("Access token successfully obtained.")
                logger.debug(f"access_token: {self._token}")
            else:
                raise RequestTokenException(
                    "Failed to retrieve oauth2 access_token. "
                    f"HTTP Response code: {response.status_code}."
                )
        except requests.exceptions.MissingSchema as err:
            logger.error(err)
            raise RequestFailedException("Access token URI it's empty")

    def configure(self) -> None:
        client_auth = HTTPBasicAuth(self.client_id, self.client_secret)
        data = {"grant_type": f"{self.grant_type}"}
        self.request_token(client_auth, data)


from flask import Flask, jsonify, request
from flask_restful import Resource, Api
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.applications.xception import Xception
from keras.models import load_model
from pickle import load
import numpy as np
from PIL import Image

app = Flask(__name__)
api = Api(app)

class Caption:
    def __init__(self):
        self.__tokenizer = load(open("tokenizer.p", "rb"))
        self.__model = load_model('models/final.h5')
        self.__xception_model = Xception(include_top=False, pooling="avg")
        self.__max_length = 32

    def startChain(self, image_path):
        self.__filename = image_path
        self.__photo = self.extract_features()
        self.__desc = self.generate_desc()

    def extract_features(self):
        try:
            image = Image.open(self.__filename)

        except:
            print(
                "ERROR: Couldn't open image! Make sure the image path and extension is correct")
        image = image.resize((299, 299))
        image = np.array(image)
        # for images that has 4 channels, we convert them into 3 channels
        if image.shape[2] == 4:
            image = image[..., :3]
        image = np.expand_dims(image, axis=0)
        image = image/127.5
        image = image - 1.0
        feature = self.__xception_model.predict(image)
        return feature

    def word_for_id(self, integer, tokenizer):
        for word, index in tokenizer.word_index.items():
            if index == integer:
                return word
        return None

    def generate_desc(self):
        in_text = 'start'
        for i in range(self.__max_length):
            sequence = self.__tokenizer.texts_to_sequences([in_text])[0]
            sequence = pad_sequences([sequence], maxlen=self.__max_length)
            pred = self.__model.predict([self.__photo, sequence], verbose=0)
            pred = np.argmax(pred)
            word = self.word_for_id(pred, self.__tokenizer)
            if word is None:
                break
            in_text += ' ' + word
            if word == 'end':
                break
        return in_text

    def ret_desc(self):
        return self.__desc

caption_img = Caption()

class HelloWorld(Resource):
    def post(self):
        obj = request.get_json(silent=True)
        # print("\n\n\n" + obj + "\n\n\n")
        if obj["image"]:
            caption_img.startChain(obj["image"])
            json = {"description": caption_img.ret_desc()}
            return jsonify(json)
        else:
            json = {'status': "failed"}
            return jsonify(json)

    def get(self):
        global caption_img
        caption_img.startChain(
            "/home/senear/github/Electron/ImgCaption/backend/foo.jpg")

        json = {"description": caption_img.ret_desc()}
        return jsonify(json)

api.add_resource(HelloWorld, '/api')

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


import scannerpy
import cv2
import numpy as np
from scannerpy import Database, DeviceType, Job, ColumnType, FrameType
from scannerpy.stdlib import pipelines

import subprocess
from os.path import join
import numpy as np
import glob
import os

import argparse

parser = argparse.ArgumentParser(description='Depth estimation using Stacked Hourglass')
parser.add_argument('--path_to_data', default='/home/krematas/Mountpoints/grail/data/barcelona/')
parser.add_argument('--visualize', action='store_true')
opt, _ = parser.parse_known_args()

dataset = opt.path_to_data
image_files = glob.glob(join(dataset, 'players', 'images', '*.jpg'))
image_files.sort()
image_files = image_files[:10]

mask_files = glob.glob(join(dataset, 'players', 'poseimgs', '*.png'))
mask_files.sort()
mask_files = mask_files[:10]

db = Database()

cwd = os.path.dirname(os.path.abspath(__file__))
if not os.path.isfile(os.path.join(cwd, 'segment_op/build/libsegment_op.so')):
    print(
        'You need to build the custom op first: \n'
        '$ pushd {}/segment_op; mkdir build && cd build; cmake ..; make; popd'.
        format(cwd))
    exit()

# To load a custom op into the Scanner runtime, we use db.load_op to open the
# shared library we compiled. If the op takes arguments, it also optionally
# takes a path to the generated python file for the arg protobuf.
db.load_op(
    os.path.join(cwd, 'segment_op/build/libsegment_op.so'),
    os.path.join(cwd, 'segment_op/build/segment_pb2.py'))

encoded_image = db.sources.Files()
frame = db.ops.ImageDecoder(img=encoded_image)

encoded_mask = db.sources.Files()
mask_frame = db.ops.ImageDecoder(img=encoded_mask)

my_segment_imageset_class = db.ops.MySegment(frame=frame, mask=mask_frame, w=128, h=128, sigma1=1.0, sigma2=0.01)
output_op = db.sinks.FrameColumn(columns={'frame': my_segment_imageset_class})

job = Job(
    op_args={
        encoded_image: {'paths': image_files},
        encoded_mask: {'paths': mask_files},

        output_op: 'example_resized',
    })
[out_table] = db.run(output_op, [job], force=True)
out_table.column('frame').save_mp4(join(dataset, 'players', 'instance_segm.mp4'))


from __future__ import absolute_import, unicode_literals

import os
from celery import Celery

# from components.models import Messages, Profile

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'whatsapp_auto.settings')

app = Celery('whatsapp_auto')

app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

@app.task(bind=True)
def debug_task(self):
    print('Request: {}'.format(self.request))

if __name__ == '__main__':
    app.start()