Skip to content

Commit 1596229

Browse files
committed
Removed tempfile
1 parent 70d1dae commit 1596229

12 files changed

Lines changed: 104 additions & 58 deletions

File tree

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import tempfile
2-
import h5py
3-
import numpy as np
41
from edgeml.TimeSeries import TimeSeries
5-
from edgeml.Labeling import Labeling, Label
2+
from edgeml.Labeling import Labeling
63
from functools import reduce
74
import pandas as pd
85

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import tempfile
1+
import io
22
import h5py
33
import numpy as np
44
from edgeml.consts import getProjectEndpoint
@@ -48,13 +48,11 @@ def data(self, value):
4848

4949
def loadData(self) -> pd.DataFrame:
5050
res = req.get(self._backendURL + getProjectEndpoint + self._readKey + "/" + self._datasetId + "/" + self._id)
51-
with tempfile.NamedTemporaryFile(suffix=".h5", delete=False) as temp_file:
52-
temp_file.write(res.content)
53-
temp_file.flush()
51+
with io.BytesIO(res.content) as temp_file:
5452
if self.length == 0 or self.length == None:
5553
self.data = pd.DataFrame(columns=['time', self.name])
5654
else:
57-
with h5py.File(temp_file.name, "r") as hf:
55+
with h5py.File(temp_file, "r") as hf:
5856
time_array = np.array(hf["time"])
5957
data_array = np.array(hf["data"])
6058
df = pd.DataFrame({"time": time_array, self.name: data_array})

edgeml/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .edgeml import DatasetCollector, DatasetReceiver
2+
from . import edgeml
File renamed without changes.
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import requests as req
2-
from edgeml.consts import getProjectEndpoint, initDatasetIncrement, addDatasetIncrement
3-
from edgeml.Dataset import Dataset
2+
from .consts import getProjectEndpoint, initDatasetIncrement, addDatasetIncrement
3+
from .Dataset import Dataset
44
import time
55

66
class DatasetReceiver:
@@ -82,7 +82,7 @@ def __init__(
8282
)
8383

8484
if res.status_code != 200:
85-
raise RuntimeError(res.text.split(':')[1][1:-2])
85+
raise RuntimeError(res.text)
8686

8787
res_data = res.json()
8888
if not res_data or not res_data["id"]:
@@ -101,8 +101,8 @@ async def _addDataPoint_OwnTimeStamps(self, timestamp, name, value):
101101
if not isinstance(value, (int, float)):
102102
raise ValueError("Datapoint is not a number")
103103

104-
if not isinstance(timestamp, (int)):
105-
raise ValueError("Provide a valid timestamp")
104+
if not isinstance(timestamp, int):
105+
raise ValueError(f"Timestamp is not an integer: {timestamp}")
106106
self.dataStore[name].append([timestamp, value])
107107

108108
if time.time() * 1000 - self.lastChecked > UPLOAD_INTERVAL:
@@ -145,4 +145,4 @@ def onComplete(self):
145145
raise RuntimeError(self.error)
146146
self.uploadComplete = True
147147
self.dataStore = {x: [] for x in self.timeSeries}
148-
return True
148+
return True

notebook.ipynb

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 1,
5+
"execution_count": null,
66
"metadata": {},
77
"outputs": [],
88
"source": [
@@ -22,7 +22,7 @@
2222
},
2323
{
2424
"cell_type": "code",
25-
"execution_count": 2,
25+
"execution_count": null,
2626
"metadata": {},
2727
"outputs": [
2828
{
@@ -37,8 +37,8 @@
3737
}
3838
],
3939
"source": [
40-
"readKey = \"4c1137ca4a959601522c5d4367c3737d\" # Device API Read Access Key\n",
41-
"writeKey = \"e49f4833a04db7647d9e6c43b38d247f\" # Device API Write Access Key\n",
40+
"readKey = \"f81fc94a0f68d6780bc580b4b9a8d8fc\" # Device API Read Access Key\n",
41+
"writeKey = \"454014dfe1e39bca18ad61b80c1fedb6\" # Device API Write Access Key\n",
4242
"localUrl = \"http://localhost:3004\" # Backend URL\n",
4343
"datasetName = \"Example Dataset\" # Name of the dataset\n",
4444
"useDeviceTime = False # Change it to true if you want to use timestamps generated by the server \n",
@@ -76,9 +76,21 @@
7676
},
7777
{
7878
"cell_type": "code",
79-
"execution_count": 98,
79+
"execution_count": null,
8080
"metadata": {},
81-
"outputs": [],
81+
"outputs": [
82+
{
83+
"ename": "AttributeError",
84+
"evalue": "module 'edgeml.edgeml' has no attribute 'getProject'",
85+
"output_type": "error",
86+
"traceback": [
87+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
88+
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
89+
"Input \u001b[0;32mIn [4]\u001b[0m, in \u001b[0;36m<cell line: 3>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m remoteUrl \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mhttps://app.edge-ml.org\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 2\u001b[0m remoteKey \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPSJSIwhSuN6TSK9LEMzuFbfXZwACWKYFFtKWxXy2Upkrmhv4U7MjD25b+QcPl0d20n6vsfBWV4Gz4iWBhSmnXw==\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m----> 3\u001b[0m project \u001b[38;5;241m=\u001b[39m \u001b[43medgeml\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgetProject\u001b[49m(remoteUrl, remoteKey) \u001b[38;5;66;03m# retrieve project data from remote server\u001b[39;00m\n\u001b[1;32m 4\u001b[0m datasets \u001b[38;5;241m=\u001b[39m project[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mdatasets\u001b[39m\u001b[38;5;124m'\u001b[39m]\n",
90+
"\u001b[0;31mAttributeError\u001b[0m: module 'edgeml.edgeml' has no attribute 'getProject'"
91+
]
92+
}
93+
],
8294
"source": [
8395
"remoteUrl = \"https://app.edge-ml.org\"\n",
8496
"remoteKey = \"PSJSIwhSuN6TSK9LEMzuFbfXZwACWKYFFtKWxXy2Upkrmhv4U7MjD25b+QcPl0d20n6vsfBWV4Gz4iWBhSmnXw==\"\n",
@@ -97,7 +109,7 @@
97109
},
98110
{
99111
"cell_type": "code",
100-
"execution_count": 99,
112+
"execution_count": null,
101113
"metadata": {},
102114
"outputs": [
103115
{
@@ -138,7 +150,7 @@
138150
},
139151
{
140152
"cell_type": "code",
141-
"execution_count": 109,
153+
"execution_count": null,
142154
"metadata": {},
143155
"outputs": [
144156
{
@@ -185,7 +197,7 @@
185197
},
186198
{
187199
"cell_type": "code",
188-
"execution_count": 100,
200+
"execution_count": null,
189201
"metadata": {},
190202
"outputs": [
191203
{
@@ -218,7 +230,7 @@
218230
},
219231
{
220232
"cell_type": "code",
221-
"execution_count": 101,
233+
"execution_count": null,
222234
"metadata": {},
223235
"outputs": [],
224236
"source": [
@@ -254,7 +266,7 @@
254266
},
255267
{
256268
"cell_type": "code",
257-
"execution_count": 102,
269+
"execution_count": null,
258270
"metadata": {},
259271
"outputs": [
260272
{
@@ -6266,7 +6278,7 @@
62666278
},
62676279
{
62686280
"cell_type": "code",
6269-
"execution_count": 103,
6281+
"execution_count": null,
62706282
"metadata": {},
62716283
"outputs": [
62726284
{
@@ -6377,7 +6389,7 @@
63776389
},
63786390
{
63796391
"cell_type": "code",
6380-
"execution_count": 104,
6392+
"execution_count": null,
63816393
"metadata": {},
63826394
"outputs": [
63836395
{
@@ -6409,7 +6421,7 @@
64096421
},
64106422
{
64116423
"cell_type": "code",
6412-
"execution_count": 105,
6424+
"execution_count": null,
64136425
"metadata": {},
64146426
"outputs": [],
64156427
"source": [
@@ -6426,7 +6438,7 @@
64266438
},
64276439
{
64286440
"cell_type": "code",
6429-
"execution_count": 106,
6441+
"execution_count": null,
64306442
"metadata": {},
64316443
"outputs": [
64326444
{
@@ -6760,7 +6772,7 @@
67606772
},
67616773
{
67626774
"cell_type": "code",
6763-
"execution_count": 107,
6775+
"execution_count": null,
67646776
"metadata": {},
67656777
"outputs": [
67666778
{
@@ -6789,7 +6801,7 @@
67896801
},
67906802
{
67916803
"cell_type": "code",
6792-
"execution_count": 108,
6804+
"execution_count": null,
67936805
"metadata": {},
67946806
"outputs": [
67956807
{
@@ -6808,11 +6820,8 @@
68086820
}
68096821
],
68106822
"metadata": {
6811-
"interpreter": {
6812-
"hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1"
6813-
},
68146823
"kernelspec": {
6815-
"display_name": "Python 3 (ipykernel)",
6824+
"display_name": "edgeml",
68166825
"language": "python",
68176826
"name": "python3"
68186827
},
@@ -6826,7 +6835,7 @@
68266835
"name": "python",
68276836
"nbconvert_exporter": "python",
68286837
"pygments_lexer": "ipython3",
6829-
"version": "3.8.10"
6838+
"version": "3.10.0"
68306839
},
68316840
"orig_nbformat": 4
68326841
},

src/edgeml/__init__.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

test_getdataset.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from edgeml import DatasetReceiver
2+
3+
4+
5+
6+
WRITE_KEY = "399b57a1a98c6f7450ef8235f086a815"
7+
READ_KEY = "0f1a0ea264a33383969f21b4b45b82ad"
8+
9+
localUrl = "https://beta.edge-ml.org"
10+
datasetName = "Example Dataset"
11+
12+
project = DatasetReceiver(localUrl, READ_KEY, WRITE_KEY)
13+
14+
print(project)
15+
16+
project.loadData()
17+
18+
print(project.datasets[0].timeSeries[0].data.head())
19+
20+
print(project.data)

test_insert.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import edgeml
2+
import time
3+
from tqdm.auto import tqdm
4+
5+
6+
WRITE_KEY = "399b57a1a98c6f7450ef8235f086a815"
7+
localUrl = "https://beta.edge-ml.org"
8+
datasetName = "Example Dataset"
9+
10+
import math
11+
import asyncio
12+
13+
timeSeries = ["Acc", "Mag"]
14+
metaData = {}
15+
16+
async def main():
17+
print("Creating DatasetCollector")
18+
collector = edgeml.DatasetCollector(localUrl,
19+
WRITE_KEY,
20+
datasetName,
21+
False,
22+
timeSeries,
23+
metaData)
24+
25+
print("DatasetCollector created")
26+
27+
timestamp = round(time.time() * 1000)
28+
for i in tqdm(range(10)):
29+
timestamp += 40
30+
x = i / 10000 # Adjust the divisor to control the frequency of the wave
31+
y_acc = math.sin(x) # Generate the y-coordinate for "Acc"
32+
y_mag = math.cos(x) # Generate the y-coordinate for "Mag"
33+
await collector.addDataPoint(timestamp, "Acc", y_acc)
34+
await collector.addDataPoint(timestamp, "Mag", y_mag)
35+
36+
# signal data collection is complete
37+
collector.onComplete()
38+
39+
40+
print("Starting data collection")
41+
asyncio.run(main())

0 commit comments

Comments
 (0)