Stream: helpdesk (published)

Topic: DICOM.jl not loading a directory


view this post on Zulip Dale Black (Nov 26 2021 at 17:36):

I am trying to load a DICOM directory using dcmdir_parse(dcm_path) and it's returning this error:

BoundsError: attempt to access 0-element Vector{UInt8} at index [-3:0]

throw_boundserror(::Vector{UInt8}, ::Tuple{UnitRange{Int64}})@abstractarray.jl:651
checkbounds@abstractarray.jl:616[inlined]
getindex@array.jl:807[inlined]
isdicom(::String)@DICOM.jl:139
#7@DICOM.jl:134[inlined]
filter(::DICOM.var"#7#8", ::Vector{String})@array.jl:2507
find_dicom_files(::String)@DICOM.jl:134
#dcmdir_parse#9@DICOM.jl:144[inlined]
dcmdir_parse@DICOM.jl:144[inlined]
top-level scope@Local: 1[inlined]

This loads fine using pydicom or ImageJ. Are there any tricks to making this work with DICOM.jl that I am missing?

Here it is in imageJ with some header info:
image.png

view this post on Zulip Dale Black (Nov 26 2021 at 17:42):

For reference, dcm_parse(dcm_file) works fine with an individual file from that same directory, but DICOM.find_dicom_files(dcm_path) returns a similar error as the one above:

BoundsError: attempt to access 0-element Vector{UInt8} at index [-3:0]

throw_boundserror(::Vector{UInt8}, ::Tuple{UnitRange{Int64}})@abstractarray.jl:651
checkbounds@abstractarray.jl:616[inlined]
getindex@array.jl:807[inlined]
isdicom(::String)@DICOM.jl:139
#7@DICOM.jl:134[inlined]
filter(::DICOM.var"#7#8", ::Vector{String})@array.jl:2507
find_dicom_files(::String)@DICOM.jl:134
top-level scope@Local: 1[inlined]

view this post on Zulip Sundar R (Nov 26 2021 at 22:54):

Is there an empty file in that directory?

view this post on Zulip Sundar R (Nov 26 2021 at 22:57):

It seems to be a bug in DICOM.jl here, where it assumes that every file in the directory has at least 132 bytes to read from it. In your case, there's either an empty file or soemthing that's being read as an empty file, hence the error.

view this post on Zulip Dale Black (Nov 26 2021 at 23:06):

Oh, it does contain these two files ".DS_Store" "Icon\r"which I have noticed pop up from time to time and seem to be associated with vscode. Do you have any insight into this, since this might be an easier fix than modifying the DICOM.jl code

view this post on Zulip Sukera (Nov 26 2021 at 23:08):

.DS_Store and Icon are a mac-specific metadata store for the files of a directory

view this post on Zulip Dale Black (Nov 26 2021 at 23:13):

Oh, so nothing I can do to avoid those then?

view this post on Zulip Dale Black (Nov 26 2021 at 23:14):

Also, it looks like the function find_dicom_files found here should be able to skip any non-dicom files in the folder, so I am not sure why that would be an issue?

function find_dicom_files(dir)
    files = joinpath.(dir, readdir(dir))
    dicom_files = filter(file -> isdicom(file), files)
    return dicom_files
end

view this post on Zulip Dale Black (Nov 26 2021 at 23:17):

Oh wait, you're right @Sundar R. If I delete the Icon and .DS_Store, both find_dicom_files and dcmdir_parse work, but when those are included, neither of the functions work

view this post on Zulip Sundar R (Nov 26 2021 at 23:41):

Dale Black said:

Oh, so nothing I can do to avoid those then?

There seem to be some third party tools: https://apple.stackexchange.com/a/296000 to avoid those files, but I don't use a Mac and can't recommend any of it myself. It's probably overkill anyway if this is the only place it bothers you, this should be fixed at the package level.

view this post on Zulip Sundar R (Nov 26 2021 at 23:43):

Dale Black said:

Also, it looks like the function find_dicom_files found here should be able to skip any non-dicom files in the folder, so I am not sure why that would be an issue?

It tries to do that by reading every file in the directory (regardless of name or extension), and seeing if the initial bytes match what a DICOM file should have. The isdicom(file) call in the second line is the one that does that, and that's where the problem comes from.

view this post on Zulip Dale Black (Nov 27 2021 at 01:53):

Thank you, this seems to fix the problem so I made a PR to DICOM.jl. I am sure they will tell me if there is a better way to go about this

function isdicom(file)
    try
        bytes = read(file, 132)[end-3:end]
        String(bytes) == "DICM"
    catch
        false
    end
end

Last updated: Oct 02 2023 at 04:34 UTC