Example #1
0
def load_fmri_surf_data(boldFiles, meshFile, roiMaskFile=None):
    """ Load FMRI BOLD surface data from files.

    Arguments:
    boldFiles --  pathes to 2D texture data files (ie multisession data).
    roiMaskFile -- a path to a 1D texture data file.
    meshFile -- a path to a mesh file

    Return:
    A tuple (graphs, bolds, sessionScans, roiMask, dataHeader)
    graphs -- a dict {roiId:roiGraph}. Each roiGraph is a list of neighbours list
    bold -- a dict {roiId:bold}. Each bold is a 2D numpy float array
            with axes [time,position]
    sessionScans -- a list of scan indexes for every session
    roiMask -- a 1D numpy int array defining ROIs (0 stands for the background)
    dataHeader -- the header from the BOLD data file
    """

    #Load ROIs:
    pyhrf.verbose(1, 'load roi mask: ' + roiMaskFile)
    #roiMask = Texture.read(roiMaskFile).data.astype(int)
    roiMask,_ = read_texture(roiMaskFile)
    pyhrf.verbose(1, 'roi mask shape: ' + str(roiMask.shape))

    #Load BOLD:
    lastScan = 0
    sessionScans = []
    bolds = []
    for boldFile in boldFiles:
        pyhrf.verbose(1, 'load bold: ' + boldFile)
        b,_ = read_texture(boldFile)
        pyhrf.verbose(1, 'bold shape: ' + str(b.shape))
        bolds.append(b)
        sessionScans.append(np.arange(lastScan, lastScan+b.shape[0], dtype=int))
        lastScan += b.shape[0]
    bold = np.concatenate(tuple(bolds))

    # discard bad data (bold with var=0 and nan values):
    discard_bad_data(bold, roiMask, time_axis=0)

    #Load mesh:
    pyhrf.verbose(1, 'load mesh: ' + meshFile)
    coords,triangles,coord_sys = read_mesh(meshFile)
    #from soma import aims
    # mesh = aims.read(meshFile)
    # triangles = [t.arraydata() for t in mesh.polygon().list()]

    pyhrf.verbose(3, 'building graph ... ')
    wholeGraph = graph_from_mesh(triangles)
    assert graph_is_sane(wholeGraph)
    pyhrf.verbose(1, 'Mesh has %d nodes' %len(wholeGraph))
    assert len(roiMask) == len(wholeGraph)

    assert bold.shape[1] == len(wholeGraph)

    pyhrf.verbose(3, 'Computing length of edges ... ')
    edges_l = np.array([np.array([distance(coords[i],coords[n],coord_sys.xform) \
                                      for n in nl]) \
                            for i,nl in enumerate(wholeGraph)], dtype=object)

    #Split bold and graph into rois:
    roiBold = {}
    graphs = {}
    edge_lengthes = {}
    for roiId in np.unique(roiMask):
        mroi = np.where(roiMask==roiId)
        g, nm = sub_graph(wholeGraph, mroi[0])
        edge_lengthes[roiId] = edges_l[mroi]
        graphs[roiId] = g
        roiBold[roiId] = bold[:, mroi[0]].astype(np.float32)

    return graphs, roiBold, sessionScans, roiMask, edge_lengthes
Example #2
0
def load_surf_bold_mask(bold_files, mesh_file, mask_file=None):
    from pyhrf.tools._io import read_mesh, read_texture, discard_bad_data

    logger.info('Load mesh: ' + mesh_file)
    coords, triangles, coord_sys = read_mesh(mesh_file)
    logger.debug('Build graph...')
    fgraph = graph_from_mesh(triangles)
    assert graph_is_sane(fgraph)

    logger.info('Mesh has %d nodes', len(fgraph))

    logger.debug('Compute length of edges ... ')
    edges_l = np.array([
        np.array([distance(coords[i], coords[n], coord_sys.xform) for n in nl])
        for i, nl in enumerate(fgraph)
    ],
                       dtype=object)

    if mask_file is None or not op.exists(mask_file):
        logger.warning('Mask file %s does not exist. Taking '
                       'all nodes ...', mask_file)
        mask = np.ones(len(fgraph))
        mask_meta_obj = None
        mask_loaded_from_file = False
    else:
        mask, mask_meta_obj = read_texture(mask_file)
        mask_loaded_from_file = True

    if not (np.round(mask) == mask).all():
        raise Exception("Mask is not n-ary")

    if len(mask) != len(fgraph):
        raise Exception('Size of mask (%d) is different from size '
                        'of graph (%d)' % (len(mask), len(fgraph)))

    mask = mask.astype(np.int32)
    if mask.min() == -1:
        mask += 1

    # Split graph into rois:
    graphs = {}
    edge_lengths = {}
    for roi_id in np.unique(mask):
        mroi = np.where(mask == roi_id)
        graph, _ = sub_graph(fgraph, mroi[0])
        edge_lengths[roi_id] = edges_l[mroi]
        graphs[roi_id] = graph

    # Load BOLD:
    last_scan = 0
    session_scans = []
    bolds = []
    for bold_file in bold_files:
        logger.info('load bold: %s', bold_file)
        bold, _ = read_texture(bold_file)
        logger.info('bold shape: %s', str(bold.shape))
        bolds.append(bold)
        session_scans.append(
            np.arange(last_scan, last_scan + bold.shape[0], dtype=int))
        last_scan += bold.shape[0]

    bold = np.concatenate(tuple(bolds))
    if len(fgraph) != bold.shape[1]:
        raise Exception('Nb positions not consistent between BOLD (%d) '
                        'and mesh (%d)' % (bold.shape[1], len(fgraph)))

    # discard bad data (bold with var=0 and nan values):
    discard_bad_data(bold, mask, time_axis=0)

    return mask, mask_meta_obj, mask_loaded_from_file, bold, session_scans, \
        graphs, edge_lengths
Example #3
0
def load_surf_bold_mask(bold_files, mesh_file, mask_file=None):
    from pyhrf.tools._io import read_mesh, read_texture, discard_bad_data

    logger.info('Load mesh: ' + mesh_file)
    coords, triangles, coord_sys = read_mesh(mesh_file)
    logger.debug('Build graph...')
    fgraph = graph_from_mesh(triangles)
    assert graph_is_sane(fgraph)

    logger.info('Mesh has %d nodes', len(fgraph))

    logger.debug('Compute length of edges ... ')
    edges_l = np.array([np.array([distance(coords[i],
                                           coords[n],
                                           coord_sys.xform)
                                  for n in nl])
                        for i, nl in enumerate(fgraph)], dtype=object)

    if mask_file is None or not op.exists(mask_file):
        logger.warning('Mask file %s does not exist. Taking '
                       'all nodes ...', mask_file)
        mask = np.ones(len(fgraph))
        mask_meta_obj = None
        mask_loaded_from_file = False
    else:
        mask, mask_meta_obj = read_texture(mask_file)
        mask_loaded_from_file = True

    if not (np.round(mask) == mask).all():
        raise Exception("Mask is not n-ary")

    if len(mask) != len(fgraph):
        raise Exception('Size of mask (%d) is different from size '
                        'of graph (%d)' % (len(mask), len(fgraph)))

    mask = mask.astype(np.int32)
    if mask.min() == -1:
        mask += 1

    # Split graph into rois:
    graphs = {}
    edge_lengths = {}
    for roi_id in np.unique(mask):
        mroi = np.where(mask == roi_id)
        graph, _ = sub_graph(fgraph, mroi[0])
        edge_lengths[roi_id] = edges_l[mroi]
        graphs[roi_id] = graph

    # Load BOLD:
    last_scan = 0
    session_scans = []
    bolds = []
    for bold_file in bold_files:
        logger.info('load bold: %s', bold_file)
        bold, _ = read_texture(bold_file)
        logger.info('bold shape: %s', str(bold.shape))
        bolds.append(bold)
        session_scans.append(np.arange(last_scan, last_scan + bold.shape[0],
                                       dtype=int))
        last_scan += bold.shape[0]

    bold = np.concatenate(tuple(bolds))
    if len(fgraph) != bold.shape[1]:
        raise Exception('Nb positions not consistent between BOLD (%d) '
                        'and mesh (%d)' % (bold.shape[1], len(fgraph)))

    # discard bad data (bold with var=0 and nan values):
    discard_bad_data(bold, mask, time_axis=0)

    return mask, mask_meta_obj, mask_loaded_from_file, bold, session_scans, \
        graphs, edge_lengths
Example #4
0
def load_fmri_surf_data(boldFiles, meshFile, roiMaskFile=None):
    """ Load FMRI BOLD surface data from files.

    Arguments:
    boldFiles --  pathes to 2D texture data files (ie multisession data).
    roiMaskFile -- a path to a 1D texture data file.
    meshFile -- a path to a mesh file

    Return:
    A tuple (graphs, bolds, sessionScans, roiMask, dataHeader)
    graphs -- a dict {roiId:roiGraph}. Each roiGraph is a list of neighbours list
    bold -- a dict {roiId:bold}. Each bold is a 2D numpy float array
            with axes [time,position]
    sessionScans -- a list of scan indexes for every session
    roiMask -- a 1D numpy int array defining ROIs (0 stands for the background)
    dataHeader -- the header from the BOLD data file
    """
    from pyhrf.graph import graph_from_mesh, sub_graph, graph_is_sane

    # Load ROIs:
    logger.info('load roi mask: %s', roiMaskFile)
    #roiMask = Texture.read(roiMaskFile).data.astype(int)
    roiMask, _ = read_texture(roiMaskFile)
    logger.info('roi mask shape: %s', str(roiMask.shape))

    # Load BOLD:
    lastScan = 0
    sessionScans = []
    bolds = []
    for boldFile in boldFiles:
        logger.info('load bold: %s', boldFile)
        b, _ = read_texture(boldFile)
        logger.info('bold shape: %s', str(b.shape))
        bolds.append(b)
        sessionScans.append(
            np.arange(lastScan, lastScan + b.shape[0], dtype=int))
        lastScan += b.shape[0]
    bold = np.concatenate(tuple(bolds))

    # discard bad data (bold with var=0 and nan values):
    discard_bad_data(bold, roiMask, time_axis=0)

    # Load mesh:
    logger.info('load mesh: %s', meshFile)
    coords, triangles, coord_sys = read_mesh(meshFile)
    #from soma import aims
    # mesh = aims.read(meshFile)
    # triangles = [t.arraydata() for t in mesh.polygon().list()]

    logger.info('building graph ... ')
    wholeGraph = graph_from_mesh(triangles)
    assert graph_is_sane(wholeGraph)
    logger.info('Mesh has %d nodes', len(wholeGraph))
    assert len(roiMask) == len(wholeGraph)

    assert bold.shape[1] == len(wholeGraph)

    logger.info('Computing length of edges ... ')
    edges_l = np.array([np.array([distance(coords[i], coords[n], coord_sys.xform)
                                  for n in nl])
                        for i, nl in enumerate(wholeGraph)], dtype=object)

    # Split bold and graph into rois:
    roiBold = {}
    graphs = {}
    edge_lengthes = {}
    for roiId in np.unique(roiMask):
        mroi = np.where(roiMask == roiId)
        g, nm = sub_graph(wholeGraph, mroi[0])
        edge_lengthes[roiId] = edges_l[mroi]
        graphs[roiId] = g
        roiBold[roiId] = bold[:, mroi[0]].astype(np.float32)

    return graphs, roiBold, sessionScans, roiMask, edge_lengthes