Anonymizing Freesurfer datasets

Summary of what happened:

Our subject identifiers that we push through Freesurfer are internal-only. We have a public-facing set of IDs, and we’d like to include anonymized Freesurfer data in the BIDS that we share. So: we need to take the stuff we have in $SUBJECT_DIR and transform it. Unfortunately, this isn’t as simple as just renaming the directories, as many of the outputs and logs include “# subjectname sub-XXXX” comments and cmdlines that include the subject name.

On the Freesurfer list, Douglas N. Greve said that one method might be:

# For mgz files, you can run
mri_convert --delete-cmds file.mgz file.mgz
# for surfaces, you can run the similar
mris_convert --delete-cmds lh.white lh.white

Command used (and if a helper script was used, a link to the helper script or the command generated):

mris_convert --delete-cmds original/sub-2001/surf/lh.pial test.pial
mris_info test.pial

This unfortunately still keeps the original “sub-2001” in the info output in a “file” line, note that the path is from the Singularity container mount that ran recon-all:

<snip>
z_(ras) : ( 0.0000,  1.0000,  0.0000)
c_(ras) : ( 0.5000, -6.0625, -33.2500)
file    : /mnt/mr2m4/intermediate_data/freesurfer/sub-2001/mri/wm.mgz
Volume Geometry vox2ras
-1.00000   0.00000   0.00000   128.50000;
<snip>

and

mri_convert --delete-cmds original/sub-2001/mri/T1.mgz test.mgz
mri_info test.mgz

also still has the original “sub-2001” in its output:

ras xform present
    xform info: x_r =  -1.0000, y_r =   0.0000, z_r =   0.0000, c_r =     0.5000
              : x_a =   0.0000, y_a =   0.0000, z_a =   1.0000, c_a =    -6.0625
              : x_s =   0.0000, y_s =  -1.0000, z_s =   0.0000, c_s =   -33.2500

talairach xfm : original/sub-2001/mri/transforms/talairach.xfm
Orientation   : LIA

Version:

mris_convert freesurfer 7.4.1

Environment (Docker, Singularity / Apptainer, custom installation):

Custom installation of Freesurfer on Alma Linux 9

More information:

I have attempted to use nibabel’s freesurfer.read_geometry on .pial files, but that spits out ValueError: cannot reshape array of size 501062 into shape (3) and I’m definitely not smart enough to figure out what’s going on in the bowels of that function.