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

implementation question: possible to subclass the unyt array type? #39

Closed
zingale opened this issue Jul 26, 2018 · 2 comments
Closed

implementation question: possible to subclass the unyt array type? #39

zingale opened this issue Jul 26, 2018 · 2 comments

Comments

@zingale
Copy link
Member

zingale commented Jul 26, 2018

We currently have a data type ArrayIndexer that is built from subclassing np.ndarray:

https://github.com/zingale/pyro2/blob/master/mesh/array_indexer.py

We do this to add methods that we wanted for basic finite-difference operations and boundary conditions for our code.

Is there an easy way to subclass unyt_array just like one would for np.ndarray? We'd like to be able to add units to our type.

It maybe that what we are doing is not the best approach anyway, so if there is better way to add methods to the unyt_array, we're happy to hear that as well.

@ngoldbaum
Copy link
Member

It looks like your subclass just attaches metadata to the array, I think it should work without too much hassle. I'd do this by overriding __new__ and __array_finalize__ in your subclass but make sure to call those methods on the superclass explicitly.

For inspiration you can look at ImageArray in yt, which is a subclass of YTArray and will be a subclass of unyt_array in yt 4.0. We also explicitly test that subclassing works correctly (at least in a limited sense):

https://github.com/yt-project/unyt/blob/master/unyt/tests/test_unyt_array.py#L1179

If you run into issues please let us know.

@zingale
Copy link
Member Author

zingale commented Jul 26, 2018

Thanks Nathan.

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