File size: 5,314 Bytes
f4a5df4
 
 
 
 
 
9a08cf2
f4a5df4
 
 
 
 
 
 
ee67f51
 
6c9d7cd
 
 
ee67f51
9a08cf2
 
 
 
 
 
ee67f51
 
 
 
9a08cf2
 
ee67f51
 
9a08cf2
 
 
f4a5df4
 
 
 
9a08cf2
 
 
 
 
 
 
f4a5df4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a08cf2
f4a5df4
 
 
 
 
9a08cf2
f4a5df4
 
 
 
9a08cf2
f4a5df4
9a08cf2
 
ee67f51
 
 
9a08cf2
ee67f51
f4a5df4
 
 
 
 
 
 
9a08cf2
f4a5df4
 
 
 
 
 
9a08cf2
f4a5df4
9a08cf2
f4a5df4
9a08cf2
 
ee67f51
 
 
 
 
 
 
 
 
 
f4a5df4
 
 
 
 
 
 
 
9a08cf2
f4a5df4
 
 
 
 
 
9a08cf2
f4a5df4
9a08cf2
f4a5df4
9a08cf2
f4a5df4
 
9a08cf2
 
ee67f51
 
 
9a08cf2
ee67f51
f4a5df4
 
 
 
 
9a08cf2
 
 
 
 
 
 
 
 
 
 
 
f4a5df4
 
 
9a08cf2
 
a82f905
6c9d7cd
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
# 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")