Evaluator
stickler.structured_object_evaluator.evaluator
Evaluator for StructuredModel objects.
This module provides an evaluator class for computing metrics on StructuredModel objects, leveraging their built-in comparison capabilities to generate comprehensive metrics. It also supports documenting non-matches (false positives, false negatives) for detailed analysis.
stickler.structured_object_evaluator.evaluator.StructuredModelEvaluator
Evaluator for StructuredModel objects.
This evaluator computes comprehensive metrics for StructuredModel objects, leveraging their built-in comparison capabilities. It includes confusion matrix calculations, field-level metrics, non-match documentation, and memory optimization capabilities.
Source code in stickler/structured_object_evaluator/evaluator.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 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 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 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 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 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 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 | |
__init__(model_class=None, threshold=0.5, verbose=False, document_non_matches=True, recall_with_fd=False)
Initialize the evaluator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_class
|
Optional[Type[StructuredModel]]
|
Optional StructuredModel class for type checking |
None
|
threshold
|
float
|
Similarity threshold for considering a match |
0.5
|
verbose
|
bool
|
Whether to print detailed progress information |
False
|
document_non_matches
|
bool
|
Whether to document detailed non-match information |
True
|
Source code in stickler/structured_object_evaluator/evaluator.py
47 48 49 50 51 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 | |
add_non_match(field_path, non_match_type, gt_value, pred_value, similarity_score=None, details=None)
Document a non-match with detailed information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field_path
|
str
|
Dot-notation path to the field (e.g., 'address.city') |
required |
non_match_type
|
NonMatchType
|
Type of non-match |
required |
gt_value
|
Any
|
Ground truth value |
required |
pred_value
|
Any
|
Predicted value |
required |
similarity_score
|
Optional[float]
|
Optional similarity score if available |
None
|
details
|
Optional[Dict[str, Any]]
|
Optional additional context or details |
None
|
document_id
|
Optional ID of the document this non-match belongs to |
required |
Source code in stickler/structured_object_evaluator/evaluator.py
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 | |
calculate_derived_confusion_matrix_metrics(cm_counts)
Calculate derived metrics from confusion matrix counts.
This method uses MetricsHelper to maintain consistency and avoid code duplication.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cm_counts
|
Dict[str, Union[int, float]]
|
Dictionary with confusion matrix counts containing keys: 'tp', 'fp', 'tn', 'fn', and optionally 'fd', 'fa' |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, float]
|
Dictionary with derived metrics: cm_precision, cm_recall, cm_f1, cm_accuracy |
Source code in stickler/structured_object_evaluator/evaluator.py
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 | |
clear_non_match_documents()
Clear the stored non-match documents.
Source code in stickler/structured_object_evaluator/evaluator.py
292 293 294 | |
combine_cm_dicts(cm1, cm2)
Combine two confusion matrix dictionaries by adding corresponding values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cm1
|
Dict[str, int]
|
First confusion matrix dictionary |
required |
cm2
|
Dict[str, int]
|
Second confusion matrix dictionary |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, int]
|
Combined confusion matrix dictionary |
Source code in stickler/structured_object_evaluator/evaluator.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | |
evaluate(ground_truth, predictions, recall_with_fd=False)
Evaluate predictions against ground truth and return comprehensive metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ground_truth
|
StructuredModel
|
Ground truth data (StructuredModel instance) |
required |
predictions
|
StructuredModel
|
Predicted data (StructuredModel instance) |
required |
recall_with_fd
|
bool
|
If True, include FD in recall denominator (TP/(TP+FN+FD)) If False, use traditional recall (TP/(TP+FN)) |
False
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary with the following structure: |
Dict[str, Any]
|
{ "overall": { "precision": float, # Overall precision [0-1] "recall": float, # Overall recall [0-1] "f1": float, # Overall F1 score [0-1] "accuracy": float, # Overall accuracy [0-1] "anls_score": float # Overall ANLS similarity score [0-1] }, "fields": {
" }, "confusion_matrix": {
"fields": {
# AGGREGATED metrics for all field types
" } |
Dict[str, Any]
|
} |
Key Usage Patterns:
-
Individual Item Metrics (per-instance analysis):
# Access metrics for each individual item in a list for i, item_metrics in enumerate(results['fields']['products']['items']): print(f"Product {i}: {item_metrics['overall']['f1']}") -
Aggregated Field Metrics (recommended for field performance analysis):
# Access aggregated metrics across all instances of a field type cm_fields = results['confusion_matrix']['fields'] product_id_performance = cm_fields['products.product_id'] print(f"Product ID field: {product_id_performance['derived']['cm_precision']}") # Get all aggregated product field metrics product_fields = {k: v for k, v in cm_fields.items() if k.startswith('products.')} -
Helper Function for Aggregated Metrics:
def get_aggregated_metrics(results, list_field_name): '''Extract aggregated field metrics for a list field.''' cm_fields = results['confusion_matrix']['fields'] prefix = f"{list_field_name}." return {k.replace(prefix, ''): v for k, v in cm_fields.items() if k.startswith(prefix)} # Usage: product_metrics = get_aggregated_metrics(results, 'products') print(f"Product name precision: {product_metrics['name']['derived']['cm_precision']}")
Note
- Use
results['fields'][field]['items']for per-instance analysis - Use
results['confusion_matrix']['fields'][field.subfield]for aggregated field analysis - Aggregated metrics provide rolled-up performance across all instances of a field type
- Confusion matrix metrics use standard TP/FP/TN/FN/FD classification with derived metrics
Source code in stickler/structured_object_evaluator/evaluator.py
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 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 | |
stickler.structured_object_evaluator.bulk_structured_model_evaluator
Stateful Bulk Evaluator for StructuredModel objects.
This module provides a modern stateful bulk evaluator inspired by PyTorch Lightning's stateful metrics and scikit-learn's incremental learning patterns. It supports memory-efficient processing of large datasets through accumulation-based evaluation.
stickler.structured_object_evaluator.bulk_structured_model_evaluator.BulkStructuredModelEvaluator
Stateful bulk evaluator for StructuredModel objects.
Inspired by PyTorch Lightning's stateful metrics and scikit-learn's incremental learning patterns. This evaluator accumulates evaluation state across multiple document processing calls, enabling memory-efficient evaluation of arbitrarily large datasets without loading everything into memory at once.
Key Features: - Stateful accumulation (like PyTorch Lightning metrics) - Memory-efficient streaming processing (like scikit-learn partial_fit) - External control over data flow and error handling - Checkpointing and recovery capabilities - Distributed processing support via state merging - Uses StructuredModel.compare_with() method directly
Source code in stickler/structured_object_evaluator/bulk_structured_model_evaluator.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 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 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 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 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 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 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 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 | |
__init__(target_schema, verbose=False, document_non_matches=True, elide_errors=False, individual_results_jsonl=None)
Initialize the stateful bulk evaluator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_schema
|
Type[StructuredModel]
|
StructuredModel class for validation and processing |
required |
verbose
|
bool
|
Whether to print detailed progress information |
False
|
document_non_matches
|
bool
|
Whether to document detailed non-match information |
True
|
elide_errors
|
bool
|
If True, skip documents with errors; if False, accumulate error metrics |
False
|
individual_results_jsonl
|
Optional[str]
|
Optional path to JSONL file for appending individual comparison results |
None
|
Source code in stickler/structured_object_evaluator/bulk_structured_model_evaluator.py
42 43 44 45 46 47 48 49 50 51 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 | |
compute()
Calculate final aggregated metrics from accumulated state.
This method performs the final computation of all derived metrics from the accumulated confusion matrix state, similar to PyTorch Lightning's training_epoch_end pattern.
Returns:
| Type | Description |
|---|---|
ProcessEvaluation
|
ProcessEvaluation with final aggregated metrics |
Source code in stickler/structured_object_evaluator/bulk_structured_model_evaluator.py
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
evaluate_dataframe(df)
Legacy compatibility method for DataFrame-based evaluation.
This method provides backward compatibility with the original DataFrame-based API while leveraging the new stateful processing internally.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame with columns for ground truth and predictions |
required |
Returns:
| Type | Description |
|---|---|
ProcessEvaluation
|
ProcessEvaluation with aggregated results |
Source code in stickler/structured_object_evaluator/bulk_structured_model_evaluator.py
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 | |
get_current_metrics()
Get current accumulated metrics without clearing state.
This method allows monitoring evaluation progress by returning current metrics computed from accumulated state. Unlike compute(), this does not clear the internal state.
Returns:
| Type | Description |
|---|---|
ProcessEvaluation
|
ProcessEvaluation with current accumulated metrics |
Source code in stickler/structured_object_evaluator/bulk_structured_model_evaluator.py
200 201 202 203 204 205 206 207 208 209 210 211 | |
get_state()
Get serializable state for checkpointing and recovery.
Returns a dictionary containing all internal state that can be serialized and later restored using load_state(). This enables checkpointing for long-running evaluation jobs.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary containing serializable evaluator state |
Source code in stickler/structured_object_evaluator/bulk_structured_model_evaluator.py
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 | |
load_state(state)
Restore evaluator state from serialized data.
This method restores the internal state from data previously saved with get_state(), enabling recovery from checkpoints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
Dict[str, Any]
|
State dictionary from get_state() |
required |
Source code in stickler/structured_object_evaluator/bulk_structured_model_evaluator.py
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 | |
merge_state(other_state)
Merge results from another evaluator instance.
This method enables distributed processing by merging confusion matrix counts from multiple evaluator instances that processed different portions of a dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other_state
|
Dict[str, Any]
|
State dictionary from another evaluator instance |
required |
Source code in stickler/structured_object_evaluator/bulk_structured_model_evaluator.py
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 | |
pretty_print_metrics()
Pretty print current accumulated metrics in a format similar to StructuredModel.
Displays overall metrics, field-level metrics, and evaluation summary in a human-readable format.
Source code in stickler/structured_object_evaluator/bulk_structured_model_evaluator.py
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 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 | |
reset()
Clear all accumulated state and start fresh evaluation.
This method resets all internal counters, metrics, and error tracking to initial state, enabling reuse of the same evaluator instance for multiple evaluation runs.
Source code in stickler/structured_object_evaluator/bulk_structured_model_evaluator.py
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 | |
save_metrics(filepath)
Save current accumulated metrics to a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Path where metrics will be saved as JSON |
required |
Source code in stickler/structured_object_evaluator/bulk_structured_model_evaluator.py
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 | |
update(gt_model, pred_model, doc_id=None)
Process a single document pair and accumulate the results in internal state.
This is the core method for stateful evaluation, inspired by PyTorch Lightning's training_step pattern. Each call processes one document pair and updates the internal confusion matrix counters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gt_model
|
StructuredModel
|
Ground truth StructuredModel instance |
required |
pred_model
|
StructuredModel
|
Predicted StructuredModel instance |
required |
doc_id
|
Optional[str]
|
Optional document identifier for error tracking |
None
|
Source code in stickler/structured_object_evaluator/bulk_structured_model_evaluator.py
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 | |
update_batch(batch_data)
Process multiple document pairs efficiently in a batch.
This method provides efficient batch processing by calling update() multiple times with optional garbage collection for memory management.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_data
|
List[Tuple[StructuredModel, StructuredModel, Optional[str]]]
|
List of tuples containing (gt_model, pred_model, doc_id) |
required |
Source code in stickler/structured_object_evaluator/bulk_structured_model_evaluator.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | |