GrimAge2CystatinC#
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.GrimAge2CystatinC)
class GrimAge2CystatinC(pyagingModel):
def __init__(self):
super().__init__()
def preprocess(self, x):
return x
def postprocess(self, x):
return x
[19]:
model = pya.models.GrimAge2CystatinC()
Define clock metadata#
[20]:
model.metadata["clock_name"] = 'grimage2cystatinc'
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'] == 'DNAmCystatin_C']
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 | |
|---|---|---|---|---|---|
| 292 | DNAmCystatin_C | Intercept | 1.091528e+06 | Intercept | 1.091528e+06 |
| 293 | DNAmCystatin_C | Age | 2.589668e+03 | Age | 2.589668e+03 |
| 294 | DNAmCystatin_C | cg00695391 | -1.508866e+04 | cg00695391 | -1.508866e+04 |
| 295 | DNAmCystatin_C | cg16305333 | 3.655312e+04 | cg16305333 | 3.655312e+04 |
| 296 | DNAmCystatin_C | cg00970752 | -1.419440e+04 | cg00970752 | -1.419440e+04 |
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': 'grimage2cystatinc',
'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.324296328128978, 0.110947182656128, 0.599964776306258, 0.033798963669035, 0.501122209172917, 0.927249525526882, 0.0264846427378418, 0.706308157997548, 0.616984149006488, 0.952560036323233, 0.914914878486734, 0.480728610437502, 0.0203892784279929, 0.591072581390008, 0.39033794555722, 0.143730730016895, 0.934336765964471, 0.0238644749119876, 0.53813155435689, 0.222068390557704, 0.521690074884018, 0.322768214443012, 0.0343123382918661, 0.834256097355906, 0.0330349356427245, 0.149149117983497, 0.0277661466610348, 0.857167445559308, 0.945303680845768]... [Total elements: 88]
preprocess_name: None
preprocess_dependencies: None
postprocess_name: 'cox_to_years'
postprocess_dependencies: None
features: ['age', 'cg00695391', 'cg16305333', 'cg00970752', 'cg04967775', 'cg06901711', 'cg01431830', 'cg10578779', 'cg26359730', 'cg10585941', 'cg01939962', 'cg06809285', 'cg11336382', 'cg09920725', 'cg24000528', 'cg13772414', 'cg08007899', 'cg15808795', 'cg24110177', 'cg10753966', 'cg00481951', 'cg04748223', 'cg22518157', 'cg22714094', 'cg10177080', 'cg15845365', 'cg27628536', 'cg24765579', 'cg15988937', 'cg02081065']... [Total elements: 88]
base_model_features: None
%==================================== Model Details ====================================%
Model Structure:
base_model: LinearModel(
(linear): Linear(in_features=88, out_features=1, bias=True)
)
%==================================== Model Details ====================================%
Model Parameters and Weights:
base_model.linear.weight: [2589.667724609375, -15088.66015625, 36553.1171875, -14194.40234375, 177517.65625, 2264.057861328125, -3639.993896484375, 146477.484375, 11819.2109375, 438.5729064941406, -35147.8515625, -146515.59375, 3482.841796875, 46536.5078125, -19400.65625, 5430.861328125, -2332.132080078125, 28774.947265625, 37130.91796875, 4642.5302734375, 2118.04541015625, 8312.26953125, 1070.0323486328125, 53286.4609375, 6551.3515625, 1233.9503173828125, -2420.194580078125, 3439.35546875, 13203.8330078125, 35212.96875]... [Tensor of shape torch.Size([1, 88])]
base_model.linear.bias: tensor([1091528.5000])
%==================================== 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([[1423887.5710],
[2200198.5268],
[ 709535.9120],
[1259329.3764],
[2154753.3545],
[ 523167.1884],
[ 12927.1883],
[ 387033.5313],
[1940121.0051],
[ 586847.2274]], 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