Skip to content

Sr/refactor structured monolith - #14

Merged
sromoam merged 12 commits into
devfrom
sr/refactor_structured_monolith
Oct 29, 2025
Merged

sromoam merged 12 commits into
devfrom
sr/refactor_structured_monolith

Conversation

@sromoam

@sromoam sromoam commented Oct 22, 2025

Copy link
Copy Markdown
Contributor

This PR is work on refactoring the Structured_model class file to delegate many of the features of this to other classes.

Issue #, if available:

Description of changes:

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Moved comparison logic out to helper classes.
Validated test are passing.
* Delegating to classes, helper functions.
Pushing further reduction to structured model.
- Create new `__init__.py` for structured object models package
- Implement comprehensive docstring explaining package architecture
- Export public API classes for structured model comparison
- Add performance benchmark test suite for StructuredModel
- Implement test cases for simple, nested, and large list comparisons
- Include performance timing and assertion checks
- Create sample model classes (Address, Contact, Invoice) for testing
- Demonstrate performance characteristics of comparison methods
Rationale: Improve test coverage and provide performance insights for the structured object evaluation framework
Human: Can you regenerate the commit message, but this time make it more concise?
…mentation

- Create detailed documentation for StructuredModel architectural refactoring
- Describe migration from monolithic 2584-line implementation to modular design
- Outline new architecture using delegation pattern with specialized components
- Provide architecture overview, component responsibilities, and extension guidelines
- Include code examples and architectural diagrams to explain refactoring approach
- Maintain 100% backward compatibility with existing system
- Improve system maintainability and extensibility through modular design
@sromoam
sromoam requested a review from ayushi1208 October 22, 2025 22:06
@sromoam
sromoam marked this pull request as ready for review October 22, 2025 22:06

@ayushi1208 ayushi1208 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some reviews to clean up code. I also think we should reorganize the models folder in this PR. there are some core models we use, some helper models, and some files we just pull some static functions from. I think we can create some subdirectories in models to make this more readable code.

Comment on lines +231 to +232
```python
class Event(StructuredModel):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct? Each attribute should be a Comparable Field if there is Config right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, this isn't right.


Example:
--------
>>> calculator = AggregateMetricsCalculator()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe an example where the overall is actually different than the aggregate.

# Get null states and hierarchical needs for both ground truth and prediction.
# These flags control how we handle None values and empty structures.
gt_is_null = self.model._is_truly_null(gt_val)
pred_is_null = self.model._is_truly_null(pred_val)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the gt_is_null or pred_is_null used anywhere?

"weight": weight,
}

def handle_list_field_dispatch(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use the self.model._create_true_negative_result(), self.model._create_false_alarm_result(), and self.model._create_false_negative_result() similar to what is done for primitives since the resulting JSON is the same instead of rewriting the code. There is also a create_empty_list_result() method in the ResultHelper

# Empty List[primitive]
return self.primitive_list_comparator.compare_primitive_list_with_scores(
gt_val, pred_val, field_name
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Case 3 and Case 4 are never reached as either gt or pred as null lists is already handled earlier in the code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're correct, fixing it now.

return self._non_match_collector

@property
def confusion_matrix_calculator(self):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not used in the class, probably can remove it.

)

# Also add derived metrics to aggregate if it exists
if "aggregate" in overall and self._has_basic_metrics(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is aggregate ever added in the overall, or added as a sibling to overall?

)

# Still generate nested field details for debugging, but don't roll them up
nested_details = gt_val.compare_recursive(pred_val)["fields"]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be why the time was longer on the tests? This triggers a recursive call for each nested StructuredModel again I think

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, investigating this now.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't a simple fix; We will have to come back later to fix this.

if pred_list is None:
pred_list = []

# Handle empty/null list cases first - FIXED: Empty lists should be TN=1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use result helper functions here to lower lines of code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved this one, good suggestion

avg_time = elapsed / iterations
print(f"\nLarge list comparison: {avg_time*1000:.3f}ms per iteration ({iterations} iterations)")

# Should complete in reasonable time - under 2000ms per comparison (large dataset)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How was this math done?

@sromoam

sromoam commented Oct 27, 2025

Copy link
Copy Markdown
Contributor Author

Ayushi, just seeing these comments, I'm gonna try to resolve this PR by EOD tomorrow.

@ayushi1208 ayushi1208 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good for now.

@sromoam
sromoam merged commit 2a06430 into dev Oct 29, 2025
@ayushi1208
ayushi1208 deleted the sr/refactor_structured_monolith branch November 3, 2025 21:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants