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

Add get_bus_demand method #311

Merged
merged 2 commits into from
Oct 8, 2020
Merged

Add get_bus_demand method #311

merged 2 commits into from
Oct 8, 2020

Conversation

danielolsen
Copy link
Contributor

Purpose

Add a method to calculate demand by bus. This came up in some analysis work we've been doing, it'll probably come up again sometime.

What is the code doing

In helpers.py: adding a new helper function to decompose zone demand to bus demand.

In create.py and analyze.py: adding a method to use this function.

Demonstration / Validation

>>> from powersimdata.scenario.scenario import Scenario
>>> scenario = Scenario('1270')
>>> demand = scenario.state.get_demand()
>>> bus_demand = scenario.state.get_bus_demand()
>>> demand.shape
(8784, 76)
>>> bus_demand.shape
(8784, 82071)
>>> demand.sum().sum()
4535007498.534381
>>> bus_demand.sum().sum()
4535007498.534377

So the sum looks good, and the shape looks good, what happens if we try to sum back to zone?

>>> grid = scenario.state.get_grid()
>>> bus = grid.bus
>>> bus_demand.loc['zone_id'] = [bus.loc[b, 'zone_id'] for b in bus_demand.columns]
>>> regrouped = bus_demand.groupby(bus_demand.loc['zone_id'], axis=1).sum()
>>> regrouped.drop('zone_id', inplace=True)
(demand - regrouped).abs().max().max()
1.6007106751203537e-10

Time to review

15 minutes. This is a low priority.

@rouille
Copy link
Collaborator

rouille commented Oct 8, 2020

I ran the following script:

from powersimdata.scenario.scenario import Scenario
import time

scenario = Scenario('1270')

start_time = time.time()
bus_demand = scenario.state.get_bus_demand()
print("--- %s seconds ---" % (time.time() - start_time))

I killed it after 30 min. How long does it take to run on your machine?

@danielolsen
Copy link
Contributor Author

@rouille I refactored but forgot to push the fix. It runs in <10 seconds now.

@rouille
Copy link
Collaborator

rouille commented Oct 8, 2020

@rouille I refactored but forgot to push the fix. It runs in <10 seconds now.

Indeed: --- 7.498027086257935 seconds ---
It makes sense, these are big arrays.

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.

Looks good.

zone_idx = zone_idx_lookup[bus.loc[b, "zone_id"]]
bus_to_zone[zone_idx, bus_idx] = bus_zone_share.loc[b]
bus_demand = pd.DataFrame(
np.dot(demand.to_numpy(), bus_to_zone), index=demand.index, columns=bus.index
Copy link
Collaborator

Choose a reason for hiding this comment

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

To be fancier, we can do demand.to_numpy()@bus_to_zone here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Aha! Thanks for pointing this out, it looks much cleaner.

Copy link
Collaborator

@BainanXia BainanXia left a comment

Choose a reason for hiding this comment

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

Thanks.

@danielolsen danielolsen merged commit 28fce98 into develop Oct 8, 2020
@danielolsen danielolsen deleted the daniel/bus_demand branch October 8, 2020 16:10
@ahurli ahurli mentioned this pull request Mar 11, 2021
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