forked from richardjgowers/zeoplusplus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tdpldhistogram.cc
402 lines (289 loc) · 9.97 KB
/
tdpldhistogram.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* Time-dependent PLD and Histogram tool
*
* added by M. Haranczyk
* May 2014
*
* It reads a list of .pld files from Zeo++ and perform analysis
*
*
*/
#include <stdlib.h>
#include <cmath>
#include <cstring>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
double bin_size = 0.1;
int NBINS = 201;
int n_segments;
vector<int> DiType; // stores a vector with pore type ID
vector<double> DiTable; // vector storing Di for each segment
vector< vector<double> > PLDTable; // vector storing PLD table (Df/PLD for each segment pair)
vector< vector<double> > TDDiTable; // vector storing frames of Di information (Di for each segment)
vector< vector< vector<double> > > TDPLDTable; // vector storing frames of PLD tables (Df/PLD for each segment pair)
vector< vector<int> > PLDActiveTable; // highlights connectivity between segments to be monitored
/* Calculates histogram based on vector */
vector<int> histogram(vector <double> valuelist){
vector<int> histogram;
histogram.resize(NBINS,0);
for(unsigned int i=0; i<valuelist.size(); i++)
{
histogram[ int(floor(valuelist[i] / bin_size))]++;
};
/* histogram sanity check */
int nhist=0;
for(unsigned int k=0;k<NBINS;k++) nhist=nhist+histogram[k];
if(nhist!=valuelist.size())
{
cerr << "Histogram sanity check failed, exit\n";
abort();
};
return histogram;
}
/* Calculates Mean and Std. Dev. based on distribution */
pair<double,double> normal_distr_parameters(vector <double> valuelist){
pair<double,double> mean_stddev;
mean_stddev.first = 0.0;
for(unsigned int i=0; i<valuelist.size(); i++)
{
mean_stddev.first += valuelist[i];
};
mean_stddev.first = mean_stddev.first / (double) valuelist.size();
mean_stddev.second = 0.0;
for(unsigned int i=0; i<valuelist.size(); i++)
{
mean_stddev.second += ( (valuelist[i] - mean_stddev.first)*(valuelist[i] - mean_stddev.first) );
};
mean_stddev.second = mean_stddev.second /(double) valuelist.size();
mean_stddev.second = sqrt(mean_stddev.second);
return mean_stddev;
}
int main(int argc, char * argv[]){
if(argc != 4){
cout << "This tool requires 2 arguments: filename1 filename2 filename3\n"
<< " filename1 is .pld file that defines topology used in analysis\n"
<< " filename2 is a file with a list of .pld files to be analyzed\n"
<< " filename3 is one specifying types of cages (5th column with type id)"
<< " \n";
// cout << "FORGET ABOUT BINSIZE IN THIS VERIOSN\n"; // @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ override
return 0;
} else
{ // assume correct input parameters have been provided
// proceed with analysis
string summaryfilename, listfilename, poreidfile;
summaryfilename = argv[1];
listfilename = argv[2];
poreidfile = argv[3];
// bin_size = atof(argv[3]);
bin_size = 0.1; // @@@@@@@@@@@@@@@@@2 overridding to match NBINS
cout << "Proceeding with arguments: " << summaryfilename << " " << listfilename << " " << bin_size << "\n";
// First open summaryfilename to setup required parameters
fstream sumfile;
sumfile.open(summaryfilename.c_str());
if(sumfile.is_open()==false){
cerr << "Error: A file with summary/topology (" << summaryfilename << ") failed to open. \n" ;
return 1;
}else
{
string buffer;
sumfile >> n_segments;
getline(sumfile, buffer);
cout << "Expecting " << n_segments << " segments.\n";
DiTable.resize(n_segments, -1);
vector<int> vinttemp;
vinttemp.resize(n_segments, -1);
for(int i=0; i<n_segments; i++)
{
PLDTable.push_back(DiTable);
PLDActiveTable.push_back(vinttemp);
};
/* Read in pore id */
fstream poreidf;
poreidf.open(poreidfile.c_str());
if(poreidf.is_open()==false)
{
cerr << "poreid file error\n";
return 1;
}
else{
int pid;
for(int i=0; i<n_segments; i++)
{
poreidf >> pid;
DiType.push_back(pid);
};
};
getline(sumfile, buffer); // skipping sgment Di line
getline(sumfile, buffer); // skipping divider line
for(int i=0; i<n_segments; i++)
{
for(int j=0;j<n_segments; j++)
{
double d;
sumfile >> d;
if(d>1.0) { PLDActiveTable.at(i).at(j) = 1;} else { PLDActiveTable.at(i).at(j) = 0;};
};
getline(sumfile, buffer); // reading end of line
};
};
/* double check connectivity - pore type connections */
/*
for(int i=0; i<n_segments; i++)
{
for(int j=i+1;j<n_segments; j++)
{
if(PLDActiveTable.at(i).at(j) == 1)
{
if(DiType[i]==DiType[j])
{
cerr << "Segments " << i << " " << j << " of the same type are connected.\nEXIT\n";
return 1;
};
}
};
};
*/
/* loading data */
fstream flist;
flist.open(listfilename.c_str());
if(flist.is_open()==false){
cerr << "Error: A file with frames (" << listfilename << ") failed to open. \n" ;
}else{
// file with a list of frames is open. proceed.
int nfiles=0;
cout << "Loading filenames from " << listfilename << endl;
while(!flist.eof())
{
string inputfilename;
flist >> inputfilename;
if(flist.eof()){
// nfiles--;
break;
};
nfiles++;
fstream input;
input.open(inputfilename.c_str());
if(input.is_open()==false){
cerr << "Error: .pld failed to open " << inputfilename << endl;
}else{
// reading frame in .pld format
string buffer;
int ns;
input >> ns;
if(ns !=n_segments)
{
cerr << "Number of segments in this frame is wrong\n";
return 1;
};
getline(input, buffer);
input >> buffer; // skipping a string
for(int i=0; i<n_segments; i++)
{
input >> DiTable[i];
};
TDDiTable.push_back(DiTable);
getline(input, buffer); // skipping sgment Di line
getline(input, buffer); // skipping divider line
for(int i=0; i<n_segments; i++)
{
for(int j=0;j<n_segments; j++)
{
input >> PLDTable.at(i).at(j);
};
getline(input, buffer); // reading end of line
};
TDPLDTable.push_back(PLDTable);
input.close();
}; // ends input.is_open() for reading-in the current frame
}; // ends while loop over a file list
cout << nfiles << " frames loaded.\n";
// ANALYSIS BLOCK COMES HERE
vector <double> di1values;
// vector <double> di2values;
vector <double> dfvalues;
vector <double> dfnonconvalues; // stores unusual connections
vector <double> trackdi1, trackdi2, trackdf, trackdfall;
for(unsigned int k=0; k<TDDiTable.size(); k++)
{
for(int i=0; i<n_segments; i++)
{
// cout << "k and i= " << k << " " << i << "\n";
//tracking
// if(DiType[i]==204) di1values.push_back(TDDiTable[k].at(i));
// if(DiType[i]==168) di2values.push_back(TDDiTable[k].at(i));
di1values.push_back(TDDiTable[k].at(i));
for(int j=i+1; j<n_segments; j++)
{
if(PLDActiveTable.at(i).at(j)==1)
{
dfvalues.push_back(TDPLDTable[k].at(i).at(j));
// if(i==0&&j==8) trackdf.push_back(TDPLDTable[k].at(i).at(j));
// if(i==0) trackdfall.push_back(TDPLDTable[k].at(i).at(j));
} else
{
if(TDPLDTable[k].at(i).at(j)>0)
{
// cerr << "Unusual segment connection detected " << i << " " << j << " with diameter " << TDPLDTable[k].at(i).at(j) << "\n";
dfnonconvalues.push_back(TDPLDTable[k].at(i).at(j));
};
};
};
};
};
cout << "Sizes of vectors: divalue, dfvalues and dfnonconvalues " << di1values.size()
<< " " << dfvalues.size() << " " << dfnonconvalues.size() << "\n";
vector<int> di1hist = histogram(di1values);
// vector<int> di2hist = histogram(di2values);
vector<int> dfhist = histogram(dfvalues);
vector<int> dfnonconhist = histogram(dfnonconvalues);
for(unsigned int i=0;i<di1hist.size();i++)
{
if(i==0)
{
di1hist[i]=di1values.size();
// di2hist[i]=di2values.size();
dfhist[i]=dfvalues.size();
dfnonconhist[i]=dfnonconvalues.size();
} else
{
di1hist[0]-=di1hist[i];
// di2hist[0]-=di2hist[i];
dfhist[0]-=dfhist[i];
dfnonconhist[0]-=dfnonconhist[i];
};
};
cout << "\n\nDi1 and Dfi histogram, and normalized Di1, Df and Dfnoncon histogram\n";
for(unsigned int i=0;i<di1hist.size();i++)
{
if(i<81)
cout << i*bin_size << " " << di1hist[i] << " "<< dfhist[i] << " ";
cout << double(di1hist[i])/double(di1values.size()) << " "
<< double(dfhist[i])/double(dfvalues.size()) << " "
<< double(dfnonconhist[i])/double(dfvalues.size()) << "\n";
};
cout << "\n\n Gaussian fitting\n";
pair<double,double> DiG = normal_distr_parameters(di1values);
pair<double,double> DfG = normal_distr_parameters(dfvalues);
cout << "Di: mean = " << DiG.first << " std.dev.= " << DiG.second << "\n";
cout << "Df: mean = " << DfG.first << " std.dev.= " << DfG.second << "\n";
/*
cout << "\n\n\nTD data\nFrame Di1 Di2 Df_D1-D2 Df1 Df2 Df3 Df4 (for cage D1)\n\n";
for(unsigned int i=0; i<trackdi1.size(); i++)
{
cout << i << " "<< trackdi1[i] << " " << trackdi2[i] << " " <<trackdf[i] << " " <<trackdfall[4*i] << " "<<trackdfall[4*i+1] << " "<<trackdfall[4*i+2] << " "<<trackdfall[4*i+3] << "\n ";
};
cout << "\n\nHistogram for Di1 and Df_D1-D2\n\n";
dihist = histogram(trackdi1);
dfhist = histogram(trackdf);
for(unsigned int i=0;i<dihist.size();i++)
{
if(i<81)
cout << i*bin_size << " " << dihist[i] << " " << dfhist[i];
cout << " " << double(dihist[i])/double(trackdi1.size()) << " " << double(dfhist[i])/double(trackdf.size()) << "\n";
};
*/
}; // ends block corresponding to successful opening of file with frame list
}; // ends if(argc!=4
}