Persistence 1D incl. Reconstruct1D  1.3
Finding extrema in one dimensional data, filtering them by persistence and reconstructing smooth functions
MatplotlibVisualization.cpp
Go to the documentation of this file.
1 /*! \file MatplotlibVisualization.cpp
2  Demonstrates how to run the C++ code and visualize the results using Matplotlib.
3 */
4 /*! \example MatplotlibVisualization.cpp
5 
6  Demonstrates how to run the C++ code and visualize the results using Matplotlib.
7 
8  # Contents
9  This example demonstrates how to:
10  -# Read data from a file,
11  -# Use Persistence1D class to extract extrema
12  -# Filter results according to persistence values.
13  -# Visualize results using Matplotlib scripts.
14 
15  # Results Visualization
16  -# Run visualize.py
17 
18  ## Matplotlib Plot of Results
19  \image html MatplotlibVisRes.png "Matplotlib Plot of Results"
20  \image latex MatplotlibVisRes.png "Matplotlib Plot of Results"
21 
22  # Code Documentation
23 */
24 
25 #include "..\..\persistence1d\persistence1d.hpp"
26 #include <fstream>
27 
28 using namespace std;
29 using namespace p1d;
30 
31 int main()
32 {
33  //Input and class variables declaration
34  Persistence1D p;
35  vector<float> data;
36  float currdata;
37  ifstream datafile;
38  ofstream outfile;
39  char * filename = "data.txt";
40  char * outfilename = "res.txt";
41 
42  //Output variables declaration
43  vector<int> min, max;
44  int globalMinIndex;
45 
46  //Open and read the data file
47  datafile.open(filename);
48  if (!datafile)
49  {
50  cout << "Cannot open data file for reading: " << filename << endl;
51  return -1;
52  }
53 
54  while(datafile >> currdata)
55  {
56  data.push_back(currdata);
57  }
58 
59  datafile.close();
60 
61  //To start data processing, run persistence on data
63 
64  //It is possible to verify the correctness of the results via sanity tests.
65  //Check the function documentation for further information.
66  if (!p.VerifyResults())
67  {
68  cout << "ERROR" << endl;
69  }
70 
71  //Now, set a threshold for features filtering
72  float filterThreshold = 1.0;
73 
74  //Retrieve the filtered results from the class.
75  p.GetExtremaIndices(min, max, filterThreshold);
76  globalMinIndex = p.GetGlobalMinimumIndex();
77 
78  //Print all found indices to a file
79  outfile.open(outfilename);
80  if (!outfile)
81  return -2;
82 
83  for (unsigned int i = 0; i < min.size() && i < max.size(); i++)
84  {
85  outfile << min[i] << endl;
86  outfile << max[i] << endl;
87  }
88 
89  //Add the global minimum to the file as well:
90  //The global minimum does is not returned via GetExtremaIndices, as it is not paired.
91  outfile << globalMinIndex << endl;
92 
93  outfile.close();
94 
95  //To visualize the results:
96  //Run visualize.py via Python, which can be found in the same folder as this file
97 
98  return 0;
99 }
int GetGlobalMinimumIndex(const bool matlabIndexing=false) const
Returns the index of the global minimum.
bool GetExtremaIndices(std::vector< int > &min, std::vector< int > &max, const float threshold=0, const bool matlabIndexing=false) const
Use this method to get two vectors with all indices of PairedExterma.
bool RunPersistence(const std::vector< float > &InputData)
Call this function with a vector of one dimensional data to find extrema features in the data.
Finds extrema and their persistence in one-dimensional data.
bool VerifyResults()
Runs basic sanity checks on results of RunPersistence: