Refactored contourlines function; Dropped unused dependencies
This commit is contained in:
parent
069bbbc4b2
commit
a4bb94bfde
@ -4,8 +4,6 @@ uuid = "dc211083-a33a-5b79-959f-2ff34033469d"
|
||||
[deps]
|
||||
ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4"
|
||||
ColorTypes = "3da002f7-5984-5a60-b8a6-cbb66c0b333f"
|
||||
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
|
||||
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
|
||||
ReusePatterns = "a39b5e78-89b5-562b-97d8-70689129df0c"
|
||||
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
|
||||
StructC14N = "d2514e9c-36c4-5b8e-97e2-51e7675c221c"
|
||||
|
||||
@ -1,12 +1,9 @@
|
||||
module Gnuplot
|
||||
|
||||
using StructC14N, ColorTypes, Printf, StatsBase, ReusePatterns, DataFrames
|
||||
using ColorSchemes
|
||||
using StatsBase, ColorSchemes, ColorTypes, StructC14N, ReusePatterns
|
||||
|
||||
import Base.reset
|
||||
import Base.write
|
||||
import Base.iterate
|
||||
import Base.convert
|
||||
import Base.string
|
||||
|
||||
export @gp, @gsp, save, linestyles, palette, contourlines, hist
|
||||
@ -1069,7 +1066,7 @@ end
|
||||
#=
|
||||
Example:
|
||||
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"
|
||||
=#
|
||||
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")
|
||||
tmpfile = Base.Filesystem.tempname()
|
||||
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, "reset")
|
||||
|
||||
out = DataFrame()
|
||||
curlevel = NaN
|
||||
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
|
||||
|
||||
cur = ContourLine(NaN)
|
||||
out = Vector{ContourLine}()
|
||||
for l in readlines(tmpfile)
|
||||
if length(strip(l)) == 0
|
||||
dump()
|
||||
l = strip(l)
|
||||
if l == ""
|
||||
(length(cur.x) > 2) && push!(out, cur)
|
||||
cur = ContourLine(cur.z)
|
||||
continue
|
||||
end
|
||||
if !isnothing(findfirst("# Contour ", l))
|
||||
dump()
|
||||
curlevel = Meta.parse(strip(split(l, ':')[2]))
|
||||
(length(cur.x) > 2) && push!(out, cur)
|
||||
cur = ContourLine(Meta.parse(strip(split(l, ':')[2])))
|
||||
continue
|
||||
end
|
||||
(l[1] == '#') && continue
|
||||
|
||||
n = Meta.parse.(split(l))
|
||||
@assert length(n) == 3
|
||||
push!(curx, n[1])
|
||||
push!(cury, n[2])
|
||||
push!(cur.x, n[1])
|
||||
push!(cur.y, n[2])
|
||||
end
|
||||
(length(cur.x) > 2) && push!(out, cur)
|
||||
rm(tmpfile)
|
||||
|
||||
if nrow(out) > 0
|
||||
levels = unique(out.level)
|
||||
sort!(levels)
|
||||
out[!, :levelcount] .= 0
|
||||
for i in 1:length(levels)
|
||||
j = findall(out.level .== levels[i])
|
||||
out[j, :levelcount] .= i
|
||||
if length(out) > 0
|
||||
out = out[sortperm(getfield.(out, :z))]
|
||||
for i in 2:length(out)
|
||||
@assert out[i].z >= out[i-1].z
|
||||
if out[i].z > out[i-1].z
|
||||
out[i].level = out[i-1].level + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
return out
|
||||
|
||||
@ -28,18 +28,30 @@ s = Gnuplot.data2string(x, y, x.+y)
|
||||
|
||||
z = [X+Y for X in x, Y in y];
|
||||
s = Gnuplot.data2string(z)
|
||||
@test all(s .== [" 5" ,
|
||||
" 6" ,
|
||||
" 7" ,
|
||||
"" ,
|
||||
" 6" ,
|
||||
" 7" ,
|
||||
" 8" ,
|
||||
"" ,
|
||||
" 7" ,
|
||||
" 8" ,
|
||||
" 9" ])
|
||||
@test all(s .== ["1 1 5",
|
||||
"2 1 6",
|
||||
"3 1 7",
|
||||
"" ,
|
||||
"1 2 6",
|
||||
"2 2 7",
|
||||
"3 2 8",
|
||||
"" ,
|
||||
"1 3 7",
|
||||
"2 3 8",
|
||||
"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)
|
||||
@test all(s .== [" 1 4 5" ,
|
||||
@ -137,7 +149,7 @@ name = "\$MyDataSet1"
|
||||
@gp :- "set multiplot layout 2,1" :-
|
||||
@gp :- "plot $name w points" ylab="Data and model" :-
|
||||
@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"
|
||||
|
||||
# Retrieve values for a, b and c
|
||||
@ -153,7 +165,7 @@ name = "\$MyDataSet1"
|
||||
@gp :- :dry x y+noise e name :-
|
||||
@gp :- :dry "plot $name w points" :-
|
||||
@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
|
||||
save("test.gp") # write on file test.gp
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user