Skip to content

Commit afde97a

Browse files
committed
Add a warning to avoid signals with the same name
2 parents dbbdaf8 + 34d4db8 commit afde97a

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

neo/io/nwbio.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@
7373
"experiment_description", "session_id", "institution", "keywords", "notes",
7474
"pharmacology", "protocol", "related_publications", "slices", "source_script",
7575
"source_script_file_name", "data_collection", "surgery", "virus", "stimulus_notes",
76-
"lab", "session_description"
76+
"lab", "session_description",
77+
"rec_datetime",
7778
)
7879

7980
POSSIBLE_JSON_FIELDS = (
@@ -273,7 +274,7 @@ def read_all_blocks(self, lazy=False, **kwargs):
273274
"session_start_time"]
274275
if "file_create_date" in self.global_block_metadata:
275276
self.global_block_metadata["file_datetime"] = self.global_block_metadata[
276-
"file_create_date"]
277+
"rec_datetime"]
277278

278279
self._blocks = {}
279280
self._read_acquisition_group(lazy=lazy)
@@ -435,11 +436,13 @@ def write_all_blocks(self, blocks, **kwargs):
435436
annotations["session_description"] = blocks[0].description or self.filename
436437
# todo: concatenate descriptions of multiple blocks if different
437438
if "session_start_time" not in annotations:
438-
raise Exception("Writing to NWB requires an annotation 'session_start_time'")
439+
annotations["session_start_time"] = blocks[0].rec_datetime
440+
if annotations["session_start_time"] is None:
441+
raise Exception("Writing to NWB requires an annotation 'session_start_time'")
442+
self.annotations = {"rec_datetime": "rec_datetime"}
443+
self.annotations["rec_datetime"] = blocks[0].rec_datetime
439444
# todo: handle subject
440-
# todo: store additional Neo annotations somewhere in NWB file
441445
nwbfile = NWBFile(**annotations)
442-
443446
assert self.nwb_file_mode in ('w',) # possibly expand to 'a'ppend later
444447
if self.nwb_file_mode == "w" and os.path.exists(self.filename):
445448
os.remove(self.filename)
@@ -518,7 +521,7 @@ def _write_segment(self, nwbfile, segment, electrodes):
518521
signal.name = "%s %s %i" % (signal.name, segment.name, i)
519522
logging.warning("Warning signal name exists. New name: %s" % (signal.name))
520523
if not signal.name:
521-
signal.name = "%s : analogsignal%d" % (segment.name, i)
524+
signal.name = "%s : analogsignal%d %i" % (segment.name, i, i)
522525
self._write_signal(nwbfile, signal, electrodes)
523526

524527
for i, train in enumerate(segment.spiketrains):
@@ -632,7 +635,8 @@ class AnalogSignalProxy(BaseAnalogSignalProxy):
632635
common_metadata_fields = (
633636
# fields that are the same for all TimeSeries subclasses
634637
"comments", "description", "unit", "starting_time", "timestamps", "rate",
635-
"data", "starting_time_unit", "timestamps_unit", "electrode"
638+
"data", "starting_time_unit", "timestamps_unit", "electrode",
639+
"stream_id",
636640
)
637641

638642
def __init__(self, timeseries, nwb_group):

neo/test/iotest/test_nwbio.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,18 @@ def test_roundtrip(self):
6363
for seg in blk.segments: # AnalogSignal objects
6464

6565
# 3 Neo AnalogSignals
66-
a = AnalogSignal(np.random.randn(44, num_chan) * pq.nA,
66+
a = AnalogSignal(name='Signal_a %s' % (seg.name),
67+
signal=np.random.randn(44, num_chan) * pq.nA,
6768
sampling_rate=10 * pq.kHz,
6869
t_start=50 * pq.ms)
69-
b = AnalogSignal(np.random.randn(64, num_chan) * pq.mV,
70+
b = AnalogSignal(name='Signal_b %s' % (seg.name),
71+
signal=np.random.randn(64, num_chan) * pq.mV,
7072
sampling_rate=8 * pq.kHz,
7173
t_start=40 * pq.ms)
72-
c = AnalogSignal(np.random.randn(33, num_chan) * pq.uA,
74+
c = AnalogSignal(name='Signal_c %s' % (seg.name),
75+
signal=np.random.randn(33, num_chan) * pq.uA,
7376
sampling_rate=10 * pq.kHz,
7477
t_start=120 * pq.ms)
75-
7678
# 2 Neo IrregularlySampledSignals
7779
d = IrregularlySampledSignal(np.arange(7.0) * pq.ms,
7880
np.random.randn(7, num_chan) * pq.mV)
@@ -83,7 +85,8 @@ def test_roundtrip(self):
8385
# todo: add waveforms
8486

8587
# 1 Neo Event
86-
evt = Event(times=np.arange(0, 30, 10) * pq.ms,
88+
evt = Event(name='Event',
89+
times=np.arange(0, 30, 10) * pq.ms,
8790
labels=np.array(['ev0', 'ev1', 'ev2']))
8891

8992
# 2 Neo Epochs
@@ -228,17 +231,17 @@ def test_roundtrip_with_annotations(self):
228231

229232
nwbfile = pynwb.NWBHDF5IO(test_file_name, mode="r").read()
230233

231-
self.assertIsInstance(nwbfile.acquisition["response"], pynwb.icephys.CurrentClampSeries)
232-
self.assertIsInstance(nwbfile.stimulus["stimulus"],
234+
self.assertIsInstance(nwbfile.acquisition[response.name], pynwb.icephys.CurrentClampSeries)
235+
self.assertIsInstance(nwbfile.stimulus[stimulus.name],
233236
pynwb.icephys.CurrentClampStimulusSeries)
234-
self.assertEqual(nwbfile.acquisition["response"].bridge_balance,
237+
self.assertEqual(nwbfile.acquisition[response.name].bridge_balance,
235238
response_annotations["nwb:bridge_balance"])
236239

237240
ior = NWBIO(filename=test_file_name, mode='r')
238241
retrieved_block = ior.read_all_blocks()[0]
239242

240-
original_response = original_block.segments[0].filter(name="response")[0]
241-
retrieved_response = retrieved_block.segments[0].filter(name="response")[0]
243+
original_response = original_block.segments[0].filter(name=response.name)[0]
244+
retrieved_response = retrieved_block.segments[0].filter(name=response.name)[0]
242245
for attr_name in ("name", "units", "sampling_rate", "t_start"):
243246
retrieved_attribute = getattr(retrieved_response, attr_name)
244247
original_attribute = getattr(original_response, attr_name)

0 commit comments

Comments
 (0)