add size getters to the AICSImage class#38
Conversation
Codecov Report
@@ Coverage Diff @@
## master #38 +/- ##
=========================================
+ Coverage 87.61% 87.8% +0.18%
=========================================
Files 27 27
Lines 1066 1099 +33
=========================================
+ Hits 934 965 +31
- Misses 132 134 +2
Continue to review full report at Codecov.
|
| return_dims=self.dims) | ||
| return self._data | ||
|
|
||
| def size(self, dims: str = "STCZYX"): |
There was a problem hiding this comment.
This is a really smart implementation :)
| # Image dimension sizes can be obtained via properties: | ||
| im.size_z # returns the size of the Z dimension. X,Y,Z,C,T, and S supported. | ||
|
|
||
| # Image dimensions can also be obtained as a tuple in two ways: | ||
| im.size("ZYX") # returns a tuple containing the Z, Y, and X sizes only | ||
| im.get_image_data(out_orientation="ZYX").shape # returns same as above | ||
|
|
||
| # Image metadata is stored in `metadata` attribute | ||
| im.metadata # returns whichever metadata parser best suites the file format | ||
| im.metadata # returns whichever metadata parser best suits the file format | ||
|
|
||
| # Subsets or transposes of the image data can be requested: | ||
| im.get_image_data(out_orientation="ZYX") # returns a 3d data block containing only the ZYX dimensions |
There was a problem hiding this comment.
Thanks for adding this to the README!
heeler
left a comment
There was a problem hiding this comment.
Nice work.
I'm thinking now that my comment might be better for readers like pylibczi as opposed to in the aicsimage parent class. I forgot that we're locked to 6 dims when I wrote the comment.
| @property | ||
| def size_x(self): | ||
| """ | ||
| Returns | ||
| ------- | ||
| Returns the x size | ||
| """ | ||
| return self.size("X")[0] | ||
|
|
||
| @property | ||
| def size_y(self): | ||
| """ | ||
| Returns | ||
| ------- | ||
| Returns the y size | ||
| """ | ||
| return self.size("Y")[0] | ||
|
|
||
| @property | ||
| def size_z(self): | ||
| """ | ||
| Returns | ||
| ------- | ||
| Returns the z size | ||
| """ | ||
| return self.size("Z")[0] | ||
|
|
||
| @property | ||
| def size_c(self): | ||
| """ | ||
| Returns | ||
| ------- | ||
| Returns the c size | ||
| """ | ||
| return self.size("C")[0] | ||
|
|
||
| @property | ||
| def size_t(self): | ||
| """ | ||
| Returns | ||
| ------- | ||
| Returns the t size | ||
| """ | ||
| return self.size("T")[0] | ||
|
|
||
| @property | ||
| def size_s(self): | ||
| """ | ||
| Returns | ||
| ------- | ||
| Returns the s size | ||
| """ | ||
| return self.size("S")[0] |
There was a problem hiding this comment.
I like the functionality but might I suggest
def __init__(self):
self.pattern = re.compile(r'size_([A-z])$')
def __getattr__(self, attr_name):
match = re.match(self.pattern, attr_name)
if match:
return self.size(match.group(1))[0]
else:
raise AttributeError(f'No such attribute: {attr_name}')
def size(self, dims: str = "STCZYX"):
upper_dims = dims.upper()
if len(dims) != len(set(dims).intersection(set(self.dims))):
print("Dimension not present in data!")
return tuple([0])
return tuple([self.data.shape[self.dims.index(dim)] for dim in upper_dims])There was a problem hiding this comment.
This kind of thing is popular in ruby but I'd not sure how it's thought of in python
There was a problem hiding this comment.
I will definitely take your cue about making sure to convert to upper, and assert that the requested dims are valid. But I'm not sure I understand the ruby comment. What is the thing that's popular in ruby?
* add size getters to the AICSImage class * add some extra validation to the size() function, and a couple extra tests
I added size getters to AICSImage. TODO eventually one day we should get the dimensions initialized from metadata rather than having to load the entire file. :)