|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +from sklearn.base import BaseEstimator, TransformerMixin |
| 5 | +from sklearn.preprocessing._data import KernelCenterer |
| 6 | +from sklearn.utils.validation import ( |
| 7 | + FLOAT_DTYPES, |
| 8 | + _check_sample_weight, |
| 9 | + check_is_fitted, |
| 10 | + validate_data, |
| 11 | +) |
| 12 | + |
| 13 | +from qstack.qml import b2r2, slatm |
| 14 | + |
| 15 | +def _restore_from_pickle(objname: str, version: int, hypers: dict, params: dict|None): |
| 16 | + pass |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +class B2R2Representation(TransformerMixin, BaseEstimator): |
| 24 | + """Transform reactions into their B2R2 representations |
| 25 | +
|
| 26 | + Reference: |
| 27 | + P. van Gerwen, A. Fabrizio, M. D. Wodrich, C. Corminboeuf, |
| 28 | + "Physics-based representations for machine learning properties of chemical reactions", |
| 29 | + Mach. Learn.: Sci. Technol. 3, 045005 (2022), doi:10.1088/2632-2153/ac8f1a |
| 30 | +
|
| 31 | + This representation can be computed for molecules or for reactions, |
| 32 | + by giving to this transformer a list of one or a list of the other. |
| 33 | + Note that no fitting is required, and this object is a simple shim best used |
| 34 | + in pipeline objects. |
| 35 | +
|
| 36 | + Molecules are ASE molecule objects, or any object with `.numbers` and `.positions` (in Å) properties. |
| 37 | + Reactions, however, are ``qstack.qml.b2r2.Reaction objects, |
| 38 | + or tuples of two lists of molecules (reactants, products). |
| 39 | +
|
| 40 | + Parameters |
| 41 | + ---------- |
| 42 | + variant: str, default "l" |
| 43 | + B2R2 variant to compute. Options: |
| 44 | + - 'l': Local variant with element-resolved skewed Gaussians (default). |
| 45 | + - 'a': Agnostic variant with element-pair Gaussians. |
| 46 | + - 'n': Nuclear variant with combined skewed Gaussians. |
| 47 | +
|
| 48 | + progress: bool, default False |
| 49 | + If True, displays progress bar |
| 50 | +
|
| 51 | + rcut: float, default 3.5 |
| 52 | + Cutoff radius for bond detection in Å |
| 53 | +
|
| 54 | + gridspace: float, default 0.03 |
| 55 | + Grid spacing for discretization in Å |
| 56 | +
|
| 57 | +
|
| 58 | + Attributes |
| 59 | + ---------- |
| 60 | + None |
| 61 | +
|
| 62 | + Examples |
| 63 | + -------- |
| 64 | + [ fixme ] |
| 65 | + """ |
| 66 | + |
| 67 | + def __init__( |
| 68 | + self, |
| 69 | + variant='l', |
| 70 | + progress=False, |
| 71 | + rcut=b2r2.defaults.rcut, |
| 72 | + gridspace=b2r2.defaults.gridspace, |
| 73 | + ): |
| 74 | + """Initialize StandardFlexibleScaler.""" |
| 75 | + self.variant = variant |
| 76 | + self.progress = progress |
| 77 | + self.rcut = rcut |
| 78 | + self.gridspace = gridspace |
| 79 | + self.elements_ = [] |
| 80 | + |
| 81 | + def __reduce__(self): |
| 82 | + return ( |
| 83 | + _restore_from_pickle, |
| 84 | + "B2R2", 1, |
| 85 | + dict( |
| 86 | + variant = self.variant, |
| 87 | + progress = self.progress, |
| 88 | + rcut = self.rcut, |
| 89 | + gridspace = self.gridspace, |
| 90 | + ), |
| 91 | + {'elements_': self.elements} if self.elements else None, |
| 92 | + ) |
| 93 | + |
| 94 | + def fit(self, X, y=None, sample_weight=None): |
| 95 | + """Determine the types of elements to consider, by feeding them from all objects to consider later. |
| 96 | +
|
| 97 | + Parameters |
| 98 | + ---------- |
| 99 | + X : numpy.ndarray of shape (n_samples, n_features) |
| 100 | + The data used to compute the mean and standard deviation |
| 101 | + used for later scaling along the features axis. |
| 102 | + y: None |
| 103 | + Ignored. |
| 104 | + sample_weight: numpy.ndarray of shape (n_samples,) |
| 105 | + Weights for each sample. Sample weighting can be used to center |
| 106 | + (and scale) data using a weighted mean. Weights are internally |
| 107 | + normalized before preprocessing. |
| 108 | +
|
| 109 | + Returns |
| 110 | + ------- |
| 111 | + self : object |
| 112 | + Fitted scaler. |
| 113 | + """ |
| 114 | + |
| 115 | + elems = set() |
| 116 | + for obj in X: |
| 117 | + if isinstance(obj, tuple) and len(obj) == 2: |
| 118 | + reac_mols = obj[0] + obj[1] |
| 119 | + elif isinstance(obj, b2r2.Reaction): |
| 120 | + reac_mols = obj.reactants + obj.products |
| 121 | + elif hasattr(X[0], "numbers") and hasattr(X[0], "positions"): |
| 122 | + reac_mols = [obj] |
| 123 | + for mol in reac_mols: |
| 124 | + elems.update(mol.numbers) |
| 125 | + self.elements_ = sorted(elems) |
| 126 | + |
| 127 | + return self |
| 128 | + |
| 129 | + def transform(self, X, y=None, copy=None): |
| 130 | + """Normalize a vector based on previously computed mean and scaling. |
| 131 | +
|
| 132 | + Parameters |
| 133 | + ---------- |
| 134 | + X : list of length n_samples, of molecules or list of reactions |
| 135 | + The chemical objects to compute representations of. |
| 136 | + Please note they should all be of the same type (reactions OR molecules) |
| 137 | + y: None |
| 138 | + Ignored. |
| 139 | + copy : bool, default=None |
| 140 | + Ignored |
| 141 | +
|
| 142 | + Returns |
| 143 | + ------- |
| 144 | + X : {array-like} of shape (n_samples, n_features) |
| 145 | + Transformed data. |
| 146 | + """ |
| 147 | + |
| 148 | + if self.variant=='l': |
| 149 | + get_b2r2_molecular=b2r2.get_b2r2_l_molecular |
| 150 | + combine = lambda r,p: p-r |
| 151 | + elif self.variant=='a': |
| 152 | + get_b2r2_molecular = b2r2.get_b2r2_a_molecular |
| 153 | + combine = lambda r,p: p-r |
| 154 | + elif self.variant=='n': |
| 155 | + get_b2r2_molecular=b2r2.get_b2r2_n_molecular |
| 156 | + combine = lambda r,p: np.hstack((r,p)) |
| 157 | + else: |
| 158 | + raise RuntimeError(f'Unknown B2R2 {variant=}') |
| 159 | + |
| 160 | + if isinstance(X[0], tuple) and len(X[0]) == 2: |
| 161 | + mode = "reac-2" |
| 162 | + first_array = self._get_reac_array(X[0][0], X[0][1], get_b2r2_molecular, combine) |
| 163 | + elif isinstance(X[0], b2r2.Reaction): |
| 164 | + mode = "reac" |
| 165 | + first_array = self._get_reac_array(X[0].reactants, X[0].products, get_b2r2_molecular, combine) |
| 166 | + elif hasattr(X[0], "numbers") and hasattr(X[0], "positions"): |
| 167 | + mode = "mol" |
| 168 | + first_array = self._get_mol_array(X[0], get_b2r2_molecular) |
| 169 | + else: |
| 170 | + raise ValueError("unknown type of input") |
| 171 | + |
| 172 | + assert first_array.ndim==1 |
| 173 | + full_array = np.empty_like(first_array, shape=(len(X), *first_array.shape)) |
| 174 | + full_array[0] = first_array |
| 175 | + |
| 176 | + for object_i,x in enumerate(X[1:]): |
| 177 | + if mode == "reac-2": |
| 178 | + full_array[object_i+1] = self._get_reac_array(x[0], x[1], get_b2r2_molecular, combine) |
| 179 | + elif mode == "reac": |
| 180 | + full_array[object_i+1] = self._get_reac_array(x.reactants, x.products, get_b2r2_molecular, combine) |
| 181 | + elif mode == "mol": |
| 182 | + full_array[object_i+1] = self._get_mol_array(x, get_b2r2_molecular) |
| 183 | + return full_array |
| 184 | + |
| 185 | + def _get_reac_array(self, reactants, products, mol_rep_func, combine): |
| 186 | + reac_repr = self._get_mol_array(reactants[0], mol_rep_func) |
| 187 | + for reac in reactants[1:]: |
| 188 | + reac_repr += self._get_mol_array(reac, mol_rep_func) |
| 189 | + prod_repr = self._get_mol_array(products[0], mol_rep_func) |
| 190 | + for prod in products[1:]: |
| 191 | + prod_repr += self._get_mol_array(prod, mol_rep_func) |
| 192 | + return combine(reac_repr, prod_repr) |
| 193 | + |
| 194 | + def _get_mol_array(self, mol, mol_rep_func): |
| 195 | + return mol_rep_func(mol.numbers, mol.positions, self.rcut, self.gridspace, self.elements_) |
0 commit comments