Jupyter notebooks from the Pygers-MEG user group.
| Raw to BIDS Notebooks (01-explore-raw-files/) | What it covers |
|---|---|
01_Explore_Raw_OPM-MEG_Files.ipynb |
Exploring raw Cerca .cMEG sessions, converting them to FIF with cMEG2FIF, and checking the result |
02_Explore_Eye_Data_Files.ipynb |
Converting raw .EDF files to BIDS with edf2bids, and checking the result |
03_Dicom_to_BIDS_heudiconv.ipynb |
Converting DICOM files to NIFTI BIDS with HeuDiConv, and checking the result |
04_FIF_to_BIDS.ipynb |
Transitioning the FIF MEG data into the BIDS structure |
05_Sync_eye_to_MEG.ipynb |
Aligning eye data with MEG data |
06_Validate_BIDS.ipynb |
Final step running BIDS Validator |
| Preprocessing Notebooks (02-preprocessing/) | What it covers |
|---|---|
01_Explore_Data_and_Sensor_Quality.ipynb |
A first look at the recording and a check of every OPM sensor: flat and noisy channels, power spectra, time courses; saves the bad channels with reasons |
02_Downsample_Filter.ipynb |
Filter and downsample the eye-synced task recording and empty room recording |
03_Noise_Reduction.ipynb |
Reduce noise with reference regression, HFC, or both |
04_Artifact_Detection.ipynb |
Detect blinks, saccades, and other muscle movements and artifacts |
TBD1_FreeSurfer_recon-all.ipynb |
Generate surface reconstructions and BEM/scalp surfaces (step number to be assigned) |
TBD2_Coreg_and_Forward.ipynb |
Perform coregistration, create surface and volume source spaces and forward solutions (step number to be assigned) |
Put the notebook wherever Jupyter will run it. That means scotty for Option A, or your laptop for Option B.
Clone the repository (recommended, so you can git pull future notebooks):
git clone https://github.com/mrribbits/pygers-meg.gitOr download a single notebook: open it on GitHub and click Download raw file.
Before running a notebook, make a personal copy and work in that instead of the original:
cd pygers-meg/01-explore-raw-files
cp 01_Explore_Raw_OPM-MEG_Files.ipynb my_01_Explore_Raw_OPM-MEG_Files.ipynbRunning a notebook saves outputs into the file. Working in a my_ copy keeps the original unchanged, so updates always pull cleanly and your notes and outputs are never overwritten. Git ignores files starting with my_.
To get new notebooks and fixes, run this in your clone:
cd pygers-meg
git pullIf git pull stops with "Your local changes … would be overwritten by merge", you edited or ran an original notebook. Discard those changes (keep anything you want in a my_ copy first), then pull again:
git restore 01-explore-raw-files/01_Explore_Raw_OPM-MEG_Files.ipynb
git pullAfter an update, make a fresh my_ copy of any notebook that changed. Your old copy won't pick up the changes.
Choose one of the two setups below, then set your paths in Section 0.3 of the notebook.
- 0.1 Option A: Cluster (preferred). Jupyter runs on scotty, and you use it from your laptop's browser. The data never leave the server.
- 0.2 Option B: Local laptop. Jupyter runs on your laptop and reads the data from your mounted lab partition.
Choose a port number in the 4000–9999 range, avoiding 8800–8899. On a Mac, also avoid 5000 and 7000. The examples below use 4817. Replace it with your number everywhere it appears, and use the same number every time you start Jupyter (0.1.4 explains why).
In your laptop's Terminal, SSH to scotty with the tunnel:
ssh -L 4817:localhost:4817 NETID@scotty.pni.princeton.eduOn scotty, check that nobody else is using the port:
ss -ltn | grep 4817If this prints nothing, the port is free. If it prints a line, log out and start again with a different port.
You should install miniconda in your folder on your lab directory so that you do not run out of space in your home directory. You can then create a symlink in your home directory.
cd /jukebox/<lab>/<personal folder>
#download
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
#install
bash Miniconda3-latest-Linux-x86_64.sh
#if you don't want this base environment automatically loaded when you login:
conda config --set auto_activate_base false
source .bashrc
#remove install file
rm -r Miniconda3-latest-Linux-x86_64.sh
#create symlink in home directory
ln -s /jukebox/<lab>/<personal folder>/miniconda3 ~/.miniconda3
#check your setup
cd ~
conda info
conda env list
conda --helpconda create -n pygers-meg -c conda-forge python=3.11 mne mne-bids jupyterlab pandas ipykernel ipympl
conda activate pygers-meg
pip install "cmeg2fif[plot] @ git+https://github.com/mrribbits/cMEG2FIF-Scully.git"conda env create -f scotty-environment.yml -n pygers-meg
conda activate pygers-megcmeg2fif --versionOn later sessions, you only need conda activate pygers-meg.
jupyter lab --no-browser --ip=127.0.0.1 --port=4817 --ServerApp.port_retries=0 \
--ServerApp.custom_display_url=http://localhost:4817--ServerApp.port_retries=0 makes Jupyter fail loudly if the port is taken, instead of silently switching to a different port that your tunnel isn't connected to. --ServerApp.custom_display_url makes Jupyter print its address with localhost (see 0.1.4).
Copy the URL that Jupyter prints, which looks like http://localhost:4817/lab?token=..., and paste it into your local browser.
Why the same port, and localhost? Each Jupyter session saves a login cookie in your browser, named after its port, and browsers don't keep cookies apart by port. A new port every time piles up cookies until some tools refuse the request; the 3D coregistration window in the preprocessing notebooks fails with LineTooLong ("Got more than 8190 bytes"). With one port you keep one cookie. Using localhost instead of 127.0.0.1 also leaves behind any old cookies piled up for 127.0.0.1.
Press Ctrl-C twice in the scotty session. If you ever leave a server running by accident, jupyter server list shows it and jupyter server stop 4817 stops it.
conda create -n pygers-meg -c conda-forge python=3.11 mne jupyterlab pandas ipympl mne-bids
conda activate pygers-meg
pip install "cmeg2fif[plot] @ git+https://github.com/mrribbits/cMEG2FIF-Scully.git"cmeg2fif --versionTo start Jupyter, run conda activate pygers-meg and then jupyter lab.
All data stay on the server: do not copy them to your laptop. Mount the server instead:
- macOS: In Finder, choose Go → Connect to Server…
- Windows: In File Explorer, choose Map network drive.
ssh-keygen -t ed25519 -C "my-mac" # accept defaults; set a passphrase
ssh-copy-id NETID@scotty.pni.princeton.edu # the last time you type your passwordHost scotty scotty.pni.princeton.edu
HostName scotty.pni.princeton.edu
User NETID #EDIT THIS
IdentityFile ~/.ssh/id_ed25519
AddKeysToAgent yes
UseKeychain yes
ControlMaster auto
ControlPath ~/.ssh/cm-%r@%h:%p
ControlPersist 4h- In each script, add your NETID, PROJECT_DIR, and CONDA_ENV
mkdir -p ~/bin && mv ~/Downloads/scotty-jupyter ~/Downloads/scotty-shell ~/bin/
chmod +x ~/bin/scotty-jupyter ~/bin/scotty-shell
echo 'export PATH="$HOME/bin:$PATH"' >> ~/.zshrc && source ~/.zshrcThese lines will create a ~/bin folder and move the scripts from ~/Downloads to it. They will then make the scripts executable and add ~/bin to your path.
- The script will SSH into Scotty for you without a password.
- It uses your own port, the same every time (worked out from your NetID; if it's busy, the script tries the next one), and opens Jupyter at
localhost. See "Why the same port, andlocalhost?" in 0.1.4. - It will activate your pygers-meg environment.
cdyou into the pygers-meg folder.- And launch JupyterLab so it prints the URL you can use to paste into your Mac's browser.
- Pressing Ctrl-C twice stops Jupyter and closes the tunnel.
scotty-shell does everything scotty-jupyter does except start JupyterLab. It leaves you at a terminal prompt on scotty, so you can switch git branches or do other work before (or after) starting Jupyter.
- Like
scotty-jupyter, it SSHes into scotty with the tunnel on your own port,cds you into the pygers-meg folder, and activates your pygers-meg environment. - It prints the folder, environment, and current git branch, then waits at the prompt.
- Type
jlto start JupyterLab on the tunneled port. It prints the URL to paste into your Mac's browser, as in 0.1.4. - Press Ctrl-C twice to stop Jupyter and return to the prompt. You can then change branches and type
jlagain. - Type
exitto leave scotty and close the tunnel.
Both scripts use the same port, so run only one of them at a time.