Skip to content

Commit

Permalink
Merge pull request #18 from Ouranosinc/fix-15
Browse files Browse the repository at this point in the history
Avoid inplace sort of input of lmom_ratios
  • Loading branch information
aulemahal authored Jul 25, 2024
2 parents a1305a9 + 6c3010b commit e3f8de3
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ version 1.0.7 (unreleased)
--------------------------
- Fix algorithm convergence for the `kappa` distribution.
- Added Python 3.12 to the test suite and supported versions.
- Avoid inplace modifications of input arrays in ``lmom_ratios``.

version 1.0.6 (2023-07-06)
--------------------------
Expand Down
4 changes: 2 additions & 2 deletions lmoments3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def _samlmularge(x, nmom=5):
try:
x = np.asarray(x, dtype=np.float64)
n = len(x)
x.sort()
x = np.sort(x)
except ValueError:
raise ValueError("Input data to estimate L-moments must be numeric.")

Expand Down Expand Up @@ -142,7 +142,7 @@ def _samlmusmall(x, nmom=5):
try:
x = np.asarray(x, dtype=np.float64)
n = len(x)
x.sort()
x = np.sort(x)
except ValueError:
raise ValueError("Input data to estimate L-moments must be numeric.")

Expand Down
6 changes: 3 additions & 3 deletions lmoments3/distr.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def _lmom_ratios(self, *shapes, locs, scale, nmom):
"""When overriding, *shapes can be replaced by the actual distribution's shape parameter(s), if any."""
raise NotImplementedError

def lmom_fit(self, data=[], lmom_ratios=[]):
def lmom_fit(self, data=None, lmom_ratios=None):
"""Fit the distribution function to the given data or given L-moments.
:param data: Data to use in calculating the distribution parameters
Expand All @@ -59,11 +59,11 @@ def lmom_fit(self, data=[], lmom_ratios=[]):
:rtype: :class:`OrderedDict`
"""
n_min = self.numargs + 2
if len(data) > 0:
if data is not None:
if len(data) <= n_min:
raise ValueError(f"At least {n_min} data points must be provided.")
lmom_ratios = lm.lmom_ratios(data, nmom=n_min)
elif not lmom_ratios:
elif lmom_ratios is None:
raise Exception("Either `data` or `lmom_ratios` must be provided.")
elif len(lmom_ratios) < n_min:
raise ValueError(f"At least {n_min} number of L-moments must be provided.")
Expand Down
5 changes: 4 additions & 1 deletion lmoments3/tests/test_lmoments.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest

import numpy as np
from numpy.testing import assert_almost_equal

import lmoments3 as lm
Expand All @@ -9,10 +10,12 @@

class TestLmoments(unittest.TestCase):
def test_samlmu(self):
testdata = [2.0, 3.0, 4.0, 2.4, 5.5, 1.2, 5.4, 2.2, 7.1, 1.3, 1.5]
testdata = np.array([2.0, 3.0, 4.0, 2.4, 5.5, 1.2, 5.4, 2.2, 7.1, 1.3, 1.5])
expected = [3.23636364, 1.14181818, 0.27388535, 0.02335456, -0.04246285]
result = lm.lmom_ratios(testdata)
assert_almost_equal(result, expected)
# Ensure input was not modified by `lmom_ratios`.
assert all(testdata[-3:] == [7.1, 1.3, 1.5])

def test_aic(self):
data = [
Expand Down

0 comments on commit e3f8de3

Please sign in to comment.