Skip to content

Commit a4630af

Browse files
committed
geospatial analysis use auto_template
1 parent 0ef212c commit a4630af

9 files changed

+115
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pvdeg.geospatial.plot\_sparse\_analysis\_land
2+
=============================================
3+
4+
.. currentmodule:: pvdeg.geospatial
5+
6+
.. autofunction:: plot_sparse_analysis_land

docs/source/_autosummary/pvdeg.geospatial.rst

+8
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pvdeg.geospatial
3434
pvdeg.geospatial.plot_Europe
3535
pvdeg.geospatial.plot_USA
3636
pvdeg.geospatial.plot_sparse_analysis
37+
pvdeg.geospatial.plot_sparse_analysis_land
3738
pvdeg.geospatial.start_dask
3839
pvdeg.geospatial.template_parameters
3940
pvdeg.geospatial.zero_template
@@ -157,6 +158,13 @@ pvdeg.geospatial
157158
.. minigallery:: pvdeg.geospatial.plot_sparse_analysis
158159
:add-heading:
159160

161+
.. autofunction:: plot_sparse_analysis_land
162+
163+
.. _sphx_glr_backref_pvdeg.geospatial.plot_sparse_analysis_land:
164+
165+
.. minigallery:: pvdeg.geospatial.plot_sparse_analysis_land
166+
:add-heading:
167+
160168
.. autofunction:: start_dask
161169

162170
.. _sphx_glr_backref_pvdeg.geospatial.start_dask:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pvdeg.utilities.linear\_normalize
2+
=================================
3+
4+
.. currentmodule:: pvdeg.utilities
5+
6+
.. autofunction:: linear_normalize
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pvdeg.utilities.merge\_sparse
2+
=============================
3+
4+
.. currentmodule:: pvdeg.utilities
5+
6+
.. autofunction:: merge_sparse

docs/source/_autosummary/pvdeg.utilities.rst

+16
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ pvdeg.utilities
2626
pvdeg.utilities.get_kinetics
2727
pvdeg.utilities.get_state_bbox
2828
pvdeg.utilities.gid_downsampling
29+
pvdeg.utilities.linear_normalize
30+
pvdeg.utilities.merge_sparse
2931
pvdeg.utilities.meta_as_dict
3032
pvdeg.utilities.new_id
3133
pvdeg.utilities.nrel_kestrel_check
@@ -100,6 +102,20 @@ pvdeg.utilities
100102
.. minigallery:: pvdeg.utilities.gid_downsampling
101103
:add-heading:
102104

105+
.. autofunction:: linear_normalize
106+
107+
.. _sphx_glr_backref_pvdeg.utilities.linear_normalize:
108+
109+
.. minigallery:: pvdeg.utilities.linear_normalize
110+
:add-heading:
111+
112+
.. autofunction:: merge_sparse
113+
114+
.. _sphx_glr_backref_pvdeg.utilities.merge_sparse:
115+
116+
.. minigallery:: pvdeg.utilities.merge_sparse
117+
:add-heading:
118+
103119
.. autofunction:: meta_as_dict
104120

105121
.. _sphx_glr_backref_pvdeg.utilities.meta_as_dict:

docs/source/whatsnew/index.rst

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ What's New
55
PVDegradationTools (pvdeg) change log:
66

77

8+
.. include:: releases/v0.4.3.rst
9+
.. include:: releases/v0.4.2.rst
810
.. include:: releases/v0.4.1.rst
911
.. include:: releases/v0.4.0.rst
1012
.. include:: releases/v0.3.3.rst

docs/source/whatsnew/releases/v0.4.2.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ v0.4.2 (2024-09-13)
44

55
Bug Fixes
66
---------
7-
* Remove duplicate gid's from `pvdeg.geospatial.elevation_stochastic_downselection`
7+
* Remove duplicate gid's from ``pvdeg.geospatial.elevation_stochastic_downselection``
88

99
Tests
1010
-----
11-
* Added a test for Xmin in `test_standards.py` and removed dependency on pvgis.
11+
* Added a test for Xmin in ``test_standards.py`` and removed dependency on pvgis.
1212

1313
Contributors
1414
~~~~~~~~~~~~
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
v0.4.3 (2024-11-1)
2+
=======================
3+
4+
Enhancements
5+
-------------
6+
``pvdeg.geospatial.analysis`` implements autotemplating. No need to specify a template for common ``pvdeg`` functions during analysis. Manually creating and providing templates is still an option. Docstrings updated with examples.
7+
8+
Contributors
9+
~~~~~~~~~~~~
10+
* Tobin Ford (:ghuser:`tobin-ford`)

pvdeg/geospatial.py

+59-12
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,12 @@ def calc_block(weather_ds_block, future_meta_df, func, func_kwargs):
190190

191191
def analysis(weather_ds, meta_df, func, template=None, **func_kwargs):
192192
"""
193-
Applies a function to each gid of a weather dataset.
193+
Applies a function to each gid of a weather dataset. `analysis` will attempt to create a template using `geospatial.auto_template`.
194+
If this process fails you will have to provide a geospatial template to the template argument.
195+
196+
ValueError: <function-name> cannot be autotemplated. create a template manually with `geospatial.output_template`
197+
198+
194199
195200
Parameters
196201
----------
@@ -212,8 +217,10 @@ def analysis(weather_ds, meta_df, func, template=None, **func_kwargs):
212217
"""
213218

214219
if template is None:
215-
param = template_parameters(func)
216-
template = output_template(weather_ds, **param)
220+
template = auto_template(
221+
func=func,
222+
ds_gids=weather_ds
223+
)
217224

218225
# future_meta_df = client.scatter(meta_df)
219226
kwargs = {"func": func, "future_meta_df": meta_df, "func_kwargs": func_kwargs}
@@ -241,14 +248,43 @@ def analysis(weather_ds, meta_df, func, template=None, **func_kwargs):
241248

242249

243250
def output_template(
244-
ds_gids, shapes, attrs=dict(), global_attrs=dict(), add_dims=dict()
251+
ds_gids: xr.Dataset,
252+
shapes: dict,
253+
attrs=dict(),
254+
global_attrs=dict(),
255+
add_dims=dict()
245256
):
246257
"""
247258
Generates a xarray template for output data. Output variables and
248259
associated dimensions need to be specified via the shapes dictionary.
249260
The dimension length are derived from the input data. Additonal output
250261
dimensions can be defined with the add_dims argument.
251262
263+
Examples
264+
--------
265+
Providing the shapes dictionary can be confusing. Here is what the `shapes` dictionary should look like for `pvdeg.standards.standoff`.
266+
Refer to the docstring, the function will have one result per location so the only dimension for each return value is "gid", a geospatial ID number.
267+
268+
.. code-block:: python
269+
shapes = {
270+
"x": ("gid",),
271+
"T98_inf": ("gid",),
272+
"T98_0": ("gid",),
273+
}
274+
275+
**Note: The dimensions are stored in a tuple, this this why all of the parenthesis have commas after the single string, otherwise python will interpret the value as a string.**
276+
277+
This is what the shapes dictinoary should look like for `pvdeg.humidity.module`. Refering to the docstring,
278+
we can see that the function will return a timeseries result for each location. This means we need dimensions of "gid" and "time".
279+
280+
.. code-block:: python
281+
shapes = {
282+
"RH_surface_outside": ("gid", "time"),
283+
"RH_front_encap": ("gid", "time"),
284+
"RH_back_encap": ("gid", "time"),
285+
"RH_backsheet": ("gid", "time"),
286+
}
287+
252288
Parameters
253289
----------
254290
ds_gids : xarray.Dataset
@@ -290,9 +326,8 @@ def output_template(
290326
return output_template
291327

292328

293-
# we should be able to get rid of this with the new autotemplating function and decorator
294-
# this is helpful for users so we should move it to a section in the documenation,
295-
# discuss with group
329+
# This has been replaced with pvdeg.geospatial.auto_templates inside of pvdeg.geospatial.analysis.
330+
# it is here for completeness. it can be removed.
296331
def template_parameters(func):
297332
"""
298333
Output parameters for xarray template.
@@ -417,11 +452,10 @@ def auto_template(func: Callable, ds_gids: xr.Dataset) -> xr.Dataset:
417452
Only works on functions that have the `numeric_or_timeseries` and `shape_names` attributes.
418453
These attributes are assigned at function definition with the `@geospatial_quick_shape` decorator.
419454
420-
Otherwise you will have to create your own template.
421-
Don't worry, this is easy. See the Geospatial Templates Notebook
422-
for more information.
455+
Otherwise you will have to create your own template using `geospatial.output_template`.
456+
See the Geospatial Templates Notebook for more information.
423457
424-
examples:
458+
Examples
425459
---------
426460
427461
the function returns a numeric value
@@ -430,11 +464,24 @@ def auto_template(func: Callable, ds_gids: xr.Dataset) -> xr.Dataset:
430464
the function returns a timeseries result
431465
>>> pvdeg.module.humidity
432466
433-
counter example:
467+
Counter example:
434468
----------------
435469
the function could either return a single numeric or a series based on changed in
436470
the input. Because it does not have a known result shape we cannot determine the
437471
attributes required for autotemplating ahead of time.
472+
473+
Parameters
474+
----------
475+
func: callable
476+
function to create template from. This will raise an error if the function was not declared with the `@geospatial_quick_shape` decorator.
477+
ds_gids : xarray.Dataset
478+
Dataset containing the gids and their associated dimensions. (geospatial weather dataset)
479+
Dataset should already be chunked.
480+
481+
Returns
482+
-------
483+
output_template : xarray.Dataset
484+
Template for output data.
438485
"""
439486

440487
if not (hasattr(func, "numeric_or_timeseries") and hasattr(func, "shape_names")):

0 commit comments

Comments
 (0)