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

Ignore non-DICOM files in a directory #83

Merged
merged 15 commits into from
Dec 13, 2021
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "DICOM"
uuid = "a26e6606-dd52-5f6a-a97f-4f611373d757"
version = "0.10.0"
version = "0.10.1"

[compat]
julia = "0.7, 1"
Expand Down
8 changes: 6 additions & 2 deletions src/DICOM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,12 @@ function find_dicom_files(dir)
end

function isdicom(file)
bytes = read(file, 132)[end-3:end]
String(bytes) == "DICM"
all_bytes = read(file, 132)
if length(all_bytes) < 132
return false
end
my_bytes = all_bytes[end-3:end]
return String(my_bytes) == "DICM"
end

function dcmdir_parse(dir; kwargs...)
Expand Down
14 changes: 13 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,21 @@ end
@testset "Parse entire folder" begin
# Following files have missing preamble and won't be parsed
# ["OT_Implicit_Little_Headless.dcm", "CT_Implicit_Little_Headless_Retired.dcm"]
# along with an invalid notdicom.dcm file which we will first create
notdicomfile = joinpath(data_folder, "notdicom.dcm")
open(notdicomfile, "w") do io
print(io, "!")
end

# First, test the isdicom() function
fileDX = download_dicom("DX_Implicit_Little_Interleaved.dcm")
@test DICOM.isdicom(fileDX) == true
@test DICOM.isdicom(notdicomfile) == false

# Second, test if all valid dicom file can be parsed
dcms = dcmdir_parse(data_folder)
@test issorted([dcm[tag"Instance Number"] for dcm in dcms])
@test length(dcms) == length(readdir(data_folder)) - 2 # -2 because of note above
@test length(dcms) == length(readdir(data_folder)) - 3 # -3 because of note above
end

@testset "Test tag macro" begin
Expand Down