Skip to content

Commit

Permalink
more lynting
Browse files Browse the repository at this point in the history
  • Loading branch information
scfleming committed Jan 16, 2025
1 parent 0e1397f commit 87139b7
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 35 deletions.
2 changes: 1 addition & 1 deletion astronify/series/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ def sonify_preview(self, plotting=True, verbose=False):
self.amplitudes = np.asarray(area_vals) / total_area

if std_dev_norm == 0.0:
std_dev_norm = 1.0
std_dev_norm = 1.0

# Set the tremolo values based on the standard deviation of the piece normalized by the
# `std_dev_norm` factor.
Expand Down
111 changes: 77 additions & 34 deletions astronify/utils/tests/test_pitch_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,93 +17,130 @@ def test_data_to_pitch():
# basic linear stretch
data_arr = np.array([[1.0, 0.0, 0.25, 0.75]])
pitch_arr = data_arr * (pitch_range[1] - pitch_range[0]) + pitch_range[0]

assert (
pitch_arr == data_to_pitch(data_arr, pitch_range, center_pitch,
stretch='linear')).all()
pitch_arr
== data_to_pitch(data_arr, pitch_range, center_pitch, stretch='linear')
).all()

# invert
pitch_arr = pitch_range[1] - data_arr * (pitch_range[1] - pitch_range[0])
assert (
pitch_arr == data_to_pitch(data_arr, pitch_range, center_pitch,
stretch='linear', invert=True)).all()
pitch_arr
== data_to_pitch(
data_arr, pitch_range, center_pitch, stretch='linear', invert=True
)
).all()

# linear stretch where input image must be scaled
# linear stretch where input image must be scaled
data_arr = np.array([10.0, 20.0, 12.5, 17.5])
pitch_arr = ((data_arr - data_arr.min()) / (data_arr.max() - data_arr.min()) *
(pitch_range[1] - pitch_range[0])) + pitch_range[0]
pitch_arr = (
(data_arr - data_arr.min())
/ (data_arr.max() - data_arr.min())
* (pitch_range[1] - pitch_range[0])
) + pitch_range[0]
assert (
pitch_arr == data_to_pitch(data_arr, pitch_range, center_pitch,
stretch='linear')).all()
pitch_arr
== data_to_pitch(data_arr, pitch_range, center_pitch, stretch='linear')
).all()

# linear stretch with non-equal lower/upper pitch ranges
data_arr = np.array([[1.0, 0.0, 0.25, 0.75]])
pitch_arr = data_arr * (pitch_range[1] - pitch_range[0]) + pitch_range[0]

pitch_range = [300, 500]
assert (
pitch_arr == data_to_pitch(data_arr, [300, 500],
center_pitch, stretch="linear")).all()
pitch_arr == data_to_pitch(data_arr, [300, 500], center_pitch, stretch="linear")
).all()
pitch_range = [400, 600]
assert (
pitch_arr == data_to_pitch(data_arr, [400, 600],
center_pitch, stretch="linear")).all()
pitch_arr == data_to_pitch(data_arr, [400, 600], center_pitch, stretch="linear")
).all()
pitch_range = [400, 500]

# min_max val
minval, maxval = 0, 1
data_arr = np.array([1, 0, -1, 2])
pitch_arr = data_to_pitch(
data_arr, pitch_range, center_pitch,
stretch="linear", minmax_value=[minval, maxval])
data_arr,
pitch_range,
center_pitch,
stretch="linear",
minmax_value=[minval, maxval]
)
data_arr[data_arr < minval] = minval
data_arr[data_arr > maxval] = maxval
manual_pitch_arr = data_arr * (pitch_range[1] - pitch_range[0]) + pitch_range[0]
assert (manual_pitch_arr == pitch_arr).all()

minval, maxval = 0, 1
data_arr = np.array([1.0, 0.0, 0.25, 0.75])
pitch_arr = data_to_pitch(data_arr, pitch_range, center_pitch,
stretch="linear", minmax_value=[minval, maxval])
pitch_arr = data_to_pitch(
data_arr,
pitch_range,
center_pitch,
stretch="linear",
minmax_value=[minval, maxval]
)
data_arr[data_arr < minval] = minval
data_arr[data_arr > maxval] = maxval
manual_pitch_arr = data_arr * (pitch_range[1] - pitch_range[0]) + pitch_range[0]
assert (manual_pitch_arr == pitch_arr).all()

# min_max percent
data_arr = np.array([1.1, -0.1, 1.0, 0.0, 0.25, 0.75])
pitch_arr = data_to_pitch(data_arr, pitch_range, center_pitch,
stretch="linear", minmax_percent=[20, 80])
pitch_arr = data_to_pitch(
data_arr, pitch_range, center_pitch, stretch="linear", minmax_percent=[20, 80]
)
assert (
np.isclose(pitch_arr, np.array([500, 400, 500, 400,
422.22222222, 477.77777778]))).all()
np.isclose(
pitch_arr, np.array([500, 400, 500, 400, 422.22222222, 477.77777778]))
).all()

# asinh
data_arr = np.array([1.0, 0.0, 0.25, 0.75])
zero_point = 0.21271901209248895
pitch_arr = data_to_pitch(data_arr, pitch_range, center_pitch, zero_point, stretch="asinh")
manual_pitch_arr = np.arcsinh(data_arr * 10) / np.arcsinh(10) * (pitch_range[1] - pitch_range[0]) + pitch_range[0]
pitch_arr = data_to_pitch(
data_arr, pitch_range, center_pitch, zero_point, stretch="asinh"
)
manual_pitch_arr = (
np.arcsinh(data_arr * 10) / np.arcsinh(10) * (pitch_range[1] - pitch_range[0])
+ pitch_range[0]
)
assert (manual_pitch_arr == pitch_arr).all()

# sinh
data_arr = np.array([1.0, 0.0, 0.25, 0.75])
zero_point = 0.7713965391706435
pitch_arr = data_to_pitch(data_arr, pitch_range, center_pitch, zero_point, stretch="sinh")
manual_pitch_arr = np.sinh(data_arr * 3) / np.sinh(3) * (pitch_range[1] - pitch_range[0]) + pitch_range[0]
pitch_arr = data_to_pitch(
data_arr, pitch_range, center_pitch, zero_point, stretch="sinh"
)
manual_pitch_arr = (
np.sinh(data_arr * 3) / np.sinh(3) * (pitch_range[1] - pitch_range[0])
+ pitch_range[0]
)
assert (manual_pitch_arr == pitch_arr).all()

# sqrt
data_arr = np.array([1.0, 0.0, 0.25, 0.75])
zero_point = 0.25
pitch_arr = data_to_pitch(data_arr, pitch_range, center_pitch, zero_point, stretch="sqrt")
manual_pitch_arr = np.sqrt(data_arr) * (pitch_range[1] - pitch_range[0]) + pitch_range[0]
pitch_arr = data_to_pitch(
data_arr, pitch_range, center_pitch, zero_point, stretch="sqrt"
)
manual_pitch_arr = (
np.sqrt(data_arr) * (pitch_range[1] - pitch_range[0]) + pitch_range[0]
)
assert (manual_pitch_arr == pitch_arr).all()

# log
data_arr = np.array([1.0, 0.0, 0.25, 0.75])
zero_point = 0.030638584039112748
pitch_arr = data_to_pitch(data_arr, pitch_range, center_pitch, zero_point, stretch="log")
manual_pitch_arr = np.log(1000 * data_arr + 1) / np.log(1001) * (pitch_range[1] - pitch_range[0]) + pitch_range[0]
pitch_arr = data_to_pitch(
data_arr, pitch_range, center_pitch, zero_point, stretch="log"
)
manual_pitch_arr = (
np.log(1000 * data_arr + 1) / np.log(1001) * (pitch_range[1] - pitch_range[0]) + pitch_range[0]
)
assert (manual_pitch_arr == pitch_arr).all()

# Bad stretch
Expand All @@ -113,10 +150,16 @@ def test_data_to_pitch():

# Giving both minmax percent and cut
data_arr = np.array([1.1, -0.1, 1.0, 0.0, 0.25, 0.75])
pitch_arr = data_to_pitch(data_arr, pitch_range, center_pitch, stretch="linear", minmax_percent=[20, 80])
pitch_arr = data_to_pitch(
data_arr, pitch_range, center_pitch, stretch="linear", minmax_percent=[20, 80]
)
with pytest.warns(InputWarning):
test_arr = data_to_pitch(
data_arr, pitch_range, center_pitch, stretch="linear",
minmax_value=[0, 1], minmax_percent=[20, 80])
data_arr,
pitch_range,
center_pitch,
stretch="linear",
minmax_value=[0, 1],
minmax_percent=[20, 80]
)
assert (pitch_arr == test_arr).all()

0 comments on commit 87139b7

Please sign in to comment.