Skip to content

Commit 9cfd49a

Browse files
authored
Merge pull request #19 from brews/template_refactor
Refactor *workflow to *template, in its various forms
2 parents 9a348a5 + 56c9682 commit 9cfd49a

7 files changed

Lines changed: 58 additions & 53 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515

1616
### Changed
1717

18+
- BREAKING: `isku.ExtractionWorkflow` is now `isku.ExtractionTemplate`.
19+
- BREAKING: `isku.ProjectionWorkflow` is now `isku.ProjectionTemplate`.
20+
- BREAKING: `isku.build_extraction_workflow` is now `isku.build_extraction_template`.
21+
- BREAKING: `isku.build_projection_workflow` is now `isku.build_projection_template`.
22+
- BREAKING: In `isku.extract_regions`, the `workflow` argument is now `template`.
1823
- Improved README.
1924
- Improved CONTRIBUTING.
2025
- Add project URLs to package metadata.

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def _postprocess(ds):
6161
return ds[["impact"]] + 10
6262

6363

64-
test_impact_model = isku.build_projection_workflow(
64+
test_impact_model = isku.build_projection_template(
6565
pre=_preprocess,
6666
project=_linear_impact_model,
6767
post=_postprocess,
@@ -71,7 +71,7 @@ test_impact_model = isku.build_projection_workflow(
7171
projected = isku.project(input_data, model=test_impact_model)
7272
```
7373

74-
This example uses pure functions to define workflow steps. This can be useful for quick analysis but `isku` also accepts
74+
This example uses pure functions to define workflow, or template, steps. This can be useful for quick analysis but `isku` also accepts
7575
custom objects adhering to the select protocols. The intent is that components can be quickly used, ignored, extended or
7676
replaced as needed by a project.
7777

@@ -126,7 +126,7 @@ def _add_ten(ds):
126126
return ds[["variable1"]] + 10
127127

128128

129-
my_extraction_workflow = isku.build_extraction_workflow(
129+
my_extraction_workflow = isku.build_extraction_template(
130130
pre=_add_one, # Before regionalization.
131131
post=_add_ten, # After regionalization.
132132
)
@@ -135,7 +135,7 @@ my_extraction_workflow = isku.build_extraction_workflow(
135135
# Put it all together to extract regions from gridded data.
136136
transformed = isku.extract_regions(
137137
gridded_data,
138-
workflow=my_extraction_workflow,
138+
template=my_extraction_workflow,
139139
regions=my_regions,
140140
)
141141
```

docs/user_guide.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def _postprocess(ds):
3939
return ds[["impact"]] + 10
4040

4141

42-
test_impact_model = isku.build_projection_workflow(
42+
test_impact_model = isku.build_projection_template(
4343
pre=_preprocess,
4444
project=_linear_impact_model,
4545
post=_postprocess,
@@ -49,7 +49,7 @@ test_impact_model = isku.build_projection_workflow(
4949
projected = isku.project(input_data, model=test_impact_model)
5050
```
5151

52-
This example uses pure functions to define workflow steps. This can be useful for quick analysis but `isku` also accepts
52+
This example uses pure functions to define workflow, or "template", steps. This can be useful for quick analysis but `isku` also accepts
5353
custom objects adhering to the select protocols. The intent is that components can be quickly used, ignored, extended or
5454
replaced as needed by a project.
5555

@@ -104,7 +104,7 @@ def _add_ten(ds):
104104
return ds[["variable1"]] + 10
105105

106106

107-
my_extraction_workflow = isku.build_extraction_workflow(
107+
my_extraction_workflow = isku.build_extraction_template(
108108
pre=_add_one, # Before regionalization.
109109
post=_add_ten, # After regionalization.
110110
)
@@ -113,7 +113,7 @@ my_extraction_workflow = isku.build_extraction_workflow(
113113
# Put it all together to extract regions from gridded data.
114114
transformed = isku.extract_regions(
115115
gridded_data,
116-
workflow=my_extraction_workflow,
116+
template=my_extraction_workflow,
117117
regions=my_regions,
118118
)
119119
```

src/isku/__init__.py

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@
55
import xarray as xr
66

77
__all__ = [
8-
"ExtractionWorkflow",
8+
"ExtractionTemplate",
99
"GridWeightingRegions",
10-
"ProjectionWorkflow",
10+
"ProjectionTemplate",
1111
"RegionExtractor",
12-
"build_extraction_workflow",
13-
"build_projection_workflow",
12+
"build_extraction_template",
13+
"build_projection_template",
1414
"extract_regions",
1515
"project",
1616
]
1717

1818

19-
class ExtractionWorkflow(Protocol):
19+
class ExtractionTemplate(Protocol):
2020
"""
2121
Template for pre and post region extraction transformation
2222
2323
See Also
2424
--------
25-
build_extraction_workflow: Quickly build extraction workflow from functions for regionalization with pre/post transformations.
26-
extract_regions: Apply a workflow to extract a new regionalized dataset from gridded data.
25+
build_extraction_template: Quickly build extraction template from functions for regionalization with pre/post transformations.
26+
extract_regions: Apply a template to extract a new regionalized dataset from gridded data.
2727
RegionExtractor: Protocol for regionalizing, or extracting regions from a dataset.
2828
"""
2929

@@ -46,8 +46,8 @@ class RegionExtractor(Protocol):
4646
4747
See Also
4848
--------
49-
extract_regions: Apply a workflow to extract a new regionalized dataset from gridded data with pre/post transformations.
50-
ExtractionWorkflow: Technical protocol for a workflow with pre/post regionalization transformations.
49+
extract_regions: Apply a template to extract a new regionalized dataset from gridded data with pre/post transformations.
50+
ExtractionTemplate: Technical protocol for a workflow with pre/post regionalization transformations.
5151
"""
5252

5353
def extract_regions(self, ds: xr.Dataset) -> xr.Dataset:
@@ -59,18 +59,18 @@ def extract_regions(self, ds: xr.Dataset) -> xr.Dataset:
5959

6060
# This dataclass is a quick and simple way to get a concrete instance of the protocol.
6161
@dataclass(frozen=True)
62-
class _SimpleExtractionWorkflow(ExtractionWorkflow):
62+
class _SimpleExtractionTemplate(ExtractionTemplate):
6363
pre_extract: Callable[[xr.Dataset], xr.Dataset]
6464
post_extract: Callable[[xr.Dataset], xr.Dataset]
6565

6666

67-
def build_extraction_workflow(
67+
def build_extraction_template(
6868
*, pre: Callable[[xr.Dataset], xr.Dataset], post: Callable[[xr.Dataset], xr.Dataset]
69-
) -> ExtractionWorkflow:
69+
) -> ExtractionTemplate:
7070
"""
71-
Build a workflow of tranformation steps applied to input gridded data, pre/post regionalization, to create a derived variable as output
71+
Build a template of tranformation steps applied to input gridded data, pre/post regionalization, to create a derived variable as output
7272
73-
This function is a quick and simple way to build an ExtractionWorkflow from two simple functions.
73+
This function is a quick and simple way to build an ExtractionTemplate from two simple functions.
7474
7575
These steps should be general. They may contain logic for sanity checks
7676
on inputs and outputs, calculating derived variables and climate indices,
@@ -81,11 +81,11 @@ def build_extraction_workflow(
8181
8282
See Also
8383
--------
84-
extract_regions: Apply a workflow to extract a new regionalized dataset from gridded data.
85-
build_extraction_workflow: Quickly build extraction workflow from functions for regionalization.
86-
ExtractionWorkflow: The underlaying protocol for a workflow that extracts a regionalized dataset.
84+
extract_regions: Apply a template to extract a new regionalized dataset from gridded data.
85+
build_extraction_template: Quickly build extraction template from functions for regionalization.
86+
ExtractionTemplate: The underlaying protocol for a workflow that extracts a regionalized dataset.
8787
"""
88-
return _SimpleExtractionWorkflow(pre_extract=pre, post_extract=post)
88+
return _SimpleExtractionTemplate(pre_extract=pre, post_extract=post)
8989

9090

9191
# Use class for segment weights because we're making assumptions/enforcements about the weight data's content and interactions...
@@ -102,8 +102,8 @@ class GridWeightingRegions(RegionExtractor):
102102
103103
See Also
104104
--------
105-
extract_regions: Use SegmentWeights in a workflow to extract new regionalized dataset.
106-
build_extraction_workflow: Quickly build extraction workflow from functions for regionalization.
105+
extract_regions: Extract new regionalized dataset.
106+
build_extraction_template: Quickly build extraction template from functions for regionalization.
107107
RegionExtractor: Protocol for regionalizing, or extracting regions from a dataset.
108108
"""
109109

@@ -133,27 +133,27 @@ def extract_regions(self, ds: xr.Dataset) -> xr.Dataset:
133133

134134

135135
def extract_regions(
136-
ds: xr.Dataset, *, workflow: ExtractionWorkflow, regions: RegionExtractor
136+
ds: xr.Dataset, *, template: ExtractionTemplate, regions: RegionExtractor
137137
) -> xr.Dataset:
138138
"""
139-
Use transformations in 'workflow' to extract 'regions' from gridded dataset, 'ds', returning a regionalized dataset
139+
Use transformations in 'template' to extract 'regions' from gridded dataset, 'ds', returning a regionalized dataset
140140
141-
This function specifically does not just regionalize through zonal aggregation. It uses 'workflow' to apply pre/post regionalization transformations to create new datasets and variables.
141+
This function specifically does not just regionalize through zonal aggregation. It uses 'template' to apply pre/post regionalization transformations to create new datasets and variables.
142142
143143
See Also
144144
--------
145-
build_extraction_workflow: Quickly build extraction workflow from functions for regionalization.
145+
build_extraction_template: Quickly build extraction workflow from functions for regionalization.
146146
"""
147-
return workflow.post_extract(regions.extract_regions(workflow.pre_extract(ds)))
147+
return template.post_extract(regions.extract_regions(template.pre_extract(ds)))
148148

149149

150-
class ProjectionWorkflow(Protocol):
150+
class ProjectionTemplate(Protocol):
151151
"""
152152
Template for projecting a model with pre and post processing.
153153
154154
See Also
155155
--------
156-
build_projection_workflow: Build a projection workflow from simple functions.
156+
build_projection_template: Build a projection template from simple functions.
157157
"""
158158

159159
def pre_project(self, d: xr.Dataset) -> xr.Dataset:
@@ -177,43 +177,43 @@ def post_project(self, d: xr.Dataset) -> xr.Dataset:
177177

178178
# This dataclass is a quick and simple way to get a concrete instance of the protocol.
179179
@dataclass(frozen=True)
180-
class _SimpleProjectionWorkflow(ProjectionWorkflow):
180+
class _SimpleProjectionTemplate(ProjectionTemplate):
181181
pre_project: Callable[[xr.Dataset], xr.Dataset]
182182
project: Callable[[xr.Dataset], xr.Dataset]
183183
post_project: Callable[[xr.Dataset], xr.Dataset]
184184

185185

186-
def build_projection_workflow(
186+
def build_projection_template(
187187
*,
188188
pre: Callable[[xr.Dataset], xr.Dataset],
189189
project: Callable[[xr.Dataset], xr.Dataset],
190190
post: Callable[[xr.Dataset], xr.Dataset],
191-
) -> ProjectionWorkflow:
191+
) -> ProjectionTemplate:
192192
"""
193193
Use simple functions to quickly build a model to project effects, impacts and/or damages.
194194
195-
This function is a quick and simple way to build an ProjectionWorkflow from three simple functions.
195+
This function is a quick and simple way to build an ProjectionTemplate from three simple functions.
196196
197197
See Also
198198
--------
199-
project: Apply a projection workflow to a dataset.
200-
ProjectionWorkflow: Technical ProjectionWorkflow protocol.
199+
project: Apply a projection template to a dataset.
200+
ProjectionTemplate: Technical ProjectionTemplate protocol.
201201
"""
202-
return _SimpleProjectionWorkflow(
202+
return _SimpleProjectionTemplate(
203203
pre_project=pre,
204204
project=project,
205205
post_project=post,
206206
)
207207

208208

209-
def project(d: xr.Dataset, *, model: ProjectionWorkflow) -> xr.Dataset:
209+
def project(d: xr.Dataset, *, model: ProjectionTemplate) -> xr.Dataset:
210210
"""
211211
Project a dataset of predictors, 'd', with 'model' to return a projected dataset
212212
213213
See Also
214214
--------
215-
build_projection_workflow: Build a projection workflow from simple functions.
216-
ProjectionWorkflow: Technical ProjectionWorkflow protocol.
215+
build_projection_template: Build a projection template from simple functions.
216+
ProjectionTemplate: Technical ProjectionTemplate protocol.
217217
"""
218218
preprocessed = model.pre_project(d)
219219
projected = model.project(preprocessed)

tests/smoke_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
def test_basic_projection():
1111
"""
12-
Basic test running build_projection_workflow() with project().
12+
Basic test running build_projection_template() with project().
1313
"""
1414
predictors = xr.Dataset({"foobar": (["idx"], [0, 0, 0])})
1515
params = xr.Dataset({"ni": (["idx"], [1, 2, 3])})
@@ -27,7 +27,7 @@ def _post(x):
2727
def _model(x):
2828
return (x["foobar"] * 2 + x["ni"]).to_dataset(name="impact")
2929

30-
test_impact_model = isku.build_projection_workflow(
30+
test_impact_model = isku.build_projection_template(
3131
pre=_pre,
3232
project=_model,
3333
post=_post,

tests/test_extraction.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
def test_extract_regions():
1212
"""
13-
Create simple extraction workflow and test basic region extraction.
13+
Create simple extraction template and test basic region extraction.
1414
"""
1515
input_ds = xr.Dataset({"variable1": (["idx"], [0, 0, 0])})
1616
expected = xr.Dataset({"variable1": (["idx"], [13.5, 13.5, 13.5])})
@@ -23,7 +23,7 @@ def _pre(x):
2323
def _post(x):
2424
return x[["variable1"]] + 10
2525

26-
test_transform = isku.build_extraction_workflow(pre=_pre, post=_post)
26+
test_transform = isku.build_extraction_template(pre=_pre, post=_post)
2727

2828
class FakeRegionalization(isku.RegionExtractor):
2929
"""
@@ -35,7 +35,7 @@ def extract_regions(self, ds):
3535

3636
output = isku.extract_regions(
3737
input_ds,
38-
workflow=test_transform,
38+
template=test_transform,
3939
regions=FakeRegionalization(),
4040
)
4141

tests/test_projection.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
import xarray as xr
66

7-
from isku import build_projection_workflow, project
7+
from isku import build_projection_template, project
88

99

1010
def test_basic_projection():
1111
"""
12-
Basic test running build_projection_workflow() with project().
12+
Basic test running build_projection_template() with project().
1313
"""
1414
predictors = xr.Dataset({"foobar": (["idx"], [0, 0, 0])})
1515
params = xr.Dataset({"ni": (["idx"], [1, 2, 3])})
@@ -27,7 +27,7 @@ def _post(x):
2727
def _model(x):
2828
return (x["foobar"] * 2 + x["ni"]).to_dataset(name="impact")
2929

30-
test_impact_model = build_projection_workflow(
30+
test_impact_model = build_projection_template(
3131
pre=_pre,
3232
project=_model,
3333
post=_post,

0 commit comments

Comments
 (0)