用于Boon Amber传感器分析的SDK
boonamber的Python项目详细描述
Boon琥珀Python SDK
用于Boon Amber传感器分析的SDK
安装
Boon Amber SDK是一个Python3项目,可以通过pip安装。在
pip install boonamber
凭据设置
注意:必须从Boon Logic获得Boon Amber cloud中的帐户才能使用Amber SDK。在
用户名和密码应该放在一个名为~/的文件中。琥珀色许可证其内容如下:
^{pr2}$~/。琥珀色许可证文件将由Amber SDK查询,以在Amber服务器上查找并验证您的帐户凭据。可以选择通过环境变量AMBER_USERNAME
和{
连通性测试
以下Python脚本提供了连接的基本证明:
fromboonamberimportAmberClient# At initialization the client discovers Amber account credentials# under the "default" entry in the ~/.Amber.license file.amber=AmberClient()sensors=amber.list_sensors()print("sensors: {}".format(sensors))
运行连接-示例.py脚本应产生如下输出:
$ python connect-example.py
sensors: {}
其中字典{}
列出了当前存在于给定Boon琥珀色帐户下的所有传感器。在
完整的例子
下面的Python脚本将演示Amber pythonsdk中的每个API调用。在
importsysfromboonamberimportAmberClient,AmberCloudError,AmberUserError"""Demonstrates usage of all Amber SDK endpoints."""# connect with default license# use 'license_id=<name>' to specify something other than 'default'amber=AmberClient()# List all sensors belonging to current userprint("listing sensors")try:sensors=amber.list_sensors()exceptAmberCloudErrorase:print(e)sys.exit(1)exceptAmberUserErrorase:print(e)sys.exit(1)print("sensors: {}".format(sensors))print()# Create a new sensorprint("creating sensor")try:sensor_id=amber.create_sensor('new-test-sensor')exceptAmberCloudErrorase:print(e)sys.exit(1)exceptAmberUserErrorase:print(e)sys.exit(1)print("sensor-id: {}".format(sensor_id))print()# Get sensor infoprint("getting sensor")try:sensor=amber.get_sensor(sensor_id)exceptAmberCloudErrorase:print("Amber Cloud error: {}".format(e))sys.exit(1)exceptAmberUserErrorase:print("Amber user error: {}".format(e))sys.exit(1)print("sensor: {}".format(sensor))print()# Update the label of a sensorprint("updating label")try:label=amber.update_label(sensor_id,'test-sensor')exceptAmberCloudErrorase:print("Amber Cloud error: {}".format(e))sys.exit(1)exceptAmberUserErrorase:print("Amber user error: {}".format(e))sys.exit(1)print("label: {}".format(label))print()# Configure a sensorprint("configuring sensor")try:config=amber.configure_sensor(sensor_id,feature_count=1,streaming_window_size=25)exceptAmberCloudErrorase:print("Amber Cloud error: {}".format(e))sys.exit(1)exceptAmberUserErrorase:print("Amber user error: {}".format(e))sys.exit(1)print("config: {}".format(config))print()# Get sensor configurationprint("getting configuration")try:config=amber.get_config(sensor_id)exceptAmberCloudErrorase:print("Amber Cloud error: {}".format(e))sys.exit(1)exceptAmberUserErrorase:print("Amber user error: {}".format(e))sys.exit(1)print("config: {}".format(config))print()# Stream data to a sensorprint("streaming data")data=[0,1,2,3,4]try:results=amber.stream_sensor(sensor_id,data)exceptAmberCloudErrorase:print("Amber Cloud error: {}".format(e))sys.exit(1)exceptAmberUserErrorase:print("Amber user error: {}".format(e))sys.exit(1)print("results: {},".format(results))print()# Get clustering status from a sensorprint("getting status")try:status=amber.get_status(sensor_id)exceptAmberCloudErrorase:print("Amber Cloud error: {}".format(e))sys.exit(1)exceptAmberUserErrorase:print("Amber user error: {}".format(e))sys.exit(1)print("status: {}".format(status))print()# Delete a sensor instanceprint("deleting sensor")try:amber.delete_sensor(sensor_id)exceptAmberCloudErrorase:print("Amber Cloud error: {}".format(e))sys.exit(1)exceptAmberUserErrorase:print("Amber user error: {}".format(e))sys.exit(1)print("succeeded")print()
高级CSV文件处理器
下面将使用批处理样式的流式处理请求来处理CSV文件。在每个流式处理请求之后,将显示完整的琥珀色分析结果。在
stream-advanced.py
output_current.csv
importcsvimportsysfromdatetimeimportdatetimefromboonamberimportAmberClient,AmberCloudError"""Demonstrates a streaming use case in which we read continuously from a CSV file, inference the data line by line, and print results."""classAmberStream:def__init__(self,sensor_id=None):""" Initializes the AmberStream example class. :param sensor_id: The sensor_id to be used by AmberStream. If sensor_id is None, then a sensor is created """self.data=[]self.sample_cnt=0try:self.amber=AmberClient()ifsensor_idisNone:self.sensor_id=self.amber.create_sensor(label='stream-example-sensor')print("created sensor {}".format(sensor_id))else:self.sensor_id=sensor_idprint("using sensor {}".format(sensor_id))config=self.amber.configure_sensor(sensor_id,feature_count=1,streaming_window_size=25,samples_to_buffer=1000,learning_max_clusters=1000,learning_max_samples=20000,learning_rate_numerator=0,learning_rate_denominator=20000)print("{} config: {}".format(self.sensor_id,config))exceptAmberCloudErrorase:print(e)sys.exit(1)defdo_analytics(self):""" Run analytics based on self.data and provide example of formatted results :return: None """self.sample_cnt+=len(self.data)d1=datetime.now()results=self.amber.stream_sensor(self.sensor_id,self.data)d2=datetime.now()delta=(d2-d1).microseconds/1000print("State: {}({}%), inferences: {}, clusters: {}, samples: {}, duration: {}".format(results['state'],results['progress'],results['totalInferences'],results['clusterCount'],self.sample_cnt,delta))foranalyticin['ID','SI','AD','AH','AM','AW']:ifanalytic=='AM':analytic_pretty=','.join("{:.6f}".format(a)forainresults[analytic])else:analytic_pretty=','.join("{}".format(a)forainresults[analytic])print("{}: {} ".format(analytic,analytic_pretty))self.data=[]defstream_csv(self,csv_file,batch_size=20):""" Given a path to a csv file, stream data to Amber in sizes specified by batch_size :param csv_file: Path to csv file :param batch_size: Batch size to be used on each request :return: None """# Open csv data file and begin streamingwithopen(csv_file,'r')asf:csv_reader=csv.reader(f,delimiter=',')self.data=[]self.sample_cnt=0forrowincsv_reader:fordinrow:self.data.append(float(d))iflen(self.data)==batch_size:try:self.do_analytics()exceptExceptionase:print(e)sys.exit(1)# send the remaining partial batch (if any)iflen(self.data)>0:self.do_analytics()streamer=AmberStream(sensor_id='b76b3cc542f434a7')streamer.stream_csv('output_current.csv',batch_size=25)
样本输出:
State: Monitoring(0%), inferences: 20201, clusters: 247, samples: 20275, duration: 228.852
ID: 29,30,31,32,33,34,35,36,37,38,39,245,41,42,219,44,45,220,47,48,49,50,51,52,1
SI: 306,307,307,307,307,307,307,308,308,308,308,308,309,311,315,322,336,364,421,532,350,393,478,345,382
AD: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
AH: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
AM: 0.000013,0.000013,0.000013,0.000013,0.000013,0.000013,0.000013,0.000013,0.000013,0.000013,0.000013,0.000013,0.000013,0.000013,0.000013,0.000013,0.000013,0.000013,0.000013,0.000013,0.000013,0.000013,0.000013,0.000013,0.000013
AW: 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
- 项目
标签: