forked from ieeg-portal/ieegpy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhbt_dataset.py
More file actions
executable file
·85 lines (63 loc) · 3.08 KB
/
hbt_dataset.py
File metadata and controls
executable file
·85 lines (63 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/python
import xml.etree.ElementTree as ET
import datetime
import requests
import numpy as np
import hbt_auth as auth
class Dataset:
""" Class representing Dataset on the platform """
snapID = ""
chLabels = [] # Channel Labels
tsArray = [] # Channel
def __init__(self, tsDetails, snapshotID, parent):
self.session = parent
self.snapID = snapshotID
details = tsDetails.findall('details')[0] #only one details in timeseriesdetails
for dt in details.findall('detail'):
name = dt.findall('name')[0].text
self.chLabels.append(name)
self.tsArray.append(dt)
def __repr__(self):
return "Dataset with: " + str(len(self.chLabels)) + " channels."
def __str__(self):
return "Dataset with: " + str(len(self.chLabels)) + " channels."
def getChannelLabels(self):
return self.chLabels
def getData(self, start, duration, channels):
""" Returns MEF data from Platform """
def all_same(items):
return all(x == items[0] for x in items)
# Request location
getDataStr = "/services/timeseries/getUnscaledTimeSeriesSetBinaryRaw/"
# Build Data Content XML
wrapper1 = ET.Element('timeSeriesIdAndDChecks')
wrapper2 = ET.SubElement(wrapper1, 'timeSeriesIdAndDChecks')
i=0
for ts in self.tsArray:
if(i in channels):
el1 = ET.SubElement(wrapper2, 'timeSeriesIdAndCheck')
el2 = ET.SubElement(el1, 'dataCheck')
el2.text = ts.findall('revisionId')[0].text
el3 = ET.SubElement(el1, 'id')
el3.text = ts.findall('revisionId')[0].text
i += 1
data = ET.tostring(wrapper1, encoding="us-ascii", method="xml")
data = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' + data
# Create request content
reqPath = getDataStr + self.snapID
httpMethod = "POST"
params = {'start': start, 'duration': duration}
payload = self.session._createWSHeader(reqPath, httpMethod, params, data)
urlStr = self.session.urlBuilder(reqPath)
# response to request
r = requests.post(urlStr, headers=payload, params=params,data=data,verify=False)
# collect data in numpy array
d = np.fromstring(r.content, dtype='>i4')
# Check all channels are the same length
samplePerRow = [int(numeric_string) for numeric_string in r.headers['samples-per-row'].split(',')]
if not all_same(samplePerRow):
raise auth.ConnectionError('Not all channels in response have equal length')
convF = np.array([float(numeric_string) for numeric_string in r.headers['voltage-conversion-factors-mv'].split(',')])
#Reshape to 2D array and Multiply by conversionFactor
d2 = np.reshape(d, (-1,len(samplePerRow))) * convF[np.newaxis,:]
return d2