-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllama_hinn.py
More file actions
1068 lines (849 loc) · 35.9 KB
/
llama_hinn.py
File metadata and controls
1068 lines (849 loc) · 35.9 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""Llama_HINN.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1JglKev5XMvbqh2KlNVuWrQO0z_SXY5OW
"""
!pip install captum torchinfo
!git clone https://github.com/bozdaglab/HINN.git
# Commented out IPython magic to ensure Python compatibility.
# %cd HINN
import os
import torch
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from torch.utils.data import Dataset, DataLoader
from transformers import AutoTokenizer, AutoModelForCausalLM
import warnings
warnings.filterwarnings('ignore')
os.environ["KERAS_BACKEND"] = "torch"
import torch.nn as nn
import torch.nn.functional as F
from torchinfo import summary
import captum
from captum.attr import DeepLift
import plotly.express as px
import plotly.graph_objects as go
import plotly.colors as pc
def load_and_process_data():
def preprocess(file_path, suffix):
df = pd.read_csv(file_path)
df.index = df.iloc[:, 0]
df = df.drop(df.columns[0], axis=1)
df.columns = [f"{col}_{suffix}" for col in df.columns]
return df
expression = preprocess("gene_data.csv", "expression")
methy = preprocess("methyl_data.csv", "methy")
snp = preprocess("snp_data.csv", "snp")
demograph = pd.read_csv("demo_label_data.csv", usecols=range(7))
demograph.index = demograph.iloc[:, 0]
demograph = demograph.drop(demograph.columns[0], axis=1)
demograph.columns = [f"{col}_demograph" for col in demograph.columns]
label = pd.read_csv("demo_label_data.csv", usecols=[0, 8])
label.index = label.iloc[:, 0]
label = label.drop(label.columns[0], axis=1)
label.columns = [f"{col}_label" for col in label.columns]
data = snp.join(expression, how="inner") \
.join(methy, how="inner") \
.join(demograph, how="inner") \
.join(label, how="inner")
return data
class PrimaryInputLayer(nn.Module):
def __init__(self, units, output_dim, activation="sigmoid", mask=None):
super().__init__()
self.units = units
self.output_dim = output_dim
if activation == "sigmoid":
self.activation = nn.Sigmoid()
else:
raise ValueError(f"Unsupported activation: {activation}")
self.w = nn.Parameter(torch.empty(units, output_dim))
self.b = nn.Parameter(torch.zeros(output_dim))
nn.init.xavier_normal_(self.w)
if mask is None:
raise ValueError("mask tensor is required")
self.register_buffer("mask", mask.float())
def forward(self, x):
masked_w = self.w * self.mask
out = x @ masked_w + self.b
return self.activation(out)
class SecondaryInputLayer(nn.Module):
def __init__(self, units):
super().__init__()
self.units = units
self.register_buffer("mask", torch.eye(units))
self.w = nn.Parameter(torch.empty(units, units))
nn.init.xavier_normal_(self.w)
def forward(self, x):
masked_w = self.w * self.mask
return x @ masked_w
class MultiplicationInputLayer(nn.Module):
def __init__(self, units, activation="sigmoid"):
super().__init__()
self.units = units
if activation == "sigmoid":
self.activation = nn.Sigmoid()
else:
raise ValueError(f"Unsupported activation: {activation}")
self.b = nn.Parameter(torch.zeros(units))
nn.init.xavier_normal_(self.b.unsqueeze(0))
def forward(self, x):
return self.activation(x + self.b)
class CustomDataset(Dataset):
def __init__(self, inputs, targets):
self.inputs = inputs
self.targets = targets
def __len__(self):
return len(self.targets)
def __getitem__(self, idx):
return [input[idx] for input in self.inputs], self.targets[idx]
class EarlyStopping:
def __init__(self, patience=50, delta=0.0, restore_best_weights=True):
self.patience = patience
self.delta = delta
self.restore_best_weights = restore_best_weights
self.best_loss = float("inf")
self.counter = 0
self.best_model_state = None
def __call__(self, val_loss, model):
if isinstance(val_loss, torch.Tensor):
val_loss = val_loss.item()
if val_loss < self.best_loss - self.delta:
self.best_loss = val_loss
self.counter = 0
if self.restore_best_weights:
self.best_model_state = {k: v.cpu().clone() for k, v in model.state_dict().items()}
else:
self.counter += 1
if self.counter >= self.patience:
print(f"Early stopping triggered. Best val_loss = {self.best_loss:.4f}")
if self.restore_best_weights and self.best_model_state is not None:
model.load_state_dict(self.best_model_state)
return True
return False
def train_model_torch(model, train_loader, val_loader, device="cuda",
lr=1e-3, epochs=1000, patience=500):
criterion = torch.nn.L1Loss() # MAE
optimizer = torch.optim.Adam(model.parameters(), lr=lr)
early_stopper = EarlyStopping(patience=patience, delta=0.0, restore_best_weights=True)
model.to(device)
for epoch in range(epochs):
model.train()
train_loss = 0.0
for inputs, targets in train_loader:
inputs = [x.to(device).float() for x in inputs]
targets = targets.to(device).float()
if inputs[0].size(0) == 1:
model.eval()
with torch.no_grad():
outputs = model(*inputs).squeeze()
model.train()
else:
optimizer.zero_grad()
outputs = model(*inputs).squeeze()
loss = criterion(outputs, targets)
loss.backward()
optimizer.step()
train_loss += loss.item() * targets.size(0)
train_loss /= len(train_loader.dataset)
model.eval()
val_loss = 0.0
with torch.no_grad():
for inputs, targets in val_loader:
inputs = [x.to(device).float() for x in inputs]
targets = targets.to(device).float()
outputs = model(*inputs).squeeze()
loss = criterion(outputs, targets)
val_loss += loss.item() * targets.size(0)
val_loss /= len(val_loader.dataset)
if (epoch + 1) % 10 == 0:
print(f"Epoch {epoch+1:03d} | train_loss={train_loss:.4f} | val_loss={val_loss:.4f}")
if early_stopper(val_loss, model):
print(f"Stopping at epoch {epoch+1}")
break
return model
def evaluate_model_torch(model, test_loader, device="cuda"):
model.eval()
model.to(device)
all_targets = []
all_preds = []
with torch.no_grad():
for inputs, targets in test_loader:
inputs = [x.to(device).float() for x in inputs]
targets = targets.to(device).float().unsqueeze(1)
preds = model(*inputs)
all_targets.append(targets.cpu().numpy())
all_preds.append(preds.cpu().numpy())
y_true = np.concatenate(all_targets, axis=0).squeeze()
y_pred = np.concatenate(all_preds, axis=0).squeeze()
mse = np.mean((y_true - y_pred) ** 2)
mae = np.mean(np.abs(y_true - y_pred))
return {"mse": mse, "mae": mae, "y_true": y_true, "y_pred": y_pred}
def interpret_model(model, test_inputs, baselines, device="cuda"):
model.eval()
model.to(device)
test_inputs = tuple(t.to(device) for t in test_inputs)
baselines = tuple(b.to(device) for b in baselines)
explainer = DeepLift(model)
attributions = explainer.attribute(
test_inputs,
baselines=baselines,
return_convergence_delta=False,
)
return attributions
def export_attributions(attributions, feature_names, save_path_prefix):
for i, name in enumerate(['snp', 'methy', 'gene', 'demo']):
df = pd.DataFrame(attributions[i].detach().cpu().numpy(), columns=feature_names[i])
df.to_csv(f"{save_path_prefix}_{name}.csv", index=False)
def filter_matrices_by_top_features(snp_list, methy_list, gene_list,
sparse_methy, sparse_gene, sparse_pathway):
subset_methy_matrix = sparse_methy.loc[snp_list, methy_list]
subset_gene_matrix = sparse_gene.loc[methy_list, gene_list]
subset_pathway_matrix = sparse_pathway.loc[gene_list, :]
subset_methy_matrix = subset_methy_matrix.loc[subset_methy_matrix.any(axis=1) == 1, subset_methy_matrix.any(axis=0)]
subset_gene_matrix = subset_gene_matrix.loc[subset_gene_matrix.any(axis=1) == 1, subset_gene_matrix.any(axis=0)]
subset_pathway_matrix = subset_pathway_matrix.loc[subset_pathway_matrix.index.isin(subset_gene_matrix.columns)]
subset_pathway_matrix = subset_pathway_matrix.loc[subset_pathway_matrix.any(axis=1) == 1, subset_pathway_matrix.any(axis=0)]
return subset_methy_matrix, subset_gene_matrix, subset_pathway_matrix
def summarize_connections(*matrices):
connection_counts = [int(matrix.sum().sum()) for matrix in matrices]
labels = ["SNP-Methylation", "Methylation-Gene", "Gene-Pathway"]
for label, count in zip(labels, connection_counts):
print(f"Total connections ({label}): {count}")
def build_edge_list(subset_methy_matrix, subset_gene_matrix, subset_pathway_matrix):
edges_snp_methy = (
subset_methy_matrix[subset_methy_matrix == 1]
.stack()
.reset_index()
)
edges_snp_methy.columns = ["source", "target", "value"]
edges_snp_methy["layer"] = "snp_methy"
edges_methy_gene = (
subset_gene_matrix[subset_gene_matrix == 1]
.stack()
.reset_index()
)
edges_methy_gene.columns = ["source", "target", "value"]
edges_methy_gene["layer"] = "methy_gene"
edges_gene_go = (
subset_pathway_matrix[subset_pathway_matrix == 1]
.stack()
.reset_index()
)
edges_gene_go.columns = ["source", "target", "value"]
edges_gene_go["layer"] = "gene_go"
edges_all = pd.concat(
[edges_snp_methy, edges_methy_gene, edges_gene_go],
ignore_index=True,
)
edges_all["value"] = 1
return edges_all
def plot_sankey_from_edges(edges_all):
edges_all_filtered = edges_all.copy()
nodes = pd.unique(edges_all_filtered[["source", "target"]].values.ravel())
snps = [
node for node in nodes
if ((node.startswith("rs") or ":" in node) and not node.startswith("GO"))
]
methylation = [node for node in nodes if node.startswith("cg")]
genes = [node for node in nodes if "_at" in node]
go_terms = [node for node in nodes if node.startswith("GO:")]
ordered_nodes = snps + methylation + genes + go_terms
node_indices = {name: i for i, name in enumerate(ordered_nodes)}
edges_all_filtered = edges_all_filtered[
edges_all_filtered["source"].isin(ordered_nodes)
& edges_all_filtered["target"].isin(ordered_nodes)
].copy()
edges_all_filtered["source_index"] = edges_all_filtered["source"].map(node_indices)
edges_all_filtered["target_index"] = edges_all_filtered["target"].map(node_indices)
node_positions_x = [
0.0 if node in snps
else 0.33 if node in methylation
else 0.66 if node in genes
else 0.99
for node in ordered_nodes
]
unique_colors = pc.qualitative.Dark24
repeated_colors = (unique_colors * ((len(ordered_nodes) // len(unique_colors)) + 1))[:len(ordered_nodes)]
node_colors = repeated_colors
fig = go.Figure(go.Sankey(
arrangement="snap",
node=dict(
pad=10,
thickness=20,
line=dict(color="black", width=0.5),
label=ordered_nodes,
color=node_colors,
x=node_positions_x,
),
link=dict(
source=edges_all_filtered["source_index"],
target=edges_all_filtered["target_index"],
value=edges_all_filtered["value"],
),
))
fig.update_layout(
font_size=14,
height=1500,
width=2000,
)
fig.show()
class HINN(nn.Module):
def __init__(
self,
snp_dim,
methy_dim,
exp_dim,
demo_dim,
sparse_methy_tensor,
sparse_gene_tensor,
sparse_pathway_tensor,
dense_nodes_1=128,
drop_rate=0.7,
activation_function="sigmoid",
):
super().__init__()
# First block: SNP -> Methy
self.primary1 = PrimaryInputLayer(
units=snp_dim,
output_dim=methy_dim,
activation=activation_function,
mask=sparse_methy_tensor,
)
self.secondary1 = SecondaryInputLayer(units=methy_dim)
self.mult1 = MultiplicationInputLayer(
units=methy_dim,
activation=activation_function,
)
self.snp_fc = nn.Linear(snp_dim, 20)
# Second block: Methy -> Gene
self.primary2 = PrimaryInputLayer(
units=methy_dim,
output_dim=exp_dim,
activation=activation_function,
mask=sparse_gene_tensor,
)
self.secondary2 = SecondaryInputLayer(units=exp_dim)
self.mult2 = MultiplicationInputLayer(
units=exp_dim,
activation=activation_function,
)
self.mid_fc = nn.Linear(methy_dim + 20, 20)
# Third block: Gene -> Pathway
pathway_dim = sparse_pathway_tensor.shape[1]
self.primary3 = PrimaryInputLayer(
units=exp_dim,
output_dim=pathway_dim,
activation=activation_function,
mask=sparse_pathway_tensor,
)
self.mid_fc2 = nn.Linear(exp_dim + 20, 20)
# Dense layers
custom_input_dim = pathway_dim + 20
self.bn1 = nn.BatchNorm1d(custom_input_dim)
self.fc1 = nn.Linear(custom_input_dim, dense_nodes_1)
self.drop1 = nn.Dropout(drop_rate)
self.bn2 = nn.BatchNorm1d(dense_nodes_1)
self.fc2 = nn.Linear(dense_nodes_1, dense_nodes_1)
self.drop2 = nn.Dropout(drop_rate)
self.bn3 = nn.BatchNorm1d(dense_nodes_1)
self.fc3 = nn.Linear(dense_nodes_1, dense_nodes_1)
self.drop3 = nn.Dropout(drop_rate)
self.bn4 = nn.BatchNorm1d(dense_nodes_1)
self.fc4 = nn.Linear(dense_nodes_1, dense_nodes_1)
self.drop4 = nn.Dropout(drop_rate)
self.dense_fourth = nn.Linear(dense_nodes_1, 20)
self.bn_demo = nn.BatchNorm1d(20 + demo_dim)
self.fc_demo = nn.Linear(20 + demo_dim, dense_nodes_1)
self.drop_demo = nn.Dropout(drop_rate)
self.out = nn.Linear(dense_nodes_1, 1)
self.activation_function = activation_function
def _nonlin(self, x):
return torch.sigmoid(x)
def forward(self, snp, methy, exp, demo):
# First block
primary1 = self.primary1(snp)
secondary1 = self.secondary1(methy)
mult_res1 = primary1 * secondary1
mult1 = self.mult1(mult_res1)
snp_fc = self._nonlin(self.snp_fc(snp))
out2 = torch.cat([mult1, snp_fc], dim=1)
# Second block
primary2 = self.primary2(mult1)
secondary2 = self.secondary2(exp)
eps = 1e-6
denom = primary2.clone()
denom = torch.where(denom.abs() < eps, eps * torch.ones_like(denom), denom)
div_res1 = secondary2 / denom
div_res1 = torch.clamp(div_res1, -1e6, 1e6)
mult2 = self.mult2(div_res1)
mid_fc = self._nonlin(self.mid_fc(out2))
out3 = torch.cat([mult2, mid_fc], dim=1)
# Third block
primary3 = self.primary3(mult2)
mid_fc2 = self._nonlin(self.mid_fc2(out3))
out4 = torch.cat([primary3, mid_fc2], dim=1)
# Dense stack
x = self.bn1(out4)
x = self._nonlin(self.fc1(x))
x = self.drop1(x)
x = self.bn2(x)
x = self._nonlin(self.fc2(x))
x = self.drop2(x)
x = self.bn3(x)
x = self._nonlin(self.fc3(x))
x = self.drop3(x)
x = self.bn4(x)
x = self._nonlin(self.fc4(x))
x = self.drop4(x)
dense_fourth = self._nonlin(self.dense_fourth(x))
demo_concat = torch.cat([dense_fourth, demo], dim=1)
x = self.bn_demo(demo_concat)
x = self._nonlin(self.fc_demo(x))
x = self.drop_demo(x)
out = self.out(x)
return out
class MultiOmicReportGenerator:
def __init__(self, model_name="NousResearch/Llama-2-7b-chat-hf", device="cuda",
offload_to_cpu=True, use_8bit=False):
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
if use_8bit:
try:
from transformers import BitsAndBytesConfig
quantization_config = BitsAndBytesConfig(load_in_8bit=True)
self.llm = AutoModelForCausalLM.from_pretrained(
model_name,
quantization_config=quantization_config,
device_map="auto",
low_cpu_mem_usage=True,
)
self.device = device
except ImportError:
offload_to_cpu = True
use_8bit = False
if not use_8bit:
if offload_to_cpu and device == "cuda":
self.llm = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.float16,
device_map="auto", # Automatically distribute across GPU/CPU
low_cpu_mem_usage=True,
offload_folder="offload", # Offload to disk if needed
offload_state_dict=True,
)
self.device = device
else:
self.llm = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
device_map="auto" if device == "cuda" else None,
low_cpu_mem_usage=True,
)
if device == "cpu":
self.llm = self.llm.to(device)
self.device = device
def extract_patient_features(self, attributions, feature_names, top_k=10):
attr_snp, attr_methy, attr_gene, attr_demo = attributions
snp_scores = attr_snp.abs().squeeze().detach().cpu().numpy()
methy_scores = attr_methy.abs().squeeze().detach().cpu().numpy()
gene_scores = attr_gene.abs().squeeze().detach().cpu().numpy()
demo_scores = attr_demo.abs().squeeze().detach().cpu().numpy()
top_snp_idx = np.argsort(-snp_scores)[:top_k]
top_methy_idx = np.argsort(-methy_scores)[:top_k]
top_gene_idx = np.argsort(-gene_scores)[:top_k]
top_features = {
'snp': [
{
'name': feature_names[0][i].replace('_snp', ''),
'score': float(snp_scores[i])
}
for i in top_snp_idx
],
'methylation': [
{
'name': feature_names[1][i].replace('_methy', ''),
'score': float(methy_scores[i])
}
for i in top_methy_idx
],
'gene': [
{
'name': feature_names[2][i].replace('_expression', ''),
'score': float(gene_scores[i])
}
for i in top_gene_idx
],
'demographics': [
{
'name': feature_names[3][i].replace('_demograph', ''),
'score': float(demo_scores[i])
}
for i in range(min(len(demo_scores), 5))
]
}
return top_features
def get_pathways_for_features(self, top_features, sparse_methy, sparse_gene, sparse_pathway):
snp_list = [f['name'] for f in top_features['snp']]
methy_list = [f['name'] for f in top_features['methylation']]
gene_list = [f['name'] for f in top_features['gene']]
try:
# SNP -> Methylation connections
subset_methy = sparse_methy.loc[
sparse_methy.index.intersection(snp_list),
sparse_methy.columns.intersection(methy_list)
]
# Methylation -> Gene connections
subset_gene = sparse_gene.loc[
sparse_gene.index.intersection(methy_list),
sparse_gene.columns.intersection(gene_list)
]
# Gene -> Pathway connections
subset_pathway = sparse_pathway.loc[
sparse_pathway.index.intersection(gene_list), :
]
subset_pathway = subset_pathway.loc[:, subset_pathway.any(axis=0)]
connected_pathways = subset_pathway.columns[subset_pathway.sum(axis=0) > 0].tolist()
pathway_info = {
'pathways': connected_pathways[:15],
'connection_counts': {
'snp_to_methylation': int(subset_methy.sum().sum()),
'methylation_to_gene': int(subset_gene.sum().sum()),
'gene_to_pathway': int(subset_pathway.sum().sum())
}
}
except Exception as e:
print(f"Warning: Error extracting pathways: {e}")
pathway_info = {
'pathways': [],
'connection_counts': {
'snp_to_methylation': 0,
'methylation_to_gene': 0,
'gene_to_pathway': 0
}
}
return pathway_info
def create_clinical_prompt(self, patient_id, predicted_score, true_score,
top_features, pathway_info):
prompt = f"""[INST] You are a clinical genetics and neuroscience expert analyzing multi-omic data for cognitive decline prediction.
**PATIENT CASE SUMMARY**
Patient ID: {patient_id}
Predicted MMSE Score: {predicted_score:.2f}
Actual MMSE Score: {true_score:.2f}
Prediction Error: {abs(predicted_score - true_score):.2f} points
**TOP GENETIC VARIANTS (SNPs)**
The following SNPs showed the highest importance in the prediction:
{self._format_features(top_features['snp'][:5])}
**TOP METHYLATION SITES**
The following CpG methylation sites were most influential:
{self._format_features(top_features['methylation'][:5])}
**TOP GENE EXPRESSIONS**
The following genes showed the strongest predictive signal:
{self._format_features(top_features['gene'][:5])}
**BIOLOGICAL PATHWAYS**
These features connect to {len(pathway_info['pathways'])} biological pathways including:
{', '.join(pathway_info['pathways'][:10])}
Network connectivity: {pathway_info['connection_counts']['snp_to_methylation']} SNP-Methylation links,
{pathway_info['connection_counts']['methylation_to_gene']} Methylation-Gene links,
{pathway_info['connection_counts']['gene_to_pathway']} Gene-Pathway links.
**TASK**
Please provide a comprehensive clinical interpretation report with the following sections:
1. **Clinical Interpretation**: Explain what the MMSE score indicates about the patient's cognitive status.
2. **Genetic Risk Factors**: Describe the biological significance of the top SNPs and their known associations with cognitive decline, Alzheimer's disease, or related conditions.
3. **Epigenetic Findings**: Explain the role of the key methylation sites and what changes in methylation at these loci might indicate.
4. **Gene Expression Patterns**: Interpret the expression changes in the top genes and their relevance to neurodegeneration.
5. **Pathway Analysis**: Provide a biological narrative connecting the genetic variants through methylation and gene expression to the identified pathways. What cellular processes are most affected?
6. **Clinical Recommendations**: Suggest potential follow-up investigations or considerations for clinicians based on these findings.
Keep the report professional, scientifically accurate, and accessible to clinicians. Focus on biological mechanisms and clinical relevance. [/INST]"""
return prompt
def _format_features(self, features):
if not features:
return "None identified"
return '\n'.join([f" - {f['name']} (importance: {f['score']:.4f})"
for f in features])
def generate_report(self, prompt, max_length=2048, temperature=0.7):
inputs = self.tokenizer(prompt, return_tensors="pt", truncation=True, max_length=4096)
inputs = {k: v.to(self.device) for k, v in inputs.items()}
with torch.no_grad():
outputs = self.llm.generate(
**inputs,
max_new_tokens=max_length,
temperature=temperature,
do_sample=True,
top_p=0.9,
num_return_sequences=1,
pad_token_id=self.tokenizer.eos_token_id,
)
full_text = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
if "[/INST]" in full_text:
report = full_text.split("[/INST]")[-1].strip()
else:
report = full_text
return report
def create_patient_report(self, patient_id, patient_data, predicted_score,
true_score, model, feature_names, sparse_matrices,
device="cuda"):
print(f"Generating Report for Patient {patient_id}")
patient_data = tuple(
x.unsqueeze(0) if x.dim() == 1 else x
for x in patient_data
)
model.eval()
patient_data = tuple(x.to(device).requires_grad_(True) for x in patient_data)
baselines = tuple(torch.zeros_like(x) for x in patient_data)
explainer = DeepLift(model)
attributions = explainer.attribute(
patient_data,
baselines=baselines,
return_convergence_delta=False
)
top_features = self.extract_patient_features(attributions, feature_names, top_k=10)
sparse_methy, sparse_gene, sparse_pathway = sparse_matrices
pathway_info = self.get_pathways_for_features(
top_features, sparse_methy, sparse_gene, sparse_pathway
)
prompt = self.create_clinical_prompt(
patient_id, predicted_score, true_score, top_features, pathway_info
)
report = self.generate_report(prompt, max_length=2048, temperature=0.7)
return {
'patient_id': patient_id,
'predicted_score': predicted_score,
'true_score': true_score,
'top_features': top_features,
'pathway_info': pathway_info,
'report': report,
'prompt': prompt
}
def generate_patient_reports(model, test_loader, test_df, y_test, feature_names,
sparse_matrices, device="cuda", num_reports=3,
move_hinn_to_cpu=True, use_8bit_llm=False):
model.eval()
all_predictions = []
with torch.no_grad():
for inputs, _ in test_loader:
inputs = [x.to(device).float() for x in inputs]
preds = model(*inputs).squeeze()
all_predictions.append(preds.cpu().numpy())
all_predictions = np.concatenate(all_predictions)
errors = np.abs(all_predictions - y_test.values)
indices_to_report = [
np.argmin(errors),
np.argmax(errors),
np.argsort(errors)[len(errors)//2],
]
indices_to_report = indices_to_report[:num_reports]
if move_hinn_to_cpu and device == "cuda":
model = model.cpu() #move the hinn model to the cpu
torch.cuda.empty_cache()
hinn_device = "cpu"
else:
hinn_device = device
report_gen = MultiOmicReportGenerator(
model_name="NousResearch/Llama-2-7b-chat-hf",
device=device,
offload_to_cpu=True if device == "cuda" else False,
use_8bit=use_8bit_llm
)
reports = []
for idx in indices_to_report:
patient_id = test_df.index[idx]
snp_data = torch.tensor(
test_df.filter(like="_snp").iloc[idx].values,
dtype=torch.float32
)
methy_data = torch.tensor(
test_df.filter(like="_methy").iloc[idx].values,
dtype=torch.float32
)
exp_data = torch.tensor(
test_df.filter(like="_expression").iloc[idx].values,
dtype=torch.float32
)
demo_data = torch.tensor(
test_df.filter(like="_demograph").iloc[idx].values,
dtype=torch.float32
)
patient_data = (snp_data, methy_data, exp_data, demo_data)
report_dict = report_gen.create_patient_report(
patient_id=patient_id,
patient_data=patient_data,
predicted_score=all_predictions[idx],
true_score=y_test.values[idx],
model=model,
feature_names=feature_names,
sparse_matrices=sparse_matrices,
device=hinn_device
)
reports.append(report_dict)
print(f"CLINICAL REPORT - Patient {patient_id}")
print(report_dict['report'])
with open(f"patient_report_{patient_id}.txt", "w") as f:
f.write(f"CLINICAL REPORT - Patient {patient_id}\n")
f.write(report_dict['report'])
print(f"Report saved to: patient_report_{patient_id}.txt")
#if move_hinn_to_cpu and device == "cuda":
# model = model.to(device)
return reports
def main():
device = "cuda" if torch.cuda.is_available() else "cpu"
data = load_and_process_data()
y = data["MMSE_label"]
X = data.drop(columns=[c for c in data.columns if c.endswith("MMSE_label")])
X_train_int, X_test_df, y_train_int, y_test = train_test_split(
X, y, test_size=0.3, random_state=42
)
X_train_df, X_val_df, y_train, y_val = train_test_split(
X_train_int, y_train_int, test_size=0.2, random_state=42
)
print(f"Train samples: {len(X_train_df)}")
print(f"Val samples: {len(X_val_df)}")
print(f"Test samples: {len(X_test_df)}")
X_train_list = [
torch.tensor(X_train_df.filter(like=s).values, dtype=torch.float32)
for s in ["_snp", "_methy", "_expression", "_demograph"]
]
y_train_t = torch.tensor(y_train.values, dtype=torch.float32)
X_val_list = [
torch.tensor(X_val_df.filter(like=s).values, dtype=torch.float32)
for s in ["_snp", "_methy", "_expression", "_demograph"]
]
y_val_t = torch.tensor(y_val.values, dtype=torch.float32)
X_test_list = [
torch.tensor(X_test_df.filter(like=s).values, dtype=torch.float32)
for s in ["_snp", "_methy", "_expression", "_demograph"]
]
y_test_t = torch.tensor(y_test.values, dtype=torch.float32)
train_dataset = CustomDataset(X_train_list, y_train_t)
val_dataset = CustomDataset(X_val_list, y_val_t)
test_dataset = CustomDataset(X_test_list, y_test_t)
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
val_loader = DataLoader(val_dataset, batch_size=32, shuffle=False)
test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False)
sparse_methy = pd.read_csv("snp_methyl_matrix.csv", index_col=0)
sparse_gene = pd.read_csv("methyl_gene_matrix.csv.zip", compression='zip', index_col=0)
sparse_pathway = pd.read_csv("gene_pathway_matrix.csv", index_col=0)
print(f"SNP-Methylation matrix: {sparse_methy.shape}")
print(f"Methylation-Gene matrix: {sparse_gene.shape}")
print(f"Gene-Pathway matrix: {sparse_pathway.shape}")
sparse_methy_tensor = torch.tensor(sparse_methy.values, dtype=torch.float32)
sparse_gene_tensor = torch.tensor(sparse_gene.values, dtype=torch.float32)
sparse_pathway_tensor = torch.tensor(sparse_pathway.values, dtype=torch.float32)
snp_dim = X_train_list[0].shape[1]
methy_dim = X_train_list[1].shape[1]
exp_dim = X_train_list[2].shape[1]
demo_dim = X_train_list[3].shape[1]
print(f"Input dimensions:")
print(f" SNP: {snp_dim}")
print(f" Methylation: {methy_dim}")
print(f" Gene Expression: {exp_dim}")
print(f" Demographics: {demo_dim}")
model = HINN(
snp_dim=snp_dim,
methy_dim=methy_dim,
exp_dim=exp_dim,
demo_dim=demo_dim,
sparse_methy_tensor=sparse_methy_tensor,
sparse_gene_tensor=sparse_gene_tensor,
sparse_pathway_tensor=sparse_pathway_tensor,
dense_nodes_1=128,
drop_rate=0.7,
activation_function="sigmoid",
)
total_params = sum(p.numel() for p in model.parameters())
trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
model = train_model_torch(
model,
train_loader,
val_loader,
device=device,
lr=1e-3,
epochs=1000,
patience=50,
)
eval_results = evaluate_model_torch(model, test_loader, device=device)
print(f"Test MAE: {eval_results['mae']:.4f}")
print(f"Test MSE: {eval_results['mse']:.4f}")
print(f"Test RMSE: {np.sqrt(eval_results['mse']):.4f}")
test_inputs = tuple(
torch.tensor(arr, dtype=torch.float32, requires_grad=True).to(device)
for arr in [
X_test_df.filter(like="_snp").values,
X_test_df.filter(like="_methy").values,
X_test_df.filter(like="_expression").values,
X_test_df.filter(like="_demograph").values
]
)
baselines = tuple(
torch.tensor(arr.mean(axis=0), dtype=torch.float32)
.unsqueeze(0)
.expand_as(torch.tensor(arr, dtype=torch.float32))
.to(device)
for arr in [
X_test_df.filter(like="_snp").values,
X_test_df.filter(like="_methy").values,
X_test_df.filter(like="_expression").values,
X_test_df.filter(like="_demograph").values
]
)
attributions = interpret_model(model, test_inputs, baselines, device=device)
attr_snp, attr_methy, attr_gene, attr_demo = attributions
snp_importance = attr_snp.abs().mean(dim=0).detach().cpu().numpy()
methy_importance = attr_methy.abs().mean(dim=0).detach().cpu().numpy()
gene_importance = attr_gene.abs().mean(dim=0).detach().cpu().numpy()
print(f"Computed attributions for {len(snp_importance)} SNPs, "
f"{len(methy_importance)} methylation sites, "
f"{len(gene_importance)} genes")
feature_names = [