Skip to content

Commit

Permalink
Improve logging and level handling (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathieu Scheltienne authored Feb 27, 2024
1 parent 9515159 commit 948235d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 15 deletions.
3 changes: 2 additions & 1 deletion doc/changes/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
Version 1.3
===========

- xxx
- Improve logging to set MNE's logger level to currently set MNE-LSL level in MNE-LSL functions using MNE's function under the hood (:pr:`219` by `Mathieu Scheltienne`_)
- Use the currently set logger level as replacement for ``verbose=None`` in MNE-LSL functions (:pr:`219` by `Mathieu Scheltienne`_)
21 changes: 18 additions & 3 deletions mne_lsl/player/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from ..utils._checks import check_type, ensure_int, ensure_path
from ..utils._docs import fill_doc
from ..utils.logs import logger, verbose
from ..utils.meas_info import _set_channel_units

if TYPE_CHECKING:
Expand Down Expand Up @@ -71,6 +72,7 @@ def __init__(self, fname: Union[str, Path, BaseRaw], chunk_size: int = 64) -> No
self._raw = read_raw(self._fname, preload=True)
# This method should end on a self._reset_variables()

@verbose
@fill_doc
def anonymize(
self,
Expand Down Expand Up @@ -103,7 +105,11 @@ def anonymize(
RuntimeWarning,
stacklevel=2,
)
super().anonymize(daysback=daysback, keep_his=keep_his, verbose=verbose)
super().anonymize(
daysback=daysback,
keep_his=keep_his,
verbose=logger.level if verbose is None else verbose,
)
return self

@fill_doc
Expand Down Expand Up @@ -137,6 +143,7 @@ def get_channel_units(
return channel_units

@abstractmethod
@verbose
@fill_doc
def rename_channels(
self,
Expand Down Expand Up @@ -164,14 +171,20 @@ def rename_channels(
The player instance modified in-place.
"""
self._check_not_started("rename_channels()")
rename_channels(self.info, mapping, allow_duplicates)
rename_channels(
self.info,
mapping,
allow_duplicates,
verbose=logger.level if verbose is None else verbose,
)

@abstractmethod
def start(self) -> BasePlayer: # pragma: no cover
"""Start streaming data."""
pass

@abstractmethod
@verbose
@fill_doc
def set_channel_types(
self,
Expand Down Expand Up @@ -206,7 +219,9 @@ def set_channel_types(
"""
self._check_not_started("set_channel_types()")
super().set_channel_types(
mapping=mapping, on_unit_change=on_unit_change, verbose=verbose
mapping=mapping,
on_unit_change=on_unit_change,
verbose=logger.level if verbose is None else verbose,
)
self._sinfo.set_channel_types(self.get_channel_types(unique=False))
return self
Expand Down
25 changes: 18 additions & 7 deletions mne_lsl/stream/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import numpy as np
from mne import pick_info, pick_types
from mne.channels import rename_channels
from mne.utils import check_version
from mne.utils import check_version, use_log_level

if check_version("mne", "1.6"):
from mne._fiff.constants import FIFF, _ch_unit_mul_named
Expand All @@ -27,7 +27,7 @@

from ..utils._checks import check_type, check_value
from ..utils._docs import copy_doc, fill_doc
from ..utils.logs import logger
from ..utils.logs import logger, verbose
from ..utils.meas_info import _HUMAN_UNITS, _set_channel_units

if TYPE_CHECKING:
Expand Down Expand Up @@ -214,6 +214,7 @@ def add_reference_channels(

return self

@verbose
@fill_doc
def anonymize(
self,
Expand All @@ -240,7 +241,11 @@ def anonymize(
%(anonymize_info_notes)s
"""
self._check_connected(name="anonymize()")
super().anonymize(daysback=daysback, keep_his=keep_his, verbose=verbose)
super().anonymize(
daysback=daysback,
keep_his=keep_his,
verbose=logger.level if verbose is None else verbose,
)
return self

@abstractmethod
Expand Down Expand Up @@ -501,6 +506,7 @@ def record(self):
self._check_connected(name="record()")
raise NotImplementedError

@verbose
@fill_doc
def rename_channels(
self,
Expand Down Expand Up @@ -532,7 +538,7 @@ def rename_channels(
self._info,
mapping=mapping,
allow_duplicates=allow_duplicates,
verbose=verbose,
verbose=logger.level if verbose is None else verbose,
)
return self

Expand All @@ -547,6 +553,7 @@ def set_bipolar_reference(self) -> BaseStream:
self._check_connected_and_regular_sampling("set_bipolar_reference()")
raise NotImplementedError

@verbose
@fill_doc
def set_channel_types(
self,
Expand Down Expand Up @@ -581,7 +588,9 @@ def set_channel_types(
"""
self._check_connected(name="set_channel_types()")
super().set_channel_types(
mapping=mapping, on_unit_change=on_unit_change, verbose=verbose
mapping=mapping,
on_unit_change=on_unit_change,
verbose=logger.level if verbose is None else verbose,
)
return self

Expand Down Expand Up @@ -714,6 +723,7 @@ def set_meas_date(
super().set_meas_date(meas_date)
return self

@verbose
@fill_doc
def set_montage(
self,
Expand Down Expand Up @@ -759,7 +769,7 @@ def set_montage(
match_case=match_case,
match_alias=match_alias,
on_missing=on_missing,
verbose=verbose,
verbose=logger.level if verbose is None else verbose,
)
return self

Expand Down Expand Up @@ -836,7 +846,8 @@ def _pick(self, picks: NDArray[+ScalarIntType]) -> None:
)

with self._interrupt_acquisition():
self._info = pick_info(self._info, picks)
with use_log_level(logger.level):
self._info = pick_info(self._info, picks)
self._picks_inlet = self._picks_inlet[picks_inlet]
self._buffer = self._buffer[:, picks]

Expand Down
2 changes: 1 addition & 1 deletion mne_lsl/utils/_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
verbose : int | str | bool | None
Sets the verbosity level. The verbosity increases gradually between
``"CRITICAL"``, ``"ERROR"``, ``"WARNING"``, ``"INFO"`` and ``"DEBUG"``.
If None is provided, the verbosity is set to ``"WARNING"``.
If None is provided, the verbosity is set to the currently set logger's level.
If a bool is provided, the verbosity is set to ``"WARNING"`` for False and
to ``"INFO"`` for True."""

Expand Down
8 changes: 5 additions & 3 deletions mne_lsl/utils/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,16 @@ def __init__(
):
self._logger: Logger = logger_obj if logger_obj is not None else logger
self._old_level: int = self._logger.level
self._level: int = check_verbose(verbose)
self._level: Optional[int] = None if verbose is None else check_verbose(verbose)

def __enter__(self):
self._logger.setLevel(self._level)
if self._level is not None:
self._logger.setLevel(self._level)
return self

def __exit__(self, *args):
self._logger.setLevel(self._old_level)
if self._level is not None:
self._logger.setLevel(self._old_level)


logger = _init_logger(verbose="WARNING") # equivalent to verbose=None

0 comments on commit 948235d

Please sign in to comment.