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

feat: powerflow snapshot figure #241

Merged
merged 2 commits into from
Mar 17, 2021
Merged

Conversation

danielolsen
Copy link
Contributor

Purpose

Add plot of powerflow snapshot to the codebase.

What the code is doing

plot_powerflow_snapshot is the main function, aggregate_plant_generation and plot_arrows are helper functions.

Testing

Tested against the notebooks that @victoriahunt used to generate figures for the Macro Grid report, the results are nearly identical (small differences in plot order, some line thicknesses, etc.). I figure that those small details can be ironed out in parallel with the main code review.

Usage Example/Visuals

Using a CSV of demand center locations, we can reproduce the USA powerflow snapshots with e.g.:

>>> from powersimdata.scenario.scenario import Scenario
>>> from postreise.plot.plot_powerflow_snapshot import plot_powerflow_snapshot
>>> import pandas as pd
>>> plot_powerflow_snapshot(Scenario(1270), '2016-11-02 22:00:00', b2b_dclines={"from": range(7), "to": [7, 8]}, demand_centers=pd.read_csv("loadzone_centers.csv", index_col=0), legend_font_size=20)

Original notebook:
usa_orig
Reproduction:
usa_new

To reproduce the Texas figure, which features a zoomed-in area, different scalings, and four arrows per DC line:

>>> plot_powerflow_snapshot(Scenario(1270), '2016-11-03 00:00:00', x_range=(-11.9e6, -10.2e6), y_range=(2.7e6, 4.4e6), figsize=(800, 800), b2b_dclines={"from": range(7), "to": [7, 8]}, demand_centers=pd.read_csv("loadzone_centers.csv", index_col=0), pf_width_scale_factor=0.002, bg_width_scale_factor=0.00125, circle_scale_factor=(1 / 3), legend_font_size=20, num_dc_arrows=4)

Original notebook:
texas_orig
Reproduction:
texas_new

Time estimate

30 minutes. By line count, it's a bit heavier than we'd usually like for a PR, but a lot of that is due to documentation and code formatting. The code itself is fairly straightforward I think.

@danielolsen danielolsen self-assigned this Mar 15, 2021
@rouille
Copy link
Collaborator

rouille commented Mar 15, 2021

Should we add the loadzone_centers.csv file to the data folder and rename it something like loadzone_center_usa_tamu_model.csv?

@danielolsen
Copy link
Contributor Author

Should we add the loadzone_centers.csv file to the data folder and rename it something like loadzone_center_usa_tamu_model.csv?

That's a good idea. Default behavior remains the same (don't plot), but we have these data available if the user wants to use them?

@rouille
Copy link
Collaborator

rouille commented Mar 15, 2021

Should we add the loadzone_centers.csv file to the data folder and rename it something like loadzone_center_usa_tamu_model.csv?

That's a good idea. Default behavior remains the same (don't plot), but we have these data available if the user wants to use them?

In fact the data frame could be part of the powersimdata.network.usa_tamu.constants.zones module and hence retrievable vie the the model_immutables attributes of a Grid object

@BainanXia
Copy link
Collaborator

@danielolsen @rouille I would suggest we handle the csvs in the same way as we do in the shortfall plot, i.e. make the plot function taking a dataframe as inputs and use the csv file as an example input in the demo notebook. The current demand center csv is generated by my simple back of envelope algorithm with many manual adjustments and should not be tied to the usa_tamu model.

@danielolsen
Copy link
Contributor Author

Demo locations and a script are added.


:param bokeh.plotting.figure.Figure bokeh_figure: figure to plot arrows onto.
:param pandas.DataFrame branch: data frame containing:
'pf', 'dist', 'arrow_size', 'from_x', 'from_y', 'to_x', 'to_y'.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would add here that :func:postreise.plot.projection_helpers.project_branch can be used to obtain the coordinates of a branch from its (lat, lon).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

"""Plot a snapshot of powerflow.

:param powersimdata.scenario.scenario.Scenario scenario: scenario to plot.
:param str hour: interval to use for snapshot.
Copy link
Collaborator

Choose a reason for hiding this comment

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

In other plotting routines, we pass a pandas.Timestamp/numpy.datetime64/datetime.datetime as hour to avoid any issue related to weird string formatting when locating the indices. So instead of passing "2016-11-02 22:00:00",, the user would pass pd.Timestamp(2016, 11, 2, 22)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, if houris a pandas.Timestamp/numpy.datetime64/datetime.datetime then later you can use _check_date_range_in_scenario to make sure the query hour is in the scenario

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

:param tuple(float, float) x_range: x range to zoom plot to (EPSG:3857).
:param tuple(float, float) y_range: y range to zoom plot to (EPSG:3857).
:param int/str legend_font_size: size to display legend specified as e.g. 12/'12pt'.
"""
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can check that the scnario is in analyze state here using _check_scenario_is_in_analyze_state

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Collaborator

@rouille rouille left a comment

Choose a reason for hiding this comment

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

Very nice. You are now a bokeh expert, congratulations!

@danielolsen danielolsen force-pushed the daniel/powerflow_snapshot branch from 3c7289f to 0bdd128 Compare March 17, 2021 21:14
@danielolsen danielolsen force-pushed the daniel/powerflow_snapshot branch from 0bdd128 to ed313cf Compare March 17, 2021 21:24
@danielolsen danielolsen merged commit 96ac9e4 into develop Mar 17, 2021
@danielolsen danielolsen deleted the daniel/powerflow_snapshot branch March 17, 2021 21:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants