5
5
import pathlib
6
6
import unittest
7
7
8
+ import quantities as pq
9
+ import numpy as np
10
+
8
11
from neo .io .medio import MedIO
9
12
from neo .test .iotest .common_io_test import BaseTestIO
10
- from neo .test .iotest .tools import get_test_file_full_path
11
- from neo .io .proxyobjects import (AnalogSignalProxy ,
12
- SpikeTrainProxy , EventProxy , EpochProxy )
13
- from neo import (AnalogSignal , SpikeTrain )
14
13
15
- import quantities as pq
16
- import numpy as np
14
+ try :
15
+ import dhn_med_py
16
+ HAVE_DHN_MED = True
17
+ except ImportError :
18
+ HAVE_DHN_MED = False
17
19
18
20
19
- # This run standard tests, this is mandatory for all IOs
21
+ # This runs standard tests, this is mandatory for all IOs
22
+ @unittest .skipUnless (HAVE_DHN_MED , "requires dhn_med_py package and all its dependencies" )
20
23
class TestMedIO (BaseTestIO , unittest .TestCase , ):
21
24
ioclass = MedIO
22
25
entities_to_download = ['med' ]
@@ -31,22 +34,22 @@ def setUp(self):
31
34
self .dirname = self .get_local_path ('med/sine_waves.medd' )
32
35
self .dirname2 = self .get_local_path ('med/test.medd' )
33
36
self .password = 'L2_password'
34
-
37
+
35
38
def test_read_segment_lazy (self ):
36
-
39
+
37
40
r = MedIO (self .dirname , self .password )
38
41
seg = r .read_segment (lazy = False )
39
-
42
+
40
43
# There will only be one analogsignal in this reading
41
44
self .assertEqual (len (seg .analogsignals ), 1 )
42
45
# Test that the correct number of samples are read, 5760000 samps for 3 channels
43
46
self .assertEqual (seg .analogsignals [0 ].shape [0 ], 5760000 )
44
47
self .assertEqual (seg .analogsignals [0 ].shape [1 ], 3 )
45
-
48
+
46
49
# Test the first sample value of all 3 channels, which are
47
50
# known to be [-1, -4, -4]
48
51
np .testing .assert_array_equal (seg .analogsignals [0 ][0 ][:3 ], [- 1 , - 4 , - 4 ])
49
-
52
+
50
53
for anasig in seg .analogsignals :
51
54
self .assertNotEqual (anasig .size , 0 )
52
55
for st in seg .spiketrains :
@@ -61,61 +64,61 @@ def test_read_segment_lazy(self):
61
64
assert ev .name is not None
62
65
for ep in seg .epochs :
63
66
assert ep .name is not None
64
-
67
+
65
68
r .close ()
66
69
67
70
def test_read_block (self ):
68
-
71
+
69
72
r = MedIO (self .dirname , self .password )
70
73
bl = r .read_block (lazy = True )
71
74
self .assertTrue (bl .annotations )
72
-
75
+
73
76
for count , seg in enumerate (bl .segments ):
74
77
assert seg .name == 'Seg #' + str (count ) + ' Block #0'
75
-
78
+
76
79
for anasig in seg .analogsignals :
77
80
assert anasig .name is not None
78
-
81
+
79
82
# Verify that the block annotations from the MED session are
80
83
# read properly. There are a lot of annotations, so we'll just
81
84
# spot-check a couple of them.
82
85
assert (bl .annotations ['metadata' ]['recording_country' ] == 'United States' )
83
86
assert (bl .annotations ['metadata' ]['AC_line_frequency' ] == 60.0 )
84
-
87
+
85
88
r .close ()
86
-
89
+
87
90
def test_read_segment_with_time_slice (self ):
88
91
"""
89
92
Test loading of a time slice and check resulting times
90
93
"""
91
94
r = MedIO (self .dirname , self .password )
92
95
seg = r .read_segment (time_slice = None )
93
-
96
+
94
97
# spike and epoch timestamps are not being read
95
98
self .assertEqual (len (seg .spiketrains ), 0 )
96
99
self .assertEqual (len (seg .epochs ), 1 )
97
100
self .assertEqual (len (seg .epochs [0 ]), 0 )
98
-
101
+
99
102
# Test for 180 events (1 per second for 3 minute recording)
100
103
self .assertEqual (len (seg .events ), 1 )
101
104
self .assertEqual (len (seg .events [0 ]), 180 )
102
-
105
+
103
106
for asig in seg .analogsignals :
104
107
self .assertEqual (asig .shape [0 ], 5760000 )
105
108
n_channels = sum (a .shape [- 1 ] for a in seg .analogsignals )
106
109
self .assertEqual (n_channels , 3 )
107
-
110
+
108
111
t_start , t_stop = 500 * pq .ms , 800 * pq .ms
109
112
seg = r .read_segment (time_slice = (t_start , t_stop ))
110
-
113
+
111
114
# Test that 300 ms were read, which at 32 kHz, is 9600 samples
112
115
self .assertAlmostEqual (seg .analogsignals [0 ].shape [0 ], 9600 , delta = 1. )
113
116
# Test that it read from 3 channels
114
117
self .assertEqual (seg .analogsignals [0 ].shape [1 ], 3 )
115
-
118
+
116
119
self .assertAlmostEqual (seg .t_start .rescale (t_start .units ), t_start , delta = 5. )
117
120
self .assertAlmostEqual (seg .t_stop .rescale (t_stop .units ), t_stop , delta = 5. )
118
-
121
+
119
122
r .close ()
120
123
121
124
0 commit comments