-
Notifications
You must be signed in to change notification settings - Fork 6
Fix new entrant renaming #144
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
Open
EllieKallmier
wants to merge
1
commit into
main
Choose a base branch
from
fix-new-entrant-renaming
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 |
|---|---|---|
|
|
@@ -152,6 +152,10 @@ | |
| ("NSA", "CSA"), # (geo_id, Regional build cost zone) | ||
| } | ||
|
|
||
| # Allowed 'extra' subregion (not in sub_regional_geography) present in the names of | ||
| # some new entrant gas plant; see Open-ISP/ISPyPSA#131 | ||
| _EXTRA_SUBREGION_IN_NAMES = {"WOO"} | ||
|
|
||
| # Data scale diff for 'BOTN - Cethana' in LCF table: see first comment on Open-ISP/ISPyPSA#131. | ||
| _LCF_COLUMNS_IN_PERCENT = ["BOTN - Cethana"] | ||
|
|
||
|
|
@@ -342,8 +346,9 @@ def _collapse_geo_id_to_granularity( | |
| 1. Splits ``new_entrants`` into REZ rows (left untouched) and subregion rows. | ||
| 2. Subregion rows get grouped by ``group_key_columns`` + the re-keyed geo_id and | ||
| averaged over ``value_columns``. | ||
| 3. Aggregated rows' 'name' set to "{geo_id} {technology}" (except BOTN - see | ||
| ``_name_collapsed_rows``). | ||
| 3. Aggregated rows keep the first 'name' picked by the groupby, with any stale | ||
| leading sub-region-style token (a real geo_id, or a known extra like "WOO") | ||
| replaced by the new, collapsed geo_id — see ``_rekey_names_to_collapsed_geo_id``. | ||
| 4. Returns concatted REZ rows and aggregated rows. | ||
|
|
||
| Args: | ||
|
|
@@ -368,8 +373,8 @@ def _collapse_geo_id_to_granularity( | |
| SNW subregion NSW | ||
|
|
||
| returns: | ||
| name technology geo_id lcf_build | ||
| NSW OCGT (small GT) OCGT (small GT) NSW 102.0 # mean(104, 100) | ||
| name technology geo_id lcf_build | ||
| NSW OCGT Small OCGT (small GT) NSW 102.0 # mean(104, 100) | ||
| """ | ||
| if regional_granularity == "sub_regions": | ||
| return new_entrants | ||
|
|
@@ -380,11 +385,17 @@ def _collapse_geo_id_to_granularity( | |
| if to_collapse.empty: | ||
| return new_entrants | ||
|
|
||
| to_collapse["geo_id"] = _map_geo_id_to_granularity( | ||
| to_collapse["geo_id"], regional_granularity, sub_regional_geography | ||
| old_geo_ids = to_collapse["geo_id"].copy() | ||
| new_geo_ids = _map_geo_id_to_granularity( | ||
| old_geo_ids, regional_granularity, sub_regional_geography | ||
| ) | ||
| collapsed = _aggregate_by_geo_id( | ||
| to_collapse.assign(geo_id=new_geo_ids), group_key_columns, value_columns | ||
| ) | ||
| collapsed["name"] = _rekey_names_to_collapsed_geo_id( | ||
| collapsed, | ||
| set(old_geo_ids) | _EXTRA_SUBREGION_IN_NAMES, | ||
| ) | ||
| collapsed = _aggregate_by_geo_id(to_collapse, group_key_columns, value_columns) | ||
| collapsed = _name_collapsed_rows(collapsed) | ||
|
|
||
| return pd.concat([unchanged, collapsed], ignore_index=True)[new_entrants.columns] | ||
|
|
||
|
|
@@ -394,38 +405,48 @@ def _aggregate_by_geo_id( | |
| group_key_columns: list[str], | ||
| value_columns: list[str], | ||
| ) -> pd.DataFrame: | ||
| """Groups by ``group_key_columns`` + 'geo_id' and averages ``value_columns``.""" | ||
| # 'dropna=False' set to keep thermal generator rows (w/ NaN 'resource_type') | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was this comment incorrect? |
||
| """Groups by ``group_key_columns`` + 'geo_id', averages ``value_columns``, keeps | ||
| the first instance of 'name' for each group.""" | ||
| return new_entrants.groupby( | ||
| group_key_columns + ["geo_id"], dropna=False, as_index=False | ||
| )[value_columns].mean() | ||
| ).agg({"name": "first", **{col: "mean" for col in value_columns}}) | ||
|
|
||
|
|
||
| # TODO (coming in next PR): fix this to keep naming convention from AEMO sources even when | ||
| # granularity collapses. NOTE: IASR names have slightly different order/ | ||
| # convention than trace names, but for VRE (REZ-based) so less important here. | ||
| def _name_collapsed_rows(collapsed: pd.DataFrame) -> pd.DataFrame: | ||
| """Sets 'name' on merged rows to "{geo_id} {technology}". | ||
| def _rekey_names_to_collapsed_geo_id( | ||
| new_entrants: pd.DataFrame, known_name_prefixes: set[str] | ||
| ) -> pd.Series: | ||
| """Replaces a stale leading sub-region-style token in each name with the row's | ||
| (post-collapse) geo_id. | ||
|
|
||
| The lone documented exception is BOTN - Cethana (see ``_BOTN_CETHANA_DETAILS``): | ||
| a named, site-specific project rather than a generic technology archetype, which | ||
| keeps its original 'name'. | ||
| Runs on the already-aggregated frame, after ``.groupby(...).first()`` has picked | ||
| one 'name' per group — so it doesn't matter which row's name survived the pick; | ||
| every stale geo_id-like prefix gets normalised to the same, correct new geo_id. | ||
|
|
||
| I/O Example: | ||
| collapsed: | ||
| technology geo_id | ||
| OCGT (small GT) NSW | ||
| BOTN - Cethana TAS | ||
| df: | ||
| name technology geo_id ... | ||
| NQ OCGT Small OCGT (small GT) NEM ... | ||
| WOO OCGT Large OCGT (large GT) NEM ... | ||
| BOTN - Cethana - 20h BOTN - Cethana NEM ... | ||
|
|
||
| known_name_prefixes: {"NQ", "WOO", "SNW", ...} # real sub_regions + "WOO" | ||
|
|
||
| returns: | ||
| technology geo_id name | ||
| OCGT (small GT) NSW NSW OCGT (small GT) | ||
| BOTN - Cethana TAS BOTN - Cethana - 20h # original name kept | ||
| name | ||
| NEM OCGT Small | ||
| NEM OCGT Large | ||
| BOTN - Cethana - 20h | ||
|
|
||
| # BOTN doesn't start with any known prefix, so it's untouched - no special case needed. | ||
| """ | ||
| fresh_name = collapsed["geo_id"] + " " + collapsed["technology"] | ||
| is_botn = collapsed["technology"] == _BOTN_CETHANA_DETAILS["name"] | ||
| collapsed["name"] = fresh_name.mask(is_botn, _BOTN_CETHANA_DETAILS["full_name"]) | ||
| return collapsed | ||
|
|
||
| parts = new_entrants["name"].str.partition() | ||
| parts.columns = ["old_prefix", "separator", "rest_of_name"] | ||
|
|
||
| rekeyed_names = new_entrants["geo_id"].str.cat(parts[["separator", "rest_of_name"]]) | ||
| is_known_prefix = parts["old_prefix"].isin(known_name_prefixes) | ||
|
|
||
| return new_entrants["name"].where(~is_known_prefix, rekeyed_names) | ||
|
|
||
|
|
||
| # --- locational cost factor (LCF) helpers --- | ||
|
|
||
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
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.
Not sure that the tests cover this? i.e. that might all pass fine even without it.
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.
Actually if you make Dylan change move
set(old_geo_ids) | _EXTRA_SUBREGION_IN_NAMESthen I think the test you added would then cover this.