From a1ec78db09484695faafb8937ef8bd59bd0edd5e Mon Sep 17 00:00:00 2001 From: emanueldellipaoli Date: Fri, 20 Nov 2026 00:48:41 +0100 Subject: [PATCH] Fix Quantity merging across concatenation semantics --- neo/core/baseneo.py | 22 ++++++ neo/core/dataobject.py | 5 +- neo/core/epoch.py | 4 +- neo/core/event.py | 2 +- neo/core/irregularlysampledsignal.py | 4 +- neo/core/spiketrain.py | 10 +-- neo/core/spiketrainlist.py | 18 +++-- neo/io/brainwaresrcio.py | 3 + neo/io/elphyio.py | 51 +++++++------- neo/test/coretest/test_base.py | 27 ++++++++ neo/test/coretest/test_dataobject.py | 44 +++++++++++- neo/test/coretest/test_epoch.py | 5 ++ neo/test/coretest/test_event.py | 3 + .../coretest/test_irregularysampledsignal.py | 10 +++ neo/test/coretest/test_spiketrain.py | 67 ++++++++++++++++++- neo/test/coretest/test_spiketrainlist.py | 42 ++++++++++++ neo/test/iotest/test_brainwaresrcio.py | 17 +++++ neo/test/iotest/test_elphyio.py | 20 ++++++ 18 files changed, 302 insertions(+), 52 deletions(-) diff --git a/neo/core/baseneo.py b/neo/core/baseneo.py index 1e6716084..b6dede35a 100644 --- a/neo/core/baseneo.py +++ b/neo/core/baseneo.py @@ -10,6 +10,7 @@ from numbers import Number import numpy as np +import quantities as pq ALLOWED_ANNOTATION_TYPES = ( int, @@ -35,6 +36,25 @@ class MergeError(Exception): pass +def _merge_quantity_arrays(quantities, axis=0): + """Merge compatible Quantity arrays while preserving uncertainty metadata.""" + reference_units = quantities[0].units + normalized = [quantity.rescale(reference_units) for quantity in quantities] + magnitudes = np.concatenate([quantity.magnitude for quantity in normalized], axis=axis) + + uncertain_quantities = [isinstance(quantity, pq.UncertainQuantity) for quantity in normalized] + if any(uncertain_quantities): + if not all(uncertain_quantities): + raise ValueError( + "Cannot merge Quantity and UncertainQuantity arrays without losing uncertainty information" + ) + uncertainties = [quantity.uncertainty.rescale(reference_units).magnitude for quantity in normalized] + uncertainty = np.concatenate(uncertainties, axis=axis) + return pq.UncertainQuantity(magnitudes, units=reference_units, uncertainty=uncertainty) + + return magnitudes * reference_units + + class NeoReadWriteError(IOError): """ This is the main neo-specific error that has to deal with @@ -95,6 +115,8 @@ def merge_annotation(a, b): raise TypeError(f"type({a}) {type(a)} != type({b}) {type(b)}") if isinstance(a, dict): return merge_annotations(a, b) + elif isinstance(a, pq.Quantity): + return _merge_quantity_arrays([a, b], axis=None) elif isinstance(a, np.ndarray): # concatenate b to a return np.append(a, b) elif isinstance(a, list): # concatenate b to a diff --git a/neo/core/dataobject.py b/neo/core/dataobject.py index 71652d66c..72a992b49 100644 --- a/neo/core/dataobject.py +++ b/neo/core/dataobject.py @@ -11,7 +11,7 @@ import quantities as pq import numpy as np -from neo.core.baseneo import BaseNeo, _check_annotations +from neo.core.baseneo import BaseNeo, _check_annotations, _merge_quantity_arrays def _normalize_array_annotations(value, length): @@ -276,10 +276,9 @@ def _merge_array_annotations(self, other): # Quantities need to be rescaled to common unit if isinstance(value, pq.Quantity): try: - other_value = other_value.rescale(value.units) + merged_array_annotations[key] = _merge_quantity_arrays([value, other_value], axis=None) except ValueError: raise ValueError("Could not merge array annotations " "due to different units") - merged_array_annotations[key] = np.append(value, other_value) * value.units else: merged_array_annotations[key] = np.append(value, other_value) diff --git a/neo/core/epoch.py b/neo/core/epoch.py index c5e2698e4..541d2b81d 100644 --- a/neo/core/epoch.py +++ b/neo/core/epoch.py @@ -321,8 +321,8 @@ def merge(self, other): """ othertimes = other.times.rescale(self.times.units) otherdurations = other.durations.rescale(self.durations.units) - times = np.hstack([self.times, othertimes]) * self.times.units - durations = np.hstack([self.durations, otherdurations]) * self.durations.units + times = np.hstack([self.times.magnitude, othertimes.magnitude]) * self.times.units + durations = np.hstack([self.durations.magnitude, otherdurations.magnitude]) * self.durations.units labels = np.hstack([self.labels, other.labels]) kwargs = {} for name in ("name", "description", "file_origin"): diff --git a/neo/core/event.py b/neo/core/event.py index 236f46cbc..ce658fcb4 100644 --- a/neo/core/event.py +++ b/neo/core/event.py @@ -272,7 +272,7 @@ def merge(self, other): compatible, and Exception is raised. """ othertimes = other.times.rescale(self.times.units) - times = np.hstack([self.times, othertimes]) * self.times.units + times = np.hstack([self.times.magnitude, othertimes.magnitude]) * self.times.units labels = np.hstack([self.labels, other.labels]) kwargs = {} for name in ("name", "description", "file_origin"): diff --git a/neo/core/irregularlysampledsignal.py b/neo/core/irregularlysampledsignal.py index 9c0da5318..f257e028c 100644 --- a/neo/core/irregularlysampledsignal.py +++ b/neo/core/irregularlysampledsignal.py @@ -661,7 +661,9 @@ def concatenate(self, other, allow_overlap=False): if other.units != self.units: other = other.rescale(self.units) - new_times = np.hstack((self.times, other.times)) + time_units = self.times.units + other_times = other.times.rescale(time_units) + new_times = np.hstack((self.times.magnitude, other_times.magnitude)) * time_units sorting = np.argsort(new_times) new_samples = np.vstack((self.magnitude, other.magnitude)) diff --git a/neo/core/spiketrain.py b/neo/core/spiketrain.py index e2a3c30c9..689210583 100644 --- a/neo/core/spiketrain.py +++ b/neo/core/spiketrain.py @@ -22,7 +22,7 @@ import numpy as np import quantities as pq -from neo.core.baseneo import BaseNeo, MergeError, merge_annotations +from neo.core.baseneo import BaseNeo, MergeError, _merge_quantity_arrays, merge_annotations from neo.core.dataobject import DataObject, ArrayDict # need this to avoid circular import issue @@ -942,7 +942,7 @@ def merge(self, *others): **kwargs, ) if all(wfs): - wfs_stack = np.vstack([st.waveforms.rescale(self.waveforms.units) for st in all_spiketrains]) + wfs_stack = np.vstack([st.waveforms.rescale(self.waveforms.units).magnitude for st in all_spiketrains]) wfs_stack = wfs_stack[sorting] * self.waveforms.units train.waveforms = wfs_stack train.segment = self.segment @@ -972,11 +972,11 @@ def _merge_array_annotations(self, others, sorting=None): for key in keys: try: self_ann = deepcopy(self.array_annotations[key]) - other_ann = np.concatenate([deepcopy(other.array_annotations[key]) for other in others]) if isinstance(self_ann, pq.Quantity): - other_ann.rescale(self_ann.units) - arr_ann = np.concatenate([self_ann, other_ann]) * self_ann.units + quantities = [self_ann] + [deepcopy(other.array_annotations[key]) for other in others] + arr_ann = _merge_quantity_arrays(quantities, axis=0) else: + other_ann = np.concatenate([deepcopy(other.array_annotations[key]) for other in others]) arr_ann = np.concatenate([self_ann, other_ann]) merged_array_annotations[key] = arr_ann[sorting] # Annotation only available in 'self', must be skipped diff --git a/neo/core/spiketrainlist.py b/neo/core/spiketrainlist.py index 887b028c4..a4f364f18 100644 --- a/neo/core/spiketrainlist.py +++ b/neo/core/spiketrainlist.py @@ -199,7 +199,16 @@ def _add_spiketrainlists(self, other, in_place=False): if self._spiketrain_metadata["t_stop"] != other._spiketrain_metadata["t_stop"]: raise ValueError("Incompatible t_stop") # todo: adjust t_stop of self and other as necessary - combined_spike_time_array = np.hstack((self._spike_time_array, other._spike_time_array)) + reference_units = self._spike_time_array.units + combined_spike_time_array = ( + np.hstack( + ( + self._spike_time_array.rescale(reference_units).magnitude, + other._spike_time_array.rescale(reference_units).magnitude, + ) + ) + * reference_units + ) combined_channel_id_array = np.hstack((self._channel_id_array, other._channel_id_array)) combined_channel_ids = set(list(self._all_channel_ids) + other._all_channel_ids) if len(combined_channel_ids) != (len(self._all_channel_ids) + len(other._all_channel_ids)): @@ -386,15 +395,10 @@ def multiplexed(self): else: channel_ids = [] spike_times = [] - dim = self._items[0].units.dimensionality for i, spiketrain in enumerate(self._items): if hasattr(spiketrain, "load"): # proxy object spiketrain = spiketrain.load() - if spiketrain.times.dimensionality.items() == dim.items(): - # no need to rescale - spike_times.append(spiketrain.times) - else: - spike_times.append(spiketrain.times.rescale(dim)) + spike_times.append(spiketrain.times.rescale(self._items[0].units).magnitude) if "channel_id" in spiketrain.annotations and isinstance(spiketrain.annotations["channel_id"], int): ch_id = spiketrain.annotations["channel_id"] else: diff --git a/neo/io/brainwaresrcio.py b/neo/io/brainwaresrcio.py index aaa7e69ac..9232aaba4 100755 --- a/neo/io/brainwaresrcio.py +++ b/neo/io/brainwaresrcio.py @@ -538,6 +538,9 @@ def _combine_spiketrains(self, spiketrains): if not spiketrains: return self._default_spiketrain.copy() + # Reader-created SpikeTrains are normalized to milliseconds by this method below. + # The source decoder returns raw millisecond values, so dropping Quantity metadata + # here and rebuilding the result in milliseconds preserves the file-format values. # get the times of the spiketrains and combine them waveforms = [itrain.waveforms for itrain in spiketrains] rawtrains = np.array(np.concatenate(spiketrains, axis=1)) diff --git a/neo/io/elphyio.py b/neo/io/elphyio.py index 590133af6..552bee633 100644 --- a/neo/io/elphyio.py +++ b/neo/io/elphyio.py @@ -3695,6 +3695,25 @@ def get_names(self): # -------------------------------------------------------- +def _stack_analog_signal_magnitudes(analogsignalarrays): + """Stack analog signal channels without converting their per-channel units.""" + return np.hstack([np.asarray(signal.magnitude) for signal in analogsignalarrays]) + + +def _serialize_analog_signal_channels(analogsignalarrays): + """Return Elphy channel metadata and RDATA bytes in each channel's native units.""" + if not analogsignalarrays: + return 0, 0, b"", [] + + analogsignals = _stack_analog_signal_magnitudes(analogsignalarrays).T + nbchan, nbpt = analogsignals.shape + analog_data = np.array(analogsignals.flatten("F"), dtype=np.float32) + data_format = "<" + str(analogsignals.size) + "f" + serialized_analog_data = struct.pack(data_format, *analog_data) + aa_units = [signal.units for signal in analogsignalarrays for _ in range(signal.shape[1])] + return nbchan, nbpt, serialized_analog_data, aa_units + + class ElphyIO(BaseIO): """ Class for reading from and writing to an Elphy file. @@ -3831,17 +3850,13 @@ def write_block(self, block): # print "\n\n--------------------------------------------\n" # print "write_block() - n_segments:",len(block.segments) for seg in block.segments: - analogsignals = 0 # init - nbchan = 0 - nbpt = 0 - chls = 0 + analogsignal_arrays = [] Dxu = 1e-8 # 0.0000001 Rxu = 1e8 # 10000000.0 X0uSpk = 0.0 CyberTime = 0.0 - aa_units = [] NbEv = [] - serialized_analog_data = "" + serialized_analog_data = b"" serialized_spike_data = "" # AnalogSignals # Neo signalarrays are 2D numpy array where each row is an array of samples for a @@ -3871,30 +3886,10 @@ def write_block(self, block): annotations, "analogsignal", asigar.annotations.items(), asigar.name, idx ) # array structure - _, chls = asigar.shape - # units - for _ in range(chls): - aa_units.append(asigar.units) Dxu = asigar.sampling_period Rxu = asigar.sampling_rate - if isinstance(analogsignals, np.ndarray): - analogsignals = np.hstack((analogsignals, asigar)) - else: - analogsignals = asigar # first time - # collect and reshape all analogsignals - if isinstance(analogsignals, np.ndarray): - # transpose matrix since in Neo channels are column-wise while in Elphy are - # row-wise - analogsignals = analogsignals.T - # get dimensions - nbchan, nbpt = analogsignals.shape - # serialize AnalogSignal - analog_data_fmt = "<" + str(analogsignals.size) + "f" - # serialized flattened numpy channels in 'F'ortran style - analog_data_64 = analogsignals.flatten("F") - # elphy normally uses float32 values (for performance reasons) - analog_data = np.array(analog_data_64, dtype=np.float32) - serialized_analog_data += struct.pack(analog_data_fmt, *analog_data) + analogsignal_arrays.append(asigar) + nbchan, nbpt, serialized_analog_data, aa_units = _serialize_analog_signal_channels(analogsignal_arrays) # SpikeTrains # Neo spiketrains are stored as a one-dimensional array of times # [ 0.11, 1.23, 2.34, 3.45, 4.56, 5.67, 6.78, 7.89 ... ] diff --git a/neo/test/coretest/test_base.py b/neo/test/coretest/test_base.py index 54387deb4..624dfc2ba 100644 --- a/neo/test/coretest/test_base.py +++ b/neo/test/coretest/test_base.py @@ -414,6 +414,33 @@ def test_merge_annotation__func__ndarray(self): assert_arrays_equal(ann1, ann1c) assert_arrays_equal(ann2, ann2c) + def test_merge_annotation__func__quantity(self): + ann1 = np.array([1, 2]) * pq.s + ann2 = np.array([500, 250]) * pq.ms + + result = merge_annotation(ann1, ann2) + + self.assertEqual(result.units, pq.s) + np.testing.assert_array_equal(result.magnitude, [1, 2, 0.5, 0.25]) + + with self.assertRaises(ValueError): + merge_annotation(ann1, np.array([1, 2]) * pq.m) + + def test_merge_annotation__func__uncertain_quantity(self): + ann1 = pq.UncertainQuantity([1, 2], units=pq.s, uncertainty=[0.1, 0.2]) + ann2 = pq.UncertainQuantity([500], units=pq.ms, uncertainty=[25]) + + result = merge_annotation(ann1, ann2) + + self.assertIsInstance(result, pq.UncertainQuantity) + self.assertEqual(result.units, pq.s) + np.testing.assert_array_equal(result.magnitude, [1, 2, 0.5]) + self.assertEqual(result.uncertainty.units, pq.s) + np.testing.assert_array_equal(result.uncertainty.magnitude, [0.1, 0.2, 0.025]) + + with self.assertRaises(ValueError): + merge_annotation(ann1, pq.UncertainQuantity([1], units=pq.m, uncertainty=[0.1])) + def test_merge_annotation__func__list(self): ann1 = [0, 1, 2] ann2 = [4, 5, 6] diff --git a/neo/test/coretest/test_dataobject.py b/neo/test/coretest/test_dataobject.py index 455f98060..aebb4b1ab 100644 --- a/neo/test/coretest/test_dataobject.py +++ b/neo/test/coretest/test_dataobject.py @@ -1,9 +1,10 @@ import copy +import unittest import numpy as np -import unittest +import quantities as pq -from neo.core.dataobject import DataObject, _normalize_array_annotations, ArrayDict +from neo.core.dataobject import ArrayDict, DataObject, _normalize_array_annotations class Test_DataObject(unittest.TestCase): @@ -12,6 +13,45 @@ def test(self): class Test_array_annotations(unittest.TestCase): + def test_merge_quantity_array_annotations(self): + first = DataObject([1, 2]) + first.array_annotate(quantity=[1, 2] * pq.s, plain=np.array([10, 20])) + second = DataObject([3, 4]) + second.array_annotate(quantity=[500, 250] * pq.ms, plain=np.array([30, 40])) + + merged = first._merge_array_annotations(second) + + self.assertEqual(merged["quantity"].units, pq.s) + np.testing.assert_array_equal(merged["quantity"].magnitude, [1, 2, 0.5, 0.25]) + self.assertEqual(merged["quantity"].shape, (4,)) + np.testing.assert_array_equal(merged["plain"], [10, 20, 30, 40]) + + second.array_annotations["quantity"] = [1, 2] * pq.m + with self.assertRaisesRegex(ValueError, "Could not merge array annotations"): + first._merge_array_annotations(second) + + def test_merge_uncertain_quantity_array_annotations(self): + first = DataObject([1, 2]) + first.array_annotate(quantity=pq.UncertainQuantity([1, 2], units=pq.s, uncertainty=[0.1, 0.2])) + second = DataObject([3]) + second.array_annotate(quantity=pq.UncertainQuantity([500], units=pq.ms, uncertainty=[25])) + + merged = first._merge_array_annotations(second)["quantity"] + + self.assertIsInstance(merged, pq.UncertainQuantity) + self.assertEqual(merged.units, pq.s) + np.testing.assert_array_equal(merged.magnitude, [1, 2, 0.5]) + self.assertEqual(merged.uncertainty.units, pq.s) + np.testing.assert_array_equal(merged.uncertainty.magnitude, [0.1, 0.2, 0.025]) + + second.array_annotations["quantity"] = [500] * pq.ms + with self.assertRaisesRegex(ValueError, "Could not merge array annotations"): + first._merge_array_annotations(second) + + second.array_annotations["quantity"] = pq.UncertainQuantity([1], units=pq.m, uncertainty=[0.1]) + with self.assertRaisesRegex(ValueError, "Could not merge array annotations"): + first._merge_array_annotations(second) + def test_check_arr_ann(self): # DataObject instance that handles checking datobj = DataObject([1, 2]) # Inherits from Quantity, so some data is required diff --git a/neo/test/coretest/test_epoch.py b/neo/test/coretest/test_epoch.py index 3a6dc72b3..4bf0467d4 100644 --- a/neo/test/coretest/test_epoch.py +++ b/neo/test/coretest/test_epoch.py @@ -201,6 +201,11 @@ def test_Epoch_merge(self): ) assert_neo_object_is_compliant(epcres) + self.assertEqual(epcres.times.units, pq.ms) + self.assertEqual(epcres.durations.units, pq.us) + np.testing.assert_allclose(epcres.times.magnitude, [1.1, 1.5, 1.7, 0.0021, 0.0025, 0.0027]) + np.testing.assert_allclose(epcres.durations.magnitude, [20, 40, 60, 3000, 5000, 7000]) + assert_array_equal(epcres.labels, epctarg.labels) assert_same_sub_schema(epctarg, epcres) # Remove this, when array_annotations are added to assert_same_sub_schema assert_arrays_equal(epcres.array_annotations["index"], np.array([10, 11, 12, 0, 1, 2])) diff --git a/neo/test/coretest/test_event.py b/neo/test/coretest/test_event.py index 8743ca676..45ae01fe8 100644 --- a/neo/test/coretest/test_event.py +++ b/neo/test/coretest/test_event.py @@ -516,6 +516,9 @@ def test_Event_merge(self): ) assert_neo_object_is_compliant(evtres) + self.assertEqual(evtres.times.units, pq.ms) + np.testing.assert_allclose(evtres.times.magnitude, [1.1, 1.5, 1.7, 0.0021, 0.0025, 0.0027]) + assert_array_equal(evtres.labels, evttarg.labels) assert_same_sub_schema(evttarg, evtres) # Remove this, when array_annotations are added to assert_same_sub_schema assert_arrays_equal(evtres.array_annotations["index"], np.array([10, 11, 12, 0, 1, 2])) diff --git a/neo/test/coretest/test_irregularysampledsignal.py b/neo/test/coretest/test_irregularysampledsignal.py index 9fde8a578..49740c00a 100644 --- a/neo/test/coretest/test_irregularysampledsignal.py +++ b/neo/test/coretest/test_irregularysampledsignal.py @@ -978,6 +978,16 @@ def test_concatenate_simple(self): continue self.assertEqual(getattr(signal1, attr[0], None), getattr(result, attr[0], None)) + def test_concatenate_rescales_time_units(self): + signal1 = IrregularlySampledSignal(signal=[1, 2] * pq.mV, times=[1, 2] * pq.s) + signal2 = IrregularlySampledSignal(signal=[3, 4] * pq.mV, times=[3000, 4000] * pq.ms) + + result = signal1.concatenate(signal2) + + self.assertEqual(result.times.units, pq.s) + assert_array_equal(result.times.magnitude, [1, 2, 3, 4]) + assert_array_equal(result.magnitude, [[1], [2], [3], [4]]) + def test_concatenate_no_overlap(self): signal1 = IrregularlySampledSignal(signal=[0, 1, 2, 3] * pq.s, times=range(4) * pq.s) signal2 = IrregularlySampledSignal(signal=[4, 5, 6] * pq.s, times=range(4, 7) * pq.s) diff --git a/neo/test/coretest/test_spiketrain.py b/neo/test/coretest/test_spiketrain.py index f483f3fce..0ff81bed0 100644 --- a/neo/test/coretest/test_spiketrain.py +++ b/neo/test/coretest/test_spiketrain.py @@ -1161,6 +1161,67 @@ def test_merge_multiple(self): ) self.assertIsInstance(result.array_annotations, ArrayDict) + def test_merge_quantity_array_annotations_with_unit_conversion(self): + first = SpikeTrain( + [3, 1] * pq.ms, + t_stop=10 * pq.ms, + array_annotations={"quantity": [10, 20] * pq.s, "plain": [30, 10]}, + ) + second = SpikeTrain( + [2] * pq.ms, + t_stop=10 * pq.ms, + array_annotations={"quantity": [500] * pq.ms, "plain": [20]}, + ) + third = SpikeTrain( + [0.5] * pq.ms, + t_stop=10 * pq.ms, + array_annotations={"quantity": [250000] * pq.us, "plain": [5]}, + ) + + result = first.merge(second, third) + + self.assertEqual(result.array_annotations["quantity"].units, pq.s) + self.assertEqual(result.array_annotations["quantity"].shape, (4,)) + np.testing.assert_array_equal(result.array_annotations["quantity"].magnitude, [0.25, 20, 0.5, 10]) + np.testing.assert_array_equal(result.array_annotations["plain"], [5, 10, 20, 30]) + + def test_merge_uncertain_quantity_array_annotations_with_unit_conversion(self): + first = SpikeTrain( + [3, 1] * pq.ms, + t_stop=10 * pq.ms, + array_annotations={ + "uncertain": pq.UncertainQuantity([10, 20], units=pq.s, uncertainty=[0.1, 0.2]), + }, + ) + second = SpikeTrain( + [2] * pq.ms, + t_stop=10 * pq.ms, + array_annotations={"uncertain": pq.UncertainQuantity([500], units=pq.ms, uncertainty=[25])}, + ) + third = SpikeTrain( + [0.5] * pq.ms, + t_stop=10 * pq.ms, + array_annotations={"uncertain": pq.UncertainQuantity([250000], units=pq.us, uncertainty=[12500])}, + ) + + result = first.merge(second, third) + merged = result.array_annotations["uncertain"] + + self.assertIsInstance(merged, pq.UncertainQuantity) + self.assertEqual(merged.units, pq.s) + self.assertEqual(merged.shape, (4,)) + np.testing.assert_array_equal(merged.magnitude, [0.25, 20, 0.5, 10]) + self.assertEqual(merged.uncertainty.units, pq.s) + np.testing.assert_allclose(merged.uncertainty.magnitude, [0.0125, 0.2, 0.025, 0.1]) + + incompatible = SpikeTrain( + [4] * pq.ms, + t_stop=10 * pq.ms, + array_annotations={"uncertain": pq.UncertainQuantity([1], units=pq.m, uncertainty=[0.1])}, + ) + with self.assertRaises(ValueError): + first.merge(incompatible) + def test_merge_with_waveforms(self): # Array annotations merge warning was already tested, can be ignored now with warnings.catch_warnings(record=True) as w: @@ -1200,9 +1261,9 @@ def test_merge_multiple_with_waveforms(self): assert_arrays_equal( result.waveforms, - np.vstack([st.waveforms.rescale(self.train1.waveforms.units) for st in (self.train1, train3, train4)])[ - sorting - ] + np.vstack( + [st.waveforms.rescale(self.train1.waveforms.units).magnitude for st in (self.train1, train3, train4)] + )[sorting] * self.train1.waveforms.units, ) diff --git a/neo/test/coretest/test_spiketrainlist.py b/neo/test/coretest/test_spiketrainlist.py index e3202a17c..c9ce55b33 100644 --- a/neo/test/coretest/test_spiketrainlist.py +++ b/neo/test/coretest/test_spiketrainlist.py @@ -196,6 +196,28 @@ def test_iadd_spiketrainlists(self): self.assertEqual(len(a), 8) self.assertEqual(id(c), id(a)) + first = SpikeTrainList.from_spike_time_array( + [1, 3], + [10, 10], + all_channel_ids=[10], + units=pq.s, + t_start=0 * pq.s, + t_stop=10 * pq.s, + ) + second = SpikeTrainList.from_spike_time_array( + [500, 1500], + [11, 11], + all_channel_ids=[11], + units=pq.ms, + t_start=0 * pq.ms, + t_stop=10000 * pq.ms, + ) + combined = first + second + channel_ids, spike_times = combined.multiplexed + assert_array_equal(channel_ids, [10, 10, 11, 11]) + self.assertEqual(spike_times.units, pq.s) + assert_array_equal(spike_times.magnitude, [1, 3, 0.5, 1.5]) + def test_add_list_of_spiketrains(self): """Adding a list of SpikeTrains to a SpikeTrainList should return a new SpikeTrainList""" extended_stl = self.stl_from_array + [ @@ -205,6 +227,7 @@ def test_add_list_of_spiketrains(self): ] self.assertIsInstance(extended_stl, SpikeTrainList) self.assertEqual(len(extended_stl), 7) + self.assertNotEqual(id(extended_stl), id(self.stl_from_array)) extended_stl = self.stl_from_obj_list_incl_proxy + [ @@ -215,6 +238,20 @@ def test_add_list_of_spiketrains(self): self.assertIsInstance(extended_stl, SpikeTrainList) self.assertEqual(len(extended_stl), 7) + def test_multiplexed_rescales_object_backed_spiketrains(self): + trains = [ + SpikeTrain([1, 3] * pq.s, t_start=0 * pq.s, t_stop=10 * pq.s, channel_id=10), + SpikeTrain([500, 1500] * pq.ms, t_start=0 * pq.ms, t_stop=10000 * pq.ms, channel_id=11), + SpikeTrain([1000000, 2500000] * pq.us, t_start=0 * pq.us, t_stop=10000000 * pq.us, channel_id=12), + ] + stl = SpikeTrainList(items=trains) + + channel_ids, spike_times = stl.multiplexed + + assert_array_equal(channel_ids, [10, 10, 11, 11, 12, 12]) + self.assertEqual(spike_times.units, pq.s) + assert_array_equal(spike_times.magnitude, [1, 3, 0.5, 1.5, 1, 2.5]) + def test_iadd_list_of_spiketrains(self): """Adding a list of SpikeTrains to a SpikeTrainList in place should return the original SpikeTrainList""" @@ -325,3 +362,8 @@ def test_multiplexed(self): assert type(channel_id_array) == np.ndarray assert_array_equal(channel_id_array, np.array([101, 101, 101, 101, 102, 102, 103, 103])) assert_array_equal(spike_time_array, np.array([0.5, 0.6, 23.6, 99.2, 0.7, 11.2, 1.1, 88.5]) * pq.ms) + + channel_ids, spike_times = self.stl_from_obj_list.multiplexed + assert_array_equal(channel_ids, [101, 101, 101, 101, 102, 102, 103, 103]) + self.assertEqual(spike_times.units, pq.ms) + assert_array_equal(spike_times.magnitude, [0.5, 0.6, 23.6, 99.2, 0.7, 11.2, 1.1, 88.5]) diff --git a/neo/test/iotest/test_brainwaresrcio.py b/neo/test/iotest/test_brainwaresrcio.py index a4d67880b..6bc8f5731 100644 --- a/neo/test/iotest/test_brainwaresrcio.py +++ b/neo/test/iotest/test_brainwaresrcio.py @@ -324,6 +324,23 @@ def test_against_reference(self): raise +class BrainwareSrcIOHelperTest(unittest.TestCase): + def test_raw_spike_records_are_milliseconds_before_quantity_construction(self): + reader = BrainwareSrcIO() + reader._default_spiketrain = reader._init_default_spiketrain.copy() + raw_spikes = [ + (np.array([1.0, 3.0]), np.zeros((2, 1, 2), dtype=np.int8), np.array([2, 3], dtype=np.uint8)), + (np.array([5.0]), np.zeros((1, 1, 2), dtype=np.int8), np.array([4], dtype=np.uint8)), + ] + + train = reader._combine_spiketrains(raw_spikes) + + self.assertEqual(train.units, pq.ms) + np.testing.assert_array_equal(train.magnitude, [1, 3, 5]) + self.assertEqual(train.annotations["trig2"].units, pq.ms) + np.testing.assert_array_equal(train.annotations["trig2"].magnitude, [2, 3, 4]) + + if __name__ == "__main__": logger = logging.getLogger(BrainwareSrcIO.__module__ + "." + BrainwareSrcIO.__name__) logger.setLevel(100) diff --git a/neo/test/iotest/test_elphyio.py b/neo/test/iotest/test_elphyio.py index 60062f192..0b3baa67f 100644 --- a/neo/test/iotest/test_elphyio.py +++ b/neo/test/iotest/test_elphyio.py @@ -2,9 +2,15 @@ Tests of neo.io.elphyo """ +import struct import unittest +import quantities as pq + +from neo import AnalogSignal + from neo.io import ElphyIO +from neo.io.elphyio import _serialize_analog_signal_channels from neo.test.iotest.common_io_test import BaseTestIO @@ -29,5 +35,19 @@ def test_read_data(self): self.assertTrue(any(list(bl.segments[0].size.values()))) +class TestElphyIOSerialization(unittest.TestCase): + def test_analog_signal_serialization_keeps_each_channel_unit(self): + signals = [ + AnalogSignal([[1], [2]] * pq.mV, sampling_rate=1000 * pq.Hz), + AnalogSignal([[1], [2]] * pq.V, sampling_rate=1000 * pq.Hz), + ] + + nbchan, nbpt, serialized, units = _serialize_analog_signal_channels(signals) + + self.assertEqual((nbchan, nbpt), (2, 2)) + self.assertEqual(units, [pq.mV, pq.V]) + self.assertEqual(struct.unpack("<4f", serialized), (1.0, 1.0, 2.0, 2.0)) + + if __name__ == "__main__": unittest.main()