el1xr_opt.Modules.oM_InputSource#

el1xr_opt input source — the InputSource interface and open_source factory.

The model reads its input through one of two backends:

  • CSVSource — a directory of oM_Dict_* / oM_Data_* CSV files

    (the historical layout).

  • DuckDBSource — a single <case>.duckdb file holding the same tables.

open_source(path) looks at the path and returns the right backend: a directory gives a CSVSource, a .duckdb file gives a DuckDBSource. Both backends return identical DataFrames, so the rest of the model does not know or care which one was used.

The duckdb backend is imported lazily, so a checkout without duckdb installed can still build the CSV path.

class el1xr_opt.Modules.oM_InputSource.InputSource[source]#

Bases: ABC

Abstract input source. Implementations: CSVSource, DuckDBSource.

case_name: str#
abstract list_data_stems()[source]#

Stems of the data tables present (no oM_Data_ prefix, no _<case>.csv suffix).

Return type:

set

abstract read_dict(stem)[source]#

Return the dimension dict for stem as a plain DataFrame (no index).

Returns an empty DataFrame if the dict is absent.

Return type:

DataFrame

Parameters:

stem (str)

abstract read_data(stem)[source]#

Return a data table with its leading unnamed columns set as a nameless index.

This is the exact shape oM_InputData expects: the same DataFrame the old pd.read_csv + set_index(unnamed columns) code produced. Raises FileNotFoundError if the stem is absent.

Return type:

DataFrame

Parameters:

stem (str)

close()[source]#
Return type:

None

el1xr_opt.Modules.oM_InputSource.open_source(path)[source]#

Return a CSVSource for a directory or a DuckDBSource for a .duckdb file.

Return type:

InputSource

Parameters:

path (str | PathLike)

el1xr_opt.Modules.oM_InputSource.resolve_source(dir_name, case_name)[source]#

Pick the input for (dir_name, case_name).

Prefers the CSV case folder <dir_name>/<case_name> when it holds the case’s oM_Data_Parameter file (so CSV stays the default whenever a real case folder is present), otherwise falls back to the DuckDB file <dir_name>/<case_name>.duckdb. Checking for the Parameter file rather than just the folder means an empty results folder of the same name does not shadow a .duckdb input.

Return type:

InputSource

Parameters:
  • dir_name (str | PathLike)

  • case_name (str)

el1xr_opt.Modules.oM_InputSource.finalize_data_index(df, idx_cols)[source]#

Move idx_cols into the index and clear their names (the model expects a nameless index).

Return type:

DataFrame

Parameters:
  • df (DataFrame)

  • idx_cols (list)

el1xr_opt.Modules.oM_InputSource.df_to_set_values(df)[source]#

Convert a dict DataFrame into the values a Pyomo Set(initialize=...) accepts. :rtype: list

  • one column -> [v1, v2, ...]

  • two+ columns -> [(a, b, ...), ...] (relation / membership)

Parameters:

df (DataFrame)

Return type:

list