saving the CTF MEG data after tSSS

Hello,
While I am going through the tutorials (still very new to MNE-python), I needed to go ahead to try out tSSS on one of my CTF MEG data. I ‘think’ I was able to apply tSSS although I am not super confident that my codes are collect. After looking the output of tSSS, I wanted to save the data but I am getting the error message. I think I have a wrong options there. I would be appreciate if you could take look my codes and see if any error exist as well as if you could provide the command line for saving the data as .fiff format.
Thank you for your help in advnace!
import os
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
import mne
from mne.preprocessing import find_bad_channels_maxwell
file_path = r’<F:\SSS<CRF-MEG-data>.ds’
raw = mne.io.read_raw_ctf(file_path)

Let’s see if we can automatically detect it

raw.info[‘bads’] =
raw_check = raw.copy()
auto_noisy_chs, auto_flat_chs, auto_scores = find_bad_channels_maxwell(raw_check, return_scores=True, verbose=True)

print(auto_noisy_chs) # we should find them!
print(auto_flat_chs) # none for this dataset

#if here were bad channls, update the list of bad channels in the dataset with:
bads = raw.info[‘bads’] + auto_noisy_chs + auto_flat_chs
raw.info[‘bads’] = bads

In the following, we will generate such visualizations for the automated detection of noisy gradiometer channels.

ch_type = ‘grad’
ch_subset = auto_scores[‘ch_types’] == ch_type
ch_names = auto_scores[‘ch_names’][ch_subset]
scores = auto_scores[‘scores_noisy’][ch_subset]
limits = auto_scores[‘limits_noisy’][ch_subset]
bins = auto_scores[‘bins’] # The the windows that were evaluated.

We will label each segment by its start and stop time, with up to 3

digits before and 3 digits after the decimal place (1 ms precision).

bin_labels = [f’{start:3.3f} – {stop:3.3f}’
for start, stop in bins]

We store the data in a Pandas DataFrame. The seaborn heatmap function

we will call below will then be able to automatically assign the correct

labels to all axes.

data_to_plot = pd.DataFrame(data=scores,
columns=pd.Index(bin_labels, name=‘Time (s)’),
index=pd.Index(ch_names, name=‘Channel’))

First, plot the “raw” scores.

fig, ax = plt.subplots(1, 2, figsize=(12, 8))
fig.suptitle(f’Automated noisy channel detection: {ch_type}',
fontsize=16, fontweight=‘bold’)
sns.heatmap(data=data_to_plot, cmap=‘Reds’, cbar_kws=dict(label=‘Score’),
ax=ax[0])
[ax[0].axvline(x, ls=‘dashed’, lw=0.25, dashes=(25, 15), color=‘gray’)
for x in range(1, len(bins))]
ax[0].set_title(‘All Scores’, fontweight=‘bold’)

Now, adjust the color range to highlight segments that exceeded the limit.

sns.heatmap(data=data_to_plot,
vmin=np.nanmin(limits), # bads in input data have NaN limits
cmap=‘Reds’, cbar_kws=dict(label=‘Score’), ax=ax[1])
[ax[1].axvline(x, ls=‘dashed’, lw=0.25, dashes=(25, 15), color=‘gray’)
for x in range(1, len(bins))]
ax[1].set_title(‘Scores > Limit’, fontweight=‘bold’)

The figure title should not overlap with the subplots.

fig.tight_layout(rect=[0, 0.03, 1, 0.95])

#performing SSS and Maxwell filtering is done with a single call to maxwell_filter(), with the crosstalk and fine calibration filenames provided (if available):
raw_sss = mne.preprocessing.maxwell_filter(raw, verbose=True)

#To see the effect, we can plot the data before and after SSS / Maxwell filtering
raw.pick([‘meg’]).plot(duration=2, butterfly=True)
raw_sss.pick([‘meg’]).plot(duration=2, butterfly=True)

#tSSS:To use tSSS in MNE-Python, pass a time (in seconds) to the parameter st_duration of maxwell_filter()
raw_sss = mne.preprocessing.maxwell_filter( raw, verbose=True, st_duration=10)
raw_sss.pick([‘meg’]).plot(duration=2, butterfly=True)

raw_sss.pick([‘meg’]).plot(duration=2, butterfly=True)
<mne_qt_browser._pg_figure.MNEQtBrowser object at 0x000002B3DDB177F0>
save(raw_tsss.ds , drop_small_buffer=True, proj=True, fmt=single, overwrite=False)
Traceback (most recent call last):
File “”, line 1, in
NameError: name ‘save’ is not defined
new_data = save(raw_tsss.ds , drop_small_buffer=True, proj=True, fmt=single, overwrite=False)
Traceback (most recent call last):
File “”, line 1, in
NameError: name ‘save’ is not defined
def save(raw_tsss.ds , drop_small_buffer=True, proj=True, fmt=single, overwrite=False)
File “”, line 1
def save(raw_tsss.ds , drop_small_buffer=True, proj=True, fmt=single, overwrite=False)
^
SyntaxError: invalid syntax
def save(raw_tsss.fif , drop_small_buffer=True, proj=True, fmt=single, overwrite=False)
File “”, line 1
def save(raw_tsss.fif , drop_small_buffer=True, proj=True, fmt=single, overwrite=False)
^
SyntaxError: invalid syntax
save(‘raw_tsss.fif’ , drop_small_buffer=True, proj=True, fmt=single, overwrite=False)
File “”, line 1
save(‘raw_tsss.fif’ , drop_small_buffer=True, proj=True, fmt=single, overwrite=False)

I think I was able to save the data with
raw.save(‘.fif’ , drop_small_buffer=True, proj=True, overwrite=False)

And now I want to convert back to CTF format. I am trying with using fiff2ctf but now successful. I am not sure this code is available:
mne_fiff2ctf --fif '.fif --ds data.ds
I appreciate any advices. Thank you.

Hello! I’m curious about how to apply MaxFilter to CTF data. In my practice, MaxFilter requires crosstalk files and calibration files, but it seems that the MEG data collected by CTF doesn’t include them.