-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathdataset.py
More file actions
50 lines (39 loc) · 1.57 KB
/
Copy pathdataset.py
File metadata and controls
50 lines (39 loc) · 1.57 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
import itertools
import numpy as np
import pandas as pd
def create_index(sessions):
lens = np.fromiter(map(len, sessions), dtype=np.long)
session_idx = np.repeat(np.arange(len(sessions)), lens - 1)
label_idx = map(lambda l: range(1, l), lens)
label_idx = itertools.chain.from_iterable(label_idx)
label_idx = np.fromiter(label_idx, dtype=np.long)
idx = np.column_stack((session_idx, label_idx))
return idx
def read_sessions(filepath):
sessions = pd.read_csv(filepath, sep='\t', header=None, squeeze=True)
sessions = sessions.apply(lambda x: list(map(int, x.split(',')))).values
return sessions
def read_dataset(dataset_dir):
train_sessions = read_sessions(dataset_dir / 'train.txt')
test_sessions = read_sessions(dataset_dir / 'test.txt')
with open(dataset_dir / 'num_items.txt', 'r') as f:
num_items = int(f.readline())
return train_sessions, test_sessions, num_items
class AugmentedDataset:
def __init__(self, sessions, sort_by_length=False):
self.sessions = sessions
# self.graphs = graphs
index = create_index(sessions) # columns: sessionId, labelIndex
if sort_by_length:
# sort by labelIndex in descending order
ind = np.argsort(index[:, 1])[::-1]
index = index[ind]
self.index = index
def __getitem__(self, idx):
#print(idx)
sid, lidx = self.index[idx]
seq = self.sessions[sid][:lidx]
label = self.sessions[sid][lidx]
return seq, label #,seq
def __len__(self):
return len(self.index)