DevelopmentalImagingMCRI / karawun

Convert tractography studies from mrtrix into dicom for use in navigation software
Apache License 2.0
14 stars 4 forks source link

Error converting tck to dicom #36

Open EromFreitas opened 1 year ago

EromFreitas commented 1 year ago

When trying to convert tck it gives the error below. I'm using the latest version of karawun:

Captura de tela de 2023-10-03 00-58-25

richardbeare commented 1 year ago

Can you post the header of the tck file? It looks like some of the standard fields aren't being detected.

EromFreitas commented 1 year ago

Here is the head of the tck file: Captura de tela de 2023-10-03 09-43-16

richardbeare commented 1 year ago

Are there "lmax" or "method" entries lower down?

If not, that is the likely problem. Transformed tck files aren't a scenario we tested, and it seems that some of the original tags are removed by transformation. They are used in creating some basic descriptive metadata that is visible inside brainlab.

I guess the first thing to do is confirm that those tags are in your original tck file. If they are, and tcktransform removes them, then the temporary fix is to add those tags back in. I guess that will need to be done by hand - make sure you count the number of extra characters so that the "file" field can be updated.

This is messy and we will need to fix in future versions.

EromFreitas commented 1 year ago

I tried to use it in another tck file and the error below occurred: /anaconda3/lib/python3.8/site-packages/pydicom/valuerep.py:290: UserWarning: Invalid value for VR DA: '21000000'. warnings.warn(msg) Exception handler: TypeError - With tag (0066, 0002) got exception: With tag (0066, 0011) got exception: With tag (0066, 0016) got exception: a bytes-like object is required, not 'MultiValue' Traceback (most recent call last): File "/anaconda3/lib/python3.8/site-packages/pydicom/tag.py", line 28, in tag_in_exception yield File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 662, in write_dataset write_data_element(fp, dataset.get_item(tag), dataset_encoding) File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 569, in write_data_element fn(buffer, elem) # type: ignore[operator] File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 297, in write_OWvalue fp.write(cast(bytes, elem.value)) TypeError: a bytes-like object is required, not 'MultiValue'

Traceback (most recent call last): File "/anaconda3/lib/python3.8/site-packages/pydicom/tag.py", line 28, in tag_in_exception yield File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 662, in write_dataset write_data_element(fp, dataset.get_item(tag), dataset_encoding) File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 569, in write_data_element fn(buffer, elem) # type: ignore[operator] File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 297, in write_OWvalue fp.write(cast(bytes, elem.value)) TypeError: a bytes-like object is required, not 'MultiValue'

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/anaconda3/lib/python3.8/site-packages/pydicom/tag.py", line 28, in tag_in_exception yield File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 662, in write_dataset write_data_element(fp, dataset.get_item(tag), dataset_encoding) File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 562, in write_data_element fn(buffer, elem, encodings=encodings) # type: ignore[operator] File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 700, in write_sequence write_sequence_item(fp, ds, encodings) File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 726, in write_sequence_item write_dataset(fp, dataset, parent_encoding=encodings) File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 662, in write_dataset write_data_element(fp, dataset.get_item(tag), dataset_encoding) File "/anaconda3/lib/python3.8/contextlib.py", line 131, in exit self.gen.throw(type, value, traceback) File "/anaconda3/lib/python3.8/site-packages/pydicom/tag.py", line 32, in tag_in_exception raise type(exc)(msg) from exc TypeError: With tag (0066, 0016) got exception: a bytes-like object is required, not 'MultiValue' Traceback (most recent call last): File "/anaconda3/lib/python3.8/site-packages/pydicom/tag.py", line 28, in tag_in_exception yield File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 662, in write_dataset write_data_element(fp, dataset.get_item(tag), dataset_encoding) File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 569, in write_data_element fn(buffer, elem) # type: ignore[operator] File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 297, in write_OWvalue fp.write(cast(bytes, elem.value)) TypeError: a bytes-like object is required, not 'MultiValue'

Traceback (most recent call last): File "/anaconda3/lib/python3.8/site-packages/pydicom/tag.py", line 28, in tag_in_exception yield File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 662, in write_dataset write_data_element(fp, dataset.get_item(tag), dataset_encoding) File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 569, in write_data_element fn(buffer, elem) # type: ignore[operator] File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 297, in write_OWvalue fp.write(cast(bytes, elem.value)) TypeError: a bytes-like object is required, not 'MultiValue'

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/anaconda3/lib/python3.8/site-packages/pydicom/tag.py", line 28, in tag_in_exception yield File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 662, in write_dataset write_data_element(fp, dataset.get_item(tag), dataset_encoding) File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 562, in write_data_element fn(buffer, elem, encodings=encodings) # type: ignore[operator] File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 700, in write_sequence write_sequence_item(fp, ds, encodings) File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 726, in write_sequence_item write_dataset(fp, dataset, parent_encoding=encodings) File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 662, in write_dataset write_data_element(fp, dataset.get_item(tag), dataset_encoding) File "/anaconda3/lib/python3.8/contextlib.py", line 131, in exit self.gen.throw(type, value, traceback) File "/anaconda3/lib/python3.8/site-packages/pydicom/tag.py", line 32, in tag_in_exception raise type(exc)(msg) from exc TypeError: With tag (0066, 0016) got exception: a bytes-like object is required, not 'MultiValue' Traceback (most recent call last): File "/anaconda3/lib/python3.8/site-packages/pydicom/tag.py", line 28, in tag_in_exception yield File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 662, in write_dataset write_data_element(fp, dataset.get_item(tag), dataset_encoding) File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 569, in write_data_element fn(buffer, elem) # type: ignore[operator] File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 297, in write_OWvalue fp.write(cast(bytes, elem.value)) TypeError: a bytes-like object is required, not 'MultiValue'

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/anaconda3/lib/python3.8/site-packages/pydicom/tag.py", line 28, in tag_in_exception yield File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 662, in write_dataset write_data_element(fp, dataset.get_item(tag), dataset_encoding) File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 562, in write_data_element fn(buffer, elem, encodings=encodings) # type: ignore[operator] File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 700, in write_sequence write_sequence_item(fp, ds, encodings) File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 726, in write_sequence_item write_dataset(fp, dataset, parent_encoding=encodings) File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 662, in write_dataset write_data_element(fp, dataset.get_item(tag), dataset_encoding) File "/anaconda3/lib/python3.8/contextlib.py", line 131, in exit self.gen.throw(type, value, traceback) File "/anaconda3/lib/python3.8/site-packages/pydicom/tag.py", line 32, in tag_in_exception raise type(exc)(msg) from exc TypeError: With tag (0066, 0011) got exception: With tag (0066, 0016) got exception: a bytes-like object is required, not 'MultiValue' Traceback (most recent call last): File "/anaconda3/lib/python3.8/site-packages/pydicom/tag.py", line 28, in tag_in_exception yield File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 662, in write_dataset write_data_element(fp, dataset.get_item(tag), dataset_encoding) File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 569, in write_data_element fn(buffer, elem) # type: ignore[operator] File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 297, in write_OWvalue fp.write(cast(bytes, elem.value)) TypeError: a bytes-like object is required, not 'MultiValue'

Traceback (most recent call last): File "/anaconda3/lib/python3.8/site-packages/pydicom/tag.py", line 28, in tag_in_exception yield File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 662, in write_dataset write_data_element(fp, dataset.get_item(tag), dataset_encoding) File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 569, in write_data_element fn(buffer, elem) # type: ignore[operator] File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 297, in write_OWvalue fp.write(cast(bytes, elem.value)) TypeError: a bytes-like object is required, not 'MultiValue'

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/anaconda3/lib/python3.8/site-packages/pydicom/tag.py", line 28, in tag_in_exception yield File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 662, in write_dataset write_data_element(fp, dataset.get_item(tag), dataset_encoding) File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 562, in write_data_element fn(buffer, elem, encodings=encodings) # type: ignore[operator] File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 700, in write_sequence write_sequence_item(fp, ds, encodings) File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 726, in write_sequence_item write_dataset(fp, dataset, parent_encoding=encodings) File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 662, in write_dataset write_data_element(fp, dataset.get_item(tag), dataset_encoding) File "/anaconda3/lib/python3.8/contextlib.py", line 131, in exit self.gen.throw(type, value, traceback) File "/anaconda3/lib/python3.8/site-packages/pydicom/tag.py", line 32, in tag_in_exception raise type(exc)(msg) from exc TypeError: With tag (0066, 0016) got exception: a bytes-like object is required, not 'MultiValue' Traceback (most recent call last): File "/anaconda3/lib/python3.8/site-packages/pydicom/tag.py", line 28, in tag_in_exception yield File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 662, in write_dataset write_data_element(fp, dataset.get_item(tag), dataset_encoding) File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 569, in write_data_element fn(buffer, elem) # type: ignore[operator] File "/anaconda3/lib/python3.8/site-packages/pydicom/filewriter.py", line 297, in write_OWvalue fp.write(cast(bytes, elem.value)) TypeError: a bytes-like object is required, not 'MultiValue'

The head of the file: Captura de tela de 2023-10-03 21-42-52

richardbeare commented 1 year ago

This is odd - can you confirm that the failure occurred writing the tck file to dicom, rather than writing the image data? i.e. are any output dicom files created, or does it fail at the first attempt?

Are you able to send something through to test? I'll send a voucher to your listed email.

EromFreitas commented 1 year ago

It fails when trying to create the tck dicom. It can even convert the T1 nifti. I sent the tck and T1 files using the voucher. Thank you for your willingness to help!

richardbeare commented 1 year ago

Those files worked for me, with the following setup on Linux:

conda create --name KB python=3.8
conda activate KB
pip install karawun
importTractography -d ~/Projects/BrainLab/karawun/tests/Data/Dicom/1.3.12.2.1107.5.2.43.167031.2019040213095021814319052.dcm --nifti T1_brain.nii.gz -t primary_motor_left_struct.tck -o /tmp/TT -v
ls /tmp/TT/primary_motor_left_struct/
FT_00.dcm

I'll send you the results.

Note that I've used the dicom template file that is available in the karawun repo, which you can also use, but it is usually preferable to use the dicom from which the T1 was derived.

However, that is another cause of potential problems - multiframe dicoms have caused problems, and I have a temporary fix in one of the branches. I don't think that is your problem as you said that the dicoms for the T1 were generated.

What platform and OS are you running on?

EromFreitas commented 1 year ago

Thank you very much for your feedback!

My OS is Ubuntu 20.04

richardbeare commented 1 year ago

Hm, I'm on 22.04, but I've run the same code from version 18, and we use 20.04 in the office. We may need to check the dicom template - are you able to deidentify and send using the previous voucher?

treanus commented 1 year ago

@EromFreitas What version of tckgen are you using? If you are using a newer version (like the mrtrix3 dev branch), tckgen generates some extra info in the .tck header. In my case that generated this "TypeError: can only str (not "NoneType" to str". Have a look at https://github.com/DevelopmentalImagingMCRI/karawun/compare/MFFVersioneer...treanus:karawun:dev_mrtrix. There is some extra checking for those new values in the header. @richardbeare might be nice to incorporate?

richardbeare commented 1 year ago

Thanks both - we're hoping to release some new features in the near future, and will incorporate these fixes. I'll probably look at more graceful failures around tck files - e.g. more robust conversions of fields and skipping fields that fail, only insisting on a minimum set.