Skip to content

Commit dc0b30e

Browse files
committed
Added PrintProgressBar function (utilities.cpp) and use of it in bootstrap resampling mode.
Updated make_export.sh to use updated Vagrant VM directories.
1 parent c8102c3 commit dc0b30e

File tree

6 files changed

+73
-13
lines changed

6 files changed

+73
-13
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ at this point.
2121

2222
### Changed:
2323

24+
- When doing bootstrap resampling, a one-line progress bar (with updating
25+
iteration count and percentage completion) is drawn to stdout; this replaces
26+
the printing of individual iteration numbers in previous versions, which
27+
could end up spilling over many lines of output. (Thanks to Iskren Georgiev
28+
for suggesting this.)
29+
2430
- The SConstruct file has been updated to allow compiling on a Mac using
2531
Apple's Clang compiler, which (apparently since late 2017) finally has
2632
support for OpenMP! This *does*, however, require installing`libomp` (e.g., `brew

core/bootstrap_errors.cpp

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* nonlinfit (imfit's conceptual predecessor), so yay for reuse!
1010
*/
1111

12-
// Copyright 2013-2018 by Peter Erwin.
12+
// Copyright 2013-2019 by Peter Erwin.
1313
//
1414
// This file is part of Imfit.
1515
//
@@ -48,11 +48,13 @@
4848
#include "bootstrap_errors.h"
4949
#include "statistics.h"
5050
#include "print_results.h"
51+
#include "utilities_pub.h"
5152

5253
using namespace std;
5354

5455

5556
const int MIN_ITERATIONS_FOR_STATISTICS = 3;
57+
const int PROGRESS_BAR_WIDTH = 80;
5658

5759

5860
/* ------------------- Function Prototypes ----------------------------- */
@@ -195,13 +197,15 @@ int BootstrapErrorsBase( const double *bestfitParams, vector<mp_par> parameterLi
195197
double **outputParamArray, FILE *outputFile_ptr, unsigned long rngSeed )
196198
{
197199
double *paramsVect, *paramOffsets;
198-
int i, status, nIter, nSuccessfulIters;
200+
double progress;
201+
int i, status, nIter, nDone, nSuccessfulIters;
202+
int progressBarPos;
199203
int nParams = theModel->GetNParams();
200204
int nValidPixels = theModel->GetNValidPixels();
201205
int verboseLevel = -1; // ensure minimizer stays silent
202206
bool saveToFile = false;
203-
string outputLine;
204-
207+
string outputLine, iterTemplate;
208+
205209
if (outputFile_ptr != NULL)
206210
saveToFile = true;
207211

@@ -221,18 +225,21 @@ int BootstrapErrorsBase( const double *bestfitParams, vector<mp_par> parameterLi
221225
}
222226

223227
if ((whichStatistic == FITSTAT_CHISQUARE) || (whichStatistic == FITSTAT_POISSON_MLR))
224-
printf("\nStarting bootstrap iterations (L-M solver): ");
228+
printf("Starting bootstrap iterations (L-M solver):\n");
225229
else
226230
#ifndef NO_NLOPT
227-
printf("\nStarting bootstrap iterations (N-M simplex solver): ");
231+
printf("Starting bootstrap iterations (N-M simplex solver):\n");
228232
#else
229-
printf("\nStarting bootstrap iterations (DE solver): ");
233+
printf("Starting bootstrap iterations (DE solver):\n");
230234
#endif
231235

236+
237+
int nDigits = floor(log10(nIterations)) + 1;
238+
iterTemplate = PrintToString("] %%%dd", nDigits) + " (%3.1f%%)\r";
239+
232240
// Bootstrap iterations:
233241
nSuccessfulIters = 0;
234242
for (nIter = 0; nIter < nIterations; nIter++) {
235-
printf("%d... ", nIter + 1);
236243
fflush(stdout);
237244
theModel->MakeBootstrapSample();
238245
for (i = 0; i < nParams; i++)
@@ -265,7 +272,25 @@ int BootstrapErrorsBase( const double *bestfitParams, vector<mp_par> parameterLi
265272
}
266273
nSuccessfulIters += 1;
267274
}
275+
276+
// print/update progress bar
277+
nDone = nIter + 1;
278+
PrintProgressBar(nDone, nIterations, iterTemplate, PROGRESS_BAR_WIDTH);
279+
// progress = nDone / (1.0*nIterations);
280+
// progressBarPos = PROGRESS_BAR_WIDTH * progress;
281+
// printf("[");
282+
// for (int j = 0; j < PROGRESS_BAR_WIDTH; j++) {
283+
// if (j < progressBarPos)
284+
// printf("=");
285+
// else if (j == progressBarPos)
286+
// printf(">");
287+
// else
288+
// printf(" ");
289+
// }
290+
// printf(iterTemplate.c_str(), nDone, (100.0 * progress));
291+
// fflush(stdout);
268292
}
293+
printf("\n");
269294

270295

271296
free(paramsVect);

core/utilities.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,30 @@ void PrintParametersSimple( ModelObject *model, double *parameters )
174174

175175

176176

177+
/* ---------------- FUNCTION: PrintProgressBar() ------------------- */
178+
/// Prints an updated progress bar.
179+
/// printTemplate should be a string with one "%d" and one "%d" formatting
180+
/// sequences -- e.g., " niter = %3d (%.1f%%)"
181+
/// barWidth = full width of progress bar in characters
182+
void PrintProgressBar( int nDone, int nTotal, string printTemplate, int barWidth )
183+
{
184+
double progress = nDone / (1.0*nTotal);
185+
int progressBarPos = (int)(progress * barWidth);
186+
187+
printf("[");
188+
for (int i = 0; i < barWidth; i++) {
189+
if (i < progressBarPos)
190+
printf("=");
191+
else if (i == progressBarPos)
192+
printf(">");
193+
else
194+
printf(" ");
195+
}
196+
printf(printTemplate.c_str(), nDone, (100.0 * progress));
197+
fflush(stdout);
198+
}
199+
200+
177201

178202
/* ---------------- FUNCTION: SplitString() ------------------------ */
179203
/// This function tokenizes a string, splitting it into substrings using

core/utilities_pub.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ void PrepareImageComments( vector<string> *comments, const string &programName,
5151
/// with names) to stdout
5252
void PrintParametersSimple( ModelObject *model, double *parameters );
5353

54+
/// \brief Utility function to draw/update a progress bar by printing to stdout
55+
void PrintProgressBar( int nDone, int nTotal, string printTemplate, int barWidth );
56+
5457

5558

5659

make_export.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
# Script to bundle up current repo (renamed to imfit-export) and copy it
44
# to Vagrant directory for compiling Linux version
55

6-
linux_vm_dest="/Users/erwin/vagrant/ubuntu-16_test"
6+
linux_vm_dest64="/Users/erwin/vagrant/ubuntu64-16_dev/transfer"
7+
linux_vm_dest32="/Users/erwin/vagrant/ubuntu32-16_dev/transfer"
78

89
cd /Users/erwin/coding
910
rm imfit-export.tar.gz
1011
rm -rf imfit-export
1112
hg clone imfit imfit-export
1213
tar -cf imfit-export.tar imfit-export && gzip imfit-export.tar
13-
cp imfit-export.tar.gz ${linux_vm_dest}
14+
cp imfit-export.tar.gz ${linux_vm_dest1}
15+
cp imfit-export.tar.gz ${linux_vm_dest2}

tests/imfit_reference/imfit_textout5e_tail

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
Now doing bootstrap resampling (5 iterations) to estimate errors...
1+
Starting bootstrap iterations (L-M solver):
2+
[================> ] 1 (20.0%)[================================> ] 2 (40.0%)[================================================> ] 3 (60.0%)[================================================================> ] 4 (80.0%)[================================================================================] 5 (100.0%)
23

3-
Starting bootstrap iterations (L-M solver): 1... 2... 3... 4... 5...
44
Statistics for parameter values from bootstrap resampling (5 successful iterations):
55
Best-fit Bootstrap [68% conf.int., half-width]; (mean +/- standard deviation)
66
X0 = 32.9497 +0.0547039, -0.00239716 [32.9473 -- 33.0044, 0.0285506]; (32.9758 +/- 0.0255166)
@@ -13,6 +13,6 @@ r_e = 58.0709 +3.24553, -1.56195 [56.509 -- 61.3164, 2.40374]; (58.3155 +/-
1313
Bootstrap-resampling output saved to file "temptest/temp_bootstrap_output2.dat".
1414
Saving best-fit parameters in file "bestfit_parameters_imfit.dat"
1515

16-
(Elapsed time: 0.049036 sec for fit, 0.068760 for bootstrap, 0.121389 sec total)
16+
(Elapsed time: 0.133669 sec for fit, 0.198461 for bootstrap, 0.342853 sec total)
1717
Done!
1818

0 commit comments

Comments
 (0)