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

reduce code duplication in IOHandler _read_particle_coords and _read_particle_fields #4597

Merged
merged 2 commits into from
Jul 27, 2023

Conversation

chrishavlin
Copy link
Contributor

This PR reduces duplicated code related to iterating over data_file chunks in _read_particle_coords and _read_particle_fields across IOHandlers. Shouldn't affect any behavior...

missed one

fix ahf modification
@chrishavlin chrishavlin added index: particle refactor improve readability, maintainability, modularity labels Jul 24, 2023
matthewturk
matthewturk previously approved these changes Jul 24, 2023
return data_files

def _sorted_chunk_iterator(self, chunks):
data_files = self._get_data_files(chunks)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should explicitly turn chunks into a list here, unless it has been elsewhere higher up the stack; recently we exhausted an iterator and ran into issues. (Maybe with nc_cm1?) Also, we sort with x.start and x.filename but not all frontends did that -- does x.start have a default value of 0 or something constant that we can rely on?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya, _get_data_files turns chunks into a list.

Will check on how reliable the start attribute is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh and I left the front ends that do not sort by both filename and start intact. They override this method so that they are unchanged

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so ParticleFile.start does initialize to 0 if it's not provided (within ParticleFile.__init__ (link to relevant code).

But in any case, the changes in this PR do not change any behavior -- only the frontends that were sorting by filename and start are using this function. Those that were not already doing that either override this method or don't use this method at all.

@@ -194,6 +185,10 @@ def members(self, ihalo):
members = fpu.read_attrs(todo.pop(0))["particle_identities"]
return members

def _sorted_chunk_iterator(self, chunks):
data_files = self._get_data_files(chunks)
yield from sorted(data_files, key=attrgetter("filename"))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matthewturk just pointing out an example where I added an override of _sorted_chunk_iterator so that the frontend continues only sorting by filename. The change in ahf is similar.

for chunk in chunks:
for obj in chunk.objs:
data_files.update(obj.data_files)
data_files = self._get_data_files(chunks)
assert len(data_files) == 1
for _data_file in sorted(data_files, key=lambda x: (x.filename, x.start)):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a note: I did not use the _sorted_chunk_iterator here because I wanted to keep the assert len(data_files) == 1 line above.

Copy link
Member

@neutrinoceros neutrinoceros left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glad to see this merged if @matthewturk is still happy with it.

@matthewturk matthewturk merged commit 28defca into yt-project:main Jul 27, 2023
10 checks passed
@neutrinoceros neutrinoceros added this to the 4.3.0 milestone Jul 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
index: particle refactor improve readability, maintainability, modularity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants