pyaging 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of pyaging might be problematic. Click here for more details.
- pyaging/__init__.py +10 -0
- pyaging/data/__init__.py +3 -0
- pyaging/data/_data.py +75 -0
- pyaging/logger/__init__.py +3 -0
- pyaging/logger/_logger.py +477 -0
- pyaging/models/__init__.py +3 -0
- pyaging/models/_models.py +154 -0
- pyaging/predict/__init__.py +5 -0
- pyaging/predict/_postprocessing.py +37 -0
- pyaging/predict/_pred.py +909 -0
- pyaging/predict/_preprocessing.py +47 -0
- pyaging/preprocess/__init__.py +3 -0
- pyaging/preprocess/_preprocess.py +578 -0
- pyaging/utils/__init__.py +3 -0
- pyaging/utils/_utils.py +438 -0
- pyaging-0.0.1.dist-info/LICENSE +28 -0
- pyaging-0.0.1.dist-info/METADATA +72 -0
- pyaging-0.0.1.dist-info/RECORD +20 -0
- pyaging-0.0.1.dist-info/WHEEL +4 -0
- pyaging-0.0.1.dist-info/entry_points.txt +3 -0
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
import torch.nn as nn
|
|
3
|
+
import torch.nn.functional as F
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class LinearModel(nn.Module):
|
|
7
|
+
def __init__(self, input_dim):
|
|
8
|
+
"""
|
|
9
|
+
Initialize a simple linear model.
|
|
10
|
+
"""
|
|
11
|
+
super(LinearModel, self).__init__()
|
|
12
|
+
self.linear = nn.Linear(input_dim, 1) # Define a linear layer
|
|
13
|
+
|
|
14
|
+
def forward(self, x):
|
|
15
|
+
x = self.linear(x)
|
|
16
|
+
return x
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class AltumAge(nn.Module):
|
|
20
|
+
def __init__(self):
|
|
21
|
+
"""
|
|
22
|
+
Initialize the AltumAge model with multiple linear layers and batch normalization.
|
|
23
|
+
"""
|
|
24
|
+
super(AltumAge, self).__init__()
|
|
25
|
+
|
|
26
|
+
# Define the linear layers
|
|
27
|
+
self.linear1 = nn.Linear(20318, 32)
|
|
28
|
+
self.linear2 = nn.Linear(32, 32)
|
|
29
|
+
self.linear3 = nn.Linear(32, 32)
|
|
30
|
+
self.linear4 = nn.Linear(32, 32)
|
|
31
|
+
self.linear5 = nn.Linear(32, 32)
|
|
32
|
+
self.linear6 = nn.Linear(32, 1)
|
|
33
|
+
|
|
34
|
+
# Define batch normalization layers
|
|
35
|
+
self.bn1 = nn.BatchNorm1d(20318, eps=0.001, momentum=0.99)
|
|
36
|
+
self.bn2 = nn.BatchNorm1d(32, eps=0.001, momentum=0.99)
|
|
37
|
+
self.bn3 = nn.BatchNorm1d(32, eps=0.001, momentum=0.99)
|
|
38
|
+
self.bn4 = nn.BatchNorm1d(32, eps=0.001, momentum=0.99)
|
|
39
|
+
self.bn5 = nn.BatchNorm1d(32, eps=0.001, momentum=0.99)
|
|
40
|
+
self.bn6 = nn.BatchNorm1d(32, eps=0.001, momentum=0.99)
|
|
41
|
+
|
|
42
|
+
def forward(self, x):
|
|
43
|
+
x = self.bn1(x)
|
|
44
|
+
x = self.linear1(x)
|
|
45
|
+
x = F.selu(x)
|
|
46
|
+
|
|
47
|
+
x = self.bn2(x)
|
|
48
|
+
x = self.linear2(x)
|
|
49
|
+
x = F.selu(x)
|
|
50
|
+
|
|
51
|
+
x = self.bn3(x)
|
|
52
|
+
x = self.linear3(x)
|
|
53
|
+
x = F.selu(x)
|
|
54
|
+
|
|
55
|
+
x = self.bn4(x)
|
|
56
|
+
x = self.linear4(x)
|
|
57
|
+
x = F.selu(x)
|
|
58
|
+
|
|
59
|
+
x = self.bn5(x)
|
|
60
|
+
x = self.linear5(x)
|
|
61
|
+
x = F.selu(x)
|
|
62
|
+
|
|
63
|
+
x = self.bn6(x)
|
|
64
|
+
x = self.linear6(x)
|
|
65
|
+
|
|
66
|
+
return x
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class PCARDModel(nn.Module):
|
|
70
|
+
def __init__(self, input_dim, pc_dim):
|
|
71
|
+
"""
|
|
72
|
+
Initialize a PCA ARD (PCARD) model.
|
|
73
|
+
"""
|
|
74
|
+
super(PCARDModel, self).__init__()
|
|
75
|
+
self.rotation = nn.Parameter(
|
|
76
|
+
torch.empty((input_dim, pc_dim)), requires_grad=False
|
|
77
|
+
)
|
|
78
|
+
self.linear = nn.Linear(pc_dim, 1)
|
|
79
|
+
|
|
80
|
+
def forward(self, x):
|
|
81
|
+
x = torch.mm(x, self.rotation) # Apply PCA rotation
|
|
82
|
+
x = self.linear(x)
|
|
83
|
+
return x
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class PCLinearModel(nn.Module):
|
|
87
|
+
def __init__(self, input_dim, pc_dim):
|
|
88
|
+
"""
|
|
89
|
+
Initialize a Principal Component Linear Model.
|
|
90
|
+
"""
|
|
91
|
+
super(PCLinearModel, self).__init__()
|
|
92
|
+
self.center = nn.Parameter(torch.empty(input_dim), requires_grad=False)
|
|
93
|
+
self.rotation = nn.Parameter(
|
|
94
|
+
torch.empty((input_dim, pc_dim)), requires_grad=False
|
|
95
|
+
)
|
|
96
|
+
self.linear = nn.Linear(pc_dim, 1)
|
|
97
|
+
|
|
98
|
+
def forward(self, x):
|
|
99
|
+
x = x - self.center # Apply centering
|
|
100
|
+
x = torch.mm(x, self.rotation) # Apply PCA rotation
|
|
101
|
+
x = self.linear(x)
|
|
102
|
+
return x
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
class PCGrimAge(nn.Module):
|
|
106
|
+
def __init__(self, input_dim, pc_dim, comp_dims=[]):
|
|
107
|
+
"""
|
|
108
|
+
Initialize a PC Grim Age model.
|
|
109
|
+
"""
|
|
110
|
+
super(PCGrimAge, self).__init__()
|
|
111
|
+
|
|
112
|
+
self.center = nn.Parameter(torch.empty(input_dim), requires_grad=False)
|
|
113
|
+
self.rotation = nn.Parameter(
|
|
114
|
+
torch.empty((input_dim, pc_dim)), requires_grad=False
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
# Define linear layers for each component dimension
|
|
118
|
+
step1_layers = [nn.Linear(comp_dim, 1) for comp_dim in comp_dims]
|
|
119
|
+
self.step1_layers = nn.ModuleList(step1_layers)
|
|
120
|
+
|
|
121
|
+
# Create and store parameters for features
|
|
122
|
+
self.step1_features = []
|
|
123
|
+
for idx, comp_dim in enumerate(comp_dims):
|
|
124
|
+
feature_param = nn.Parameter(torch.empty(comp_dim), requires_grad=False)
|
|
125
|
+
self.step1_features.append(feature_param)
|
|
126
|
+
setattr(self, f"step1_feature{idx}", feature_param)
|
|
127
|
+
|
|
128
|
+
self.step2 = nn.Linear(len(comp_dims) + 2, 1)
|
|
129
|
+
|
|
130
|
+
def forward(self, x):
|
|
131
|
+
# Extract and separate age and gender features
|
|
132
|
+
age = x[:, -1]
|
|
133
|
+
female = x[:, -2]
|
|
134
|
+
x = x[:, :-2]
|
|
135
|
+
|
|
136
|
+
# Apply centering and PCA rotation
|
|
137
|
+
x = x - self.center
|
|
138
|
+
x = torch.mm(x, self.rotation)
|
|
139
|
+
|
|
140
|
+
# Concatenate transformed features, gender, and age
|
|
141
|
+
x = torch.cat((x, female.unsqueeze(1), age.unsqueeze(1)), dim=1)
|
|
142
|
+
|
|
143
|
+
# Process through linear layers
|
|
144
|
+
xs = [
|
|
145
|
+
layer(x[:, features.long()])[:, 0]
|
|
146
|
+
for features, layer in zip(self.step1_features, self.step1_layers)
|
|
147
|
+
]
|
|
148
|
+
x = torch.stack(xs, dim=1)
|
|
149
|
+
|
|
150
|
+
# Add gender and age again for final linear layer
|
|
151
|
+
x = torch.cat((x, age.unsqueeze(1), female.unsqueeze(1)), dim=1)
|
|
152
|
+
x = self.step2(x)
|
|
153
|
+
|
|
154
|
+
return x
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import math
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def anti_log_linear(x, adult_age=20):
|
|
5
|
+
"""
|
|
6
|
+
Applies an anti-logarithmic linear transformation to a value.
|
|
7
|
+
"""
|
|
8
|
+
if x < 0:
|
|
9
|
+
# Apply exponential transformation for negative values
|
|
10
|
+
return (1 + adult_age) * math.exp(x) - 1
|
|
11
|
+
else:
|
|
12
|
+
# Apply linear transformation for non-negative values
|
|
13
|
+
return (1 + adult_age) * x + adult_age
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def anti_logp2(x):
|
|
17
|
+
"""
|
|
18
|
+
Applies an anti-logarithmic transformation with an offset of -2.
|
|
19
|
+
"""
|
|
20
|
+
# Exponential transformation with an offset
|
|
21
|
+
return math.exp(x) - 2
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def anti_log(x):
|
|
25
|
+
"""
|
|
26
|
+
Applies a simple anti-logarithmic transformation.
|
|
27
|
+
"""
|
|
28
|
+
# Simple exponential transformation
|
|
29
|
+
return math.exp(x)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def anti_log_log(x):
|
|
33
|
+
"""
|
|
34
|
+
Applies a double transformation: logarithmic followed by anti-logarithmic.
|
|
35
|
+
"""
|
|
36
|
+
# Double transformation: logarithmic followed by anti-logarithmic
|
|
37
|
+
return math.exp(-math.exp(-x))
|