-
Notifications
You must be signed in to change notification settings - Fork 8
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
multiscale class not being inherited correctly #80
Comments
@TomNicholas what do you think? |
Hi, thanks for making me aware of what you're doing here! So DataTree was not intended to be subclassed. In fact, we currently recommend against subclassing xarray objects in general, and this this recommendation really applies to datatree too because it follows similar patterns internally. It's worth asking why you are subclassing? Looking at the definition of If you really want to subclass it has been done, and if you want to improve the experience of subclassing xarray objects we would love someone to take that project on upstream. |
@TomNicholas thank you for the thoughts and pointers! What we are looking for is:
From what you shared it sounds like an accessor would be better, and appropriate, addressing scverse/spatialdata#269, etc.? Will |
Thanks for the discussion. I think this would be a good solution and it would be compatible with the type identification used in spatialdata. In fact in our case we can replace all the One note, I find a bit inconsistent that the scales of a |
thanks all for the discussion, agree with @LucaMarconato that type consistency across spatial-image project would be useful, seems like an accessor could be the best solution.
yes indeed, I think we might end up refactor the models to infer type based on data type + number/type of dimensions. |
@thewtex no I don't think this would work - when the accessor is registered it becomes available on all
@LucaMarconato this seems to line up with what's currently recommended. 👍
This would of course still work. (You wouldn't be technically inferring "Type", but you could use it to distinguish valid from invalid datasets.) |
@LucaMarconato @TomNicholas excellent, thank you for the thoughts! Let's migrate to an accessor, then. @giovp already has a draft in #81 -- please take a look. 🎇 🚀 🤘
👍 agreed. |
This issue occurs currently also when using pytest fixtures. Pytest passes the return value of a fixture function (1) directly to test functions using the fixture. However, when another fixture (2) depends on fixture (1), it receives a deepcopy, where multiscale images are suddenly datatrees, and fail dispatching in overloaded functions. In case someone else encounters this, here my workaround that I apply in the test function on the fixture argument: def workaround_pytest_fixture_breaking_multiscale_spatial_image(sdata: SpatialData) -> SpatialData:
# When a fixture depends on another fixture which returns a MultiscaleSpatialImage instance,
# the first fixture receives a DataTree instead. The object looses the ability to be recognized
# by isinstance(obj, MultiscaleSpatialImage).
# Maybe this is caused by a copy/deepcopy operation.
for name, image in sdata.images.items():
if isinstance(image, DataTree) and not isinstance(image, MultiscaleSpatialImage):
repaired_image = MultiscaleSpatialImage.from_dict(image.to_dict())
sdata.images[name] = repaired_image
for name, labels in sdata.labels.items():
if isinstance(labels, DataTree) and not isinstance(labels, MultiscaleSpatialImage):
repaired_labels = MultiscaleSpatialImage.from_dict(labels.to_dict())
sdata.labels[name] = repaired_labels
return sdata |
@aeisenbarth thanks for the report 👍 In your use case, is Regardless, we should probably also have tests for the copy/deepcopy operation. |
I just updated to multiscale-spatial-image 1.0.0, but the issue persists with latest spatialdata main branch. |
hi @aeisenbarth , indeed we haven't completed the migration to msi 1.0.0 yet (unfortunately). From your message I thought it was a behaviour of the pytest fixture but I haven't investigated further. |
hi @aeisenbarth I think the latest sptialdata (on pypi) supports this (thanks to scverse/spatialdata#594 ). I will close this for now but feel free to reopen! |
Also @aeisenbarth I have implemented a |
There are several operations that do not return a
MultiscaleSpatialImage
class but adatatree.DataTree
. e.g.I've been playing around with subclassing differently
DataTree
without success. I wonder what's the best solution for this? I think it should either always return aMultiscaleSpatialImage
or always aDataTree
(andMultiscaleSpatialImage
would then be a parser class, but IMHOSpatialImage
should then be consistent, and it doesn't suffer from such issues).xref scverse/spatialdata#269
The text was updated successfully, but these errors were encountered: