2
2
import os
3
3
import random
4
4
5
+ from re import X
5
6
from typing import List , Tuple , Dict , Optional , Any
6
7
7
8
import sklearn .preprocessing
@@ -202,8 +203,11 @@ def zscore_trial_segs(
202
203
Normalized train data, normalized rest features, and normalization parameters.
203
204
"""
204
205
is_2D = train [0 ].ndim == 1
205
- concat_train = train if is_2D else np .concatenate (train )
206
- train_mean = normparams ['X_train_mean' ] if normparams is not None else bn .nanmean (concat_train , axis = 0 )
206
+ concat_train = train if is_2D else np .concatenate (train ).astype (float )
207
+ train_mean = (
208
+ normparams ['X_train_mean' ] if normparams is not None
209
+ else bn .nanmean (concat_train , axis = 0 )
210
+ )
207
211
train_std = normparams ['X_train_std' ] if normparams is not None else bn .nanstd (concat_train , axis = 0 )
208
212
209
213
train_notnan_cols = train_std != 0
@@ -213,15 +217,17 @@ def zscore_trial_segs(
213
217
# if train is not jagged, it gets converted completely to object
214
218
# np.ndarray. Hence, cannot exclusively use normed_train.loc
215
219
if isinstance (normed_train , pd .DataFrame ):
216
- normed_train = normed_train .loc
217
- normed_train [:, train_nan_cols ] = 0
220
+ normed_train .loc [:, train_nan_cols ] = 0
221
+ else :
222
+ normed_train [:, train_nan_cols ] = 0
218
223
else :
219
224
normed_train = np .empty_like (train )
220
225
for i , nsvstseg in enumerate (train ):
221
226
zscored = np .divide (nsvstseg - train_mean , train_std , where = train_notnan_cols )
222
227
if isinstance (zscored , pd .DataFrame ):
223
- zscored = zscored .loc
224
- zscored [:, train_nan_cols ] = 0
228
+ zscored .loc [:, train_nan_cols ] = 0
229
+ else :
230
+ zscored [:, train_nan_cols ] = 0
225
231
normed_train [i ] = zscored
226
232
227
233
normed_rest_feats = []
@@ -230,16 +236,18 @@ def zscore_trial_segs(
230
236
if is_2D :
231
237
normed_feats = np .divide (feats - train_mean , train_std , where = train_notnan_cols )
232
238
if isinstance (normed_feats , pd .DataFrame ):
233
- normed_feats = normed_feats .loc
234
- normed_feats [:, train_nan_cols ] = 0
239
+ normed_feats .loc [:, train_nan_cols ] = 0
240
+ else :
241
+ normed_feats [:, train_nan_cols ] = 0
235
242
normed_rest_feats .append (normed_feats )
236
243
else :
237
244
normed_feats = np .empty_like (feats )
238
245
for i , trialSegROI in enumerate (feats ):
239
246
zscored = np .divide (feats [i ]- train_mean , train_std , where = train_notnan_cols )
240
247
if isinstance (zscored , pd .DataFrame ):
241
- zscored = zscored .loc
242
- zscored [:, train_nan_cols ] = 0
248
+ zscored .loc [:, train_nan_cols ] = 0
249
+ else :
250
+ zscored [:, train_nan_cols ] = 0
243
251
normed_feats [i ] = zscored
244
252
normed_rest_feats .append (normed_feats )
245
253
@@ -351,6 +359,13 @@ def minibatchify(
351
359
"""
352
360
g_seed = torch .Generator ()
353
361
g_seed .manual_seed (seed )
362
+ if Xtrain .ndim == 2 : # handle object arrays
363
+ Xtrain = Xtrain .astype (np .float32 )
364
+ Xval = Xval .astype (np .float32 )
365
+ Xtest = Xtest .astype (np .float32 )
366
+ ytrain = ytrain .astype (np .float32 )
367
+ yval = yval .astype (np .float32 )
368
+ ytest = ytest .astype (np .float32 )
354
369
train = torch .utils .data .TensorDataset (
355
370
torch .from_numpy (Xtrain ).type (torch .float32 ),
356
371
torch .from_numpy (ytrain ).type (torch .float32 ))
0 commit comments