Exemplo n.º 1
0
def ply2gii(in_file, metadata, out_file=None):
    """Convert from ply to GIfTI"""
    from pathlib import Path
    from numpy import eye
    from nibabel.gifti import (
        GiftiMetaData,
        GiftiCoordSystem,
        GiftiImage,
        GiftiDataArray,
    )
    from pyntcloud import PyntCloud

    in_file = Path(in_file)
    surf = PyntCloud.from_file(str(in_file))

    # Update centroid metadata
    metadata.update(
        zip(
            ("SurfaceCenterX", "SurfaceCenterY", "SurfaceCenterZ"),
            ["%.4f" % c for c in surf.centroid],
        ))

    # Prepare data arrays
    da = (
        GiftiDataArray(
            data=surf.xyz.astype("float32"),
            datatype="NIFTI_TYPE_FLOAT32",
            intent="NIFTI_INTENT_POINTSET",
            meta=GiftiMetaData.from_dict(metadata),
            coordsys=GiftiCoordSystem(xform=eye(4), xformspace=3),
        ),
        GiftiDataArray(
            data=surf.mesh.values,
            datatype="NIFTI_TYPE_INT32",
            intent="NIFTI_INTENT_TRIANGLE",
            coordsys=None,
        ),
    )
    surfgii = GiftiImage(darrays=da)

    if out_file is None:
        out_file = fname_presuffix(in_file.name,
                                   suffix=".gii",
                                   use_ext=False,
                                   newpath=str(Path.cwd()))

    surfgii.to_filename(str(out_file))
    return out_file
Exemplo n.º 2
0
def ply2gii(in_file, metadata, out_file=None):
    """Convert from ply to GIfTI"""
    from pathlib import Path
    from numpy import eye
    from nibabel.gifti import (
        GiftiMetaData, GiftiCoordSystem, GiftiImage, GiftiDataArray,
    )
    from pyntcloud import PyntCloud

    in_file = Path(in_file)
    surf = PyntCloud.from_file(str(in_file))

    # Update centroid metadata
    metadata.update(
        zip(('SurfaceCenterX', 'SurfaceCenterY', 'SurfaceCenterZ'),
            ['%.4f' % c for c in surf.centroid])
    )

    # Prepare data arrays
    da = (
        GiftiDataArray(
            data=surf.xyz.astype('float32'),
            datatype='NIFTI_TYPE_FLOAT32',
            intent='NIFTI_INTENT_POINTSET',
            meta=GiftiMetaData.from_dict(metadata),
            coordsys=GiftiCoordSystem(xform=eye(4), xformspace=3)),
        GiftiDataArray(
            data=surf.mesh.values,
            datatype='NIFTI_TYPE_INT32',
            intent='NIFTI_INTENT_TRIANGLE',
            coordsys=None))
    surfgii = GiftiImage(darrays=da)

    if out_file is None:
        out_file = fname_presuffix(
            in_file.name, suffix='.gii', use_ext=False, newpath=str(Path.cwd()))

    surfgii.to_filename(str(out_file))
    return out_file
    if do_surface:
        for hemi in ['lh', 'rh']:
            z_maps = [
                pjoin(work_dir, acq, 'z_surf',
                      'effects_interest_%s.gii' % hemi) for acq in acqs
            ]

            mean_z = np.mean([
                np.ravel([darrays.data for darrays in load(z_map).darrays])
                for z_map in z_maps
            ], 0)
            n_maps = len(z_maps)
            fixed_effects = mean_z * np.sqrt(n_maps)
            gii = GiftiImage(
                darrays=[GiftiDataArray().from_array(fixed_effects, 't test')])
            gii.to_filename(pjoin(write_dir, 'retinotopicity_%s.gii' % hemi))
            fixed_effects[np.isnan(fixed_effects)] = 0
            bh = fdr_threshold(fixed_effects, alpha)
            print(bh)
            mask = fixed_effects > bh

            # todo: plot on a surface
            """
            output_file = pjoin(write_dir, 'retinotopicity_%s.png' % hemi)
            if hemi == 'lh':
                plot_surf_stat_map(
                    lh_inflated, fixed_effects, bg_map=sulc_left, output_file=output_file,
                    hemi='left', view='medial', bg_on_data=True, darkness=1, alpha=1,
                    threshold=THRESHOLD)
            else:
                plot_surf_stat_map(