1
1
import importlib .util as getmod
2
- import matplotlib .pyplot as plt
3
2
import os
4
3
import runpy
5
4
import unittest
6
5
7
6
from pathlib import Path
8
7
from shutil import copyfile
9
8
9
+ import matplotlib .pyplot as plt
10
+
11
+
12
+ TEST_IMAGE = "lenna.png"
13
+
10
14
11
15
class TestDocumentationExamples (unittest .TestCase ):
12
16
17
+ def run_script (self , script_name ):
18
+ file_path = Path .joinpath (self .examples_path , script_name )
19
+ runpy .run_path (path_name = str (file_path ), run_name = "__main__" )
20
+
21
+ def handle_lif (self ):
22
+ lif_path = Path .joinpath (self .examples_path , "lif.py" )
23
+ spec = getmod .spec_from_file_location ("lif" , str (lif_path ))
24
+ spec .loader .load_module ("lif" )
25
+
26
+ def handle_image (self ):
27
+ image_path = Path .joinpath (self .examples_path , TEST_IMAGE )
28
+ copyfile (str (image_path ), str (Path .joinpath (Path (os .getcwd ()), TEST_IMAGE )))
29
+
13
30
def setUp (self ):
14
31
curr_path = os .getcwd ()
15
32
if os .path .basename (curr_path ) == "nixpy" :
@@ -28,184 +45,131 @@ def setUp(self):
28
45
def tearDown (self ):
29
46
plt .close ("all" )
30
47
plt .ioff ()
48
+ if os .path .exists (TEST_IMAGE ):
49
+ os .remove (TEST_IMAGE )
31
50
32
51
def test_annotations (self ):
33
- file_path = Path .joinpath (self .examples_path , "annotations.py" )
34
- runpy .run_path (path_name = str (file_path ), run_name = "__main__" )
52
+ self .run_script ("annotations.py" )
35
53
# cleanup
36
54
os .remove ("annotations.nix" )
37
55
38
56
def test_category_data (self ):
39
- file_path = Path .joinpath (self .examples_path , "categoryData.py" )
40
- runpy .run_path (path_name = str (file_path ), run_name = "__main__" )
57
+ self .run_script ("categoryData.py" )
41
58
# cleanup
42
59
os .remove ("categoryData.nix" )
43
60
44
61
def test_continuous_recording (self ):
45
- file_path = Path .joinpath (self .examples_path , "continuousRecording.py" )
46
- runpy .run_path (path_name = str (file_path ), run_name = "__main__" )
62
+ self .run_script ("continuousRecording.py" )
47
63
# cleanup
48
64
os .remove ("continuous_recording.nix" )
49
65
50
66
def test_file_create (self ):
51
- file_path = Path .joinpath (self .examples_path , "fileCreate.py" )
52
- runpy .run_path (path_name = str (file_path ), run_name = "__main__" )
67
+ self .run_script ("fileCreate.py" )
53
68
# cleanup
54
69
os .remove ("file_create_example.nix" )
55
70
56
71
def test_image_data (self ):
57
72
# Requires PIL package and the "Lenna" image.
58
- image_path = Path .joinpath (self .examples_path , "lenna.png" )
59
- copyfile (str (image_path ), str (Path .joinpath (Path (os .getcwd ()), "lenna.png" )))
60
-
61
- file_path = Path .joinpath (self .examples_path , "imageData.py" )
62
- runpy .run_path (path_name = str (file_path ), run_name = "__main__" )
63
-
73
+ self .handle_image ()
74
+ self .run_script ("imageData.py" )
64
75
# cleanup
65
76
os .remove ("image_example.nix" )
66
- os .remove ("lenna.png" )
67
77
68
78
def test_image_with_metadata (self ):
69
79
# Requires PIL package and the "Lenna" image.
70
- image_path = Path .joinpath (self .examples_path , "lenna.png" )
71
- copyfile (str (image_path ), str (Path .joinpath (Path (os .getcwd ()), "lenna.png" )))
72
-
73
- file_path = Path .joinpath (self .examples_path , "imageWithMetadata.py" )
74
- runpy .run_path (path_name = str (file_path ), run_name = "__main__" )
75
-
80
+ self .handle_image ()
81
+ self .run_script ("imageWithMetadata.py" )
76
82
# cleanup
77
83
os .remove ("image_with_source_example.h5" )
78
84
os .remove ("image_with_metadata.png" )
79
- os .remove ("lenna.png" )
80
85
81
86
def test_irregularly_sampled_data (self ):
82
- file_path = Path .joinpath (self .examples_path , "irregularlySampledData.py" )
83
- runpy .run_path (path_name = str (file_path ), run_name = "__main__" )
87
+ self .run_script ("irregularlySampledData.py" )
84
88
# cleanup
85
89
os .remove ("irregular_data_example.nix" )
86
90
87
91
def test_lif (self ):
88
- file_path = Path .joinpath (self .examples_path , "lif.py" )
89
- runpy .run_path (path_name = str (file_path ), run_name = "__main__" )
92
+ self .run_script ("lif.py" )
90
93
91
94
def test_multiple_points (self ):
92
- file_path = Path .joinpath (self .examples_path , "multiple_points.py" )
93
- runpy .run_path (path_name = str (file_path ), run_name = "__main__" )
95
+ self .run_script ("multiple_points.py" )
94
96
# cleanup
95
97
os .remove ("multiple_points.nix" )
96
98
97
99
def test_multiple_regions (self ):
98
- file_path = Path .joinpath (self .examples_path , "multiple_regions.py" )
99
- runpy .run_path (path_name = str (file_path ), run_name = "__main__" )
100
+ self .run_script ("multiple_regions.py" )
100
101
# cleanup
101
102
os .remove ("multiple_regions.nix" )
102
103
103
104
def test_multiple_rois (self ):
104
105
# Requires PIL package and the "Lenna" image.
105
- image_path = Path .joinpath (self .examples_path , "lenna.png" )
106
- copyfile (str (image_path ), str (Path .joinpath (Path (os .getcwd ()), "lenna.png" )))
107
-
108
- file_path = Path .joinpath (self .examples_path , "multipleROIs.py" )
109
- runpy .run_path (path_name = str (file_path ), run_name = "__main__" )
110
-
106
+ self .handle_image ()
107
+ self .run_script ("multipleROIs.py" )
111
108
# cleanup
112
- os .remove ("lenna.png" )
113
109
os .remove ("multiple_roi.nix" )
114
110
115
111
def test_range_dimension_link (self ):
116
- file_path = Path .joinpath (self .examples_path , "rangeDimensionLink.py" )
117
- runpy .run_path (path_name = str (file_path ), run_name = "__main__" )
112
+ self .run_script ("rangeDimensionLink.py" )
118
113
# cleanup
119
114
os .remove ("range_link.nix" )
120
115
121
116
def test_regularly_sampled_data (self ):
122
- file_path = Path .joinpath (self .examples_path , "regularlySampledData.py" )
123
- runpy .run_path (path_name = str (file_path ), run_name = "__main__" )
117
+ self .run_script ("regularlySampledData.py" )
124
118
# cleanup
125
119
os .remove ("regular_data_example.nix" )
126
120
127
121
def test_single_roi (self ):
128
122
# Requires PIL package and the "Lenna" image.
129
- image_path = Path .joinpath (self .examples_path , "lenna.png" )
130
- copyfile (str (image_path ), str (Path .joinpath (Path (os .getcwd ()), "lenna.png" )))
131
-
132
- file_path = Path .joinpath (self .examples_path , "singleROI.py" )
133
- runpy .run_path (path_name = str (file_path ), run_name = "__main__" )
134
-
123
+ self .handle_image ()
124
+ self .run_script ("singleROI.py" )
135
125
# cleanup
136
- os .remove ("lenna.png" )
137
126
os .remove ("single_roi.nix" )
138
127
139
128
def test_sources (self ):
140
- file_path = Path .joinpath (self .examples_path , "sources.py" )
141
- runpy .run_path (path_name = str (file_path ), run_name = "__main__" )
129
+ self .run_script ("sources.py" )
142
130
# cleanup
143
131
os .remove ("sources.nix" )
144
132
145
133
def test_spike_features (self ):
146
134
# Requires scipy package and "lif.py"
147
- lif_path = Path .joinpath (self .examples_path , "lif.py" )
148
- spec = getmod .spec_from_file_location ("lif" , str (lif_path ))
149
- spec .loader .load_module ("lif" )
150
-
151
- file_path = Path .joinpath (self .examples_path , "spikeFeatures.py" )
152
- runpy .run_path (path_name = str (file_path ), run_name = "__main__" )
153
-
135
+ self .handle_lif ()
136
+ self .run_script ("spikeFeatures.py" )
154
137
# cleanup
155
138
os .remove ("spike_features.h5" )
156
139
157
140
def test_spike_tagging (self ):
158
141
# Requires "lif.py"
159
- lif_path = Path .joinpath (self .examples_path , "lif.py" )
160
- spec = getmod .spec_from_file_location ("lif" , str (lif_path ))
161
- spec .loader .load_module ("lif" )
162
-
163
- file_path = Path .joinpath (self .examples_path , "spikeTagging.py" )
164
- runpy .run_path (path_name = str (file_path ), run_name = "__main__" )
165
-
142
+ self .handle_lif ()
143
+ self .run_script ("spikeTagging.py" )
166
144
# cleanup
167
145
os .remove ("spike_tagging.nix" )
168
146
169
147
def test_tabular_data (self ):
170
- file_path = Path .joinpath (self .examples_path , "tabulardata.py" )
171
- runpy .run_path (path_name = str (file_path ), run_name = "__main__" )
148
+ self .run_script ("tabulardata.py" )
172
149
# cleanup
173
150
os .remove ("dataframe.nix" )
174
151
175
152
def test_tagged_feature (self ):
176
153
# Requires scipy package and "lif.py"
177
- lif_path = Path .joinpath (self .examples_path , "lif.py" )
178
- spec = getmod .spec_from_file_location ("lif" , str (lif_path ))
179
- spec .loader .load_module ("lif" )
180
-
181
- file_path = Path .joinpath (self .examples_path , "taggedFeature.py" )
182
- runpy .run_path (path_name = str (file_path ), run_name = "__main__" )
183
-
154
+ self .handle_lif ()
155
+ self .run_script ("taggedFeature.py" )
184
156
# cleanup
185
157
os .remove ("spike_features.nix" )
186
158
187
159
def test_tagging_example (self ):
188
160
# Requires scipy package
189
- file_path = Path .joinpath (self .examples_path , "tagging_example.py" )
190
- runpy .run_path (path_name = str (file_path ), run_name = "__main__" )
191
-
161
+ self .run_script ("tagging_example.py" )
192
162
# cleanup
193
163
os .remove ("tagging1.nix" )
194
164
195
165
def test_tagging_nd (self ):
196
166
# not testing any nix feature
197
- # file_path = Path.joinpath(self.examples_path, "tagging_nd.py")
198
- # runpy.run_path(path_name=str(file_path), run_name="__main__")
167
+ # self.run_script("tagging_nd.py")
199
168
pass
200
169
201
170
def test_untagged_feature (self ):
202
171
# Requires scipy package and "lif.py"
203
- lif_path = Path .joinpath (self .examples_path , "lif.py" )
204
- spec = getmod .spec_from_file_location ("lif" , str (lif_path ))
205
- spec .loader .load_module ("lif" )
206
-
207
- file_path = Path .joinpath (self .examples_path , "untaggedFeature.py" )
208
- runpy .run_path (path_name = str (file_path ), run_name = "__main__" )
209
-
172
+ self .handle_lif ()
173
+ self .run_script ("untaggedFeature.py" )
210
174
# cleanup
211
175
os .remove ("untagged_feature.h5" )
0 commit comments