# MetaImage: M\_ReadElementsROI: data not read completely

**URL:** https://discourse.itk.org/t/metaimage-m-readelementsroi-data-not-read-completely/4269
**Category:** Uncategorized
**Created:** [July 14, 2021, 8:54pm UTC](https://discourse.itk.org/t/metaimage-m-readelementsroi-data-not-read-completely/4269 "2021-07-14T20:54:01Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![JK\_Kim](https://discourse.itk.org/user_avatar/discourse.itk.org/jk_kim/32/2173_2.png) [@JK\_Kim](https://discourse.itk.org/u/JK_Kim)
#### Post date: [July 14, 2021, 8:54pm UTC](https://discourse.itk.org/t/metaimage-m-readelementsroi-data-not-read-completely/4269/1 "2021-07-14T20:54:02Z")

</div>

Hi

I am using SimpleITK 2.0.2 and Python 3.7.9 to read a 3D CT image in the compressed mhd format (ct.mhd, ct.zraw). When I run the code below, I am getting the following message. “data not read completely”. So, I saved the read image back to file, opened in a viewer and I got the correct image. So, it seems like there is no issue for the code… Anyone has any clue why I am getting this message?

Thanks.  
JK

[message]  
MetaImage: M\_ReadElementsROI: data not read completely  
ideal = 1069833 : actual = 633067

[code]  
file\_reader = sitk.ImageFileReader()  
file\_reader.SetFileName(img\_path)  
file\_reader.SetExtractIndex(start\_index)  
file\_reader.SetExtractSize(extract\_size)  
img = file\_reader.Execute()

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [July 15, 2021, 12:20pm UTC](https://discourse.itk.org/t/metaimage-m-readelementsroi-data-not-read-completely/4269/2 "2021-07-15T12:20:08Z")

</div>

That might be true for an image that is mostly zero, so the last 40% of it which was not read only contained zeroes. This is practically possible with label images.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [July 15, 2021, 12:21pm UTC](https://discourse.itk.org/t/metaimage-m-readelementsroi-data-not-read-completely/4269/3 "2021-07-15T12:21:12Z")

</div>

I can’t quite imaging that being the case for a CT image though. Can you share your image?

---

<div class="post-metadata">

### Author: ![Will\_G](https://discourse.itk.org/user_avatar/discourse.itk.org/will_g/32/2282_2.png) [@Will\_G](https://discourse.itk.org/u/Will_G)
#### Post date: [September 15, 2021, 12:44pm UTC](https://discourse.itk.org/t/metaimage-m-readelementsroi-data-not-read-completely/4269/4 "2021-09-15T12:44:02Z")

</div>

Did you manage to find a resoluton to this issue?, I am having the same one as well.

---

<div class="post-metadata">

### Author: ![Z\_Huang](https://discourse.itk.org/user_avatar/discourse.itk.org/z_huang/32/2797_2.png) [@Z\_Huang](https://discourse.itk.org/u/Z_Huang)
#### Post date: [June 26, 2022, 4:18am UTC](https://discourse.itk.org/t/metaimage-m-readelementsroi-data-not-read-completely/4269/5 "2022-06-26T04:18:33Z")

</div>

True for me. I have experienced the same “MetaImage: M\_ReadElementsROI: data not read completely” issue and figured the 3D image is a label, where 28,295,732 pixels are 0 and 15,820 pixels are labels.

---

<div class="post-metadata">

### Author: ![jkasten](https://discourse.itk.org/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@jkasten](https://discourse.itk.org/u/jkasten)
#### Post date: [July 11, 2023, 9:49pm UTC](https://discourse.itk.org/t/metaimage-m-readelementsroi-data-not-read-completely/4269/6 "2023-07-11T21:49:09Z")

</div>

I’m experiencing similar issues with (non-binary) CT images.  
As an experiment I tried reading a CT image with a purposely corrupted .raw file.  
when doing so, I see the console message:

```auto
MetaImage: M_ReadElementsData: data not read completely
   ideal = 203948032 : actual = 16384

```

What is concerning is that with ITK 4.13, attempting to read this image with the standard itk::ImageFileReader would throw an itk::ExceptionObject. However, after updating to ITK 5.2 reading fails silently.

---

<div class="post-metadata">

### Author: ![Stephen\_Aylward](https://discourse.itk.org/user_avatar/discourse.itk.org/stephen_aylward/32/654_2.png) [@Stephen\_Aylward](https://discourse.itk.org/u/Stephen_Aylward)
#### Post date: [July 12, 2023, 1:05pm UTC](https://discourse.itk.org/t/metaimage-m-readelementsroi-data-not-read-completely/4269/7 "2023-07-12T13:05:03Z")

</div>

This is a bug! Great catch!

The M\_ReadElementsROI() function correctly returns false in this condition:

> <https://github.com/Kitware/MetaIO/blob/0b8c5284f3fe126968fd66c24639fd70b88e8788/src/metaImage.cxx#L3012>

but that return value is now being lost within the M\_ReadROIStream() function

> <https://github.com/Kitware/MetaIO/blob/0b8c5284f3fe126968fd66c24639fd70b88e8788/src/metaImage.cxx#L2593>

and also in lines 2661, 2767, and 2832 in that file.

I’ll create a pull request to fix these errors in the MetaIO library. I’ll need to work with the other ITK devs to determine how to update that library in ITK.

Thanks!  
Stephen

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [July 12, 2023, 1:07pm UTC](https://discourse.itk.org/t/metaimage-m-readelementsroi-data-not-read-completely/4269/8 "2023-07-12T13:07:05Z")

</div>

Once the PR is merged in MetaIO repository, invoking [UpdateFromUpstream.sh](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/ThirdParty/MetaIO/UpdateFromUpstream.sh) script in ITK should update it appropriately.

---

<div class="post-metadata">

### Author: ![Stephen\_Aylward](https://discourse.itk.org/user_avatar/discourse.itk.org/stephen_aylward/32/654_2.png) [@Stephen\_Aylward](https://discourse.itk.org/u/Stephen_Aylward)
#### Post date: [July 12, 2023, 1:14pm UTC](https://discourse.itk.org/t/metaimage-m-readelementsroi-data-not-read-completely/4269/9 "2023-07-12T13:14:53Z")

</div>

Actually, ignoring the return value is a now systemic problem in the metaImage.cxx file - happens with writing and reading. It is going to take some hunting to fix all of them. git blame…

---

<div class="post-metadata">

### Author: ![Stephen\_Aylward](https://discourse.itk.org/user_avatar/discourse.itk.org/stephen_aylward/32/654_2.png) [@Stephen\_Aylward](https://discourse.itk.org/u/Stephen_Aylward)
#### Post date: [July 12, 2023, 11:50pm UTC](https://discourse.itk.org/t/metaimage-m-readelementsroi-data-not-read-completely/4269/10 "2023-07-12T23:50:57Z")

</div>

Here is the MetaIO pull request:

> <https://github.com/Kitware/MetaIO/pull/115>
>
> Many calls to private functions (e.g., M\_ReadElementsROI) did not check or propa…gate return values, so errors were not being caught, and ITK reading was failing silently on invalid files.
> 
> Also ROI reading of compressed data was not computing size of data being read correctly, so once we started catching errors, the function was reporting requested data size not matching returned
> data size and was now failing with checks added. Fixed the
> computation of gc in M\_ReadElementsROI for compressed data.
> 
> See discussion: https://discourse.itk.org/t/metaimage-m-readelementsroi-data-not-read-completely/4269/9

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [July 17, 2023, 2:45pm UTC](https://discourse.itk.org/t/metaimage-m-readelementsroi-data-not-read-completely/4269/11 "2023-07-17T14:45:06Z")

</div>

ITK PR is now open:

> <https://github.com/InsightSoftwareConsortium/ITK/pull/4116>
>
> Code extracted from:
> 
> https://github.com/Kitware/MetaIO.git
> 
> at commit 5…56864f5a7bb2853466d0d22068b268e49137e52 (master).
> 
> \## PR Checklist
> \- \[x\] No \[API changes\](https://github.com/InsightSoftwareConsortium/ITK/blob/master/CONTRIBUTING.md#breaking-changes) were made (or the changes have been approved)
> \- \[x\] No \[major design changes\](https://github.com/InsightSoftwareConsortium/ITK/blob/master/CONTRIBUTING.md#design-changes) were made (or the changes have been approved)

---

<div class="post-metadata">

### Author: ![Stephen\_Aylward](https://discourse.itk.org/user_avatar/discourse.itk.org/stephen_aylward/32/654_2.png) [@Stephen\_Aylward](https://discourse.itk.org/u/Stephen_Aylward)
#### Post date: [July 22, 2023, 1:23pm UTC](https://discourse.itk.org/t/metaimage-m-readelementsroi-data-not-read-completely/4269/12 "2023-07-22T13:23:01Z")

</div>

Merged!
