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.

167 lines
4.4 KiB

  1. """
  2. Legacy class for backwards compatibility
  3. """
  4. import warnings
  5. from nd2reader import ND2Reader
  6. class Nd2(object):
  7. """ Warning: this module is deprecated and only maintained for backwards compatibility with the non-PIMS version of
  8. nd2reader.
  9. """
  10. def __init__(self, filename):
  11. warnings.warn(
  12. "The 'Nd2' class is deprecated, please consider using the new ND2Reader interface which uses pims.",
  13. DeprecationWarning)
  14. self.reader = ND2Reader(filename)
  15. def __repr__(self):
  16. return "\n".join(["<Deprecated ND2 %s>" % self.reader.filename,
  17. "Created: %s" % (self.date if self.date is not None else "Unknown"),
  18. "Image size: %sx%s (HxW)" % (self.height, self.width),
  19. "Frames: %s" % len(self.frames),
  20. "Channels: %s" % ", ".join(["%s" % str(channel) for channel in self.channels]),
  21. "Fields of View: %s" % len(self.fields_of_view),
  22. "Z-Levels: %s" % len(self.z_levels)
  23. ])
  24. def __enter__(self):
  25. return self
  26. def __exit__(self, exc_type, exc_val, exc_tb):
  27. if self.reader is not None:
  28. self.reader.close()
  29. def __len__(self):
  30. return len(self.reader)
  31. def __getitem__(self, item):
  32. return self.reader[item]
  33. def select(self, fields_of_view=None, channels=None, z_levels=None, start=0, stop=None):
  34. """Select images based on criteria.
  35. Args:
  36. fields_of_view: the fields of view
  37. channels: the color channels
  38. z_levels: the z levels
  39. start: the starting frame
  40. stop: the last frame
  41. Returns:
  42. ND2Reader: Sliced ND2Reader which contains the frames
  43. """
  44. if stop is None:
  45. stop = len(self.frames)
  46. return self.reader[start:stop]
  47. def get_image(self, frame_number, field_of_view, channel_name, z_level):
  48. """Deprecated. Returns the specified image from the ND2Reader class.
  49. Args:
  50. frame_number: the frame number
  51. field_of_view: the field of view number
  52. channel_name: the name of the color channel
  53. z_level: the z level number
  54. Returns:
  55. Frame: the specified image
  56. """
  57. return self.reader.parser.get_image_by_attributes(frame_number, field_of_view, channel_name, z_level,
  58. self.height, self.width)
  59. def close(self):
  60. """Closes the ND2Reader
  61. """
  62. if self.reader is not None:
  63. self.reader.close()
  64. @property
  65. def height(self):
  66. """Deprecated. Fetches the height of the image.
  67. Returns:
  68. int: the pixel height of the image
  69. """
  70. return self._get_width_or_height("height")
  71. @property
  72. def width(self):
  73. """Deprecated. Fetches the width of the image.
  74. Returns:
  75. int: the pixel width of the image
  76. """
  77. return self._get_width_or_height("width")
  78. def _get_width_or_height(self, key):
  79. return self.reader.metadata[key] if self.reader.metadata[key] is not None else 0
  80. @property
  81. def z_levels(self):
  82. """Deprecated. Fetches the available z levels.
  83. Returns:
  84. list: z levels.
  85. """
  86. return self.reader.metadata["z_levels"]
  87. @property
  88. def fields_of_view(self):
  89. """Deprecated. Fetches the fields of view.
  90. Returns:
  91. list: fields of view.
  92. """
  93. return self.reader.metadata["fields_of_view"]
  94. @property
  95. def channels(self):
  96. """Deprecated. Fetches all color channels.
  97. Returns:
  98. list: the color channels.
  99. """
  100. return self.reader.metadata["channels"]
  101. @property
  102. def frames(self):
  103. """Deprecated. Fetches all frames.
  104. Returns:
  105. list: list of frames
  106. """
  107. return self.reader.metadata["frames"]
  108. @property
  109. def date(self):
  110. """Deprecated. Fetches the acquisition date.
  111. Returns:
  112. string: the date
  113. """
  114. return self.reader.metadata["date"]
  115. @property
  116. def pixel_microns(self):
  117. """Deprecated. Fetches the amount of microns per pixel.
  118. Returns:
  119. float: microns per pixel
  120. """
  121. return self.reader.metadata["pixel_microns"]