Skip to content

Commit 4fa614f

Browse files
committed
Import numpy as np, remove end of line, improve docstring
1 parent 3ec44dc commit 4fa614f

File tree

8 files changed

+45
-56
lines changed

8 files changed

+45
-56
lines changed

src/diffpy/utils/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,3 @@
2020

2121
# silence the pyflakes syntax checker
2222
assert __version__ or True
23-
24-
# End of file

src/diffpy/utils/parsers/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,3 @@
1515

1616
"""Various utilities related to data parsing and manipulation.
1717
"""
18-
19-
# End of file

src/diffpy/utils/parsers/loaddata.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,3 @@ def isfloat(s):
344344
except ValueError:
345345
pass
346346
return False
347-
348-
349-
# End of file

src/diffpy/utils/resampler.py

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,52 +15,50 @@
1515

1616
"""Various utilities related to data parsing and manipulation."""
1717

18-
import numpy
18+
import numpy as np
1919

2020

21-
# NOTE - this should be faster than resample below and conforms more closely to
22-
# numpy.interp. I'm keeping resample for legacy reasons.
2321
def wsinterp(x, xp, fp, left=None, right=None):
2422
"""One-dimensional Whittaker-Shannon interpolation.
2523
26-
This uses the Whittaker-Shannon interpolation formula to interpolate the value of fp (array),
27-
which is defined over xp (array), at x (array or float).
24+
Reconstruct a continuous signal from discrete data points by utilizing sinc functions
25+
as interpolation kernels. This function interpolates the values of fp (array),
26+
which are defined over xp (array), at new points x (array or float).
2827
2928
Parameters
3029
----------
3130
x: ndarray
32-
Desired range for interpolation.
31+
The x values at which interpolation is computed.
3332
xp: ndarray
34-
Defined range for fp.
33+
The array of known x values.
3534
fp: ndarray
36-
Function to be interpolated.
35+
The array of y values associated xp.
3736
left: float
3837
If given, set fp for x < xp[0] to left. Otherwise, if left is None (default) or not given,
3938
set fp for x < xp[0] to fp evaluated at xp[-1].
4039
right: float
4140
If given, set fp for x > xp[-1] to right. Otherwise, if right is None (default) or not given, set fp for
42-
x > xp[-1] to fp evaluated at xp[-1].
41+
x > xp[-1] to fp evaluated at xp[-1]
4342
4443
Returns
4544
-------
46-
float:
47-
If input x is a scalar (not an array), return the interpolated value at x.
48-
ndarray:
49-
If input x is an array, return the interpolated array with dimensions of x.
45+
ndarray or float
46+
Interpolated values at points x. Returns a single float if x is a scalar,
47+
otherwise returns a numpy.ndarray.
5048
"""
51-
scalar = numpy.isscalar(x)
49+
scalar = np.isscalar(x)
5250
if scalar:
53-
x = numpy.array(x)
51+
x = np.array(x)
5452
x.resize(1)
5553
# shape = (nxp, nx), nxp copies of x data span axis 1
56-
u = numpy.resize(x, (len(xp), len(x)))
54+
u = np.resize(x, (len(xp), len(x)))
5755
# Must take transpose of u for proper broadcasting with xp.
5856
# shape = (nx, nxp), v(xp) data spans axis 1
5957
v = (xp - u.T) / (xp[1] - xp[0])
6058
# shape = (nx, nxp), m(v) data spans axis 1
61-
m = fp * numpy.sinc(v)
59+
m = fp * np.sinc(v)
6260
# Sum over m(v) (axis 1)
63-
fp_at_x = numpy.sum(m, axis=1)
61+
fp_at_x = np.sum(m, axis=1)
6462

6563
# Enforce left and right
6664
if left is None:
@@ -100,36 +98,33 @@ def resample(r, s, dr):
10098
dr0 = r[1] - r[0] # Constant timestep
10199

102100
if dr0 < dr:
103-
rnew = numpy.arange(r[0], r[-1] + 0.5 * dr, dr)
104-
snew = numpy.interp(rnew, r, s)
101+
rnew = np.arange(r[0], r[-1] + 0.5 * dr, dr)
102+
snew = np.interp(rnew, r, s)
105103
return rnew, snew
106104

107105
elif dr0 > dr:
108106
# Tried to pad the end of s to dampen, but nothing works.
109107
# m = (s[-1] - s[-2]) / dr0
110108
# b = (s[-2] * r[-1] - s[-1] * r[-2]) / dr0
111-
# rpad = r[-1] + numpy.arange(1, len(s))*dr0
109+
# rpad = r[-1] + np.arange(1, len(s))*dr0
112110
# spad = rpad * m + b
113-
# spad = numpy.concatenate([s,spad])
114-
# rnew = numpy.arange(0, rpad[-1], dr)
115-
# snew = numpy.zeros_like(rnew)
111+
# spad = np.concatenate([s,spad])
112+
# rnew = np.arange(0, rpad[-1], dr)
113+
# snew = np.zeros_like(rnew)
116114
# Accommodate for the fact that r[0] might not be 0
117115
# u = (rnew-r[0]) / dr0
118116
# for n in range(len(spad)):
119-
# snew += spad[n] * numpy.sinc(u - n)
117+
# snew += spad[n] * np.sinc(u - n)
120118

121-
# sel = numpy.logical_and(rnew >= r[0], rnew <= r[-1])
119+
# sel = np.logical_and(rnew >= r[0], rnew <= r[-1])
122120

123-
rnew = numpy.arange(0, r[-1], dr)
124-
snew = numpy.zeros_like(rnew)
121+
rnew = np.arange(0, r[-1], dr)
122+
snew = np.zeros_like(rnew)
125123
u = (rnew - r[0]) / dr0
126124
for n in range(len(s)):
127-
snew += s[n] * numpy.sinc(u - n)
128-
sel = numpy.logical_and(rnew >= r[0], rnew <= r[-1])
125+
snew += s[n] * np.sinc(u - n)
126+
sel = np.logical_and(rnew >= r[0], rnew <= r[-1])
129127
return rnew[sel], snew[sel]
130128

131129
# If we got here, then no resampling is required
132130
return r.copy(), s.copy()
133-
134-
135-
# End of file

src/diffpy/utils/version.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,3 @@
2222
from importlib.metadata import version
2323

2424
__version__ = version("diffpy.utils")
25-
26-
# End of file

src/diffpy/utils/wx/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,3 @@
1515

1616
"""Utilities related wx Python GUIs.
1717
"""
18-
19-
# End of file

src/diffpy/utils/wx/gridutils.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,3 @@ def _indicesToBlocks(indices):
163163
i0 = i
164164
rv = [tuple(ij) for ij in rngs]
165165
return rv
166-
167-
168-
# End of file

tests/test_resample.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
1+
import random
2+
13
import numpy as np
24
import pytest
35

46
from diffpy.utils.resampler import wsinterp
57

68

79
def test_wsinterp():
8-
import random
9-
10-
# Check known points are unchanged by interpolation
1110
# FIXME: if another SW interp function exists, run comparisons for interpolated points
11+
1212
# Sampling rate
1313
ssr = 44100**-1 # Standard sampling rate for human-hearable frequencies
14-
t = ssr
14+
15+
# Creating a symmetric set of sample points around zero.
1516
n = 5
16-
xp = np.array([i * t for i in range(-n, n + 1, 1)])
17-
x = np.array([i * t for i in range(-n - 1, n + 2, 1)])
17+
xp = np.array([i * ssr for i in range(-n, n + 1, 1)])
18+
x = np.array([i * ssr for i in range(-n - 1, n + 2, 1)])
19+
assert len(xp) == 11 and len(x) == 13
1820

19-
# Interpolate a few random datasets
21+
# Generating fp values across 10 trial runs
2022
trials = 10
21-
for trial in range(trials):
22-
fp = np.array([random.random() * ssr for i in range(-n, n + 1, 1)])
23+
24+
for _ in range(trials):
25+
# Create random function values (fp) at the points defined in xp above
26+
fp = np.array([random.random() * ssr for _ in range(-n, n + 1, 1)])
27+
28+
# Interpolate the values at new x points
2329
fp_at_x = wsinterp(x, xp, fp)
30+
31+
# Check that the known points are unchanged by interpolation
2432
assert np.allclose(fp_at_x[1:-1], fp)
2533
for i in range(len(x)):
2634
assert fp_at_x[i] == pytest.approx(wsinterp(x[i], xp, fp))

0 commit comments

Comments
 (0)