When using Python wrapper, GetBuffer returns too many bytes
Cross-platform DICOM implementation
Brought to you by:
malat
I am trying to read one of the example CETAUTOMATIX datasets from here:
http://www.osirix-viewer.com/datasets/
The minimal example below causes the buffer returned at the end to have 4737024 bytes, even though the image should only be 192 * 256 * 2 = 49152 bytes (the data is uint16)
import gdcm
import numpy as np
filename = 'CETAUTOMATIX/CETAUTOMATIX/Cardiovascular Heart-Cardiac Function/cine_retro_normal_hla/IM-0001-0007.dcm'
gdcm_image_reader = gdcm.ImageReader()
gdcm_image_reader.SetFileName(filename)
gdcm_image_reader.Read()
gdcm_image = gdcm_image_reader.GetImage()
gdcm_numpy_typemap = {
gdcm.PixelFormat.INT8: np.int8,
gdcm.PixelFormat.UINT8: np.uint8,
gdcm.PixelFormat.UINT16: np.uint16,
gdcm.PixelFormat.INT16: np.int16,
gdcm.PixelFormat.UINT32: np.uint32,
gdcm.PixelFormat.INT32: np.int32,
gdcm.PixelFormat.FLOAT32: np.float32,
gdcm.PixelFormat.FLOAT64: np.float64
}
gdcm_pixel_format = gdcm_image.GetPixelFormat().GetScalarType()
numpy_dtype = gdcm_numpy_typemap[gdcm_pixel_format]
pixel_bytearray = gdcm_image.GetBuffer()
print(len(pixel_bytearray))
# returns 4737024 instead of 49152
Furthermore, after the 49152-th byte, all the remaining bytes are zero. So this is a case of GetBuffer including unecessary padding at the end.
I think you are using an ancient python version. Tested with 2.6.1 does not exhibit the issue you show.
I'm actually using Python 2.7.11!
So I think this is a real issue.
Meant to say python-gdcm version sorry. Try GDCM > 2.6.1
Hum...using OpenJPEG 2.x I can get trigger an assert(). My guess is that there is an unitilized read happening in your case. Could you confirm that you are using a custom OpenJPEG 2.x library instead of the default ship-in openjpeg lib.
I just installed the latest version using
The latest commit is:
and to be extra sure:
I do think this is a real bug in the latest version.
That's an impossible combination. You cannot use the default static lib with python binding. Please describe exactly the option(s) you used in cmake. Ideally post the actual
$ cmake -DBLA:BOOL=ON /path/to/gdcm/sourceWhat is the easiest way to find out which OpenJPEG is being used? I do have library files called
libgdcmopenjpeg.afor instance.Just in case it helps, here is the install log:
https://gist.github.com/astrofrog/96b769397a3c0aff86f0c24efc805e2c
and I also see this in IPython:
Here's what I get for the cmake command you asked for:
https://gist.github.com/astrofrog/bcb21b1fdbe878d3eadd3d593a33c9cc
This was in a clean build directory. Does this help?
No this is impossible. On one hand you have
_gdcmswig.sobuilding, but on the other hand you are specifing the python binding during cmake configuration.And just to be clear, when I originally ran cmake, I ran the exact commands in INSTALL - i.e.
cmake ../gdcm- without any flags.I think I understand what you did:
_gdcmswig.so, use python -v to see which one is used)I am going to close this bug. If you want to re-open this bug report, delete all your system
_gdcmswig.sofiles and then only then report a bug report. gdcm build system is somewhat flexible and should be able to run your python script within the gdcmbin/bin directory directly. If you do so, I'll only accept if you do provide thepython -voutput.Last edit: Mathieu Malaterre 2016-06-29
Ah sorry when I say I didn't add any flags, in the cmake config menu I did select to install the Python bindings - so I'm pretty sure the
_gdcmswig.sofile WAS installed at the same time.Hum...in this case you are not using
x86_64, are you ? Please be a little more verbose on your setup:I'm using MacOS X 10.11, and my Python installation is 64-bit, installed from Anaconda.
So I wiped all GDCM files from my system and started fresh. This time, I did
then I went inside the
bindirectory, and copied my debugging script and data there. I then ran the debug script and got the same answer again. The output frompython -vis:Let me know if there is any other information I can provide!
Ah and the SWIG version is:
I think that the issue is somehow caused by a conflict with MacPorts. I haven't managed to narrow down exactly why, but by moving MacPorts out of the way, I managed to avoid this issue. It would still be interesting to understand why the issue occurred, but I think we're all short on time :)
wow ! Thanks for the update, I feared it was a MacOSX + clang specific issue when compiling OpenJPEG code ...
As mentionned I do not have neither the time nor the hardware (mac) to reproduce the issue. For a start I would still reduce the bug to the C++ layer to check if we can remove python and swig from the depdencies:
See also: http://gdcm.sourceforge.net/html/PatchFile_8cxx-example.html#a39
This might be a silly question, but I'm trying to compile the example you provided (for now, just the PatchFile one) and am trying to compile with:
but I get the following error:
Do you know what this is due to?
You are missing a MacOSX specific library. Either use cmake:
Or, simply do a
touchon any*.cxxfile within Source/Common and then type:make gdcmCommon VERBOSE=1You should see what is being added by cmake (link line is at the end).
From my linux system all I can see is that it is referenced by:
target_link_libraries(gdcmCommon ${COREFOUNDATION_LIBRARY})but I do not know the value of this thingy...