-
Notifications
You must be signed in to change notification settings - Fork 19
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
Pandas: ParticleContainer_*.to_df()
#220
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
""" | ||
This file is part of pyAMReX | ||
|
||
Copyright 2023 AMReX community | ||
Authors: Axel Huebl | ||
License: BSD-3-Clause-LBNL | ||
""" | ||
|
||
|
||
def pc_to_df(self, local=True, comm=None, root_rank=0): | ||
""" | ||
Copy all particles into a pandas.DataFrame | ||
|
||
Parameters | ||
---------- | ||
self : amrex.ParticleContainer_* | ||
A ParticleContainer class in pyAMReX | ||
local : bool | ||
MPI-local particles | ||
comm : MPI Communicator | ||
if local is False, this defaults to mpi4py.MPI.COMM_WORLD | ||
root_rank : MPI root rank to gather to | ||
if local is False, this defaults to 0 | ||
|
||
Returns | ||
------- | ||
A concatenated pandas.DataFrame with particles from all levels. | ||
|
||
Returns None if no particles were found. | ||
If local=False, then all ranks but the root_rank will return None. | ||
""" | ||
import pandas as pd | ||
|
||
# create a DataFrame per particle box and append it to the list of | ||
# local DataFrame(s) | ||
dfs_local = [] | ||
for lvl in range(self.finest_level + 1): | ||
for pti in self.const_iterator(self, level=lvl): | ||
if pti.size == 0: | ||
continue | ||
|
||
if self.is_soa_particle: | ||
next_df = pd.DataFrame() | ||
else: | ||
# AoS | ||
aos_np = pti.aos().to_numpy(copy=True) | ||
next_df = pd.DataFrame(aos_np) | ||
next_df.set_index("cpuid") | ||
next_df.index.name = "cpuid" | ||
|
||
# SoA | ||
soa_view = pti.soa().to_numpy(copy=True) | ||
soa_np_real = soa_view.real | ||
soa_np_int = soa_view.int | ||
|
||
for idx, array in enumerate(soa_np_real): | ||
next_df[f"SoA_real_{idx}"] = array | ||
for idx, array in enumerate(soa_np_int): | ||
next_df[f"SoA_int_{idx}"] = array | ||
|
||
dfs_local.append(next_df) | ||
|
||
# MPI Gather to root rank if requested | ||
if local: | ||
if len(dfs_local) == 0: | ||
df = None | ||
else: | ||
df = pd.concat(dfs_local) | ||
else: | ||
from mpi4py import MPI | ||
|
||
if comm is None: | ||
comm = MPI.COMM_WORLD | ||
rank = comm.Get_rank() | ||
|
||
# a list for each rank's list of DataFrame(s) | ||
df_list_list = comm.gather(dfs_local, root=root_rank) | ||
|
||
if rank == root_rank: | ||
flattened_list = [df for sublist in df_list_list for df in sublist] | ||
|
||
if len(flattened_list) == 0: | ||
df = pd.DataFrame() | ||
else: | ||
df = pd.concat(flattened_list, ignore_index=True) | ||
else: | ||
df = None | ||
|
||
return df | ||
|
||
|
||
def register_ParticleContainer_extension(amr): | ||
"""ParticleContainer helper methods""" | ||
import inspect | ||
import sys | ||
|
||
# register member functions for every ParticleContainer_* type | ||
for _, ParticleContainer_type in inspect.getmembers( | ||
sys.modules[amr.__name__], | ||
lambda member: inspect.isclass(member) | ||
and member.__module__ == amr.__name__ | ||
and member.__name__.startswith("ParticleContainer_"), | ||
): | ||
ParticleContainer_type.to_df = pc_to_df |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
todo: I think this might miss on the runtime attributes (they are also in SoA format).
This should go into
SoA::to_numpy
akasoa_to_numpy
&soa_to_cupy
,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.
Requires to call:
AddRealComp
ResizeRuntimeRealComp
again with redundant info (again the comm and number of components)but AMReX-Codes/amrex#3615 could simplify this.