예제 #1
0
def augmentconfig(c):
    '''add more configuration values that are *derived* from the current configuration,
    and also checks whether some options are set properly'''

    # ensure surfdir is set properly.
    # normally it should end in 'surf'

    # try to be smart and deduce subject id from surfdir (if not set explicitly)
    surfdir = c['surfdir']
    if surfdir:
        surfdir = os.path.abspath(surfdir)
        parent, nm = os.path.split(surfdir)

        if nm != 'surf':
            jn = pathjoin(surfdir, 'surf')
            if os.path.exists(jn):
                surfdir = jn
        elif nm == 'SUMA':
            surfdir = parent

        if not (os.path.exists(surfdir) or os.path.split(surfdir)[1] == 'surf'):
            print('Warning: surface directory %s not does exist or does not end in "surf"' % surfdir)
            surfdir = None

        c['surfdir'] = surfdir

        # set SUMADIR as well
        c['sumadir'] = '%(surfdir)s/SUMA/' % c

    # derive subject id from surfdir

    sid = os.path.split(os.path.split(surfdir)[0])[1] if surfdir else None

    if c.get('sid') is None:
        c['sid'] = sid

    if c['sid'] is None:
        print"Warning: no subject id specified"


    c['prefix_sv2anat'] = 'SurfVol2anat'

    # update steps
    if c.get('steps', 'all') == 'all':
        c['steps'] = 'toafni+mapico+moresurfs+skullstrip+align+makespec+makespecboth+makesurfmasks'



    hasanatvol = 'anatvol' in c and c['anatvol']
    hasepivol = 'epivol' in c and c['epivol']
    hasexpvol = 'expvol' in c and c['expvol']
    hasisepi = 'isepi' in c

    if hasexpvol:
        if hasanatvol or hasepivol:
            raise Exception("expvol specified, but also anatvol or epivol - illegal!")
        if not hasisepi:
            raise Exception("not specified whether expvol is EPI (yes) or anat (no)")

    else:
        if hasanatvol:
            if 'epivol' in c and c['epivol']:
                raise Exception("Cannot have both anatvol and epivol")
            else:
                c['expvol'] = c['anatvol']
                c['isepi'] = False
                del(c['anatvol'])
        else:
            if hasepivol:
                c['expvol'] = c['epivol']
                c['isepi'] = True
                del(c['epivol'])
            else:
                print("Warning: no anatomical or functional experimental voume defined")

    def yesno2bool(d, k): # dict, key
        '''Assuming d[k] contains the word 'yes' or 'no', makes d[k] a boolean
        (True=='yes',False=='no'); otherwise an exception is thrown. The dict is updated'''
        val = d[k]
        if val is None:
            b = False
        elif type(val) == bool:
            b = val
        else:
            v = val.lower()
            if v == 'yes':
                b = True
            elif v == 'no':
                b = False
            else:
                raise Exception("Not yes or no: %s" % val)
        d[k] = b

    yesno2bool(c, 'AddEdge')

    if c['identity']:
        c['expvol_ss'] = c['anatval_ss'] = False
    else:
        yesno2bool(c, 'expvol_ss')
        yesno2bool(c, 'isepi')

    # see if we can get the fs_sid
    # (only if surfdir is set properly)
    # XXX not sure if this still makes sense
    c['fs_sid'] = None
    surfdir = c.get('surfdir', None)
    if not surfdir is None and os.path.exists(surfdir):
        fs_log_fn = pathjoin(surfdir, '..', 'scripts', 'recon-all.done')
        print "Looking in %s" % fs_log_fn
        if os.path.exists(fs_log_fn):
            with open(fs_log_fn) as f:
                lines = f.read().split('\n')
                for line in lines:
                    if line.startswith('SUBJECT'):
                        fs_sid = line[8:]
                        c['fs_sid'] = fs_sid
                        print "Found Freesurfer sid %s" % fs_sid
                        break

    if c['fs_sid'] is None:
        c['fs_sid'] = sid
        print "Unable to find proper Freesurfer sid"

    pathvars = ['anatvol', 'expvol', 'epivol', 'refdir', 'surfdir']
    for pathvar in pathvars:
        if pathvar in c and c[pathvar]:
            c[pathvar] = os.path.abspath(c[pathvar])
            print "Set absolute path for %s: %s" % (pathvar, c[pathvar])

    if c['template'] and c['notemplate']:
        error('Cannot have both template and notemplate')

    if 'expvol' in c:
        p, n, o, e = utils.afni_fileparts(c['expvol'])

        if c.get('outvol_space', None) is None:
            cmd = '3dAttribute TEMPLATE_SPACE %s' % c['expvol']
            outvol_space = utils.cmd_capture_output(cmd)
            outvol_space = outvol_space.split('~')[0].strip()
            if len(outvol_space) and not c['notemplate'] and outvol_space.lower() != 'orig':
                print "Detected TEMPLATE_SPACE=%s" % outvol_space
                c['outvol_space'] = outvol_space
                if o == '+orig':
                    o = '+tlrc'
                    print "Template space '%s' detected: output has extension %s" % (outvol_space, o)
                c['template'] = True
            else:
                c['outvol_space'] = '+orig'

        if len(o): #'+orig' or '+tlrc'
            c['outvol_ext'] = o
            c['outvol_fullext'] = o + e
            c['outvol_view'] = o[1:]
        else:
            # For NIFTI - output in orig or tlrc
            c['outvol_view'] = 'tlrc' if c['template'] else 'orig'
            c['outvol_ext'] = '+' + c['outvol_view']
            c['outvol_fullext'] = c['outvol_ext'] + '.HEAD'


    return c
예제 #2
0
def augmentconfig(c):
    '''add more configuration values that are *derived* from the current configuration,
    and also checks whether some options are set properly'''

    # ensure surfdir is set properly.
    # normally it should end in 'surf'

    # try to be smart and deduce subject id from surfdir (if not set explicitly)
    surfdir = c['surfdir']
    if surfdir:
        surfdir = os.path.abspath(surfdir)
        parent, nm = os.path.split(surfdir)

        if nm != 'surf':
            jn = os.path.join(surfdir, 'surf')
            if os.path.exists(jn):
                surfdir = jn
        elif nm == 'SUMA':
            surfdir = parent

        if not (os.path.exists(surfdir)
                or os.path.split(surfdir)[1] == 'surf'):
            print(
                'Warning: surface directory %s not does exist or does not end in "surf"'
                % surfdir)
            surfdir = None

        c['surfdir'] = surfdir

        # set SUMADIR as well
        c['sumadir'] = '%(surfdir)s/SUMA/' % c

    # derive subject id from surfdir

    sid = os.path.split(os.path.split(surfdir)[0])[1] if surfdir else None

    if c.get('sid') is None:
        c['sid'] = sid

    if c['sid'] is None:
        print "Warning: no subject id specified"

    c['prefix_sv2anat'] = 'SurfVol2anat'

    # update steps
    if c.get('steps', 'all') == 'all':
        c['steps'] = 'toafni+mapico+moresurfs+skullstrip+align+makespec+makespecboth+makesurfmasks'

    hasanatvol = 'anatvol' in c and c['anatvol']
    hasepivol = 'epivol' in c and c['epivol']
    hasexpvol = 'expvol' in c and c['expvol']
    hasisepi = 'isepi' in c

    if hasexpvol:
        if hasanatvol or hasepivol:
            raise Exception(
                "expvol specified, but also anatvol or epivol - illegal!")
        if not hasisepi:
            raise Exception(
                "not specified whether expvol is EPI (yes) or anat (no)")

    else:
        if hasanatvol:
            if 'epivol' in c and c['epivol']:
                raise Exception("Cannot have both anatvol and epivol")
            else:
                c['expvol'] = c['anatvol']
                c['isepi'] = False
                del (c['anatvol'])
        else:
            if hasepivol:
                c['expvol'] = c['epivol']
                c['isepi'] = True
                del (c['epivol'])
            else:
                print(
                    "Warning: no anatomical or functional experimental voume defined"
                )

    def yesno2bool(d, k):  # dict, key
        '''Assuming d[k] contains the word 'yes' or 'no', makes d[k] a boolean
        (True=='yes',False=='no'); otherwise an exception is thrown. The dict is updated'''
        val = d[k]
        if val is None:
            b = False
        elif type(val) == bool:
            b = val
        else:
            v = val.lower()
            if v == 'yes':
                b = True
            elif v == 'no':
                b = False
            else:
                raise Exception("Not yes or no: %s" % val)
        d[k] = b

    yesno2bool(c, 'AddEdge')

    if c['identity']:
        c['expvol_ss'] = c['anatval_ss'] = False
    else:
        yesno2bool(c, 'expvol_ss')
        yesno2bool(c, 'isepi')

    # see if we can get the fs_sid
    # (only if surfdir is set properly)
    # XXX not sure if this still makes sense
    c['fs_sid'] = None
    surfdir = c.get('surfdir', None)
    if not surfdir is None and os.path.exists(surfdir):
        fs_log_fn = os.path.join(surfdir, '..', 'scripts', 'recon-all.done')
        print "Looking in %s" % fs_log_fn
        if os.path.exists(fs_log_fn):
            with open(fs_log_fn) as f:
                lines = f.read().split('\n')
                for line in lines:
                    if line.startswith('SUBJECT'):
                        fs_sid = line[8:]
                        c['fs_sid'] = fs_sid
                        print "Found Freesurfer sid %s" % fs_sid
                        break

    if c['fs_sid'] is None:
        c['fs_sid'] = sid
        print "Unable to find proper Freesurfer sid"

    pathvars = ['anatvol', 'expvol', 'epivol', 'refdir', 'surfdir']
    for pathvar in pathvars:
        if pathvar in c and c[pathvar]:
            c[pathvar] = os.path.abspath(c[pathvar])
            print "Set absolute path for %s: %s" % (pathvar, c[pathvar])

    if c['template'] and c['notemplate']:
        error('Cannot have both template and notemplate')

    if 'expvol' in c:
        p, n, o, e = utils.afni_fileparts(c['expvol'])

        if c.get('outvol_space', None) is None:
            cmd = '3dAttribute TEMPLATE_SPACE %s' % c['expvol']
            outvol_space = utils.cmd_capture_output(cmd)
            outvol_space = outvol_space.split('~')[0].strip()
            if len(outvol_space) and not c[
                    'notemplate'] and outvol_space.lower() != 'orig':
                print "Detected TEMPLATE_SPACE=%s" % outvol_space
                c['outvol_space'] = outvol_space
                if o == '+orig':
                    o = '+tlrc'
                    print "Template space '%s' detected: output has extension %s" % (
                        outvol_space, o)
                c['template'] = True
            else:
                c['outvol_space'] = '+orig'

        if len(o):  #'+orig' or '+tlrc'
            c['outvol_ext'] = o
            c['outvol_fullext'] = o + e
            c['outvol_view'] = o[1:]
        else:
            # For NIFTI - output in orig or tlrc
            c['outvol_view'] = 'tlrc' if c['template'] else 'orig'
            c['outvol_ext'] = '+' + c['outvol_view']
            c['outvol_fullext'] = c['outvol_ext'] + '.HEAD'

    return c
예제 #3
0
def augmentconfig(c):
    """add more configuration values that are *derived* from the current configuration,
    and also checks whether some options are set properly"""

    # ensure surfdir is set properly.
    # normally it should end in 'surf'

    # try to be smart and deduce subject id from surfdir (if not set explicitly)
    surfdir = c["surfdir"]
    if surfdir:
        surfdir = os.path.abspath(surfdir)
        parent, nm = os.path.split(surfdir)

        if nm != "surf":
            jn = pathjoin(surfdir, "surf")
            if os.path.exists(jn):
                surfdir = jn
        elif nm == "SUMA":
            surfdir = parent

        if not (os.path.exists(surfdir) or os.path.split(surfdir)[1] == "surf"):
            print ('Warning: surface directory %s not does exist or does not end in "surf"' % surfdir)
            surfdir = None

        c["surfdir"] = surfdir

        # set SUMADIR as well
        c["sumadir"] = "%(surfdir)s/SUMA/" % c

    # derive subject id from surfdir

    sid = os.path.split(os.path.split(surfdir)[0])[1] if surfdir else None

    if c.get("sid") is None:
        c["sid"] = sid

    if c["sid"] is None:
        print "Warning: no subject id specified"

    c["prefix_sv2anat"] = "SurfVol2anat"

    # update steps
    if c.get("steps", "all") == "all":
        c["steps"] = "toafni+mapico+moresurfs+skullstrip+align+makespec+makespecboth+makesurfmasks"

    hasanatvol = "anatvol" in c and c["anatvol"]
    hasepivol = "epivol" in c and c["epivol"]
    hasexpvol = "expvol" in c and c["expvol"]
    hasisepi = "isepi" in c

    if hasexpvol:
        if hasanatvol or hasepivol:
            raise Exception("expvol specified, but also anatvol or epivol - illegal!")
        if not hasisepi:
            raise Exception("not specified whether expvol is EPI (yes) or anat (no)")

    else:
        if hasanatvol:
            if "epivol" in c and c["epivol"]:
                raise Exception("Cannot have both anatvol and epivol")
            else:
                c["expvol"] = c["anatvol"]
                c["isepi"] = False
                del (c["anatvol"])
        else:
            if hasepivol:
                c["expvol"] = c["epivol"]
                c["isepi"] = True
                del (c["epivol"])
            else:
                print ("Warning: no anatomical or functional experimental voume defined")

    def yesno2bool(d, k):  # dict, key
        """Assuming d[k] contains the word 'yes' or 'no', makes d[k] a boolean
        (True=='yes',False=='no'); otherwise an exception is thrown. The dict is updated"""
        val = d[k]
        if val is None:
            b = False
        elif type(val) == bool:
            b = val
        else:
            v = val.lower()
            if v == "yes":
                b = True
            elif v == "no":
                b = False
            else:
                raise Exception("Not yes or no: %s" % val)
        d[k] = b

    yesno2bool(c, "AddEdge")

    if c["identity"]:
        c["expvol_ss"] = c["anatval_ss"] = False
    else:
        yesno2bool(c, "expvol_ss")
        yesno2bool(c, "isepi")

    # see if we can get the fs_sid
    # (only if surfdir is set properly)
    # XXX not sure if this still makes sense
    c["fs_sid"] = None
    surfdir = c.get("surfdir", None)
    if surfdir is not None and os.path.exists(surfdir):
        fs_log_fn = pathjoin(surfdir, "..", "scripts", "recon-all.done")
        print "Looking in %s" % fs_log_fn
        if os.path.exists(fs_log_fn):
            with open(fs_log_fn) as f:
                lines = f.read().split("\n")
                for line in lines:
                    if line.startswith("SUBJECT"):
                        fs_sid = line[8:]
                        c["fs_sid"] = fs_sid
                        print "Found Freesurfer sid %s" % fs_sid
                        break

    if c["fs_sid"] is None:
        c["fs_sid"] = sid
        print "Unable to find proper Freesurfer sid"

    pathvars = ["anatvol", "expvol", "epivol", "refdir", "surfdir"]
    for pathvar in pathvars:
        if pathvar in c and c[pathvar]:
            c[pathvar] = os.path.abspath(c[pathvar])
            print "Set absolute path for %s: %s" % (pathvar, c[pathvar])

    if c["template"] and c["notemplate"]:
        error("Cannot have both template and notemplate")

    if "expvol" in c:
        p, n, o, e = utils.afni_fileparts(c["expvol"])

        if c.get("outvol_space", None) is None:
            cmd = "3dAttribute TEMPLATE_SPACE %s" % c["expvol"]
            outvol_space = utils.cmd_capture_output(cmd)
            outvol_space = outvol_space.split("~")[0].strip()
            if len(outvol_space) and not c["notemplate"] and outvol_space.lower() != "orig":
                print "Detected TEMPLATE_SPACE=%s" % outvol_space
                c["outvol_space"] = outvol_space
                if o == "+orig":
                    o = "+tlrc"
                    print "Template space '%s' detected: output has extension %s" % (outvol_space, o)
                c["template"] = True
            else:
                c["outvol_space"] = "+orig"

        if len(o):  #'+orig' or '+tlrc'
            c["outvol_ext"] = o
            c["outvol_fullext"] = o + e
            c["outvol_view"] = o[1:]
        else:
            # For NIFTI - output in orig or tlrc
            c["outvol_view"] = "tlrc" if c["template"] else "orig"
            c["outvol_ext"] = "+" + c["outvol_view"]
            c["outvol_fullext"] = c["outvol_ext"] + ".HEAD"

    return c