Refactored contourlines function; Dropped unused dependencies

This commit is contained in:
Giorgio Calderone 2020-03-22 18:17:19 +01:00
parent 069bbbc4b2
commit a4bb94bfde
3 changed files with 53 additions and 63 deletions

View File

@ -4,8 +4,6 @@ uuid = "dc211083-a33a-5b79-959f-2ff34033469d"
[deps] [deps]
ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4" ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4"
ColorTypes = "3da002f7-5984-5a60-b8a6-cbb66c0b333f" ColorTypes = "3da002f7-5984-5a60-b8a6-cbb66c0b333f"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
ReusePatterns = "a39b5e78-89b5-562b-97d8-70689129df0c" ReusePatterns = "a39b5e78-89b5-562b-97d8-70689129df0c"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
StructC14N = "d2514e9c-36c4-5b8e-97e2-51e7675c221c" StructC14N = "d2514e9c-36c4-5b8e-97e2-51e7675c221c"

View File

@ -1,12 +1,9 @@
module Gnuplot module Gnuplot
using StructC14N, ColorTypes, Printf, StatsBase, ReusePatterns, DataFrames using StatsBase, ColorSchemes, ColorTypes, StructC14N, ReusePatterns
using ColorSchemes
import Base.reset import Base.reset
import Base.write import Base.write
import Base.iterate
import Base.convert
import Base.string import Base.string
export @gp, @gsp, save, linestyles, palette, contourlines, hist export @gp, @gsp, save, linestyles, palette, contourlines, hist
@ -1069,7 +1066,7 @@ end
#= #=
Example: Example:
v = randn(1000) v = randn(1000)
h = hist(v, bs=0.2) h = hist(v, bs=0.5)
@gp h.loc h.counts "w histep" h.loc h.counts "w l" @gp h.loc h.counts "w histep" h.loc h.counts "w l"
=# =#
function hist(v::Vector{T}; range=[NaN,NaN], bs=NaN, nbins=0, pad=true) where T <: Number function hist(v::Vector{T}; range=[NaN,NaN], bs=NaN, nbins=0, pad=true) where T <: Number
@ -1137,6 +1134,14 @@ end
# -------------------------------------------------------------------- # --------------------------------------------------------------------
mutable struct ContourLine
level::Int
x::Vector{Float64}
y::Vector{Float64}
z::Float64
ContourLine(z) = new(1, Vector{Float64}(), Vector{Float64}(), z)
end
function contourlines(args...; cntrparam="level auto 10") function contourlines(args...; cntrparam="level auto 10")
tmpfile = Base.Filesystem.tempname() tmpfile = Base.Filesystem.tempname()
sid = Symbol("j", Base.Libc.getpid()) sid = Symbol("j", Base.Libc.getpid())
@ -1152,62 +1157,37 @@ function contourlines(args...; cntrparam="level auto 10")
Gnuplot.exec(sid, "unset table") Gnuplot.exec(sid, "unset table")
Gnuplot.exec(sid, "reset") Gnuplot.exec(sid, "reset")
out = DataFrame() cur = ContourLine(NaN)
curlevel = NaN out = Vector{ContourLine}()
curx = Vector{Float64}()
cury = Vector{Float64}()
curid = 1
elength(x, y) = sqrt.((x[2:end] .- x[1:end-1]).^2 .+
(y[2:end] .- y[1:end-1]).^2)
function dump()
((length(curx) < 2) || isnan(curlevel)) && return nothing
tmp = DataFrame([Int, Float64, Vector{Float64}, Vector{Float64}, Vector{Float64}],
[:id, :level , :len , :x , :y])
push!(tmp, (curid, curlevel, [0.; elength(curx, cury)], [curx...], [cury...]))
append!(out, tmp)
curid += 1
# d = cumsum(elength(curx, cury))
# i0 = findall(d .<= width); sort!(i0)
# i1 = findall(d .> width)
# if (length(i0) > 0) && (length(i1) > 0)
# rot1 = atan(cury[i0[end]]-cury[i0[1]], curx[i0[end]]-curx[i0[1]]) * 180 / pi
# rot = round(mod(rot1, 360))
# x = mean(curx[i0])
# y = mean(cury[i0])
# push!(outl, "set label " * string(length(outl)+1) * " '$curlevel' at $x, $y center front rotate by $rot")
# curx = curx[i1]
# cury = cury[i1]
# end
empty!(curx)
empty!(cury)
end
for l in readlines(tmpfile) for l in readlines(tmpfile)
if length(strip(l)) == 0 l = strip(l)
dump() if l == ""
(length(cur.x) > 2) && push!(out, cur)
cur = ContourLine(cur.z)
continue continue
end end
if !isnothing(findfirst("# Contour ", l)) if !isnothing(findfirst("# Contour ", l))
dump() (length(cur.x) > 2) && push!(out, cur)
curlevel = Meta.parse(strip(split(l, ':')[2])) cur = ContourLine(Meta.parse(strip(split(l, ':')[2])))
continue continue
end end
(l[1] == '#') && continue (l[1] == '#') && continue
n = Meta.parse.(split(l)) n = Meta.parse.(split(l))
@assert length(n) == 3 @assert length(n) == 3
push!(curx, n[1]) push!(cur.x, n[1])
push!(cury, n[2]) push!(cur.y, n[2])
end end
(length(cur.x) > 2) && push!(out, cur)
rm(tmpfile) rm(tmpfile)
if nrow(out) > 0 if length(out) > 0
levels = unique(out.level) out = out[sortperm(getfield.(out, :z))]
sort!(levels) for i in 2:length(out)
out[!, :levelcount] .= 0 @assert out[i].z >= out[i-1].z
for i in 1:length(levels) if out[i].z > out[i-1].z
j = findall(out.level .== levels[i]) out[i].level = out[i-1].level + 1
out[j, :levelcount] .= i end
end end
end end
return out return out

View File

@ -28,18 +28,30 @@ s = Gnuplot.data2string(x, y, x.+y)
z = [X+Y for X in x, Y in y]; z = [X+Y for X in x, Y in y];
s = Gnuplot.data2string(z) s = Gnuplot.data2string(z)
@test all(s .== [" 5" , @test all(s .== ["1 1 5",
" 6" , "2 1 6",
" 7" , "3 1 7",
"" , "" ,
" 6" , "1 2 6",
" 7" , "2 2 7",
" 8" , "3 2 8",
"" , "" ,
" 7" , "1 3 7",
" 8" , "2 3 8",
" 9" ]) "3 3 9"])
s = Gnuplot.data2string(z, z)
@test all(s .== [" 5 5",
" 6 6",
" 7 7",
"" ,
" 6 6",
" 7 7",
" 8 8",
"" ,
" 7 7",
" 8 8",
" 9 9"])
s = Gnuplot.data2string(x, y, z) s = Gnuplot.data2string(x, y, z)
@test all(s .== [" 1 4 5" , @test all(s .== [" 1 4 5" ,
@ -137,7 +149,7 @@ name = "\$MyDataSet1"
@gp :- "set multiplot layout 2,1" :- @gp :- "set multiplot layout 2,1" :-
@gp :- "plot $name w points" ylab="Data and model" :- @gp :- "plot $name w points" ylab="Data and model" :-
@gp :- "plot $name u 1:(f(\$1)) w lines" :- @gp :- "plot $name u 1:(f(\$1)) w lines" :-
@gp :- 2 xlab="X label" ylab="Residuals" :- @gp :- 2 xlab="X label" ylab="Residuals" :-
@gp :- "plot $name u 1:((f(\$1)-\$2) / \$3):(1) w errorbars notit" @gp :- "plot $name u 1:((f(\$1)-\$2) / \$3):(1) w errorbars notit"
# Retrieve values for a, b and c # Retrieve values for a, b and c
@ -153,7 +165,7 @@ name = "\$MyDataSet1"
@gp :- :dry x y+noise e name :- @gp :- :dry x y+noise e name :-
@gp :- :dry "plot $name w points" :- @gp :- :dry "plot $name w points" :-
@gp :- :dry "plot $name u 1:(f(\$1)) w lines" :- @gp :- :dry "plot $name u 1:(f(\$1)) w lines" :-
@gp :- :dry 2 xlab="X label" ylab="Residuals" :- @gp :- :dry 2 xlab="X label" ylab="Residuals" :-
@gp :- :dry "plot $name u 1:((f(\$1)-\$2) / \$3):(1) w errorbars notit" :- @gp :- :dry "plot $name u 1:((f(\$1)-\$2) / \$3):(1) w errorbars notit" :-
@gp :- :dry @gp :- :dry
save("test.gp") # write on file test.gp save("test.gp") # write on file test.gp