Skip to content
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

refactor: upgrade CA-NS with event classes #6050

Merged
merged 15 commits into from
Feb 10, 2024

Conversation

kruschk
Copy link
Contributor

@kruschk kruschk commented Oct 25, 2023

Issue

Refs: #6011, #6050

Description

Upgrade the parser to use the ExchangeList, ProductionBreakdownList, ProductionMix, and ZoneKey classes. This should yield no functional change. Additionally:

  • Comply with Black's 88-column default line length limit.
  • Define global constants to replace a number of local variables and literals.
  • Try to use more consistent names throughout.

Double check

  • I have tested my parser changes locally with poetry run test_parser "zone_key"
  • I have run pnpx prettier --write . and poetry run format to format my changes.

parsers/CA_NS.py Outdated Show resolved Hide resolved
Upgrade the parser to use the ExchangeList, ProductionBreakdownList,
ProductionMix, and ZoneKey classes. This should yield no functional
change. Additionally:

- Comply with Black's 88-column default line length limit.
- Define global constants to replace a number of local variables and
  literals.
- Try to use more consistent names throughout.

Refs: electricitymaps#6011, electricitymaps#6050
The JSON arrays a and b returned by the two endpoints appear to be
parallel in the sense that element a[i] corresponds with element b[i]
for all i of interest. Zip these arrays together and iterate over the
resulting pairs instead of iterating over one and performing a linear
search to find the corresponding element in the other. Additionally:

- Check whether we've been bitten by a race condition while requesting
  the above array data and bail out if so.
- Skip the first element of each array because the reported base load is
  always 0 MW.

Refs: electricitymaps#6011, electricitymaps#6050
Copy link
Member

@VIKTORVAV99 VIKTORVAV99 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few suggestions, but otherwise this looks good!

parsers/CA_NS.py Outdated Show resolved Hide resolved
parsers/CA_NS.py Outdated Show resolved Hide resolved
parsers/CA_NS.py Outdated Show resolved Hide resolved
parsers/CA_NS.py Outdated Show resolved Hide resolved
- When a pair of requests have failed, retry them until they succeed or
  the maximum number of attempts is exceeded.
- Simplify the validation logic by making better use of the event
  classes instead of introducing extraneous data structures.

Refs: electricitymaps#6011, electricitymaps#6050
parsers/CA_NS.py Outdated Show resolved Hide resolved
Reliably merge the source's base load array with the corresponding
electricity mix array by correlating events based on their timestamps.

Refs: electricitymaps#6011, electricitymaps#6050
@VIKTORVAV99 VIKTORVAV99 self-requested a review November 21, 2023 09:44
parsers/CA_NS.py Outdated Show resolved Hide resolved
parsers/CA_NS.py Outdated Show resolved Hide resolved
parsers/CA_NS.py Outdated
Comment on lines 84 to 92
# In this source, imports are positive. In the expected result for
# CA-NB->CA-NS, "net" represents a flow from NB to NS, i.e., an import
# to NS, so the value can be used directly. Note that this API only
# specifies imports; when NS is exporting energy, the API returns 0.
exchanges.append(
datetime=timestamp,
netFlow=load * mix["Imports"] / 100,
source=SOURCE,
zoneKey=ZoneKey("CA-NB->CA-NS"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not strictly related to this refactor, but just to mention. The assumption that all imports are from NB is now problematic because Nova Scotia's interconnection to Newfoundland is now live. But I've never seen live data on its operation, the closest thing was that it amounted to 14% of NS supply in 2023H1, per https://www.nspower.ca/cleanandgreen/clean-energy "Our Energy Stats" chart

Copy link
Contributor

@q-- q-- Jan 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference related to your comment: the daily generation reports for Newfoundland might be usable as a source for the interconnector data, see #2541 (comment)

Copy link
Contributor

@q-- q-- Jan 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, look: I found a page on the NSPower website that has the information you need (Maritime Link interconnector) and more, updated roughly every 5 minutes, with more granular data than the current source: it's called Daily Report, but under the heading "Current System Conditions" you'll find "10 minute averages" of e.g. load, wind, and imports and exports through various interconnectors that are at most five minutes old.

Edit: well, turns out you found it at some point as well: #3206 (comment)

Copy link
Contributor

@q-- q-- Jan 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I dug deeper into this, and it turns out we aren't actually using this exchange data since we get the data on the NS-NB interconnection from New Brunswick Power, not from NSPower. So it might be best to just delete the exchanges-related part, or, alternatively, switch to obtaining exchange info from the source in #6317 (and perhaps implement the NS-Newfoundland interconnector as well). But that could be done in a separate issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @VIKTORVAV99, any thoughts on this? Should we simply delete the exchange parser if it's not used?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify, my idea was to delete the exchange parser now so we are more certain we don't accidentally fetch incorrect data, and then re-add an exchange parser based on the new source from #6317 in a separate pull request.

But doing both in this PR is also acceptable, if you've got the time for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, gotcha! It's not entirely clear to me what needs to be done to implement the new exchange parser (there are a lot of links to follow and I'm a bit lost), so I'll just delete it here and we can address it separately. Would those changes have any impact on the production parser?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exchange stuff shouldn't. The "Current System Conditions" also has wind generation, but as the page has data updated roughly every 5 minutes I think it'll be hard to integrate with the hourly data provided by the current source – and probably not that useful either, if the current source stays online.

As for implementing the exchange parser, the short summary is: from https://www.nspower.ca/oasis/system-reports-messages/daily-report, under "Current System Conditions", grab:

  1. The timestamp, e.g. "Last Updated: 10-Jan-24 20:38:10";
  2. "NS Export" for (most likely) the New Brunswick interconnector (optional, already provided by the New Brunswick parser);
  3. "Maritime Link Import" for the Newfoundland interconnector.

The rest of the stuff in the issue is useful for exchange configuration and historical data.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks for clarifying! That doesn't sound as arduous as I thought; maybe I can roll it into this PR. I'll try to carve out some time to work on it this weekend.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, that wasn't so bad! A few notes:

  • I created the CA-NL-NF->CA-NS exchange, but this is my first time doing so, so I very well could have done something wrong. In particular, I haven't tested how it will look because I don't have the front-end set up on my machine (I used Google Maps for the coordinates and a protractor for the rotation 😁). Further, I wasn't sure whether to use CA-NL-LB or CA-NL-NF for the other zone (I chose the latter based on the physical location of the link).
  • I also implemented the CA-NB->CA-NS exchange, but the back-end is still configured to use the NB parser for this pair.

- Stop discarding events whose production mix percentages exceed
  percentage thresholds, as these imposed limits may not be sound.
- Provide defaults when getting attributes from the production mix to
  avoid comparing numbers with Nones.

Refs: electricitymaps#6011, electricitymaps#6050
- New Brunswick and Newfoundland and Labrador exchange data is available
  at https://www.nspower.ca/oasis/system-reports-messages/daily-report;
  upgrade the exchange parser to leverage this new source.
- Since it no longer reduces duplication, eliminate the _get_info
  function and roll its logic into fetch_production.

Refs: electricitymaps#2541, electricitymaps#3206, electricitymaps#6011, electricitymaps#6050, electricitymaps#6317
@github-actions github-actions bot added python Pull requests that update Python code exchange config Pull request or issue for exchange configurations labels Jan 14, 2024
@jarek
Copy link
Collaborator

jarek commented Jan 27, 2024

Can this five-month-old PR be reviewed and merged?

Current JSON endpoints aren't sending current data (#5587), if this persists, we would probably want to switch to the OASIS report being implemented in this PR, so having it merged would be a useful base.

Apply the following corrections and merge conflic resolutions:

- Fix an incorrect zone in a call to fetch_exchange.
- Treat CT generation as oil instead of gas.
- Update the comment regarding hydro capacities.
- Update the production mix thresholds for hydro and oil.
parsers/CA_NS.py Outdated Show resolved Hide resolved
Gracefully handle errors that might occur while navigating the HTML
document containing exchange data.

Refs: electricitymaps#6011
@VIKTORVAV99 VIKTORVAV99 self-requested a review January 31, 2024 07:36
parsers/CA_NS.py Outdated Show resolved Hide resolved
- Re-throw caught errors to provide better diagnostic information.
- Format log messages lazily.

Refs: electricitymaps#6011
@ghost
Copy link

ghost commented Feb 2, 2024

Hi, we will merge a quick fix today to make sure that the data stops being overwritten with 0s. This PR looks great and should be merged after as the quality of the refactoring is really good. :)

parsers/CA_NS.py Outdated Show resolved Hide resolved
Copy link
Member

@VIKTORVAV99 VIKTORVAV99 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

Thanks for bearing with me on this PR and sorry for some reviews taking longer than they should.

I'm approving this now but will let @mathilde-daugy have the final say if we should remove the in parser validation for this parser.

PS: I left some suggestions that have some tiny performance improvements as it avoid a reverse walk in the string for the value replacement.

parsers/CA_NS.py Outdated Show resolved Hide resolved
parsers/CA_NS.py Outdated Show resolved Hide resolved
parsers/CA_NS.py Show resolved Hide resolved
@kruschk
Copy link
Contributor Author

kruschk commented Feb 10, 2024

All good to merge?

@VIKTORVAV99 VIKTORVAV99 enabled auto-merge (squash) February 10, 2024 08:21
@VIKTORVAV99 VIKTORVAV99 merged commit ac7c253 into electricitymaps:master Feb 10, 2024
12 checks passed
@kruschk kruschk deleted the kruschk/6011-ca-ns branch February 10, 2024 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
exchange config Pull request or issue for exchange configurations hacktoberfest-accepted parser python Pull requests that update Python code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants