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.

82 lines
2.0 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. return self._parser.get_image(i)
  39. def get_frame_2D(self, c, t, z):
  40. """
  41. Gets a given frame using the parser
  42. :param c:
  43. :param t:
  44. :param z:
  45. :return:
  46. """
  47. c_name = self.metadata["channels"][c]
  48. return self._parser.get_image_by_attributes(t, 0, c_name, z, self.metadata["width"], self.metadata["height"])
  49. @property
  50. def pixel_type(self):
  51. """
  52. Return the pixel data type
  53. :return:
  54. """
  55. return self._dtype
  56. def _setup_axes(self):
  57. """
  58. Setup the xyctz axes, iterate over t axis by default
  59. :return:
  60. """
  61. self._init_axis('x', self.metadata["width"])
  62. self._init_axis('y', self.metadata["height"])
  63. self._init_axis('c', len(self.metadata["channels"]))
  64. self._init_axis('t', len(self.metadata["frames"]))
  65. self._init_axis('z', len(self.metadata["z_levels"]))
  66. # provide the default
  67. self.iter_axes = 't'