-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpreprocessing.py
More file actions
89 lines (70 loc) · 3.28 KB
/
Copy pathpreprocessing.py
File metadata and controls
89 lines (70 loc) · 3.28 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
81
82
83
84
85
86
87
88
89
# preprocessing.py
from simulation import create_feature_matrix
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import pandas as pd
def prepare_data(n_mice=5, n_trials_per_mouse=40, test_size=0.2, random_state=42):
"""
Prepare data for ML models.
Returns:
X_train_scaled: Scaled training features
X_test_scaled: Scaled test features
y_train: Training labels (1=Correct, 0=Incorrect)
y_test: Test labels
feature_cols: List of feature column names
scaler: Fitted StandardScaler object
"""
# Generate data
feature_matrix, trial_labels = create_feature_matrix(n_mice, n_trials_per_mouse, random_state=random_state)
# Create target
feature_matrix['Correct'] = (feature_matrix['Hit'] + feature_matrix['CR']).astype(int)
# One-hot encode learning stage
# Converts: "Naive", "Learning", "Proficient" → binary columns
# drop_first=True: Uses Naive as reference (encoded as [0,0])
feature_matrix_encoded = pd.get_dummies(feature_matrix,
columns=['Learning_Stage'],
drop_first=True)
# Select features
feature_cols = ['theta_frequency', 'beta_frequency', 'gamma_frequency',
'noise_level', 'Trial_Number',
'Learning_Stage_Naive', 'Learning_Stage_Proficient']
X = feature_matrix_encoded[feature_cols]
y = feature_matrix_encoded['Correct']
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=test_size, random_state=random_state, stratify=y
)
# Scale features
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
return X_train_scaled, X_test_scaled, y_train, y_test, feature_cols, scaler
def prepare_data_neural_only(n_mice=5, n_trials_per_mouse=40, test_size=0.2, random_state=42):
"""
Prepare data using ONLY neural features (no behavioral/trial info).
This is the purest test of whether neural activity predicts behavior.
Returns:
X_train_scaled: Scaled training features (ONLY neural)
X_test_scaled: Scaled test features
y_train: Training labels
y_test: Test labels
feature_cols: List of feature column names
scaler: Fitted StandardScaler object
"""
# Generate data
feature_matrix, trial_labels = create_feature_matrix(n_mice, n_trials_per_mouse, random_state=random_state)
# Create binary target
feature_matrix['Correct'] = (feature_matrix['Hit'] + feature_matrix['CR']).astype(int)
# Select only neural features
feature_cols = ['theta_frequency', 'beta_frequency', 'gamma_frequency', 'noise_level']
X = feature_matrix[feature_cols]
y = feature_matrix['Correct']
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=test_size, random_state=random_state, stratify=y
)
# Scale features
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
return X_train_scaled, X_test_scaled, y_train, y_test, feature_cols, scaler