importpandasaspdimportosfromWORC.addexceptionsimportWORCKeyError# All standard texture features acceptedtexture_features=['GLCM','GLDZM','GLRLM','GLSZM','NGLDM','NGTDM']
[docs]defconvert_radiomix_features(input_file,output_folder):''' Convert .xlsx from RadiomiX to WORC compatible .hdf5 format Input: -------------- input_file: .xlsx in which the feature are stored. output_folder: folder in which features are stored '''print('Converting .xlsx from RadiomiX to WORC compatible .hdf5 format...')# Check if output folder exists: otherwise createifnotos.path.exists(output_folder):os.mkdir(output_folder)# Read the input file and extract relevant fieldsf=pd.read_excel(input_file)pids=f.values[:,4]segs=f.values[:,5]features=f.values[:,10:]# Read the feature labels, and rename them according to the group they belong tofeature_labels=list(f.keys()[10:])foriinrange(0,len(feature_labels)):l=feature_labels[i]ifany(l.startswith(j)forjintexture_features):# Texture featurefeature_labels[i]='tf_'+'RadiomiX_'+lelifany(l.startswith(j)forjin['IH_','Stats_']):# Histogram featurefeature_labels[i]='hf_'+'RadiomiX_'+lelifl.startswith('Shape_'):# Shape featurefeature_labels[i]='sf_'+'RadiomiX_'+lelifl.startswith('LoG_'):# LoG featurefeature_labels[i]='logf_'+'RadiomiX_'+lelifl.startswith('Fractal_'):# Fractal featurefeature_labels[i]='fracf_'+'RadiomiX_'+lelifl.startswith('LocInt_'):# Location featurefeature_labels[i]='locf_'+'RadiomiX_'+lelifl.startswith('RGRD_'):# RGRD featurefeature_labels[i]='rgrdf_'+'RadiomiX_'+lelifl.startswith('Wavelet_'):# RGRD featurefeature_labels[i]='waveletf_'+'RadiomiX_'+lelse:raiseWORCKeyError(f'Unknown feature {l}.')# Initiate labels for pandas filepanda_labels=['feature_values','feature_labels']# For each patient, convert featuresfori_patientinrange(0,len(pids)):feature_values=features[i_patient,:].tolist()# Make an output folder per patient, remove invalid symbols.output=pids[i_patient]+segs[i_patient]output=output.replace(' ','_')output=output.replace('(','_')output=output.replace(')','_')output=os.path.join(output_folder,output)# Check if output folder exists: otherwise createifnotos.path.exists(output):os.mkdir(output)output=os.path.join(output,'features.hdf5')print(f'\t Writing {output}')# Convert to pandas Series and save as hdf5panda_data=pd.Series([feature_values,feature_labels],index=panda_labels,name='Image features')# Save the features to the .hdf5 fileprint('\t Saving image features')panda_data.to_hdf(output,'image_features')