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.

151 lines
4.9 KiB

10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
10 years ago
  1. # nd2reader
  2. ### Don't use this library, use Micro-Manager
  3. I am no longer supporting this library, as my lab has discovered [Micro-Manager](https://micro-manager.org/) and found it to be a far superior application for acquiring microscope data.
  4. It provides all of the features of NIS Elements that my lab cares about and has none of the bugs/drawbacks.
  5. I will not be accepting pull requests at this time. If you find a bug, you can fork the repo and fix it yourself, or look for someone else's fork which may already contain a fix.
  6. If you would like to take control of the nd2reader namespace on PyPI, please shoot me an email. I'm not going to just give it away but I will consider it for someone who produces a high-quality fork that's widely-used.
  7. ### About
  8. `nd2reader` is a pure-Python package that reads images produced by NIS Elements 4.0+. It has only been definitively tested on NIS Elements 4.30.02 Build 1053. Support for older versions is being actively worked on.
  9. .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.
  10. `nd2reader` loads images as Numpy arrays, which makes it trivial to use with the image analysis packages such as `scikit-image` and `OpenCV`.
  11. ### Installation
  12. If you don't already have the packages `numpy`, `six` and `xmltodict`, they will be installed automatically:
  13. `pip3 install nd2reader` for Python 3.x
  14. `pip install nd2reader` for Python 2.x
  15. `nd2reader` is an order of magnitude faster in Python 3. I recommend using it unless you have no other choice.
  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: 'brightfield', 'GFP'
  27. Fields of View: 8
  28. Z-Levels: 3
  29. ```
  30. You can iterate over each image in the order they were acquired:
  31. ```python
  32. import nd2reader
  33. nd2 = nd2reader.Nd2("/path/to/my_images.nd2")
  34. for image in nd2:
  35. do_something(image)
  36. ```
  37. `Image` objects are just Numpy arrays with some extra metadata bolted on:
  38. ```python
  39. >>> image = nd2[20]
  40. >>> image
  41. array([[1894, 1949, 1941, ..., 2104, 2135, 2114],
  42. [1825, 1846, 1848, ..., 1994, 2149, 2064],
  43. [1909, 1820, 1821, ..., 1995, 1952, 2062],
  44. ...,
  45. [3487, 3512, 3594, ..., 3603, 3643, 3492],
  46. [3642, 3475, 3525, ..., 3712, 3682, 3609],
  47. [3687, 3777, 3738, ..., 3784, 3870, 4008]], dtype=uint16)
  48. >>> image.timestamp
  49. 10.1241241248
  50. >>> image.frame_number
  51. 11
  52. >>> image.field_of_view
  53. 6
  54. >>> image.channel
  55. 'GFP'
  56. >>> image.z_level
  57. 0
  58. ```
  59. If you only want to view images that meet certain criteria, you can use `select()`. It's much faster than iterating
  60. and checking attributes of images manually. You can specify scalars or lists of values. Criteria that aren't specified
  61. default to every possible value. Currently, slicing and selecting can't be done at the same time, but you can
  62. set a range with the `start` and `stop` arguments:
  63. ```python
  64. for image in nd2.select(channels="GFP", fields_of_view=(1, 2, 7)):
  65. # gets all GFP images in fields of view 1, 2 and 7, regardless of z-level or frame
  66. do_something(image)
  67. for image in nd2.select(z_levels=(0, 1), start=12, stop=3000):
  68. # gets images of any channel or field of view, with z-level 0 or 1, between images 12 and 3000
  69. do_something(image)
  70. ```
  71. Slicing is also supported and is extremely memory efficient, as images are only read when directly accessed:
  72. ```python
  73. for image in nd2[50:433]:
  74. do_something(image)
  75. # get every other image in the first 100 images
  76. for image in nd2[:100:2]:
  77. do_something(image)
  78. # iterate backwards over every image
  79. for image in nd2[::-1]:
  80. do_something(image)
  81. ```
  82. You can also just index a single image:
  83. ```python
  84. # gets the 18th image
  85. my_important_image = nd2[17]
  86. ```
  87. The `Nd2` object has some programmatically-accessible metadata:
  88. ```python
  89. >>> nd2.height # in pixels
  90. 1280
  91. >>> nd2.width # in pixels
  92. 800
  93. >>> len(nd2) # the number of images
  94. 30528
  95. >>> nd2.pixel_microns # the width of a pixel in microns
  96. 0.22
  97. ```
  98. ### Contributing
  99. If you'd like to help with the development of nd2reader or just have an idea for improvement, please see the [contributing](https://github.com/jimrybarski/nd2reader/blob/master/CONTRIBUTING.md) page
  100. for more information.
  101. ### Bug Reports and Features
  102. If this fails to work exactly as expected, please open an [issue](https://github.com/jimrybarski/nd2reader/issues).
  103. If you get an unhandled exception, please paste the entire stack trace into the issue as well.
  104. ### Citation
  105. You can cite nd2reader in your research if you want:
  106. ```
  107. Rybarski, Jim (2015): nd2reader. figshare.
  108. http://dx.doi.org/10.6084/m9.figshare.1619960
  109. ```
  110. ### Acknowledgments
  111. Support for the development of this package was provided by the [Finkelstein Laboratory](http://finkelsteinlab.org/).