Save and Load
In [1]:
Copied!
%load_ext autoreload
%autoreload 2
import datamol as dm
%load_ext autoreload
%autoreload 2
import datamol as dm
Any Molfeat featurizer should be serializable. This makes it easy to share a specific instantiation of a featurizer. Any featurizer can thus be created from - or saved to - a state dictionary. We support native support to save these state dicts to both YAML and JSON.
Example¶
In [2]:
Copied!
from molfeat.trans import MoleculeTransformer
featurizer = MoleculeTransformer("ecfp")
from molfeat.trans import MoleculeTransformer
featurizer = MoleculeTransformer("ecfp")
In [3]:
Copied!
print(featurizer.to_state_yaml())
print(featurizer.to_state_yaml())
_molfeat_version: 0.8.2.dev26+g8b6a1c9.d20230401 args: dtype: null featurizer: ecfp n_jobs: 1 parallel_kwargs: null verbose: false name: MoleculeTransformer
In [4]:
Copied!
print(featurizer.to_state_json())
print(featurizer.to_state_json())
{"name": "MoleculeTransformer", "args": {"featurizer": "ecfp", "n_jobs": 1, "verbose": false, "dtype": null, "parallel_kwargs": null}, "_molfeat_version": "0.8.2.dev26+g8b6a1c9.d20230401"}
In [5]:
Copied!
print(featurizer.to_state_dict())
print(featurizer.to_state_dict())
{'name': 'MoleculeTransformer', 'args': {'featurizer': 'ecfp', 'n_jobs': 1, 'verbose': False, 'dtype': None, 'parallel_kwargs': None}, '_molfeat_version': '0.8.2.dev26+g8b6a1c9.d20230401'}
In [6]:
Copied!
print(featurizer.to_state_json())
print(featurizer.to_state_json())
{"name": "MoleculeTransformer", "args": {"featurizer": "ecfp", "n_jobs": 1, "verbose": false, "dtype": null, "parallel_kwargs": null}, "_molfeat_version": "0.8.2.dev26+g8b6a1c9.d20230401"}
In [7]:
Copied!
# You can also directly save to files:
import tempfile
with tempfile.TemporaryDirectory() as tmpdir:
path = dm.fs.join(tmpdir, "state_dict.yml")
featurizer.to_state_yaml_file(path)
featurizer = MoleculeTransformer.from_state_yaml_file(path)
# You can also directly save to files:
import tempfile
with tempfile.TemporaryDirectory() as tmpdir:
path = dm.fs.join(tmpdir, "state_dict.yml")
featurizer.to_state_yaml_file(path)
featurizer = MoleculeTransformer.from_state_yaml_file(path)
Loop over multiple featurizers¶
One implication of this, is that despite the various interfaces, you can easily loop over multiple featurizers.
In [8]:
Copied!
FEATURIZERS = [
{"name": "MoleculeTransformer", "args": {"featurizer": "ecfp", "n_jobs": 1, "verbose": False, "dtype": None, "parallel_kwargs": None}, "_molfeat_version": "0.0.1"},
{"name": "MoleculeTransformer", "args": {"featurizer": "maccs", "n_jobs": 1, "verbose": False, "dtype": None, "parallel_kwargs": None}, "_molfeat_version": "0.0.1"},
{"name": "MoleculeTransformer", "args": {"featurizer": "fcfp", "n_jobs": 1, "verbose": False, "dtype": None, "parallel_kwargs": None}, "_molfeat_version": "0.0.1"},
]
FEATURIZERS = [
{"name": "MoleculeTransformer", "args": {"featurizer": "ecfp", "n_jobs": 1, "verbose": False, "dtype": None, "parallel_kwargs": None}, "_molfeat_version": "0.0.1"},
{"name": "MoleculeTransformer", "args": {"featurizer": "maccs", "n_jobs": 1, "verbose": False, "dtype": None, "parallel_kwargs": None}, "_molfeat_version": "0.0.1"},
{"name": "MoleculeTransformer", "args": {"featurizer": "fcfp", "n_jobs": 1, "verbose": False, "dtype": None, "parallel_kwargs": None}, "_molfeat_version": "0.0.1"},
]
In [9]:
Copied!
for state_dict in FEATURIZERS:
featurizer = MoleculeTransformer.from_state_dict(state_dict)
# ... Work your magic!
for state_dict in FEATURIZERS:
featurizer = MoleculeTransformer.from_state_dict(state_dict)
# ... Work your magic!