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.

105 lines
4.2 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 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. @property
  22. def height(self):
  23. """
  24. :return: height of each image, in pixels
  25. :rtype: int
  26. """
  27. return self.metadata[six.b('ImageAttributes')][six.b('SLxImageAttributes')][six.b('uiHeight')]
  28. @property
  29. def width(self):
  30. """
  31. :return: width of each image, in pixels
  32. :rtype: int
  33. """
  34. return self.metadata[six.b('ImageAttributes')][six.b('SLxImageAttributes')][six.b('uiWidth')]
  35. def __iter__(self):
  36. """
  37. Iterates over every image, in the order they were taken.
  38. :return: model.Image()
  39. """
  40. for i in range(self._image_count):
  41. for fov in range(self._field_of_view_count):
  42. for z_level in range(self._z_level_count):
  43. for channel_name in self._channels:
  44. image = self.get_image(i, fov, channel_name, z_level)
  45. if image is not None:
  46. yield image
  47. @property
  48. def image_sets(self):
  49. """
  50. Iterates over groups of related images. This is useful if your ND2 contains multiple fields of view.
  51. A typical use case might be that you have, say, four areas of interest that you're monitoring, and every
  52. minute you take a bright field and GFP image of each one. For each cycle, this method would produce four
  53. ImageSet objects, each containing one bright field and one GFP image.
  54. :return: model.ImageSet()
  55. """
  56. for time_index in range(self._time_index_count):
  57. image_set = ImageSet()
  58. for fov in range(self._field_of_view_count):
  59. for channel_name in self._channels:
  60. for z_level in range(self._z_level_count):
  61. image = self.get_image(time_index, fov, channel_name, z_level)
  62. if image is not None:
  63. image_set.add(image)
  64. yield image_set
  65. def get_image(self, time_index, field_of_view, channel_name, z_level):
  66. """
  67. Returns an Image if data exists for the given parameters, otherwise returns None. In general, you should avoid
  68. using this method unless you're very familiar with the structure of ND2 files. If you have a use case that
  69. cannot be met by the `__iter__` or `image_sets` methods above, please create an issue on Github.
  70. :param time_index: the frame number
  71. :type time_index: int
  72. :param field_of_view: the label for the place in the XY-plane where this image was taken.
  73. :type field_of_view: int
  74. :param channel_name: the name of the color of this image
  75. :type channel_name: str
  76. :param z_level: the label for the location in the Z-plane where this image was taken.
  77. :type z_level: int
  78. :rtype: nd2reader.model.Image() or None
  79. """
  80. image_set_number = self._calculate_image_group_number(time_index, field_of_view, z_level)
  81. try:
  82. timestamp, raw_image_data = self._get_raw_image_data(image_set_number, self._channel_offset[channel_name])
  83. image = Image(timestamp, raw_image_data, field_of_view, channel_name, z_level, self.height, self.width)
  84. except TypeError:
  85. return None
  86. else:
  87. return image