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.

136 lines
3.6 KiB

10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 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 planned.
  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. 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)
  16. `pip3 install nd2reader` (Python 3.x)
  17. ### ND2s
  18. A quick summary of ND2 metadata can be obtained as shown below.
  19. ```python
  20. >>> import nd2reader
  21. >>> nd2 = nd2reader.Nd2("/path/to/my_images.nd2")
  22. >>> nd2
  23. <ND2 /path/to/my_images.nd2>
  24. Created: 2014-11-11 15:59:19
  25. Image size: 1280x800 (HxW)
  26. Image cycles: 636
  27. Channels: '', 'GFP'
  28. Fields of View: 8
  29. Z-Levels: 3
  30. ```
  31. You can also get some metadata about the nd2 programatically:
  32. ```python
  33. >>> nd2.height
  34. 1280
  35. >>> nd2.width
  36. 800
  37. >>> len(nd2)
  38. 30528
  39. ```
  40. `Nd2` is also a context manager, if you care about that sort of thing:
  41. ```
  42. >>> import nd2reader
  43. >>> with nd2reader.Nd2("/path/to/my_images.nd2") as nd2:
  44. ... for image in nd2:
  45. ... do_something(image)
  46. ```
  47. ### Images
  48. `Image` objects are just Numpy arrays with some extra metadata bolted on:
  49. ```python
  50. >>> image = nd2[20]
  51. >>> print(image)
  52. array([[1894, 1949, 1941, ..., 2104, 2135, 2114],
  53. [1825, 1846, 1848, ..., 1994, 2149, 2064],
  54. [1909, 1820, 1821, ..., 1995, 1952, 2062],
  55. ...,
  56. [3487, 3512, 3594, ..., 3603, 3643, 3492],
  57. [3642, 3475, 3525, ..., 3712, 3682, 3609],
  58. [3687, 3777, 3738, ..., 3784, 3870, 4008]], dtype=uint16)
  59. >>> print(image.timestamp)
  60. 10.1241241248
  61. >>> print(image.frame_number)
  62. 11
  63. >>> print(image.field_of_view)
  64. 6
  65. >>> print(image.channel)
  66. 'GFP'
  67. >>> print(image.z_level)
  68. 0
  69. ```
  70. Often, you may want to just iterate over each image in the order they were acquired:
  71. ```python
  72. import nd2reader
  73. nd2 = nd2reader.Nd2("/path/to/my_images.nd2")
  74. for image in nd2:
  75. do_something(image)
  76. ```
  77. Slicing is also supported and is extremely memory efficient, as images are only read when directly accessed:
  78. ```python
  79. my_subset = nd2[50:433]
  80. for image in my_subset:
  81. do_something(image)
  82. ```
  83. Step sizes are also accepted:
  84. ```python
  85. for image in nd2[:100:2]:
  86. # gets every other image in the first 100 images
  87. do_something(image)
  88. for image in nd2[::-1]:
  89. # iterate backwards over every image, if you're into that kind of thing
  90. do_something(image)
  91. ```
  92. ### Protips
  93. nd2reader is about 14 times faster under Python 3.4 compared to Python 2.7. If you know why, please get in touch!
  94. ### Bug Reports and Features
  95. If this fails to work exactly as expected, please open a Github issue. If you get an unhandled exception, please
  96. paste the entire stack trace into the issue as well.
  97. ### Contributing
  98. Please feel free to submit a pull request with any new features you think would be useful. You can also create an
  99. issue if you'd just like to propose or discuss a potential idea.
  100. ### Acknowledgments
  101. Support for the development of this package was provided by the [Finkelstein Laboratory](http://finkelsteinlab.org/).