Skip to content

Commit

Permalink
use importlib.resources instead of __file__
Browse files Browse the repository at this point in the history
  • Loading branch information
rgommers committed Feb 21, 2024
1 parent 2f12a21 commit 9a03baf
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions pywt/data/_readers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import importlib
import os

import numpy as np
Expand Down Expand Up @@ -36,8 +37,8 @@ def ascent():
>>> plt.show() # doctest: +SKIP
"""
fname = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'ascent.npz')
with open(fname, 'rb') as f:
_datadir = importlib.resources.files('pywt.data')
with importlib.resources.as_file(_datadir.joinpath('ascent.npz')) as f:
ascent = np.load(f)['data']

return ascent
Expand Down Expand Up @@ -73,8 +74,8 @@ def aero():
>>> plt.show() # doctest: +SKIP
"""
fname = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'aero.npz')
with open(fname, 'rb') as f:
_datadir = importlib.resources.files('pywt.data')
with importlib.resources.as_file(_datadir.joinpath('aero.npz')) as f:
aero = np.load(f)['data']

return aero
Expand Down Expand Up @@ -121,8 +122,8 @@ def camera():
>>> plt.show() # doctest: +SKIP
"""
fname = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'camera.npz')
with open(fname, 'rb') as f:
_datadir = importlib.resources.files('pywt.data')
with importlib.resources.as_file(_datadir.joinpath('camera.npz')) as f:
camera = np.load(f)['data']

return camera
Expand Down Expand Up @@ -153,8 +154,8 @@ def ecg():
[<matplotlib.lines.Line2D object at ...>]
>>> plt.show() # doctest: +SKIP
"""
fname = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'ecg.npy')
with open(fname, 'rb') as f:
_datadir = importlib.resources.files('pywt.data')
with importlib.resources.as_file(_datadir.joinpath('ecg.npy')) as f:
ecg = np.load(f)

return ecg
Expand Down Expand Up @@ -191,8 +192,8 @@ def nino():
[<matplotlib.lines.Line2D object at ...>]
>>> plt.show() # doctest: +SKIP
"""
fname = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'sst_nino3.npy')
with open(fname, 'rb') as f:
_datadir = importlib.resources.files('pywt.data')
with importlib.resources.as_file(_datadir.joinpath('sst_nino3.npy')) as f:
sst_csv = np.load(f)

# sst_csv = pd.read_csv("http://www.cpc.ncep.noaa.gov/data/indices/ersst4.nino.mth.81-10.ascii", sep=' ', skipinitialspace=True)
Expand Down

0 comments on commit 9a03baf

Please sign in to comment.