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.

184 lines
4.8 KiB

10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. # nd2reader
  2. ### About
  3. `nd2reader` is a pure-Python package that reads images produced by NIS Elements.
  4. .nd2 files contain images and metadata, which can be split along multiple dimensions: time, fields of view (xy-plane), focus (z-plane), and filter channel.
  5. `nd2reader` produces data in numpy arrays, which makes it trivial to use with the image analysis packages such as `scikit-image` and `OpenCV`.
  6. ### Installation
  7. Dependencies will automatically be installed if you don't have them. That said, for optimal performance, you should
  8. install the following packages before installing nd2reader:
  9. #### Ubuntu
  10. `apt-get install python-numpy python-six` (Python 2.x)
  11. `apt-get install python3-numpy python3-six` (Python 3.x)
  12. #### Other operating systems
  13. These have not been tested yet.
  14. nd2reader is compatible with both Python 2.x and 3.x. I recommend installing using pip:
  15. `pip install nd2reader` (Python 2.x) or `pip3 install nd2reader` (Python 3.x)
  16. ### ND2s
  17. A quick summary of ND2 metadata can be obtained as shown below.
  18. ```python
  19. >>> import nd2reader
  20. >>> nd2 = nd2reader.Nd2("/path/to/my_images.nd2")
  21. >>> nd2
  22. <ND2 /path/to/my_images.nd2>
  23. Created: 2014-11-11 15:59:19
  24. Image size: 1280x800 (HxW)
  25. Image cycles: 636
  26. Channels: '', 'GFP'
  27. Fields of View: 8
  28. Z-Levels: 3
  29. ```
  30. You can also get some metadata about the nd2 programatically:
  31. ```python
  32. >>> nd2.height
  33. 1280
  34. >>> nd2.width
  35. 800
  36. >>> len(nd2)
  37. 30528
  38. ```
  39. ### Images
  40. Every method returns an `Image` object, which contains some metadata about the image as well as the
  41. raw pixel data itself. Images are always a 16-bit grayscale image. The `data` attribute holds the numpy array
  42. with the image data:
  43. ```python
  44. >>> image = nd2[20]
  45. >>> print(image.data)
  46. array([[1894, 1949, 1941, ..., 2104, 2135, 2114],
  47. [1825, 1846, 1848, ..., 1994, 2149, 2064],
  48. [1909, 1820, 1821, ..., 1995, 1952, 2062],
  49. ...,
  50. [3487, 3512, 3594, ..., 3603, 3643, 3492],
  51. [3642, 3475, 3525, ..., 3712, 3682, 3609],
  52. [3687, 3777, 3738, ..., 3784, 3870, 4008]], dtype=uint16)
  53. ```
  54. You can get a quick summary of image data by examining the `Image` object:
  55. ```python
  56. >>> image
  57. <ND2 Image>
  58. 1280x800 (HxW)
  59. Timestamp: 1699.79478134
  60. Field of View: 2
  61. Channel: GFP
  62. Z-Level: 1
  63. ```
  64. Or you can access it programmatically:
  65. ```python
  66. image = nd2[0]
  67. print(image.timestamp)
  68. print(image.field_of_view)
  69. print(image.channel)
  70. print(image.z_level)
  71. ```
  72. Often, you may want to just iterate over each image:
  73. ```python
  74. import nd2reader
  75. nd2 = nd2reader.Nd2("/path/to/my_images.nd2")
  76. for image in nd2:
  77. do_something(image.data)
  78. ```
  79. You can also get an image directly by indexing. Here, we look at the 38th image:
  80. ```python
  81. >>> nd2[37]
  82. <ND2 Image>
  83. 1280x800 (HxW)
  84. Timestamp: 1699.79478134
  85. Field of View: 2
  86. Channel: GFP
  87. Z-Level: 1
  88. ```
  89. Slicing is also supported and is extremely memory efficient, as images are only read when directly accessed:
  90. ```python
  91. my_subset = nd2[50:433]
  92. for image in my_subset:
  93. do_something(image.data)
  94. ```
  95. Step sizes are also accepted:
  96. ```python
  97. for image in nd2[:100:2]:
  98. # gets every other image in the first 100 images
  99. do_something(image.data)
  100. for image in nd2[::-1]:
  101. # iterate backwards over every image, if you're into that kind of thing
  102. do_something_image.data)
  103. ```
  104. ### Image Sets
  105. If you have complicated hierarchical data, it may be easier to use image sets, which groups images together if they
  106. share the same time index (not timestamp!) and field of view:
  107. ```python
  108. import nd2reader
  109. nd2 = nd2reader.Nd2("/path/to/my_complicated_images.nd2")
  110. for image_set in nd2.image_sets:
  111. # you can select images by channel
  112. gfp_image = image_set.get("GFP")
  113. do_something_gfp_related(gfp_image.data)
  114. # you can also specify the z-level. this defaults to 0 if not given
  115. out_of_focus_image = image_set.get("Bright Field", z_level=1)
  116. do_something_out_of_focus_related(out_of_focus_image.data)
  117. ```
  118. To get an image from an image set, you must specify a channel. It defaults to the 0th z-level, so if you have
  119. more than one z-level you will need to specify it when using `get`:
  120. ```python
  121. image = image_set.get("YFP")
  122. image = image_set.get("YFP", z_level=2)
  123. ```
  124. You can also see how many images are in your image set:
  125. ```python
  126. >>> len(image_set)
  127. 7
  128. ```
  129. ### Protips
  130. nd2reader is about 14 times faster under Python 3.4 compared to Python 2.7. If you know why, please get in touch!
  131. ### Bug Reports and Features
  132. If this fails to work exactly as expected, please open a Github issue. If you get an unhandled exception, please
  133. paste the entire stack trace into the issue as well.
  134. ### Contributing
  135. Please feel free to submit a pull request with any new features you think would be useful. You can also create an
  136. issue if you'd just like to propose or discuss a potential idea.
  137. ### Acknowledgments
  138. Support for the development of this package was provided by the [Finkelstein Laboratory](http://finkelsteinlab.org/).