G-Node / nixpy

Python library for NIX
https://readthedocs.org/projects/nixpy
Other
19 stars 26 forks source link

MultiTag.tagged_data: OOB and non-intuitive slicing #502

Closed plodocus closed 3 years ago

plodocus commented 3 years ago

Suppose I have a sparsely firing neuron and I want to count how many spikes I have per trial. I'd define a MultiTag as the trials with some positions and extents. With tagged_data I would get all and only those spikes present in a range defined by a position and its extent. When I try this, I get out-of-bound errors or incorrect data. Wouldn't it be possible to just return an empty numpy array if there is no data within a range? This is probably a design choice but it seems a bit weird to me.

timestamps = np.array([2.8, 3.8])

f = nixio.File.open("test.nix", "w")
b = f.create_block("block0", "foo")

da_ts = b.create_data_array("spike_times", "foo", data=timestamps)
da_ts.unit = "s" 
da_ts.label = "time"
dim = da_ts.append_range_dimension()
dim.link_data_array(da_ts, [-1])

# only trial 2 (3.8 to 4.2) has a spike
pos = b.create_data_array("pos", "foo", data=[2.0, 3.0, 3.8, 6.4])
ext = b.create_data_array("ext", "foo", data=[0.4, 0.4, 0.4, 0.4])
mt = b.create_multi_tag("trials", "foo", positions=pos)
mt.extents = ext 
mt.references.append(da_ts)
# trial 0: no spikes: expect empty array, get OOB
print(mt.tagged_data(0, "spike_times")[:])
# trial 1: no spikes: expect empty array (or at least the closest spike), get next trial's spike
print(mt.tagged_data(1, "spike_times")[:])
# trial 2: 1 spike: expect 1 spike, get the correct spike
print(mt.tagged_data(2, "spike_times")[:])
# trial 3: no spikes: expect empty array, get OOB
print(mt.tagged_data(3, "spike_times")[:])
jgrewe commented 3 years ago

We had a short meeting in which we discussed about this point. The rationale was to keep the python implementation similar to the C++ implementation in which we so far had no invalid or empty DataView. We will change this and, much as you suggest, return empty arrays in cases the tagged range is invalid or does not contain data. Thanks for the report