-
Notifications
You must be signed in to change notification settings - Fork 0
Lagrangian respects effect handlers #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
19b7f1e
Reinstate check after renaming caused bugs in sampling
willGraham01 a5f55bc
Remove old CausalProblem class
willGraham01 de904bf
Add barebones classes to be populated later
willGraham01 32ccf0b
Update two normal example test to use new infrastructure
willGraham01 c8fbaa5
Rework so that the lagrangian can be passed model parameters and the …
willGraham01 87d128f
ruffing
willGraham01 7c171c4
Refactor out g.model argument from the Lagrangian call
willGraham01 3372193
Make TODOs obvious so I don't forget to do them
willGraham01 87fc5a2
Add docstrings and more TODOs
willGraham01 d202568
Todo resolution and addition
willGraham01 d80aec8
Merge branch 'main' into wgraham/causal-problem-rework
willGraham01 7f32319
Make _CPConstraint callable
willGraham01 3189f2d
Hide _CPComponent attributes that we don't expect to change
willGraham01 c2dab0c
Test __call__ for _CPComponents
willGraham01 648ff9d
Add note about __call__ in docstring
willGraham01 deea4dc
Merge branch 'main' into wgraham/make-ce-con-callables
willGraham01 77eb893
Fix bug in how handlers are applied
willGraham01 cb7d923
Write tests for features
willGraham01 1302338
Merge branch 'main' into wgraham/make-ce-con-callables
willGraham01 05a3d5b
Edit Constraint so it is created in pieces
willGraham01 f87b2fa
Rework Constraint.__init__ and docstring to match new format
willGraham01 de2f623
Update two_normal_example integration test
willGraham01 28f0772
Remove todo note
willGraham01 b7ba1c9
Merge branch 'main' into wgraham/constraint-creation-ease
willGraham01 3366e94
Create wrapper class to make handlers easier. __eq__ placeholder for now
willGraham01 fe73614
Tidy eq docstring
willGraham01 1e396e2
Write necessary comparison methods
willGraham01 440873b
Write model association method and implement in Lagrangian
willGraham01 12d2b71
Docstirngs and breakout HandlerToApply class to submodule
willGraham01 cc3d2a0
Docstirngs and breakout HandlerToApply class to submodule
willGraham01 65ab781
Tests for HandlerToApply class
willGraham01 9335477
Tests for can_use_same_model
willGraham01 8a4483e
Tests for associating models to components of the CP
willGraham01 c5f8270
Merge branch 'main' into wgraham/constraint-creation-ease
willGraham01 821072e
Reorganise classes now that structure is somewhat rigid
willGraham01 2c359ed
Add module-level import for user-facing functions
willGraham01 fb9025b
Merge branch 'wgraham/constraint-creation-ease' into wgraham/handle-h…
willGraham01 e7c7b26
Update src/causalprog/causal_problem/causal_problem.py
willGraham01 783163c
Merge branch 'main' into wgraham/handle-handlers
willGraham01 5036d81
Reinstate if not else fix
willGraham01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,7 @@ | ||
| """Classes for defining causal problems.""" | ||
|
|
||
| from .causal_problem import CausalProblem | ||
| from .components import CausalEstimand, Constraint | ||
| from .handlers import HandlerToApply | ||
|
|
||
| __all__ = ("CausalEstimand", "CausalProblem", "Constraint", "HandlerToApply") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| """Base class for components of causal problems.""" | ||
|
|
||
| from collections.abc import Callable | ||
| from typing import Any | ||
|
|
||
| import numpy.typing as npt | ||
|
|
||
| from causalprog.causal_problem.handlers import EffectHandler, HandlerToApply, Model | ||
|
|
||
|
|
||
| class _CPComponent: | ||
| """ | ||
| Base class for components of a Causal Problem. | ||
|
|
||
| A _CPComponent has an attached method that it can apply to samples | ||
| (`do_with_samples`), which will be passed sample values of the RVs | ||
| during solution of a Causal Problem and used to evaluate the causal | ||
| estimand or constraint the instance represents. | ||
|
|
||
| It also has a sequence of effect handlers that need to be applied | ||
| to the sampling model before samples can be drawn to evaluate this | ||
| component. For example, if a component requires conditioning on the | ||
| value of a RV, the `condition` handler needs to be applied to the | ||
| underlying model, before generating samples to pass to the | ||
| `do_with_sample` method. `effect_handlers` will be applied to the model | ||
| in the order they are given. | ||
| """ | ||
|
|
||
| do_with_samples: Callable[..., npt.ArrayLike] | ||
| effect_handlers: tuple[HandlerToApply, ...] | ||
|
|
||
| @property | ||
| def requires_model_adaption(self) -> bool: | ||
| """Return True if effect handlers need to be applied to model.""" | ||
| return len(self.effect_handlers) > 0 | ||
|
|
||
| def __call__(self, samples: dict[str, npt.ArrayLike]) -> npt.ArrayLike: | ||
| """ | ||
| Evaluate the estimand or constraint, given sample values. | ||
|
|
||
| Args: | ||
| samples: Mapping of RV (node) labels to samples of that RV. | ||
|
|
||
| Returns: | ||
| Value of the estimand or constraint, given the samples. | ||
|
|
||
| """ | ||
| return self._do_with_samples(**samples) | ||
|
|
||
| def __init__( | ||
willGraham01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self, | ||
| *effect_handlers: HandlerToApply | tuple[EffectHandler, dict[str, Any]], | ||
| do_with_samples: Callable[..., npt.ArrayLike], | ||
| ) -> None: | ||
| self.effect_handlers = tuple( | ||
| h if isinstance(h, HandlerToApply) else HandlerToApply.from_pair(h) | ||
| for h in effect_handlers | ||
| ) | ||
| self._do_with_samples = do_with_samples | ||
|
|
||
| def apply_effects(self, model: Model) -> Model: | ||
| """Apply any necessary effect handlers prior to evaluating.""" | ||
| adapted_model = model | ||
| for handler in self.effect_handlers: | ||
| adapted_model = handler.handler(adapted_model, handler.options) | ||
| return adapted_model | ||
|
|
||
| def can_use_same_model_as(self, other: "_CPComponent") -> bool: | ||
| """ | ||
| Determine if two components use the same (predictive) model. | ||
|
|
||
| Two components rely on the same model if they apply the same handlers | ||
| to the model, which occurs if and only if `self.effect_handlers` and | ||
| `other.effect_handlers` contain identical entries, in the same order. | ||
| """ | ||
| if (not isinstance(other, _CPComponent)) or ( | ||
| len(self.effect_handlers) != len(other.effect_handlers) | ||
| ): | ||
| return False | ||
|
|
||
| return all( | ||
| my_handler == their_handler | ||
| for my_handler, their_handler in zip( | ||
| self.effect_handlers, other.effect_handlers, strict=True | ||
| ) | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.