StocH#

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:

[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.StocH)
class StocH(pyagingModel):
    def __init__(self):
        super().__init__()

    def preprocess(self, x):
        return x

    def postprocess(self, x):
        return x

[3]:
model = pya.models.StocH()

Define clock metadata#

[4]:
model.metadata["clock_name"] = 'stoch'
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_1.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': 'stoch',
 '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: ['cg00075967', 'cg00374717', 'cg00864867', 'cg00945507', 'cg01027739', 'cg01353448', 'cg01584473', 'cg01644850', 'cg01656216', 'cg01873645', 'cg01968178', 'cg02085507', 'cg02154074', 'cg02217159', 'cg02331561', 'cg02332492', 'cg02364642', 'cg02388150', 'cg02479575', 'cg02489552', 'cg02580606', 'cg02654291', 'cg02827112', 'cg02972551', 'cg03103192', 'cg03167275', 'cg03270204', 'cg03565323', 'cg03588357', 'cg03760483']... [Total elements: 353]
base_model_features: None

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

base_model: LinearModel(
  (linear): Linear(in_features=353, out_features=1, bias=True)
)

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

base_model.linear.weight: [3.585235357284546, 0.29056739807128906, 0.008150330744683743, 7.167356014251709, 0.07318803668022156, 0.14952702820301056, -0.12038026005029678, -0.6500811576843262, -0.24336983263492584, 0.24592465162277222, -8.944670844357461e-05, -0.2048512101173401, 0.3703913986682892, -1.0299664735794067, 1.7403517961502075, -0.7057934403419495, 0.5467443466186523, 5.111024856567383, 7.80165433883667, 4.5496368408203125, 0.3366563022136688, 1.6183545589447021, 0.034926775842905045, -5.03125524520874, 6.2505011558532715, -2.024871349334717, 0.145379438996315, 0.5371573567390442, -0.9422363042831421, 0.05253010243177414]... [Tensor of shape torch.Size([1, 353])]
base_model.linear.bias: tensor([59.8016])

%==================================== 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([[ 18.5127],
        [146.4057],
        [166.5776],
        [ 85.6306],
        [379.5047],
        [-99.5351],
        [ 42.5195],
        [234.2738],
        [213.7955],
        [ 62.3945]], 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