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.

115 lines
4.5 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. # -*- coding: utf-8 -*-
  2. from nd2reader.model import Image, ImageSet
  3. from nd2reader.parser import Nd2Parser
  4. import six
  5. class Nd2(Nd2Parser):
  6. """
  7. Allows easy access to NIS Elements .nd2 image files.
  8. """
  9. def __init__(self, filename):
  10. super(Nd2, self).__init__(filename)
  11. self._filename = filename
  12. def __repr__(self):
  13. return "\n".join(["<ND2 %s>" % self._filename,
  14. "Created: %s" % self._absolute_start.strftime("%Y-%m-%d %H:%M:%S"),
  15. "Image size: %sx%s (HxW)" % (self.height, self.width),
  16. "Image cycles: %s" % self._time_index_count,
  17. "Channels: %s" % ", ".join(["'%s'" % str(channel) for channel in self._channels]),
  18. "Fields of View: %s" % self._field_of_view_count,
  19. "Z-Levels: %s" % self._z_level_count
  20. ])
  21. def __len__(self):
  22. """
  23. This should be the total number of images in the ND2, but it may be inaccurate. If the ND2 contains a
  24. different number of images in a cycle (i.e. there are "gap" images) it will be higher than reality.
  25. :rtype: int
  26. """
  27. return self._image_count * self._channel_count
  28. @property
  29. def height(self):
  30. """
  31. :return: height of each image, in pixels
  32. :rtype: int
  33. """
  34. return self.metadata[six.b('ImageAttributes')][six.b('SLxImageAttributes')][six.b('uiHeight')]
  35. @property
  36. def width(self):
  37. """
  38. :return: width of each image, in pixels
  39. :rtype: int
  40. """
  41. return self.metadata[six.b('ImageAttributes')][six.b('SLxImageAttributes')][six.b('uiWidth')]
  42. def __iter__(self):
  43. """
  44. Iterates over every image, in the order they were taken.
  45. :return: model.Image()
  46. """
  47. for i in range(self._image_count):
  48. for fov in range(self._field_of_view_count):
  49. for z_level in range(self._z_level_count):
  50. for channel_name in self._channels:
  51. image = self.get_image(i, fov, channel_name, z_level)
  52. if image is not None:
  53. yield image
  54. @property
  55. def image_sets(self):
  56. """
  57. Iterates over groups of related images. This is useful if your ND2 contains multiple fields of view.
  58. A typical use case might be that you have, say, four areas of interest that you're monitoring, and every
  59. minute you take a bright field and GFP image of each one. For each cycle, this method would produce four
  60. ImageSet objects, each containing one bright field and one GFP image.
  61. :return: model.ImageSet()
  62. """
  63. for time_index in range(self._time_index_count):
  64. image_set = ImageSet()
  65. for fov in range(self._field_of_view_count):
  66. for channel_name in self._channels:
  67. for z_level in range(self._z_level_count):
  68. image = self.get_image(time_index, fov, channel_name, z_level)
  69. if image is not None:
  70. image_set.add(image)
  71. yield image_set
  72. def get_image(self, time_index, field_of_view, channel_name, z_level):
  73. """
  74. Returns an Image if data exists for the given parameters, otherwise returns None. In general, you should avoid
  75. using this method unless you're very familiar with the structure of ND2 files. If you have a use case that
  76. cannot be met by the `__iter__` or `image_sets` methods above, please create an issue on Github.
  77. :param time_index: the frame number
  78. :type time_index: int
  79. :param field_of_view: the label for the place in the XY-plane where this image was taken.
  80. :type field_of_view: int
  81. :param channel_name: the name of the color of this image
  82. :type channel_name: str
  83. :param z_level: the label for the location in the Z-plane where this image was taken.
  84. :type z_level: int
  85. :rtype: nd2reader.model.Image() or None
  86. """
  87. image_set_number = self._calculate_image_group_number(time_index, field_of_view, z_level)
  88. try:
  89. timestamp, raw_image_data = self._get_raw_image_data(image_set_number, self._channel_offset[channel_name])
  90. image = Image(timestamp, raw_image_data, field_of_view, channel_name, z_level, self.height, self.width)
  91. except TypeError:
  92. return None
  93. else:
  94. return image