Exemplo n.º 1
0
    def getData(self):

        if (self.fileName.endswith('.raw') or self.fileName.endswith('.RAW')
                or self.fileName.endswith('.img')):
            File = open(str(self.fileName), "rb")
            size = os.path.getsize(str(self.fileName)) / 4.
            self.width = int(size**.5)
            self.height = self.width
            self.currentSlice = np.fromfile(File, dtype='<f4')
            self.currentSlice.resize(self.height, self.width)

        elif (self.fileName.endswith('.edf')
              or self.fileName.endswith('.EDF')):
            fileEdf = EdfFile(str(self.fileName), access='rb')
            self.currentSlice = fileEdf.GetData(0)

        elif (self.fileName.endswith('.DOWN')
              or self.fileName.endswith('.UP')):
            fileEdf = EdfFile(str(self.fileName), access='rb')
            self.currentSlice = fileEdf.GetData(0)

        elif (self.fileName.endswith('.tif') or self.fileName.endswith('.TIF')
              or self.fileName.endswith('.TIFF')
              or self.fileName.endswith('.tiff')):
            fileTif = TiffIO(str(self.fileName), mode='rb')
            self.currentSlice = fileTif.getImage(0)

        elif (self.fileName.endswith('.png')
              or self.fileName.endswith('.PNG')):
            filePng = mpimg.imread(self.fileName)
            self.currentSlice = filePng

        elif (self.fileName.endswith('.dcm')
              or self.fileName.endswith('.DCM')):
            image = dicom.read_file(str(self.fileName), force=True)

            self.currentSlice = image.pixel_array

        elif (self.fileName.endswith('.mat')
              or self.fileName.endswith('.MAT')):
            image = sio.loadmat(self.fileName)
            self.currentSlice = image[image.keys()[0].keys()[0]]

        else:
            image = dicom.read_file(str(self.fileName), force=True)

            self.currentSlice = image

        return self.currentSlice
Exemplo n.º 2
0
    def edf(self,
            x_start=0,
            x_end=0,
            x_step=1,
            y_start=0,
            y_end=0,
            y_step=1,
            z_start=0,
            z_end=0,
            z_step=1):
        """ 
        Read 3-D tomographic projection data from an EDF (ESRF) file.
        
        Parameters
        
        file_name : str
            Input edf file.
            
        x_start, x_end, x_step : scalar, optional
            Values of the start, end and step of the
            slicing for the whole array.
        
        y_start, y_end, y_step : scalar, optional
            Values of the start, end and step of the
            slicing for the whole array.
        
        z_start, z_end, z_step : scalar, optional
            Values of the start, end and step of the
            slicing for the whole array.
        
        Returns
        
        out : array
            Returns the data as a matrix.
        """

        # Read data from file.
        f = EdfFile(self.file_name, access='r')
        dic = f.GetStaticHeader(0)
        tmpdata = np.empty((f.NumImages, int(dic['Dim_2']), int(dic['Dim_1'])))

        for (i, ar) in enumerate(tmpdata):
            tmpdata[i::] = f.GetData(i)

        num_z, num_y, num_x = np.shape(tmpdata)
        if x_end is 0:
            x_end = num_x
        if y_end is 0:
            y_end = num_y
        if z_end is 0:
            z_end = num_z

        # Construct dataset from desired y.
        dataset = tmpdata[z_start:z_end:z_step, y_start:y_end:y_step,
                          x_start:x_end:x_step]
        return dataset
Exemplo n.º 3
0
    def edf2(self, x_start=0, x_end=0, x_step=1, y_start=0, y_end=0, y_step=1):
        """ 
        Read 2-D tomographic projection data from an EDF (ESRF) file.
        
        Parameters
        
        file_name : str
            Input edf file.
            
        x_start, x_end, x_step : scalar, optional
            Values of the start, end and step of the
            slicing for the whole array.
        
        y_start, y_end, y_step : scalar, optional
            Values of the start, end and step of the
            slicing for the whole array.
                
        Returns
        
        out : array
            Output 2-D matrix as numpy array.
        """
        try:
            # Read data from file.
            f = EdfFile(self.file_name, access='r')
            dic = f.GetStaticHeader(0)
            tmpdata = np.empty((int(dic['Dim_2']), int(dic['Dim_1'])))

            tmpdata[::] = f.GetData(0)

            num_y, num_x = np.shape(tmpdata)

            if x_end is 0:
                x_end = num_x
            if y_end is 0:
                y_end = num_y
            array = tmpdata[y_start:y_end:y_step, x_start:x_end:x_step]

        except KeyError:
            self.logger.error(
                "FILE DOES NOT CONTAIN A VALID TOMOGRAPHY DATA SET")
            array = None

        return array
Exemplo n.º 4
0
    def edf2(self, x_start=0, x_end=0, x_step=1, y_start=0, y_end=0, y_step=1):
        """ 
        Read 2-D tomographic projection data from an EDF (ESRF) file.
        
        Parameters
        
        file_name : str
            Input edf file.
            
        x_start, x_end, x_step : scalar, optional
            Values of the start, end and step of the
            slicing for the whole array.
        
        y_start, y_end, y_step : scalar, optional
            Values of the start, end and step of the
            slicing for the whole array.
                
        Returns
        
        out : array
            Output 2-D matrix as numpy array.
        """

        # Read data from file.
        f = EdfFile(self.file_name, access='r')
        dic = f.GetStaticHeader(0)
        tmpdata = np.empty((int(dic['Dim_2']), int(dic['Dim_1'])))

        tmpdata[::] = f.GetData(0)

        num_y, num_x = np.shape(tmpdata)

        if x_end is 0:
            x_end = num_x
        if y_end is 0:
            y_end = num_y

        return tmpdata[y_start:y_end:y_step, x_start:x_end:x_step]
Exemplo n.º 5
0
    def process(self, _edObject=None):
        EDPluginControl.process(self)
        EDVerbose.DEBUG("EDPluginBioSaxsMetadatav1_0.process")
        if not os.path.isfile(self.strInputImage):
            EDVerbose.WARNING("The given input file does not exist !!!")
            header = {}
        else:
            header = EdfFile(self.strInputImage).GetHeader(0)
            for key in EDUtilsBioSaxs.TRANSLATION:
                if key in dir(self) and self.__getattribute__(key) is None:
                    if EDUtilsBioSaxs.TRANSLATION[key] in header:
                        if key in EDUtilsBioSaxs.FLOAT_KEYS:
                            setattr(
                                self, key,
                                float(header[EDUtilsBioSaxs.TRANSLATION[key]]))
                        else:
                            setattr(self, key,
                                    header[EDUtilsBioSaxs.TRANSLATION[key]])
        if self.strOutputImage is not None:
            if os.path.abspath(self.strOutputImage) != os.path.abspath(
                    self.strInputImage):
                shutil.copy(self.strInputImage, self.strOutputImage)
            keyToUpgrade = []
            for key in EDUtilsBioSaxs.TRANSLATION:
                if key in dir(self) and self.__getattribute__(key) is None:
                    if EDUtilsBioSaxs.TRANSLATION[key] not in header:
                        keyToUpgrade.append(key)
                    else:

                        if key in EDUtilsBioSaxs.FLOAT_KEYS:
                            oneHeader = float(
                                header[EDUtilsBioSaxs.TRANSLATION[key]])
                        else:
                            oneHeader = header[EDUtilsBioSaxs.TRANSLATION[key]]
                        oneValue = eval("self.%s" % key)
                        EDVerbose.DEBUG(
                            "key: %s value_header=%s(%s) value_extra=%s(%s)" %
                            (key, oneHeader, oneHeader.__class__,
                             eval("self.%s" % key), eval(
                                 "self.%s" % key).__class__))
                        if oneHeader != oneValue:
                            keyToUpgrade.append(key)
            for key in keyToUpgrade:
                if not self.__bSaxsMetadataFailed:
                    xsdi = XSDataInputSaxsAddMetadatav1_0()
                    xsdi.setInputImage(self.xsdInputData.getOutputImage())
                    xsdi.setKey(XSDataString(EDUtilsBioSaxs.TRANSLATION[key]))
                    xsdi.setValue(XSDataString("%s" % eval("self.%s" % key)))
                    edPlugin = self.loadPlugin(
                        self.__strControlledPluginMetadata)
                    edPlugin.setDataInput(xsdi)
                    edPlugin.connectSUCCESS(self.doSuccessMetadata)
                    edPlugin.connectFAILURE(self.doFailureMetadata)
                    edPlugin.executeSynchronous()

            if self.__bSaxsMetadataFailed:
                EDVerbose.screen(
                    "EDPluginBioSaxsMetadatav1_0.process: writeMetadata using EdfFile "
                )
                edf = EdfFile(self.strOutputImage)
                headers = [
                    edf.GetHeader(i) for i in xrange(edf.GetNumImages())
                ]
                data = [edf.GetData(i) for i in xrange(edf.GetNumImages())]
                del edf
                for key in EDUtilsBioSaxs.TRANSLATION:
                    if key in dir(self) and self.__getattribute__(key) is None:
                        header[EDUtilsBioSaxs.TRANSLATION[key]] = eval(
                            "self.%s" % key)

                edf = EdfFile(self.strOutputImage)
                if len(data) == 1:
                    edf.WriteImage(header, data[0], Append=0)
                elif len(data) > 1:
                    edf.WriteImage(header, data[0], Append=0)
                    for datum in data[1:]:
                        edf.WriteImage({}, datum, Append=1)
                else:
                    EDVerbose.WARNING("There are not data in %s !!!" %
                                      self.strInputImage)
Exemplo n.º 6
0
roi = PowderIntegrator(cif)
pyintXpos, pyintYpos = roi.getSinogramPosition()

strLockFile = lockFile(suffix=os.path.splitext(strPathToCif)[0],
                       path='.',
                       basename="lock")

startWriteTime = time.time()

for pystSinogramFilename in [
        "sinogramPhotonFlux.edf", "sinogramIntegratedRaw.edf",
        "sinogramIntegratedCor.edf"
]:
    if os.path.isfile(pystSinogramFilename):
        edf = EdfFile(pystSinogramFilename)
        npSinogramArray = edf.GetData(0)
    else:
        npSinogramArray = roi.getEmptySinogramArray()
        edf = EdfFile(pystSinogramFilename)

    if pystSinogramFilename == "sinogramPhotonFlux.edf":
        npSinogramArray[pyintXpos, pyintYpos] = roi.pyfPhotonFlux
    elif pystSinogramFilename == "sinogramIntegratedRaw.edf":
        npSinogramArray[pyintXpos, pyintYpos] = roi.integrate()
    elif pystSinogramFilename == "sinogramIntegratedCor.edf":
        npSinogramArray[pyintXpos,
                        pyintYpos] = roi.integrate() / roi.pyfPhotonFlux
    edf.WriteImage(pydMetaDataEDF, npSinogramArray, Append=0)

pylRegionsOfInterest = []