Source code for WORC.exampledata.create_example_data
#!/usr/bin/env python# Copyright 2016-2023 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.importpandasaspdimportnumpyasnpimportosimportSimpleITKassitkcurrentdir=os.path.dirname(os.path.realpath(__file__))
[docs]defcreate_random_features(n_objects=7,n_features=10):""" Create n_objects sets of random features and save in files. Format based on PREDICT python package. """# Create some input values for all objectsfeature_labels=[f'rf_randomlabel_{i}'foriinrange(n_features)]image_type='None'parameters={'Random':'True'}panda_labels=['image_type','parameters','feature_values','feature_labels']foriinrange(n_objects):# Create output name and random feature values and labelsifi<float(n_objects)/2.0:feature_values=[np.random.normal(loc=5.0,scale=2.0)foriinrange(n_features)]else:feature_values=[np.random.normal(loc=10.0,scale=2.0)foriinrange(n_features)]output=os.path.join(currentdir,f'examplefeatures_Patient-{str(i).zfill(3)}.hdf5')# Convert to pandas Series and save as hdf5panda_data=pd.Series([image_type,parameters,feature_values,feature_labels],index=panda_labels,name='Image features')print(f'Saving image features for object {i}.')panda_data.to_hdf(output,'image_features')
[docs]defcreate_random_imageandmask(size=512,slices=20):# Create the imageimage=np.rand.rand((size,size,slices))# Create a sphere mask in the centerra=range(-int(slices/2),int(slices/2)+1)x,y,z=np.meshgrid(ra,ra,ra)radius=np.sqrt(x**2+y**2+z**2)mask=radius.astype(int)==int(size/4)# Convert the images to ITK objectsimage=sitk.GetImageFromArray(image.astype(np.float32))mask=sitk.GetImageFromArray(mask.astype(int))returnimage,mask