Example #1
0
def make_parcellation_from_files(betaFiles,
                                 maskFile,
                                 outFile,
                                 nparcels,
                                 method,
                                 dry=False,
                                 spatial_weight=10.):

    if not op.exists(maskFile):
        print 'Error, file %s not found' % maskFile
        return

    betaFiles = sorted(betaFiles)
    for b in betaFiles:
        if not op.exists(b):
            raise Exception('Error, file %s not found' % b)

    logger.info('Mask image: %s', op.basename(maskFile))
    logger.info('Betas: %s ... %s', op.basename(betaFiles[0]),
                op.basename(betaFiles[-1]))
    logger.info("Method: %s, nb parcels: %d", method, nparcels)
    logger.info('Spatial weight: %f', spatial_weight)

    if not dry:
        logger.info('Running parcellation ... ')
        logger.info('Start date is: %s', strftime('%c', localtime()))
        t0 = time()
        v = logger.getEffectiveLevel() <= logging.INFO
        lpa = fixed_parcellation(maskFile,
                                 betaFiles,
                                 nparcels,
                                 nn=6,
                                 method=method,
                                 fullpath=outFile,
                                 verbose=v,
                                 mu=spatial_weight)

        from pyhrf.ndarray import xndarray
        c = xndarray.load(outFile)
        if c.min() == -1:
            c.data += 1

        for i in np.unique(c.data):
            # remove parcel with size < 2:
            if i != 0 and len(c.data == 1) < 2:
                c.data[np.where(c.data == i)] = 0

        c.save(outFile)

        logger.info('Parcellation complete, took %s',
                    format_duration(time() - t0))
        return lpa
    else:
        logger.info('Dry run.')
Example #2
0
def make_parcellation_from_files(betaFiles, maskFile, outFile, nparcels,
                                 method, dry=False, spatial_weight=10.):

    if not op.exists(maskFile):
        print 'Error, file %s not found' %maskFile
        return

    betaFiles = sorted(betaFiles)
    for b in betaFiles:
        if not op.exists(b):
            print 'Error, file %s not found' %b
            return

    pyhrf.verbose(1, 'Mask image: ' + op.basename(maskFile))
    pyhrf.verbose(1, 'Betas: ' + op.basename(betaFiles[0]) + ' ... ' + \
                      op.basename(betaFiles[-1]))
    pyhrf.verbose(1, "Method: %s, nb parcels: %d" %(method, nparcels))
    pyhrf.verbose(1, 'Spatial weight: %f' %spatial_weight)

    if not dry:
        pyhrf.verbose(1, 'Running parcellation ... ')
        pyhrf.verbose(1, 'Start date is: %s' %strftime('%c',localtime()))
        t0 = time()
        #lpa = one_subj_parcellation(maskFile, betaFiles, nparcels, 6,
        #                            method, 10, 1, fullpath=outFile)
        v = pyhrf.verbose.verbosity
        lpa = fixed_parcellation(maskFile, betaFiles, nparcels, nn=6,
                                 method=method, fullpath=outFile, verbose=v,
                                 mu=spatial_weight)

        from pyhrf.ndarray import xndarray
        c = xndarray.load(outFile)
        if c.min() == -1:
            c.data += 1

        for i in np.unique(c.data):
            # remove parcel with size < 2:
            if i!=0 and len(c.data==1) < 2:
                c.data[np.where(c.data==i)] = 0

        c.save(outFile)

        pyhrf.verbose(1, 'Parcellation complete, took %s' \
                          %format_duration(time() - t0))
        return lpa
    else:
        pyhrf.verbose(1, 'Dry run.')
Example #3
0
def make_parcellation_from_files(betaFiles, maskFile, outFile, nparcels,
                                 method, dry=False, spatial_weight=10.):

    if not op.exists(maskFile):
        print 'Error, file %s not found' % maskFile
        return

    betaFiles = sorted(betaFiles)
    for b in betaFiles:
        if not op.exists(b):
            raise Exception('Error, file %s not found' % b)

    logger.info('Mask image: %s', op.basename(maskFile))
    logger.info('Betas: %s ... %s', op.basename(betaFiles[0]),
                op.basename(betaFiles[-1]))
    logger.info("Method: %s, nb parcels: %d", method, nparcels)
    logger.info('Spatial weight: %f', spatial_weight)

    if not dry:
        logger.info('Running parcellation ... ')
        logger.info('Start date is: %s', strftime('%c', localtime()))
        t0 = time()
        v = logger.getEffectiveLevel() <= logging.INFO
        lpa = fixed_parcellation(maskFile, betaFiles, nparcels, nn=6,
                                 method=method, fullpath=outFile, verbose=v,
                                 mu=spatial_weight)

        from pyhrf.ndarray import xndarray
        c = xndarray.load(outFile)
        if c.min() == -1:
            c.data += 1

        for i in np.unique(c.data):
            # remove parcel with size < 2:
            if i != 0 and len(c.data == 1) < 2:
                c.data[np.where(c.data == i)] = 0

        c.save(outFile)

        logger.info(
            'Parcellation complete, took %s', format_duration(time() - t0))
        return lpa
    else:
        logger.info('Dry run.')
Example #4
0
from nipy.labs.spatial_models.parcel_io import fixed_parcellation

# ------------------------------------
# Get the data (mask+functional image)
# take several experimental conditions
# time courses could be used instead

n_beta = [29]
data_dir = op.expanduser(op.join('~', '.nipy', 'tests', 'data'))
mask_image = op.join(data_dir, 'mask.nii.gz')
betas = [op.join(data_dir, 'spmT_%04d.nii.gz' % n) for n in n_beta]
missing_file = array(
    [op.exists(m) == False for m in [mask_image] + betas]).any()
if missing_file:
    import get_data_light
    get_data_light.get_it()

# set the parameters
n_parcels = 500
mu = 10
nn = 6
write_dir = tempfile.mkdtemp()
verbose = 1

lpa = fixed_parcellation(mask_image, betas, n_parcels, nn, 'gkm', 
                            write_dir, mu, verbose)
lpa = fixed_parcellation(mask_image, betas, n_parcels, nn, 'ward', 
                            write_dir, mu, verbose)
lpa = fixed_parcellation(mask_image, betas, n_parcels, nn, 'ward_and_gkm', 
                            write_dir, mu, verbose)
Example #5
0
from get_data_light import DATA_DIR, get_second_level_dataset

# ------------------------------------
# Get the data (mask+functional image)
# take several experimental conditions
# time courses could be used instead

n_beta = [29]
mask_image = path.join(DATA_DIR, 'mask.nii.gz')
betas = [path.join(DATA_DIR, 'spmT_%04d.nii.gz' % n) for n in n_beta]
missing_file = array([not path.exists(m) for m in [mask_image] + betas]).any()
if missing_file:
    get_second_level_dataset()

# set the parameters
n_parcels = 500
mu = 10
nn = 6
verbose = 1
# write directory
write_dir = path.join(getcwd(), 'results')
if not path.exists(write_dir):
    mkdir(write_dir)

lpa = fixed_parcellation(mask_image, betas, n_parcels, nn, 'gkm', write_dir,
                         mu, verbose)
lpa = fixed_parcellation(mask_image, betas, n_parcels, nn, 'ward', write_dir,
                         mu, verbose)
lpa = fixed_parcellation(mask_image, betas, n_parcels, nn, 'ward_and_gkm',
                         write_dir, mu, verbose)