Utils Io#
io
#
| CLASS | DESCRIPTION |
|---|---|
DataFormat |
Supported data formats for loading/saving. |
| FUNCTION | DESCRIPTION |
|---|---|
detect_format |
Auto-detect data file format. |
create_dataset |
Create DatasetWeighted from various data sources. |
load_data |
Load data from files with automatic format detection. |
skip_every_nth |
Create a function that skips every nth row. |
keep_every_nth |
Create a function that keeps every nth row. |
save_data |
Save data in specified format. |
save_object |
Serialize an object using pickle. |
load_object |
Load serialized object from pickle file. |
save_metrics |
Save metrics dict to file. |
Classes#
DataFormat
#
Bases: Enum
Supported data formats for loading/saving.
Functions#
detect_format(filename: str) -> DataFormat
#
Auto-detect data file format.
| PARAMETER | DESCRIPTION |
|---|---|
filename
|
File path to analyze.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFormat
|
Detected format. |
Source code in spectre/utils/io.py
create_dataset(data_source: str | list[str] | pd.DataFrame | np.ndarray | torch.Tensor, target_cols: str | list[str] | None = None, weight_cols: str | list[str] | None = None, weight_reduce: Literal['prod', 'sum'] = 'prod', **load_kwargs) -> DatasetWeighted
#
Create DatasetWeighted from various data sources.
| PARAMETER | DESCRIPTION |
|---|---|
data_source
|
Data source - file path(s) or data array/DataFrame.
TYPE:
|
target_cols
|
Target column names/indices, by default None.
TYPE:
|
weight_cols
|
Weight column names/indices, by default None.
TYPE:
|
weight_reduce
|
Weight reduction method, by default "prod".
TYPE:
|
**load_kwargs
|
Additional arguments passed to load_data(). Common options: - skip_rows: int | Callable for row skipping - format: DataFormat for explicit format specification - delimiter: str for custom column separators
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
DatasetWeighted
|
Created dataset with data, weights, and targets. |
Examples:
>>> # From files
>>> dataset = create_dataset("data.txt", target_cols="target", weight_cols="weight")
>>>
>>> # From PLUMED files
>>> dataset = create_dataset(["file1.dat", "file2.dat"], format="plumed")
>>>
>>> # From DataFrame
>>> dataset = create_dataset(df, target_cols=["y"], weight_cols=["w"])
Source code in spectre/utils/io.py
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 | |
load_data(filename: str | list[str], format: DataFormat | str = DataFormat.AUTO, col_names: list[str] | None = None, skip_columns: int | list[int] | None = None, select_columns: list[int] | list[str] | str | None = None, skip_rows: int | Callable | None = None, delimiter: str | None = None, header: bool | None = None) -> pd.DataFrame
#
Load data from files with automatic format detection.
| PARAMETER | DESCRIPTION |
|---|---|
filename
|
Single file or list of files to load and concatenate.
TYPE:
|
format
|
Data format, by default DataFormat.AUTO for auto-detection.
TYPE:
|
col_names
|
Column names for the data, by default None.
TYPE:
|
skip_columns
|
Column indices to skip, by default None.
TYPE:
|
select_columns
|
Columns to extract. Can be:
By default None (keep all columns).
TYPE:
|
skip_rows
|
Row skipping specification, by default None. Can be: - int: Skip first N rows - Callable: Function that takes row index and returns True to skip Examples: lambda i: i % 2 == 0 (skip even rows), lambda i: i % 10 != 0 (keep every 10th row)
TYPE:
|
delimiter
|
Column delimiter (auto-detected if None), by default None.
TYPE:
|
header
|
Whether first row is header (auto-detected if None), by default None.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
Loaded data as DataFrame. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If files cannot be loaded or have incompatible formats. |
Examples:
>>> # Load specific columns by index
>>> df = load_data("data.txt", select_columns=[0, 2, 5])
>>>
>>> # Load columns by name
>>> df = load_data("data.csv", select_columns=["time", "temperature", "pressure"])
>>>
>>> # Load columns matching pattern
>>> df = load_data("data.txt", select_columns="d-*") # columns starting with "d-"
Source code in spectre/utils/io.py
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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 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 | |
skip_every_nth(n: int) -> Callable
#
Create a function that skips every nth row.
| PARAMETER | DESCRIPTION |
|---|---|
n
|
Skip every nth row (keep rows where index % n != 0).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Callable
|
Function that returns True for rows to skip. |
Examples:
>>> # Keep every 10th row (skip 9 out of 10)
>>> skip_func = skip_every_nth(10)
>>> data = load_data("large_file.txt", skip_rows=skip_func)
Source code in spectre/utils/io.py
keep_every_nth(n: int) -> Callable
#
Create a function that keeps every nth row.
| PARAMETER | DESCRIPTION |
|---|---|
n
|
Keep every nth row (skip rows where index % n != 0).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Callable
|
Function that returns True for rows to skip. |
Examples:
>>> # Keep every 5th row
>>> skip_func = keep_every_nth(5)
>>> data = load_data("file.txt", skip_rows=skip_func)
Source code in spectre/utils/io.py
save_data(data: np.ndarray | torch.Tensor | pd.DataFrame, filename: str, format: DataFormat | str = DataFormat.AUTO, col_names: list[str] | None = None, fmt: str = '%.6f', delimiter: str | None = None, header: bool = True) -> None
#
Save data in specified format.
| PARAMETER | DESCRIPTION |
|---|---|
data
|
Data to save.
TYPE:
|
filename
|
Output file path.
TYPE:
|
format
|
Output format, by default DataFormat.AUTO (inferred from filename).
TYPE:
|
col_names
|
Column names for output, by default None.
TYPE:
|
fmt
|
Number format string, by default "%.6f".
TYPE:
|
delimiter
|
Column delimiter, by default None (format-specific default).
TYPE:
|
header
|
Include header in output, by default True.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If data format is invalid or incompatible. |
OSError
|
If file cannot be written. |
Source code in spectre/utils/io.py
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 | |
save_object(obj: Any, filename: str) -> None
#
Serialize an object using pickle.
Source code in spectre/utils/io.py
load_object(filename: str) -> Any
#
Load serialized object from pickle file.
Source code in spectre/utils/io.py
save_metrics(metrics: dict, filename: str | Path, format: str | None = None) -> None
#
Save metrics dict to file.
| PARAMETER | DESCRIPTION |
|---|---|
metrics
|
Metrics to save.
TYPE:
|
filename
|
Output file path.
TYPE:
|
format
|
Save format ("pickle" or "json"). If None, uses
TYPE:
|
Notes
Format auto-detection rules:
- .json extension → JSON format
- .pkl, .pickle extension → pickle format
- Otherwise → uses save_format attribute (default: pickle)