PyPSA / pypsa-usa

PyPSA-USA: An Open-Source Energy System Optimization Model for the United States
https://pypsa-usa.readthedocs.io
MIT License
35 stars 15 forks source link

Issues found on `LAF_states` #169

Closed casinocullen closed 4 months ago

casinocullen commented 4 months ago

Checklist

The Issue

I integrated the latest version but there is an issue on simplifing and clustering network. I think the issues come from LAF_states added in the code. I temporarily fixed it by adding a line LAF_states = 'sum' in pypsa/clustering/spatial.py, like

DEFAULT_BUS_STRATEGIES = dict(
    x="mean",
    y="mean",
    v_nom="max",
    v_mag_pu_max="min",
    v_mag_pu_min="max",
    LAF_states = 'sum',
    generator=lambda x: "",
)

but I am wondering if I am the only one getting the error.

Steps To Reproduce

No response

Expected Behavior

No response

Error Message

Traceback (most recent call last):
  File "C:\Users\cc143\Documents\GitHub\pypsa-usa\workflow\.snakemake\scripts\tmpy5t369lc.cluster_network_eur.py", line 427, in <module>
    clustering = clustering_for_n_clusters(n, n_clusters, custom_busmap, aggregate_carriers,
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cc143\Documents\GitHub\pypsa-usa\workflow\.snakemake\scripts\tmpy5t369lc.cluster_network_eur.py", line 320, in clustering_for_n_clusters
    clustering = get_clustering_from_busmap(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cc143\.conda\envs\pypsa-usa\Lib\site-packages\pypsa\clustering\spatial.py", line 535, in get_clustering_from_busmap
    buses = aggregatebuses(n, busmap, custom_strategies=bus_strategies)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cc143\.conda\envs\pypsa-usa\Lib\site-packages\pypsa\clustering\spatial.py", line 342, in aggregatebuses
    aggregated = n.buses.groupby(busmap).agg(strategies)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cc143\AppData\Roaming\Python\Python311\site-packages\pandas\core\groupby\generic.py", line 1269, in aggregate
    result = op.agg()
             ^^^^^^^^
  File "C:\Users\cc143\AppData\Roaming\Python\Python311\site-packages\pandas\core\apply.py", line 163, in agg
    return self.agg_dict_like()
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cc143\AppData\Roaming\Python\Python311\site-packages\pandas\core\apply.py", line 420, in agg_dict_like
    results = {
              ^
  File "C:\Users\cc143\AppData\Roaming\Python\Python311\site-packages\pandas\core\apply.py", line 421, in <dictcomp>
    key: obj._gotitem(key, ndim=1).agg(how) for key, how in arg.items()
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cc143\AppData\Roaming\Python\Python311\site-packages\pandas\core\groupby\generic.py", line 269, in aggregate
    return self._python_agg_general(func, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cc143\AppData\Roaming\Python\Python311\site-packages\pandas\core\groupby\generic.py", line 288, in _python_agg_general
    result = self.grouper.agg_series(obj, f)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cc143\AppData\Roaming\Python\Python311\site-packages\pandas\core\groupby\ops.py", line 994, in agg_series
    result = self._aggregate_series_pure_python(obj, func)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cc143\AppData\Roaming\Python\Python311\site-packages\pandas\core\groupby\ops.py", line 1015, in _aggregate_series_pure_python
    res = func(group)
          ^^^^^^^^^^^
  File "C:\Users\cc143\AppData\Roaming\Python\Python311\site-packages\pandas\core\groupby\generic.py", line 285, in <lambda>
    f = lambda x: func(x, *args, **kwargs)
                  ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\cc143\.conda\envs\pypsa-usa\Lib\site-packages\pypsa\clustering\spatial.py", line 137, in consense
    assert (x == v).all() or x.isnull().all(), (
AssertionError: In Bus cluster LAF_states, the values of attribute LAF_states do not agree:
Bus
40684    0.000000
40689    0.000708
40690    0.001148
40699    0.000000
40706    0.001246
40707    0.001316
40712    0.001355
40716    0.002158
40717    0.000000
40720    0.000717
40730    0.000834
40733    0.000787
40734    0.000000
40738    0.000667
40745    0.000000
40747    0.001310
40748    0.001929
40758    0.001051
40762    0.001672
40776    0.000000
40778    0.001125
40781    0.001025
40786    0.001167
40793    0.000682
40800    0.000642
40801    0.000569
40805    0.001056
40811    0.000963
40812    0.000438
40818    0.001706
40820    0.001959
40821    0.001672
40823    0.000000
40825    0.000615
40830    0.000000
40831    0.000000
40843    0.000928
40851    0.001917
40852    0.001505
40857    0.000000
40860    0.001465
40865    0.000984
40870    0.001086
40871    0.000000
40873    0.001271
40874    0.001048
40879    0.002156
40882    0.000698
40884    0.000502
40888    0.000987
40890    0.000000
40891    0.000935
40902    0.001435
Name: LAF_states, dtype: float64

Anything else?

No response

ktehranchi commented 4 months ago

My mistake. The 'LAF_States' should actually be added here: https://github.com/PyPSA/pypsa-usa/blob/acedca90f5dea55a17f228b7e60b0a6f70023346/workflow/scripts/add_electricity.py#L1183