File: progress.c

package info (click to toggle)
minc 2.1.10-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 8,160 kB
  • sloc: ansic: 82,507; sh: 10,666; yacc: 1,187; perl: 612; makefile: 586; lex: 319
file content (306 lines) | stat: -rw-r--r-- 10,463 bytes parent folder | download | duplicates (4)
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
/* ----------------------------------------------------------------------------
@COPYRIGHT  :
              Copyright 1993,1994,1995 David MacDonald,
              McConnell Brain Imaging Centre,
              Montreal Neurological Institute, McGill University.
              Permission to use, copy, modify, and distribute this
              software and its documentation for any purpose and without
              fee is hereby granted, provided that the above copyright
              notice appear in all copies.  The author and McGill University
              make no representations about the suitability of this
              software for any purpose.  It is provided "as is" without
              express or implied warranty.
---------------------------------------------------------------------------- */

#include  <internal_volume_io.h>

#define  FIRST_MESSAGE_THRESHOLD   5.0

#define  ONE_LINE_THRESHOLD        160.0

#define  LINE_LENGTH               77

#define  MIN_UPDATE_RATE           20.0    /* seconds */
#define  UPDATE_RATE_FACTOR        0.05

#define  RATIO_FOR_LINEAR          0.5

#define  DOUBLE_THRESHOLD          0.01
#define  HALF_THRESHOLD            0.5

static  void  show_one_line_progress(
    progress_struct    *progress,
    int                current_step );

static  void  show_multi_line_progress(
    progress_struct    *progress,
    int                current_step,
    Real               time_so_far,
    Real               est_total_time );

/* ----------------------------- MNI Header -----------------------------------
@NAME       : initialize_progress_report
@INPUT      : one_line_only        - whether line of dots is desired
            : n_steps
            : title
@OUTPUT     : progress             - structure is filled in
@RETURNS    : 
@DESCRIPTION: Initializes the progress report, which is either a line of dots
            : crossing the screen, or if the progress is too slow, a line
            : every 20 seconds or so indicating the amount of time left.
            : If one_line_only is true, then it is always a single line of dots.
            : n_steps is the total number of items or times through the loop.
            : If it is really fast, no messages at all are displayed.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    :                      David MacDonald
@MODIFIED   : 
---------------------------------------------------------------------------- */

VIOAPI  void  initialize_progress_report(
    progress_struct   *progress,
    BOOLEAN           one_line_only,
    int               n_steps,
    STRING            title )
{
    progress->force_one_line = one_line_only;
    progress->first_msg_displayed = FALSE;
    progress->one_line_flag = TRUE;
    progress->n_steps = n_steps;
    progress->title = create_string( title );
    progress->start_time = current_realtime_seconds();
    progress->previous_time = progress->start_time;
    progress->last_check_time = progress->start_time;
    progress->last_check_step = 0;
    progress->next_check_step = 1;
    progress->check_every = 1;
    progress->update_rate = MIN_UPDATE_RATE;
    progress->sum_xy = 0.0;
    progress->sum_xx = 0.0;
    progress->n_dots_so_far = 0;
    progress->total_n_dots = LINE_LENGTH - string_length( progress->title );

    if( progress->total_n_dots < 1 )
        progress->total_n_dots = 2;
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : update_progress_report
@INPUT      : progress
            : current_step   (an integer between 1 and n_steps)
@OUTPUT     : 
@RETURNS    : 
@DESCRIPTION: Checks the current time and determines if it is time to output
            : a progress message.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    :                 David MacDonald
@MODIFIED   : Sep.  1, 1995   D. MacDonald - changed update rate to be relative
                                             to time so far
---------------------------------------------------------------------------- */

VIOAPI  void  update_progress_report(
    progress_struct   *progress,
    int               current_step )
{
    Real    current_time, constant, n_seconds_per;
    Real    time_so_far, est_total_time;

    if( current_step < 1 || current_step < progress->next_check_step )
        return;

    if( current_step > progress->n_steps )
        current_step = progress->n_steps;

    current_time = current_realtime_seconds();

    n_seconds_per = (Real) progress->check_every *
                    (current_time - progress->last_check_time) /
                    (Real) (current_step - progress->last_check_step);

    if( n_seconds_per < DOUBLE_THRESHOLD )
        progress->check_every *= 2;
    else if( n_seconds_per > HALF_THRESHOLD && progress->check_every > 1 )
        progress->check_every /= 2;

    progress->last_check_time = current_time;
    progress->last_check_step = current_step;
    progress->next_check_step = current_step + progress->check_every;
    if( progress->next_check_step > progress->n_steps )
        progress->next_check_step = progress->n_steps;

    time_so_far = current_time - progress->start_time;

    progress->sum_xy = RATIO_FOR_LINEAR * progress->sum_xy +
                        (Real) current_step * time_so_far;
    progress->sum_xx = RATIO_FOR_LINEAR * progress->sum_xx +
                        (Real) current_step * (Real) current_step;

    if( time_so_far > FIRST_MESSAGE_THRESHOLD )
    {
        constant = progress->sum_xy / progress->sum_xx;
        est_total_time = (Real) progress->n_steps * constant;

        if( est_total_time <= time_so_far )
        {
            est_total_time = time_so_far * (Real) progress->n_steps /
                             (Real) current_step;
        }

        if( progress->force_one_line ||
            (progress->one_line_flag && est_total_time < ONE_LINE_THRESHOLD) )
        {
            show_one_line_progress( progress, current_step );
            progress->first_msg_displayed = TRUE;
        }
        else
        {
            if( progress->first_msg_displayed && progress->one_line_flag )
                print( "\n" );

            progress->one_line_flag = FALSE;

            if( current_time - progress->previous_time >= progress->update_rate)
            {
                show_multi_line_progress( progress, current_step, time_so_far,
                                          est_total_time );
                progress->first_msg_displayed = TRUE;
                progress->previous_time = current_time;

                progress->update_rate = (current_time - progress->start_time) *
                                        UPDATE_RATE_FACTOR;
                if( progress->update_rate < MIN_UPDATE_RATE )
                    progress->update_rate = MIN_UPDATE_RATE;
            }
        }
    }
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : show_one_line_progress
@INPUT      : progress
            : current_step
@OUTPUT     : 
@RETURNS    : 
@DESCRIPTION: Given the current_step, and the total number, ensures that the
            : number of dots on the line is representative.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    :                      David MacDonald
@MODIFIED   : 
---------------------------------------------------------------------------- */

static  void  show_one_line_progress(
    progress_struct    *progress,
    int                current_step )
{
    int     i, n_dots;

    n_dots = ROUND( (Real) current_step / (Real) progress->n_steps *
                    (Real) progress->total_n_dots );

    if( n_dots > progress->total_n_dots )
        handle_internal_error( "show_one_line_progress" );

    if( n_dots > progress->n_dots_so_far )
    {
        if( progress->n_dots_so_far == 0 )
        {
            print( "%s: ", progress->title );
        }

        for_less( i, progress->n_dots_so_far, n_dots )
        {
            print( "." );
        }

        (void) flush_file( stdout );

        progress->n_dots_so_far = n_dots;
    }
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : show_multi_line_progress
@INPUT      : progress
            : current_step
            : time_so_far
            : est_total_time
@OUTPUT     : 
@RETURNS    : 
@DESCRIPTION: Displays report about time so far, estimated time left, etc.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    :                      David MacDonald
@MODIFIED   : 
---------------------------------------------------------------------------- */

static  void  show_multi_line_progress(
    progress_struct    *progress,
    int                current_step,
    Real               time_so_far,
    Real               est_total_time )
{
    int     percent_done;
    STRING  time_so_far_str, est_total_time_str;

    percent_done = ROUND( 100.0 * (Real) current_step /
                          (Real) progress->n_steps );

    time_so_far_str = format_time( "%g %s", time_so_far );
    est_total_time_str = format_time( "%g %s", est_total_time );

    print( "%s: %3d%% done. (%d/%d)   Time: %s out of approx %s\n",
           progress->title, percent_done, current_step, progress->n_steps,
           time_so_far_str, est_total_time_str );

    delete_string( time_so_far_str );
    delete_string( est_total_time_str );

    (void) flush_file( stdout );
}

/* ----------------------------- MNI Header -----------------------------------
@NAME       : terminate_progress_report
@INPUT      : progress
@OUTPUT     : 
@RETURNS    : 
@DESCRIPTION: Terminates the progress report.
@METHOD     : 
@GLOBALS    : 
@CALLS      : 
@CREATED    :                      David MacDonald
@MODIFIED   : 
---------------------------------------------------------------------------- */

VIOAPI  void  terminate_progress_report(
    progress_struct   *progress )
{
    Real    total_time;
    STRING  time_str;

    if( progress->first_msg_displayed )
    {
        if( progress->one_line_flag )
        {
            show_one_line_progress( progress, progress->n_steps );
            print( "\n" );
        }
        else
        {
            total_time = current_realtime_seconds() - progress->start_time;

            time_str = format_time( "%g %s", total_time );

            print( "%s: DONE in %s\n", progress->title, time_str );

            delete_string( time_str );
        }
    }

    delete_string( progress->title );
}