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.

81 lines
2.1 KiB

8 years ago
8 years ago
8 years ago
  1. from pims import FramesSequenceND
  2. from nd2reader.parser import Parser
  3. class ND2Reader(FramesSequenceND):
  4. """
  5. PIMS wrapper for the ND2 parser
  6. """
  7. def __init__(self, filename):
  8. self.filename = filename
  9. # first use the parser to parse the file
  10. self._fh = open(filename, "rb")
  11. self._parser = Parser(self._fh)
  12. # Setup metadata
  13. self.metadata = self._parser.metadata
  14. # Set data type
  15. self._dtype = self._parser.get_dtype_from_metadata()
  16. # Setup the axes
  17. self._setup_axes()
  18. @classmethod
  19. def class_exts(cls):
  20. """
  21. So PIMS open function can use this reader for opening .nd2 files
  22. :return:
  23. """
  24. return {'nd2'} | super(ND2Reader, cls).class_exts()
  25. def close(self):
  26. """
  27. Correctly close the file handle
  28. :return:
  29. """
  30. if self._fh is not None:
  31. self._fh.close()
  32. def get_frame(self, i):
  33. """
  34. Return one frame
  35. :param i:
  36. :return:
  37. """
  38. c_name = self.metadata["channels"][0]
  39. return self._parser.get_image_by_attributes(i, 0, c_name, 0, self.metadata["height"], self.metadata["width"])
  40. def get_frame_2D(self, c, t, z):
  41. """
  42. Gets a given frame using the parser
  43. :param c:
  44. :param t:
  45. :param z:
  46. :return:
  47. """
  48. c_name = self.metadata["channels"][c]
  49. return self._parser.get_image_by_attributes(t, 0, c_name, z, self.metadata["height"], self.metadata["width"])
  50. @property
  51. def pixel_type(self):
  52. """
  53. Return the pixel data type
  54. :return:
  55. """
  56. return self._dtype
  57. def _setup_axes(self):
  58. """
  59. Setup the xyctz axes, iterate over t axis by default
  60. :return:
  61. """
  62. self._init_axis('x', self.metadata["width"])
  63. self._init_axis('y', self.metadata["height"])
  64. self._init_axis('c', len(self.metadata["channels"]))
  65. self._init_axis('t', len(self.metadata["frames"]))
  66. self._init_axis('z', len(self.metadata["z_levels"]))
  67. # provide the default
  68. self.iter_axes = 't'