Ejemplo n.º 1
0
    def GetData(self,Index, DataType="",Pos=None,Size=None):
        """ Returns numpy array with image data
            Index:          The zero-based index of the image in the file
            DataType:       The edf type of the array to be returnd
                            If ommited, it is used the default one for the type
                            indicated in the image header
                            Attention to the absence of UnsignedShort,
                            UnsignedInteger and UnsignedLong types in
                            Numpy Python
                            Default relation between Edf types and NumPy's typecodes:
                                SignedByte          int8   b
                                UnsignedByte        uint8  B       
                                SignedShort         int16  h
                                UnsignedShort       uint16 H
                                SignedInteger       int32  i
                                UnsignedInteger     uint32 I
                                SignedLong          int32  i
                                UnsignedLong        uint32 I
                                Signed64            int64  (l in 64bit, q in 32 bit)
                                Unsigned64          uint64 (L in 64bit, Q in 32 bit) 
                                FloatValue          float32 f
                                DoubleValue         float64 d
            Pos:            Tuple (x) or (x,y) or (x,y,z) that indicates the begining
                            of data to be read. If ommited, set to the origin (0),
                            (0,0) or (0,0,0)
            Size:           Tuple, size of the data to be returned as x) or (x,y) or
                            (x,y,z) if ommited, is the distance from Pos to the end.

            If Pos and Size not mentioned, returns the whole data.                         
        """
        fastedf = self.fastedf
        if Index < 0 or Index >= self.NumImages: raise ValueError, "EdfFile: Index out of limit"
        if fastedf is None:fastedf = 0
        if Pos is None and Size is None:
            self.File.seek(self.Images[Index].DataPosition,0)
            datatype = self.__GetDefaultNumpyType__(self.Images[Index].DataType, index= Index)
            try:
                datasize = self.__GetSizeNumpyType__(datatype)
            except TypeError:
                print "What is the meaning of this error?"
                datasize = 8
            if self.Images[Index].NumDim==3:
                sizeToRead = self.Images[Index].Dim1 * \
                             self.Images[Index].Dim2 * \
                             self.Images[Index].Dim3 * datasize
                Data = numpy.fromstring(self.File.read(sizeToRead),
                            datatype)
                Data = numpy.reshape(Data, (self.Images[Index].Dim3,self.Images[Index].Dim2, self.Images[Index].Dim1))
            elif self.Images[Index].NumDim==2:
                sizeToRead = self.Images[Index].Dim1 * \
                             self.Images[Index].Dim2 * datasize
                Data = numpy.fromstring(self.File.read(sizeToRead),
                            datatype)
                #print "datatype = ",datatype
                #print "Data.type = ", Data.dtype.char
                #print "self.Images[Index].DataType ", self.Images[Index].DataType
                #print "Data.shape",Data.shape
                #print "datasize = ",datasize
                #print "sizeToRead ",sizeToRead 
                #print "lenData = ", len(Data)
                Data = numpy.reshape(Data, (self.Images[Index].Dim2, self.Images[Index].Dim1))
            elif self.Images[Index].NumDim==1:
                sizeToRead = self.Images[Index].Dim1 * datasize
                Data = numpy.fromstring(self.File.read(sizeToRead),
                            datatype)
        elif fastedf and CAN_USE_FASTEDF:
            type= self.__GetDefaultNumpyType__(self.Images[Index].DataType, index= Index)
            size_pixel=self.__GetSizeNumpyType__(type)
            Data=Numpy.array([],type)
            if self.Images[Index].NumDim==1:
                if Pos==None: Pos=(0,)
                if Size==None: Size=(0,)
                sizex=self.Images[Index].Dim1
                Size=list(Size)                
                if Size[0]==0:Size[0]=sizex-Pos[0]
                self.File.seek((Pos[0]*size_pixel)+self.Images[Index].DataPosition,0)
                Data = numpy.fromstring(self.File.read(Size[0]*size_pixel), type)
            elif self.Images[Index].NumDim==2:                
                if Pos==None: Pos=(0,0)
                if Size==None: Size=(0,0)
                Size=list(Size)
                sizex,sizey=self.Images[Index].Dim1,self.Images[Index].Dim2
                if Size[0]==0:Size[0]=sizex-Pos[0]
                if Size[1]==0:Size[1]=sizey-Pos[1]
                Data=numpy.zeros([Size[1],Size[0]],type)
                self.File.seek((((Pos[1]*sizex)+Pos[0])*size_pixel)+self.Images[Index].DataPosition,0)
                extended_fread(Data, Size[0]*size_pixel , numpy.array([Size[1]]),
                               numpy.array([sizex*size_pixel]) ,self.File)

            elif self.Images[Index].NumDim==3:
                if Pos==None: Pos=(0,0,0)
                if Size==None: Size=(0,0,0)
                Size=list(Size)
                sizex,sizey,sizez=self.Images[Index].Dim1,self.Images[Index].Dim2,self.Images[Index].Dim3
                if Size[0]==0:Size[0]=sizex-Pos[0]
                if Size[1]==0:Size[1]=sizey-Pos[1]
                if Size[2]==0:Size[2]=sizez-Pos[2]
                Data=numpy.zeros([Size[2],Size[1],Size[0]],type)
                self.File.seek(((((Pos[2]*sizey+Pos[1])*sizex)+Pos[0])*size_pixel)+self.Images[Index].DataPosition,0)
                extended_fread(Data, Size[0]*size_pixel , numpy.array([Size[2],Size[1]]),
                        numpy.array([ sizey*sizex*size_pixel , sizex*size_pixel]) ,self.File)

        else:
            if fastedf:print "I could not use fast routines"
            type= self.__GetDefaultNumpyType__(self.Images[Index].DataType, index= Index)
            size_pixel=self.__GetSizeNumpyType__(type)
            Data=numpy.array([],type)
            if self.Images[Index].NumDim==1:
                if Pos==None: Pos=(0,)
                if Size==None: Size=(0,)
                sizex=self.Images[Index].Dim1
                Size=list(Size)                
                if Size[0]==0:Size[0]=sizex-Pos[0]
                self.File.seek((Pos[0]*size_pixel)+self.Images[Index].DataPosition,0)
                Data = numpy.fromstring(self.File.read(Size[0]*size_pixel), type)
            elif self.Images[Index].NumDim==2:                
                if Pos==None: Pos=(0,0)
                if Size==None: Size=(0,0)
                Size=list(Size)
                sizex,sizey=self.Images[Index].Dim1,self.Images[Index].Dim2
                if Size[0]==0:Size[0]=sizex-Pos[0]
                if Size[1]==0:Size[1]=sizey-Pos[1]
                #print len(range(Pos[1],Pos[1]+Size[1])), "LECTURES OF ", Size[0], "POINTS"
                #print "sizex = ", sizex, "sizey = ", sizey
                Data = numpy.zeros((Size[1],Size[0]), type)
                dataindex =0
                for y in range(Pos[1],Pos[1]+Size[1]):
                    self.File.seek((((y*sizex)+Pos[0])*size_pixel)+self.Images[Index].DataPosition,0)
                    line = numpy.fromstring(self.File.read(Size[0]*size_pixel), type)
                    Data[dataindex,:] =  line[:]
                    #Data=numpy.concatenate((Data,line))
                    dataindex += 1
                #print "DataSize = ",Data.shape
                #print "Requested reshape = ",Size[1],'x',Size[0]
                #Data = numpy.reshape(Data, (Size[1],Size[0]))                            
            elif self.Images[Index].NumDim==3:
                if Pos==None: Pos=(0,0,0)
                if Size==None: Size=(0,0,0)
                Size=list(Size)
                sizex,sizey,sizez=self.Images[Index].Dim1,self.Images[Index].Dim2,self.Images[Index].Dim3
                if Size[0]==0:Size[0]=sizex-Pos[0]
                if Size[1]==0:Size[1]=sizey-Pos[1]
                if Size[2]==0:Size[2]=sizez-Pos[2]
                for z in range(Pos[2],Pos[2]+Size[2]):
                    for y in range(Pos[1],Pos[1]+Size[1]):
                        self.File.seek(((((z*sizey+y)*sizex)+Pos[0])*size_pixel)+self.Images[Index].DataPosition,0)
                        line = numpy.fromstring(self.File.read(Size[0]*size_pixel), type)
                        Data=numpy.concatenate((Data,line))                
                Data = numpy.reshape(Data, (Size[2],Size[1],Size[0]))

        if string.upper(self.SysByteOrder)!=string.upper(self.Images[Index].ByteOrder):
            Data=Data.byteswap()
        if DataType != "":
            Data=self.__SetDataType__ (Data,DataType)
        return Data
Ejemplo n.º 2
0
    def GetData(self, Index, DataType="", Pos=None, Size=None):
        """ Returns numeric array with image data
            Index:          The zero-based index of the image in the file
            DataType:       The edf type of the array to be returnd
                            If ommited, it is used the default one for the type
                            indicated in the image header
                            Attention to the absence of UnsignedShort,
                            UnsignedInteger and UnsignedLong types in
                            Numeric Python
                            Default relation between Edf types and NumPy's typecodes:
                                SignedByte          1
                                UnsignedByte        b       
                                SignedShort         s
                                UnsignedShort       w
                                SignedInteger       i
                                UnsignedInteger     u
                                SignedLong          l
                                UnsignedLong        u
                                FloatValue          f
                                DoubleValue         d
            Pos:            Tuple (x) or (x,y) or (x,y,z) that indicates the begining
                            of data to be read. If ommited, set to the origin (0),
                            (0,0) or (0,0,0)
            Size:           Tuple, size of the data to be returned as x) or (x,y) or
                            (x,y,z) if ommited, is the distance from Pos to the end.

            If Pos and Size not mentioned, returns the whole data.                         
        """
        fastedf = self.fastedf
        if Index < 0 or Index >= self.NumImages:
            raise "EdfFile: Index out of limit"
        if fastedf is None: fastedf = 0
        if Pos is None and Size is None:
            self.File.seek(self.Images[Index].DataPosition, 0)
            datatype = self.__GetDefaultNumericType__(
                self.Images[Index].DataType)
            if datatype in [1, "1", "b"]: datasize = 1
            elif datatype in ["s", "w"]: datasize = 2
            elif datatype in ["i", "u", "l", "f"]:
                #I assume 32 bit because longs are 8 bit in 64 bit machines
                datasize = 4
            else:
                datasize = 8
            if self.Images[Index].NumDim == 3:
                sizeToRead = self.Images[Index].Dim1 * \
                             self.Images[Index].Dim2 * \
                             self.Images[Index].Dim3 * datasize
                Data = Numeric.fromstring(self.File.read(sizeToRead), datatype)
                Data = Numeric.reshape(
                    Data, (self.Images[Index].Dim3, self.Images[Index].Dim2,
                           self.Images[Index].Dim1))
            elif self.Images[Index].NumDim == 2:
                sizeToRead = self.Images[Index].Dim1 * \
                             self.Images[Index].Dim2 * datasize
                Data = Numeric.fromstring(self.File.read(sizeToRead), datatype)
                #print "Data.type = ", Data.typecode()
                #print "self.Images[Index].DataType ", self.Images[Index].DataType
                #print Data.shape
                #print sizeToRead
                #print len(Data)
                Data = Numeric.reshape(
                    Data, (self.Images[Index].Dim2, self.Images[Index].Dim1))
        elif fastedf and CAN_USE_FASTEDF:
            type = self.__GetDefaultNumericType__(self.Images[Index].DataType)
            size_pixel = self.__GetSizeNumericType__(type)
            Data = Numeric.array([], type)
            if self.Images[Index].NumDim == 1:
                if Pos == None: Pos = (0, )
                if Size == None: Size = (0, )
                sizex = self.Images[Index].Dim1
                Size = list(Size)
                if Size[0] == 0: Size[0] = sizex - Pos[0]
                self.File.seek(
                    (Pos[0] * size_pixel) + self.Images[Index].DataPosition, 0)
                Data = Numeric.fromstring(self.File.read(Size[0] * size_pixel),
                                          type)
            elif self.Images[Index].NumDim == 2:
                if Pos == None: Pos = (0, 0)
                if Size == None: Size = (0, 0)
                Size = list(Size)
                sizex, sizey = self.Images[Index].Dim1, self.Images[Index].Dim2
                if Size[0] == 0: Size[0] = sizex - Pos[0]
                if Size[1] == 0: Size[1] = sizey - Pos[1]
                Data = Numeric.zeros([Size[1], Size[0]], type)
                self.File.seek((((Pos[1] * sizex) + Pos[0]) * size_pixel) +
                               self.Images[Index].DataPosition, 0)
                extended_fread(Data, Size[0] * size_pixel,
                               Numeric.array([Size[1]]),
                               Numeric.array([sizex * size_pixel]), self.File)

            elif self.Images[Index].NumDim == 3:
                if Pos == None: Pos = (0, 0, 0)
                if Size == None: Size = (0, 0, 0)
                Size = list(Size)
                sizex, sizey, sizez = self.Images[Index].Dim1, self.Images[
                    Index].Dim2, self.Images[Index].Dim3
                if Size[0] == 0: Size[0] = sizex - Pos[0]
                if Size[1] == 0: Size[1] = sizey - Pos[1]
                if Size[2] == 0: Size[2] = sizez - Pos[2]
                Data = Numeric.zeros([Size[2], Size[1], Size[0]], type)
                self.File.seek(
                    ((((Pos[2] * sizey + Pos[1]) * sizex) + Pos[0]) *
                     size_pixel) + self.Images[Index].DataPosition, 0)
                extended_fread(
                    Data, Size[0] * size_pixel,
                    Numeric.array([Size[2], Size[1]]),
                    Numeric.array(
                        [sizey * sizex * size_pixel, sizex * size_pixel]),
                    self.File)

        else:
            if fastedf: print "I could not use fast routines"
            type = self.__GetDefaultNumericType__(self.Images[Index].DataType)
            size_pixel = self.__GetSizeNumericType__(type)
            Data = Numeric.array([], type)
            if self.Images[Index].NumDim == 1:
                if Pos == None: Pos = (0, )
                if Size == None: Size = (0, )
                sizex = self.Images[Index].Dim1
                Size = list(Size)
                if Size[0] == 0: Size[0] = sizex - Pos[0]
                self.File.seek(
                    (Pos[0] * size_pixel) + self.Images[Index].DataPosition, 0)
                Data = Numeric.fromstring(self.File.read(Size[0] * size_pixel),
                                          type)
            elif self.Images[Index].NumDim == 2:
                if Pos == None: Pos = (0, 0)
                if Size == None: Size = (0, 0)
                Size = list(Size)
                sizex, sizey = self.Images[Index].Dim1, self.Images[Index].Dim2
                if Size[0] == 0: Size[0] = sizex - Pos[0]
                if Size[1] == 0: Size[1] = sizey - Pos[1]
                #print len(range(Pos[1],Pos[1]+Size[1])), "LECTURES OF ", Size[0], "POINTS"
                #print "sizex = ", sizex, "sizey = ", sizey
                Data = Numeric.zeros((Size[1], Size[0]), type)
                dataindex = 0
                for y in range(Pos[1], Pos[1] + Size[1]):
                    self.File.seek((((y * sizex) + Pos[0]) * size_pixel) +
                                   self.Images[Index].DataPosition, 0)
                    line = Numeric.fromstring(
                        self.File.read(Size[0] * size_pixel), type)
                    Data[dataindex, :] = line[:]
                    #Data=Numeric.concatenate((Data,line))
                    dataindex += 1
                #print "DataSize = ",Data.shape
                #print "Requested reshape = ",Size[1],'x',Size[0]
                #Data = Numeric.reshape(Data, (Size[1],Size[0]))
            elif self.Images[Index].NumDim == 3:
                if Pos == None: Pos = (0, 0, 0)
                if Size == None: Size = (0, 0, 0)
                Size = list(Size)
                sizex, sizey, sizez = self.Images[Index].Dim1, self.Images[
                    Index].Dim2, self.Images[Index].Dim3
                if Size[0] == 0: Size[0] = sizex - Pos[0]
                if Size[1] == 0: Size[1] = sizey - Pos[1]
                if Size[2] == 0: Size[2] = sizez - Pos[2]
                for z in range(Pos[2], Pos[2] + Size[2]):
                    for y in range(Pos[1], Pos[1] + Size[1]):
                        self.File.seek((((
                            (z * sizey + y) * sizex) + Pos[0]) * size_pixel) +
                                       self.Images[Index].DataPosition, 0)
                        line = Numeric.fromstring(
                            self.File.read(Size[0] * size_pixel), type)
                        Data = Numeric.concatenate((Data, line))
                Data = Numeric.reshape(Data, (Size[2], Size[1], Size[0]))

        if string.upper(self.SysByteOrder) != string.upper(
                self.Images[Index].ByteOrder):
            Data = Data.byteswapped()
        if DataType != "":
            Data = self.__SetDataType__(Data, DataType)
        return Data
Ejemplo n.º 3
0
    def GetData(self, Index, DataType="", Pos=None, Size=None):
        """ Returns numpy array with image data
            Index:          The zero-based index of the image in the file
            DataType:       The edf type of the array to be returnd
                            If ommited, it is used the default one for the type
                            indicated in the image header
                            Attention to the absence of UnsignedShort,
                            UnsignedInteger and UnsignedLong types in
                            Numpy Python
                            Default relation between Edf types and NumPy's typecodes:
                                SignedByte          int8   b
                                UnsignedByte        uint8  B       
                                SignedShort         int16  h
                                UnsignedShort       uint16 H
                                SignedInteger       int32  i
                                UnsignedInteger     uint32 I
                                SignedLong          int32  i
                                UnsignedLong        uint32 I
                                Signed64            int64  (l in 64bit, q in 32 bit)
                                Unsigned64          uint64 (L in 64bit, Q in 32 bit) 
                                FloatValue          float32 f
                                DoubleValue         float64 d
            Pos:            Tuple (x) or (x,y) or (x,y,z) that indicates the begining
                            of data to be read. If ommited, set to the origin (0),
                            (0,0) or (0,0,0)
            Size:           Tuple, size of the data to be returned as x) or (x,y) or
                            (x,y,z) if ommited, is the distance from Pos to the end.

            If Pos and Size not mentioned, returns the whole data.                         
        """
        fastedf = self.fastedf
        if Index < 0 or Index >= self.NumImages: raise ValueError, "EdfFile: Index out of limit"
        if fastedf is None:fastedf = 0
        if Pos is None and Size is None:
            if self.ADSC or self.MARCCD or self.PILATUS_CBF:
                return self.__data
            else:
                self.File.seek(self.Images[Index].DataPosition, 0)
                datatype = self.__GetDefaultNumpyType__(self.Images[Index].DataType, index=Index)
                try:
                    datasize = self.__GetSizeNumpyType__(datatype)
                except TypeError:
                    print "What is the meaning of this error?"
                    datasize = 8
                if self.Images[Index].NumDim == 3:
                    sizeToRead = self.Images[Index].Dim1 * \
                                 self.Images[Index].Dim2 * \
                                 self.Images[Index].Dim3 * datasize
                    Data = numpy.fromstring(self.File.read(sizeToRead),
                                datatype)
                    Data = numpy.reshape(Data, (self.Images[Index].Dim3, self.Images[Index].Dim2, self.Images[Index].Dim1))
                elif self.Images[Index].NumDim == 2:
                    sizeToRead = self.Images[Index].Dim1 * \
                                 self.Images[Index].Dim2 * datasize
                    Data = numpy.fromstring(self.File.read(sizeToRead),
                                datatype)
                    #print "datatype = ",datatype
                    #print "Data.type = ", Data.dtype.char
                    #print "self.Images[Index].DataType ", self.Images[Index].DataType
                    #print "Data.shape",Data.shape
                    #print "datasize = ",datasize
                    #print "sizeToRead ",sizeToRead 
                    #print "lenData = ", len(Data)
                    Data = numpy.reshape(Data, (self.Images[Index].Dim2, self.Images[Index].Dim1))
                elif self.Images[Index].NumDim == 1:
                    sizeToRead = self.Images[Index].Dim1 * datasize
                    Data = numpy.fromstring(self.File.read(sizeToRead),
                                datatype)
        elif self.ADSC or self.MARCCD or self.PILATUS_CBF:
            return self.__data[Pos[1]:(Pos[1] + Size[1]),
                               Pos[0]:(Pos[0] + Size[0])]
        elif fastedf and CAN_USE_FASTEDF:
            type = self.__GetDefaultNumpyType__(self.Images[Index].DataType, index=Index)
            size_pixel = self.__GetSizeNumpyType__(type)
            Data = numpy.array([], type)
            if self.Images[Index].NumDim == 1:
                if Pos == None: Pos = (0,)
                if Size == None: Size = (0,)
                sizex = self.Images[Index].Dim1
                Size = list(Size)
                if Size[0] == 0:Size[0] = sizex - Pos[0]
                self.File.seek((Pos[0] * size_pixel) + self.Images[Index].DataPosition, 0)
                Data = numpy.fromstring(self.File.read(Size[0] * size_pixel), type)
            elif self.Images[Index].NumDim == 2:
                if Pos == None: Pos = (0, 0)
                if Size == None: Size = (0, 0)
                Size = list(Size)
                sizex, sizey = self.Images[Index].Dim1, self.Images[Index].Dim2
                if Size[0] == 0:Size[0] = sizex - Pos[0]
                if Size[1] == 0:Size[1] = sizey - Pos[1]
                Data = numpy.zeros([Size[1], Size[0]], type)
                self.File.seek((((Pos[1] * sizex) + Pos[0]) * size_pixel) + self.Images[Index].DataPosition, 0)
                extended_fread(Data, Size[0] * size_pixel , numpy.array([Size[1]]),
                               numpy.array([sizex * size_pixel]) , self.File)

            elif self.Images[Index].NumDim == 3:
                if Pos == None: Pos = (0, 0, 0)
                if Size == None: Size = (0, 0, 0)
                Size = list(Size)
                sizex, sizey, sizez = self.Images[Index].Dim1, self.Images[Index].Dim2, self.Images[Index].Dim3
                if Size[0] == 0:Size[0] = sizex - Pos[0]
                if Size[1] == 0:Size[1] = sizey - Pos[1]
                if Size[2] == 0:Size[2] = sizez - Pos[2]
                Data = numpy.zeros([Size[2], Size[1], Size[0]], type)
                self.File.seek(((((Pos[2] * sizey + Pos[1]) * sizex) + Pos[0]) * size_pixel) + self.Images[Index].DataPosition, 0)
                extended_fread(Data, Size[0] * size_pixel , numpy.array([Size[2], Size[1]]),
                        numpy.array([ sizey * sizex * size_pixel , sizex * size_pixel]) , self.File)

        else:
            if fastedf:print "I could not use fast routines"
            type = self.__GetDefaultNumpyType__(self.Images[Index].DataType, index=Index)
            size_pixel = self.__GetSizeNumpyType__(type)
            Data = numpy.array([], type)
            if self.Images[Index].NumDim == 1:
                if Pos == None: Pos = (0,)
                if Size == None: Size = (0,)
                sizex = self.Images[Index].Dim1
                Size = list(Size)
                if Size[0] == 0:Size[0] = sizex - Pos[0]
                self.File.seek((Pos[0] * size_pixel) + self.Images[Index].DataPosition, 0)
                Data = numpy.fromstring(self.File.read(Size[0] * size_pixel), type)
            elif self.Images[Index].NumDim == 2:
                if Pos == None: Pos = (0, 0)
                if Size == None: Size = (0, 0)
                Size = list(Size)
                sizex, sizey = self.Images[Index].Dim1, self.Images[Index].Dim2
                if Size[0] == 0:Size[0] = sizex - Pos[0]
                if Size[1] == 0:Size[1] = sizey - Pos[1]
                #print len(range(Pos[1],Pos[1]+Size[1])), "LECTURES OF ", Size[0], "POINTS"
                #print "sizex = ", sizex, "sizey = ", sizey
                Data = numpy.zeros((Size[1], Size[0]), type)
                dataindex = 0
                for y in range(Pos[1], Pos[1] + Size[1]):
                    self.File.seek((((y * sizex) + Pos[0]) * size_pixel) + self.Images[Index].DataPosition, 0)
                    line = numpy.fromstring(self.File.read(Size[0] * size_pixel), type)
                    Data[dataindex, :] = line[:]
                    #Data=numpy.concatenate((Data,line))
                    dataindex += 1
                #print "DataSize = ",Data.shape
                #print "Requested reshape = ",Size[1],'x',Size[0]
                #Data = numpy.reshape(Data, (Size[1],Size[0]))                            
            elif self.Images[Index].NumDim == 3:
                if Pos == None: Pos = (0, 0, 0)
                if Size == None: Size = (0, 0, 0)
                Size = list(Size)
                sizex, sizey, sizez = self.Images[Index].Dim1, self.Images[Index].Dim2, self.Images[Index].Dim3
                if Size[0] == 0:Size[0] = sizex - Pos[0]
                if Size[1] == 0:Size[1] = sizey - Pos[1]
                if Size[2] == 0:Size[2] = sizez - Pos[2]
                for z in range(Pos[2], Pos[2] + Size[2]):
                    for y in range(Pos[1], Pos[1] + Size[1]):
                        self.File.seek(((((z * sizey + y) * sizex) + Pos[0]) * size_pixel) + self.Images[Index].DataPosition, 0)
                        line = numpy.fromstring(self.File.read(Size[0] * size_pixel), type)
                        Data = numpy.concatenate((Data, line))
                Data = numpy.reshape(Data, (Size[2], Size[1], Size[0]))

        if string.upper(self.SysByteOrder) != string.upper(self.Images[Index].ByteOrder):
            Data = Data.byteswap()
        if DataType != "":
            Data = self.__SetDataType__ (Data, DataType)
        return Data
Ejemplo n.º 4
0
    def GetData(self,Index, DataType="",Pos=None,Size=None):
        """ Returns numeric array with image data
            Index:          The zero-based index of the image in the file
            DataType:       The edf type of the array to be returnd
                            If ommited, it is used the default one for the type
                            indicated in the image header
                            Attention to the absence of UnsignedShort,
                            UnsignedInteger and UnsignedLong types in
                            Numeric Python
                            Default relation between Edf types and NumPy's typecodes:
                                SignedByte          1
                                UnsignedByte        b       
                                SignedShort         s
                                UnsignedShort       w
                                SignedInteger       i
                                UnsignedInteger     u
                                SignedLong          l
                                UnsignedLong        u
                                FloatValue          f
                                DoubleValue         d
            Pos:            Tuple (x) or (x,y) or (x,y,z) that indicates the begining
                            of data to be read. If ommited, set to the origin (0),
                            (0,0) or (0,0,0)
            Size:           Tuple, size of the data to be returned as x) or (x,y) or
                            (x,y,z) if ommited, is the distance from Pos to the end.

            If Pos and Size not mentioned, returns the whole data.                         
        """
        fastedf = self.fastedf
        if Index < 0 or Index >= self.NumImages: raise "EdfFile: Index out of limit"
        if fastedf is None:fastedf = 0
        if Pos is None and Size is None:
            self.File.seek(self.Images[Index].DataPosition,0)
            datatype = self.__GetDefaultNumericType__(self.Images[Index].DataType)
            if   datatype in [1, "1", "b"]:         datasize = 1
            elif datatype in ["s", "w"]:            datasize = 2
            elif datatype in ["i", "u", "l", "f"]:
                #I assume 32 bit because longs are 8 bit in 64 bit machines  
                datasize = 4
            else:datasize = 8
            if self.Images[Index].NumDim==3:
                sizeToRead = self.Images[Index].Dim1 * \
                             self.Images[Index].Dim2 * \
                             self.Images[Index].Dim3 * datasize
                Data = Numeric.fromstring(self.File.read(sizeToRead),
                            datatype)
                Data = Numeric.reshape(Data, (self.Images[Index].Dim3,self.Images[Index].Dim2, self.Images[Index].Dim1))
            elif self.Images[Index].NumDim==2:
                sizeToRead = self.Images[Index].Dim1 * \
                             self.Images[Index].Dim2 * datasize
                Data = Numeric.fromstring(self.File.read(sizeToRead),
                            datatype)
                #print "Data.type = ", Data.typecode()
                #print "self.Images[Index].DataType ", self.Images[Index].DataType
                #print Data.shape
                #print sizeToRead
                #print len(Data)
                Data = Numeric.reshape(Data, (self.Images[Index].Dim2, self.Images[Index].Dim1))
        elif fastedf and CAN_USE_FASTEDF:
            type= self.__GetDefaultNumericType__(self.Images[Index].DataType)
            size_pixel=self.__GetSizeNumericType__(type)
            Data=Numeric.array([],type)
            if self.Images[Index].NumDim==1:
                if Pos==None: Pos=(0,)
                if Size==None: Size=(0,)
                sizex=self.Images[Index].Dim1
                Size=list(Size)                
                if Size[0]==0:Size[0]=sizex-Pos[0]
                self.File.seek((Pos[0]*size_pixel)+self.Images[Index].DataPosition,0)
                Data = Numeric.fromstring(self.File.read(Size[0]*size_pixel), type)
            elif self.Images[Index].NumDim==2:                
                if Pos==None: Pos=(0,0)
                if Size==None: Size=(0,0)
                Size=list(Size)
                sizex,sizey=self.Images[Index].Dim1,self.Images[Index].Dim2
                if Size[0]==0:Size[0]=sizex-Pos[0]
                if Size[1]==0:Size[1]=sizey-Pos[1]
                Data=Numeric.zeros([Size[1],Size[0]],type)
                self.File.seek((((Pos[1]*sizex)+Pos[0])*size_pixel)+self.Images[Index].DataPosition,0)
                extended_fread(Data, Size[0]*size_pixel , Numeric.array([Size[1]]), Numeric.array([sizex*size_pixel]) ,self.File)

            elif self.Images[Index].NumDim==3:
                if Pos==None: Pos=(0,0,0)
                if Size==None: Size=(0,0,0)
                Size=list(Size)
                sizex,sizey,sizez=self.Images[Index].Dim1,self.Images[Index].Dim2,self.Images[Index].Dim3
                if Size[0]==0:Size[0]=sizex-Pos[0]
                if Size[1]==0:Size[1]=sizey-Pos[1]
                if Size[2]==0:Size[2]=sizez-Pos[2]
                Data=Numeric.zeros([Size[2],Size[1],Size[0]],type)
                self.File.seek(((((Pos[2]*sizey+Pos[1])*sizex)+Pos[0])*size_pixel)+self.Images[Index].DataPosition,0)
                extended_fread(Data, Size[0]*size_pixel , Numeric.array([Size[2],Size[1]]),
                        Numeric.array([ sizey*sizex*size_pixel , sizex*size_pixel]) ,self.File)

        else:
            if fastedf:print "I could not use fast routines"
            type= self.__GetDefaultNumericType__(self.Images[Index].DataType)
            size_pixel=self.__GetSizeNumericType__(type)
            Data=Numeric.array([],type)
            if self.Images[Index].NumDim==1:
                if Pos==None: Pos=(0,)
                if Size==None: Size=(0,)
                sizex=self.Images[Index].Dim1
                Size=list(Size)                
                if Size[0]==0:Size[0]=sizex-Pos[0]
                self.File.seek((Pos[0]*size_pixel)+self.Images[Index].DataPosition,0)
                Data = Numeric.fromstring(self.File.read(Size[0]*size_pixel), type)
            elif self.Images[Index].NumDim==2:                
                if Pos==None: Pos=(0,0)
                if Size==None: Size=(0,0)
                Size=list(Size)
                sizex,sizey=self.Images[Index].Dim1,self.Images[Index].Dim2
                if Size[0]==0:Size[0]=sizex-Pos[0]
                if Size[1]==0:Size[1]=sizey-Pos[1]
                #print len(range(Pos[1],Pos[1]+Size[1])), "LECTURES OF ", Size[0], "POINTS"
                #print "sizex = ", sizex, "sizey = ", sizey
                Data = Numeric.zeros((Size[1],Size[0]), type)
                dataindex =0
                for y in range(Pos[1],Pos[1]+Size[1]):
                    self.File.seek((((y*sizex)+Pos[0])*size_pixel)+self.Images[Index].DataPosition,0)
                    line = Numeric.fromstring(self.File.read(Size[0]*size_pixel), type)
                    Data[dataindex,:] =  line[:]
                    #Data=Numeric.concatenate((Data,line))
                    dataindex += 1
                #print "DataSize = ",Data.shape
                #print "Requested reshape = ",Size[1],'x',Size[0]
                #Data = Numeric.reshape(Data, (Size[1],Size[0]))                            
            elif self.Images[Index].NumDim==3:
                if Pos==None: Pos=(0,0,0)
                if Size==None: Size=(0,0,0)
                Size=list(Size)
                sizex,sizey,sizez=self.Images[Index].Dim1,self.Images[Index].Dim2,self.Images[Index].Dim3
                if Size[0]==0:Size[0]=sizex-Pos[0]
                if Size[1]==0:Size[1]=sizey-Pos[1]
                if Size[2]==0:Size[2]=sizez-Pos[2]
                for z in range(Pos[2],Pos[2]+Size[2]):
                    for y in range(Pos[1],Pos[1]+Size[1]):
                        self.File.seek(((((z*sizey+y)*sizex)+Pos[0])*size_pixel)+self.Images[Index].DataPosition,0)
                        line = Numeric.fromstring(self.File.read(Size[0]*size_pixel), type)
                        Data=Numeric.concatenate((Data,line))                
                Data = Numeric.reshape(Data, (Size[2],Size[1],Size[0]))

        if string.upper(self.SysByteOrder)!=string.upper(self.Images[Index].ByteOrder):
            Data=Data.byteswapped()
        if DataType != "":
            Data=self.__SetDataType__ (Data,DataType)
        return Data