StocP#
Index#
Let’s first import some packages:
[1]:
import os
import inspect
import shutil
import json
import torch
import pandas as pd
import pyaging as pya
Instantiate model class#
[2]:
def print_entire_class(cls):
source = inspect.getsource(cls)
print(source)
print_entire_class(pya.models.StocP)
class StocP(pyagingModel):
def __init__(self):
super().__init__()
def preprocess(self, x):
return x
def postprocess(self, x):
return x
[3]:
model = pya.models.StocP()
Define clock metadata#
[4]:
model.metadata["clock_name"] = 'stocp'
model.metadata["data_type"] = 'methylation'
model.metadata["species"] = 'Homo sapiens'
model.metadata["year"] = 2024
model.metadata["approved_by_author"] = '✅'
model.metadata["citation"] = "Tong, Huige, et al. \"Quantifying the stochastic component of epigenetic aging.\" Nature Aging (2024): 1-16."
model.metadata["doi"] = "https://doi.org/10.1038/s43587-024-00600-8"
model.metadata["research_only"] = None
model.metadata["notes"] = None
Download clock dependencies#
Download directly with curl#
[5]:
supplementary_url = "https://figshare.com/ndownloader/files/42406308"
supplementary_file_name = "glmStocAll.Rd"
os.system(f"curl -L -o {supplementary_file_name} {supplementary_url}")
[5]:
0
Download from R package#
[6]:
%%writefile download.r
options(repos = c(CRAN = "https://cloud.r-project.org/"))
# Function to extract and save coefficients and intercepts
ExtractCoefficients <- function(){
load("glmStocALL.Rd") # Load in stochastic clock information
# Check the loaded object structure
if (!exists("glmStocALL.lo")) {
stop("The object glmStocALL.lo was not found in the loaded .Rd file.")
}
# List to store coefficients and intercepts for each clock
coefficients_list <- list()
for (c in 1:length(glmStocALL.lo)) {
glm.o <- glmStocALL.lo[[c]]
# Ensure glm.o is a glmnet object
if (!inherits(glm.o, "glmnet")) {
warning(paste("Object at index", c, "is not a glmnet object. Skipping."))
next
}
# Extract the coefficients and intercept from the final iteration
intercept <- glm.o$a0[length(glm.o$a0)]
coefficients <- as.matrix(glm.o$beta)[, length(glm.o$lambda)]
print(length(coefficients))
print(length(rownames(coefficients)))
# Create a data frame with feature names and coefficients
coef_df <- data.frame(
Feature = rownames(as.matrix(glm.o$beta)),
Coefficient = as.numeric(coefficients),
Intercept = rep(intercept, length(coefficients))
)
# Save each clock's coefficients to a CSV file
write.csv(coef_df, file = paste0("Coefficients_Clock_", c, ".csv"), row.names = FALSE)
# Append to the list
coefficients_list[[c]] <- coef_df
}
return(coefficients_list) # Return the list for further inspection if needed
}
# Run the function
coefficients_list <- ExtractCoefficients()
Writing download.r
[7]:
os.system("Rscript download.r")
[7]:
0
Load features#
From CSV file#
[8]:
df = pd.read_csv('Coefficients_Clock_3.csv')
df['feature'] = df['Feature']
df['coefficient'] = df['Coefficient']
model.features = df['feature'].tolist()
Load weights into base model#
[9]:
weights = torch.tensor(df['coefficient'].tolist()).unsqueeze(0)
intercept = torch.tensor([df['Intercept'][0]])
Linear model#
[10]:
base_model = pya.models.LinearModel(input_dim=len(model.features))
base_model.linear.weight.data = weights.float()
base_model.linear.bias.data = intercept.float()
model.base_model = base_model
Load reference values#
[11]:
model.reference_values = None
Load preprocess and postprocess objects#
[12]:
model.preprocess_name = None
model.preprocess_dependencies = None
[13]:
model.postprocess_name = None
model.postprocess_dependencies = None
Check all clock parameters#
[14]:
pya.utils.print_model_details(model)
%==================================== Model Details ====================================%
Model Attributes:
training: True
metadata: {'approved_by_author': '✅',
'citation': 'Tong, Huige, et al. "Quantifying the stochastic component of '
'epigenetic aging." Nature Aging (2024): 1-16.',
'clock_name': 'stocp',
'data_type': 'methylation',
'doi': 'https://doi.org/10.1038/s43587-024-00600-8',
'notes': None,
'research_only': None,
'species': 'Homo sapiens',
'version': None,
'year': 2024}
reference_values: None
preprocess_name: None
preprocess_dependencies: None
postprocess_name: None
postprocess_dependencies: None
features: ['cg15611364', 'cg17605084', 'cg26382071', 'cg12743894', 'cg19287114', 'cg12985418', 'cg19398783', 'cg15963417', 'cg27187881', 'cg09892203', 'cg00943950', 'cg18996776', 'cg16340918', 'cg23832061', 'cg22736354', 'cg04084157', 'cg07265300', 'cg02503970', 'cg11426590', 'cg23710218', 'cg02802055', 'cg13631913', 'cg06493994', 'cg24304712', 'cg01131735', 'cg24208206', 'cg01930621', 'cg19104072', 'cg07850604', 'cg27493997']... [Total elements: 513]
base_model_features: None
%==================================== Model Details ====================================%
Model Structure:
base_model: LinearModel(
(linear): Linear(in_features=513, out_features=1, bias=True)
)
%==================================== Model Details ====================================%
Model Parameters and Weights:
base_model.linear.weight: [-0.26072555780410767, -0.4015197157859802, 0.04175494238734245, 0.08702494204044342, -0.13476140797138214, 0.016701284795999527, 0.07106481492519379, 0.23266158998012543, -0.01433953084051609, -0.9317121505737305, -0.005014453548938036, 0.4903888404369354, -1.6926921606063843, -0.523848831653595, 4.922214031219482, 1.7051185369491577, -0.011739220470190048, -0.7235432863235474, -0.06498837471008301, -0.6380035281181335, 0.22486011683940887, 0.013822232373058796, 10.945517539978027, 0.04070192202925682, -0.028888650238513947, 0.16922490298748016, 0.6264103651046753, -0.018560025840997696, -1.6304043531417847, 0.083981454372406]... [Tensor of shape torch.Size([1, 513])]
base_model.linear.bias: tensor([92.8311])
%==================================== Model Details ====================================%
Basic test#
[15]:
torch.manual_seed(42)
input = torch.randn(10, len(model.features), dtype=float)
model.eval()
model.to(float)
pred = model(input)
pred
[15]:
tensor([[ 272.9167],
[ -8.4764],
[-152.8284],
[ 167.9197],
[ -3.3252],
[ 154.7005],
[ 2.9908],
[ 57.1228],
[ 123.9416],
[ 83.7115]], dtype=torch.float64, grad_fn=<AddmmBackward0>)
Save torch model#
[16]:
torch.save(model, f"../weights/{model.metadata['clock_name']}.pt")
Clear directory#
[17]:
# 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: glmStocAll.Rd
Deleted file: download.r
Deleted folder: .ipynb_checkpoints
Deleted file: Coefficients_Clock_3.csv
Deleted file: Coefficients_Clock_2.csv
Deleted file: Coefficients_Clock_1.csv