examples/pl-functions/pl-vineyard.cpp
author Dmitriy Morozov <dmitriy@mrzv.org>
Sun Dec 20 10:43:00 2009 -0800 (2 years ago)
branchdev
changeset 183 0ca59b0ebc47
parent 180examples/grid/pl-vineyard.cpp@27508309a680
child 1992bde4c56101c
permissions -rw-r--r--
Moved files around: lsvineyard.h -> include/topology, examples/grid -> examples/pl-functions
     1 #include <utilities/log.h>
     2 
     3 #include <iostream>
     4 #include <fstream>
     5 #include <string>
     6 #include <vector>
     7 
     8 #include <topology/lsvineyard.h>
     9 
    10 #include <boost/program_options.hpp>
    11 #include <boost/iterator/counting_iterator.hpp>
    12 #include <boost/function.hpp>
    13 #include <boost/bind.hpp>
    14 #include <boost/lambda/lambda.hpp>
    15 namespace bl = boost::lambda;
    16 
    17 
    18 typedef     float                                       VertexValue;
    19 typedef     unsigned                                    Vertex;
    20 typedef     std::vector<VertexValue>                    VertexVector;
    21 struct SubscriptFunctor: public std::unary_function<Vertex, VertexValue>
    22 {
    23                                 SubscriptFunctor(const VertexVector& v): vec(&v)    {}
    24         float                   operator()(Vertex i) const                          { return (*vec)[i]; }
    25         SubscriptFunctor&       operator=(const SubscriptFunctor& other)            { vec = other.vec; return *this; }
    26         const VertexVector*     vec;
    27 };
    28 typedef     SubscriptFunctor                            VertexEvaluator;
    29 typedef     std::vector<VertexVector>                   VertexVectorVector;
    30 typedef     LSVineyard<Vertex, VertexEvaluator>         PLVineyard;
    31 typedef     PLVineyard::Simplex                         Smplx;              // gotta start using namespaces
    32 
    33 void        program_options(int argc, char* argv[], std::string& complex_fn, 
    34                                                     std::string& values_fn, 
    35                                                     std::string& output_prefix, 
    36                                                     bool& skip_infinite_vines, 
    37                                                     bool& save_vines,
    38                                                     bool& explicit_events);
    39 void        read_simplices(const std::string& complex_fn, PLVineyard::LSFiltration& simplices);
    40 void        read_vertices(const std::string& vertex_fn, VertexVectorVector& vertices); 
    41 
    42 
    43 int main(int argc, char** argv)
    44 {
    45 #ifdef LOGGING
    46     rlog::RLogInit(argc, argv);
    47 #endif     
    48     
    49     std::string                 complex_fn, values_fn, output_prefix;
    50     bool                        skip_infinite_vines = false, explicit_events = false, save_vines = false;
    51     program_options(argc, argv, complex_fn, values_fn, output_prefix, skip_infinite_vines, save_vines, explicit_events);
    52 
    53 
    54     // Read in the complex
    55     PLVineyard::LSFiltration            simplices;
    56     read_simplices(complex_fn, simplices);
    57     std::cout << "Complex read, size: " << simplices.size() << std::endl;
    58 
    59     // Read in vertex values
    60     VertexVectorVector                  vertices;
    61     read_vertices(values_fn, vertices);
    62 
    63     // Setup the vineyard
    64     VertexEvaluator                     veval(vertices[0]);
    65     PLVineyard::VertexComparison        vcmp(veval);
    66     PLVineyard::SimplexComparison       scmp(vcmp);
    67     simplices.sort(scmp);
    68     PLVineyard                      v(boost::counting_iterator<Vertex>(0),
    69                                       boost::counting_iterator<Vertex>(vertices[0].size()), 
    70                                       simplices, veval);
    71     std::cout << "Pairing computed" << std::endl;
    72 
    73     // Compute vineyard
    74     for (size_t i = 1; i < vertices.size(); ++i)
    75     {
    76         veval = VertexEvaluator(vertices[i]);
    77         v.compute_vineyard(veval, explicit_events);
    78         std::cout << "Processed frame: " << i << std::endl;
    79     }
    80     std::cout << "Vineyard computed" << std::endl;
    81     
    82     if (save_vines)
    83         v.vineyard().save_vines(output_prefix, skip_infinite_vines);
    84     else
    85         v.vineyard().save_edges(output_prefix, skip_infinite_vines);
    86 }
    87 
    88 
    89 void        read_simplices(const std::string& complex_fn, PLVineyard::LSFiltration& simplices)
    90 {
    91     std::ifstream   in(complex_fn.c_str());
    92     std::string     line;
    93     while (std::getline(in, line))
    94     {
    95         std::istringstream  strin(line);
    96         simplices.push_back(Smplx(std::istream_iterator<Vertex>(strin), std::istream_iterator<Vertex>()));
    97     }
    98     std::cout << "Simplices read:" << std::endl;
    99     std::copy(simplices.begin(), simplices.end(), std::ostream_iterator<Smplx>(std::cout, "\n"));
   100 }
   101 
   102 void        read_vertices(const std::string& vertex_fn, VertexVectorVector& vertices)
   103 {
   104     std::ifstream   in(vertex_fn.c_str());
   105     std::string     line;
   106     while (std::getline(in, line))
   107     {
   108         std::istringstream  strin(line);
   109         vertices.push_back(VertexVector(std::istream_iterator<VertexValue>(strin), std::istream_iterator<VertexValue>()));
   110     }
   111     std::cout << "Vertex values read:" << std::endl;
   112     for (size_t i = 0; i < vertices.size(); ++i)
   113     {
   114         std::copy(vertices[i].begin(), vertices[i].end(), std::ostream_iterator<VertexValue>(std::cout, " "));
   115         std::cout << std::endl;
   116     }
   117 }
   118 
   119 void        program_options(int argc, char* argv[], std::string& complex_fn, 
   120                                                     std::string& values_fn, 
   121                                                     std::string& output_prefix, 
   122                                                     bool& skip_infinite_vines,
   123                                                     bool& save_vines,
   124                                                     bool& explicit_events)
   125 {
   126     namespace po = boost::program_options;
   127 
   128     po::options_description     hidden("Hidden options");
   129     hidden.add_options()
   130         ("complex-file",        po::value<std::string>(&complex_fn),            "file listing the simplices of the complex")
   131         ("values-file",         po::value<std::string>(&values_fn),             "file listing the values at the vertices")
   132         ("output-prefix",       po::value<std::string>(&output_prefix),         "output prefix");
   133     
   134     po::options_description visible("Allowed options", 100);
   135     visible.add_options()
   136         ("skip-infinite,s",     po::bool_switch(&skip_infinite_vines),                          "skip infinite vines in the output")
   137         ("explicit-events,e",   po::bool_switch(&explicit_events),                              "process kinetic sort events one by one")
   138         ("save-vines,v",        po::bool_switch(&save_vines),                                   "save vines instead of edges")
   139         ("help,h",                                                                              "produce help message");
   140 #if LOGGING
   141     std::vector<std::string>    log_channels;
   142     visible.add_options()
   143         ("log,l",               po::value< std::vector<std::string> >(&log_channels),           "log channels to turn on (info, debug, etc)");
   144 #endif
   145 
   146     po::positional_options_description pos;
   147     pos.add("complex-file", 1);
   148     pos.add("values-file", 1);
   149     pos.add("output-prefix", 1);
   150     
   151     po::options_description all; all.add(visible).add(hidden);
   152 
   153     po::variables_map vm;
   154     po::store(po::command_line_parser(argc, argv).
   155                   options(all).positional(pos).run(), vm);
   156     po::notify(vm);
   157 
   158 #if LOGGING
   159     for (std::vector<std::string>::const_iterator cur = log_channels.begin(); cur != log_channels.end(); ++cur)
   160         stderrLog.subscribeTo( RLOG_CHANNEL(cur->c_str()) );
   161 #endif
   162 
   163     if (vm.count("help") || !vm.count("complex-file") || !vm.count("values-file") || !vm.count("output-prefix"))
   164     { 
   165         std::cout << "Usage: " << argv[0] << " [options] complex-file values-file output-prefix" << std::endl;
   166         std::cout << visible << std::endl; 
   167         std::abort();
   168     }
   169 }