-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateDifferencestest.py
More file actions
80 lines (62 loc) · 3.64 KB
/
Copy pathcreateDifferencestest.py
File metadata and controls
80 lines (62 loc) · 3.64 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
import os
import pandas as pd
from glob import glob
def create_differences():
# Get the directory where the current script is located (inside the folder just before /collected/)
script_dir = os.path.dirname(os.path.abspath(__file__))
# Define the relative path to trial_number.txt based on the current script directory
trial_path = os.path.join(script_dir, 'trial_number.txt')
# Step 1: Open the file and read the trial number, then set it as the trial variable
with open(trial_path, 'r') as file:
trial = int(file.read()) # Read the trial number from the file and convert it to an integer
# Step 2: Define the path to the folder containing CSV files relative to the script location
folder_path = os.path.join(script_dir, 'collected', str(trial))
# List all CSV files in the folder
csv_files = glob(os.path.join(folder_path, '*.csv'))
# Extract unique movements and planes from the CSV filenames
movements = set()
planes = set()
ids = set()
for file in csv_files:
parts = os.path.basename(file).split('_') # Use os.path.basename to get the file name only
movements.add(parts[2])
planes.add(parts[1])
ids.add(parts[0])
print(f"IDs: {ids}")
print(f"Movements: {movements}")
print(f"Planes: {planes}")
# Create a 'NEW' folder if it doesn't exist
new_folder = os.path.join(folder_path, 'diffs')
os.makedirs(new_folder, exist_ok=True)
print(f"New folder path: {new_folder}")
# Iterate through each movement, plane, and angle
for movement in movements:
for plane in planes:
for angle_column in ['Plane of Elevation', 'Angle of Elevation', 'Rotation', 'Flexion Extension', 'Pronation Supination', 'Carrying Angle']:
# Create an empty DataFrame to store differences for each participant
diff_df = pd.DataFrame()
for id in ids:
# Find CSV files for the specified movement, plane, and devices
matching_files_kinect = [file for file in csv_files if f"{id}_{plane}_{movement}_angles_kinect_processed.csv" in os.path.basename(file)]
matching_files_mm = [file for file in csv_files if f"{id}_{plane}_{movement}_angles_mm_processed.csv" in os.path.basename(file)]
# Skip if no files are found for the current combination
if not matching_files_kinect or not matching_files_mm:
print(f"No files found for id {id}, plane {plane}, movement {movement}")
continue
# Load CSV files for both devices
df_kinect = pd.read_csv(matching_files_kinect[0])
df_mm = pd.read_csv(matching_files_mm[0])
# Calculate the difference for the specific angle
diff = df_mm[angle_column] - df_kinect[angle_column]
# Add the difference to the DataFrame with participant column name
diff_df[f"Participant {id}"] = diff
if not diff_df.empty:
# Save the DataFrame to a new CSV file
diff_filename = f"{plane}_{movement}_{angle_column.replace(' ', '_')}_diff.csv"
diff_filepath = os.path.join(new_folder, diff_filename)
diff_df.to_csv(diff_filepath, index=False)
print(f"Differences saved to {diff_filepath}")
else:
print(f"No data to save for {plane}, {movement}, {angle_column}")
if __name__ == "__main__":
create_differences()