This repository was archived by the owner on Feb 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_preprocessing.py
212 lines (177 loc) · 6.97 KB
/
run_preprocessing.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import pandas as pd
from tqdm import tqdm
from data.types import (
DataType,
BasePreprocessingConfig,
)
import torch as t
import json
import re
from run_data_splitting import train_test_split_by_time
from utils.labelencoder import encode_labels
from config import preprocessing_config
from typing import List
from utils.preprocessing import (
create_data_pyg,
create_data_dgl,
create_ids_and_maps,
extract_edges,
extract_reverse_edges,
)
from data.neo4j.save import save_to_neo4j
def save_to_csv(dataframe: pd.DataFrame, name: str):
dataframe.to_csv(f"data/saved/{name}.csv", index=False)
def preprocess(config: BasePreprocessingConfig):
config.print()
print("| Loading customers...")
customers = pd.read_csv(
"data/original/users.dat",
delimiter="::",
names=["customer_id", "gender", "age", "occupation", "zip"],
)
print("| Loading articles...")
articles = []
# pandas dies when is trying to parse this file, so falling back to the example code
with open("data/original/movies.dat", encoding="latin1") as f:
for l in f:
id_, title, genres = l.strip().split("::")
genres_set = set(genres.split("|"))
# extract year
assert re.match(r".*\([0-9]{4}\)$", title)
year = title[-5:-1]
title = title[:-6].strip()
data = {"article_id": int(id_), "title": title, "year": year}
for g in genres_set:
data[g] = 1
articles.append(data)
articles = pd.DataFrame(articles).fillna(0)
print("| Loading transactions...")
transactions = pd.read_csv(
"data/original/ratings.dat",
delimiter="::",
names=["customer_id", "article_id", "rating", "timestamp"],
)
if config.data_size is not None:
transactions = transactions[: config.data_size]
print("| Encoding article features...")
for column in tqdm(articles.columns):
if column != "article_id":
articles[column] = encode_labels(articles[column])
print("| Encoding customer features...")
for column in tqdm(customers.columns):
if column != "customer_id":
customers[column] = encode_labels(customers[column])
if config.filter_out_unconnected_nodes:
print("| Removing unconnected nodes...")
all_article_ids_referenced = set(transactions["article_id"].unique())
all_customer_ids_referenced = set(transactions["customer_id"].unique())
disjoint_customers = set(customers["customer_id"].unique()).difference(
all_customer_ids_referenced
)
print("| Removing {} customers...".format(len(disjoint_customers)))
disjoint_articles = set(articles["article_id"].unique()).difference(
all_article_ids_referenced
)
print("| Removing {} articles...".format(len(disjoint_articles)))
customers = customers[~customers["customer_id"].isin(disjoint_customers)]
articles = articles[~articles["article_id"].isin(disjoint_articles)]
customers, customer_id_map_forward, customer_id_map_reverse = create_ids_and_maps(
customers, "customer_id", 0
)
articles, article_id_map_forward, article_id_map_reverse = create_ids_and_maps(
articles,
"article_id",
0,
)
print("| Parsing transactions...")
transactions["article_id"] = transactions["article_id"].apply(
lambda x: article_id_map_reverse[x]
)
transactions["customer_id"] = transactions["customer_id"].apply(
lambda x: customer_id_map_reverse[x]
)
print(
"| Splitting into train/test/val edges (using chronological stratified splitting)..."
)
transactions = transactions.sort_values("timestamp")
transactions = train_test_split_by_time(transactions, "customer_id")
transactions_train = transactions[transactions["train_mask"] == True]
transactions_val = pd.concat(
[transactions_train, transactions[transactions["val_mask"] == True]], axis=0
)
transactions_test = pd.concat(
[transactions_val, transactions[transactions["test_mask"] == True]], axis=0
)
print("| Removing unused columns...")
customers.drop(["customer_id"], axis=1, inplace=True)
articles.drop(["article_id"], axis=1, inplace=True)
if config.save_to_neo4j:
save_to_neo4j(customers, articles, transactions)
print("| Converting to tensors...")
customers = t.tensor(customers.to_numpy(), dtype=t.long)
assert t.isnan(customers).any() == False
articles = t.tensor(articles.to_numpy(), dtype=t.long)
assert t.isnan(articles).any() == False
print("| Creating Data...")
# If we ever want to get dgl data creation back
# create_func = (
# create_data_dgl if config.data_type == DataType.dgl else create_data_pyg
# )
train_graph = create_data_pyg(
customers,
articles,
None,
None,
transactions_train["customer_id"].to_numpy(),
transactions_train["article_id"].to_numpy(),
None,
None,
None,
)
val_graph = create_data_pyg(
customers,
articles,
None,
None,
transactions_val["customer_id"].to_numpy(),
transactions_val["article_id"].to_numpy(),
None,
None,
None,
)
test_graph = create_data_pyg(
customers,
articles,
None,
None,
transactions_test["customer_id"].to_numpy(),
transactions_test["article_id"].to_numpy(),
None,
None,
None,
)
print("| Saving the graph...")
t.save(train_graph, "data/derived/train_graph.pt")
t.save(val_graph, "data/derived/val_graph.pt")
t.save(test_graph, "data/derived/test_graph.pt")
print("| Extracting edges per customer / per article...")
t.save(extract_edges(transactions_train), "data/derived/edges_train.pt")
t.save(extract_reverse_edges(transactions_train), "data/derived/rev_edges_train.pt")
t.save(extract_edges(transactions_val), "data/derived/edges_val.pt")
t.save(extract_reverse_edges(transactions_val), "data/derived/rev_edges_val.pt")
t.save(extract_edges(transactions_test), "data/derived/edges_test.pt")
t.save(extract_reverse_edges(transactions_test), "data/derived/rev_edges_test.pt")
print("| Saving the node-to-id mapping...")
with open("data/derived/customer_id_map_forward.json", "w") as fp:
json.dump(customer_id_map_forward, fp)
with open("data/derived/article_id_map_forward.json", "w") as fp:
json.dump(article_id_map_forward, fp)
def extract_users_per_location(customers: pd.DataFrame) -> dict:
return customers.groupby("postal_code")["index"].apply(list).to_dict()
def extract_location_for_user(customers: pd.DataFrame) -> dict:
return customers["postal_code"].to_dict()
def read_file(filename: str) -> List[str]:
with open(filename, encoding="latin1") as file:
return list(file)
if __name__ == "__main__":
preprocess(preprocessing_config)