예제 #1
0
 def process(self, _edObject=None):
     EDPluginControl.process(self)
     self.DEBUG("EDPluginControlGridScreeningv1_0.process")
     xsDataInputReadImageHeader = XSDataInputReadImageHeader()
     xsDataInputReadImageHeader.setImage(image=XSDataFile(path=XSDataString(self.strImageFile)))
     self.edPluginControlReadImageHeader.setDataInput(xsDataInputReadImageHeader)
     self.edPluginControlReadImageHeader.connectSUCCESS(self.doSuccessReadImageHeader)
     self.edPluginControlReadImageHeader.connectFAILURE(self.doFailureReadImageHeader)
     self.executePluginSynchronous(self.edPluginControlReadImageHeader)
 def process(self, _edObject=None):
     EDPluginControl.process(self)
     self.DEBUG("EDPluginControlGridScreeningv1_0.process")
     xsDataInputReadImageHeader = XSDataInputReadImageHeader()
     xsDataInputReadImageHeader.setImage(image=XSDataFile(path=XSDataString(self.strImageFile)))
     self.edPluginControlReadImageHeader.setDataInput(xsDataInputReadImageHeader)
     self.edPluginControlReadImageHeader.connectSUCCESS(self.doSuccessReadImageHeader)
     self.edPluginControlReadImageHeader.connectFAILURE(self.doFailureReadImageHeader)
     self.executePluginSynchronous(self.edPluginControlReadImageHeader)
예제 #3
0
    def process(self, _edObject=None):
        EDPluginControl.process(self)
        self.DEBUG("EDPluginControlDozorv1_0.process")
        xsDataResultControlDozor = XSDataResultControlDozor()
        for xsDataFile in self.dataInput.image:
            edPluginControlReadImageHeader = self.loadPlugin(self.strEDPluginControlReadImageHeaderName)
            xsDataInputReadImageHeader = XSDataInputReadImageHeader()
            xsDataInputReadImageHeader.image = xsDataFile
            edPluginControlReadImageHeader.dataInput = xsDataInputReadImageHeader
            edPluginControlReadImageHeader.executeSynchronous()
            subWedge = edPluginControlReadImageHeader.dataOutput.subWedge
            xsDataInputDozor = XSDataInputDozor()
            beam = subWedge.experimentalCondition.beam
            detector = subWedge.experimentalCondition.detector
            goniostat = subWedge.experimentalCondition.goniostat
            xsDataInputDozor.detectorType = detector.type
            xsDataInputDozor.exposureTime = XSDataDouble(beam.exposureTime.value)
            xsDataInputDozor.spotSize = XSDataDouble(5.0)
            xsDataInputDozor.detectorDistance = XSDataDouble(detector.distance.value)
            xsDataInputDozor.wavelength = XSDataDouble(beam.wavelength.value)
#            xsDataInputDozor.fractionPolatization : XSDataDouble optional
            orgx = detector.beamPositionY.value / detector.pixelSizeY.value
            orgy = detector.beamPositionX.value / detector.pixelSizeX.value
            xsDataInputDozor.orgx = XSDataDouble(orgx)
            xsDataInputDozor.orgy = XSDataDouble(orgy)
            xsDataInputDozor.oscillationRange = XSDataDouble(goniostat.oscillationWidth.value)
#            xsDataInputDozor.imageStep : XSDataDouble optional
            xsDataInputDozor.startingAngle = XSDataDouble(goniostat.rotationAxisStart.value)
            xsDataInputDozor.firstImageNumber = subWedge.image[0].number
            xsDataInputDozor.numberImages = XSDataInteger(1)
            strFileName = subWedge.image[0].path.value
            strPrefix = EDUtilsImage.getPrefix(strFileName)
            strSuffix = EDUtilsImage.getSuffix(strFileName)
            strXDSTemplate = "%s_????.%s" % (strPrefix, strSuffix)
            xsDataInputDozor.nameTemplateImage = XSDataString(os.path.join(os.path.dirname(strFileName), strXDSTemplate))
            edPluginDozor = self.loadPlugin(self.strEDPluginDozorName, "Dozor")
            edPluginDozor.dataInput = xsDataInputDozor
            edPluginDozor.executeSynchronous()
            if edPluginDozor.dataOutput.imageDozor != []:
                xsDataResultDozor = edPluginDozor.dataOutput.imageDozor[0]
                xsDataControlImageDozor = XSDataControlImageDozor()
                xsDataControlImageDozor.image = xsDataFile
                xsDataControlImageDozor.spots_num_of = xsDataResultDozor.spots_num_of
                xsDataControlImageDozor.spots_int_aver = xsDataResultDozor.spots_int_aver
                xsDataControlImageDozor.spots_resolution = xsDataResultDozor.spots_resolution
                xsDataControlImageDozor.powder_wilson_scale = xsDataResultDozor.powder_wilson_scale
                xsDataControlImageDozor.powder_wilson_bfactor = xsDataResultDozor.powder_wilson_bfactor
                xsDataControlImageDozor.powder_wilson_resolution = xsDataResultDozor.powder_wilson_resolution
                xsDataControlImageDozor.powder_wilson_correlation = xsDataResultDozor.powder_wilson_correlation
                xsDataControlImageDozor.powder_wilson_rfactor = xsDataResultDozor.powder_wilson_rfactor
                xsDataControlImageDozor.score = xsDataResultDozor.score
                xsDataResultControlDozor.addImageDozor(xsDataControlImageDozor)
        self.dataOutput = xsDataResultControlDozor
    def process(self, _edObject=None):
        EDPluginControl.process(self)
        self.DEBUG("EDPluginControlBackground3Dv1_0.process")
        xsDataResultControlBackground3D = XSDataResultControlBackground3D()
        for xsDataFile in self.dataInput.image:
            edPluginControlReadImageHeader = self.loadPlugin(self.strEDPluginControlReadImageHeaderName)
            xsDataInputReadImageHeader = XSDataInputReadImageHeader()
            xsDataInputReadImageHeader.image = xsDataFile
            edPluginControlReadImageHeader.dataInput = xsDataInputReadImageHeader
            edPluginControlReadImageHeader.executeSynchronous()
            subWedge = edPluginControlReadImageHeader.dataOutput.subWedge
            xsDataInputBackground3D = XSDataInputBackground3D()
            beam = subWedge.experimentalCondition.beam
            detector = subWedge.experimentalCondition.detector
            goniostat = subWedge.experimentalCondition.goniostat
            xsDataInputBackground3D.detectorType = detector.type
            xsDataInputBackground3D.exposureTime = XSDataDouble(beam.exposureTime.value)
            xsDataInputBackground3D.detectorDistance = XSDataDouble(detector.distance.value)
            xsDataInputBackground3D.wavelength = XSDataDouble(beam.wavelength.value)
#            xsDataInputBackground3D.fractionPolatization : XSDataDouble optional
            orgx = detector.beamPositionY.value / detector.pixelSizeY.value
            orgy = detector.beamPositionX.value / detector.pixelSizeX.value
            xsDataInputBackground3D.orgx = XSDataDouble(orgx)
            xsDataInputBackground3D.orgy = XSDataDouble(orgy)
            xsDataInputBackground3D.oscillationRange = XSDataDouble(goniostat.oscillationWidth.value)
#            xsDataInputBackground3D.imageStep : XSDataDouble optional
            xsDataInputBackground3D.startingAngle = XSDataDouble(goniostat.rotationAxisStart.value)
            xsDataInputBackground3D.firstImageNumber = subWedge.image[0].number
            xsDataInputBackground3D.numberImages = XSDataInteger(1)
            strFileName = subWedge.image[0].path.value
            strPrefix = EDUtilsImage.getPrefix(strFileName)
            strSuffix = EDUtilsImage.getSuffix(strFileName)
            strXDSTemplate = "%s_????.%s" % (strPrefix, strSuffix)
            xsDataInputBackground3D.nameTemplateImage = XSDataString(os.path.join(os.path.dirname(strFileName), strXDSTemplate))
            edPluginBackground3D = self.loadPlugin(self.strEDPluginBackground3DName, "Background3D")
            edPluginBackground3D.dataInput = xsDataInputBackground3D
            edPluginBackground3D.executeSynchronous()
            if edPluginBackground3D.dataOutput.imageBackground != []:
                xsDataResultBackground3D = edPluginBackground3D.dataOutput.imageBackground[0]
                xsDataControlImageBackground3D = XSDataControlImageBackground3D()
                xsDataControlImageBackground3D.image = xsDataFile
                xsDataControlImageBackground3D.scale = xsDataResultBackground3D.scale
                xsDataControlImageBackground3D.bfactor = xsDataResultBackground3D.bfactor
                xsDataControlImageBackground3D.resolution = xsDataResultBackground3D.resolution
                xsDataControlImageBackground3D.correlation = xsDataResultBackground3D.correlation
                xsDataControlImageBackground3D.rfactor = xsDataResultBackground3D.rfactor
                xsDataControlImageBackground3D.b_coef = xsDataResultBackground3D.b_coef
                xsDataControlImageBackground3D.b_cryst = xsDataResultBackground3D.b_cryst
                xsDataControlImageBackground3D.estimate = xsDataResultBackground3D.estimate
                xsDataResultControlBackground3D.addImageBackground(xsDataControlImageBackground3D)
        self.dataOutput = xsDataResultControlBackground3D
 def createDataCollectionFromImageHeaders(self, _edPlugin):
     """
     This method creates a list of XSDataSubwedges by reading the header of the images
     given as a list of paths as input.
     """
     self.DEBUG(
         "EDPluginControlSubWedgeAssemblev1_1.createDataCollectionFromImageHeaders"
     )
     xsDataInputSubWedgeAssemble = _edPlugin.getDataInput()
     listXSDataFile = xsDataInputSubWedgeAssemble.getFile()
     if (len(listXSDataFile) > 0):
         listSubWedge = []
         iIndex = 1
         for xsDataFile in listXSDataFile:
             xsDataFileCopy = XSDataFile.parseString(xsDataFile.marshal())
             xsDataInputReadImageHeader = XSDataInputReadImageHeader()
             xsDataInputReadImageHeader.setImage(xsDataFileCopy)
             self.__edPluginReadImageHeader = self.loadPlugin(
                 self.__strPluginReadImageHeaderName,
                 "ReadImageHeader_%d" % (iIndex))
             self.__edPluginReadImageHeader.setDataInput(
                 xsDataInputReadImageHeader)
             self.__edPluginReadImageHeader.executeSynchronous()
             xsDataResultReadImageHeader = self.__edPluginReadImageHeader.getDataOutput(
             )
             bSuccess = False
             if (xsDataResultReadImageHeader is not None):
                 xsDataSubWedge = xsDataResultReadImageHeader.getSubWedge()
                 if (xsDataSubWedge is not None):
                     xsDataSubWedge.setSubWedgeNumber(XSDataInteger(iIndex))
                     listSubWedge.append(xsDataSubWedge)
                     bSuccess = True
             if (not bSuccess):
                 # Fix for bug #223: raise an error if an image could not be read
                 strErrorMessage = "EDPluginControlSubWedgeAssemblev1_1.createDataCollectionFromImageHeaders: %s %s" % (
                                     self.__strPluginReadImageHeaderName, \
                                     "Could not read header from image %s" % xsDataFileCopy.getPath().getValue())
                 self.error(strErrorMessage)
                 self.addErrorMessage(strErrorMessage)
                 self.setFailure()
             iIndex += 1
         if (not self.isFailure()):
             xsDataInputSubWedgeMerge = XSDataInputSubWedgeMerge()
             for xsDataSubWedge in listSubWedge:
                 xsDataInputSubWedgeMerge.addSubWedge(xsDataSubWedge)
             self.__edPluginSubWedgeMerge.setDataInput(
                 xsDataInputSubWedgeMerge)
             self.__edPluginSubWedgeMerge.executeSynchronous()
 def createDataCollectionFromImageHeaders(self, _edPlugin):
     """
     This method creates a list of XSDataSubwedges by reading the header of the images
     given as a list of paths as input.
     """
     EDVerbose.DEBUG("EDPluginControlSubWedgeAssemblev1_1.createDataCollectionFromImageHeaders")
     xsDataInputSubWedgeAssemble = _edPlugin.getDataInput()
     listXSDataFile = xsDataInputSubWedgeAssemble.getFile()
     if (len(listXSDataFile) > 0):
         listSubWedge = []
         iIndex = 1
         for xsDataFile in listXSDataFile:
             xsDataFileCopy = XSDataFile.parseString(xsDataFile.marshal())
             xsDataInputReadImageHeader = XSDataInputReadImageHeader()
             xsDataInputReadImageHeader.setImage(xsDataFileCopy)
             self.__edPluginReadImageHeader = self.loadPlugin(self.__strPluginReadImageHeaderName, "ReadImageHeader_%d" % (iIndex))
             self.__edPluginReadImageHeader.setDataInput(xsDataInputReadImageHeader)
             self.__edPluginReadImageHeader.executeSynchronous()
             xsDataResultReadImageHeader = self.__edPluginReadImageHeader.getDataOutput()
             bSuccess = False
             if (xsDataResultReadImageHeader is not None):
                 xsDataSubWedge = xsDataResultReadImageHeader.getSubWedge()
                 if (xsDataSubWedge is not None):
                     xsDataSubWedge.setSubWedgeNumber(XSDataInteger(iIndex))
                     listSubWedge.append(xsDataSubWedge)
                     bSuccess = True
             if (not bSuccess):
                 # Fix for bug #223: raise an error if an image could not be read
                 strErrorMessage = "EDPluginControlSubWedgeAssemblev1_1.createDataCollectionFromImageHeaders: %s %s" % (
                                     self.__strPluginReadImageHeaderName, \
                                     "Could not read header from image %s" % xsDataFileCopy.getPath().getValue())
                 EDVerbose.error(strErrorMessage)
                 self.addErrorMessage(strErrorMessage)
                 self.setFailure()
             iIndex += 1
         if (not self.isFailure()):
             xsDataInputSubWedgeMerge = XSDataInputSubWedgeMerge()
             for xsDataSubWedge in listSubWedge:
                 xsDataInputSubWedgeMerge.addSubWedge(xsDataSubWedge)
             self.__edPluginSubWedgeMerge.setDataInput(xsDataInputSubWedgeMerge)
             self.__edPluginSubWedgeMerge.executeSynchronous()
 def createDataCollectionFromImageHeaders(self, _edPlugin):
     """
     This method creates a list of XSDataSubwedges by reading the header of the images
     given as a list of paths as input.
     """
     EDVerbose.DEBUG("EDPluginControlSubWedgeAssemblev10.createDataCollectionFromImageHeaders")
     xsDataInputSubWedgeAssemble = _edPlugin.getDataInput()
     listXSDataFile = xsDataInputSubWedgeAssemble.getFile()
     if (len(listXSDataFile) > 0):
         listSubWedge = []
         iIndex = 1
         for xsDataFile in listXSDataFile:
             xsDataFileCopy = XSDataFile.parseString(xsDataFile.marshal())
             xsDataInputReadImageHeader = XSDataInputReadImageHeader()
             xsDataInputReadImageHeader.setImage(xsDataFileCopy)
             self.__edPluginReadImageHeader = self.loadPlugin(self.__strPluginReadImageHeaderName, "ReadImageHeader_%d" % (iIndex))
             self.__edPluginReadImageHeader.setDataInput(xsDataInputReadImageHeader)
             self.__edPluginReadImageHeader.executeSynchronous()
             xsDataResultReadImageHeader = self.__edPluginReadImageHeader.getDataOutput()
             xsDataSubWedge = xsDataResultReadImageHeader.getSubWedge()
             if (xsDataSubWedge is None):
                 # Fix for bug #223: raise an error if an image could not be read
                 errorMessage = EDMessage.ERROR_EXECUTION_03 % ("EDPluginControlSubWedgeAssemblev10.createDataCollectionFromImageHeaders", \
                                                                 self.__strPluginReadImageHeaderName, \
                                                                 "Could not read header from image %s" % xsDataFileCopy.getPath().getValue())
                 EDVerbose.error(errorMessage)
                 self.addErrorMessage(errorMessage)
                 raise RuntimeError, errorMessage
             else:
                 listSubWedge.append(xsDataSubWedge)
             iIndex += 1
         xsDataInputSubWedgeMerge = XSDataInputSubWedgeMerge()
         for xsDataSubWedge in listSubWedge:
             xsDataInputSubWedgeMerge.addSubWedge(xsDataSubWedge)
         self.__edPluginSubWedgeMerge.setDataInput(xsDataInputSubWedgeMerge)
         self.__edPluginSubWedgeMerge.executeSynchronous()
예제 #8
0
    def process(self, _edPlugin=None):
        """
        Executes the execution plugins
        """
        EDPluginControl.process(self, _edPlugin)
        self.DEBUG("EDPluginControlImageQualityIndicatorsv1_3.process")
        EDUtilsParallel.initializeNbThread()
        # Check if we should do indexing:
        bDoIndexing = False
        if self.dataInput.doIndexing is not None:
            if self.dataInput.doIndexing.value:
                bDoIndexing = True
        # Loop through all the incoming reference images
        listXSDataImage = self.dataInput.image
        xsDataInputMXWaitFile = XSDataInputMXWaitFile()
        self.xsDataResultControlImageQualityIndicators = XSDataResultControlImageQualityIndicators(
        )
        listPlugin = []
        for xsDataImage in listXSDataImage:
            self.edPluginMXWaitFile = self.loadPlugin(
                self.strPluginMXWaitFileName)
            xsDataInputMXWaitFile.file = XSDataFile(xsDataImage.path)
            xsDataInputMXWaitFile.setSize(XSDataInteger(self.minImageSize))
            xsDataInputMXWaitFile.setTimeOut(
                XSDataTime(self.fMXWaitFileTimeOut))
            self.DEBUG("Wait file timeOut set to %f" % self.fMXWaitFileTimeOut)
            self.edPluginMXWaitFile.setDataInput(xsDataInputMXWaitFile)
            self.edPluginMXWaitFile.executeSynchronous()
            if not os.path.exists(xsDataImage.path.value):
                self.edPluginMXWaitFile = self.loadPlugin(
                    self.strPluginMXWaitFileName)
                xsDataInputMXWaitFile.file = XSDataFile(xsDataImage.path)
                xsDataInputMXWaitFile.setSize(XSDataInteger(5000000))
                xsDataInputMXWaitFile.setTimeOut(
                    XSDataTime(self.fMXWaitFileTimeOut))
                self.DEBUG("Wait file timeOut set to %f" %
                           self.fMXWaitFileTimeOut)
                self.edPluginMXWaitFile.setDataInput(xsDataInputMXWaitFile)
                self.edPluginMXWaitFile.executeSynchronous()
            if not os.path.exists(xsDataImage.path.value):
                strError = "Time-out while waiting for image %s" % xsDataImage.path.value
                self.error(strError)
                self.addErrorMessage(strError)
                self.setFailure()
            else:
                if self.bUseThinClient:
                    strPluginName = self.strPluginNameThinClient
                else:
                    strPluginName = self.strPluginName
                edPluginPluginExecImageQualityIndicator = self.loadPlugin(
                    strPluginName)
                self.listPluginExecImageQualityIndicator.append(
                    edPluginPluginExecImageQualityIndicator)
                xsDataInputDistlSignalStrength = XSDataInputDistlSignalStrength(
                )
                xsDataInputDistlSignalStrength.setReferenceImage(xsDataImage)
                edPluginPluginExecImageQualityIndicator.setDataInput(
                    xsDataInputDistlSignalStrength)
                edPluginPluginExecImageQualityIndicator.execute()
                edPluginControlBackground3D = self.loadPlugin(
                    self.strPluginNameControlBackground3D)
                listPlugin.append([
                    edPluginPluginExecImageQualityIndicator,
                    edPluginControlBackground3D
                ])
                xsDataInputControlBackground3D = XSDataInputControlBackground3D(
                )
                xsDataInputControlBackground3D.addImage(
                    XSDataFile(xsDataImage.path))
                edPluginControlBackground3D.dataInput = xsDataInputControlBackground3D
                edPluginControlBackground3D.execute()
        listIndexing = []
        # Synchronize all image quality indicator plugins and upload to ISPyB
        xsDataInputStoreListOfImageQualityIndicators = XSDataInputStoreListOfImageQualityIndicators(
        )
        for pluginPair in listPlugin:
            edPluginPluginExecImageQualityIndicator = pluginPair[0]
            edPluginControlBackground3D = pluginPair[1]
            edPluginPluginExecImageQualityIndicator.synchronize()
            edPluginControlBackground3D.synchronize()
            xsDataImageQualityIndicators = XSDataImageQualityIndicators.parseString(\
                                             edPluginPluginExecImageQualityIndicator.dataOutput.imageQualityIndicators.marshal())
            if edPluginControlBackground3D.dataOutput.imageBackground != []:
                xsDataImageQualityIndicators.background3D_estimate = edPluginControlBackground3D.dataOutput.imageBackground[
                    0].estimate
            self.xsDataResultControlImageQualityIndicators.addImageQualityIndicators(
                xsDataImageQualityIndicators)
            xsDataISPyBImageQualityIndicators = \
                XSDataISPyBImageQualityIndicators.parseString(xsDataImageQualityIndicators.marshal())
            xsDataInputStoreListOfImageQualityIndicators.addImageQualityIndicators(
                xsDataISPyBImageQualityIndicators)
#        print xsDataInputStoreListOfImageQualityIndicators.marshal()
        if self.dataInput.doUploadToIspyb is not None and self.dataInput.doUploadToIspyb.value:
            self.edPluginISPyB = self.loadPlugin(self.strISPyBPluginName)
            self.edPluginISPyB.dataInput = xsDataInputStoreListOfImageQualityIndicators
            self.edPluginISPyB.execute()
        #
        if bDoIndexing:
            # Find the 5 most intensive images (TIS):
            listImage = []
            # Check that we have background3D_estimate from all images:
            has_background3D_estimate = True
            for imageQualityIndicators in self.xsDataResultControlImageQualityIndicators.imageQualityIndicators:
                if imageQualityIndicators.background3D_estimate is None:
                    has_background3D_estimate = False
            if has_background3D_estimate:
                listSorted = sorted(
                    self.xsDataResultControlImageQualityIndicators.
                    imageQualityIndicators,
                    key=lambda imageQualityIndicators: imageQualityIndicators.
                    background3D_estimate.value)
            else:
                listSorted = sorted(
                    self.xsDataResultControlImageQualityIndicators.
                    imageQualityIndicators,
                    key=lambda imageQualityIndicators: imageQualityIndicators.
                    totalIntegratedSignal.value)
            for xsDataResultControlImageQualityIndicator in listSorted[-5:]:
                if xsDataResultControlImageQualityIndicator.goodBraggCandidates.value > 30:
                    xsDataInputReadImageHeader = XSDataInputReadImageHeader()
                    xsDataInputReadImageHeader.image = XSDataFile(
                        xsDataResultControlImageQualityIndicator.image.path)
                    self.edPluginReadImageHeader = self.loadPlugin(
                        self.strPluginReadImageHeaderName)
                    self.edPluginReadImageHeader.dataInput = xsDataInputReadImageHeader
                    self.edPluginReadImageHeader.executeSynchronous()
                    xsDataResultReadImageHeader = self.edPluginReadImageHeader.dataOutput
                    if xsDataResultReadImageHeader is not None:
                        xsDataSubWedge = xsDataResultReadImageHeader.subWedge
                        self.xsDataCollection = XSDataCollection()
                        self.xsDataCollection.addSubWedge(xsDataSubWedge)
                        xsDataIndexingInput = XSDataIndexingInput()
                        xsDataIndexingInput.setDataCollection(
                            self.xsDataCollection)
                        xsDataMOSFLMIndexingInput = EDHandlerXSDataMOSFLMv10.generateXSDataMOSFLMInputIndexing(
                            xsDataIndexingInput)
                        edPluginMOSFLMIndexing = self.loadPlugin(
                            self.strIndexingMOSFLMPluginName)
                        self.listPluginMOSFLM.append([
                            edPluginMOSFLMIndexing,
                            xsDataResultControlImageQualityIndicator
                        ])
                        edPluginMOSFLMIndexing.setDataInput(
                            xsDataMOSFLMIndexingInput)
                        edPluginMOSFLMIndexing.execute()
            for tupleMOSFLM in self.listPluginMOSFLM:
                edPluginMOSFLMIndexing = tupleMOSFLM[0]
                xsDataResultControlImageQualityIndicator = tupleMOSFLM[1]
                edPluginMOSFLMIndexing.synchronize()
                if not edPluginMOSFLMIndexing.isFailure():
                    xsDataMOSFLMOutput = edPluginMOSFLMIndexing.dataOutput
                    xsDataIndexingResult = EDHandlerXSDataMOSFLMv10.generateXSDataIndexingResult(
                        xsDataMOSFLMOutput)
                    selectedSolution = xsDataIndexingResult.selectedSolution
                    if selectedSolution is not None:
                        xsDataResultControlImageQualityIndicator.selectedIndexingSolution = selectedSolution
    def process(self, _edPlugin=None):
        """
        Executes the execution plugins
        """
        EDPluginControl.process(self, _edPlugin)
        self.DEBUG("EDPluginControlImageQualityIndicatorsv1_2.process")
        EDUtilsParallel.initializeNbThread()
        # Check if we should do indexing:
        bDoIndexing = False
        if self.dataInput.doIndexing is not None:
            if self.dataInput.doIndexing.value:
                bDoIndexing = True
        # Loop through all the incoming reference images
        listXSDataImage = self.dataInput.image
        xsDataInputMXWaitFile = XSDataInputMXWaitFile()
        self.xsDataResultControlImageQualityIndicators = XSDataResultControlImageQualityIndicators(
        )
        listPlugin = []
        for xsDataImage in listXSDataImage:
            self.edPluginMXWaitFile = self.loadPlugin(
                self.strPluginMXWaitFileName)
            xsDataInputMXWaitFile.file = XSDataFile(xsDataImage.path)
            xsDataInputMXWaitFile.setSize(XSDataInteger(self.minImageSize))
            xsDataInputMXWaitFile.setTimeOut(
                XSDataTime(self.fMXWaitFileTimeOut))
            self.DEBUG("Wait file timeOut set to %f" % self.fMXWaitFileTimeOut)
            self.edPluginMXWaitFile.setDataInput(xsDataInputMXWaitFile)
            self.edPluginMXWaitFile.executeSynchronous()
            if not os.path.exists(xsDataImage.path.value):
                strError = "Time-out while waiting for image %s" % xsDataImage.path.value
                self.error(strError)
                self.addErrorMessage(strError)
                self.setFailure()
            else:
                if self.bUseThinClient:
                    strPluginName = self.strPluginNameThinClient
                else:
                    strPluginName = self.strPluginName
                edPluginPluginExecImageQualityIndicator = self.loadPlugin(
                    strPluginName)
                listPlugin.append(edPluginPluginExecImageQualityIndicator)
                self.listPluginExecImageQualityIndicator.append(
                    edPluginPluginExecImageQualityIndicator)
                xsDataInputDistlSignalStrength = XSDataInputDistlSignalStrength(
                )
                xsDataInputDistlSignalStrength.setReferenceImage(xsDataImage)
                edPluginPluginExecImageQualityIndicator.setDataInput(
                    xsDataInputDistlSignalStrength)
                edPluginPluginExecImageQualityIndicator.execute()
        listIndexing = []
        # Synchronize all image quality indicator plugins and upload to ISPyB
        xsDataInputStoreListOfImageQualityIndicators = XSDataInputStoreListOfImageQualityIndicators(
        )
        for edPluginPluginExecImageQualityIndicator in listPlugin:
            edPluginPluginExecImageQualityIndicator.synchronize()
            xsDataImageQualityIndicators = XSDataImageQualityIndicators.parseString( \
                                             edPluginPluginExecImageQualityIndicator.dataOutput.imageQualityIndicators.marshal())
            self.xsDataResultControlImageQualityIndicators.addImageQualityIndicators(
                xsDataImageQualityIndicators)
            xsDataISPyBImageQualityIndicators = \
                XSDataISPyBImageQualityIndicators.parseString(xsDataImageQualityIndicators.marshal())
            xsDataInputStoreListOfImageQualityIndicators.addImageQualityIndicators(
                xsDataISPyBImageQualityIndicators)
#        print xsDataInputStoreListOfImageQualityIndicators.marshal()
        if self.bDoISPyBUpload:
            self.edPluginISPyB = self.loadPlugin(self.strISPyBPluginName)
            self.edPluginISPyB.dataInput = xsDataInputStoreListOfImageQualityIndicators
            self.edPluginISPyB.execute()
        #
        if bDoIndexing:
            # Find the 5 most intensive images (TIS):
            listImage = []
            listSorted = sorted(
                self.xsDataResultControlImageQualityIndicators.
                imageQualityIndicators,
                key=
                lambda imageQualityIndicators: imageQualityIndicators.totalIntegratedSignal.value
            )
            for xsDataResultControlImageQualityIndicator in listSorted[-5:]:
                if xsDataResultControlImageQualityIndicator.goodBraggCandidates.value > 30:
                    xsDataInputReadImageHeader = XSDataInputReadImageHeader()
                    xsDataInputReadImageHeader.image = XSDataFile(
                        xsDataResultControlImageQualityIndicator.image.path)
                    self.edPluginReadImageHeader = self.loadPlugin(
                        self.strPluginReadImageHeaderName)
                    self.edPluginReadImageHeader.dataInput = xsDataInputReadImageHeader
                    self.edPluginReadImageHeader.executeSynchronous()
                    xsDataResultReadImageHeader = self.edPluginReadImageHeader.dataOutput
                    if xsDataResultReadImageHeader is not None:
                        xsDataSubWedge = xsDataResultReadImageHeader.subWedge
                        self.xsDataCollection = XSDataCollection()
                        self.xsDataCollection.addSubWedge(xsDataSubWedge)
                        xsDataIndexingInput = XSDataIndexingInput()
                        xsDataIndexingInput.setDataCollection(
                            self.xsDataCollection)
                        xsDataMOSFLMIndexingInput = EDHandlerXSDataMOSFLMv10.generateXSDataMOSFLMInputIndexing(
                            xsDataIndexingInput)
                        edPluginMOSFLMIndexing = self.loadPlugin(
                            self.strIndexingMOSFLMPluginName)
                        self.listPluginMOSFLM.append([
                            edPluginMOSFLMIndexing,
                            xsDataResultControlImageQualityIndicator
                        ])
                        edPluginMOSFLMIndexing.setDataInput(
                            xsDataMOSFLMIndexingInput)
                        edPluginMOSFLMIndexing.execute()
            for tupleMOSFLM in self.listPluginMOSFLM:
                edPluginMOSFLMIndexing = tupleMOSFLM[0]
                xsDataResultControlImageQualityIndicator = tupleMOSFLM[1]
                edPluginMOSFLMIndexing.synchronize()
                if not edPluginMOSFLMIndexing.isFailure():
                    xsDataMOSFLMOutput = edPluginMOSFLMIndexing.dataOutput
                    xsDataIndexingResult = EDHandlerXSDataMOSFLMv10.generateXSDataIndexingResult(
                        xsDataMOSFLMOutput)
                    selectedSolution = xsDataIndexingResult.selectedSolution
                    if selectedSolution is not None:
                        xsDataResultControlImageQualityIndicator.selectedIndexingSolution = selectedSolution
    def process(self, _edPlugin=None):
        """
        Executes the execution plugins
        """
        EDPluginControl.process(self, _edPlugin)
        self.DEBUG("EDPluginControlImageQualityIndicatorsv1_5.process")
        EDUtilsParallel.initializeNbThread()
        # Check batch size
        if self.dataInput.batchSize is None:
            batchSize = 1
        else:
            batchSize = self.dataInput.batchSize.value
        # Check if we should do distlSignalStrength:
        bDoDistlSignalStrength = True
        if self.dataInput.doDistlSignalStrength is not None:
            if not self.dataInput.doDistlSignalStrength.value:
                bDoDistlSignalStrength = False
        # Check if we should do indexing:
        bDoIndexing = False
        if self.dataInput.doIndexing is not None:
            if self.dataInput.doIndexing.value:
                 bDoIndexing = True
        # Loop through all the incoming reference images
        listXSDataImage = self.dataInput.image
        xsDataInputMXWaitFile = XSDataInputMXWaitFile()
        self.xsDataResultControlImageQualityIndicators = XSDataResultControlImageQualityIndicators()
        listPluginDistl = []
        listPluginDozor = []
        listBatch = []
        indexBatch = 0
        for xsDataImage in listXSDataImage:
            self.edPluginMXWaitFile = self.loadPlugin(self.strPluginMXWaitFileName)
            xsDataInputMXWaitFile.file = XSDataFile(xsDataImage.path)
            xsDataInputMXWaitFile.setSize(XSDataInteger(self.minImageSize))
            xsDataInputMXWaitFile.setTimeOut(XSDataTime(self.fMXWaitFileTimeOut))
            self.DEBUG("Wait file timeOut set to %f" % self.fMXWaitFileTimeOut)
            self.edPluginMXWaitFile.setDataInput(xsDataInputMXWaitFile)
            self.edPluginMXWaitFile.executeSynchronous()
            if not os.path.exists(xsDataImage.path.value):
                self.edPluginMXWaitFile = self.loadPlugin(self.strPluginMXWaitFileName)
                xsDataInputMXWaitFile.file = XSDataFile(xsDataImage.path)
                xsDataInputMXWaitFile.setSize(XSDataInteger(5000000))
                xsDataInputMXWaitFile.setTimeOut(XSDataTime(self.fMXWaitFileTimeOut))
                self.DEBUG("Wait file timeOut set to %f" % self.fMXWaitFileTimeOut)
                self.edPluginMXWaitFile.setDataInput(xsDataInputMXWaitFile)
                self.edPluginMXWaitFile.executeSynchronous()
            if not os.path.exists(xsDataImage.path.value):
                strError = "Time-out while waiting for image %s" % xsDataImage.path.value
                self.error(strError)
                self.addErrorMessage(strError)
                self.setFailure()
            else:
                # Check if we should run distl.signalStrength
                edPluginPluginExecImageQualityIndicator = None
                if bDoDistlSignalStrength:
                    if self.bUseThinClient:
                        strPluginName = self.strPluginNameThinClient
                    else:
                        strPluginName = self.strPluginName
                    edPluginPluginExecImageQualityIndicator = self.loadPlugin(strPluginName)
                    self.listPluginExecImageQualityIndicator.append(edPluginPluginExecImageQualityIndicator)
                    xsDataInputDistlSignalStrength = XSDataInputDistlSignalStrength()
                    xsDataInputDistlSignalStrength.setReferenceImage(xsDataImage)
                    edPluginPluginExecImageQualityIndicator.setDataInput(xsDataInputDistlSignalStrength)
                    edPluginPluginExecImageQualityIndicator.execute()
                listPluginDistl.append((xsDataImage.copy(), edPluginPluginExecImageQualityIndicator))
                listBatch.append(xsDataImage.copy())
                if len(listBatch) == batchSize:
                    edPluginControlDozor = self.loadPlugin(self.strPluginNameControlDozor)
                    xsDataInputControlDozor = XSDataInputControlDozor()
                    for image in listBatch:
                        xsDataInputControlDozor.addImage(XSDataFile(image.path))
                    xsDataInputControlDozor.batchSize = XSDataInteger(batchSize)
                    edPluginControlDozor.dataInput = xsDataInputControlDozor
                    edPluginControlDozor.execute()
                    listPluginDozor.append((edPluginControlDozor, listBatch))
                    listBatch = []
        if len(listBatch) > 0:
            # Process the remaining images...
            edPluginControlDozor = self.loadPlugin(self.strPluginNameControlDozor)
            xsDataInputControlDozor = XSDataInputControlDozor()
            for image in listBatch:
                xsDataInputControlDozor.addImage(XSDataFile(image.path))
            xsDataInputControlDozor.batchSize = XSDataInteger(batchSize)
            edPluginControlDozor.dataInput = xsDataInputControlDozor
            edPluginControlDozor.execute()
            listPluginDozor.append([edPluginControlDozor, listBatch])
        listIndexing = []
        # Synchronize all image quality indicator plugins and upload to ISPyB
        xsDataInputStoreListOfImageQualityIndicators = XSDataInputStoreListOfImageQualityIndicators()

        for (xsDataImage, edPluginPluginExecImageQualityIndicator) in listPluginDistl:
            xsDataImageQualityIndicators = XSDataImageQualityIndicators()
            xsDataImageQualityIndicators.image = xsDataImage.copy()
            if edPluginPluginExecImageQualityIndicator is not None:
                edPluginPluginExecImageQualityIndicator.synchronize()
                if edPluginPluginExecImageQualityIndicator.dataOutput.imageQualityIndicators is not None:
                    xsDataImageQualityIndicators = XSDataImageQualityIndicators.parseString(\
                            edPluginPluginExecImageQualityIndicator.dataOutput.imageQualityIndicators.marshal())
            self.xsDataResultControlImageQualityIndicators.addImageQualityIndicators(xsDataImageQualityIndicators)

        for (edPluginControlDozor, listBatch) in listPluginDozor:
            edPluginControlDozor.synchronize()
            for imageDozor in edPluginControlDozor.dataOutput.imageDozor:
                for xsDataImageQualityIndicators in self.xsDataResultControlImageQualityIndicators.imageQualityIndicators:
                    if xsDataImageQualityIndicators.image.path.value == imageDozor.image.path.value:
                        xsDataImageQualityIndicators.dozor_score = imageDozor.mainScore
                        xsDataImageQualityIndicators.dozorSpotFile = imageDozor.spotFile
                        xsDataImageQualityIndicators.dozorSpotsIntAver = imageDozor.spotsIntAver
                        xsDataImageQualityIndicators.dozorSpotsResolution = imageDozor.spotsResolution
                        if self.xsDataResultControlImageQualityIndicators.inputDozor is None:
                            if edPluginControlDozor.dataOutput.inputDozor is not None:
                                self.xsDataResultControlImageQualityIndicators.inputDozor = XSDataDozorInput().parseString(
                                               edPluginControlDozor.dataOutput.inputDozor.marshal())
            if self.dataInput.doUploadToIspyb is not None and self.dataInput.doUploadToIspyb.value:
                xsDataISPyBImageQualityIndicators = \
                    XSDataISPyBImageQualityIndicators.parseString(xsDataImageQualityIndicators.marshal())
                xsDataInputStoreListOfImageQualityIndicators.addImageQualityIndicators(xsDataISPyBImageQualityIndicators)
#        print xsDataInputStoreListOfImageQualityIndicators.marshal()
        if self.dataInput.doUploadToIspyb is not None and self.dataInput.doUploadToIspyb.value:
            self.edPluginISPyB = self.loadPlugin(self.strISPyBPluginName)
            self.edPluginISPyB.dataInput = xsDataInputStoreListOfImageQualityIndicators
            self.edPluginISPyB.execute()
        #
        if bDoIndexing:
            # Find the 5 most intensive images (TIS):
            listImage = []
            # Check that we have dozor_score from all images:
            has_dozor_score = True
            for imageQualityIndicators in self.xsDataResultControlImageQualityIndicators.imageQualityIndicators:
                if imageQualityIndicators.dozor_score is None:
                    has_dozor_score = False
            if has_dozor_score:
                listSorted = sorted(self.xsDataResultControlImageQualityIndicators.imageQualityIndicators,
                                    key=lambda imageQualityIndicators: imageQualityIndicators.dozor_score.value)
            else:
                listSorted = sorted(self.xsDataResultControlImageQualityIndicators.imageQualityIndicators,
                                    key=lambda imageQualityIndicators: imageQualityIndicators.totalIntegratedSignal.value)
            for xsDataResultControlImageQualityIndicator in listSorted[-5:]:
                if xsDataResultControlImageQualityIndicator.goodBraggCandidates.value > 30:
                    xsDataInputReadImageHeader = XSDataInputReadImageHeader()
                    xsDataInputReadImageHeader.image = XSDataFile(xsDataResultControlImageQualityIndicator.image.path)
                    self.edPluginReadImageHeader = self.loadPlugin(self.strPluginReadImageHeaderName)
                    self.edPluginReadImageHeader.dataInput = xsDataInputReadImageHeader
                    self.edPluginReadImageHeader.executeSynchronous()
                    xsDataResultReadImageHeader = self.edPluginReadImageHeader.dataOutput
                    if xsDataResultReadImageHeader is not None:
                        edPluginLabelitIndexing = self.loadPlugin(self.strIndexingLabelitPluginName)
                        xsDataInputLabelitIndexing = XSDataInputLabelitIndexing()
                        xsDataInputLabelitIndexing.image.append(XSDataImage(xsDataResultControlImageQualityIndicator.image.path))
                        edPluginLabelitIndexing.setDataInput(xsDataInputLabelitIndexing)
                        self.listPluginLabelit.append([edPluginLabelitIndexing, xsDataResultControlImageQualityIndicator])
                        edPluginLabelitIndexing.execute()
            for tupleLabelit in self.listPluginLabelit:
                edPluginLabelitIndexing = tupleLabelit[0]
                xsDataResultControlImageQualityIndicator = tupleLabelit[1]
                edPluginLabelitIndexing.synchronize()
                if not edPluginLabelitIndexing.isFailure() and edPluginLabelitIndexing.dataOutput is not None:
                    xsDataResultLabelitIndexing = edPluginLabelitIndexing.getDataOutput()
                    xsDataIndexingResult = EDHandlerXSDataPhenixv1_1.generateXSDataIndexingResult(xsDataResultLabelitIndexing)
                    selectedSolution = xsDataIndexingResult.selectedSolution
                    if selectedSolution is not None:
                        xsDataResultControlImageQualityIndicator.selectedIndexingSolution = selectedSolution
예제 #11
0
    def process(self, _edObject=None):
        EDPluginControl.process(self)
        self.DEBUG("EDPluginControlDozorv1_0.process")
        self.sendMessageToMXCuBE("Processing started...", "info")
        EDUtilsParallel.initializeNbThread()
        xsDataResultControlDozor = XSDataResultControlDozor()
        # Check if connection to ISPyB needed
        if self.dataInput.dataCollectionId is not None:
            edPluginRetrieveDataCollection = self.loadPlugin("EDPluginISPyBRetrieveDataCollectionv1_4")
            xsDataInputRetrieveDataCollection = XSDataInputRetrieveDataCollection()
            xsDataInputRetrieveDataCollection.dataCollectionId = self.dataInput.dataCollectionId
            edPluginRetrieveDataCollection.dataInput = xsDataInputRetrieveDataCollection
            edPluginRetrieveDataCollection.executeSynchronous()
            ispybDataCollection = edPluginRetrieveDataCollection.dataOutput.dataCollection
            if self.batchSize is None:
                batchSize = ispybDataCollection.numberOfImages
            else:
                batchSize = self.batchSize
            if batchSize > self.maxBatchSize:
                batchSize = self.maxBatchSize
            if abs(ispybDataCollection.overlap) > 1:
                self.hasOverlap = True
                self.overlap = ispybDataCollection.overlap
            dictImage = self.createImageDictFromISPyB(ispybDataCollection)
        else:
            # No connection to ISPyB, take parameters from input
            if self.dataInput.batchSize is None:
                batchSize = self.maxBatchSize
            else:
                batchSize = self.dataInput.batchSize.value
            dictImage = self.createImageDict(self.dataInput)
        self.screen("Dozor batch size: {0}".format(batchSize))
        if self.dataInput.hdf5BatchSize is not None:
            self.hdf5BatchSize = self.dataInput.hdf5BatchSize.value
        listAllBatches = self.createListOfBatches(dictImage.keys(), batchSize)
        if dictImage[listAllBatches[0][0]].path.value.endswith("h5"):
            # Convert HDF5 images to CBF
            self.screen("HDF5 converter batch size: {0}".format(self.batchSize))
            if self.doRadiationDamage:
                self.cbfTempDir = None
            else:
                self.cbfTempDir = tempfile.mkdtemp(prefix="CbfTemp_")
            listHdf5Batches = self.createListOfBatches(dictImage.keys(), self.batchSize)
            dictImage, self.hasHdf5Prefix = self.convertToCBF(dictImage, listHdf5Batches, self.doRadiationDamage)
        for listBatch in listAllBatches:
            # Read the header from the first image in the batch
            xsDataFile = dictImage[listBatch[0]]
            edPluginControlReadImageHeader = self.loadPlugin(self.strEDPluginControlReadImageHeaderName)
            xsDataInputReadImageHeader = XSDataInputReadImageHeader()
            xsDataInputReadImageHeader.image = xsDataFile
            edPluginControlReadImageHeader.dataInput = xsDataInputReadImageHeader
            edPluginControlReadImageHeader.executeSynchronous()
            subWedge = edPluginControlReadImageHeader.dataOutput.subWedge
            xsDataInputDozor = XSDataInputDozor()
            beam = subWedge.experimentalCondition.beam
            detector = subWedge.experimentalCondition.detector
            goniostat = subWedge.experimentalCondition.goniostat
            xsDataInputDozor.detectorType = detector.type
            xsDataInputDozor.exposureTime = XSDataDouble(beam.exposureTime.value)
            xsDataInputDozor.spotSize = XSDataInteger(3)
            xsDataInputDozor.detectorDistance = XSDataDouble(detector.distance.value)
            xsDataInputDozor.wavelength = XSDataDouble(beam.wavelength.value)
#            xsDataInputDozor.fractionPolatization : XSDataDouble optional
            orgx = detector.beamPositionY.value / detector.pixelSizeY.value
            orgy = detector.beamPositionX.value / detector.pixelSizeX.value
            xsDataInputDozor.orgx = XSDataDouble(orgx)
            xsDataInputDozor.orgy = XSDataDouble(orgy)
            xsDataInputDozor.oscillationRange = XSDataDouble(goniostat.oscillationWidth.value)
#            xsDataInputDozor.imageStep : XSDataDouble optional
            xsDataInputDozor.startingAngle = XSDataDouble(goniostat.rotationAxisStart.value)
            xsDataInputDozor.firstImageNumber = subWedge.image[0].number
            xsDataInputDozor.numberImages = XSDataInteger(len(listBatch))
            if self.hasOverlap:
                xsDataInputDozor.overlap = XSDataAngle(self.overlap)
            strFileName = subWedge.image[0].path.value
            strPrefix = EDUtilsImage.getPrefix(strFileName)
            strSuffix = EDUtilsImage.getSuffix(strFileName)
            if EDUtilsPath.isEMBL():
                strXDSTemplate = "%s_?????.%s" % (strPrefix, strSuffix)
            elif self.hasHdf5Prefix and not self.hasOverlap:
                strXDSTemplate = "%s_??????.%s" % (strPrefix, strSuffix)
            else:
                strXDSTemplate = "%s_????.%s" % (strPrefix, strSuffix)
            xsDataInputDozor.nameTemplateImage = XSDataString(os.path.join(os.path.dirname(strFileName), strXDSTemplate))
            xsDataInputDozor.wedgeNumber = self.dataInput.wedgeNumber
            xsDataInputDozor.radiationDamage = self.dataInput.radiationDamage
            edPluginDozor = self.loadPlugin(self.strEDPluginDozorName, "Dozor_%05d" % subWedge.image[0].number.value)
            edPluginDozor.dataInput = xsDataInputDozor
            edPluginDozor.execute()
            edPluginDozor.synchronize()
            indexImage = 0
            imageDozorBatchList = []

            for xsDataResultDozor in edPluginDozor.dataOutput.imageDozor:
                xsDataControlImageDozor = XSDataControlImageDozor()
                xsDataControlImageDozor.number = xsDataResultDozor.number
                xsDataControlImageDozor.image = dictImage[listBatch[indexImage]]
                xsDataControlImageDozor.spotsNumOf = xsDataResultDozor.spotsNumOf
                xsDataControlImageDozor.spotsIntAver = xsDataResultDozor.spotsIntAver
                xsDataControlImageDozor.spotsResolution = xsDataResultDozor.spotsResolution
                xsDataControlImageDozor.powderWilsonScale = xsDataResultDozor.powderWilsonScale
                xsDataControlImageDozor.powderWilsonBfactor = xsDataResultDozor.powderWilsonBfactor
                xsDataControlImageDozor.powderWilsonResolution = xsDataResultDozor.powderWilsonResolution
                xsDataControlImageDozor.powderWilsonCorrelation = xsDataResultDozor.powderWilsonCorrelation
                xsDataControlImageDozor.powderWilsonRfactor = xsDataResultDozor.powderWilsonRfactor
                xsDataControlImageDozor.mainScore = xsDataResultDozor.mainScore
                xsDataControlImageDozor.spotScore = xsDataResultDozor.spotScore
                xsDataControlImageDozor.visibleResolution = xsDataResultDozor.visibleResolution
                xsDataControlImageDozor.spotFile = xsDataResultDozor.spotFile
                xsDataControlImageDozor.angle = xsDataResultDozor.angle
                xsDataResultControlDozor.addImageDozor(xsDataControlImageDozor)
                if xsDataResultControlDozor.inputDozor is None:
                    xsDataResultControlDozor.inputDozor = XSDataDozorInput().parseString(xsDataInputDozor.marshal())
                indexImage += 1

                dozorSpotListShape = []
                dozorSpotList = []
                spotFile = None
                if xsDataControlImageDozor.spotFile is not None:
                    spotFile = xsDataControlImageDozor.spotFile.path.value
                    if os.path.exists(spotFile):
                        numpyArray = numpy.loadtxt(spotFile, skiprows=3)
                        dozorSpotList = base64.b64encode(numpyArray.tostring())
                        dozorSpotListShape.append(numpyArray.shape[0])
                        if len(numpyArray.shape) > 1:
                            dozorSpotListShape.append(numpyArray.shape[1])

                imageDozorDict = {"index": xsDataControlImageDozor.number.value,
                                  "imageName": xsDataControlImageDozor.image.path.value,
                                  "dozor_score": xsDataControlImageDozor.mainScore.value,
                                  "dozorSpotsNumOf" : xsDataControlImageDozor.spotsNumOf.value,
                                  "dozorSpotFile": spotFile,
                                  "dozorSpotList" : dozorSpotList,
                                  "dozorSpotListShape": dozorSpotListShape,
                                  "dozorSpotsIntAver": xsDataControlImageDozor.spotsIntAver.value,
                                  "dozorSpotsResolution": xsDataControlImageDozor.spotsResolution.value
                                  }
                imageDozorBatchList.append(imageDozorDict)

            xsDataResultControlDozor.halfDoseTime = edPluginDozor.dataOutput.halfDoseTime
            xsDataResultControlDozor.pngPlots = edPluginDozor.dataOutput.pngPlots

            self.sendResultToMXCuBE(imageDozorBatchList)
            self.sendMessageToMXCuBE("Batch processed")
        self.dataOutput = xsDataResultControlDozor
        if self.cbfTempDir is not None:
            if self.dataInput.keepCbfTmpDirectory is not None and self.dataInput.keepCbfTmpDirectory.value:
                self.dataOutput.pathToCbfDirectory = XSDataFile(XSDataString(self.cbfTempDir))
            else:
                shutil.rmtree(self.cbfTempDir)
    def process(self, _edPlugin=None):
        """
        Executes the execution plugins
        """
        EDPluginControl.process(self, _edPlugin)
        self.DEBUG("EDPluginControlImageQualityIndicatorsv1_4.process")
        EDUtilsParallel.initializeNbThread()
        # Check batch size
        if self.dataInput.batchSize is None:
            batchSize = 1
        else:
            batchSize = self.dataInput.batchSize.value
        self.screen("Batch size: {0}".format(batchSize))
        # Check if we should do distlSignalStrength:
        bDoDistlSignalStrength = True
        if self.dataInput.doDistlSignalStrength is not None:
            if not self.dataInput.doDistlSignalStrength.value:
                bDoDistlSignalStrength = False
        # Check if we should do indexing:
        bDoIndexing = False
        if self.dataInput.doIndexing is not None:
            if self.dataInput.doIndexing.value:
                 bDoIndexing = True
        # Loop through all the incoming reference images
        listXSDataImage = self.dataInput.image
        xsDataInputMXWaitFile = XSDataInputMXWaitFile()
        self.xsDataResultControlImageQualityIndicators = XSDataResultControlImageQualityIndicators()
        listPluginDistl = []
        listPluginDozor = []
        listBatch = []
        indexBatch = 0
        listH5FilePath = []
        ispybDataCollection = None
        for xsDataImage in listXSDataImage:
            strPathToImage = xsDataImage.path.value
            # If Eiger, just wait for the h5 file
            if "id30a3" in strPathToImage:
                h5MasterFilePath, h5DataFilePath, hdf5ImageNumber = self.getH5FilePath(strPathToImage, batchSize)
#                print(h5FilePath)
#                print(hdf5ImageNumber)
                if not h5DataFilePath in listH5FilePath:
                    self.screen("ID30a3 Eiger data, waiting for master and data files...")
                    listH5FilePath.append(h5DataFilePath)
                    self.edPluginMXWaitFile = self.loadPlugin(self.strPluginMXWaitFileName)
                    xsDataInputMXWaitFile.file = XSDataFile(XSDataString(h5DataFilePath))
                    xsDataInputMXWaitFile.setSize(XSDataInteger(self.minImageSize))
                    xsDataInputMXWaitFile.setTimeOut(XSDataTime(self.fMXWaitFileTimeOut))
                    self.screen("Waiting for file {0}".format(h5DataFilePath))
                    self.DEBUG("Wait file timeOut set to %f" % self.fMXWaitFileTimeOut)
                    self.edPluginMXWaitFile.setDataInput(xsDataInputMXWaitFile)
                    self.edPluginMXWaitFile.executeSynchronous()
#                    hdf5FilePath = strPathToImage.replace(".cbf", ".h5")
                    ispybDataCollection = None
                    time.sleep(1)
                indexLoop = 1
                continueLoop = True
                while continueLoop:
                    xsDataInputControlH5ToCBF = XSDataInputControlH5ToCBF()
                    xsDataInputControlH5ToCBF.hdf5File = XSDataFile(XSDataString(strPathToImage))
                    imageNumber = EDUtilsImage.getImageNumber(strPathToImage)
                    xsDataInputControlH5ToCBF.imageNumber = XSDataInteger(imageNumber)
                    xsDataInputControlH5ToCBF.hdf5ImageNumber = XSDataInteger(hdf5ImageNumber)
                    xsDataInputControlH5ToCBF.ispybDataCollection = ispybDataCollection
                    edPluginControlH5ToCBF = self.loadPlugin(self.strPluginControlH5ToCBF, "ControlH5ToCBF_%04d_%02d" % (imageNumber, indexLoop))
                    edPluginControlH5ToCBF.dataInput = xsDataInputControlH5ToCBF
                    edPluginControlH5ToCBF.executeSynchronous()
                    cbfFile = edPluginControlH5ToCBF.dataOutput.outputCBFFile
#                    print(cbfFile)
#                    print(indexLoop)
                    if cbfFile is not None:
                        strPathToImage = cbfFile.path.value
#                        print(cbfFile.path.value)
                        if os.path.exists(strPathToImage):
                            self.screen("Image has been converted to CBF file: {0}".format(strPathToImage))
                            continueLoop = False
#                    print(continueLoop)
                    if continueLoop:
                        self.screen("Still waiting for converting to CBF file: {0}".format(strPathToImage))
                        indexLoop += 1
                        time.sleep(5)
                        if indexLoop > 10:
                            continueLoop = False

                ispybDataCollection = edPluginControlH5ToCBF.dataOutput.ispybDataCollection


            elif not os.path.exists(strPathToImage):
                self.screen("Waiting for file {0}".format(strPathToImage))
                self.edPluginMXWaitFile = self.loadPlugin(self.strPluginMXWaitFileName)
                xsDataInputMXWaitFile.file = XSDataFile(XSDataString(strPathToImage))
                xsDataInputMXWaitFile.setSize(XSDataInteger(self.minImageSize))
                xsDataInputMXWaitFile.setTimeOut(XSDataTime(self.fMXWaitFileTimeOut))
                self.DEBUG("Wait file timeOut set to %f" % self.fMXWaitFileTimeOut)
                self.edPluginMXWaitFile.setDataInput(xsDataInputMXWaitFile)
                self.edPluginMXWaitFile.executeSynchronous()
            if not os.path.exists(strPathToImage):
                strError = "Time-out while waiting for image %s" % strPathToImage
                self.error(strError)
                self.addErrorMessage(strError)
                self.setFailure()
            else:
                # Check if we should run distl.signalStrength
                xsDataImageNew = XSDataImage(XSDataString(strPathToImage))
                edPluginPluginExecImageQualityIndicator = None
                if bDoDistlSignalStrength:
                    if self.bUseThinClient:
                        strPluginName = self.strPluginNameThinClient
                    else:
                        strPluginName = self.strPluginName
                    edPluginPluginExecImageQualityIndicator = self.loadPlugin(strPluginName)
                    self.listPluginExecImageQualityIndicator.append(edPluginPluginExecImageQualityIndicator)
                    xsDataInputDistlSignalStrength = XSDataInputDistlSignalStrength()
                    xsDataInputDistlSignalStrength.setReferenceImage(xsDataImageNew)
                    edPluginPluginExecImageQualityIndicator.setDataInput(xsDataInputDistlSignalStrength)
                    edPluginPluginExecImageQualityIndicator.execute()
                listPluginDistl.append((xsDataImageNew.copy(), edPluginPluginExecImageQualityIndicator))
                listBatch.append(xsDataImageNew.copy())
                if len(listBatch) == batchSize:
                    edPluginControlDozor = self.loadPlugin(self.strPluginNameControlDozor)
                    xsDataInputControlDozor = XSDataInputControlDozor()
                    for image in listBatch:
                        xsDataInputControlDozor.addImage(XSDataFile(image.path))
                    xsDataInputControlDozor.batchSize = XSDataInteger(batchSize)
                    edPluginControlDozor.dataInput = xsDataInputControlDozor
                    edPluginControlDozor.execute()
                    listPluginDozor.append((edPluginControlDozor, listBatch))
                    listBatch = []
        if len(listBatch) > 0:
            # Process the remaining images...
            edPluginControlDozor = self.loadPlugin(self.strPluginNameControlDozor)
            xsDataInputControlDozor = XSDataInputControlDozor()
            for image in listBatch:
                xsDataInputControlDozor.addImage(XSDataFile(image.path))
            xsDataInputControlDozor.batchSize = XSDataInteger(batchSize)
            edPluginControlDozor.dataInput = xsDataInputControlDozor
            edPluginControlDozor.execute()
            listPluginDozor.append([edPluginControlDozor, listBatch])
        listIndexing = []
        # Synchronize all image quality indicator plugins and upload to ISPyB
        xsDataInputStoreListOfImageQualityIndicators = XSDataInputStoreListOfImageQualityIndicators()

        for (xsDataImage, edPluginPluginExecImageQualityIndicator) in listPluginDistl:
            xsDataImageQualityIndicators = XSDataImageQualityIndicators()
            xsDataImageQualityIndicators.image = xsDataImage.copy()
            if edPluginPluginExecImageQualityIndicator is not None:
                edPluginPluginExecImageQualityIndicator.synchronize()
                if edPluginPluginExecImageQualityIndicator.dataOutput.imageQualityIndicators is not None:
                    xsDataImageQualityIndicators = XSDataImageQualityIndicators.parseString(\
                            edPluginPluginExecImageQualityIndicator.dataOutput.imageQualityIndicators.marshal())
            self.xsDataResultControlImageQualityIndicators.addImageQualityIndicators(xsDataImageQualityIndicators)

        for (edPluginControlDozor, listBatch) in listPluginDozor:
            edPluginControlDozor.synchronize()
            for imageDozor in edPluginControlDozor.dataOutput.imageDozor:
                for xsDataImageQualityIndicators in self.xsDataResultControlImageQualityIndicators.imageQualityIndicators:
                    if xsDataImageQualityIndicators.image.path.value == imageDozor.image.path.value:
                        xsDataImageQualityIndicators.dozor_score = imageDozor.mainScore
                        xsDataImageQualityIndicators.dozorSpotFile = imageDozor.spotFile
                        if imageDozor.spotFile is not None:
                            if os.path.exists(imageDozor.spotFile.path.value):
                                numpyArray = numpy.loadtxt(imageDozor.spotFile.path.value, skiprows=3)
                                xsDataImageQualityIndicators.dozorSpotList = XSDataString(base64.b64encode(numpyArray.tostring()))
                                xsDataImageQualityIndicators.addDozorSpotListShape(XSDataInteger(numpyArray.shape[0]))
                                if len(numpyArray.shape) > 1:
                                    xsDataImageQualityIndicators.addDozorSpotListShape(XSDataInteger(numpyArray.shape[1]))
                        xsDataImageQualityIndicators.dozorSpotsIntAver = imageDozor.spotsIntAver
                        xsDataImageQualityIndicators.dozorSpotsResolution = imageDozor.spotsResolution
                        if self.xsDataResultControlImageQualityIndicators.inputDozor is None:
                            if edPluginControlDozor.dataOutput.inputDozor is not None:
                                self.xsDataResultControlImageQualityIndicators.inputDozor = XSDataDozorInput().parseString(
                                               edPluginControlDozor.dataOutput.inputDozor.marshal())
            if self.dataInput.doUploadToIspyb is not None and self.dataInput.doUploadToIspyb.value:
                xsDataISPyBImageQualityIndicators = \
                    XSDataISPyBImageQualityIndicators.parseString(xsDataImageQualityIndicators.marshal())
                xsDataInputStoreListOfImageQualityIndicators.addImageQualityIndicators(xsDataISPyBImageQualityIndicators)
#        print xsDataInputStoreListOfImageQualityIndicators.marshal()
        if self.dataInput.doUploadToIspyb is not None and self.dataInput.doUploadToIspyb.value:
            self.edPluginISPyB = self.loadPlugin(self.strISPyBPluginName)
            self.edPluginISPyB.dataInput = xsDataInputStoreListOfImageQualityIndicators
            self.edPluginISPyB.execute()
        #
        if bDoIndexing:
            # Find the 5 most intensive images (TIS):
            listImage = []
            # Check that we have dozor_score from all images:
            has_dozor_score = True
            for imageQualityIndicators in self.xsDataResultControlImageQualityIndicators.imageQualityIndicators:
                if imageQualityIndicators.dozor_score is None:
                    has_dozor_score = False
            if has_dozor_score:
                listSorted = sorted(self.xsDataResultControlImageQualityIndicators.imageQualityIndicators,
                                    key=lambda imageQualityIndicators: imageQualityIndicators.dozor_score.value)
            else:
                listSorted = sorted(self.xsDataResultControlImageQualityIndicators.imageQualityIndicators,
                                    key=lambda imageQualityIndicators: imageQualityIndicators.totalIntegratedSignal.value)
            for xsDataResultControlImageQualityIndicator in listSorted[-5:]:
                if xsDataResultControlImageQualityIndicator.goodBraggCandidates.value > 30:
                    xsDataInputReadImageHeader = XSDataInputReadImageHeader()
                    xsDataInputReadImageHeader.image = XSDataFile(xsDataResultControlImageQualityIndicator.image.path)
                    self.edPluginReadImageHeader = self.loadPlugin(self.strPluginReadImageHeaderName)
                    self.edPluginReadImageHeader.dataInput = xsDataInputReadImageHeader
                    self.edPluginReadImageHeader.executeSynchronous()
                    xsDataResultReadImageHeader = self.edPluginReadImageHeader.dataOutput
                    if xsDataResultReadImageHeader is not None:
                        xsDataSubWedge = xsDataResultReadImageHeader.subWedge
                        self.xsDataCollection = XSDataCollection()
                        self.xsDataCollection.addSubWedge(xsDataSubWedge)
                        xsDataIndexingInput = XSDataIndexingInput()
                        xsDataIndexingInput.setDataCollection(self.xsDataCollection)
                        xsDataMOSFLMIndexingInput = EDHandlerXSDataMOSFLMv10.generateXSDataMOSFLMInputIndexing(xsDataIndexingInput)
                        edPluginMOSFLMIndexing = self.loadPlugin(self.strIndexingMOSFLMPluginName)
                        self.listPluginMOSFLM.append([edPluginMOSFLMIndexing, xsDataResultControlImageQualityIndicator])
                        edPluginMOSFLMIndexing.setDataInput(xsDataMOSFLMIndexingInput)
                        edPluginMOSFLMIndexing.execute()
            for tupleMOSFLM in self.listPluginMOSFLM:
                edPluginMOSFLMIndexing = tupleMOSFLM[0]
                xsDataResultControlImageQualityIndicator = tupleMOSFLM[1]
                edPluginMOSFLMIndexing.synchronize()
                if not edPluginMOSFLMIndexing.isFailure():
                    xsDataMOSFLMOutput = edPluginMOSFLMIndexing.dataOutput
                    xsDataIndexingResult = EDHandlerXSDataMOSFLMv10.generateXSDataIndexingResult(xsDataMOSFLMOutput)
                    selectedSolution = xsDataIndexingResult.selectedSolution
                    if selectedSolution is not None:
                        xsDataResultControlImageQualityIndicator.selectedIndexingSolution = selectedSolution
    def process(self, _edPlugin=None):
        """
        Executes the execution plugins
        """
        EDPluginControl.process(self, _edPlugin)
        self.DEBUG("EDPluginControlImageQualityIndicatorsv1_5.process")
        EDUtilsParallel.initializeNbThread()
        # Check batch size
        if self.dataInput.batchSize is None:
            batchSize = 1
        else:
            batchSize = self.dataInput.batchSize.value
        self.screen(
            "Image quality indicators batch size: {0}".format(batchSize))
        # Check if we should do distlSignalStrength:
        bDoDistlSignalStrength = True
        if self.dataInput.doDistlSignalStrength is not None:
            if not self.dataInput.doDistlSignalStrength.value:
                bDoDistlSignalStrength = False
        # Check if we should do indexing:
        bDoIndexing = False
        if self.dataInput.doIndexing is not None:
            if self.dataInput.doIndexing.value:
                bDoIndexing = True
        # Check if fast mesh (for HDF5)
        isFastMesh = False
        if self.dataInput.fastMesh:
            isFastMesh = self.dataInput.fastMesh.value
        # Loop through all the incoming reference images
        if len(self.dataInput.image) == 0:
            directory = self.dataInput.directory.path.value
            template = self.dataInput.template.value
            startNo = self.dataInput.startNo.value
            endNo = self.dataInput.endNo.value
            listXSDataImage = []
            for index in range(startNo, endNo + 1):
                imageName = template.replace("####", "{0:04d}".format(index))
                imagePath = os.path.join(directory, imageName)
                xsDataImage = XSDataImage(path=XSDataString(imagePath),
                                          number=XSDataInteger(index))
                listXSDataImage.append(xsDataImage)
        else:
            listXSDataImage = self.dataInput.image
        xsDataInputMXWaitFile = XSDataInputMXWaitFile()
        self.xsDataResultControlImageQualityIndicators = XSDataResultControlImageQualityIndicators(
        )
        listPluginDistl = []
        listPluginDozor = []
        listOfImagesInBatch = []
        listOfAllBatches = []
        indexBatch = 0
        listH5FilePath = []
        # Process data in batches
        for xsDataImage in listXSDataImage:
            listOfImagesInBatch.append(xsDataImage.copy())
            if len(listOfImagesInBatch) == batchSize:
                listOfAllBatches.append(listOfImagesInBatch)
                listOfImagesInBatch = []
        if len(listOfImagesInBatch) > 0:
            listOfAllBatches.append(listOfImagesInBatch)
            listOfImagesInBatch = []
        # Loop over batches
        for listOfImagesInBatch in listOfAllBatches:
            # First wait for images
            for image in listOfImagesInBatch:
                strPathToImage = image.path.value
                # If Eiger, just wait for the h5 file
                if strPathToImage.endswith(".h5"):
                    h5MasterFilePath, h5DataFilePath, hdf5ImageNumber = self.getH5FilePath(
                        strPathToImage,
                        batchSize=batchSize,
                        isFastMesh=isFastMesh)
                    #                print(h5FilePath)
                    #                print(hdf5ImageNumber)
                    if not h5DataFilePath in listH5FilePath:
                        self.screen(
                            "ID30a3 Eiger data, waiting for master and data files..."
                        )
                        listH5FilePath.append(h5DataFilePath)
                        self.edPluginMXWaitFile = self.loadPlugin(
                            self.strPluginMXWaitFileName)
                        xsDataInputMXWaitFile.file = XSDataFile(
                            XSDataString(h5DataFilePath))
                        xsDataInputMXWaitFile.setSize(
                            XSDataInteger(self.minImageSize))
                        xsDataInputMXWaitFile.setTimeOut(
                            XSDataTime(self.fMXWaitFileTimeOut))
                        self.screen(
                            "Waiting for file {0}".format(h5DataFilePath))
                        self.DEBUG("Wait file timeOut set to %f" %
                                   self.fMXWaitFileTimeOut)
                        self.edPluginMXWaitFile.setDataInput(
                            xsDataInputMXWaitFile)
                        self.edPluginMXWaitFile.executeSynchronous()
                        #                    hdf5FilePath = strPathToImage.replace(".cbf", ".h5")
                        time.sleep(1)
                    if not os.path.exists(h5DataFilePath):
                        strError = "Time-out while waiting for image %s" % h5DataFilePath
                        self.error(strError)
                        self.addErrorMessage(strError)
                        self.setFailure()
                else:
                    if not os.path.exists(strPathToImage):
                        # self.screen("Waiting for file {0}".format(strPathToImage))
                        self.edPluginMXWaitFile = self.loadPlugin(
                            self.strPluginMXWaitFileName)
                        xsDataInputMXWaitFile.file = XSDataFile(
                            XSDataString(strPathToImage))
                        xsDataInputMXWaitFile.setSize(
                            XSDataInteger(self.minImageSize))
                        xsDataInputMXWaitFile.setTimeOut(
                            XSDataTime(self.fMXWaitFileTimeOut))
                        self.screen("Wait file timeOut set to %.0f s" %
                                    self.fMXWaitFileTimeOut)
                        self.edPluginMXWaitFile.setDataInput(
                            xsDataInputMXWaitFile)
                        self.edPluginMXWaitFile.executeSynchronous()
                    if not os.path.exists(strPathToImage):
                        strError = "Time-out while waiting for image %s" % strPathToImage
                        self.error(strError)
                        self.addErrorMessage(strError)
                        self.setFailure()
            if not self.isFailure():
                strPathToFirstImage = listOfImagesInBatch[0].path.value
                if strPathToImage.endswith(".h5"):
                    indexLoop = 1
                    continueLoop = True
                    while continueLoop:
                        directory = os.path.dirname(strPathToFirstImage)
                        firstImage = EDUtilsImage.getImageNumber(
                            listOfImagesInBatch[0].path.value)
                        lastImage = EDUtilsImage.getImageNumber(
                            listOfImagesInBatch[-1].path.value)
                        xsDataInputH5ToCBF = XSDataInputH5ToCBF()
                        xsDataInputH5ToCBF.hdf5File = XSDataFile(
                            listOfImagesInBatch[0].path)
                        xsDataInputH5ToCBF.hdf5ImageNumber = XSDataInteger(1)
                        xsDataInputH5ToCBF.startImageNumber = XSDataInteger(
                            firstImage)
                        xsDataInputH5ToCBF.endImageNumber = XSDataInteger(
                            lastImage)
                        xsDataInputH5ToCBF.forcedOutputDirectory = XSDataFile(
                            XSDataString(directory))
                        edPluginH5ToCBF = self.loadPlugin(
                            "EDPluginH5ToCBFv1_1")
                        edPluginH5ToCBF.dataInput = xsDataInputH5ToCBF
                        edPluginH5ToCBF.execute()
                        edPluginH5ToCBF.synchronize()
                        outputCBFFileTemplate = edPluginH5ToCBF.dataOutput.outputCBFFileTemplate
                        if outputCBFFileTemplate is not None:
                            lastCbfFile = outputCBFFileTemplate.path.value.replace(
                                "######", "{0:06d}".format(
                                    EDUtilsImage.getImageNumber(
                                        listOfImagesInBatch[-1].path.value)))
                            strPathToImage = os.path.join(
                                directory, lastCbfFile)
                            #                        print(cbfFile.path.value)
                            if os.path.exists(strPathToImage):
                                # Rename all images
                                for image in listOfImagesInBatch:
                                    image.path.value = image.path.value.replace(
                                        ".h5", ".cbf")
                                    imageNumber = EDUtilsImage.getImageNumber(
                                        image.path.value)
                                    oldPath = os.path.join(
                                        directory,
                                        outputCBFFileTemplate.path.value.
                                        replace("######",
                                                "{0:06d}".format(imageNumber)))
                                    newPath = os.path.join(
                                        directory,
                                        outputCBFFileTemplate.path.value.
                                        replace("######",
                                                "{0:04d}".format(imageNumber)))
                                    os.rename(oldPath, newPath)
                                lastCbfFile = outputCBFFileTemplate.path.value.replace(
                                    "######", "{0:04d}".format(
                                        EDUtilsImage.getImageNumber(
                                            listOfImagesInBatch[-1].path.value)
                                    ))
                                strPathToImage = os.path.join(
                                    directory, lastCbfFile)
                                self.screen(
                                    "Image has been converted to CBF file: {0}"
                                    .format(strPathToImage))
                                continueLoop = False
    #                    print(continueLoop)
                        if continueLoop:
                            self.screen(
                                "Still waiting for converting to CBF file: {0}"
                                .format(strPathToImage))
                            indexLoop += 1
                            time.sleep(5)
                            if indexLoop > 10:
                                continueLoop = False

                for image in listOfImagesInBatch:
                    strPathToImage = image.path.value
                    # Check if we should run distl.signalStrength
                    xsDataImageNew = XSDataImage(XSDataString(strPathToImage))
                    xsDataImageNew.number = XSDataInteger(
                        EDUtilsImage.getImageNumber(image.path.value))
                    edPluginPluginExecImageQualityIndicator = None
                    if bDoDistlSignalStrength:
                        if self.bUseThinClient:
                            strPluginName = self.strPluginNameThinClient
                        else:
                            strPluginName = self.strPluginName
                        edPluginPluginExecImageQualityIndicator = self.loadPlugin(
                            strPluginName)
                        self.listPluginExecImageQualityIndicator.append(
                            edPluginPluginExecImageQualityIndicator)
                        xsDataInputDistlSignalStrength = XSDataInputDistlSignalStrength(
                        )
                        xsDataInputDistlSignalStrength.setReferenceImage(
                            xsDataImageNew)
                        edPluginPluginExecImageQualityIndicator.setDataInput(
                            xsDataInputDistlSignalStrength)
                        edPluginPluginExecImageQualityIndicator.execute()
                    listPluginDistl.append(
                        (xsDataImageNew.copy(),
                         edPluginPluginExecImageQualityIndicator))

                edPluginControlDozor = self.loadPlugin(
                    self.strPluginNameControlDozor, "ControlDozor_{0}".format(
                        os.path.splitext(
                            os.path.basename(strPathToFirstImage))[0]))
                xsDataInputControlDozor = XSDataInputControlDozor()
                for image in listOfImagesInBatch:
                    xsDataInputControlDozor.addImage(XSDataFile(image.path))
                xsDataInputControlDozor.batchSize = XSDataInteger(
                    len(listOfImagesInBatch))
                edPluginControlDozor.dataInput = xsDataInputControlDozor
                edPluginControlDozor.execute()
                listPluginDozor.append(
                    (edPluginControlDozor, list(listOfImagesInBatch)))

        if not self.isFailure():
            listIndexing = []
            # Synchronize all image quality indicator plugins and upload to ISPyB
            xsDataInputStoreListOfImageQualityIndicators = XSDataInputStoreListOfImageQualityIndicators(
            )

            for (xsDataImage,
                 edPluginPluginExecImageQualityIndicator) in listPluginDistl:
                xsDataImageQualityIndicators = XSDataImageQualityIndicators()
                xsDataImageQualityIndicators.image = xsDataImage.copy()
                if edPluginPluginExecImageQualityIndicator is not None:
                    edPluginPluginExecImageQualityIndicator.synchronize()
                    if edPluginPluginExecImageQualityIndicator.dataOutput is not None:
                        if edPluginPluginExecImageQualityIndicator.dataOutput.imageQualityIndicators is not None:
                            xsDataImageQualityIndicators = XSDataImageQualityIndicators.parseString(\
                                    edPluginPluginExecImageQualityIndicator.dataOutput.imageQualityIndicators.marshal())
                self.xsDataResultControlImageQualityIndicators.addImageQualityIndicators(
                    xsDataImageQualityIndicators)

            for (edPluginControlDozor, listBatch) in listPluginDozor:
                edPluginControlDozor.synchronize()
                # Check that we got at least one result
                if len(edPluginControlDozor.dataOutput.imageDozor) == 0:
                    # Run the dozor plugin again, this time synchronously
                    firstImage = os.path.basename(listBatch[0].path.value)
                    lastImage = os.path.basename(listBatch[-1].path.value)
                    self.screen(
                        "No dozor results! Re-executing Dozor for images {0} to {1}"
                        .format(firstImage, lastImage))
                    time.sleep(5)
                    edPluginControlDozor = self.loadPlugin(
                        self.strPluginNameControlDozor,
                        "ControlDozor_reexecution_{0}".format(
                            os.path.splitext(firstImage)[0]))
                    xsDataInputControlDozor = XSDataInputControlDozor()
                    for image in listBatch:
                        xsDataInputControlDozor.addImage(XSDataFile(
                            image.path))
                    xsDataInputControlDozor.batchSize = XSDataInteger(
                        batchSize)
                    edPluginControlDozor.dataInput = xsDataInputControlDozor
                    edPluginControlDozor.executeSynchronous()
                for imageDozor in edPluginControlDozor.dataOutput.imageDozor:
                    for xsDataImageQualityIndicators in self.xsDataResultControlImageQualityIndicators.imageQualityIndicators:
                        if xsDataImageQualityIndicators.image.path.value == imageDozor.image.path.value:
                            xsDataImageQualityIndicators.dozor_score = imageDozor.mainScore
                            xsDataImageQualityIndicators.dozorSpotFile = imageDozor.spotFile
                            if imageDozor.spotFile is not None:
                                if os.path.exists(
                                        imageDozor.spotFile.path.value):
                                    numpyArray = numpy.loadtxt(
                                        imageDozor.spotFile.path.value,
                                        skiprows=3)
                                    xsDataImageQualityIndicators.dozorSpotList = XSDataString(
                                        base64.b64encode(
                                            numpyArray.tostring()))
                                    xsDataImageQualityIndicators.addDozorSpotListShape(
                                        XSDataInteger(numpyArray.shape[0]))
                                    if len(numpyArray.shape) > 1:
                                        xsDataImageQualityIndicators.addDozorSpotListShape(
                                            XSDataInteger(numpyArray.shape[1]))
                            xsDataImageQualityIndicators.dozorSpotsIntAver = imageDozor.spotsIntAver
                            xsDataImageQualityIndicators.dozorSpotsResolution = imageDozor.spotsResolution
                            xsDataImageQualityIndicators.dozorVisibleResolution = imageDozor.visibleResolution
                            if self.xsDataResultControlImageQualityIndicators.inputDozor is None:
                                if edPluginControlDozor.dataOutput.inputDozor is not None:
                                    self.xsDataResultControlImageQualityIndicators.inputDozor = XSDataDozorInput(
                                    ).parseString(
                                        edPluginControlDozor.dataOutput.
                                        inputDozor.marshal())
                if self.dataInput.doUploadToIspyb is not None and self.dataInput.doUploadToIspyb.value:
                    xsDataISPyBImageQualityIndicators = \
                        XSDataISPyBImageQualityIndicators.parseString(xsDataImageQualityIndicators.marshal())
                    xsDataInputStoreListOfImageQualityIndicators.addImageQualityIndicators(
                        xsDataISPyBImageQualityIndicators)

    #        print xsDataInputStoreListOfImageQualityIndicators.marshal()
            if self.dataInput.doUploadToIspyb is not None and self.dataInput.doUploadToIspyb.value:
                self.edPluginISPyB = self.loadPlugin(self.strISPyBPluginName)
                self.edPluginISPyB.dataInput = xsDataInputStoreListOfImageQualityIndicators
                self.edPluginISPyB.execute()
            #
            if bDoIndexing:
                # Find the 5 most intensive images (TIS):
                listImage = []
                # Check that we have dozor_score from all images:
                has_dozor_score = True
                for imageQualityIndicators in self.xsDataResultControlImageQualityIndicators.imageQualityIndicators:
                    if imageQualityIndicators.dozor_score is None:
                        has_dozor_score = False
                if has_dozor_score:
                    listSorted = sorted(
                        self.xsDataResultControlImageQualityIndicators.
                        imageQualityIndicators,
                        key=lambda imageQualityIndicators:
                        imageQualityIndicators.dozor_score.value)
                else:
                    listSorted = sorted(
                        self.xsDataResultControlImageQualityIndicators.
                        imageQualityIndicators,
                        key=lambda imageQualityIndicators:
                        imageQualityIndicators.totalIntegratedSignal.value)
                for xsDataResultControlImageQualityIndicator in listSorted[
                        -5:]:
                    if xsDataResultControlImageQualityIndicator.dozor_score.value > 1:
                        xsDataInputReadImageHeader = XSDataInputReadImageHeader(
                        )
                        xsDataInputReadImageHeader.image = XSDataFile(
                            xsDataResultControlImageQualityIndicator.image.path
                        )
                        self.edPluginReadImageHeader = self.loadPlugin(
                            self.strPluginReadImageHeaderName)
                        self.edPluginReadImageHeader.dataInput = xsDataInputReadImageHeader
                        self.edPluginReadImageHeader.executeSynchronous()
                        xsDataResultReadImageHeader = self.edPluginReadImageHeader.dataOutput
                        if xsDataResultReadImageHeader is not None:
                            edPluginLabelitIndexing = self.loadPlugin(
                                self.strIndexingLabelitPluginName)
                            xsDataInputLabelitIndexing = XSDataInputLabelitIndexing(
                            )
                            xsDataInputLabelitIndexing.image.append(
                                XSDataImage(
                                    xsDataResultControlImageQualityIndicator.
                                    image.path))
                            edPluginLabelitIndexing.setDataInput(
                                xsDataInputLabelitIndexing)
                            self.listPluginLabelit.append([
                                edPluginLabelitIndexing,
                                xsDataResultControlImageQualityIndicator
                            ])
                            edPluginLabelitIndexing.execute()
                for tupleLabelit in self.listPluginLabelit:
                    edPluginLabelitIndexing = tupleLabelit[0]
                    xsDataResultControlImageQualityIndicator = tupleLabelit[1]
                    edPluginLabelitIndexing.synchronize()
                    if not edPluginLabelitIndexing.isFailure(
                    ) and edPluginLabelitIndexing.dataOutput is not None:
                        xsDataResultLabelitIndexing = edPluginLabelitIndexing.getDataOutput(
                        )
                        xsDataIndexingResult = EDHandlerXSDataPhenixv1_1.generateXSDataIndexingResult(
                            xsDataResultLabelitIndexing)
                        selectedSolution = xsDataIndexingResult.selectedSolution
                        if selectedSolution is not None:
                            xsDataResultControlImageQualityIndicator.selectedIndexingSolution = selectedSolution
 def process(self, _edObject=None):
     EDPluginControl.process(self)
     self.DEBUG("EDPluginControlDozorv1_0.process")
     xsDataResultControlDozor = XSDataResultControlDozor()
     # Check if connection to ISPyB needed
     if self.dataInput.dataCollectionId is not None:
         edPluginRetrieveDataCollection = self.loadPlugin("EDPluginISPyBRetrieveDataCollectionv1_4")
         xsDataInputRetrieveDataCollection = XSDataInputRetrieveDataCollection()
         xsDataInputRetrieveDataCollection.dataCollectionId = self.dataInput.dataCollectionId
         edPluginRetrieveDataCollection.dataInput = xsDataInputRetrieveDataCollection
         edPluginRetrieveDataCollection.executeSynchronous()
         ispybDataCollection = edPluginRetrieveDataCollection.dataOutput.dataCollection
         batchSize = ispybDataCollection.numberOfImages
         if batchSize > self.maxBatchSize:
             batchSize = self.maxBatchSize
         if abs(ispybDataCollection.overlap) > 1:
             self.hasOverlap = True
             self.overlap = ispybDataCollection.overlap
         dictImage = self.createImageDictFromISPyB(ispybDataCollection)
     else:
         # No connection to ISPyB, take parameters from input
         if self.dataInput.batchSize is None:
             batchSize = 1
         else:
             batchSize = self.dataInput.batchSize.value
         dictImage = self.createImageDict(self.dataInput)
     listAllBatches = self.createListOfBatches(dictImage.keys(), batchSize)
     if dictImage[listAllBatches[0][0]].path.value.endswith("h5"):
         # Convert HDF5 images to CBF
         self.cbfTempDir = tempfile.mkdtemp(prefix="CbfTemp_")
         dictImage, self.hasHdf5Prefix = self.convertToCBF(dictImage)
     for listBatch in listAllBatches:
         # Read the header from the first image in the batch
         xsDataFile = dictImage[listBatch[0]]
         edPluginControlReadImageHeader = self.loadPlugin(self.strEDPluginControlReadImageHeaderName)
         xsDataInputReadImageHeader = XSDataInputReadImageHeader()
         xsDataInputReadImageHeader.image = xsDataFile
         edPluginControlReadImageHeader.dataInput = xsDataInputReadImageHeader
         edPluginControlReadImageHeader.executeSynchronous()
         subWedge = edPluginControlReadImageHeader.dataOutput.subWedge
         xsDataInputDozor = XSDataInputDozor()
         beam = subWedge.experimentalCondition.beam
         detector = subWedge.experimentalCondition.detector
         goniostat = subWedge.experimentalCondition.goniostat
         xsDataInputDozor.detectorType = detector.type
         xsDataInputDozor.exposureTime = XSDataDouble(beam.exposureTime.value)
         xsDataInputDozor.spotSize = XSDataInteger(3)
         xsDataInputDozor.detectorDistance = XSDataDouble(detector.distance.value)
         xsDataInputDozor.wavelength = XSDataDouble(beam.wavelength.value)
         #            xsDataInputDozor.fractionPolatization : XSDataDouble optional
         orgx = detector.beamPositionY.value / detector.pixelSizeY.value
         orgy = detector.beamPositionX.value / detector.pixelSizeX.value
         xsDataInputDozor.orgx = XSDataDouble(orgx)
         xsDataInputDozor.orgy = XSDataDouble(orgy)
         xsDataInputDozor.oscillationRange = XSDataDouble(goniostat.oscillationWidth.value)
         #            xsDataInputDozor.imageStep : XSDataDouble optional
         xsDataInputDozor.startingAngle = XSDataDouble(goniostat.rotationAxisStart.value)
         xsDataInputDozor.firstImageNumber = subWedge.image[0].number
         xsDataInputDozor.numberImages = XSDataInteger(len(listBatch))
         if self.hasOverlap:
             xsDataInputDozor.overlap = XSDataAngle(self.overlap)
         strFileName = subWedge.image[0].path.value
         strPrefix = EDUtilsImage.getPrefix(strFileName)
         strSuffix = EDUtilsImage.getSuffix(strFileName)
         if EDUtilsPath.isEMBL():
             strXDSTemplate = "%s_?????.%s" % (strPrefix, strSuffix)
         elif self.hasHdf5Prefix and not self.hasOverlap:
             strXDSTemplate = "%s_??????.%s" % (strPrefix, strSuffix)
         else:
             strXDSTemplate = "%s_????.%s" % (strPrefix, strSuffix)
         xsDataInputDozor.nameTemplateImage = XSDataString(
             os.path.join(os.path.dirname(strFileName), strXDSTemplate)
         )
         xsDataInputDozor.wedgeNumber = self.dataInput.wedgeNumber
         xsDataInputDozor.radiationDamage = self.dataInput.radiationDamage
         edPluginDozor = self.loadPlugin(self.strEDPluginDozorName, "Dozor_%05d" % subWedge.image[0].number.value)
         edPluginDozor.dataInput = xsDataInputDozor
         edPluginDozor.executeSynchronous()
         indexImage = 0
         for xsDataResultDozor in edPluginDozor.dataOutput.imageDozor:
             xsDataControlImageDozor = XSDataControlImageDozor()
             xsDataControlImageDozor.number = xsDataResultDozor.number
             xsDataControlImageDozor.image = dictImage[listBatch[indexImage]]
             xsDataControlImageDozor.spotsNumOf = xsDataResultDozor.spotsNumOf
             xsDataControlImageDozor.spotsIntAver = xsDataResultDozor.spotsIntAver
             xsDataControlImageDozor.spotsResolution = xsDataResultDozor.spotsResolution
             xsDataControlImageDozor.powderWilsonScale = xsDataResultDozor.powderWilsonScale
             xsDataControlImageDozor.powderWilsonBfactor = xsDataResultDozor.powderWilsonBfactor
             xsDataControlImageDozor.powderWilsonResolution = xsDataResultDozor.powderWilsonResolution
             xsDataControlImageDozor.powderWilsonCorrelation = xsDataResultDozor.powderWilsonCorrelation
             xsDataControlImageDozor.powderWilsonRfactor = xsDataResultDozor.powderWilsonRfactor
             xsDataControlImageDozor.mainScore = xsDataResultDozor.mainScore
             xsDataControlImageDozor.spotScore = xsDataResultDozor.spotScore
             xsDataControlImageDozor.visibleResolution = xsDataResultDozor.visibleResolution
             xsDataControlImageDozor.spotFile = xsDataResultDozor.spotFile
             xsDataControlImageDozor.angle = xsDataResultDozor.angle
             xsDataResultControlDozor.addImageDozor(xsDataControlImageDozor)
             if xsDataResultControlDozor.inputDozor is None:
                 xsDataResultControlDozor.inputDozor = XSDataDozorInput().parseString(xsDataInputDozor.marshal())
             indexImage += 1
         xsDataResultControlDozor.halfDoseTime = edPluginDozor.dataOutput.halfDoseTime
     self.dataOutput = xsDataResultControlDozor
     if self.cbfTempDir is not None:
         shutil.rmtree(self.cbfTempDir)
예제 #15
0
    def process(self, _edObject=None):
        EDPluginControl.process(self)
        self.DEBUG("EDPluginControlDozorv1_1.process")
        msg = "EDPluginControlDozorXmlRpcv1_0 started..."
        self.sendMessageToMXCuBE("Processing started...", "info")
        xsDataResultControlDozor = XSDataResultControlDozor()

        firstFileName = self.dataInput.template.value % (
            self.dataInput.first_run_number.value,
            self.dataInput.first_image_number.value)

        fWaitFileTimeout = 180  #sec

        self.waitFileFirst = self.loadPlugin("EDPluginMXWaitFilev1_1",
                                             "MXWaitFileFirst")
        xsDataInputMXWaitFileFirst = XSDataInputMXWaitFile()
        xsDataInputMXWaitFileFirst.file = XSDataFile(
            XSDataString(firstFileName))
        xsDataInputMXWaitFileFirst.timeOut = XSDataTime(fWaitFileTimeout)

        self.waitFileFirst.size = XSDataInteger(1000000)
        self.waitFileFirst.dataInput = xsDataInputMXWaitFileFirst
        self.waitFileFirst.executeSynchronous()
        if self.waitFileFirst.dataOutput.timedOut.value:
            strWarningMessage = "Timeout after %d seconds waiting for the first image %s!" % (
                fWaitFileTimeout, firstFileName)
            #self.addWarningMessage(strWarningMessage)
            #self.WARNING(strWarningMessage)
            #self.sendMessageToMXCuBE(strWarningMessage, "error")

        edPluginControlReadImageHeader = self.loadPlugin(
            self.strEDPluginControlReadImageHeaderName)
        xsDataInputReadImageHeader = XSDataInputReadImageHeader()
        xsDataInputReadImageHeader.image = XSDataFile(
            XSDataString(firstFileName))

        edPluginControlReadImageHeader.dataInput = xsDataInputReadImageHeader
        edPluginControlReadImageHeader.executeSynchronous()
        subWedge = edPluginControlReadImageHeader.dataOutput.subWedge

        xsDataInputDozor = XSDataInputDozor()
        beam = subWedge.experimentalCondition.beam
        detector = subWedge.experimentalCondition.detector
        goniostat = subWedge.experimentalCondition.goniostat
        xsDataInputDozor.detectorType = detector.type
        xsDataInputDozor.exposureTime = XSDataDouble(beam.exposureTime.value)
        xsDataInputDozor.spotSize = XSDataDouble(3.0)
        xsDataInputDozor.detectorDistance = XSDataDouble(
            detector.distance.value)
        xsDataInputDozor.wavelength = XSDataDouble(beam.wavelength.value)
        orgx = detector.beamPositionY.value / detector.pixelSizeY.value
        orgy = detector.beamPositionX.value / detector.pixelSizeX.value
        xsDataInputDozor.orgx = XSDataDouble(orgx)
        xsDataInputDozor.orgy = XSDataDouble(orgy)

        # GB: the 50 might need tunig to CPU speed and number of. 2000 is a current limit of Dozor.
        self.maxChunkSize = 4000  #a min (150 * 2527 * 2463 /detector.numberPixelX.value / detector.numberPixelY.value, 2000)

        _beamstop = self.beamstop(detector)
        if _beamstop is not None:
            #self.WARNING("Setting beamstop shadow: %s"%_beamstop)
            xsDataInputDozor.ixMin = XSDataInteger(_beamstop['ix_min'])
            xsDataInputDozor.iyMin = XSDataInteger(_beamstop['iy_min'])
            xsDataInputDozor.ixMax = XSDataInteger(_beamstop['ix_max'])
            xsDataInputDozor.iyMax = XSDataInteger(_beamstop['iy_max'])

        if self.dataInput.pixelMin is not None:
            xsDataInputDozor.pixelMin = self.dataInput.pixelMin

        if self.dataInput.pixelMax is not None:
            xsDataInputDozor.pixelMax = self.dataInput.pixelMax

        _serial = 0
        _startTime = time.time()
        chunk_list = self.schedule(goniostat.rotationAxisStart.value,
                                   goniostat.oscillationWidth.value)

        for chunk in chunk_list:

            if not self.poll_file(
                    self.dataInput.template.value %
                (chunk['run_number'], chunk['first'] + chunk['number_of'] - 1),
                (beam.exposureTime.value + 0.003) * chunk['number_of'] + 30):
                self.sendMessageToMXCuBE("Timeout waiting for frame: %d" %
                                         (chunk['first'] + chunk['number_of']),
                                         level="error")
                return

            xsDataInputDozor.oscillationRange = XSDataDouble(
                chunk['rotation_range'])
            xsDataInputDozor.startingAngle = XSDataDouble(
                chunk['rotation_start'])
            xsDataInputDozor.firstImageNumber = XSDataInteger(chunk['first'])
            xsDataInputDozor.numberImages = XSDataInteger(chunk['number_of'])

            strFileName = self.dataInput.template.value % (chunk['run_number'],
                                                           chunk['first'])
            strXDSTemplate = EDUtilsImage.getTemplate(strFileName, '?')
            xsDataInputDozor.nameTemplateImage = XSDataString(
                os.path.join(os.path.dirname(strFileName), strXDSTemplate))

            edPluginDozor = self.loadPlugin(self.strEDPluginDozorName, "Dozor")
            edPluginDozor.dataInput = xsDataInputDozor

            edPluginDozor.executeSynchronous()

            xsDataChunkResultControlImageDozor = XSDataResultControlDozor()

            dozor_batch_list = []  #[[1,4,3,8,6],
            #[2, 5, 3, 3, 7],[1, 3, 12, 11, 4],[4, 2, 3, 5, 7]]
            #ozor_image_dict = {}
            diff_image_count = 0

            xsDataControlImageDozor = XSDataControlImageDozor()

            for xsDataResultDozor in edPluginDozor.dataOutput.imageDozor:
                xsDataControlImageDozor = XSDataControlImageDozor()

                xsDataControlImageDozor.number = xsDataResultDozor.number
                strFileName = self.dataInput.template.value % (
                    chunk['run_number'], xsDataControlImageDozor.number.value)
                xsDataControlImageDozor.image = XSDataFile(
                    XSDataString(strFileName))

                xsDataControlImageDozor.spots_num_of = xsDataResultDozor.spots_num_of
                xsDataControlImageDozor.spots_int_aver = xsDataResultDozor.spots_int_aver
                xsDataControlImageDozor.spots_resolution = xsDataResultDozor.spots_resolution
                xsDataControlImageDozor.powder_wilson_scale = xsDataResultDozor.powder_wilson_scale
                xsDataControlImageDozor.powder_wilson_bfactor = xsDataResultDozor.powder_wilson_bfactor
                xsDataControlImageDozor.powder_wilson_resolution = xsDataResultDozor.powder_wilson_resolution
                xsDataControlImageDozor.powder_wilson_correlation = xsDataResultDozor.powder_wilson_correlation
                xsDataControlImageDozor.powder_wilson_rfactor = xsDataResultDozor.powder_wilson_rfactor
                xsDataControlImageDozor.score = xsDataResultDozor.score
                xsDataResultControlDozor.addImageDozor(xsDataControlImageDozor)

                dozor_batch_list.append(
                    (xsDataControlImageDozor.number.getValue(),
                     xsDataControlImageDozor.spots_num_of.getValue(),
                     xsDataControlImageDozor.spots_int_aver.getValue(),
                     xsDataControlImageDozor.spots_resolution.getValue(),
                     xsDataControlImageDozor.score.getValue()))
                if xsDataControlImageDozor.spots_num_of.getValue() > 0:
                    diff_image_count += 1
                xsDataChunkResultControlImageDozor.addImageDozor(
                    xsDataControlImageDozor)

            xsDataChunkResultControlImageDozor.exportToFile(
                "ResultControlDozor_Chunk_%06d.xml" % _serial)

            self.sendResultToMXCuBE(dozor_batch_list)

            _serial += 1
            self.screen("Chunk %d/%d done in %.3f seconds" % \
                 (_serial, len(chunk_list), time.time()-_startTime))
            self.sendMessageToMXCuBE("Chunk %d/%d done in %.2f sec., num diffr. frames: %d/%d" % \
                                     (_serial,
                                      len(chunk_list),
                                      time.time() - _startTime,
                                      diff_image_count,
                                      len(dozor_batch_list)
                                     )
                                    )

            _startTime = time.time()
    def process(self, _edPlugin=None):
        """
        Executes the execution plugins
        """
        EDPluginControl.process(self, _edPlugin)
        self.DEBUG("EDPluginControlImageQualityIndicatorsv1_5.process")
        EDUtilsParallel.initializeNbThread()
        # Check batch size
        if self.dataInput.batchSize is None:
            batchSize = 1
        else:
            batchSize = self.dataInput.batchSize.value
        self.screen("Image quality indicators batch size: {0}".format(batchSize))
        # Check if we should do distlSignalStrength:
        bDoDistlSignalStrength = True
        if self.dataInput.doDistlSignalStrength is not None:
            if not self.dataInput.doDistlSignalStrength.value:
                bDoDistlSignalStrength = False
        # Check if we should do indexing:
        bDoIndexing = False
        if self.dataInput.doIndexing is not None:
            if self.dataInput.doIndexing.value:
                 bDoIndexing = True
        # Check if fast mesh (for HDF5)
        isFastMesh = False
        if self.dataInput.fastMesh:
            isFastMesh = self.dataInput.fastMesh.value
        # Loop through all the incoming reference images
        if len(self.dataInput.image) == 0:
            directory = self.dataInput.directory.path.value
            template = self.dataInput.template.value
            startNo = self.dataInput.startNo.value
            endNo = self.dataInput.endNo.value
            listXSDataImage = []
            for index in range(startNo, endNo + 1):
                imageName = template.replace("####", "{0:04d}".format(index))
                imagePath = os.path.join(directory, imageName)
                xsDataImage = XSDataImage(path=XSDataString(imagePath), number=XSDataInteger(index))
                listXSDataImage.append(xsDataImage)
        else:
            listXSDataImage = self.dataInput.image
        xsDataInputMXWaitFile = XSDataInputMXWaitFile()
        self.xsDataResultControlImageQualityIndicators = XSDataResultControlImageQualityIndicators()
        listPluginDistl = []
        listPluginDozor = []
        listOfImagesInBatch = []
        listOfAllBatches = []
        indexBatch = 0
        listH5FilePath = []
        # Process data in batches
        for xsDataImage in listXSDataImage:
            listOfImagesInBatch.append(xsDataImage.copy())
            if len(listOfImagesInBatch) == batchSize:
                listOfAllBatches.append(listOfImagesInBatch)
                listOfImagesInBatch = []
        if len(listOfImagesInBatch) > 0:
            listOfAllBatches.append(listOfImagesInBatch)
            listOfImagesInBatch = []
        # Loop over batches
        for listOfImagesInBatch in listOfAllBatches:
            # First wait for images
            for image in listOfImagesInBatch:
                strPathToImage = image.path.value
                # If Eiger, just wait for the h5 file
                if strPathToImage.endswith(".h5"):
                    h5MasterFilePath, h5DataFilePath, hdf5ImageNumber = self.getH5FilePath(strPathToImage,
                                                                                           batchSize=batchSize,
                                                                                           isFastMesh=isFastMesh)
    #                print(h5FilePath)
    #                print(hdf5ImageNumber)
                    if not h5DataFilePath in listH5FilePath:
                        self.screen("ID30a3 Eiger data, waiting for master and data files...")
                        listH5FilePath.append(h5DataFilePath)
                        self.edPluginMXWaitFile = self.loadPlugin(self.strPluginMXWaitFileName)
                        xsDataInputMXWaitFile.file = XSDataFile(XSDataString(h5DataFilePath))
                        xsDataInputMXWaitFile.setSize(XSDataInteger(self.minImageSize))
                        xsDataInputMXWaitFile.setTimeOut(XSDataTime(self.fMXWaitFileTimeOut))
                        self.screen("Waiting for file {0}".format(h5DataFilePath))
                        self.DEBUG("Wait file timeOut set to %f" % self.fMXWaitFileTimeOut)
                        self.edPluginMXWaitFile.setDataInput(xsDataInputMXWaitFile)
                        self.edPluginMXWaitFile.executeSynchronous()
    #                    hdf5FilePath = strPathToImage.replace(".cbf", ".h5")
                        time.sleep(1)
                    if not os.path.exists(h5DataFilePath):
                        strError = "Time-out while waiting for image %s" % h5DataFilePath
                        self.error(strError)
                        self.addErrorMessage(strError)
                        self.setFailure()
                else:
                    if not os.path.exists(strPathToImage):
                        # self.screen("Waiting for file {0}".format(strPathToImage))
                        self.edPluginMXWaitFile = self.loadPlugin(self.strPluginMXWaitFileName)
                        xsDataInputMXWaitFile.file = XSDataFile(XSDataString(strPathToImage))
                        xsDataInputMXWaitFile.setSize(XSDataInteger(self.minImageSize))
                        xsDataInputMXWaitFile.setTimeOut(XSDataTime(self.fMXWaitFileTimeOut))
                        self.screen("Wait file timeOut set to %.0f s" % self.fMXWaitFileTimeOut)
                        self.edPluginMXWaitFile.setDataInput(xsDataInputMXWaitFile)
                        self.edPluginMXWaitFile.executeSynchronous()
                    if not os.path.exists(strPathToImage):
                        strError = "Time-out while waiting for image %s" % strPathToImage
                        self.error(strError)
                        self.addErrorMessage(strError)
                        self.setFailure()
            if not self.isFailure():
                strPathToFirstImage = listOfImagesInBatch[0].path.value
                if strPathToImage.endswith(".h5"):
                    indexLoop = 1
                    continueLoop = True
                    while continueLoop:
                        directory = os.path.dirname(strPathToFirstImage)
                        firstImage = EDUtilsImage.getImageNumber(listOfImagesInBatch[0].path.value)
                        lastImage = EDUtilsImage.getImageNumber(listOfImagesInBatch[-1].path.value)
                        xsDataInputH5ToCBF = XSDataInputH5ToCBF()
                        xsDataInputH5ToCBF.hdf5File = XSDataFile(listOfImagesInBatch[0].path)
                        xsDataInputH5ToCBF.hdf5ImageNumber = XSDataInteger(1)
                        xsDataInputH5ToCBF.startImageNumber = XSDataInteger(firstImage)
                        xsDataInputH5ToCBF.endImageNumber = XSDataInteger(lastImage)
                        xsDataInputH5ToCBF.forcedOutputDirectory = XSDataFile(XSDataString(directory))
                        edPluginH5ToCBF = self.loadPlugin("EDPluginH5ToCBFv1_1")
                        edPluginH5ToCBF.dataInput = xsDataInputH5ToCBF
                        edPluginH5ToCBF.execute()
                        edPluginH5ToCBF.synchronize()
                        outputCBFFileTemplate = edPluginH5ToCBF.dataOutput.outputCBFFileTemplate
                        if outputCBFFileTemplate is not None:
                            lastCbfFile = outputCBFFileTemplate.path.value.replace("######", "{0:06d}".format(EDUtilsImage.getImageNumber(listOfImagesInBatch[-1].path.value)))
                            strPathToImage = os.path.join(directory, lastCbfFile)
    #                        print(cbfFile.path.value)
                            if os.path.exists(strPathToImage):
                                # Rename all images
                                for image in listOfImagesInBatch:
                                    image.path.value = image.path.value.replace(".h5", ".cbf")
                                    imageNumber = EDUtilsImage.getImageNumber(image.path.value)
                                    oldPath = os.path.join(directory, outputCBFFileTemplate.path.value.replace("######", "{0:06d}".format(imageNumber)))
                                    newPath = os.path.join(directory, outputCBFFileTemplate.path.value.replace("######", "{0:04d}".format(imageNumber)))
                                    os.rename(oldPath, newPath)
                                lastCbfFile = outputCBFFileTemplate.path.value.replace("######", "{0:04d}".format(EDUtilsImage.getImageNumber(listOfImagesInBatch[-1].path.value)))
                                strPathToImage = os.path.join(directory, lastCbfFile)
                                self.screen("Image has been converted to CBF file: {0}".format(strPathToImage))
                                continueLoop = False
    #                    print(continueLoop)
                        if continueLoop:
                            self.screen("Still waiting for converting to CBF file: {0}".format(strPathToImage))
                            indexLoop += 1
                            time.sleep(5)
                            if indexLoop > 10:
                                continueLoop = False

                for image in listOfImagesInBatch:
                    strPathToImage = image.path.value
                    # Check if we should run distl.signalStrength
                    xsDataImageNew = XSDataImage(XSDataString(strPathToImage))
                    xsDataImageNew.number = XSDataInteger(EDUtilsImage.getImageNumber(image.path.value))
                    edPluginPluginExecImageQualityIndicator = None
                    if bDoDistlSignalStrength:
                        if self.bUseThinClient:
                            strPluginName = self.strPluginNameThinClient
                        else:
                            strPluginName = self.strPluginName
                        edPluginPluginExecImageQualityIndicator = self.loadPlugin(strPluginName)
                        self.listPluginExecImageQualityIndicator.append(edPluginPluginExecImageQualityIndicator)
                        xsDataInputDistlSignalStrength = XSDataInputDistlSignalStrength()
                        xsDataInputDistlSignalStrength.setReferenceImage(xsDataImageNew)
                        edPluginPluginExecImageQualityIndicator.setDataInput(xsDataInputDistlSignalStrength)
                        edPluginPluginExecImageQualityIndicator.execute()
                    listPluginDistl.append((xsDataImageNew.copy(), edPluginPluginExecImageQualityIndicator))

                edPluginControlDozor = self.loadPlugin(self.strPluginNameControlDozor,
                                                       "ControlDozor_{0}".format(os.path.splitext(os.path.basename(strPathToFirstImage))[0]))
                xsDataInputControlDozor = XSDataInputControlDozor()
                for image in listOfImagesInBatch:
                    xsDataInputControlDozor.addImage(XSDataFile(image.path))
                xsDataInputControlDozor.batchSize = XSDataInteger(len(listOfImagesInBatch))
                edPluginControlDozor.dataInput = xsDataInputControlDozor
                edPluginControlDozor.execute()
                listPluginDozor.append((edPluginControlDozor, list(listOfImagesInBatch)))

        if not self.isFailure():
            listIndexing = []
            # Synchronize all image quality indicator plugins and upload to ISPyB
            xsDataInputStoreListOfImageQualityIndicators = XSDataInputStoreListOfImageQualityIndicators()

            for (xsDataImage, edPluginPluginExecImageQualityIndicator) in listPluginDistl:
                xsDataImageQualityIndicators = XSDataImageQualityIndicators()
                xsDataImageQualityIndicators.image = xsDataImage.copy()
                if edPluginPluginExecImageQualityIndicator is not None:
                    edPluginPluginExecImageQualityIndicator.synchronize()
                    if edPluginPluginExecImageQualityIndicator.dataOutput is not None:
                        if edPluginPluginExecImageQualityIndicator.dataOutput.imageQualityIndicators is not None:
                            xsDataImageQualityIndicators = XSDataImageQualityIndicators.parseString(\
                                    edPluginPluginExecImageQualityIndicator.dataOutput.imageQualityIndicators.marshal())
                self.xsDataResultControlImageQualityIndicators.addImageQualityIndicators(xsDataImageQualityIndicators)

            for (edPluginControlDozor, listBatch) in listPluginDozor:
                edPluginControlDozor.synchronize()
                # Check that we got at least one result
                if len(edPluginControlDozor.dataOutput.imageDozor) == 0:
                    # Run the dozor plugin again, this time synchronously
                    firstImage = os.path.basename(listBatch[0].path.value)
                    lastImage = os.path.basename(listBatch[-1].path.value)
                    self.screen("No dozor results! Re-executing Dozor for images {0} to {1}".format(firstImage, lastImage))
                    time.sleep(5)
                    edPluginControlDozor = self.loadPlugin(self.strPluginNameControlDozor, "ControlDozor_reexecution_{0}".format(os.path.splitext(firstImage)[0]))
                    xsDataInputControlDozor = XSDataInputControlDozor()
                    for image in listBatch:
                        xsDataInputControlDozor.addImage(XSDataFile(image.path))
                    xsDataInputControlDozor.batchSize = XSDataInteger(batchSize)
                    edPluginControlDozor.dataInput = xsDataInputControlDozor
                    edPluginControlDozor.executeSynchronous()
                for imageDozor in edPluginControlDozor.dataOutput.imageDozor:
                    for xsDataImageQualityIndicators in self.xsDataResultControlImageQualityIndicators.imageQualityIndicators:
                        if xsDataImageQualityIndicators.image.path.value == imageDozor.image.path.value:
                            xsDataImageQualityIndicators.dozor_score = imageDozor.mainScore
                            xsDataImageQualityIndicators.dozorSpotFile = imageDozor.spotFile
                            if imageDozor.spotFile is not None:
                                if os.path.exists(imageDozor.spotFile.path.value):
                                    numpyArray = numpy.loadtxt(imageDozor.spotFile.path.value, skiprows=3)
                                    xsDataImageQualityIndicators.dozorSpotList = XSDataString(base64.b64encode(numpyArray.tostring()))
                                    xsDataImageQualityIndicators.addDozorSpotListShape(XSDataInteger(numpyArray.shape[0]))
                                    if len(numpyArray.shape) > 1:
                                        xsDataImageQualityIndicators.addDozorSpotListShape(XSDataInteger(numpyArray.shape[1]))
                            xsDataImageQualityIndicators.dozorSpotsIntAver = imageDozor.spotsIntAver
                            xsDataImageQualityIndicators.dozorSpotsResolution = imageDozor.spotsResolution
                            xsDataImageQualityIndicators.dozorVisibleResolution = imageDozor.visibleResolution
                            if self.xsDataResultControlImageQualityIndicators.inputDozor is None:
                                if edPluginControlDozor.dataOutput.inputDozor is not None:
                                    self.xsDataResultControlImageQualityIndicators.inputDozor = XSDataDozorInput().parseString(
                                                   edPluginControlDozor.dataOutput.inputDozor.marshal())
                if self.dataInput.doUploadToIspyb is not None and self.dataInput.doUploadToIspyb.value:
                    xsDataISPyBImageQualityIndicators = \
                        XSDataISPyBImageQualityIndicators.parseString(xsDataImageQualityIndicators.marshal())
                    xsDataInputStoreListOfImageQualityIndicators.addImageQualityIndicators(xsDataISPyBImageQualityIndicators)
    #        print xsDataInputStoreListOfImageQualityIndicators.marshal()
            if self.dataInput.doUploadToIspyb is not None and self.dataInput.doUploadToIspyb.value:
                self.edPluginISPyB = self.loadPlugin(self.strISPyBPluginName)
                self.edPluginISPyB.dataInput = xsDataInputStoreListOfImageQualityIndicators
                self.edPluginISPyB.execute()
            #
            if bDoIndexing:
                # Find the 5 most intensive images (TIS):
                listImage = []
                # Check that we have dozor_score from all images:
                has_dozor_score = True
                for imageQualityIndicators in self.xsDataResultControlImageQualityIndicators.imageQualityIndicators:
                    if imageQualityIndicators.dozor_score is None:
                        has_dozor_score = False
                if has_dozor_score:
                    listSorted = sorted(self.xsDataResultControlImageQualityIndicators.imageQualityIndicators,
                                        key=lambda imageQualityIndicators: imageQualityIndicators.dozor_score.value)
                else:
                    listSorted = sorted(self.xsDataResultControlImageQualityIndicators.imageQualityIndicators,
                                        key=lambda imageQualityIndicators: imageQualityIndicators.totalIntegratedSignal.value)
                for xsDataResultControlImageQualityIndicator in listSorted[-5:]:
                    if xsDataResultControlImageQualityIndicator.dozor_score.value > 1:
                        xsDataInputReadImageHeader = XSDataInputReadImageHeader()
                        xsDataInputReadImageHeader.image = XSDataFile(xsDataResultControlImageQualityIndicator.image.path)
                        self.edPluginReadImageHeader = self.loadPlugin(self.strPluginReadImageHeaderName)
                        self.edPluginReadImageHeader.dataInput = xsDataInputReadImageHeader
                        self.edPluginReadImageHeader.executeSynchronous()
                        xsDataResultReadImageHeader = self.edPluginReadImageHeader.dataOutput
                        if xsDataResultReadImageHeader is not None:
                            edPluginLabelitIndexing = self.loadPlugin(self.strIndexingLabelitPluginName)
                            xsDataInputLabelitIndexing = XSDataInputLabelitIndexing()
                            xsDataInputLabelitIndexing.image.append(XSDataImage(xsDataResultControlImageQualityIndicator.image.path))
                            edPluginLabelitIndexing.setDataInput(xsDataInputLabelitIndexing)
                            self.listPluginLabelit.append([edPluginLabelitIndexing, xsDataResultControlImageQualityIndicator])
                            edPluginLabelitIndexing.execute()
                for tupleLabelit in self.listPluginLabelit:
                    edPluginLabelitIndexing = tupleLabelit[0]
                    xsDataResultControlImageQualityIndicator = tupleLabelit[1]
                    edPluginLabelitIndexing.synchronize()
                    if not edPluginLabelitIndexing.isFailure() and edPluginLabelitIndexing.dataOutput is not None:
                        xsDataResultLabelitIndexing = edPluginLabelitIndexing.getDataOutput()
                        xsDataIndexingResult = EDHandlerXSDataPhenixv1_1.generateXSDataIndexingResult(xsDataResultLabelitIndexing)
                        selectedSolution = xsDataIndexingResult.selectedSolution
                        if selectedSolution is not None:
                            xsDataResultControlImageQualityIndicator.selectedIndexingSolution = selectedSolution