<jupyter_start><jupyter_text>Demonstrate MNE-Python visualization @ OpenMR From raw data to source reconstruction. For a more thorough and verbose introduction to (beamformer) source reconstruction see [the MNE-Python tutorial on beamforming](https://mne.tools/stable/auto_tutorials/source-modeling/plot_beamformer_lcmv.html).<jupyter_code># Author: <NAME> <<EMAIL>>
# License: BSD (3-clause)

import matplotlib

import mne
from mne.beamformer import make_lcmv, apply_lcmv
from mne.datasets import sample
%matplotlib qt  
mne.set_log_level('WARNING')  # do not show verbose output for live demonstration<jupyter_output><empty_output><jupyter_text>Read data and plot.raw()We will use the `sample` public data set for this tutorial (MEG, EEG, and EOG data).<jupyter_code>data_path = sample.data_path()  # downloads data automatically if not on disk yet
raw_fname = data_path + '/MEG/sample/sample_audvis_raw.fif'

# Read the raw data
raw = mne.io.read_raw_fif(raw_fname)

# We make a copy for plotting (only to not mess up live demonstration!)
raw_plot = raw.copy()
raw_plot.plot()
# look at the bad channels we added:
raw_plot.info
# see the annotations we added:
raw_plot.annotations<jupyter_output><empty_output><jupyter_text>Create evoked<jupyter_code>raw.info['bads'] = ['MEG 2443']  # bad MEG channel

# Set up the epoching
event_id = 1  # those are the trials with left-ear auditory stimuli
tmin, tmax = -0.2, 0.5
events = mne.find_events(raw)

# pick relevant channels
raw.pick(['meg', 'eog'])  # pick channels of interest

# Create epochs
proj = False  # already applied
epochs = mne.Epochs(raw, events, event_id, tmin, tmax,
                    baseline=(None, 0), preload=True, proj=proj,
                    reject=dict(grad=4000e-13, mag=4e-12, eog=150e-6))

# for speed purposes, we cut to a window of interest
evoked = epochs.average().crop(-0.1, 0.2)<jupyter_output><empty_output><jupyter_text>Plot evoked data<jupyter_code>%matplotlib inline
# Visualize averaged sensor space data for magnetometers
evoked.plot_joint(picks=['mag']);
%matplotlib inline
from ipywidgets import interact

# create an interactive topoplot with interactive widgets:
@interact(time=(evoked.times[0], evoked.times[-1], 0.01), continuous_update=False)
def plot_topo(time):
    evoked.plot_topomap(ch_type='mag', times=[time], size=3.)<jupyter_output><empty_output><jupyter_text>Prepare beamformer comptuation<jupyter_code># compute the datat covariance matrices
data_cov = mne.compute_covariance(epochs, tmin=0.01, tmax=tmax,
                                  method='empirical')
noise_cov = mne.compute_covariance(epochs, tmin=tmin, tmax=0,
                                   method='empirical')
# Read forward model
fwd_fname = data_path + '/MEG/sample/sample_audvis-meg-oct-6-fwd.fif'
forward = mne.read_forward_solution(fwd_fname)<jupyter_output><empty_output><jupyter_text>Compute the beamformer spatial filter and apply itBeamforming is one method to estimate the source activity that gave rise to the sensor data we recorded.<jupyter_code># compute the spatial filter:
filters = make_lcmv(evoked.info, forward, data_cov, reg=0.05,
                    noise_cov=noise_cov, pick_ori='max-power',
                    weight_norm='unit-noise-gain', rank=None)

# apply the spatial filter (= get source estimate):
stc = apply_lcmv(evoked, filters, max_ori_out='signed')<jupyter_output><empty_output><jupyter_text>Visualize the reconstructed source activity<jupyter_code>%matplotlib qt

# find the maximum activity to initialize plot
vertno_max, time_max = stc.get_peak(hemi='rh')

# set up the plotting specifics and plot:
subjects_dir = data_path + '/subjects'
plot_kwargs = dict(
    hemi='rh', subjects_dir=subjects_dir,
    views='lateral', clim=dict(kind='value', pos_lims=[0., 1., 1.85]),
    initial_time=time_max, time_unit='s', size=(600, 600), verbose=False)
brain = stc.plot(**plot_kwargs)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10836308006119327, 'fraction_numerical': 0.013513513513513514, 'mean_word_length': 5.072755417956657, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '108976', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1378, 'n_tokens_neox': 1300, 'n_words': 337, 'path': 'demonstration_openMR.ipynb', 'repo_name': 'britta-wstnr/demo_openMR', 'size': '3945'}