test_ind_nonsmokers = np.sort(random.sample(range(num_nonsmokers), int(num_smokers / folds))) train_ind_nonsmokers = np.setdiff1d(np.arange(num_nonsmokers), test_ind_nonsmokers)[0:len(train_ind_smokers)] # collect training features and labels for smokers and training the smoker model training_features_smokers = np.array([]) label_smokers = [] label_smokers_numeric = [] for ixs in train_ind_smokers: spx = 0 # to choose only one recording for each speaker for jxs in [x for x in os.listdir(input_folder_plp + 'Y/' + smokers[ixs]) if x.endswith('.npy')][:-1]: # if spx < 5: plp_features = np.load(input_folder_plp + 'Y/' + smokers[ixs] + '/' + jxs) if not np.isnan(np.sum(plp_features)): plp_mean = np.mean(plp_features[:,0:13],axis=0) plp_std = np.std(plp_features[:,0:13],axis=0,ddof=1) acoustic_features = a2v(np.concatenate((plp_mean,plp_std),axis=0)).T # acoustic_features = a2v(plp_features).T if len(training_features_smokers) == 0: training_features_smokers = acoustic_features else: training_features_smokers = np.append(training_features_smokers,acoustic_features,axis=0) spx += 1 label_smokers.append('Y') label_smokers_numeric.append(1) label_smokers_numeric = np.array(label_smokers_numeric) # collect training features and labels for non-smokers and training the non-smoker model training_features_nonsmokers = np.array([]) label_nonsmokers = [] label_nonsmokers_numeric = []
usecols=['speaker ID']).T.values.tolist()[0]) trn_labels = np.asarray( pd.read_csv(path_training_filelists + training_filelists[itx], usecols=['smoking label']).T.values.tolist()[0]) ####################################################################### training_features_mfcc = np.array([]) trn_label_numeric = [] for rx in range(len(trn_filenames)): feat_file_name = path_mfcc_features + trn_labels[rx] + '/' + str( trn_spk_ids[rx]) + '/' + str( trn_spk_ids[rx]) + '_' + trn_filenames[rx][:-4] + '.npy' if os.path.isfile(feat_file_name): # check if the feature file exists mfcc_features = np.load(feat_file_name) # load MFCCs mfcc_mean = np.mean(mfcc_features, axis=0) # average over frames acoustic_features_mfcc = a2v( mfcc_mean ).T # convert it to vector to be able to append to other features (of other training recordings) if len(training_features_mfcc ) == 0: # when the array is empty (first time appending) training_features_mfcc = acoustic_features_mfcc else: # when the array is not empty training_features_mfcc = np.append(training_features_mfcc, acoustic_features_mfcc, axis=0) if trn_labels[rx] == 'Y': # make numeric labels (Y:smoker --> 1) trn_label_numeric.append(1) elif trn_labels[ rx] == 'N': # make numeric labels (N:nonsmoker --> 2) trn_label_numeric.append(2) else: print('no feature file name -- training -- %d, %d' % (itx, rx))
for ixs in train_ind_smokers: spx = 0 # to choose only one recording for each speaker for jxs in [ x for x in os.listdir(input_folder_mfcc + 'Y/' + smokers[ixs]) if x.endswith('.npy') ][:-1]: # if spx < 5: pros_feat_file_name = input_folder_form + 'Y/' + jxs if os.path.isfile(pros_feat_file_name): formant_features = np.load(input_folder_form + 'Y/' + jxs) if not np.isnan(np.sum(formant_features)): mfcc_features = np.load(input_folder_mfcc + 'Y/' + smokers[ixs] + '/' + jxs) mfcc_features = np.mean(mfcc_features, axis=0) # acoustic_features = a2v(np.concatenate((mfcc_features,formant_features[1:]),axis=0)).T acoustic_features = a2v(mfcc_features).T if len(training_features_smokers) == 0: training_features_smokers = acoustic_features else: training_features_smokers = np.append( training_features_smokers, acoustic_features, axis=0) spx += 1 label_smokers.append('Y') label_smokers_numeric.append(1) label_smokers_numeric = np.array(label_smokers_numeric) # collect training features and labels for non-smokers and training the non-smoker model training_features_nonsmokers = np.array([])
for ixs in train_ind_smokers: spx = 0 # to choose only one recording for each speaker for jxs in [ x for x in os.listdir(input_folder_plp + 'Y/' + smokers[ixs]) if x.endswith('.npy') ][:-1]: # if spx < 5: plp_features = np.load(input_folder_plp + 'Y/' + smokers[ixs] + '/' + jxs) plp_mean = np.mean(plp_features[:, 0:13], axis=0) mfcc_features = np.load(input_folder_mfcc + 'Y/' + smokers[ixs] + '/' + jxs) mfcc_mean = np.mean(mfcc_features, axis=0) # acoustic_features_plp = a2v(np.concatenate((plp_mean,plp_std),axis=0)).T acoustic_features_plp = a2v(plp_mean).T acoustic_features_mfcc = a2v(mfcc_mean).T if len(training_features_smokers_plp) == 0: training_features_smokers_plp = acoustic_features_plp training_features_smokers_mfcc = acoustic_features_mfcc else: training_features_smokers_plp = np.append( training_features_smokers_plp, acoustic_features_plp, axis=0) training_features_smokers_mfcc = np.append( training_features_smokers_mfcc, acoustic_features_mfcc, axis=0) spx += 1 label_smokers.append('Y')
if x.endswith('.npy') ][:-1]: # if spx < 5: pros_feat_file_name = input_folder_form + 'Y/' + jxs if os.path.isfile(pros_feat_file_name): formant_features = np.load(input_folder_form + 'Y/' + jxs) if not np.isnan(np.sum(formant_features)): # pitch_harmonicity = np.load(pros_feat_file_name).T # pitch_harmonicity[pitch_harmonicity == 0] = np.nan # pitch_harmonicity = np.nanmean(pitch_harmonicity, axis=0) # pitch_harmonicity = (pitch_harmonicity - np.tile(np.mean(pitch_harmonicity, axis=0),(pitch_harmonicity.shape[0], 1))) / np.tile(np.std(pitch_harmonicity, axis=0, ddof=1), (pitch_harmonicity.shape[0], 1)) mfcc_features = np.load(input_folder_mfcc + 'Y/' + smokers[ixs] + '/' + jxs) mfcc_features = np.mean(mfcc_features, axis=0) acoustic_features = a2v( np.concatenate((mfcc_features, formant_features), axis=0)).T if len(training_features_smokers) == 0: training_features_smokers = acoustic_features else: training_features_smokers = np.append( training_features_smokers, acoustic_features, axis=0) spx += 1 label_smokers.append('Y') label_smokers_numeric.append(1) label_smokers_numeric = np.array(label_smokers_numeric) toc_feat_s = time.time() print(' Done! ---> Elapsed Time: %2.2f sec.' % (toc_feat_s - tic_feat_s))