Skip to content

Commit 724c663

Browse files
authored
Merge pull request #517 from mpsonntag/dev
Add documentation examples tests
2 parents 3d4f21e + 47d0414 commit 724c663

File tree

11 files changed

+206
-26
lines changed

11 files changed

+206
-26
lines changed

.github/workflows/pythonpackage.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
pylint --rcfile=.pylintrc nixio
3838
- name: Test with pytest
3939
run: |
40-
pip install pytest
40+
pip install pytest scipy pillow matplotlib
4141
# pytest will skip cross-compatibility tests since NIX isn't installed
4242
pytest
4343
@@ -69,7 +69,7 @@ jobs:
6969
- name: Install Python dependencies
7070
run: |
7171
python setup.py install
72-
pip install pytest
72+
pip install pytest scipy pillow matplotlib
7373
- name: Run tests
7474
run: |
7575
pytest
@@ -86,7 +86,7 @@ jobs:
8686
- name: Install dependencies
8787
run: |
8888
python setup.py install
89-
pip install pytest coveralls
89+
pip install pytest coveralls scipy pillow matplotlib
9090
- name: Create coverage
9191
run: |
9292
coverage run --source=nixio -m pytest nixio/test/
@@ -107,7 +107,7 @@ jobs:
107107
- name: Install dependencies
108108
run: |
109109
python setup.py install
110-
pip install pytest pytest-cov
110+
pip install pytest pytest-cov scipy pillow matplotlib
111111
- name: Create coverage
112112
run: |
113113
pytest --cov=./ --cov-report=xml

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ install:
6060
# - cd ..
6161
- python -m pip install certifi
6262
- python -m pip install -U pip wheel
63-
- python -m pip install numpy h5py pytest-xdist
63+
- python -m pip install numpy h5py pytest-xdist scipy pillow matplotlib
6464
- ps: cd "$env:APPVEYOR_BUILD_FOLDER"
6565
- set DISTUTILS_USE_SDK="1"
6666
- set MSSdk="1"

docs/source/examples/imageData.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ def load_image():
3838

3939

4040
def plot_data(data_array):
41-
img_data = np.zeros(data_array.data.shape)
42-
data_array.data.read_direct(img_data)
41+
img_data = np.zeros(data_array.shape)
42+
data_array.read_direct(img_data)
4343
img_data = np.array(img_data, dtype='uint8')
4444
new_img = img.fromarray(img_data)
4545
new_img.show()

docs/source/examples/imageWithMetadata.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def print_metadata_table(section, ax):
3535
cell_text = []
3636
for p in section.items():
3737
for i, v in enumerate(p[1].values):
38-
value = str(v.value)
38+
value = str(v)
3939
if len(value) > 30:
4040
value = value[:30] + '...'
4141
if i == 0:
@@ -63,8 +63,8 @@ def load_image():
6363

6464

6565
def plot_data(data_array):
66-
img_data = np.zeros(data_array.data.shape)
67-
data_array.data.read_direct(img_data)
66+
img_data = np.zeros(data_array.shape)
67+
data_array.read_direct(img_data)
6868
img_data = np.array(img_data, dtype='uint8')
6969
new_img = img.fromarray(img_data)
7070

docs/source/examples/multipleTimeSeries.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ def create_data(duration=1, freq=10, stepsize=0.01):
2929
def plot_data(data_array):
3030
set_dim = data_array.dimensions[0]
3131
x_axis = data_array.dimensions[1]
32-
x = np.arange(0, data_array.data.shape[1])
32+
x = np.arange(0, data_array.shape[1])
3333
x = x * x_axis.sampling_interval + x_axis.offset
34-
y = np.zeros(data_array.data.shape)
35-
data_array.data.read_direct(y)
34+
y = np.zeros(data_array.shape)
35+
data_array.read_direct(y)
3636
for i, label in enumerate(set_dim.labels):
3737
plt.plot(x, y[i, :], label=label)
3838

docs/source/examples/regularlySampledData.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ def create_sinewave(duration=1, freq=10, stepsize=0.01):
2727

2828
def plot_data(data_array):
2929
x_axis = data_array.dimensions[0]
30-
x = x_axis.axis(data_array.data.shape[0])
31-
y = data_array.data[:]
30+
x = x_axis.axis(data_array.shape[0])
31+
y = data_array[:]
3232

3333
plt.plot(x, y, marker=".", markersize=5, label=data_array.name)
3434
plt.xlabel(x_axis.label + " [" + x_axis.unit + "]")

docs/source/examples/singleROI.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ def draw_rect(img_data, position, extent):
4343

4444
def plot_data(tag):
4545
data_array = tag.references[0]
46-
img_data = np.zeros(data_array.data.shape)
47-
data_array.data.read_direct(img_data)
46+
img_data = np.zeros(data_array.shape)
47+
data_array.read_direct(img_data)
4848
img_data = np.array(img_data, dtype='uint8')
4949
# positions and extents are double by default, need to convert to int
5050
pos = tuple(map(int, tag.position))
5151
ext = tuple(map(int, tag.extent))
5252
draw_rect(img_data, pos, ext)
5353
new_img = img.fromarray(img_data)
5454

55-
new_img.save("../images/single_roi.png")
55+
# new_img.save("../images/single_roi.png")
5656
new_img.show()
5757

5858

docs/source/examples/spikeFeatures.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ def fake_neuron(stepsize=0.001, offset=.8, sta_offset=1000):
4747

4848
def plot_data(tag):
4949
data_array = tag.references[0]
50-
voltage = np.zeros(data_array.data.shape)
51-
data_array.data.read_direct(voltage)
50+
voltage = np.zeros(data_array.shape)
51+
data_array.read_direct(voltage)
5252

5353
x_axis = data_array.dimensions[0]
5454
time = x_axis.axis(data_array.data_extent[0])

docs/source/examples/spikeTagging.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ def fake_neuron():
2929

3030
def plot_data(tag):
3131
data_array = tag.references[0]
32-
voltage = np.zeros(data_array.data.shape)
33-
data_array.data.read_direct(voltage)
32+
voltage = np.zeros(data_array.shape)
33+
data_array.read_direct(voltage)
3434

3535
x_axis = data_array.dimensions[0]
36-
time = x_axis.axis(data_array.data.shape[0])
36+
time = x_axis.axis(data_array.shape[0])
3737

3838
spike_times = np.zeros(tag.positions.data_extent)
39-
tag.positions.data.read_direct(spike_times)
39+
tag.positions.read_direct(spike_times)
4040

4141
fig = plt.figure(figsize=(5.5, 2.5))
4242
ax = fig.add_subplot(111)
@@ -48,7 +48,7 @@ def plot_data(tag):
4848
ax.set_ylim((1.5 * np.min(voltage), 1.5 * np.max(voltage)))
4949
ax.legend()
5050
fig.subplots_adjust(bottom=0.175, top=0.975, right=0.975)
51-
fig.savefig("../images/spike_tagging.png")
51+
# fig.savefig("../images/spike_tagging.png")
5252
plt.show()
5353

5454

nixio/test/test_doc_examples.py

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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

Comments
 (0)