| 1 |
Custom progress reporting |
| 2 |
========================= |
| 3 |
|
| 4 |
.. _custom_progress_reporting: |
| 5 |
|
| 6 |
Progress reporting |
| 7 |
------------------ |
| 8 |
For custom progress reporting (e.g. graphical output, writing to a file, etc.), |
| 9 |
the ``report`` keyword accepts a callable (i.e. a function or an object with a |
| 10 |
``__call__`` method) that will be called with four parameters: |
| 11 |
|
| 12 |
* ``elapsed``: the total (real) time since the start of the run |
| 13 |
* ``completed``: the fraction of the total simulation that is completed, |
| 14 |
i.e. a value between 0 and 1 |
| 15 |
* ``start``: The start of the simulation (in biological time) |
| 16 |
* ``duration``: the total duration (in biological time) of the simulation |
| 17 |
|
| 18 |
The function will be called every ``report_period`` during the simulation, but |
| 19 |
also at the beginning and end with ``completed`` equal to 0.0 and 1.0, |
| 20 |
respectively. |
| 21 |
|
| 22 |
For the C++ standalone mode, the same standard options are available. It is |
| 23 |
also possible to implement custom progress reporting by directly passing the |
| 24 |
code (as a multi-line string) to the ``report`` argument. This code will be |
| 25 |
filled into a progress report function template, it should therefore only |
| 26 |
contain a function body. The simplest use of this might look like:: |
| 27 |
|
| 28 |
net.run(duration, report='std::cout << (int)(completed*100.) << "% completed" << std::endl;') |
| 29 |
|
| 30 |
Examples of custom reporting |
| 31 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 32 |
**Progress printed to a file** |
| 33 |
:: |
| 34 |
|
| 35 |
from brian2.core.network import TextReport |
| 36 |
report_file = open('report.txt', 'w') |
| 37 |
file_reporter = TextReport(report_file) |
| 38 |
net.run(duration, report=file_reporter) |
| 39 |
report_file.close() |
| 40 |
|
| 41 |
**"Graphical" output on the console** |
| 42 |
|
| 43 |
This needs a "normal" Linux console, i.e. it might not work in an integrated |
| 44 |
console in an IDE. |
| 45 |
|
| 46 |
Adapted from http://stackoverflow.com/questions/3160699/python-progress-bar |
| 47 |
|
| 48 |
:: |
| 49 |
|
| 50 |
import sys |
| 51 |
|
| 52 |
class ProgressBar(object): |
| 53 |
def __init__(self, toolbar_width=40): |
| 54 |
self.toolbar_width = toolbar_width |
| 55 |
self.ticks = 0 |
| 56 |
|
| 57 |
def __call__(self, elapsed, complete, start, duration): |
| 58 |
if complete == 0.0: |
| 59 |
# setup toolbar |
| 60 |
sys.stdout.write("[%s]" % (" " * self.toolbar_width)) |
| 61 |
sys.stdout.flush() |
| 62 |
sys.stdout.write("\b" * (self.toolbar_width + 1)) # return to start of line, after '[' |
| 63 |
else: |
| 64 |
ticks_needed = int(round(complete * self.toolbar_width)) |
| 65 |
if self.ticks < ticks_needed: |
| 66 |
sys.stdout.write("-" * (ticks_needed-self.ticks)) |
| 67 |
sys.stdout.flush() |
| 68 |
self.ticks = ticks_needed |
| 69 |
if complete == 1.0: |
| 70 |
sys.stdout.write("\n") |
| 71 |
|
| 72 |
net.run(duration, report=ProgressBar(), report_period=1*second) |
| 73 |
|
| 74 |
**"Standalone Mode" Text based progress bar on console** |
| 75 |
|
| 76 |
This needs a "normal" Linux console, i.e. it might not work in an integrated |
| 77 |
console in an IDE. |
| 78 |
|
| 79 |
Adapted from https://stackoverflow.com/questions/14539867/how-to-display-a-progress-indicator-in-pure-c-c-cout-printf |
| 80 |
|
| 81 |
:: |
| 82 |
|
| 83 |
set_device('cpp_standalone') |
| 84 |
|
| 85 |
report_func = ''' |
| 86 |
int remaining = (int)((1-completed)/completed*elapsed+0.5); |
| 87 |
if (completed == 0.0) |
| 88 |
{ |
| 89 |
std::cout << "Starting simulation at t=" << start << " s for duration " << duration << " s"<<std::flush; |
| 90 |
} |
| 91 |
else |
| 92 |
{ |
| 93 |
int barWidth = 70; |
| 94 |
std::cout << "\\r["; |
| 95 |
int pos = barWidth * completed; |
| 96 |
for (int i = 0; i < barWidth; ++i) { |
| 97 |
if (i < pos) std::cout << "="; |
| 98 |
else if (i == pos) std::cout << ">"; |
| 99 |
else std::cout << " "; |
| 100 |
} |
| 101 |
std::cout << "] " << int(completed * 100.0) << "% completed. | "<<int(remaining) <<"s remaining"<<std::flush; |
| 102 |
} |
| 103 |
''' |
| 104 |
run(100*second, report=report_func) |