Esempio n. 1
0
    def accept(self, *args, **kwargs):
        dlg = MultiStepProgressDialog(self)
        #thunkEventHandler = ThunkEventHandler(dlg)
        dlg.setNumberOfSteps(2)
        #Step1: 
        
        roi = sliceToRoi(self.roi,self.input.meta.shape)
        subRegion = OpSubRegion(self.input.getRealOperator())

        subRegion.Start.setValue(tuple([k for k in roi[0]]))
        subRegion.Stop.setValue(tuple([k for k in roi[1]]))
        subRegion.Input.connect(self.input)

        inputVolume = subRegion

        #handle different outputTypes


        if self.normalizationMethod in [1,2]:
            normalizer = OpPixelOperator(self.input.getRealOperator())
            normalizer.Input.connect(inputVolume.Output)
            minVal, maxVal = numpy.nan, numpy.nan

            if self.normalizationMethod == 1:
                inputVolume = normalizer.Output
                minVal,maxVal = self.normalizationValues[0]
                outputMinVal, outputMaxVal = self.normalizationValues[1]
            elif self.normalizationMethod == 2:
                raise Exception("Not Implemented yet")
                
            def normalize(val):
                invVal = 1./(maxVal - minVal)
                return outputMinVal + (val - minVal)  * (outputMaxVal - outputMinVal) * invVal 
            
            normalizer.Function.setValue(normalize)
            inputVolume = normalizer

        outputDtype = self.getOutputDtype()
        if outputDtype is not self.input.meta.dtype:
            converter = OpPixelOperator(self.input.getRealOperator())
            converter.Input.connect(inputVolume.Output)
            
            def convertToType(val):
                return outputDtype(val)
            converter.Function.setValue(convertToType)
            inputVolume = converter

        dlg.finishStep()
        #step 2
        if self.radioButtonStack.isChecked():
            key = self.createKeyForOutputShape()
            
            writer = OpStackWriter(self.input.getRealOperator())
            writer.inputs["input"].connect(self.input)
            writer.inputs["filepath"].setValue(str(self.lineEditFilePath.displayText()))
            writer.inputs["dummy"].setValue(["zt"])
            writer.outputs["WritePNGStack"][key].allocate().wait()

        elif self.radioButtonH5.isChecked():
            h5f = h5py.File(str(self.lineEditFilePath.displayText()), 'w')
            hdf5path = str(self.lineEditHdf5Path.displayText())
   
            writerH5 = OpH5WriterBigDataset(self.input.getRealOperator())
            writerH5.hdf5File.setValue(h5f)
            writerH5.hdf5Path.setValue(hdf5path)
            writerH5.Image.connect(inputVolume.Output)

            self._storageRequest = writerH5.WriteImage[...]

            def handleFinish(result):
                self.finishedStepSignal.emit()
            def handleCancel():
                print "Full volume prediction save CANCELLED."
            def cancelRequest():
                print "Cancelling request"
                self._storageRequest.cancel()
            def onProgressGUI(x):
                print "xxx",x
                dlg.setStepProgress(x)
            def onProgressLazyflow(x):
                self.progressSignal.emit(x)
            
            self.progressSignal.connect(onProgressGUI)
            self.finishedStepSignal.connect(dlg.finishStep)
           
           # Trigger the write and wait for it to complete or cancel.
            self._storageRequest.notify_finished(handleFinish)
            self._storageRequest.notify_cancelled(handleCancel)
            
            dlg.rejected.connect(cancelRequest)
            writerH5.progressSignal.subscribe( onProgressLazyflow )
            self._storageRequest.submit() 
            
            dlg.exec_()
            
            writerH5.cleanUp()
        
        else:
            raise RuntimeError("unhandled button")
        
        return QDialog.accept(self, *args, **kwargs)
Esempio n. 2
0
    def write(self, inputData, slicing, ranges, outputDType, fileWriter="h5"):
        dlg = MultiStepProgressDialog()
        # thunkEventHandler = ThunkEventHandler(dlg)
        dlg.setNumberOfSteps(2)
        # Step1:

        roi = sliceToRoi(slicing, inputData.meta.shape)

        subRegion = OpSubRegion(graph=self.graph)

        subRegion.Start.setValue(tuple([k for k in roi[0]]))
        subRegion.Stop.setValue(tuple([k for k in roi[1]]))
        subRegion.Input.connect(inputData)

        inputVolume = subRegion

        # handle different outputTypes

        if ranges is not None:

            normalizer = OpPixelOperator(graph=self.graph)
            normalizer.Input.connect(inputVolume.Output)

            minVal, maxVal = ranges[0]
            outputMinVal, outputMaxVal = ranges[1]

            def normalize(val):
                frac = numpy.float(outputMaxVal - outputMinVal) / numpy.float(maxVal - minVal)
                return outputDType(outputMinVal + (val - minVal) * frac)

            normalizer.Function.setValue(normalize)
            inputVolume = normalizer

        elif outputDType != inputData.meta.dtype:
            converter = OpPixelOperator(graph=self.graph)

            def convertToType(val):
                return outputDType(val)

            converter.Function.setValue(convertToType)
            converter.Input.connect(inputVolume.Output)
            inputVolume = converter

        dlg.finishStep()
        # step 2
        writer = None
        if fileWriter == "h5":
            writer = self.h5Writer
        elif fileWriter == "stack":
            writer = self.stackWriter

        writer.Image.disconnect()
        writer.Image.connect(inputVolume.Output)
        self._storageRequest = writer.WriteImage[:]

        def handleFinish(result):
            self.finishedStepSignal.emit()

        def handleCancel():
            print "Full volume prediction save CANCELLED."

        def cancelRequest():
            print "Cancelling request"
            self._storageRequest.cancel()

        def onProgressGUI(x):
            print "xxx", x
            dlg.setStepProgress(x)

        def onProgressLazyflow(x):
            self.progressSignal.emit(x)

        self.progressSignal.connect(onProgressGUI)
        self.finishedStepSignal.connect(dlg.finishStep)

        # Trigger the write and wait for it to complete or cancel.
        self._storageRequest.notify_finished(handleFinish)
        self._storageRequest.notify_cancelled(handleCancel)

        dlg.rejected.connect(cancelRequest)
        writer.progressSignal.subscribe(onProgressLazyflow)
        self._storageRequest.submit()

        dlg.exec_()

        writer.cleanUp()
        if self.h5f is not None:
            self.h5f.close()
            self.h5f = None

        return 0