flextool 4.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- flextool/__init__.py +41 -0
- flextool/_mem_sampler.py +193 -0
- flextool/_resources.py +43 -0
- flextool/calibrate/__init__.py +51 -0
- flextool/calibrate/__main__.py +11 -0
- flextool/calibrate/_cli.py +316 -0
- flextool/calibrate/_db_alt.py +166 -0
- flextool/calibrate/_final_outputs.py +110 -0
- flextool/calibrate/_guard.py +151 -0
- flextool/calibrate/_loop.py +558 -0
- flextool/calibrate/_readers.py +223 -0
- flextool/calibrate/_report.py +263 -0
- flextool/calibrate/_sizing.py +699 -0
- flextool/calibrate/_solve.py +134 -0
- flextool/calibrate/_solve_status.py +495 -0
- flextool/cli/__init__.py +9 -0
- flextool/cli/_console.py +51 -0
- flextool/cli/_timing.py +147 -0
- flextool/cli/cmd_execute_flextool_workflow.py +187 -0
- flextool/cli/cmd_export_to_tabular.py +56 -0
- flextool/cli/cmd_import_sensitivities.py +75 -0
- flextool/cli/cmd_migrate_database.py +13 -0
- flextool/cli/cmd_open_results_db.py +269 -0
- flextool/cli/cmd_read_matpower.py +66 -0
- flextool/cli/cmd_read_old_flextool.py +63 -0
- flextool/cli/cmd_read_self_describing_tabular_input.py +50 -0
- flextool/cli/cmd_read_tabular_input.py +81 -0
- flextool/cli/cmd_run_flextool.py +1095 -0
- flextool/cli/cmd_scenario_results.py +284 -0
- flextool/cli/cmd_solve_mps.py +169 -0
- flextool/cli/cmd_update_flextool.py +17 -0
- flextool/cli/cmd_write_outputs.py +125 -0
- flextool/common_utils/__init__.py +1 -0
- flextool/common_utils/plot_mem_shape.py +77 -0
- flextool/common_utils/precision.py +451 -0
- flextool/decomposition/__init__.py +0 -0
- flextool/decomposition/region_decomposition.py +128 -0
- flextool/decomposition/region_filter.py +1261 -0
- flextool/engine_polars/__init__.py +110 -0
- flextool/engine_polars/_axis_enums.py +742 -0
- flextool/engine_polars/_benders.py +3462 -0
- flextool/engine_polars/_block_layout.py +1479 -0
- flextool/engine_polars/_blocks.py +1515 -0
- flextool/engine_polars/_commodity_ladder.py +660 -0
- flextool/engine_polars/_cumulative_invest.py +1165 -0
- flextool/engine_polars/_db_loader.py +153 -0
- flextool/engine_polars/_db_reader.py +127 -0
- flextool/engine_polars/_dc_power_flow.py +445 -0
- flextool/engine_polars/_delay.py +442 -0
- flextool/engine_polars/_derived_arithmetic.py +432 -0
- flextool/engine_polars/_derived_block.py +990 -0
- flextool/engine_polars/_derived_branch.py +769 -0
- flextool/engine_polars/_derived_existing.py +1353 -0
- flextool/engine_polars/_derived_npv.py +1297 -0
- flextool/engine_polars/_derived_params.py +9850 -0
- flextool/engine_polars/_derived_profile.py +881 -0
- flextool/engine_polars/_derived_walks.py +276 -0
- flextool/engine_polars/_determinism.py +70 -0
- flextool/engine_polars/_direct_params.py +2186 -0
- flextool/engine_polars/_dump_csvs.py +1009 -0
- flextool/engine_polars/_emit_arc_unions.py +1631 -0
- flextool/engine_polars/_emit_calc_params.py +729 -0
- flextool/engine_polars/_emit_chain_params.py +709 -0
- flextool/engine_polars/_emit_co2_accumulators.py +400 -0
- flextool/engine_polars/_emit_dispatchers.py +690 -0
- flextool/engine_polars/_emit_energy_margin.py +125 -0
- flextool/engine_polars/_emit_energy_margin_adder.py +290 -0
- flextool/engine_polars/_emit_entity_annual.py +428 -0
- flextool/engine_polars/_emit_inflow_scaling.py +1420 -0
- flextool/engine_polars/_emit_leaf_sets.py +550 -0
- flextool/engine_polars/_emit_lp_scaling.py +665 -0
- flextool/engine_polars/_emit_mid_sets.py +859 -0
- flextool/engine_polars/_emit_pdt_params.py +759 -0
- flextool/engine_polars/_emit_per_solve.py +774 -0
- flextool/engine_polars/_emit_period_calc.py +504 -0
- flextool/engine_polars/_emit_period_params.py +2398 -0
- flextool/engine_polars/_emit_provider_io.py +141 -0
- flextool/engine_polars/_emit_reserve.py +574 -0
- flextool/engine_polars/_emit_solve_time.py +311 -0
- flextool/engine_polars/_emit_solve_writers.py +1249 -0
- flextool/engine_polars/_flex_data_accumulator.py +388 -0
- flextool/engine_polars/_flex_data_provider.py +478 -0
- flextool/engine_polars/_group_slack.py +1253 -0
- flextool/engine_polars/_inmemory_reader.py +140 -0
- flextool/engine_polars/_input_source.py +336 -0
- flextool/engine_polars/_invest_seeds.py +191 -0
- flextool/engine_polars/_native_input_writer.py +100 -0
- flextool/engine_polars/_native_run_model.py +1348 -0
- flextool/engine_polars/_orchestration.py +4314 -0
- flextool/engine_polars/_output_writer.py +439 -0
- flextool/engine_polars/_param_shapes.py +1595 -0
- flextool/engine_polars/_parquet_bundle.py +723 -0
- flextool/engine_polars/_pdt_join.py +167 -0
- flextool/engine_polars/_pdt_lookup.py +547 -0
- flextool/engine_polars/_per_solve_sets.py +335 -0
- flextool/engine_polars/_projection_params.py +2056 -0
- flextool/engine_polars/_provider_keys.py +173 -0
- flextool/engine_polars/_provider_translators.py +225 -0
- flextool/engine_polars/_recursive_solve.py +703 -0
- flextool/engine_polars/_region_filter.py +2508 -0
- flextool/engine_polars/_reserve.py +649 -0
- flextool/engine_polars/_solve_acceptance.py +331 -0
- flextool/engine_polars/_solve_config.py +1001 -0
- flextool/engine_polars/_solve_context.py +885 -0
- flextool/engine_polars/_solve_handoff.py +164 -0
- flextool/engine_polars/_solve_state.py +232 -0
- flextool/engine_polars/_solver_base.py +36 -0
- flextool/engine_polars/_solver_dispatch.py +511 -0
- flextool/engine_polars/_spinedb_reader.py +1165 -0
- flextool/engine_polars/_stochastic.py +593 -0
- flextool/engine_polars/_subprocess_solve.py +1838 -0
- flextool/engine_polars/_timeline.py +1416 -0
- flextool/engine_polars/_vectorize.py +438 -0
- flextool/engine_polars/_warm.py +858 -0
- flextool/engine_polars/autoscale/__init__.py +107 -0
- flextool/engine_polars/autoscale/_config.py +218 -0
- flextool/engine_polars/autoscale/_layer2.py +1253 -0
- flextool/engine_polars/autoscale/_layer2_types.py +584 -0
- flextool/engine_polars/autoscale/_quantity_types.py +621 -0
- flextool/engine_polars/autoscale/_report.py +336 -0
- flextool/engine_polars/chain.py +259 -0
- flextool/engine_polars/input.py +6638 -0
- flextool/engine_polars/model.py +4754 -0
- flextool/env_check.py +388 -0
- flextool/export_to_tabular/__init__.py +5 -0
- flextool/export_to_tabular/db_reader.py +224 -0
- flextool/export_to_tabular/excel_writer.py +3559 -0
- flextool/export_to_tabular/export_settings.yaml +377 -0
- flextool/export_to_tabular/export_to_excel.py +227 -0
- flextool/export_to_tabular/formatting.py +543 -0
- flextool/export_to_tabular/sheet_config.py +876 -0
- flextool/gui/__init__.py +0 -0
- flextool/gui/__main__.py +118 -0
- flextool/gui/calibrate_commands.py +184 -0
- flextool/gui/calibrate_jobs.py +424 -0
- flextool/gui/check_tree.py +142 -0
- flextool/gui/cli_format.py +83 -0
- flextool/gui/config_parser.py +68 -0
- flextool/gui/data_models.py +362 -0
- flextool/gui/db_editor_integration.py +202 -0
- flextool/gui/db_version_check.py +269 -0
- flextool/gui/dialogs/__init__.py +0 -0
- flextool/gui/dialogs/add_dialog.py +1098 -0
- flextool/gui/dialogs/calibrate_dialog.py +1259 -0
- flextool/gui/dialogs/file_picker.py +473 -0
- flextool/gui/dialogs/group_picker.py +299 -0
- flextool/gui/dialogs/migration_consent_dialog.py +106 -0
- flextool/gui/dialogs/migration_progress_dialog.py +237 -0
- flextool/gui/dialogs/plot_dialog.py +459 -0
- flextool/gui/dialogs/plot_settings_picker.py +2184 -0
- flextool/gui/dialogs/project_dialog.py +426 -0
- flextool/gui/dialogs/update_dialog.py +212 -0
- flextool/gui/downsampling.py +88 -0
- flextool/gui/error_handling.py +50 -0
- flextool/gui/execution_manager.py +1715 -0
- flextool/gui/execution_window.py +1377 -0
- flextool/gui/hover_tooltip.py +111 -0
- flextool/gui/input_sources.py +730 -0
- flextool/gui/main_window.py +6181 -0
- flextool/gui/network_graph.py +215 -0
- flextool/gui/output_actions.py +393 -0
- flextool/gui/output_log_window.py +159 -0
- flextool/gui/platform_utils.py +421 -0
- flextool/gui/plot_cache.py +88 -0
- flextool/gui/plot_canvas.py +543 -0
- flextool/gui/plot_config_reader.py +272 -0
- flextool/gui/project_utils.py +100 -0
- flextool/gui/result_viewer.py +4394 -0
- flextool/gui/scenario_key.py +162 -0
- flextool/gui/scenario_lists.py +516 -0
- flextool/gui/settings_io.py +360 -0
- flextool/gui/solve_reader.py +103 -0
- flextool/gui/tree_reorder.py +88 -0
- flextool/gui/ui_metrics.py +420 -0
- flextool/input_derivation/__init__.py +281 -0
- flextool/input_derivation/_commodity_ladder.py +375 -0
- flextool/input_derivation/_commodity_ladder_sets.py +70 -0
- flextool/input_derivation/_dc_power_flow.py +377 -0
- flextool/input_derivation/_method_constants.py +77 -0
- flextool/input_derivation/_process_method.py +258 -0
- flextool/input_derivation/_specs.py +1026 -0
- flextool/input_derivation/_validators.py +321 -0
- flextool/lean_parquet.py +159 -0
- flextool/model_builder/__init__.py +5 -0
- flextool/model_builder/build_model.py +589 -0
- flextool/model_builder/encoding.py +67 -0
- flextool/model_builder/names.py +34 -0
- flextool/model_builder/profiles.py +129 -0
- flextool/plot_outputs/__init__.py +14 -0
- flextool/plot_outputs/axis_helpers.py +355 -0
- flextool/plot_outputs/color_template.py +888 -0
- flextool/plot_outputs/config.py +171 -0
- flextool/plot_outputs/format_helpers.py +345 -0
- flextool/plot_outputs/legend_helpers.py +143 -0
- flextool/plot_outputs/orchestrator.py +1141 -0
- flextool/plot_outputs/perf.py +37 -0
- flextool/plot_outputs/plan.py +1787 -0
- flextool/plot_outputs/plot_bars.py +1510 -0
- flextool/plot_outputs/plot_bars_detail.py +753 -0
- flextool/plot_outputs/plot_lines.py +951 -0
- flextool/plot_outputs/shared_manifest.py +564 -0
- flextool/plot_outputs/subplot_helpers.py +137 -0
- flextool/process_inputs/__init__.py +188 -0
- flextool/process_inputs/import_old_excel_input.json +4159 -0
- flextool/process_inputs/read_matpower.py +451 -0
- flextool/process_inputs/read_old_flextool.py +1288 -0
- flextool/process_inputs/read_self_describing_excel.py +1423 -0
- flextool/process_inputs/read_tabular_with_specification.py +1114 -0
- flextool/process_inputs/write_old_flextool_to_db.py +3077 -0
- flextool/process_inputs/write_self_describing_to_db.py +977 -0
- flextool/process_inputs/write_to_input_db.py +269 -0
- flextool/process_outputs/__init__.py +7 -0
- flextool/process_outputs/_annualize.py +55 -0
- flextool/process_outputs/_inmemory_helpers.py +292 -0
- flextool/process_outputs/_output_meta.py +672 -0
- flextool/process_outputs/calc_capacity_flows.py +107 -0
- flextool/process_outputs/calc_connections.py +136 -0
- flextool/process_outputs/calc_costs.py +260 -0
- flextool/process_outputs/calc_group_flows.py +192 -0
- flextool/process_outputs/calc_slacks.py +103 -0
- flextool/process_outputs/calc_storage_vre.py +160 -0
- flextool/process_outputs/drop_levels.py +208 -0
- flextool/process_outputs/handoff_writers.py +1315 -0
- flextool/process_outputs/out_ancillary.py +544 -0
- flextool/process_outputs/out_capacity.py +179 -0
- flextool/process_outputs/out_costs.py +334 -0
- flextool/process_outputs/out_flowgroup.py +189 -0
- flextool/process_outputs/out_flows.py +301 -0
- flextool/process_outputs/out_group.py +475 -0
- flextool/process_outputs/out_node.py +190 -0
- flextool/process_outputs/persist_realized_slice.py +601 -0
- flextool/process_outputs/process_results.py +24 -0
- flextool/process_outputs/read_highs_solution.py +2256 -0
- flextool/process_outputs/read_parameters.py +1799 -0
- flextool/process_outputs/read_sets.py +1095 -0
- flextool/process_outputs/read_variables.py +553 -0
- flextool/process_outputs/solve_order.py +81 -0
- flextool/process_outputs/spinedb_replay.py +412 -0
- flextool/process_outputs/union_realized_slice.py +224 -0
- flextool/process_outputs/write_outputs.py +1286 -0
- flextool/process_outputs/write_spinedb.py +1267 -0
- flextool/representative_periods/__init__.py +5 -0
- flextool/representative_periods/clustering.py +165 -0
- flextool/representative_periods/force_include.py +563 -0
- flextool/representative_periods/netload.py +365 -0
- flextool/representative_periods/netload_inputs.py +345 -0
- flextool/representative_periods/netload_iterate.py +722 -0
- flextool/representative_periods/preprocess.py +948 -0
- flextool/representative_periods/scenario_stack.py +195 -0
- flextool/representative_periods/weights.py +124 -0
- flextool/scenario_comparison/__init__.py +13 -0
- flextool/scenario_comparison/config_builder.py +158 -0
- flextool/scenario_comparison/constants.py +20 -0
- flextool/scenario_comparison/data_models.py +222 -0
- flextool/scenario_comparison/db_reader.py +399 -0
- flextool/scenario_comparison/dispatch_data.py +1002 -0
- flextool/scenario_comparison/dispatch_mappings.py +205 -0
- flextool/scenario_comparison/dispatch_plots.py +691 -0
- flextool/scenario_comparison/input_entity_colors.py +319 -0
- flextool/scenario_comparison/orchestrator.py +453 -0
- flextool/scenario_comparison/plan_union.py +244 -0
- flextool/scenario_comparison/plot_settings_seed.py +205 -0
- flextool/schemas/AXIS_CONTRACT.md +71 -0
- flextool/schemas/canonical_databases/howto_aggregate_output.json +6225 -0
- flextool/schemas/canonical_databases/howto_connections.json +5606 -0
- flextool/schemas/canonical_databases/howto_demand.json +5518 -0
- flextool/schemas/canonical_databases/howto_hydro_reservoir.json +6239 -0
- flextool/schemas/canonical_databases/howto_hydro_reservoir_with_pump.json +5933 -0
- flextool/schemas/canonical_databases/howto_non_sync_and_curtailment.json +5794 -0
- flextool/schemas/canonical_databases/howto_ramp_and_start_up.json +5707 -0
- flextool/schemas/canonical_databases/howto_stochastics.json +6032 -0
- flextool/schemas/canonical_databases/templates_examples.json +13532 -0
- flextool/schemas/canonical_databases/templates_time_settings_only.json +5340 -0
- flextool/schemas/comparison_settings_template.json +197 -0
- flextool/schemas/default_plot_settings.yaml +260 -0
- flextool/schemas/default_plots.yaml +2293 -0
- flextool/schemas/flextool_axis_contract.json +303 -0
- flextool/schemas/flextool_axis_contract.schema.json +247 -0
- flextool/schemas/old_flextool_import_template.json +4443 -0
- flextool/schemas/output_info_template.json +48 -0
- flextool/schemas/output_settings_template.json +256 -0
- flextool/schemas/pre_v26/flextool_template_constant_default.json +2105 -0
- flextool/schemas/pre_v26/flextool_template_default_optional_output.json +2152 -0
- flextool/schemas/pre_v26/flextool_template_default_value.json +2094 -0
- flextool/schemas/pre_v26/flextool_template_drop_down.json +2080 -0
- flextool/schemas/pre_v26/flextool_template_lifetime_method.json +1990 -0
- flextool/schemas/pre_v26/flextool_template_optional_outputs.json +2094 -0
- flextool/schemas/pre_v26/flextool_template_output_node_flows.json +2105 -0
- flextool/schemas/pre_v26/flextool_template_results_master.json +493 -0
- flextool/schemas/pre_v26/flextool_template_rolling_start_remove.json +2087 -0
- flextool/schemas/pre_v26/flextool_template_rolling_window.json +2059 -0
- flextool/schemas/pre_v26/flextool_template_storage_binding_defaults.json +46 -0
- flextool/schemas/pre_v26/flextool_template_v2.json +1990 -0
- flextool/schemas/pre_v26/flextool_template_v25.json +3864 -0
- flextool/schemas/spinedb_results_schema.json +581 -0
- flextool/schemas/spinedb_schema.json +4636 -0
- flextool/solver_config/copt.opt.template +18 -0
- flextool/solver_config/cplex.opt.template +25 -0
- flextool/solver_config/gurobi.opt.template +18 -0
- flextool/solver_config/highs.opt.template +18 -0
- flextool/solver_config/xpress.opt.template +26 -0
- flextool/spinedb_backend/__init__.py +26 -0
- flextool/spinedb_backend/_axis_enums.py +1119 -0
- flextool/spinedb_backend/_backend.py +1139 -0
- flextool/update_flextool/__init__.py +12 -0
- flextool/update_flextool/canonical_databases.py +251 -0
- flextool/update_flextool/db_migration.py +7108 -0
- flextool/update_flextool/ensure_settings_db.py +138 -0
- flextool/update_flextool/export_database.py +103 -0
- flextool/update_flextool/extend_tests_fixture.py +772 -0
- flextool/update_flextool/generate_canonical.py +274 -0
- flextool/update_flextool/initialize_database.py +42 -0
- flextool/update_flextool/install_info.py +225 -0
- flextool/update_flextool/self_update.py +464 -0
- flextool/update_flextool/sync_master_json_template.py +125 -0
- flextool/update_flextool/test_fixtures.py +187 -0
- flextool-4.0.0.dist-info/METADATA +217 -0
- flextool-4.0.0.dist-info/RECORD +322 -0
- flextool-4.0.0.dist-info/WHEEL +5 -0
- flextool-4.0.0.dist-info/entry_points.txt +17 -0
- flextool-4.0.0.dist-info/licenses/LICENSE.txt +19 -0
- flextool-4.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
"""Annuity / lifetime-fixed-cost per-solve emitters.
|
|
2
|
+
|
|
3
|
+
Called per-solve via ``write_entity_annual_calc_params``.
|
|
4
|
+
|
|
5
|
+
The six CSVs emitted are precision-sensitive: the values are float
|
|
6
|
+
annuities and discounted sums. We pre-stringify with ``repr(float(v))``
|
|
7
|
+
to keep round-trip safety byte-for-byte.
|
|
8
|
+
|
|
9
|
+
Outputs (all written under ``solve_data/``):
|
|
10
|
+
|
|
11
|
+
* ``ed_entity_annual.csv`` (entity, period, value)
|
|
12
|
+
* ``ed_entity_annual_discounted.csv`` (entity, period, value)
|
|
13
|
+
* ``ed_entity_annual_divest.csv`` (entity, period, value)
|
|
14
|
+
* ``ed_entity_annual_divest_discounted.csv`` (entity, period, value)
|
|
15
|
+
* ``ed_lifetime_fixed_cost.csv`` (entity, period, value)
|
|
16
|
+
* ``ed_lifetime_fixed_cost_divest.csv`` (entity, period, value)
|
|
17
|
+
|
|
18
|
+
Method-enum constants mirror flextool/flextool_base.dat:184 and
|
|
19
|
+
:211-212. ``PdLookup`` is the native 4-branch resolver from
|
|
20
|
+
:mod:`._pdt_lookup` (same machinery used by ``_emit_dispatchers`` and
|
|
21
|
+
``_emit_period_params``).
|
|
22
|
+
"""
|
|
23
|
+
from __future__ import annotations
|
|
24
|
+
|
|
25
|
+
from pathlib import Path
|
|
26
|
+
|
|
27
|
+
import polars as pl
|
|
28
|
+
|
|
29
|
+
from flextool.engine_polars._pdt_lookup import PdLookup
|
|
30
|
+
from flextool.engine_polars._emit_provider_io import _emit
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
# ---------------------------------------------------------------------------
|
|
34
|
+
# Method-enum constants — mirror flextool/flextool_base.dat:211-212.
|
|
35
|
+
# ---------------------------------------------------------------------------
|
|
36
|
+
|
|
37
|
+
_INVEST_NOT_ALLOWED: frozenset[str] = frozenset((
|
|
38
|
+
"not_allowed", "retire_period", "retire_total", "retire_no_limit",
|
|
39
|
+
))
|
|
40
|
+
_DIVEST_NOT_ALLOWED: frozenset[str] = frozenset((
|
|
41
|
+
"not_allowed", "invest_period", "invest_total", "invest_no_limit",
|
|
42
|
+
))
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
# ---------------------------------------------------------------------------
|
|
46
|
+
# CSV I/O helpers — same conventions as ``_emit_per_solve``.
|
|
47
|
+
# ---------------------------------------------------------------------------
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _read_csv(path: Path, columns: list[str],
|
|
51
|
+
*, provider: "object | None" = None) -> pl.DataFrame:
|
|
52
|
+
"""Provider-only — returns empty all-Utf8 frame on Provider miss.
|
|
53
|
+
Step 2.5 Phase C dropped the disk-fallback arm."""
|
|
54
|
+
from flextool.engine_polars._emit_provider_io import (
|
|
55
|
+
_provider_key,
|
|
56
|
+
_provider_lookup_positional,
|
|
57
|
+
)
|
|
58
|
+
seeded = _provider_lookup_positional(
|
|
59
|
+
provider, _provider_key(path), path, columns,
|
|
60
|
+
)
|
|
61
|
+
if seeded is not None:
|
|
62
|
+
return seeded
|
|
63
|
+
return pl.DataFrame(
|
|
64
|
+
{c: [] for c in columns},
|
|
65
|
+
schema={c: pl.Utf8 for c in columns},
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _read_singles(path: Path,
|
|
70
|
+
*, provider: "object | None" = None) -> list[str]:
|
|
71
|
+
df = _read_csv(path, ["v"], provider=provider)
|
|
72
|
+
return [v for v in df["v"].to_list() if v]
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _read_pairs(path: Path,
|
|
76
|
+
*, provider: "object | None" = None) -> list[tuple[str, str]]:
|
|
77
|
+
df = _read_csv(path, ["a", "b"], provider=provider)
|
|
78
|
+
return [(a, b) for a, b in zip(df["a"].to_list(), df["b"].to_list())
|
|
79
|
+
if a and b]
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _read_keyed_value(path: Path,
|
|
83
|
+
*, provider: "object | None" = None) -> dict[str, float]:
|
|
84
|
+
"""``(key, value)`` 2-col CSV → ``dict[str, float]``, blanks skipped."""
|
|
85
|
+
df = _read_csv(path, ["key", "value"], provider=provider)
|
|
86
|
+
out: dict[str, float] = {}
|
|
87
|
+
for k, v in zip(df["key"].to_list(), df["value"].to_list()):
|
|
88
|
+
if not k or v is None or v == "":
|
|
89
|
+
continue
|
|
90
|
+
try:
|
|
91
|
+
out[k] = float(v)
|
|
92
|
+
except ValueError:
|
|
93
|
+
continue
|
|
94
|
+
return out
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def _read_pdv(path: Path,
|
|
98
|
+
*, provider: "object | None" = None) -> dict[tuple[str, str], float]:
|
|
99
|
+
"""``(entity, period, value)`` 3-col CSV → dict."""
|
|
100
|
+
df = _read_csv(path, ["entity", "period", "value"], provider=provider)
|
|
101
|
+
out: dict[tuple[str, str], float] = {}
|
|
102
|
+
for e, d, v in zip(df["entity"].to_list(),
|
|
103
|
+
df["period"].to_list(),
|
|
104
|
+
df["value"].to_list()):
|
|
105
|
+
if not e or not d:
|
|
106
|
+
continue
|
|
107
|
+
try:
|
|
108
|
+
out[(e, d)] = float(v)
|
|
109
|
+
except (ValueError, TypeError):
|
|
110
|
+
continue
|
|
111
|
+
return out
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def _rows_to_frame(rows: list[tuple[str, str, float]]) -> pl.DataFrame:
|
|
115
|
+
"""Materialise (entity, period, repr(value)) rows as an all-Utf8 frame.
|
|
116
|
+
|
|
117
|
+
Pre-stringifies values via ``repr(float(v))`` so the polars
|
|
118
|
+
``write_csv`` output is byte-identical to the legacy
|
|
119
|
+
``f"{a},{b},{repr(float(v))}\\n"`` text emitter.
|
|
120
|
+
"""
|
|
121
|
+
return pl.DataFrame(
|
|
122
|
+
{
|
|
123
|
+
"entity": [r[0] for r in rows],
|
|
124
|
+
"period": [r[1] for r in rows],
|
|
125
|
+
"value": [repr(float(r[2])) for r in rows],
|
|
126
|
+
},
|
|
127
|
+
schema={"entity": pl.Utf8, "period": pl.Utf8, "value": pl.Utf8},
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
# ---------------------------------------------------------------------------
|
|
132
|
+
# Numeric primitives.
|
|
133
|
+
# ---------------------------------------------------------------------------
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def _annuity(invest_value: float, discount_rate: float, lifetime: float) -> float:
|
|
137
|
+
"""Mod-faithful annuity: inv * 1000 * r / (1 - 1/(1+r)^n).
|
|
138
|
+
|
|
139
|
+
Falls back to ``r = 0.05`` when discount_rate ≤ 0 and ``n = 20`` when
|
|
140
|
+
lifetime ≤ 0 (mirrors the ``else 0.05`` / ``else 20`` legs in
|
|
141
|
+
``flextool.mod``).
|
|
142
|
+
"""
|
|
143
|
+
r = discount_rate if discount_rate > 0 else 0.05
|
|
144
|
+
n = lifetime if lifetime > 0 else 20.0
|
|
145
|
+
if r == 0:
|
|
146
|
+
return 0.0
|
|
147
|
+
return invest_value * 1000.0 * r / (1.0 - (1.0 / (1.0 + r)) ** n)
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def _inflation_window_sum(
|
|
151
|
+
pdy_d: float,
|
|
152
|
+
life: float,
|
|
153
|
+
period_in_use: list[str],
|
|
154
|
+
p_discount_years: dict[str, float],
|
|
155
|
+
inflation: dict[str, float],
|
|
156
|
+
open_window: bool,
|
|
157
|
+
) -> float:
|
|
158
|
+
"""Σ inflation[d_all] over period_in_use with the lifetime window.
|
|
159
|
+
|
|
160
|
+
``open_window=True`` matches reinvest_automatic — sums forward
|
|
161
|
+
from ``pdy_d`` with no upper bound. ``open_window=False`` matches
|
|
162
|
+
reinvest_choice / no_investment — sums only while
|
|
163
|
+
``pdy < pdy_d + life``.
|
|
164
|
+
"""
|
|
165
|
+
s = 0.0
|
|
166
|
+
upper = pdy_d + life
|
|
167
|
+
for d_all in period_in_use:
|
|
168
|
+
pdy = p_discount_years.get(d_all, 0.0)
|
|
169
|
+
if pdy < pdy_d:
|
|
170
|
+
continue
|
|
171
|
+
if not open_window and pdy >= upper:
|
|
172
|
+
continue
|
|
173
|
+
s += inflation.get(d_all, 1.0)
|
|
174
|
+
return s
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
# ---------------------------------------------------------------------------
|
|
178
|
+
# Top-level writer.
|
|
179
|
+
# ---------------------------------------------------------------------------
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
# ---------------------------------------------------------------------------
|
|
183
|
+
# Phase E-b — derive_X family for each emitted CSV.
|
|
184
|
+
#
|
|
185
|
+
# The 6 CSVs share substantial input scans (PdLookup over pd_*, p_*, plus
|
|
186
|
+
# entity-class sets and period lists), so the wrapper builds the bundle
|
|
187
|
+
# once via :func:`_entity_annual_inputs` and threads it through private
|
|
188
|
+
# ``_compute_*`` helpers. Public ``derive_*`` functions are thin
|
|
189
|
+
# rebuilders for standalone seed lookups.
|
|
190
|
+
# ---------------------------------------------------------------------------
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def _entity_annual_inputs(input_dir: Path, solve_data_dir: Path,
|
|
194
|
+
*, provider: "object | None" = None) -> dict:
|
|
195
|
+
"""Shared input bundle for the 6 entity-annual derives."""
|
|
196
|
+
pp = PdLookup(
|
|
197
|
+
pd_csv=input_dir / "pd_process.csv",
|
|
198
|
+
p_csv=input_dir / "p_process.csv",
|
|
199
|
+
period_branch_csv=solve_data_dir / "period__branch.csv",
|
|
200
|
+
provider=provider,
|
|
201
|
+
)
|
|
202
|
+
pn = PdLookup(
|
|
203
|
+
pd_csv=input_dir / "pd_node.csv",
|
|
204
|
+
p_csv=input_dir / "p_node.csv",
|
|
205
|
+
period_branch_csv=solve_data_dir / "period__branch.csv",
|
|
206
|
+
provider=provider,
|
|
207
|
+
)
|
|
208
|
+
methods_for_entity: dict[str, list[str]] = {}
|
|
209
|
+
for e, m in _read_pairs(input_dir / "entity__invest_method.csv",
|
|
210
|
+
provider=provider):
|
|
211
|
+
methods_for_entity.setdefault(e, []).append(m)
|
|
212
|
+
lifetime_methods_for_entity: dict[str, list[str]] = {}
|
|
213
|
+
for e, m in _read_pairs(solve_data_dir / "entity__lifetime_method.csv",
|
|
214
|
+
provider=provider):
|
|
215
|
+
lifetime_methods_for_entity.setdefault(e, []).append(m)
|
|
216
|
+
return {
|
|
217
|
+
"pp": pp,
|
|
218
|
+
"pn": pn,
|
|
219
|
+
"process_set": frozenset(_read_singles(input_dir / "process.csv", provider=provider)),
|
|
220
|
+
"node_set": frozenset(_read_singles(input_dir / "node.csv", provider=provider)),
|
|
221
|
+
"entityInvest": _read_singles(solve_data_dir / "entityInvest.csv", provider=provider),
|
|
222
|
+
"entityDivest": _read_singles(solve_data_dir / "entityDivest.csv", provider=provider),
|
|
223
|
+
"period_invest": _read_singles(
|
|
224
|
+
solve_data_dir / "invest_periods_of_current_solve.csv", provider=provider),
|
|
225
|
+
"period_in_use": _read_singles(
|
|
226
|
+
solve_data_dir / "period_in_use_set.csv", provider=provider),
|
|
227
|
+
"methods_for_entity": methods_for_entity,
|
|
228
|
+
"lifetime_methods_for_entity": lifetime_methods_for_entity,
|
|
229
|
+
"p_discount_years": _read_keyed_value(
|
|
230
|
+
solve_data_dir / "p_discount_years.csv", provider=provider),
|
|
231
|
+
"p_inflation_invest": _read_keyed_value(
|
|
232
|
+
solve_data_dir / "p_inflation_factor_investment_yearly.csv", provider=provider),
|
|
233
|
+
"p_inflation_ops": _read_keyed_value(
|
|
234
|
+
solve_data_dir / "p_inflation_factor_operations_yearly.csv", provider=provider),
|
|
235
|
+
"edEntity_lifetime": _read_pdv(
|
|
236
|
+
solve_data_dir / "edEntity_lifetime.csv", provider=provider),
|
|
237
|
+
"ed_fixed_cost": _read_pdv(solve_data_dir / "ed_fixed_cost.csv", provider=provider),
|
|
238
|
+
"period_with_history": _read_singles(
|
|
239
|
+
solve_data_dir / "period_with_history.csv", provider=provider),
|
|
240
|
+
"entities": _read_singles(input_dir / "entity.csv", provider=provider),
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def _per_method_annuity_invest(inp: dict, e: str, d: str) -> float:
|
|
245
|
+
v = 0.0
|
|
246
|
+
for m in inp["methods_for_entity"].get(e, ()):
|
|
247
|
+
if m in _INVEST_NOT_ALLOWED:
|
|
248
|
+
continue
|
|
249
|
+
if e in inp["node_set"]:
|
|
250
|
+
v += _annuity(
|
|
251
|
+
inp["pn"].get(e, "invest_cost", d),
|
|
252
|
+
inp["pn"].get(e, "discount_rate", d),
|
|
253
|
+
inp["pn"].get(e, "lifetime", d),
|
|
254
|
+
)
|
|
255
|
+
elif e in inp["process_set"]:
|
|
256
|
+
v += _annuity(
|
|
257
|
+
inp["pp"].get(e, "invest_cost", d),
|
|
258
|
+
inp["pp"].get(e, "discount_rate", d),
|
|
259
|
+
inp["pp"].get(e, "lifetime", d),
|
|
260
|
+
)
|
|
261
|
+
return v
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def _per_method_annuity_divest(inp: dict, e: str, d: str) -> float:
|
|
265
|
+
v = 0.0
|
|
266
|
+
for m in inp["methods_for_entity"].get(e, ()):
|
|
267
|
+
if m in _DIVEST_NOT_ALLOWED:
|
|
268
|
+
continue
|
|
269
|
+
if e in inp["node_set"]:
|
|
270
|
+
v += _annuity(
|
|
271
|
+
inp["pn"].get(e, "salvage_value", d),
|
|
272
|
+
inp["pn"].get(e, "discount_rate", d),
|
|
273
|
+
inp["pn"].get(e, "lifetime", d),
|
|
274
|
+
)
|
|
275
|
+
elif e in inp["process_set"]:
|
|
276
|
+
v += _annuity(
|
|
277
|
+
inp["pp"].get(e, "salvage_value", d),
|
|
278
|
+
inp["pp"].get(e, "discount_rate", d),
|
|
279
|
+
inp["pp"].get(e, "lifetime", d),
|
|
280
|
+
)
|
|
281
|
+
return v
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
def _ann_pair(inp: dict) -> tuple[
|
|
285
|
+
list[tuple[str, str, float]], list[tuple[str, str, float]]
|
|
286
|
+
]:
|
|
287
|
+
"""Compute both ed_entity_annual and ed_entity_annual_discounted in one
|
|
288
|
+
pass (they share the per-method annuity scan)."""
|
|
289
|
+
rows_ann: list[tuple[str, str, float]] = []
|
|
290
|
+
rows_ann_disc: list[tuple[str, str, float]] = []
|
|
291
|
+
for e in inp["entityInvest"]:
|
|
292
|
+
elm_set = frozenset(inp["lifetime_methods_for_entity"].get(e, ()))
|
|
293
|
+
is_choice_or_no_invest = (
|
|
294
|
+
"reinvest_choice" in elm_set or "no_investment" in elm_set
|
|
295
|
+
)
|
|
296
|
+
is_automatic = "reinvest_automatic" in elm_set
|
|
297
|
+
for d in inp["period_invest"]:
|
|
298
|
+
ann = _per_method_annuity_invest(inp, e, d)
|
|
299
|
+
rows_ann.append((e, d, ann))
|
|
300
|
+
|
|
301
|
+
disc = 0.0
|
|
302
|
+
pdy_d = inp["p_discount_years"].get(d, 0.0)
|
|
303
|
+
life = inp["edEntity_lifetime"].get((e, d), 0.0)
|
|
304
|
+
if is_choice_or_no_invest:
|
|
305
|
+
disc += ann * _inflation_window_sum(
|
|
306
|
+
pdy_d, life, inp["period_in_use"],
|
|
307
|
+
inp["p_discount_years"], inp["p_inflation_invest"],
|
|
308
|
+
open_window=False,
|
|
309
|
+
)
|
|
310
|
+
if is_automatic:
|
|
311
|
+
disc += ann * _inflation_window_sum(
|
|
312
|
+
pdy_d, life, inp["period_in_use"],
|
|
313
|
+
inp["p_discount_years"], inp["p_inflation_invest"],
|
|
314
|
+
open_window=True,
|
|
315
|
+
)
|
|
316
|
+
rows_ann_disc.append((e, d, disc))
|
|
317
|
+
return rows_ann, rows_ann_disc
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
def _div_pair(inp: dict) -> tuple[
|
|
321
|
+
list[tuple[str, str, float]], list[tuple[str, str, float]]
|
|
322
|
+
]:
|
|
323
|
+
"""Compute both ed_entity_annual_divest and its _discounted variant."""
|
|
324
|
+
rows_div: list[tuple[str, str, float]] = []
|
|
325
|
+
rows_div_disc: list[tuple[str, str, float]] = []
|
|
326
|
+
for e in inp["entityDivest"]:
|
|
327
|
+
for d in inp["period_invest"]:
|
|
328
|
+
ann = _per_method_annuity_divest(inp, e, d)
|
|
329
|
+
rows_div.append((e, d, ann))
|
|
330
|
+
|
|
331
|
+
pdy_d = inp["p_discount_years"].get(d, 0.0)
|
|
332
|
+
disc = 0.0
|
|
333
|
+
if e in inp["node_set"]:
|
|
334
|
+
life = inp["pn"].get(e, "lifetime", d)
|
|
335
|
+
disc = ann * _inflation_window_sum(
|
|
336
|
+
pdy_d, life, inp["period_in_use"],
|
|
337
|
+
inp["p_discount_years"], inp["p_inflation_invest"],
|
|
338
|
+
open_window=False,
|
|
339
|
+
)
|
|
340
|
+
elif e in inp["process_set"]:
|
|
341
|
+
life = inp["pp"].get(e, "lifetime", d)
|
|
342
|
+
disc = ann * _inflation_window_sum(
|
|
343
|
+
pdy_d, life, inp["period_in_use"],
|
|
344
|
+
inp["p_discount_years"], inp["p_inflation_invest"],
|
|
345
|
+
open_window=False,
|
|
346
|
+
)
|
|
347
|
+
rows_div_disc.append((e, d, disc))
|
|
348
|
+
return rows_div, rows_div_disc
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
def _rows_lifetime_fixed_cost(inp: dict) -> list[tuple[str, str, float]]:
|
|
352
|
+
rows: list[tuple[str, str, float]] = []
|
|
353
|
+
for e in inp["entities"]:
|
|
354
|
+
elm_set = frozenset(inp["lifetime_methods_for_entity"].get(e, ()))
|
|
355
|
+
is_choice_or_no_invest = (
|
|
356
|
+
"reinvest_choice" in elm_set or "no_investment" in elm_set
|
|
357
|
+
)
|
|
358
|
+
is_automatic = "reinvest_automatic" in elm_set
|
|
359
|
+
for d in inp["period_with_history"]:
|
|
360
|
+
fc = inp["ed_fixed_cost"].get((e, d), 0.0)
|
|
361
|
+
v = 0.0
|
|
362
|
+
pdy_d = inp["p_discount_years"].get(d, 0.0)
|
|
363
|
+
life = inp["edEntity_lifetime"].get((e, d), 0.0)
|
|
364
|
+
if is_choice_or_no_invest:
|
|
365
|
+
v += fc * _inflation_window_sum(
|
|
366
|
+
pdy_d, life, inp["period_in_use"],
|
|
367
|
+
inp["p_discount_years"], inp["p_inflation_ops"],
|
|
368
|
+
open_window=False,
|
|
369
|
+
)
|
|
370
|
+
if is_automatic:
|
|
371
|
+
v += fc * _inflation_window_sum(
|
|
372
|
+
pdy_d, life, inp["period_in_use"],
|
|
373
|
+
inp["p_discount_years"], inp["p_inflation_ops"],
|
|
374
|
+
open_window=True,
|
|
375
|
+
)
|
|
376
|
+
rows.append((e, d, v))
|
|
377
|
+
return rows
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
def _rows_lifetime_fixed_cost_divest(inp: dict) -> list[tuple[str, str, float]]:
|
|
381
|
+
# NB: mod L1651 deliberately uses p_inflation_factor_INVESTMENT_yearly
|
|
382
|
+
# here (not operations-yearly) — asymmetric vs the non-divest variant.
|
|
383
|
+
rows: list[tuple[str, str, float]] = []
|
|
384
|
+
for e in inp["entityDivest"]:
|
|
385
|
+
for d in inp["period_invest"]:
|
|
386
|
+
fc = inp["ed_fixed_cost"].get((e, d), 0.0)
|
|
387
|
+
pdy_d = inp["p_discount_years"].get(d, 0.0)
|
|
388
|
+
if e in inp["node_set"]:
|
|
389
|
+
life = inp["pn"].get(e, "lifetime", d)
|
|
390
|
+
elif e in inp["process_set"]:
|
|
391
|
+
life = inp["pp"].get(e, "lifetime", d)
|
|
392
|
+
else:
|
|
393
|
+
life = 0.0
|
|
394
|
+
v = fc * _inflation_window_sum(
|
|
395
|
+
pdy_d, life, inp["period_in_use"],
|
|
396
|
+
inp["p_discount_years"], inp["p_inflation_invest"],
|
|
397
|
+
open_window=False,
|
|
398
|
+
)
|
|
399
|
+
rows.append((e, d, v))
|
|
400
|
+
return rows
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
def emit_entity_annual_calc_params(
|
|
404
|
+
input_dir: Path, solve_data_dir: Path,
|
|
405
|
+
*, provider,
|
|
406
|
+
) -> None:
|
|
407
|
+
"""Emit ``entity_annual_calc_params`` to the Provider.
|
|
408
|
+
Emits the same six frames under ``solve_data/<basename>`` keys via
|
|
409
|
+
:func:`_emit` (dual-key registration: basename and parent/basename).
|
|
410
|
+
"""
|
|
411
|
+
inp = _entity_annual_inputs(input_dir, solve_data_dir, provider=provider)
|
|
412
|
+
|
|
413
|
+
rows_ann, rows_ann_disc = _ann_pair(inp)
|
|
414
|
+
_emit(provider, "solve_data/ed_entity_annual.csv",
|
|
415
|
+
_rows_to_frame(rows_ann))
|
|
416
|
+
_emit(provider, "solve_data/ed_entity_annual_discounted.csv",
|
|
417
|
+
_rows_to_frame(rows_ann_disc))
|
|
418
|
+
|
|
419
|
+
rows_div, rows_div_disc = _div_pair(inp)
|
|
420
|
+
_emit(provider, "solve_data/ed_entity_annual_divest.csv",
|
|
421
|
+
_rows_to_frame(rows_div))
|
|
422
|
+
_emit(provider, "solve_data/ed_entity_annual_divest_discounted.csv",
|
|
423
|
+
_rows_to_frame(rows_div_disc))
|
|
424
|
+
|
|
425
|
+
_emit(provider, "solve_data/ed_lifetime_fixed_cost.csv",
|
|
426
|
+
_rows_to_frame(_rows_lifetime_fixed_cost(inp)))
|
|
427
|
+
_emit(provider, "solve_data/ed_lifetime_fixed_cost_divest.csv",
|
|
428
|
+
_rows_to_frame(_rows_lifetime_fixed_cost_divest(inp)))
|