-
Notifications
You must be signed in to change notification settings - Fork 2
/
reverse_index.py
50 lines (34 loc) · 1.53 KB
/
reverse_index.py
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
import pandas as pd
import numpy as np
import torch
__all__ = ['ReverseIndex']
class ReverseIndex():
def __init__(self, dataset, splits, device='cuda'):
self.df = pd.DataFrame(columns=['group', 'labels'])
for k in splits.keys():
labels = list(dataset.df.loc[splits[k]['train'],'labels'].value_counts().index)
group = [k for i in range(len(labels))]
data = pd.DataFrame(np.array([group, labels]).T, columns=['group', 'labels'])
self.df = self.df.append(data, ignore_index=True)
self.df['nodes'] = self.df.index
self.device = device
def _changeIndex(self, reverse_index, column):
reverse_index = reverse_index.set_index(column)
reverse_index[column] = reverse_index.index
return reverse_index
def getLabels(self, outputs):
outs = outputs.cpu().numpy()
reverse_index = self._changeIndex(self.df, 'nodes')
labels = reverse_index.loc[outs, 'labels']
labels = torch.tensor(list(labels))
return labels.to(self.device)
def getNodes(self, labels):
labels = labels.cpu().numpy()
reverse_index = self._changeIndex(self.df, 'labels')
nodes = reverse_index.loc[labels, 'nodes']
nodes = torch.tensor(list(nodes))
return nodes.to(self.device)
def getGroups(self, distinct=True):
return self.df['group'].value_counts().index.sort_values()
def getLabelsOfGroup(self, group):
return self.df.loc[self.df['group'] == group, 'labels']