Skip to content

ENH: Add GIFTI object __repr__ methods #1092

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

Merged
merged 1 commit into from
Feb 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions nibabel/gifti/gifti.py
Original file line number Diff line number Diff line change
@@ -95,6 +95,9 @@ class GiftiLabelTable(xml.XmlSerializable):
def __init__(self):
self.labels = []

def __repr__(self):
return f"<GiftiLabelTable {self.labels!r}>"

def get_labels_as_dict(self):
self.labels_as_dict = {}
for ele in self.labels:
@@ -153,6 +156,13 @@ def __init__(self, key=0, red=None, green=None, blue=None, alpha=None):
self.blue = blue
self.alpha = alpha

def __repr__(self):
chars = 255 * np.array(
[self.red or 0, self.green or 0, self.blue or 0, self.alpha or 0]
)
r, g, b, a = chars.astype('u1')
return f'<GiftiLabel {self.key}="#{r:02x}{g:02x}{b:02x}{a:02x}">'

@deprecate_with_version(
'get_rgba method deprecated. '
"Use the rgba property instead.",
@@ -235,6 +245,11 @@ def __init__(self, dataspace=0, xformspace=0, xform=None):
else:
self.xform = xform

def __repr__(self):
src = xform_codes.label[self.dataspace]
dst = xform_codes.label[self.xformspace]
return f"<GiftiCoordSystem {src}-to-{dst}>"

def _to_xml_element(self):
coord_xform = xml.Element('CoordinateSystemTransformMatrix')
if self.xform is not None:
@@ -375,6 +390,9 @@ def __init__(self,
self.ext_offset = ext_offset
self.dims = [] if self.data is None else list(self.data.shape)

def __repr__(self):
return f"<GiftiDataArray {intent_codes.label[self.intent]}{self.dims}>"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not knowing how this would work precisely, do you maybe need a space?

Suggested change
return f"<GiftiDataArray {intent_codes.label[self.intent]}{self.dims}>"
return f"<GiftiDataArray {intent_codes.label[self.intent]} {self.dims}>"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This currently shows as: <GiftiDataArray pointset[32492, 3]>. Your change would make it <GiftiDataArray pointset [32492, 3]>. I don't have a strong opinion on the aesthetics here.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just me being nitpicky, I think. I also have no strong opinion but I know spaces are easy to miss in the f-strings when they get crowded like that.


@property
def num_dim(self):
return len(self.dims)