-
Notifications
You must be signed in to change notification settings - Fork 3
Map driven demand parsing #56
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e99b396
Add new mapping file for demand data
dylanjmcconnell d03195b
Update demand yaml to correctly map IASR names
dylanjmcconnell 0b1e430
Deleted old scenario mapping
dylanjmcconnell f9d4013
Completely removed old regex extractors
dylanjmcconnell 7bb55b3
Added tests for demand_trace_metadata
dylanjmcconnell 1ab9192
Remove old regex extrator method from demand_traces (and use metadata…
dylanjmcconnell cddc83e
Updated docstring for restructure_demand_file
dylanjmcconnell e6b4fca
Minor edit to doc str
dylanjmcconnell 5e7e280
Minor change to doc string
dylanjmcconnell cec23b8
Added demand_trace_metadata.py - similar to resource_trace_metadata.py
dylanjmcconnell efbdbb2
Simplify and tighten demand filename parsing
dylanjmcconnell 126bb9e
clarified name (renamed year--> refyear)
dylanjmcconnell 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 hidden or 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,53 @@ | ||
| from pathlib import Path | ||
|
|
||
| from isp_trace_parser import mappings | ||
|
|
||
|
|
||
| def build( | ||
| files: list[Path], | ||
| version: str, | ||
| ) -> dict[Path, dict[str, str | int]]: | ||
| """Build metadata for demand files by lookup in the demand mapping. | ||
|
|
||
| The demand YAML is option-keyed, so `_expand_lookup` first expands the | ||
| dimensions into a `(location_prefix, dimensions_suffix)`-keyed dict; | ||
| each filename then decomposes into those two literal slices (either | ||
| side of `_RefYear_<refyear>_`) for a single lookup. | ||
| """ | ||
| lookup = _expand_lookup(version) | ||
|
|
||
| file_metadata: dict[Path, dict[str, str | int]] = {} | ||
| for path in files: | ||
| location_prefix, _, after = path.stem.partition("_RefYear_") | ||
| refyear, _, dimensions_suffix = after.partition("_") | ||
| key = (location_prefix, dimensions_suffix) | ||
| if not refyear.isdigit() or key not in lookup: | ||
| raise ValueError(f"Unexpected trace filename: {path.name}") | ||
| file_metadata[path] = {**lookup[key], "reference_year": int(refyear)} | ||
| return file_metadata | ||
|
|
||
|
|
||
| def _expand_lookup(version: str) -> dict[tuple[str, str], dict[str, str]]: | ||
| """Expand the demand dimensions into a refyear-agnostic lookup. | ||
|
|
||
| Keyed by `(location_prefix, dimensions_suffix)` — the two literal | ||
| slices of the filename either side of `_RefYear_<refyear>_`. For 2024, | ||
| `location_prefix` is the subregion and `dimensions_suffix` is | ||
| `<scenario>_<poe>_<demand_type>`. `reference_year` is added by `build`. | ||
| """ | ||
| demand = mappings.load("demand", version=version) | ||
| topography = mappings.load("topography", version=version) | ||
|
|
||
| lookup: dict[tuple[str, str], dict[str, str]] = {} | ||
| for subregion in topography["subregions"]: | ||
| for scenario in demand["scenarios"]: | ||
| for poe in demand["poe_levels"]: | ||
| for demand_type in demand["demand_types"]: | ||
| key = (subregion, f"{scenario}_{poe}_{demand_type}") | ||
| lookup[key] = { | ||
| "subregion": subregion, | ||
| "scenario": scenario, | ||
| "poe": poe, | ||
| "demand_type": demand_type, | ||
| } | ||
| return lookup | ||
This file contains hidden or 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 hidden or 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,21 @@ | ||
| # 2024 ISP demand trace metadata | ||
| #(based on the 2024 traces and IASR workbook). | ||
| # | ||
| # 2024 AEMO demand trace filenames take the form: | ||
| # <subregion>_RefYear_<year>_<scenario>_<poe>_<demand_type>.csv | ||
| # e.g. CNSW_RefYear_2011_HYDROGEN_EXPORT_POE10_OPSO_MODELLING.csv | ||
| # | ||
| # Valid filenames are the product of these dimensions (with subregion, | ||
| # sourced from topography.yaml, and reference_year). | ||
|
|
||
| # scenarios: raw AEMO filename code -> IASR workbook display name. | ||
| # Keys appear in the filename; values are used in output filenames and | ||
| # in the `scenario` column of the parsed parquet. | ||
| scenarios: | ||
| STEP_CHANGE: Step Change | ||
| PROGRESSIVE_CHANGE: Progressive Change | ||
| HYDROGEN_EXPORT: Green Energy Exports | ||
|
|
||
| poe_levels: [POE10, POE50] | ||
|
|
||
| demand_types: [OPSO_MODELLING, OPSO_MODELLING_PVLITE, PV_TOT] |
3 changes: 0 additions & 3 deletions
3
src/isp_trace_parser/mappings/2024/demand_scenario_mapping.yaml
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,45 @@ | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| from isp_trace_parser import demand_trace_metadata | ||
|
|
||
|
|
||
| def test_build(): | ||
| """Two examples spanning different scenario / poe / demand_type / | ||
| subregion values. Every combination resolves through the same single | ||
| dict lookup, so two are enough for testing. | ||
| """ | ||
| files = [ | ||
| Path("VIC_RefYear_2011_STEP_CHANGE_POE10_OPSO_MODELLING.csv"), | ||
| Path("CNSW_RefYear_2023_HYDROGEN_EXPORT_POE50_OPSO_MODELLING_PVLITE.csv"), | ||
| ] | ||
| metadata = demand_trace_metadata.build(files, version="2024") | ||
|
|
||
| assert metadata[files[0]] == { | ||
| "subregion": "VIC", | ||
| "reference_year": 2011, | ||
| "scenario": "STEP_CHANGE", | ||
| "poe": "POE10", | ||
| "demand_type": "OPSO_MODELLING", | ||
| } | ||
| assert metadata[files[1]] == { | ||
| "subregion": "CNSW", | ||
| "reference_year": 2023, | ||
| "scenario": "HYDROGEN_EXPORT", | ||
| "poe": "POE50", | ||
| "demand_type": "OPSO_MODELLING_PVLITE", | ||
| } | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "filename", | ||
| [ | ||
| "VIC_2011_STEP_CHANGE_POE10_OPSO_MODELLING.csv", # missing _RefYear_ | ||
| "VIC_RefYear_201a_STEP_CHANGE_POE10_OPSO_MODELLING.csv", # non-digit year | ||
| "VIC_RefYear_2011_MYSTERY_POE10_OPSO_MODELLING.csv", # lookup miss | ||
| ], | ||
| ) | ||
| def test_build_rejects_unexpected_filename(filename): | ||
| with pytest.raises(ValueError, match="Unexpected trace filename"): | ||
| demand_trace_metadata.build([Path(filename)], version="2024") |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.
I actually found the code in the for loop pretty hard to understand. This is total overkill, but I was curious on how it might be made clearer, so here's what Claude and I came up with. Please, just treat as a comment for you to take or leave as you please.
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.
This also might be clearer. Anyway, I'll stop now.
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.
Thanks Nick - yeah think you are right .. will make some changes (..probably what you've suggested)