aggfly 0.1.0__tar.gz
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.
- aggfly-0.1.0/LICENSE +201 -0
- aggfly-0.1.0/PKG-INFO +47 -0
- aggfly-0.1.0/README.md +19 -0
- aggfly-0.1.0/aggfly/__init__.py +32 -0
- aggfly-0.1.0/aggfly/aggregate/__init__.py +5 -0
- aggfly-0.1.0/aggfly/aggregate/aggregate.py +228 -0
- aggfly-0.1.0/aggfly/aggregate/aggregate_utils.py +76 -0
- aggfly-0.1.0/aggfly/aggregate/spatial.py +154 -0
- aggfly-0.1.0/aggfly/aggregate/temporal.py +198 -0
- aggfly-0.1.0/aggfly/aggregate/z_old/spatial-Copy1.py +102 -0
- aggfly-0.1.0/aggfly/aggregate/z_old/temporal-Copy1.py +430 -0
- aggfly-0.1.0/aggfly/cache/__init__.py +1 -0
- aggfly-0.1.0/aggfly/cache/project_cache.py +161 -0
- aggfly-0.1.0/aggfly/dataset/__init__.py +3 -0
- aggfly-0.1.0/aggfly/dataset/dataset.py +546 -0
- aggfly-0.1.0/aggfly/dataset/grid.py +196 -0
- aggfly-0.1.0/aggfly/dataset/grid_utils.py +142 -0
- aggfly-0.1.0/aggfly/regions/__init__.py +1 -0
- aggfly-0.1.0/aggfly/regions/georegions.py +222 -0
- aggfly-0.1.0/aggfly/regions/shp_utils.py +47 -0
- aggfly-0.1.0/aggfly/tests/__init__.py +1 -0
- aggfly-0.1.0/aggfly/tests/test_aggregate.py +155 -0
- aggfly-0.1.0/aggfly/utils.py +51 -0
- aggfly-0.1.0/aggfly/weights/__init__.py +4 -0
- aggfly-0.1.0/aggfly/weights/crop_weights.py +138 -0
- aggfly-0.1.0/aggfly/weights/crop_weights_utils.py +0 -0
- aggfly-0.1.0/aggfly/weights/grid_weights.py +486 -0
- aggfly-0.1.0/aggfly/weights/pop_weights.py +76 -0
- aggfly-0.1.0/aggfly/weights/secondary_weights.py +119 -0
- aggfly-0.1.0/pyproject.toml +31 -0
aggfly-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
aggfly-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: aggfly
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Efficient spatial and temporal aggregation of gridded climate data
|
|
5
|
+
Author: Dylan Hogan
|
|
6
|
+
Author-email: dth2133@columbia.edu
|
|
7
|
+
Requires-Python: >=3.11.6
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
10
|
+
Requires-Dist: dask (>=2023.11.0,<2024.0.0)
|
|
11
|
+
Requires-Dist: dask-geopandas (>=0.3.1,<0.4.0)
|
|
12
|
+
Requires-Dist: dill (>=0.3.8,<0.4.0)
|
|
13
|
+
Requires-Dist: geopandas (>=0.14.4,<0.15.0)
|
|
14
|
+
Requires-Dist: matplotlib (>=3.9.0,<4.0.0)
|
|
15
|
+
Requires-Dist: netcdf4 (>=1.6.5,<2.0.0)
|
|
16
|
+
Requires-Dist: numba (>=0.59.1,<0.60.0)
|
|
17
|
+
Requires-Dist: numpy (>=1.26.4,<2.0.0)
|
|
18
|
+
Requires-Dist: pandas (>=2.2.2,<3.0.0)
|
|
19
|
+
Requires-Dist: pygeos (>=0.14,<0.15)
|
|
20
|
+
Requires-Dist: pytest (>=8.2.1,<9.0.0)
|
|
21
|
+
Requires-Dist: pyyaml (>=6.0.1,<7.0.0)
|
|
22
|
+
Requires-Dist: rasterio (>=1.3.10,<2.0.0)
|
|
23
|
+
Requires-Dist: rioxarray (>=0.15.5,<0.16.0)
|
|
24
|
+
Requires-Dist: xarray (>=2024.5.0,<2025.0.0)
|
|
25
|
+
Requires-Dist: zarr (>=2.18.2,<3.0.0)
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
# aggfly
|
|
29
|
+
A more permanent home for my climate data aggregation code
|
|
30
|
+
|
|
31
|
+
Will add docs and instructions in the next few weeks as I have time. For questions in the meantime, feel free to email me at `dth2133@columbia.edu`.
|
|
32
|
+
|
|
33
|
+
The `aggfly-dev-environment.yml` file provides the core packages you'll need to run the code. You can use this file to recreate the conda environment I use to develop the package. Check out the [conda documentation](https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#creating-an-environment-from-an-environment-yml-file) if you need help using it. The following command (executed from root directory of the repository) should be sufficient for most users:
|
|
34
|
+
|
|
35
|
+
```
|
|
36
|
+
conda env create --file aggfly-dev-environment.yml -n aggfly-dev
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Once you have all the dependencies, you can install this package by running `pip install -e .` from the root directory of the repository.
|
|
40
|
+
|
|
41
|
+
```
|
|
42
|
+
conda activate aggfly-dev
|
|
43
|
+
pip install -e .
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Of course, please feel free to suggest changes/improvements/extensions in [Issues](https://github.com/dylanhogan/aggfly/issues) :)
|
|
47
|
+
|
aggfly-0.1.0/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# aggfly
|
|
2
|
+
A more permanent home for my climate data aggregation code
|
|
3
|
+
|
|
4
|
+
Will add docs and instructions in the next few weeks as I have time. For questions in the meantime, feel free to email me at `dth2133@columbia.edu`.
|
|
5
|
+
|
|
6
|
+
The `aggfly-dev-environment.yml` file provides the core packages you'll need to run the code. You can use this file to recreate the conda environment I use to develop the package. Check out the [conda documentation](https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#creating-an-environment-from-an-environment-yml-file) if you need help using it. The following command (executed from root directory of the repository) should be sufficient for most users:
|
|
7
|
+
|
|
8
|
+
```
|
|
9
|
+
conda env create --file aggfly-dev-environment.yml -n aggfly-dev
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Once you have all the dependencies, you can install this package by running `pip install -e .` from the root directory of the repository.
|
|
13
|
+
|
|
14
|
+
```
|
|
15
|
+
conda activate aggfly-dev
|
|
16
|
+
pip install -e .
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Of course, please feel free to suggest changes/improvements/extensions in [Issues](https://github.com/dylanhogan/aggfly/issues) :)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
from .aggregate import (
|
|
2
|
+
TemporalAggregator,
|
|
3
|
+
SpatialAggregator,
|
|
4
|
+
aggregate_dataset,
|
|
5
|
+
aggregate_time,
|
|
6
|
+
aggregate_space,
|
|
7
|
+
distributed_client,
|
|
8
|
+
is_distributed,
|
|
9
|
+
start_dask_client,
|
|
10
|
+
shutdown_dask_client
|
|
11
|
+
)
|
|
12
|
+
from .dataset import Dataset, Grid, dataset_from_path
|
|
13
|
+
from .weights import (
|
|
14
|
+
CropWeights,
|
|
15
|
+
PopWeights,
|
|
16
|
+
GridWeights,
|
|
17
|
+
SecondaryWeights,
|
|
18
|
+
weights_from_objects,
|
|
19
|
+
pop_weights_from_path,
|
|
20
|
+
crop_weights_from_path,
|
|
21
|
+
secondary_weights_from_path
|
|
22
|
+
)
|
|
23
|
+
from .regions import GeoRegions, georegions_from_path, georegions_from_name
|
|
24
|
+
from .tests import (
|
|
25
|
+
georegion,
|
|
26
|
+
dataset_360,
|
|
27
|
+
secondary_weights,
|
|
28
|
+
weights,
|
|
29
|
+
test_weights,
|
|
30
|
+
test_aggregate_time,
|
|
31
|
+
test_aggregate
|
|
32
|
+
)
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
"""
|
|
2
|
+
This module contains functions for aggregating datasets over time and space.
|
|
3
|
+
|
|
4
|
+
Functions:
|
|
5
|
+
- transform_dataset(dataset: Dataset, key: str, **kwargs: Union[int, List[int]]) -> Dict[str, Dataset]:
|
|
6
|
+
Transform a given dataset by raising it to a power or interacting with another dataset.
|
|
7
|
+
|
|
8
|
+
- aggregate_time(dataset: Dataset, aggregator_dict: Dict[str, Union[List[Tuple], TemporalAggregator]] = None, weights: GridWeights = None, **kwargs) -> Dict[str, Dataset]:
|
|
9
|
+
Aggregate a dataset over time using the specified temporal aggregators.
|
|
10
|
+
|
|
11
|
+
- aggregate_space(dataset_dict: Dict[str, Dataset], weights: GridWeights) -> pd.DataFrame:
|
|
12
|
+
Aggregate a dictionary of datasets over space.
|
|
13
|
+
|
|
14
|
+
- aggregate_dataset(dataset: Dataset, weights: GridWeights, aggregator_dict: Dict[str, Union[List[Tuple], TemporalAggregator]] = None, **kwargs) -> pd.DataFrame:
|
|
15
|
+
Aggregate a dataset over time and space.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from typing import List, Dict, Union, Tuple
|
|
19
|
+
import pandas as pd
|
|
20
|
+
import dask
|
|
21
|
+
from dask.distributed import LocalCluster, Client, progress
|
|
22
|
+
|
|
23
|
+
from .temporal import TemporalAggregator
|
|
24
|
+
from .spatial import SpatialAggregator
|
|
25
|
+
from ..dataset import Dataset
|
|
26
|
+
from ..weights import GridWeights
|
|
27
|
+
from .aggregate_utils import distributed_client, is_distributed, start_dask_client, shutdown_dask_client
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
from dask.diagnostics import ProgressBar
|
|
31
|
+
ProgressBar().register()
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def transform_dataset(
|
|
35
|
+
dataset: Dataset, key: str, **kwargs: Union[int, List[int]]
|
|
36
|
+
) -> Dict[str, Dataset]:
|
|
37
|
+
"""
|
|
38
|
+
Transform a given dataset by raising it to a power or interacting with another dataset.
|
|
39
|
+
|
|
40
|
+
Args:
|
|
41
|
+
dataset (Dataset): The dataset to transform.
|
|
42
|
+
key (str): The name of the dataset.
|
|
43
|
+
**kwargs: Keyword arguments for the transformation.
|
|
44
|
+
If 'exp' is provided, raise the dataset to the power of the provided exponent(s).
|
|
45
|
+
If 'inter' is provided, interact the dataset with another dataset.
|
|
46
|
+
|
|
47
|
+
Returns:
|
|
48
|
+
dict: A dictionary containing the transformed dataset(s).
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
if "exp" in kwargs:
|
|
52
|
+
if not isinstance(kwargs["exp"], list):
|
|
53
|
+
kwargs["exp"] = [kwargs["exp"]]
|
|
54
|
+
dataset = [dataset.power(exp) for exp in kwargs["exp"][0]]
|
|
55
|
+
new_keys = [f"{key}_{exp}" for exp in kwargs["exp"][0]]
|
|
56
|
+
output_dict = dict(zip(new_keys, dataset))
|
|
57
|
+
elif "inter" in kwargs:
|
|
58
|
+
dataset = dataset.interact(kwargs["inter"])
|
|
59
|
+
output_dict = {key: dataset}
|
|
60
|
+
else:
|
|
61
|
+
raise ValueError("No valid transform argument provided.")
|
|
62
|
+
return output_dict.values(), output_dict.keys()
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def aggregate_time(
|
|
66
|
+
dataset: Dataset,
|
|
67
|
+
weights: GridWeights = None,
|
|
68
|
+
aggregator_dict: Dict[str, Union[List[Tuple], TemporalAggregator]] = None,
|
|
69
|
+
**kwargs,
|
|
70
|
+
) -> Dict[str, Dataset]:
|
|
71
|
+
"""
|
|
72
|
+
Aggregate a dataset over time using the specified temporal aggregators.
|
|
73
|
+
|
|
74
|
+
Args:
|
|
75
|
+
dataset (Dataset): The dataset to aggregate.
|
|
76
|
+
weights (GridWeights, optional): The weights to use for aggregation. Defaults to None.
|
|
77
|
+
aggregator_dict (Dict[str, Union[List[Tuple], TemporalAggregator]], optional): A dictionary of temporal aggregators to apply to the dataset. Defaults to None.
|
|
78
|
+
**kwargs: Additional keyword arguments to use if `aggregator_dict` is not provided.
|
|
79
|
+
|
|
80
|
+
Returns:
|
|
81
|
+
Dict[str, Dataset]: A dictionary of aggregated datasets, with keys corresponding to the keys in `aggregator_dict`.
|
|
82
|
+
"""
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def aggregate_time(
|
|
86
|
+
dataset: Dataset,
|
|
87
|
+
weights: GridWeights = None,
|
|
88
|
+
aggregator_dict: Dict[str, Union[List[Tuple], TemporalAggregator]] = None,
|
|
89
|
+
**kwargs,
|
|
90
|
+
) -> Dict[str, Dataset]:
|
|
91
|
+
if aggregator_dict is None:
|
|
92
|
+
if kwargs is None:
|
|
93
|
+
raise ValueError("No arguments provided.")
|
|
94
|
+
else:
|
|
95
|
+
aggregator_dict = kwargs
|
|
96
|
+
out_dict = {}
|
|
97
|
+
for key, value in aggregator_dict.items():
|
|
98
|
+
keys = [key]
|
|
99
|
+
data = [dataset.deepcopy()]
|
|
100
|
+
for key2, value2 in value:
|
|
101
|
+
if key2 == "aggregate":
|
|
102
|
+
if not isinstance(value2, TemporalAggregator):
|
|
103
|
+
value2 = TemporalAggregator(**value2)
|
|
104
|
+
data = [value2.execute(x, weights) for x in data]
|
|
105
|
+
|
|
106
|
+
if value2.multi_dd:
|
|
107
|
+
if len(data) > 1:
|
|
108
|
+
raise ValueError(
|
|
109
|
+
"Cannot aggregate multiple datasets with multiple ddargs, e.g., multiple polynomials for multiple bins"
|
|
110
|
+
)
|
|
111
|
+
data, keys = multi_dd_to_dict(data[0], key, value2.ddargs)
|
|
112
|
+
|
|
113
|
+
elif key2 == "transform":
|
|
114
|
+
transformed_data, transformed_keys = [], []
|
|
115
|
+
for d, k in zip(data, keys):
|
|
116
|
+
d2, k2 = transform_dataset(d, k, **value2)
|
|
117
|
+
transformed_data.extend(d2)
|
|
118
|
+
transformed_keys.extend(k2)
|
|
119
|
+
data, keys = transformed_data, transformed_keys
|
|
120
|
+
|
|
121
|
+
data_dict = dict(zip(keys, data))
|
|
122
|
+
out_dict = out_dict | data_dict
|
|
123
|
+
return out_dict
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def aggregate_space(
|
|
127
|
+
dataset_dict: Dict[str, Dataset], weights: GridWeights,
|
|
128
|
+
npartitions=None, **kwargs
|
|
129
|
+
) -> pd.DataFrame:
|
|
130
|
+
"""
|
|
131
|
+
Aggregate a dictionary of datasets over space.
|
|
132
|
+
|
|
133
|
+
Args:
|
|
134
|
+
dataset_dict (dict): A dictionary containing the datasets to aggregate, where
|
|
135
|
+
the keys are the names of the datasets and the values are the datasets themselves.
|
|
136
|
+
weights (GridWeights): The weights to use for aggregation.
|
|
137
|
+
|
|
138
|
+
Returns:
|
|
139
|
+
df: A dataframe containing the aggregated data.
|
|
140
|
+
"""
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
dataset_list = list(dataset_dict.values())
|
|
144
|
+
|
|
145
|
+
# client = distributed_client()
|
|
146
|
+
# if client is None and npartitions is None:
|
|
147
|
+
# npartitions=1
|
|
148
|
+
# else:
|
|
149
|
+
# npartitions=len(client.scheduler_info()["workers"])
|
|
150
|
+
# da_list = dask.persist([x.da for x in dataset_list])[0]
|
|
151
|
+
# progress(da_list)
|
|
152
|
+
# for i, dataset in enumerate(dataset_list):
|
|
153
|
+
# dataset.da = da_list[i]
|
|
154
|
+
df = SpatialAggregator(
|
|
155
|
+
dataset_list, weights, names=list(dataset_dict.keys()),
|
|
156
|
+
).compute(npartitions=npartitions)
|
|
157
|
+
return df
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def aggregate_dataset(
|
|
161
|
+
weights: GridWeights,
|
|
162
|
+
dataset: Dataset = None,
|
|
163
|
+
aggregator_dict: Dict[str, Union[List[Tuple], TemporalAggregator]] = None,
|
|
164
|
+
dataset_dict = None,
|
|
165
|
+
n_workers = 50,
|
|
166
|
+
threads_per_worker = 1,
|
|
167
|
+
processes = True,
|
|
168
|
+
memory_limit=None,
|
|
169
|
+
cluster_args = {},
|
|
170
|
+
**kwargs,
|
|
171
|
+
) -> pd.DataFrame:
|
|
172
|
+
"""
|
|
173
|
+
Aggregate a dataset over time and space.
|
|
174
|
+
|
|
175
|
+
Args:
|
|
176
|
+
dataset (Dataset): The dataset to aggregate.
|
|
177
|
+
weights (GridWeights): The weights to use for aggregation.
|
|
178
|
+
agg_dict (dict): A dictionary containing the arguments for creating TemporalAggregator objects.
|
|
179
|
+
The keys of the dictionary are names, and the values are a list of either tuples or TemporalAggregator objects.
|
|
180
|
+
If the list contains tuples, use them as arguments to instantiate a temporal aggregator.
|
|
181
|
+
|
|
182
|
+
Returns:
|
|
183
|
+
df: A dataframe containing the aggregated data.
|
|
184
|
+
"""
|
|
185
|
+
|
|
186
|
+
if dataset is None:
|
|
187
|
+
raise ValueError("No dataset provided.")
|
|
188
|
+
|
|
189
|
+
if aggregator_dict is None and kwargs is not None:
|
|
190
|
+
aggregator_dict = kwargs
|
|
191
|
+
|
|
192
|
+
if aggregator_dict is not None:
|
|
193
|
+
dataset_dict = aggregate_time(dataset, weights, aggregator_dict)
|
|
194
|
+
elif dataset_dict is None:
|
|
195
|
+
dataset_dict = {"variable": dataset}
|
|
196
|
+
if dataset_dict is None and dataset is None:
|
|
197
|
+
raise ValueError("No aggregator dict or dataset dict provided.")
|
|
198
|
+
|
|
199
|
+
df = aggregate_space(dataset_dict, weights)
|
|
200
|
+
df = (
|
|
201
|
+
weights.georegions.shp[[weights.georegions.regionid]].merge(
|
|
202
|
+
df, left_index=True, right_on="region_id"
|
|
203
|
+
)
|
|
204
|
+
).drop(columns="region_id")
|
|
205
|
+
|
|
206
|
+
# client.shutdown()
|
|
207
|
+
|
|
208
|
+
# _ = shutdown_dask_client()
|
|
209
|
+
|
|
210
|
+
return df
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def multi_dd_to_dict(data, key, ddargs):
|
|
214
|
+
"""
|
|
215
|
+
Converts a multi-variable list of datasets to a dictionary with keys
|
|
216
|
+
generated from the given key and ddargs.
|
|
217
|
+
|
|
218
|
+
Args:
|
|
219
|
+
data (list): The list of Datasets to convert to a dictionary.
|
|
220
|
+
key (str): The base key to use for generating the dictionary keys.
|
|
221
|
+
ddargs (list): A list of tuples representing the dimensions of the array.
|
|
222
|
+
|
|
223
|
+
Returns:
|
|
224
|
+
dict: A dictionary with keys generated from the given key and ddargs, and values from the list of datasets.
|
|
225
|
+
"""
|
|
226
|
+
keys = [f"{key}_{x[0]}_{x[1]}" for x in ddargs]
|
|
227
|
+
# data_dict = dict(zip(keys, data))
|
|
228
|
+
return data, keys
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import dask.distributed
|
|
2
|
+
from dask.distributed import Client
|
|
3
|
+
import logging
|
|
4
|
+
|
|
5
|
+
def is_distributed():
|
|
6
|
+
"""
|
|
7
|
+
Returns True if the code is running in a distributed environment, False otherwise.
|
|
8
|
+
|
|
9
|
+
This function checks if the code is running in a distributed environment by attempting to get the global Dask client.
|
|
10
|
+
If the client is not None, it means that the code is running in a distributed environment.
|
|
11
|
+
|
|
12
|
+
Returns:
|
|
13
|
+
bool: True if the code is running in a distributed environment, False otherwise.
|
|
14
|
+
"""
|
|
15
|
+
client = dask.distributed.client._get_global_client()
|
|
16
|
+
if client is not None:
|
|
17
|
+
return True
|
|
18
|
+
else:
|
|
19
|
+
return False
|
|
20
|
+
|
|
21
|
+
def distributed_client():
|
|
22
|
+
"""
|
|
23
|
+
Returns the global Dask distributed client object.
|
|
24
|
+
|
|
25
|
+
Returns:
|
|
26
|
+
--------
|
|
27
|
+
client : dask.distributed.Client
|
|
28
|
+
The global Dask distributed client object.
|
|
29
|
+
"""
|
|
30
|
+
client = dask.distributed.client._get_global_client()
|
|
31
|
+
return client
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def start_dask_client(n_workers: int = 2, threads_per_worker: int = 2, **kwargs):
|
|
35
|
+
"""
|
|
36
|
+
Start a dask distributed cluster.
|
|
37
|
+
|
|
38
|
+
Args:
|
|
39
|
+
n_workers (int, optional): The number of workers to use. Defaults to 2.
|
|
40
|
+
threads_per_worker (int, optional): The number of threads per worker. Defaults to 2.
|
|
41
|
+
**kwargs: Additional keyword arguments to pass to the dask Client constructor.
|
|
42
|
+
|
|
43
|
+
Returns:
|
|
44
|
+
client: A dask distributed client.
|
|
45
|
+
"""
|
|
46
|
+
client = Client(
|
|
47
|
+
n_workers=n_workers,
|
|
48
|
+
threads_per_worker=threads_per_worker,
|
|
49
|
+
**kwargs
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
arg_dict = {
|
|
53
|
+
"n_workers": n_workers,
|
|
54
|
+
"threads_per_worker": threads_per_worker,
|
|
55
|
+
}
|
|
56
|
+
all_dict = {**arg_dict, **kwargs}
|
|
57
|
+
for k in all_dict.keys():
|
|
58
|
+
client.set_metadata(['args', k], all_dict[k])
|
|
59
|
+
|
|
60
|
+
return client
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def shutdown_dask_client():
|
|
64
|
+
"""
|
|
65
|
+
Shutdown the global Dask distributed client object.
|
|
66
|
+
"""
|
|
67
|
+
client = dask.distributed.client._get_global_client()
|
|
68
|
+
if client is not None:
|
|
69
|
+
try:
|
|
70
|
+
args = client.get_metadata('args')
|
|
71
|
+
except:
|
|
72
|
+
raise ValueError("Please start Dask client with af.start_dask_client() or run weight calculation without Dask distributed client.")
|
|
73
|
+
client.shutdown()
|
|
74
|
+
return args
|
|
75
|
+
else:
|
|
76
|
+
return None
|