def preprocesscombineddata(trainingdata): features = [] labels = [] numsamplespersubject…
- User
def preprocess_combined_data(training_data): features = [] labels = [] num_samples_per_subject = [] number_of_subject = 0 labels_per_subject = [] for key, file_contents_list in training_data.items(): activity_name, data_type_folder = key for file_contents in file_contents_list: rows = file_contents.strip().split('\n')[5:] # Skip the first three lines for row in rows: row_values = row.split(',') # Assuming comma-separated values # Extract features (excluding first two columns) and labels features_row = [] for val in row_values[2:]: if val.strip(): # Check if the value is not an empty string features_row.append(float(val)) if any(feature != 0.0 for feature in features_row): # Check if any non-zero value features.append(features_row) labels.append(activity_name) # Using activity_name as the label
labels_row = activity_name # Using activity_name as the label # Append features and labels to the respective lists features.append(features_row) labels.append(labels_row) num_samples_per_subject.append(int(len(rows))) labels_per_subject.append(labels_row) # Convert features to numpy array features_array = np.array(features) # Find columns with all zeros non_zero_columns = np.any(features_array != 0, axis=0) # Filter features and labels based on non-zero columns filtered_features = features_array[:, non_zero_columns] filtered_labels = np.array(labels_per_subject) labels = np.array(labels) return filtered_features, labels, num_samples_per_subject - Assistant
- User
keep it as before but solve this: local variable 'labels_row' referenced before assignment
- Assistant
- User
need to print the lenght of the data
- Assistant
- User
def fuse_sensor_data(imu_data, emg_data): combined_data = {}
# Extract unique activity names from both datasets while ignoring the second element of the tuple imu_activity_names = {key[0] for key in imu_data.keys()} emg_activity_names = {key[0] for key in emg_data.keys()} # Combine the unique activity names all_activities = imu_activity_names.union(emg_activity_names) print(f"Total unique activities to merge: {len(all_activities)}") # Should now correctly show the count for activity in all_activities: imu_features = imu_data.get((activity, 'IMU_Data'), []) # Get IMU features or an empty list emg_features = emg_data.get((activity, 'EMG_Data'), []) # Get EMG features or an empty list print(f'Activity: {activity} | IMU Features: {len(imu_features)} | EMG Features: {len(emg_features)}') # Combine features for the activity combined_activity_features = [] # Check if there are EMG samples if emg_features: print(f"Processing {len(emg_features)} EMG samples for activity '{activity}'") # Concatenate features where lengths match for imu_sample in imu_features: for emg_sample in emg_features: print(f'Checking IMU Sample Length: {len(imu_sample)} | EMG Sample Length: {len(emg_sample)}') # Combine features only if they have the same length if len(imu_sample) == len(emg_sample): combined_sample = imu_sample + emg_sample # Concatenating the feature vectors print(f'Combined Sample Length: {(combined_sample)}') combined_activity_features.append(combined_sample) if not combined_activity_features: print(f"No matching samples found for activity '{activity}'") # Store combined data, ensuring the key is correctly formatted combined_data[(activity, 'Combined_Data')] = combined_activity_features print(f"Length of combined data: {(combined_data)}") # Print length after filtering return combined_data normaly Emg data and imu data not the same lenght in each sample - Assistant