#!/usr/bin/env python# Copyright 2016-2019 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.importWORC.addexceptionsasWORCexceptionsimportfastrfromfastr.apiimportResourceLimitimportos
[docs]def__init__(self,images=None,segmentations=None,network=None,fastr_plugin='ProcessPoolExecution',name='Example'):''' Build a network that evaluates the performance of an estimator. Parameters ---------- network: fastr network, default None If you input a network, the evaluate network is added to the existing network. '''ifnetworkisnotNone:self.network=networkself.mode='WORC'else:self.mode='StandAlone'self.fastr_plugin=fastr_pluginself.name='WORC_Slicer_'+nameself.network=fastr.create_network(id=self.name)self.fastr_tmpdir=os.path.join(fastr.config.mounts['tmp'],self.name)ifimagesisNoneandself.mode=='StandAlone':message='Either images and segmentations as input or a WORC'+\
'network is required for the Evaluate network.'raiseWORCexceptions.WORCIOError(message)self.images=imagesself.segmentations=segmentationsself.create_network()
[docs]defcreate_network(self):''' Add evaluate components to network. '''# Create all nodesself.node_slicer=\
self.network.create_node('worc/Slicer:1.0',tool_version='1.0',id='Slicer',resources=ResourceLimit(memory='20G'))# Create sinksself.sink_PNG=\
self.network.create_sink('PNGFile',id='PNG')self.sink_PNGZoomed=\
self.network.create_sink('PNGFile',id='PNGZoomed')# Create links to sinksself.sink_PNG.input=self.node_slicer.outputs['out']self.sink_PNGZoomed.input=self.node_slicer.outputs['outzoom']# Create sources if not supplied by a WORC networkifself.mode=='StandAlone':self.source_images=self.network.create_source('ITKImageFile',id='Images')self.source_segmentations=self.network.create_source('ITKImageFile',id='Segmentations')# Create links to sources that are not supplied by a WORC network# Not needed in this network# Create links to the sources that could be in a WORC networkifself.mode=='StandAlone':# Sources from the Evaluate network are usedself.node_slicer.inputs['image']=self.source_images.outputself.node_slicer.inputs['segmentation']=self.source_segmentations.outputelse:# Sources from the WORC network are usedprint('WIP')
[docs]defset(self,images=None,segmentations=None,sink_data={}):''' Set the sources and sinks based on the provided attributes. '''ifself.mode=='StandAlone':self.source_data=dict()self.sink_data=dict()self.source_data['Images']=self.imagesself.source_data['Segmentations']=self.segmentationsif'PNG'notinsink_data.keys():self.sink_data['PNG']=("vfs://output/{}/Slice_{{sample_id}}_{{cardinality}}{{ext}}").format(self.name)if'PNGZoomed'notinsink_data.keys():self.sink_data['PNGZoomed']=("vfs://output/{}/Slice_{{sample_id}}_{{cardinality}}_zoomed{{ext}}").format(self.name)else:print('[WORC Warning] Slicer set attribute not needed when WORC network is provided!')
[docs]defexecute(self):""" Execute the network through the fastr.network.execute command. """# Draw and execute nwtworkself.network.draw(file_path=self.network.id+'.svg',draw_dimensions=True)self.network.execute(self.source_data,self.sink_data,execution_plugin=self.fastr_plugin,tmpdir=self.fastr_tmpdir)