-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathAFCI_LabellingScript.py
More file actions
378 lines (301 loc) · 12.6 KB
/
Copy pathAFCI_LabellingScript.py
File metadata and controls
378 lines (301 loc) · 12.6 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import argparse
import os
import re
import random
import shutil
from pathlib import Path
import numpy as np
import pandas as pd
SEED = 42
random.seed(SEED)
np.random.seed(SEED)
def numeric_key(name: str) -> int:
m = re.search(r"_(\d+)\.txt$", name)
return int(m.group(1)) if m else -1
def read_csv_numeric_matrix(file_path: Path) -> np.ndarray:
df = pd.read_csv(file_path, header=None, low_memory=False)
arr = df.apply(pd.to_numeric, errors="coerce").to_numpy()
return arr[~np.all(np.isnan(arr), axis=1)]
def process_arc_csv(csv_file, FRAME_SIZE, WINDOW_FRAMES, V_MIN, V_MAX, FRAME_ARC_THRESHOLD):
df = pd.read_csv(csv_file)
voltage = df["y"].values.astype(float) - (2**11)
current = (df["x"].values.astype(int) - 2048) << 4
current = np.clip(current, -32768, 32767).astype(np.int16)
num_frames = len(voltage) // FRAME_SIZE
num_windows = num_frames - WINDOW_FRAMES + 1
arc_windows, arc_normals = [], []
base = os.path.splitext(os.path.basename(csv_file))[0]
for w in range(num_windows):
start = w * FRAME_SIZE
end = start + FRAME_SIZE * WINDOW_FRAMES
if end > len(voltage):
break
arc_frame_count = 0
v_max = None
for f in range(WINDOW_FRAMES):
fs, fe = start + f * FRAME_SIZE, start + (f + 1) * FRAME_SIZE
v_frame = voltage[fs:fe]
v_rms = np.sqrt(np.mean(v_frame.astype(np.float32) ** 2))
v_max = np.max(np.abs(v_frame))
if (v_rms <= V_MAX) and (V_MIN <= v_max):
arc_frame_count += 1
if arc_frame_count >= FRAME_ARC_THRESHOLD:
arc_windows.append((f"{base}_arc_window_{w}.txt", current[start:end]))
else:
if (v_max is not None) and (v_max <= V_MIN / 2):
arc_normals.append((f"{base}_normal_window_{w}.txt", current[start:end]))
return arc_windows, arc_normals
def process_normal_csv(csv_file, FRAME_SIZE, WINDOW_FRAMES):
df = pd.read_csv(csv_file)
current = (df["x"].values.astype(int) - 2048) << 4
current = np.clip(current, -32768, 32767).astype(np.int16)
num_frames = len(current) // FRAME_SIZE
num_windows = num_frames - WINDOW_FRAMES + 1
pure_normals = []
base = os.path.splitext(os.path.basename(csv_file))[0]
for w in range(num_windows):
start = w * FRAME_SIZE
end = start + FRAME_SIZE * WINDOW_FRAMES
if end > len(current):
break
pure_normals.append((f"{base}_normal_window_{w}.txt", current[start:end]))
return pure_normals
def flatten_window_outputs(classes_dir: Path):
arc_out = classes_dir / "arc"
normal_out = classes_dir / "normal"
arc_out.mkdir(parents=True, exist_ok=True)
normal_out.mkdir(parents=True, exist_ok=True)
mapping = {
"arc_dir": arc_out,
"normal_dir": normal_out,
}
for dir_type, out_dir in mapping.items():
source = classes_dir / dir_type
if not source.exists():
continue
app_dirs = sorted([p for p in source.iterdir() if p.is_dir()], key=lambda p: p.name.lower())
for app_dir in app_dirs:
files = sorted([p for p in app_dir.iterdir() if p.is_file() and p.suffix == ".txt"],
key=lambda p: p.name.lower())
for file in files:
dst_file = out_dir / f"{app_dir.name}_{file.name}"
shutil.copy(file, dst_file)
shutil.rmtree(app_dir)
shutil.rmtree(source)
def run_window_mode(args):
dataset_path = Path(args.dataset_path).resolve()
classes_dir = dataset_path / "classes"
ARC_DIR = classes_dir / "arc_dir"
NORMAL_DIR = classes_dir / "normal_dir"
classes_dir.mkdir(parents=True, exist_ok=True)
ARC_DIR.mkdir(parents=True, exist_ok=True)
NORMAL_DIR.mkdir(parents=True, exist_ok=True)
FRAME_SIZE = args.frame_size
WINDOW_FRAMES = args.window_frames
V_MIN = (2**11) * 0.12
V_MAX = (2**11) * (0.34) / 0.707
T = args.frame_thresh
csv_root = Path(args.csv_folder).resolve()
arc_root = csv_root / "arc"
normal_root = csv_root / "normal"
arc_csvs_at_root = sorted(list(arc_root.glob("*.csv")), key=lambda p: p.name.lower())
arc_subdirs = sorted([p for p in arc_root.iterdir() if p.is_dir()], key=lambda p: p.name.lower())
if arc_subdirs:
app_folders = arc_subdirs
root_mode = False
elif arc_csvs_at_root:
app_folders = [arc_root]
root_mode = True
else:
raise FileNotFoundError(f"No CSVs found under {arc_root} (neither subfolders nor root CSVs).")
print(f"[DEBUG] arc_root = {arc_root}")
print(f"[DEBUG] normal_root = {normal_root}")
print(f"[DEBUG] app_folders = {[p.name for p in app_folders]}")
for app_folder in app_folders:
app = "root" if (root_mode and app_folder == arc_root) else app_folder.name
arc_csvs = (
arc_csvs_at_root
if (root_mode and app_folder == arc_root)
else sorted(list(app_folder.glob("*.csv")), key=lambda p: p.name.lower())
)
normal_app = normal_root if root_mode else (normal_root / app_folder.name)
normal_csvs = sorted(list(normal_app.glob("*.csv")), key=lambda p: p.name.lower()) if normal_app.exists() else []
arc_outdir = ARC_DIR / app
normal_outdir = NORMAL_DIR / app
arc_outdir.mkdir(parents=True, exist_ok=True)
normal_outdir.mkdir(parents=True, exist_ok=True)
arc_windows, arc_normals, pure_normals = [], [], []
for csv in arc_csvs:
aw, an = process_arc_csv(csv, FRAME_SIZE, WINDOW_FRAMES, V_MIN, V_MAX, T)
arc_windows.extend(aw)
arc_normals.extend(an)
for csv in normal_csvs:
pure_normals.extend(process_normal_csv(csv, FRAME_SIZE, WINDOW_FRAMES))
if args.balanced:
N = len(arc_windows)
sel_an = min(len(arc_normals), N)
sel_pn = min(len(pure_normals), N)
selected_arc_normals = random.sample(arc_normals, sel_an) if sel_an > 0 else []
selected_pure_normals = random.sample(pure_normals, sel_pn) if sel_pn > 0 else []
else:
selected_arc_normals = arc_normals
selected_pure_normals = pure_normals
if arc_windows:
ordered = sorted(arc_windows, key=lambda x: numeric_key(x[0]))
out = arc_outdir / f"{app}_arc.txt"
with open(out, "w") as f:
for _, data in ordered:
f.writelines(f"{v}\n" for v in data)
if selected_arc_normals:
ordered = sorted(selected_arc_normals, key=lambda x: numeric_key(x[0]))
out = normal_outdir / f"{app}_arc_normal.txt"
with open(out, "w") as f:
for _, data in ordered:
f.writelines(f"{v}\n" for v in data)
if selected_pure_normals:
ordered = sorted(selected_pure_normals, key=lambda x: numeric_key(x[0]))
out = normal_outdir / f"{app}_pure_normal.txt"
with open(out, "w") as f:
for _, data in ordered:
f.writelines(f"{v}\n" for v in data)
flatten_window_outputs(classes_dir)
def run_frame_mode(args):
dataset_path = Path(args.dataset_path).resolve()
classes_dir = dataset_path / "classes"
arc_dir = classes_dir / "arc_dir"
normal_dir = classes_dir / "normal_dir"
classes_dir.mkdir(parents=True, exist_ok=True)
arc_dir.mkdir(parents=True, exist_ok=True)
normal_dir.mkdir(parents=True, exist_ok=True)
csvs = sorted(Path(args.csv_folder).rglob("*.csv"), key=lambda p: str(p).lower())
uid = 0
save_col = args.frame_save_col # 1-based
FRAME_SIZE = args.frame_size
FRAME_SMOOTH_FRAMES = args.frame_smooth_frames
FRAME_THRESH = args.frame_thresh
MIN_V = 150.0
MAX_V = (2**11) * (0.24) / 0.707
NORM_V = 110.0
for file_path in csvs:
pname = file_path.stem.replace("_", "")
arc_ivl = read_csv_numeric_matrix(file_path)
if arc_ivl.shape[0] == 0:
continue
arc_ivl[:, 1:3] = arc_ivl[:, 1:3] - (2**11) + 60 #The bandpass and voltage are both centered at 1.6 V , hence -2**11
arc_ivl[:, 3] = arc_ivl[:, 3] * 8 #multiply raw log300 value by 8
new_size = (arc_ivl.shape[0] // FRAME_SIZE) * FRAME_SIZE
if new_size == 0:
continue
mcu_iv = np.column_stack([
arc_ivl[:new_size, 0],
arc_ivl[:new_size, save_col - 1],
arc_ivl[:new_size, 2],
])
n_frames = new_size // FRAME_SIZE
frames = np.reshape(mcu_iv.T, (3, FRAME_SIZE, n_frames), order="F")
labels = np.zeros(n_frames, dtype=np.int32)
norms = np.zeros(n_frames, dtype=np.int32)
combined = np.zeros(n_frames, dtype=np.int32)
for j in range(n_frames):
v = frames[2, :, j]
v_max = np.max(np.abs(v))
v_rms = np.sqrt(np.mean(v**2))
if (v_max >= MIN_V) and (v_rms <= MAX_V):
labels[j] = 1
if v_max <= NORM_V:
norms[j] = 1
for j in range(0, n_frames - FRAME_SMOOTH_FRAMES):
s = int(np.sum(labels[j:j + FRAME_SMOOTH_FRAMES + 1]))
if s >= FRAME_THRESH:
combined[j:j + FRAME_SMOOTH_FRAMES + 1] = FRAME_THRESH + 1
norms[combined >= FRAME_THRESH] = 0
abs_index = 1
file_len = 1
label_arc = False
label_norm = False
condition = "none"
for j in range(1, n_frames + 1):
prev = condition
if norms[j - 1] >= 1:
condition = "normal"
elif combined[j - 1] >= FRAME_THRESH:
condition = "arc"
else:
condition = "none"
start = abs_index - 1
end = (j - 1) * FRAME_SIZE
if prev != condition:
if label_arc:
if j != 1:
if file_len > 8:
uid += 1
np.savetxt(
arc_dir / f"arc_{pname}{uid}_{file_len}.txt",
mcu_iv[start:end, 1],
fmt="%.18g",
)
file_len = 1
label_arc = False
elif label_norm:
if j != 1:
if file_len > 8:
uid += 1
np.savetxt(
normal_dir / f"normal_{pname}{uid}_{file_len}.txt",
mcu_iv[start:end, 1],
fmt="%.18g",
)
file_len = 1
label_norm = False
if condition == "arc":
if label_arc:
file_len += 1
else:
file_len = 1
abs_index = (j - 1) * FRAME_SIZE + 1
label_arc = True
elif condition == "normal":
if label_norm:
file_len += 1
else:
file_len = 1
abs_index = (j - 1) * FRAME_SIZE + 1
label_norm = True
if condition == "arc":
uid += 1
np.savetxt(
arc_dir / f"arc_{pname}{uid}_{file_len}.txt",
mcu_iv[abs_index - 1:, 1],
fmt="%.18g",
)
elif condition == "normal":
uid += 1
np.savetxt(
normal_dir / f"normal_{pname}{uid}_{file_len}.txt",
mcu_iv[abs_index - 1:, 1],
fmt="%.18g",
)
def main():
p = argparse.ArgumentParser()
p.add_argument("--mode", choices=["frame", "window"], required=True)
p.add_argument("--dataset_path", required=True)
p.add_argument("--csv_folder", required=True)
p.add_argument("--frame_size", type=int, required=True)
p.add_argument("--frame_thresh", type=int, required=True)
p.add_argument(
"--frame_save_col",
type=int,
default=4,
help="1-based CSV column to save as output waveform in frame mode. "
"Use 4 for LOG300 (4-col CSV), 2 for current (3-col LPF). (This method assumes that column 1 is time and column 3 is Voltage)"
)
p.add_argument("--frame_smooth_frames", type=int, default=6)
p.add_argument("--window_frames", type=int, default=8)
p.add_argument("--balanced", action="store_true")
args = p.parse_args()
if args.mode == "frame":
run_frame_mode(args)
else:
run_window_mode(args)
if __name__ == "__main__":
main()