From bcdf9e795ae5c75c87f5b667b5fc9a0e59e31030 Mon Sep 17 00:00:00 2001 From: Gard Spreemann Date: Thu, 15 Mar 2018 16:45:16 +0100 Subject: Support for reading persistence diagrams in the DIPHA format. --- geom_matching/wasserstein/CMakeLists.txt | 9 ++ .../wasserstein/example/wasserstein_dist_dipha.cpp | 132 +++++++++++++++++++++ .../wasserstein/include/basic_defs_ws.hpp | 26 ++++ geom_matching/wasserstein/include/diagram_reader.h | 84 ++++++++++++- 4 files changed, 250 insertions(+), 1 deletion(-) create mode 100644 geom_matching/wasserstein/example/wasserstein_dist_dipha.cpp diff --git a/geom_matching/wasserstein/CMakeLists.txt b/geom_matching/wasserstein/CMakeLists.txt index 98f3cfd..b59378c 100644 --- a/geom_matching/wasserstein/CMakeLists.txt +++ b/geom_matching/wasserstein/CMakeLists.txt @@ -5,6 +5,12 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON) include (GenerateExportHeader) +include(TestBigEndian) +test_big_endian(BIG_ENDIAN) +if(BIG_ENDIAN) + add_definitions(-DBIGENDIAN) +endif() + # Default to Release if (NOT CMAKE_BUILD_TYPE) @@ -43,6 +49,9 @@ set (libraries ${libraries} ${CMAKE_THREAD_LIBS_INIT}) add_executable(wasserstein_dist ${CMAKE_CURRENT_SOURCE_DIR}/example/wasserstein_dist.cpp ${WS_HEADERS}) target_link_libraries(wasserstein_dist PUBLIC ${libraries}) +add_executable(wasserstein_dist_dipha ${CMAKE_CURRENT_SOURCE_DIR}/example/wasserstein_dist_dipha.cpp ${WS_HEADERS}) +target_link_libraries(wasserstein_dist_dipha PUBLIC ${libraries}) + # pure geometric version, arbitrary dimension add_executable(wasserstein_dist_point_cloud ${CMAKE_CURRENT_SOURCE_DIR}/example/wasserstein_dist_point_cloud.cpp ${WS_HEADERS}) target_link_libraries(wasserstein_dist_point_cloud PUBLIC ${libraries}) diff --git a/geom_matching/wasserstein/example/wasserstein_dist_dipha.cpp b/geom_matching/wasserstein/example/wasserstein_dist_dipha.cpp new file mode 100644 index 0000000..a9029fe --- /dev/null +++ b/geom_matching/wasserstein/example/wasserstein_dist_dipha.cpp @@ -0,0 +1,132 @@ +/* + +Copyright (c) 2015, M. Kerber, D. Morozov, A. Nigmetov +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +You are under no obligation whatsoever to provide any bug fixes, patches, or +upgrades to the features, functionality or performance of the source code +(Enhancements) to anyone; however, if you choose to make your Enhancements +available either publicly, or directly to copyright holder, +without imposing a separate written license agreement for such Enhancements, +then you hereby grant the following license: a non-exclusive, royalty-free +perpetual license to install, use, modify, prepare derivative works, incorporate +into other computer software, distribute, and sublicense such enhancements or +derivative works thereof, in binary and source code form. + + */ + +#include +#include +#include +#include + +//#define LOG_AUCTION + +//#include "auction_runner_fr.h" +//#include "auction_runner_fr.hpp" + +#include "wasserstein.h" + +// any container of pairs of doubles can be used, +// we use vector in this example. + +int main(int argc, char* argv[]) +{ + using PairVector = std::vector>; + PairVector diagramA, diagramB; + + hera::AuctionParams params; + + if (argc < 4 ) { + std::cerr << "Usage: " << argv[0] << " file1 file2 ph_degree [wasserstein_degree] [relative_error] [internal norm] [initial epsilon] [epsilon_factor] [max_bids_per_round] [gamma_threshold][log_filename_prefix]. By default power is 1.0, relative error is 0.01, internal norm is l_infinity, initall epsilon is chosen automatically, epsilon factor is 5.0, Jacobi variant is used (max bids per round is maximal), gamma_threshold = 0.0." << std::endl; + return 1; + } + + unsigned int dim = std::stoul(argv[3]); + + if (!hera::read_diagram_dipha(argv[1], dim, diagramA)) { + std::exit(1); + } + + if (!hera::read_diagram_dipha(argv[2], dim, diagramB)) { + std::exit(1); + } + + params.wasserstein_power = (5 <= argc) ? atof(argv[4]) : 1.0; + if (params.wasserstein_power < 1.0) { + std::cerr << "The third argument (wasserstein_degree) was \"" << argv[4] << "\", must be a number >= 1.0. Cannot proceed. " << std::endl; + std::exit(1); + } + + if (params.wasserstein_power == 1.0) { + hera::remove_duplicates(diagramA, diagramB); + } + + //default relative error: 1% + params.delta = (6 <= argc) ? atof(argv[5]) : 0.01; + if ( params.delta <= 0.0) { + std::cerr << "The 4th argument (relative error) was \"" << argv[5] << "\", must be a number > 0.0. Cannot proceed. " << std::endl; + std::exit(1); + } + + // default for internal metric is l_infinity + params.internal_p = ( 7 <= argc ) ? atof(argv[6]) : hera::get_infinity(); + if (std::isinf(params.internal_p)) { + params.internal_p = hera::get_infinity(); + } + + + if (not hera::is_p_valid_norm(params.internal_p)) { + std::cerr << "The 6th argument (internal norm) was \"" << argv[6] << "\", must be a number >= 1.0 or inf. Cannot proceed. " << std::endl; + std::exit(1); + } + + // if you want to specify initial value for epsilon and the factor + // for epsilon-scaling + params.initial_epsilon= ( 8 <= argc ) ? atof(argv[7]) : 0.0 ; + + if (params.initial_epsilon < 0.0) { + std::cerr << "The 7th argument (initial epsilon) was \"" << argv[7] << "\", must be a non-negative number. Cannot proceed." << std::endl; + std::exit(1); + } + + params.epsilon_common_ratio = ( 9 <= argc ) ? atof(argv[8]) : 0.0 ; + if (params.epsilon_common_ratio <= 1.0 and params.epsilon_common_ratio != 0.0) { + std::cerr << "The 8th argument (epsilon factor) was \"" << argv[8] << "\", must be a number greater than 1. Cannot proceed." << std::endl; + std::exit(1); + } + + + params.max_bids_per_round = ( 10 <= argc ) ? atoi(argv[9]) : 0; + if (params.max_bids_per_round == 0) + params.max_bids_per_round = std::numeric_limits::max(); + + + params.gamma_threshold = (11 <= argc) ? atof(argv[10]) : 0.0; + + std::string log_filename_prefix = ( 12 <= argc ) ? argv[11] : ""; + + params.max_num_phases = 800; + +#ifdef LOG_AUCTION + spdlog::set_level(spdlog::level::info); +#endif + + double res = hera::wasserstein_dist(diagramA, diagramB, params, log_filename_prefix); + + std::cout << std::setprecision(15) << res << std::endl; + + return 0; + +} diff --git a/geom_matching/wasserstein/include/basic_defs_ws.hpp b/geom_matching/wasserstein/include/basic_defs_ws.hpp index 629a2f8..de25d48 100644 --- a/geom_matching/wasserstein/include/basic_defs_ws.hpp +++ b/geom_matching/wasserstein/include/basic_defs_ws.hpp @@ -25,9 +25,14 @@ derivative works thereof, in binary and source code form. */ +#pragma once + #include #include #include +#include +#include +#include #include "basic_defs_ws.h" #ifndef FOR_R_TDA @@ -37,6 +42,9 @@ derivative works thereof, in binary and source code form. #include namespace hera { +static const int64_t DIPHA_MAGIC = 8067171840; +static const int64_t DIPHA_PERSISTENCE_DIAGRAM = 2; + namespace ws { // Point @@ -190,4 +198,22 @@ std::string format_int(T i) } // end of namespace ws + + +template inline void reverse_endianness(T & x) +{ + uint8_t * p = reinterpret_cast(&x); + std::reverse(p, p + sizeof(T)); +} + +template inline T read_le(std::istream & s) +{ + T result; + s.read(reinterpret_cast(&result), sizeof(T)); + #ifdef BIGENDIAN + reverse_endianness(result); + #endif + return result; +} + } // hera diff --git a/geom_matching/wasserstein/include/diagram_reader.h b/geom_matching/wasserstein/include/diagram_reader.h index 5c690fa..b6ec635 100644 --- a/geom_matching/wasserstein/include/diagram_reader.h +++ b/geom_matching/wasserstein/include/diagram_reader.h @@ -40,8 +40,10 @@ derivative works thereof, in binary and source code form. #include #include #include -#include #include +#include + +#include "basic_defs_ws.hpp" #ifdef WASSERSTEIN_PURE_GEOM #include "dnn/geometry/euclidean-dynamic.h" @@ -200,6 +202,86 @@ bool read_diagram_point_set(const std::string& fname, ContType_& result) return read_diagram_point_set(fname.c_str(), result, decPrecision); } + template > > + bool read_diagram_dipha(const std::string& fname, unsigned int degree, ContType_& result) +{ + std::ifstream file; + file.open(fname, std::ios::in | std::ios::binary); + + if (!file.is_open()) + { + #ifndef FOR_R_TDA + std::cerr << "Could not open file " << fname << "." << std::endl; + #endif + return false; + } + + if (read_le(file) != DIPHA_MAGIC) + { + #ifndef FOR_R_TDA + std::cerr << "File " << fname << " is not a valid DIPHA file." << std::endl; + #endif + file.close(); + return false; + } + + if (read_le(file) != DIPHA_PERSISTENCE_DIAGRAM) + { + #ifndef FOR_R_TDA + std::cerr << "File " << fname << " is not a valid DIPHA persistence diagram file." << std::endl; + #endif + file.close(); + return false; + } + + result.clear(); + + int n = read_le(file); + + for (int i = 0; i < n; ++i) + { + int tmp_d = read_le(file); + double birth = read_le(file); + double death = read_le(file); + + if (death < birth) + { + #ifndef FOR_R_TDA + std::cerr << "File " << fname << " is malformed." << std::endl; + #endif + file.close(); + return false; + } + + int d = 0; + if (tmp_d < 0) + { + d = -tmp_d - 1; + death = std::numeric_limits::infinity(); + } + else + d = tmp_d; + + if ((unsigned int)d == degree) + { + if (death == birth) + { + #ifndef FOR_R_TDA + std::cerr << "Warning: point with 0 persistence ignored in " << fname << "." << std::endl; + #endif + } + else + { + result.push_back(std::make_pair(birth, death)); + } + } + } + + file.close(); + + return true; +} + template void remove_duplicates(ContType& dgm_A, ContType& dgm_B) -- cgit v1.2.3