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.

164 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.reader.metadata["height"] if self.reader.metadata["height"] is not None else 0
  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.reader.metadata["width"] if self.reader.metadata["width"] is not None else 0
  78. @property
  79. def z_levels(self):
  80. """Deprecated. Fetches the available z levels.
  81. Returns:
  82. list: z levels.
  83. """
  84. return self.reader.metadata["z_levels"]
  85. @property
  86. def fields_of_view(self):
  87. """Deprecated. Fetches the fields of view.
  88. Returns:
  89. list: fields of view.
  90. """
  91. return self.reader.metadata["fields_of_view"]
  92. @property
  93. def channels(self):
  94. """Deprecated. Fetches all color channels.
  95. Returns:
  96. list: the color channels.
  97. """
  98. return self.reader.metadata["channels"]
  99. @property
  100. def frames(self):
  101. """Deprecated. Fetches all frames.
  102. Returns:
  103. list: list of frames
  104. """
  105. return self.reader.metadata["frames"]
  106. @property
  107. def date(self):
  108. """Deprecated. Fetches the acquisition date.
  109. Returns:
  110. string: the date
  111. """
  112. return self.reader.metadata["date"]
  113. @property
  114. def pixel_microns(self):
  115. """Deprecated. Fetches the amount of microns per pixel.
  116. Returns:
  117. float: microns per pixel
  118. """
  119. return self.reader.metadata["pixel_microns"]