-
Notifications
You must be signed in to change notification settings - Fork 47
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
Add Type Hints, Fix Mypy Errors, and Refactor Code in AEPsych Directory #413
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some typehints are missing:
from_config all over are missing return hints
lookahead_type
init missing return None -- Semi_p.py file
problem.py Problem class properties missing return typehints.
problem.py has a TODO comment in sample_y method, maybe make a small PR to fix this if possible.
config.py Config missing type hints in methods (e.g., get_section())
db.py Database missing type hints
Likelihood semi_p.py file "expected_log_prob" missing type hints.
Shouldn't everything be using Tensors now?
I’ve gone ahead and added return type hints for the The TODO comment in For Lastly, most parts of the code where tensors are needed for GPU support have been converted. The one exception is in the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Many comments.
In general: avoid adding extra asserts to fix mypy problems, instead work out how to make the type hints align. Basically, no actual functional code should be written for most mypy problems, just modifying hints.
@@ -61,7 +61,7 @@ def __init__( | |||
self.samps = samps | |||
self.stimuli_per_trial = stimuli_per_trial | |||
|
|||
def _instantiate_acquisition_fn(self, model: ModelProtocol): | |||
def _instantiate_acquisition_fn(self, model: ModelProtocol) -> AnalyticExpectedUtilityOfBestOption: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Double check again. Doesn't appear changed.
aepsych/models/gp_classification.py
Outdated
inducing_points = select_inducing_points( | ||
inducing_size=self.inducing_size, | ||
covar_module=self.covar_module, | ||
X=self.train_inputs[0], | ||
bounds=self.bounds, | ||
method=self.inducing_point_method, | ||
) | ||
assert inducing_points is not None, "Inducing points cannot be None" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are presumably for mypy errors, I don't like adding asserts to solve mypy errors, see if you can figure out a different way.
aepsych/models/utils.py
Outdated
@@ -57,7 +58,7 @@ def select_inducing_points( | |||
X: Optional[torch.Tensor] = None, | |||
bounds: Optional[Union[torch.Tensor, np.ndarray]] = None, | |||
method: str = "auto", | |||
): | |||
) -> Optional[torch.Tensor]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should always be tensors (also this Optional might be the cause of other problems above that require Asserts).
aepsych/plotting.py
Outdated
contour_levels_list: List[float] = [] | ||
|
||
if contour_levels is True: | ||
contour_levels_list = [0.75] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a very complex argument it seems. It has to allow bool to default it to all ints. Double check if this is absolutely necessary.
aepsych/strategy.py
Outdated
self.is_finished = True | ||
|
||
@property | ||
def finished(self): | ||
def finished(self) -> Union[bool,torch.Tensor, None]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be strictly bool, why not?
aepsych/strategy.py
Outdated
if self.can_fit: | ||
if self.keep_most_recent is not None: | ||
try: | ||
assert self.model is not None, "a model is needed here!" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assert probably due to above Optional on can_fit. Wherever assert ends up being weirdly needed, it's likely because of an odd typing somewhere else that should be fixed instead.
aepsych/models/ordinal_gp.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add type hints for arguments, then try again, if not ask again.
@JasonKChow has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
@JasonKChow has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
@JasonKChow has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
@JasonKChow merged this pull request in fba7f99. |
Description:
This PR adds type hints, resolves Mypy errors, and refactors code across multiple modules in AEPsych. Key changes include type hint additions, tensor conversions for compatibility, and improved error handling. This is the first step towards resolving issue #366.
Changes by Directory:
1.
acquisitions/
:construct_inputs_global_lookahead
to use tensors forlb
andub
instead of lists.2.
benchmark/
:problem.py
, updatedLSEProblemWithEdgeLogging.evaluate
to use tensors formetrics["prop_edge_sampling_err"]
.3.
database/
,generators/
,kernels/
,likelihoods/
:kernels/pairwisekernel.py
: UpdatedPairwiseKernel.forward
to avoid assigning tensord
as both an integer and tensor, ensuring Mypy compatibility.4.
models/
:gp_classification.py
: Added assertions (inducing_points is not None
) in the constructor and_reset_variational_strategy
.semi_p.py
: Added tensor conversion forX
inSemiParametricGPModel.posterior
to avoid Mypy errors.5. Root-Level AEPsych Files:
strategy.py
: Added assertions to check forNone
inself.model
,self.x
, andself.y
to prevent errors.utils.py
: Wrapped all occurrences ofparname
withstr()
for compatibility with theConfig
class.plotting.py
:plot_strat_3d
to improve handling ofcontour_levels
:contour_levels_list
to ensure it isSized
(e.g., list or array).slice_vals
, ensuring proper error messages and type checking.None
, raising aRuntimeError
instead of silently failing.plot_slice
to ensure better control overcontour_levels
:contour_levels
and ensured non-None
values are provided, with clearer error messages.ValueError
) ifcontour_levels
is incorrectly set toNone
.