simpleml.persistables.base_persistable

Base class for all database tracked records, called “Persistables”

Module Contents

Classes

Persistable

Base class for all SimpleML database objects. Defaults to PostgreSQL

simpleml.persistables.base_persistable.LOGGER[source]
simpleml.persistables.base_persistable.__author__ = Elisha Yadgaran[source]
class simpleml.persistables.base_persistable.Persistable(name=None, has_external_files=False, author=None, project=None, version_description=None, save_patterns=None, **kwargs)[source]

Bases: future.utils.with_metaclass()

Base class for all SimpleML database objects. Defaults to PostgreSQL but can be swapped out for any supported SQLAlchemy backend.

Takes advantage of sqlalchemy-mixins to enable active record operations (TableModel.save(), create(), where(), destroy())

Uses private class attributes for internal artifact registry Does not need to be persisted because it gets populated on import (and can therefore be changed between versions) cls._ARTIFACT_{artifact_name} = {‘save’: save_attribute, ‘restore’: restore_attribute}

id: Random UUID(4). Used over auto incrementing id to minimize collision probability

with distributed trainings and authors (especially if using central server to combine results across different instantiations of SimpleML)

hash_id: Use hash of object to uniquely identify the contents at train time registered_name: class name of object defined when importing

Can be used for the drag and drop GUI - also for prescribing training config

author: creator project: Project objects are associated with. Useful if multiple persistables

relate to the same project and want to be grouped (but have different names) also good for implementing row based security across teams

name: friendly name - primary way of tracking evolution of “same” object over time version: autoincrementing id of “friendly name” version_description: description that explains what is new or different about this version

# Persistence of fitted states has_external_files = boolean field to signify presence of saved files not in (main) db filepaths = JSON object with external file details

The nested notation is because any persistable can implement multiple save options (with arbitrary priority) and arbitrary inputs. Simple serialization could have only a single string location whereas complex artifacts might have a list or map of filepaths

Structure: {

artifact_name: {

‘save_pattern’: filepath_data

}, “example”: {

“disk_pickled”: path to file, relative to base simpleml folder (default ~/.simpleml), “database”: {“schema”: schema, “table”: table_name}, # (for files extractable with select * from) …

}

}

metadata: Generic JSON store for random attributes

__abstract__ = True[source]
author[source]
filepaths[source]
has_external_files[source]
hash_[source]
id[source]
metadata_[source]
name[source]
project[source]
registered_name[source]
version[source]
version_description[source]
_get_latest_version(self)[source]

Versions should be autoincrementing for each object (constrained over friendly name). Executes a database lookup and increments..

abstract _hash(self)[source]

Each subclass should implement a hashing routine to uniquely AND consistently identify the object contents. Consistency is important to ensure ability to assert identity across code definitions

_load_class(self)[source]

Wrapper function to call global registry of all imported class names

property config(self)[source]
get_artifact(self, artifact_name: str)Any[source]

Accessor method to lookup the artifact in the registry and return the corresponding data value

property library_versions(self)[source]
load(self, load_externals=True)[source]

Counter operation for save Needs to load any file and db objects

Class definition is stored by registered_name param and Pickled objects are stored in external_filename param

Parameters

load_externals – Boolean flag whether to load the external files

useful for relationships that only need class definitions and not data

load_external_file(self, artifact_name: str, save_pattern: str, cls: Optional[Type] = None)Any[source]

Define pattern for loading external files returns the object for assignment Inverted operation from saving. Registered functions should take in the same data (in the same form) of what is saved in the filepath

load_external_files(self, artifact_name: Optional[str] = None)[source]

Main routine to restore registered external artifacts. Will iterate through save patterns and break after the first successful restore (allows robustness in the event of unavailable resources)

load_if_unloaded(self, artifact_name: str)None[source]

Convenience method to load an artifact if not already loaded. Easy dropin in property methods ``` @property def artifact(self):

self.load_if_unloaded(artifact_name) if not hasattr(self, artifact_attribute):

self.create_artifact()

return self.artifact_attribute

```

restore_artifact(self, artifact_name: str, obj: Any)None[source]

Setter method to lookup the restore attribute and set to the passed object

save(self)[source]

Each subclass needs to instantiate a save routine to persist to the database and any other required filestore

sqlalchemy_mixins supports active record style TableModel.save() so can still call super(Persistable, self).save()

save_external_file(self, artifact_name: str, save_pattern: str, cls: Optional[Type] = None, **save_params)None[source]

Abstracted pattern to save an artifact via one of the registered patterns and update the filepaths location

save_external_files(self)[source]

Main routine to save registered external artifacts. Each save pattern is defined using the standard api for the save params defined here. If a pattern requires more imports, it needs to be added here

Uses a standardized nomenclature to reuse params regardless of save pattern {

‘persistable_id’: the database id of the persistable. typically used as the root name of the saved object. implementations will pre/suffix, ‘persistable_type’: the persistable type (DATASET/PIPELINE..), ‘overwrite’: boolean. shortcut in case save pattern redefines a serialization routine

}

property state(self)[source]