File: base.py

package info (click to toggle)
brian 2.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,872 kB
  • sloc: python: 51,820; cpp: 2,033; makefile: 108; sh: 72
file content (726 lines) | stat: -rw-r--r-- 24,482 bytes parent folder | download | duplicates (2)
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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
import itertools
import os
import pickle
import re
import shutil
import subprocess
import sys
import tempfile
from collections import defaultdict

import numpy

import brian2
from brian2.utils.stringtools import indent

__all__ = [
    "FeatureTest",
    "SpeedTest",
    "InaccuracyError",
    "Configuration",
    "run_feature_tests",
    "run_single_feature_test",
    "run_speed_tests",
    "DefaultConfiguration",
    "LocalConfiguration",
    "NumpyConfiguration",
    "CythonConfiguration",
    "CPPStandaloneConfiguration",
    "CPPStandaloneConfigurationOpenMP",
]


class InaccuracyError(AssertionError):
    def __init__(self, error, *args):
        self.error = error
        AssertionError.__init__(self, *args)


class BaseTest:
    """ """

    category = None  # a string with the category of features
    name = None  # a string with the particular feature name within the category
    tags = None  # a list of tags (strings) of features used
    # whether or not to allow the device to override the time: this can be used to remove the
    # compilation overheads on certain devices (but some tests might want to include this)
    allow_time_override = True

    @classmethod
    def fullname(cls):
        return f"{cls.category}: {cls.name}"

    def run(self):
        """
        Runs the feature test but do not return results (some devices may
        require an extra step before results are available).
        """
        raise NotImplementedError

    def timed_run(self, duration):
        """
        Do a timed run. This means that for RuntimeDevice it will run for defaultclock.dt before running for the
        rest of the duration. This means total run duration will be duration+defaultclock.dt.
        For standalone devices, this feature may or may not be implemented.
        """
        if isinstance(brian2.get_device(), brian2.devices.RuntimeDevice):
            brian2.run(brian2.defaultclock.dt, level=1)
            brian2.run(duration, level=1)
        else:
            brian2.run(duration, level=1)


class FeatureTest(BaseTest):
    """ """

    def results(self):
        """
        Return the results after a run call.
        """
        raise NotImplementedError

    def compare(self, maxrelerr, results_base, results_test):
        """
        Compare results from standard Brian run to another run.

        This method or `check` should be implemented.
        """
        raise NotImplementedError

    def check(self, maxrelerr, results):
        """
        Check results are valid (e.g. analytically).

        This method or `compare` should be implemented.
        """
        raise NotImplementedError

    def compare_arrays(self, maxrelerr, v_base, v_test):
        """
        Often you just want to compare the values of some arrays, this does that.
        """
        if isinstance(v_base, dict):
            for k in v_base:
                self.compare_arrays(maxrelerr, v_base[k], v_test[k])
        else:
            I = v_base != 0
            err = numpy.amax(numpy.abs(v_base[I] - v_test[I]) / v_base[I])
            if err > maxrelerr:
                raise InaccuracyError(err)
            if (v_test[-I] != 0).any():
                raise InaccuracyError(numpy.inf)


class SpeedTest(BaseTest):
    n_range = [1]
    n_label = "n"
    n_axis_log = True
    time_axis_log = True

    def __init__(self, n):
        self.n = n

    def results(self):
        return self.n

    def compare(self, maxrelerr, results_base, results_test):
        pass

    def check(self, maxrelerr, results):
        pass

    def __call__(self):
        return self


class Configuration:
    """ """

    name = None  # The name of this configuration

    def __init__(self, maximum_run_time=1e7 * brian2.second):
        maximum_run_time = float(maximum_run_time) * brian2.second
        self.maximum_run_time = maximum_run_time

    def before_run(self):
        pass

    def after_run(self):
        pass

    def get_last_run_time(self):
        """
        Implement this to overwrite the measured runtime (e.g. to remove overhead).
        """
        if hasattr(brian2.device, "_last_run_time"):
            return brian2.device._last_run_time
        raise NotImplementedError

    def get_last_run_completed_fraction(self):
        """
        Implement this to overwrite the amount of the last run that was completed (for devices that allow breaking
        early if the maximum run time is exceeded).
        """
        if hasattr(brian2.device, "_last_run_completed_fraction"):
            return brian2.device._last_run_completed_fraction
        return 1.0


class DefaultConfiguration(Configuration):
    name = "Default"

    def before_run(self):
        brian2.prefs.reset_to_defaults()
        brian2.set_device("runtime")


class LocalConfiguration(Configuration):
    name = "Local"

    def before_run(self):
        brian2.prefs.reset_to_defaults()
        brian2.set_device("runtime")
        brian2.prefs.load_preferences()


class NumpyConfiguration(Configuration):
    name = "Numpy"

    def before_run(self):
        brian2.prefs.reset_to_defaults()
        brian2.set_device("runtime")
        brian2.prefs.codegen.target = "numpy"


class CythonConfiguration(Configuration):
    name = "Cython"

    def before_run(self):
        brian2.prefs.reset_to_defaults()
        brian2.set_device("runtime")
        brian2.prefs.codegen.target = "cython"


class CPPStandaloneConfiguration(Configuration):
    name = "C++ standalone"

    def before_run(self):
        brian2.prefs.reset_to_defaults()
        brian2.set_device("cpp_standalone", build_on_run=False)

    def after_run(self):
        if os.path.exists("cpp_standalone"):
            shutil.rmtree("cpp_standalone")
        brian2.device.build(
            directory="cpp_standalone", compile=True, run=True, with_output=False
        )


class CPPStandaloneConfigurationOpenMP(Configuration):
    name = "C++ standalone (OpenMP)"

    def before_run(self):
        brian2.prefs.reset_to_defaults()
        brian2.set_device("cpp_standalone", build_on_run=False)
        brian2.prefs.devices.cpp_standalone.openmp_threads = 4

    def after_run(self):
        if os.path.exists("cpp_standalone"):
            shutil.rmtree("cpp_standalone")
        brian2.device.build(
            directory="cpp_standalone", compile=True, run=True, with_output=False
        )


def results(configuration, feature, n=None, maximum_run_time=1e7 * brian2.second):
    tempfilename = tempfile.mktemp("exception")
    if n is None:
        init_args = ""
    else:
        init_args = str(n)
    code_string = """
__file__ = '{fname}'
import brian2
from {config_module} import {config_name}
from {feature_module} import {feature_name}
configuration = {config_name}()
feature = {feature_name}({init_args})
import warnings, traceback, pickle, sys, os, time
warnings.simplefilter('ignore')
try:
    start_time = time.time()
    configuration.before_run()
    brian2.device._set_maximum_run_time({maximum_run_time})
    feature.run()
    configuration.after_run()
    results = feature.results()
    run_time = time.time()-start_time
    if feature.allow_time_override:
        try:
            run_time = configuration.get_last_run_time()
        except NotImplementedError:
            pass
    lrcf = configuration.get_last_run_completed_fraction()
    run_time = run_time/lrcf
    prof_info = brian2.magic_network.profiling_info
    new_prof_info = []
    for n, t in prof_info:
        new_prof_info.append((n, t/lrcf))
    f = open(r'{tempfname}', 'wb')
    pickle.dump((None, results, run_time, new_prof_info), f, -1)
    f.close()
except Exception, ex:
    #traceback.print_exc(file=sys.stdout)
    tb = traceback.format_exc()
    f = open(r'{tempfname}', 'wb')
    pickle.dump((tb, ex, 0.0, []), f, -1)
    f.close()
    """.format(
        config_module=configuration.__module__,
        config_name=configuration.__name__,
        feature_module=feature.__module__,
        feature_name=feature.__name__,
        tempfname=tempfilename,
        fname=__file__,
        init_args=init_args,
        maximum_run_time=float(maximum_run_time),
    )
    args = [sys.executable, "-c", code_string]
    # Run the example in a new process and make sure that stdout gets
    # redirected into the capture plugin
    p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = p.communicate()
    # sys.stdout.write(stdout)
    # sys.stderr.write(stderr)
    with open(tempfilename, "rb") as f:
        tb, res, runtime, profiling_info = pickle.load(f)
    return tb, res, runtime, profiling_info


def check_or_compare(feature, res, baseline, maxrelerr):
    feature = feature()
    try:
        feature.check(maxrelerr, res)
    except NotImplementedError:
        feature.compare(maxrelerr, baseline, res)


def run_single_feature_test(configuration, feature):
    return results(configuration, feature)


def run_feature_tests(
    configurations=None,
    feature_tests=None,
    strict=1e-5,
    tolerant=0.05,
    verbose=True,
    maximum_run_time=1e7 * brian2.second,
):
    if configurations is None:
        # some configurations to attempt to import
        try:
            import brian2genn.correctness_testing
        except:
            pass
        configurations = Configuration.__subclasses__()
    if feature_tests is None:
        feature_tests = FeatureTest.__subclasses__()
    if DefaultConfiguration in configurations:
        configurations.remove(DefaultConfiguration)
    configurations = [DefaultConfiguration] + configurations
    feature_tests.sort(key=lambda ft: ft.fullname())
    if verbose:
        print("Running feature tests")
        print("Configurations:", ", ".join(c.name for c in configurations))

    full_results = {}
    tag_results = defaultdict(lambda: defaultdict(list))
    for ft in feature_tests:
        baseline = None
        if verbose:
            print(f"{ft.fullname()}: [", end=" ")
        for configuration in configurations:
            txt = "OK"
            sym = "."
            exc = None
            tb, res, runtime, prof_info = results(
                configuration, ft, maximum_run_time=maximum_run_time
            )
            if isinstance(res, Exception):
                if isinstance(res, NotImplementedError):
                    sym = "N"
                    txt = "Not implemented"
                else:
                    sym = "E"
                    txt = "Error"
                if configuration is DefaultConfiguration:
                    raise res
            else:
                if configuration is DefaultConfiguration:
                    baseline = res
                try:
                    check_or_compare(ft, res, baseline, strict)
                except InaccuracyError as exc:
                    try:
                        check_or_compare(ft, res, baseline, tolerant)
                        sym = "I"
                        txt = f"Poor (error={100.0 * exc.error:.2f}%)"
                    except InaccuracyError as exc:
                        sym = "F"
                        txt = f"Fail (error={100.0 * exc.error:.2f}%)"
            sys.stdout.write(sym)
            full_results[configuration.name, ft.fullname()] = (
                sym,
                txt,
                exc,
                tb,
                runtime,
                prof_info,
            )
            for tag in ft.tags:
                tag_results[tag][configuration.name].append(
                    (sym, txt, exc, tb, runtime, prof_info)
                )
        if verbose:
            print("]")

    return FeatureTestResults(full_results, tag_results, configurations, feature_tests)


class FeatureTestResults:
    def __init__(self, full_results, tag_results, configurations, feature_tests):
        self.full_results = full_results
        self.tag_results = tag_results
        self.configurations = configurations
        self.feature_tests = feature_tests

    @property
    def test_table(self):
        table = []
        table.append(["Test"] + [c.name for c in self.configurations])
        curcat = ""

        for ft in self.feature_tests:
            cat = ft.category
            if cat != curcat:
                table.append([cat] + [""] * len(self.configurations))
                curcat = cat
            row = [ft.name]
            for configuration in self.configurations:
                sym, txt, exc, tb, runtime, prof_info = self.full_results[
                    configuration.name, ft.fullname()
                ]
                row.append(txt)
            table.append(row)
        return make_table(table)

    @property
    def tag_table(self):
        table = []
        table.append(["Tag"] + [c.name for c in self.configurations])
        tags = sorted(self.tag_results.keys())

        for tag in tags:
            row = [tag]
            for configuration in self.configurations:
                tag_res = self.tag_results[tag][configuration.name]
                syms = [sym for sym, txt, exc, tb, runtime, prof_info in tag_res]
                n = len(syms)
                okcount = sum(sym == "." for sym in syms)
                poorcount = sum(sym == "I" for sym in syms)
                failcount = sum(sym == "F" for sym in syms)
                errcount = sum(sym == "E" for sym in syms)
                nicount = sum(sym == "N" for sym in syms)
                if okcount == n:
                    txt = "OK"
                elif nicount == n:
                    txt = "Not implemented"
                elif errcount == n:
                    txt = "Unsupported"
                elif okcount + poorcount == n:
                    txt = f"Poor ({int(int(poorcount * 100.0 / n))}%)"
                elif okcount + poorcount + failcount == n:
                    txt = (
                        f"Fail: {int(failcount * 100.0 / n)}%"
                        f" (poor={int(poorcount * 100.0 / n)}%)"
                    )
                else:
                    txt = (
                        "Fail: OK={ok}%, Poor={poor}%, Fail={fail}%, NotImpl={ni}%"
                        " Error={err}%".format(
                            ok=int(okcount * 100.0 / n),
                            poor=int(poorcount * 100.0 / n),
                            fail=int(failcount * 100.0 / n),
                            err=int(errcount * 100.0 / n),
                            ni=int(nicount * 100.0 / n),
                        )
                    )
                row.append(txt)
            table.append(row)
        return make_table(table)

    @property
    def tables(self):
        r = ""
        s = "Feature test results"
        r += f"{s}\n{'-' * len(s)}\n\n{self.test_table}\n"
        s = "Tag results"
        r += f"{s}\n{'-' * len(s)}\n\n{self.tag_table}\n"
        return r

    @property
    def exceptions(self):
        exc_list = []
        for configuration in self.configurations:
            curconfig = []
            for ft in self.feature_tests:
                sym, txt, exc, tb, runtime, prof_info = self.full_results[
                    configuration.name, ft.fullname()
                ]
                if tb is not None:
                    curconfig.append((ft.fullname(), tb))
            if len(curconfig):
                exc_list.append((configuration.name, curconfig))
        if len(exc_list) == 0:
            return ""
        r = ""
        s = "Exceptions"
        r += f"{s}\n{'-' * len(s)}\n\n"
        for config_name, curconfig in exc_list:
            s = config_name
            r += f"{s}\n{'^' * len(s)}\n\n"
            for name, tb in curconfig:
                r += f"{name}::\n\n{indent(tb)}\n\n"
        return r

    @property
    def tables_and_exceptions(self):
        return f"{self.tables}\n{self.exceptions}"

    def __str__(self):
        return self.tables

    __repr__ = __str__


def run_speed_tests(
    configurations=None,
    speed_tests=None,
    run_twice=True,
    verbose=True,
    n_slice=slice(None),
    maximum_run_time=1e7 * brian2.second,
):
    if configurations is None:
        # some configurations to attempt to import
        try:
            import brian2genn.correctness_testing
        except:
            pass
        configurations = Configuration.__subclasses__()
    if speed_tests is None:
        speed_tests = SpeedTest.__subclasses__()
    speed_tests.sort(key=lambda ft: ft.fullname())
    if verbose:
        print("Running speed tests")
        print("Configurations:", ", ".join(c.name for c in configurations))

    full_results = {}
    tag_results = defaultdict(lambda: defaultdict(list))
    for ft in speed_tests:
        if verbose:
            print(f"{ft.fullname()}: ", end=" ")
        for n in ft.n_range[n_slice]:
            if verbose:
                print(f"n={int(n)} [", end=" ")
            for configuration in configurations:
                sym = "."
                for _ in range(1 + int(run_twice)):
                    tb, res, runtime, prof_info = results(
                        configuration, ft, n, maximum_run_time=maximum_run_time
                    )
                if isinstance(res, Exception):
                    if isinstance(res, NotImplementedError):
                        sym = "N"
                    else:
                        sym = "E"
                    if configuration is DefaultConfiguration:
                        raise res
                    runtime = numpy.NAN
                sys.stdout.write(sym)
                full_results[configuration.name, ft.fullname(), n, "All"] = runtime
                suffixtime = defaultdict(float)
                overheadstime = float(runtime)
                for codeobjname, proftime in prof_info:
                    # parts = codeobjname.split('_')
                    # parts = [part for part in parts if not re.match(r'\d+', part)]
                    # suffix = '_'.join(parts)
                    suffix = codeobjname
                    suffixtime[suffix] += proftime
                    overheadstime -= float(proftime)
                for suffix, proftime in list(suffixtime.items()):
                    full_results[configuration.name, ft.fullname(), n, suffix] = (
                        proftime
                    )
                full_results[configuration.name, ft.fullname(), n, "Overheads"] = (
                    overheadstime
                )
            if verbose:
                print("]", end=" ")
        if verbose:
            print()

    return SpeedTestResults(full_results, configurations, speed_tests)


class SpeedTestResults:
    def __init__(self, full_results, configurations, speed_tests):
        self.full_results = full_results
        self.configurations = configurations
        self.speed_tests = speed_tests

    def get_ns(self, fullname):
        L = [(cn, fn, n, s) for cn, fn, n, s in self.full_results if fn == fullname]
        confignames, fullnames, n, codeobjsuffixes = zip(*L)
        return numpy.array(sorted(list(set(n))))

    def get_codeobjsuffixes(self, fullname):
        L = [(cn, fn, n, s) for cn, fn, n, s in self.full_results if fn == fullname]
        confignames, fullnames, n, codeobjsuffixes = zip(*L)
        return set(codeobjsuffixes)

    def plot_all_tests(self, relative=False, profiling_minimum=1.0):
        if relative and profiling_minimum < 1:
            raise ValueError("Cannot use relative plots with profiling")
        import pylab

        for st in self.speed_tests:
            fullname = st.fullname()
            pylab.figure()
            ns = self.get_ns(fullname)
            codeobjsuffixes = self.get_codeobjsuffixes(fullname)
            codeobjsuffixes.remove("All")
            codeobjsuffixes.remove("Overheads")
            codeobjsuffixes = ["All", "Overheads"] + sorted(codeobjsuffixes)
            if relative or profiling_minimum == 1:
                codeobjsuffixes = ["All"]
            baseline = None
            havelabel = set()
            markerstyles_cycle = iter(
                itertools.cycle(["o", "s", "d", "v", "p", "h", "^", "<", ">"])
            )
            dashes = {}
            markerstyles = {}
            for isuffix, suffix in enumerate(codeobjsuffixes):
                cols = itertools.cycle(pylab.rcParams["axes.color_cycle"])
                for (iconfig, config), col in zip(enumerate(self.configurations), cols):
                    configname = config.name
                    runtimes = []
                    skip = True
                    for n in ns:
                        runtime = self.full_results.get(
                            (configname, fullname, n, "All"), numpy.nan
                        )
                        thistime = self.full_results.get(
                            (configname, fullname, n, suffix), numpy.nan
                        )
                        if float(thistime / runtime) >= profiling_minimum:
                            skip = False
                        runtimes.append(thistime)
                    if skip:
                        continue
                    runtimes = numpy.array(runtimes)
                    if relative:
                        if baseline is None:
                            baseline = runtimes
                        runtimes = baseline / runtimes
                    if suffix == "All":
                        lw = 2
                        label = configname
                    else:
                        lw = 1
                        label = suffix
                    plottable = sum(-numpy.isnan(runtimes[1:] + runtimes[:-1]))
                    if plottable:
                        if label in havelabel:
                            label = None
                        else:
                            havelabel.add(label)
                        dash = None
                        msty = None
                        if suffix != "All":
                            if suffix in dashes:
                                dash = dashes[suffix]
                                msty = markerstyles[suffix]
                            else:
                                j = len(dashes)
                                dash = (8, 2)
                                for b in bin(j)[2:]:
                                    if b == "0":
                                        dash = dash + (2, 2)
                                    else:
                                        dash = dash + (4, 2)
                                dashes[suffix] = dash
                                markerstyles[suffix] = msty = next(markerstyles_cycle)
                        line = pylab.plot(
                            ns,
                            runtimes,
                            lw=lw,
                            color=col,
                            marker=msty,
                            mec="none",
                            ms=8,
                            label=label,
                        )[0]
                        if dash is not None:
                            line.set_dashes(dash)
            pylab.title(fullname)
            pylab.legend(loc="best", fontsize="x-small", handlelength=8.0)
            pylab.xlabel(st.n_label)
            if st.n_axis_log:
                pylab.gca().set_xscale("log")
            if st.time_axis_log:
                pylab.gca().set_yscale("log")


# Code below auto generates restructured text tables, copied from:
# http://stackoverflow.com/questions/11347505/what-are-some-approaches-to-outputting-a-python-data-structure-to-restructuredte


def make_table(grid):
    max_cols = [
        max(out)
        for out in map(list, zip(*[[len(item) for item in row] for row in grid]))
    ]
    rst = table_div(max_cols, 1)

    for i, row in enumerate(grid):
        header_flag = False
        if i == 0 or i == len(grid) - 1:
            header_flag = True
        rst += normalize_row(row, max_cols)
        rst += table_div(max_cols, header_flag)
    return rst


def table_div(max_cols, header_flag=1):
    out = ""
    if header_flag == 1:
        style = "="
    else:
        style = "-"

    for max_col in max_cols:
        out += f"{max_col * style} "

    out += "\n"
    return out


def normalize_row(row, max_cols):
    r = ""
    for i, max_col in enumerate(max_cols):
        r += row[i] + (max_col - len(row[i]) + 1) * " "

    return f"{r}\n"