def from_fileobj(klass, fileobj, endianness=None, check=True): if fileobj.mode == 'rb': fileobj = FileHolder(fileobj._name).get_prepare_fileobj(mode='r') header_dict = klass.file_to_dict(fileobj.fobj) endianness = klass.guessed_endian(header_dict) return klass(header_dict=header_dict, endianness=endianness)
def test_data_array_round_trip(): # Test valid XML generated from new in-memory array # See: https://github.com/nipy/nibabel/issues/469 verts = np.zeros((4, 3), np.float32) verts[0, 0] = 10.5 verts[1, 1] = 20.5 verts[2, 2] = 30.5 vertices = GiftiDataArray(verts) img = GiftiImage() img.add_gifti_data_array(vertices) bio = BytesIO() fmap = dict(image=FileHolder(fileobj=bio)) bio.write(img.to_xml()) bio.seek(0) gio = GiftiImage.from_file_map(fmap) vertices = gio.darrays[0].data assert_array_equal(vertices, verts)
def filespec_to_file_map(klass, filespec): ''' Method reimplemented from nibabel.spatialimages because it does not allow enforce_extensions in types_filenames() to be False. This is needed because MGH's compressed format is not filename.mgh.gz, but it is filename.mgz.. types_filenames() removes the .mgz and gives an error because it then doesn't find the file 'filename' in the system ''' try: filenames = types_filenames( filespec, klass.files_types, trailing_suffixes=klass._compressed_exts, enforce_extensions=False) except TypesFilenamesError: raise ImageFileError('Filespec "%s" does not look right for ' 'class %s ' % (filespec, klass)) file_map = {} for key, fname in filenames.items(): file_map[key] = FileHolder(filename=fname) return file_map
def test_gifti_round_trip(): # From section 14.4 in GIFTI Surface Data Format Version 1.0 # (with some adaptations) test_data = b'''<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE GIFTI SYSTEM "http://www.nitrc.org/frs/download.php/1594/gifti.dtd"> <GIFTI xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.nitrc.org/frs/download.php/1303/GIFTI_Caret.xsd" Version="1.0" NumberOfDataArrays="2"> <MetaData> <MD> <Name><![CDATA[date]]></Name> <Value><![CDATA[Thu Nov 15 09:05:22 2007]]></Value> </MD> </MetaData> <LabelTable/> <DataArray Intent="NIFTI_INTENT_POINTSET" DataType="NIFTI_TYPE_FLOAT32" ArrayIndexingOrder="RowMajorOrder" Dimensionality="2" Dim0="4" Dim1="3" Encoding="ASCII" Endian="LittleEndian" ExternalFileName="" ExternalFileOffset=""> <CoordinateSystemTransformMatrix> <DataSpace><![CDATA[NIFTI_XFORM_TALAIRACH]]></DataSpace> <TransformedSpace><![CDATA[NIFTI_XFORM_TALAIRACH]]></TransformedSpace> <MatrixData> 1.000000 0.000000 0.000000 0.000000 0.000000 1.000000 0.000000 0.000000 0.000000 0.000000 1.000000 0.000000 0.000000 0.000000 0.000000 1.000000 </MatrixData> </CoordinateSystemTransformMatrix> <Data> 10.5 0 0 0 20.5 0 0 0 30.5 0 0 0 </Data> </DataArray> <DataArray Intent="NIFTI_INTENT_TRIANGLE" DataType="NIFTI_TYPE_INT32" ArrayIndexingOrder="RowMajorOrder" Dimensionality="2" Dim0="4" Dim1="3" Encoding="ASCII" Endian="LittleEndian" ExternalFileName="" ExternalFileOffset=""> <Data> 0 1 2 1 2 3 0 1 3 0 2 3 </Data> </DataArray> </GIFTI>''' exp_verts = np.zeros((4, 3)) exp_verts[0, 0] = 10.5 exp_verts[1, 1] = 20.5 exp_verts[2, 2] = 30.5 exp_faces = np.asarray([[0, 1, 2], [1, 2, 3], [0, 1, 3], [0, 2, 3]], dtype=np.int32) def _check_gifti(gio): vertices = gio.get_arrays_from_intent('NIFTI_INTENT_POINTSET')[0].data faces = gio.get_arrays_from_intent('NIFTI_INTENT_TRIANGLE')[0].data assert_array_equal(vertices, exp_verts) assert_array_equal(faces, exp_faces) bio = BytesIO() fmap = dict(image=FileHolder(fileobj=bio)) bio.write(test_data) bio.seek(0) gio = GiftiImage.from_file_map(fmap) _check_gifti(gio) # Write and read again bio.seek(0) gio.to_file_map(fmap) bio.seek(0) gio2 = GiftiImage.from_file_map(fmap) _check_gifti(gio2)
def filespec_to_file_map(klass, filespec): """ Check for compressed .mgz format, then .mgh format """ if splitext(filespec)[1] == '.mgz': return dict(image=FileHolder(filename=filespec)) return super(MGHImage, klass).filespec_to_file_map(filespec)