|
8 | 8 |
|
9 | 9 | from gifflar.data.modules import LGI_GDM |
10 | 10 | from gifflar.data.datasets import GlycanOnDiskDataset |
11 | | -from experiments.lgi_model import LectinStorage |
| 11 | +from experiments.protein_encoding import ENCODER_MAP |
| 12 | +from gifflar.model.utils import LectinStorage |
12 | 13 |
|
13 | | -le = LectinStorage("ESM", 33) |
| 14 | + |
| 15 | +le = LectinStorage(ENCODER_MAP["ESM"](33), "ESM", 33) |
14 | 16 |
|
15 | 17 | class LGI_OnDiskDataset(GlycanOnDiskDataset): |
16 | 18 | @property |
@@ -39,69 +41,82 @@ def get_ds(dl, split_idx: int): |
39 | 41 |
|
40 | 42 |
|
41 | 43 | def collate_lgi(data): |
42 | | - for d in data: |
43 | | - d["train_idx"] = le.query(d["aa_seq"]) |
44 | | - |
| 44 | + print(len(data)) |
45 | 45 | offset = 0 |
46 | 46 | labels, edges, y, train_idx, batch = [], [], [], [], [] |
47 | | - for i, d in enumerate(data): |
48 | | - labels.append(d["labels"]) |
| 47 | + for i, x in enumerate(data): |
| 48 | + labels.append(x["sweetnet_x"]) |
| 49 | + edge_index = x["sweetnet_edge_index"] |
49 | 50 | edges.append(torch.stack([ |
50 | | - d["edge_index"][0] + offset, |
51 | | - d["edge_index"][1] + offset, |
| 51 | + edge_index[0] + offset, |
| 52 | + edge_index[1] + offset, |
52 | 53 | ])) |
53 | | - offset += len(d["labels"]) |
54 | | - y.append(d["y"]) |
55 | | - train_idx.append(le.query(d["aa_seq"])) |
56 | | - batch += [i for _ in range(len(d["labels"]))] |
| 54 | + offset += len(x["sweetnet_x"]) |
| 55 | + y.append(x["y"]) |
| 56 | + train_idx.append(a := le.query(x["aa_seq"])) |
| 57 | + if a is None: |
| 58 | + print(x["aa_seq"]) |
| 59 | + batch += [i for _ in range(len(x["labels"]))] |
57 | 60 |
|
58 | 61 | labels = torch.cat(labels, dim=0) |
59 | 62 | edges = torch.cat(edges, dim=1) |
60 | 63 | y = torch.stack(y) |
61 | 64 | train_idx = torch.stack(train_idx) |
62 | 65 | batch = torch.tensor(batch) |
63 | | - |
| 66 | + |
64 | 67 | return Batch( |
65 | | - labels=labels, |
66 | | - edge_index=edges, |
67 | | - y=y, |
68 | | - train_idx=train_idx, |
69 | | - batch=batch, |
| 68 | + labels=labels.to("cuda"), |
| 69 | + edge_index=edges.to("cuda"), |
| 70 | + y=y.to("cuda"), |
| 71 | + train_idx=train_idx.to("cuda"), |
| 72 | + batch=batch.to("cuda"), |
70 | 73 | ) |
71 | 74 |
|
72 | | -datamodule = LGI_GDM( |
73 | | - root="/scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/lgi_data", filename="/home/rjo21/Desktop/GIFFLAR/lgi_data_full.pkl", hash_code="8b34af2a", |
74 | | - batch_size=1, transform=None, pre_transform={"GIFFLARTransform": "", "SweetNetTransform": ""}, |
75 | | -) |
76 | | - |
77 | | -get_ds(datamodule.train_dataloader(), 0) |
78 | | -get_ds(datamodule.val_dataloader(), 1) |
79 | | -get_ds(datamodule.test_dataloader(), 2) |
80 | | - |
81 | | -train_set = LGI_OnDiskDataset("/scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/glycowork_full", path_idx=0) |
82 | | -val_set = LGI_OnDiskDataset("/scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/glycowork_full", path_idx=1) |
83 | | - |
84 | | -model = prep_model("LectinOracle", num_classes=1) |
85 | | -optimizer = torch.optim.Adam(model.parameters()) |
86 | | -scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer) |
87 | | - |
88 | | -m, met = train_model( |
89 | | - model=model, |
90 | | - dataloaders={"train": torch.utils.data.DataLoader(train_set, batch_size=128, collate_fn=collate_lgi), |
91 | | - "val": torch.utils.data.DataLoader(val_set, batch_size=128, collate_fn=collate_lgi)}, |
92 | | - criterion=torch.nn.MSELoss(), |
93 | | - optimizer=optimizer, |
94 | | - scheduler=scheduler, |
95 | | - return_metrics=True, |
96 | | - mode="regression", |
97 | | - num_epochs=100, |
98 | | - patience=100, |
99 | | -) |
100 | | - |
101 | | -import pickle |
102 | | - |
103 | | -with open("lectinoracle_full_model.pkl", "wb") as f: |
104 | | - pickle.dump(m, f) |
105 | | -with open("lectinoracle_full_metrics.pkl", "wb") as f: |
106 | | - pickle.dump(met, f) |
107 | 75 |
|
| 76 | +def train(): |
| 77 | + datamodule = LGI_GDM( |
| 78 | + root="/scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/lgi_data", filename="/home/rjo21/Desktop/GIFFLAR/lgi_data_full.pkl", hash_code="8b34af2a", |
| 79 | + batch_size=1, transform=None, pre_transform={"GIFFLARTransform": "", "SweetNetTransform": ""}, force_reload=True, |
| 80 | + ) |
| 81 | + |
| 82 | + model = prep_model("LectinOracle", num_classes=1) |
| 83 | + optimizer = torch.optim.Adam(model.parameters()) |
| 84 | + scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer) |
| 85 | + |
| 86 | + m, met = train_model( |
| 87 | + model=model, |
| 88 | + dataloaders={"train": torch.utils.data.DataLoader(datamodule.train, batch_size=128, collate_fn=collate_lgi), |
| 89 | + "val": torch.utils.data.DataLoader(datamodule.val, batch_size=128, collate_fn=collate_lgi)}, |
| 90 | + criterion=torch.nn.MSELoss(), |
| 91 | + optimizer=optimizer, |
| 92 | + scheduler=scheduler, |
| 93 | + return_metrics=True, |
| 94 | + mode="regression", |
| 95 | + num_epochs=100, |
| 96 | + patience=100, |
| 97 | + ) |
| 98 | + |
| 99 | + import pickle |
| 100 | + |
| 101 | + with open("lectinoracle_full_model_2.pkl", "wb") as f: |
| 102 | + pickle.dump(m, f) |
| 103 | + with open("lectinoracle_full_metrics_2.pkl", "wb") as f: |
| 104 | + pickle.dump(met, f) |
| 105 | + |
| 106 | + |
| 107 | +def test(): |
| 108 | + datamodule = LGI_GDM( |
| 109 | + root="/scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/lgi_data", filename="/home/rjo21/Desktop/GIFFLAR/experiments/results/unilectin.tsv", |
| 110 | + hash_code="8b34af2a", batch_size=1, transform=None, pre_transform={"GIFFLARTransform": "", "SweetNetTransform": ""}#, force_reload=True, |
| 111 | + ) |
| 112 | + |
| 113 | + get_ds(datamodule.train_dataloader(), 0) |
| 114 | + get_ds(datamodule.val_dataloader(), 1) |
| 115 | + get_ds(datamodule.test_dataloader(), 2) |
| 116 | + |
| 117 | + train_set = LGI_OnDiskDataset("/scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/glycowork_full", path_idx=0) |
| 118 | + val_set = LGI_OnDiskDataset("/scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/glycowork_full", path_idx=1) |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == "__main__": |
| 122 | + train() |
0 commit comments