Format Handlers¶
Base Handler¶
base
¶
Base classes for file format handlers.
This module defines the abstract interface that all file format handlers must implement, enabling a plug-and-play architecture for supporting different optimization result file formats.
Classes¶
BaseFormatHandler
¶
Bases: ABC
Abstract base class for optimization result file format handlers.
Each format handler must implement methods to: - Detect if a file is in their format - Read optimization results from files - Write optimization results to files
Functions¶
can_handle
abstractmethod
classmethod
¶
Check if this handler can read the given file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
Path
|
Path to file to check |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if this handler can read the file |
read
abstractmethod
¶
read(filepath: Path, **kwargs: Any) -> OptimizationResult
Read optimization results from file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
Path
|
Path to file to read |
required |
**kwargs
|
Any
|
Format-specific options |
{}
|
Returns:
| Type | Description |
|---|---|
OptimizationResult
|
OptimizationResult object |
Source code in optiscope/io/base.py
write
abstractmethod
¶
write(result: OptimizationResult, filepath: Path, **kwargs: Any) -> None
Write optimization results to file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
OptimizationResult
|
OptimizationResult to write |
required |
filepath
|
Path
|
Path to output file |
required |
**kwargs
|
Any
|
Format-specific options |
{}
|
Source code in optiscope/io/base.py
validate_result
¶
validate_result(result: OptimizationResult) -> None
Validate that the result can be written by this handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
OptimizationResult
|
OptimizationResult to validate |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If result cannot be written |
Source code in optiscope/io/base.py
StructuredFormatHandler
¶
Bases: BaseFormatHandler
Base class for handlers of structured formats with explicit metadata.
Structured formats (like JSON, YAML, HDF5) can store metadata alongside data, making round-trip reading/writing lossless.
TabularFormatHandler
¶
Bases: BaseFormatHandler
Base class for handlers of tabular formats (CSV, Excel, etc.).
Tabular formats typically store only data without metadata, requiring inference or separate metadata files.
Functions¶
infer_column_categories
¶
infer_column_categories(columns: list[str], design_var_prefix: str | None = None, objective_prefix: str | None = None, equality_constraint_prefix: str | None = None, inequality_constraint_prefix: str | None = None, observable_prefix: str | None = None) -> tuple[dict[str, list[str]], dict[str, str]]
Infer which columns belong to which category based on naming.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
columns
|
list[str]
|
List of column names |
required |
design_var_prefix
|
str | None
|
Prefix for design variables |
None
|
objective_prefix
|
str | None
|
Prefix for objectives |
None
|
equality_constraint_prefix
|
str | None
|
Prefix for constraints |
None
|
inequality_constraint_prefix
|
str | None
|
Prefix for constraints |
None
|
observable_prefix
|
str | None
|
Prefix for observables |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, list[str]]
|
A tuple containing: |
dict[str, str]
|
|
tuple[dict[str, list[str]], dict[str, str]]
|
|
Source code in optiscope/io/base.py
create_default_metadata
¶
create_default_metadata(n_points: int, categorized_columns: dict[str, list[str]]) -> ProblemMetadata
Create default problem metadata when none is available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_points
|
int
|
Number of data points |
required |
categorized_columns
|
dict[str, list[str]]
|
Categorized column names |
required |
Returns:
| Type | Description |
|---|---|
ProblemMetadata
|
ProblemMetadata with inferred values |
Source code in optiscope/io/base.py
FormatDetectionError
¶
Bases: Exception
Exception raised when file format cannot be detected.
FormatReadError
¶
Bases: Exception
Exception raised when file cannot be read.
FormatWriteError
¶
Bases: Exception
Exception raised when file cannot be written.
CSV Handler¶
csv_handler
¶
CSV file format handler for optimization results.
Supports reading and writing optimization results in CSV format with configurable column naming conventions and optional metadata sidecar files.
Classes¶
CSVHandler
¶
Bases: TabularFormatHandler
Handler for CSV files containing optimization results.
CSV files can be read with automatic column categorization based on naming conventions. Metadata can be stored in a separate JSON sidecar file.
Functions¶
can_handle
classmethod
¶
Check if file is a CSV.
Source code in optiscope/io/csv_handler.py
read
¶
read(filepath: Path, design_var_prefix: str | None = None, objective_prefix: str | None = None, inequality_constraint_prefix: str | None = None, equality_constraint_prefix: str | None = None, observable_prefix: str | None = None, metadata_file: Path | None = None, **kwargs: Any) -> OptimizationResult
Read optimization results from CSV file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
Path
|
Path to CSV file |
required |
design_var_prefix
|
str | None
|
Prefix for design variable columns |
None
|
objective_prefix
|
str | None
|
Prefix for objective columns |
None
|
inequality_constraint_prefix
|
str | None
|
Prefix for inequality constraint columns (g(x) <= 0) |
None
|
equality_constraint_prefix
|
str | None
|
Prefix for equality constraint columns (h(x) = 0) |
None
|
observable_prefix
|
str | None
|
Prefix for observable columns |
None
|
metadata_file
|
Path | None
|
Optional path to metadata JSON file |
None
|
**kwargs
|
Any
|
Additional arguments passed to pd.read_csv |
{}
|
Returns:
| Type | Description |
|---|---|
OptimizationResult
|
OptimizationResult object |
Source code in optiscope/io/csv_handler.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
write
¶
write(result: OptimizationResult, filepath: Path, include_metadata: bool = True, **kwargs: Any) -> None
Write optimization results to CSV file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
OptimizationResult
|
OptimizationResult to write |
required |
filepath
|
Path
|
Output CSV path |
required |
include_metadata
|
bool
|
Whether to write metadata sidecar file |
True
|
**kwargs
|
Any
|
Additional arguments passed to pd.to_csv |
{}
|
Source code in optiscope/io/csv_handler.py
JSON Handler¶
json_handler
¶
JSON file format handler for optimization results.
Supports reading and writing optimization results in JSON format with full metadata preservation and support for result sets.
Classes¶
JSONHandler
¶
Bases: StructuredFormatHandler
Handler for JSON files containing optimization results.
JSON format preserves all metadata, variable information, and result sets. This is the recommended format for complete data preservation.
Functions¶
can_handle
classmethod
¶
Check if file is a valid JSON optimization result.
Source code in optiscope/io/json_handler.py
read
¶
read(filepath: Path, **kwargs: Any) -> OptimizationResult
Read optimization results from JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
Path
|
Path to JSON file |
required |
**kwargs
|
Any
|
Unused (for interface compatibility) |
{}
|
Returns:
| Type | Description |
|---|---|
OptimizationResult
|
OptimizationResult object |
Source code in optiscope/io/json_handler.py
write
¶
write(result: OptimizationResult, filepath: Path, indent: int = 2, **kwargs: Any) -> None
Write optimization results to JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
OptimizationResult
|
OptimizationResult to write |
required |
filepath
|
Path
|
Output JSON path |
required |
indent
|
int
|
JSON indentation level |
2
|
**kwargs
|
Any
|
Unused (for interface compatibility) |
{}
|
Source code in optiscope/io/json_handler.py
read_metadata
¶
Read just metadata from JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
Path
|
Path to JSON file |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing metadata |
Source code in optiscope/io/json_handler.py
MIDACO Handler¶
midaco_handler
¶
MIDACO file format handler.
MIDACO is a solver for mixed-integer optimization. It outputs results in two file formats: - SOLUTION.TXT: Complete history of solutions evaluated - PARETO.TXT: Pareto front approximation (for multi-objective problems)
File Format: - Header lines start with # - First data line: O (objectives), M (constraints), N (design vars), [PSIZE] - Following lines: F(1:O) G(1:M) X(1:N) values space-separated
Classes¶
MIDACoHandler
¶
Bases: TabularFormatHandler
Handler for MIDACO solution files.
Supports both SOLUTION.TXT (history) and PARETO.TXT (Pareto front) formats.
Functions¶
can_handle
classmethod
¶
Check if file is a MIDACO format.
Source code in optiscope/io/midaco_handler.py
read
¶
read(filepath: Path, parse_pareto: bool = True, **kwargs: Any) -> OptimizationResult
Read MIDACO solution file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
Path
|
Path to MIDACO file (SOLUTION.TXT or PARETO.TXT) |
required |
parse_pareto
|
bool
|
If True and this is PARETO.TXT, automatically create 'pareto' and 'midaco_best' result sets |
True
|
**kwargs
|
Any
|
Additional arguments (unused) |
{}
|
Returns:
| Type | Description |
|---|---|
OptimizationResult
|
OptimizationResult object |
Source code in optiscope/io/midaco_handler.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
write
¶
write(result: OptimizationResult, filepath: Path, file_type: str = 'SOLUTION', include_pareto_set: str | None = None, **kwargs: Any) -> None
Write optimization results to MIDACO format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
OptimizationResult
|
OptimizationResult to write |
required |
filepath
|
Path
|
Output file path |
required |
file_type
|
str
|
"SOLUTION" for history or "PARETO" for Pareto front |
'SOLUTION'
|
include_pareto_set
|
str | None
|
Name of result set to write as Pareto front (only used if file_type="PARETO") |
None
|
**kwargs
|
Any
|
Additional arguments (unused) |
{}
|
Source code in optiscope/io/midaco_handler.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | |
Functions¶
read_midaco_pair
¶
read_midaco_pair(solution_file: Path, pareto_file: Path) -> tuple[OptimizationResult, OptimizationResult]
Read both SOLUTION.TXT and PARETO.TXT files from MIDACO.
Convenience function to load both files at once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
solution_file
|
Path
|
Path to SOLUTION.TXT |
required |
pareto_file
|
Path
|
Path to PARETO.TXT |
required |
Returns:
| Type | Description |
|---|---|
tuple[OptimizationResult, OptimizationResult]
|
Tuple of (solution_history, pareto_front) OptimizationResult objects |
Source code in optiscope/io/midaco_handler.py
detect_midaco_files
¶
Detect MIDACO files in a directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory
|
Path
|
Directory to search |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Path]
|
Dictionary mapping file types to paths: |
dict[str, Path]
|
{'SOLUTION': path, 'PARETO': path} |