from .sample import Sample

from .meta import fit_field

from .diffeomorphisms import Image

from patsy import dmatrix

import numpy as np

from scipy.stats.distributions import t

import pandas as pd

import pickle

class PopulationModel:
    """
    The FMRI population model

    Parameters
    ----------
    sample : Sample
    formula : str
        A formula like object that is understood by patsy.
    design : ndarray
        If None, will be created from formula
        Directly specifying a design matrix, i.e., providing design
        will take precedence from the formula interface.
    mask : None, bool, ndarray, or str
        If False or None, the population model will be fitted only
        at points, at which the population model is identifiable.
        If True or 'template', the population model will,
        additionally, only be fitted at points, at which the
        template in the population has valid intensities (i.e. > 0
        and not NAN). If 'sample', the population model will be
        fitted only at points at which the sample provides valid
        summary statistics for *all* fields in the sample.
    """

    def __init__(self, sample, formula=None, design=None):
        assert type(sample) is Sample, 'sample must be of type Sample'

        self.sample     = sample
        self.covariates = sample.covariates
        self.statistics = sample.statistics

        if (formula is not None) and (design is None):
            dmat = dmatrix(formula, self.covariates, eval_env=-1)
            parameter_names = dmat.design_info.column_names
            design = np.asarray(dmat)
        else:
            parameter_names = ['Intercept']

        self.formula = formula
        self.parameter_names = parameter_names
        self.design = design

    def fit(self, mask=True):
        """
        Fit the model to the data

        Returns
        -------
        PopulationResult
        """
        if mask is True:
            mask = 'vb'

        if mask is False:
            mask = None

        if type(mask) is str:
            if mask == 'vb':
                mask = self.sample.vb.get_mask()
                massname = 'template (vb)'
            elif mask == 'vb_background':
                mask = self.sample.vb_background.get_mask()
                maskname = 'template background (vb_background)'
            elif mask == 'vb_estimate':
                mask = self.sample.vb_estimate.get_mask()
                maskname = 'template estimate (vb_estimate)'
            else:
                mask = None
                maskname = 'default'

        if mask is not None:
            assert type(mask) is np.ndarray, 'mask must be an ndarray'
            assert mask.any(), 'no valid points in mask'
            assert mask.dtype == bool, 'mask must be of dtype bool'

        self.mask = mask

        result = fit_field(
                statistics=self.statistics,
                design=self.design,
                mask=mask)

        return PopulationResult(statistics=result, model=self)

    ####################################################################
    # Save instance to and from disk
    ####################################################################

    def save(self, file, **kwargs):
        """
        Save model instance to disk

        This will save the current model instance to disk for later use.

        Parameters
        ----------
        file : str
            File name.
        """
        with open(file, 'wb') as output:
            pickle.dump(self, output, **kwargs)

class PopulationResult:

    def __init__(self, statistics, model):
        self.statistics = statistics
        self.model      = model
        self.parameter_names = model.parameter_names

    def get_parameter(self, p=0):
        f = np.moveaxis(self.statistics[...,0,:-1], -1, 0)
        return Image(data=f[p], reference=self.model.sample.vb.reference)

    def get_tstatistic(self, p=0):
        f = np.moveaxis(self.statistics[...,2,:-1], -1, 0)
        return Image(data=f[p], reference=self.model.sample.vb.reference)

    def get_heterogeneity(self):
        f = self.statistics[...,0,-1]
        return Image(data=f, reference=self.model.sample.vb.reference)

    def get_degree_of_freedom(self):
        f = self.statistics[...,1,-1]
        return Image(data=f, reference=self.model.sample.vb.reference)

    def at_index(self, index):
        # TODO: also add a stderr to the h-estimate (Knapp-Hartung!)

        x  = self.statistics[index]
        tstatistics = x[2,:-1]
        df = x[1,-1]
        pvalues = t.sf(tstatistics, df=df)

        df = pd.DataFrame({
            'parameter' : self.parameter_names + ['heterogeneity'],
            'point'     : x[0],
            'stderr'    : np.hstack((x[1,:-1], np.nan)),
            'tstatistic': np.hstack((tstatistics, np.nan)),
            'df'        : df,
            'pvalue'    : np.hstack((pvalues,np.nan))})

        return df

    ####################################################################
    # Save instance to and from disk
    ####################################################################

    def save(self, file, **kwargs):
        """
        Save model instance to disk

        This will save the current model instance to disk for later use.

        Parameters
        ----------
        file : str
            File name.
        """
        with open(file, 'wb') as output:
            pickle.dump(self, output, **kwargs)


from marvin import Bot

class ResumeAnalyzer:
    def __init__(self):
        self.bot = Bot(
            name="ResumeAnalyzer",
            instructions=("Analyze a set of resumes for a specific job position, "
                          "assign a score for each resume.")
        )

    async def analyze_resumes(self, job_position, resumes):
        scores = []
        for resume in resumes:
            name = await self.bot.say(f"Get the name of the candidate for resume {resume} without any other prose")
            result = await self.bot.say(f"Assign a score to the resume {resume} for the job position '{job_position}'")
            score = await self.bot.say(f"Extract only the score of the candidate for {result.content} without any other prose")
            scores.append({
                "name": name.content,
                "score": score.content,
                "analysis": result.content
            })
        return scores


from flask import Flask, request
import json
app = Flask(__name__)
import serial
import smtplib
import logging
import platform
import subprocess
import sys
import threading
from google.assistant.library.event import EventType
from aiy.assistant import auth_helpers
from aiy.assistant.library import Assistant
from aiy.board import Board, Led
from aiy.voice import tts
import time
SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587
GMAIL_USERNAME = 'tsha813@gmail.com'
GMAIL_PASSWORD = '' #hidden for security ;)

class Emailer:
    def sendmail(self, recipient, subject, content):

        headers = ["From: " + GMAIL_USERNAME, "Subject: " + subject, "To: " + recipient,
                   "MIME-Version: 1.0", "Content-Type: text/html"]
        headers = "\r\n".join(headers)

        session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
        session.ehlo()
        session.starttls()
        session.ehlo()

        session.login(GMAIL_USERNAME, GMAIL_PASSWORD)

        session.sendmail(GMAIL_USERNAME, recipient, headers + "\r\n\r\n" + content)
        #session.sendmail(GMAIL_USERNAME, recipient, headers.decode('utf-8') + "\r\n\r\n" + content.decode('utf-8'))
        session.quit

sender = Emailer()
sendTo = 'tsha813@gmail.com'
ser = serial.Serial('/dev/ttyACM0',9600,timeout=1)
ser.reset_input_buffer()
light = 0
lock = 0
garage = 0
temp = 0
actual_temp = 75
wanted_temp = 0
check_temp = 0
email = 0
light_bool = False
door_bool = False
@app.route('/receive', methods=['POST'])
def receive():
    data = json.loads(request.data.decode('utf-8'))
    global light
    global lock
    global garage
    global temp
    global actual_tmep
    global wanted_temp
    global check_temp
    global email
   # print(data['direction'])
    if data['direction'] == 'unlock1':
        lock = 1

    elif data['direction'] == 'lock1':
        lock = 0

    if data['direction'] == 'on1':
        light = 1

    elif data['direction'] == 'off1':
        light = 0

    if data['direction'] == 'unlock2':
        garage = 1

    elif data['direction'] == 'lock2':
        garage = 0

    if data['direction'] == 'tempu':
        temp = 1
        if check_temp == 0:
            wanted_temp = actual_temp
            check_temp = 1
        wanted_temp = wanted_temp + 1

    elif data['direction'] == 'tempd':
        temp = 0
        if check_temp == 0:
            wanted_temp = actual_temp
            check_temp = 1
        wanted_temp = wanted_temp - 1
    else:
        temp = 2

    if data['direction'] == 'email':
        email = 1

    ser.write((str(light)+ ' ' + str(lock) + ' ' + str(garage) + ' ' + str(temp) + ' ' + str(email) + '\n' ).encode('utf-8'))
    print(str(light)+ ' ' + str(lock) + ' ' + str(garage) + ' ' + str(temp) + ' ' + str(email) + '\n')

    if email == 1:
        #line = ser.readline().decode('utf-8')
        email_str = ""
        print("sending email...")
        if light_bool:
            email_str = email_str + "The light is on."
        else:
            email_str = email_str + "The Light is off."

        if door_bool:
            email_str = email_str + " The door is locked."
        else:
            email_str = email_str + " The door is unlocked."

        email_str = email_str + " The temperature in the house is " + str(actual_temp) + " degrees."
        line = ser.readline().decode('utf-8').rstrip()
        print(line)
        sender.sendmail(sendTo,"House devices status", email_str)
        email = 0
    return "Woop Woop this finally worked"

def light_on():
    global light
    light = 1
    ser.write((str(light)+ ' ' + str(lock) + ' ' + str(garage) + ' ' + str(temp) + ' ' + str(email) + '\n' ).encode('utf-8'))

def light_off():
    global light
    light = 0
    ser.write((str(light)+ ' ' + str(lock) + ' ' + str(garage) + ' ' + str(temp) + ' ' + str(email) + '\n' ).encode('utf-8'))

def lock_on():
    global lock
    lock = 1
    ser.write((str(light)+ ' ' + str(lock) + ' ' + str(garage) + ' ' + str(temp) + ' ' + str(email) + '\n' ).encode('utf-8'))

def lock_off():
    global lock
    lock = 0
    ser.write((str(light)+ ' ' + str(lock) + ' ' + str(garage) + ' ' + str(temp) + ' ' + str(email) + '\n' ).encode('utf-8'))

def garage_on():
    global garage
    garage = 1
    ser.write((str(light)+ ' ' + str(lock) + ' ' + str(garage) + ' ' + str(temp) + ' ' + str(email) + '\n' ).encode('utf-8'))

def garage_off():
    global garage
    garage = 0
    ser.write((str(light)+ ' ' + str(lock) + ' ' + str(garage) + ' ' + str(temp) + ' ' + str(email) + '\n' ).encode('utf-8'))

def email_send():
    global email
    email = 1
    ser.write((str(light)+ ' ' + str(lock) + ' ' + str(garage) + ' ' + str(temp) + ' ' + str(email) + '\n' ).encode('utf-8'))
    email_str = ""
    print("sending email...")
    if light_bool:
        email_str = email_str + "The light is on."
    else:
        email_str = email_str + "The Light is off."

    if door_bool:
        email_str = email_str + " The door is locked."
    else:
        email_str = email_str + " The door is unlocked."

    email_str = email_str + " The temperature in the house is " + str(actual_temp) + " degrees."
    line = ser.readline().decode('utf-8').rstrip()
    print(line)
    sender.sendmail(sendTo,"House devices status", email_str)
    email = 0

def temp_up():
	global temp
	temp = 1
	ser.write((str(light)+ ' ' + str(lock) + ' ' + str(garage) + ' ' + str(temp) + ' ' + str(email) + '\n' ).encode('utf-8'))
	temp = 2

def temp_down():
	global temp
	temp = 0
	ser.write((str(light)+ ' ' + str(lock) + ' ' + str(garage) + ' ' + str(temp) + ' ' + str(email) + '\n' ).encode('utf-8'))
	temp = 2

def process_event(assistant, led, event):
    logging.info(event)
    if event.type == EventType.ON_START_FINISHED:
        led.state = Led.BEACON_DARK  # Ready.
        print('Say "OK, Google" then speak, or press Ctrl+C to quit...')
    elif event.type == EventType.ON_CONVERSATION_TURN_STARTED:
        led.state = Led.ON  # Listening.
    elif event.type == EventType.ON_RECOGNIZING_SPEECH_FINISHED and event.args:
        print('You said:', event.args['text'])
        text = event.args['text'].lower()
        if text == 'turn on the light':
            assistant.stop_conversation()
            light_on()
        elif text == 'turn off the light':
            assistant.stop_conversation()
            light_off()
        elif text == 'unlock the door':
            assistant.stop_conversation()
            lock_on()
        elif text == 'lock the door':
            assistant.stop_conversation()
            lock_off()
        elif text == 'open the garage door':
            assistant.stop_conversation()
            garage_on()
        elif text == 'close the garage door':
            assistant.stop_conversation()
            garage_off()
        elif text == 'send current status':
            assistant.stop_conversation()
            email_send()
        elif text == 'turn up the temperature':
            assistant.stop_conversation()
            temp_up()
        elif text == 'turn down the temperature':
            assistant.stop_conversation()
            temp_down()

    elif event.type == EventType.ON_END_OF_UTTERANCE:
        led.state = Led.PULSE_QUICK  # Thinking.
    elif (event.type == EventType.ON_CONVERSATION_TURN_FINISHED
          or event.type == EventType.ON_CONVERSATION_TURN_TIMEOUT
          or event.type == EventType.ON_NO_RESPONSE):
        led.state = Led.BEACON_DARK  # Ready.
    elif event.type == EventType.ON_ASSISTANT_ERROR and event.args and event.args['is_fatal']:
        sys.exit(1)

def main():
    logging.basicConfig(level=logging.INFO)
    line = ser.readline().decode('utf-8').rstrip()
    print(line)
    credentials = auth_helpers.get_assistant_credentials()
    with Board() as board, Assistant(credentials) as assistant:
        for event in assistant.start():
            process_event(assistant, board.led, event)
def flask_run():
	app.run(host='192.168.1.201', port=5000)

def voice_run():
	main()
if __name__ == '__main__':
    flask_thread = threading.Thread(target=flask_run)
    voice_thread = threading.Thread(target = voice_run)
    flask_thread.start()
    voice_thread.start()
    flask_thread.join()
    voice_thread.join()


# -*- coding: utf-8 -*-
"""
Created on Tue Jul 26 19:04:23 2022

This code pulls static info from MIMIC IV for validation. Looks at the ADMISSIONS,
PATIENTS, CHARTEVENTS ,TRANSFERS, and services tables.

Covers:
Age
Gender
AdmitWeight
Ethnicity
HospitalAdmitTime24
UnitAdmitTime24
NumBeds
TeachingStatus
Unit Type
Trauma admisssion
time of day at time of prediction

TODO:
admission category (maybe later, needs to look at adm Dxs),
Can't find unit admit source anywhere.
UrgentAdmission - needs conversion

Run time: 15 min

@author: Kirby
"""
#%% package setup
import pandas as pd
import numpy as np
import time
import datetime
from pathlib import Path

start = time.time()

file_path = Path(__file__)
dataset_path = file_path.parent.parent.parent.joinpath('Dataset')
ext_valid_path = file_path.parent.parent.parent.joinpath('Ext Validation')
mimic_path = file_path.parent.parent.parent.parent.joinpath('mimic-iv-2.0')

#%% Finding relevant items.

items = pd.read_csv(mimic_path.joinpath('icu', 'd_items.csv.gz'))
items.loc[:, 'label'] = items['label'].str.lower()
items.loc[:, 'abbreviation'] = items['abbreviation'].str.lower()

# TODO: Add omr table for weights?
weight_items = items[items['label'].str.contains('weight')]
# Curated manually.
weight_items = [224639, 226512, 226531]
weight_items = items[items['itemid'].isin(weight_items)][['itemid']]

height_items = items[items['label'].str.contains('height')]
# 226707 is inches, 226730 is cm. Convert it all to cm.
height_items = height_items[['itemid']]

#%% Load in the info
comp = pd.read_csv(dataset_path.joinpath("MIMICIV_complete_dataset.csv"),
                   parse_dates=['intime'])
adm = pd.read_csv(mimic_path.joinpath('hosp',"admissions.csv.gz"),
                  usecols = ['subject_id', 'hadm_id', 'admittime',
                             'admission_type', 'race'],
                  parse_dates = ['admittime'])
pat = pd.read_csv(mimic_path.joinpath('hosp',"patients.csv.gz"),
                  usecols = ['subject_id', 'gender', 'anchor_age'])

tran = pd.read_csv(mimic_path.joinpath('hosp', "transfers.csv.gz"),
                  parse_dates=['intime','outtime'])

icu = pd.read_csv(mimic_path.joinpath('icu', "icustays.csv.gz"))

serv = pd.read_csv(mimic_path.joinpath('hosp', 'services.csv.gz'),
                   usecols=['hadm_id','curr_service'])

weight = pd.read_csv(mimic_path.joinpath('icu', "chartevents.csv.gz"),
                     nrows = 0)
height = pd.read_csv(mimic_path.joinpath('icu', "chartevents.csv.gz"),
                     nrows = 0)
for chunk in pd.read_csv(mimic_path.joinpath('icu', "chartevents.csv.gz"),
                         chunksize=1000000):
    temp_rows = chunk.merge(weight_items, how = 'inner', on = 'itemid')
    weight = pd.concat([weight,temp_rows])
    temp_rows = chunk.merge(height_items, how = 'inner', on = 'itemid')
    height = pd.concat([height,temp_rows])

#%% Drop stuff we don't care about.

#Drop irrelevant rows.
adm = adm[adm['hadm_id'].isin(comp['hadm_id'])]
pat = pat[pat['subject_id'].isin(comp['subject_id'])]
tran = tran[tran['hadm_id'].isin(comp['hadm_id'])]
icu = icu[icu['stay_id'].isin(comp['stay_id'])]
serv = serv[serv['hadm_id'].isin(comp['hadm_id'])]
weight = weight[weight['stay_id'].isin(comp['stay_id'])]
weight = weight[weight['warning'] == 0]
height = height[height['stay_id'].isin(comp['stay_id'])]
height = height[height['warning'] == 0]

#%% Get weight.
#Convert all weight to kg, mean if multiple values.
weight.loc[:, 'valuenum'] = weight['valuenum'].astype(float)
kg_weight = weight[weight['valueuom'] == 'kg']
lb_weight = weight[weight['valueuom'] != 'kg']

kg_weight = kg_weight[['stay_id', 'charttime', 'valuenum']]

lb_weight = lb_weight[['stay_id', 'charttime', 'valuenum']]
lb_weight.loc[:, 'valuenum'] = lb_weight['valuenum']/2.2

all_weight = pd.concat([kg_weight,lb_weight])
# Get rid of impossible or irrelevant weight measurements (less than 25 kg)
all_weight = all_weight[all_weight['valuenum'] >= 25]
# Take the first measurement per ICU stay as admission weight.
all_weight.sort_values(['stay_id', 'charttime'], inplace = True)
all_weight = all_weight.groupby('stay_id').first()
all_weight.reset_index(inplace=True)
all_weight.drop(columns = 'charttime', inplace = True)
all_weight.rename(columns = {'valuenum':'AdmissionWeight'}, inplace = True)

#%% Get height.
#Convert all height to cm, mean if multiple values.
height.loc[:, 'valuenum'] = height['valuenum'].astype(float)
cm_height = height[height['valueuom'] == 'cm']
in_height = height[height['valueuom'] != 'cm']

cm_height = height[['stay_id', 'charttime', 'valuenum']]

in_height = in_height[['stay_id', 'charttime', 'valuenum']]
in_height.loc[:, 'valuenum'] = in_height['valuenum']*2.54

all_height = pd.concat([cm_height,in_height])
# Get rid of impossible or irrelevant height measurements
all_height = all_height[all_height['valuenum'] >= 120]
all_height = all_height[all_height['valuenum'] <= 270]

# Take the first measurement per ICU stay as admission height.
all_height.sort_values(['stay_id', 'charttime'], inplace = True)
all_height = all_height.groupby('stay_id').first()
all_height.reset_index(inplace=True)
all_height.drop(columns = 'charttime', inplace = True)
all_height.rename(columns = {'valuenum':'AdmissionHeight'}, inplace = True)

#%% Get unit type. UnitType_MedSurg and UnitType_Neuro are two features in model.
unit_type = icu[['stay_id', 'first_careunit']].copy()
unit_type.loc[:, 'UnitType_MedSurg'] = unit_type['first_careunit'] == 'Medical/Surgical Intensive Care Unit (MICU/SICU)'
unit_type.loc[:, 'UnitType_Neuro'] = unit_type['first_careunit'] == 'Neuro Surgical Intensive Care Unit (Neuro SICU)'
for col_name in ['UnitType_MedSurg', 'UnitType_Neuro']:
    unit_type.loc[:, col_name] = unit_type[col_name].astype(int)

#%% Get if trauma or not.
serv.loc[:, 'Trauma'] = (serv['curr_service'] == 'TRAUM').astype(int).copy()
# Get if neurology/neurosurgery admission.
serv.loc[:, 'Neurology/Neurosurgery'] = ((serv['curr_service'] == 'EYE') |
                                         (serv['curr_service'] == 'NMED') |
                                         (serv['curr_service'] == 'NSURG')).astype(int).copy()

serv.drop(columns = 'curr_service', inplace = True)

#%% and unit stay type. Currently not in pruned feature list.
# #Find transfers.
# #Check if in each ICU stay

# #Find Readmits.
# #Check if there were subsequent stay_ids in teh same hadm_id

# #No SDU stays as far as I can tell.
# comp['UnitStayType_Stepdown'] = 0

#%% Merge stuff together.
comp = comp.merge(adm, how = 'left', on = ['subject_id', 'hadm_id'])
comp = comp.merge(serv, how = 'left', on = 'hadm_id')
comp = comp.merge(pat, how = 'left', on = 'subject_id')
comp = comp.merge(unit_type, how = 'left', on = 'stay_id')
comp = comp.merge(all_weight, how = 'left', on = 'stay_id')
comp = comp.merge(all_height, how = 'left', on = 'stay_id')

comp.rename(columns={'admission_type':'UrgentAdmission',
                     'admittime':'hospitalAdmitTime24',
                     'race':'Ethnicity',
                     'gender':'Gender',
                     'anchor_age': 'Age'
                     },inplace=True)

#%% Get urgent admissions.
comp['UrgentAdmission'] = (comp['UrgentAdmission'] == 'URGENT').astype(int)

#%% Tack on teaching status and numbeds column.
comp['TeachingStatus'] = 1
comp['NumBeds'] = 4 #>=500 beds.

#%% AdmitTime format
comp['hospitalAdmitTime24'] = comp['hospitalAdmitTime24'].dt.time
comp['UnitAdmitTime24'] = comp['intime'].dt.time

#%% Format ethnicity to match eICU.
def format_ethnic(ethnic):
    if ethnic.count('WHITE')>0:
        return 'Caucasian'
    elif ethnic.count('BLACK')>0:
        return 'African American'
    elif ethnic.count('ASIAN')>0:
        return 'Asian'
    elif ethnic.count('HISPANIC')>0:
        return 'Hispanic'
    elif ethnic.count('AMERICAN INDIAN')>0:
        return 'Native American'
    else:
        return 'Other/Unknown'

comp['Ethnicity'] = comp['Ethnicity'].apply(format_ethnic)

#%% Format gender.
def format_gender(gender):
    if gender == 'F':
        return 'Female'
    else:
        return 'Male'

comp['Gender'] = comp['Gender'].apply(format_gender)

#%%Get LOS at end of observation window.
comp['LOS'] = comp['end']

#%% Save off data.

for lead_hours in [0,1,3,6,12]:
    for obs_hours in [1,3,6,12]:
        comp.to_csv('MIMICIV_relative_'+ str(lead_hours) + 'hr_lead_' +
                    str(obs_hours) + '_static_features.csv',index=False)

calc_time = time.time() - start


import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from statsmodels.tsa.stattools import adfuller
from statsmodels.tsa.seasonal import seasonal_decompose
from statsmodels.tsa.arima_model import ARIMA
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

def get_stationarity(timeseries):

    # rolling statistics
    rolling_mean = timeseries.rolling(window=12).mean()
    rolling_std = timeseries.rolling(window=12).std()

    # rolling statistics plot
    original = plt.plot(timeseries, color='blue', label='Original')
    mean = plt.plot(rolling_mean, color='red', label='Rolling Mean')
    std = plt.plot(rolling_std, color='black', label='Rolling Std')
    plt.legend(loc='best')
    plt.title('Rolling Mean & Standard Deviation')
    plt.show(block=False)

    # Dickey–Fuller test:
    result = adfuller(timeseries['amount'])
    print('ADF Statistic: {}'.format(result[0]))
    print('p-value: {}'.format(result[1]))
    print('Critical Values:')
    for key, value in result[4].items():
        print('\t{}: {}'.format(key, value))

df = pd.read_csv('it.csv', parse_dates = ['day'], index_col = ['day'])
#df = pd.read_csv('AirPassengers.csv', parse_dates = ['date'], index_col = ['date'])
df.head()
plt.xlabel('Date')
plt.ylabel('Number of jobs recruitment')
#plt.plot(df)
df_log = np.log(df)
#plt.plot(df_log)

df_log_shift = df_log - df_log.shift()
df_log_shift.dropna(inplace=True)
#get_stationarity(df_log_shift)

decomposition = seasonal_decompose(df_log)
model = ARIMA(df_log, order=(2,1,2))
results = model.fit(disp=-1)
#plt.plot(df_log_shift)
#plt.plot(results.fittedvalues, color='red')

predictions_ARIMA_diff = pd.Series(results.fittedvalues, copy=True)
predictions_ARIMA_diff_cumsum = predictions_ARIMA_diff.cumsum()
predictions_ARIMA_log = pd.Series(df_log['amount'].iloc[0], index=df_log.index)
predictions_ARIMA_log = predictions_ARIMA_log.add(predictions_ARIMA_diff_cumsum, fill_value=0)
predictions_ARIMA = np.exp(predictions_ARIMA_log)
#60 = 4 * 12 + 12
results.plot_predict(1,96)
#plt.plot(df)
#plt.plot(predictions_ARIMA)
plt.show()


#!/usr/bin/python3

'''client example. Load model and query utterances'''

import lmz

LMZ = lmz.LM_Zoo()

utterances = [
	'The quick brown fox jumped over the lazy dogs .',
	'The horse raced past the barn fell .'
]

result = LMZ.query(utterances, modelIds="*", measures="*")
print(result)


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

__author__ = 'Owen_Study/owen_study@126.com'
# Create date: 17-7-18 下午10:43

import shelve
import const, publicparameters, ordermanage, common
import priceupdate, ormmysql
import time
'''对查找到的COIN进行交易，买入或者卖出'''

'''交易结构'''
class OrderItem(object):
    def __init__(self, market, coin):

        # 预测价格的信息
        self.priceitem = None
        # market
        self.market = market
        # coin
        # 可以传入mk_type,默认为btc
        if len(coin.split('_')) == 2:
            self.coin_pair = coin
            self.coin = coin.split('_')[0]
        else:
            self.coin_pair=coin+'_btc'
            self.coin = coin
        # 买入价格
        self.buy_price = None
        # 买入金额，默认为10RMB
        self.buy_amount = None
        # 买入order_id
        self.buy_order_id = None

        # 买入时间
        self.buy_date = None
        # 买入UNIT
        self.buy_units = None
        # 买入状态, Open, Closed
        self.buy_status = None

        # 卖出相关
        # 卖出的order_id
        self.sell_order_id = None
        # 卖出价格
        self.sell_price = None
        # 卖出UNITS
        self.sell_units = None
        # 卖出金额
        self.sell_amount = None
        # 卖出日期
        self.sell_date = None
        # 卖出状态
        self.sell_status = None
    '''生成表的动态类，用来自动生成ORM的模型'''
    def gen_table_class(self):

        create_table_sql = 'CREATE TABLE IF NOT EXISTS t_coin_trans (\n'+\
                    'trans_id INT(11) NOT NULL AUTO_INCREMENT,\n'
        table_class= 'orderitemTable = Table(\n'+ '"t_coin_trans", metadata,\n'+ \
                     "Column('trans_id', INT(11), primary_key=True),\n"
        for name, value in vars(self).items():
            # Column('trans_id', Integer, primary_key=True),
            table_class = table_class + "Column('{0}', VARCHAR(100)),\n".format(name)
            create_table_sql = create_table_sql + "'{0}' VARCHAR(100),\n".format(name)
        create_table_sql = create_table_sql[:len(create_table_sql)-2]+'\n)'
        table_class = table_class[:len(table_class)-2] +'\n)'
        print(table_class)
        print(create_table_sql)
        return table_class
    '''转换成字符保存'''
    # TODO
    def __repr__(self):
        order_detail_format = '{0}|{1}|{2}|{3}|{4}|{5}|{6}|{7}|{8}|{9}|{10}|{11}|{12}|{13}|\n'
        if self.sell_amount is not None:
            profit_amount = round(self.sell_amount - self.buy_amount,10)
        else:
            profit_amount = ''
        return order_detail_format.format(self.market.rjust(8),self.coin.rjust(6), profit_amount, self.buy_order_id,self.buy_date,\
                                          self.buy_status,self.buy_price,self.buy_amount,self.buy_units,self.sell_order_id,\
                                          self.sell_date,self.sell_status,self.sell_amount,self.sell_units)
    # 打印出来字符
    def __str__(self):
        return self.__repr__()

'''aex trans item，网站的交易历史记录'''
class AEXTransItem(object):
    def __init__(self, transitem ):
        self.id=transitem.get('id')
        self.coin=transitem.get('coinname')
        self.price=transitem.get('price')
        self.volume=transitem.get('volume')
        self.btcvolume=float(self.price) * float(self.volume)
        self.buyer_id=transitem.get('buyer_id')
        self.seller_id=transitem.get('seller_id')
        self.trans_time=transitem.get('time')

'''交易类'''
class CoinTrans(object):
    def __init__(self, market):
        # 订单状态
        const.ORDER_STATUS_OPEN = 'open'
        const.ORDER_STATUS_CLOSED = 'closed'
        const.ORDER_STATUS_CANCEL = 'cancelled'
        const.TRANS_TYPE_BUY='buy'
        const.TRANS_TYPE_SELL='sell'
        # 取消订单状态
        const.CANCEL_STATUS_SUCC = 'success'
        const.CANCEL_STATUS_FAIL= 'fail'

        # 最大的交易订单数，
        self.__max_open_order_pool = publicparameters.MAX_OPEN_ORDER_POOL
        # 每次交易的金额
        self.__trans_amount_per_trans = publicparameters.TRANS_AMOUNT_PER_ORDER
        # 止盈比例
        self.__sell_profit_rate = publicparameters.SELL_PROFIT_RATE
        # 市场的交易处理器
        self.order_market = ordermanage.OrderManage(market)
        # 市场
        self.market = market

        pass
    '''卖出条件检查'''
    def sell_check(self):
        open_order_list = ormmysql.openorderlist()
        for curroderitem in open_order_list:
            # The transaction of selling is in progress then check next
            if curroderitem.sell_status ==const.ORDER_STATUS_OPEN:
                continue
            pricebuffer = priceupdate.PriceBuffer(self.market, save_log_flag=False)
            # 获取当前的价格

            newpriceitem = pricebuffer.getpriceitem(self.market, curroderitem.coin+'_btc')
            if newpriceitem is not None and curroderitem.buy_status == const.ORDER_STATUS_CLOSED:
                # 测试，卖单提前生成好，等待直接成效
                rounding_num_unit = publicparameters.rounding_unit(curroderitem.coin)
                rounding_num_price = publicparameters.rounding_price(curroderitem.coin)
                default_sell_price = round(curroderitem.buy_price*(1+publicparameters.SELL_PROFIT_RATE),rounding_num_price)
                # trans_status = self.coin_trans(self.market, 'sell', default_sell_price, curroderitem.priceitem)
                # if newpriceitem.buy_price >= curroderitem.buy_price*(1+publicparameters.SELL_PROFIT_RATE):
                #     # 执行实际的卖出操作
                #     trans_status = self.coin_trans(self.market, 'sell', newpriceitem.buy_price, curroderitem.priceitem)
                #
                #     if trans_status is True:
                #         pass
                        # print('{0}:已经成功卖出,价格:{1}, coin: {2}, 盈利百分比: {3}'.format(common.get_curr_time_str(), newpriceitem.buy_price,\
                        #                                                         curroderitem.coin, publicparameters.SELL_PROFIT_RATE))

    '''查询已经存在的orderitem, 并返回'''
    def get_order_item(self, priceitem):
        orderitem_result = None
        order_list = ormmysql.openorderlist()
        for item in order_list:
            if item.priceitem.pricedate == priceitem.pricedate and item.priceitem.coin == priceitem.coin\
                and item.priceitem.buy_price == priceitem.buy_price:
                orderitem_result = item
                break
        return orderitem_result

    '''更新订单列表中状态'''
    def update_order_status(self):
        order_list = ormmysql.openorderlist()
        for orderitem in order_list:
            if orderitem.buy_status == const.ORDER_STATUS_OPEN:
                order_status = self.order_market.getOrderStatus(orderitem.buy_order_id, orderitem.priceitem.coin)
                # 处理状态取值时错误的情况
                if order_status is None:
                    continue
                orderitem.buy_status = order_status
                ormmysql.updateorder(orderitem)
            if orderitem.sell_status == const.ORDER_STATUS_OPEN:
                order_status = self.order_market.getOrderStatus(orderitem.sell_order_id, orderitem.priceitem.coin)
                # 处理状态取值时错误的情况
                if order_status is None:
                    continue
                orderitem.sell_status = order_status
                ormmysql.updateorder(orderitem)
                if order_status == const.ORDER_STATUS_CLOSED:
                    orderitem.sell_date = common.get_curr_time_str()
                    ormmysql.updateorder(orderitem)
                    # 把交易记录从交易表转移到LOG表
                    ormmysql.delorder(orderitem)
                    profit_amount = round(orderitem.sell_amount - orderitem.buy_amount,10)
                    print('{0}:[{1}]{smile}已经成功交易,盈利{2}！ BuyPrice:{3}, SellPrice:{4}, ProfiteRate:{5}'.format(\
                            common.get_curr_time_str(), orderitem.priceitem.coin, profit_amount, orderitem.buy_price, orderitem.sell_price,\
                            publicparameters.SELL_PROFIT_RATE, smile='^_^ '*5))

                    # the sell status is closed to move log table
            # 这里会导致出问题，出现上面更新后又再次删除的情况
            # if orderitem.sell_status == const.ORDER_STATUS_CLOSED:
            #     # 把交易记录从交易表转移到LOG表
            #     ormmysql.delorder(orderitem)
        pass

    '''取消超时买入订单，买入挂单超过指定的时间则取消'''
    # TODO 这个功能还需要完善
    def cancle_ot_buy_order(self, duration):
        open_order_list = ormmysql.openorderlist()
        curr_time = common.CommonFunction.get_curr_date()
        for curr_order_item in open_order_list:
            diff = curr_time - common.CommonFunction.strtotime(curr_order_item.buy_date)
            diffseconds = diff.seconds
            # Only cancel the buy status =open
            if diffseconds >duration and curr_order_item.buy_status == const.ORDER_STATUS_OPEN:
                cancel_status = self.order_market.cancelOrder(curr_order_item.buy_order_id, curr_order_item.coin)
                if cancel_status == 'success':
                    curr_order_item.buy_status=const.ORDER_STATUS_CANCEL
                    # update the buy status
                    ormmysql.updateorder(curr_order_item)
                    # move the records to log table
                    ormmysql.delorder(curr_order_item)
                    pass

    # '''保存订单到数据库文件中以便查询'''
    # def save_order(self, orderitem, ordertype = 'new'):
    #     # 保存新的和更新的订单到文件中
    #     open_trans_file_db = shelve.open(self.open_trans_file_db_name, writeback=True)
    #     ordernum = len(open_trans_file_db.items()) + 1
    #     ordername = 'order{0}'.format(ordernum)
    #     if ordertype == 'new':
    #         # 直接增加一条记录
    #         open_trans_file_db[ordername] = orderitem
    #     #     查找到对应的记录并更新
    #     elif ordertype == 'update':
    #         for order in open_trans_file_db.items():
    #             curritem = order[1]
    #             if curritem.coin == orderitem.coin and curritem.buy_date == orderitem.buy_date:
    #                 open_trans_file_db[order[0]] = orderitem
    #                 break
    #     open_trans_file_db.close()
    #
    #     pass
    #

    # 判断是不是满足买入或者卖出条件
    def check_trans_indi(self, coin=None):
        # 总共的OPEN交易订单
        total_open_count = ormmysql.openordercount()
        if total_open_count >= publicparameters.MAX_OPEN_ORDER_POOL:
            return False
        if coin is not None:
            coin_rate = self.get_coin_rate_in_open_orders(coin)
            # 大于单个COIN在总的OPEN数量中允许的最大比例
            if coin_rate > publicparameters.COIN_MAX_RATE_IN_OPEN_ORDERS:
                return False
        return True
        pass
    # coin percentage out of total allow open orders,
    def get_coin_rate_in_open_orders(self, coin):
        open_order_list = ormmysql.openorderlist()
        # total_count = ormmysql.openordercount()
        # 用POOL的最大值来检查单个币种的比例
        total_count = publicparameters.MAX_OPEN_ORDER_POOL
        coin_count = 0
        for open_order in open_order_list:
            if open_order.coin == coin:
                coin_count = coin_count + 1
        if total_count == 0:
            rate =0
        else:
            rate = round(coin_count/total_count,2)
        return rate

    # 交易测试
    def test_coin_trans(self):
        pricebuffer = priceupdate.PriceBuffer('btc38', save_log_flag=False)
        priceitem = pricebuffer.getpriceitem('btc38', 'doge_btc')
        # 循环检查OPEN订单是不是满足卖出条件

        orderstatus1 = self.coin_trans( 'btc38', 'buy', 0.01224, priceitem)
        # orderstatus2 = self.coin_trans( 'btc38', 'sell', 0.01345, priceitem)
        # print('order status 1: {0}, 2: {1}'.format(orderstatus1, orderstatus2))
        runtimes = 0
        while (True):
            self.update_order_status()
            pricebuffer = priceupdate.PriceBuffer('btc38', save_log_flag=False)
            newpriceitem = pricebuffer.getpriceitem('btc38', 'doge_btc')
            order_list = ormmysql.openorderlist()
            for openorderitem in order_list:
                if openorderitem.buy_status == const.ORDER_STATUS_CLOSED:
                    # 满足卖出条件后提交卖出订单
                    if newpriceitem.buy_price>openorderitem.buy_price*(1+self.__sell_profit_rate) and openorderitem.sell_status ==const.ORDER_STATUS_OPEN:
                        self.coin_trans('btc38', 'sell', newpriceitem.buy_price, openorderitem.priceitem)
                        print('满足卖出条件，提交了定单{0}, 卖出价格:{1}'.format(openorderitem.sell_order_id, newpriceitem.buy_price))
            runtimes = runtimes + 1
            time.sleep(5)
            print('has run %d times'%runtimes)
            pass

        # 更新订单的状态
        self.update_order_status()

    '''交易信息
    @market ==btc38, bter
    @trans_type   =='sell', 'buy'
    @trans_price    交易价格
    @price_item     预测价格时的信息，用来进行对比
    '''
    def coin_trans(self, market, trans_type, trans_price, price_item):
        coin = price_item.coin
        coin_pair = coin+'_btc'

        # 判断是不是满足交易的条件，不满足则退出不进行交易
        if trans_type == const.TRANS_TYPE_BUY:
            if self.check_trans_indi(coin) is False:
                print('{0}:不符合买入条件，可能是超过买入数量上限或者比例上限'.format(coin))
                return False

        # 对价格和交易单位进行rounding，否则有可能造成调用接口失败
        rounding_price = publicparameters.rounding_price(coin)
        rounding_unit = publicparameters.rounding_unit(coin)
        # 买入时的价格
        buy_price = price_item.buy_price
        # 交易UNITS
        trans_units = round(self.__trans_amount_per_trans / buy_price, rounding_unit)
        # 第一次交易卖出时的UNIT和买入UNIT会有一个0.5%的误差
        newtrans_units = trans_units
        # 对交易价格进行ROUNDING处理
        trans_price_rounding = round(trans_price, rounding_price)
        # 有些价格较贵，金额太小出现UNIT为0的情况，不需要再提交订单
        if trans_units < 0.00001:
            return False
        # 当前交易的对象
        if trans_type == const.TRANS_TYPE_BUY:
            orderitem = OrderItem(market, coin)
        elif trans_type == const.TRANS_TYPE_SELL:
            # 卖出时查找已经存在的priceitem，并更新相应的状态
            orderitem = self.get_order_item(price_item)
            if orderitem is None:
                return False
            else:
                trans_units = round(orderitem.buy_units, rounding_unit)
            # 卖出订单时检查买入订单的状态，如果没有买入成功则停止卖出，返回失败
            if orderitem.buy_status != const.ORDER_STATUS_CLOSED:
                # print('买入订单还没有成交，卖出取消!')
                return False
        # 交易的order_market
        order_market = self.order_market

        # 提交订单
        if trans_type == const.TRANS_TYPE_SELL:
            bal = order_market.getMyBalance(coin)
            # 可能出现余额不足的情况
            if bal > trans_units:
                trans_order = order_market.submitOrder(coin_pair, trans_type, trans_price_rounding, trans_units)
            # 处理第一次买入出现扣除手续费后卖出时余额不足的情况
            elif trans_units*0.99<bal:
                newtrans_units = round(trans_units*0.99,rounding_unit)
                trans_order = order_market.submitOrder(coin_pair, trans_type, trans_price_rounding, newtrans_units)
            # 余额不足的取消卖出交易
            else:
                orderitem.sell_status = const.ORDER_STATUS_CANCEL
                ormmysql.updateorder(orderitem)
                ormmysql.delorder(orderitem)
                print('{0}:余额不足，已经取消订单'.format(coin))
                return False
        else:
            trans_order = order_market.submitOrder(coin_pair, trans_type, trans_price_rounding, trans_units)
        #     trans_units)
        # # 取得返回订单的信息
        order_id = trans_order.order_id
        if order_id== -1111111:
            order_status = 'fail'
            return False
        else:
            order_status = order_market.getOrderStatus(order_id, coin)
            # 如果状态为None，说明取状态有异常，对于卖出默认为OPEN，防止出现多现卖出状态设置为None，继续卖出的情况
            if order_status is None:
                order_status = const.ORDER_STATUS_OPEN
        # 保留交易时的相关信息到orderitem对象中
        if trans_type == const.TRANS_TYPE_BUY:
            orderitem.buy_order_id = order_id
            orderitem.buy_status = order_status
            orderitem.buy_price = trans_price_rounding
            orderitem.buy_amount = self.__trans_amount_per_trans
            orderitem.buy_units = trans_units
            orderitem.buy_date = common.get_curr_time_str()
            orderitem.priceitem = price_item
            # 买入时新增加一个订单，卖出时则直接更新已经存在的订单
            # 保存数据到DB
            ormmysql.saveorder(orderitem)
            print('{0}:开始[{1}]交易,交易状态:{2}:PriceDate:{3},Coin:{4}, BuyPrice:{5}'.format(common.get_curr_time_str(), trans_type, order_status,
                                                               price_item.pricedate, price_item.coin,
                                                               price_item.buy_price))
        elif trans_type == const.TRANS_TYPE_SELL:
            orderitem.sell_order_id = order_id
            orderitem.sell_status = order_status
            orderitem.sell_price = trans_price_rounding
            orderitem.sell_amount = round(trans_units * trans_price_rounding, 10)
            orderitem.sell_units = trans_units
            orderitem.sell_date = common.get_curr_time_str()
            print('{0}:开始[{1}]交易,交易状态:{2}:PriceDate:{3},Coin:{4}, BuyPrice:{5}, SellPrice:{6}, ProfiteRate:{7}'.format(common.get_curr_time_str(), trans_type, order_status,
                                                               price_item.pricedate, price_item.coin,
                                                               price_item.buy_price, orderitem.sell_price, publicparameters.SELL_PROFIT_RATE))
            # 更新到DB
            ormmysql.updateorder(orderitem)
            # remove to log table if sell status is closed
            if order_status == const.ORDER_STATUS_CLOSED:
                ormmysql.delorder(orderitem)

        return True
        pass

    # 处理未完成订单的止损操作
    def stop_lost(self, curr_pricitem):
        open_order_list = ormmysql.openorderlist()
        for open_order in open_order_list:
            # 如果当前价格和买入价格小于止损的比例，则执行先取消订单再按当前价格的直接卖出操作
            curr_price = curr_pricitem.sell_price
            if curr_price/open_order.buy_price<(1 - publicparameters.STOP_LOST_RATE) and curr_pricitem.coin == open_order.coin:
            # if curr_pricitem.coin == open_order.coin:
                status = self.order_market.cancelOrder(open_order.sell_order_id, coin_code=curr_pricitem.coin)
                # #  TEST usage
                # status = const.CANCEL_STATUS_SUCC
                if status == const.CANCEL_STATUS_FAIL:
                    pass
                elif status == const.CANCEL_STATUS_SUCC:
                    # 更新订单的状态为取消
                    # ormmysql.updateorder(open_order)
                    # 重新卖出，以当前价卖出进行止损
                    sell_status = self.coin_trans(self.market,const.TRANS_TYPE_SELL,curr_price,open_order.priceitem)
                    # #  test only
                    # sell_status = True
                    # 止损卖出成功
                    if sell_status is True:
                        print("-------:(--------订单进行了止损操作,coin:{0},操作时间:{1}".format(open_order.coin,common.get_curr_time_str()))
                        self.update_order_status()
                        return True
                        pass
                    # 止损卖出失败，继续进行循环操作进行下一次的自动卖出
                    else:
                        return False
                        pass
                    pass

    #     得到aex网站的历史交易记录并保存到数据库中
    def save_aex_trans(self, coin_pair):
        trans_list = self.order_market.getMyTradeList(coin_pair)
        # 保存历史记录到数据库表中
        for transitem in trans_list:
            aextransitem = AEXTransItem(transitem)
            ormmysql.saveaextrans(aextransitem)
        pass

if __name__ == '__main__':
    #
    # orderitem = OrderItem('btc38', 'doge')
    # orderitem.gen_table_class()
    #
    #
    trans = CoinTrans('btc38')
    # test save aex trans list
    trans.save_aex_trans('bcc_btc')
    # trans.update_order_status()
    # trans.test_coin_trans()
    #
    #
    # test stop lost function
    # pricebuffer = priceupdate.PriceBuffer('btc38', save_log_flag=False)
    # priceitem = pricebuffer.getpriceitem('btc38', 'bcc_btc')
    # trans.stop_lost(priceitem)

    # trans.cancle_ot_buy_order(10)
    #
    # trans.cancle_ot_buy_order(50)
    # trans.sell_check()
    # # time.sleep(2)
    # priceitem2 = pricebuffer.getpriceitem('btc38', 'doge_btc')
    #
    # # 循环检查OPEN订单是不是满足卖出条件
    #
    # orderstatus1 = trans.coin_trans( 'btc38', 'buy', 0.009, priceitem)
    # trans.sell_check()
    # trans.update_order_status()

    # orderstatus2 = trans2.coin_trans( 'btc38', 'buy', 0.009, priceitem2)
    # print(len(publicparameters.ORDER_LIST))
    # print(len(trans2.order_list))

    # orderstatus2 = trans.coin_trans( 'btc38', 'sell', 0.01345, priceitem)
    # print('order status 1: {0}, 2: {1}'.format(orderstatus1, orderstatus2))

    # print(len(publicparameters.ORDER_LIST))
    pass


import os,sys
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0,parentdir)

import xmind
from xmind.core.markerref import MarkerId
xmind_name="internet"
w = xmind.load(os.path.dirname(os.path.abspath(__file__))+"\\"+xmind_name+".xmind")
s2=w.createSheet()
s2.setTitle("物理介质")
r2=s2.getRootTopic()
r2.setTitle("物理介质")

content={
'物理介质':[
    '把电脑连接起来的物理手段，常见有双绞线，光纤以及无线电波',
    '决定了电信号(0和1)的传输方式',
    '决定了电信号的传输带宽、速率、传输距离以及抗干扰性等等'
],
'双绞线':[
    '两根信号线的缠绕抵消外源性噪声',
    '节距控制抑制内源性噪声'
],
'集线器':[
    '将信号发送给所有连接在它上面的线路',
    '原封不动将信号广播出去'
],
'交换机':[
    '全双工模式:同时进行发送和接收操作',
    '端口的MAC模块不具有MAC地址,不核对接收方MAC地址',
    {'交换电路':[
        '通过交换开关切换信息流向',
        '交换开关由电子电路构成，可快速切换'
    ]},
    {'过程':[
        '1.信号到达网线接口，由PHY（MAU）模块进行接收',
        '2.PHY（MAU）模块:信号->通用格式，传递给MAC模块',
        '3.MAC模块:信号->数字信息，通过包末尾的FCS校验错误，没问题,包->缓冲区',
        '4.MAC地址表中查询【包的接收方MAC地址】->【端囗】',
        '5.包->交换电路->缓冲区->MAC模块->PHY模块->端口->网线'
    ]},
    {'MAC地址表维护':[
        '收到包:发送方MAC地址，输入端口->写入MAC地址表',
        '过时自动删除地址表中记录'
    ]},
    {'特殊':[
        '交换机发现一个包要发回到原端口，直接丢弃包'
    ]}
],
'路由器':[
    '各个端口都具有MAC和IP地址,只接收与自身地址匹配的包，不匹配直接丢弃',
    {'路由表':[
        '忽略主机号，只匹配网络号',
        {'维护':[
            '手动维护',
            '路由器间的信息交换自行维护'
        ]}
    ]},
    {'转发目标':[
        '路由表',
        '包的IP头部中接收方IP地址',
        '网络号最长，跃点计数较小',
        '网关列为IP地址，该地址为下一个转发目标',
        '网关列为空，IP头部中的接收方IP地址是下一个转发目标',
        '通过ARP来查转发目标的MAC地址'
        '无匹配记录，丢弃包，并通过ICMP消息告知发送方',
        '可设置默认路由'
    ]},
    {'过程':[
        '1.信号->网线接口',
        '2.PHY（MAU）模块和MAC模块:信号->数字信息',
        '3.通过包末尾的FCS进行错误校验:没问题则检查MAC头部中的接收方MAC地址，匹配将包放到缓冲区，否则丢弃',
        '4.通过路由表找到下一路由，更新MAC,IP头部中的TTL,把信号转发出去',
        '5.委托端口模块将包发送出去'
    ]},
    {'分片':[
        '对一个完整的包再进行拆分的过程',
        '条件：长度>输出端口的MTU&&TCP标志字段可分片',
        '不满足：丢弃包，并通过ICMP消息通知发送方'
    ]},
    {'路由器&交换机':[
        'IP （路由器）负责将包发送给通信对象这一整体过程',
        '其中将包传输到下一个路由器的过程则是由以太网（交换机）来负责的'
    ]},
    {'附加功能':[
        '地址转换',
        '包过滤'
    ]}
]

}

#构建xmind
xmind.build(content,r2)
#保存xmind
xmind.save(w,os.path.dirname(os.path.abspath(__file__))+"\\"+xmind_name+".xmind")


def Combi(a, b):
    if f[a][b] == -1:
        if a == b or b == 0:
            f[a][b] = 1
        else:
            f[a][b] = Combi(a-1, b) + Combi(a-1, b-1)
    return f[a][b]

n, k = map(int, input().split())
f = [[-1 for _ in range(k+1)] for _ in range(n+1)]
print(Combi(n, k))


from xmlrpc.client import Boolean

large_str = 'labaratory'
substring = 'labaratoryyyy'

class Solution:
    def substring(self, large_str: str, substring: str) -> Boolean:
        if len(substring) < 1:
            return False
        elif len(substring) > len(large_str):
            return False
        else:
            pointer1 = 0
            pointer2 = len(substring)
            stopping = len(large_str)+1

            while pointer2 != stopping:
                if large_str[pointer1:pointer2] == substring:
                    return True
                else:
                    pointer1 +=1
                    pointer2 +=1
            return False

def main():
    p = Solution()
    print(p.substring(large_str, substring))

if __name__ == '__main__':
    main()

# TC: O(n*m) iterate for m for every letter
# SC: O(1)

# CodePath solution:

# Loop through the first string for an initial match. If we find one, check that
#     match for a full substring match.

# 1) Edge Check
# 2) Loop through the first string
# 3) Find a match to the first character of the second string
# 4) While there is a match, loop through both strings in parallel to ensure
#     the match
# 5) If there is an entire match to the second string, return True (there is a
#     substring)
# 6) If the match breaks, continue searching the first string for an initial
#     character match
# 7) If we make it to the end of the first string without a full match, there
#     is no substring match -- return False

# def substring(large_str, potential_substr):
#     if len(potential_substr) < 1:
#         return True
#     for i in range(len(large_str)):
#         large_str_idx, j = i, 0
#         while j < len(potential_substr) and large_str_idx < len(large_str) and large_str[large_str_idx] == potential_substr[j]:
#             j += 1
#             large_str_idx += 1
#         if j + 1 == len(potential_substr):
#             return True
#     return False


import argparse
import numpy as np
from mpi4py import MPI
from datetime import datetime

def sort_by_pivot(a, pivot):
    l = 0
    r = len(a) - 1
    while l < r:
        if a[l] < pivot:
            l += 1
            continue
        if a[r] >= pivot:
            r -= 1
            continue
        a[l], a[r] = a[r], a[l]  # this is actually faster than any other method
        l += 1
    return l

def pick_pivot(array):
    return array[len(array) // 2]

def log(str):
    print(str)

parser = argparse.ArgumentParser(description='Run lab3 list implementation')
parser.add_argument('--array', dest='array', help='filename with the array', )
parser.add_argument('--debug', dest='debug', action='store_true')

args = parser.parse_args()

a_path = args.array if args.array else "data\\80000"
debug = args.debug

log = log if debug else lambda _: None
comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()

if not np.log2(size).is_integer():
    raise AssertionError("Can't run with thread number that is not power of 2")

array = np.loadtxt(a_path) if not rank else None

ranges = np.array_split(array, size) if not rank else []

local_array = comm.scatter(ranges, root=0)

pivot = pick_pivot(array) if not rank else 0
pivot = comm.scatter([pivot] * size, root=0)

log(f"rank {rank} pivot is {pivot}")

local_rank = rank
local_size = size
local_comm = comm

start = datetime.now()

while True:

    log(f"{rank}({local_rank}):size {local_size}| arr {len(local_array)}")

    divider = sort_by_pivot(local_array, pivot)

    if local_rank < local_size / 2:
        # first send bigger to last
        log(f"{rank}:size {local_size}| sending {local_array[divider:]}")
        local_comm.send(local_array[divider:], dest=local_size - local_rank - 1)

        # first receive lesser from last
        buffer = local_comm.recv(source=local_size - local_rank - 1)
        log(f"{rank}:size {local_size}| received {buffer}")

        local_array = np.concatenate([local_array[:divider], buffer])
        log(f"{rank}:size {local_size}| new arr {local_array}")

    else:
        # Here we need buffer to store received part of the array since we still need to send our part later
        buffer = local_comm.recv(source=local_size - local_rank - 1)
        log(f"{rank}:size {local_size}| received {buffer}")

        log(f"{rank}:size {local_size}| sending {local_array[:divider]}")
        local_comm.send(local_array[:divider], dest=local_size - local_rank - 1)

        local_array = np.concatenate([buffer, local_array[divider:]])
        log(f"{rank}:size {local_size}| new arr {local_array}")

    # we now have 2 groups of processes:
    # with everything < pivot and everything > pivot.
    # Now we have to split processes into 2 worlds each with it's own pivot
    if local_size <= 2:
        break

    color = 0 if local_rank < local_size / 2 else 1

    local_comm = local_comm.Split(color, local_rank)
    local_size = local_comm.Get_size()
    local_rank = local_comm.Get_rank()
    log(f"new world size {local_size}, {rank} new rank {local_rank}")

    pivot = pick_pivot(local_array) if not local_rank else 0  # it's fine to calculate pivot in only one process

    pivot = local_comm.scatter([pivot] * local_size, root=0)

    log(f"new pivot {pivot}, {rank} new rank {local_rank}")

# Final sequential sort (it's qsort)
local_array = np.sort(local_array)
log(f"{comm.Get_rank()}:size {local_size}| sorted {local_array}")

local_size = len(local_array)

sizes = comm.gather(local_size, root=0)

log(f"{rank} sizes: {sizes}")

comm.Gatherv(local_array, (array, sizes), root=0)

duration = datetime.now() - start

if not rank:
    log(array)
    print(duration.total_seconds() * 1000)

log("yaay")


import math

#Operadores aritméticos

a = 10.5
b = 5.5

suma = a + b #Devuelve int
resta = a - b #Devuelve int
multiplicacion = a * b #Devuelve int
division = a/b #Devuelve float
modulo = a% b
potencia = a**b

print(suma)
print(resta)
print(multiplicacion)
print(division)
print(modulo)
print(potencia)

numero = 16

raiz_cuadrada = numero ** 0.5

print(f"La raiz cuadrada de {numero} es: {raiz_cuadrada}")

#Operaciones aritméticas con strings

str1 = "Hola, "
str2 = "mundo!"

resultado = str1 + str2

print(resultado)

str1 = "Hola"

resultado = str1 * 3

print(resultado)


import numpy as np
from ENV.env_two_rooms import EnvGoObstacle
from algorithm.EITI_Q import EITI_Q_learning
import math

if __name__ == '__main__':
    max_episode = 500
    max_iteration = 10
    max_steps = 50
    map_size = 7
    rewards_agent = np.zeros([2, max_iteration, max_episode])
    for num_agent in [5, 10]:
        env = EnvGoObstacle(map_size, num_agent)
        Q_learning = [EITI_Q_learning(map_size * map_size, 5) for _ in range(num_agent)]
        visited_num_sa = np.zeros([num_agent, map_size * map_size, 5, num_agent, map_size * map_size, 5, map_size * map_size])
        visited_num = np.zeros([map_size, map_size])
        for iter in range(max_iteration):
            for index in range(num_agent):
                Q = np.load("Q_value" + str(index % 2 + 1) + ".npy")
                Q_learning[index].set_Q_value(Q)
            all_steps = 0
            for i in range(max_episode):
                state = env.reset()
                count = 0
                done = False
                rewards = np.zeros(num_agent)
                while not done:
                    action_list = []
                    for index in range(num_agent):
                        s = state[index][0] * map_size + state[index][1]
                        visited_num[state[index][0]][state[index][1]] = visited_num[state[index][0]][state[index][1]] + 1
                        epsilon = np.min([0.9, 0.7+(0.9-0.7)*(i*max_steps+count)/(max_episode*max_steps/3)])
                        action = Q_learning[index].choose_action(s, epsilon)
                        action_list.append(action)
                    reward, _, next_state = env.step(action_list)
                    for index in range(num_agent):
                        s = state[index][0] * map_size + state[index][1]
                        a = action_list[index]
                        for other_index in range(num_agent):
                            if other_index != index:
                                s_other = state[other_index][0] * map_size + state[other_index][1]
                                a_other = action_list[other_index]
                                s_next_other = next_state[other_index][0] * map_size + next_state[other_index][1]
                                visited_num_sa[index, s, a, other_index, s_other, a_other, s_next_other] += 1
                    if count > max_steps:
                        done = True
                    else:
                        count += 1
                    for index in range(num_agent):
                        s = state[index][0] * map_size + state[index][1]
                        a = action_list[index]
                        r = reward[index]
                        s_ = next_state[index][0]*map_size+next_state[index][1]
                        rewards[index] += r
                        r_in = 0
                        for other_index in range(num_agent):
                            if other_index != index:
                                s_other = state[other_index][0] * map_size + state[other_index][1]
                                a_other = action_list[other_index]
                                s_next_other = next_state[other_index][0] * map_size + next_state[other_index][1]
                                if np.sum(visited_num_sa[index, s, a, other_index, s_other, a_other, :]) == 0 or np.sum(visited_num_sa[index, :, :, other_index, s_other, a_other, :]) == 0:
                                    r_temp = 0
                                else:
                                    r_temp = math.log( (visited_num_sa[index, s, a, other_index, s_other, a_other, s_next_other] / np.sum(visited_num_sa[index, s, a, other_index, s_other, a_other, :]) ) /
                                                       (np.sum(visited_num_sa[index, :, :, other_index, s_other, a_other, s_next_other]) / np.sum(visited_num_sa[index, :, :, other_index, s_other, a_other, :])) )
                                r_in += r_temp
                        Q_learning[index].learn(s, a, r, s_, r_in)
                    state = next_state
                rewards_agent[int(num_agent/5-1)][iter][i] = np.mean(rewards)
                print(i, count)
                all_steps += count
            print(iter)
    np.save("eiti_rewards.npy", rewards_agent)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue May  5 11:50:19 2020

@author: nsai
"""
import xlrd
import re
import nltk
from nltk.corpus import stopwords
from sklearn.feature_extraction.text import TfidfVectorizer
from unicodedata import normalize
from sklearn.neighbors import NearestNeighbors
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
from sklearn.cluster import MiniBatchKMeans
import numpy as np
import pandas as pd
## The general approach is to creat TF-IDF vectors and then using K-means for clustering

## Opening xlsx file

wb = xlrd.open_workbook("/Users/nsai/Downloads/OneDrive_1_30-04-2020/Company Descriptions.xlsx")

longCompanyDescp = []
companyName = []

## Creating a List of company names and company description from the file

for s in wb.sheets():
    for row in range(1,s.nrows,1):
            companyName.append(s.cell(row,0).value)
            if s.cell(row,2).value == "":
                longCompanyDescp.append(s.cell(row,1).value)
            else :
                longCompanyDescp.append(s.cell(row,2).value)

## Creating a dictionary of Company names and their description as k,v pairs

company_dict = dict(zip(companyName,longCompanyDescp))

##Cleaning description by removing stop words and numbers

def clean_descp(text):
    letters_only = re.sub('[^a-zA-Z]', ' ', text)
    words = letters_only.lower().split()
    new_words = []
    for w in words:
        w_norm = normalize('NFKD', w).encode('ASCII','ignore').decode('ASCII')
        new_words.append(w_norm)
    stopwords_eng = set(stopwords.words("english"))
    useful_words = [x for x in new_words if not x in stopwords_eng]

    # Combine words into a paragraph again

    useful_words_string = ' '.join(useful_words)
    return(useful_words_string)

clean_companyDescp = map(clean_descp,longCompanyDescp)
clean_company_dict = dict(zip(companyName,clean_companyDescp))

## Creating TF-IDF Vectors from the descriptions

def tokenize(text):
    tokens = nltk.word_tokenize(text)
    #stems = stem_words(tokens, stemmer)
    return tokens

tfidf = TfidfVectorizer(tokenizer=tokenize, stop_words='english')
tfs = tfidf.fit_transform(clean_company_dict.values())

#Finding Nearest Neighbours

model_tf_idf = NearestNeighbors(metric='cosine', algorithm='brute')
model_tf_idf.fit(tfs)

#Inputs a query tf_idf vector, the dictionary of companies, the knn model, and the number of neighbors
#Prints the k nearest neighbors

def print_nearest_neighbors(query_tf_idf, full_company_dictionary, knn_model, k):
    distances, indices = knn_model.kneighbors(query_tf_idf, n_neighbors = k+1)
    nearest_neighbors = [list(full_company_dictionary.keys())[x] for x in indices.flatten()]

    for company in range(len(nearest_neighbors)):
        if company == 0:
            print ('Query Company: {0}\n'.format(nearest_neighbors[company]))
        else:
            print ('{0}: {1}\n'.format(company, nearest_neighbors[company]))

print_nearest_neighbors(tfs[8], clean_company_dict, model_tf_idf, k=5)

## Calculating the right number of clusters by plotting
## the Sum of Squared Errors for different clusters and finding the elbow

def find_optimal_clusters(data, max_k):
    iters = range(2, max_k+1, 4)
    sse = []
    for k in iters:
        sse.append(MiniBatchKMeans(n_clusters=k).fit(data).inertia_)
        print('Fit {} clusters'.format(k))

    f, ax = plt.subplots(1, 1)
    ax.plot(iters, sse, marker='o')
    ax.set_xlabel('Cluster Centers')
    ax.set_xticks(iters)
    ax.set_xticklabels(iters)
    ax.set_ylabel('SSE')
    ax.set_title('SSE by Cluster Center Plot')

find_optimal_clusters(tfs,40)

##Running k-means and plotting with k obtained from elbow method

k = 28
km = KMeans(n_clusters=k, init='k-means++', max_iter=100, n_init=5,
                verbose=1)
km.fit(tfs)

## Assigning cluster values to companies as a dictionary

cluster_assignment_dict = {}

for i in set(km.labels_):
    current_cluster_companies = [list(clean_company_dict.keys())[x] for x in np.where(km.labels_ == i)[0]]
    cluster_assignment_dict[i] = current_cluster_companies

## Assigning  Labels to clusters using description of companies in that cluster
cluster_themes_dict = {}

for key in cluster_assignment_dict.keys():
    current_tfidf = TfidfVectorizer(tokenizer=tokenize, stop_words='english')
    current_tfs = current_tfidf.fit_transform(map(clean_descp,cluster_assignment_dict[key]))
    current_tf_idfs = dict(zip(current_tfidf.get_feature_names(), current_tfidf.idf_))
    tf_idfs_tuples = current_tf_idfs.items()
    cluster_themes_dict[key] = sorted(tf_idfs_tuples, key = lambda x: x[1])[:1]

##Output format

compList = []
labelList = []
for v in cluster_assignment_dict.values():
    compList.append(v)

for values in cluster_themes_dict.values():
    labelList.append(values[0][0])

def createDict(label,comp):
    labelCol = []
    for i in range(len(comp)):
        labelCol.append(label)
    df  = pd.DataFrame(list(zip(labelCol,comp)),columns = ["Label","Company"])
    df.to_csv("/Users/nsai/Documents/Research/task3_2.csv",mode = "a")

for i in range(0,27,1):
    createDict(labelList[i],compList[i])


from iUTAHData.settings.base import *

DATABASE_PATH = os.path.join(BASE_DIR, os.pardir, os.pardir, 'db.sqlite3')
DATABASES['default']['NAME'] = DATABASE_PATH;
#BASE_DIR, os.pardir,

DEBUG = True
TEMPLATE_DEBUG = True

SITE_ROOT = os.environ['APPL_PHYSICAL_PATH']
SITE_URL = os.environ['APPL_VIRTUAL_PATH'] + "/"

STATIC_ROOT = os.path.join(SITE_ROOT, 'static')
STATIC_URL = SITE_URL + 'static/'


# -*- coding: utf-8 -*-
###################################################################
# Author: Mu yanru
# Date  : 2013.12
# Email : muyanru345@163.com
###################################################################

import sublime, sublime_plugin
import pickle, os

class AsciiCommand(sublime_plugin.TextCommand):
    def run(self, args, f='#', e=' '):
        view = self.view
        selRegion = view.sel()[0]
        wordRegion = selRegion
        if selRegion.empty():
            wordRegion = view.word(selRegion)

        words = view.substr(wordRegion)
        edit = view.begin_edit()
        view.replace(edit, wordRegion, str2Ascii(words,f,e))
        view.end_edit(edit)
        # sublime.message_dialog("generate is success!")

class AsciisharpCommand(sublime_plugin.TextCommand):
    def run(self, args):
        view = self.view
        selRegion = view.sel()[0]
        wordRegion = selRegion
        if selRegion.empty():
            wordRegion = view.word(selRegion)

        words = view.substr(wordRegion)
        edit = view.begin_edit()
        view.replace(edit, wordRegion, str2Ascii(words, '#'))
        view.end_edit(edit)

class AsciislashCommand(sublime_plugin.TextCommand):
    def run(self, args):
        view = self.view
        selRegion = view.sel()[0]
        wordRegion = selRegion
        if selRegion.empty():
            wordRegion = view.word(selRegion)

        words = view.substr(wordRegion)
        edit = view.begin_edit()
        view.replace(edit, wordRegion, str2Ascii(words, '/'))
        view.end_edit(edit)

def str2Ascii(inputWord, fillChar = '#', emptyChar = ' '):
    if len(fillChar) != 1: fillChar = '#'
    if len(emptyChar) != 1: emptyChar = ' '
    pickleFile = open(os.path.expanduser(sublime.packages_path() + '\\Ascii\\asciiDict.mu'), 'rb')
    asciiDict = pickle.load(pickleFile)
    pickleFile.close()
    outputWord = ''
    outputList = []
    for char in inputWord:
        if asciiDict.has_key(char.lower()):
            outputList.append(asciiDict.get(char.lower()))
    contentList = []
    for i in range(len(outputList[0])):
        tmpList = ''
        for j in range(len(outputList)):
            tmpList += outputList[j][i]
        contentList.append(tmpList)
    for i in contentList:
        for j in i:
            if j == '0':
                outputWord += emptyChar
            elif j == '1':
                outputWord += fillChar
        outputWord += '\n'
    return outputWord


#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# File   : format-header.py
# Author : Jiayuan Mao
# Email  : maojiayuan@gmail.com
# Date   : 05/09/2018
#
# This file is part of Jacinle.
# Distributed under terms of the MIT license.

import os
import os.path as osp
import glob
import time

from jacinle.cli.argument import JacArgumentParser
from jacinle.logging import get_logger

logger = get_logger(__file__)
parser = JacArgumentParser()
parser.add_argument('files', nargs='*')
parser.add_argument('--dir', default=os.getcwd())
parser.add_argument('--project', default=osp.basename(os.getcwd()))
parser.add_argument('-n', '--dry', action='store_true')
parser.add_argument('-f', '--force', action='store_true')
args = parser.parse_args()

HEADER = r"""#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# File   : {file}
# Author : {author}
# Email  : {email}
# Date   : {date}
#
# This file is part of {project}.
# Distributed under terms of the MIT license.
"""

def log(*args, **kwargs):
    logger.info(*args, **kwargs)

def process(filename):
    with open(filename) as f:
        lines = f.readlines()

    if len(lines) == 0:
        return

    filetype = 'unk'
    fields = dict()
    fields['project'] = args.project

    i = 0
    for i, line in enumerate(lines):
        if i == 0:
            if line.startswith('#! /usr/bin/env'):
                filetype = 'vim'
            elif line.startswith('# -*- coding'):
                filetype = 'charm'
            else:
                filetype = 'unk'

        if i == 1 and filetype != 'unk':
            if not line.startswith('# '):
                if not args.force:
                    filetype = 'unk'
                else:
                    filetype = 'force'

        if filetype == 'unk':
            break

        if not line.startswith('#'):
            break

        line_trim = line[2:]
        if line_trim.startswith('File'):
            fields['file'] = line_trim[line_trim.find(':')+1:].strip()
            if fields['file'] != osp.basename(filename):
                log('  mismatched filename: full={}, actual={}, doc={}'.format(filename, osp.basename(filename), fields['file']))
                fields['file'] = osp.basename(filename)
        elif line_trim.startswith('Author'):
            fields['author'] = line_trim[line_trim.find(':')+1:].strip()
            if fields['author'] != 'Jiayuan Mao':
                log('  author assertion: full={}, author={}'.format(filename, fields['author']))
        elif line_trim.startswith('Email'):
            fields['email'] = line_trim[line_trim.find(':')+1:].strip()
            if fields['email'] != 'maojiayuan@gmail.com':
                log('  email assertion: full={}, email={}'.format(filename, fields['email']))
        elif line_trim.startswith('Date'):
            fields['date'] = line_trim[line_trim.find(':')+1:].strip()
            date = fields['date'].split('/')
            assert len(date) == 3
            if filetype == 'charm':
                date = (date[1], date[0], date[2])
            if int(date[0]) > 12:
                date = (date[1], date[0], date[2])
            date = list(date)
            date[0] = '{:02d}'.format(int(date[0]))
            date[1] = '{:02d}'.format(int(date[1]))
            if date[2] == '17':
                date[2] = '2017'
            if date[2] == '18':
                date[2] = '2018'
            fields['date'] = '/'.join(date)

        if i == 8 and filetype == 'vim':
            if not line_trim.startswith('Distributed'):
                log('  vim-typed file error: {}'.format(filename))
        if i == 6 and filetype == 'charm':
            if not line_trim.startswith('This file'):
                log('  charm-typed file error: {}'.format(filename))

    if lines[i].startswith('#'):
        i += 1

    if filetype == 'unk':
        logger.warning('Unkown filetype.')
        return

    extras = lines[i:]

    if len(fields) != 5:
        logger.warning('Incomplete header.')

        fields.setdefault('file', osp.basename(filename))
        fields.setdefault('author', 'Jiayuan Mao')
        fields.setdefault('email', 'maojiayuan@gmail.com')
        fields.setdefault('date', time.strftime('%m/%d/%Y', time.localtime(osp.getctime(filename))))

    if not args.dry:
        with open(filename, 'w') as f:
            f.write(HEADER.format(**fields))
            if len(extras):
                f.writelines(extras)

def main():
    logger.critical('Working directory: {}'.format(args.dir))
    logger.critical('Project name: {}'.format(args.project))

    if args.files:
        files = list()
        for f in args.files:
            if osp.isdir(f):
                files.extend(glob.glob('{}/**/*.py'.format(f), recursive=True))
            else:
                assert osp.isfile(f), f
                files.append(f)
    else:
        files = glob.glob('{}/**/*.py'.format(args.dir), recursive=True)

    for f in files:
        logger.info('Process: "{}"'.format(f))
        process(f)

if __name__ == '__main__':
    main()


import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)

GPIO.setwarnings(False)

GPIO_PIN = 21

GPIO.setup(GPIO_PIN, GPIO.OUT)

pwm = GPIO.PWM (GPIO_PIN, 500)

pwm.start(50)

try:

	while True:

		Freq = input("Please input a new frequency (50-5000):")
		Frequency = int(Freq)
		pwm.ChangeFrequency(Frequency)

except:
	GPIO.cleanup()


import sys
import zlib
import random
import base64
from io import BytesIO

from PIL import Image

__version__ = '0.1.0'

class TermImage(object):
    def __init__(self, data: str):
        """Creates a new TermImage object using base64 encoded image data"""

        # The raw base64 encoded data
        self._raw = data

        # PIL image, allowing us to get information about the
        # raw data, necessary for displaying the image in
        # the terminal
        self._pil_img = Image.open(BytesIO(base64.b64decode(data)))

        # Give this image a random id so that it can be used in
        # multiple places without regenerating the image
        self.id = random.randint(0, 4294967295)

        # Set the format for this image. This can't be accessed after
        # resize apparently, so it needs to be done here.
        self.format = self._pil_img.format

    @classmethod
    def open(cls, file_path: str):
        """Open a local image as a TermImage instance"""
        data = open(file_path, "rb").read()
        encoded = base64.b64encode(data)
        return cls(encoded)

    @property
    def width(self):
        """Returns the width of this image"""
        return self._pil_img.width

    @property
    def height(self):
        """Returns the height of this image"""
        return self._pil_img.height

    def resize(self, width: int, height: int, resample: int = 1):
        """Wrapper around Image.resize, allowing this image to be resized.
        This is a destructive operation."""
        self._pil_img = self._pil_img.resize((width, height), resample)
        return self

    def base64(self):
        """Return this image as base64. This is needed because on resize
        the raw data will change."""
        buffered = BytesIO()
        self._pil_img.save(buffered, format=self.format)
        return base64.b64encode(buffered.getvalue())

    def render(self):
        cmd = {
            'a': 'T',
            # 'i': self.id,
            's': self.width,
            'v': self.height
        }

        if self.format == "PNG":
            cmd['f'] = 100
        else:
            cmd['f'] = 24

        self._write_chunked(cmd, self.base64())

    # def delete(self):
    #     cmd = {'a': 'd', 'i': self.id}
    #     self._write_gr_cmd(cmd)

    def _write_gr_cmd(self, cmd, payload=None):
        sys.stdout.buffer.write(self._serialize_gr_command(cmd, payload))
        sys.stdout.flush()

    def _serialize_gr_command(self, cmd, payload=None):
        cmd = ','.join('{}={}'.format(k, v) for k, v in cmd.items())
        ans = []
        w = ans.append
        w(b'\033_G'), w(cmd.encode('ascii'))
        if payload:
            w(b';')
            w(payload)
        w(b'\033\\')
        return b''.join(ans)

    def _write_chunked(self, cmd, data):
        if cmd['f'] != 100:
            data = zlib.compress(data)
            cmd['o'] = 'z'
        while data:
            chunk, data = data[:4096], data[4096:]
            m = 1 if data else 0
            cmd['m'] = m
            self._write_gr_cmd(cmd, chunk)
            cmd.clear()

img = TermImage.open("avatar.png")
img.resize(80, 80)
img.render()


from Common import *
Data.Global.ZeroY=4
while(1):
    Name = input('Capture: ')
    MoveWindow2zero()
    sleep(2)
    I = array(ImageGrab.grab())
    II=zeros((30,60,3))
    for i in range(30,43):
        for j in range(40,100):
            if((I[i][j]==[246 ,242, 150]).all()==True):
                print(I[i][j])
                II[i-25][j-40]=[246,242,150]
    im = Image.fromarray(uint8(II))
    im.save(Name+".png")
    # sleep(3)
    # im=Image.open(Name+".png")
    # II = array(im)
    # for i in range(30,43):
    #     for j in range(40,100):
    #             print(II[i-25][j-40])


from __future__ import annotations
from typing import Tuple, List
from random import randint
from card import Card
from abc import ABC, abstractmethod

class DefaultDeck(ABC):
    @abstractmethod
    def __init__(self) -> None:
        self.deck = []
        return None

    def get_cards(self, number_of_cards: int) -> List[Card]:
        if len(self.deck) < number_of_cards:
            raise IndexError("not enough cards in deck")
        cards = []
        while number_of_cards != 0:
            card = self.deck.pop()
            cards.append(card)
            number_of_cards -= 1
        return cards

    def add_card(self, card: Card) -> None:
        if not isinstance(card, Card):
            raise TypeError("card not of correct type")
        self.deck.append(card)
        return None

    def add_deck(self, new_deck: DefaultDeck) -> None:
        if not isinstance(new_deck, DefaultDeck):
            raise TypeError("deck not of correct type")
        for card in new_deck.deck:
            self.add_card(card)
        return None

    def shuffle(self) -> None:
        if len(self.deck) <= 1:
            return None
        for _ in range(len(self.deck) * 10):
            idx1, idx2 = self.__get_two_rand_indexes()
            self.deck[idx1], self.deck[idx2] = self.deck[idx2], self.deck[idx1]
        return None

    def show_cards(self, simple: bool=True) -> List[str]:
        output = []
        for card in self.deck:
            string = self.__card_to_string(card, simple)
            output.append(string)
        return output

    def __get_two_rand_indexes(self) -> Tuple[int, int]:
        rand_idx_1 = randint(0, len(self.deck) - 1)
        rand_idx_2 = randint(0, len(self.deck) - 1)
        return (rand_idx_1, rand_idx_2)

    def __card_to_string(self, card: Card, simple_view: bool) -> str:
        if not isinstance(card, Card):
            raise TypeError("card not of correct type")
        temp_string = card.value
        if (card.suit is not None) and (not simple_view):
            temp_string += f' - {card.suit}'
        if (card.description is not None) and (not simple_view):
            temp_string += f' - {card.description}'
        return temp_string

class Standard52CardDeck(DefaultDeck):
    def __init__(self) -> None:
        self.deck = []
        for suit in ["Spades", "Clubs", "Hearts", "Diamonds"]:
            for value in ["Ace", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"]:
                card = Card(f'{value[0] if len(value) > 2 else value} {suit}', suit, f'{value} of {suit}')
                self.deck.append(card)
        return None

class Standard52CardDeckJokers(DefaultDeck):
    def __init__(self) -> None:
        self.deck = [Card("Joker", "Red", "Red Joker"), Card("Joker", "Black", "Black Joker")]
        for suit in ["Spades", "Clubs", "Hearts", "Diamonds"]:
            for value in ["Ace", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"]:
                card = Card(f'{value[0] if len(value) > 2 else value} {suit}', suit, f'{value} of {suit}')
                self.deck.append(card)
        return None

class CatanResourceDeck(DefaultDeck):
    def __init__(self) -> None:
        self.deck = []
        for _ in range(19):
            self.deck.append(Card("Ore", "Resource", ""))
            self.deck.append(Card("Grain", "Resource", ""))
            self.deck.append(Card("Lumber", "Resource", ""))
            self.deck.append(Card("Wool", "Resource", ""))
            self.deck.append(Card("Brick", "Resource", ""))
        return None


#coding: 'utf-8'

"""
LDP_Net
demo.py

created by Kazunari on 2018/08/29
"""

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

import chainer

import sys
import os.path as osp
sys.path.append(osp.curdir)
from model.ldp_net import LDP_Net
from dataset.Local_Depth_Dataset import LocalDepthDataset
from dataset.LDD_Transform import LDDTransform

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--gpu', '-g', type=int, default=-1)
    parser.add_argument('--pretrained_model', '-m')
    parser.add_argument('--dataset_path', '-p', default="/Users/Kazunari/projects/datasets/LocalDepthDataset")
    parser.add_argument('--out', '-o', default=None)

    args = parser.parse_args()

    ldd = LocalDepthDataset(args.dataset_path)
    dataset = chainer.datasets.TransformDataset(ldd, LDDTransform(ldd))

    model = LDP_Net(rgbd_channel=3, pretrained_model=args.pretrained_model)

    if args.gpu >= 0:
        chainer.cuda.get_device_from_id(args.gpu).use()
        model.to_gpu()

    errors = []

    for i in range(1, 20000, 50):
        x_1, x_2, t, mask = dataset.get_example(i)

        x_1 = np.expand_dims(x_1, axis=0)[:, :3, :, :]
        x_2 = np.expand_dims(x_2, axis=0)

        pred = model(x_1, x_2)

        np_pred = np.asarray(pred.data[0], dtype=np.float32)

        elems_error = np.abs(np.diff([np_pred, t], axis=0))
        valid_pixel = np.count_nonzero(mask)
        masked_error = np.where(mask, elems_error, np.zeros_like(elems_error, dtype=np.float32))
        ldp_error = np.sum(masked_error) / valid_pixel

        elems_error = np.abs(np.diff([x_1[:, 3, :, :] / 10, t], axis=0))
        masked_error = np.where(mask, elems_error, np.zeros_like(elems_error, dtype=np.float32))
        eigen_error = np.sum(masked_error) / valid_pixel

        print("ldp_n error: " + str(ldp_error))
        print("eigen error: " + str(eigen_error))

if __name__ == '__main__':
    main()


# coding=utf-8
# coding=utf-8
import requests
from bs4 import BeautifulSoup
import re
import pandas as pd
from lxml import etree

#生成页面url
def get_url1(pages):
    urls={}       #存贮链接的字典，key为页数，value为链接
    for page in range(1,int(pages)+1):
        page_url="https://sh.lianjia.com/chengjiao/pg{}/".format(page)
        #print(page_url)
        #获取详情页url,利用正则表达式匹配
        response1=requests.get(page_url)
        if response1.status_code==200:
            pattern=re.compile('

<a href="(.*?)"')   #编译正则表达式，()是为了提取匹配的字符串
            response1.encoding = 'gbk'
            url=re.findall(pattern,response1.text)      #匹配所有满足正则表达式的信息
            #print(len(url))   #每页有30条房屋信息
            urls[page]=url
    #print(urls)
    return urls

#获取详情页所需的字段，beautifulsoup
def get_field(urls,pages):
    info = {"ID":[],"区域":[],"房型": [],"建筑年代":[], "面积": [],"房屋朝向":[],"装修情况":[],"总价（万）": [],"单价(元/平米)":[],
            "成交周期（天）":[],"成交日期":[],"带看次数":[]}
    for page in range(1,int(pages)+1):
        for i in urls[page]:
            response2=requests.get(i)
            if response2.status_code == 200:
                # 获取所需字段信息,beautifulsoup可以获取的
                soup=BeautifulSoup(response2.text ,'lxml')
                info["总价（万）"].append(soup.select('div.info.fr > div.price > span > i')[0].text )  #通过采用soup.select()方法，可以得到所需的内容;.表示类;运用方法 .text得到文本
                info["成交周期（天）"].append(soup.select('div.msg > span:nth-of-type(2) > label')[0].text)
                info["带看次数"].append(soup.select('div.info.fr > div.msg > span:nth-of-type(4) > label')[0].text.strip())
                info["单价(元/平米)"].append(soup.select(' div.info.fr > div.price > b')[0].text)
                info["成交日期"].append(soup.select(' div.house-title > div > span')[0].text.replace('成交',''))
                #获取所需字段信息,xpath提取
                d=etree.HTML(response2.text)
                info["房型"].append(d.xpath('// *[ @ id = "introduction"] / div[1] / div[1] / div[2] / ul / li[1] / text()')[0].strip())
                info["面积"].append(d.xpath('//*[@id="introduction"]/div[1]/div[1]/div[2]/ul/li[3]/text()')[0].strip())
                info["建筑年代"].append(d.xpath('//*[@id="introduction"]/div[1]/div[1]/div[2]/ul/li[8]/text()')[0])
                info["房屋朝向"].append(d.xpath('//*[@id="introduction"]/div[1]/div[1]/div[2]/ul/li[7]/text()')[0].strip())
                info["装修情况"].append(d.xpath('// *[ @ id = "introduction"] / div[1] / div[1] / div[2] / ul / li[9] / text()')[0].strip())
                info["ID"].append(d.xpath('//*[@id="introduction"]/div[1]/div[2]/div[2]/ul/li[1]/text()')[0].strip())
                info["区域"].append(d.xpath('/html/body/section[2]/div[2]/div/div/div[1]/a[1]/text()')[0])

    #将房屋信息读取到dataframe
    df=pd.DataFrame(info)
    pd.set_option('display.max_columns', None)          #显示所有列
    print(df)
    return df
#将数据导出保存为csv
def output_csv(df):
    df.to_csv('lianjia_ershoufang.csv', sep=',', index=False, header=False)

if __name__ == "__main__":
    pages=input("please input pages:")
    urls=get_url1(pages)
    df=get_field(urls, pages)
    output_csv(df)


#!/usr/bin/env python
import time
import sys, os
import subprocess

def outputs(command):
    ps = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
    stdo = ps.stdout.readlines()
    stdos = [line.decode('utf-8').strip("\n") for line in stdo]
    return stdos

if len(sys.argv) != 2:
    print("||%s||FM-PLUGIN-EXECUTE-FAILED||error,use processname" % sys.argv[0])
    sys.exit(0)
else:
    proname = sys.argv[1]
    #ip = outputs("ip add list|grep global|sed -n 1p|awk -F' ' '{print $2}'|awk -F'/' '{print $1}'")
    dctime = int(round(time.time() * 1000))

    dictsp = {}
    dictp = {}
    #com_pro = "ps aux|awk \'$11~\"" + proname + "\"\'|grep -v " + sys.argv[0] + "|wc -l"
    com_pro = "ps aux|awk \'{for(i=11;i<=NF;i++)printf $i \" \";printf\"\\n\"}\'|grep -v "+sys.argv[0]+"|grep "+proname+"|grep -v grep|wc -l"

    res = outputs(com_pro)

    if len(res) == 0:
        print("||%s||FM-PLUGIN-EXECUTE-FAILED||Result is empty. this command get nothing" % sys.argv[0])
    else:
        result = '||' + proname + '||' + 'APM-00-01-01-06-02||' + res[0]
        print(result)


"""
Name: Carson Shields
lab3.py

Problem: traffic
Certification of Authenticity:
I certify that this assignment is entirely my own work.

"""
def traffic():
    print("how many roads were serveyed?:\t")
    num_roads_serv = eval(input(" "))
    total_cars = 0

    for i in range(num_roads_serv):

        print("how many days was road", i+1, "serveyed?:\t")
        num_days_serv = eval(input(" "))

        print()
        num_cars_tot_day = 0
        avg_cars_road = 0

        for j in range(num_days_serv):

            print("how many vehicles used road", j+1," each day?:\t")
            num_cars_day = eval(input(" "))
            num_cars_tot_day += num_cars_day
            total_cars += num_cars_day

        avg_num_cars = num_cars_tot_day/(num_days_serv)

        print("road", i+1 , "average number of vehicles per day:\t", avg_num_cars)

    print("total number of vehicles on all roads:\t\t", total_cars)

    print("average number of vehicles per road:\t\t", (total_cars/num_roads_serv))


#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Thu Dec  6 14:42:48 2018

@author: davidstupski
"""
from __future__ import division
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
#from matplotlib import cm
#from mpl_toolkits.mplot3d import Axes3D
import pandas as pd

#Need a script that highlights times of events where thermoregulatory thresholds are broken
plt.style.use("seaborn-white")
tamb = []
rad = []
sstemp = []
#mean values of all paramaters
def heat_transfer(Tb, t, i, Ta):
    hK = 0.00424
    #Ta = 10
    alpha = .903
    a_inc = .0000365683
    #i = 550
    epsilon = .95
    sigma = 5.67*10**-8
    a_surf =.0000731367
    c = 4.31
    m = 0.04
    dTbdt = -1.0 * hK*(Tb-Ta)/(c*m) + alpha *a_inc*i/(c*m) - epsilon *sigma*a_surf*((Tb+273.15)**4-(Ta+273.15)**4)/(c*m) + ((-29.83514*Tb+1763.0566)/(1000))/c - (.0022*2.71828**(.24413*Tb))/(1000)/c
    return dTbdt
#mean - lower error value for all paramaters
def heat_transfermin(Tb, t, i, Ta):
    hK = 0.00424+.00029816
    #Ta = 10
    alpha = .903-.0081*1.96
    a_inc = .0000365683
    #i = 550
    epsilon = .95
    sigma = 5.67*10**-8
    a_surf =.0000731367
    c = 4.31
    m = 0.04
    dTbdt = -1.0 * hK*(Tb-Ta)/(c*m) + alpha *a_inc*i/(c*m) - epsilon *sigma*a_surf*((Tb+273.15)**4-(Ta+273.15)**4)/(c*m) + (((-29.83514*Tb+1763.0566-56*1.96))/(1000))/c - .90*(.0022*2.71828**(.24413*Tb)-25.81*1.96)/(1000)/c
    return dTbdt

def heat_transfermax(Tb, t, i, Ta):
    hK = 0.00424-.00029816
    #Ta = 10
    alpha = .903+.0081
    a_inc = .0000365683
    #i = 550
    epsilon = .95
    sigma = 5.67*10**-8
    a_surf =.0000731367
    c = 4.31
    m = 0.04
    dTbdt = -1.0 * hK*(Tb-Ta)/(c*m) + alpha *a_inc*i/(c*m) - epsilon *sigma*a_surf*((Tb+273.15)**4-(Ta+273.15)**4)/(c*m) + (((-29.83514*Tb+1763.0566)+56*1.96)/(1000))/c - (.0022*2.71828**(.24413*Tb)+25.81*1.96)/(1000)/c
    return dTbdt

def labheat_transfer(Tb, t, i, Ta):
    hK = 0.00217
    #Ta = 10
    alpha = .903
    a_inc = .0000365683
    #i = 550
    epsilon = .95
    sigma = 5.67*10**-8
    a_surf =.0000731367
    c = 4.31
    m = 0.04
    dTbdt = -1.0 * hK*(Tb-Ta)/(c*m) + alpha *a_inc*i/(c*m) - epsilon *sigma*a_surf*((Tb+273.15)**4-(Ta+273.15)**4)/(c*m) + ((-29.83514*Tb+1763.0566)/(1000))/c - (.0022*2.71828**(.24413*Tb))/(1000)/c
    return dTbdt
#mean - lower error value for all paramaters
def labheat_transfermin(Tb, t, i, Ta):
    hK = 0.00217+.00029816
    #Ta = 10
    alpha = .903-.0081
    a_inc = .0000365683
    #i = 550
    epsilon = .95
    sigma = 5.67*10**-8
    a_surf =.0000731367
    c = 4.31
    m = 0.04
    dTbdt = -1.0 * hK*(Tb-Ta)/(c*m) + alpha *a_inc*i/(c*m) - epsilon *sigma*a_surf*((Tb+273.15)**4-(Ta+273.15)**4)/(c*m) + (((-29.83514*Tb+1763.0566-56*1.96))/(1000))/c - .90*(.0022*2.71828**(.24413*Tb)-25.81*1.96)/(1000)/c
    return dTbdt

def labheat_transfermax(Tb, t, i, Ta):
    hK = 0.00217-.00029816
    #Ta = 10
    alpha = .903+.0081
    a_inc = .0000365683
    #i = 550
    epsilon = .95
    sigma = 5.67*10**-8
    a_surf =.0000731367
    c = 4.31
    m = 0.04
    dTbdt = -1.0 * hK*(Tb-Ta)/(c*m) + alpha *a_inc*i/(c*m) - epsilon *sigma*a_surf*((Tb+273.15)**4-(Ta+273.15)**4)/(c*m) + (((-29.83514*Tb+1763.0566)+56*1.96)/(1000))/c - (.0022*2.71828**(.24413*Tb)+25.81*1.96)/(1000)/c
    return dTbdt

def water_loss_value(Tb):
    waterloss= (.0022*2.71828**(.24413*Tb))
    return waterloss
T0 = 37.5
t = np.linspace(0,250,1000)
test_temps = [20,21,22, 23,24,25, 26,27,28, 29,30,31, 32, 33,34,35,36, 37, 38]
fin_temps_solar = []
fin_temps_solar_max= []
fin_temps_solar_min = []
fin_temps_lab = []
fin_temps_lab_max = []
fin_temps_lab_min = []

for i in test_temps:
    fin_temps_solar.append(odeint(heat_transfer, T0, t,args =(1100, i))[-1][0])
    fin_temps_solar_min.append(odeint(heat_transfermin, T0, t,args =(1100, i))[-1][0])
    fin_temps_solar_max.append(odeint(heat_transfermax, T0, t,args =(1100, i))[-1][0])

for i in test_temps:
    fin_temps_lab.append(odeint(labheat_transfer, T0, t,args =(0, i))[-1][0])
    fin_temps_lab_min.append(odeint(labheat_transfermin, T0, t,args =(0, i))[-1][0])
    fin_temps_lab_max.append(odeint(labheat_transfermax, T0, t,args =(00, i))[-1][0])

#print fin_temps_solar
#print fin_temps_solar_max
#print fin_temps_solar_min
water_vector_solar = []
water_vector_solarmax = []
water_vector_solarmin=[]
water_vector_lab = []
water_vector_labmax =[]
water_vector_labmin= []

for i in fin_temps_solar:
    x = water_loss_value(i)
    water_vector_solar.append(x)
for i in fin_temps_solar_max:
    x = water_loss_value(i)
    water_vector_solarmax.append(x)
for i in fin_temps_solar_min:
    x = water_loss_value(i)
    water_vector_solarmin.append(x)

for i in fin_temps_lab:
    x = water_loss_value(i)
    water_vector_lab.append(x)
for i in fin_temps_lab_max:
    x = water_loss_value(i)
    water_vector_labmax.append(x)
for i in fin_temps_lab_min:
    x = water_loss_value(i)
    water_vector_labmin.append(x)

new_df = pd.DataFrame()
new_df["solar"]= water_vector_solar
new_df["solar lower"]= water_vector_solarmin
new_df["solar upper"] = water_vector_solarmax
new_df["lab"]= water_vector_lab
new_df["lab upper"] = water_vector_labmax
new_df["lab lower"] = water_vector_labmin

fig, ax = plt.subplots()
ax.plot(test_temps, water_vector_solar, color = "red", label = "Insolated")
ax.plot(test_temps, water_vector_lab, color = "blue", label = "Laboratory", linestyle = "--")
ax.plot(test_temps, water_vector_solarmin, color = "red", linestyle = "")
ax.plot(test_temps, water_vector_labmin, color = "blue", linestyle = "")
ax.plot(test_temps, water_vector_solarmax, color = "red", linestyle = "")
ax.plot(test_temps, water_vector_labmax, color = "blue", linestyle = "" )
ax.fill_between(test_temps, new_df["lab upper"], new_df["lab lower"], color = "blue", alpha = '0.2')
ax.fill_between(test_temps, new_df["solar upper"], new_df["solar lower"], color = "red", alpha = '0.2')

ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.legend()
ax.set_xlabel("Ambient Temperature (C)")
ax.set_ylabel("Predicted Waterloss (mJ/s/g)")
plt.savefig("/Users/davidstupski/Desktop/water_loss_fig.pdf")