Skip to content

Commit 915a232

Browse files
authored
Merge pull request #542 from jgrewe/numpy
Numpy
2 parents a89d844 + 34c4f49 commit 915a232

File tree

8 files changed

+59
-52
lines changed

8 files changed

+59
-52
lines changed

.github/workflows/pythonpackage.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ jobs:
5858
- uses: actions/checkout@v2
5959
- uses: conda-incubator/setup-miniconda@v2
6060
with:
61+
miniconda-version: "latest"
6162
activate-environment: deptest-${{ matrix.python-version }}
6263
python-version: ${{ matrix.python-version }}
6364
channels: conda-forge

docs/source/examples/multiple_regions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ def plot(nixfile):
7070
rect = patches.Rectangle((interval, 1.05), extent, -2.1, alpha=0.5,
7171
facecolor="silver", edgecolor='k', lw=0.75, ls='--')
7272
ax.add_patch(rect)
73+
stim_freq = mtag.feature_data(i, "stimulus frequency")[:] # should be scalar, we will use item() to read it
7374
ax.text(interval + extent / 2, -1.25,
74-
"%.1f %s" % (mtag.feature_data(i, "stimulus frequency")[:],
75+
"%.1f %s" % (stim_freq.item(),
7576
mtag.features["stimulus frequency"].data.unit), fontsize=8, ha="center")
7677
ax.legend((l, rect), (signal_da.name, mtag.name), loc=1, frameon=False, ncol=2)
7778

docs/source/examples/spikeFeatures.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
1616
"""
1717

18-
import nixio
1918
import lif
19+
import nixio
2020
import numpy as np
2121
import scipy.signal as signal
2222
import matplotlib.pylab as plt
@@ -68,7 +68,7 @@ def plot_data(tag):
6868
average_snippet_axis = plt.subplot2grid((2, 2), (1, 1), rowspan=1, colspan=1)
6969

7070
response_axis.plot(time, voltage, color='dodgerblue', label=data_array.name)
71-
response_axis.scatter(spike_times, np.ones(spike_times.shape)*np.max(voltage), color='red', label=tag.name)
71+
response_axis.scatter(spike_times, np.ones(spike_times.shape) * np.max(voltage), color='red', label=tag.name)
7272
response_axis.set_xlabel(x_axis.label + ((" [" + x_axis.unit + "]") if x_axis.unit else ""))
7373
response_axis.set_ylabel(data_array.label + ((" [" + data_array.unit + "]") if data_array.unit else ""))
7474
response_axis.set_title(data_array.name)
@@ -81,7 +81,7 @@ def plot_data(tag):
8181
single_snippet_axis.set_ylabel(feature_data_array.label + ((" [" + feature_data_array.unit + "]") if feature_data_array.unit else ""))
8282
single_snippet_axis.set_title("single stimulus snippet")
8383
single_snippet_axis.set_xlim(np.min(snippet_time), np.max(snippet_time))
84-
single_snippet_axis.set_ylim((1.2 * np.min(snippets[3,:]), 1.2 * np.max(snippets[3,:])))
84+
single_snippet_axis.set_ylim((1.2 * np.min(snippets[3, :]), 1.2 * np.max(snippets[3, :])))
8585
single_snippet_axis.legend()
8686

8787
mean_snippet = np.mean(snippets, axis=0)
@@ -129,7 +129,7 @@ def main():
129129
# save stimulus snippets in a DataArray
130130
snippets = block.create_data_array("spike triggered stimulus", "nix.regular_sampled.multiple_series", data=sts, label="stimulus", unit="nA")
131131
snippets.append_set_dimension()
132-
snippets.append_sampled_dimension(stepsize, offset= -sta_offset * stepsize, label="time", unit="s")
132+
snippets.append_sampled_dimension(stepsize, offset=-sta_offset * stepsize, label="time", unit="s")
133133

134134
# set snippets as an indexed feature of the multi_tag
135135
multi_tag.create_feature(snippets, nixio.LinkType.Indexed)

docs/source/examples/taggedFeature.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def fake_neuron(stepsize=0.001, offset=.8):
3333

3434

3535
def main():
36-
stepsize = 0.0001 # s
36+
stepsize = 0.0001 # s
3737
time, voltage, stimulus, spike_times = fake_neuron(stepsize=0.0001)
3838

3939
# create a new file overwriting any existing content
@@ -90,7 +90,7 @@ def plot_data(tag):
9090
stim_at_spike_time = np.zeros(len(tag.positions[:]))
9191

9292
for i in range(len(tag.positions)):
93-
stim_at_spike_time[i] = tag.feature_data(i, 0)[:]
93+
stim_at_spike_time[i] = tag.feature_data(i, 0)[:].item()
9494

9595
response_axis = plt.subplot2grid((2, 3), (0, 0), rowspan=1, colspan=2)
9696
stimulus_axis = plt.subplot2grid((2, 3), (1, 0), rowspan=1, colspan=2, sharex=response_axis)
@@ -109,7 +109,7 @@ def plot_data(tag):
109109
response_axis.legend(loc="lower center", ncol=2, fontsize=8)
110110

111111
stimulus_axis.plot(stimulus_time, stimulus, color="darkgray", label="stimulus", lw=1)
112-
stimulus_axis.scatter(spike_times, np.ones(spike_times.shape)*np.max(stimulus), color='red', label=tag.name)
112+
stimulus_axis.scatter(spike_times, np.ones(spike_times.shape) * np.max(stimulus), color='red', label=tag.name)
113113
stimulus_axis.set_xlabel(stim_time_dim.label + ((" [" + stim_time_dim.unit + "]") if stim_time_dim.unit else ""))
114114
stimulus_axis.set_ylabel(feature_data_array.label + ((" [" + feature_data_array.unit + "]") if feature_data_array.unit else ""))
115115
stimulus_axis.set_xlim(np.min(stimulus_time), np.max(stimulus_time))
@@ -121,6 +121,6 @@ def plot_data(tag):
121121
# plt.savefig('../images/tagged_feature.png')
122122
plt.show()
123123

124+
124125
if __name__ == '__main__':
125126
main()
126-

docs/source/examples/untaggedFeature.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
1616
"""
1717

18-
import nixio
1918
import lif
19+
import nixio
2020
import numpy as np
2121
import scipy.signal as signal
2222
import matplotlib.pylab as plt
@@ -25,15 +25,15 @@
2525
def fake_neuron(stepsize=0.001, offset=.8):
2626
stimulus = np.random.randn(82000) * 2.5
2727

28-
b, a = signal.butter(2, 12.5, fs=1/stepsize, btype="low")
28+
b, a = signal.butter(2, 12.5, fs=1 / stepsize, btype="low")
2929
stimulus = signal.filtfilt(b, a, stimulus[:])
3030
stimulus = stimulus[1000:-1000]
3131
s = np.hstack((np.zeros(10000), stimulus, np.zeros(10000)))
3232
lif_model = lif.LIF(stepsize=stepsize, offset=offset)
3333
time, v, spike_times = lif_model.run_stimulus(s)
3434

35-
stimulus_onset = 10000*stepsize
36-
stimulus_duration = len(stimulus)*stepsize
35+
stimulus_onset = 10000 * stepsize
36+
stimulus_duration = len(stimulus) * stepsize
3737

3838
return time, v, stimulus, stimulus_onset, stimulus_duration
3939

@@ -73,7 +73,7 @@ def plot_data(tag):
7373
response_axis.set_ylabel(data_array.label + ((" [" + data_array.unit + "]") if data_array.unit else ""))
7474
response_axis.set_xlim(0, np.max(time))
7575
response_axis.set_ylim((1.2 * np.min(voltage), 1.2 * np.max(voltage)))
76-
response_axis.barh((np.max(voltage) - np.min(voltage))/2, stimulus_duration, np.min(voltage) - np.max(voltage),
76+
response_axis.barh((np.max(voltage) - np.min(voltage)) / 2, stimulus_duration, np.min(voltage) - np.max(voltage),
7777
stimulus_onset, color='silver', alpha=0.5, zorder=0, label="stimulus epoch")
7878
response_axis.legend(fontsize=9, ncol=2, loc=9)
7979

nixio/datatype.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
# LICENSE file in the root of the Project.
99
from numbers import Integral, Real
1010
from six import string_types
11+
from packaging import version
1112

1213
import numpy as np
1314

@@ -26,7 +27,10 @@ class DataType(object):
2627
Int64 = np.int64
2728
Float = np.float32
2829
Double = np.double
29-
String = np.unicode_
30+
if version.parse(np.__version__) < version.parse("2.0"):
31+
String = np.unicode_
32+
else:
33+
String = np.str_
3034
Bool = np.bool_
3135

3236
# type groups

nixio/test/test_doc_examples.py

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import importlib.util as getmod
2-
import os
3-
import runpy
41
import sys
2+
import runpy
53
import unittest
4+
import importlib.util
5+
import importlib.machinery
6+
import matplotlib.pyplot as plt
67

78
from pathlib import Path
89
from shutil import copyfile
910

10-
import matplotlib.pyplot as plt
11-
12-
1311
TEST_IMAGE = "lenna.png"
1412

1513

@@ -23,22 +21,25 @@ def run_script(self, script_name):
2321

2422
def handle_lif(self):
2523
lif_path = Path.joinpath(self.examples_path, "lif.py")
26-
spec = getmod.spec_from_file_location("lif", str(lif_path))
27-
spec.loader.load_module("lif")
24+
print(lif_path)
25+
spec = importlib.util.spec_from_file_location("lif", lif_path)
26+
module = importlib.util.module_from_spec(spec)
27+
sys.modules["lif"] = module
28+
spec.loader.exec_module(module)
2829

2930
def handle_image(self):
3031
image_path = Path.joinpath(self.examples_path, TEST_IMAGE)
31-
copyfile(str(image_path), str(Path.joinpath(Path(os.getcwd()), TEST_IMAGE)))
32+
copyfile(str(image_path), str(Path.joinpath(Path(Path.cwd()), TEST_IMAGE)))
3233

3334
def setUp(self):
34-
curr_path = os.getcwd()
35-
if os.path.basename(curr_path) == "nixpy":
35+
curr_path = Path.cwd()
36+
if curr_path.stem == "nixpy":
3637
self.examples_path = Path.joinpath(Path(curr_path),
3738
"docs", "source", "examples")
38-
elif os.path.basename(curr_path) == "nixio":
39+
elif curr_path.stem == "nixio":
3940
self.examples_path = Path.joinpath(Path(curr_path).parent,
4041
"docs", "source", "examples")
41-
elif os.path.basename(curr_path) == "test":
42+
elif curr_path.stem == "test":
4243
self.examples_path = Path.joinpath(Path(curr_path).parent.parent,
4344
"docs", "source", "examples")
4445

@@ -48,28 +49,28 @@ def setUp(self):
4849
def tearDown(self):
4950
plt.close("all")
5051
plt.ioff()
51-
if os.path.exists(TEST_IMAGE):
52-
os.remove(TEST_IMAGE)
52+
if Path.exists(Path("TEST_IMAGE")):
53+
Path.unlink(Path(TEST_IMAGE))
5354

5455
def test_annotations(self):
5556
self.run_script("annotations.py")
5657
# cleanup
57-
os.remove("annotations.nix")
58+
Path.unlink(Path("annotations.nix"))
5859

5960
def test_category_data(self):
6061
self.run_script("categoryData.py")
6162
# cleanup
62-
os.remove("categoryData.nix")
63+
Path.unlink(Path("categoryData.nix"))
6364

6465
def test_continuous_recording(self):
6566
self.run_script("continuousRecording.py")
6667
# cleanup
67-
os.remove("continuous_recording.nix")
68+
Path.unlink(Path("continuous_recording.nix"))
6869

6970
def test_file_create(self):
7071
self.run_script("fileCreate.py")
7172
# cleanup
72-
os.remove("file_create_example.nix")
73+
Path.unlink(Path("file_create_example.nix"))
7374

7475
def test_image_data(self):
7576
# test will open image with an external program; does not work on windows
@@ -78,50 +79,50 @@ def test_image_data(self):
7879
self.handle_image()
7980
self.run_script("imageData.py")
8081
# cleanup
81-
os.remove("image_example.nix")
82+
Path.unlink(Path("image_example.nix"))
8283

8384
def test_image_with_metadata(self):
8485
# Requires PIL package and the "Lenna" image.
8586
self.handle_image()
8687
self.run_script("imageWithMetadata.py")
8788
# cleanup
88-
os.remove("image_with_source_example.h5")
89-
os.remove("image_with_metadata.png")
89+
Path.unlink(Path("image_with_source_example.h5"))
90+
Path.unlink(Path("image_with_metadata.png"))
9091

9192
def test_irregularly_sampled_data(self):
9293
self.run_script("irregularlySampledData.py")
9394
# cleanup
94-
os.remove("irregular_data_example.nix")
95+
Path.unlink(Path("irregular_data_example.nix"))
9596

9697
def test_lif(self):
9798
self.run_script("lif.py")
9899

99100
def test_multiple_points(self):
100101
self.run_script("multiple_points.py")
101102
# cleanup
102-
os.remove("multiple_points.nix")
103+
Path.unlink(Path("multiple_points.nix"))
103104

104105
def test_multiple_regions(self):
105106
self.run_script("multiple_regions.py")
106107
# cleanup
107-
os.remove("multiple_regions.nix")
108+
Path.unlink(Path("multiple_regions.nix"))
108109

109110
def test_multiple_rois(self):
110111
# Requires PIL package and the "Lenna" image.
111112
self.handle_image()
112113
self.run_script("multipleROIs.py")
113114
# cleanup
114-
os.remove("multiple_roi.nix")
115+
Path.unlink(Path("multiple_roi.nix"))
115116

116117
def test_range_dimension_link(self):
117118
self.run_script("rangeDimensionLink.py")
118119
# cleanup
119-
os.remove("range_link.nix")
120+
Path.unlink(Path("range_link.nix"))
120121

121122
def test_regularly_sampled_data(self):
122123
self.run_script("regularlySampledData.py")
123124
# cleanup
124-
os.remove("regular_data_example.nix")
125+
Path.unlink(Path("regular_data_example.nix"))
125126

126127
def test_single_roi(self):
127128
# test will open image with an external program; does not work on windows
@@ -130,44 +131,44 @@ def test_single_roi(self):
130131
self.handle_image()
131132
self.run_script("singleROI.py")
132133
# cleanup
133-
os.remove("single_roi.nix")
134+
Path.unlink(Path("single_roi.nix"))
134135

135136
def test_sources(self):
136137
self.run_script("sources.py")
137138
# cleanup
138-
os.remove("sources.nix")
139+
Path.unlink(Path("sources.nix"))
139140

140141
def test_spike_features(self):
141142
# Requires scipy package and "lif.py"
142143
self.handle_lif()
143144
self.run_script("spikeFeatures.py")
144145
# cleanup
145-
os.remove("spike_features.h5")
146+
Path.unlink(Path("spike_features.h5"))
146147

147148
def test_spike_tagging(self):
148149
# Requires "lif.py"
149150
self.handle_lif()
150151
self.run_script("spikeTagging.py")
151152
# cleanup
152-
os.remove("spike_tagging.nix")
153+
Path.unlink(Path("spike_tagging.nix"))
153154

154155
def test_tabular_data(self):
155156
self.run_script("tabulardata.py")
156157
# cleanup
157-
os.remove("dataframe.nix")
158+
Path.unlink(Path("dataframe.nix"))
158159

159160
def test_tagged_feature(self):
160161
# Requires scipy package and "lif.py"
161162
self.handle_lif()
162163
self.run_script("taggedFeature.py")
163164
# cleanup
164-
os.remove("spike_features.nix")
165+
Path.unlink(Path("spike_features.nix"))
165166

166167
def test_tagging_example(self):
167168
# Requires scipy package
168169
self.run_script("tagging_example.py")
169170
# cleanup
170-
os.remove("tagging1.nix")
171+
Path.unlink(Path("tagging1.nix"))
171172

172173
def test_tagging_nd(self):
173174
# not testing any nix feature
@@ -179,4 +180,4 @@ def test_untagged_feature(self):
179180
self.handle_lif()
180181
self.run_script("untaggedFeature.py")
181182
# cleanup
182-
os.remove("untagged_feature.h5")
183+
Path.unlink(Path("untagged_feature.h5"))

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def get_wheel_data():
7878
tests_require=['pytest', 'scipy', 'pillow', 'matplotlib'],
7979
test_suite='pytest',
8080
setup_requires=['pytest-runner'],
81-
install_requires=['numpy', 'h5py', 'six', 'enum34;python_version<"3.4"'],
81+
install_requires=['numpy<2.0', 'h5py', 'six', 'enum34;python_version<"3.4"'],
8282
package_data={'nixio': [license_text, description_text]},
8383
include_package_data=True,
8484
zip_safe=False,

0 commit comments

Comments
 (0)