diff --git a/neo/test/coretest/test_analogsignal.py b/neo/test/coretest/test_analogsignal.py index 44c93b49a..f3e9c0b4b 100644 --- a/neo/test/coretest/test_analogsignal.py +++ b/neo/test/coretest/test_analogsignal.py @@ -286,7 +286,7 @@ def test__repr(self): def test__pretty(self): for i, signal in enumerate(self.signals): prepr = pretty(signal) - targ = (('AnalogSignal with %d channels of length %d; units %s; datatype %s \n' + targ = (('AnalogSignal with %d channels of length %d; units %s; datatype %s\n' '' % (signal.shape[1], signal.shape[0], signal.units.dimensionality.unicode, signal.dtype)) + ('annotations: %s\n' % signal.annotations) diff --git a/neo/test/coretest/test_irregularysampledsignal.py b/neo/test/coretest/test_irregularysampledsignal.py index fd05f5d07..5ec4742e1 100644 --- a/neo/test/coretest/test_irregularysampledsignal.py +++ b/neo/test/coretest/test_irregularysampledsignal.py @@ -806,7 +806,7 @@ def test__divide_signal_by_const_should_preserve_data_complement(self): def test__pretty(self): res = pretty(self.signal1) signal = self.signal1 - targ = (("IrregularlySampledSignal with %d channels of length %d; units %s; datatype %s \n" + targ = (("IrregularlySampledSignal with %d channels of length %d; units %s; datatype %s\n" "" % (signal.shape[1], signal.shape[0], signal.units.dimensionality.unicode, signal.dtype)) + ("name: '{}'\ndescription: '{}'\n".format(signal.name, signal.description)) diff --git a/neo/test/iotest/test_bci2000.py b/neo/test/iotest/test_bci2000io.py similarity index 100% rename from neo/test/iotest/test_bci2000.py rename to neo/test/iotest/test_bci2000io.py diff --git a/neo/test/iotest/test_medio.py b/neo/test/iotest/test_medio.py index 9f75d7d54..f91485d8f 100644 --- a/neo/test/iotest/test_medio.py +++ b/neo/test/iotest/test_medio.py @@ -5,18 +5,21 @@ import pathlib import unittest +import quantities as pq +import numpy as np + from neo.io.medio import MedIO from neo.test.iotest.common_io_test import BaseTestIO -from neo.test.iotest.tools import get_test_file_full_path -from neo.io.proxyobjects import (AnalogSignalProxy, - SpikeTrainProxy, EventProxy, EpochProxy) -from neo import (AnalogSignal, SpikeTrain) -import quantities as pq -import numpy as np +try: + import dhn_med_py + HAVE_DHN_MED = True +except ImportError: + HAVE_DHN_MED = False -# This run standard tests, this is mandatory for all IOs +# This runs standard tests, this is mandatory for all IOs +@unittest.skipUnless(HAVE_DHN_MED, "requires dhn_med_py package and all its dependencies") class TestMedIO(BaseTestIO, unittest.TestCase, ): ioclass = MedIO entities_to_download = ['med'] @@ -31,22 +34,22 @@ def setUp(self): self.dirname = self.get_local_path('med/sine_waves.medd') self.dirname2 = self.get_local_path('med/test.medd') self.password = 'L2_password' - + def test_read_segment_lazy(self): - + r = MedIO(self.dirname, self.password) seg = r.read_segment(lazy=False) - + # There will only be one analogsignal in this reading self.assertEqual(len(seg.analogsignals), 1) # Test that the correct number of samples are read, 5760000 samps for 3 channels self.assertEqual(seg.analogsignals[0].shape[0], 5760000) self.assertEqual(seg.analogsignals[0].shape[1], 3) - + # Test the first sample value of all 3 channels, which are # known to be [-1, -4, -4] np.testing.assert_array_equal(seg.analogsignals[0][0][:3], [-1, -4, -4]) - + for anasig in seg.analogsignals: self.assertNotEqual(anasig.size, 0) for st in seg.spiketrains: @@ -61,61 +64,61 @@ def test_read_segment_lazy(self): assert ev.name is not None for ep in seg.epochs: assert ep.name is not None - + r.close() def test_read_block(self): - + r = MedIO(self.dirname, self.password) bl = r.read_block(lazy=True) self.assertTrue(bl.annotations) - + for count, seg in enumerate(bl.segments): assert seg.name == 'Seg #' + str(count) + ' Block #0' - + for anasig in seg.analogsignals: assert anasig.name is not None - + # Verify that the block annotations from the MED session are # read properly. There are a lot of annotations, so we'll just # spot-check a couple of them. assert(bl.annotations['metadata']['recording_country'] == 'United States') assert(bl.annotations['metadata']['AC_line_frequency'] == 60.0) - + r.close() - + def test_read_segment_with_time_slice(self): """ Test loading of a time slice and check resulting times """ r = MedIO(self.dirname, self.password) seg = r.read_segment(time_slice=None) - + # spike and epoch timestamps are not being read self.assertEqual(len(seg.spiketrains), 0) self.assertEqual(len(seg.epochs), 1) self.assertEqual(len(seg.epochs[0]), 0) - + # Test for 180 events (1 per second for 3 minute recording) self.assertEqual(len(seg.events), 1) self.assertEqual(len(seg.events[0]), 180) - + for asig in seg.analogsignals: self.assertEqual(asig.shape[0], 5760000) n_channels = sum(a.shape[-1] for a in seg.analogsignals) self.assertEqual(n_channels, 3) - + t_start, t_stop = 500 * pq.ms, 800 * pq.ms seg = r.read_segment(time_slice=(t_start, t_stop)) - + # Test that 300 ms were read, which at 32 kHz, is 9600 samples self.assertAlmostEqual(seg.analogsignals[0].shape[0], 9600, delta=1.) # Test that it read from 3 channels self.assertEqual(seg.analogsignals[0].shape[1], 3) - + self.assertAlmostEqual(seg.t_start.rescale(t_start.units), t_start, delta=5.) self.assertAlmostEqual(seg.t_stop.rescale(t_stop.units), t_stop, delta=5.) - + r.close() diff --git a/neo/test/iotest/test_plexon2io.py b/neo/test/iotest/test_plexon2io.py index 97c09b287..81d076d16 100644 --- a/neo/test/iotest/test_plexon2io.py +++ b/neo/test/iotest/test_plexon2io.py @@ -10,6 +10,14 @@ from neo.test.rawiotest.test_plexon2rawio import TestPlexon2RawIO +try: + from neo.rawio.plexon2rawio.pypl2 import pypl2lib + HAVE_PYPL2 = True +except (ImportError, TimeoutError): + HAVE_PYPL2 = False + + +@unittest.skipUnless(HAVE_PYPL2, "requires pypl package and all its dependencies") class TestPlexon2IO(BaseTestIO, unittest.TestCase): entities_to_download = TestPlexon2RawIO.entities_to_download entities_to_test = TestPlexon2RawIO.entities_to_test diff --git a/neo/test/rawiotest/test_cedrawio.py b/neo/test/rawiotest/test_cedrawio.py index 50a0e4beb..ea94ae2a0 100644 --- a/neo/test/rawiotest/test_cedrawio.py +++ b/neo/test/rawiotest/test_cedrawio.py @@ -4,6 +4,15 @@ from neo.test.rawiotest.common_rawio_test import BaseTestRawIO +try: + import sonpy + HAVE_SONPY = True +except ImportError: + HAVE_SONPY = False + + +# This runs standard tests, this is mandatory for all IOs +@unittest.skipUnless(HAVE_SONPY, "requires sonpy package and all its dependencies") class TestCedRawIO(BaseTestRawIO, unittest.TestCase, ): rawioclass = CedRawIO diff --git a/neo/test/rawiotest/test_medrawio.py b/neo/test/rawiotest/test_medrawio.py index 7118cacfd..b2fd97c59 100644 --- a/neo/test/rawiotest/test_medrawio.py +++ b/neo/test/rawiotest/test_medrawio.py @@ -5,49 +5,58 @@ from neo.test.rawiotest.common_rawio_test import BaseTestRawIO +try: + import dhn_med_py + HAVE_DHN_MED = True +except ImportError: + HAVE_DHN_MED = False + + +# This runs standard tests, this is mandatory for all IOs +@unittest.skipUnless(HAVE_DHN_MED, "requires dhn_med_py package and all its dependencies") class TestMedRawIO(BaseTestRawIO, unittest.TestCase, ): rawioclass = MedRawIO entities_to_download = [ 'med' ] entities_to_test = ['med/sine_waves.medd', 'med/test.medd'] - + def test_close(self): - + filename = self.get_local_path('med/sine_waves.medd') - + raw_io1 = MedRawIO(filename, password='L2_password') raw_io1.parse_header() raw_io1.close() - + raw_io2 = MedRawIO(filename, password='L2_password') raw_io2.parse_header() raw_io2.close() - - + + def test_scan_med_directory(self): - + filename = self.get_local_path('med/sine_waves.medd') - + rawio = MedRawIO(filename, password='L2_password') rawio.parse_header() - + # Test that correct metadata and boundaries are extracted # from the MED session. We know the correct answers since # we generated the test files. self.assertEqual(rawio.signal_streams_count(), 1) self.assertEqual(rawio._segment_t_start(0, 0), 0) self.assertEqual(rawio._segment_t_stop(0, 0), 180) - + # Verify it found all 3 channels self.assertEqual(rawio.num_channels_in_session, 3) self.assertEqual(rawio.header['signal_channels'].size, 3) - + # Verify if found the names of the 3 channels self.assertEqual(rawio.header['signal_channels'][0][0], 'CSC_0001') self.assertEqual(rawio.header['signal_channels'][1][0], 'CSC_0002') self.assertEqual(rawio.header['signal_channels'][2][0], 'CSC_0003') - + # Read first 3 seconds of data from all channels raw_chunk = rawio.get_analogsignal_chunk(block_index=0, seg_index=0, @@ -55,11 +64,11 @@ def test_scan_med_directory(self): i_stop=96000, stream_index=0, channel_indexes=None) - + # Test the first sample value of all 3 channels, which are # known to be [-1, -4, -4] np.testing.assert_array_equal(raw_chunk[0][:3], [-1, -4, -4]) - + # Read 1 second of data from the second channel raw_chunk = rawio.get_analogsignal_chunk(block_index=0, seg_index=0, @@ -67,28 +76,28 @@ def test_scan_med_directory(self): i_stop=32000, stream_index=0, channel_indexes=[1]) - + # Test known first sample of second channel: [-4] self.assertEqual(raw_chunk[0][0], -4) - + rawio.close() - + # Test on second test dataset, test.medd. filename = self.get_local_path('med/test.medd') - + rawio = MedRawIO(filename, password='L2_password') rawio.parse_header() - + # Test that correct metadata and boundaries are extracted # from the MED session. We know the correct answers since # we generated the test files. - + # For this dataset, there are 3 continuous data ranges, with # approximately 10 seconds between the ranges. # There are 3 channels, two with a frequency of 1000 Hz and one # with a frequency of 5000 Hz. self.assertEqual(rawio.signal_streams_count(), 2) - + # Segment 0 self.assertEqual(rawio._segment_t_start(0, 0), 0) self.assertEqual(rawio._segment_t_stop(0, 0), 39.8898) @@ -98,16 +107,16 @@ def test_scan_med_directory(self): # Segment 2 self.assertEqual(rawio._segment_t_start(0, 2), 97.242057) self.assertEqual(rawio._segment_t_stop(0, 2), 180.016702) - + # Verify it found all 3 channels. self.assertEqual(rawio.num_channels_in_session, 3) self.assertEqual(rawio.header['signal_channels'].size, 3) - + # Verity if found the names of the 3 channels self.assertEqual(rawio.header['signal_channels'][0][0], '5k_ch1') self.assertEqual(rawio.header['signal_channels'][1][0], '1k_ch1') self.assertEqual(rawio.header['signal_channels'][2][0], '1k_ch2') - + # Read first 3 seconds of data from the first channel (5k_ch1) raw_chunk = rawio.get_analogsignal_chunk(block_index=0, seg_index=0, @@ -115,14 +124,14 @@ def test_scan_med_directory(self): i_stop=15000, stream_index=0, channel_indexes=None) - + # Test the first three sample values returned, which are # known to be [-80, -79, -78] self.assertEqual(raw_chunk[0][0], -80) self.assertEqual(raw_chunk[1][0], -79) self.assertEqual(raw_chunk[2][0], -78) - - + + # Read first 3 seconds of data from the second channel and third # channels (1k_ch1 and 1k_ch2) raw_chunk = rawio.get_analogsignal_chunk(block_index=0, @@ -131,11 +140,11 @@ def test_scan_med_directory(self): i_stop=3000, stream_index=1, channel_indexes=None) - + # Test first sample returned of both channels, which are known # to be [-79, -80] np.testing.assert_array_equal(raw_chunk[0][:2], [-79, -80]) - + # Read first 3 seconds of data from the second segment of the first channel (5k_ch1) raw_chunk = rawio.get_analogsignal_chunk(block_index=0, seg_index=1, @@ -143,15 +152,15 @@ def test_scan_med_directory(self): i_stop=15000, stream_index=0, channel_indexes=None) - + # Test the first three sample values returned, which are # known to be [22, 23, 24] self.assertEqual(raw_chunk[0][0], 22) self.assertEqual(raw_chunk[1][0], 23) self.assertEqual(raw_chunk[2][0], 24) - + self.assertEqual(len(rawio.header['event_channels']), 2) - + # Verify that there are 5 events in the dataset. # They are 3 "Note" type events, and 2 "NlxP", or neuralynx, type events. # The first segment has one event, and the second and third segments @@ -159,39 +168,39 @@ def test_scan_med_directory(self): self.assertEqual(rawio.event_count(0, 0, 0), 1) self.assertEqual(rawio.event_count(0, 1, 0), 2) self.assertEqual(rawio.event_count(0, 2, 0), 2) - + # Get array of all events in first segment of data events = rawio._get_event_timestamps(0, 0, 0, rawio._segment_t_start(0, 0), rawio._segment_t_stop(0, 0)) # Make sure it read 1 event self.assertEqual(len(events[0]), 1) - + # Get array of all events in second segment of data events = rawio._get_event_timestamps(0, 1, 0, rawio._segment_t_start(0, 1), rawio._segment_t_stop(0, 1)) # Make sure it read 2 events self.assertEqual(len(events[0]), 2) - + # Verify the first event of the second segment is a Neuralynx type event, with correct time self.assertEqual(events[2][0][:4], 'NlxP') self.assertEqual(events[0][0], 51.703509) - + # Get array of all events in third segment of data events = rawio._get_event_timestamps(0, 2, 0, rawio._segment_t_start(0, 2), rawio._segment_t_stop(0, 2)) # Make sure it read 2 events self.assertEqual(len(events[0]), 2) - + # Verify the second event of the second segment is a Neuralynx type event, with correct time self.assertEqual(events[2][1][:4], 'NlxP') self.assertEqual(events[0][1], 161.607036) - + rawio.close() - + # Test on second test dataset, test.medd, with preserving original timestamps. # Timestamps here are in UTC (seconds since midnight, 1 Jan 1970) filename = self.get_local_path('med/test.medd') - + rawio = MedRawIO(filename, password='L2_password', keep_original_times=True) rawio.parse_header() - + # Segment 0 self.assertEqual(rawio._segment_t_start(0, 0), 1678111774.012236) self.assertEqual(rawio._segment_t_stop(0, 0), 1678111813.902036) @@ -201,7 +210,7 @@ def test_scan_med_directory(self): # Segment 2 self.assertEqual(rawio._segment_t_start(0, 2), 1678111871.254293) self.assertEqual(rawio._segment_t_stop(0, 2), 1678111954.028938) - + # Verify that there are 5 events in the dataset. # They are 3 "Note" type events, and 2 "NlxP", or neuralynx, type events. # The first segment has one event, and the second and third segments @@ -209,33 +218,32 @@ def test_scan_med_directory(self): self.assertEqual(rawio.event_count(0, 0, 0), 1) self.assertEqual(rawio.event_count(0, 1, 0), 2) self.assertEqual(rawio.event_count(0, 2, 0), 2) - + # Get array of all events in first segment of data events = rawio._get_event_timestamps(0, 0, 0, rawio._segment_t_start(0, 0), rawio._segment_t_stop(0, 0)) # Make sure it read 1 event self.assertEqual(len(events[0]), 1) - + # Get array of all events in second segment of data events = rawio._get_event_timestamps(0, 1, 0, rawio._segment_t_start(0, 1), rawio._segment_t_stop(0, 1)) # Make sure it read 2 events self.assertEqual(len(events[0]), 2) - + # Verify the first event of the second segment is a Neuralynx type event, with correct time self.assertEqual(events[2][0][:4], 'NlxP') self.assertEqual(events[0][0], 1678111825.715745) - + # Get array of all events in third segment of data events = rawio._get_event_timestamps(0, 2, 0, rawio._segment_t_start(0, 2), rawio._segment_t_stop(0, 2)) # Make sure it read 2 events self.assertEqual(len(events[0]), 2) - + # Verify the second event of the second segment is a Neuralynx type event, with correct time self.assertEqual(events[2][1][:4], 'NlxP') self.assertEqual(events[0][1], 1678111935.619272) - + rawio.close() - - + + if __name__ == "__main__": unittest.main() - diff --git a/neo/test/rawiotest/test_plexon2rawio.py b/neo/test/rawiotest/test_plexon2rawio.py index 618fa0970..627024fe3 100644 --- a/neo/test/rawiotest/test_plexon2rawio.py +++ b/neo/test/rawiotest/test_plexon2rawio.py @@ -13,7 +13,7 @@ try: from neo.rawio.plexon2rawio.pypl2 import pypl2lib HAVE_PYPL2 = True -except ImportError: +except (ImportError, TimeoutError): HAVE_PYPL2 = False