- File:
edisgo/run/tasks/grid.py → task_setup_grid (~line 64)
- Branch:
fix/setup-grid-saved-edisgo
Problem. setup_grid always assumes a raw ding0 dir
(EDisGo(ding0_grid=...)). For an already-saved EDisGo object (CSVs in a
topology/ subfolder + configs.json) it fails with
KeyError: ['mv_grid_id','lv_grid_id','in_building'] not in index. After loading,
a stale configs.json (older schema) then raises
KeyError: 'mv_max_voltage_deviation' downstream (in select_cable).
Change. Detect the topology/ layout, load via import_edisgo_from_files,
and reset config to current defaults. (Two coupled aspects — format detection +
config reset; review as two commits if preferred.)
@@ def task_setup_grid
"""
+ import os
+
from edisgo import EDisGo
grid_cfg = ctx.raw_config.get("grid", {})
@@
if legacy_ding0_grids is None:
legacy_ding0_grids = grid_cfg.get("legacy_ding0_grids", False)
- if edisgo is None:
+ # Detect grid format: a saved EDisGo object has its CSVs in a
+ # 'topology' subfolder (+ configs.json) and must be loaded via
+ # import_edisgo_from_files. A raw ding0 grid has the CSVs directly
+ # in the directory and is loaded via the ding0 importer.
+ is_saved_edisgo = os.path.isdir(os.path.join(str(ding0_path), "topology"))
+
+ if is_saved_edisgo:
+ from edisgo.edisgo import import_edisgo_from_files
+
+ ctx.logger.info(
+ f"setup_grid: '{ding0_path}' looks like a saved EDisGo object "
+ f"(topology/ subfolder) -> loading via import_edisgo_from_files."
+ )
+ from edisgo.tools.config import Config
+
+ edisgo = import_edisgo_from_files(
+ edisgo_path=str(ding0_path),
+ import_topology=True,
+ )
+ edisgo.legacy_grids = False
+ # The saved configs.json can stem from an older eDisGo version with a
+ # different config schema (e.g. missing 'mv_max_voltage_deviation').
+ # Reset to the current defaults so downstream tasks find all keys.
+ edisgo._config = Config()
+ elif edisgo is None:
edisgo = EDisGo(
ding0_grid=str(ding0_path),
legacy_ding0_grids=legacy_ding0_grids,
)
else:
edisgo.import_ding0_grid(
path=str(ding0_path), legacy_ding0_grids=legacy_ding0_grids
)
Verify. Pointing grid.ding0_path at a saved-EDisGo dir loads the topology
and the pipeline reaches import_generators; raw ding0 dirs still load unchanged.
Part of #665.
edisgo/run/tasks/grid.py→task_setup_grid(~line 64)fix/setup-grid-saved-edisgoProblem.
setup_gridalways assumes a raw ding0 dir(
EDisGo(ding0_grid=...)). For an already-saved EDisGo object (CSVs in atopology/subfolder +configs.json) it fails withKeyError: ['mv_grid_id','lv_grid_id','in_building'] not in index. After loading,a stale
configs.json(older schema) then raisesKeyError: 'mv_max_voltage_deviation'downstream (inselect_cable).Change. Detect the
topology/layout, load viaimport_edisgo_from_files,and reset config to current defaults. (Two coupled aspects — format detection +
config reset; review as two commits if preferred.)
@@ def task_setup_grid """ + import os + from edisgo import EDisGo grid_cfg = ctx.raw_config.get("grid", {}) @@ if legacy_ding0_grids is None: legacy_ding0_grids = grid_cfg.get("legacy_ding0_grids", False) - if edisgo is None: + # Detect grid format: a saved EDisGo object has its CSVs in a + # 'topology' subfolder (+ configs.json) and must be loaded via + # import_edisgo_from_files. A raw ding0 grid has the CSVs directly + # in the directory and is loaded via the ding0 importer. + is_saved_edisgo = os.path.isdir(os.path.join(str(ding0_path), "topology")) + + if is_saved_edisgo: + from edisgo.edisgo import import_edisgo_from_files + + ctx.logger.info( + f"setup_grid: '{ding0_path}' looks like a saved EDisGo object " + f"(topology/ subfolder) -> loading via import_edisgo_from_files." + ) + from edisgo.tools.config import Config + + edisgo = import_edisgo_from_files( + edisgo_path=str(ding0_path), + import_topology=True, + ) + edisgo.legacy_grids = False + # The saved configs.json can stem from an older eDisGo version with a + # different config schema (e.g. missing 'mv_max_voltage_deviation'). + # Reset to the current defaults so downstream tasks find all keys. + edisgo._config = Config() + elif edisgo is None: edisgo = EDisGo( ding0_grid=str(ding0_path), legacy_ding0_grids=legacy_ding0_grids, ) else: edisgo.import_ding0_grid( path=str(ding0_path), legacy_ding0_grids=legacy_ding0_grids )Verify. Pointing
grid.ding0_pathat a saved-EDisGo dir loads the topologyand the pipeline reaches
import_generators; raw ding0 dirs still load unchanged.Part of #665.