# Get path of the mhd's data file

**URL:** https://discourse.itk.org/t/get-path-of-the-mhds-data-file/4049
**Category:** Beginner Questions
**Tags:** simpleitk
**Created:** [April 20, 2021, 1:06pm UTC](https://discourse.itk.org/t/get-path-of-the-mhds-data-file/4049 "2021-04-20T13:06:32Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![reox](https://discourse.itk.org/user_avatar/discourse.itk.org/reox/32/4490_2.png) [@reox](https://discourse.itk.org/u/reox)
#### Post date: [April 20, 2021, 1:06pm UTC](https://discourse.itk.org/t/get-path-of-the-mhds-data-file/4049/1 "2021-04-20T13:06:32Z")

</div>

I use MetaImage files (mhd) a lot and sometimes need the path of the data file. Usually, I have the file loaded via SimpleITK already. Thus, is there a way to retrieve the name of the data file from the python interface somehow?

```auto
>>> import SimpleITK as sitk
>>> img = sitk.ReadImage('myimage.mhd')
>>> img.????()
'myimage.zraw'

```

It would be easy if the file would be named always `filename.raw` but sometimes I have compressed files too…

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [April 20, 2021, 1:35pm UTC](https://discourse.itk.org/t/get-path-of-the-mhds-data-file/4049/2 "2021-04-20T13:35:52Z")

</div>

Hello @reox,

This is not built into SimpleITK. You can trivially add it via the metadata dictionary, see code below for SimpleITK\<2.1 and SimpleITK\>=2.1 which will be out shortly and supports more Pythonic access:

```auto
import SimpleITK as sitk
from pathlib import Path

def sitk_read_image(file_name):
  image = sitk.ReadImage(file_name)
  image.SetMetaData('file_path', str(Path(file_name).absolute()))
  return image
  
file_name = 'training_001_ct.mha'
image = sitk_read_image(file_name)
print(image.GetMetaData('file_path'))

```

Upcoming more Pythonic version:

```auto
import SimpleITK as sitk
from pathlib import Path

def sitk_read_image(file_name):
  image = sitk.ReadImage(file_name)
  image['file_path'] = str(Path(file_name).absolute())
  return image

file_name = 'training_001_ct.mha'
image = sitk_read_image(file_name)
print(image['file_path'])

```

---

<div class="post-metadata">

### Author: ![reox](https://discourse.itk.org/user_avatar/discourse.itk.org/reox/32/4490_2.png) [@reox](https://discourse.itk.org/u/reox)
#### Post date: [April 20, 2021, 1:39pm UTC](https://discourse.itk.org/t/get-path-of-the-mhds-data-file/4049/3 "2021-04-20T13:39:51Z")

</div>

That means there is also no access to the data file? But the `SetMetaData` sounds good, I can simply write a wrapper for ReadImage, just like in your example, and read the .mhd textfile on my own to extract the path.

Thanks a lot!

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.itk.org/user_avatar/discourse.itk.org/lassoan/32/27_2.png) [@lassoan](https://discourse.itk.org/u/lassoan)
#### Post date: [April 22, 2021, 1:39am UTC](https://discourse.itk.org/t/get-path-of-the-mhds-data-file/4049/4 "2021-04-22T01:39:29Z")

</div>

Note that the metaimage format allows using many data files and there are other fields in the header that are essential for interpreting the data file (offset, compression method, etc), so just storing one data file name will not be sufficient for reliably accessing the data. It is better to let ITK take care of interpreting the header and reading and decoding the data files.

Metaimage is not a very good file format by the way. It does not have standard way to specify axis types (so you cannot tell if you have a 2D color image or a 3-slice volume; or robustly store time sequence or tensor data), there is ambiguity in interpretation of axis directions, it is unnecessarily complex (allowing using many data files), and does not support measurement frames. I would recommend using nrrd file format instead, which is very similar but does not have any of these limitations. Nifti format is popular, too, but it has many issues.

---

<div class="post-metadata">

### Author: ![reox](https://discourse.itk.org/user_avatar/discourse.itk.org/reox/32/4490_2.png) [@reox](https://discourse.itk.org/u/reox)
#### Post date: [April 22, 2021, 5:14am UTC](https://discourse.itk.org/t/get-path-of-the-mhds-data-file/4049/5 "2021-04-22T05:14:52Z")

</div>

I agree, Metaimage is not the best format. However, it is used a lot at our institute and we have TB of data in that format… It has simply developed over time. But fortunately, our data is in 99.5% CT volume data, which makes things a bit easier 🙂

I wont use the data file name for reading purposes but only for managing files. That is the reason why I want it in addition to the `Image` class.

right now, I have this piece of code:

```auto
def sitk_read_image(path):
    img = sitk.ReadImage(path)
    img.SetMetaData('file_path', os.path.abspath(path))
    _, ext = os.path.splitext(path)
    if ext in ['.mhd',]:
        with open(path, 'r') as fp:
            for line in fp.read().splitlines():
                if line.startswith('ElementDataFile'):
                    img.SetMetaData('data_path',
                                    os.path.join(os.path.dirname(path), line.split('=', 1)[1].strip()))
                    break
    return img

```

Which is a bit ugly but works for my purposes.  
With this I can for example get the size on disk of the file:

```auto
>>> os.path.getsize(x.GetMetaData('data_path'))
744065076

```

But to avoid the XY problem, maybe I should have asked in the first place: Is there a way in SITK to retrieve the size on disk for metaimage files?

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [April 22, 2021, 1:10pm UTC](https://discourse.itk.org/t/get-path-of-the-mhds-data-file/4049/6 "2021-04-22T13:10:43Z")

</div>

Hello @reox,

No, SimpleITK does not have a way of retrieving the file size on disk.

One last word of caution, there is a reason why most file formats do not contain the file name or file path as part of the header/internal information, you can readily change the path or file name without the “file” being aware of this making that internal information wrong. We want our IO to be oblivious to file names as these are just one way for humans to specify what data they want to work with and is not inherently part of the data. Think patient name in database of patients as another way to specify which “files” you want to display/process.

---

<div class="post-metadata">

### Author: ![reox](https://discourse.itk.org/user_avatar/discourse.itk.org/reox/32/4490_2.png) [@reox](https://discourse.itk.org/u/reox)
#### Post date: [April 22, 2021, 1:32pm UTC](https://discourse.itk.org/t/get-path-of-the-mhds-data-file/4049/7 "2021-04-22T13:32:10Z")

</div>

> [@zivy](#):
>
> One last word of caution, there is a reason why most file formats do not contain the file name or file path as part of the header/internal information, you can readily change the path or file name without the “file” being aware of this making that internal information wrong.

yes, that is clear.  
To not fall into this trap accidentally, I’ll put the on-disk file size as metadata and not the path.
