File: test_doc_examples.py

package info (click to toggle)
python-nixio 1.5.4%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,888 kB
  • sloc: python: 12,527; cpp: 832; makefile: 25
file content (191 lines) | stat: -rw-r--r-- 6,435 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import sys
import runpy
import pytest
import unittest
import importlib.util
import importlib.machinery
import matplotlib.pyplot as plt

from pathlib import Path
from shutil import copyfile

TEST_IMAGE = "lenna.png"

@pytest.mark.skip(reason="docs tests often leads to errors during ci")
class TestDocumentationExamples(unittest.TestCase):

    examples_path = Path("docs/source/examples")

    examples_path = Path("docs/source/examples")

    def run_script(self, script_name):
        file_path = Path.joinpath(self.examples_path, script_name)
        runpy.run_path(path_name=str(file_path), run_name="__main__")

    def handle_lif(self):
        lif_path = Path.joinpath(self.examples_path, "lif.py")
        spec = importlib.util.spec_from_file_location("lif", lif_path)
        module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(module)
        sys.modules["lif"] = module

    def handle_image(self):
        image_path = Path.joinpath(self.examples_path, TEST_IMAGE)
        copyfile(str(image_path), str(Path.joinpath(Path(Path.cwd()), TEST_IMAGE)))

    def setUp(self):
        curr_path = Path.cwd()
        if curr_path.stem == "nixpy":
            self.examples_path = Path.joinpath(Path(curr_path),
                                               "docs", "source", "examples")
        elif curr_path.stem == "nixio":
            self.examples_path = Path.joinpath(Path(curr_path).parent,
                                               "docs", "source", "examples")
        elif curr_path.stem == "test":
            self.examples_path = Path.joinpath(Path(curr_path).parent.parent,
                                               "docs", "source", "examples")

        util_path = Path.joinpath(self.examples_path, "docutils.py")
        spec = importlib.util.spec_from_file_location("docutils", util_path)
        module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(module)
        sys.modules["docutils"] = module

        # Render matplotlib plots non-blocking
        plt.ion()

    def tearDown(self):
        plt.close("all")
        plt.ioff()
        if Path.exists(Path(TEST_IMAGE)):
            Path.unlink(Path(TEST_IMAGE), True)

    def test_annotations(self):
        self.run_script("annotations.py")
        # cleanup
        Path.unlink(Path("annotations.nix"), True)

    def test_category_data(self):
        self.run_script("categoryData.py")
        # cleanup
        Path.unlink(Path("categoryData.nix"), True)

    def test_continuous_recording(self):
        self.run_script("continuousRecording.py")
        # cleanup
        Path.unlink(Path("continuous_recording.nix"), True)

    def test_file_create(self):
        self.run_script("fileCreate.py")
        # cleanup
        Path.unlink(Path("file_create_example.nix"), True)

    def test_image_data(self):
        # test will open image with an external program; does not work on windows
        if sys.platform == 'linux':
            # Requires PIL package and the "Lenna" image.
            self.handle_image()
            self.run_script("imageData.py")
            # cleanup
            Path.unlink(Path("image_example.nix"), True)

    def test_image_with_metadata(self):
        # Requires PIL package and the "Lenna" image.
        self.handle_image()
        self.run_script("imageWithMetadata.py")
        # cleanup
        Path.unlink(Path("image_with_source_example.h5"), True)
        Path.unlink(Path("image_with_metadata.png"), True)

    def test_irregularly_sampled_data(self):
        self.run_script("irregularlySampledData.py")
        # cleanup
        Path.unlink(Path("irregular_data_example.nix"), True)

    def test_lif(self):
        self.run_script("lif.py")

    def test_multiple_points(self):
        self.run_script("multiple_points.py")
        # cleanup
        Path.unlink(Path("multiple_points.nix"), True)

    def test_multiple_regions(self):
        self.run_script("multiple_regions.py")
        # cleanup
        Path.unlink(Path("multiple_regions.nix"), True)

    def test_multiple_rois(self):
        # Requires PIL package and the "Lenna" image.
        self.handle_image()
        self.run_script("multipleROIs.py")
        # cleanup
        Path.unlink(Path("multiple_roi.nix"), True)

    def test_range_dimension_link(self):
        self.run_script("rangeDimensionLink.py")
        # cleanup
        Path.unlink(Path("range_link.nix"), True)

    def test_regularly_sampled_data(self):
        self.run_script("regularlySampledData.py")
        # cleanup
        Path.unlink(Path("regular_data_example.nix"), True)

    def test_single_roi(self):
        # test will open image with an external program; does not work on windows
        if sys.platform == 'linux':
            # Requires PIL package and the "Lenna" image.
            self.handle_image()
            self.run_script("singleROI.py")
            # cleanup
            Path.unlink(Path("single_roi.nix"), True)

    def test_sources(self):
        self.run_script("sources.py")
        # cleanup
        Path.unlink(Path("sources.nix"), True)

    def test_spike_features(self):
        # Requires scipy package and "lif.py"
        self.handle_lif()
        self.run_script("spikeFeatures.py")
        # cleanup
        Path.unlink(Path("spike_features.h5"), True)

    def test_spike_tagging(self):
        # Requires "lif.py"
        self.handle_lif()
        self.run_script("spikeTagging.py")
        # cleanup
        Path.unlink(Path("spike_tagging.nix"), True)

    def test_tabular_data(self):
        self.run_script("tabulardata.py")
        # cleanup
        Path.unlink(Path("dataframe.nix"), True)

    def test_tagged_feature(self):
        # Requires scipy package and "lif.py"
        self.handle_lif()
        self.run_script("taggedFeature.py")
        # cleanup
        Path.unlink(Path("spike_features.nix"), True)

    def test_tagging_example(self):
        # Requires scipy package
        self.run_script("tagging_example.py")
        # cleanup
        Path.unlink(Path("tagging1.nix"), True)

    def test_tagging_nd(self):
        # not testing any nix feature
        # self.run_script("tagging_nd.py")
        pass

    def test_untagged_feature(self):
        # Requires scipy package and "lif.py"
        self.handle_lif()
        self.run_script("untaggedFeature.py")
        # cleanup
        Path.unlink(Path("untagged_feature.h5"), True)