Skip to content

Saving bifiltration to RIVET #39

@odinhg

Description

@odinhg

Describe the bug:

I believe the to_rivet() method of the SimplexTreeMulti class does not work as expected for multi-critical filtrations. It includes brackets that should not be there if I understand the RIVET documentation correctly. For example, for a simplex $\sigma=\lbrace0,1\rbrace$ with birth set $b(\sigma)=\lbrace(0,1),(1,0)\rbrace$, calling to_rivet() creates a line 0 1 ; [0. 1.] [1. 0.] in the output file, but it should be 0 1 ; 0 1 1 0.

Also, for one-critical filtrations, if a filtration value is -inf then RIVET does not recognize this value and throws an error.

Minimal example:

import gudhi as gd
import multipers as mp
import numpy as np

def test_rivet(st):
    for s, F in st.get_simplices():
        print(f"Simplex: {s}, filtration value(s): {F}")

    st.to_rivet("rivet_dataset.txt", overwrite=True)

    with open("rivet_dataset.txt", "r") as f:
        print(f.read())

# One-critical example
st = gd.SimplexTree() # usual gudhi simplextree
st.insert([0,1],1)
st.insert([1],0)
# converts the simplextree into a multiparameter simplextree
st_multi = mp.SimplexTreeMulti(st, num_parameters=2)
test_rivet(st_multi)

# Multi-critical example
st = mp.SimplexTreeMulti(num_parameters=2, kcritical=True, dtype = np.float64)
st.insert([0,1,2], [0,1])
st.insert([0,1,2], [1,0])
st.remove_maximal_simplex([0,1,2])
st.insert([0,1,2], [1,2])
st.insert([0,1,2], [2,1])
st.insert([0,1,2],[1.5,1.5])
st.insert([0,1,2], [2.5,.5])
st.insert([0,1,2], [.5,2.5])
test_rivet(st)

which outputs

Simplex: [0 1], filtration value(s): [  1. -inf]
Simplex: [0], filtration value(s): [  1. -inf]
Simplex: [1], filtration value(s): [  0. -inf]
# This file was generated by multipers.
--datatype bifiltration
--xlabel time of appearance
--ylabel density

# block of dimension 0
0 ; 1.0 -inf 
1 ; 0.0 -inf 
# block of dimension 1
0 1 ; 1.0 -inf 

Simplex: [0 1 2], filtration value(s): [array([0.5, 2.5]), array([1., 2.]), array([1.5, 1.5]), array([2., 1.]), array([2.5, 0.5])]
Simplex: [0 1], filtration value(s): [array([0., 1.]), array([1., 0.])]
Simplex: [0 2], filtration value(s): [array([0., 1.]), array([1., 0.])]
Simplex: [0], filtration value(s): [array([0., 1.]), array([1., 0.])]
Simplex: [1 2], filtration value(s): [array([0., 1.]), array([1., 0.])]
Simplex: [1], filtration value(s): [array([0., 1.]), array([1., 0.])]
Simplex: [2], filtration value(s): [array([0., 1.]), array([1., 0.])]
# This file was generated by multipers.
--datatype bifiltration
--xlabel time of appearance
--ylabel density

# block of dimension 0
0 ; [0. 1.] [1. 0.] 
1 ; [0. 1.] [1. 0.] 
2 ; [0. 1.] [1. 0.] 
# block of dimension 1
0 1 ; [0. 1.] [1. 0.] 
0 2 ; [0. 1.] [1. 0.] 
1 2 ; [0. 1.] [1. 0.] 
# block of dimension 2
0 1 2 ; [0.5 2.5] [1. 2.] [1.5 1.5] [2. 1.] [2.5 0.5] 

Trying to import the last one in RIVET gives the following (expected) error message:

Computation thread running
Checking for a module invariants file
# Thi
console:  READING FILE: rivet_dataset.txt 

console:  DataReader: Found a bifiltration file.

console:   

console:  terminate called after throwing an instance of 'InputError'

console:    what():  line 7: Could not read vertex: '[0.' is not a number

terminate called after throwing an instance of 'std::runtime_error'
  what():  Arrangement was not delivered
Aborted (core dumped)

For the first one, we get:

Computation thread running
Checking for a module invariants file
# Thi
console:  READING FILE: rivet_dataset.txt 

console:  DataReader: Found a bifiltration file.

console:   

console:  terminate called after throwing an instance of 'InputError'

console:    what():  line 7: Could not read vertex: '-inf' is not a number

terminate called after throwing an instance of 'std::runtime_error'
  what():  Arrangement was not delivered
Aborted (core dumped)

Maybe writing to a string or a buffer before writing the bifiltration to file can speed things up a bit too?

Setup:

  • OS [e.g., macOS 14 on arm, fedora]: EndeavourOS Linux (Kernel is 6.13.2-arch1-1)
  • version [e.g. 2.0, or commit hash if compiled]: f0bcd41
  • C++ compiler (if compiled from source) [e.g., gcc 12.1, clang 18.1]: g++ (conda-forge gcc 13.3.0-1) 13.3.0

Checks.

  • I have a working environment (i.e., pytest multipers passes)
  • I have checked that the bug is not already present in the issue tracker
  • I am willing to do a PR to fix this issue (Thank you!)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions