예제 #1
0
 def get(self, index):
     ra = None
     try:
         # Read cell origin and dimensions for cell at index
         cellMin = zeros(3, 'l')  # long, 3 dimensions
         cellDims = zeros(3, 'i')  # integer, 3 dimensions
         grid.getCellDimensions(index, cellMin, cellDims)
         # Unpack Cell origin (in pixel coordinates)
         x, y, z = cellMin
         # Unpack Cell dimensions: at margins, may be smaller than cell_width, cell_height
         width, height, _ = cellDims  # ignore depth: it's 1
         # Read cell from file into a byte array
         ra = RandomAccessFile(filepaths[z], 'r')
         read_width = width * bytesPerPixel
         bytes = zeros(read_width * height, 'b')
         # Initial offset to the Cell origin
         offset = (section_width * y + x) * bytesPerPixel
         n_read = 0
         n_pixels = width * height
         # Read line by line
         while n_read < n_pixels:
             ra.seek(offset)
             ra.read(bytes, n_read, read_width)
             n_read += read_width
             offset += section_width * bytesPerPixel
         # Create a new Cell of the right pixel type
         return Cell(cellDims, cellMin, createAccess(bytes, bytesPerPixel))
     except:
         print sys.exc_info()
     finally:
         if ra:
             ra.close()
예제 #2
0
파일: io.py 프로젝트: mwinding/scripts
def parse_TIFF_IFDs(filepath):
    """ Returns a generator of dictionaries of tags for each IFD in the TIFF file,
      as defined by the 'parseIFD' function above. """
    ra = RandomAccessFile(filepath, 'r')
    try:
        # TIFF file format can have metadata at the end after the images, so the above approach can fail
        # TIFF file header is 8-bytes long:
        # (See: http://paulbourke.net/dataformats/tiff/tiff_summary.pdf )
        #
        # Bytes 1 and 2: identifier. Either the value 4949h (II) or 4D4Dh (MM),
        #                            meaning little-endian and big-endian, respectively.
        # All data encountered past the first two bytes in the file obey
        # the byte-ordering scheme indicated by the identifier field.
        b1, b2 = ra.read(), ra.read()  # as two java int, each one byte sized
        bigEndian = chr(b1) == 'M'
        parseNextInt = parseNextIntBigEndian if bigEndian else parseNextIntLittleEndian
        # Bytes 3 and 4: Version: Always 42
        ra.skipBytes(2)
        # Bytes 5,6,7,8: IFDOffset: offset to first image file directory (IFD), the metadata entry for the first image.
        nextIFDoffset = parseNextInt(ra, 4)  # offset to first IFD
        while nextIFDoffset != 0:
            ra.seek(nextIFDoffset)
            tags, nextIFDoffset = parseIFD(ra, parseNextInt)
            tags["bigEndian"] = bigEndian
            yield tags
    finally:
        ra.close()
예제 #3
0
def updateCmdForDeltaScanning(commandLine, Framework):
    originalScanFileFolderPath = CollectorsParameters.PROBE_MGR_INVENTORY_XMLENRICHER_FILES_FOLDER + XmlEnricherConstants.ORIGINAL_FOLDER_NAME
    originalScanFile = File(originalScanFileFolderPath, InventoryUtils.generateScanFileName(Framework))
    if originalScanFile.exists():
        scan = None
        try:
            try:
                buffer = jarray.zeros(0x24, 'b')
                fileSize = originalScanFile.length()
                if fileSize > 0x24:
                    scan = RandomAccessFile(originalScanFile, "r")
                    scan.readFully(buffer)
                    if (buffer[0] == 0x1F) and ((buffer[1] & 0xFF) == 0x8B) and (buffer[2] == 0x08):
                        scan.seek(fileSize - 8)
                        scan.readFully(buffer, 0, 8)
                        crc32 = getInt(buffer, 0)
                        size = getInt(buffer, 4)
                        deltaParams = ' -oldscanid:' + str(crc32) + ' -oldscansize:' + str(size) + ' '
                        index = String(commandLine).indexOf(ENTERPRISE_MODE) + String(ENTERPRISE_MODE).length()
                        commandLine = commandLine[0:index] + deltaParams + commandLine[index + 1:]
                        logger.debug('Scanner execution command updated to ', commandLine)
            except:
                logger.debugException("Failed to calculate CRC32 and size of zipped scan file " + originalScanFile.getAbsolutePath())
        finally:
            if scan is not None:
                try:
                    scan.close()
                except:
                    pass
    return commandLine
예제 #4
0
def readFIBSEMdat(path, channel_index=-1, header=1024, magic_number=3555587570, asImagePlus=False, toUnsigned=True):
  """ Read a file from Shan Xu's FIBSEM software, where two or more channels are interleaved.
      Assumes channels are stored in 16-bit.
      
      path: the file path to the .dat file.
      channel_index: the 0-based index of the channel to parse, or -1 (default) for all.
      header: defaults to a length of 1024 bytes
      magic_number: defaults to that for version 8 of Shan Xu's .dat image file format.
      isSigned: defaults to True, will subtract the min value when negative.
      asImagePlus: return a list of ImagePlus instead of ArrayImg which is the default.
  """
  ra = RandomAccessFile(path, 'r')
  try:
    # Check the magic number
    ra.seek(0)
    magic = ra.readInt() & 0xffffffff
    if magic != magic_number:
      msg = "magic number mismatch: v8 magic " + str(magic_number) + " != " + str(magic) + " for path:\n" + path
      System.out.println(msg)
      print msg
      # Continue: attempt to parse the file anyway
    # Read the number of channels
    ra.seek(32)
    numChannels = ra.readByte() & 0xff # a single byte as unsigned integer
    # Parse width and height
    ra.seek(100)
    width = ra.readInt()
    ra.seek(104)
    height = ra.readInt()
    # Read the whole interleaved pixel array
    ra.seek(header)
    bytes = zeros(width * height * 2 * numChannels, 'b') # 2 for 16-bit
    ra.read(bytes)
    # Parse as 16-bit array
    sb = ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN).asShortBuffer()
    bytes = None
  finally:
    ra.close()
  #
  shorts = zeros(width * height * numChannels, 'h')
  sb.get(shorts)
  sb = None
  # Deinterleave channels and convert to unsigned short
  # Shockingly, these values are signed shorts, not unsigned! (for first popeye2 squid volume, December 2021)
  # With ASM: fast
  channels = DAT_handler.deinterleave(shorts, numChannels, channel_index)
  shorts = None
  #
  if toUnsigned:
    for s in channels:
      DAT_handler.toUnsigned(s)
  # With python array sampling: very slow, and not just from iterating whole array once per channel
  #seq = xrange(numChannels) if -1 == channel_index else [channel_index]
  #channels = [shorts[i::numChannels] for i in seq]
  if asImagePlus:
    return [ImagePlus(str(i), ShortProcessor(width, height, s, None)) for i, s in enumerate(channels)]
  else:
    return [ArrayImgs.unsignedShorts(s, [width, height]) for s in channels]
예제 #5
0
def readBinaryMaskImg(filepath, width, height, depth, header_size):
  ra = RandomAccessFile(filepath, 'r')
  try:
    ra.skipBytes(header_size)
    bytes = zeros(width * height * depth, 'b')
    ra.read(bytes)
    return ArrayImgs.unsignedBytes(bytes, [width, height, depth])
  finally:
    ra.close()
예제 #6
0
 def get(self, index):
   IFD = self.IFDs[index]
   ra = RandomAccessFile(self.filepath, 'r')
   try:
     cell_position = [0, 0, index]
     pixels = read_TIFF_plane(ra, IFD) # a native array
     access = self.types[IFD["bitDepth"]][0] # e.g. ByteArray, FloatArray ...
     return Cell(self.cell_dimensions, cell_position, access(pixels))
   finally:
     ra.close()
예제 #7
0
def read2DImageROI(path, dimensions, interval, pixelType=UnsignedShortType, header=0, byte_order=ByteOrder.LITTLE_ENDIAN):
  """ Read a region of interest (the interval) of an image in a file.
      Assumes the image is written with the first dimension moving slowest.

      path: the file path to the image file.
      dimensions: a sequence of integer values e.g. [512, 512, 512]
      interval: two sequences of integer values defining the min and max coordinates, e.g.
                [[20, 0], [400, 550]]
      pixeltype: e.g. UnsignedShortType, FloatType
      header: defaults to zero, the number of bytes between the start of the file and the start of the image data.

      Supports only these types: UnsignedByteType, UnsignedShortType, FloatType.

      Returns an ArrayImg of the given type.
  """
  ra = RandomAccessFile(path, 'r')
  try:
    width, height = dimensions
    minX, minY = interval[0]
    maxX, maxY = interval[1]
    roi_width, roi_height = maxX - minX + 1, maxY - minY + 1
    tailX = width - roi_width - minX

    #print minX, minY
    #print maxX, maxY
    #print roi_width, roi_height

    size = roi_width * roi_height
    n_bytes_per_pixel = pixelType().getBitsPerPixel() / 8

    #print n_bytes_per_pixel

    bytes = zeros(size * n_bytes_per_pixel, 'b')

    # Read only the 2D ROI
    ra.seek(header + (minY * width + minX) * n_bytes_per_pixel)
    for h in xrange(roi_height):
      ra.readFully(bytes, h * roi_width * n_bytes_per_pixel, roi_width * n_bytes_per_pixel)
      ra.skipBytes((tailX + minX) * n_bytes_per_pixel)
    # Make an image
    roiDims = [roi_width, roi_height]
    if UnsignedByteType == pixelType:
      return ArrayImgs.unsignedBytes(bytes, roiDims)
    if UnsignedShortType == pixelType:
      shorts = zeros(size, 'h')
      ByteBuffer.wrap(bytes).order(byte_order).asShortBuffer().get(shorts)
      return ArrayImgs.shorts(shorts, roiDims)
    if FloatType == pixelType:
      floats = zeros(size, 'f')
      ByteBuffer.wrap(bytes).order(byte_order).asFloatBuffer().get(floats)
      return ArrayImgs.floats(floats, roiDims)
  finally:
    ra.close()
예제 #8
0
파일: io.py 프로젝트: mwinding/scripts
def readUnsignedBytes(path, dimensions, header=0):
    """ Read a file as an ArrayImg of UnsignedShortType """
    ra = RandomAccessFile(path, 'r')
    try:
        if header < 0:
            # Interpret from the end: useful for files with variable header lengths
            # such as some types of uncompressed TIFF formats
            header = ra.length() + header
        ra.skipBytes(header)
        bytes = zeros(reduce(operator.mul, dimensions), 'b')
        ra.read(bytes)
        return ArrayImgs.unsignedBytes(bytes, dimensions)
    finally:
        ra.close()
예제 #9
0
 def get(self, index):
     """ Assumes:
  - uncompressed image
  - one sample per pixel (one channel only)
 """
     IFD = self.IFDs[index]
     ra = RandomAccessFile(self.filepath, 'r')
     try:
         cell_dimensions = [IFD["width"], IFD["height"], 1]
         cell_position = [0, 0, index]
         pixels = read_TIFF_plane(ra, IFD)
         return Cell(cell_dimensions, cell_position,
                     self.types[pixels.typecode](pixels))
     finally:
         ra.close()
예제 #10
0
def readFIBSEMdat(path, channel_index=-1, header=1024, magic_number=3555587570):
  """ Read a file from Shan Xu's FIBSEM software, where two channels are interleaved.
      Assumes channels are stored in 16-bit.
      
      path: the file path to the .dat file.
      channel_index: the 0-based index of the channel to parse, or -1 (default) for all.
      header: defaults to a length of 1024 bytes
      magic_number: defaults to that for version 8 of Shan Xu's .dat image file format.
  """
  ra = RandomAccessFile(path, 'r')
  try:
    # Check the magic number
    ra.seek(0)
    if ra.readInt() & 0xffffffff != magic_number:
      print "Magic number mismatch"
      return None
    # Read the number of channels
    ra.seek(32)
    numChannels = ra.readByte() & 0xff # a single byte as unsigned integer
    # Parse width and height
    ra.seek(100)
    width = ra.readInt()
    ra.seek(104)
    height = ra.readInt()
    print numChannels, width, height
    # Read the whole interleaved pixel array
    ra.seek(header)
    bytes = zeros(width * height * 2 * numChannels, 'b') # 2 for 16-bit
    ra.read(bytes)
    print "read", len(bytes), "bytes" # takes ~2 seconds
    # Parse as 16-bit array
    sb = ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN).asShortBuffer()
    shorts = zeros(width * height * numChannels, 'h')
    sb.get(shorts)
    # Deinterleave channels
    # With Weaver: fast
    channels = w.deinterleave(shorts, numChannels, channel_index)
    # With python array sampling: very slow, and not just from iterating whole array once per channel
    # seq = xrange(numChannels) if -1 == channel_index else [channel_index]
    #channels = [shorts[i::numChannels] for i in seq]
    # With clojure: extremely slow, may be using reflection unexpectedly
    #channels = deinterleave.invoke(shorts, numChannels)
    print len(channels)
    # Shockingly, these values are signed shorts, not unsigned!
    return [ArrayImgs.shorts(s, [width, height]) for s in channels]
  finally:
    ra.close()
예제 #11
0
파일: io.py 프로젝트: mwinding/scripts
def readFloats(path, dimensions, header=0, byte_order=ByteOrder.LITTLE_ENDIAN):
    """ Read a file as an ArrayImg of FloatType """
    size = reduce(operator.mul, dimensions)
    ra = RandomAccessFile(path, 'r')
    try:
        if header < 0:
            # Interpret from the end: useful for files with variable header lengths
            # such as some types of uncompressed TIFF formats
            header = ra.length() + header
        ra.skipBytes(header)
        bytes = zeros(size * 4, 'b')
        ra.read(bytes)
        floats = zeros(size, 'f')
        ByteBuffer.wrap(bytes).order(byte_order).asFloatBuffer().get(floats)
        return ArrayImgs.floats(floats, dimensions)
    finally:
        ra.close()
예제 #12
0
def readUnsignedShorts(path, dimensions, header=0, return_array=False, byte_order=ByteOrder.LITTLE_ENDIAN):
  """ Read a file as an ArrayImg of UnsignedShortType """
  size = reduce(operator.mul, dimensions)
  ra = RandomAccessFile(path, 'r')
  try:
    if header < 0:
      # Interpret from the end: useful for files with variable header lengths
      # such as some types of uncompressed TIFF formats
      header = ra.length() + header
    ra.skipBytes(header)
    bytes = zeros(size * 2, 'b')
    ra.read(bytes)
    shorts = zeros(size, 'h') # h is for short
    ByteBuffer.wrap(bytes).order(byte_order).asShortBuffer().get(shorts)
    return shorts if return_array else ArrayImgs.unsignedShorts(shorts, dimensions)
  finally:
    ra.close()
예제 #13
0
def updateCmdForDeltaScanning(commandLine, Framework):
    originalScanFileFolderPath = CollectorsParameters.PROBE_MGR_INVENTORY_XMLENRICHER_FILES_FOLDER + XmlEnricherConstants.ORIGINAL_FOLDER_NAME
    originalScanFile = File(originalScanFileFolderPath,
                            InventoryUtils.generateScanFileName(Framework))
    if originalScanFile.exists():
        scan = None
        try:
            try:
                buffer = jarray.zeros(0x24, 'b')
                fileSize = originalScanFile.length()
                if fileSize > 0x24:
                    scan = RandomAccessFile(originalScanFile, "r")
                    scan.readFully(buffer)
                    if (buffer[0] == 0x1F) and (
                        (buffer[1] & 0xFF) == 0x8B) and (buffer[2] == 0x08):
                        scan.seek(fileSize - 8)
                        scan.readFully(buffer, 0, 8)
                        crc32 = getInt(buffer, 0)
                        size = getInt(buffer, 4)
                        deltaParams = ' -oldscanid:' + str(
                            crc32) + ' -oldscansize:' + str(size) + ' '
                        index = String(commandLine).indexOf(
                            ENTERPRISE_MODE) + String(
                                ENTERPRISE_MODE).length()
                        commandLine = commandLine[
                            0:index] + deltaParams + commandLine[index + 1:]
                        logger.debug('Scanner execution command updated to ',
                                     commandLine)
            except:
                logger.debugException(
                    "Failed to calculate CRC32 and size of zipped scan file " +
                    originalScanFile.getAbsolutePath())
        finally:
            if scan is not None:
                try:
                    scan.close()
                except:
                    pass
    return commandLine
 def get(self, index):
     ra = None
     try:
         # Read cell origin and dimensions for cell at index
         cellMin = zeros(3, 'l')  # long[3]
         cellDims = zeros(3, 'i')  # integer[3]
         grid.getCellDimensions(index, cellMin, cellDims)
         # Unpack Cell origin (in pixel coordinates)
         x, y, z = cellMin
         # Unpack Cell dimensions: at margins, may be smaller than cell_width, cell_height
         width, height, _ = cellDims  # ignore depth: it's 1
         # Read cell from file into a byte array
         ra = RandomAccessFile(filepaths[z], 'r')
         read_width = width * bytesPerPixel
         bytes = zeros(read_width * height,
                       'b')  # will contain the entire Cell pixel data
         # Initial offset to the Cell origin
         offset = (section_width * y + x) * bytesPerPixel
         n_pixels = width * height
         if width == section_width:
             # Read whole block in one go: cell data is continuous in the file
             ra.seek(offset)
             ra.read(bytes, 0, n_pixels * bytesPerPixel)
         else:
             # Read line by line
             n_read = 0
             while n_read < n_pixels:
                 ra.seek(offset)
                 ra.read(bytes, n_read, read_width)
                 n_read += read_width  # ensure n_read advances in case file is truncated to avoid infinite loop
                 offset += section_width * bytesPerPixel
         # Create a new Cell of the right pixel type
         return Cell(cellDims, cellMin, createAccess(bytes, bytesPerPixel))
     except:
         print sys.exc_info()
     finally:
         if ra:
             ra.close()
        # Bytes 3 and 4: Version: Always 42
        ra.skipBytes(2)
        # Bytes 5,6,7,8: IFDOffset: offset to first image file directory (IFD), the metadata entry for the first image.
        firstIFDoffset = parseNextInt(ra, 4)
        ra.skipBytes(firstIFDoffset - ra.getFilePointer()
                     )  # minus the current position: all offsets are absolute
        firstTags, _ = parseIFD(ra, parseNextInt)
        # Correct headerSize for TIFF files (and then assuming images are contiguous and in order,
        # which they don't have to be either in TIFF)
        headerSize = firstTags["offset"]
        # Sanity check:
        if width != firstTags["width"] or height != firstTags[
                "height"] or bitDepth != firstTags["samples_per_pixel"] * 8:
            print "TIFF header disagrees with ChannelSeparator's parsing of metadata."
    finally:
        ra.close()
"""

# Try to read the header size using OME as per Curtis Rueden's suggestion

from loci.common import RandomAccessInputStream, Location
ira = None
cs = None
try:
  #rais = RandomAccessInputStream(filepath)
  #Location.mapFile("my-rais", rais)
  cs = ChannelSeparator()
  cs.setId(filepath)
  Location.mapFile("my-rais", cs)
  ira = Location.getHandle("my-rais")
  print "IRA file pointer:", ira.getFilePointer()
예제 #16
0
def savePointMatches(img_filename1,
                     img_filename2,
                     pointmatches,
                     directory,
                     params,
                     coords_header=["x1", "y1", "x2", "y2"]):
    filename = basename(img_filename1) + '.' + basename(
        img_filename2) + ".pointmatches.csv"
    path = os.path.join(directory, filename)
    msg = [str(len(pointmatches))]
    ra = None
    try:
        """
    with open(path, 'w') as csvfile:
      w = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_NONNUMERIC)
      # First two rows: parameter names and values
      keys = params.keys()
      msg.append("keys: " + ",".join(map(str, keys)))
      msg.append("vals: " + ",".join(str(params[key]) for key in keys))
      #for pm in pointmatches:
      #  msg.append(", ".join(map(str, PointMatches.asRow(pm))))
      w.writerow(keys)
      w.writerow(tuple(params[key] for key in keys))
      # PointMatches header
      if 0 == len(pointmatches):
        # Can't know whether there are 2 or 3 dimensions per coordinate
        w.writerow(coords_header)
      else:
        w.writerow(PointMatches.csvHeader(next(iter(pointmatches)))) # support both lists and sets
      # One PointMatch per row
      for pm in pointmatches:
        w.writerow(PointMatches.asRow(pm))
      # Ensure it's written
      csvfile.flush()
      os.fsync(csvfile.fileno())
    """
        # DEBUG write differently, the above FAILS for ~20 out of 130,000 files
        lines = []
        keys = params.keys()
        lines.append(",".join(map(str, keys)))
        lines.append(",".join(map(str, (params[key] for key in keys))))
        header = coords_header if 0 == len(pointmatches) \
                               else PointMatches.csvHeader(next(iter(pointmatches)))
        lines.append(",".join(header))
        for pm in pointmatches:
            p1 = pm.getP1().getW()  # a double[] array
            p2 = pm.getP2().getW()  # a double[] array
            lines.append("%f,%f,%f,%f" % (p1[0], p1[1], p2[0], p2[1]))
        body = "\n".join(lines)
        ra = RandomAccessFile(path, 'rw')
        ra.writeBytes(body)
        ra.getFD().sync()  # ensure it's written
    except:
        syncPrintQ("Failed to save pointmatches at %s\n%s" %
                   (path, "\n".join(msg)))
        printException()
        if os.path.exists(path):
            os.remove(path)
    finally:
        if ra is not None:
            ra.close()