Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
174 lines (147 loc) · 6.21 KB
/
Copy pathutils.py
File metadata and controls
174 lines (147 loc) · 6.21 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import os
import json
import torch
import random
from pathlib import Path
import numpy as np
from math import radians, cos, sin, asin, sqrt, floor
from torch.utils.data import Sampler
from torch.nn.utils.rnn import pad_sequence
from einops import reduce, repeat
try:
import cPickle as _pickle
except ImportError:
import pickle as _pickle
def serialize(obj, path, in_json=False):
if in_json:
with open(path, "w") as file:
json.dump(obj, file, indent=2)
else:
with open(path, 'wb') as file:
_pickle.dump(obj, file)
def set_seed(seed):
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
# some cudnn methods can be random even after fixing the seed
# unless you tell it to be deterministic
torch.backends.cudnn.benchmark = False
# torch.backends.cudnn.enabled = False
torch.backends.cudnn.deterministic = True
torch.cuda.init()
def mk_dir(path_str):
file = Path(path_str)
file.mkdir(parents=True, exist_ok=True)
return path_str
def get_attn_mask(max_len, device):
mask = (torch.triu(torch.ones(max_len, max_len))).transpose(0, 1)
return mask.to(device)
def get_pad_mask(data_size, max_len, device):
mask = torch.zeros([len(data_size), max_len, max_len])
for i in range(len(data_size)):
mask[i][0: data_size[i], 0: data_size[i]] = torch.ones(data_size[i], data_size[i])
return mask.to(device)
def get_key_pad_mask(data_size, max_len, num_neg, device):
mask = torch.zeros([len(data_size), max_len, max_len])
for i in range(len(data_size)):
mask[i][0: data_size[i], 0: data_size[i]] = torch.ones(data_size[i], data_size[i])
mask = mask.repeat(1, num_neg + 1, num_neg + 1).to(device)
return mask
def get_mem_mask(max_len, num_neg, device):
mask = torch.zeros((1 + num_neg) * max_len, (1 + num_neg) * max_len)
attend_items = (torch.triu(torch.ones(max_len, max_len))).transpose(0, 1)
for i in range(0, (1 + num_neg) * max_len, max_len):
mask[i:i + max_len, i:i + max_len] = attend_items
mask = mask.to(device)
return mask
def unserialize(path):
suffix = os.path.basename(path).split(".")[-1]
if suffix == "json":
with open(path, "r") as file:
return json.load(file)
else:
with open(path, 'rb') as file:
return _pickle.load(file)
class LadderSampler(Sampler):
def __init__(self, data_source, batch_sz, fix_order=False):
super(LadderSampler, self).__init__(data_source)
self.data = [len(e[0]) for e in data_source]
self.batch_size = batch_sz * 100
self.fix_order = fix_order
def __iter__(self):
if self.fix_order:
d = zip(self.data, np.arange(len(self.data)), np.arange(len(self.data)))
else:
d = zip(self.data, np.random.permutation(len(self.data)), np.arange(len(self.data)))
d = sorted(d, key=lambda e: (e[1] // self.batch_size, e[0]), reverse=True)
return iter(e[2] for e in d)
def __len__(self):
return len(self.data)
def fix_length(sequences, n_axies, max_len, dtype='source'):
if dtype != 'eval trg loc':
padding_term = torch.zeros_like(sequences[0])
length = padding_term.size(0)
# (l, any) -> (1, any) -> (max_len any)
if n_axies == 1:
padding_term = reduce(padding_term, '(h l) -> h', 'max', l=length)
padding_term = repeat(padding_term, 'h -> (repeat h)', repeat=max_len)
elif n_axies == 2:
padding_term = reduce(padding_term, '(h l) any -> h any', 'max', l=length)
padding_term = repeat(padding_term, 'h any -> (repeat h) any', repeat=max_len)
else:
padding_term = reduce(padding_term, '(h l) any_1 any_2 -> h any_1 any_2', 'max', l=length)
padding_term = repeat(padding_term, 'h any_1 any_2 -> (repeat h) any_1 any_2', repeat=max_len)
sequences.append(padding_term)
tensor = pad_sequence(sequences, True)
return tensor[:-1]
else:
tensor = pad_sequence(sequences, True)
return tensor
def get_visited_locs(dataset):
user_visited_locs = {}
for u in range(len(dataset.user_seq)):
seq = dataset.user_seq[u]
user = seq[0][0]
user_visited_locs[user] = set()
for i in reversed(range(len(seq))):
if not seq[i][-1]:
break
user_visited_locs[user].add(seq[i][1])
seq = seq[:i]
for check_in in seq:
user_visited_locs[user].add(check_in[1])
return user_visited_locs
def build_st_matrix(dataset, sequence, max_len):
seq_len = len(sequence)
temporal_matrix = torch.zeros(max_len, max_len)
spatial_matrix = torch.zeros(max_len, max_len)
seq_loc = [r[1] for r in sequence]
seq_time = [r[3] for r in sequence]
for x in range(seq_len):
for y in range(x):
temporal_matrix[x, y] = floor((seq_time[x] - seq_time[y]) / 3600)
spatial_matrix[x, y] = haversine(dataset.idx2gps[seq_loc[x]], dataset.idx2gps[seq_loc[y]])
return temporal_matrix, spatial_matrix
def haversine(point_1, point_2):
lat_1, lng_1 = point_1
lat_2, lng_2 = point_2
lat_1, lng_1, lat_2, lng_2 = map(radians, [lat_1, lng_1, lat_2, lng_2])
d_lon = lng_2 - lng_1
d_lat = lat_2 - lat_1
a = sin(d_lat / 2) ** 2 + cos(lat_1) * cos(lat_2) * sin(d_lon / 2) ** 2
c = 2 * asin(sqrt(a))
r = 6371
return floor(c * r)
def extract_sub_matrix(start_idx, end_idx, seq_len, max_len, temporal_matrix, spatial_matrix):
sub_t_mat = torch.zeros(max_len, max_len)
sub_s_mat = torch.zeros(max_len, max_len)
if seq_len == max_len:
sub_t_mat = temporal_matrix[start_idx: end_idx, start_idx: end_idx]
sub_s_mat = spatial_matrix[start_idx: end_idx, start_idx: end_idx]
else:
sub_t_mat[start_idx - start_idx: end_idx - start_idx, start_idx - start_idx: end_idx - start_idx] = temporal_matrix[start_idx: end_idx, start_idx: end_idx]
sub_s_mat[start_idx - start_idx: end_idx - start_idx, start_idx - start_idx: end_idx - start_idx] = spatial_matrix[start_idx: end_idx, start_idx: end_idx]
return sub_t_mat, sub_s_mat