-
Notifications
You must be signed in to change notification settings - Fork 0
Fittable interface #178
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
Open
markshowalter
wants to merge
3
commits into
main
Choose a base branch
from
fittable-2025-09
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Fittable interface #178
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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,117 @@ | ||
| ########################################################################################## | ||
| # oops/cache.py: Support for caching of OOPS objects | ||
| ########################################################################################## | ||
|
|
||
| import numpy as np | ||
| from polymath import Qube | ||
|
|
||
|
|
||
| class Cache: | ||
| """Class that can be indexed like a dictionary, where `maxsize` items are preserved. | ||
| When the size of the cache exceeds `maxsize` by ~ 10%, the least-recently accessed | ||
| items are deleted. | ||
| Indexing a Cache using a key that is not present, or has been deleted, returns None. | ||
| A KeyError is never raised. | ||
| Dictionary keys can include mutable items, which are converted to immutable. The class | ||
| method `clean_key` performs this conversion. | ||
| """ | ||
|
|
||
| # These are filled in by oops/__init__.py to avoid circular imports | ||
| FRAME_CLASS = None | ||
| PATH_CLASS = None | ||
|
|
||
| def __init__(self, maxsize=100): | ||
| """Constructor for a Cache. | ||
| Parameters: | ||
| maxsize (int, optional): The rough limit on the number of items stored in the | ||
| Cache. When this value is exceeded by ~ 10%, the number of elements is | ||
| reduced back to `maxsize` by removing the items accessed least recently. | ||
| """ | ||
|
|
||
| self._maxsize = maxsize | ||
| self._extras = max(3, maxsize//10) | ||
| self._limit = maxsize + self._extras | ||
| self._dict = {} | ||
| self._counter = 0 | ||
|
|
||
| def __len__(self): | ||
| """The number of items currently in this Cache.""" | ||
| return len(self._dict) | ||
|
|
||
| @staticmethod | ||
| def clean_key(key): | ||
| """Convert the given key to immutable so it can be used as a dictionary key.""" | ||
|
|
||
| def clean_item(item): | ||
| match item: | ||
| case Qube(): | ||
| vals = tuple(item.vals.ravel()) if np.shape(item.vals) else item.vals | ||
| mask = tuple(item.mask.ravel()) if np.shape(item.mask) else item.mask | ||
| return (type(item).__name__, item.shape, vals, mask) | ||
| case np.ndarray(): | ||
| return (item.shape, tuple(item.ravel())) | ||
| case Cache.PATH_CLASS(): | ||
| return Cache.PATH_CLASS.as_waypoint(item) | ||
| case Cache.FRAME_CLASS(): | ||
| return Cache.FRAME_CLASS.as_wayframe(item) | ||
| case x if hasattr(x, '__data__'): | ||
| return id(item) | ||
| case list(): | ||
| return tuple(item) | ||
| case _: | ||
| return item | ||
|
|
||
| if isinstance(key, (list, tuple)): | ||
| return tuple(clean_item(item) for item in key) | ||
|
|
||
| return clean_item(key) | ||
|
|
||
| def __contains__(self, key): | ||
| """True if the given key is currently in the Cache.""" | ||
|
|
||
| if self._maxsize: | ||
| key = Cache.clean_key(key) | ||
| if key in self._dict: | ||
| self._counter += 1 | ||
| self._dict[key][0] = self._counter | ||
| return True | ||
| return False | ||
|
|
||
| def __getitem__(self, key): | ||
| """The value associated with the given key, or None if the key is missing. | ||
| Supports index notation using square brackets "[]". | ||
| """ | ||
|
|
||
| if self._maxsize: | ||
| key = Cache.clean_key(key) | ||
| if key in self._dict: | ||
| self._counter += 1 | ||
| count_key_value = self._dict[key] | ||
| count_key_value[0] = self._counter | ||
| return count_key_value[2] | ||
|
|
||
| return None | ||
|
|
||
| def __setitem__(self, key, value): | ||
| """Set the value associated with the given key. | ||
| Supports index notation using square brackets "[]". | ||
| """ | ||
|
|
||
| if self._maxsize: | ||
| key = Cache.clean_key(key) | ||
| self._counter += 1 | ||
| self._dict[key] = [self._counter, key, value] | ||
|
|
||
| if len(self._dict) > self._limit: | ||
| tuples = list(self._dict.values()) | ||
| tuples.sort() | ||
| extras = tuples[:-self._maxsize] | ||
| for (_, k, _) in extras: | ||
| del self._dict[k] | ||
|
|
||
| ########################################################################################## | ||
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| ################################################################################ | ||
| # oops/cadence/metronome.py: Metronome subclass of class Cadence | ||
| ################################################################################ | ||
markshowalter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| from oops.cadence import Metronome | ||
|
|
||
| class SnapCadence(Metronome): | ||
| """A shapeless Cadence subclass with a single start and stop.""" | ||
|
|
||
| def __init__(self, tstart, texp, clip=True): | ||
| """Constructor for a SnapCadence. | ||
| Input: | ||
| tstart the start time of the observation in seconds TDB. | ||
| texp the exposure time in seconds associated with each step. | ||
| This may be shorter than tstride due to readout times, | ||
| etc. It may also be longer. | ||
| clip if True (the default), times and index values are always | ||
| clipped into the valid range. | ||
| """ | ||
markshowalter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Metronome.__init__(self, tstart, texp, texp, 1, clip=clip) | ||
|
|
||
| def __getstate__(self): | ||
| return (self.tstart, self.texp, self.clip) | ||
|
|
||
| def __setstate__(self, state): | ||
| self.__init__(*state) | ||
|
|
||
| ################################################################################ | ||
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.