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