Skip to content

Features/#800 clustering with focus#840

Open
KathiEsterl wants to merge 35 commits intodevfrom
features/#800-clustering-with-focus
Open

Features/#800 clustering with focus#840
KathiEsterl wants to merge 35 commits intodevfrom
features/#800-clustering-with-focus

Conversation

@KathiEsterl
Copy link
Copy Markdown
Contributor

Closes #800

This PR introduces a new functionality enabling spatial clustering with a regional focus.

The focus region can currently be defined either via a shapefile, or by selecting the corresponding Kreis.
A default parameterization is implemented to control the distribution of buses outside the focus region. The clustering weights are adjusted based on the shortest path distance of each bus to the boundary of the focus region. Therefore, the distance it used within a sigmoid decay function with a characteristic length scale of 50 km.

While the current implementation is fully functional and usable, several aspects require further refinement. These improvements will be tracked in a dedicated follow-up issue.

In addition to the clustering extensions, the spatial clustering arguments were partially restructured to improve usability and clarity. Some related aspects are documented in #811.

@ClaraBuettner
Copy link
Copy Markdown
Contributor

Do you have anything in particular in mind that I should try out?

@KathiEsterl
Copy link
Copy Markdown
Contributor Author

Maybe you could try it once with a shape-file and once with a list of Kreise and check the clustered network afterwards? I also did that, of course, but that is mainly what I think could be beneficial.

@ClaraBuettner
Copy link
Copy Markdown
Contributor

I ran into some problems when I wanted to test the focus region:

  • Do I have to add in the focus region list sth. like "Schleswig-Flensburg" or "DEF01"? I guess it is the first option, but it is not completely clear yet. It would be also nice if it fails if names are added there that don't exist.
  • When I try to run it for "Schleswig-Flensburg" I run into this error:
  File ~/etrago/eTraGo/etrago/appl.py:624 in run_etrago
    etrago.spatial_clustering()

  File ~/etrago/eTraGo/etrago/cluster/electrical.py:1074 in run_spatial_clustering
    weight = focus_weighting(

  File ~/etrago/eTraGo/etrago/cluster/spatial.py:923 in focus_weighting
    border_buses = buses_gdf.loc[

  File ~/etrago/venv-py310/lib/python3.10/site-packages/pandas/core/indexing.py:1073 in __getitem__
    return self._getitem_axis(maybe_callable, axis=axis)

  File ~/etrago/venv-py310/lib/python3.10/site-packages/pandas/core/indexing.py:1301 in _getitem_axis
    return self._getitem_iterable(key, axis=axis)

  File ~/etrago/venv-py310/lib/python3.10/site-packages/pandas/core/indexing.py:1239 in _getitem_iterable
    keyarr, indexer = self._get_listlike_indexer(key, axis)

  File ~/etrago/venv-py310/lib/python3.10/site-packages/pandas/core/indexing.py:1432 in _get_listlike_indexer
    keyarr, indexer = ax._get_indexer_strict(key, axis_name)

  File ~/etrago/venv-py310/lib/python3.10/site-packages/pandas/core/indexes/base.py:6070 in _get_indexer_strict
    self._raise_if_missing(keyarr, indexer, axis_name)

  File ~/etrago/venv-py310/lib/python3.10/site-packages/pandas/core/indexes/base.py:6133 in _raise_if_missing
    raise KeyError(f"{not_found} not in index")

KeyError: "['9395', '14304', '20264', '14772'] not in index"

@ClaraBuettner
Copy link
Copy Markdown
Contributor

When I try to use the "kmeans" method, the gas_clsutering fails:

  File ~/etrago/eTraGo/etrago/appl.py:625 in run_etrago
    etrago.spatial_clustering_gas()

  File ~/etrago/eTraGo/etrago/cluster/gas.py:1194 in run_spatial_clustering_gas
    self.network, busmap = gas_postprocessing(self, busmap, medoid_idx)

  File ~/etrago/eTraGo/etrago/cluster/gas.py:448 in gas_postprocessing
    medoid_idx[busmap.values.astype(int)].values,

TypeError: 'NoneType' object is not subscriptable

@KathiEsterl
Copy link
Copy Markdown
Contributor Author

I ran into some problems when I wanted to test the focus region:

* Do I have to add in the focus region list sth. like "Schleswig-Flensburg" or "DEF01"? I guess it is the first option, but it is not completely clear yet.  It would be also nice if it fails if names are added there that don't exist.

* When I try to run it for "Schleswig-Flensburg" I run into this error:
  File ~/etrago/eTraGo/etrago/appl.py:624 in run_etrago
    etrago.spatial_clustering()

  File ~/etrago/eTraGo/etrago/cluster/electrical.py:1074 in run_spatial_clustering
    weight = focus_weighting(

  File ~/etrago/eTraGo/etrago/cluster/spatial.py:923 in focus_weighting
    border_buses = buses_gdf.loc[

  File ~/etrago/venv-py310/lib/python3.10/site-packages/pandas/core/indexing.py:1073 in __getitem__
    return self._getitem_axis(maybe_callable, axis=axis)

  File ~/etrago/venv-py310/lib/python3.10/site-packages/pandas/core/indexing.py:1301 in _getitem_axis
    return self._getitem_iterable(key, axis=axis)

  File ~/etrago/venv-py310/lib/python3.10/site-packages/pandas/core/indexing.py:1239 in _getitem_iterable
    keyarr, indexer = self._get_listlike_indexer(key, axis)

  File ~/etrago/venv-py310/lib/python3.10/site-packages/pandas/core/indexing.py:1432 in _get_listlike_indexer
    keyarr, indexer = ax._get_indexer_strict(key, axis_name)

  File ~/etrago/venv-py310/lib/python3.10/site-packages/pandas/core/indexes/base.py:6070 in _get_indexer_strict
    self._raise_if_missing(keyarr, indexer, axis_name)

  File ~/etrago/venv-py310/lib/python3.10/site-packages/pandas/core/indexes/base.py:6133 in _raise_if_missing
    raise KeyError(f"{not_found} not in index")

KeyError: "['9395', '14304', '20264', '14772'] not in index"

I added a warning for non-valid names.

I also fixed the problem with 'Schleswig-Flensburg' which was related to cross-border lines. Should be working now for this case and also other Kreise near borders.

@KathiEsterl
Copy link
Copy Markdown
Contributor Author

When I try to use the "kmeans" method, the gas_clsutering fails:

  File ~/etrago/eTraGo/etrago/appl.py:625 in run_etrago
    etrago.spatial_clustering_gas()

  File ~/etrago/eTraGo/etrago/cluster/gas.py:1194 in run_spatial_clustering_gas
    self.network, busmap = gas_postprocessing(self, busmap, medoid_idx)

  File ~/etrago/eTraGo/etrago/cluster/gas.py:448 in gas_postprocessing
    medoid_idx[busmap.values.astype(int)].values,

TypeError: 'NoneType' object is not subscriptable

Solved this issue.

@ClaraBuettner
Copy link
Copy Markdown
Contributor

With these settings:

    "network_clustering": {
        "method": {
            "focus_region": ["Schleswig-Flensburg", "Flensburg"],  # None, shape-file or list with string for Kreise
            "per_country": True,  # if True, buses are restricted to one cluster per country
            "algorithm": "kmeans",  # choose clustering method: kmeans or kmedoids-dijkstra
            "remove_stubs": False,  # remove stubs before kmeans clustering
            "use_reduced_coordinates": False,  # if True, do not average cluster coordinates (in remove stubs)
            "line_length_factor": 1,  # Factor to multiply distance between new buses for new line lengths
            "random_state": 42,  # random state for replicability of clustering results
            "n_init": 10,  # affects clustering algorithm, only change when neccesary
            "max_iter": 100,  # affects clustering algorithm, only change when neccesary
            "tol": 1e-6,  # affects clustering algorithm, only change when neccesary
            "cpu_cores": 4,  # number of cores used during clustering, "max" for all cores available.
        },
        "electricity_grid": {
            "active": True,  # choose if clustering is activated
            "cluster_within_focus": False,  # False for very low clustering within focus region
            "n_clusters": 100,  # total number of resulting AC nodes
            "k_elec_busmap": False,  # False or path/to/busmap.csv
        },
        "gas_grids": {
            "active": True,  # choose if clustering is activated
            "cluster_within_focus": False,  #  False for very low clustering within focus region
            "n_clusters_ch4": 15,  # total number of resulting CH4 nodes
            "n_clusters_h2": 15,  # total number of resulting H2 nodes
            "k_ch4_busmap": False,  # False or path/to/ch4_busmap.csv
        },
    },
    

I unfortunatly run into this error:

  File ~/etrago/eTraGo/etrago/appl.py:651
    etrago = run_etrago(args, json_path=None)

  File ~/etrago/eTraGo/etrago/appl.py:621 in run_etrago
    etrago.spatial_clustering()

  File ~/etrago/eTraGo/etrago/cluster/electrical.py:1076 in run_spatial_clustering
    weight = focus_weighting(

  File ~/etrago/eTraGo/etrago/cluster/spatial.py:996 in focus_weighting
    container = p.starmap(shortest_path, gen(paths, chunksize, graph))

  File /usr/lib/python3.10/multiprocessing/pool.py:375 in starmap
    return self._map_async(func, iterable, starmapstar, chunksize).get()

  File /usr/lib/python3.10/multiprocessing/pool.py:774 in get
    raise self._value

NodeNotFound: Node 13665 not found in graph

@KathiEsterl
Copy link
Copy Markdown
Contributor Author

I tested with the same settings and a few minor changes, but I still can't reproduce this error. Maybe we can have a look together tomorrow in the office.

@KathiEsterl
Copy link
Copy Markdown
Contributor Author

We found this error and fixed it.

I will start one more bigger calculation with all the new updates and fixes for a final check today.

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.

Implement Clustering Algorithm considering Focus Regions

2 participants