Skip to content

Commit b4ae900

Browse files
authored
Merge pull request #520 from JuliaSprenger/enh/set_dim
Allow for list-like labels of SetDimension
2 parents 724c663 + 7f9219a commit b4ae900

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

nixio/data_array.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,15 @@ def append_set_dimension(self, labels=None):
8282
Append a new SetDimension to the list of existing dimension
8383
descriptors.
8484
85+
:param labels: The set of sample labels
86+
:type labels: list or convertible to list
87+
8588
:returns: The newly created SetDimension.
8689
:rtype: nixio.SetDimension
8790
"""
8891
index = len(self.dimensions) + 1
8992
setdim = SetDimension.create_new(self, index)
90-
if labels:
93+
if labels is not None:
9194
setdim.labels = labels
9295
if self.file.auto_update_timestamps:
9396
self.force_updated_at()
@@ -101,6 +104,12 @@ def append_sampled_dimension(self, sampling_interval, label=None,
101104
102105
:param sampling_interval: The sampling interval of the SetDimension to create.
103106
:type sampling_interval: float
107+
:param label: The label of the dimension
108+
:type label: str
109+
:param unit: The physical unit of the dimension
110+
:type unit: str
111+
:param offset: The offset between 0 and the first sample
112+
:type offset: float
104113
105114
:returns: The newly created SampledDimension.
106115
:rtype: nixio.SampledDimension
@@ -124,6 +133,10 @@ def append_range_dimension(self, ticks=None, label=None, unit=None):
124133
125134
:param ticks: The ticks of the RangeDimension to create.
126135
:type ticks: list of float
136+
:param label: The label of the dimension
137+
:type label: str
138+
:param unit: The physical unit of the dimension
139+
:type unit: str
127140
128141
:returns: The newly created RangeDimension.
129142
:rtype: nixio.RangeDimension

nixio/dimensions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,13 @@ def labels(self, labels):
684684
raise RuntimeError("The labels of a SetDimension linked to a "
685685
"data object cannot be modified")
686686
dt = util.vlen_str_dtype
687+
if not hasattr(labels, '__iter__') or isinstance(labels, str):
688+
raise ValueError('`labels` has to be a list-like object.')
689+
for label in labels:
690+
if not isinstance(label, str):
691+
raise ValueError(f'`labels` has to contain string objects, not {type(label)}')
692+
if not isinstance(labels, list):
693+
labels = list(labels)
687694
self._h5group.write_data("labels", labels, dtype=dt)
688695

689696
def index_of(self, position, mode=IndexMode.LessOrEqual):

nixio/test/test_dimensions.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,22 @@ def test_set_dim_label_resize(self):
153153
setdim.labels = newlabels
154154
assert tuple(newlabels) == setdim.labels
155155

156+
def test_set_dim_labels_array(self):
157+
labels = np.array(["A", "B"])
158+
setdim = self.array.append_set_dimension(labels)
159+
assert tuple(labels) == setdim.labels
160+
161+
def test_set_dim_invalid_labels(self):
162+
# don't accept non list-like labels
163+
with self.assertRaises(ValueError):
164+
self.array.append_set_dimension('Sample 1')
165+
with self.assertRaises(ValueError):
166+
self.array.append_set_dimension(1000)
167+
168+
# don't accept list of non-string objects
169+
with self.assertRaises(ValueError):
170+
self.array.append_set_dimension([1, 2, 3])
171+
156172
def test_range_dim_ticks_resize(self):
157173
rangedim = self.array.append_range_dimension([1, 2, 100])
158174
ticks = [1, 1, 30]

0 commit comments

Comments
 (0)