GrimAge2TIMP1#
Index#
Let’s first import some packages:
[18]:
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#
[19]:
def print_entire_class(cls):
source = inspect.getsource(cls)
print(source)
print_entire_class(pya.models.GrimAge2TIMP1)
class GrimAge2TIMP1(pyagingModel):
def __init__(self):
super().__init__()
def preprocess(self, x):
return x
def postprocess(self, x):
return x
[20]:
model = pya.models.GrimAge2TIMP1()
Define clock metadata#
[21]:
model.metadata["clock_name"] = 'grimage2timp1'
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#
[22]:
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#
[23]:
df = pd.read_csv('grimage2_subcomponents.csv', index_col=0)
df['Y.pred'].unique()
[23]:
array(['DNAmGDF_15', 'DNAmB2M', 'DNAmCystatin_C', 'DNAmTIMP_1', 'DNAmadm',
'DNAmpai_1', 'DNAmleptin', 'DNAmPACKYRS', 'DNAmlog.CRP',
'DNAmlog.A1C'], dtype=object)
[24]:
df = df[df['Y.pred'] == 'DNAmTIMP_1']
df['feature'] = df['var']
df['coefficient'] = df['beta']
model.features = ['age'] + df['feature'][2:].tolist()
[25]:
df.head()
[25]:
| Y.pred | var | beta | feature | coefficient | |
|---|---|---|---|---|---|
| 381 | DNAmTIMP_1 | Intercept | 15844.595550 | Intercept | 15844.595550 |
| 382 | DNAmTIMP_1 | Age | 127.237985 | Age | 127.237985 |
| 383 | DNAmTIMP_1 | cg19414383 | 576.614231 | cg19414383 | 576.614231 |
| 384 | DNAmTIMP_1 | cg25536676 | -161.490705 | cg25536676 | -161.490705 |
| 385 | DNAmTIMP_1 | cg22820188 | -186.516682 | cg22820188 | -186.516682 |
Load weights into base model#
Linear model#
[26]:
weights = torch.tensor(df['coefficient'][1:].tolist()).unsqueeze(0)
intercept = torch.tensor([df['coefficient'].iloc[0]])
Linear model#
[27]:
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#
[28]:
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#
[29]:
model.preprocess_name = None
model.preprocess_dependencies = None
[30]:
model.postprocess_name = None
model.postprocess_dependencies = None
Check all clock parameters#
[31]:
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': 'grimage2timp1',
'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.468094302501278, 0.156161850378068, 0.322173849647903, 0.53813155435689, 0.464891686467136, 0.273719522051698, 0.572473769572986, 0.150595596016938, 0.296671896347774, 0.905048479501512, 0.0266467658021909, 0.0264560891485892, 0.0329444212464383, 0.755111801817224, 0.952926394062866, 0.32939880219368, 0.757734205728272, 0.195042392161238, 0.684734494069519, 0.770021544584495, 0.737128961116135, 0.834200226405781, 0.371225378253411, 0.556154209442564, 0.21105711074758, 0.575857710666885, 0.587322222427102, 0.572924167653284, 0.146884327789592]... [Total elements: 43]
preprocess_name: None
preprocess_dependencies: None
postprocess_name: 'cox_to_years'
postprocess_dependencies: None
features: ['age', 'cg19414383', 'cg25536676', 'cg22820188', 'cg10753966', 'cg23944298', 'cg07553761', 'cg10202557', 'cg05604079', 'cg11444009', 'cg25149516', 'cg18584747', 'cg20405584', 'cg11299854', 'cg24708145', 'cg25663874', 'cg25325512', 'cg13001142', 'cg06649410', 'cg17501210', 'cg03473532', 'cg20468415', 'cg11782409', 'cg07205627', 'cg08410533', 'cg12483947', 'cg23371436', 'cg24894584', 'cg26266429', 'cg10068417']... [Total elements: 43]
base_model_features: None
%==================================== Model Details ====================================%
Model Structure:
base_model: LinearModel(
(linear): Linear(in_features=43, out_features=1, bias=True)
)
%==================================== Model Details ====================================%
Model Parameters and Weights:
base_model.linear.weight: [127.23798370361328, 576.6142578125, -161.49070739746094, -186.5166778564453, 571.6375732421875, 174.81607055664062, 23.66378402709961, -228.55433654785156, 58.980308532714844, 469.25677490234375, 723.093994140625, 1335.6502685546875, 542.5457153320312, 2160.827880859375, 922.79736328125, 7743.75146484375, -1151.7979736328125, -43.27967834472656, 407.7511901855469, -5735.69287109375, -11.83304500579834, -665.969970703125, 340.971923828125, 207.72994995117188, -32.84348678588867, -1965.6759033203125, 253.16822814941406, 23.78565788269043, 3192.898681640625, 67.02117156982422]... [Tensor of shape torch.Size([1, 43])]
base_model.linear.bias: tensor([15844.5957])
%==================================== Model Details ====================================%
Basic test#
[32]:
torch.manual_seed(42)
input = torch.randn(10, len(model.features), dtype=float).double()
model.eval()
model.to(float)
pred = model(input)
pred
[32]:
tensor([[ 12604.4938],
[ -702.9277],
[-10733.5192],
[ 32897.7930],
[ 5511.9481],
[ 32294.2303],
[ 19263.9128],
[ 19902.7039],
[ 51716.0471],
[ 37174.8860]], dtype=torch.float64, grad_fn=<AddmmBackward0>)
Save torch model#
[33]:
torch.save(model, f"../weights/{model.metadata['clock_name']}.pt")
Clear directory#
[34]:
# 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