Skip to content

Commit

Permalink
bug fix for merrors getitem (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
HDembinski authored Jun 14, 2020
1 parent 788158b commit b8b40a2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
4 changes: 2 additions & 2 deletions doc/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Minuit
.. autoclass:: Minuit
:members:
:undoc-members:
:exclude-members: is_fixed, print_all_minos, print_fmin, print_param, print_initial_param, print_matrix, set_errordef, set_strategy, set_print_level, set_up, from_array_func, get_merrors, list_of_vary_param, list_of_fixed_param, get_fmin, get_initial_param_states, get_param_states, get_num_call_fcn, get_num_call_grad, matrix_accurate, merrors, migrad_ok
:exclude-members: is_fixed, print_all_minos, print_fmin, print_param, print_initial_param, print_matrix, set_errordef, set_strategy, set_print_level, set_up, from_array_func, get_merrors, list_of_vary_param, list_of_fixed_param, get_fmin, get_initial_param_states, get_param_states, get_num_call_fcn, get_num_call_grad, matrix_accurate, migrad_ok, merrors_struct

.. automethod:: from_array_func

Expand Down Expand Up @@ -128,7 +128,7 @@ Minos Data Object
~~~~~~~~~~~~~~~~~

Subclass of NamedTuple which stores information about the Minos result. It is returned by :meth:`Minuit.minos`
(as part of a dictionary from parameter name -> data object). You can get it also from :meth:`Minuit.get_merrors`. It has the following attributes:
(as part of a dictionary from parameter name -> data object). You can get it also from :meth:`Minuit.merrors`. It has the following attributes:

* *lower*: lower error value

Expand Down
8 changes: 6 additions & 2 deletions src/iminuit/tests/test_deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ def minuit():

def test_minos_merrors(minuit):
minuit.minos()
m = minuit.merrors
with pytest.warns(DeprecationWarning):
assert minuit.merrors[("x", -1.0)] == approx(-1)
assert m[("x", -1)] == approx(-1)
with pytest.warns(DeprecationWarning):
assert minuit.merrors[("x", 1.0)] == approx(1)
assert m[("x", 1)] == approx(1)
with pytest.warns(DeprecationWarning):
with pytest.raises(ValueError):
m[("x", 2)]


def test_default_errordef():
Expand Down
8 changes: 8 additions & 0 deletions src/iminuit/tests/test_iminuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,14 @@ def test_minos_all(grad, sigma):
assert m.merrors["y"].upper == approx(sigma * 1)
assert m.merrors[0].lower == approx(-sigma * 2)
assert m.merrors[1].upper == approx(sigma * 1)
assert m.merrors[-1].upper == approx(sigma * 1)

with pytest.raises(KeyError):
m.merrors["xy"]
with pytest.raises(KeyError):
m.merrors["z"]
with pytest.raises(IndexError):
m.merrors[-3]


@pytest.mark.parametrize("grad", (None, func0_grad))
Expand Down
35 changes: 23 additions & 12 deletions src/iminuit/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,26 +227,37 @@ def _repr_pretty_(self, p, cycle):

def __getitem__(self, key):
is_deprecated_call = False
try:
key, ul = key
ul = int(ul)
is_deprecated_call = True
warnings.warn(
"`merrors[(name, +-1)]` is deprecated; use `merrors[name]`",
DeprecationWarning,
stacklevel=2,
)
except (ValueError, TypeError):
pass

if isinstance(key, int):
if key >= len(self):
raise IndexError("index out of range")
if key < 0:
key += len(self)
if key < 0:
raise IndexError("index out of range")
for k in self.keys():
if key == 0:
key = k
break
key -= 1
else:
if not isinstance(key, str):
try:
k, ul = key
ul = int(ul)
is_deprecated_call = True
key = k
warnings.warn(
"`merrors[(name, +-1)]` is deprecated; use `merrors[name]`",
DeprecationWarning,
stacklevel=2,
)
except (ValueError, TypeError):
pass

v = super(MErrors, self).__getitem__(key)
if is_deprecated_call:
if ul not in (-1, 1):
raise ValueError("key must be (name, 1) or (name, -1)")
return v.lower if ul == -1 else v.upper
return v

Expand Down

0 comments on commit b8b40a2

Please sign in to comment.