Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Improve Covariance.__repr__ #12181

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changes/devel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Enhancements
- Add support for writing forward solutions to HDF5 and convenience function :meth:`mne.Forward.save` (:gh:`12036` by `Eric Larson`_)
- Refactored internals of :func:`mne.read_annotations` (:gh:`11964` by `Paul Roujansky`_)
- Add support for drawing MEG sensors in :ref:`mne coreg` (:gh:`12098` by `Eric Larson`_)
- Improve string representation of :class:`mne.Covariance` (:gh:`12181` by `Eric Larson`_)
- Add ``check_version=True`` to :ref:`mne sys_info` to check for a new release on GitHub (:gh:`12146` by `Eric Larson`_)
- Bad channels are now colored gray in addition to being dashed when spatial colors are used in :func:`mne.viz.plot_evoked` and related functions (:gh:`12142` by `Eric Larson`_)
- By default MNE-Python creates matplotlib figures with ``layout='constrained'`` rather than the default ``layout='tight'`` (:gh:`12050`, :gh:`12103` by `Mathieu Scheltienne`_ and `Eric Larson`_)
Expand Down
12 changes: 5 additions & 7 deletions mne/cov.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
)
from .rank import compute_rank
from .utils import (
_array_repr,
_check_fname,
_check_on_missing,
_check_option,
Expand Down Expand Up @@ -273,13 +274,10 @@ def _get_square(self):
return np.diag(self.data) if self["diag"] else self.data.copy()

def __repr__(self): # noqa: D105
if self.data.ndim == 2:
s = "size : %s x %s" % self.data.shape
else: # ndim == 1
s = "diagonal : %s" % self.data.size
s += ", n_samples : %s" % self.nfree
s += ", data : %s" % self.data
return "<Covariance | %s>" % s
s = "<Covariance | kind : "
s += "full" if self.data.ndim == 2 else "diagonal"
s += f", {_array_repr(self.data)}, n_samples : {self.nfree}>"
return s

def __add__(self, cov):
"""Add Covariance taking into account number of degrees of freedom."""
Expand Down
2 changes: 2 additions & 0 deletions mne/tests/test_cov.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ def test_io_cov(tmp_path):
assert_equal(cov["method"], cov2["method"])
assert_equal(cov["loglik"], cov2["loglik"])
assert "Covariance" in repr(cov)
assert "range :" in repr(cov)
assert "\n" not in repr(cov)

cov2 = read_cov(cov_gz_fname)
assert_array_almost_equal(cov.data, cov2.data)
Expand Down
2 changes: 2 additions & 0 deletions mne/utils/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ __all__ = [
"_apply_scaling_cov",
"_arange_div",
"_array_equal_nan",
"_array_repr",
"_assert_no_instances",
"_auto_weakref",
"_build_data_frame",
Expand Down Expand Up @@ -347,6 +348,7 @@ from .numerics import (
_apply_scaling_cov,
_arange_div,
_array_equal_nan,
_array_repr,
_cal_to_julian,
_check_dt,
_compute_row_norms,
Expand Down
6 changes: 6 additions & 0 deletions mne/utils/numerics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1152,3 +1152,9 @@ def cache_fun(*args):
return cache_fun

return dec


def _array_repr(x):
"""Produce compact info about float ndarray x."""
assert isinstance(x, np.ndarray), type(x)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think bandit will complain about assert appearing outside a test file (but maybe not?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

never mind, bandit does complain but it's considered "low" severity so the job still passes. And I don't mind having this kind of assert usage in our code anyway.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we ignore that rule because we do this so often

return f"shape : {x.shape}, range : [{np.nanmin(x):+0.2g}, {np.nanmax(x):+0.2g}]"
Loading