-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabels_processing_example.py
More file actions
78 lines (62 loc) · 2.52 KB
/
labels_processing_example.py
File metadata and controls
78 lines (62 loc) · 2.52 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
#
# Copyright 2020-present by A. Mathis Group and contributors. All rights reserved.
#
# This project and all its files are licensed under GNU AGPLv3 or later version.
# A copy is included in dlc2action/LICENSE.AGPL.
#
# Process OFT and EPM labels CSV files to split them into individual files based on the 'DLCFile' column.
import os
import re
import pandas as pd
def split_annotations_by_column(
csv_path,
group_column="DLCFile",
data_suffix="DeepCut_resnet50_Blockcourse1May9shuffle1_1030000.csv",
output_dir="split_annotations",
):
"""
Split a master CSV file into separate files grouped by a specified column.
Parameters:
-----------
csv_path : str
Path to the master CSV file
group_column : str
Column name to group by (default: "DLCFile")
data_suffix : str
Suffix to remove from the grouped column value when naming output files
output_dir : str
Directory to save the output files (will be created if it doesn't exist)
"""
# Create output directory if it doesn't exist
if not os.path.exists(output_dir):
os.makedirs(output_dir)
print(f"Created directory: {output_dir}")
# Read the CSV file (with semicolon separator)
df = pd.read_csv(csv_path, sep=";")
# Get unique values in the group column
unique_values = df[group_column].unique()
print(f"Found {len(unique_values)} unique {group_column} values")
# Process each unique value
for value in unique_values:
# Extract the subset of data for this value
subset = df[df[group_column] == value]
# Create a clean filename by removing the data_suffix
clean_name = value
if data_suffix and clean_name.endswith(data_suffix):
clean_name = clean_name[: -len(data_suffix)]
# Remove any remaining special characters for the filename
clean_name = re.sub(r"[^\w\-]", "", clean_name)
# Create the output path
output_path = os.path.join(output_dir, f"{clean_name}.csv")
# Save the subset to a new CSV file
subset.to_csv(output_path, sep=",", index=False)
print(f"Saved {len(subset)} rows to {output_path}")
print(f"Split complete! {len(unique_values)} files created in {output_dir}")
# Example usage
if __name__ == "__main__":
split_annotations_by_column(
csv_path="path/to/AllLabDataOFT_final.csv",
group_column="DLCFile",
data_suffix="DeepCut_resnet50_Blockcourse1May9shuffle1_1030000.csv",
output_dir="examples/OFT/OFT/Labels/",
)