DevelopmentalImagingMCRI / karawun

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

trouble writing with pydicom #34

Closed mb3152 closed 1 year ago

mb3152 commented 1 year ago

Hi, this is such a great tool! I can't seem to get it to work for me, though

I want to overlay the trk on the T1. The T1 is aligned, but different resolution to the dwi. Not sure if that matters. Anyways, let me know if this error has been seen before?

importTractography \
-d /Users/maxbertolero/Desktop/test.dcm \
-o /Users/maxbertolero/Desktop/deriv/dicoms/ \
-n /Users/maxbertolero/Desktop/deriv/sub-1_desc-preproc_T1w.nii.gz \
-t /Users/maxbertolero/Desktop/deriv/sub-1_ses-15b1000and60b3000_space-T1w_desc-preproc_gqi.Corticospinal_Tract_R.trk

However, it seems pydicom is not liking the write of this:

Exception handler: OSError - With tag (0028, 0106) got exception: ushort format requires 0 <= number <= (32767 *2 +1)
for data_element:
(0028, 0106) Smallest Image Pixel Value          US: -107
Traceback (most recent call last):
  File "/Users/maxbertolero/opt/anaconda3/lib/python3.9/site-packages/pydicom/filewriter.py", line 230, in write_numbers
    value.append  # works only if list, not if string or number
AttributeError: 'int' object has no attribute 'append'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/maxbertolero/opt/anaconda3/lib/python3.9/site-packages/pydicom/filewriter.py", line 232, in write_numbers
    fp.write(pack(format_string, value))
struct.error: ushort format requires 0 <= number <= (32767 *2 +1)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/maxbertolero/opt/anaconda3/lib/python3.9/site-packages/pydicom/tag.py", line 30, in tag_in_exception
    yield
  File "/Users/maxbertolero/opt/anaconda3/lib/python3.9/site-packages/pydicom/filewriter.py", line 555, in write_dataset
    write_data_element(fp, dataset.get_item(tag), dataset_encoding)
  File "/Users/maxbertolero/opt/anaconda3/lib/python3.9/site-packages/pydicom/filewriter.py", line 482, in write_data_element
    writer_function(buffer, data_element, writer_param)
  File "/Users/maxbertolero/opt/anaconda3/lib/python3.9/site-packages/pydicom/filewriter.py", line 237, in write_numbers
    raise IOError(
OSError: ushort format requires 0 <= number <= (32767 *2 +1)
for data_element:
richardbeare commented 1 year ago

Hi, Thanks for the report. I haven't seen this error before, but my guess is that it is about the scaling of the T1. I have not attempted to make karawun do anything clever with scaling, but it looks like I should add some basic checks.

I think the MR mode dicom requires non negative pixel values, so check the range of your T1 and scale/shift the values so that all pixels are greater than or equal to 0.

The different resolution of T1 and dwi should not matter. The brainlab view will be correct if the trk file overlays correctly on the T1 using mrview.

mb3152 commented 1 year ago

Thank you for the quick reply! That seemed to get rid of that issue, but now it just throws:

Exception handler: UnicodeDecodeError - 'utf-8' codec can't decode byte 0xc0 in position 443: invalid start byte

It produced output DICOMS but they look empty. Also, I should note I made the trk file in dsi studio. Not sure if that matters.

Would it help to have the files on your end? https://www.dropbox.com/s/72rt5bh1hg6630u/karawun.zip?dl=0

richardbeare commented 1 year ago

Using a trk file will be a problem. Karawun only supports tck files (mrtrix format). Chances are that the error is caused by the tck file reader not finding the expected header data. You can confirm by converting the nifti data without the associated tractography file.

mb3152 commented 1 year ago

Oh boy, huge facepalm! Seems to be working now. I should expect a single dicom for the tck file, and then a separate folder for the T1? Do any other dicom viewers support these outputs? I don't have access to a brain lab viewer right now. I tried loading it as a segmentation in 3D Slicer, but no luck. Thank you so much for your help!

Here are the new input/outputs

https://www.dropbox.com/scl/fo/qz2xpg8xjy6zqhcrabyqs/h?dl=0&rlkey=oqzux9l7p8qf1ic2i26eynprn

richardbeare commented 1 year ago

The number of dicoms sounds right. I'm not aware of any other viewer that can display the brainlab tractography files. In the future we aim to support the newer standards for tracts that are supported by more vendors, and those will probably be viewable by 3D slicer. I believe that the dicom segmentation standard supported by 3D slicer is about voxel segmentation, rather than the surface segmentation used by brainlab for streamlines.

mb3152 commented 1 year ago

Makes sense, I’ll track down a brain lab viewer and check these out. If I end up developing anything additional with your code I’ll do a PR incase you want it! Cheers.