Skip to content

Commit 5d09232

Browse files
* Fix issue lina-usc#154. * codespell * Fix example bug.
1 parent 5ea5951 commit 5d09232

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

pylossless/config/rejection.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# License: MIT
55

66
import numpy as np
7+
from importlib.metadata import version
8+
import warnings
79

810
from .config import ConfigMixin
911

@@ -90,7 +92,7 @@ def __init__(
9092
)
9193

9294
def __repr__(self):
93-
"""Return a summary of the Calibration object."""
95+
"""Return a summary of the RejectionPolicy object."""
9496
return (
9597
f"RejectionPolicy: |\n"
9698
f" config_fname: {self['config_fname']}\n"
@@ -101,7 +103,7 @@ def __repr__(self):
101103
f" remove_flagged_ics: {self['remove_flagged_ics']}\n"
102104
)
103105

104-
def apply(self, pipeline, return_ica=False):
106+
def apply(self, pipeline, return_ica=False, version_mismatch="raise"):
105107
"""Return a cleaned new raw object based on the rejection policy.
106108
107109
Parameters
@@ -119,6 +121,21 @@ def apply(self, pipeline, return_ica=False):
119121
An :class:`~mne.io.Raw` instance with the appropriate channels and ICs
120122
added to mne bads, interpolated, or dropped.
121123
"""
124+
if pipeline.config["version"] != version("pylossless"):
125+
error_message = (
126+
"The output of the pipeline was saved with pylossless version "
127+
f"{pipeline.config['version']} and you are currently using "
128+
f"version {version('pylossless')}. The behavior is undefined."
129+
)
130+
if version_mismatch == "raise":
131+
raise RuntimeError(error_message)
132+
elif version_mismatch == "warning":
133+
warnings.warn(error_message, RuntimeWarning)
134+
elif version_mismatch != "ignore":
135+
raise ValueError("version_mismatch can take values 'raise', "
136+
"'warning', or 'ignore'. Received "
137+
f"{version_mismatch}.")
138+
122139
# Get the raw object
123140
raw = pipeline.raw.copy()
124141

pylossless/pipeline.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from copy import deepcopy
1111
from pathlib import Path
1212
from functools import partial
13+
from importlib.metadata import version
1314

1415
# Math and data structures
1516
import numpy as np
@@ -461,6 +462,8 @@ def __init__(self, config_path=None, config=None):
461462
"epoch": FlaggedEpochs(self),
462463
"ic": FlaggedICs(),
463464
}
465+
self._config = None
466+
464467
if config:
465468
self.config = config
466469
if config_path is None:
@@ -526,6 +529,15 @@ def _repr_html_(self):
526529

527530
return html
528531

532+
@property
533+
def config(self):
534+
return self._config
535+
536+
@config.setter
537+
def config(self, config):
538+
self._config = config
539+
self._config["version"] = version("pylossless")
540+
529541
@property
530542
def config_fname(self):
531543
warn('config_fname is deprecated and will be removed from future versions.',
@@ -1094,6 +1106,7 @@ def save(self, derivatives_path, overwrite=False, format="EDF", event_id=None):
10941106
config_bidspath = bpath.update(
10951107
extension=".yaml", suffix="ll_config", check=False
10961108
)
1109+
10971110
self.config.save(config_bidspath)
10981111

10991112
# Save flag["ch"]

pylossless/tests/test_rejection.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,19 @@ def test_rejection_policy(clean_ch_mode, pipeline_fixture):
1616
want_flags = ["noisy", "uncorrelated", "bridged"]
1717
assert rejection_config["ch_flags_to_reject"] == want_flags
1818

19-
raw, ica = rejection_config.apply(pipeline_fixture, return_ica=True)
19+
pipeline_fixture.config["version"] = "-1"
20+
with pytest.raises(RuntimeError, match="The output of the pipeline was"):
21+
raw, ica = rejection_config.apply(pipeline_fixture,
22+
version_mismatch="raise")
23+
with pytest.raises(RuntimeWarning, match="The output of the pipeline was"):
24+
raw, ica = rejection_config.apply(pipeline_fixture,
25+
version_mismatch="warning")
26+
with pytest.raises(ValueError, match="version_mismatch can take values"):
27+
raw, ica = rejection_config.apply(pipeline_fixture,
28+
version_mismatch="sdfdf")
29+
raw, ica = rejection_config.apply(pipeline_fixture, return_ica=True,
30+
version_mismatch="ignore")
31+
2032
flagged_chs = []
2133
for key in rejection_config["ch_flags_to_reject"]:
2234
flagged_chs.extend(pipeline_fixture.flags["ch"][key].tolist())

0 commit comments

Comments
 (0)