openPMD-api 0.17.1.post5__cp315-cp315-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,149 @@
1
+ """
2
+ This file is part of the openPMD-api.
3
+
4
+ Copyright 2021-2023 openPMD contributors
5
+ Authors: Axel Huebl, Pawel Ordyna
6
+ License: LGPLv3+
7
+ """
8
+ import math
9
+
10
+ import numpy as np
11
+
12
+
13
+ class DaskRecordComponent:
14
+ # shape, .ndim, .dtype and support numpy-style slicing
15
+ def __init__(self, record_component):
16
+ self.rc = record_component
17
+
18
+ @property
19
+ def shape(self):
20
+ # fixme: https://github.com/openPMD/openPMD-api/issues/808
21
+ return tuple(self.rc.shape)
22
+
23
+ @property
24
+ def ndim(self):
25
+ return self.rc.ndim
26
+
27
+ @property
28
+ def dtype(self):
29
+ return self.rc.dtype
30
+
31
+ def __getitem__(self, slices):
32
+ """here we support what Record_Component implements: a tuple of slices,
33
+ a slice or an index; we do not support fancy indexing
34
+ """
35
+ # FIXME: implement handling of zero-slices in Record_Component
36
+ # https://github.com/openPMD/openPMD-api/issues/957
37
+ all_zero = True
38
+ for s in slices:
39
+ if s != np.s_[0:0]:
40
+ all_zero = False
41
+ if all_zero:
42
+ return np.array([], dtype=self.dtype)
43
+
44
+ data = self.rc[slices]
45
+ self.rc.series_flush()
46
+ if not math.isclose(1.0, self.rc.unit_SI):
47
+ data = np.multiply(data, self.rc.unit_SI)
48
+
49
+ return data
50
+
51
+
52
+ def record_component_to_daskarray(record_component, chunks=None):
53
+ """
54
+ Load a RecordComponent into a Dask.array.
55
+
56
+ Parameters
57
+ ----------
58
+ record_component : openpmd_api.Record_Component
59
+ A record component class in openPMD-api.
60
+ chunks : chunks parameter to pass to dask.array.from_array.
61
+ See dask documentation for more details.
62
+ When set to None (default) the chunking will be automaticaly
63
+ determined based on record_component.available_chunks().
64
+
65
+ Returns
66
+ -------
67
+ dask.array
68
+ A dask array.
69
+
70
+ Raises
71
+ ------
72
+ ImportError
73
+ Raises an exception if dask is not installed
74
+
75
+ See Also
76
+ --------
77
+ openpmd_api.BaseRecordComponent.available_chunks : available chunks that
78
+ are used internally to parallelize reading
79
+ dask.array : the (potentially distributed) array object created here
80
+ """
81
+ # Import dask here for a lazy import
82
+ try:
83
+ from dask.array import from_array
84
+ found_dask = True
85
+ except ImportError:
86
+ found_dask = False
87
+
88
+ if not found_dask:
89
+ raise ImportError("dask NOT found. Install dask for Dask DataFrame "
90
+ "support.")
91
+ if chunks is None:
92
+ # get optimal chunks
93
+ chunks = record_component.available_chunks()
94
+
95
+ # sort and prepare the chunks for Dask's array API
96
+ # https://docs.dask.org/en/latest/array-chunks.html
97
+ # https://docs.dask.org/en/latest/array-api.html?highlight=from_array#other-functions
98
+ # sorted and unique
99
+ offsets_per_dim = list(
100
+ map(list, zip(*[chunk.offset for chunk in chunks])))
101
+ offsets_sorted_unique_per_dim = [
102
+ sorted(set(o)) for o in offsets_per_dim]
103
+
104
+ # print("offsets_sorted_unique_per_dim=",
105
+ # list(offsets_sorted_unique_per_dim))
106
+
107
+ # case 1: PIConGPU static load balancing (works with Dask assumptions,
108
+ # chunk option no. 3)
109
+ # all chunks in the same column have the same column width although
110
+ # individual columns have different widths
111
+ # case 2: AMReX boxes
112
+ # all chunks are multiple of a common block size, offsets
113
+ # are a multiple of a common blocksize
114
+ # problem: too limited description in Dask
115
+ # https://github.com/dask/dask/issues/7475
116
+ # work-around: create smaller chunks (this incurs a read cost)
117
+ # by forcing into case 1
118
+ # (this can lead to larger blocks than using
119
+ # the gcd of the extents aka AMReX block size)
120
+ common_chunk_widths_per_dim = list()
121
+ for d, offsets_in_dim in enumerate(offsets_sorted_unique_per_dim):
122
+ # print("d=", d, offsets_in_dim, record_component.shape[d])
123
+ offsets_in_dim_arr = np.array(offsets_in_dim)
124
+ # note: this is in the right order of rows/columns, contrary to a
125
+ # sorted extent list from chunks
126
+ extents_in_dim = np.zeros_like(offsets_in_dim_arr)
127
+ extents_in_dim[:-1] = offsets_in_dim_arr[1:]
128
+ extents_in_dim[-1] = record_component.shape[d]
129
+ if len(extents_in_dim) > 1:
130
+ extents_in_dim[:-1] -= offsets_in_dim_arr[:-1]
131
+ extents_in_dim[-1] -= offsets_in_dim_arr[-1]
132
+ # print("extents_in_dim=", extents_in_dim)
133
+ common_chunk_widths_per_dim.append(tuple(extents_in_dim))
134
+
135
+ chunks = tuple(common_chunk_widths_per_dim)
136
+ # print("chunks=", chunks)
137
+
138
+ da = from_array(
139
+ DaskRecordComponent(record_component),
140
+ chunks=chunks,
141
+ # name=None,
142
+ asarray=True,
143
+ fancy=False,
144
+ # getitem=None,
145
+ # meta=None,
146
+ # inline_array=False
147
+ )
148
+
149
+ return da
@@ -0,0 +1,102 @@
1
+ """
2
+ This file is part of the openPMD-api.
3
+
4
+ Copyright 2021 openPMD contributors
5
+ Authors: Axel Huebl, Dmitry Ganyushin, John Kirkham
6
+ License: LGPLv3+
7
+ """
8
+ import numpy as np
9
+
10
+
11
+ def read_chunk_to_df(species, chunk, attributes=None):
12
+ stride = np.s_[chunk.offset[0]:chunk.offset[0]+chunk.extent[0]]
13
+ return species.to_df(attributes=attributes, slice=stride)
14
+
15
+
16
+ def particles_to_daskdataframe(particle_species, attributes=None):
17
+ """
18
+ Load all records of a particle species into a Dask DataFrame.
19
+
20
+ Parameters
21
+ ----------
22
+ particle_species : openpmd_api.ParticleSpecies
23
+ A ParticleSpecies class in openPMD-api.
24
+ attributes : list of strings, optional
25
+ A list of attributes of the particle_species that should be read and
26
+ added as extra columns.
27
+
28
+ Returns
29
+ -------
30
+ dask.dataframe
31
+ A dask dataframe with particles as index and openPMD record
32
+ components of the particle_species as columns.
33
+
34
+ Raises
35
+ ------
36
+ ImportError
37
+ Raises an exception if dask or pandas are not installed
38
+
39
+ See Also
40
+ --------
41
+ openpmd_api.BaseRecordComponent.available_chunks : available chunks that
42
+ are used internally to parallelize particle processing
43
+ dask.dataframe : the central dataframe object created here
44
+ """
45
+ # import here for lazy imports
46
+ try:
47
+ import dask.dataframe as dd
48
+ from dask.delayed import delayed
49
+ found_dask = True
50
+ except ImportError:
51
+ found_dask = False
52
+ try:
53
+ import pandas # noqa
54
+ found_pandas = True
55
+ except ImportError:
56
+ found_pandas = False
57
+
58
+ if not found_dask:
59
+ raise ImportError("dask NOT found. Install dask for Dask DataFrame "
60
+ "support.")
61
+ if not found_pandas: # catch this early: before delayed functions
62
+ raise ImportError("pandas NOT found. Install pandas for DataFrame "
63
+ "support.")
64
+
65
+ # get optimal chunks: query first non-constant record component and
66
+ # assume the same chunking applies for all of them
67
+ # in a particle species
68
+ chunks = None
69
+ for k_r, r in particle_species.items():
70
+ for k_rc, rc in r.items():
71
+ if not rc.constant:
72
+ chunks = rc.available_chunks()
73
+ break
74
+ if chunks:
75
+ break
76
+
77
+ # only constant record components:
78
+ # fall back to a single, big chunk here
79
+ if chunks is None:
80
+ for k_r, r in particle_species.items():
81
+ for k_rc, rc in r.items():
82
+ chunks = rc.available_chunks()
83
+ break
84
+ if chunks:
85
+ break
86
+
87
+ # merge DataFrames
88
+ dfs = [
89
+ delayed(read_chunk_to_df)(
90
+ particle_species, chunk=chunk, attributes=attributes
91
+ ) for chunk in chunks
92
+ ]
93
+ df = dd.from_delayed(dfs)
94
+
95
+ # set a header for the first column (row index)
96
+ # note: this is NOT the particle id
97
+ # TODO both these do not work:
98
+ # https://github.com/dask/dask/issues/10440
99
+ # df.index.name = "row"
100
+ # df.index = df.index.rename("row")
101
+
102
+ return df
@@ -0,0 +1,216 @@
1
+ """
2
+ This file is part of the openPMD-api.
3
+
4
+ Copyright 2021 openPMD contributors
5
+ Authors: Axel Huebl
6
+ License: LGPLv3+
7
+ """
8
+ import math
9
+
10
+ import numpy as np
11
+
12
+
13
+ def particles_to_dataframe(particle_species,
14
+ *legacy_args,
15
+ attributes=None,
16
+ slice=None):
17
+ """
18
+ Load all records of a particle species into a Pandas DataFrame.
19
+
20
+ Parameters
21
+ ----------
22
+ particle_species : openpmd_api.ParticleSpecies
23
+ A ParticleSpecies class in openPMD-api.
24
+ legacy_args : tuple
25
+ DO NOT USE. Catch-all for legacy, unnamed arguments.
26
+ attributes : list of strings, optional
27
+ A list of attributes of the particle_species that should be read and
28
+ added as extra columns.
29
+ slice : np.s_, optional
30
+ A numpy slice that can be used to load only a sub-selection of
31
+ particles.
32
+
33
+ Returns
34
+ -------
35
+ pandas.DataFrame
36
+ A pandas dataframe with particles as index and openPMD record
37
+ components of the particle_species as columns.
38
+
39
+ Raises
40
+ ------
41
+ ImportError
42
+ Raises an exception if pandas is not installed
43
+
44
+ See Also
45
+ --------
46
+ numpy.s_ : the slice object to sub-select
47
+ openpmd_api.BaseRecordComponent.available_chunks : available chunks that
48
+ are optimal arguments for the slice parameter
49
+ pandas.DataFrame : the central dataframe object created here
50
+ """
51
+ # backwards compatibility: in openPMD-api 0.17+, we added the
52
+ # additional "attributes" argument and moved slice= to the end.
53
+ if legacy_args:
54
+ if attributes is None and slice is None and len(legacy_args) == 1:
55
+ slice = legacy_args[0]
56
+ import warnings
57
+ warnings.warn("The to_df() argument order changed in "
58
+ "openPMD-api 0.17.0!\nThe slice "
59
+ "argument must be passed as a named argument.",
60
+ DeprecationWarning
61
+ )
62
+ else:
63
+ raise RuntimeError("to_df() does not support unnamed arguments!")
64
+
65
+ # import pandas here for a lazy import
66
+ try:
67
+ import pandas as pd
68
+ found_pandas = True
69
+ except ImportError:
70
+ found_pandas = False
71
+
72
+ if not found_pandas:
73
+ raise ImportError("pandas NOT found. Install pandas for DataFrame "
74
+ "support.")
75
+ if slice is None:
76
+ slice = np.s_[()]
77
+
78
+ columns = {}
79
+
80
+ for record_name, record in particle_species.items():
81
+ for rc_name, rc in record.items():
82
+ if record.scalar:
83
+ column_name = record_name
84
+ else:
85
+ column_name = record_name + "_" + rc_name
86
+ columns[column_name] = rc[slice]
87
+ particle_species.series_flush()
88
+ if not math.isclose(1.0, rc.unit_SI):
89
+ columns[column_name] = np.multiply(
90
+ columns[column_name], rc.unit_SI)
91
+
92
+ df = pd.DataFrame(columns)
93
+
94
+ if attributes is not None:
95
+ for attribute in attributes:
96
+ df[attribute] = particle_species.get_attribute(attribute)
97
+
98
+ # set a header for the first column (row index)
99
+ # note: this is NOT the particle id
100
+ df.index.name = "row"
101
+
102
+ return df
103
+
104
+
105
+ def iterations_to_dataframe(series, species_name, attributes=None):
106
+ """
107
+ Load all iterations of a particle species into a Pandas DataFrame.
108
+
109
+ Parameters
110
+ ----------
111
+ series : openpmd_api.Series
112
+ A Series class in openPMD-api.
113
+ species_name : string
114
+ The name of a particle species.
115
+ attributes : list of strings, optional
116
+ A list of attributes of the particle_species that should be read and
117
+ added as extra columns.
118
+
119
+ Returns
120
+ -------
121
+ pandas.DataFrame
122
+ A pandas dataframe with particles as index and openPMD record
123
+ components of the particle_species as columns. Particles might be
124
+ repeated over multiple iterations and an "iteration" column is
125
+ added.
126
+
127
+ Raises
128
+ ------
129
+ ImportError
130
+ Raises an exception if pandas is not installed
131
+
132
+ See Also
133
+ --------
134
+ pandas.DataFrame : the central dataframe object created here
135
+ """
136
+ # import pandas here for a lazy import
137
+ try:
138
+ import pandas as pd
139
+ except ImportError:
140
+ raise ImportError("pandas NOT found. Install pandas for DataFrame "
141
+ "support.")
142
+
143
+ df = pd.concat(
144
+ (
145
+ iteration
146
+ .particles[species_name]
147
+ .to_df(attributes=attributes)
148
+ .assign(iteration=i)
149
+ for i, iteration in series.snapshots().items()
150
+ ),
151
+ axis=0,
152
+ ignore_index=True,
153
+ )
154
+
155
+ return df
156
+
157
+
158
+ def iterations_to_cudf(series, species_name, attributes=None):
159
+ """
160
+ Load all iterations of a particle species into a cuDF DataFrame.
161
+
162
+ Parameters
163
+ ----------
164
+ series : openpmd_api.Series
165
+ A Series class in openPMD-api.
166
+ species_name : string
167
+ The name of a particle species.
168
+ attributes : list of strings, optional
169
+ A list of attributes of the particle_species that should be read and
170
+ added as extra columns.
171
+
172
+ Returns
173
+ -------
174
+ cudf.DataFrame
175
+ A cuDF (RAPIDS) dataframe with particles as index and openPMD record
176
+ components of the particle_species as columns. Particles might be
177
+ repeated over multiple iterations and an "iteration" column is
178
+ added.
179
+
180
+ Raises
181
+ ------
182
+ ImportError
183
+ Raises an exception if cuDF (RAPIDS) is not installed
184
+
185
+ See Also
186
+ --------
187
+ cudf.DataFrame : the central dataframe object created here
188
+ """
189
+ # import pandas here for a lazy import
190
+ try:
191
+ import pandas # noqa
192
+ except ImportError:
193
+ raise ImportError("pandas NOT found. Install pandas for DataFrame "
194
+ "support.")
195
+ # import cudf here for a lazy import
196
+ try:
197
+ import cudf
198
+ except ImportError:
199
+ raise ImportError("cudf NOT found. Install RAPIDS for CUDA DataFrame "
200
+ "support.")
201
+
202
+ cdf = cudf.concat(
203
+ (
204
+ cudf.from_pandas(
205
+ iteration
206
+ .particles[species_name]
207
+ .to_df(attributes=attributes)
208
+ .assign(iteration=i)
209
+ )
210
+ for i, iteration in series.snapshots().items()
211
+ ),
212
+ axis=0,
213
+ ignore_index=True,
214
+ )
215
+
216
+ return cdf
@@ -0,0 +1,21 @@
1
+ from . import openpmd_api_cxx as cxx
2
+ from .DaskArray import record_component_to_daskarray
3
+ from .DaskDataFrame import particles_to_daskdataframe
4
+ from .DataFrame import (iterations_to_cudf, iterations_to_dataframe,
5
+ particles_to_dataframe)
6
+ from .openpmd_api_cxx import * # noqa
7
+
8
+ __version__ = cxx.__version__
9
+ __doc__ = cxx.__doc__
10
+ __license__ = cxx.__license__
11
+ # __author__ = cxx.__author__
12
+
13
+ # extend CXX classes with extra methods
14
+ ParticleSpecies.to_df = particles_to_dataframe # noqa
15
+ ParticleSpecies.to_dask = particles_to_daskdataframe # noqa
16
+ Record_Component.to_dask_array = record_component_to_daskarray # noqa
17
+ Series.to_df = iterations_to_dataframe # noqa
18
+ Series.to_cudf = iterations_to_cudf # noqa
19
+
20
+ # TODO remove in future versions (deprecated)
21
+ Access_Type = Access # noqa
@@ -0,0 +1 @@
1
+ __doc__ = "for usage documentation, call this module's main with --help"
@@ -0,0 +1,22 @@
1
+ """
2
+ This file is part of the openPMD-api.
3
+
4
+ This module provides functions that are wrapped into sys.exit(...()) calls by
5
+ the setuptools (setup.py) "entry_points" -> "console_scripts" generator.
6
+
7
+ Copyright 2020-2021 openPMD contributors
8
+ Authors: Axel Huebl
9
+ License: LGPLv3+
10
+ """
11
+ import sys
12
+
13
+ from ..openpmd_api_cxx import _ls_run
14
+
15
+
16
+ def main():
17
+ """ for usage documentation, call this with a --help argument """
18
+ return _ls_run(sys.argv)
19
+
20
+
21
+ if __name__ == "__main__":
22
+ sys.exit(main())
Binary file
Binary file
@@ -0,0 +1 @@
1
+ __doc__ = "for usage documentation, call this module's main with --help"