Skip to content

Commit

Permalink
Merge pull request #417 from CPJKU/scipy_derivative_deprecation
Browse files Browse the repository at this point in the history
Replaced deprecated scipy derivative
  • Loading branch information
fosfrancesco authored Jan 24, 2025
2 parents d86cd22 + 6e82c27 commit e204c08
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
6 changes: 3 additions & 3 deletions partitura/musicanalysis/performance_codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
from partitura.performance import PerformedPart, PerformanceLike
from partitura.musicanalysis import note_features
from partitura.utils.misc import deprecated_alias
from partitura.utils.generic import interp1d, monotonize_times
from partitura.utils.generic import interp1d, monotonize_times, first_order_derivative
from partitura.utils.music import ensure_notearray
from scipy.misc import derivative


__all__ = ["encode_performance", "decode_performance", "to_matched_score"]

Expand Down Expand Up @@ -595,7 +595,7 @@ def tempo_by_derivative(
if input_onsets is None:
input_onsets = unique_s_onsets[:-1]

tempo_curve = derivative(onset_fun, input_onsets, dx=0.5)
tempo_curve = first_order_derivative(onset_fun, input_onsets, dx=0.5)

if return_onset_idxs:
return tempo_curve, input_onsets, unique_onset_idxs
Expand Down
32 changes: 32 additions & 0 deletions partitura/utils/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,38 @@ def iter_subclasses(cls, _seen=None):
yield sub


def first_order_derivative(func, x0, dx=0.5):
"""
Compute the first order derivative of a function at a point `x0` using
a central difference scheme. This function is an adaptation of the scipy _derivative
function, which is no longer available in the public API after version 1.15.
More information can be found at:
https://github.com/scipy/scipy/blob/v1.7.0/scipy/misc/common.py#L75-L145
Parameters
----------
func: callable
Function to differentiate
x0: np.ndarray
Points at which to differentiate
dx: float
Step size
Returns
-------
np.ndarray
First order derivative of `func` at `x0`
"""
# fixed number of points to use, must be odd.
num_points = 3
weights = np.array([-1, 0, 1]) / 2.0
val = 0.0
ho = num_points >> 1
for k in range(num_points):
val += weights[k] * func(x0 + (k - ho) * dx)
return val / dx


class ReplaceRefMixin(object):
"""This class is a utility mixin class to replace references to
objects with references to other objects. This is functionality is
Expand Down

0 comments on commit e204c08

Please sign in to comment.