libatomprobe
Library for Atom Probe Tomography (APT) computation
errorEstimate.cpp
Go to the documentation of this file.
1 #include "atomprobe/atomprobe.h"
2 
3 using namespace std;
4 using namespace AtomProbe;
5 
6 
7 //This uses poisson ratio error to determine error bars on a line profile from IVAS
8 int main(int argc, char *argv[])
9 {
10  if(argc !=2)
11  {
12  cerr << "Usage: " << argv[0] << " CSV_FILE" << endl;
13  return 1;
14  }
15 
16 
17  //Load CSV Data
18  vector<string> headings;
19  vector<vector<float > > intensityData;
20 
21  auto errCode=loadTextData(argv[1],intensityData,headings,",\t");
22  if(errCode)
23  {
24  cerr << "Error loading text file :" << errCode <<endl;
25  return 1;
26  }
27 
28  if(!intensityData.size())
29  {
30  cerr << "input data appears to be empty" << endl;
31  return 1;
32  }
33 
34  cerr << "Loaded :" << intensityData.size() << "Rows";
35 
36 
37  vector<vector<float > > counts;
38 
39  counts.resize(intensityData.size());
40  for(auto &v : counts)
41  {
42  v.resize(intensityData[0].size());
43  }
44 
45  unsigned int distanceCol=-1;
46  unsigned int countCol=-1;
47  vector<unsigned int> compCols;
48 
49 
50  for(auto ui=0;ui<headings.size();ui++)
51  {
52  if(headings[ui].find("Distance") != std::string::npos)
53  {
54  distanceCol=ui;
55  continue;
56  }
57 
58  if(headings[ui].find("Atom Count") != std::string::npos)
59  {
60  countCol=ui;
61  continue;
62  }
63 
64  //Check for a column we care about
65  if(headings[ui].find("%") != std::string::npos &&
66  headings[ui].find("Sigma") == std::string::npos)
67  {
68  cerr << "Using :" << headings[ui] << endl;
69  compCols.push_back(ui);
70  }
71  }
72 
73  if(distanceCol == -1 || countCol == -1)
74  {
75  cerr << "Unable to locate distance or atom count columns. Aborting" << endl;
76  return 1;
77  }
78 
79 
80  //Perform selection from large table to make subtable
81 
82  vector<string> perColumnHeading;
83  vector<vector<float> > perColumnCounts;
84  for(auto v : compCols)
85  {
86  perColumnHeading.push_back(headings[v]);
87  perColumnCounts.push_back(intensityData[v]);
88  }
89 
90 
91  vector<float> distances;
92  string distHeader = headings[distanceCol];
93  for(auto v : intensityData[distanceCol])
94  distances.push_back(v);
95 
96  const float ALPHA=0.99;
97  const unsigned int NTRIALS=1000;
98 
99  //Now extract the counts across the row, and feed into error routine
100  vector<vector<pair<float,float> > > errBounds;
101 
102  vector<vector<float> > perRowCounts=perColumnCounts;
103  transposeVector(perRowCounts);
104 
105  for(auto vec: perRowCounts)
106  {
107  //Denominator
108  float dCounts=0;
109  for(auto v : vec)
110  {
111  if(!isnan(v))
112  dCounts+=v;
113  }
114 
115 
116  vector<pair<float,float> > errs;
117  if(!dCounts)
118  {
119  errs.resize(vec.size(),make_pair(-1,-1));
120  errBounds.push_back(errs);
121  errs.clear();
122  continue;
123  }
124 
125  float lBound,uBound;
126  for(auto ui=0;ui<vec.size();ui++)
127  {
128  lBound=-1;uBound=-1;
129  if(!isnan(vec[ui]))
130  {
131  if(!numericalEstimatePoissRatioConf(vec[ui],dCounts,
132  ALPHA,NTRIALS,randGen.getRng(),lBound,uBound))
133  {
134  //could not determine confidence
135  lBound=0;uBound=1;
136  }
137 
138  //Cap lBound,uBound
139  if(uBound > 1)
140  uBound=1;
141 
142  }
143  errs.push_back(make_pair(lBound,uBound));
144  }
145  cerr <<" Error bound size:" << errs.size() << endl;
146  errBounds.push_back(errs);
147  errs.clear();
148  }
149 
150  //Dump error estimates
151  cout <<distHeader << "\t" ;
152  for(auto s : perColumnHeading)
153  cout << s << "(low)\t" << s << "(hi)" << "\t";
154  cout << endl;
155 
156  for(auto ui=0u;ui<errBounds.size();ui++)
157  {
158  cout << distances[ui] << "\t";
159  for(auto e : errBounds[ui])
160  {
161  cout << e.first << "\t" << e.second << "\t";
162  }
163  cout << endl;
164  }
165 
166 }
STL namespace.
void transposeVector(std::vector< std::vector< T > > &v)
Perform an out-of-place tranposition of a given vector.
Definition: misc.h:81
unsigned int loadTextData(const char *cpFilename, std::vector< std::vector< float > > &dataVec, std::vector< std::string > &headerVec, const char *delim="\", bool allowNan=true, bool allowConvFails=false, unsigned int headerCount=-1)
Load a CSV, TSV or similar text file. Assumes "C" Locale for input (ie "." as decimal separator)...
Definition: dataFiles.cpp:1504
bool numericalEstimatePoissRatioConf(float lambda1, float lambda2, float alpha, unsigned int nTrials, gsl_rng *r, float &lBound, float &uBound)
Brute-force poisson ratio confidence estimator. Returns the confidence interval in the estimate of th...
Definition: confidence.cpp:68
int main(int argc, char *argv[])
gsl_rng * getRng() const
Obtain a GSL random number generator.
Definition: atomprobe.h:115
RandNumGen randGen
Definition: atomprobe.cpp:29