-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInformation.cpp
More file actions
712 lines (622 loc) · 14.9 KB
/
Information.cpp
File metadata and controls
712 lines (622 loc) · 14.9 KB
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
#include "Information.h"
/**
24/10/2011
- Edited COM to move pixels if they are greater than 50% of a step from desired search area - dealt with
problems assoicated with moving pixels into random areas of search area.
- Edited read txt to no longer be reliant on the pointer to a random number seed
**/
using namespace std;
void Information::init(int *s, vector<string> *o)
// Constructor - clears all variables ready for use
// Takes seed as input variable
{
this->reset();
// Initiate Random Numbers
idum = s;
output_content = o;
}
void Information::reset()
// Used to reset all data to 0.
// Called when initialising data sets, takes no inputs
{
this->clearCounters();
this->emptyVectors();
}
void Information::clearCounters()
// Used to reset all Counters, not vectors
{
x_total = 0.0;
y_total = 0.0;
z_total = 0.0;
max_x = 0;
max_y = 0;
max_z = 0;
min_x = LARGE;
min_y = LARGE;
min_z = LARGE;
count = 0;
m = 0;
n = 0;
step = 0;
}
bool Information::readFile(const string &name)
// Reads in new data
// Input(name) - filename
// Outputs - boolean of success or failure
{
CompareV c;
c.filename1 = c.filename2 = "";
// c.lsf_file = c.covariance_file = "";
c.scale_intensity = false;
c.print_to_screen = false;
c.lsf = false;
c.covariance = false;
c.save_results = false;
c.centre_image = false;
c.translate = false;
c.save_lsf_difference = false;
c.translate_x = 0;
c.translate_y = 0;
return readFile(name,c);
}
bool Information::readFile(const string &file, const CompareV &coV)
// Reads in new data
// Input(name) - filename 1 or 2
// (custom) - list of all used boolean operators
// Outputs - boolean of success or failure
{
reset();
compare_variables = coV;
setFilename(file);
string sub = filename.substr(filename.size()-4);
if (cmpStr(sub,"zcon"))
{
return readZcon(filename);
}
else if (cmpStr(sub,".txt"))
{
return readTxt(filename);
}
else
{
return false;
}
}
bool Information::saveImage(string sz)
// Saves data currently held in zcon format
// Input(sz) - filename
// Outputs - boolean of success or failure
{
ofstream fsFile;
fsFile.open(sz.c_str(), ios::out);
int position;
if(!fsFile.is_open())
{
#pragma omp critical
{
output_content->push_back("Error Writing output file: " + sz);
}
return EXIT_FAILURE;
}
else
{
for(int y = 0; y < getN(); y++)
{
for(int x = 0; x < getM(); x++)
{
position = (y*n) + x;
fsFile << x * getStep() << "\t" << y * getStep() << "\t" << dataArray[position] << endl;
}
fsFile << endl; //we require this for pm3d plot to work in gnuplot
}
fsFile.close();
return EXIT_SUCCESS;
}
}
bool Information::saveImage(const string sz, vector<float> newArray, const int x, const int y, const float grid)
// Saves data passed to class as Zcon
// Input string(sz) - filename
// vector<float>(newArray) - Data to be saved
// int(x) - width
// int(y) - height
// float(grid) - step sizes
// Outputs - boolean success or failure
{
this->reset();
dataArray = newArray;
this->setM(x);
this->setN(y);
this->setStep(grid);
return saveImage(sz);
}
bool Information::saveImageMatrix(string sz)
// Saves data passed to class as Txt
// Input string(sz) - filename
// Outputs - boolean success or failure
{
ofstream fsFile;
fsFile.open(sz.c_str(), ios::out);
int position;
if(!fsFile.is_open())
{
#pragma omp critical
{
output_content->push_back("Error Writing output file: " + sz);
}
return EXIT_FAILURE;
}
else
{
for(signed int y = (int) getN(); y > -1; y--)
{
for(int x = 0; x < getM(); x++)
{
position = (y*n) + x;
// cout << position << " " << dataArray.size() << endl;
fsFile << dataArray[position] << " ";
}
fsFile << std::endl; //we require this for pm3d plot to work in gnuplot
}
fsFile.close();
return EXIT_SUCCESS;
}
}
bool Information::saveImageMatrix(const string &sz, vector<float> &newArray, const int &x, const int &y, const float &grid)
// Saves data passed to class as Txt matrix
// Input string(sz) - filename
// vector<float>(newArray) - Data to be saved
// int(x) - width
// int(y) - height
// float(grid) - step sizes
// Outputs - boolean success or failure
{
this -> reset();
dataArray = newArray;
this->setM(x);
this->setN(y);
this->setStep(grid);
return saveImageMatrix(sz);
}
bool Information::readTxt(const string &name)
// Read in text file. Perhaps we should change this to you use the tokenizer at some point
// Inputs: string(name) - filename
// Outputs - boolean success or failure
{
ifstream inData;
inData.open( name.c_str() );
string buffer;
if ( !inData )
{
return EXIT_FAILURE;
}
else
{
int lines = 0;
while ( getline(inData,buffer) )
{
if (buffer.size() != 0)
{
lines++;
}
}
inData.clear();
inData.seekg(ios::beg);
lines--; // We seem to over read by one line
unsigned int i, j, x, y, string_size;
string ts;
point tPB,oldPB;
float dx, odx, dy, ody;
dx = odx = dy = ody = 0;
//oldPB.x = randomNumber(101,idum);
//oldPB.y = randomNumber(101,idum);
//oldPB.z = randomNumber(101,idum);
oldPB.x = 77777;
oldPB.y = 88888;
oldPB.z = 99999;
y = lines;
while ( getline(inData,buffer) )
{
j = 0;
i = 0;
x = 0;
while( i < buffer.size())
{
if(buffer.size() != 0);
{
j=i;
find_character_end(buffer,j);
string_size = j-i;
ts = buffer.substr(i,string_size);
i=j;
find_white_space_end(buffer,j);
i=j;
tPB.x = x;
tPB.y = y;
StringToNumber(ts,tPB.z);
data_points.push_back(tPB);
//inputAnalysis(tPB);
odx = dx;
dx = (x - oldPB.x);
if ((odx == dx) && (ody == dy))
{
step = dx;
}
oldPB = tPB;
x++;
}
}
ody = dy;
dy = (y - oldPB.y);
y--;
}
}
inData.close();
inputAnalysis();
if (compare_variables.translate)
{
if (compare_variables.centre_image)
{
calcCOM(); // Calculate Centre of Mass
}
else
{
translate(compare_variables.translate_x,compare_variables.translate_y);
}
}
calculateDataArray();
// Save Centred Image if need be
if (compare_variables.translate && compare_variables.save_results)
{
if (compare_variables.centre_image)
{
saveImage("COM_" + name.substr(0,name.length()-4) + ".zcon");
}
else
{
saveImage("TRANS_" + name.substr(0,name.length()-4) + ".zcon");
}
}
return EXIT_SUCCESS;
}
bool Information::readZcon(const string &name)
// Read in zcon file. Perhaps we should change this to you use the tokenizer at some point
// Inputs: string(name) - filename
// Outputs - boolean success or failure
{
ifstream inData;
inData.open( name.c_str() );
if ( !inData )
{
return EXIT_FAILURE;
}
else
{
point tPB, oldPB;
tPB.x = oldPB.x = randomNumber(101,idum);
tPB.y = oldPB.y = randomNumber(101,idum);
tPB.z = oldPB.z = randomNumber(101,idum);
float dx, odx, dy, ody;
dx = odx = dy = ody = 0;
while (!inData.eof())
{
inData >> tPB.x;
inData >> tPB.y;
inData >> tPB.z;
// cout << dataArray.size() << " ";
// cout << tPB.x << " " << tPB.y << " " << tPB.z << endl;
if (tPB.x == oldPB.x && tPB.y == oldPB.y && tPB.z == oldPB.z)
{
// Skip through
}
else
{
data_points.push_back(tPB);
//inputAnalysis(tPB);
//cout << data_points.size() << " ";
//cout << tPB.x << " " << tPB.y << " " << tPB.z << endl;
odx = dx;
dx = (tPB.x - oldPB.x);
ody = dy;
dy = (tPB.y - oldPB.y);
if ((odx == dx) && (ody == dy))
step = dx;
oldPB = tPB;
}
}
}
inData.close();
inputAnalysis();
if (compare_variables.translate)
{
if (compare_variables.centre_image)
{
calcCOM(); // Calculate Centre of Mass
}
else
{
translate(compare_variables.translate_x,compare_variables.translate_y);
}
}
calculateDataArray();
// Save Centred Image if need be
if (compare_variables.translate && compare_variables.save_results)
{
if (compare_variables.centre_image)
{
saveImage("COM_" + name);
}
else
{
saveImage("TRANS_" + name);
}
}
return EXIT_SUCCESS;
}
void Information::inputAnalysis()
// After being passed a new dataArray this is used to work out totals, maximums, minimums, etc.
// No inputs or outputs as all data is held internally
{
float st_save = step;
int n_save = n;
clearCounters();
step = st_save;
n = n_save;
com_x.clear();
com_y.clear();
point tPB;
tPB.x = tPB.y = tPB.z = 0;
if (dataArray.size() > 0)
{
for (unsigned int i = 0; i < dataArray.size(); i++)
{
tPB.z = dataArray[i];
tPB.x = (i%n)*step;
tPB.y = (i/n)*step;
/////////////////
checkLimits(tPB);
}
}
else if (data_points.size() > 0)
{
for (unsigned int i = 0; i < data_points.size(); i++)
{
tPB = data_points[i];
// cout << i << " " << tPB.x << " " << tPB.y << " " << tPB.z << endl;
/////////////////
checkLimits(tPB);
}
}
//calcM();
// Calculates total width
// Split up due to previous calculation errors
m = (int) floor(((max_x-min_x+step)/step)+0.5);
//calcN();
// Calculates total height
// Split up due to previous calculation errors
n = (int) floor(((max_y-min_y+step)/step)+0.5);
// cout << m << " " << n << " " << m/step << " " << n/step << endl;
// cout << max_x << " " << min_x << " " << max_y << " " << min_y << endl;
}
void Information::checkLimits(point tPB)
// Compare tPB to current limits
// Inputs(tPB) - point to be analysed
{
// cout << tPB.x << " " << tPB.y << " " << tPB.z << endl;
if (tPB.x > max_x)
{
max_x = tPB.x;
}
if (tPB.y > max_y)
{
max_y = tPB.y;
}
if (tPB.z > max_z)
{
max_z = tPB.z;
}
if (tPB.x < min_x)
{
min_x = tPB.x;
}
if (tPB.y < min_y)
{
min_y = tPB.y;
}
if (tPB.z < min_z)
{
min_z = tPB.z;
}
count++;
x_total+=tPB.x;
y_total+=tPB.y;
z_total+=tPB.z;
// This is for calculation of centre of mass of input //
if (compare_variables.centre_image)
{
bool bCom_x = false;
for (unsigned int i = 0; i < com_x.size(); i++)
{
if (com_x[i].x == tPB.x)
{
com_x[i].z+=tPB.z;
bCom_x = true;
}
}
if (!bCom_x)
{
com_x.push_back(tPB);
}
bool bCom_y = false;
for (unsigned int i = 0; i < com_y.size(); i++)
{
if (com_y[i].y == tPB.y)
{
com_y[i].z+=tPB.z;
bCom_y = true;
}
}
if (!bCom_y)
{
com_y.push_back(tPB);
}
}
///////////////////////////////////////////////////////
}
void Information::calcCOM()
// Calculates centre of mass, and shifts image if required
// Added 07/09/2010
{
float x, y, mass, total;
x = y = mass = total = 0;
for (unsigned int i = 0; i < com_x.size(); i++)
{
mass += com_x[i].z;
// Currently we have the value by pixels - commented out here gives the real value
total += (((com_x[i].x-min_x)/step)*com_x[i].z);
// total += (com_x[i].x*com_x[i].z);
}
// COM for X
x = round(total/mass);
mass = total = 0;
for (unsigned int i = 0; i < com_y.size(); i++)
{
mass += com_y[i].z;
// Currently we have the value by pixels - commented out here gives the real value
total += (((com_y[i].y-min_y)/step)*com_y[i].z);
// total += (com_y[i].y*com_y[i].z);
}
// COM for Y
y = round(total/mass);
// Calculate Centre of Grids
float middle_x, middle_y;
middle_x = round((max_x - min_x) / (2*step));
middle_y = round((max_y - min_y) / (2*step));
// Calculate Difference between COM and Centre of Grid
float diff_x, diff_y;
diff_x = middle_x - x;
diff_y = middle_y - y;
string xS;
string yS;
string middle_xS;
string middle_yS;
NumberToString(x,xS);
NumberToString(y,yS);
NumberToString(middle_x,middle_xS);
NumberToString(middle_y,middle_yS);
#pragma omp critical
{
// Shift points around!
// Points beyond original remit are moved to 0 values
output_content->push_back("");
output_content->push_back("!!!!!!!!!!!!!!!!!! CENTRING IMAGE !!!!!!!!!!!!!!!!!!");
output_content->push_back("Image COM X = " + xS + ", Y = " + yS + " Grid Centre X = " + middle_xS + ", Y = " + middle_yS);
output_content->push_back("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
translate(diff_x,diff_y);
}
void Information::translate(const float diff_x, const float diff_y)
// Translate data across array to new position
// Hopefully we'll be able to use this again
// Inputs - diff_x = shift in x axis
// - diff_y = shift in y axis
{
string diff_xS;
string diff_yS;
NumberToString(diff_x,diff_xS);
NumberToString(diff_y,diff_yS);
#pragma omp critical
{
output_content->push_back("!!!!!!!!!!!!!!!!!!!! NOTIFICATION !!!!!!!!!!!!!!!!!!!!");
output_content->push_back("Shifting Image: X = +" + diff_xS + ", Y = +" + diff_yS);
output_content->push_back("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
//dataArray.clear(); // Have to remove all these points to start the array fresh
if ((diff_x != 0) || (diff_y != 0))
{
// cout << max_x << " " << min_x << " " << max_y << " " << min_y << endl;
for (unsigned int i = 0; i < data_points.size(); i++)
{
data_points[i].x += diff_x;
data_points[i].y += diff_y;
// If beyond boundaries move to other side and set value to zero
if (data_points[i].x > (max_x+(step/2)))
{
data_points[i].x -= (m*step);
data_points[i].z = 0;
}
else if (data_points[i].x < (min_x-(step/2)))
{
data_points[i].x += (m*step);
data_points[i].z = 0;
}
if (data_points[i].y > (max_y+(step/2)))
{
data_points[i].y -= (n*step);
data_points[i].z = 0;
}
else if (data_points[i].y < (min_y-(step/2)))
{
data_points[i].y += (n*step);
data_points[i].z = 0;
}
}
}
// Analyse new data
inputAnalysis();
//calculateDataArray();
}
void Information::calculateDataArray()
// This will transfer the data from point format, i.e. just read, to an array, for quicker access
// Takes no inputs or outputs, just internal variables
{
//vector<point> backup_data_points = data_points;
float position;
point tempPB;
// Initiate incase of misuse
position = tempPB.x = tempPB.y = tempPB.z = 0;
//
unsigned int i, j, total_size;
i = j = 0;
total_size = data_points.size();
while (i < total_size)
{
for (j = 0; j < data_points.size(); j++)
{
tempPB = data_points[j];
position = ((((tempPB.y-min_y)/step)*n) + ((tempPB.x-min_x)/step));
if (fabs(position-i) < ERRORBAR )
{
break;
}
}
dataArray.push_back(tempPB.z);
data_points.erase(data_points.begin()+j);
i++;
}
// Restore data points in case we need them in the future
//data_points = backup_data_points;
}
void Information::scale(float scalar)
// Scale totals by amount requested. Best bet is max_z_first/max_z_second
// Inputs: float(scalar) - Value by which to multiply all intensities
{
float newValue, oldValue;
z_total = 0;
max_z = 0;
min_z = pow((float)rand(),2);
for (int i = 0; i < count; i++)
{
oldValue = dataArray[i];
newValue = oldValue*scalar;
dataArray[i] = newValue;
z_total+=newValue;
if (newValue > max_z)
{
max_z = newValue;
}
if (newValue < min_z)
{
min_z = newValue;
}
}
}