Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More robust DICOM reading #90

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/DICOM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ end
function dcmdir_parse(dir; kwargs...)
dicom_files = find_dicom_files(dir)
unsorted_dicoms = [dcm_parse(file; kwargs...) for file in dicom_files]
dicoms = sort!(unsorted_dicoms, by = dicom -> dicom[tag"Instance Number"])
dicoms = sort!(unsorted_dicoms, by = dicom -> haskey(dicom, tag"Instance Number") ? dicom[tag"Instance Number"] : -1)
return dicoms
end

Expand Down Expand Up @@ -261,6 +261,21 @@ function read_element(st::IO, dcm::DICOMData)
return (read_element(st::IO, dcm))
end

function parse_is(x)
if x == ""
return 0
end

try
return parse(Int, x)
catch e
@warn """Cannot parse data in element $gelt:
$e
"""
return 0
end
end

data = vr == "ST" || vr == "LT" || vr == "UT" || vr == "AS" ?
String(read!(st, Array{UInt8}(undef, sz))) :
sz == 0 || vr == "XX" ? Any[] :
Expand All @@ -281,7 +296,7 @@ function read_element(st::IO, dcm::DICOMData)
vr == "DS" ?
map(x -> x == "" ? 0.0 : parse(Float64, x), string_parse(st, sz, 16, false)) :
vr == "IS" ?
map(x -> x == "" ? 0 : parse(Int, x), string_parse(st, sz, 12, false)) :
map(parse_is, string_parse(st, sz, 12, false)) :
vr == "AE" ? string_parse(st, sz, 16, false) :
vr == "CS" ? string_parse(st, sz, 16, false) :
vr == "SH" ? string_parse(st, sz, 16, false) :
Expand Down
Loading