DeconvoluteBloodEPIC#
Index#
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"] = 'deconvolutebloodepiccd4tcell'
model.metadata["data_type"] = 'methylation'
model.metadata["species"] = 'Homo sapiens'
model.metadata["year"] = 2024
model.metadata["approved_by_author"] = '⌛'
model.metadata["citation"] = "Ying, Kejun, et al. \"A unified framework for systematic curation and evaluation of aging biomarkers.\" Nature Aging (2025): 1-17."
model.metadata["doi"] = "https://doi.org/10.1038/s43587-025-00987-y"
model.metadata["research_only"] = None
model.metadata["notes"] = "Estimated proportion of cd4_t_cell from EPIC reference."
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 = 3
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': 'deconvolutebloodepiccd4tcell',
'data_type': 'methylation',
'doi': 'https://doi.org/10.1038/s43587-025-00987-y',
'notes': 'Estimated proportion of cd4_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: 3
%==================================== 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.0430],
[0.7736],
[0.3586],
[0.0814],
[0.3474],
[0.2807],
[0.8061],
[0.5854],
[0.1188],
[0.5425]], 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