Skip to content
New issue

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

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

Already on GitHub? # to your account

ADD Apply ICA function #43

Merged
merged 9 commits into from
Jul 5, 2019
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 CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## [Unreleased] - 2019-04-26
### Added
- Show version number in About dialog ([#28](https://github.com/cbrnr/mnelab/pull/28) by [Clemens Brunner](https://github.com/cbrnr))
- Add "Apply ICA" to Tools menu that allows users to apply a fitted ICA solution to the current dataset ([#43](https://github.com/cbrnr/mnelab/pull/43) by [Victor Férat](https://github.com/vferat))

### Fixed
- Fix crash when no channel labels are present in XDF file by [Clemens Brunner](https://github.com/cbrnr))
Expand Down
19 changes: 18 additions & 1 deletion mnelab/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,11 @@ def __init__(self, model):
self.filter_data)
self.actions["find_events"] = tools_menu.addAction("Find &events...",
self.find_events)
tools_menu.addSeparator()
self.actions["run_ica"] = tools_menu.addAction("Run &ICA...",
self.run_ica)

self.actions["apply_ica"] = tools_menu.addAction("Apply &ICA",
self.apply_ica)
view_menu = self.menuBar().addMenu("&View")
self.actions["statusbar"] = view_menu.addAction("Statusbar",
self._toggle_statusbar)
Expand Down Expand Up @@ -276,6 +278,7 @@ def data_changed(self):
montage = bool(self.model.current["montage"])
self.actions["plot_montage"].setEnabled(enabled and montage)
ica = bool(self.model.current["ica"])
self.actions["apply_ica"].setEnabled(enabled and ica)
self.actions["export_ica"].setEnabled(enabled and ica)
self.actions["plot_ica_components"].setEnabled(enabled and ica and
montage)
Expand Down Expand Up @@ -494,12 +497,18 @@ def run_ica(self):
method = dialog.method.currentText()
exclude_bad_segments = dialog.exclude_bad_segments.isChecked()
fit_params = {}

if not dialog.extended.isHidden():
fit_params["extended"] = dialog.extended.isChecked()

if not dialog.ortho.isHidden():
fit_params["ortho"] = dialog.ortho.isChecked()

ica = mne.preprocessing.ICA(method=dialog.methods[method],
fit_params=fit_params)
self.model.history.append(f"ica = mne.preprocessing.ICA("
f"method={dialog.methods[method]}, "
f"fit_params={fit_params})")
pool = mp.Pool(1)
kwds = {"reject_by_annotation": exclude_bad_segments}
res = pool.apply_async(func=ica.fit,
Expand All @@ -509,8 +518,16 @@ def run_ica(self):
pool.terminate()
else:
self.model.current["ica"] = res.get(timeout=1)
self.model.history.append(f"ica.fit(inst=raw, "
f"reject_by_annotation="
f"{exclude_bad_segments})")
self.data_changed()

def apply_ica(self):
"""Apply current fitted ICA."""
self.auto_duplicate()
self.model.apply_ica()

def filter_data(self):
"""Filter data."""
dialog = FilterDialog(self)
Expand Down
7 changes: 7 additions & 0 deletions mnelab/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,13 @@ def filter(self, low, high):
self.current["name"] += " ({}-{} Hz)".format(low, high)
self.history.append("raw.filter({}, {})".format(low, high))

@data_changed
def apply_ica(self):
self.current["ica"].apply(self.current["raw"])
self.history.append(f"ica.apply(inst=raw, "
f"exclude={self.current['ica'].exclude})")
self.current["name"] += " (ICA)"

@data_changed
def set_reference(self, ref):
self.current["reference"] = ref
Expand Down