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.

119 lines
3.8 KiB

7 years ago
7 years ago
  1. # Tutorial
  2. ### Installation
  3. The package is available on PyPi. Install it using:
  4. ```
  5. pip install nd2reader
  6. ```
  7. If you don't already have the packages `numpy`, `pims`, `six` and `xmltodict`, they will be installed automatically if you use the `setup.py` script.
  8. `nd2reader` is an order of magnitude faster in Python 3. I recommend using it unless you have no other choice. Python 2.7 and Python >= 3.4 are supported.
  9. #### Installation via Conda Forge
  10. Installing `nd2reader` from the `conda-forge` channel can be achieved by adding `conda-forge` to your channels with:
  11. ```
  12. conda config --add channels conda-forge
  13. ```
  14. Once the `conda-forge` channel has been enabled, `nd2reader` can be installed with:
  15. ```
  16. conda install nd2reader
  17. ```
  18. It is possible to list all of the versions of `nd2reader` available on your platform with:
  19. ```
  20. conda search nd2reader --channel conda-forge
  21. ```
  22. ### Opening ND2s
  23. `nd2reader` follows the [pims](https://github.com/soft-matter/pims) framework. To open a file and show the first frame:
  24. ```python
  25. from nd2reader import ND2Reader
  26. import matplotlib.pyplot as plt
  27. with ND2Reader('my_directory/example.nd2') as images:
  28. plt.imshow(images[0])
  29. ```
  30. After opening the file, all `pims` features are supported. Please refer to the [pims documentation](http://soft-matter.github.io/pims/).
  31. ### ND2 metadata
  32. The ND2 file contains various metadata, such as acquisition information,
  33. regions of interest and custom user comments. Most of this metadata is parsed
  34. and available in dictionary form. For example:
  35. ```python
  36. from nd2reader import ND2Reader
  37. with ND2Reader('my_directory/example.nd2') as images:
  38. # width and height of the image
  39. print('%d x %d px' % (images.metadata['width'], images.metadata['height']))
  40. ```
  41. All metadata properties are:
  42. * `width`: the width of the image in pixels
  43. * `height`: the height of the image in pixels
  44. * `date`: the date the image was taken
  45. * `fields_of_view`: the fields of view in the image
  46. * `frames`: a list of all frame numbers
  47. * `z_levels`: the z levels in the image
  48. * `total_images_per_channel`: the number of images per color channel
  49. * `channels`: the color channels
  50. * `pixel_microns`: the amount of microns per pixel
  51. * `rois`: the regions of interest (ROIs) defined by the user
  52. * `experiment`: information about the nature and timings of the ND experiment
  53. ### Iterating over fields of view
  54. Using `NDExperiments` in the Nikon software, it is possible to acquire images on different `(x, y)` positions.
  55. This is referred to as different fields of view. Using this reader, the fields of view are on the `v` axis.
  56. For example:
  57. ```python
  58. from nd2reader import ND2Reader
  59. with ND2Reader('my_directory/example.nd2') as images:
  60. # width and height of the image
  61. print(images.metadata)
  62. ```
  63. will output
  64. ```python
  65. {'channels': ['BF100xoil-1x-R', 'BF+RITC'],
  66. 'date': datetime.datetime(2017, 10, 30, 14, 35, 18),
  67. 'experiment': {'description': 'ND Acquisition',
  68. 'loops': [{'duration': 0,
  69. 'sampling_interval': 0.0,
  70. 'start': 0,
  71. 'stimulation': False}]},
  72. 'fields_of_view': [0, 1],
  73. 'frames': [0],
  74. 'height': 1895,
  75. 'num_frames': 1,
  76. 'pixel_microns': 0.09214285714285715,
  77. 'total_images_per_channel': 6,
  78. 'width': 2368,
  79. 'z_levels': [0, 1, 2]}
  80. ```
  81. for our example file. As you can see from the metadata, it has two fields of view. We can also look at the sizes of the axes:
  82. ```python
  83. print(images.sizes)
  84. ```
  85. ```python
  86. {'c': 2, 't': 1, 'v': 2, 'x': 2368, 'y': 1895, 'z': 3}
  87. ```
  88. As you can see, the fields of view are listed on the `v` axis. It is therefore possible to loop over them like this:
  89. ```python
  90. images.iter_axes = 'v'
  91. for fov in images:
  92. print(fov) # Frame containing one field of view
  93. ```
  94. For more information on axis bundling and iteration, refer to the [pims documentation](http://soft-matter.github.io/pims/v0.4/multidimensional.html#axes-bundling).