Han#

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

    def preprocess(self, x):
        return x

    def postprocess(self, x):
        """
        Applies an anti-logarithmic linear transformation to a PyTorch tensor.
        """
        adult_age = 20

        # Create a mask for negative and non-negative values
        mask_negative = x < 0
        mask_non_negative = ~mask_negative

        # Initialize the result tensor
        age_tensor = torch.empty_like(x)

        # Exponential transformation for negative values
        age_tensor[mask_negative] = (1 + adult_age) * torch.exp(x[mask_negative]) - 1

        # Linear transformation for non-negative values
        age_tensor[mask_non_negative] = (1 + adult_age) * x[mask_non_negative] + adult_age

        return age_tensor

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

Define clock metadata#

[4]:
model.metadata["clock_name"] = "han"
model.metadata["data_type"] = "DNA methylation"  # Paper: The predictor uses CpG DNA methylation beta values.
model.metadata["species"] = "Homo sapiens"  # Paper: The training data consisted of healthy human blood samples.
model.metadata["year"] = 2020
model.metadata["approved_by_author"] = "✅"
model.metadata["citation"] = "Han, Y., et al. “New targeted approaches for epigenetic age predictions.” BMC Biology 18, 71 (2020)."
model.metadata["doi"] = "https://doi.org/10.1186/s12915-020-00807-2"
model.metadata["notes"] = "Whole-blood 65-CpG multivariable linear age predictor selected for robust targeted measurement; it fits Horvath-transformed chronological age and inverse-transforms the return to years."
model.metadata["research_only"] = None
model.metadata["tissue"] = ["whole blood"]  # Paper: The 973-sample training set consisted of healthy human blood profiles.
model.metadata["predicts"] = ["chronological age"]  # Paper: The model provides epigenetic predictions of chronological age.
model.metadata["training_target"] = ["chronological age"]  # Paper: Before fitting the multivariable model, chronological age was transformed with adult.age set to 20 years.
model.metadata["unit"] = ["years"]  # Paper: pyaging applies the inverse Horvath age transformation and returns age in years.
model.metadata["model_type"] = "linear regression"  # Paper: A multivariable regression model was established from the 65 CpGs; no penalized selection was used for this 65-CpG microarray model.
model.metadata["platform"] = ["Illumina 450K"]  # Paper: The 973 training profiles came from seven studies measured on the 450K Illumina BeadChip.
model.metadata["population"] = "all ages"  # Paper: The training set comprised 973 healthy blood profiles from donors aged 1 to 101 years.
model.metadata["journal"] = "BMC Biology"
model.metadata["last_author"] = "Wolfgang Wagner"
model.metadata["n_features"] = 65
model.metadata["citations"] = 102
model.metadata["citations_date"] = "2026-07-05"

Download clock dependencies#

[5]:
# from authors
cpg_sites = [
    "(Intercept)",
    'cg19283806',
    'cg11807280',
    'cg00329615',
    'cg22454769',
    'cg16867657',
    'cg22796704',
    'cg09809672',
    'cg18618815',
    'cg25533247',
    'cg02286081',
    'cg20222376',
    'cg19344626',
    'cg07082267',
    'cg15845821',
    'cg11741201',
    'cg16054275',
    'cg18933331',
    'cg20249566',
    'cg16604658',
    'cg07583137',
    'cg16008966',
    'cg14556683',
    'cg03746976',
    'cg14314729',
    'cg03431918',
    'cg22156456',
    'cg23078123',
    'cg09748749',
    'cg17457912',
    'cg06492796',
    'cg17593342',
    'cg05308819',
    'cg22512670',
    'cg01820962',
    'cg06639320',
    'cg03224418',
    'cg17436656',
    'cg19500607',
    'cg03735592',
    'cg20669012',
    'cg19761273',
    'cg07080372',
    'cg03638795',
    'cg19722847',
    'cg24711336',
    'cg26935102',
    'cg10221746',
    'cg02085953',
    'cg04604946',
    'cg08558886',
    'cg22361181',
    'cg04208403',
    'cg12623930',
    'cg21572722',
    'cg17885226',
    'cg00748589',
    'cg13033938',
    'cg19784428',
    'cg22016779',
    'cg01974375',
    'cg25256723',
    'cg24724428',
    'cg07547549',
    'cg25410668',
    'cg21296230'
]

coefficients = [
    0.711184864,
    -0.588354066,
    -0.212038592,
    0.014351188,
    0.051285529,
    2.152191741,
    -0.689940565,
    -0.643729974,
    -0.772516118,
    0.116662569,
    -0.233409678,
    0.002802259,
    -0.062172432,
    -0.224027294,
    1.535209377,
    0.344367661,
    0.188826525,
    -0.409150014,
    -0.776065004,
    0.500336643,
    0.06125005,
    -0.391624093,
    0.100449175,
    0.02000403,
    0.266044453,
    -0.259829677,
    0.254063071,
    -0.726178338,
    -1.141947121,
    -0.06322441,
    -0.196926134,
    0.85613244,
    -0.887977059,
    -0.334654336,
    -0.854110638,
    1.916122401,
    0.92208575,
    -0.070665617,
    0.524707402,
    0.319375235,
    0.376055859,
    0.033361038,
    -1.458360975,
    -0.267930475,
    -0.590085273,
    0.642506165,
    0.470352872,
    0.273581649,
    -0.637989789,
    -1.109388991,
    -0.16886654,
    0.662451226,
    -0.091891613,
    0.086290028,
    -0.426089316,
    0.32615363,
    2.535639458,
    -3.626802894,
    0.097619541,
    -0.427604263,
    -0.41418774,
    -0.27412342,
    0.703772384,
    -0.110027226,
    0.283649813,
    0.928585964
]

Load features#

[6]:
df = pd.DataFrame({
    'feature': cpg_sites,
    'coefficient': coefficients
})
model.features = df['feature'][1:].tolist()

Load weights into base model#

[7]:
weights = torch.tensor(df['coefficient'][1:].tolist()).unsqueeze(0)
intercept = torch.tensor([df['coefficient'][0]])

Linear model#

[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 = 'anti_log_linear'
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': 'Han, Yang, et al. "New targeted approaches for epigenetic age '
             'predictions." BMC biology 18 (2020): 1-15.',
 'clock_name': 'han',
 'data_type': 'methylation',
 'doi': 'https://doi.org/10.1186/s12915-020-00807-2',
 'notes': None,
 'research_only': None,
 'species': 'Homo sapiens',
 'version': None,
 'year': 2020}
reference_values: None
preprocess_name: None
preprocess_dependencies: None
postprocess_name: 'anti_log_linear'
postprocess_dependencies: None
features: ['cg19283806', 'cg11807280', 'cg00329615', 'cg22454769', 'cg16867657', 'cg22796704', 'cg09809672', 'cg18618815', 'cg25533247', 'cg02286081', 'cg20222376', 'cg19344626', 'cg07082267', 'cg15845821', 'cg11741201', 'cg16054275', 'cg18933331', 'cg20249566', 'cg16604658', 'cg07583137', 'cg16008966', 'cg14556683', 'cg03746976', 'cg14314729', 'cg03431918', 'cg22156456', 'cg23078123', 'cg09748749', 'cg17457912', 'cg06492796']... [Total elements: 65]
base_model_features: None

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

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

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

base_model.linear.weight: [-0.5883540511131287, -0.21203859150409698, 0.014351188205182552, 0.051285527646541595, 2.152191638946533, -0.6899405717849731, -0.6437299847602844, -0.772516131401062, 0.11666256934404373, -0.23340967297554016, 0.0028022590558975935, -0.062172431498765945, -0.22402729094028473, 1.5352094173431396, 0.344367653131485, 0.1888265311717987, -0.40915000438690186, -0.7760649919509888, 0.5003366470336914, 0.061250049620866776, -0.3916240930557251, 0.10044917464256287, 0.0200040303170681, 0.2660444676876068, -0.25982967019081116, 0.25406306982040405, -0.7261783480644226, -1.1419471502304077, -0.0632244125008583, -0.19692613184452057]... [Tensor of shape torch.Size([1, 65])]
base_model.linear.bias: tensor([0.7112])

%==================================== 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([[ -1.0000],
        [  2.2557],
        [310.9586],
        [  4.2231],
        [ -0.7209],
        [161.4707],
        [ -0.7927],
        [557.5405],
        [  0.8421],
        [ -0.9992]], dtype=torch.float64, grad_fn=<IndexPutBackward0>)

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)