Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why not implementing __repr__ and __str__ for Nifti1Image? #1057

Open
dangom opened this issue Sep 17, 2021 · 3 comments
Open

Why not implementing __repr__ and __str__ for Nifti1Image? #1057

dangom opened this issue Sep 17, 2021 · 3 comments

Comments

@dangom
Copy link

dangom commented Sep 17, 2021

Hi guys,

I was wondering whether there is a reason why the Nifti1Image class doesn't implement repr nor str. I think it'd be a great addition, and very informative for end users to be able to get simple info on the REPL instead of the default <nibabel.nifti1.Nifti1Image at 0x17d895ab0>. But maybe there is a motivation behind that?

Perhaps topic for another issue, but it'd be lovely if that simple info printed on the str would be implemented as properties. So say we print dtype, voxel size and TR, then calling nifti_image.TR would also print the TR (and similarly nifti_image.TR = 3. would change it by adapting the header).

what do you think?!

Example:

import nibabel

def example_str(self):
    dimension = "4D" if len(self.dataobj.shape) == 4 else "3D"

    info = f"""
    Nifti1Image (type: {self.dataobj.dtype})

    Dimension: {dimension}
    Shape: {self.dataobj.shape}
    Voxel Dimensions: {self.header["pixdim"][1:4]}
    Repetition Time: {self.header["pixdim"][4]:.3f}
    """
    return info


nibabel.nifti1.Nifti1Image.__str__ = example_str

Already improves things a lot:

Screen Shot 2021-09-17 at 17 20 50

@effigies
Copy link
Member

__str__ is implemented in SpatialImage:

>>> print(nb.Nifti1Image(np.zeros((5,5,5)), np.eye(4)))
<class 'nibabel.nifti1.Nifti1Image'>
data shape (5, 5, 5)
affine: 
[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]
metadata:
<class 'nibabel.nifti1.Nifti1Header'> object, endian='<'
sizeof_hdr      : 348
data_type       : b''
db_name         : b''
extents         : 0
session_error   : 0
regular         : b''
dim_info        : 0
dim             : [3 5 5 5 1 1 1 1]
intent_p1       : 0.0
intent_p2       : 0.0
intent_p3       : 0.0
intent_code     : none
datatype        : float64
bitpix          : 64
slice_start     : 0
pixdim          : [1. 1. 1. 1. 1. 1. 1. 1.]
vox_offset      : 0.0
scl_slope       : nan
scl_inter       : nan
slice_end       : 0
slice_code      : unknown
xyzt_units      : 0
cal_max         : 0.0
cal_min         : 0.0
slice_duration  : 0.0
toffset         : 0.0
glmax           : 0
glmin           : 0
descrip         : b''
aux_file        : b''
qform_code      : unknown
sform_code      : aligned
quatern_b       : 0.0
quatern_c       : 0.0
quatern_d       : 0.0
qoffset_x       : 0.0
qoffset_y       : 0.0
qoffset_z       : 0.0
srow_x          : [1. 0. 0. 0.]
srow_y          : [0. 1. 0. 0.]
srow_z          : [0. 0. 1. 0.]
intent_name     : b''
magic           : b'n+1'

I would be open to improving the __repr__, though I would expect it to (usually) be <80 characters to keep the REPL usable.

@dangom
Copy link
Author

dangom commented Sep 17, 2021

amazing!! I must've overlooked the str implementation. That's pretty good, thank you. Do you think it'd be possible to add some provenance info to it, such as whether the nifti was loaded directly from a file, and if so print its filename?

If <80 characters then it'd be hard to have it output something that could be used to instantiate the object upon eval. But we could take inspiration from ANTsPy:

Screen Shot 2021-09-17 at 18 01 52

@effigies
Copy link
Member

Yeah, I wouldn't expect a repr to be eval-able. But we could report filenames. nibabel.filebasedimages.FileBasedImage has a get_filename() method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants