Skip to content

Commit

Permalink
Add download_series() function
Browse files Browse the repository at this point in the history
  • Loading branch information
notZaki committed Sep 2, 2020
1 parent 3bf51f9 commit b0aec45
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/CancerImagingArchive.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ module CancerImagingArchive

using HTTP, CSV, DataFrames, JSON

include("download_series.jl")
export download_series

export tcia_collections, tcia_modalities, tcia_bodyparts, tcia_manufacturers, tcia_studies, tcia_series, tcia_series_size
export tcia_patients, tcia_patients_by_modality, tcia_newpatients, tcia_newstudies, tcia_sop
export tcia_single_image, tcia_images
Expand Down
43 changes: 43 additions & 0 deletions src/download_series.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
function _initialize_destination(destination, overwrite)
if overwrite
rm(destination; force = true, recursive = true)
end
if !isdir(destination)
mkpath(destination)
end
return destination
end

function download_series(series_id::AbstractString, destination = "./", overwrite = true)
_initialize_destination(destination, overwrite)
zip_file = joinpath(destination, "downloaded.zip")
tcia_images(series = series_id, file = zip_file)
unzip_command = `unzip -o $zip_file -d $destination`
run(unzip_command)
rm(zip_file)
return destination
end

function download_series(series_df::DataFrames.DataFrame, destination = "./"; append_desc = true, overwrite = true)
return [download_series(row, destination; append_desc=append_desc, overwrite=overwrite) for row in eachrow(series_df)]
end

function download_series(series::DataFrames.DataFrameRow, destination = "./"; append_desc = true, overwrite = true)
series_id = series.SeriesInstanceUID
if append_desc
destination = joinpath(destination, series.SeriesDescription)
end
return download_series(series_id, destination, overwrite)
end

function download_series(series::Dict, destination = "./"; append_desc = true, overwrite = true)
series_id = series["SeriesInstanceUID"]
if append_desc
destination = joinpath(destination, series["SeriesDescription"])
end
return download_series(series_id, destination, overwrite)
end

function download_series(series_array::Array, destination = "./"; append_desc = true, overwrite = true)
return [download_series(series, destination; append_desc=append_desc, overwrite=overwrite) for series in series_array]
end
9 changes: 9 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,15 @@ end
@test filesize(dicom_file) == 980794
end

@testset "Download series" begin
series = tcia_series(collection = "AAPM-RT-MAC", patient = "RTMAC-LIVE-001")
seriesjs = tcia_series(collection = "AAPM-RT-MAC", patient = "RTMAC-LIVE-001", format="json")
download_series(series, "./testdf")
download_series(seriesjs, "./testjs")
download_series(series, "./testdf"; overwrite = false)
end


@testset "Utilities - remove_empty!()" begin
dict_potentialy_empty_values = Dict(1 => "", 2 => "hello", 3 => "b", 4 => "", 5 => "ye")
CancerImagingArchive.remove_empty!(dict_potentialy_empty_values)
Expand Down

0 comments on commit b0aec45

Please sign in to comment.