本教程介绍如何配置MNE-Python以适应本地系统和分析首选项。
老样子:
1 | import os |
获取和设置配置变量
使用mne.get_config()
和mne.set_config()
函数读取和写入配置变量。要读取一个特定的配置变量,将其名称作为关键参数传递给get_config()
(key
是第一个参数,所以如果你愿意,你可以不命名地传递它):
1 | print(mne.get_config("MNE_USE_CUDA")) |
false
<class ‘str’>
请注意,从JSON文件读取的字符串值不会以任何方式进行解析,因此get_config()
即使对于true/false配置值也会返回字符串,而不是Python布尔值。类似地,set_config()将只设置字符串值:
1 | try: |
value must be an instance of str, path-like, or NoneType, got <class ‘bool’> instead.
如果我们不确定是否设置了配置变量,有一种方便的方法可以检查它,并在它不存在的情况下fallback:get_config()
的一个default
参数。
1 | print(mne.get_config("missing_config_key", default="fallback value")) |
fallback value
get_config()
还有两种方便的使用模式。第一个将返回一个字典,其中包含在系统上设置的所有配置变量及其值,这是通过传递key=None(默认值,所以以下代码省略了)来完成的:
1 | print(mne.get_config()) # same as mne.get_config(key=None) |
{‘MNE_BROWSE_RAW_SIZE’: ‘8.0,8.0’, ‘MNE_DATASETS_BRAINSTORM_PATH’: ‘/home/circleci/mne_data’, ‘MNE_DATASETS_EEGBCI_PATH’: ‘/home/circleci/mne_data’, ‘MNE_DATASETS_EPILEPSY_ECOG_PATH’: ‘/home/circleci/mne_data’, ‘MNE_DATASETS_ERP_CORE_PATH’: ‘/home/circleci/mne_data’, ‘MNE_DATASETS_EYELINK_PATH’: ‘/home/circleci/mne_data’, ‘MNE_DATASETS_FIELDTRIP_CMC_PATH’: ‘/home/circleci/mne_data’, ‘MNE_DATASETS_FNIRS_MOTOR_PATH’: ‘/home/circleci/mne_data’, ‘MNE_DATASETS_HF_SEF_PATH’: ‘/home/circleci/mne_data’, ‘MNE_DATASETS_KILOWORD_PATH’: ‘/home/circleci/mne_data’, ‘MNE_DATASETS_LIMO_PATH’: ‘/home/circleci/mne_data’, ‘MNE_DATASETS_MISC_PATH’: ‘/home/circleci/mne_data’, ‘MNE_DATASETS_MTRF_PATH’: ‘/home/circleci/mne_data’, ‘MNE_DATASETS_MULTIMODAL_PATH’: ‘/home/circleci/mne_data’, ‘MNE_DATASETS_OPM_PATH’: ‘/home/circleci/mne_data’, ‘MNE_DATASETS_PHANTOM_4DBTI_PATH’: ‘/home/circleci/mne_data’, ‘MNE_DATASETS_PHANTOM_KERNEL_PATH’: ‘/home/circleci/mne_data’, ‘MNE_DATASETS_PHANTOM_KIT_PATH’: ‘/home/circleci/mne_data’, ‘MNE_DATASETS_REFMEG_NOISE_PATH’: ‘/home/circleci/mne_data’, ‘MNE_DATASETS_SAMPLE_PATH’: ‘/home/circleci/mne_data’, ‘MNE_DATASETS_SOMATO_PATH’: ‘/home/circleci/mne_data’, ‘MNE_DATASETS_SPM_FACE_PATH’: ‘/home/circleci/mne_data’, ‘MNE_DATASETS_SSVEP_PATH’: ‘/home/circleci/mne_data’, ‘MNE_DATASETS_TESTING_PATH’: ‘/home/circleci/mne_data’, ‘MNE_DATASETS_UCL_OPM_AUDITORY_PATH’: ‘/home/circleci/mne_data’, ‘MNE_LOGGING_LEVEL’: ‘info’, ‘MNE_USE_CUDA’: ‘false’, ‘SUBJECTS_DIR’: ‘/home/circleci/mne_data/MNE-fsaverage-data’, ‘MNE_3D_OPTION_THEME’: ‘light’, ‘MNE_BROWSER_BACKEND’: ‘qt’, ‘MNE_BROWSER_THEME’: ‘light’, ‘MNE_BROWSER_PRECOMPUTE’: ‘false’, ‘MNE_BROWSER_OVERVIEW_MODE’: ‘hidden’}
第二种将返回一个的元组,可以看配置量的描述,不管它们是否已被设置。这是通过传递一个空字符串’ ‘作为key来实现的:
1 | print(mne.get_config(key="")) |
{‘MNE_3D_OPTION_ANTIALIAS’: ‘bool, whether to use full-screen antialiasing in 3D plots’, ‘MNE_3D_OPTION_DEPTH_PEELING’: ‘bool, whether to use depth peeling in 3D plots’, ‘MNE_3D_OPTION_MULTI_SAMPLES’: ‘int, number of samples to use for full-screen antialiasing’, ‘MNE_3D_OPTION_SMOOTH_SHADING’: ‘bool, whether to use smooth shading in 3D plots’, ‘MNE_3D_OPTION_THEME’: ‘str, the color theme (light or dark) to use for 3D plots’, ‘MNE_BROWSE_RAW_SIZE’: ‘tuple, width and height of the raw browser window (in inches)’, ‘MNE_BROWSER_BACKEND’: ‘str, the backend to use for the MNE Browse Raw window (qt or matplotlib)’, ‘MNE_BROWSER_OVERVIEW_MODE’: ‘str, the overview mode to use in the MNE Browse Raw window )(see mne.viz.plot_raw for valid options)’, ‘MNE_BROWSER_PRECOMPUTE’: ‘bool, whether to precompute raw data in the MNE Browse Raw window’, ‘MNE_BROWSER_THEME’: ‘str, the color theme (light or dark) to use for the browser’, ‘MNE_BROWSER_USE_OPENGL’: ‘bool, whether to use OpenGL for rendering in the MNE Browse Raw window’, ‘MNE_CACHE_DIR’: ‘str, path to the cache directory for parallel execution’, ‘MNE_COREG_ADVANCED_RENDERING’: ‘bool, whether to use advanced OpenGL rendering in mne coreg’, ‘MNE_COREG_COPY_ANNOT’: ‘bool, whether to copy the annotation files during warping’, ‘MNE_COREG_FULLSCREEN’: ‘bool, whether to use full-screen mode in mne coreg’, ‘MNE_COREG_GUESS_MRI_SUBJECT’: ‘bool, whether to guess the MRI subject in mne coreg’, ‘MNE_COREG_HEAD_HIGH_RES’: ‘bool, whether to use high-res head surface in mne coreg’, ‘MNE_COREG_HEAD_OPACITY’: ‘bool, the head surface opacity to use in mne coreg’, ‘MNE_COREG_HEAD_INSIDE’: ‘bool, whether to add an opaque inner scalp head surface to help occlude points behind the head in mne coreg’, ‘MNE_COREG_INTERACTION’: ‘str, interaction style in mne coreg (trackball or terrain)’, ‘MNE_COREG_MARK_INSIDE’: ‘bool, whether to mark points inside the head surface in mne coreg’, ‘MNE_COREG_PREPARE_BEM’: ‘bool, whether to prepare the BEM solution after warping in mne coreg’, ‘MNE_COREG_ORIENT_TO_SURFACE’: ‘bool, whether to orient the digitization markers to the head surface in mne coreg’, ‘MNE_COREG_SCALE_LABELS’: ‘bool, whether to scale the MRI labels during warping in mne coreg’, ‘MNE_COREG_SCALE_BY_DISTANCE’: ‘bool, whether to scale the digitization markers by their distance from the scalp in mne coreg’, ‘MNE_COREG_SCENE_SCALE’: ‘float, the scale factor of the 3D scene in mne coreg (default 0.16)’, ‘MNE_COREG_WINDOW_HEIGHT’: ‘int, window height for mne coreg’, ‘MNE_COREG_WINDOW_WIDTH’: ‘int, window width for mne coreg’, ‘MNE_COREG_SUBJECTS_DIR’: ‘str, path to the subjects directory for mne coreg’, ‘MNE_CUDA_DEVICE’: ‘int, CUDA device to use for GPU processing’, ‘MNE_DATA’: ‘str, default data directory’, ‘MNE_DATASETS_BRAINSTORM_PATH’: ‘str, path for brainstorm data’, ‘MNE_DATASETS_EEGBCI_PATH’: ‘str, path for EEGBCI data’, ‘MNE_DATASETS_EPILEPSY_ECOG_PATH’: ‘str, path for epilepsy_ecog data’, ‘MNE_DATASETS_HF_SEF_PATH’: ‘str, path for HF_SEF data’, ‘MNE_DATASETS_MEGSIM_PATH’: ‘str, path for MEGSIM data’, ‘MNE_DATASETS_MISC_PATH’: ‘str, path for misc data’, ‘MNE_DATASETS_MTRF_PATH’: ‘str, path for MTRF data’, ‘MNE_DATASETS_SAMPLE_PATH’: ‘str, path for sample data’, ‘MNE_DATASETS_SOMATO_PATH’: ‘str, path for somato data’, ‘MNE_DATASETS_MULTIMODAL_PATH’: ‘str, path for multimodal data’, ‘MNE_DATASETS_FNIRS_MOTOR_PATH’: ‘str, path for fnirs_motor data’, ‘MNE_DATASETS_OPM_PATH’: ‘str, path for OPM data’, ‘MNE_DATASETS_SPM_FACE_DATASETS_TESTS’: ‘str, path for spm_face data’, ‘MNE_DATASETS_SPM_FACE_PATH’: ‘str, path for spm_face data’, ‘MNE_DATASETS_TESTING_PATH’: ‘str, path for testing data’, ‘MNE_DATASETS_VISUAL_92_CATEGORIES_PATH’: ‘str, path for visual_92_categories data’, ‘MNE_DATASETS_KILOWORD_PATH’: ‘str, path for kiloword data’, ‘MNE_DATASETS_FIELDTRIP_CMC_PATH’: ‘str, path for fieldtrip_cmc data’, ‘MNE_DATASETS_PHANTOM_KIT_PATH’: ‘str, path for phantom_kit data’, ‘MNE_DATASETS_PHANTOM_4DBTI_PATH’: ‘str, path for phantom_4dbti data’, ‘MNE_DATASETS_PHANTOM_KERNEL_PATH’: ‘str, path for phantom_kernel data’, ‘MNE_DATASETS_LIMO_PATH’: ‘str, path for limo data’, ‘MNE_DATASETS_REFMEG_NOISE_PATH’: ‘str, path for refmeg_noise data’, ‘MNE_DATASETS_SSVEP_PATH’: ‘str, path for ssvep data’, ‘MNE_DATASETS_ERP_CORE_PATH’: ‘str, path for erp_core data’, ‘MNE_FORCE_SERIAL’: ‘bool, force serial rather than parallel execution’, ‘MNE_LOGGING_LEVEL’: ‘str or int, controls the level of verbosity of any function decorated with @verbose. See https://mne.tools/stable/auto_tutorials/intro/50_configure_mne.html#logging‘, ‘MNE_MEMMAP_MIN_SIZE’: ‘str, threshold on the minimum size of arrays passed to the workers that triggers automated memory mapping, e.g., 1M or 0.5G’, ‘MNE_REPR_HTML’: ‘bool, represent some of our objects with rich HTML in a notebook environment’, ‘MNE_SKIP_NETWORK_TESTS’: ‘bool, used in a test decorator (@requires_good_network) to skip tests that include large downloads’, ‘MNE_SKIP_TESTING_DATASET_TESTS’: ‘bool, used in test decorators (@requires_spm_data, @requires_bstraw_data) to skip tests that require specific datasets’, ‘MNE_STIM_CHANNEL’: ‘string, the default channel name for mne.find_events’, ‘MNE_TQDM’: ‘str, either “tqdm”, “tqdm.auto”, or “off”. Controls presence/absence of progress bars’, ‘MNE_USE_CUDA’: ‘bool, use GPU for filtering/resampling’, ‘MNE_USE_NUMBA’: ‘bool, use Numba just-in-time compiler for some of our intensive computations’, ‘SUBJECTS_DIR’: ‘path-like, directory of freesurfer MRI files for each subject’}
通过向set_config()
传递任意键,可以添加不属于可识别列表的配置变量。这将产生一个警告,这是一个很好的检查(如果你想设置一个有效的密钥):
1 | mne.set_config("MNEE_USE_CUUDAA", "false") |
/home/circleci/project/tutorials/intro/50_configure_mne.py:77: RuntimeWarning: Setting non-standard config type: “MNEE_USE_CUUDAA”
mne.set_config(“MNEE_USE_CUUDAA”, “false”)
让我们删除刚才创建的配置变量。要取消设置一个配置变量,使用set_config()
, value=None
。由于我们仍然在处理一个无法识别的键,我们仍然会得到一个警告,但键将被取消设置:
1 | mne.set_config("MNEE_USE_CUUDAA", None) |
/home/circleci/project/tutorials/intro/50_configure_mne.py:85: RuntimeWarning: Setting non-standard config type: “MNEE_USE_CUUDAA”
mne.set_config(“MNEE_USE_CUUDAA”, None)
配置储存的位置
MNE-Python将配置变量存储在JSON文件中。默认情况下,该文件位于window是的%USERPROFILE%\.mne\mne-python
中,或linux和macOS的$HOME/.mne/mne-python
。可以使用mne.get_config_path()
获取配置文件的完整路径。
1 | print(mne.get_config_path()) |
/home/circleci/.mne/mne-python.json
然而,直接编辑.mne目录中的文件并不是一个好主意。如果出于某种原因希望从其他位置加载配置,可以将home_dir参数传递给get_config_path()
,指定要加载的配置文件所在的.mne目录的父目录。
使用环境变量
为了与MNE-C兼容,MNE-Python还读取和写入环境变量来指定配置。这是通过读取和写入JSON配置的相同函数完成的,并由参数use_env和set_env控制。
默认情况下,在检查MNE-Python JSON文件之前,get_config()
将检查操作系统。如果要只检查JSON文件,使用use_env=False
。为了演示,这里有一个环境变量,它不是特定于MNE-Python的,因此不在JSON配置文件中:
1 | # make sure it's not in the JSON file (no error means our assertion held): |
/home/circleci/python_env/bin:/home/circleci/.local/bin/:/home/circleci/minimal_cmds/bin:/home/circleci/bin:/home/circleci/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
在这里,我们将使用print()
来确认环境变量正在被创建和删除:
1 | mne.set_config("foo", "bar", set_env=False) |
/home/circleci/project/tutorials/intro/50_configure_mne.py:136: RuntimeWarning: Setting non-standard config type: “foo”
mne.set_config(“foo”, “bar”, set_env=False)
False
/home/circleci/project/tutorials/intro/50_configure_mne.py:138: RuntimeWarning: Setting non-standard config type: “foo”
mne.set_config(“foo”, “bar”)
True
/home/circleci/project/tutorials/intro/50_configure_mne.py:140: RuntimeWarning: Setting non-standard config type: “foo”
mne.set_config(“foo”, None) # unsetting a key deletes var from environment
False
日志
一个重要的配置变量是MNE_LOGGING_LEVEL
。在这个模块中,会生成消息描述MNE-Python执行的操作。设置MNE_LOGGING_LEVEL
的方式决定了我们可以看到这些消息的数量。新安装的MNE-Python的默认日志级别为info:
1 | print(mne.get_config("MNE_LOGGING_LEVEL")) |
info
可以设置为配置变量的日志级别有debug、info、warning、error和critical。在MNE-Python中,大约90%的日志消息是信息消息,所以对于大多数用户来说,选择是在info和warning之间。
对于许多MNE-Python函数,可以通过使用verbose参数临时更改该函数调用的日志记录级别。为了说明这一点,我们将加载一些设置了不同日志级别的样例数据。首先,使用日志级别警告:
1 | kit_data_path = os.path.join( |
没有生成任何消息,因为没有消息的严重程度为“warning”或更糟。接下来,我们将加载带有默认级别的相同文件:
1 | raw = mne.io.read_raw_kit(kit_data_path, verbose="info") |
Extracting SQD Parameters from /home/circleci/project/mne/io/kit/tests/data/test.sqd…
Creating Raw.info structure…
Setting channel info structure…
Creating Info structure…
Ready.
最后,如果我们请求debug级别的信息,我们会得到更多的细节——这次我们使用mne.use_log_level()
上下文管理器,这是与传递verbose='debug'
完成相同事情的另一种方式:
1 | with mne.use_log_level("debug"): |
Extracting SQD Parameters from /home/circleci/project/mne/io/kit/tests/data/test.sqd…
Creating Raw.info structure…
KIT dir entry 0 @ 16
KIT dir entry 1 @ 32
KIT dir entry 2 @ 48
KIT dir entry 3 @ 64
KIT dir entry 4 @ 80
KIT dir entry 5 @ 96
KIT dir entry 6 @ 112
KIT dir entry 7 @ 128
KIT dir entry 8 @ 144
KIT dir entry 9 @ 160
KIT dir entry 10 @ 176
KIT dir entry 11 @ 192
KIT dir entry 12 @ 208
KIT dir entry 13 @ 224
KIT dir entry 14 @ 240
KIT dir entry 15 @ 256
KIT dir entry 16 @ 272
KIT dir entry 17 @ 288
KIT dir entry 18 @ 304
KIT dir entry 19 @ 320
KIT dir entry 20 @ 336
KIT dir entry 21 @ 352
KIT dir entry 22 @ 368
KIT dir entry 23 @ 384
KIT dir entry 24 @ 400
KIT dir entry 25 @ 416
KIT dir entry 26 @ 432
KIT dir entry 27 @ 448
KIT dir entry 28 @ 464
KIT dir entry 29 @ 480
KIT dir entry 30 @ 496
SQD file basic information:
Meg160 version = V2R004
System ID = 34
System name = NYU 160ch System since Jan24 2009
Model name = EQ1160C
Channel count = 192
Comment =
Dewar style = 2
FLL type = 10
Trigger type = 21
A/D board type = 12
ADC range = +/-5.0[V]
ADC allocate = 16[bit]
ADC bit = 12[bit]
Setting channel info structure…
Creating Info structure…
Ready.
最后注意:verbose=None,这是具有verbose参数的函数的默认值,将返回到mne.set_log_level()
最近设置的日志级别。
获取系统的信息
1 | $ mne sys_info |
或
1 | mne.sys_info() |
将会显示:
Platform Linux-5.15.0-1057-aws-x86_64-with-glibc2.35
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]
Executable /home/circleci/python_env/bin/python
CPU x86_64 (36 cores)
Memory 69.1 GB
Core
├☑ mne 1.7.1 (latest release)
├☑ numpy 1.26.4 (OpenBLAS 0.3.23.dev with 4 threads)
├☑ scipy 1.14.0
└☑ matplotlib 3.9.0 (backend=agg)
Numerical (optional)
├☑ sklearn 1.5.0
├☑ numba 0.60.0
├☑ nibabel 5.2.1
├☑ nilearn 0.10.4
├☑ dipy 1.8.0
├☑ openmeeg 2.5.11
├☑ pandas 2.2.2
├☑ h5io 0.2.3
├☑ h5py 3.11.0
└☐ unavailable cupy
Visualization (optional)
├☑ pyvista 0.43.10 (OpenGL 4.5 (Core Profile) Mesa 23.2.1-1ubuntu3.1~22.04.2 via llvmpipe (LLVM 15.0.7, 256 bits))
├☑ pyvistaqt 0.11.1
├☑ vtk 9.3.1
├☑ qtpy 2.4.1 (PyQt6=6.7.1)
├☑ ipympl 0.9.4
├☑ pyqtgraph 0.13.7
├☑ mne-qt-browser 0.6.3
├☑ ipywidgets 8.1.3
├☑ trame_client 3.2.0
├☑ trame_server 3.0.2
├☑ trame_vtk 2.8.9
└☑ trame_vuetify 2.6.0
Ecosystem (optional)
├☑ mne-bids 0.15.0
├☑ mne-connectivity 0.7.0
├☑ neo 0.13.1
├☑ eeglabio 0.0.2-4
├☑ edfio 0.4.3
├☑ mffpy 0.9.0
├☑ pybv 0.7.5
└☐ unavailable mne-nirs, mne-features, mne-icalabel, mne-bids-pipeline