DeconvoluteBloodEPIC#

Index#

  1. Instantiate model class

  2. Define clock metadata

  3. Download clock dependencies

  4. Load features

  5. Load weights into base model

  6. Load reference values

  7. Load preprocess and postprocess objects

  8. Check all clock parameters

  9. Basic test

  10. Save torch model

  11. Clear directory

Let’s first import some packages:

[9]:
import os
import inspect
import shutil
import json
import torch
import pandas as pd
import pyaging as pya

Instantiate model class#

[10]:
def print_entire_class(cls):
    source = inspect.getsource(cls)
    print(source)

print_entire_class(pya.models.DeconvoluteBloodEPIC)

class DeconvoluteBloodEPIC(DeconvolutionSingleCell):
    def __init__(self):
        super().__init__()

[11]:
model = pya.models.DeconvoluteBloodEPIC()

Define clock metadata#

[12]:
model.metadata["clock_name"] = "deconvolutebloodepiccd8tcell"
model.metadata["data_type"] = "DNA methylation"  # Paper: The deconvolution models consume bulk DNA-methylation measurements.
model.metadata["species"] = "Homo sapiens"  # Paper: The EPIC reference represents major human blood-cell types.
model.metadata["year"] = 2018
model.metadata["approved_by_author"] = "⌛"
model.metadata["citation"] = "Salas, L.A., Koestler, D.C., Butler, R.A. et al. An optimized library for reference-based deconvolution of whole-blood biospecimens assayed using the Illumina HumanMethylationEPIC BeadArray. Genome Biology 19, 64 (2018)."
model.metadata["doi"] = "https://doi.org/10.1186/s13059-018-1448-7"
model.metadata["notes"] = "Reference-based constrained deconvolution returning the CD8+ T cell proportion from EPIC-array blood methylation. Pyaging uses the paper's automatic 600-CpG top-hypermethylated/top-hypomethylated reference, not the paper's preferred 450-CpG EPIC IDOL library."
model.metadata["research_only"] = None
model.metadata["tissue"] = ["purified blood leukocytes"]  # Paper: Reference methylation profiles represent six abundant blood-cell types; CpGs are selected for cell-type-specific methylation.
model.metadata["predicts"] = ["CD8+ T cell proportion"]  # Paper: The EPIC mode estimates proportions of six blood-cell types, including the assigned cell type.
model.metadata["training_target"] = ["cell-type-specific methylation contrast"]  # Paper: The automatic reference selects the most hypermethylated and hypomethylated CpGs for each purified leukocyte type.
model.metadata["unit"] = ["proportion"]  # Paper: Estimated proportions are constrained between zero and one and to sum to one.
model.metadata["model_type"] = "reference-based constrained deconvolution"  # Paper: The model solves B = X × P for P by constrained quadratic programming.
model.metadata["platform"] = ["Illumina EPIC"]  # Paper: DeconvoluteBloodEPIC is the variant tailored to the EPIC methylation platform.
model.metadata["population"] = "adults"  # Paper: The reference cells came from 37 anonymous healthy adults; donor ages ranged from 19 to 59 years.
model.metadata["journal"] = "Genome Biology"
model.metadata["last_author"] = "Brock C. Christensen"
model.metadata["n_features"] = 600
model.metadata["citations"] = 13
model.metadata["citations_date"] = "2026-07-05"

Download clock dependencies#

Download reference file#

[13]:
coeff_url = "https://raw.githubusercontent.com/bio-learn/biolearn/master/biolearn/data/EPIC_salas_18_reference.csv"
os.system(f"curl -L {coeff_url} -o EPIC_salas_18_reference.csv")

[13]:
0

Load features#

From CSV file#

[14]:
import numpy as np
import pandas as pd

ref = pd.read_csv('EPIC_salas_18_reference.csv', index_col=0)
model.features = ref.index.astype(str).tolist()

ref_matrix = torch.tensor(ref.values, dtype=torch.float64)
pseudo_inv = torch.linalg.pinv(ref_matrix)

model.pseudo_inv = pseudo_inv
model.cell_index = 4
model.reference_values = torch.nanmean(ref_matrix, dim=1)

Load weights into base model#

From CSV file#

[15]:
# No linear base model; deconvolution logic in model forward

Linear model#

[16]:
model.base_model = None

Load reference values#

[17]:
# reference_values already set above

Load preprocess and postprocess objects#

[18]:
model.preprocess_name = "fill_with_reference_means"
model.preprocess_dependencies = None

[19]:
model.postprocess_name = None
model.postprocess_dependencies = None

Check all clock parameters#

[20]:
pya.utils.print_model_details(model)

%==================================== Model Details ====================================%
Model Attributes:

training: True
metadata: {'approved_by_author': '⌛',
 'citation': 'Ying, Kejun, et al. "A unified framework for systematic curation '
             'and evaluation of aging biomarkers." Nature Aging (2025): 1-17.',
 'clock_name': 'deconvolutebloodepiccd8tcell',
 'data_type': 'methylation',
 'doi': 'https://doi.org/10.1038/s43587-025-00987-y',
 'notes': 'Estimated proportion of cd8_t_cell from EPIC reference.',
 'research_only': None,
 'species': 'Homo sapiens',
 'version': None,
 'year': 2024}
reference_values: [0.20518415451984126, 0.19970558314682532, 0.1024157725119047, 0.2008561107023811, 0.14881559691666668, 0.18691947926984118, 0.09904868163095237, 0.11866279766269845, 0.11413279887698413, 0.07614163463888889, 0.11287183193253969, 0.09002383478571424, 0.15332935412698398, 0.10906152481746033, 0.12821537873412694, 0.11714246567063487, 0.1349594052698413, 0.21058724882142854, 0.23903708456349196, 0.09275976726984132, 0.08500618109523812, 0.17428245038095244, 0.24038344935714295, 0.24988523996428577, 0.1389251376904763, 0.11076903522222221, 0.10888998828968255, 0.09822708860317463, 0.09986797370634924, 0.11328396424206344]... [Tensor of shape torch.Size([600])]
preprocess_name: 'fill_with_reference_means'
preprocess_dependencies: None
postprocess_name: None
postprocess_dependencies: None
features: ['cg02091275', 'cg10613215', 'cg04738410', 'cg23954655', 'cg10825315', 'cg12093275', 'cg05915362', 'cg15472145', 'cg14781394', 'cg03753191', 'cg04554131', 'cg08708961', 'cg05366453', 'cg23889684', 'cg09936080', 'cg11612354', 'cg13525276', 'cg21264165', 'cg24197536', 'cg19935718', 'cg24756631', 'cg14637146', 'cg11311865', 'cg04449166', 'cg08311647', 'cg21870144', 'cg06516865', 'cg03172796', 'cg25446602', 'cg24102319']... [Total elements: 600]
base_model_features: None
base_model: None
pseudo_inv: [0.018423516597868533, 0.02076496108902344, 0.006246045739563567, 0.017322627415437906, 0.012596462279237032, 0.014680027443715469, 0.005387403052654981, 0.00826322984364358, 0.006720451696782447, 0.003520144956793594, 0.007097605150112215, 0.006828305361872053, 0.009242538044463792, 0.005067549321932598, 0.00998902642967594, 0.009749104467339021, 0.010083925381389934, 0.01566659748873227, 0.017857728483860698, 0.005648755947299582, 0.003880839808950153, 0.010944187408221806, 0.014789742065456597, 0.01823321395305361, 0.009616296123845524, 0.005330229811626641, 0.007168874993235975, 0.003581639488212511, 0.004981727200100809, 0.006052278008950252]... [Tensor of shape torch.Size([6, 600])]
cell_index: 4

%==================================== Model Details ====================================%
Model Structure:


%==================================== Model Details ====================================%
Model Parameters and Weights:


%==================================== Model Details ====================================%

Basic Test#

[21]:
torch.manual_seed(42)
input = torch.randn(10, len(model.features), dtype=float)
model.eval()
model.to(float)
pred = model(input)
pred
[21]:
tensor([[0.0857],
        [0.0000],
        [0.2368],
        [0.1179],
        [0.1003],
        [0.4343],
        [0.0000],
        [0.0000],
        [0.4009],
        [0.0000]], dtype=torch.float64)

Save torch model#

[22]:
torch.save(model, f"../weights/{model.metadata['clock_name']}.pt")

Clear directory#

[23]:
# Function to remove a folder and all its contents
def remove_folder(path):
    try:
        shutil.rmtree(path)
        print(f"Deleted folder: {path}")
    except Exception as e:
        print(f"Error deleting folder {path}: {e}")

# Get a list of all files and folders in the current directory
all_items = os.listdir('.')

# Loop through the items
for item in all_items:
    # Check if it's a file and does not end with .ipynb
    if os.path.isfile(item) and not item.endswith('.ipynb'):
        os.remove(item)
        print(f"Deleted file: {item}")
    # Check if it's a folder
    elif os.path.isdir(item):
        remove_folder(item)
Deleted file: EPIC_salas_18_reference.csv