Skip to content

Commit 4dab851

Browse files
authored
Refactored imports for pyearthtools/training/wrapper (#270)
* Refactored imports for pyearthtools/training/wrapper * Added a SimpleModel class for notebooks / worked examples * Improved cache file finding code
1 parent 0a9aec0 commit 4dab851

7 files changed

Lines changed: 57 additions & 6 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ src/mypy.txt
172172
*.sublime-project
173173
*.sublime-workspace
174174

175+
# Zed Editor
176+
.zed
177+
175178
# Ignore .DS_Store files
176179
.DS_Store
177180

packages/data/src/pyearthtools/data/indexes/_indexes.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,17 @@ def get(self, *args, **kwargs):
200200
Loaded Data
201201
"""
202202
try:
203-
return self.load(self.search(*args), **kwargs)
203+
204+
load_index = self.search(*args)
205+
206+
cache = getattr(self, "object_cache", {})
207+
cached_result = cache.get(str(load_index), None)
208+
result = cached_result or self.load(load_index, **kwargs)
209+
210+
if getattr(self, "cache_last_used", True):
211+
self.object_cache = {str(load_index): result}
212+
213+
return result
204214
except Exception as e:
205215
raise DataNotFoundError(f"Data with args: {str(args)} could not be found.") from e
206216

packages/training/src/pyearthtools/training/wrapper/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# ruff: noqa: F401
1717

1818

19-
from pyearthtools.training.wrapper.wrapper import ModelWrapper
19+
from pyearthtools.training.wrapper._wrapper import ModelWrapper, SimpleModel
2020

2121
from pyearthtools.training.wrapper import predict, train, utils
2222

@@ -35,7 +35,7 @@
3535
except (ImportError, ModuleNotFoundError):
3636
LIGHTNING_IMPORTED = False
3737

38-
__all__ = ["ModelWrapper", "predict", "train", "utils", "TrainingWrapper", "Predictor"]
38+
__all__ = ["ModelWrapper", "SimpleModel", "predict", "train", "utils", "TrainingWrapper", "Predictor"]
3939

4040
if ONNX_IMPORTED:
4141
__all__.append("onnx")

packages/training/src/pyearthtools/training/wrapper/wrapper.py renamed to packages/training/src/pyearthtools/training/wrapper/_wrapper.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,44 @@
2222
from pyearthtools.training.data import PipelineDataModule
2323

2424

25+
class SimpleModel(InitialisationRecordingMixin, metaclass=ABCMeta):
26+
"""
27+
The SimpleModel base wrapper removes assumptions from the primary model wrapper class.
28+
This provides a simpler on-ramp for early-stage model development, without imposing
29+
as many requirements on the developer for comprehensive functionality.
30+
31+
It also allows the direct use of a single Pipeline, without requiring a
32+
train/validate split to be defined, avoiding the need for a PipelineDataModule.
33+
34+
New users may wish to start here before implementing the full ModelWrapper class.
35+
"""
36+
37+
def __init__(
38+
self,
39+
):
40+
"""
41+
Construct Base model wrapper
42+
43+
`model` will not be recorded in the initialisation by default, set `_record_model` to change
44+
this behaviour.
45+
"""
46+
47+
pass
48+
49+
@abstractmethod
50+
def fit(self, pipeline, epochs=1):
51+
"""
52+
Perform a single epoch 'fit' operation based on walking the pipeline
53+
"""
54+
pass
55+
56+
@abstractmethod
57+
def predict(self, pipeline, query=None):
58+
"""
59+
Perform a single prediction based either on a pipeline or a single sample
60+
"""
61+
62+
2563
class ModelWrapper(InitialisationRecordingMixin, metaclass=ABCMeta):
2664
"""
2765
Base Model Wrapper

packages/training/src/pyearthtools/training/wrapper/predict/predict.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from pyearthtools.utils.initialisation import InitialisationRecordingMixin
2424

2525
from pyearthtools.pipeline.controller import Pipeline
26-
from pyearthtools.training.wrapper.wrapper import ModelWrapper
26+
from pyearthtools.training.wrapper import ModelWrapper
2727

2828

2929
class Predictor(InitialisationRecordingMixin, metaclass=ABCMeta):

packages/training/src/pyearthtools/training/wrapper/predict/timeseries.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from pyearthtools.data.time import TimeDelta, Petdt, TimeRange
3030

3131
from pyearthtools.pipeline.controller import Pipeline
32-
from pyearthtools.training.wrapper.wrapper import ModelWrapper
32+
from pyearthtools.training.wrapper import ModelWrapper
3333
from pyearthtools.training.wrapper.predict.predict import Predictor
3434

3535
from pyearthtools.training.manage import Variables

packages/training/src/pyearthtools/training/wrapper/train.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from abc import abstractmethod
1919

20-
from pyearthtools.training.wrapper.wrapper import ModelWrapper
20+
from pyearthtools.training.wrapper import ModelWrapper
2121

2222

2323
class TrainingWrapper(ModelWrapper):

0 commit comments

Comments
 (0)