|
17 | 17 | from neo.core import AnalogSignal, SpikeTrain, Event, Epoch, IrregularlySampledSignal, Segment, \ |
18 | 18 | Block |
19 | 19 |
|
| 20 | +from neo.rawio.examplerawio import ExampleRawIO |
| 21 | +from neo.io.proxyobjects import (AnalogSignalProxy, SpikeTrainProxy, EventProxy, EpochProxy) |
| 22 | + |
20 | 23 | try: |
21 | 24 | import pynwb |
22 | 25 | from neo.io.nwbio import NWBIO |
@@ -250,6 +253,62 @@ def test_roundtrip_with_annotations(self): |
250 | 253 |
|
251 | 254 | os.remove(test_file_name) |
252 | 255 |
|
| 256 | + def test_write_proxy_objects(self): |
| 257 | + test_file_name = self.local_test_dir / "test_round_trip_with_annotations.nwb" |
| 258 | + |
| 259 | + # generate dummy IO as basis for ProxyObjects |
| 260 | + self.proxy_reader = ExampleRawIO(filename='my_filename.fake') |
| 261 | + self.proxy_reader.parse_header() |
| 262 | + |
| 263 | + # generate test structure with proxy objects |
| 264 | + original_block = Block(name='myblock', session_start_time=datetime.now().astimezone(), |
| 265 | + session_description=str(test_file_name), |
| 266 | + identifier=str(test_file_name)) |
| 267 | + seg = Segment(name='mysegment') |
| 268 | + original_block.segments.append(seg) |
| 269 | + |
| 270 | + # create proxy objects |
| 271 | + proxy_anasig = AnalogSignalProxy(rawio=self.proxy_reader, stream_index=0, |
| 272 | + inner_stream_channels=None, block_index=0, seg_index=0,) |
| 273 | + proxy_anasig.segment = seg |
| 274 | + seg.analogsignals.append(proxy_anasig) |
| 275 | + |
| 276 | + proxy_sptr = SpikeTrainProxy(rawio=self.proxy_reader, spike_channel_index=0, block_index=0, |
| 277 | + seg_index=0) |
| 278 | + proxy_sptr.segment = seg |
| 279 | + seg.spiketrains.append(proxy_sptr) |
| 280 | + |
| 281 | + proxy_event = EventProxy(rawio=self.proxy_reader, event_channel_index=0, block_index=0, |
| 282 | + seg_index=0) |
| 283 | + proxy_event.segment = seg |
| 284 | + seg.events.append(proxy_event) |
| 285 | + |
| 286 | + proxy_epoch = EpochProxy(rawio=self.proxy_reader, event_channel_index=1, block_index=0, |
| 287 | + seg_index=0) |
| 288 | + proxy_epoch.segment = seg |
| 289 | + seg.epochs.append(proxy_epoch) |
| 290 | + |
| 291 | + original_block.create_relationship() |
| 292 | + |
| 293 | + iow = NWBIO(filename=test_file_name, mode='w') |
| 294 | + |
| 295 | + # writing data via proxyobjects |
| 296 | + iow.write_all_blocks([original_block]) |
| 297 | + |
| 298 | + # checking written data |
| 299 | + ior = NWBIO(filename=test_file_name, mode='r') |
| 300 | + retrieved_block = ior.read_all_blocks()[0] |
| 301 | + |
| 302 | + for original_segment, retrieved_segment in zip(original_block.segments, |
| 303 | + retrieved_block.segments): |
| 304 | + assert_array_equal(original_segment.analogsignals[0].load().magnitude, |
| 305 | + retrieved_segment.analogsignals[0].magnitude) |
| 306 | + assert_array_equal(original_segment.spiketrains[0].load().magnitude, |
| 307 | + retrieved_segment.spiketrains[0].magnitude) |
| 308 | + assert_array_equal(original_segment.events[0].load().magnitude, |
| 309 | + retrieved_segment.events[0].magnitude) |
| 310 | + assert_array_equal(original_segment.epochs[0].load().magnitude, |
| 311 | + retrieved_segment.epochs[0].magnitude) |
253 | 312 |
|
254 | 313 | if __name__ == "__main__": |
255 | 314 | if HAVE_PYNWB: |
|
0 commit comments