Skip to content

Commit 11d5e0e

Browse files
committed
[SampledDimension] add axis overload to specifiy a start position, not only a start index
1 parent e5c74d2 commit 11d5e0e

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

nixio/dimensions.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -392,21 +392,41 @@ def index_of(self, position, mode=IndexMode.LessOrEqual):
392392

393393
raise ValueError("Unknown IndexMode: {}".format(mode))
394394

395-
def axis(self, count, start=0):
395+
def axis(self, count, start=None, start_position=None):
396396
"""
397-
Get an axis as defined by this sampled dimension.
397+
Get an axis as defined by this nixio.SampledDimension. It either starts at the offset of the dimension,
398+
a number of sample points later, or at a given position. The latter must not be less than the offset. If
399+
start and start_position are given, start takes precedence.
398400
399-
:param count: A positive integer specifying the length of the axis
400-
(no of samples).
401+
:param count: A positive integer specifying the length of the axis (no of samples).
402+
:type count: int
403+
:param start: positive integer, indicates the starting sample. Defaults to None. Precedes over the start_position.
404+
:type start: int
405+
:param start_position: The start position of the axis. Defaults to None.
406+
:type start_position: double
401407
402-
:param start: positive integer, indicates the starting sample.
408+
:raises: ValueError if start is negative or if the start_position is given and is less than offset.
403409
404410
:returns: The created axis
405411
:rtype: tuple
406412
"""
407413
offset = self.offset if self.offset else 0.0
408414
sample = self.sampling_interval
409-
start_val = start * sample + offset
415+
416+
if start is not None:
417+
if start < 0:
418+
raise ValueError("Start index (%i) must not be negative!" % start)
419+
start_val = start * sample + offset
420+
else:
421+
if start_position is not None:
422+
if start_position < offset:
423+
raise ValueError("SampledDimension.axis: Start position (%.f) must not be "
424+
"less than the offset of the dimension (%.f)!" % (start_position, offset))
425+
else:
426+
start_val = start_position
427+
else:
428+
start_val = offset
429+
410430
return tuple(np.arange(count) * sample + start_val)
411431

412432
@property

nixio/test/test_dimensions.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,23 @@ def test_sample_dimension(self):
8585
assert type(self.sample_dim.index_of(23.) == int)
8686

8787
assert self.sample_dim.position_at(0) == 3.
88-
assert self.sample_dim.position_at(200) == 200*2.+3.
88+
assert self.sample_dim.position_at(200) == 200 * 2. + 3.
8989

9090
assert len(self.sample_dim.axis(10)) == 10
9191
assert self.sample_dim.axis(10)[0] == 3.
92-
assert self.sample_dim.axis(10)[-1] == 9*2.+3.
92+
assert self.sample_dim.axis(10)[-1] == 9 * 2. + 3.
9393

9494
assert len(self.sample_dim.axis(10, 2)) == 10
9595
assert self.sample_dim.axis(10, 2)[0] == 2 * 2. + 3.
9696
assert self.sample_dim.axis(10, 2)[-1] == (9 + 2) * 2. + 3.
9797

98+
with self.assertRaises(ValueError):
99+
self.sample_dim.axis(10, -10)
100+
self.sample_dim.axis(10, start_position=0.0)
101+
assert self.sample_dim.axis(10, 0, 5.0)[0] == 3
102+
assert self.sample_dim.axis(10, start_position=5.0)[0] == 5.0
103+
assert self.sample_dim.axis(10, start_position=5.0)[-1] == 5.0 + 9 * 2
104+
98105
def test_range_dimension(self):
99106
assert self.range_dim.index == 3
100107
assert self.range_dim.dimension_type == nix.DimensionType.Range

0 commit comments

Comments
 (0)