Skip to content

Commit

Permalink
Lazy Vietoris-Rips which is sorted by construction. Worth writing up???
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikael Vejdemo-Johansson committed May 6, 2024
1 parent 132595b commit 4e5161b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
23 changes: 20 additions & 3 deletions src/main/scala/org/appliedtopology/tda4j/VietorisRips.scala
Expand Up @@ -349,7 +349,8 @@ object LazyStratifiedVietorisRips {
def apply[VertexT: Ordering](
metricSpace: FiniteMetricSpace[VertexT],
maxFiltrationValue: Double,
maxDimension: Int = 2) : Array[LazyList[AbstractSimplex[VertexT]]] =
maxDimension: Int = 2
): Array[LazyList[AbstractSimplex[VertexT]]] =
this(metricSpace, Some(maxFiltrationValue), maxDimension)

def apply[VertexT: Ordering](
Expand All @@ -360,6 +361,9 @@ object LazyStratifiedVietorisRips {

val filtrationValue = FiniteMetricSpace.MaximumDistanceFiltrationValue[VertexT](metricSpace)

inline def maxSize: Int = maxDimension + 1
val maxFVal = maxFiltrationValue.getOrElse(metricSpace.minimumEnclosingRadius)

val lazyLists: Array[LazyList[AbstractSimplex[VertexT]]] = Array.ofDim(maxDimension + 1)
lazyLists(0) = LazyList.from(metricSpace.elements.map(AbstractSimplex(_)))
(1 to maxDimension).foreach(lazyLists(_) = LazyList.empty)
Expand All @@ -368,6 +372,9 @@ object LazyStratifiedVietorisRips {
metricSpace.elements.toList
.combinations(2)
.toVector
.filter { xys =>
val List(x,y) = xys; metricSpace.distance(x,y) <= maxFVal
}
.sortBy { xys =>
val List(x, y) = xys; metricSpace.distance(x, y)
}
Expand All @@ -393,8 +400,7 @@ object LazyStratifiedVietorisRips {
)
@tailrec def oneStep(foldState: FoldState): Array[LazyList[AbstractSimplex[VertexT]]] =
if (foldState.taskStack.isEmpty) foldState.outputLists
else if (foldState.taskStack.head._1.size > maxDimension)
oneStep(foldState.copy(taskStack = foldState.taskStack.tail))
else if (foldState.taskStack.head._1.size > maxSize) oneStep(foldState.copy(taskStack = foldState.taskStack.tail))
else {
val (simplex, edge) = foldState.taskStack.head
val List(src, tgt) = edge.toList.sorted // ensure src < tgt
Expand Down Expand Up @@ -431,3 +437,14 @@ object LazyStratifiedVietorisRips {
oneStep(startState)
}
}

class LazyStratifiedCliqueFinder[VertexT: Ordering]() extends CliqueFinder[VertexT] {
override val className: String = "LazyStratifiedCliqueFinder"
def apply(
metricSpace: FiniteMetricSpace[VertexT],
maxFiltrationValue: Double = Double.PositiveInfinity,
maxDimension: Int = 2
): Seq[AbstractSimplex[VertexT]] =
LazyStratifiedVietorisRips(metricSpace, maxFiltrationValue, maxDimension)
.foldRight(LazyList[AbstractSimplex[VertexT]]())((lz, sq) => sq #::: lz)
}
14 changes: 13 additions & 1 deletion src/test/scala/org/appliedtopology/tda4j/ProfilingSpec.scala
Expand Up @@ -11,7 +11,7 @@ import collection.immutable.BitSet
class ProfilingSpec(args: Arguments) extends mutable.Specification {
"""This is a profiling script to measure performance of different implementations.""" >> {
val bitlength: Int = args.commandLine.intOr("bitlength", 3)
val maxFVal: Double = args.commandLine.doubleOr("maxFVal", 7.0)
val maxFVal: Double = args.commandLine.doubleOr("maxFVal", 4.0)
val maxDim: Int = args.commandLine.intOr("maxDim", 7)

val symmetry: HyperCubeSymmetry = HyperCubeSymmetry(bitlength)
Expand Down Expand Up @@ -110,5 +110,17 @@ class ProfilingSpec(args: Arguments) extends mutable.Specification {
symmetry_gen.representatives.size === symmetry_gen.representatives.size
}
section("ripser-gens")

section("lazy-stratified")
"Lazy Vietoris-Rips sorted by construction" >> {
val zi: HyperCubeProfiling =
HyperCubeProfiling(
LazyStratifiedCliqueFinder[Int](),
symmetry,
bitlength,
"LS"
)
}
section("lazy-stratified")
}
}

0 comments on commit 4e5161b

Please sign in to comment.