import pandas as pd import numpy as np from concurrent.futures import ThreadPoolExecutor max_timesteps=299 feature_num=16 label_nums=9 def generate_partial_sequences(row, max_timesteps=max_timesteps, features_per_timestep=feature_num, fill_value=-10): # 确定实际长度 actual_length_indices = np.where(row[:-label_nums] != fill_value)[0] # 排除最后的标签列 if len(actual_length_indices) > 0: actual_length = (actual_length_indices[-1] // features_per_timestep) + 1 else: actual_length = 0 partial_sequences = [] # step_size = max(1, int(actual_length * 0.1)) # 步长为实际长度的10%,至少为1 step_size =5 for end_length in range(step_size, actual_length + step_size, step_size): # 计算结束点,不超过实际长度 end_length = min(end_length, actual_length) partial_sequence_list = row[:end_length * features_per_timestep].tolist() selected_features_list = [partial_sequence_list[i:i + 10] for i in range(0, len(partial_sequence_list), features_per_timestep)] selected_features_list = [item for sublist in selected_features_list for item in sublist] ProgressOfTask= end_length/actual_length hand_rotation_axis = partial_sequence_list[-6:-3] hand_direction = partial_sequence_list[-3:] padding_length = (max_timesteps - end_length) * (features_per_timestep-6) # 计算填充长度 selected_features_list.extend([fill_value] * padding_length) # 添加填充 selected_features_list.extend(row[-9:]) # 添加标签 selected_features_list.extend(hand_rotation_axis) # 添加HandRotationAxis和HandDirection selected_features_list.extend(hand_direction) selected_features_list.append(ProgressOfTask) # 添加ProgressOfTask partial_sequences.append(selected_features_list) return partial_sequences def process_row(index, df): """Wrapper function to handle the DataFrame row.""" row=df.iloc[index] return generate_partial_sequences(row) def main(df, num_threads=20): """Process the DataFrame using multiple threads.""" with ThreadPoolExecutor(max_workers=num_threads) as executor: # 创建一个future列表,对每一行数据并行应用 process_row 函数 futures = [executor.submit(process_row, index, df) for index in range(len(df))] # 使用 as_completed 来获取已完成的future结果 results = [] for future in futures: results.extend(future.result()) # 将结果转换为 DataFrame columns = df.columns # 选择不以'HandRotationAxis'和'HandDirection'开头的列 columns_to_keep = [column for column in columns if not column.startswith('HandRotationAxis') and not column.startswith('HandDirection')] # 现在添加具体的旋转轴和方向列到列表的末尾 # 如果有特定的顺序要求,这里按照特定顺序添加 columns_to_keep.extend([ 'HandRotationAxis_X', 'HandRotationAxis_Y', 'HandRotationAxis_Z', 'HandDirection_X', 'HandDirection_Y', 'HandDirection_Z', 'ProgressOfTask' ]) #print(len(columns_to_keep)) partial_sequences_df = pd.DataFrame(results, columns=columns_to_keep) return partial_sequences_df if __name__ == '__main__': #加载数据集 for i in range(79, 80): # if i ==3 or i ==6 or i ==15 or i ==19 or i== 22: # continue file_path = f'../Data/Study2Evaluation/Supervised/{i}_train_data_preprocessed_evaluation.csv' df = pd.read_csv(file_path) partial_sequences_df = main(df) save_path_csv = f'../Data/Study2Evaluation/Dataset/{i}_traindataset.csv' partial_sequences_df.to_csv(save_path_csv, index=False) file_path = f'../Data/Study2Evaluation/Supervised/{i}_test_data_preprocessed_evaluation.csv' df = pd.read_csv(file_path) partial_sequences_df = main(df) save_path_csv = f'../Data/Study2Evaluation/Dataset/{i}_testdataset.csv' partial_sequences_df.to_csv(save_path_csv, index=False)