GrimAge2GDF15#
Index#
Let’s first import some packages:
[17]:
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#
[18]:
def print_entire_class(cls):
source = inspect.getsource(cls)
print(source)
print_entire_class(pya.models.GrimAge2GDF15)
class GrimAge2GDF15(pyagingModel):
def __init__(self):
super().__init__()
def preprocess(self, x):
return x
def postprocess(self, x):
return x
[19]:
model = pya.models.GrimAge2GDF15()
Define clock metadata#
[20]:
model.metadata["clock_name"] = 'grimage2gdf15'
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#
[21]:
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)
|-----------> Downloading data to ./grimage2_subcomponents.csv
|-----------> in progress: 100.0000%
|-----------> Downloading data to ./grimage2.csv
|-----------> in progress: 100.0000%%
|-----------> Downloading data to ./datMiniAnnotation3_Gold.csv
|-----------> in progress: 100.0000%
Load features#
From CSV#
[22]:
df = pd.read_csv('grimage2_subcomponents.csv', index_col=0)
df['Y.pred'].unique()
[22]:
array(['DNAmGDF_15', 'DNAmB2M', 'DNAmCystatin_C', 'DNAmTIMP_1', 'DNAmadm',
'DNAmpai_1', 'DNAmleptin', 'DNAmPACKYRS', 'DNAmlog.CRP',
'DNAmlog.A1C'], dtype=object)
[23]:
df = df[df['Y.pred'] == 'DNAmGDF_15']
df['feature'] = df['var']
df['coefficient'] = df['beta']
model.features = ['age'] + df['feature'][2:].tolist()
[24]:
df.head()
[24]:
| Y.pred | var | beta | feature | coefficient | |
|---|---|---|---|---|---|
| 1 | DNAmGDF_15 | Intercept | 1975.798380 | Intercept | 1975.798380 |
| 2 | DNAmGDF_15 | Age | 9.351357 | Age | 9.351357 |
| 3 | DNAmGDF_15 | cg21789941 | 84.364575 | cg21789941 | 84.364575 |
| 4 | DNAmGDF_15 | cg22866430 | 143.696062 | cg22866430 | 143.696062 |
| 5 | DNAmGDF_15 | cg18234973 | 81.378646 | cg18234973 | 81.378646 |
Load weights into base model#
Linear model#
[25]:
weights = torch.tensor(df['coefficient'][1:].tolist()).unsqueeze(0)
intercept = torch.tensor([df['coefficient'].iloc[0]])
Linear model#
[26]:
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#
[27]:
reference_df = pd.read_csv('datMiniAnnotation3_Gold.csv', index_col=0)
model.reference_values = [65] + reference_df.loc[model.features[1:]]['gold'].tolist()
Load preprocess and postprocess objects#
[28]:
model.preprocess_name = None
model.preprocess_dependencies = None
[29]:
model.postprocess_name = None
model.postprocess_dependencies = None
Check all clock parameters#
[30]:
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': 'grimage2gdf15',
'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: [65, 0.401307658505067, 0.860293923434293, 0.947280006512448, 0.930527165222189, 0.255565869348342, 0.0264846427378418, 0.902879804270564, 0.941001569709992, 0.694086158029063, 0.915468273729728, 0.592724594624117, 0.0447197866177092, 0.938589245529331, 0.441107352002649, 0.584475440293952, 0.908361953693711, 0.830024180811995, 0.687215910925347, 0.919973258903412, 0.949349810160961, 0.931787690661021, 0.0689538820445645, 0.703634507571187, 0.912810466343666, 0.946549875970756, 0.949645872518435, 0.0614416243373215, 0.934361053755199, 0.815829866270951]... [Total elements: 138]
preprocess_name: None
preprocess_dependencies: None
postprocess_name: 'cox_to_years'
postprocess_dependencies: None
features: ['age', 'cg21789941', 'cg22866430', 'cg18234973', 'cg26110733', 'cg10636246', 'cg10578779', 'cg17591574', 'cg13371627', 'cg12458003', 'cg13180152', 'cg21459583', 'cg18278247', 'cg06022832', 'cg21566642', 'cg01940273', 'cg08110610', 'cg00684178', 'cg27380813', 'cg03827386', 'cg15849439', 'cg06349851', 'cg25598319', 'cg25845889', 'cg15228492', 'cg03900798', 'cg20576510', 'cg12988996', 'cg11569478', 'cg01603456']... [Total elements: 138]
base_model_features: None
%==================================== Model Details ====================================%
Model Structure:
base_model: LinearModel(
(linear): Linear(in_features=138, out_features=1, bias=True)
)
%==================================== Model Details ====================================%
Model Parameters and Weights:
base_model.linear.weight: [9.351357460021973, 84.36457824707031, 143.69606018066406, 81.37864685058594, 29.040103912353516, -112.49447631835938, 11.24372673034668, -118.38355255126953, -1980.7607421875, 12.090482711791992, 381.68292236328125, 20.09428596496582, 79.332275390625, 93.16657257080078, -42.04895782470703, -56.21128463745117, 50.33696746826172, -4.690526962280273, 23.865774154663086, 110.17974090576172, -504.0067138671875, 454.8924255371094, 3.939922571182251, 10.757635116577148, -21.75938606262207, 191.6622314453125, 342.773193359375, -183.74392700195312, 297.9110107421875, 50.03202438354492]... [Tensor of shape torch.Size([1, 138])]
base_model.linear.bias: tensor([1975.7983])
%==================================== Model Details ====================================%
Basic test#
[31]:
torch.manual_seed(42)
input = torch.randn(10, len(model.features), dtype=float).double()
model.eval()
model.to(float)
pred = model(input)
pred
[31]:
tensor([[ 1552.1130],
[ 4822.7574],
[-4143.4484],
[-1189.7159],
[-3966.4775],
[ 3561.1223],
[-3886.9276],
[ 9188.9426],
[ 7062.6772],
[21668.6671]], dtype=torch.float64, grad_fn=<AddmmBackward0>)
Save torch model#
[32]:
torch.save(model, f"../weights/{model.metadata['clock_name']}.pt")
Clear directory#
[33]:
# 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