def process_subject(subdir, despike=False, spm=False):

    _, sid = os.path.split(subdir)
    rawdir = os.path.join(subdir, 'raw')
    rlgndir, coregdir = find_make_workdir(subdir, despike, spm)
    logger = setup_logging(coregdir, sid)
    globtype = utils.defaults['aligned'].format(sid)
    aligned = get_file(rlgndir, globtype)
    bmask = get_file(rawdir, utils.defaults['anat_glob'])
    aparc = get_file(rawdir, utils.defaults['aparc_glob'])
    # copy files to coregdir
    bmask = copy_file(bmask, coregdir)
    aparc = copy_file(aparc, coregdir)
    ## mask brainmask with aparc
    #bmask = utils.simple_mask(bmask, aparc, bmask)
    ## make mean
    mean_aligned = os.path.join(coregdir,
        '{0}_meanaligned.nii.gz'.format(sid))
    mean_aligned = utils.mean_from4d(aligned, mean_aligned)
    logger.info(mean_aligned)
    ## remove small values from mean (helps bias correction etc)
    mean_aligned = utils.simple_mask(mean_aligned,
        mean_aligned, mean_aligned, thr=100)
    logger.info('thr meanaligned by {}'.format(100))
    ## register mean to bmask
    xfm = reg.affine_register_mi(bmask, mean_aligned)
    ## invert and apply to brainmask and aparc
    if xfm is None:
        logger.error('ERROR: {0}: meanepi -> anat failed'.format(sid))
        raise IOError('{0}: meanepi -> anat failed'.format(sid))
    transform = '-i {0}'.format(xfm)
    logger.info(transform)
    rbmask = reg.apply_transform(bmask, transform, target=mean_aligned)
    raparc = reg.apply_transform(aparc, transform,
        target=mean_aligned, use_nn=True)
    ## make masks
    wm_labels = utils.defaults['wm_labels']
    vent_labels = utils.defaults['vent_labels']
    wm_maskf = os.path.join(coregdir, '{0}_WM_mask.nii.gz'.format(sid))
    wm_mask = utils.aparc_mask(raparc, wm_labels, wm_maskf)
    vent_maskf = os.path.join(coregdir, '{0}_VENT_mask.nii.gz'.format(sid))
    vent_mask = utils.aparc_mask(raparc, vent_labels, vent_maskf)
    eroded_wm = utils.erode(wm_mask)
    eroded_vent = utils.erode(vent_mask)
    logger.info(vent_mask)
    logger.info(eroded_vent)
    logger.info(wm_mask)
    logger.info(eroded_wm)
def make_realign(funcs):
    """ uses ANTS to realign files"""
    target = funcs[0]
    aligned = [target]
    xfms = []
    for moving in funcs[1:]:
        xfm = reg.affine_register_cc(target, moving)
        if xfm is None:
            raise IOError("{0} :affine register failed".format(moving))
        xfms.append(xfm)
        transformed = reg.apply_transform(moving, xfm, target=target)
        if transformed is None:
            raise IOError("{0}: apply xfm failed".format(moving))
        aligned.append(transformed)
    movement_array = transform.collate_affines(xfms)
    return aligned, movement_array