-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpreprocess.py
More file actions
285 lines (250 loc) · 10.3 KB
/
Copy pathpreprocess.py
File metadata and controls
285 lines (250 loc) · 10.3 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import argparse
import importlib
import os
from pathlib import Path
import pickle
import numpy as np
import pandas as pd
from pykt.preprocess.split_datasets import main as split_concept
from pykt.preprocess.split_datasets_que import main as split_question
DATASETS: dict[str, dict[str, str | list[str]]] = {
"algebra2005": {
"url": "https://pslcdatashop.web.cmu.edu/KDDCup/",
"files": ["algebra_2005_2006_train.txt"],
},
"assist2009": {
"url": "https://sites.google.com/site/assistmentsdata/home/2009-2010-assistment-data/skill-builder-data-2009-2010",
"files": ["skill_builder_data_corrected_collapsed.csv"],
},
"assist2015": {
"url": "https://sites.google.com/site/assistmentsdata/datasets/2015-assistments-skill-builder-data",
"files": ["2015_100_skill_builders_main_problems.csv"],
},
"bridge2algebra2006": {
"url": "https://pslcdatashop.web.cmu.edu/KDDCup/",
"files": ["bridge_to_algebra_2006_2007_train.txt"],
},
"ednet": {
"url": "https://github.com/riiid/ednet => Download the complete folder with subfolders 'content' and 'KT1'",
"files": [""],
},
"nips_task34": {
"url": "https://eedi.com/projects/neurips-education-challenge => Also add the 'metadata' subfolder",
"files": ["train_task_3_4.csv"],
},
"poj": {
"url": "https://drive.google.com/drive/folders/1LRljqWfODwTYRMPw6wEJ_mMt1KZ4xBDk",
"files": ["poj_log.csv"],
},
"statics2011": {
"url": "https://pslcdatashop.web.cmu.edu/DatasetInfo?datasetId=507", # Other channel: https://drive.google.com/drive/folders/1CqYkJWno9oh0kPAt3eHRWowFR6BqeQsS
"files": ["AllData_student_step_2011F.csv"],
},
}
# write description used in readme + help for cli
description = f"""Preprocess datasets with pykt-toolkit.
First, download the necessary files from the provided URLs. After you have successfully downloaded the files, store them in your designated `file_path`.
Make sure to organize these files into folders that correspond to the specific dataset's name.
While preprocessing, information about the dataset is written to config.json in the root of the `file_path`.\n\n"""
for n, d in DATASETS.items():
description += (
f"""{n:20}{d.get('url')}\n{'':19} (Relevant files: {d.get('files', [])})\n"""
)
def get_args(description) -> tuple[str, str, int, int, int]:
parser = argparse.ArgumentParser(
description=description,
formatter_class=argparse.RawTextHelpFormatter,
)
parser.add_argument(
"file_path",
type=str,
help="file path to your data folder",
)
parser.add_argument(
"dataset",
type=str,
help="name of the dataset",
)
parser.add_argument(
"-m",
"--min_len",
type=int,
help="min length of sequences (default: 3)",
default=3,
)
parser.add_argument(
"-l",
"--max_len",
type=int,
help="max length of a sequence before being split (default: 200)",
default=200,
)
parser.add_argument(
"-k",
"--k_fold",
type=int,
help="num train/test folds (default: 5)",
default=5,
)
args = parser.parse_args()
return args.file_path, args.dataset, args.min_len, args.max_len, args.k_fold
def generate_selectmasks(select_only_last: bool, length: int) -> list[str]:
if select_only_last:
selectmasks = ["-1"] * (length - 1) + ["1"]
else:
selectmasks = ["1"] * (length)
return selectmasks
def pad_with_minus_ones(x: list[str], max_len: int):
return x + ["-1"] * (max_len - len(x))
if __name__ == "__main__":
# init cli
file_path, dataset, min_len, max_len, k_fold = get_args(description)
# script ...
path_data = str(Path(file_path) / dataset)
path_src = Path(file_path) / dataset / DATASETS[dataset]["files"][0]
path_tgt = Path(file_path) / dataset / "data.txt"
path_cfg = Path(file_path) / "config.json"
try:
mod = importlib.import_module(f"pykt.preprocess.{dataset}_preprocess")
read_data_from_csv = getattr(mod, "read_data_from_csv")
except ModuleNotFoundError:
raise ValueError(f"Cannot import functions from package `{dataset}`")
if dataset == "ednet":
path_data, path_tgt = read_data_from_csv(
str(path_src), str(path_tgt), dataset_name=dataset
)
elif dataset == "nips_task34":
metap = os.path.join(path_data, "metadata")
read_data_from_csv(path_src, metap, "task_3_4", path_tgt)
else:
read_data_from_csv(path_src, path_tgt)
### PREPROCESS DATASETS WITH PYKT (this writes/overwrites files) ###
# concept level models
split_concept(
dname=path_data,
fname=path_tgt,
dataset_name=dataset,
configf=path_cfg,
min_seq_len=min_len,
maxlen=max_len,
kfold=k_fold,
)
# question level models
split_question(
dname=path_data,
fname=path_tgt,
dataset_name=dataset,
configf=path_cfg,
min_seq_len=min_len,
maxlen=max_len,
kfold=k_fold,
)
# overwrite `test_window_sequences_quelevel.csv` files with length-corrected version
test_quelevel = pd.read_csv(Path(file_path) / dataset / "test_quelevel.csv")
# generate corrected sequences
if "questions" in test_quelevel.columns and "concepts" in test_quelevel.columns:
print(
"Overwrite `test_window_sequences_quelevel.csv` files with length-corrected version ..."
)
fold = -1
corrected_test_window_sequences_quelevel_list = []
for i, row in test_quelevel.iterrows():
assert row["fold"] == fold
uid = row["uid"]
questions = row["questions"].split(",")
concepts = row["concepts"].split(",")
responses = row["responses"].split(",")
# get number of questions
num_concepts = np.array([c.count("_") + 1 for c in concepts])
assert len(num_concepts) == len(questions)
# loop
start = 0
first_end_idx = (num_concepts.cumsum() > max_len).argmax()
if first_end_idx == 0:
first_end_idx = len(questions)
for end in range(first_end_idx, len(questions) + 1):
while sum(num_concepts[start:end]) > max_len:
start += 1
corrected_test_window_sequences_quelevel_list.append(
dict(
fold=fold,
uid=uid,
questions=pad_with_minus_ones(
questions[start:end], max_len=max_len
),
concepts=pad_with_minus_ones(
concepts[start:end], max_len=max_len
),
responses=pad_with_minus_ones(
responses[start:end], max_len=max_len
),
selectmasks=pad_with_minus_ones(
generate_selectmasks(
select_only_last=(end != first_end_idx),
length=end - start,
),
max_len=max_len,
),
)
)
# to DataFrame, lists as string + saving
corrected_test_window_sequences_quelevel_df = pd.DataFrame(
corrected_test_window_sequences_quelevel_list
)
for column in ["questions", "concepts", "responses", "selectmasks"]:
corrected_test_window_sequences_quelevel_df[column] = (
corrected_test_window_sequences_quelevel_df[column].apply(
lambda x: ",".join(x)
)
)
corrected_target_path = Path(path_data) / "test_window_sequences_quelevel.csv"
corrected_test_window_sequences_quelevel_df.to_csv(
corrected_target_path, index=False
)
### generate `unique_concept_mapping.pkl` (for KTST) ###
# pd.read train_valid_sequences.csv, test.csv
train_data = pd.read_csv(Path(file_path) / dataset / "train_valid_sequences.csv")
test_data = pd.read_csv(Path(file_path) / dataset / "test.csv")
# concat
df = pd.concat([train_data, test_data])
# get_tensor_dataset_from_pykt_dataset
set_of_concept_sets = set()
# Iterate over rows due to inhomogeneous shapes
if "questions" in df.columns and "concepts" in df.columns:
for i, row in df.iterrows():
get_tensor = lambda x: np.array([int(s) for s in x.split(",")])
q, c, r = (
get_tensor(row[x]) for x in ["questions", "concepts", "responses"]
)
qic = get_tensor(row["is_repeat"]) == 1
assert q.shape == c.shape == r.shape == qic.shape
assert qic.ndim == 1
qic[0] = (
False # this change is consistent with how we load data in __init__.py
)
m = q != -1
q, c, r, qic = (
q[m],
c[m],
r[m],
qic[m],
) # masked tensors is what we work with in `format_question_combinatorial_dense`
q_reduced = q[~qic]
individual_questions = np.cumsum(~qic) - 1
assert individual_questions.min() == 0
# Make dense_concept_tensor
list_of_concept_sets = [set() for _ in range(len(q_reduced))]
# Add concepts of individual questions to separate lists
for iq, ic in zip(individual_questions.tolist(), c.tolist()):
list_of_concept_sets[iq].add(ic)
for s in list_of_concept_sets:
set_of_concept_sets.add(tuple(sorted(s)))
assert (-1,) not in set_of_concept_sets
set_of_concepts_to_unique_id = {}
for i, concept_set in enumerate(set_of_concept_sets):
set_of_concepts_to_unique_id[concept_set] = i
# format_question_combinatorial_dense -> save mapping for unique concept combs.
with open(Path(file_path) / dataset / "unique_concept_mapping.pkl", "wb") as f:
pickle.dump(
set_of_concepts_to_unique_id, f, protocol=pickle.HIGHEST_PROTOCOL
)