Persistence 1D incl. Reconstruct1D  1.3
Finding extrema in one dimensional data, filtering them by persistence and reconstructing smooth functions
FilesAndFilters.cpp
Go to the documentation of this file.
1 /*! \file FilesAndFilters.cpp
2  Example of working with data in files and using filters.
3 */
4 /*! \example FilesAndFilters.cpp
5 
6  Example of working with data in files and using filters.
7 
8  # Contents
9  This example demonstrates:
10  -# Reading data into a vector from a text file
11  -# Using Persistence1D class to extract features
12  -# Filtering features according to their persistence.
13  -# Automatic reset of output variables - reused output variables are reset.
14 
15  # Expected Output
16  Output reference file: [persistence_base_dir]\\src\\examples\\FilesAndFilters\\FilesAndFiltersRes.ref
17  with contents:
18 
19  \include FilesAndFiltersRes.ref
20 
21  # Code Documentation
22 */
23 
24 #include "..\..\persistence1d\persistence1d.hpp"
25 
26 #include <fstream>
27 
28 using namespace std;
29 using namespace p1d;
30 
31 int main()
32 {
33  //Declare class and input variables
34  Persistence1D p;
35  vector<float> data;
36  float currdata;
37  ifstream datafile;
38  char * filename = "data.txt";
39 
40  //Declare variables for results
41  vector<TPairedExtrema> pairs;
42  vector<int> min, max;
43  int globalMinIndex;
44  float globalMinValue;
45 
46 
47  //Open the data file for reading
48  datafile.open(filename);
49  if (!datafile)
50  {
51  cout << "Cannot open data file for reading: " << filename << endl;
52  return -1;
53  }
54 
55  //Read the values from data file
56  while(datafile >> currdata)
57  {
58  data.push_back(currdata);
59  }
60 
61  datafile.close();
62 
63  //Find extrema and compute their persistence.
65 
66  //Use the Get functions to get a copy of the results.
67  //You can use this function multiple times with multiple threshold values without re-running the
68  //main algorithm.
69  p.GetExtremaIndices(min, max);
70  p.GetPairedExtrema(pairs);
71  globalMinValue = p.GetGlobalMinimumValue();
72  globalMinIndex = p.GetGlobalMinimumIndex();
73 
74  //To print the results - including all pairs and the global minimum, call PrintResults.
75  p.PrintResults();
76  cout << endl;
77 
78  //Now set a threshold for features filtering
79  //In this case, we choose a threshold between largest and smallest persistence, to select from of the data.
80  float filterThreshold = (pairs.front().Persistence + pairs.back().Persistence)/2;
81 
82  //The vector which hold the data for the Get functions are being reset before use.
83  //Use them to retrieve the filtered results:
84  p.GetExtremaIndices(min, max, filterThreshold);
85  p.GetPairedExtrema(pairs, filterThreshold);
86 
87  cout << "Filtered results with persistence > " << filterThreshold << endl;
88 
89  //To print the results, it is also possible to print the contents of the local pairs structure
90  p.PrintPairs(pairs);
91 
92  //In this case, the global minimum needs to be printed manually
93  cout << "Global minimum value: " << globalMinValue
94  << " index: " << globalMinIndex << endl << endl;
95 
96  //It is also possible to call PrintResuls with a threshold value to achieve identical results:
97  cout << "PrintResults(filterThreshold) results:" << endl;
98  p.PrintResults(filterThreshold);
99  cout << endl;
100 
101  //Now set a threshold greater than the largest persistence, such that no paired extrema are returned:
102  filterThreshold = pairs.back().Persistence + 1;
103  p.GetExtremaIndices(min, max, filterThreshold);
104  p.GetPairedExtrema(pairs, filterThreshold);
105 
106  cout << "Filtered results with persistence > " << filterThreshold << endl;
107  cout << "Number of found paired extrema: " << pairs.size() << endl;
108 
109  //Trying to print the results using the local pairs structure via PrintPairs will not print anything
110  cout << "PrintPairs(pairs) start" << endl;
111  p.PrintPairs(pairs);
112  cout << "PrintPairs(pairs) end" << endl << endl;
113 
114  //But calling the class's PrintPairs will print the global minimum even when no
115  //pairs pass the persistence threshold.
116  p.PrintResults(filterThreshold);
117 
118  return 0;
119 }
int main()
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.
void PrintPairs(const std::vector< TPairedExtrema > &pairs) const
Prints the contents of the TPairedExtrema vector.
Finds extrema and their persistence in one-dimensional data.
bool GetPairedExtrema(std::vector< TPairedExtrema > &pairs, const float threshold=0, const bool matlabIndexing=false) const
Use this method to get the results of RunPersistence.
void PrintResults(const float threshold=0.0, const bool matlabIndexing=false) const
Prints the global minimum and all paired extrema whose persistence is greater or equal to threshold.
float GetGlobalMinimumValue() const
Returns the value of the global minimum.