-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathbase.py
More file actions
59 lines (49 loc) · 1.75 KB
/
Copy pathbase.py
File metadata and controls
59 lines (49 loc) · 1.75 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
import torch
import seaborn as sns
from pathlib import Path
from torch_geometric.data import InMemoryDataset
class BaseDataset(InMemoryDataset):
labels = []
_label2index = None
_index2label = None
_colors = None
def __init__(self, name, split, transform):
assert split in ['train', 'val', 'test']
super().__init__(f'data/dataset/{name}/', transform)
idx = self.processed_file_names.index('{}.pt'.format(split))
self.data, self.slices = torch.load(self.processed_paths[idx])
@property
def label2index(self):
if self._label2index is None:
self._label2index = dict()
for idx, label in enumerate(self.labels):
self._label2index[label] = idx
return self._label2index
@property
def index2label(self):
if self._index2label is None:
self._index2label = dict()
for idx, label in enumerate(self.labels):
self._index2label[idx] = label
return self._index2label
@property
def colors(self):
if self._colors is None:
n_colors = self.num_classes
colors = sns.color_palette('husl', n_colors=n_colors)
self._colors = [tuple(map(lambda x: int(x * 255), c))
for c in colors]
return self._colors
@property
def raw_file_names(self):
raw_dir = Path(self.raw_dir)
if not raw_dir.exists():
return []
return [p.name for p in raw_dir.iterdir()]
@property
def processed_file_names(self):
return ['train.pt', 'val.pt', 'test.pt']
def download(self):
raise FileNotFoundError('See data/README.md to prepare dataset')
def process(self):
raise NotImplementedError