-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmissing_csv.py
More file actions
309 lines (250 loc) · 9.82 KB
/
missing_csv.py
File metadata and controls
309 lines (250 loc) · 9.82 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
#!/usr/bin/env python3
"""
Create mapping_remaining.csv from mapping_queue.csv by removing *segments* that
already have bbox CSV outputs.
Segment is considered processed if a file exists matching:
<bbox_dir>/{video_id}_{start_time}_*.csv
Row is removed entirely if, after segment removal, there are no videos left in that row.
"""
from __future__ import annotations
import ast
import csv
from pathlib import Path
from typing import Any, Dict, List, Set, Tuple
import pandas as pd
import common
# =========================
# EDIT THESE PATHS
# =========================
INPUT_CSV = common.get_configs("mapping")
OUTPUT_CSV = "mapping_remaining.csv"
# Put ALL bbox output directories you want to check here.
BBOX_OUTPUT_DIRS = common.get_configs("data")
# =========================
# FLAG: print removed cities
# =========================
PRINT_REMOVED_CITIES = False # set True to print which locality rows are dropped
# =========================
# Parsing helpers
# =========================
def parse_bracketed_str_list(value: Any) -> List[str]:
"""Parse '[a,b,c]' (items not quoted) -> ['a','b','c']."""
if value is None:
return []
s = str(value).strip()
if not s or s.lower() == "nan":
return []
if s.startswith("[") and s.endswith("]"):
s = s[1:-1]
s = s.strip()
if not s:
return []
return [x.strip() for x in s.split(",") if x.strip()]
def parse_bracketed_int_list(value: Any) -> List[int]:
"""Parse '[1,2,3]' -> [1,2,3]."""
if value is None:
return []
s = str(value).strip()
if not s or s.lower() == "nan":
return []
try:
obj = ast.literal_eval(s)
if isinstance(obj, list):
return [int(x) for x in obj]
except Exception:
pass
out: List[int] = []
for it in parse_bracketed_str_list(s):
try:
out.append(int(it))
except Exception:
out.append(0)
return out
def parse_list_of_lists(value: Any) -> List[List[int]]:
"""Parse '[[1,2],[3]]' -> [[1,2],[3]]."""
if value is None:
return []
s = str(value).strip()
if not s or s.lower() == "nan":
return []
try:
obj = ast.literal_eval(s)
except Exception:
return []
if not isinstance(obj, list):
return []
out: List[List[int]] = []
for item in obj:
if isinstance(item, list):
try:
out.append([int(x) for x in item])
except Exception:
out.append([])
else:
try:
out.append([int(item)])
except Exception:
out.append([])
return out
def normalize_len(lst: List[Any], n: int, pad: Any) -> List[Any]:
"""Pad or truncate list to length n."""
if len(lst) >= n:
return lst[:n]
return lst + [pad] * (n - len(lst))
# =========================
# Formatting helpers
# =========================
def format_str_list_no_quotes(items: List[str]) -> str:
return "[" + ",".join(items) + "]"
def format_int_list(items: List[int]) -> str:
return "[" + ",".join(str(x) for x in items) + "]"
def format_list_of_lists(lol: List[List[int]]) -> str:
inner = []
for lst in lol:
inner.append("[" + ",".join(str(x) for x in lst) + "]")
return "[" + ",".join(inner) + "]"
# =========================
# Bbox presence indexing
# =========================
def index_existing_bbox_segments(bbox_dirs: List[Path]) -> Set[Tuple[str, int]]:
"""
Build a set of (video_id, start_time) from bbox files.
Expected bbox filename: {video_id}_{start}_{fps}.csv
video_id may contain underscores, so parse from the right:
parts[-2] = start_time
parts[:-2] join back to video_id
"""
found: Set[Tuple[str, int]] = set()
for d in bbox_dirs:
if not d.exists() or not d.is_dir():
continue
for p in d.glob("*.csv"):
stem = p.stem
parts = stem.split("_")
if len(parts) < 3:
continue
st_str = parts[-2]
if not st_str.lstrip("-").isdigit():
continue
vid = "_".join(parts[:-2])
found.add((vid, int(st_str)))
return found
# =========================
# Main transformation
# =========================
def build_remaining() -> None:
bbox_dirs = [Path(p) for p in BBOX_OUTPUT_DIRS]
bbox_present = index_existing_bbox_segments(bbox_dirs)
df = pd.read_csv(INPUT_CSV, dtype=str, keep_default_na=False)
required_cols = ["videos", "start_time", "end_time", "time_of_day"]
for c in required_cols:
if c not in df.columns:
raise ValueError(f"Missing required column: {c}")
# Only enforce locality column if printing is enabled (keeps change minimal)
if PRINT_REMOVED_CITIES and "locality" not in df.columns:
raise ValueError("Missing required column: locality (required when PRINT_REMOVED_CITIES=True)")
has_vehicle = "vehicle_type" in df.columns
has_upload = "upload_date" in df.columns
has_channel = "channel" in df.columns
kept_rows: List[Dict[str, Any]] = []
removed_segments = 0
kept_segments = 0
dropped_rows = 0
removed_cities: List[str] = [] # collects locality names of dropped rows (optional)
for _, row in df.iterrows():
videos = parse_bracketed_str_list(row.get("videos", ""))
n = len(videos)
if n == 0:
dropped_rows += 1
if PRINT_REMOVED_CITIES:
removed_cities.append(str(row.get("locality", "")).strip())
continue
st_ll = normalize_len(parse_list_of_lists(row.get("start_time", "")), n, [])
et_ll = normalize_len(parse_list_of_lists(row.get("end_time", "")), n, [])
tod_ll = normalize_len(parse_list_of_lists(row.get("time_of_day", "")), n, [])
vehicle = normalize_len(parse_bracketed_int_list(row.get("vehicle_type", "[]")), n, 0) if has_vehicle else []
upload = normalize_len(parse_bracketed_int_list(row.get("upload_date", "[]")), n, 0) if has_upload else []
channel = normalize_len(parse_bracketed_str_list(row.get("channel", "[]")), n, "") if has_channel else []
new_videos: List[str] = []
new_st_ll: List[List[int]] = []
new_et_ll: List[List[int]] = []
new_tod_ll: List[List[int]] = []
new_vehicle: List[int] = []
new_upload: List[int] = []
new_channel: List[str] = []
for i, vid in enumerate(videos):
st_list = st_ll[i] if isinstance(st_ll[i], list) else []
et_list = et_ll[i] if isinstance(et_ll[i], list) else []
tod_list = tod_ll[i] if isinstance(tod_ll[i], list) else []
kept_st: List[int] = []
kept_et: List[int] = []
kept_tod: List[int] = []
# Segment-level pruning (not video-level)
for st, et, tod in zip(st_list, et_list, tod_list):
try:
st_i = int(st)
except Exception:
# Malformed start_time: keep it (safer than dropping incorrectly)
kept_st.append(st)
kept_et.append(et)
kept_tod.append(tod)
kept_segments += 1
continue
if (vid, st_i) in bbox_present:
removed_segments += 1
continue
kept_st.append(int(st))
kept_et.append(int(et) if str(et).lstrip("-").isdigit() else et)
kept_tod.append(int(tod) if str(tod).lstrip("-").isdigit() else tod)
kept_segments += 1
# Keep this video only if it still has at least one remaining segment
if kept_st:
new_videos.append(vid)
new_st_ll.append(kept_st)
new_et_ll.append(kept_et)
new_tod_ll.append(kept_tod)
if has_vehicle:
new_vehicle.append(vehicle[i])
if has_upload:
new_upload.append(upload[i])
if has_channel:
new_channel.append(channel[i])
# DROP THE WHOLE ROW if all videos are already processed (i.e., nothing remains)
if not new_videos:
dropped_rows += 1
if PRINT_REMOVED_CITIES:
removed_cities.append(str(row.get("locality", "")).strip())
continue
out_row = dict(row)
out_row["videos"] = format_str_list_no_quotes(new_videos)
out_row["start_time"] = format_list_of_lists(new_st_ll)
out_row["end_time"] = format_list_of_lists(new_et_ll)
out_row["time_of_day"] = format_list_of_lists(new_tod_ll)
if has_vehicle:
out_row["vehicle_type"] = format_int_list(new_vehicle)
if has_upload:
out_row["upload_date"] = format_int_list(new_upload)
if has_channel:
out_row["channel"] = format_str_list_no_quotes(new_channel)
kept_rows.append(out_row)
out_df = pd.DataFrame(kept_rows, columns=df.columns)
Path(OUTPUT_CSV).parent.mkdir(parents=True, exist_ok=True)
out_df.to_csv(OUTPUT_CSV, index=False, quoting=csv.QUOTE_MINIMAL)
print(f"Wrote: {OUTPUT_CSV}")
print(f"Segments removed (bbox exists): {removed_segments}")
print(f"Segments remaining: {kept_segments}")
print(f"Rows dropped (fully processed): {dropped_rows}")
print(f"Rows kept: {len(out_df)} / {len(df)}")
if PRINT_REMOVED_CITIES:
print("\nCities removed (rows dropped):")
# Print unique cities while preserving first-seen order
seen: Set[str] = set()
for c in removed_cities:
c = c or "(missing locality)"
if c in seen:
continue
seen.add(c)
print(f" - {c}")
print(f"Total removed rows printed: {len(removed_cities)}")
if __name__ == "__main__":
build_remaining()