def test_gifti_coord(): from ..gifti import GiftiCoordSystem gcs = GiftiCoordSystem() assert_true(gcs.xform is not None) # Smoke test gcs.xform = None gcs.print_summary() gcs.to_xml()
def test_dataarray_init(): # Test non-default dataarray initialization gda = GiftiDataArray # shortcut assert_equal(gda(None).data, None) arr = np.arange(12, dtype=np.float32).reshape((3, 4)) assert_array_equal(gda(arr).data, arr) # Intents assert_raises(KeyError, gda, intent=1) # Invalid code assert_raises(KeyError, gda, intent='not an intent') # Invalid string assert_equal(gda(intent=2).intent, 2) assert_equal(gda(intent='correlation').intent, 2) assert_equal(gda(intent='NIFTI_INTENT_CORREL').intent, 2) # Datatype assert_equal(gda(datatype=2).datatype, 2) assert_equal(gda(datatype='uint8').datatype, 2) assert_raises(KeyError, gda, datatype='not_datatype') # Float32 datatype comes from array if datatype not set assert_equal(gda(arr).datatype, 16) # Can be overriden by init assert_equal(gda(arr, datatype='uint8').datatype, 2) # Encoding assert_equal(gda(encoding=1).encoding, 1) assert_equal(gda(encoding='ASCII').encoding, 1) assert_equal(gda(encoding='GIFTI_ENCODING_ASCII').encoding, 1) assert_raises(KeyError, gda, encoding='not an encoding') # Endian assert_equal(gda(endian=1).endian, 1) assert_equal(gda(endian='big').endian, 1) assert_equal(gda(endian='GIFTI_ENDIAN_BIG').endian, 1) assert_raises(KeyError, gda, endian='not endian code') # CoordSys aff = np.diag([2, 3, 4, 1]) cs = GiftiCoordSystem(1, 2, aff) da = gda(coordsys=cs) assert_equal(da.coordsys.dataspace, 1) assert_equal(da.coordsys.xformspace, 2) assert_array_equal(da.coordsys.xform, aff) # Ordering assert_equal(gda(ordering=2).ind_ord, 2) assert_equal(gda(ordering='F').ind_ord, 2) assert_equal(gda(ordering='ColumnMajorOrder').ind_ord, 2) assert_raises(KeyError, gda, ordering='not an ordering') # metadata meta_dict = dict(one=1, two=2) assert_equal( gda(meta=GiftiMetaData.from_dict(meta_dict)).meta.metadata, meta_dict) assert_equal(gda(meta=meta_dict).meta.metadata, meta_dict) assert_equal(gda(meta=None).meta.metadata, {}) # ext_fname and ext_offset assert_equal(gda(ext_fname='foo').ext_fname, 'foo') assert_equal(gda(ext_offset=12).ext_offset, 12)
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