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.

156 lines
4.2 KiB

10 years ago
10 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.0+. It has only been definitively tested on NIS Elements 4.30.02 Build 1053. Support for older versions is being actively worked on.
  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` loads images as Numpy arrays, which makes it trivial to use with the image analysis packages such as `scikit-image` and `OpenCV`.
  6. ### Installation
  7. If you don't already have the packages `numpy`, `six` and `xmltodict`, they will be installed automatically:
  8. `pip3 install nd2reader` for Python 3.x
  9. `pip install nd2reader` for Python 2.x
  10. `nd2reader` is an order of magnitude faster in Python 3. I recommend using it unless you have no other choice.
  11. ### ND2s
  12. A quick summary of ND2 metadata can be obtained as shown below.
  13. ```python
  14. >>> import nd2reader
  15. >>> nd2 = nd2reader.Nd2("/path/to/my_images.nd2")
  16. >>> nd2
  17. <ND2 /path/to/my_images.nd2>
  18. Created: 2014-11-11 15:59:19
  19. Image size: 1280x800 (HxW)
  20. Image cycles: 636
  21. Channels: 'brightfield', 'GFP'
  22. Fields of View: 8
  23. Z-Levels: 3
  24. ```
  25. You can iterate over each image in the order they were acquired:
  26. ```python
  27. import nd2reader
  28. nd2 = nd2reader.Nd2("/path/to/my_images.nd2")
  29. for image in nd2:
  30. do_something(image)
  31. ```
  32. `Image` objects are just Numpy arrays with some extra metadata bolted on:
  33. ```python
  34. >>> image = nd2[20]
  35. >>> image
  36. array([[1894, 1949, 1941, ..., 2104, 2135, 2114],
  37. [1825, 1846, 1848, ..., 1994, 2149, 2064],
  38. [1909, 1820, 1821, ..., 1995, 1952, 2062],
  39. ...,
  40. [3487, 3512, 3594, ..., 3603, 3643, 3492],
  41. [3642, 3475, 3525, ..., 3712, 3682, 3609],
  42. [3687, 3777, 3738, ..., 3784, 3870, 4008]], dtype=uint16)
  43. >>> image.timestamp
  44. 10.1241241248
  45. >>> image.frame_number
  46. 11
  47. >>> image.field_of_view
  48. 6
  49. >>> image.channel
  50. 'GFP'
  51. >>> image.z_level
  52. 0
  53. ```
  54. If you only want to view images that meet certain criteria, you can use `select()`. It's much faster than iterating
  55. and checking attributes of images manually. You can specify scalars or lists of values. Criteria that aren't specified
  56. default to every possible value. Currently, slicing and selecting can't be done at the same time:
  57. ```python
  58. for image in nd2.select(channels="GFP", fields_of_view=(1, 2, 7)):
  59. do_something(image)
  60. ```
  61. Slicing is also supported and is extremely memory efficient, as images are only read when directly accessed:
  62. ```python
  63. for image in nd2[50:433]:
  64. do_something(image)
  65. # get every other image in the first 100 images
  66. for image in nd2[:100:2]:
  67. do_something(image)
  68. # iterate backwards over every image
  69. for image in nd2[::-1]:
  70. do_something(image)
  71. ```
  72. You can also just index a single image:
  73. ```python
  74. # gets the 18th image
  75. my_important_image = nd2[17]
  76. ```
  77. The `Nd2` object has some programmatically-accessible metadata:
  78. ```python
  79. >>> nd2.height
  80. 1280
  81. >>> nd2.width
  82. 800
  83. >>> len(nd2)
  84. 30528
  85. ```
  86. Each camera has its own settings. If you image multiple wavelengths with one camera, each channel will appear as its
  87. own camera:
  88. ```python
  89. >>> nd2.camera_settings
  90. {'GFP': <Camera Settings: GFP>
  91. Camera: Andor Zyla VSC-00461
  92. Camera ID: VSC-00461
  93. Exposure Time (ms): 100.0
  94. Binning: 2x2, 'BF': <Camera Settings: BF>
  95. Camera: Andor Zyla VSC-00461
  96. Camera ID: VSC-00461
  97. Exposure Time (ms): 100.0
  98. Binning: 2x2}
  99. ```
  100. Camera information can be accessed programmatically:
  101. ```python
  102. >>> nd2.camera_settings['GFP'].id
  103. 'VSC-00461'
  104. >>> nd2.camera_settings['GFP'].name
  105. 'Andor Zyla VSC-00461'
  106. >>> nd2.camera_settings['GFP'].exposure
  107. 100.0
  108. >>> nd2.camera_settings['GFP'].x_binning
  109. 2
  110. >>> nd2.camera_settings['GFP'].y_binning
  111. 2
  112. ```
  113. ### Bug Reports and Features
  114. If this fails to work exactly as expected, please open a Github issue. If you get an unhandled exception, please
  115. paste the entire stack trace into the issue as well.
  116. ### Contributing
  117. Please feel free to submit a pull request with any new features you think would be useful. You can also create an
  118. issue if you'd just like to propose or discuss a potential idea.
  119. ### Acknowledgments
  120. Support for the development of this package was provided by the [Finkelstein Laboratory](http://finkelsteinlab.org/).