File size: 3,103 Bytes
04ae9bb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | """Small, fixed-parameter ablations of the three presentation improvements."""
import json
from pathlib import Path
import numpy as np
from ampd.retained_models import load_retained
from ampd.protein_residual import branch_features, correct
from ampd.protein_representation import protein_matrix
from ampd.omics_boost import transform_pls
METHODS = ('reference', 'ae_full', 'ae_all_latents', 'ae_no_shrinkage',
'pca_full', 'pca_no_compression', 'pca_no_shrinkage',
'pls_full', 'pls_unsupervised', 'pls_no_shrinkage')
def load_npz(path):
with np.load(path, allow_pickle=False) as data:
return dict(data)
def raw_features(proteins, pre):
return ((protein_matrix(proteins, pre)-pre['raw_mean'])/pre['raw_scale']).astype(np.float32).astype(float)
class AblationPredictor:
def __init__(self, bundle):
bundle = Path(bundle)
self.spec = json.loads((bundle/'model.json').read_text())
for key in ('ae_bundle', 'pls_bundle'):
reference = Path(self.spec[key])
if not reference.is_absolute():
self.spec[key] = str((bundle.parents[2] / reference).resolve())
self.ae = load_retained('selected_ae', self.spec['ae_bundle'])
self.pls = load_retained('pls', self.spec['pls_bundle'])
self.pre = load_npz(bundle/'pca_extractor.npz')
self.heads = {name: load_npz(bundle/f'{name}.npz') for name in
['ae_all_latents','pca_full','pca_no_compression','pls_unsupervised']}
def predict(self, events):
# Retained predictors validate exact fields and vocabulary lengths.
ae = self.ae.components(events)
pls = self.pls.predict(events)
proteins = np.asarray([e['proteins'] for e in events], dtype=float)
months = np.asarray([e['visit_month'] for e in events], dtype=float)
baseline = pls['baseline']
np.testing.assert_array_equal(baseline, ae['baseline'])
z = (ae['latent']-self.ae.feature_pre['latent_mean'])/self.ae.feature_pre['latent_scale']
pca = branch_features(months, proteins, 'pca', self.pre)
raw = raw_features(proteins, self.pre)
unsup = transform_pls(proteins, self.heads['pls_unsupervised'])
pca_r = pca @ self.heads['pca_full']['coef'].T
pls_r = transform_pls(proteins, self.pls.params) @ self.pls.params['coef'].T
result = {'reference': baseline, 'ae_full': ae['predictions']['selected_latent_r'],
'pls_full': pls['pls'],
'ae_no_shrinkage': correct(baseline, ae['raw_residuals']['selected_latent_r'], np.ones(4)),
'pca_full': correct(baseline, pca_r, self.heads['pca_full']['weights']),
'pca_no_shrinkage': correct(baseline, pca_r, np.ones(4)),
'pls_no_shrinkage': correct(baseline, pls_r, np.ones(4))}
for name, x in [('ae_all_latents',z),('pca_no_compression',raw),('pls_unsupervised',unsup)]:
head = self.heads[name]
result[name] = correct(baseline, x @ head['coef'].T, head['weights'])
return result
|