Skip to content

Commit

Permalink
fixed plots
Browse files Browse the repository at this point in the history
  • Loading branch information
wildart committed Jul 14, 2020
1 parent 1789e13 commit 6fc9721
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 29 deletions.
7 changes: 6 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ keywords = ["topological data analysis", "data science", "persistance"]
license = "MIT"
desc = "A Julia package for topological data analysis"
repository = "https://github.com/wildart/TDA.jl.git"
version = "0.1.0"
version = "0.2.0"

[deps]
ComputationalHomology = "9393a4d0-be1e-45b0-9991-b60434c721e6"
Expand All @@ -17,6 +17,11 @@ Distances = "b4f34e82-e78d-54a5-968a-f98e89d6e8f7"
MultivariateStats = "6f286f6a-111f-5878-ab1e-185364afe411"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[compat]
MultivariateStats = "^0.7.0"
ComputationalHomology = "^0.6.0"
julia = "≥ 1.1.0"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ pkg> add TDA
```julia
using TDA, ComputationalHomology, Plots
# crate some intervals of various dimensions
ints = vcat(intervals(0, 2.0=>6.0, 5.0=>10.0, 1.0=>Inf), intervals(1, 9.0=>12.0))
ints = Dict(0=>diagram(2.0=>6.0, 5.0=>10.0, 1.0=>Inf), 1=>diagram(9.0=>12.0))

# plot persistance diagram
plot(ints)
# plot barcode
Expand Down
2 changes: 1 addition & 1 deletion src/graph.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Plot a 1D simplicial subcomplex (graph) of `cplx`
@recipe function f(cplx::T, args...) where {T<:SimplicialComplex}
data = length(args) > 0 ? args[1] :
circlepoints(size(cplx, 0)+1, 1.0)
hcat(circlepoints(size(cplx, 0)+1, 1.0)...)
for (i,c) in enumerate(cells(cplx, 1))
pts = data[values(c),:]
@series begin
Expand Down
49 changes: 23 additions & 26 deletions src/pdiag.jl
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
@recipe function f(ints::Vector{Interval};
maxoutdim=1,
skipzero = false)
# get interval dimensions
dims = unique(map(i->i.dim, ints))
# group intervals by dimensions
Dict(d=>filter(i->i.dim==d, ints) for d in dims)
@recipe function f(ints::Vector{T}) where {T<:AbstractInterval}
Dict(0=>ints)
end

# Plot intervals as a persistance diagram or barcode
@recipe function f(grints::Dict{Int, Vector{Interval}};
@recipe function f(dgm::Dict{Int, Vector{T}};
maxoutdim=1,
skipzero = false)
skipzero = false) where {T<:AbstractInterval}
# set default plot type
seriestype --> :diagram

# get interval dimensions
vals = vcat(([i.b, i.d] for i in vcat(values(grints)...))...)
dims = [d for d in sort!(filter!(d->d<=maxoutdim, collect(keys(grints))))]
vals = vcat(([i.b, i.d] for i in vcat(values(dgm)...))...)
dims = [d for d in sort!(filter!(d->d<=maxoutdim, collect(keys(dgm))))]
# compute range of intervals
minval, maxval = extrema(filter!(!isinf, vals))

Expand All @@ -28,12 +23,13 @@ end
legend := :none
layout := (length(dims),1)
for (pi, d) in enumerate(dims)
dmaxval = max(maxval, foldl(max, filter(!isinf, map(i->i.d, grints[d])); init=-Inf))
dminval = min(minval, foldl(min, filter(!isinf, map(i->i.b, grints[d])); init=Inf))
points = map(i -> i.b == i.d, grints[d])
step = 1 / (skipzero ? sum(.!points) : length(grints[d]))
dmaxval = max(maxval, foldl(max, filter(!isinf, map(i->i.d, dgm[d])); init=-Inf))
dminval = min(minval, foldl(min, filter(!isinf, map(i->i.b, dgm[d])); init=Inf))
points = map(i -> i.b == i.d, dgm[d])
step = 1 / (skipzero ? sum(.!points) : length(dgm[d]))
y = step
for (i, ispoint) in zip(grints[d], points)
idxs = sortperm(dgm[d])
for (i, ispoint) in zip(dgm[d][idxs], points)
xs, ys = [i.b, isinf(i.d) ? maxval : i.d], [y, y]
ispoint && skipzero && continue
@series begin
Expand All @@ -53,17 +49,17 @@ end
end
else # Persistance Diagram
padding = (maxval - minval)*0.01 # 3%
xs = map(l->map(i->i.b,l), grints[d] for d in dims)
ys = map(l->map(i->isinf(i.d) ? maxval : i.d,l), grints[d] for d in dims)
mkr = map(l->map(i->isinf(i.d) ? :rect : :circle,l), grints[d] for d in dims)
xs = map(l->map(i->i.b,l), dgm[d] for d in dims)
ys = map(l->map(i->isinf(i.d) ? maxval : i.d,l), dgm[d] for d in dims)
mkr = map(l->map(i->isinf(i.d) ? :rect : :circle,l), dgm[d] for d in dims)
legend --> :bottomright
legendtitle --> "Homology"
label --> ["Degree $(i-1)" for i in 1:length(grints)]
#xticks := minval:maxval
# label --> ["Degree $(i-1)" for i in 1:length(dgm)]
# xticks := minval:maxval
xlims := (minval-padding, maxval+padding)
xlabel := "birth"
#yticks := 0:maxval
ylabel := "death"
xguide := "birth"
# yticks := 0:maxval
yguide := "death"
ylims := (minval, maxval*1.03)

@series begin
Expand All @@ -74,13 +70,14 @@ end
end

for i in 1:length(xs)
idxs = skipzero ? xs[i] .!== ys[i] : .!BitArray(undef, length(xs[i]))
idxs = skipzero ? xs[i] .!== ys[i] : BitArray(true for i in 1:length(xs[i]))
@series begin
primary := true
seriestype := :scatter
markershape := mkr[i][idxs]
markersize --> 2
#markerstrokecolor := col
label --> "Degree $(i-1)"
# markerstrokecolor := col
xs[i][idxs], ys[i][idxs]
end
end
Expand Down

0 comments on commit 6fc9721

Please sign in to comment.