-
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?
Changes from 1 commit
641e590
e6315d7
26abd0d
c3f8d0f
af7bb93
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. well my thinking was this |
||
def _particle_dtypes(self) -> defaultdict[FieldKey, type]: | ||
# returns a defaultdict of data types for particle fields. | ||
# defaults for all fields are float64, except for the particle_index | ||
# field (or its alias if it exists), which is set as int64. Important | ||
# to note that the data type will only be preserved for direct reads | ||
# and any operations will either implicity (via unyt) or explicitly | ||
# convert to float64. | ||
chrishavlin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
dtypes: defaultdict[FieldKey, type] = defaultdict(lambda: np.float64) | ||
for ptype in self.ds.particle_types: | ||
p_index = (ptype, "particle_index") | ||
if p_index in self.ds.field_info.field_aliases: | ||
p_index = self.ds.field_info.field_aliases[p_index] | ||
dtypes[p_index] = np.int64 | ||
matthewturk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return dtypes | ||
|
||
def _field_in_backup(self, grid, backup_file, field_name): | ||
if os.path.exists(backup_file): | ||
fhandle = h5py.File(backup_file, mode="r") | ||
|
@@ -173,6 +189,7 @@ def _read_particle_selection( | |
# field_maps stores fields, accounting for field unions | ||
ptf: defaultdict[str, list[str]] = defaultdict(list) | ||
field_maps: defaultdict[FieldKey, list[FieldKey]] = defaultdict(list) | ||
p_dtypes = self._particle_dtypes | ||
|
||
# We first need a set of masks for each particle type | ||
chunks = list(chunks) | ||
|
@@ -206,14 +223,14 @@ def _read_particle_selection( | |
vals = data.pop(field_f) | ||
# note: numpy.concatenate has a dtype argument that would avoid | ||
# a copy using .astype(...), available in numpy>=1.20 | ||
rv[field_f] = np.concatenate(vals, axis=0).astype("float64") | ||
rv[field_f] = np.concatenate(vals, axis=0).astype(p_dtypes[field_f]) | ||
else: | ||
shape = [0] | ||
if field_f[1] in self._vector_fields: | ||
shape.append(self._vector_fields[field_f[1]]) | ||
elif field_f[1] in self._array_fields: | ||
shape.append(self._array_fields[field_f[1]]) | ||
rv[field_f] = np.empty(shape, dtype="float64") | ||
rv[field_f] = np.empty(shape, dtype=p_dtypes[field_f]) | ||
return rv | ||
|
||
def _read_particle_fields(self, chunks, ptf, selector): | ||
|
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 typefloat64
. So only converting if the units differ allow the int fields to persist through the data read.