From 9e80393d4f92b80b2f8c9fd92e10bcee00d7015c Mon Sep 17 00:00:00 2001 From: christine Date: Mon, 14 Jan 2019 17:49:51 -0800 Subject: [PATCH 01/11] added option in from_freesurfer to input .lta file (instead of only .dat) --- cortex/xfm.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/cortex/xfm.py b/cortex/xfm.py index 683feb76a..5b0cd1d7a 100644 --- a/cortex/xfm.py +++ b/cortex/xfm.py @@ -200,7 +200,7 @@ def to_fsl(self, anat_nii, direction='func>anat'): return fslx @classmethod - def from_freesurfer(cls, fs_register, func_nii, subject, freesurfer_subject_dir=None): + def from_freesurfer(cls, fs_register, func_nii, subject, freesurfer_subject_dir=None, lta=False): """Converts a FreeSurfer transform to a pycortex transform. Converts a transform computed using FreeSurfer alignment tools (e.g., bbregister) to @@ -211,9 +211,9 @@ def from_freesurfer(cls, fs_register, func_nii, subject, freesurfer_subject_dir= Parameters ---------- fs_register : array - 4x4 transformation matrix, described in an FreeSurfer register.dat file, for a transform computed + 4x4 transformation matrix, described in an FreeSurfer .dat or .lta file, for a transform computed FROM the func_nii volume TO the anatomical volume of the FreeSurfer subject `subject`. - Alternatively, a string file name for the FreeSurfer register.dat file. + Alternatively, a string file name for the FreeSurfer .dat or .lta file. func_nii : str or nibabel.Nifti1Image nibabel image object (or string path to nibabel-readable image) for (functional) data volume to be projected onto cortical surface @@ -223,6 +223,8 @@ def from_freesurfer(cls, fs_register, func_nii, subject, freesurfer_subject_dir= Directory of FreeSurfer subjects. Defaults to the value for the environment variable 'SUBJECTS_DIR' (which should be set by freesurfer) + lta : boolean | False + True if the input fs_register file is in LTA format. Defaults to False. Returns ------- @@ -246,7 +248,10 @@ def from_freesurfer(cls, fs_register, func_nii, subject, freesurfer_subject_dir= if isinstance(fs_register, string_types): with open(fs_register, 'r') as fid: L = fid.readlines() - anat2func = np.array([[np.float(s) for s in ll.split() if s] for ll in L[4:8]]) + if lta: + anat2func = np.array([[np.float(s) for s in ll.split() if s] for ll in L[5:9]]) + else: + anat2func = np.array([[np.float(s) for s in ll.split() if s] for ll in L[4:8]]) else: anat2func = fs_register From 5b8e07ed75ab68597160b5df2ed5bf5aa19242e3 Mon Sep 17 00:00:00 2001 From: christine Date: Mon, 14 Jan 2019 19:46:17 -0800 Subject: [PATCH 02/11] made function fs_manual for manual edits to freesurfer-generated alignment using FreeView --- cortex/align.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/cortex/align.py b/cortex/align.py index 502b856f5..ff4d9f519 100644 --- a/cortex/align.py +++ b/cortex/align.py @@ -82,7 +82,72 @@ def view_callback(aligner): return m -def automatic(subject, xfmname, reference, noclean=False, bbrtype="signed", pre_flirt_args='', use_fs_bbr=False): +def fs_manual(subject, xfmname, input_reg="register.dat", surf_color="green"): + """Open Freesurfer FreeView GUI for manually aligning/adjusting a functional + volume to the cortical surface for `subject`. This creates a new transform + called `xfm`. The name of a nibabel-readable file (e.g. nii) should be + supplied as `reference`. This image will be copied into the database. + + **IMPORTANT!!!** The function assumes that the resulting .lta file is saved as + "/auto/k2/share/pycortex_store/subject/transform/xfmname/{input_reg_name}.lta". + + subject : str + Subject identifier. + xfmname : str + The name of the transform to be fixed. + surf_color : str | "green" + Color of the (white matter) surfacesself. Default is "green". This can + also be adjusted in the FreeView GUI. + + Returns + ------- + xfm : Transform object + The new pycortex transform. + """ + + import subprocess as sp + from .xfm import Transform + from .database import db + + # All the checks + # Check whether transform w/ this xfmname already exists + try: + sub_xfm = db.get_xfm(subject, xfmname) + + # if masks have been cached, quit! user must remove them by hand + from glob import glob + if len(glob(db.get_paths(subject)['masks'].format(xfmname=xfmname, type='*'))): + print('Refusing to overwrite existing transform %s because there are cached masks. Delete the masks manually if you want to modify the transform.' % xfmname) + raise ValueError('Exiting...') + + # Some filenames + reference = sub_xfm.reference.get_filename() + xfm_dir = os.path.dirname(reference) + except IOError: + print("Transform does not exist!") + + + # Command for FreeView and run + cmd = ("freeview -v $SUBJECTS_DIR/{sub}/mri/orig.mgz " + "{ref}:reg={xfm_dir}/register.dat " + "-f $SUBJECTS_DIR/{sub}/surf/lh.white:edgecolor={c} $SUBJECTS_DIR/{sub}/surf/rh.white:edgecolor={c}") + cmd = cmd.format(sub=subject, ref=reference, xfm_dir=xfm_dir, c=surf_color) + + # Run and save transform when user is done editing + if sp.call(cmd, shell=True) != 0: + raise IOError("Problem with FreeView!") + else: + # Save transform + reg_name = input_reg[:-4] + ".lta" + fs_xfm = os.path.join(xfm_dir, reg_name) + xfm = Transform.from_freesurfer(fs_xfm, reference, subject, lta=True) + db.save_xfm(subject, xfmname, xfm.xfm, xfmtype='magnet', reference=reference) + print("saved xfm") + + return xfm + + +def automatic(subject, xfmname, reference, noclean=False, bbrtype="signed", pre_flirt_args='', use_fs_bbr=False, save_dat=False): """Create an automatic alignment using the FLIRT boundary-based alignment (BBR) from FSL. If `noclean`, intermediate files will not be removed from /tmp. The `reference` image and resulting @@ -113,6 +178,9 @@ def automatic(subject, xfmname, reference, noclean=False, bbrtype="signed", pre_ Additional arguments that are passed to the FLIRT pre-alignment step (not BBR). use_fs_bbr : bool, optional If True will use freesurfer bbregister instead of FSL BBR. + save_dat : bool, optional + If True, will save the register.dat file from freesurfer bbregister into + freesurfer's $SUBJECTS_DIR/subject/tmp. Returns ------- @@ -139,6 +207,7 @@ def automatic(subject, xfmname, reference, noclean=False, bbrtype="signed", pre_ print('Running freesurfer BBR') cmd = 'bbregister --s {sub} --mov {absref} --init-fsl --reg {cache}/register.dat --t1' cmd = cmd.format(sub=subject, absref=absreference, cache=cache) + if sp.call(cmd, shell=True) != 0: raise IOError('Error calling freesurfer BBR!') @@ -172,6 +241,13 @@ def automatic(subject, xfmname, reference, noclean=False, bbrtype="signed", pre_ xfm.save(subject,xfmname,'coord') print('Success') + if save_dat: + cmd = 'mv {reg} /auto/k2/share/pycortex_store/{sub}/transforms/{xfm}' + cmd = cmd.format(reg=os.path.join(cache, "register.dat"), sub=subject, + xfm=xfmname) + if sp.call(cmd, shell=True) != 0: + raise IOError('Error saving register.dat file') + finally: if not noclean: shutil.rmtree(cache) From c7e4962a1a1f8c3b2dc6627f3cb17d31cdad68e6 Mon Sep 17 00:00:00 2001 From: christine Date: Mon, 14 Jan 2019 20:06:44 -0800 Subject: [PATCH 03/11] fixed error --- cortex/align.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cortex/align.py b/cortex/align.py index ff4d9f519..4a2ec0e24 100644 --- a/cortex/align.py +++ b/cortex/align.py @@ -82,7 +82,7 @@ def view_callback(aligner): return m -def fs_manual(subject, xfmname, input_reg="register.dat", surf_color="green"): +def fs_manual(subject, xfmname, output_name="register", surf_color="green"): """Open Freesurfer FreeView GUI for manually aligning/adjusting a functional volume to the cortical surface for `subject`. This creates a new transform called `xfm`. The name of a nibabel-readable file (e.g. nii) should be @@ -129,16 +129,16 @@ def fs_manual(subject, xfmname, input_reg="register.dat", surf_color="green"): # Command for FreeView and run cmd = ("freeview -v $SUBJECTS_DIR/{sub}/mri/orig.mgz " - "{ref}:reg={xfm_dir}/register.dat " + "{ref}" "-f $SUBJECTS_DIR/{sub}/surf/lh.white:edgecolor={c} $SUBJECTS_DIR/{sub}/surf/rh.white:edgecolor={c}") - cmd = cmd.format(sub=subject, ref=reference, xfm_dir=xfm_dir, c=surf_color) + cmd = cmd.format(sub=subject, ref=reference, c=surf_color) # Run and save transform when user is done editing if sp.call(cmd, shell=True) != 0: raise IOError("Problem with FreeView!") else: # Save transform - reg_name = input_reg[:-4] + ".lta" + reg_name = output_name + ".lta" fs_xfm = os.path.join(xfm_dir, reg_name) xfm = Transform.from_freesurfer(fs_xfm, reference, subject, lta=True) db.save_xfm(subject, xfmname, xfm.xfm, xfmtype='magnet', reference=reference) From 33b4219186a1cf93e9251a27aa6f70a5a6e8f35e Mon Sep 17 00:00:00 2001 From: christine Date: Mon, 14 Jan 2019 20:08:54 -0800 Subject: [PATCH 04/11] fixed error --- cortex/align.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cortex/align.py b/cortex/align.py index 4a2ec0e24..b63cdadf7 100644 --- a/cortex/align.py +++ b/cortex/align.py @@ -129,7 +129,7 @@ def fs_manual(subject, xfmname, output_name="register", surf_color="green"): # Command for FreeView and run cmd = ("freeview -v $SUBJECTS_DIR/{sub}/mri/orig.mgz " - "{ref}" + "{ref} " "-f $SUBJECTS_DIR/{sub}/surf/lh.white:edgecolor={c} $SUBJECTS_DIR/{sub}/surf/rh.white:edgecolor={c}") cmd = cmd.format(sub=subject, ref=reference, c=surf_color) From 692d00223950d44e862943e608572b5f518bd4c1 Mon Sep 17 00:00:00 2001 From: christine Date: Mon, 14 Jan 2019 20:13:10 -0800 Subject: [PATCH 05/11] fixed file save error --- cortex/align.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cortex/align.py b/cortex/align.py index b63cdadf7..e7ed2a0f1 100644 --- a/cortex/align.py +++ b/cortex/align.py @@ -139,8 +139,7 @@ def fs_manual(subject, xfmname, output_name="register", surf_color="green"): else: # Save transform reg_name = output_name + ".lta" - fs_xfm = os.path.join(xfm_dir, reg_name) - xfm = Transform.from_freesurfer(fs_xfm, reference, subject, lta=True) + xfm = Transform.from_freesurfer(reg_name, reference, subject, lta=True) db.save_xfm(subject, xfmname, xfm.xfm, xfmtype='magnet', reference=reference) print("saved xfm") From 26205574b795cc3da0117e9fa4d8d1e3801f9d6d Mon Sep 17 00:00:00 2001 From: christine Date: Tue, 15 Jan 2019 11:55:04 -0800 Subject: [PATCH 06/11] fixed indexing errors reading in transforms for to_freesurfer --- cortex/xfm.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/cortex/xfm.py b/cortex/xfm.py index 5b0cd1d7a..a40e1ac2e 100644 --- a/cortex/xfm.py +++ b/cortex/xfm.py @@ -200,7 +200,7 @@ def to_fsl(self, anat_nii, direction='func>anat'): return fslx @classmethod - def from_freesurfer(cls, fs_register, func_nii, subject, freesurfer_subject_dir=None, lta=False): + def from_freesurfer(cls, fs_register, func_nii, subject, freesurfer_subject_dir=None): """Converts a FreeSurfer transform to a pycortex transform. Converts a transform computed using FreeSurfer alignment tools (e.g., bbregister) to @@ -223,8 +223,6 @@ def from_freesurfer(cls, fs_register, func_nii, subject, freesurfer_subject_dir= Directory of FreeSurfer subjects. Defaults to the value for the environment variable 'SUBJECTS_DIR' (which should be set by freesurfer) - lta : boolean | False - True if the input fs_register file is in LTA format. Defaults to False. Returns ------- @@ -248,10 +246,8 @@ def from_freesurfer(cls, fs_register, func_nii, subject, freesurfer_subject_dir= if isinstance(fs_register, string_types): with open(fs_register, 'r') as fid: L = fid.readlines() - if lta: - anat2func = np.array([[np.float(s) for s in ll.split() if s] for ll in L[5:9]]) - else: - anat2func = np.array([[np.float(s) for s in ll.split() if s] for ll in L[4:8]]) + + anat2func = np.array([[np.float(s) for s in ll.split() if s] for ll in L[4:8]]) else: anat2func = fs_register @@ -358,7 +354,7 @@ def to_freesurfer(self, fs_register, subject, freesurfer_subject_dir=None): # Read tkvox2ras transform for the functional volume try: cmd = ('mri_info', '--vox2ras-tkr', func_nii) - L = subprocess.check_output(cmd).splitlines() + L = subprocess.check_output(cmd).splitlines()[1:] func_tkrvox2ras = np.array([[np.float(s) for s in ll.split() if s] for ll in L]) except OSError: print ("Error occured while executing:\n{}".format(' '.join(cmd))) @@ -367,7 +363,7 @@ def to_freesurfer(self, fs_register, subject, freesurfer_subject_dir=None): # Read voxel resolution of the functional volume try: cmd = ('mri_info', '--res', func_nii) - ll = subprocess.check_output(cmd) + ll = subprocess.check_output(cmd).split("\n")[1] func_voxres = np.array([np.float(s) for s in ll.split() if s]) except OSError: print ("Error occured while executing:\n{}".format(' '.join(cmd))) From 8ba746f6f7abb6bfe7e68e6a8666d6874b777345 Mon Sep 17 00:00:00 2001 From: christine Date: Tue, 15 Jan 2019 12:03:30 -0800 Subject: [PATCH 07/11] fixed coord and reg file specification error in fs_manual --- cortex/align.py | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/cortex/align.py b/cortex/align.py index e7ed2a0f1..8bb64c759 100644 --- a/cortex/align.py +++ b/cortex/align.py @@ -82,19 +82,23 @@ def view_callback(aligner): return m -def fs_manual(subject, xfmname, output_name="register", surf_color="green"): +def fs_manual(subject, xfmname, output_name="register.lta", surf_color="green"): """Open Freesurfer FreeView GUI for manually aligning/adjusting a functional volume to the cortical surface for `subject`. This creates a new transform called `xfm`. The name of a nibabel-readable file (e.g. nii) should be supplied as `reference`. This image will be copied into the database. **IMPORTANT!!!** The function assumes that the resulting .lta file is saved as - "/auto/k2/share/pycortex_store/subject/transform/xfmname/{input_reg_name}.lta". + "{default folder chosen by FreeView (this should be cache)}/{output_name}". + Parameters + ---------- subject : str Subject identifier. xfmname : str The name of the transform to be fixed. + output_name : str + The name of the .lta file generated after FreeView editing. surf_color : str | "green" Color of the (white matter) surfacesself. Default is "green". This can also be adjusted in the FreeView GUI. @@ -106,12 +110,14 @@ def fs_manual(subject, xfmname, output_name="register", surf_color="green"): """ import subprocess as sp + import tempfile from .xfm import Transform from .database import db # All the checks # Check whether transform w/ this xfmname already exists try: + cache = tempfile.mkdtemp() sub_xfm = db.get_xfm(subject, xfmname) # if masks have been cached, quit! user must remove them by hand @@ -119,34 +125,40 @@ def fs_manual(subject, xfmname, output_name="register", surf_color="green"): if len(glob(db.get_paths(subject)['masks'].format(xfmname=xfmname, type='*'))): print('Refusing to overwrite existing transform %s because there are cached masks. Delete the masks manually if you want to modify the transform.' % xfmname) raise ValueError('Exiting...') - - # Some filenames - reference = sub_xfm.reference.get_filename() - xfm_dir = os.path.dirname(reference) except IOError: print("Transform does not exist!") + # Load transform-relevant things + reference = sub_xfm.reference.get_filename() + xfm_dir = os.path.dirname(reference) + _ = sub_xfm.to_freesurfer(os.path.join(cache, "register.dat"), subject) # Transform in freesurfer .dat format # Command for FreeView and run cmd = ("freeview -v $SUBJECTS_DIR/{sub}/mri/orig.mgz " - "{ref} " + "{ref}:reg={reg} " "-f $SUBJECTS_DIR/{sub}/surf/lh.white:edgecolor={c} $SUBJECTS_DIR/{sub}/surf/rh.white:edgecolor={c}") - cmd = cmd.format(sub=subject, ref=reference, c=surf_color) + cmd = cmd.format(sub=subject, ref=reference, reg=os.path.join(cache, "register.dat"), c=surf_color) # Run and save transform when user is done editing if sp.call(cmd, shell=True) != 0: raise IOError("Problem with FreeView!") else: - # Save transform - reg_name = output_name + ".lta" - xfm = Transform.from_freesurfer(reg_name, reference, subject, lta=True) - db.save_xfm(subject, xfmname, xfm.xfm, xfmtype='magnet', reference=reference) + # Convert transform into .dat format + reg_dat = os.path.join(cache, output_name[:-4] + ".dat") + cmd = "lta_convert --inlta {inlta} --outreg {regdat}" + cmd = cmd.format(inlta=os.path.join(cache, output_name), regdat=reg_dat) + if sp.call(cmd, shell=True) != 0: + raise IOError("Error converting lta into dat!") + + # Save transform to pycortex + xfm = Transform.from_freesurfer(reg_dat, reference, subject) + db.save_xfm(subject, xfmname, xfm.xfm, xfmtype='coord', reference=reference) print("saved xfm") return xfm -def automatic(subject, xfmname, reference, noclean=False, bbrtype="signed", pre_flirt_args='', use_fs_bbr=False, save_dat=False): +def automatic(subject, xfmname, reference, noclean=False, bbrtype="signed", pre_flirt_args='', use_fs_bbr=False): """Create an automatic alignment using the FLIRT boundary-based alignment (BBR) from FSL. If `noclean`, intermediate files will not be removed from /tmp. The `reference` image and resulting @@ -240,13 +252,6 @@ def automatic(subject, xfmname, reference, noclean=False, bbrtype="signed", pre_ xfm.save(subject,xfmname,'coord') print('Success') - if save_dat: - cmd = 'mv {reg} /auto/k2/share/pycortex_store/{sub}/transforms/{xfm}' - cmd = cmd.format(reg=os.path.join(cache, "register.dat"), sub=subject, - xfm=xfmname) - if sp.call(cmd, shell=True) != 0: - raise IOError('Error saving register.dat file') - finally: if not noclean: shutil.rmtree(cache) From 12e423578a9cb9592ddf12f7a20246343dd9e9d4 Mon Sep 17 00:00:00 2001 From: christine Date: Tue, 15 Jan 2019 14:48:14 -0800 Subject: [PATCH 08/11] added automatic loading of pial surface and temp dir cleaning, changed temp dir prefix to fsalign --- cortex/align.py | 102 ++++++++++++++++++++++++++++-------------------- 1 file changed, 60 insertions(+), 42 deletions(-) diff --git a/cortex/align.py b/cortex/align.py index 8bb64c759..5e1651421 100644 --- a/cortex/align.py +++ b/cortex/align.py @@ -82,14 +82,14 @@ def view_callback(aligner): return m -def fs_manual(subject, xfmname, output_name="register.lta", surf_color="green"): +def fs_manual(subject, xfmname, output_name="register.lta", wm_color="blue", pial_color="red", noclean=False): """Open Freesurfer FreeView GUI for manually aligning/adjusting a functional volume to the cortical surface for `subject`. This creates a new transform called `xfm`. The name of a nibabel-readable file (e.g. nii) should be supplied as `reference`. This image will be copied into the database. **IMPORTANT!!!** The function assumes that the resulting .lta file is saved as - "{default folder chosen by FreeView (this should be cache)}/{output_name}". + "{default folder chosen by FreeView (should be /tmp/fsalign_xxx)}/{output_name}". Parameters ---------- @@ -99,63 +99,81 @@ def fs_manual(subject, xfmname, output_name="register.lta", surf_color="green"): The name of the transform to be fixed. output_name : str The name of the .lta file generated after FreeView editing. - surf_color : str | "green" - Color of the (white matter) surfacesself. Default is "green". This can + wm_color : str | "blue" + Color of the white matter surface. Default is "blue". This can also be adjusted in the FreeView GUI. + pial_color : str | "red" + Color of the pial surface. Default is "red". This can also be adjusted + the FreeView GUI. + noclean : boolean | False + If True, intermediate files will not be removed from /tmp/fsalign_xxx + (this is useful for debugging things), and the returned value will be + the name of the temp directory. Default False. Returns ------- - xfm : Transform object - The new pycortex transform. + Nothing unless noclean is true. """ import subprocess as sp import tempfile + import shutil from .xfm import Transform from .database import db # All the checks # Check whether transform w/ this xfmname already exists - try: - cache = tempfile.mkdtemp() - sub_xfm = db.get_xfm(subject, xfmname) + retval = None - # if masks have been cached, quit! user must remove them by hand - from glob import glob - if len(glob(db.get_paths(subject)['masks'].format(xfmname=xfmname, type='*'))): - print('Refusing to overwrite existing transform %s because there are cached masks. Delete the masks manually if you want to modify the transform.' % xfmname) - raise ValueError('Exiting...') - except IOError: - print("Transform does not exist!") - - # Load transform-relevant things - reference = sub_xfm.reference.get_filename() - xfm_dir = os.path.dirname(reference) - _ = sub_xfm.to_freesurfer(os.path.join(cache, "register.dat"), subject) # Transform in freesurfer .dat format - - # Command for FreeView and run - cmd = ("freeview -v $SUBJECTS_DIR/{sub}/mri/orig.mgz " - "{ref}:reg={reg} " - "-f $SUBJECTS_DIR/{sub}/surf/lh.white:edgecolor={c} $SUBJECTS_DIR/{sub}/surf/rh.white:edgecolor={c}") - cmd = cmd.format(sub=subject, ref=reference, reg=os.path.join(cache, "register.dat"), c=surf_color) - - # Run and save transform when user is done editing - if sp.call(cmd, shell=True) != 0: - raise IOError("Problem with FreeView!") - else: - # Convert transform into .dat format - reg_dat = os.path.join(cache, output_name[:-4] + ".dat") - cmd = "lta_convert --inlta {inlta} --outreg {regdat}" - cmd = cmd.format(inlta=os.path.join(cache, output_name), regdat=reg_dat) + try: + try: + cache = tempfile.mkdtemp(prefix="fsalign_") + sub_xfm = db.get_xfm(subject, xfmname) + + # if masks have been cached, quit! user must remove them by hand + from glob import glob + if len(glob(db.get_paths(subject)['masks'].format(xfmname=xfmname, type='*'))): + print('Refusing to overwrite existing transform %s because there are cached masks. Delete the masks manually if you want to modify the transform.' % xfmname) + raise ValueError('Exiting...') + except IOError: + print("Transform does not exist!") + + # Load transform-relevant things + reference = sub_xfm.reference.get_filename() + xfm_dir = os.path.dirname(reference) + _ = sub_xfm.to_freesurfer(os.path.join(cache, "register.dat"), subject) # Transform in freesurfer .dat format + + # Command for FreeView and run + cmd = ("freeview -v $SUBJECTS_DIR/{sub}/mri/orig.mgz " + "{ref}:reg={reg} " + "-f $SUBJECTS_DIR/{sub}/surf/lh.white:edgecolor={wmc} $SUBJECTS_DIR/{sub}/surf/rh.white:edgecolor={wmc} " + "$SUBJECTS_DIR/{sub}/surf/lh.pial:edgecolor={pialc} $SUBJECTS_DIR/{sub}/surf/rh.pial:edgecolor={pialc}") + cmd = cmd.format(sub=subject, ref=reference, reg=os.path.join(cache, "register.dat"), + wmc=wm_color, pialc=pial_color) + + # Run and save transform when user is done editing if sp.call(cmd, shell=True) != 0: - raise IOError("Error converting lta into dat!") + raise IOError("Problem with FreeView!") + else: + # Convert transform into .dat format + reg_dat = os.path.join(cache, output_name[:-4] + ".dat") + cmd = "lta_convert --inlta {inlta} --outreg {regdat}" + cmd = cmd.format(inlta=os.path.join(cache, output_name), regdat=reg_dat) + if sp.call(cmd, shell=True) != 0: + raise IOError("Error converting lta into dat!") - # Save transform to pycortex - xfm = Transform.from_freesurfer(reg_dat, reference, subject) - db.save_xfm(subject, xfmname, xfm.xfm, xfmtype='coord', reference=reference) - print("saved xfm") + # Save transform to pycortex + xfm = Transform.from_freesurfer(reg_dat, reference, subject) + db.save_xfm(subject, xfmname, xfm.xfm, xfmtype='coord', reference=reference) + print("saved xfm") + + finally: + if not noclean: + shutil.rmtree(cache) + else: + retval = cache - return xfm + return retval def automatic(subject, xfmname, reference, noclean=False, bbrtype="signed", pre_flirt_args='', use_fs_bbr=False): From 10869f272268915681684a568af75fdadf5249da Mon Sep 17 00:00:00 2001 From: christine Date: Tue, 15 Jan 2019 14:49:14 -0800 Subject: [PATCH 09/11] removed some wrong comments --- cortex/align.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/cortex/align.py b/cortex/align.py index 5e1651421..94e0e893d 100644 --- a/cortex/align.py +++ b/cortex/align.py @@ -121,8 +121,6 @@ def fs_manual(subject, xfmname, output_name="register.lta", wm_color="blue", pia from .xfm import Transform from .database import db - # All the checks - # Check whether transform w/ this xfmname already exists retval = None try: From 4d164a43cea7cb9fc1cc6f412e7854a23579c16b Mon Sep 17 00:00:00 2001 From: christine Date: Tue, 15 Jan 2019 15:34:42 -0800 Subject: [PATCH 10/11] changed splitting path name --- cortex/align.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cortex/align.py b/cortex/align.py index 94e0e893d..81be2c509 100644 --- a/cortex/align.py +++ b/cortex/align.py @@ -154,7 +154,7 @@ def fs_manual(subject, xfmname, output_name="register.lta", wm_color="blue", pia raise IOError("Problem with FreeView!") else: # Convert transform into .dat format - reg_dat = os.path.join(cache, output_name[:-4] + ".dat") + reg_dat = os.path.join(cache, os.path.splitext(output_name)[0] + ".dat") cmd = "lta_convert --inlta {inlta} --outreg {regdat}" cmd = cmd.format(inlta=os.path.join(cache, output_name), regdat=reg_dat) if sp.call(cmd, shell=True) != 0: From d8f346bc0118bbcf97691ad184d62e4aa833bf83 Mon Sep 17 00:00:00 2001 From: Anwar Nunez-Elizalde Date: Tue, 15 Jan 2019 15:42:13 -0800 Subject: [PATCH 11/11] DOC: minor --- cortex/align.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cortex/align.py b/cortex/align.py index 81be2c509..9c4d93691 100644 --- a/cortex/align.py +++ b/cortex/align.py @@ -85,10 +85,10 @@ def view_callback(aligner): def fs_manual(subject, xfmname, output_name="register.lta", wm_color="blue", pial_color="red", noclean=False): """Open Freesurfer FreeView GUI for manually aligning/adjusting a functional volume to the cortical surface for `subject`. This creates a new transform - called `xfm`. The name of a nibabel-readable file (e.g. nii) should be + called `xfmname`. The name of a nibabel-readable file (e.g. NIfTI) should be supplied as `reference`. This image will be copied into the database. - **IMPORTANT!!!** The function assumes that the resulting .lta file is saved as + IMPORTANT: This function assumes that the resulting .lta file is saved as: "{default folder chosen by FreeView (should be /tmp/fsalign_xxx)}/{output_name}". Parameters @@ -96,7 +96,7 @@ def fs_manual(subject, xfmname, output_name="register.lta", wm_color="blue", pia subject : str Subject identifier. xfmname : str - The name of the transform to be fixed. + The name of the transform to be modified. output_name : str The name of the .lta file generated after FreeView editing. wm_color : str | "blue"