Project-MONAI / MONAILabel

MONAI Label is an intelligent open source image labeling and learning tool.
https://docs.monai.io/projects/label
Apache License 2.0
626 stars 196 forks source link

Pre-existing labels do not get imported #554

Closed jlvahldiek closed 2 years ago

jlvahldiek commented 2 years ago

Describe the bug Pre-existing labels will not be imported if the file extension differs from the file extension of the corresponding image.

To Reproduce Steps to reproduce the behavior:

  1. Put example image file in /data, i.e. /data/patient_0001.dcm
  2. Put example label file in /data/labels/final, that has another file extensions, i.e. /data/labels/final/patient_0001.nii.gz
  3. Start monailabel server to initiate file parsing/import procedure, monailabel start_server --app app --studies /data
  4. Pre-existing label file is not imported, server log shows the following:
    
    [2021-12-10 08:55:32,700] [436] [MainThread] [INFO] (monailabel.datastore.local:125) - Auto Reload: True; Extensions: ['*.dcm', '*.nii.gz', '*.nii', '*.nrrd']
    [2021-12-10 08:55:32,711] [436] [MainThread] [INFO] (monailabel.datastore.local:551) - Adding New Image: patient_0001 => patient_0001.dcm
    [2021-12-10 08:55:32,759] [436] [MainThread] [INFO] (monailabel.datastore.local:581) - Adding New Label: final => patient_0001 => patient_0001.nii.gz
    [2021-12-10 08:55:32,763] [436] [MainThread] [ERROR] (uvicorn.error:89) - Traceback (most recent call last):
    File "/opt/conda/lib/python3.8/site-packages/starlette/routing.py", line 540, in lifespan
    async for item in self.lifespan_context(app):
    File "/opt/conda/lib/python3.8/site-packages/starlette/routing.py", line 481, in default_lifespan
    await self.startup()
    File "/opt/conda/lib/python3.8/site-packages/starlette/routing.py", line 516, in startup
    await handler()
    File "/opt/conda/lib/python3.8/site-packages/monailabel/app.py", line 99, in startup_event
    instance = app_instance()
    File "/opt/conda/lib/python3.8/site-packages/monailabel/interfaces/utils/app.py", line 51, in app_instance
    app = c(app_dir=app_dir, studies=studies, conf=conf)
    File "/workspace/code/main.py", line 43, in __init__
    super().__init__(
    File "/opt/conda/lib/python3.8/site-packages/monailabel/interfaces/app.py", line 85, in __init__
    self._datastore: Datastore = self.init_datastore()
    File "/opt/conda/lib/python3.8/site-packages/monailabel/interfaces/app.py", line 136, in init_datastore
    return LocalDatastore(
    File "/opt/conda/lib/python3.8/site-packages/monailabel/datastore/local.py", line 142, in __init__
    self._reconcile_datastore()
    File "/opt/conda/lib/python3.8/site-packages/monailabel/datastore/local.py", line 530, in _reconcile_datastore
    invalidate += self._add_non_existing_labels(tag)
    File "/opt/conda/lib/python3.8/site-packages/monailabel/datastore/local.py", line 586, in _add_non_existing_labels
    "checksum": file_checksum(os.path.join(self._datastore.image_path(), name)),
    File "/opt/conda/lib/python3.8/site-packages/monailabel/utils/others/generic.py", line 112, in file_checksum
    with open(file, "rb") as content:
    FileNotFoundError: [Errno 2] No such file or directory: '/workspace/data/./patient_0001.nii.gz'

[2021-12-10 08:55:32,763] [436] [MainThread] [ERROR] (uvicorn.error:35) - Application startup failed. Exiting.


**Expected behavior**
Pre-existing label file should get imported although the file extension differs from the file extension of the corresponding image.

**Proposed solution**
Change line 586 in MONAILabel/monailabel/datastore/local.py

"checksum": file_checksum(os.path.join(self._datastore.label_path(), name)),`

self._datastore.image_path() may be changed to self._datastore.label_path()

Should I create a pull request?

**Environment**
docker: projectmonai/monailabel

python -c 'import monai; monai.config.print_debug_info()'

================================ Printing MONAI config...

MONAI version: 0.8.0+4.g8a1454a7 Numpy version: 1.21.2 Pytorch version: 1.10.0a0+0aef44c MONAI flags: HAS_EXT = True, USE_COMPILED = False MONAI rev id: 8a1454a75de71ee50099d0302ce5146c900cee09

Optional dependencies: Pytorch Ignite version: 0.4.6 Nibabel version: 3.2.1 scikit-image version: 0.18.3 Pillow version: 8.4.0 Tensorboard version: 2.6.0 gdown version: 4.2.0 TorchVision version: 0.11.0a0 tqdm version: 4.62.3 lmdb version: 1.2.1 psutil version: 5.8.0 pandas version: 1.3.4 einops version: 0.3.2 transformers version: 4.12.5 mlflow version: 1.21.0

For details about installing the optional dependencies, please visit: https://docs.monai.io/en/latest/installation.html#installing-the-recommended-dependencies

================================ Printing system config...

System: Linux Linux version: Ubuntu 20.04.3 LTS Platform: Linux-5.10.76-linuxkit-x86_64-with-glibc2.10 Processor: x86_64 Machine: x86_64 Python version: 3.8.12 Process name: python Command: ['python', '-c', 'import monai; monai.config.print_debug_info()'] Open files: [] Num physical CPUs: 2 Num logical CPUs: 2 Num usable CPUs: 2 CPU usage (%): [7.3, 25.3] CPU freq. (MHz): 3200 Load avg. in last 1, 5, 15 mins (%): [14.5, 30.0, 34.0] Disk usage (%): 46.1 Avg. sensor temp. (Celsius): UNKNOWN for given OS Total physical memory (GB): 1.9 Available memory (GB): 1.1 Used memory (GB): 0.4

================================ Printing GPU config...

Num GPUs: 0 Has CUDA: False cuDNN enabled: True cuDNN version: 8204

diazandr3s commented 2 years ago

Thanks, @jlvahldiek What do you mean by importing the label? Are you trying to load the label using Slicer? Could you please expand more on this? Also, from the error you showed (FileNotFoundError: [Errno 2] No such file or directory: '/workspace/data/./patient_0001.nii.gz'), I can see the label isn't stored in /workspace/data/labels/final/

SachidanandAlle commented 2 years ago

You are mixing the types.. image is dcm.. label is niii

jlvahldiek commented 2 years ago

Thanks, @diazandr3s and @SachidanandAlle

I was just wondering if the file path that is passed to file_checksum() in _add_non_existing_label() on line 586 in monailabel/datastore/local.py is probably constructed incorrectly.

This path should point to a label file and not to an image. I suggest to change _datastore.image_path() to _datastore.label_path() in the os.path.join statement on line 586.

SachidanandAlle commented 2 years ago

Please feel free to raise a PR.. at the end... its all open source

jlvahldiek commented 2 years ago

Just raised PR #557. Could be closed. Thank you.