You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
5.4 KiB

  1. # -*- coding: utf-8 -*-
  2. import array
  3. import numpy as np
  4. import struct
  5. import six
  6. from nd2reader.model.image import Image
  7. class V3Driver(object):
  8. CHUNK_HEADER = 0xabeceda
  9. def __init__(self, metadata, label_map, file_handle):
  10. self._metadata = metadata
  11. self._label_map = label_map
  12. self._file_handle = file_handle
  13. def _calculate_field_of_view(self, frame_number):
  14. images_per_cycle = len(self._metadata.z_levels) * len(self._metadata.channels)
  15. return int((frame_number - (frame_number % images_per_cycle)) / images_per_cycle) % len(self._metadata.fields_of_view)
  16. def _calculate_channel(self, frame_number):
  17. return self._metadata.channels[frame_number % len(self._metadata.channels)]
  18. def _calculate_z_level(self, frame_number):
  19. return self._metadata.z_levels[int(((frame_number - (frame_number % len(self._metadata.channels))) / len(self._metadata.channels)) % len(self._metadata.z_levels))]
  20. def _calculate_image_group_number(self, time_index, fov, z_level):
  21. """
  22. Images are grouped together if they share the same time index, field of view, and z-level.
  23. :type time_index: int
  24. :type fov: int
  25. :type z_level: int
  26. :rtype: int
  27. """
  28. return time_index * len(self._metadata.fields_of_view) * len(self._metadata.z_levels) + (fov * len(self._metadata.z_levels) + z_level)
  29. def _calculate_frame_number(self, image_group_number, fov, z_level):
  30. return (image_group_number - (fov * len(self._metadata.z_levels) + z_level)) / (len(self._metadata.fields_of_view) * len(self._metadata.z_levels))
  31. def get_image(self, index):
  32. channel_offset = index % len(self._metadata.channels)
  33. fov = self._calculate_field_of_view(index)
  34. channel = self._calculate_channel(index)
  35. z_level = self._calculate_z_level(index)
  36. image_group_number = int(index / len(self._metadata.channels))
  37. frame_number = self._calculate_frame_number(image_group_number, fov, z_level)
  38. timestamp, image = self._get_raw_image_data(image_group_number, channel_offset, self._metadata.height, self._metadata.width)
  39. image.add_params(timestamp, frame_number, fov, channel, z_level)
  40. return image
  41. @property
  42. def _channel_offset(self):
  43. """
  44. Image data is interleaved for each image set. That is, if there are four images in a set, the first image
  45. will consist of pixels 1, 5, 9, etc, the second will be pixels 2, 6, 10, and so forth.
  46. :rtype: dict
  47. """
  48. channel_offset = {}
  49. for n, channel in enumerate(self._metadata.channels):
  50. channel_offset[channel] = n
  51. return channel_offset
  52. def _get_raw_image_data(self, image_group_number, channel_offset, height, width):
  53. """
  54. Reads the raw bytes and the timestamp of an image.
  55. :param image_group_number: groups are made of images with the same time index, field of view and z-level.
  56. :type image_group_number: int
  57. :param channel_offset: the offset in the array where the bytes for this image are found.
  58. :type channel_offset: int
  59. :return: (int, array.array()) or None
  60. """
  61. chunk = self._label_map[six.b("ImageDataSeq|%d!" % image_group_number)]
  62. data = self._read_chunk(chunk)
  63. # All images in the same image group share the same timestamp! So if you have complicated image data,
  64. # your timestamps may not be entirely accurate. Practically speaking though, they'll only be off by a few
  65. # seconds unless you're doing something super weird.
  66. timestamp = struct.unpack("d", data[:8])[0]
  67. image_group_data = array.array("H", data)
  68. image_data_start = 4 + channel_offset
  69. # The images for the various channels are interleaved within the same array. For example, the second image
  70. # of a four image group will be composed of bytes 2, 6, 10, etc. If you understand why someone would design
  71. # a data structure that way, please send the author of this library a message.
  72. image_data = np.reshape(image_group_data[image_data_start::len(self._metadata.channels)], (height, width))
  73. # Skip images that are all zeros! This is important, since NIS Elements creates blank "gap" images if you
  74. # don't have the same number of images each cycle. We discovered this because we only took GFP images every
  75. # other cycle to reduce phototoxicity, but NIS Elements still allocated memory as if we were going to take
  76. # them every cycle.
  77. if np.any(image_data):
  78. return timestamp, Image(image_data)
  79. return None
  80. def _read_chunk(self, chunk_location):
  81. """
  82. Gets the data for a given chunk pointer
  83. :rtype: bytes
  84. """
  85. self._file_handle.seek(chunk_location)
  86. # The chunk metadata is always 16 bytes long
  87. chunk_metadata = self._file_handle.read(16)
  88. header, relative_offset, data_length = struct.unpack("IIQ", chunk_metadata)
  89. if header != V3Driver.CHUNK_HEADER:
  90. raise ValueError("The ND2 file seems to be corrupted.")
  91. # We start at the location of the chunk metadata, skip over the metadata, and then proceed to the
  92. # start of the actual data field, which is at some arbitrary place after the metadata.
  93. self._file_handle.seek(chunk_location + 16 + relative_offset)
  94. return self._file_handle.read(data_length)