PCGrimAge#

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

        self.center = nn.Parameter(torch.empty(78464), requires_grad=False)
        self.rotation = nn.Parameter(torch.empty((78464, 1933)), requires_grad=False)

        self.PCPACKYRS = None
        self.PCADM = None
        self.PCB2M = None
        self.PCCystatinC = None
        self.PCGDF15 = None
        self.PCLeptin = None
        self.PCPAI1 = None
        self.PCTIMP1 = None

        self.features_PCPACKYRS = None
        self.features_PCADM = None
        self.features_PCB2M = None
        self.features_PCCystatinC = None
        self.features_PCGDF15 = None
        self.features_PCLeptin = None
        self.features_PCPAI1 = None
        self.features_PCTIMP1 = None

    def forward(self, x):
        CpGs = x[:, :-2]
        Female = x[:, -2].unsqueeze(1)
        Age = x[:, -1].unsqueeze(1)

        CpGs = CpGs - self.center  # Apply centering
        PCs = torch.mm(CpGs, self.rotation)  # Apply PCA rotation

        x = torch.concat([PCs, Female, Age], dim=1)

        PCPACKYRS = self.PCPACKYRS(x[:, self.features_PCPACKYRS])
        PCADM = self.PCADM(x[:, self.features_PCADM])
        PCB2M = self.PCB2M(x[:, self.features_PCB2M])
        PCCystatinC = self.PCCystatinC(x[:, self.features_PCCystatinC])
        PCGDF15 = self.PCGDF15(x[:, self.features_PCGDF15])
        PCLeptin = self.PCLeptin(x[:, self.features_PCLeptin])
        PCPAI1 = self.PCPAI1(x[:, self.features_PCPAI1])
        PCTIMP1 = self.PCTIMP1(x[:, self.features_PCTIMP1])

        x = torch.concat(
            [
                PCPACKYRS,
                PCADM,
                PCB2M,
                PCCystatinC,
                PCGDF15,
                PCLeptin,
                PCPAI1,
                PCTIMP1,
                Age,
                Female,
            ],
            dim=1,
        )

        x = self.base_model(x)

        return x

    def preprocess(self, x):
        return x

    def postprocess(self, x):
        return x

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

Define clock metadata#

[4]:
model.metadata["clock_name"] = 'pcgrimage'
model.metadata["data_type"] = 'methylation'
model.metadata["species"] = 'Homo sapiens'
model.metadata["year"] = 2022
model.metadata["approved_by_author"] = '⌛'
model.metadata["citation"] = "Higgins-Chen, Albert T., et al. \"A computational solution for bolstering reliability of epigenetic clocks: Implications for clinical trials and longitudinal tracking.\" Nature aging 2.7 (2022): 644-661."
model.metadata["doi"] = "https://doi.org/10.1038/s43587-022-00248-2"
model.metadata["research_only"] = None
model.metadata["notes"] = None

Download clock dependencies#

[5]:
#download PCClock Rdata file from https://yale.app.box.com/s/kq0b0a7lxckxjvaz7x5n4keaug7tewry
logger = pya.logger.Logger()
url = "https://pyaging.s3.amazonaws.com/supporting_files/CalcAllPCClocks.RData"
dir = "."
pya.utils.download(url, dir, logger, indent_level=1)
|-----------> Downloading data to ./CalcAllPCClocks.RData
|-----------> in progress: 100.0000%

Download from R package#

[6]:
%%writefile download.r

library(dplyr)
library(tibble)
library(tidyr)
library(jsonlite)

load(file = "CalcAllPCClocks.RData")

print(ls(all.names = TRUE))

CalcPCGrimAge$rotation.names = colnames(CalcPCGrimAge$rotation)

CalcPCGrimAge$PCPACKYRS.model.names = names(CalcPCGrimAge$PCPACKYRS.model)
CalcPCGrimAge$PCADM.model.names = names(CalcPCGrimAge$PCADM.model)
CalcPCGrimAge$PCB2M.model.names = names(CalcPCGrimAge$PCB2M.model)
CalcPCGrimAge$PCCystatinC.model.names = names(CalcPCGrimAge$PCCystatinC.model)
CalcPCGrimAge$PCGDF15.model.names = names(CalcPCGrimAge$PCGDF15.model)
CalcPCGrimAge$PCLeptin.model.names = names(CalcPCGrimAge$PCLeptin.model)
CalcPCGrimAge$PCPAI1.model.names = names(CalcPCGrimAge$PCPAI1.model)
CalcPCGrimAge$PCTIMP1.model.names = names(CalcPCGrimAge$PCTIMP1.model)

write_json(CalcPCGrimAge, "CalcPCGrimAge.json", digits = 9)
write_json(CpGs, "PCGrimAgeCpGs.json")
write_json(imputeMissingCpGs, "PCGrimAgeReferenceCpGBetas.json", digits = 10)
Writing download.r
[7]:
os.system("Rscript download.r")
[7]:
0

Load features#

From JSON file#

[ ]:
with open('PCGrimAgeCpGs.json', 'r') as f:
    features = json.load(f)
model.features = features + ['female'] + ['age']

Load weights into base model#

From JSON file#

[9]:
with open('CalcPCGrimAge.json', 'r') as f:
    weights_dict = json.load(f)

PC component#

[10]:
model.center.data = torch.tensor(weights_dict['center']).float()
model.rotation.data = torch.tensor(weights_dict['rotation']).float()

Linear model#

[11]:
all_features = weights_dict['rotation.names'] + ['Female'] + ['Age']

model.PCPACKYRS = pya.models.LinearModel(input_dim=len(weights_dict['PCPACKYRS.model.names']))
model.PCPACKYRS.linear.weight.data = torch.tensor(weights_dict['PCPACKYRS.model']).unsqueeze(0).float()
model.PCPACKYRS.linear.bias.data = torch.tensor(weights_dict['PCPACKYRS.intercept']).float()
model.features_PCPACKYRS = indices = torch.tensor([all_features.index(item) for item in weights_dict['PCPACKYRS.model.names'] if item in all_features]).long()

model.PCADM = pya.models.LinearModel(input_dim=len(weights_dict['PCADM.model.names']))
model.PCADM.linear.weight.data = torch.tensor(weights_dict['PCADM.model']).unsqueeze(0).float()
model.PCADM.linear.bias.data = torch.tensor(weights_dict['PCADM.intercept']).float()
model.features_PCADM = indices = torch.tensor([all_features.index(item) for item in weights_dict['PCADM.model.names'] if item in all_features]).long()

model.PCB2M = pya.models.LinearModel(input_dim=len(weights_dict['PCB2M.model.names']))
model.PCB2M.linear.weight.data = torch.tensor(weights_dict['PCB2M.model']).unsqueeze(0).float()
model.PCB2M.linear.bias.data = torch.tensor(weights_dict['PCB2M.intercept']).float()
model.features_PCB2M = indices = torch.tensor([all_features.index(item) for item in weights_dict['PCB2M.model.names'] if item in all_features]).long()

model.PCCystatinC = pya.models.LinearModel(input_dim=len(weights_dict['PCCystatinC.model.names']))
model.PCCystatinC.linear.weight.data = torch.tensor(weights_dict['PCCystatinC.model']).unsqueeze(0).float()
model.PCCystatinC.linear.bias.data = torch.tensor(weights_dict['PCCystatinC.intercept']).float()
model.features_PCCystatinC = indices = torch.tensor([all_features.index(item) for item in weights_dict['PCCystatinC.model.names'] if item in all_features]).long()

model.PCGDF15 = pya.models.LinearModel(input_dim=len(weights_dict['PCGDF15.model.names']))
model.PCGDF15.linear.weight.data = torch.tensor(weights_dict['PCGDF15.model']).unsqueeze(0).float()
model.PCGDF15.linear.bias.data = torch.tensor(weights_dict['PCGDF15.intercept']).float()
model.features_PCGDF15 = indices = torch.tensor([all_features.index(item) for item in weights_dict['PCGDF15.model.names'] if item in all_features]).long()

model.PCLeptin = pya.models.LinearModel(input_dim=len(weights_dict['PCLeptin.model.names']))
model.PCLeptin.linear.weight.data = torch.tensor(weights_dict['PCLeptin.model']).unsqueeze(0).float()
model.PCLeptin.linear.bias.data = torch.tensor(weights_dict['PCLeptin.intercept']).float()
model.features_PCLeptin = indices = torch.tensor([all_features.index(item) for item in weights_dict['PCLeptin.model.names'] if item in all_features]).long()

model.PCPAI1 = pya.models.LinearModel(input_dim=len(weights_dict['PCPAI1.model.names']))
model.PCPAI1.linear.weight.data = torch.tensor(weights_dict['PCPAI1.model']).unsqueeze(0).float()
model.PCPAI1.linear.bias.data = torch.tensor(weights_dict['PCPAI1.intercept']).float()
model.features_PCPAI1 = indices = torch.tensor([all_features.index(item) for item in weights_dict['PCPAI1.model.names'] if item in all_features]).long()

model.PCTIMP1 = pya.models.LinearModel(input_dim=len(weights_dict['PCTIMP1.model.names']))
model.PCTIMP1.linear.weight.data = torch.tensor(weights_dict['PCTIMP1.model']).unsqueeze(0).float()
model.PCTIMP1.linear.bias.data = torch.tensor(weights_dict['PCTIMP1.intercept']).float()
model.features_PCTIMP1 = indices = torch.tensor([all_features.index(item) for item in weights_dict['PCTIMP1.model.names'] if item in all_features]).long()

Linear model#

[12]:
base_model = pya.models.LinearModel(input_dim=len(weights_dict['components']))

base_model.linear.weight.data = torch.tensor(weights_dict['PCGrimAge.model']).unsqueeze(0).float()
base_model.linear.bias.data = torch.tensor(weights_dict['PCGrimAge.intercept']).float()

model.base_model = base_model
[13]:
weights_dict['components']
[13]:
['PCPACKYRS',
 'PCADM',
 'PCB2M',
 'PCCystatinC',
 'PCGDF15',
 'PCLeptin',
 'PCPAI1',
 'PCTIMP1',
 'Age',
 'Female']

Load reference values#

From JSON file#

[14]:
with open('PCGrimAgeReferenceCpGBetas.json', 'r') as f:
    reference_feature_values = json.load(f)
model.reference_values = reference_feature_values + [1, 65] # 65yo F

Load preprocess and postprocess objects#

[15]:
model.preprocess_name = None
model.preprocess_dependencies = None
[16]:
model.postprocess_name = None
model.postprocess_dependencies = None

Check all clock parameters#

[17]:
pya.utils.print_model_details(model)

%==================================== Model Details ====================================%
Model Attributes:

training: True
metadata: {'approved_by_author': '⌛',
 'citation': 'Higgins-Chen, Albert T., et al. "A computational solution for '
             'bolstering reliability of epigenetic clocks: Implications for '
             'clinical trials and longitudinal tracking." Nature aging 2.7 '
             '(2022): 644-661.',
 'clock_name': 'pcgrimage',
 'data_type': 'methylation',
 'doi': 'https://doi.org/10.1038/s43587-022-00248-2',
 'notes': None,
 'research_only': None,
 'species': 'Homo sapiens',
 'version': None,
 'year': 2022}
reference_values: [0.82635363384, 0.18898814441, 0.72938889209, 0.8680421375, 0.090353927561, 0.0066895021761, 0.48924643338, 0.87262052546, 0.87955373232, 0.04847264273, 0.0093070979947, 0.16393676218, 0.058440936082, 0.18857484916, 0.58239394253, 0.86564960457, 0.58457176982, 0.82903550669, 0.065646928047, 0.8500055061, 0.79155429878, 0.83499889314, 0.7754384128, 0.0039641831799, 0.50570339787, 0.60547040884, 0.29093154314, 0.88154845595, 0.46844171936, 0.79205361021]... [Total elements: 78466]
preprocess_name: None
preprocess_dependencies: None
postprocess_name: None
postprocess_dependencies: None
features: ['cg00000292', 'cg00000714', 'cg00001099', 'cg00001446', 'cg00001747', 'cg00002116', 'cg00002224', 'cg00002426', 'cg00002646', 'cg00002660', 'cg00002719', 'cg00002810', 'cg00003091', 'cg00003287', 'cg00003345', 'cg00003529', 'cg00003578', 'cg00003625', 'cg00003994', 'cg00004429', 'cg00004608', 'cg00004806', 'cg00005072', 'cg00005306', 'cg00005619', 'cg00005849', 'cg00006081', 'cg00006459', 'cg00007076', 'cg00007221']... [Total elements: 78466]
base_model_features: None
features_PCPACKYRS: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]... [Tensor of shape torch.Size([1234])]
features_PCADM: [0, 1, 2, 3, 4, 6, 1232, 7, 8, 10, 11, 1233, 13, 14, 15, 16, 17, 18, 20, 21, 22, 24, 25, 26, 27, 29, 31, 1234, 36, 38]... [Tensor of shape torch.Size([331])]
features_PCB2M: [0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1233, 13, 17, 18, 20, 21, 24, 25, 27, 29, 31, 32, 1234, 34, 35, 37, 38, 40]... [Tensor of shape torch.Size([286])]
features_PCCystatinC: [0, 1, 2, 3, 4, 8, 9, 10, 11, 13, 15, 17, 19, 21, 25, 26, 27, 28, 29, 1234, 33, 39, 1235, 43, 45, 46, 47, 1236, 1530, 50]... [Tensor of shape torch.Size([174])]
features_PCGDF15: [0, 2, 3, 4, 5, 1232, 7, 9, 10, 11, 1233, 13, 14, 15, 17, 18, 21, 22, 24, 27, 30, 42, 46, 47, 1236, 55, 72, 74, 82, 120]... [Tensor of shape torch.Size([96])]
features_PCLeptin: [1, 2, 3, 4, 6, 7, 8, 11, 13, 15, 16, 17, 20, 22, 23, 24, 25, 26, 28, 29, 30, 31, 1234, 33, 34, 36, 37, 38, 40, 45]... [Tensor of shape torch.Size([192])]
features_PCPAI1: [0, 1, 3, 4, 5, 6, 7, 9, 11, 12, 13, 14, 15, 16, 17, 19, 20, 21, 24, 25, 26, 27, 28, 32, 1234, 33, 34, 35, 36, 38]... [Tensor of shape torch.Size([631])]
features_PCTIMP1: [0, 2, 4, 5, 6, 7, 9, 11, 12, 1233, 13, 14, 15, 17, 18, 19, 20, 21, 24, 25, 26, 27, 29, 31, 32, 1234, 33, 35, 40, 42]... [Tensor of shape torch.Size([102])]

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

PCPACKYRS: LinearModel(
  (linear): Linear(in_features=1234, out_features=1, bias=True)
)
PCADM: LinearModel(
  (linear): Linear(in_features=331, out_features=1, bias=True)
)
PCB2M: LinearModel(
  (linear): Linear(in_features=286, out_features=1, bias=True)
)
PCCystatinC: LinearModel(
  (linear): Linear(in_features=174, out_features=1, bias=True)
)
PCGDF15: LinearModel(
  (linear): Linear(in_features=96, out_features=1, bias=True)
)
PCLeptin: LinearModel(
  (linear): Linear(in_features=192, out_features=1, bias=True)
)
PCPAI1: LinearModel(
  (linear): Linear(in_features=631, out_features=1, bias=True)
)
PCTIMP1: LinearModel(
  (linear): Linear(in_features=102, out_features=1, bias=True)
)
base_model: LinearModel(
  (linear): Linear(in_features=10, out_features=1, bias=True)
)

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

center: [0.8050224781036377, 0.18032841384410858, 0.7313324213027954, 0.8423994779586792, 0.09859713166952133, 0.02947637252509594, 0.4717109501361847, 0.8521727323532104, 0.8586175441741943, 0.06356251239776611, 0.05615577474236488, 0.14219772815704346, 0.05609125643968582, 0.19808007776737213, 0.5805700421333313, 0.8329296708106995, 0.5806332230567932, 0.8273633718490601, 0.07795383781194687, 0.8332058191299438, 0.7649531364440918, 0.8077301979064941, 0.7726192474365234, 0.032133087515830994, 0.4985392689704895, 0.5387966632843018, 0.26533183455467224, 0.8369942307472229, 0.4400714635848999, 0.7762642502784729]... [Tensor of shape torch.Size([78464])]
rotation: [0.002423123922199011, -0.0012329419841989875, -0.002256074920296669, -0.004647746216505766, 0.0026397579349577427, -0.0018584569916129112, 0.00032724899938330054, -0.003442202927544713, -0.0015503499889746308, -0.001219906029291451, 0.00100624596234411, 0.0004586990107782185, 0.002247574971988797, 0.0017181190196424723, -0.001358055043965578, 0.0006174049922265112, 0.0002751440042629838, 0.0005500320112332702, -0.0009896219708025455, -0.0011104169534519315, -0.0028521500062197447, 0.0005775609752163291, 0.00010585499694570899, -0.001865869970060885, -0.0008212269749492407, 0.0026659369468688965, -0.002107413951307535, -0.0004820720059797168, -0.001106615993194282, -0.0017073999624699354]... [Tensor of shape torch.Size([78464, 1933])]
PCPACKYRS.linear.weight: [-0.14984282851219177, 0.3452908992767334, 1.1898494958877563, 0.2573627531528473, 0.14331825077533722, -0.6565908193588257, -0.2499941736459732, -0.5399421453475952, 0.2891318202018738, -0.8680660128593445, -0.12501518428325653, 0.1536022573709488, -0.6584336757659912, -2.950654983520508, 1.019046425819397, -3.8761327266693115, -0.8699817657470703, -4.393039703369141, -1.451832890510559, -0.655026912689209, 0.1668931096792221, -3.4419004917144775, 1.4055287837982178, -1.556980848312378, -2.518756151199341, 1.728127121925354, 1.1390047073364258, -0.04151243716478348, -0.6745365262031555, 2.9091978073120117]... [Tensor of shape torch.Size([1, 1234])]
PCPACKYRS.linear.bias: tensor([3.6898])
PCADM.linear.weight: [-1.580859899520874, 0.6895993947982788, 0.5884594321250916, 0.33028411865234375, 0.11279035359621048, -2.4742860794067383, 4.32066535949707, -1.7319400310516357, -0.6082714796066284, -0.18234848976135254, 1.7688186168670654, -0.4687173068523407, -3.820854902267456, 0.23701506853103638, -1.2404377460479736, 0.3446628451347351, -1.809430480003357, -0.35809019207954407, -1.8030858039855957, -0.9031863212585449, -0.24709266424179077, -1.57620370388031, 2.3358521461486816, 2.2262065410614014, -0.32931697368621826, -1.9858754873275757, 1.263405442237854, 2.155048131942749, 1.2939797639846802, 1.2711677551269531]... [Tensor of shape torch.Size([1, 331])]
PCADM.linear.bias: tensor([293.5512])
PCB2M.linear.weight: [-5527.6396484375, 618.7213134765625, 10538.052734375, 13935.08984375, -11641.298828125, -9476.55859375, -4081.583984375, -3303.738037109375, -2605.540283203125, -7426.14697265625, 26004.91015625, 5503.59716796875, 4332.962890625, -18251.943359375, -1677.3939208984375, -64.4184341430664, -4069.646728515625, -4367.86669921875, -11155.8095703125, 11732.5751953125, -8227.33203125, -8242.3095703125, -1011.329345703125, -3278.522216796875, 6034.12060546875, 15530.7216796875, 9036.619140625, -1259.7939453125, 5411.6298828125, -2013.9661865234375]... [Tensor of shape torch.Size([1, 286])]
PCB2M.linear.bias: tensor([2137461.7500])
PCCystatinC.linear.weight: [-758.8092651367188, 466.197021484375, 2975.08740234375, 4612.87939453125, 1127.079833984375, -943.728759765625, -1414.30126953125, -2423.542724609375, 4936.0390625, -1547.6116943359375, -582.6220703125, -1295.9365234375, -104.4135971069336, -2315.2763671875, 1610.7772216796875, -1364.6649169921875, -2691.782958984375, -2306.108154296875, -926.620849609375, 823.5294189453125, 293.9808349609375, -118.99710083007812, -247.8140869140625, -537.2789306640625, 1214.1744384765625, 1737.6654052734375, 252.37217712402344, -212.79415893554688, -33.61885070800781, 85.59772491455078]... [Tensor of shape torch.Size([1, 174])]
PCCystatinC.linear.bias: tensor([540501.8125])
PCGDF15.linear.weight: [-3.064344644546509, 6.427391052246094, 15.992425918579102, 2.824812173843384, -2.727965831756592, 1.533106803894043, -8.710307121276855, -3.053809642791748, -1.1963449716567993, 2.864203691482544, 2.7469873428344727, -13.630250930786133, 2.1277360916137695, -21.83341407775879, -26.710493087768555, -1.885144591331482, -3.6300323009490967, 2.7709856033325195, -5.206478118896484, -2.699326276779175, 0.024610411375761032, 0.23911644518375397, 10.243642807006836, 2.211014747619629, -3.9853546619415283, 0.5308716297149658, -1.5865904092788696, -0.5489709973335266, -1.4851919412612915, -3.113715410232544]... [Tensor of shape torch.Size([1, 96])]
PCGDF15.linear.bias: tensor([94.8125])
PCLeptin.linear.weight: [66.32584381103516, -29.714107513427734, -29.35553550720215, 6.036207675933838, -199.05287170410156, -188.17578125, -31.66545867919922, 53.26570129394531, -339.7561950683594, -185.63404846191406, 86.1479263305664, -73.50887298583984, -338.7237243652344, -57.23273468017578, 372.8580017089844, -84.76087951660156, 255.16909790039062, 34.87641906738281, 10.415830612182617, -65.53888702392578, 23.2608699798584, 225.397705078125, 73.31575012207031, -5.9991559982299805, 10.821805953979492, -3.767247438430786, -322.731689453125, 259.0422058105469, -270.3102722167969, 4.2313690185546875]... [Tensor of shape torch.Size([1, 192])]
PCLeptin.linear.bias: tensor([595.9894])
PCPAI1.linear.weight: [-103.13154602050781, -123.78325653076172, 115.10414123535156, 163.4703369140625, -205.56869506835938, 365.2059326171875, -141.0867919921875, -286.4826354980469, 608.5623168945312, -130.52430725097656, -770.8836059570312, 187.2977752685547, -367.0215759277344, 6.698156356811523, -153.1186981201172, -307.1435852050781, -281.5651550292969, -102.58252716064453, -2.1835734844207764, 245.5375518798828, 13.756028175354004, -65.49664306640625, 22.52094268798828, -207.51370239257812, 312.2713928222656, -12.289201736450195, 46.36296844482422, 26.23123550415039, 89.07231903076172, 133.31295776367188]... [Tensor of shape torch.Size([1, 631])]
PCPAI1.linear.bias: tensor([27206.1406])
PCTIMP1.linear.weight: [-46.880977630615234, 92.83746337890625, 40.29106903076172, -7.354409694671631, 38.623291015625, -68.65544128417969, -60.39823913574219, 196.2154998779297, -5.535160541534424, 5.4189066886901855, -127.39496612548828, -7.336097240447998, -38.622581481933594, -20.141542434692383, 6.978708267211914, -37.9903450012207, -30.112607955932617, -74.98777770996094, -12.825908660888672, 83.09314727783203, -27.617412567138672, -39.28822708129883, -51.4437255859375, 30.111513137817383, -0.7620040774345398, 71.8898696899414, 13.329928398132324, 47.78095626831055, -61.27421951293945, -33.29437255859375]... [Tensor of shape torch.Size([1, 102])]
PCTIMP1.linear.bias: tensor([26016.6270])
base_model.linear.weight: tensor([[ 2.6987e-01,  6.4222e-02,  8.8745e-06,  3.4165e-05,  7.7625e-03,
         -7.6811e-05,  4.1255e-04,  1.0161e-03,  1.3897e-01, -1.2596e+00]])
base_model.linear.bias: tensor([-63.8778])

%==================================== Model Details ====================================%

Basic test#

[18]:
torch.manual_seed(42)
input = torch.randn(10, len(model.features), dtype=float).float()
model.eval()
model.to(float)
pred = model(input)
pred
[18]:
tensor([[34.0448],
        [37.0862],
        [45.8467],
        [39.5590],
        [21.7459],
        [23.2970],
        [35.3788],
        [39.7534],
        [45.1109],
        [31.1977]], dtype=torch.float64, grad_fn=<AddmmBackward0>)

Save torch model#

[19]:
torch.save(model, f"../weights/{model.metadata['clock_name']}.pt")

Clear directory#

[20]:
# 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: PCGrimAgeReferenceCpGBetas.json
Deleted file: PCGrimAgeCpGs.json
Deleted file: CalcAllPCClocks.RData
Deleted file: download.r
Deleted file: CalcPCGrimAge.json