rnb-radar / builder.py
jingyan wang
update builder scripts
6c9d7cd
# rgb: 2025_06_16_21_27-mastlab-yao-0001
# radardata: 2025_06_16_21_27-mastlab-yao-1400-2D-2-2-0001-076-5.90
# depth: 2025_06_16_21_27-mastlab-yao-0001
#
import os
import polars as pl
import re
def build_table(data_root):
depth_table = build_single_table(data_root, "depthdata", parse_depth_data)
radar_table = build_single_table(data_root, "radardata", parse_radar_data)
rgbdata_table = build_single_table(data_root, "rgbdata", parse_rgbdata_data)
t1 = depth_table.join(
rgbdata_table,
left_on=["id", "index_depth", "sub_index_depth"],
right_on=["id", "index_rgb", "sub_index_rgb"],
).rename({"index_depth": "index_frame", "sub_index_depth": "sub_index_frame"})
assert_columns_equal(t1, "time", "time_right")
t1 = t1.drop("time_right")
print(t1["index_frame"])
print(t1)
# print(t1.columns)
data_table = t1.join(
radar_table,
left_on=["id", "index_frame"],
right_on=["id", "index_radar"],
how="inner",
)
assert_columns_equal(data_table, "time", "time_right")
data_table = data_table.drop("time_right")
return data_table
def assert_columns_equal(df, col1, col2):
"""Assert that two columns have identical values"""
are_equal = (df[col1] == df[col2]).all()
assert are_equal, f"Columns {col1} and {col2} are not identical"
print(f"✓ Columns {col1} and {col2} are identical")
def build_single_table(data_root, source_relatvie_root, parse_func):
data = []
for file_name in os.listdir(os.path.join(data_root, source_relatvie_root)):
if (
file_name.endswith(".npy")
or file_name.endswith(".npz")
or file_name.endswith(".mat")
):
try:
parsed_data = parse_func(source_relatvie_root, file_name)
data.append(parsed_data)
except ValueError as e:
print(f"Error parsing {file_name}: {e}")
return pl.DataFrame(data)
def parse_depth_data(source_relatvie_root, file_name):
"""
769E33E0_2025_06_16_21_27-mastlab-yao-0001-1.npy
"""
base_name = file_name.split(".")[0]
# Parse the base name to extract the relevant information
parts = base_name.split("-")
if len(parts) != 5:
raise ValueError(
"Base name format is incorrect. Expected format: YYYY_MM_DD_HH_MM-<other_info>-<other_info>-<other_info>"
)
id, time_part = split_id_time(parts[0])
return dict(
id=id,
time=time_part,
location_depth=parts[1],
subject_depth=parts[2],
index_depth=int(parts[3]),
sub_index_depth=int(parts[4]),
file_path_depth=os.path.join(
source_relatvie_root, file_name
), # Use the relative path
)
def parse_radar_data(source_relatvie_root, file_name):
"""
769E33E0_2025_06_16_21_27-mastlab-yao-1400-2D-2-2-0001-076-5.90.mat
"""
base_name = file_name.split(".")[0]
# Parse the base name to extract the relevant information
parts = base_name.split("-")
if len(parts) != 10:
raise ValueError(
"Base name format is incorrect. Expected format: XXXXXXXX_YYYY_MM_DD_HH_MM-<other_info>-<other_info>-<other_info>-<other_info>-<other_info>-<other_info>"
)
id, time_part = split_id_time(parts[0])
return dict(
id=id,
time=time_part,
location_radar=parts[1],
subject_radar=parts[2],
length_radar_cube=int(parts[3]),
dimension_radar=parts[4],
window_size_radar=int(parts[5]),
window_stride_radar=int(parts[6]),
index_radar=int(parts[7]),
selected_range_bin=int(parts[8]),
enthrophy=float(parts[9]),
file_path_radar=os.path.join(
source_relatvie_root,
file_name, # Use the relative path
),
)
def parse_rgbdata_data(source_relatvie_root, file_name):
"""
769E33E0_2025_06_16_21_27-mastlab-yao-0001-1.npy
"""
base_name = file_name.split(".")[0]
# Parse the base name to extract the relevant information
parts = base_name.split("-")
if len(parts) != 5:
raise ValueError(
"Base name format is incorrect. Expected format: xxxx_YYYY_MM_DD_HH_MM-<other_info>-<other_info>-<other_info>-<other_info>"
)
id, time_part = split_id_time(parts[0])
return dict(
id=id,
time=time_part,
location_rgb=parts[1],
subject_rgb=parts[2],
index_rgb=int(parts[3]),
sub_index_rgb=int(parts[4]),
file_path_rgb=os.path.join(
source_relatvie_root, file_name
), # Use the relative path
)
def split_id_time(part):
pattern = r"^([0-9A-F]+)_(\d{4}_\d{2}_\d{2}_\d{2}_\d{2})$"
id_part, time_part = part.split("_", 1)
match = re.match(pattern, part)
if match:
id_part = match.group(1)
time_part = match.group(2)
return id_part, time_part
else:
raise ValueError("String format is incorrect")
if __name__ == "__main__":
data_root = os.path.dirname(os.path.abspath(__file__))
data_table = build_table(data_root)
print(data_table)
print(data_table.columns)
# data_table.write_parquet("./train.parquet")
data_table.write_parquet("./train.parquet")