[Neuroimaging] Nibabel API change - always read as float

Satrajit Ghosh satra at mit.edu
Wed Jul 8 17:27:55 CEST 2015


hi matthew,

thanks for all the responses.

i liked nibabel's simple heuristics for reading data with scl_slope, but
thinking back over those discussions, i'm now thinking of the following
implementation level details for the load/save api for nifti:

- raise errors for non-valid Nifti's (scl_slope != 0  and scl_intercept ==
nan, scl_slope == nan), perhaps this already happens, but sounds like we
change scl_slope to 1, for scl_slope == nan

- scl_slope == 0 should always have a way of returning the default
data_type. while there is return unscaled data, it would be nice if there
was a function to return scaled or unscaled data depending on scl_slope.

----
if scl_slope == nan or (fabs(scl_slope) > 0 and scl_intercept == nan) :
    raise ValueError(' improperly defined scl_slope/intercept in nifti
header')

if scl_slope == 0:
    return unscaled_memory_mapped_data_in_native_datatype # assuming non gz

return scaled_data_as_float
----

i can write this function in my code, but it would be nice if this helper
function existed in nibabel

- saving - if we can put a dirty bit on memory mapped objects (i have a
faint recollection that this already exists), then:

----
if not dirty:
    save with original dtype
else:
    save_with_heuristic_or_user_specified_dtype # as currently done
----

a few more questions:

1. since get_data is part of a more generic api in nibabel, do we expect
every format to return data as float?

2. how would you deal with RGB data in a nifti file or the many other
intent codes that are rarely used? however, with cifti being nifti2 many of
those codes are in fact used.

cheers,

satra

On Wed, Jul 8, 2015 at 10:18 AM, Matthew Brett <matthew.brett at gmail.com>
wrote:

> On Wed, Jul 8, 2015 at 3:36 AM, Satrajit Ghosh <satra at mit.edu> wrote:
> > hi matthew,
> >
> > quick follow up question on proposed change. would saving the image back
> to
> > disk restore the original datatype? i.e. i read a uint8 image and save
> it to
> > another location, would that new file by 8 times the size of the original
> > file?
>
> If the image to be saved was created with a header, then save uses the
> defined dtype in the header.  If it was defined without a header, then
> save uses the array dtype to save the data.
>
> If you load and the save an int8 image with not-default scaling, the
> image has a defined header with dtype int8, but the in-memory dtype
> will be float, because of the scaling.   When saving, nibabel
> calculates the optimal scalefactors for the floating point array,
> where optimal means using all of the output data range from -128 to
> 127.   This might well be different from the scaling in the loaded
> image.   Nibabel does this because you might have changed the
> in-memory array so we can't depend on the previous scaling being
> valid.
>
> See you,
>
> Matthew
> _______________________________________________
> Neuroimaging mailing list
> Neuroimaging at python.org
> https://mail.python.org/mailman/listinfo/neuroimaging
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/neuroimaging/attachments/20150708/86db1c51/attachment.html>


More information about the Neuroimaging mailing list