Persistence 1D inc. Reconstruct1D  1.1
Finding extrema in one dimensional data, filtering them by persistence and reconstructing smooth functions
 All Classes Namespaces Files Functions Variables Macros Pages
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.
64  p.RunPersistence(data);
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 }