Ufunc-like functions operating on Analyze headers
Calculate scaling for data, set into header, return scaling
Check that the data can be sensibly adapted to this header data dtype. If the header type does support useful scaling to allow this, raise a HeaderTypeError.
Parameters : | hdr : header
data : array-like
|
---|---|
Returns : | divslope : None or scalar
intercept : None or scalar
mn : None or scalar
mx : None or scalar
|
Examples
>>> from nipy.io.imageformats.analyze import AnalyzeHeader
>>> hdr = AnalyzeHeader()
>>> hdr.set_data_dtype(np.float32)
>>> data = np.arange(6, dtype=np.float32).reshape(1,2,3)
>>> adapt_header(hdr, data)
(1.0, 0.0, None, None)
>>> hdr.set_data_dtype(np.int16)
>>> adapt_header(hdr, data) # The Analyze header cannot scale
Traceback (most recent call last):
...
HeaderTypeError: Cannot cast data to header dtype without large potential loss in precision
Read data from fileobj given hdr
Parameters : | hdr : header
fileobj : file-like
|
---|---|
Returns : | arr : array-like
|
Read raw (unscaled) data from fileobj
Parameters : | hdr : header
fileobj : file-like
|
---|---|
Returns : | arr : array-like
|
Write data to fileobj coercing to header dtype
Parameters : | hdr : header
data : array-like
fileobj : file-like object
intercept : scalar, optional
divslope : None or scalar, optional
mn : scalar, optional
mx : scalar, optional
|
---|
Examples
>>> from nipy.io.imageformats.analyze import AnalyzeHeader
>>> hdr = AnalyzeHeader()
>>> hdr.set_data_shape((1, 2, 3))
>>> hdr.set_data_dtype(np.float64)
>>> from StringIO import StringIO
>>> str_io = StringIO()
>>> data = np.arange(6).reshape(1,2,3)
>>> write_data(hdr, data, str_io)
>>> data.astype(np.float64).tostring('F') == str_io.getvalue()
True
We check the data shape
>>> write_data(hdr, data.reshape(3,2,1), str_io)
Traceback (most recent call last):
...
HeaderDataError: Data should be shape (1, 2, 3)
Write data to fileobj with best data match to hdr dtype
This is a convenience function that modifies the header as well as writing the data to file. Because it modifies the header, it is not very useful for general image writing, where you often need to first write the header, then the image.
Parameters : | data : array-like
fileobj : file-like object
|
---|---|
Returns : | None : |
Examples
>>> from nipy.io.imageformats.analyze import AnalyzeHeader
>>> hdr = AnalyzeHeader()
>>> hdr.set_data_shape((1, 2, 3))
>>> hdr.set_data_dtype(np.float64)
>>> from StringIO import StringIO
>>> str_io = StringIO()
>>> data = np.arange(6).reshape(1,2,3)
>>> write_scaled_data(hdr, data, str_io)
>>> data.astype(np.float64).tostring('F') == str_io.getvalue()
True