SenCultureAge#

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.SenCultureAge)
class SenCultureAge(LinearReferenceClock):
    pass

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

Define clock metadata#

[4]:
model.metadata["clock_name"] = "sencultureage"
model.metadata["data_type"] = "DNA methylation"  # Paper: The predictors use CpG DNA-methylation beta values.
model.metadata["species"] = "Homo sapiens"  # Paper: All three assigned predictors were developed from human cell or human whole-blood datasets.
model.metadata["year"] = 2026
model.metadata["approved_by_author"] = "⌛"
model.metadata["citation"] = "Kasamoto, K., Gibson, J., Moqri, M., Smith, R. & Higgins-Chen, A.T. DNA methylation signatures of cellular senescence are not reversed by senolytic treatment. Aging Cell 25, e70430 (2026)."
model.metadata["doi"] = "https://doi.org/10.1111/acel.70430"
model.metadata["notes"] = "Binomial elastic-net classifier of in-vitro cellular senescence, trained after ComBat correction on pooled human fibroblast and mesenchymal-stromal-cell datasets and restricted to direction-concordant senescence/age/mortality CpGs."
model.metadata["research_only"] = None
model.metadata["tissue"] = ["cultured fibroblasts", "cultured mesenchymal stromal cells"]  # Paper: Training pooled fibroblasts, diploid skin fibroblasts, bone-marrow MSCs and adipose-derived MSCs.
model.metadata["predicts"] = ["cellular senescence"]  # Paper: SenCultureAge is a binary senescence predictor.
model.metadata["training_target"] = ["cellular senescence"]  # Paper: Each in-vitro training sample was labeled senescent or control.
model.metadata["unit"] = ["log odds"]  # Paper: Pyaging applies the binomial-model coefficients without an inverse-logit postprocess.
model.metadata["model_type"] = "elastic net logistic regression"  # Paper: The model used glmnet with family binomial and alpha 0.5.
model.metadata["platform"] = ["Illumina 450K", "Illumina EPIC"]  # Paper: Feature discovery and predictor training used human 450K and EPIC methylation datasets.
model.metadata["population"] = "human cell cultures"  # Paper: Training pooled GSE197723 and GSE227160 senescent and control cultures.
model.metadata["journal"] = "Aging Cell"
model.metadata["last_author"] = "Albert T. Higgins-Chen"
model.metadata["n_features"] = 142
model.metadata["citations"] = 0
model.metadata["citations_date"] = "2026-07-05"

Download clock dependencies#

[5]:
os.system(f"curl -sL -o SenCultureAge_CpGs.csv https://raw.githubusercontent.com/HigginsChenLab/methylCIPHER/19b12296b0d7eb7055a97d068064df635f44ce3e/data-raw/SenescenceAge/SenCultureAge_CpGs.csv")
[5]:
0

Load features#

[6]:
coef_df = pd.read_csv('SenCultureAge_CpGs.csv')
model.features = coef_df['CpG'].tolist()

Load weights into base model#

[7]:
weights = torch.tensor(coef_df['Coefficient'].tolist()).unsqueeze(0).float()
intercept = torch.tensor([-254.6817]).float()
[8]:
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#

[9]:
model.reference_values = None

Load preprocess and postprocess objects#

[10]:
model.preprocess_name = None
model.preprocess_dependencies = None
[11]:
model.postprocess_name = None
model.postprocess_dependencies = None

Check all clock parameters#

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

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

training: True
metadata: {'approved_by_author': '⌛',
 'citation': 'Kasamoto, Kotaro, et al. "DNA methylation clocks for estimating '
             'replicative senescence in human cells." Aging Cell (2026): '
             'e70430.',
 'clock_name': 'sencultureage',
 'data_type': 'methylation',
 'doi': 'https://doi.org/10.1111/acel.70430',
 'notes': None,
 'research_only': None,
 'species': 'Homo sapiens',
 'version': None,
 'year': 2026}
reference_values: None
preprocess_name: None
preprocess_dependencies: None
postprocess_name: None
postprocess_dependencies: None
features: ['cg00127591', 'cg00296189', 'cg00518989', 'cg00666978', 'cg00899883', 'cg01587390', 'cg01779934', 'cg01788025', 'cg02389682', 'cg02484633', 'cg03063057', 'cg03917020', 'cg04081392', 'cg04224041', 'cg04514998', 'cg04796775', 'cg04842828', 'cg05076775', 'cg05198969', 'cg05275153', 'cg06955158', 'cg07166216', 'cg07381973', 'cg07390459', 'cg07528772', 'cg07894586', 'cg07959138', 'cg08155338', 'cg08190044', 'cg08600218']... [Total elements: 142]
base_model_features: None

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

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

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

base_model.linear.weight: [7.832161903381348, 12.214286804199219, 8.027161598205566, 45.58428192138672, 2.2128779888153076, 3.30290150642395, 3.9877500534057617, 0.7730445265769958, 6.563979625701904, 25.338594436645508, 0.7351537942886353, 2.5286097526550293, 2.230402708053589, 0.2821439504623413, 2.5589730739593506, 9.428633689880371, 21.527307510375977, 35.500648498535156, 2.8819007873535156, 3.544675350189209, 6.508420467376709, 6.3784918785095215, 3.5156803131103516, 14.325957298278809, 1.9541112184524536, -0.49621376395225525, 0.5369631052017212, 8.035040855407715, 10.24377727508545, 0.48844996094703674]... [Tensor of shape torch.Size([1, 142])]
base_model.linear.bias: tensor([-254.6817])

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

Basic test#

[13]:
torch.manual_seed(42)
input = torch.randn(10, len(model.features), dtype=float)
model.eval()
model.to(float)
pred = model(input)
pred
[13]:
tensor([[  77.5496],
        [-329.8605],
        [-197.1059],
        [-396.4821],
        [-519.2351],
        [-122.0558],
        [-104.6654],
        [-262.1782],
        [-182.1105],
        [-254.5931]], dtype=torch.float64, grad_fn=<AddmmBackward0>)

Save torch model#

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

Clear directory#

[15]:
# 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: SenCultureAge_CpGs.csv