Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@santisoler
Copy link
Member

Summary

Draft for redesigning base classes for objective functions, data misfits and
regularizations as abstract
classes
.

PR Checklist

  • If this is a work in progress PR, set as a Draft PR
  • Linted my code according to the style guides.
  • Added tests to verify changes to the code.
  • Added necessary documentation to any new functions/classes following the
    expect style.
  • Marked as ready for review (if this is was a draft PR), and converted
    to a Pull Request
  • Tagged @simpeg/simpeg-developers when ready for review.

Reference issue

What does this implement/fix?

Simplify the BaseObjectiveFunction:

  • Decorate __init__, __call__, deriv and deriv2 as abstractmethods.
  • Decorate nP as abstractproperty.
  • Remove the mapping. Only regularizations needs it. Data misfits rely on
    the simulation to handle mapping operations.

Simplify the ComboObjectiveFunction:

  • Remove the W, it was overwritten by child classes, and the implementation
    only applied when the objective functions used and L2 norm.
  • Simplify how it checks if the child objective functions need to get fields
    in their methods or not. Don't rely on every objective function to have
    has_fields (it doesn't make sense for regularization to have this
    attribute).

Remove L2ObjectiveFunction. It was only use to create the BaseDataMisfit
class, forcing it to be only an L2 class. Moreover, most of its methods were
being overwritten by the BaseDataMisfit and L2DataMisfit, so it didn't have
a clear purpose.

Make BaseDataMisfit and BaseRegularization abstract classes:

  • BaseDataMisfit require children to implement __call__, deriv and
    deriv2.
  • BaseRegularization require children to implement f_m and f_m_deriv

Additional information

The mapping is only used by regularizations. Data misfits rely on the
simulation to handle the mapping.
Also add methods for testing their implementations in a quick way. They
still need to be polished.
The implementation of weights should be handled by the child classes.
Remove old bits from the class and incorporate the new ones from the
drafted ObjectiveFunction abstract class.
Remove this class since it was only use to create the
`BaseDataMisfit` class, forcing it to be only an L2 class.
Moreover, most of its methods were being overwritten by the
`BaseDataMisfit` and `L2DataMisfit`, so it didn't have a clear purpose.
Don't rely on every objective function to have a `has_fields` argument,
instead check if the object has it and it's True. Also, pass fields in
case the objective function is a ComboObjectiveFunction, so we can pass
fields down the tree.
@codecov
Copy link

codecov bot commented Jan 22, 2024

Codecov Report

Attention: Patch coverage is 98.26733% with 7 lines in your changes missing coverage. Please review.

Project coverage is 86.26%. Comparing base (5e4b583) to head (ed1294f).
Report is 102 commits behind head on main.

Files with missing lines Patch % Lines
simpeg/data_misfit.py 83.33% 2 Missing ⚠️
simpeg/objective_function.py 95.45% 2 Missing ⚠️
simpeg/regularization/base.py 60.00% 1 Missing and 1 partial ⚠️
tests/base/regularizations/test_regularization.py 95.23% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1335      +/-   ##
==========================================
+ Coverage   86.23%   86.26%   +0.03%     
==========================================
  Files         396      396              
  Lines       50366    50364       -2     
  Branches     5309     5306       -3     
==========================================
+ Hits        43431    43446      +15     
+ Misses       5483     5472      -11     
+ Partials     1452     1446       -6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@santisoler
Copy link
Member Author

I noticed by running some tests on the drafted code that some regularization classes like JointTotalVariation, CrossGradient and LinearCorrespondence don't implement the f_m and/or the f_m_deriv methods. They are all childs of BaseSimilarityMeasure which overrides some methods of the BaseRegularization class.

I think we should revisit the design of these classes so we avoid violating Liskov substitution principle.

  • What methods of the BaseRegularization are required by the BaseSimilarityMeasure?
  • Which ones should be overwritten?
  • BaseSimilarityMeasure should also be an abstract class. What methods and properties should be abstract?

@santisoler
Copy link
Member Author

santisoler commented Mar 1, 2024

After discussing with @lheagy, we decided not to touch the BaseRegularization class in this PR. That branch of the hierarchy tree deserves some more thoughts and trying to tackle it here would create a massive PR. Lets focus on the BaseObjectiveFunction and in the DataMisfit branch of the tree.

Revert the changes made to the `BaseRegularization` class so it goes
back at being a non-abstract class. It still inherits from the
`ObjectiveFunction` abstract class, but it implements all abstract
methods and properties. Restore tests for error when trying to call
`f_m` and `f_m_deriv` in `BaseRegularization`.
Restore the docstrings for the `deriv` and `deriv2` abstract methods.
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.

1 participant