GrimAge2PAI1#
Index#
Let’s first import some packages:
[10]:
import os
import inspect
import shutil
import json
import torch
import pandas as pd
import pyaging as pya
import numpy as np
Instantiate model class#
[11]:
def print_entire_class(cls):
source = inspect.getsource(cls)
print(source)
print_entire_class(pya.models.GrimAge2PAI1)
class GrimAge2PAI1(pyagingModel):
def __init__(self):
super().__init__()
def preprocess(self, x):
return x
def postprocess(self, x):
return x
[12]:
model = pya.models.GrimAge2PAI1()
Define clock metadata#
[13]:
model.metadata["clock_name"] = 'grimage2pai1'
model.metadata["data_type"] = 'methylation'
model.metadata["species"] = 'Homo sapiens'
model.metadata["year"] = 2022
model.metadata["approved_by_author"] = '⌛'
model.metadata["citation"] = "Lu, Ake T., et al. \"DNA methylation GrimAge version 2.\" Aging (Albany NY) 14.23 (2022): 9484."
model.metadata["doi"] = "https://doi.org/10.18632/aging.204434"
model.metadata["research_only"] = True
model.metadata["notes"] = None
Download clock dependencies#
[14]:
logger = pya.logger.Logger()
urls = [
"https://pyaging.s3.amazonaws.com/supporting_files/grimage2_subcomponents.csv",
"https://pyaging.s3.amazonaws.com/supporting_files/grimage2.csv",
"https://pyaging.s3.amazonaws.com/supporting_files/datMiniAnnotation3_Gold.csv",
]
dir = "."
for url in urls:
pya.utils.download(url, dir, logger, indent_level=1)
|-----------> Data found in ./grimage2_subcomponents.csv
|-----------> Data found in ./grimage2.csv
|-----------> Data found in ./datMiniAnnotation3_Gold.csv
Load features#
From CSV#
[15]:
df = pd.read_csv('grimage2_subcomponents.csv', index_col=0)
df['Y.pred'].unique()
[15]:
array(['DNAmGDF_15', 'DNAmB2M', 'DNAmCystatin_C', 'DNAmTIMP_1', 'DNAmadm',
'DNAmpai_1', 'DNAmleptin', 'DNAmPACKYRS', 'DNAmlog.CRP',
'DNAmlog.A1C'], dtype=object)
[16]:
df = df[df['Y.pred'] == 'DNAmpai_1']
df['feature'] = df['var']
df['coefficient'] = df['beta']
model.features = df['feature'][1:].tolist()
[17]:
df.head()
[17]:
| Y.pred | var | beta | feature | coefficient | |
|---|---|---|---|---|---|
| 1222 | DNAmpai_1 | Intercept | -1129.601744 | Intercept | -1129.601744 |
| 1223 | DNAmpai_1 | cg10143960 | 62.578405 | cg10143960 | 62.578405 |
| 1224 | DNAmpai_1 | cg07284273 | 321.195548 | cg07284273 | 321.195548 |
| 1225 | DNAmpai_1 | cg17747265 | -476.725772 | cg17747265 | -476.725772 |
| 1226 | DNAmpai_1 | cg09971184 | 6221.585387 | cg09971184 | 6221.585387 |
Load weights into base model#
Linear model#
[18]:
weights = torch.tensor(df['coefficient'][1:].tolist()).unsqueeze(0)
intercept = torch.tensor([df['coefficient'].iloc[0]])
Linear model#
[19]:
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#
[20]:
reference_df = pd.read_csv('datMiniAnnotation3_Gold.csv', index_col=0)
model.reference_values = reference_df.loc[model.features[0:]]['gold'].tolist()
Load preprocess and postprocess objects#
[21]:
model.preprocess_name = None
model.preprocess_dependencies = None
[22]:
model.postprocess_name = None
model.postprocess_dependencies = None
Check all clock parameters#
[23]:
pya.utils.print_model_details(model)
%==================================== Model Details ====================================%
Model Attributes:
training: True
metadata: {'approved_by_author': '⌛',
'citation': 'Lu, Ake T., et al. "DNA methylation GrimAge version 2." Aging '
'(Albany NY) 14.23 (2022): 9484.',
'clock_name': 'grimage2pai1',
'data_type': 'methylation',
'doi': 'https://doi.org/10.18632/aging.204434',
'notes': None,
'research_only': True,
'species': 'Homo sapiens',
'version': None,
'year': 2022}
reference_values: [0.259761798520256, 0.111037942492215, 0.29422000126188, 0.936304436158017, 0.848665856510958, 0.644492617929823, 0.893780204840225, 0.0721244934513398, 0.884599101342416, 0.930445913821681, 0.655185531462437, 0.902782438241089, 0.183574093402218, 0.072529951052299, 0.0175800874436762, 0.0281519446957868, 0.583273419793119, 0.744700209451187, 0.0410865998356346, 0.94438869621023, 0.9447673872075, 0.107357432919823, 0.02228793678189, 0.95796467775612, 0.330065094457947, 0.8354705752375, 0.846530867755149, 0.246508332323783, 0.95191832664452, 0.190899351132785]... [Total elements: 211]
preprocess_name: None
preprocess_dependencies: None
postprocess_name: None
postprocess_dependencies: None
features: ['cg10143960', 'cg07284273', 'cg17747265', 'cg09971184', 'cg00935435', 'cg15651727', 'cg13990487', 'cg00456299', 'cg04974804', 'cg13574848', 'cg25616777', 'cg12386614', 'cg15355146', 'cg19471466', 'cg14709691', 'cg21280384', 'cg14476101', 'cg19693031', 'cg16700924', 'cg20246707', 'cg01365152', 'cg20327105', 'cg25741192', 'cg13678787', 'cg02591356', 'cg26471058', 'cg25939861', 'cg03913456', 'cg24506130', 'cg26009035']... [Total elements: 211]
base_model_features: None
%==================================== Model Details ====================================%
Model Structure:
base_model: LinearModel(
(linear): Linear(in_features=211, out_features=1, bias=True)
)
%==================================== Model Details ====================================%
Model Parameters and Weights:
base_model.linear.weight: [62.57840347290039, 321.195556640625, -476.72576904296875, 6221.58544921875, 7843.1796875, -313.1407775878906, -3855.91650390625, 3294.65234375, 752.2315673828125, -47.931236267089844, 301.0967712402344, -321.5203552246094, 24.402000427246094, -568.0665893554688, -3272.8876953125, 1760.7930908203125, -6259.56103515625, -10119.8154296875, 2037.0191650390625, 2472.403564453125, -3049.89794921875, 4225.28271484375, 982.7288208007812, -152.87660217285156, -356.9750061035156, -4542.0302734375, 433.330810546875, -169.24246215820312, -2095.550537109375, 311.5205078125]... [Tensor of shape torch.Size([1, 211])]
base_model.linear.bias: tensor([-1129.6017])
%==================================== Model Details ====================================%
Basic test#
[24]:
torch.manual_seed(42)
input = torch.randn(10, len(model.features), dtype=float).double()
model.eval()
model.to(float)
pred = model(input)
pred
[24]:
tensor([[-167391.7937],
[ 35688.6704],
[ 339492.3748],
[ 311586.7829],
[ -23585.9907],
[ 25749.6604],
[ 214765.9318],
[ 46047.1643],
[ 276867.1905],
[ -47640.9441]], dtype=torch.float64, grad_fn=<AddmmBackward0>)
Save torch model#
[25]:
torch.save(model, f"../weights/{model.metadata['clock_name']}.pt")
Clear directory#
[26]:
# 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: grimage2_subcomponents.csv
Deleted file: datMiniAnnotation3_Gold.csv
Deleted file: grimage2.csv