-
Notifications
You must be signed in to change notification settings - Fork 26
/
dimensions.py
304 lines (244 loc) · 8.73 KB
/
dimensions.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# Copyright (c) 2016, German Neuroinformatics Node (G-Node)
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted under the terms of the BSD License. See
# LICENSE file in the root of the Project.
from __future__ import (absolute_import, division)
from numbers import Number
import numpy as np
from ..value import DataType
from ..dimension_type import DimensionType
from . import util
class Dimension(object):
def __init__(self, h5group, index):
from nixio.pycore.h5group import H5Group
if not isinstance(h5group, H5Group):
raise Exception
self._h5group = h5group
self.dim_index = index
@classmethod
def _create_new(cls, parent, index):
h5group = parent.open_group(str(index))
newdim = cls(h5group, index)
return newdim
@property
def dimension_type(self):
return self._h5group.get_attr("dimension_type")
@dimension_type.setter
def dimension_type(self, dimtype):
if dimtype not in (DimensionType.Sample, DimensionType.Range,
DimensionType.Set):
raise ValueError("Invalid dimension type.")
self._h5group.set_attr("dimension_type", dimtype)
@property
def index(self):
return self.dim_index
@index.setter
def index(self, idx):
util.check_attr_type(idx, int)
self.dim_index = idx
class SampledDimension(Dimension):
def __init__(self, h5group, index):
super(SampledDimension, self).__init__(h5group, index)
@classmethod
def _create_new(cls, parent, index, sample):
newdim = super(SampledDimension, cls)._create_new(parent, index)
newdim.dimension_type = DimensionType.Sample
newdim.sampling_interval = sample
return newdim
def position_at(self, index):
"""
Returns the position corresponding to a given index.
:param index: A positive integer.
:returns: The position matching to the index.
:rtype: float
"""
offset = self.offset if self.offset else 0
sample = self.sampling_interval
return index * sample + offset
def index_of(self, position):
"""
Returns the index of a certain position in the dimension.
:param position: The position.
:returns: The nearest index.
:rtype: int
"""
offset = self.offset if self.offset else 0
sample = self.sampling_interval
index = round((position - offset) / sample)
if index < 0:
raise IndexError("Position is out of bounds of this dimension!")
return index
def axis(self, count, start=0):
"""
Get an axis as defined by this sampled dimension.
:param count: A positive integer specifying the length of the axis
(no of samples).
:param start: positive integer, indicates the starting sample.
:returns: The created axis
:rtype: list
"""
offset = self.offset if self.offset else 0
sample = self.sampling_interval
end = (count + start) * sample + offset
return tuple(np.arange(offset, end, sample))
@property
def label(self):
return self._h5group.get_attr("label")
@label.setter
def label(self, l):
util.check_attr_type(l, str)
self._h5group.set_attr("label", l)
@property
def sampling_interval(self):
return self._h5group.get_attr("sampling_interval")
@sampling_interval.setter
def sampling_interval(self, interval):
util.check_attr_type(interval, Number)
self._h5group.set_attr("sampling_interval", interval)
@property
def unit(self):
return self._h5group.get_attr("unit")
@unit.setter
def unit(self, u):
util.check_attr_type(u, str)
self._h5group.set_attr("unit", u)
@property
def offset(self):
return self._h5group.get_attr("offset")
@offset.setter
def offset(self, o):
util.check_attr_type(o, Number)
self._h5group.set_attr("offset", o)
class RangeDimension(Dimension):
def __init__(self, h5group, index):
super(RangeDimension, self).__init__(h5group, index)
@classmethod
def _create_new(cls, parent, index, ticks):
newdim = super(RangeDimension, cls)._create_new(parent, index)
newdim.dimension_type = DimensionType.Range
ticksds = newdim._h5group.create_dataset("ticks",
shape=np.shape(ticks),
dtype=DataType.Double)
ticksds.write_data(ticks)
return newdim
@classmethod
def _create_new_alias(cls, parent, index, da):
newdim = super(RangeDimension, cls)._create_new(parent, index)
newdim.dimension_type = DimensionType.Range
newdim._h5group.create_link(da, da.id)
return newdim
@property
def is_alias(self):
"""
Return True if this dimension is an Alias Range dimension.
Read-only property.
"""
if self._h5group.has_data("ticks"):
return False
return True
@property
def ticks(self):
g = self._redirgrp
if g.has_data("ticks"):
ticks = g.get_data("ticks")
elif g.has_data("data"):
ticks = g.get_data("data")
else:
raise AttributeError("Attribute 'ticks' is not set.")
return tuple(ticks)
@ticks.setter
def ticks(self, ticks):
if np.any(np.diff(ticks) < 0):
raise ValueError("Ticks are not given in an ascending order.")
ticksds = self._h5group.get_dataset("ticks")
ticksds.write_data(ticks)
@property
def _redirgrp(self):
"""
If the dimension is an Alias Range dimension, this property returns
the H5Group of the linked DataArray. Otherwise, it returns the H5Group
representing the dimension.
"""
if self.is_alias:
gname = self._h5group.get_by_pos(0).name
return self._h5group.open_group(gname)
return self._h5group
@property
def label(self):
return self._redirgrp.get_attr("label")
@label.setter
def label(self, l):
util.check_attr_type(l, str)
self._redirgrp.set_attr("label", l)
@property
def unit(self):
return self._redirgrp.get_attr("unit")
@unit.setter
def unit(self, u):
util.check_attr_type(u, str)
self._redirgrp.set_attr("unit", u)
def index_of(self, position):
"""
Returns the index of a certain position in the dimension.
:param position: The position.
:returns: The nearest index.
:rtype: int
"""
ticks = self.ticks
if position < ticks[0]:
return 0
elif position > ticks[-1]:
return len(ticks) - 1
ticks = np.array(ticks)
pidxs = np.flatnonzero((ticks - position) <= 0)
return pidxs[-1]
def tick_at(self, index):
"""
Returns the tick at the given index. Will throw an Exception if the
index is out of bounds.
:param index: The index.
:returns: The corresponding position.
:rtype: double
"""
ticks = list(self.ticks)
return ticks[index]
def axis(self, count, start=0):
"""
Get an axis as defined by this range dimension.
:param count: A positive integer specifying the length of the axis
(no of points).
:param start: positive integer, indicates the starting tick.
:returns: The created axis
:rtype: list
"""
ticks = self.ticks
end = start + count
if end > len(ticks):
raise IndexError("RangeDimension.axis: Count is invalid, "
"reaches beyond the ticks stored in this "
"dimension.")
return ticks[start:end]
class SetDimension(Dimension):
def __init__(self, h5group, index):
super(SetDimension, self).__init__(h5group, index)
@classmethod
def _create_new(cls, parent, index):
newdim = super(SetDimension, cls)._create_new(parent, index)
newdim.dimension_type = DimensionType.Set
return newdim
@property
def labels(self):
labels = tuple(self._h5group.get_data("labels"))
if len(labels) and isinstance(labels[0], bytes):
labels = tuple(l.decode() for l in labels)
return labels
@labels.setter
def labels(self, labels):
lshape = np.shape(labels)
dt = util.vlen_str_dtype
labelsds = self._h5group.create_dataset("labels", shape=lshape,
dtype=dt)
labelsds.write_data(labels)