In [50]:
import os
import sys
import time
sys.path.append(os.path.abspath(os.path.join(os.path.pardir, 'codes', 'client')))
sys.path.append(os.path.abspath(os.path.join(os.path.pardir, 'codes', 'node')))
sys.path.append(os.path.abspath(os.path.join(os.path.pardir, 'codes', 'shared')))
sys.path.append(os.path.abspath(os.path.join(os.path.pardir, 'codes', 'micropython')))
import client
from collections import OrderedDict
In [51]:
import pandas as pd
from pandas import DataFrame
from time import sleep
REFRACTORY_PERIOD = 0.1 # 0.1 seconds
In [52]:
# 每個 ESP8266模組 各代表一個 neurons
# neurons = ['neuron_x1', 'neuron_x2', 'neuron_h1', 'neuron_h2', 'neuron_h3', 'neuron_y']
neurons = ['neuron_x1', 'neuron_x2']
In [53]:
the_client = client.Client()
the_client.start()
while not the_client.status['Is connected']:
time.sleep(1)
print('Node not ready yet.')
In [54]:
# Ask Hub for a list of connected nodes
def list_nodes():
message = {'message_type': 'command',
'command': 'list connections by name',
'need_result': True}
_, asynch_result = the_client.request('Hub', message)
try:
remote_nodes = sorted(list(asynch_result.get().keys()))
print ('\n[____________ Connected nodes ____________]\n')
print('\nConnected nodes:\n{}\n'.format(remote_nodes))
return remote_nodes
except Exception as e:
print(e)
In [55]:
def reset_node(node):
message = {'message_type': 'exec',
'to_exec': 'import machine;machine.reset()'}
the_client.request(node, message)
In [56]:
# reset_node('neuron_x1')
In [57]:
def rename_node(node, new_name):
message = {'message_type': 'function',
'function': 'rename',
'kwargs': {'name': new_name}}
the_client.request(node, message)
message = {'message_type': 'function',
'function': 'set_connection_name'}
the_client.request(node, message)
def rename_nodes(nodes, neurons):
i = 0
for node in nodes:
if node != the_client.node.worker.name: # exclude client self
rename_node(node, neurons[i])
i += 1
In [58]:
def fire(node):
message = {'message_type': 'function',
'function': 'fire'}
the_client.request(node, message)
def addConnection(node, neuron):
message = {'message_type': 'function',
'function': 'addConnection',
'kwargs': {'neuron_id': neuron}}
the_client.request(node, message)
def set_connections(node, connections):
message = {'message_type': 'function',
'function': 'setConnections',
'kwargs': {'connections': connections}}
the_client.request(node, message)
def get_connections(node):
message = {'message_type': 'function',
'function': 'getConnections',
'need_result': True}
_, result = the_client.request(node, message)
return result.get()
def setWeight(node, neuron, weight):
message = {'message_type': 'function',
'function': 'setWeight',
'kwargs': {'neuron_id': neuron,
'weight': weight,}}
the_client.request(node, message)
def setThreshold(node, threshold):
message = {'message_type': 'function',
'function': 'setThreshold',
'kwargs': {'threshold': threshold}}
the_client.request(node, message)
def getConfig(node):
message = {'message_type': 'function',
'function': 'getConfig',
'need_result': True}
_, result = the_client.request(node, message)
return result.get()
def getLog(node):
message = {'message_type': 'function',
'function': 'getLog',
'need_result': True}
_, result = the_client.request(node, message)
return result.get()
def emptyLog(node):
message = {'message_type': 'function',
'function': 'emptyLog'}
the_client.request(node, message)
def emptyLogs():
for neuron in neurons:
emptyLog(neuron)
# 彙整logs。將所有 neurons 中的 logs merge 在一起,成為一個 Pandas.DataFrame
def mergeLogs():
logs = []
for neuron in neurons:
if neuron != the_client.node.worker.name: # exclude client self
currentLog = getLog(neuron)
if currentLog:
logs += currentLog
df = DataFrame(list(logs), columns = ['time', 'neuron', 'message'])
df.set_index('time', inplace = True)
df.sort_index(inplace = True)
return df
In [59]:
# print 出 一個 neuron 中的 Log
def printConfig(neuron):
print('{0:_^78}\n {1}\n'.format(neuron + " config:", getConfig(neuron)))
In [60]:
remote_nodes = list_nodes()
rename_nodes(remote_nodes, neurons)
time.sleep(2)
remote_nodes = list_nodes()
In [61]:
# 清除所有 neurons 中的 logs
emptyLogs()
In [62]:
addConnection('neuron_x1', 'neuron_x2')
getConfig('neuron_x1')
Out[62]:
In [63]:
# hidden layer
setWeight('neuron_x2', 'neuron_x1', 1) # 設定 neuron_x -> neuron_h1 的 weight = 1
In [64]:
# input layer
setThreshold('neuron_x2', 0.9)
In [65]:
fire('neuron_x1')
In [66]:
### 模擬 sensor input,強迫 neuron x 或 y ouput 1
emptyLogs() # 清除 logs
sleep(REFRACTORY_PERIOD) # 等電位歸零
mergeLogs() # 彙整 logs
Out[66]:
In [67]:
### 模擬 sensor input,強迫 neuron x 或 y ouput 1
emptyLogs() # 清除 logs
sleep(REFRACTORY_PERIOD) # 等電位歸零
fire('neuron_x1') # force neuron x1 output 1 and fire.
mergeLogs() # 彙整 logs
Out[67]:
In [68]:
### 模擬 sensor input,強迫 neuron x 或 y ouput 1
emptyLogs() # 清除 logs
sleep(REFRACTORY_PERIOD) # 等電位歸零
fire('neuron_x2') # force neuron x2 output 1 and fire.
mergeLogs() # 彙整 logs
Out[68]:
In [69]:
### 模擬 sensor input,強迫 neuron x 或 y ouput 1
emptyLogs() # 清除 logs
sleep(REFRACTORY_PERIOD) # 等電位歸零
fire('neuron_x1') # force neuron x1 output 1 and fire.
fire('neuron_x2') # force neuron x2 output 1 and fire.
mergeLogs() # 彙整 logs
Out[69]:
In [70]:
for neuron in reversed(neurons): printConfig(neuron)
In [71]:
# Stopping
the_client.stop()
the_client = None
print ('\n[________________ Demo stopped ________________]\n')