SessionIOHandler
anomaly_match.data_io.SessionIOHandler
¶
Session I/O handler for saving and loading models, labels, and predictions.
SessionIOHandler(base_save_path='anomaly_match_results/sessions')
¶
Handles saving and loading of session data from SessionTracker.
This class manages the persistence of session information, including: - Session metadata and iteration information - Labeled data CSV files - Model checkpoints - Configuration files
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_save_path
|
str
|
Base directory for saving session data. |
'anomaly_match_results/sessions'
|
Source code in anomaly_match/data_io/SessionIOHandler.py
get_session_save_path(session_tracker)
¶
Get the save path for a session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_tracker
|
SessionTracker
|
SessionTracker instance. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Path where the session should be saved. |
Source code in anomaly_match/data_io/SessionIOHandler.py
save_session(session_tracker, save_path=None, cfg=None)
¶
Save complete session data to disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_tracker
|
SessionTracker
|
SessionTracker instance to save. |
required |
save_path
|
Path | None
|
Optional custom save path. If None, uses default naming. |
None
|
cfg
|
DotMap | None
|
Optional configuration to save alongside the session. |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
Path where the session was saved. |
Source code in anomaly_match/data_io/SessionIOHandler.py
save_model(model, cfg, session_tracker=None)
¶
Save the model to the session directory or config-specified path.
If session_tracker is available, saves to the session directory, otherwise uses the path specified in the config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
FixMatch
|
FixMatch model instance to save |
required |
cfg
|
DotMap
|
Configuration containing model_path or for extracting normalisation |
required |
session_tracker
|
SessionTracker | None
|
Optional session tracker for saving in session directory |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Path where the model was saved |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no session_tracker is provided and model_path is not specified in config. |
Source code in anomaly_match/data_io/SessionIOHandler.py
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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | |
load_model(model, cfg, model_path=None)
¶
Load a saved model from the specified path or config model_path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
FixMatch
|
FixMatch model instance to load into |
required |
cfg
|
DotMap
|
Configuration object that will be updated with normalisation method |
required |
model_path
|
str | None
|
Optional specific path to load from. If None, uses cfg.model_path |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if loading was successful, False otherwise |
Source code in anomaly_match/data_io/SessionIOHandler.py
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 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 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | |
get_labeled_data_from_checkpoint(checkpoint)
staticmethod
¶
Extract labeled data DataFrame from a model checkpoint.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
checkpoint
|
dict[str, Any]
|
Loaded checkpoint dict (from load_checkpoint) |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with columns [filename, label, iteration]. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the checkpoint does not contain labeled data. |
Source code in anomaly_match/data_io/SessionIOHandler.py
load_session(session_path)
¶
Load a session from disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_path
|
Path
|
Path to the session directory. |
required |
Returns:
| Type | Description |
|---|---|
SessionTracker
|
Loaded SessionTracker instance. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the session path or session metadata file does not exist. |
Source code in anomaly_match/data_io/SessionIOHandler.py
list_sessions()
¶
List all available sessions in the base save path.
Returns:
| Type | Description |
|---|---|
list[Path]
|
List of paths to session directories. |
Source code in anomaly_match/data_io/SessionIOHandler.py
get_session_summary(session_path)
¶
Get a summary of a session without fully loading it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_path
|
Path
|
Path to the session directory. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with session summary information. |
Source code in anomaly_match/data_io/SessionIOHandler.py
save_labels_to_output_dir(labeled_data_df, output_dir, session_tracker=None)
¶
Save labeled data to the session directory or output_dir.
If session_tracker is available, saves to the session directory, otherwise uses output_dir for backward compatibility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labeled_data_df
|
DataFrame
|
DataFrame containing labeled data |
required |
output_dir
|
str
|
Output directory to save to (used only if no session_tracker) |
required |
session_tracker
|
SessionTracker | None
|
Session tracker to save to session directory |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Path where labels were saved |
Source code in anomaly_match/data_io/SessionIOHandler.py
merge_gallery_labels(existing_label_file, new_labels, output_path)
¶
Merge new gallery labels with an existing label CSV and write the result.
The CSV uses id, label columns. id is the data-source
identifier (filename for image folders, source-id for Cutana, index
for Zarr) and is read and written as text throughout, so numeric
catalogue ids survive the round trip exactly as the catalogue wrote
them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
existing_label_file
|
str | None
|
Path to the current labels CSV (may be |
required |
new_labels
|
dict[str, str]
|
Dict mapping |
required |
output_path
|
str
|
Where to write the merged CSV. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Absolute path to the written CSV file. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the existing CSV cannot be read, or carries no
|
Source code in anomaly_match/data_io/SessionIOHandler.py
523 524 525 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 605 | |
filter_labels_csv_to_ids(csv_path, valid_ids)
staticmethod
¶
Rewrite csv_path keeping only rows whose id is in valid_ids.
Used just before launching the training subprocess to drop
labels for IDs the data source didn't surface (catalogue rows
that Cutana silently rejected, files the user moved since
labelling, ...). Without this the training subprocess logs a
Filename X not found in dataset warning per orphaned label
— one line per row, which on a 1000+ label CSV floods the UI
output and masks real warnings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
csv_path
|
str
|
Path to the labels CSV to filter in place. |
required |
valid_ids
|
set[str]
|
IDs to keep. Rows whose |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int]
|
Tuple |
Source code in anomaly_match/data_io/SessionIOHandler.py
update_config_paths_for_session(cfg, session_tracker)
¶
Update configuration paths to use session directories.
This ensures all saves go to the centralized session folder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg
|
DotMap
|
Configuration object to update |
required |
session_tracker
|
SessionTracker
|
Active session tracker |
required |
Source code in anomaly_match/data_io/SessionIOHandler.py
print_session(filepath)
¶
Print session information in a formatted way.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Path to the session directory. |
required |
Source code in anomaly_match/data_io/SessionIOHandler.py
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 | |