1+ from typing import List
12import requests as req
3+ from pandas import DataFrame
24import time as timelib
35
6+ # TODO add typing
47
58uploadDataset = "/api/deviceApi/uploadDataset"
69initDatasetIncrement = "/api/deviceApi/initDatasetIncrement"
@@ -33,6 +36,76 @@ def getProject(url: str, key: str):
3336 res = req .post (url + getProjectEndpoint , json = {"key" : key })
3437 return res .json ()
3538
39+ def __extractLabels (dataset ):
40+ labels = dataset ['labels' ]
41+ labelType = "No labeling, no dataframe will be generated"
42+ if labels :
43+ labelType = labels [0 ][0 ]['labelingName' ]
44+ labelset = {} # stores different start and end times (intervals) belonging to a label
45+ labelIds = {} # assing distinct ids to labels, required for training with data
46+ labelId = 0
47+ for labelData in labels :
48+ for label in labelData :
49+ name = label ['name' ]
50+ start = label ['start' ]
51+ end = label ['end' ]
52+ if not name in labelset :
53+ labelset [name ] = []
54+ labelIds [name ] = labelId # assign id to the label
55+ labelId = labelId + 1
56+ labelset [name ].append ((start , end )) # add interval to the label
57+ return (labelType , labelset , labelIds )
58+
59+ def __processDataset (dataset ):
60+ dataTimeValueSensor = {} # sensor values fused into single timestamps
61+ sensors = dataset ['sensors' ]
62+ for sensor in sensors :
63+ sensorName = sensor ['name' ]
64+ data = sensor ['data' ]
65+ for dataPoint in data :
66+ timestamp = dataPoint ['timestamp' ]
67+ dataPointValue = dataPoint ['datapoint' ]
68+ if timestamp not in dataTimeValueSensor :
69+ dataTimeValueSensor [timestamp ] = []
70+ dataTimeValueSensor [timestamp ].append ({'value' : dataPointValue , 'sensor' : sensorName })
71+ return dataTimeValueSensor
72+
73+ #
74+ # Returns a list of Pandas.DataFrames generated from the dataset
75+ # @param {string} url - The url of the backend server
76+ # @param {string} key - The Device-Api-Key
77+ #
78+
79+ def getDataFrames (url : str , key : str ) -> List [DataFrame ]:
80+ datasets = getProject (url , key )['datasets' ]
81+ dataFrames : List [DataFrame ] = []
82+ for dataset in datasets :
83+ (labelType , labelset , labelIds ) = __extractLabels (dataset )
84+ dataTimeValueSensor = __processDataset (dataset )
85+ dataFrame = {'id' : [], labelType : []}
86+ id = 0
87+ for timestamp , timestampData in dataTimeValueSensor .items ():
88+ for data in timestampData :
89+ value = data ['value' ]
90+ sensor = data ['sensor' ]
91+ if not sensor in dataFrame :
92+ dataFrame [sensor ] = []
93+ labelFound = False
94+ for label , intervals in labelset .items ():
95+ for interval in intervals :
96+ start = interval [0 ]
97+ end = interval [1 ]
98+ if timestamp >= start and timestamp <= end :
99+ if data == timestampData [0 ]:
100+ dataFrame [labelType ].append (label )
101+ dataFrame ['id' ].append (id )
102+ id = id + 1
103+ dataFrame [sensor ].append (value )
104+ break
105+ dataFrame = DataFrame (dataFrame )
106+ dataFrames .append (dataFrame )
107+ return dataFrames
108+
36109#
37110# @param {string} url - The url of the backend server
38111# @param {string} key - The Device-Api-Key
@@ -92,4 +165,6 @@ def __upload(self):
92165 def onComplete (self ):
93166 if self .error :
94167 raise self .error
95- self .__upload ()
168+ self .__upload ()
169+
170+ getDataFrames ("https://app.edge-ml.org" , "KVH0X0i9GIM/aP6IUPcr88iSaCRhh8uTWsnyrz5651X7fbvPFXrU/qoFCEgCVEv/xMZA4QDaNWpaeVypImfMsw==" )
0 commit comments