Skip to content

Commit

Permalink
Moved history to wfAlgorithm.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfcrenshaw committed Jan 18, 2024
1 parent b72cfe0 commit 73d71e4
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 42 deletions.
44 changes: 2 additions & 42 deletions python/lsst/ts/wep/estimation/tie.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
__all__ = ["TieAlgorithm"]

import inspect
import warnings
from typing import Iterable, Optional, Union

import numpy as np
Expand Down Expand Up @@ -80,7 +79,6 @@ class TieAlgorithm(WfAlgorithm):
Dictionary of mask keyword arguments to pass to mask creation.
To see possibilities, see the docstring for
lsst.ts.wep.imageMapper.ImageMapper.createPupilMask().
(the default is an empty dictionary)
saveHistory : bool, optional
Whether to save the algorithm history in the self.history attribute.
If True, then self.history contains information about the most recent
Expand Down Expand Up @@ -115,12 +113,9 @@ def __init__(
saveHistory=saveHistory,
)

# Instantiate an empty history
self._history = {} # type: ignore

@property
def opticalModel(self) -> str:
"""The optical model to use for"""
"""The optical model to use for mapping the image to the pupil."""
return self._opticalModel

@opticalModel.setter
Expand Down Expand Up @@ -374,35 +369,6 @@ def maskKwargs(self, value: Union[dict, None]) -> None:

self._maskKwargs = value

@property
def saveHistory(self) -> bool:
"""Whether the algorithm history is saved."""
return self._saveHistory

@saveHistory.setter
def saveHistory(self, value: bool) -> None:
"""Set boolean that determines whether algorithm history is saved.
Parameters
----------
value : bool
Boolean that determines whether the algorithm history is saved.
Raises
------
TypeError
If the value is not a boolean
"""
if not isinstance(value, bool):
raise TypeError("saveHistory must be a boolean.")

self._saveHistory = value

# If we are turning history-saving off, delete any old history
# This is to avoid confusion
if value is False:
self._history = {}

@property
def history(self) -> dict:
"""The algorithm history.
Expand All @@ -426,13 +392,7 @@ def history(self) -> dict:
Note the units for all Zernikes are in meters, and the z-derivative
in dIdz is also in meters.
"""
if not self._saveHistory:
warnings.warn(
"saveHistory is False. If you want the history to be saved, "
"run self.config(saveHistory=True)."
)

return self._history
return super().history

def _expSolve(
self,
Expand Down
64 changes: 64 additions & 0 deletions python/lsst/ts/wep/estimation/wfAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

__all__ = ["WfAlgorithm"]

import warnings
from abc import ABC, abstractmethod
from typing import Any, Optional

Expand All @@ -38,6 +39,10 @@ class WfAlgorithm(ABC):
Path to file specifying values for the other parameters. If the
path starts with "policy/", it will look in the policy directory.
Any explicitly passed parameters override values found in this file
saveHistory : bool, optional
Whether to save the algorithm history in the self.history attribute.
If True, then self.history contains information about the most recent
time the algorithm was run.
...
Expand All @@ -46,18 +51,77 @@ class WfAlgorithm(ABC):
def __init__(
self,
configFile: Optional[str] = None,
saveHistory: Optional[bool] = None,
**kwargs: Any,
) -> None:
# Merge keyword arguments with defaults from configFile
params = mergeConfigWithFile(
configFile,
saveHistory=saveHistory,
**kwargs,
)

# Configure parameters
for key, value in params.items():
setattr(self, key, value)

# Instantiate an empty history
self._history = {} # type: ignore

def __init_subclass__(self) -> None:
"""This is called when you subclass.
I am using this to force you to write a docstring
for the history property.
"""
if self.history.__doc__ is None:
raise AttributeError(
"You must write a docstring for the history property. "
"Please use this to describe the contents of the history dict."
)

@property
def saveHistory(self) -> bool:
"""Whether the algorithm history is saved."""
return self._saveHistory

@saveHistory.setter
def saveHistory(self, value: bool) -> None:
"""Set boolean that determines whether algorithm history is saved.
Parameters
----------
value : bool
Boolean that determines whether the algorithm history is saved.
Raises
------
TypeError
If the value is not a boolean
"""
if not isinstance(value, bool):
raise TypeError("saveHistory must be a boolean.")

self._saveHistory = value

# If we are turning history-saving off, delete any old history
# This is to avoid confusion
if value is False:
self._history = {}

@property
def history(self) -> dict:
# Return the algorithm history
# Note I have not written a real docstring here, so that I can force
# subclasses to write a new docstring for this method
if not self._saveHistory:
warnings.warn(
"saveHistory is False. If you want the history to be saved, "
"run self.config(saveHistory=True)."
)

return self._history

@staticmethod
def _validateInputs(
I1: Image,
Expand Down

0 comments on commit 73d71e4

Please sign in to comment.