|
1 | 1 | module TestImages |
2 | 2 | using FileIO, AxisArrays |
| 3 | +using Pkg.Artifacts |
| 4 | +const artifacts_toml = abspath(joinpath(@__DIR__, "..", "Artifacts.toml")) |
3 | 5 |
|
4 | 6 | export testimage |
5 | 7 |
|
6 | | -const imagedir = joinpath(dirname(@__FILE__), "..", "images") |
7 | | - |
8 | 8 | REPO_URL = "https://github.com/JuliaImages/TestImages.jl/blob/gh-pages/images/" |
9 | 9 |
|
10 | 10 | remotefiles = [ |
@@ -46,53 +46,69 @@ remotefiles = [ |
46 | 46 | ] |
47 | 47 |
|
48 | 48 | """ |
49 | | - testimage(filename, [ops...]) |
| 49 | + img = testimage(filename; download_only=false, [ops...]) |
| 50 | +
|
| 51 | +Load test image that partially matches `filename`, the first match is used if there're more |
| 52 | +than one. |
50 | 53 |
|
51 | | -load test image that partially matches `filename`, the first match is used if there're more than one. If `ops` is specified, it will be passed to `load` function. use `TestImages.remotefiles` to get a full list of available images. |
| 54 | +If `download_only=true`, the full filepath is returned. |
| 55 | +Any other keyword arguments `ops` will be passed to image IO backend through `FileIO.load`. |
52 | 56 |
|
53 | 57 | # Example |
| 58 | +
|
54 | 59 | ```julia |
55 | 60 | julia> using TestImages |
56 | | -julia> testimage("cameraman.tif") |
57 | | -julia> testimage("cameraman") |
58 | | -julia> testimage("c") |
| 61 | +julia> img = testimage("cameraman.tif"); # fullname |
| 62 | +julia> img = testimage("cameraman"); # without extension works |
| 63 | +julia> img = testimage("c"); # with only partial name also works |
59 | 64 | ``` |
| 65 | +
|
| 66 | +`TestImages.remotefiles` stores the full list of available images. You can also check |
| 67 | +https://testimages.juliaimages.org/ |
60 | 68 | """ |
61 | | -function testimage(filename, ops...) |
62 | | - isdir(imagedir) || mkpath(imagedir) |
63 | | - imagefile = joinpath(imagedir, filename) |
64 | | - if !isfile(imagefile) |
65 | | - fls = readdir(imagedir) |
66 | | - havefile = false |
67 | | - for f in fls |
68 | | - if startswith(f, filename) |
69 | | - imagefile = joinpath(imagedir, f) |
70 | | - havefile = true |
71 | | - break |
72 | | - end |
73 | | - end |
| 69 | +function testimage(filename; download_only = false, ops...) |
| 70 | + imagefile = image_path(full_imagename(filename)) |
74 | 71 |
|
75 | | - if !havefile |
76 | | - @info "Could not find "*filename*" in directory images/ . Checking if it exists in the online repository." |
77 | | - for f in remotefiles |
78 | | - if startswith(f, filename) |
79 | | - @info "Found "*filename*" in the online repository. Downloading to the images directory." |
80 | | - download(REPO_URL*f*"?raw=true", joinpath(imagedir, f)) |
81 | | - havefile = true |
82 | | - imagefile = joinpath(imagedir, f) |
83 | | - break |
84 | | - end |
85 | | - end |
86 | | - end |
87 | | - havefile || throw(ArgumentError("$filename not found in the directory images/ or the online repository. use `TestImages.remotefiles` to get a full list of test images")) |
88 | | - end |
89 | | - img = load(imagefile, ops...) |
90 | | - if startswith(basename(imagefile), "mri-stack") |
| 72 | + download_only && return imagefile |
| 73 | + |
| 74 | + img = load(imagefile; ops...) |
| 75 | + if basename(imagefile) == "mri-stack.tif" |
91 | 76 | # orientation is posterior-right-superior, |
92 | 77 | # see http://www.grahamwideman.com/gw/brain/orientation/orientterms.htm |
93 | 78 | return AxisArray(img, (:P, :R, :S), (1, 1, 5)) |
94 | 79 | end |
95 | 80 | img |
96 | 81 | end |
97 | 82 |
|
| 83 | +""" |
| 84 | + fullname = full_imagename(shortname) |
| 85 | +
|
| 86 | +Get the first match of `shortname` in `TestImages.remotefiles` |
| 87 | +""" |
| 88 | +function full_imagename(filename) |
| 89 | + idx = findfirst(remotefiles) do x |
| 90 | + startswith(x, filename) |
| 91 | + end |
| 92 | + idx === nothing && throw(ArgumentError("$filename not found in the online repository, use `TestImages.remotefiles` to get a full list of test images.")) |
| 93 | + |
| 94 | + return remotefiles[idx] |
| 95 | +end |
| 96 | + |
| 97 | +function image_path(imagename) |
| 98 | + file_hash = artifact_hash(imagename, artifacts_toml) |
| 99 | + |
| 100 | + if file_hash === nothing || !artifact_exists(file_hash) |
| 101 | + new_hash = create_artifact() do artifact_dir |
| 102 | + download(REPO_URL*imagename*"?raw=true", joinpath(artifact_dir, imagename)) |
| 103 | + end |
| 104 | + if file_hash === nothing |
| 105 | + bind_artifact!(artifacts_toml, |
| 106 | + imagename, |
| 107 | + new_hash) |
| 108 | + end |
| 109 | + file_hash = new_hash |
| 110 | + end |
| 111 | + return joinpath(artifact_path(file_hash), imagename) |
| 112 | +end |
| 113 | + |
98 | 114 | end # module |
0 commit comments