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.

149 lines
4.2 KiB

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