| 1 |
import sys |
| 2 |
import runpy |
| 3 |
import pytest |
| 4 |
import unittest |
| 5 |
import importlib.util |
| 6 |
import importlib.machinery |
| 7 |
import matplotlib.pyplot as plt |
| 8 |
|
| 9 |
from pathlib import Path |
| 10 |
from shutil import copyfile |
| 11 |
|
| 12 |
TEST_IMAGE = "lenna.png" |
| 13 |
|
| 14 |
@pytest.mark.skip(reason="docs tests often leads to errors during ci") |
| 15 |
class TestDocumentationExamples(unittest.TestCase): |
| 16 |
|
| 17 |
examples_path = Path("docs/source/examples") |
| 18 |
|
| •19 |
examples_path = Path("docs/source/examples") |
| 20 |
|
| 21 |
def run_script(self, script_name): |
| 22 |
file_path = Path.joinpath(self.examples_path, script_name) |
| 23 |
runpy.run_path(path_name=str(file_path), run_name="__main__") |
| 24 |
|
| 25 |
def handle_lif(self): |
| 26 |
lif_path = Path.joinpath(self.examples_path, "lif.py") |
| 27 |
spec = importlib.util.spec_from_file_location("lif", lif_path) |
| 28 |
module = importlib.util.module_from_spec(spec) |
| 29 |
spec.loader.exec_module(module) |
| 30 |
sys.modules["lif"] = module |
| 31 |
|
| 32 |
def handle_image(self): |
| 33 |
image_path = Path.joinpath(self.examples_path, TEST_IMAGE) |
| 34 |
copyfile(str(image_path), str(Path.joinpath(Path(Path.cwd()), TEST_IMAGE))) |
| 35 |
|
| 36 |
def setUp(self): |
| 37 |
curr_path = Path.cwd() |
| 38 |
if curr_path.stem == "nixpy": |
| 39 |
self.examples_path = Path.joinpath(Path(curr_path), |
| 40 |
"docs", "source", "examples") |
| 41 |
elif curr_path.stem == "nixio": |
| 42 |
self.examples_path = Path.joinpath(Path(curr_path).parent, |
| 43 |
"docs", "source", "examples") |
| 44 |
elif curr_path.stem == "test": |
| 45 |
self.examples_path = Path.joinpath(Path(curr_path).parent.parent, |
| 46 |
"docs", "source", "examples") |
| 47 |
|
| 48 |
util_path = Path.joinpath(self.examples_path, "docutils.py") |
| 49 |
spec = importlib.util.spec_from_file_location("docutils", util_path) |
| 50 |
module = importlib.util.module_from_spec(spec) |
| 51 |
spec.loader.exec_module(module) |
| 52 |
sys.modules["docutils"] = module |
| 53 |
|
| 54 |
|
| 55 |
plt.ion() |
| 56 |
|
| 57 |
def tearDown(self): |
| 58 |
plt.close("all") |
| 59 |
plt.ioff() |
| 60 |
if Path.exists(Path(TEST_IMAGE)): |
| 61 |
Path.unlink(Path(TEST_IMAGE), True) |
| 62 |
|
| 63 |
def test_annotations(self): |
| 64 |
self.run_script("annotations.py") |
| 65 |
|
| 66 |
Path.unlink(Path("annotations.nix"), True) |
| 67 |
|
| 68 |
def test_category_data(self): |
| 69 |
self.run_script("categoryData.py") |
| 70 |
|
| 71 |
Path.unlink(Path("categoryData.nix"), True) |
| 72 |
|
| 73 |
def test_continuous_recording(self): |
| 74 |
self.run_script("continuousRecording.py") |
| 75 |
|
| 76 |
Path.unlink(Path("continuous_recording.nix"), True) |
| 77 |
|
| 78 |
def test_file_create(self): |
| 79 |
self.run_script("fileCreate.py") |
| 80 |
|
| 81 |
Path.unlink(Path("file_create_example.nix"), True) |
| 82 |
|
| 83 |
def test_image_data(self): |
| 84 |
|
| 85 |
if sys.platform == 'linux': |
| 86 |
|
| 87 |
self.handle_image() |
| 88 |
self.run_script("imageData.py") |
| 89 |
|
| 90 |
Path.unlink(Path("image_example.nix"), True) |
| 91 |
|
| 92 |
def test_image_with_metadata(self): |
| 93 |
|
| 94 |
self.handle_image() |
| 95 |
self.run_script("imageWithMetadata.py") |
| 96 |
|
| 97 |
Path.unlink(Path("image_with_source_example.h5"), True) |
| 98 |
Path.unlink(Path("image_with_metadata.png"), True) |
| 99 |
|
| 100 |
def test_irregularly_sampled_data(self): |
| 101 |
self.run_script("irregularlySampledData.py") |
| 102 |
|
| 103 |
Path.unlink(Path("irregular_data_example.nix"), True) |
| 104 |
|
| 105 |
def test_lif(self): |
| 106 |
self.run_script("lif.py") |
| 107 |
|
| 108 |
def test_multiple_points(self): |
| 109 |
self.run_script("multiple_points.py") |
| 110 |
|
| 111 |
Path.unlink(Path("multiple_points.nix"), True) |
| 112 |
|
| 113 |
def test_multiple_regions(self): |
| 114 |
self.run_script("multiple_regions.py") |
| 115 |
|
| 116 |
Path.unlink(Path("multiple_regions.nix"), True) |
| 117 |
|
| 118 |
def test_multiple_rois(self): |
| 119 |
|
| 120 |
self.handle_image() |
| 121 |
self.run_script("multipleROIs.py") |
| 122 |
|
| 123 |
Path.unlink(Path("multiple_roi.nix"), True) |
| 124 |
|
| 125 |
def test_range_dimension_link(self): |
| 126 |
self.run_script("rangeDimensionLink.py") |
| 127 |
|
| 128 |
Path.unlink(Path("range_link.nix"), True) |
| 129 |
|
| 130 |
def test_regularly_sampled_data(self): |
| 131 |
self.run_script("regularlySampledData.py") |
| 132 |
|
| 133 |
Path.unlink(Path("regular_data_example.nix"), True) |
| 134 |
|
| 135 |
def test_single_roi(self): |
| 136 |
|
| 137 |
if sys.platform == 'linux': |
| 138 |
|
| 139 |
self.handle_image() |
| 140 |
self.run_script("singleROI.py") |
| 141 |
|
| 142 |
Path.unlink(Path("single_roi.nix"), True) |
| 143 |
|
| 144 |
def test_sources(self): |
| 145 |
self.run_script("sources.py") |
| 146 |
|
| 147 |
Path.unlink(Path("sources.nix"), True) |
| 148 |
|
| 149 |
def test_spike_features(self): |
| 150 |
|
| 151 |
self.handle_lif() |
| 152 |
self.run_script("spikeFeatures.py") |
| 153 |
|
| 154 |
Path.unlink(Path("spike_features.h5"), True) |
| 155 |
|
| 156 |
def test_spike_tagging(self): |
| 157 |
|
| 158 |
self.handle_lif() |
| 159 |
self.run_script("spikeTagging.py") |
| 160 |
|
| 161 |
Path.unlink(Path("spike_tagging.nix"), True) |
| 162 |
|
| 163 |
def test_tabular_data(self): |
| 164 |
self.run_script("tabulardata.py") |
| 165 |
|
| 166 |
Path.unlink(Path("dataframe.nix"), True) |
| 167 |
|
| 168 |
def test_tagged_feature(self): |
| 169 |
|
| 170 |
self.handle_lif() |
| 171 |
self.run_script("taggedFeature.py") |
| 172 |
|
| 173 |
Path.unlink(Path("spike_features.nix"), True) |
| 174 |
|
| 175 |
def test_tagging_example(self): |
| 176 |
|
| 177 |
self.run_script("tagging_example.py") |
| 178 |
|
| 179 |
Path.unlink(Path("tagging1.nix"), True) |
| 180 |
|
| 181 |
def test_tagging_nd(self): |
| 182 |
|
| 183 |
|
| 184 |
pass |
| 185 |
|
| 186 |
def test_untagged_feature(self): |
| 187 |
|
| 188 |
self.handle_lif() |
| 189 |
self.run_script("untaggedFeature.py") |
| 190 |
|
| 191 |
Path.unlink(Path("untagged_feature.h5"), True) |