Array writer objects
Array writers have init signature:
def __init__(self, array, out_dtype=None)
and methods
They must have attributes / properties of:
They may have attributes:
They are designed to write arrays to a fileobj with reasonable memory efficiency.
Array writers may be able to scale the array or apply an intercept, or do something else to make sense of conversions between float and int, or between larger ints and smaller.
ArrayWriter(array[, out_dtype]) | Initialize array writer |
ScalingError | |
SlopeArrayWriter(array[, out_dtype, ...]) | ArrayWriter that can use scalefactor for writing arrays |
SlopeInterArrayWriter(array[, out_dtype, ...]) | Array writer that can use slope and intercept to scale array |
WriterError | |
get_slope_inter(writer) | Return slope, intercept from array writer object |
make_array_writer(data, out_type[, ...]) | Make array writer instance for array data and output type out_type |
Bases: object
Initialize array writer
Parameters: | array : array-like
out_dtype : None or dtype
**kwargs : keyword arguments
|
---|
Examples
>>> arr = np.array([0, 255], np.uint8)
>>> aw = ArrayWriter(arr)
>>> aw = ArrayWriter(arr, np.int8)
Traceback (most recent call last):
...
WriterError: Scaling needed but cannot scale
>>> aw = ArrayWriter(arr, np.int8, check_scaling=False)
Traceback (most recent call last):
...
WriterError: Scaling needed but cannot scale
Initialize array writer
Parameters: | array : array-like
out_dtype : None or dtype
**kwargs : keyword arguments
|
---|
Examples
>>> arr = np.array([0, 255], np.uint8)
>>> aw = ArrayWriter(arr)
>>> aw = ArrayWriter(arr, np.int8)
Traceback (most recent call last):
...
WriterError: Scaling needed but cannot scale
>>> aw = ArrayWriter(arr, np.int8, check_scaling=False)
Traceback (most recent call last):
...
WriterError: Scaling needed but cannot scale
Return array from arraywriter
Return (maybe cached) finite range of data array
True if array has NaNs
Return out_dtype from arraywriter
Checks if scaling is needed for input array
Raises WriterError if no scaling possible.
The rules are in the code, but:
Write array into fileobj
Parameters: | fileobj : file-like object order : {‘F’, ‘C’}
nan2zero : {None, True, False}, optional, deprecated
|
---|
Bases: nibabel.arraywriters.WriterError
x.__init__(...) initializes x; see help(type(x)) for signature
Bases: nibabel.arraywriters.ArrayWriter
ArrayWriter that can use scalefactor for writing arrays
The scalefactor allows the array writer to write floats to int output types, and rescale larger ints to smaller. It can therefore lose precision.
It extends the ArrayWriter class with attribute:
and methods:
Initialize array writer
Parameters: | array : array-like
out_dtype : None or dtype
calc_scale : {True, False}, optional
scaler_dtype : dtype-like, optional
**kwargs : keyword arguments
|
---|
Examples
>>> arr = np.array([0, 254], np.uint8)
>>> aw = SlopeArrayWriter(arr)
>>> aw.slope
1.0
>>> aw = SlopeArrayWriter(arr, np.int8)
>>> aw.slope
2.0
>>> aw = SlopeArrayWriter(arr, np.int8, calc_scale=False)
>>> aw.slope
1.0
>>> aw.calc_scale()
>>> aw.slope
2.0
Initialize array writer
Parameters: | array : array-like
out_dtype : None or dtype
calc_scale : {True, False}, optional
scaler_dtype : dtype-like, optional
**kwargs : keyword arguments
|
---|
Examples
>>> arr = np.array([0, 254], np.uint8)
>>> aw = SlopeArrayWriter(arr)
>>> aw.slope
1.0
>>> aw = SlopeArrayWriter(arr, np.int8)
>>> aw.slope
2.0
>>> aw = SlopeArrayWriter(arr, np.int8, calc_scale=False)
>>> aw.slope
1.0
>>> aw.calc_scale()
>>> aw.slope
2.0
Calculate / set scaling for floats/(u)ints to (u)ints
Set object to values before any scaling calculation
Checks if scaling is needed for input array
Raises WriterError if no scaling possible.
The rules are in the code, but:
get/set slope
Write array into fileobj
Parameters: | fileobj : file-like object order : {‘F’, ‘C’}
nan2zero : {None, True, False}, optional, deprecated
|
---|
Bases: nibabel.arraywriters.SlopeArrayWriter
Array writer that can use slope and intercept to scale array
The writer can subtract an intercept, and divided by a slope, in order to be able to convert floating point values into a (u)int range, or to convert larger (u)ints to smaller.
It extends the ArrayWriter class with attributes:
and methods:
Initialize array writer
Parameters: | array : array-like
out_dtype : None or dtype
calc_scale : {True, False}, optional
scaler_dtype : dtype-like, optional
**kwargs : keyword arguments
|
---|
Examples
>>> arr = np.array([0, 255], np.uint8)
>>> aw = SlopeInterArrayWriter(arr)
>>> aw.slope, aw.inter
(1.0, 0.0)
>>> aw = SlopeInterArrayWriter(arr, np.int8)
>>> (aw.slope, aw.inter) == (1.0, 128)
True
>>> aw = SlopeInterArrayWriter(arr, np.int8, calc_scale=False)
>>> aw.slope, aw.inter
(1.0, 0.0)
>>> aw.calc_scale()
>>> (aw.slope, aw.inter) == (1.0, 128)
True
Initialize array writer
Parameters: | array : array-like
out_dtype : None or dtype
calc_scale : {True, False}, optional
scaler_dtype : dtype-like, optional
**kwargs : keyword arguments
|
---|
Examples
>>> arr = np.array([0, 255], np.uint8)
>>> aw = SlopeInterArrayWriter(arr)
>>> aw.slope, aw.inter
(1.0, 0.0)
>>> aw = SlopeInterArrayWriter(arr, np.int8)
>>> (aw.slope, aw.inter) == (1.0, 128)
True
>>> aw = SlopeInterArrayWriter(arr, np.int8, calc_scale=False)
>>> aw.slope, aw.inter
(1.0, 0.0)
>>> aw.calc_scale()
>>> (aw.slope, aw.inter) == (1.0, 128)
True
get/set inter
Set object to values before any scaling calculation
Write array into fileobj
Parameters: | fileobj : file-like object order : {‘F’, ‘C’}
nan2zero : {None, True, False}, optional, deprecated
|
---|
Return slope, intercept from array writer object
Parameters: | writer : ArrayWriter instance |
---|---|
Returns: | slope : scalar
inter : scalar
|
Examples
>>> arr = np.arange(10)
>>> get_slope_inter(ArrayWriter(arr))
(1.0, 0.0)
>>> get_slope_inter(SlopeArrayWriter(arr))
(1.0, 0.0)
>>> get_slope_inter(SlopeInterArrayWriter(arr))
(1.0, 0.0)
Make array writer instance for array data and output type out_type
Parameters: | data : array-like
out_type : dtype-like
has_slope : {True, False}
has_intercept : {True, False}
**kwargs : other keyword arguments
|
---|---|
Returns: | writer : arraywriter instance
|
Examples
>>> aw = make_array_writer(np.arange(10), np.uint8, True, True)
>>> type(aw) == SlopeInterArrayWriter
True
>>> aw = make_array_writer(np.arange(10), np.uint8, True, False)
>>> type(aw) == SlopeArrayWriter
True
>>> aw = make_array_writer(np.arange(10), np.uint8, False, False)
>>> type(aw) == ArrayWriter
True