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.

47 lines
2.1 KiB

10 years ago
10 years ago
10 years ago
  1. import logging
  2. from nd2reader.service import BaseNd2
  3. from nd2reader.model import Image, ImageSet
  4. log = logging.getLogger("nd2reader")
  5. log.addHandler(logging.StreamHandler())
  6. log.setLevel(logging.DEBUG)
  7. class Nd2(BaseNd2):
  8. def __init__(self, filename):
  9. super(Nd2, self).__init__(filename)
  10. def get_image(self, timepoint, fov, channel_name, z_level):
  11. image_set_number = self._calculate_image_set_number(timepoint, fov, z_level)
  12. timestamp, raw_image_data = self._reader.get_raw_image_data(image_set_number, self.channel_offset[channel_name])
  13. return Image(timestamp, raw_image_data, fov, channel_name, z_level, self.height, self.width)
  14. def __iter__(self):
  15. """
  16. Just return every image in order (might not be exactly the order that the images were physically taken, but it will
  17. be within a few seconds). A better explanation is probably needed here.
  18. """
  19. for i in range(self._image_count):
  20. for fov in range(self.field_of_view_count):
  21. for z_level in range(self._z_level_count):
  22. for channel in self.channels:
  23. image = self.get_image(i, fov, channel.name, z_level)
  24. if image.is_valid:
  25. yield image
  26. def image_sets(self, field_of_view, timepoints=None, channels=None, z_levels=None):
  27. """
  28. Gets all the images for a given field of view and
  29. """
  30. timepoint_set = xrange(self._timepoint_count) if timepoints is None else timepoints
  31. channel_set = [channel.name for channel in self.channels] if channels is None else channels
  32. z_level_set = xrange(self._z_level_count) if z_levels is None else z_levels
  33. for timepoint in timepoint_set:
  34. image_set = ImageSet()
  35. for channel_name in channel_set:
  36. for z_level in z_level_set:
  37. image = self.get_image(timepoint, field_of_view, channel_name, z_level)
  38. if image.is_valid:
  39. image_set.add(image)
  40. yield image_set