Spaces:
Sleeping
Sleeping
File size: 19,025 Bytes
ca3e099 |
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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
import gradio as gr
from pipeline_classes import CreateCombinedDataFrame, ScaleXYZData, ExtractFeatures, TrainModel, ClassifyMovementData, LowPassFilter, PCAHandler
from sklearn.pipeline import Pipeline
#from _config import config
import pandas as pd
import numpy as np
import joblib
import json
# Define pipelines
combining_dataframes_pipeline = Pipeline([
#('import_data', ImportData(use_accel=True, use_reports=True, use_combined=False, use_features=False)),
('create_combined_dataframe', CreateCombinedDataFrame(time_window=None, label_columns=None)),
])
feature_extraction_pipeline = Pipeline([
#('import_data', ImportData(use_accel=False, use_reports=False, use_combined=True, use_features=False)),
('low_pass_filter', LowPassFilter(cutoff_frequency=None, sampling_rate=None, order=None)),
('scale_xyz_data', ScaleXYZData(scaler_type=None)),
('extract_features', ExtractFeatures(window_length=None,
window_step_size=None,
data_frequency=None,
selected_domains=None,
include_magnitude=None,
features_label_columns=None)),
])
training_model_pipeline = Pipeline([
#('import_data', ImportData(use_accel=False, use_reports=False, use_combined=False, use_features=True)),
('pca_handler', PCAHandler(apply_pca=None, variance=None)),
('train_model', TrainModel(classifier=None, train_label= None, target=None)),
])
analyzing_data_pipeline = Pipeline([
#('import_data', ImportData(use_accel=True, use_reports=False, use_combined=False, use_features=False)),
('low_pass_filter', LowPassFilter(cutoff_frequency=None, sampling_rate=None, order=None)),
('scale_xyz_data', ScaleXYZData(scaler_type=None)),
('extract_features', ExtractFeatures(window_length=None,
window_step_size=None,
data_frequency=None,
selected_domains=None,
include_magnitude=None,
features_label_columns=None)),
('classify_movement_data', ClassifyMovementData(model_file=None)),
])
complete_training_model_pipeline = Pipeline([
#('import_data', ImportData(use_accel=True, use_reports=True, use_combined=False, use_features=False)),
('create_combined_dataframe', CreateCombinedDataFrame(time_window=None, label_columns=None)),
('low_pass_filter', LowPassFilter(cutoff_frequency=None, sampling_rate=None, order=None)),
('scale_xyz_data', ScaleXYZData(scaler_type=None)),
('extract_features', ExtractFeatures(window_length=None,
window_step_size=None,
data_frequency=None,
selected_domains=None,
include_magnitude=None,
features_label_columns=None)),
('pca_handler', PCAHandler(apply_pca=None, variance=None)),
('train_model', TrainModel(classifier=None, train_label= None, target=None)),
])
def execute_combine_pipeline(accel_file, report_file,
time_window=None, label_columns=None
):
try:
# Load data files only if paths are valid
accel_data = pd.read_csv(accel_file) if accel_file else None
report_data = pd.read_csv(report_file) if report_file else None
# Validate inputs for the selected pipeline
if accel_data is None or report_data is None:
return "Error: Both accelerometer and self-report data files are required for this pipeline.", None
combining_dataframes_pipeline.set_params(
create_combined_dataframe__time_window=time_window,
create_combined_dataframe__label_columns=label_columns.split(','))
X = report_data, accel_data
result = combining_dataframes_pipeline.fit_transform(X)
output_file = "combine_dataframes_output.csv"
result.to_csv(output_file, index=False)
return output_file
except Exception as e:
print(f"Error occurred: {str(e)}")
return str(e), None
def execute_feature_extraction_pipeline(combined_file, cutoff_frequency, order, scaler_type, window_length, window_step_size, data_frequency, include_magnitude, features_label_columns):
try:
combined_data = pd.read_csv(combined_file) if combined_file else None
if combined_data is None:
return "Error: Combined data file is required for this pipeline.", None
feature_extraction_pipeline.set_params(
low_pass_filter__cutoff_frequency=cutoff_frequency,
low_pass_filter__order=order,
low_pass_filter__sampling_rate=data_frequency,
scale_xyz_data__scaler_type=scaler_type,
extract_features__window_length=window_length,
extract_features__window_step_size=window_step_size,
extract_features__data_frequency=data_frequency,
#extract_features__selected_domains=None,
extract_features__include_magnitude=include_magnitude,
extract_features__features_label_columns=features_label_columns.split(','))
result = feature_extraction_pipeline.fit_transform(combined_data)
output_file = "extract_features_output.csv"
result.to_csv(output_file, index=False)
return output_file
except Exception as e:
print(f"Error occurred: {str(e)}")
return str(e)
def execute_training_pipeline(features_file, apply_pca, pca_variance, classifier, train_label, target):
try:
print(f"features_file: {features_file}")
features_data = pd.read_csv(features_file) if features_file else None
if features_data is None:
return "Error: Features data file is required for this pipeline.", None
training_model_pipeline.set_params(
pca_handler__apply_pca=apply_pca,
pca_handler__variance=pca_variance,
train_model__classifier=classifier,
train_model__train_label=train_label,
train_model__target=target)
X = features_data
training_model_pipeline.fit(X)
output_file, secondary_output_file = training_model_pipeline.named_steps['train_model'].get_output_files()
return output_file, secondary_output_file
except Exception as e:
print(f"Error occurred: {str(e)}")
return str(e), None
def execute_analyze_pipeline(accel_file, model_file, cutoff_frequency, order, scaler_type, window_length, data_frequency, include_magnitude, features_label_columns):
try:
accel_data = pd.read_csv(accel_file) if accel_file else None
if accel_data is None:
return "Error: Accelerometer data file is required for this pipeline.", None
analyzing_data_pipeline.set_params(
low_pass_filter__cutoff_frequency=cutoff_frequency,
low_pass_filter__order=order,
low_pass_filter__sampling_rate=data_frequency,
scale_xyz_data__scaler_type=scaler_type,
extract_features__window_length=window_length,
extract_features__window_step_size=window_length,
extract_features__data_frequency=data_frequency,
#extract_features__selected_domains=None,
extract_features__include_magnitude=include_magnitude,
extract_features__features_label_columns=features_label_columns.split(','),
classify_movement_data__model_file=model_file.name
)
result = analyzing_data_pipeline.fit_transform(accel_data)
output_file = "analyze_data_output.csv"
result.to_csv(output_file, index=False)
return output_file
except Exception as e:
print(f"Error occurred: {str(e)}")
return str(e), None
def execute_complete_training_pipeline(accel_file, report_file, time_window, label_columns,
cutoff_frequency, order, scaler_type, window_length, window_step_size, data_frequency, include_magnitude, features_label_columns,
apply_pca, pca_variance, classifier, train_label, target):
try:
accel_data = pd.read_csv(accel_file) if accel_file else None
report_data = pd.read_csv(report_file) if report_file else None
if accel_data is None or report_data is None:
return "Error: Both accelerometer and self-report data files are required for this pipeline.", None
complete_training_model_pipeline.set_params(
create_combined_dataframe__time_window=time_window,
create_combined_dataframe__label_columns=label_columns.split(','),
low_pass_filter__cutoff_frequency=cutoff_frequency,
low_pass_filter__order=order,
low_pass_filter__sampling_rate=data_frequency,
scale_xyz_data__scaler_type=scaler_type,
extract_features__window_length=window_length,
extract_features__window_step_size=window_step_size,
extract_features__data_frequency=data_frequency,
#extract_features__selected_domains=None,
extract_features__include_magnitude=include_magnitude,
extract_features__features_label_columns=label_columns.split(','),
pca_handler__apply_pca=apply_pca,
pca_handler__variance=pca_variance,
train_model__classifier=classifier,
train_model__train_label=label_columns,
train_model__target=target
)
X = report_data, accel_data
complete_training_model_pipeline.fit(X)
output_file, secondary_output_file = complete_training_model_pipeline.named_steps['train_model'].get_output_files()
return output_file, secondary_output_file
except Exception as e:
print(f"Error occurred: {str(e)}")
return str(e), None
# Gradio Blocks Interface
with gr.Blocks() as demo:
with gr.Tabs():
with gr.TabItem("Combine DataFrames"):
accel_file = gr.File(label="Upload Accelerometer Data")
report_file = gr.File(label="Upload Self-Report Data")
time_window = gr.Number(label="Time Window (minutes)", value=2)
label_columns = gr.Textbox(label="Label Columns (comma-separated)", value="valence,arousal")
combine_button = gr.Button("Combine DataFrames")
combine_output = gr.File(label="Download Combined DataFrame")
def combine_dataframes(accel_file, report_file, time_window, label_columns):
output_file = execute_combine_pipeline(accel_file, report_file, time_window, label_columns)
return output_file
combine_button.click(combine_dataframes, inputs=[accel_file, report_file, time_window, label_columns], outputs=combine_output)
with gr.TabItem("Extract Features"):
combined_file = gr.File(label="Upload Combined Data")
cutoff_frequency = gr.Number(label="Cutoff Frequency (Hz)", value=10)
order = gr.Number(label="Order", value=4)
scaler_type = gr.Radio(label="Scaler Type", choices=["standard", "minmax"])
window_length = gr.Number(label="Window Length (seconds)", value=60)
window_step_size = gr.Number(label="Window Step Size (seconds)", value=20)
data_frequency = gr.Number(label="Data Frequency (Hz)", value=25)
#selected_domains= gr.Textbox(label="Only these domains (Comma-Seperated) / If you want all then leave out", value=None)
include_magnitude= gr.Checkbox(label="Include Magnitude", value=True)
features_label_columns= gr.Textbox(label="Label Columns (comma-separated)", value="valence,arousal")
extract_button = gr.Button("Extract Features")
extract_output = gr.File(label="Download Extracted Features")
def extract_features(combined_file, cutoff_frequency, order, scaler_type, window_length, window_step_size, data_frequency, include_magnitude, features_label_columns):
output_file = execute_feature_extraction_pipeline(combined_file,
cutoff_frequency, order, scaler_type, window_length, window_step_size, data_frequency,
include_magnitude, features_label_columns
)
return output_file
extract_button.click(extract_features, inputs=[combined_file, cutoff_frequency, order, scaler_type, window_length, window_step_size,
data_frequency, include_magnitude, features_label_columns], outputs=extract_output)
with gr.TabItem("Train Model"):
features_file = gr.File(label="Upload Features Data")
apply_pca = gr.Checkbox(label="Apply PCA", value=False)
pca_variance = gr.Number(label="PCA Variance", value=0.95)
classifier = gr.Dropdown(label="Classifier", choices=["xgboost", "svm", "randomforest"], value="xgboost")
train_label = gr.Textbox(label="Label Columns (comma-separated)", value="valence,arousal")
target = gr.Textbox(label="Target Label", value="arousal")
train_button = gr.Button("Train Model")
train_output_json = gr.File(label="Download Model PKL")
train_output_pkl = gr.File(label="Download Model JSON")
def train_model(features_file, apply_pca, pca_variance, classifier, train_label, target):
output_file, secondary_output_file = execute_training_pipeline(features_file, apply_pca, pca_variance, classifier, train_label, target)
return output_file, secondary_output_file
train_button.click(train_model, inputs=[features_file, apply_pca, pca_variance, classifier, train_label, target], outputs=[train_output_json, train_output_pkl])
with gr.TabItem("Analyze Data"):
accel_file = gr.File(label="Upload Accelerometer Data")
model_file = gr.File(label="Upload Model")
cutoff_frequency = gr.Number(label="Cutoff Frequency (Hz)", value=10)
order = gr.Number(label="Order", value=4)
scaler_type = gr.Radio(label="Scaler Type", choices=["standard", "minmax"])
window_length = gr.Number(label="Window Length (seconds)", value=60)
data_frequency = gr.Number(label="Data Frequency (Hz)", value=25)
#selected_domains= gr.Textbox(label="Only these domains (Comma-Seperated) / If you want all then leave out", value=None)
include_magnitude= gr.Checkbox(label="Include Magnitude", value=True)
features_label_columns= gr.Textbox(label="Label Columns (comma-separated)", value="valence,arousal")
analyze_button = gr.Button("Analyze Data")
analyze_output = gr.File(label="Download Analyzed Data")
def analyze_data(accel_file, model_file, cutoff_frequency, order, scaler_type, window_length, data_frequency, include_magnitude, features_label_columns):
output_file = execute_analyze_pipeline(accel_file, model_file, cutoff_frequency, order, scaler_type, window_length,
data_frequency, include_magnitude, features_label_columns)
return output_file
analyze_button.click(analyze_data, inputs=[accel_file, model_file, cutoff_frequency, order, scaler_type, window_length,
data_frequency, include_magnitude, features_label_columns ], outputs=analyze_output)
with gr.TabItem("Complete Train Model"):
accel_file = gr.File(label="Upload Accelerometer Data")
report_file = gr.File(label="Upload Self-Report Data")
time_window = gr.Number(label="Time Window (minutes)", value=2)
label_columns = gr.Textbox(label="Label Columns (comma-separated)", value="valence,arousal")
cutoff_frequency = gr.Number(label="Cutoff Frequency (Hz)", value=10)
order = gr.Number(label="Order", value=4)
scaler_type = gr.Radio(label="Scaler Type", choices=["standard", "minmax"])
window_length = gr.Number(label="Window Length (seconds)", value=60)
window_step_size = gr.Number(label="Window Step Size (seconds)", value=20)
data_frequency = gr.Number(label="Data Frequency (Hz)", value=25)
include_magnitude= gr.Checkbox(label="Include Magnitude", value=True)
#features_label_columns= gr.Textbox(label="Label Columns (comma-separated)", value="valence,arousal")
apply_pca = gr.Checkbox(label="Apply PCA", value=False)
pca_variance = gr.Number(label="PCA Variance", value=0.95)
classifier = gr.Dropdown(label="Classifier", choices=["xgboost", "svm", "randomforest"], value="xgboost")
#train_label = gr.Textbox(label="Label Columns (comma-separated)", value="valence,arousal")
target = gr.Textbox(label="Target Label", value="arousal")
complete_train_button = gr.Button("Complete Train Model")
complete_train_output_pkl = gr.File(label="Download Model PKL")
complete_train_output_json = gr.File(label="Download Model JSON")
def complete_train_model(accel_file, report_file, time_window, label_columns,
cutoff_frequency, order, scaler_type, window_length, window_step_size, data_frequency, include_magnitude, features_label_columns,
apply_pca, pca_variance, classifier, train_label, target):
output_file, secondary_output_file = execute_complete_training_pipeline(accel_file, report_file, time_window, label_columns,
cutoff_frequency, order, scaler_type, window_length, window_step_size, data_frequency, include_magnitude, features_label_columns,
apply_pca, pca_variance, classifier, train_label, target)
return output_file, secondary_output_file
complete_train_button.click(complete_train_model, inputs=[accel_file, report_file, time_window, label_columns,
cutoff_frequency, order, scaler_type, window_length, window_step_size, data_frequency, include_magnitude, features_label_columns,
apply_pca, pca_variance, classifier, train_label, target], outputs=[complete_train_output_pkl, complete_train_output_json])
demo.launch() |