Skip to content

Commit 8329059

Browse files
authored
Add the tests() function to get full path name of tests files. (#1898)
* Call the subplot() method that accepts the dictionary * Remove the invokelatest * Add the tests() function to get full path name of tests files. * Add images
1 parent 287bd31 commit 8329059

8 files changed

Lines changed: 63 additions & 28 deletions

File tree

src/GMT.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export
152152
mbimport, mbgetdata, mbsvplist, mblevitus,
153153

154154
blendimg!, lonlat2xy, xy2lonlat, df2ds, mat2ds, mat2grid, mat2img, slicecube, cubeslice, linspace, logspace, fileparts,
155-
fields, flipud, fliplr, flipdim, flipdim!, grdinterpolate, pow, tic, toc, theme, tern2cart, geodetic2enu, cpt4dcw,
155+
tests, fields, flipud, fliplr, flipdim, flipdim!, grdinterpolate, pow, tic, toc, theme, tern2cart, geodetic2enu, cpt4dcw,
156156
getregion, getattribs, getattrib, getres, gd2gmt, gmt2gd, gdalread, gdalshade, gdalwrite, gadm, xyzw2cube,
157157
coastlinesproj, graticules, orbits, orbits!, plotgrid!, leepacific, worldrectangular, worldrectgrid, togglemask,
158158

@@ -424,6 +424,7 @@ using .Laszip
424424
D[:Time]; D["Time", "b"];
425425
#plot(rand(5,2), marker=:point, lc=:red, ls=:dot, lw=1)
426426
grdimage(rand(Float32,32,32), R="0/32/0/32");
427+
grdimage(tests("coins"))
427428
I = mat2img(rand(UInt8, 32, 32, 3), clim=:zscale);
428429
grdimage(I, V=:q);
429430
grdview(rand(Float32,32,32), Vd=2);

src/grdimage.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function grdimage(cmd0::String="", arg1=nothing, arg2=nothing, arg3=nothing; fir
7272
(haskey(d, :stretch) || haskey(d, :histo_bounds)) && delete!(d, [:histo_bounds, :stretch])
7373
end
7474

75-
invokelatest(_grdimage, cmd0, arg1, arg2, arg3, O, K, d)
75+
_grdimage(cmd0, arg1, arg2, arg3, O, K, d)
7676
end
7777
function _grdimage(cmd0::String, arg1, arg2, arg3, O::Bool, K::Bool, d::Dict)
7878

src/montage.jl

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
export montage
22

3-
# This file was initially created by Claude and still needs lots of cleanings.
4-
3+
# This file was initially created by Claude and still needs to be worked on.
54

65
# ------------------------------------------------------------------------------------------
76
"""
@@ -21,7 +20,6 @@ Display multiple images arranged in a grid using GMT's subplot.
2120
- `frame`: Frame setting for panels. Default: :none
2221
- `indices`: Vector of indices selecting which images to display.
2322
- `show`: Display the result. Default: true.
24-
- `savefig`: Filename to save the montage.
2523
2624
### Example
2725
```julia
@@ -36,40 +34,39 @@ montage(imgs, grid=(2,3), titles=["A","B","C","D","E","F"], panels_size=5)
3634
montage(rand(UInt8, 64, 64, 9), grid=(3,3), margin="0.2c")
3735
```
3836
"""
39-
function montage(images; grid=nothing, panels_size=nothing, margin="0.0c",
40-
title=nothing, titles=nothing, frame=nothing, indices=nothing,
41-
show::Bool=true, noR::Bool=false, kw...)
37+
function montage(images; grid=nothing, panels_size=nothing, margin="-0.23c",
38+
title=nothing, titles=nothing, frame=nothing, indices=nothing,
39+
show::Bool=true, noR::Bool=false, kw...)
4240

4341
(indices !== nothing) && (images = images[indices]) # Apply indices selection
4442
((n = length(images)) == 0) && error("No images to display")
4543
nrows, ncols = _montage_grid_size(n, grid) # Calculate grid dimensions
4644

47-
subplot_kw = KW(kw)
48-
subplot_kw[:grid] = (nrows, ncols)
49-
subplot_kw[:margin] = margin
50-
subplot_kw[:frame] = frame
51-
(frame === nothing) && (subplot_kw[:D] = true) # No frames around panels
52-
(title !== nothing) && (subplot_kw[:title] = title)
53-
subplot_kw[:Vd] = 1
54-
55-
if panels_size !== nothing
56-
subplot_kw[:panels_size] = panels_size
57-
else # Auto size based on grid
58-
ps = max(3, min(8, 18 / max(nrows, ncols)))
59-
subplot_kw[:panels_size] = ps
60-
end
45+
d = KW(kw)
46+
d[:grid] = (nrows, ncols)
47+
d[:margin] = margin
48+
(frame == "0") ? (d[:par] = (MAP_FRAME_PEN="0",); d[:frame] = frame) :
49+
((frame !== nothing) && (d[:B] ="0"; d[:par] = (:MAP_FRAME_PEN,parse_pen(frame))))
50+
51+
(frame === nothing) && (d[:D] = true) # No frames around panels
52+
(title !== nothing) && (d[:title] = title)
53+
54+
ps = (panels_size !== nothing) ? panels_size : max(3, min(8, 18 / max(nrows, ncols)))
55+
d[:panels_size] = ps
56+
Vd = get(d, :Vd, 0) # Get the Vd option that will be consumed by subplot\
6157

62-
subplot("", false, subplot_kw) # Create subplot (since we already have the Dict pass it directly)
58+
subplot("", false, d) # Create subplot (since we already have the Dict pass it directly)
6359
d = CTRL.pocket_d[1] # Fetch options not consumed by subplot.
6460

6561
# Plot each ... input
66-
k = 0
62+
d[:J] = "x?"
63+
k = 0; n_inputs = length(images)
6764
for row in 1:nrows
6865
for col in 1:ncols
69-
k += 1
66+
((k += 1) > n_inputs) && break
7067
panel_title = (titles !== nothing && k <= length(titles)) ? titles[k] : nothing
7168
opt_R = isa(images[k], GItype) ? (noR ? "" : getR(images[k])) : ""
72-
viz(images[k], panel=(row, col), title=panel_title, R=(opt_R !== "" ? opt_R : nothing), Vd=1, show=false, d...)
69+
viz(images[k]; panel=(row, col), title=panel_title, R=(opt_R !== "" ? opt_R : nothing), Vd=Vd, show=false, d...)
7370
end
7471
end
7572
subplot(show ? :show : :end) # End subplot
@@ -78,6 +75,7 @@ function montage(images; grid=nothing, panels_size=nothing, margin="0.0c",
7875
end
7976

8077
# Calculate grid dimensions
78+
# ----------------------------------------------------------------------------------------------------------
8179
function _montage_grid_size(n, size)
8280
if size === nothing || all(x -> x === nothing || x == 0 || (isa(x, AbstractFloat) && isnan(x)), size)
8381
ncols = ceil(Int, sqrt(n))

src/statplots.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,7 +1327,7 @@ function cornerplot(arg1; first::Bool=true, kwargs...)
13271327
d[:Vd] = Vd
13281328
d[:grid] = "$(ndims)x$(ndims)"
13291329

1330-
r = subplot(; d...)
1330+
r = subplot("", false, d)
13311331
d = CTRL.pocket_d[1] # Get back what was not consumemd in subplot
13321332
(Vd >= 0) && (d[:Vd] = Vd) # Restore this in case
13331333
(Vd == 2) && return r # Almost useless but at least wont error
@@ -1472,7 +1472,7 @@ function marginalhist(arg1::Union{GDtype, Matrix{<:Real}}; first=true, kwargs...
14721472
CTRL.figsize[1] = W # Set figsize needed to compute hexagons size
14731473
doDensity = (find_in_dict(d, [:density :Density])[1] !== nothing) # For now, no control on the density computing params
14741474

1475-
r = subplot(; d...)
1475+
r = subplot("", false, d)
14761476
d = CTRL.pocket_d[1] # Get back what was not consumemd in subplot
14771477
(Vd >= 0) && (d[:Vd] = Vd) # Restore this in case
14781478
(Vd == 2) && return r # Almost useless but at least wont error

src/utils.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,31 @@ function fileparts(fn::String)
991991
return pato, fname, ext
992992
end
993993

994+
# ---------------------------------------------------------------------------------------------------
995+
"""
996+
tests(fname::String, subdir::String="assets") -> String
997+
998+
Return the full path of a test file `fname` under `TESTSDIR/subdir`.
999+
If `fname` has no extension, search for a matching file in the subdirectory.
1000+
If no match is found, issue a warning and return `""`.
1001+
"""
1002+
function tests(fname::String, subdir::String="assets")
1003+
dir = joinpath(TESTSDIR, subdir)
1004+
ext = fileparts(fname)[3]
1005+
if ext !== ""
1006+
fullpath = joinpath(dir, fname)
1007+
isfile(fullpath) && return fullpath
1008+
else
1009+
# Search for files whose basename (without extension) matches fname
1010+
isdir(dir) || (@warn("Directory $dir not found"); return "")
1011+
for f in readdir(dir)
1012+
splitext(f)[1] == fname && return joinpath(dir, f)
1013+
end
1014+
end
1015+
@warn("File \"$fname\" not found in $dir")
1016+
return ""
1017+
end
1018+
9941019
# ---------------------------------------------------------------------------------------------------
9951020
"""
9961021
I = eye(n=3) returns an n-by-n identity matrix with ones on the main diagonal and zeros elsewhere.

test/assets/cameraman.png

20.9 KB
Loading

test/assets/peppers.jpg

40.4 KB
Loading

test/test_utils.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,15 @@
4242

4343
@test !bissextile(100)
4444
@test bissextile(-4)
45+
46+
println(" TESTS function")
47+
# With extension - must find the file and return a non-empty path
48+
@test tests("cameraman.png") == joinpath(TESTSDIR, "assets", "cameraman.png")
49+
# Without extension - must find a matching file in assets/
50+
@test tests("cameraman") == joinpath(TESTSDIR, "assets", "cameraman.png")
51+
# Non-existent file - must warn and return ""
52+
@test (@test_logs (:warn,) tests("nao_existe_xpto.dat")) == ""
53+
# Non-existent file without extension
54+
@test (@test_logs (:warn,) tests("nao_existe_xpto")) == ""
55+
4556
end

0 commit comments

Comments
 (0)