HUANGYIFEI commited on
Commit
b8b5cda
·
verified ·
1 Parent(s): 154f024

add QM9_dataset_class.py

Browse files
Graph/GraphMAE_MQ9/QM9_dataset_class.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ from tqdm import tqdm
4
+ import networkx as nx
5
+ import torch
6
+ from torch.utils.data import Dataset
7
+
8
+ atom_number_index_dict = {
9
+ 1: 0, # H
10
+ 6: 1, # C
11
+ 7: 2, # N
12
+ 8: 3, # O
13
+ 9: 4 # F
14
+ }
15
+ atom_index_number_dict = {v: k for k, v in atom_number_index_dict.items()}
16
+ max_atom_number = max(atom_number_index_dict.keys())
17
+
18
+
19
+ def atom_number2index(atom_number):
20
+ return atom_number_index_dict[atom_number]
21
+
22
+
23
+ def atom_index2number(atom_index):
24
+ return atom_index_number_dict[atom_index]
25
+
26
+
27
+ class PreprocessedQM9Dataset(Dataset):
28
+ def __init__(self, dataset):
29
+ self.dataset = dataset
30
+ self.processed_data = []
31
+ if dataset is not None:
32
+ self._preprocess()
33
+ def _preprocess(self):
34
+ i = 0
35
+ for g, label in tqdm(self.dataset):
36
+ g.ndata["Z_index"] = torch.tensor([atom_number2index(z.item()) for z in g.ndata["Z"]])
37
+ g.ndata["sample_idx"] = i
38
+ self.processed_data.append((g, label))
39
+
40
+ def __len__(self):
41
+ return len(self.processed_data)
42
+
43
+ def __getitem__(self, idx):
44
+ return self.processed_data[idx]
45
+
46
+ def save_dataset(self, save_dir):
47
+ if not os.path.exists(save_dir):
48
+ os.makedirs(save_dir)
49
+ torch.save(self.processed_data, os.path.join(save_dir,"QM9_dataset_processed.pt"))
50
+ def load_dataset(self, dataset_path):
51
+ self.processed_data = torch.load(dataset_path)