examples/cohomology/rips-pairwise-cohomology.cpp
author Dmitriy Morozov <dmitriy@mrzv.org>
Mon Jul 25 23:21:29 2011 -0700 (6 months ago)
branchdev
changeset 244 66235db8d8b7
parent 18725e468323d77
permissions -rw-r--r--
Added cohomology/candidates counter to i/t/cohomology-persistence.hpp
     1 #include <topology/cohomology-persistence.h>
     2 #include <topology/rips.h>
     3 
     4 #include <geometry/l2distance.h>
     5 #include <geometry/distances.h>
     6 
     7 #include <utilities/containers.h>           // for BackInsertFunctor
     8 #include <utilities/property-maps.h>
     9 #include <utilities/timer.h>
    10 #include <utilities/log.h>
    11 #include <utilities/counter.h>
    12 #include <utilities/memory.h>
    13 
    14 #include <string>
    15 
    16 #include <boost/tuple/tuple.hpp>
    17 #include <boost/program_options.hpp>
    18 #include <boost/progress.hpp>
    19 
    20 #include "wrappers.h"
    21 
    22 typedef     PairwiseDistances<PointContainer, L2Distance>           PairDistances;
    23 typedef     PairDistances::DistanceType                             DistanceType;
    24 typedef     PairDistances::IndexType                                Vertex;
    25  
    26 typedef     boost::tuple<Dimension, DistanceType>                   BirthInfo;
    27 typedef     CohomologyPersistence<BirthInfo, Wrapper<unsigned> >    Persistence;
    28 typedef     Persistence::SimplexIndex                               Index;
    29 typedef     Persistence::Death                                      Death;
    30 typedef     Persistence::CocyclePtr                                 CocyclePtr;
    31 
    32 typedef     Rips<PairDistances, Simplex<Vertex, Index> >            Generator;
    33 typedef     Generator::Simplex                                      Smplx;
    34 typedef     std::vector<Smplx>                                      SimplexVector;
    35 typedef     SimplexVector::const_iterator                           SV_const_iterator;
    36 
    37 typedef     std::map<Smplx, Index, Smplx::VertexComparison>         Complex;
    38 
    39 #include "output.h"         // for output_*()
    40 
    41 void        program_options(int argc, char* argv[], std::string& infilename, Dimension& skeleton, DistanceType& max_distance, ZpField::Element& prime, std::string& boundary_name, std::string& cocycle_prefix, std::string& vertices_name, std::string& diagram_name);
    42 
    43 int main(int argc, char* argv[])
    44 {
    45 #ifdef LOGGING
    46     rlog::RLogInit(argc, argv);
    47 
    48     stderrLog.subscribeTo( RLOG_CHANNEL("error") );
    49 #endif
    50 
    51     Dimension               skeleton;
    52     DistanceType            max_distance;
    53     ZpField::Element        prime;
    54     std::string             infilename, boundary_name, cocycle_prefix, vertices_name, diagram_name;
    55 
    56     program_options(argc, argv, infilename, skeleton, max_distance, prime, boundary_name, cocycle_prefix, vertices_name, diagram_name);
    57     std::ofstream           bdry_out(boundary_name.c_str());
    58     std::ofstream           vertices_out(vertices_name.c_str());
    59     std::ofstream           diagram_out(diagram_name.c_str());
    60     std::cout << "Boundary matrix: " << boundary_name << std::endl;
    61     std::cout << "Cocycles:        " << cocycle_prefix << "*.ccl" << std::endl;
    62     std::cout << "Vertices:        " << vertices_name << std::endl;
    63     std::cout << "Diagram:         " << diagram_name << std::endl;
    64 
    65     Timer total_timer; total_timer.start();
    66     PointContainer          points;
    67     read_points(infilename, points);
    68 
    69     PairDistances           distances(points);
    70     Generator               rips(distances);
    71     Generator::Evaluator    size(distances);
    72     Generator::Comparison   cmp(distances);
    73     SimplexVector           v;
    74     
    75     Timer rips_timer; rips_timer.start();
    76     rips.generate(skeleton, max_distance, make_push_back_functor(v));
    77     std::sort(v.begin(), v.end(), Smplx::VertexComparison());
    78 
    79     std::vector<unsigned> index_in_v(v.size());
    80     for (unsigned idx = 0; idx < v.size(); ++idx)
    81         index_in_v[idx] = idx;
    82     std::sort(index_in_v.begin(), index_in_v.end(), IndirectIndexComparison<SimplexVector, Generator::Comparison>(v, cmp));
    83 
    84     BinarySearchMap<Smplx, SimplexVector::iterator, Smplx::VertexComparison> map_of_v(v.begin(), v.end());
    85 
    86     rips_timer.stop();
    87     std::cout << "Simplex vector generated, size: " << v.size() << std::endl;
    88 
    89     output_boundary_matrix(bdry_out, v, Smplx::VertexComparison());
    90     output_vertex_indices(vertices_out, v);
    91 
    92     Timer persistence_timer; persistence_timer.start();
    93     ZpField                 zp(prime);
    94     Persistence             p(zp);
    95     boost::progress_display show_progress(v.size());
    96     
    97     #ifdef COUNTERS
    98     Counter::CounterType    max_element_count = 0;
    99     unsigned                max_memory = 0;
   100     long                    max_rss = 0;
   101     long                    max_ixrss = 0;
   102     long                    max_idrss = 0;
   103     long                    max_isrss = 0;
   104 
   105     int                     max_uordblks = 0;
   106     int                     max_fordblks = 0;
   107     #endif
   108 
   109     for (unsigned j = 0; j < index_in_v.size(); ++j)
   110     {
   111         SimplexVector::const_iterator cur = v.begin() + index_in_v[j];
   112         std::vector<Index>      boundary;
   113         for (Smplx::BoundaryIterator bcur  = cur->boundary_begin(); bcur != cur->boundary_end(); ++bcur)
   114             boundary.push_back(map_of_v[*bcur]->data());
   115         
   116         Index idx; Death d; CocyclePtr ccl;
   117         bool store = cur->dimension() < skeleton;
   118         boost::tie(idx, d, ccl)     = p.add(boundary.begin(), boundary.end(), boost::make_tuple(cur->dimension(), size(*cur)), store, index_in_v[j]);
   119         
   120         // c[*cur] = idx;
   121         if (store)
   122             map_of_v[*cur]->data() = idx;
   123 
   124         if (d && (size(*cur) - d->get<1>()) > 0)
   125         {
   126             AssertMsg(d->get<0>() == cur->dimension() - 1, "Dimensions must match");
   127             diagram_out << (cur->dimension() - 1) << " " << d->get<1>() << " " << size(*cur) << std::endl;
   128         }
   129         ++show_progress;
   130         
   131         #ifdef COUNTERS
   132         max_element_count = std::max(max_element_count, cCohomologyElementCount->count);
   133         #endif
   134     }
   135     // output infinte persistence pairs 
   136     for (Persistence::CocycleIndex cur = p.begin(); cur != p.end(); ++cur)
   137         diagram_out << cur->birth.get<0>() << " " << cur->birth.get<1>() << " inf" << std::endl;
   138     persistence_timer.stop();
   139 
   140 
   141     // p.show_cocycles();
   142     // Output alive cocycles of dimension 1
   143     if (!cocycle_prefix.empty())
   144     {
   145         unsigned i = 0;
   146         for (Persistence::Cocycles::const_iterator cur = p.begin(); cur != p.end(); ++cur)
   147         {
   148             if (cur->birth.get<0>() != 1) continue;
   149             output_cocycle(cocycle_prefix, i, v, cur->birth, cur->zcolumn, prime);
   150             // std::cout << "Cocycle of dimension: " << cur->birth.get<0>() << " born at " << cur->birth.get<1>() << std::endl;
   151             ++i;
   152         }
   153     }
   154     total_timer.stop();
   155     rips_timer.check("Rips timer");
   156     persistence_timer.check("Persistence timer");
   157     total_timer.check("Total timer");
   158 
   159     #ifdef COUNTERS
   160     std::cout << "Max element count: " << max_element_count << std::endl;
   161     #endif
   162 }
   163 
   164 void        program_options(int argc, char* argv[], std::string& infilename, Dimension& skeleton, DistanceType& max_distance, ZpField::Element& prime, std::string& boundary_name, std::string& cocycle_prefix, std::string& vertices_name, std::string& diagram_name)
   165 {
   166     namespace po = boost::program_options;
   167 
   168     po::options_description     hidden("Hidden options");
   169     hidden.add_options()
   170         ("input-file",          po::value<std::string>(&infilename),        "Point set whose Rips zigzag we want to compute");
   171     
   172     po::options_description visible("Allowed options", 100);
   173     visible.add_options()
   174         ("help,h",                                                                                  "produce help message")
   175         ("skeleton-dimsnion,s", po::value<Dimension>(&skeleton)->default_value(2),                  "Dimension of the Rips complex we want to compute")
   176         ("prime,p",             po::value<ZpField::Element>(&prime)->default_value(11),             "Prime p for the field F_p")
   177         ("max-distance,m",      po::value<DistanceType>(&max_distance)->default_value(Infinity),    "Maximum value for the Rips complex construction")
   178         ("boundary,b",          po::value<std::string>(&boundary_name),                             "Filename where to output the boundary matrix")
   179         ("cocycle,c",           po::value<std::string>(&cocycle_prefix),                            "Prefix of the filename where to output the 1-dimensional cocycles")
   180         ("vertices,v",          po::value<std::string>(&vertices_name),                             "Filename where to output the simplex-vertex mapping")
   181         ("diagram,d",           po::value<std::string>(&diagram_name),                              "Filename where to output the persistence diagram");
   182 #if LOGGING
   183     std::vector<std::string>    log_channels;
   184     visible.add_options()
   185         ("log,l",               po::value< std::vector<std::string> >(&log_channels),           "log channels to turn on (info, debug, etc)");
   186 #endif
   187 
   188     po::positional_options_description pos;
   189     pos.add("input-file", 1);
   190     
   191     po::options_description all; all.add(visible).add(hidden);
   192 
   193     po::variables_map vm;
   194     po::store(po::command_line_parser(argc, argv).
   195                   options(all).positional(pos).run(), vm);
   196     po::notify(vm);
   197 
   198 #if LOGGING
   199     for (std::vector<std::string>::const_iterator cur = log_channels.begin(); cur != log_channels.end(); ++cur)
   200         stderrLog.subscribeTo( RLOG_CHANNEL(cur->c_str()) );
   201 #endif
   202 
   203     if (vm.count("help") || !vm.count("input-file"))
   204     { 
   205         std::cout << "Usage: " << argv[0] << " [options] input-file" << std::endl;
   206         std::cout << visible << std::endl; 
   207         std::abort();
   208     }
   209 }