def writeHeader(self, hdr): Mrc.initHdrArrayFrom(self.fp.hdr, hdr) self.fp.hdr.Num = hdr.Num self.fp.hdr.PixelType = hdr.PixelType self.fp._initWhenHdrArraySet() if self.byteorder == '<': self.hdr.dvid = -16224 # little_indian number if (self.extInts is not None or self.extFloats is not None) or self.byteorder == '<': # old ImageJ assumes that the dv format (byteorder == <) has extended header if self.extInts is None: self.extInts = N.zeros((1,8)) nInts = self.extInts.shape[-1] if self.extFloats is None: self.extFloats = N.zeros((1,32)) nFloats = self.extFloats.shape[-1] self.fp = addExtHdrFromExt(self.fp, nInts, nFloats, self.extInts, self.extFloats) self.fp.hdr.NumIntegers = self.fp.extInts.shape[-1] self.fp.hdr.NumFloats = self.fp.extFloats.shape[-1] self.fp.setByteOrder(self.byteorder) self.fp.writeHeader() if hasattr(self.fp, 'extInts') or hasattr(self.fp, 'extFloats'): self.fp.writeExtHeader(True) self.hdr = self.fp.hdr
def writeHeader(self, hdr, extInts=None, extFloats=None, byteorder='='): Mrc.initHdrArrayFrom(self.fp.hdr, hdr) self.fp.hdr.Num = hdr.Num self.fp.hdr.PixelType = hdr.PixelType self.fp._initWhenHdrArraySet() if extInts is not None or extFloats is not None: if extInts is None: extInts = N.zeros((1, 1)) if extFloats is None: extFloats = N.zeros((1, 1)) self.fp = addExtHdrFromExt(self.fp, hdr.NumIntegers, hdr.NumFloats, extInts, extFloats) self.fp.hdr.NumIntegers = self.fp.extInts.shape[-1] self.fp.hdr.NumFloats = self.fp.extFloats.shape[-1] self.fp.setByteOrder(byteorder) self.fp.writeHeader() if hasattr(self.fp, 'extInts') or hasattr(self.fp, 'extFloats'): self.fp.writeExtHeader(True) self.hdr = self.fp.hdr self.extInts = extInts self.extFloats = extFloats
def makeHdr_like(hdrSrc): """ hdrSrc: header of the source return header """ hdr = Mrc.makeHdrArray() init_simple(hdr, hdrSrc.PixelType, hdrSrc.Num[::-1]) Mrc.initHdrArrayFrom(hdr, hdrSrc) hdr.NumTimes = hdrSrc.NumTimes hdr.NumWaves = hdrSrc.NumWaves hdr.NumIntegers = hdrSrc.NumIntegers hdr.NumFloats = hdrSrc.NumFloats return hdr
def writeHeader(self, hdr, extInts=None, extFloats=None): Mrc.initHdrArrayFrom(self.fp.hdr, hdr) self.fp.hdr.Num = hdr.Num self.fp.hdr.PixelType = hdr.PixelType self.fp._initWhenHdrArraySet() if self.byteorder == '<': self.hdr.dvid = -16224 # little_indian number if (extInts is not None or extFloats is not None) or self.byteorder == '<': # old ImageJ assumes that the dv format (byteorder == <) has extended header if extInts is None: extInts = N.zeros((1,8)) nInts = extInts.shape[-1] else: nInts = hdr.NumIntegers if extFloats is None: extFloats = N.zeros((1,32)) nFloats = extFloats.shape[-1] else: nFloats = hdr.NumFloats self.fp = addExtHdrFromExt(self.fp, nInts, nFloats, extInts, extFloats) self.fp.hdr.NumIntegers = self.fp.extInts.shape[-1] self.fp.hdr.NumFloats = self.fp.extFloats.shape[-1] self.fp.setByteOrder(self.byteorder) self.fp.writeHeader() if hasattr(self.fp, 'extInts') or hasattr(self.fp, 'extFloats'): self.fp.writeExtHeader(True) self.hdr = self.fp.hdr self.extInts = extInts self.extFloats = extFloats
def alignAndCrop(self, wavelengths = [], timepoints = [], savePath = None): """ Align and Crop the chosen channels/timepoints according to values already set in this DataDoc, and save the new MRC file result. """ if not wavelengths: wavelengths = range(self.size[0]) if not timepoints: timepoints = range(self.cropMin[1], self.cropMax[1]) # Generate the cropped shape of the file. croppedShape = [len(wavelengths)] for min, max in zip(self.cropMin[1:], self.cropMax[1:]): croppedShape.append(max - min) # Reorder to time/wavelength/z/y/x for saving. croppedShape[0], croppedShape[1] = croppedShape[1], croppedShape[0] croppedShape = tuple(croppedShape) newHeader = Mrc.makeHdrArray() Mrc.initHdrArrayFrom(newHeader, self.imageHeader) newHeader.Num = (croppedShape[4], croppedShape[3], croppedShape[2] * len(timepoints) * len(wavelengths)) newHeader.NumTimes = len(timepoints) newHeader.NumWaves = len(wavelengths) # Size of the extended header -- forced to zero for now. newHeader.next = 0 # Ordering of data in the file; 2 means z/w/t newHeader.ImgSequence = 2 newHeader.PixelType = Mrc.dtype2MrcMode(numpy.float32) if not savePath: outputArray = numpy.empty(croppedShape, numpy.float32) else: if self.filePath == savePath: # \todo Why do we do this? del self.image.Mrc # Write out the header. outputFile = file(savePath, 'wb') outputFile.write(newHeader._array.tostring()) # Slices to use to crop out the 3D volume we want to use for each # wave-timepoint pair. volumeSlices = [] for min, max in zip(self.cropMin[2:], self.cropMax[2:]): volumeSlices.append(slice(min, max)) for timepoint in timepoints: for waveIndex, wavelength in enumerate(wavelengths): volume = self.imageArray[wavelength][timepoint] dx, dy, dz, angle, zoom = self.alignParams[wavelength] if dz and self.size[2] == 1: dz = 0 # in 2D files Z translation blanks out the slice! if dx or dy or dz or angle or zoom != 1: # Transform the volume. volume2 = self.transformArray( volume, dx, dy, dz, angle, zoom ) else: volume2 = volume.copy() # no transform # Crop to the desired shape. volume2 = volume2[volumeSlices].astype(numpy.float32) if not savePath: outputArray[timepoint, waveIndex] = volume2 else: # Write to the file. for i, zSlice in enumerate(volume2): outputFile.write(zSlice) if not savePath: # Reorder to WTZYX since that's what the user expects. return outputArray.transpose([1, 0, 2, 3, 4]) else: outputFile.close()
def saveSelection(self, wavelengths = [], timepoints = [], savePath = None): """ Save a wavelength=channel and/or timepoint=frame selection. Basically a duplicate of parts of the alignAndCrop method below, which should now be refactored to use this method instead. Note that a new MRC object is created in the process. """ if not wavelengths: wavelengths = range(self.size[0]) if not timepoints: timepoints = range(self.cropMin[1], self.cropMax[1]) newShape = numpy.array([len(wavelengths), len(timepoints), self.size[2], self.size[3], self.size[4] ], dtype = numpy.int) # make a new header newHeader = Mrc.makeHdrArray() Mrc.initHdrArrayFrom(newHeader, self.imageHeader) newHeader.Num = (self.size[4], self.size[3], self.size[2] * len(timepoints) * len(wavelengths)) newHeader.NumTimes = len(timepoints) newHeader.NumWaves = len(wavelengths) # Size of the extended header -- forced to zero for now. newHeader.next = 0 # Ordering of data in the file; 2 means z/w/t newHeader.ImgSequence = 2 newHeader.PixelType = Mrc.dtype2MrcMode(numpy.float32) if not savePath: outputArray = numpy.empty(newShape, numpy.float32) else: if self.filePath == savePath: # \todo Why do we do this? del self.image.Mrc # update wavelength info to ensure it remains correct # (we could be re-ordering here) for waveIndex, wavelength in enumerate(wavelengths): trueWavelength = self.imageHeader.wave[wavelength] newHeader.wave[waveIndex] = trueWavelength # Write out the header. outputFile = file(savePath, 'wb') outputFile.write(newHeader._array.tostring()) for timepoint in timepoints: for waveIndex, wavelength in enumerate(wavelengths): volume = self.imageArray[wavelength][timepoint] if not savePath: outputArray[timepoint, waveIndex] = volume else: # Write to the file. for i, zSlice in enumerate(volume): outputFile.write(zSlice) if not savePath: # Reorder to WTZYX since that's what the user expects. return outputArray.transpose([1, 0, 2, 3, 4]) else: outputFile.close()