Source code for WORC.featureprocessing.FeatureConverter
#!/usr/bin/env python# Copyright 2016-2020 Biomedical Imaging Group Rotterdam, Departments of# Medical Informatics and Radiology, Erasmus MC, Rotterdam, The Netherlands## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.importpandasaspdimportcollectionsimportWORC.addexceptionsasWORCexceptionsimportnumpyasnp# General labels for feature filepanda_labels=['image_type','parameters','feature_values','feature_labels']
[docs]defconvert_pyradiomics_featurevector(featureVector):"""Convert a PyRadiomics feature vector to WORC compatible features."""# Split center of mass features in the three dimensions# Save as orientation featuresif'diagnostics_Mask-original_CenterOfMassIndex'inlist(featureVector.keys()):COM_index=eval(featureVector['diagnostics_Mask-original_CenterOfMassIndex'])featureVector['of_original_COM_Index_x']=COM_index[0]featureVector['of_original_COM_Index_y']=COM_index[1]iflen(COM_index)==3:featureVector['of_original_COM_Index_z']=COM_index[2]if'diagnostics_Mask-original_CenterOfMass'inlist(featureVector.keys()):COM=eval(featureVector['diagnostics_Mask-original_CenterOfMass'])featureVector['of_original_COM_x']=COM[0]featureVector['of_original_COM_y']=COM[1]iflen(COM)==3:featureVector['of_original_COM_z']=COM[2]# Delete all diagnostics features:omitted=['Image','Mask','diagnostics']keys=list(featureVector.keys())forkinkeys:ifany(k.startswith(om)forominomitted):delfeatureVector[k]# Change label to be similar to PREDICTnew_featureVector=collections.OrderedDict()forkinfeatureVector.keys():if'_glcm'ink:kn='tf_'+k.replace('_glcm_','_GLCM_')elif'_gldm'ink:kn='tf_'+k.replace('_gldm_','_GLDM_')elif'_glrlm'ink:kn='tf_'+k.replace('_glrlm_','_GLRLM_')elif'_glszm'ink:kn='tf_'+k.replace('_glszm_','_GLSZM_')elif'_ngtdm'ink:kn='tf_'+k.replace('_ngtdm_','_NGTDM_')elif'_shape'ink:kn='sf_'+kelif'_firstorder'ink:kn='hf_'+kelif'of_'ink:# COMkn=kelse:message=('Key {} is unknown!').format(k)raiseValueError(message)# Add PyRadiomics to the keykn='PyRadiomics_'+kn# Add to new feature Vectornew_featureVector[kn]=featureVector[k]returnnew_featureVector
[docs]defconvert_PREDICT(features,feat_out):""" Convert features from PREDICT toolbox to WORC compatible format. As PREDICT is the WORC default toolbox, we only need to add the name of the toolbox. """# Read input featuresdata=pd.read_hdf(features)# Add name of toolbox to the labelsfeature_labels=data.feature_labelsfeature_labels=['PREDICT_original_'+lforlinfeature_labels]# Convert to pandas Series and save as hdf5panda_data=pd.Series([data.image_type,data.parameters,data.feature_values,feature_labels],index=panda_labels,name='Image features')print(f'Saving image features to {feat_out}.')panda_data.to_hdf(feat_out,'image_features')
[docs]defconvert_pyradiomics(features,feat_out=None):""" Convert features from PyRadiomics toolbox to WORC compatible format. Description: """# Read the csv file and split in objects# TODO: Make sure we can read multiple output typesdata=pd.read_csv(features)keys=list(data.keys())values=np.squeeze(data.values)featureVector={k:vfork,vinzip(keys,values)}# Convert feature vectorfeatureVector=convert_pyradiomics_featurevector(featureVector)# Extract the PyRadiomics parametersparameters=data['diagnostics_Configuration_Settings'][0]# Convert to pandas Series and save as hdf5image_type='None'panda_data=pd.Series([image_type,parameters,list(featureVector.values()),list(featureVector.keys())],index=panda_labels,name='Image features')iffeat_outisnotNone:print(f'Saving image features to {feat_out}.')panda_data.to_hdf(feat_out,'image_features')
[docs]defFeatureConverter(feat_in,toolbox,config,feat_out):""" Convert features as extracted by a third-party toolbox to WORC format. Parameters ---------- feat_in: string Path to input feature file as outputted by the feature extraction toolbox. toolbox: string Name of toolbox from which features are extracted. config: string Path to .ini file containing the configuration for this function. feat_out: string Path to .hdf5 file to which converted features should be saved """# Convert input featuresiftoolbox=='PREDICT':convert_PREDICT(feat_in,feat_out)eliftoolbox=='PyRadiomics':convert_pyradiomics(feat_in,feat_out)else:raiseWORCexceptions.WORCKeyError(f'Toolbox {toolbox} not recognized.')