This material has been used in the past to teach colleagues in our group how to use persistable.
The persistable package provides a general loggable superclass that provides Python users a simple way to persist load calculations and track corresponding calculation parameters.
Inheriting from Persistable automatically spools a logger and appends the PersistLoad object for easy and reproducible data persistance with loading, with parameter tracking. The PersistLoad object is based on setting a workingdatadir within which all persisted data is saved and logs are stored. Such a directory acts as a home for a specific set of experiments.
For more details, read the docs.
In [3]:
# Persistable Class:
from persistable import Persistable
# Set a persistable top path:
from pathlib import Path
LOCALDATAPATH = Path('.').absolute()
In [4]:
params = {
"hello": "world",
"another_dict": {
"test": [1,2,3]
},
"a": 1,
"b": 4
}
p = Persistable(
payload_name="first_payload",
params=params,
workingdatapath=LOCALDATAPATH / "knowledgeshare_20170929" # object will live in this local disk location
)
Simply override _generate_payload to give the Persistable object generate functionality. Note that generate here means to create the payload. The term is not meeant to indicate that a python generator is being produced.
In [18]:
# ML Example:
"""
def _generate_payload(self):
X = pd.read_csv(self.params['datafile'])
model = XGboost(X)
model.fit()
self.payload['model'] = model
"""
# Silly Example:
def _generate_payload(self):
self.payload['sum'] = self.params['a'] + self.params['b']
self.payload['msg'] = self.params['hello']
Now we will monkeypatch the payload generator to override its counterpart in Persistable object (only necessary because we've defined the generator outside of an IDE).
In [19]:
def bind(instance, method):
def binding_scope_fn(*args, **kwargs):
return method(instance, *args, **kwargs)
return binding_scope_fn
p._generate_payload = bind(p, _generate_payload)
In [20]:
p.generate()
In [22]:
class SillyPersistableExample(Persistable):
def _generate_payload(self):
self.payload['sum'] = self.params['a'] + self.params['b']
self.payload['msg'] = self.params['hello']
p2 = SillyPersistableExample(payload_name="silly_example", params=params, workingdatapath=LOCALDATAPATH / "knowledgeshare_20170929")
p2.generate()
In [24]:
p_test = Persistable(
"first_payload",
params=params,
workingdatapath=LOCALDATAPATH/"knowledgeshare_20170929"
)
p_test.load()
In [25]:
p_test.payload
Out[25]: