Open nd2 files in Python (with nd2reader=3.1.0)

python
industry
nd2reader
pims
#1

Hi everyone,

I can not open stitched Nd2 files from our Slidescanner in Python.
I use Python 3.6 and nd2reader=3.1.0.

This is the metadata that is read:

<Deprecated ND2 C:\Data\slidescanner\20190514_000911_813\Slide1-1-1_ChannelBrightfield_Seq0000.nd2>
Created: Unknown
Image size: 3302x5146 (HxW)
Frames: 1
Channels: Brightfield
Fields of View: 1
Z-Levels: 0

When I open the file the numpy array has this dimension:
(3302, 7720)

I have strange lines in the image.
The image is no longer RGB.

Images were acquired with NIS Elements 5.02.00.

Do you have any advise (other than using the dedicated Fiji Plugin…)

Thanks a lot &
Kind regards

Tobias

#2

Hi @Tobias

Have you tried pims? It has a number of different backends that it delegates to including nd2reader and Bio-Formats as well as an “improved Nikon .nd2” module – pims_nd2. It might help in comparing the results from a number of different libraries.

Cheers,
~Josh

2 Likes
#3

I’m not sure if the current maintainer of nd2reader, Ruben Verweij, is monitoring this forum. FWIW, I just invited him to this topic using the Share button.

@Tobias You might as well submit an issue on the GitHub repository of nd2reader:


Alternatively, you can try python-bioformats:

https://pythonhosted.org/python-bioformats/

#4

Dear Josh,

thanks a lot.

Nd2 reader seems to be the most recent reader.
We need to use a quite recent version of the acquisition software, thus i tried only ndreader. Even ndread has only been tested with 4.13. We use 5.02 to write the files.

pims seems correctly installed, but can not be imported…

[WinError 14001] The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail

Kind regards

Tobias

#5

Dear Jan,

thanks a lot! I made also an issue on github and wait for Ruben. As far as i am aware bioformats does not support nd2 and can not be used in ImageJ to open nd2. Only the nd2 reader plugin in ImageJ works (but is not marco recordable).

Kind regards

Tobias

#6

@Tobias,
I second the recommendation for pims_nd2. It uses the Nikon libraries bundled with their ND2 SDK (although github seems to indicate the last change was 4 years ago, so maybe not the latest). Maybe give it another try with a clean conda environment and then pip install it.

I didn’t know nd2reader but just had a loo … it does not use the Nikon SDK but also supports the pims framework which may make it easy to condue the two.

#7

@VolkerH
Dear Volker,

also in a new enviroment I have the same issue:

OSError Traceback (most recent call last)
in
----> 1 import pims

~\AppData\Local\conda\conda\envs\pyPIMS\Lib\site-packages\pims_init_.py in
----> 1 from pims.api import *
2
3 from ._version import get_versions
4 version = get_versions()[‘version’]
5 del get_versions

~\AppData\Local\conda\conda\envs\pyPIMS\Lib\site-packages\pims\api.py in
110
111 try:
–> 112 from pims_nd2 import ND2_Reader
113 except ImportError:
114 ND2_Reader = not_available(“pims_nd2”)

~\AppData\Local\conda\conda\envs\pyPIMS\Lib\site-packages\pims_nd2_init_.py in
----> 1 from .nd2reader import ND2_Reader

~\AppData\Local\conda\conda\envs\pyPIMS\Lib\site-packages\pims_nd2\nd2reader.py in
6 from pims.base_frames import FramesSequenceND
7 import os
----> 8 from . import ND2SDK as h
9 from ctypes import c_uint8, c_uint16, c_float
10

~\AppData\Local\conda\conda\envs\pyPIMS\Lib\site-packages\pims_nd2\ND2SDK.py in
21 raise OSError(“The bitsize does not equal 32 or 64.”)
22 os.environ[“PATH”] += os.pathsep + os.path.join(dlldir)
—> 23 nd2 = cdll.LoadLibrary(‘v6_w32_nd2ReadSDK.dll’)
24
25

C:\ProgramData\Anaconda3\lib\ctypes_init_.py in LoadLibrary(self, name)
432
433 def LoadLibrary(self, name):
–> 434 return self._dlltype(name)
435
436 cdll = LibraryLoader(CDLL)

C:\ProgramData\Anaconda3\lib\ctypes_init_.py in init(self, name, mode, handle, use_errno, use_last_error)
354
355 if handle is None:
–> 356 self._handle = _dlopen(self._name, mode)
357 else:
358 self._handle = handle

OSError: [WinError 14001] The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail

#8

So it fails when it tries to load the DLL.
If you google the error message

The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail

you will see quite a few different thing that may cause this, probably something specific to your local system. You probably really need to trace this down with sxstrace.exe as suggested to get to the cause of it.
If you have access to a different machine I would give it a try on a different machine first to see whether you can reproduce the error.

#9

I am the maintainer of nd2reader. Thank you for your interest in the package.

Both nd2reader and pims_nd2 are based on pims, but use a different approach. pims_nd2 is based on the Nikon SDK as was mentioned, while nd2reader is a pure Python package that was written from reverse-engineered ND2 files.

Because I can only work with ND2 files that are generated by my microscope, more ‘exotic’ files such as the file you’re trying to open may or may not work out of the box. If you can attach the ND2 file here or can upload it to a file sharing service, I can have a look at the file structure.

Best,
Ruben

3 Likes
#10

Adding my 2p about ND2 support. First of all, I will briefly answer

As far as i am aware bioformats does not support nd2 and can not be used in ImageJ to open nd2

I understand what you try express but I disagree on the absolute statement. Bio-Formats does support Nikon ND2 files and each version of the software is tested daily against a large number of representative sample ND2 files sent by the community.
What is true is that Bio-Formats does not support all existing variants of the ND2 file format. Keeping up with successively breaking versions of file formats is even harder when no open specification or implementations are available. We have discussed in a few blog posts our views on the main issues and possible solutions in order to meet the challenges of supporting proprietary file formats 1 2.

pims_nd2 is based on the Nikon SDK as was mentioned, while nd2reader is a pure Python package that was written from reverse-engineered ND2 files.

These are the two main classes of solutions for reading files. In the case of Bio-Formats, using the SDK directly is not possible primarily for license compatibility issues. Note that in a very similar cases, another manufacturer, 3i, has committed to the maintenance and distribution a update site shipping a Bio-Formats reader based on their SDK (https://www.intelligent-imaging.com/technical-answers).

To the best of our knowledge, we are not aware that Nikon has expressed interest in a similar approach. But we would obviously welcome their opinion and thoughts as this would be a huge step allowing data generated by new instrumentation to be more seamlessly usable and readable by the general imaging community

Because I can only work with ND2 files that are generated by my microscope, more ‘exotic’ files such as the file you’re trying to open may or may not work out of the box. If you can attach the ND2 file here or can upload it to a file sharing service, I can have a look at the file structure.

For reverse engineering, access to representative sample files is critical. We have recently reviewed all the files that have been submitted to us over the years and tried to make as many of them publicly available under https://downloads.openmicroscopy.org/images/ND2/. You are welcome to use these to test and improve nd2_reader as well.

More generally, using the thread as the occasion to give huge kudos to the PIMS team and @ruben1 for providing alternate open solutions reading ND2 variants that are not supported by Bio-Formats.

3 Likes
#11

Thank you very much @s.besson! I was not aware of the sample ND2 files from openmicroscopy - that looks very interesting!
I think it would be very useful to also use these files for the unit tests in nd2reader. To keep track of this, I made a bug report here.

2 Likes
#12

Dear Volker, Sebastien and Ruben,

Thank you so much for your help!

I have send you both the original nd2 file and the exported TIF.

Please feel free to add it to the repository in case you find it useful for future tests.

Just as a summary for everyone who might have similar problems:

This nd2 file type can - at current - not be opened in ImageJ with Bioformats.

It is stretched in X when opened in nd2reader = 3.1.0 in Python and distorted when opend in PIMS_nd2 in Python.

(PIMS-nd2 works otherwise nicely on a new workstation, thanks a lot @VolkerH ).

It opens well with the nd2-reader Plugin in ImageJ (but this Plugin is not macro recordable).

Thus, I currently batch-export from NIS Elements.

I think the reason why most reader fail is the way images are acquired/stored.

I scan whole slides. These are for technical reasons always a bit tilted. The software knows the real stage positions and does a tile scan that is then stitched and saved into a map that contains “empty positions” at which no images were acquired.

In that map also ROIs for acquisition of high-resolution scans are defined and stored.

I would guess that either the (arbitrarily shaped) auto-detected ROIs saved in the file or the empty positions cause the problem.

Thanks a lot and Kind regards

Tobias