Download code/ampd/improvement_ablation.py from fryabcfry/cs4486-amp-pd-checkpoints: direct link, hf CLI and curl.
- Browser
- Download file 3.1 kB
-
https://huggingface.co/fryabcfry/cs4486-amp-pd-checkpoints/resolve/main/code/ampd/improvement_ablation.py
- Command line
-
hf download hf://fryabcfry/cs4486-amp-pd-checkpoints/code/ampd/improvement_ablation.py
-
curl -L -o improvement_ablation.py https://huggingface.co/fryabcfry/cs4486-amp-pd-checkpoints/resolve/main/code/ampd/improvement_ablation.py
3.1 kB
| """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 | |