From 5c54eb40f8e427f738dbc617404a6e4a7c0eb9c2 Mon Sep 17 00:00:00 2001 From: pdlotko Date: Wed, 6 Sep 2017 06:01:49 +0000 Subject: commit git-svn-id: svn+ssh://scm.gforge.inria.fr/svnroot/gudhi/branches/persistence_representation_integration@2646 636b058d-ea47-450e-bf9e-a15bfbe3eedb Former-commit-id: 43b919b5c2bce586268f2f522389b64bf118f85a --- .../persistence_representations_landscapes.pyx | 4 +- ...rsistence_representations_landscapes_example.py | 26 ++--- .../include/Persistence_landscape_interface.h | 7 +- .../Persistence_landscape_on_grid_interface.h | 19 ++++ ...rsistence_representations_landscapes_on_grid.py | 120 +++++++++++++++++++++ 5 files changed, 148 insertions(+), 28 deletions(-) create mode 100755 src/cython/test/test_persistence_representations_landscapes_on_grid.py diff --git a/src/cython/cython/persistence_representations_landscapes.pyx b/src/cython/cython/persistence_representations_landscapes.pyx index 464f538b..9c3c2863 100644 --- a/src/cython/cython/persistence_representations_landscapes.pyx +++ b/src/cython/cython/persistence_representations_landscapes.pyx @@ -73,7 +73,7 @@ cdef class PersistenceLandscapes: #Can we have only one constructor, or can we have more - def __init__(self): + def __init__(self, vector_of_intervals=None, dimension=None, file_with_intervals='',number_of_levels=sys.maxsize): """ This is a class implementing persistence landscapes data structures. For theoretical description, please consult Statistical topological @@ -96,7 +96,7 @@ cdef class PersistenceLandscapes: - def __cinit__(self, vector_of_intervals=None, dimension=None, file_with_intervals='',number_of_levels=sys.maxint): + def __cinit__(self, vector_of_intervals=None, dimension=None, file_with_intervals='',number_of_levels=sys.maxsize): """ This is a constructor of a class PersistenceLandscapes. It either take text file and a positive integer, or a vector diff --git a/src/cython/example/persistence_representations_landscapes_example.py b/src/cython/example/persistence_representations_landscapes_example.py index d2b7b393..b7e47c0b 100755 --- a/src/cython/example/persistence_representations_landscapes_example.py +++ b/src/cython/example/persistence_representations_landscapes_example.py @@ -1,8 +1,6 @@ #!/usr/bin/env python import gudhi -from libcpp.vector cimport vector -from libcpp.utility cimport pair """This file is part of the Gudhi Library. The Gudhi library @@ -34,30 +32,18 @@ __license__ = "GPL v3" print("#####################################################################") print("Persistence representations landscapes example") -persistence1 = vector[pair[double, double]] -persistence2 = vector[pair[double, double]] +persistence1 = [(1,2),(6,8),(0,4),(3,8)] +persistence2 = [(2,9),(1,6),(3,5),(6,10)] -persistence1.push_back(make_pair(1, 2)) -persistence1.push_back(make_pair(6, 8)) -persistence1.push_back(make_pair(0, 4)) -persistence1.push_back(make_pair(3, 8)) - -persistence2.push_back(make_pair(2, 9)) -persistence2.push_back(make_pair(1, 6)) -persistence2.push_back(make_pair(3, 5)) -persistence2.push_back(make_pair(6, 10)) #create two persistence landscapes based on persistence1 and persistence2: -l1 = gudhi.Persistence_landscape(persistence1) -l2 = gudhi.Persistence_landscape(persistence2) +l1 = gudhi.PersistenceLandscapes(vector_of_intervals=persistence1, dimension=3) +l2 = gudhi.PersistenceLandscapes(vector_of_intervals=persistence2) #This is how to compute integral of landscapes: print "Integral of the first landscape : ", l1.compute_integral_of_landscape() print "Integral of the second landscape : ", l2.compute_integral_of_landscape() -#Arithmetic operations on landscapes: -sum_ = l1 + l2; - #here are the maxima of the functions: print "Maximum of l1 : ", l1.compute_maximum() print "Maximum of l2 : ", l2.compute_maximum() @@ -67,8 +53,8 @@ print "L^1 Norm of l1 : ", l1.compute_norm_of_landscape(1.) print "L^1 Norm of l2 : ", l2.compute_norm_of_landscape(1.) #here is the average of landscapes: -average = Persistence_landscape -average.compute_average({l1, l2}); +average = gudhi.PersistenceLandscapes +average.compute_average([l1, l2]) #here is the distance of landscapes: print "Distance : ", l1.distance(l2) diff --git a/src/cython/include/Persistence_landscape_interface.h b/src/cython/include/Persistence_landscape_interface.h index e18a678e..e5afffdc 100644 --- a/src/cython/include/Persistence_landscape_interface.h +++ b/src/cython/include/Persistence_landscape_interface.h @@ -37,12 +37,7 @@ class Persistence_landscape_interface : public Persistence_landscape Persistence_landscape_interface(const std::vector >& p, size_t number_of_levels = std::numeric_limits::max() ):Persistence_landscape(p,number_of_levels){} Persistence_landscape_interface(const char* filename, size_t dimension = std::numeric_limits::max() , size_t number_of_levels = std::numeric_limits::max() ):Persistence_landscape(filename,dimension,number_of_levels){} - - Persistence_landscape_interface* copy() - { - Persistence_landscape_interface* copy = new Persistence_landscape_interface(*this); - return copy; - } + Persistence_landscape_interface* new_abs_interface() { diff --git a/src/cython/include/Persistence_landscape_on_grid_interface.h b/src/cython/include/Persistence_landscape_on_grid_interface.h index 91a12d39..e16f0e34 100644 --- a/src/cython/include/Persistence_landscape_on_grid_interface.h +++ b/src/cython/include/Persistence_landscape_on_grid_interface.h @@ -59,7 +59,24 @@ class Persistence_landscape_on_grid_interface : public Persistence_landscape_on_ Persistence_landscape_on_grid_interface(const char* filename, size_t number_of_points, uint16_t dimension = std::numeric_limits::max()): Persistence_landscape_on_grid(filename,number_of_points,dimension){} + Persistence_landscape_on_grid_interface* new_abs_interface() + { + return (Persistence_landscape_on_grid_interface*)this->new_abs(); + } + + void new_compute_average(const std::vector& to_average) + { + std::vector to_average_new; + to_average_new.reserve( to_average.size() ); + for ( size_t i = 0 ; i != to_average.size() ; ++i ) + { + to_average_new.push_back( (Persistence_landscape_on_grid*)to_average[i] ); + } + this->compute_average(to_average_new); + } + +/* void load_landscape_from_file_interface(const char* filename) { this->load_landscape_from_file(filename); @@ -180,6 +197,8 @@ class Persistence_landscape_on_grid_interface : public Persistence_landscape_on_ { return this->get_y_range( level ); } + */ +}; } // namespace Persistence_representations } // namespace Gudhi diff --git a/src/cython/test/test_persistence_representations_landscapes_on_grid.py b/src/cython/test/test_persistence_representations_landscapes_on_grid.py new file mode 100755 index 00000000..c03b8312 --- /dev/null +++ b/src/cython/test/test_persistence_representations_landscapes_on_grid.py @@ -0,0 +1,120 @@ +import gudhi + +""" + This file is part of the Gudhi Library. The Gudhi library + (Geometric Understanding in Higher Dimensions) is a generic C++ + library for computational topology. + + Author(s): Pawel Dlotko + + Copyright (C) 2017 Swansea University + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +""" + +__author__ = "Pawel Dlotko" +__copyright__ = "Copyright (C) 2017 Swansea University" +__license__ = "GPL v3" + +epsilon = 0.0000005; + + + +def test_check_construction_of_landscape: + l = gudhi.PersistenceLandscapeOnGrid("data/file_with_diagram_1", 100,sys.maxsize) + l.print_to_file("landscape_from_file_with_diagram_1") + g = gudhi.PersistenceLandscapeOnGrid + g.load_landscape_from_file("landscape_from_file_with_diagram_1") + assert l == g + + +def test_check_construction_of_landscape_using_only_ten_levels: + number = 10 + gudhi.PersistenceLandscapeOnGrid l("data/file_with_diagram_1", 100, number) + gudhi.PersistenceLandscapeOnGrid g("data/file_with_diagram_1", 100, sys.maxsize) + for level in range(0,number): + v1 = l.vectorize(level) + v2 = g.vectorize(level) + assert v1 == v2 + +def test_check_computations_of_integrals: + p = gudhi.PersistenceLandscapeOnGrid("data/file_with_diagram_1", 100, sys.maxsize) + integral = p.compute_integral_of_landscape() + assert fabs(integral - 27.343) <= 0.00005 + +def test_check_computations_of_integrals_for_each_level_separatelly: + p = gudhi.PersistenceLandscapeOnGrid("data/file_with_diagram_1", 100, sys.maxsize) + integrals_fir_different_levels = [0.241168,0.239276,0.237882,0.235193,0.230115,0.227626,0.226132.0.223643,0.221651,0.220556,0.21727,0.215976,0.213685,0.211993,0.2102,0.208707,0.207014,0.205122,0.204226,0.202633] + for level in range(0,len(integrals_fir_different_levels)) + integral = p.compute_integral_of_landscape(level); + assert fabs(integral - integrals_fir_different_levels[level]) <= 0.00005 + +def test_check_computations_of_integrals_of_powers_of_landscape: + p = gudhi.PersistenceLandscapeOnGrid("data/file_with_diagram_1", 100, sys.maxsize) + integrals_fir_different_powers = [0.241168,0.239276,0.237882,0.235193,0.23011] + for power in range(0:5): + integral = p.compute_integral_of_landscape(power) + assert fabs(integral - integrals_fir_different_powers[power]) <= 0.00001 + +def test_check_computations_of_values_on_different_points: + p = gudhi.PersistenceLandscapeOnGrid("data/file_with_diagram_1", 100, sys.maxsize) + results_level_0 = [0.00997867,0.0521921,0.104312,0.156432,0.208552,0.260672,0.312792,0.364912,0.417032,0.429237] + results_level_10 = [7.21433e-05,0.0422135,0.0943335,0.146453,0.198573,0.240715,0.272877,0.324997,0.359232,0.379344] + double x = 0.0012321; + double dx = 0.05212; + for i in range(0,10): + assert almost_equal(p.compute_value_at_a_given_point(0, x), results_level_0[i])); + assert almost_equal(p.compute_value_at_a_given_point(10, x), results_level_10[i])); + x += dx; + +def test_check_computations_of_maxima_and_norms: + p = gudhi.PersistenceLandscapeOnGrid("data/file_with_diagram_1", 0., 1., 100) + second = gudhi.PersistenceLandscapeOnGrid("data/file_with_diagram_2", 0., 1., 100) + assert fabs(p.compute_maximum() - 0.46) <= 0.00001 + assert fabs(p.compute_norm_of_landscape(1) - 27.3373) <= 0.00001 + assert fabs(p.compute_norm_of_landscape(2) - 1.84143) <= 0.00001 + assert fabs(p.compute_norm_of_landscape(3) - 0.927067) <= 0.00001 + +def test_check_default_parameters_of_distances: + diag = read_persistence_intervals_in_dimension("data/file_with_diagram") + p = gudhi.PersistenceLandscapeOnGrid(diag, 0., 1., 100) + diag1 = read_persistence_intervals_in_dimension("data/file_with_diagram_1") + q = gudhi.PersistenceLandscapeOnGrid(diag1, 0., 1., 100) + dist_numeric_limit_max = p.distance(q, sys.maxsize); + dist_infinity = p.distance(q, sys.maxsize); + assert dist_numeric_limit_max == dist_infinity + +def test_check_computations_of_averages: + p = gudhi.PersistenceLandscapeOnGrid("data/file_with_diagram", 0., 1., 100) + q = gudhi.PersistenceLandscapeOnGrid("data/file_with_diagram_1", 0., 1., 100) + av = gudhi.PersistenceLandscapeOnGrid + av.compute_average({&p, &q) + + template_average = gudhi.PersistenceLandscapeOnGrid + template_average.load_landscape_from_file("data/average_on_a_grid") + assert template_average == av + + +def test_check_computations_of_distances: + p = gudhi.PersistenceLandscapeOnGrid("data/file_with_diagram", 0., 1., 10000) + q = gudhi.PersistenceLandscapeOnGrid("data/file_with_diagram_1", 0., 1., 10000) + assert fabs(p.distance(q) - 25.5779) <= 0.00005 + assert fabs(p.distance(q, 2) - 2.04891) <= 0.00001 + assert fabs(p.distance(q, sys.maxsize) - 0.359) <= 0.00001 + + +def test_check_computations_of_scalar_product: + p = gudhi.PersistenceLandscapeOnGrid("data/file_with_diagram", 0., 1., 10000) + q = gudhi.PersistenceLandscapeOnGrid("data/file_with_diagram_1", 0., 1., 10000) + assert almost_equal(p.compute_scalar_product(q), 0.754367) -- cgit v1.2.3