-
Notifications
You must be signed in to change notification settings - Fork 280
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
ENH: avoid particle_index type cast #4996
base: main
Are you sure you want to change the base?
Conversation
Converting to draft because this needs some more thorough testing: namely, does this introduce any bugs in cython operations that are expecting float arrays? Only tried it out with a projection and it works (because the int field gets converted to float before the projection), but need to check other functionality. |
59b5d85
to
9b5c823
Compare
9b5c823
to
641e590
Compare
Assuming tests pass again, I think this is ready for review. For reference, the following IO child classes override
The one other IO frontend that overrides |
oh, I suppose I should update the docs to mention this behavior, but I'll wait for review before doing that in case the functionality changes. |
As I said somewhere else I'm currently unable to compile yt so I cannot review this properly at the moment. I hope someone else can step in. |
if finfos[f].units != finfos[f].output_units: | ||
self.field_data[f].convert_to_units(finfos[f].output_units) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note to reviewers: this change is necessary because unit conversions on unyt arrays of int dtype always cast the result to float64. e.g., unyt.unyt_array([1, 2], "1", dtype='int').to("1")
yields an array of type float64
. So only converting if the units differ allow the int fields to persist through the data read.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems good to me as a first pass.
I want to note one other possible place we may want to update things. I think it might be too much work, but could be simplified with a broader particle type accessing refactor. (i.e., making it easier to figure out what type to make new fields, etc.)
I can see use cases for making the smoothing and deposition operations utilize integers. Specifically, if one wanted to deposit the nearest particle's index, that would be a use case we could support. But, it's not necessary for this.
I would also like to suggest that we avoid (for now) allowing 32bit floats and ints, which you've also avoided here.
One thing that came to mind, in addition -- using fused types we can make it somewhat easier to address issues in the cython code, but we could also utilize JIT's for fun in the distant future. |
I agree! and I think this PR opens up the possibility of doing just that.
agreed! I'll add a comment to the new |
pre-commit.ci autofix |
for more information, see https://pre-commit.ci
@chrishavlin what needs to get this across the finish line? |
hey @jzuhone -- it's just in need of some reviews. |
just updated the main comment to make this easier to review. |
@yt-fido test this please |
@@ -60,6 +60,22 @@ def push(self, grid, field, data): | |||
raise ValueError | |||
self.queue[grid][field] = data | |||
|
|||
@property |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My only comment here is that I'd love it if it was more general (as this relates to the use case that @ChunYen-Chen was talking about). But if that is not easy to do in this PR we can just submit another one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well my thinking was this _particle_dtypes
dictionary would be a general, front-end dependent dictionary mapping types to fields. Any ideas on how to make it more general/useable? Looking at it now after some months, I do think it could make sense to instead have this field-dtype mapping defined over in the FieldInfoContainer
, but that would still require each frontend to over-ride or add to it as needed.
realized i re-triggered the wrong test... decided to just merge with main since it's been so long. |
that type-checking failure looks unrelated, will open an issue for it. |
Updated Summary (2025-01-23)
This PR adds some logic to be able to set and preserve data types for particle fields. The logic defaults to float64 for all fields, except for the
particle_index
, which is set to int64. Some caveats, limitations and comments:_read_particle_selection
, which is most of them, but see this comment for those that don't (and would need separate updates): ENH: avoid particle_index type cast #4996 (comment)particle_index
) to be set to int64 would be possible in each frontend by overriding the_particle_dtypes
attribute added in this PR. But if they are fields that are used in computations that are passed down to cython functions, it's possible some errors will be exposed (though in my investigation, all the calls to cython functions explicitly cast to float or have a unyt operation, which implicitly casts to float, before the call).Original comment
This is a possible fix for #4995 which would affect all the particle frontends that don't override
_read_particle_selection
.