Skip to content

Commit 0e2dfae

Browse files
authored
Fix of matrix_format (#843)
Closes #840 This fixes an ugly bug that produced non-sense text representations of the covariance matrix. The numerical values in the matrix were correct, but the text representation, which aims to make the numbers look reasonable for humans, produced a completely wrong result, see #840 for how badly that failed. To make things worse, this wrong behavior was actually accepted by a unit test, which expected the wrong outcome 🤦
1 parent 3149dea commit 0e2dfae

File tree

10 files changed

+38
-37
lines changed

10 files changed

+38
-37
lines changed

src/iminuit/_repr_html.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ def matrix(arr):
283283

284284
n = len(names)
285285

286-
nums = matrix_format(arr.flatten())
286+
nums = matrix_format(arr)
287287

288288
grad = ColorGradient(
289289
(-1.0, 120.0, 120.0, 250.0),

src/iminuit/_repr_text.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def matrix(arr):
180180
names = tuple(arr._var2pos)
181181

182182
n = len(arr)
183-
nums = matrix_format(arr.flatten())
183+
nums = matrix_format(arr)
184184

185185
def row_fmt(args):
186186
s = "│ " + args[0] + " │"
@@ -209,6 +209,13 @@ def row_fmt(args):
209209
return "\n".join(lines)
210210

211211

212-
def matrix_format(values):
213-
s = [f"{v:.3g}" % v for v in values]
214-
return _strip(s)
212+
def matrix_format(matrix):
213+
r = []
214+
for i in range(matrix.shape[0]):
215+
for j in range(matrix.shape[1]):
216+
if i == j:
217+
r.append(f"{matrix[i, i]:.3g}")
218+
else:
219+
x = pdg_format(matrix[i, j], matrix[i, i], matrix[j, j])[0]
220+
r.append(x)
221+
return r

src/iminuit/pdg_format.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ def pdg_format(value, error, *errors, labels=None, format=term, leader=None, exp
159159

160160

161161
def _strip(items: List[str]) -> List[str]:
162+
assert all("e" not in x for x in items)
162163
# ignore inf and nan
163164
mask = tuple(i for (i, s) in enumerate(items) if "." in s)
164165
if mask:

src/iminuit/util.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -289,16 +289,9 @@ def __array_finalize__(self, obj: Any) -> None:
289289
else:
290290
self._var2pos = getattr(obj, "_var2pos", {})
291291

292-
def __getitem__( # type:ignore # mypy complains about incompatible signatures
292+
def __getitem__( # type:ignore
293293
self,
294-
key: Union[
295-
int,
296-
str,
297-
Tuple[Union[int, str], Union[int, str]],
298-
Iterable[Union[int, str]],
299-
NDArray,
300-
slice,
301-
],
294+
key: Union[Key, Tuple[Key, Key], Iterable[Key], NDArray],
302295
) -> NDArray:
303296
"""Get matrix element at key."""
304297
var2pos = self._var2pos
@@ -358,7 +351,7 @@ def to_table(self) -> Tuple[List[List[str]], Tuple[str, ...]]:
358351
y -0 4
359352
"""
360353
names = tuple(self._var2pos) # type:ignore
361-
nums = _repr_text.matrix_format(self.flatten()) # type:ignore
354+
nums = _repr_text.matrix_format(self)
362355
tab = []
363356
n = len(self)
364357
for i, name in enumerate(names):

tests/matrix.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
<tr>
88
<th> x </th>
99
<td> 1 </td>
10-
<td style="background-color:rgb(250,250,250);color:black"> 0 </td>
10+
<td style="background-color:rgb(250,250,250);color:black"> 0.00 </td>
1111
</tr>
1212
<tr>
1313
<th> y </th>
14-
<td style="background-color:rgb(250,250,250);color:black"> 0 </td>
14+
<td style="background-color:rgb(250,250,250);color:black"> 0.00 </td>
1515
<td> 0.25 </td>
1616
</tr>
1717
</table>

tests/matrix.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
┌───┬───────────┐
2-
│ │ x y │
3-
├───┼───────────┤
4-
│ x │ 1 -0
5-
│ y │ -0 0.25 │
6-
└───┴───────────┘
1+
┌───┬─────────────
2+
│ │ x y │
3+
├───┼─────────────
4+
│ x │ 1 -0.00
5+
│ y │ -0.00 0.25 │
6+
└───┴─────────────

tests/matrix_2.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
<tr>
88
<th> x </th>
99
<td> 1 </td>
10-
<td style="background-color:rgb(250,144,144);color:black"> 0.707 </td>
10+
<td style="background-color:rgb(250,144,144);color:black"> 0.7 </td>
1111
</tr>
1212
<tr>
1313
<th> y </th>
14-
<td style="background-color:rgb(250,144,144);color:black"> 0.707 </td>
14+
<td style="background-color:rgb(250,144,144);color:black"> 0.7 </td>
1515
<td> 1 </td>
1616
</tr>
1717
</table>

tests/matrix_difficult_values.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
┌───┬─────────────┐
22
│ │ x y │
33
├───┼─────────────┤
4-
│ x │ -1.23 0 │
5-
│ y │ 0 0 │
4+
│ x │ -1.23 0.0 │
5+
│ y │ 0.0 0 │
66
└───┴─────────────┘

tests/test_repr.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,23 +80,23 @@ def test_pdg_format():
8080

8181
def test_matrix_format():
8282
def a(*args):
83-
return np.array(args)
83+
return np.reshape(args, (2, 2))
8484

8585
assert _repr_text.matrix_format(a(1e1, 2e2, -3e3, -4e4)) == [
8686
"10",
8787
"200",
88-
"-3e+03",
88+
"-3.00e3",
8989
"-4e+04",
9090
]
91-
assert _repr_text.matrix_format(a(nan, 2e2, -nan, -4e4)) == [
92-
"nan",
91+
assert _repr_text.matrix_format(a(2e2, nan, -nan, -4e4)) == [
9392
"200",
9493
"nan",
94+
"nan",
9595
"-4e+04",
9696
]
97-
assert _repr_text.matrix_format(a(inf, 2e2, -nan, -4e4)) == [
98-
"inf",
97+
assert _repr_text.matrix_format(a(2e2, inf, -nan, -4e4)) == [
9998
"200",
99+
"inf",
100100
"nan",
101101
"-4e+04",
102102
]

tests/test_tabulate.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ def test_matrix():
3131
assert (
3232
framed(tab.tabulate(*m.covariance.to_table()))
3333
== """
34-
x y
35-
-- ------ ------
36-
x 1 -0.643
37-
y -0.643 4
34+
x y
35+
-- --- ---
36+
x 1 -0
37+
y -0 4
3838
"""
3939
)

0 commit comments

Comments
 (0)