Skip to content

Commit d342502

Browse files
authored
Wrap plot() and plotlinefit() under kw -> d protecting functions. (#1823)
* Wrap plot() and plotlinefit() under kw -> d protecting functions. (Completely useless apparently. So fck disparing) * Fix recent bug in peaks()
1 parent 2c26316 commit d342502

6 files changed

Lines changed: 184 additions & 13 deletions

File tree

src/GMT.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,14 @@ using GMT.Laszip
433433
theme()
434434
#rescale(mat2img(rand(UInt16, 16,16,3)))
435435
plot(rand(5,2))
436+
437+
# This one is really unbelievable. It executes only ONE F NEW LINE:
438+
# "bar(arg1, arg2; first=true, kw...) = common_plot_xyz("", cat_2_arg2(arg1, arg2, true), "bar", first, false; kw...)"
439+
# but it causes recompilation of a big chunk of common_plot_xyz() that should have been **ALL** precompiled
440+
# by the "plot(rand(5,2))" above. But no, another 3 MB of ... landed in the precompiled cache.
441+
# I'm commenting this because we can't afford adding (3)MEGABYTES for just one TINNY function.
442+
#bar(1:5, (20, 35, 30, 35, 27), width=0.5, color=:lightblue, limits=(0.5,5.5,0,40))
443+
436444
resetGMT()
437445
end
438446

src/linefit.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ plotlinefit(D, band_ab=true, band_ci=true, legend=true, show=1)
176176
"""
177177
function plotlinefit(D::Vector{<:GMTdataset}; first::Bool=true, kw...)
178178
d = KW(kw)
179+
_plotlinefit(D, first, d)
180+
end
181+
function _plotlinefit(D::Vector{<:GMTdataset}, first::Bool, d::Dict{Symbol,Any})
179182
N_clusters = numel(D)
180183
cycle_colors = (N_clusters <= 7) ? matlab_cycle_colors : simple_distinct # Will blow if > 20
181184

@@ -213,11 +216,14 @@ end
213216
# --------------------------------------------------------------------------------------------------
214217
plotlinefit(m::Matrix{<:Real}; first::Bool=true, grp::Bool=false, kw...) = plotlinefit(linearfitxy(m); first=first, grp=grp, kw...)
215218
function plotlinefit(D::GMTdataset; first::Bool=true, grp::Bool=false, kw...)
219+
d = KW(kw)
220+
_plotlinefit(D, first, grp, d)
221+
end
222+
function _plotlinefit(D::GMTdataset, first::Bool, grp::Bool, d::Dict{Symbol,Any})
216223
# Plot fit line, data points and confidence intervals (some optional) from GMTds created by linearfitxy.
217224
# `grp=true` only when originally called from plotlinefit(D::Vector{<:GMTdataset})
218225

219226
(get(D.attrib, "a", "") == "" || get(D.attrib, "b", "") == "") && (@warn("Input must be the result of linearfitxy"); return nothing)
220-
d = KW(kw)
221227

222228
function do_ribs(d, symbs, def_color::String)
223229
do_rib, rib_cor = false, ""

src/plot.jl

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,15 @@ Parameters
9999
- $(opt_savefig)
100100
"""
101101
function plot(arg1; first=true, kw...)
102+
d = KW(kw)
103+
_plot(arg1, first, d)
104+
end
105+
function _plot(arg1, first, d)
102106
# First check if arg1 is a GMTds of a linear fit and if yes, call the plotlinefit() fun
103-
if (isa(arg1, GDtype) && find_in_kwargs(kw, [:linefit :regress])[1] !== nothing)
107+
if (isa(arg1, GDtype) && is_in_dict(d, [:linefit :regress]; del=false) !== nothing)
104108
att = isa(arg1, GMTdataset) ? arg1.attrib : arg1[1].attrib
105-
(get(att, "linearfit", "") != "") && return plotlinefit(arg1; first=first, kw...)
106-
# If it didn't return above, see if we have a 'groupvar' request and if yest perform regression on grouos
107-
d = KW(kw)
109+
(get(att, "linearfit", "") != "") && return plotlinefit(arg1; first=first, d...)
110+
# If it didn't return above, see if we have a 'groupvar' request and if yes perform regression on grouops
108111
arg1 = with_xyvar(d, arg1) # But first check if we have a column selection
109112
gidx, gnames = get_group_indices(d, arg1)
110113
cycle_colors = (numel(gidx) <= 7) ? matlab_cycle_colors : simple_distinct # Will blow if > 20
@@ -119,7 +122,7 @@ function plot(arg1; first=true, kw...)
119122
return plotlinefit(Dv; first=first, d...)
120123
end
121124
end
122-
common_plot_xyz("", Tables.istable(arg1) ? arg1 : cat_1_arg(arg1, true), "plot", first, false; kw...)
125+
common_plot_xyz("", Tables.istable(arg1) ? arg1 : cat_1_arg(arg1, true), "plot", first, false, d)
123126
end
124127
plot!(arg1; kw...) = plot(arg1; first=false, kw...)
125128

@@ -464,9 +467,12 @@ function bar(cmd0::String="", arg=nothing; first=true, kw...)
464467
(cmd0 != "" && arg === nothing) && (arg = gmtread(cmd0))
465468
isa(arg, GMTdataset) && (arg::Matrix{<:Float64} = arg.data)
466469
isa(arg, Vector{<:GMTdataset}) && (arg = arg[1].data; @warn("Multi-segments not allowed in 'bar'. Keeping only first segment."))
470+
invokelatest(_bar, arg, first, d)
471+
end
472+
function _bar(arg, first::Bool, d::Dict{Symbol,Any})
467473

468474
do_cat = ((haskey(d, :stack) || haskey(d, :stacked)) && isvector(arg) && length(arg) > 2) ? false : true
469-
is_waterfall = ((val = find_in_kwargs(kw, [:stack :stacked])[1]) !== nothing && startswith(string(val), "water"))
475+
is_waterfall = ((val = hlp_desnany_str(d, [:stack :stacked])) !== "" && startswith(val, "water"))
470476
if (is_waterfall)
471477
isa(arg, Vector) && (arg = reshape(arg, 1, length(arg))) # Waterfall stacks must be matrices
472478
(arg[1] != 0) && (arg = hcat(repeat([1.0],size(arg,1)), arg)) # If first el != 0 assume coord is missing
@@ -2264,13 +2270,13 @@ function stereonet(mat::AbstractArray{T,2}; first=true, schmidt=true, wulff=fals
22642270
(is_in_dict(d, [:lt :linethickness]) === nothing) && (d[:lt] = 0.5)
22652271
d[:par] = (MAP_GRID_PEN_PRIMARY="0.25,gray", MAP_GRID_PEN_SECONDARY="0.25,black")
22662272
d[:B], d[:R], d[:J] = "pg5 sg20", :d, prj
2267-
_common_plot_xyz("", Dv, "plot", false, true, false, d) # Plot the fault planes
2273+
common_plot_xyz("", Dv, "plot", first, false, d) # Plot the fault planes
22682274

22692275
(is_in_dict(d, [:marker :Marker :shape]) === nothing) && (d[:marker] = "circ")
22702276
(is_in_dict(d, [:mec :markeredgecolor :MarkerEdgeColor]) === nothing) && (d[:mec] = "0.0p,black")
22712277
(is_in_dict(d, [:mc :markercolor]) === nothing) && (d[:mc] = "blue")
22722278
(is_in_dict(d, [:ms :markersize :MarkerSize :size]) === nothing) && (d[:ms] = "3p")
2273-
_common_plot_xyz("", Dp, "plot", true, true, false, d) # Plot the poles
2279+
common_plot_xyz("", Dp, "stereonet", false, false, d) # Plot the poles
22742280
basemap!(J="P" * CTRL.pocket_J[2] * "+a", B="a15", show=show)
22752281
end
22762282
stereonet!(mat::AbstractArray{T,2}; schmidt=true, wulff=false, kw...) where T<:Real =

src/psxy.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ function common_plot_xyz(cmd0::String, arg1, caller::String, first::Bool, is3D::
1313
(cmd0 != "" && arg1 === nothing && is_in_dict(d, [:groupvar :hue]) !== nothing) && (arg1 = gmtread(cmd0); cmd0 = "")
1414
invokelatest(_common_plot_xyz, cmd0, arg1, caller, O, K, is3D, d)
1515
end
16-
function common_plot_xyz(cmd0::String, arg1, caller::String, first::Bool, is3D::Bool, d)
16+
function common_plot_xyz(cmd0::String, arg1, caller::String, first::Bool, is3D::Bool, d::Dict{Symbol, Any})
1717
(cmd0 != "" && arg1 === nothing && is_in_dict(d, [:groupvar :hue]) !== nothing) && (arg1 = gmtread(cmd0); cmd0 = "")
1818
invokelatest(_common_plot_xyz, cmd0, arg1, caller, !first, true, is3D, d)
1919
end
20-
function _common_plot_xyz(cmd0::String, arg1, caller::String, O::Bool, K::Bool, is3D::Bool, d::Dict)
20+
function _common_plot_xyz(cmd0::String, arg1, caller::String, O::Bool, K::Bool, is3D::Bool, d::Dict{Symbol, Any})
2121
first = !O
2222
(caller != "bar") && (arg1 = if_multicols(d, arg1, is3D)) # Check if it was asked to split a GMTdataset in its columns
2323

src/utils.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ end
440440

441441
# --------------------------------------------------------------------------------------------------
442442
function peaks(n::Int=49; N::Int=49, grid::Bool=true, pixreg::Bool=false)
443-
(N != n) && (N = n)
443+
(n != 49 && N == 49) && (N = n)
444444
x,y = meshgrid(range(-3,stop=3,length=N))
445445

446446
z = 3 * (1 .- x).^2 .* exp.(-(x.^2) .- (y .+ 1).^2) .- 10*(x./5 .- x.^3 .- y.^5) .* exp.(-x.^2 .- y.^2)
@@ -540,7 +540,7 @@ function tic()
540540
end
541541

542542
function _toq()
543-
_ = time_ns()
543+
t1 = time_ns()
544544
timers = get(task_local_storage(), :TIMERS, ())
545545
(timers === ()) && error("`toc()` without `tic()`")
546546
t0 = timers[1]::UInt64

test/assets/iris.dat

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
#Sepal Length,Sepal Width,Petal Length,Petal Width,Species
2+
5.1 3.5 1.4 0.2 setosa
3+
4.9 3.0 1.4 0.2 setosa
4+
4.7 3.2 1.3 0.2 setosa
5+
4.6 3.1 1.5 0.2 setosa
6+
5.0 3.6 1.4 0.2 setosa
7+
5.4 3.9 1.7 0.4 setosa
8+
4.6 3.4 1.4 0.3 setosa
9+
5.0 3.4 1.5 0.2 setosa
10+
4.4 2.9 1.4 0.2 setosa
11+
4.9 3.1 1.5 0.1 setosa
12+
5.4 3.7 1.5 0.2 setosa
13+
4.8 3.4 1.6 0.2 setosa
14+
4.8 3.0 1.4 0.1 setosa
15+
4.3 3.0 1.1 0.1 setosa
16+
5.8 4.0 1.2 0.2 setosa
17+
5.7 4.4 1.5 0.4 setosa
18+
5.4 3.9 1.3 0.4 setosa
19+
5.1 3.5 1.4 0.3 setosa
20+
5.7 3.8 1.7 0.3 setosa
21+
5.1 3.8 1.5 0.3 setosa
22+
5.4 3.4 1.7 0.2 setosa
23+
5.1 3.7 1.5 0.4 setosa
24+
4.6 3.6 1.0 0.2 setosa
25+
5.1 3.3 1.7 0.5 setosa
26+
4.8 3.4 1.9 0.2 setosa
27+
5.0 3.0 1.6 0.2 setosa
28+
5.0 3.4 1.6 0.4 setosa
29+
5.2 3.5 1.5 0.2 setosa
30+
5.2 3.4 1.4 0.2 setosa
31+
4.7 3.2 1.6 0.2 setosa
32+
4.8 3.1 1.6 0.2 setosa
33+
5.4 3.4 1.5 0.4 setosa
34+
5.2 4.1 1.5 0.1 setosa
35+
5.5 4.2 1.4 0.2 setosa
36+
4.9 3.1 1.5 0.2 setosa
37+
5.0 3.2 1.2 0.2 setosa
38+
5.5 3.5 1.3 0.2 setosa
39+
4.9 3.6 1.4 0.1 setosa
40+
4.4 3.0 1.3 0.2 setosa
41+
5.1 3.4 1.5 0.2 setosa
42+
5.0 3.5 1.3 0.3 setosa
43+
4.5 2.3 1.3 0.3 setosa
44+
4.4 3.2 1.3 0.2 setosa
45+
5.0 3.5 1.6 0.6 setosa
46+
5.1 3.8 1.9 0.4 setosa
47+
4.8 3.0 1.4 0.3 setosa
48+
5.1 3.8 1.6 0.2 setosa
49+
4.6 3.2 1.4 0.2 setosa
50+
5.3 3.7 1.5 0.2 setosa
51+
5.0 3.3 1.4 0.2 setosa
52+
7.0 3.2 4.7 1.4 versicolor
53+
6.4 3.2 4.5 1.5 versicolor
54+
6.9 3.1 4.9 1.5 versicolor
55+
5.5 2.3 4.0 1.3 versicolor
56+
6.5 2.8 4.6 1.5 versicolor
57+
5.7 2.8 4.5 1.3 versicolor
58+
6.3 3.3 4.7 1.6 versicolor
59+
4.9 2.4 3.3 1.0 versicolor
60+
6.6 2.9 4.6 1.3 versicolor
61+
5.2 2.7 3.9 1.4 versicolor
62+
5.0 2.0 3.5 1.0 versicolor
63+
5.9 3.0 4.2 1.5 versicolor
64+
6.0 2.2 4.0 1.0 versicolor
65+
6.1 2.9 4.7 1.4 versicolor
66+
5.6 2.9 3.6 1.3 versicolor
67+
6.7 3.1 4.4 1.4 versicolor
68+
5.6 3.0 4.5 1.5 versicolor
69+
5.8 2.7 4.1 1.0 versicolor
70+
6.2 2.2 4.5 1.5 versicolor
71+
5.6 2.5 3.9 1.1 versicolor
72+
5.9 3.2 4.8 1.8 versicolor
73+
6.1 2.8 4.0 1.3 versicolor
74+
6.3 2.5 4.9 1.5 versicolor
75+
6.1 2.8 4.7 1.2 versicolor
76+
6.4 2.9 4.3 1.3 versicolor
77+
6.6 3.0 4.4 1.4 versicolor
78+
6.8 2.8 4.8 1.4 versicolor
79+
6.7 3.0 5.0 1.7 versicolor
80+
6.0 2.9 4.5 1.5 versicolor
81+
5.7 2.6 3.5 1.0 versicolor
82+
5.5 2.4 3.8 1.1 versicolor
83+
5.5 2.4 3.7 1.0 versicolor
84+
5.8 2.7 3.9 1.2 versicolor
85+
6.0 2.7 5.1 1.6 versicolor
86+
5.4 3.0 4.5 1.5 versicolor
87+
6.0 3.4 4.5 1.6 versicolor
88+
6.7 3.1 4.7 1.5 versicolor
89+
6.3 2.3 4.4 1.3 versicolor
90+
5.6 3.0 4.1 1.3 versicolor
91+
5.5 2.5 4.0 1.3 versicolor
92+
5.5 2.6 4.4 1.2 versicolor
93+
6.1 3.0 4.6 1.4 versicolor
94+
5.8 2.6 4.0 1.2 versicolor
95+
5.0 2.3 3.3 1.0 versicolor
96+
5.6 2.7 4.2 1.3 versicolor
97+
5.7 3.0 4.2 1.2 versicolor
98+
5.7 2.9 4.2 1.3 versicolor
99+
6.2 2.9 4.3 1.3 versicolor
100+
5.1 2.5 3.0 1.1 versicolor
101+
5.7 2.8 4.1 1.3 versicolor
102+
6.3 3.3 6.0 2.5 virginica
103+
5.8 2.7 5.1 1.9 virginica
104+
7.1 3.0 5.9 2.1 virginica
105+
6.3 2.9 5.6 1.8 virginica
106+
6.5 3.0 5.8 2.2 virginica
107+
7.6 3.0 6.6 2.1 virginica
108+
4.9 2.5 4.5 1.7 virginica
109+
7.3 2.9 6.3 1.8 virginica
110+
6.7 2.5 5.8 1.8 virginica
111+
7.2 3.6 6.1 2.5 virginica
112+
6.5 3.2 5.1 2.0 virginica
113+
6.4 2.7 5.3 1.9 virginica
114+
6.8 3.0 5.5 2.1 virginica
115+
5.7 2.5 5.0 2.0 virginica
116+
5.8 2.8 5.1 2.4 virginica
117+
6.4 3.2 5.3 2.3 virginica
118+
6.5 3.0 5.5 1.8 virginica
119+
7.7 3.8 6.7 2.2 virginica
120+
7.7 2.6 6.9 2.3 virginica
121+
6.0 2.2 5.0 1.5 virginica
122+
6.9 3.2 5.7 2.3 virginica
123+
5.6 2.8 4.9 2.0 virginica
124+
7.7 2.8 6.7 2.0 virginica
125+
6.3 2.7 4.9 1.8 virginica
126+
6.7 3.3 5.7 2.1 virginica
127+
7.2 3.2 6.0 1.8 virginica
128+
6.2 2.8 4.8 1.8 virginica
129+
6.1 3.0 4.9 1.8 virginica
130+
6.4 2.8 5.6 2.1 virginica
131+
7.2 3.0 5.8 1.6 virginica
132+
7.4 2.8 6.1 1.9 virginica
133+
7.9 3.8 6.4 2.0 virginica
134+
6.4 2.8 5.6 2.2 virginica
135+
6.3 2.8 5.1 1.5 virginica
136+
6.1 2.6 5.6 1.4 virginica
137+
7.7 3.0 6.1 2.3 virginica
138+
6.3 3.4 5.6 2.4 virginica
139+
6.4 3.1 5.5 1.8 virginica
140+
6.0 3.0 4.8 1.8 virginica
141+
6.9 3.1 5.4 2.1 virginica
142+
6.7 3.1 5.6 2.4 virginica
143+
6.9 3.1 5.1 2.3 virginica
144+
5.8 2.7 5.1 1.9 virginica
145+
6.8 3.2 5.9 2.3 virginica
146+
6.7 3.3 5.7 2.5 virginica
147+
6.7 3.0 5.2 2.3 virginica
148+
6.3 2.5 5.0 1.9 virginica
149+
6.5 3.0 5.2 2.0 virginica
150+
6.2 3.4 5.4 2.3 virginica
151+
5.9 3.0 5.1 1.8 virginica

0 commit comments

Comments
 (0)