Beispiel #1
0
    def render (self, include=None, exclude=None, **kwargs): # {{{
        """
            Actually returns an image (as png), and will reset the trace if the
            include or exclude parameters are different.
        """
        
        inc = self.last_include
        exc = self.last_exclude

        if include or include == "":
            inc = include.split(',')

        if exclude or exclude == "":
            exc = exclude.split(',')

        if inc != self.last_include or exc != self.last_exclude:
            # New trace, resetting the old one.

            pycallgraph.stop_trace()
            pycallgraph.start_trace(filter_func = self.filter_func(inc, exc), reset=True)

        pycallgraph.make_dot_graph("." + self.image_file, stop=False)

        cherrypy.response.headers['Content-Type'] = "image/png"
        f = open("." + self.image_file, "r+b")
        return f.read()
Beispiel #2
0
def main():

    # Do the trace, remember the values for later
    pycallgraph.start_trace()
    import HTMLParser
    pycallgraph.stop_trace()

    # Set the edge colour to black for all examples
    pycallgraph.settings['edge_colour'] = lambda a, b: 'black'

    # Default node colouring
    pycallgraph.make_dot_graph('colours-default.png')

    # Rainbow
    pycallgraph.settings['node_colour'] = rainbow
    pycallgraph.make_dot_graph('colours-rainbow.png')

    # Greyscale
    pycallgraph.settings['node_colour'] = greyscale
    pycallgraph.make_dot_graph('colours-greyscale.png')

    # Orange/Green
    pycallgraph.settings['node_colour'] = orange_green
    pycallgraph.make_dot_graph('colours-orange-green.png')

    # Random
    pycallgraph.settings['node_colour'] = rand
    pycallgraph.make_dot_graph('colours-random.png')
Beispiel #3
0
def main():

    # Do the trace, remember the values for later
    pycallgraph.start_trace()
    import HTMLParser
    pycallgraph.stop_trace()

    # Set the edge colour to black for all examples
    pycallgraph.settings['edge_colour'] = lambda a, b: 'black'

    # Default node colouring
    pycallgraph.make_dot_graph('colours-default.png')

    # Rainbow
    pycallgraph.settings['node_colour'] = rainbow
    pycallgraph.make_dot_graph('colours-rainbow.png')

    # Greyscale
    pycallgraph.settings['node_colour'] = greyscale
    pycallgraph.make_dot_graph('colours-greyscale.png')

    # Orange/Green
    pycallgraph.settings['node_colour'] = orange_green
    pycallgraph.make_dot_graph('colours-orange-green.png')

    # Random
    pycallgraph.settings['node_colour'] = rand
    pycallgraph.make_dot_graph('colours-random.png')
Beispiel #4
0
 def run_profile(self, app, *args, **kwargs):
     pycallgraph = self.pycallgraph
     pycallgraph.start_trace(reset=True, filter_func=self._filter)
     try:
         return app(*args, **kwargs)
     finally:
         pycallgraph.stop_trace()
Beispiel #5
0
 def run_profile(self, app, *args, **kwargs):
     pycallgraph = self.pycallgraph
     pycallgraph.start_trace(reset=True, filter_func=self._filter)
     try:
         return app(*args, **kwargs)
     finally:
         pycallgraph.stop_trace()
 def callwrapper(*args, **kwargs):
     if not outfile: # allow deactivating
         return fn(*args, **kwargs)
     pycallgraph.start_trace()
     fn_output = fn(*args, **kwargs)
     pycallgraph.stop_trace()
     pycallgraph.make_dot_graph(outfile)
     return fn_output
Beispiel #7
0
 def call_graph(fac, cmd='fac.query(1)'):
     import pycallgraph
     import Image
     iom = fac.hs.iom
     logmsg('Call Graph Command: '+cmd)
     callgraph_fpath = iom.get_temp_fpath('callgraph'+cmd+'.png')
     pycallgraph.start_trace()
     eval(cmd)
     pycallgraph.stop_trace()
     pycallgraph.make_dot_graph(callgraph_fpath)
     Image.open(callgraph_fpath).show()
Beispiel #8
0
 def call_graph(fac, cmd='fac.query(1)'):
     import pycallgraph
     import Image
     iom = fac.hs.iom
     logmsg('Call Graph Command: ' + cmd)
     callgraph_fpath = iom.get_temp_fpath('callgraph' + cmd + '.png')
     pycallgraph.start_trace()
     eval(cmd)
     pycallgraph.stop_trace()
     pycallgraph.make_dot_graph(callgraph_fpath)
     Image.open(callgraph_fpath).show()
Beispiel #9
0
def cuba():
    eqs = '''
    dv/dt = (ge+gi-(v+49*mV))/(20*ms) : volt
    dge/dt = -ge/(5*ms) : volt
    dgi/dt = -gi/(10*ms) : volt
    '''
    eqs = Equations(eqs)
    pycallgraph.start_trace(filter_func=ff)
    eqs.prepare()
    pycallgraph.stop_trace()
    pycallgraph.make_dot_graph('callgraphs/cuba-bigcallgraph-Equations.prepare.png')
Beispiel #10
0
def cuba():
    eqs = '''
    dv/dt = (ge+gi-(v+49*mV))/(20*ms) : volt
    dge/dt = -ge/(5*ms) : volt
    dgi/dt = -gi/(10*ms) : volt
    '''
    eqs = Equations(eqs)
    pycallgraph.start_trace(filter_func=ff)
    eqs.prepare()
    pycallgraph.stop_trace()
    pycallgraph.make_dot_graph(
        'callgraphs/cuba-bigcallgraph-Equations.prepare.png')
Beispiel #11
0
            def callgraphed_application(*args, **kwargs):
                ''' Start measuring the callgraph and dispatch. '''

                global _runcount

                # start tracing right before dispatch
                pycallgraph.start_trace()
                for chunk in app(*args, **kwargs):
                    yield chunk  # exhaust app generator

                # stop tracing after request is finished
                pycallgraph.stop_trace()
                _runcount = _runcount + 1
                pycallgraph.make_dot_graph('/'.join(
                    prefix +
                    ['.profile',
                     '%s-%s-callgraph.png' % (appname, _runcount)]))
                raise StopIteration()
    def run(self):
        '''Main code runner for testing. To set a new test, update the self.callTest attribute in __init__(). 
        '''
        fp = environLocal.getTempFile('.png')
        gf = pycallgraph.GlobbingFilter(exclude=self.excludeList)
        # create instnace; will call setup routines
        ct = self.callTest()

        # start timer
        print('starting test')
        t = common.Timer()
        t.start()

        pycallgraph.start_trace(filter_func = gf)
        ct.testFocus() # run routine

        pycallgraph.stop_trace()
        pycallgraph.make_dot_graph(fp)

        print('elpased time: %s' % t)
        # open the completed file
        environLocal.launch('png', fp)
Beispiel #13
0
def cuba():
    N = 4000
    Ne = int(N * 0.8)
    Ni = N - Ne
    p = 80. / N

    eqs = '''
    dv/dt = (ge+gi-(v+49*mV))/(20*ms) : volt
    dge/dt = -ge/(5*ms) : volt
    dgi/dt = -gi/(10*ms) : volt
    '''

    pycallgraph.start_trace(filter_func=ff_nomagicstateupdater)
    P = NeuronGroup(N, eqs, threshold=-50 * mV, reset=-60 * mV)
    pycallgraph.stop_trace()
    pycallgraph.make_dot_graph(
        'callgraphs/cuba-bigcallgraph-NeuronGroup.__init__.no_magic_state_updater.png'
    )

    P.v = -60 * mV + 10 * mV * rand(len(P))
    Pe = P.subgroup(Ne)
    Pi = P.subgroup(Ni)

    pycallgraph.start_trace(filter_func=ff)
    Ce = Connection(Pe, P, 'ge', weight=1.62 * mV, sparseness=p)
    pycallgraph.stop_trace()
    pycallgraph.make_dot_graph(
        'callgraphs/cuba-bigcallgraph-Connection.__init__.png')

    Ci = Connection(Pi, P, 'gi', weight=-9 * mV, sparseness=p)

    pycallgraph.start_trace(filter_func=ff)
    M = SpikeMonitor(P)
    trace = StateMonitor(P, 'v', record=0)
    pycallgraph.stop_trace()
    pycallgraph.make_dot_graph(
        'callgraphs/cuba-bigcallgraph-Monitors.__init__.png')

    pycallgraph.start_trace(filter_func=ff_prepare)
    run(.1 * ms)
    pycallgraph.stop_trace()
    pycallgraph.make_dot_graph('callgraphs/cuba-bigcallgraph-run-prepare.png')
Beispiel #14
0
def cuba():
    N = 4000
    Ne = int(N * 0.8)
    Ni = N - Ne
    p = 80. / N

    eqs = '''
    dv/dt = (ge+gi-(v+49*mV))/(20*ms) : volt
    dge/dt = -ge/(5*ms) : volt
    dgi/dt = -gi/(10*ms) : volt
    '''

    pycallgraph.start_trace(filter_func=ff_nomagicstateupdater)
    P = NeuronGroup(N, eqs,
                  threshold= -50 * mV, reset= -60 * mV)
    pycallgraph.stop_trace()
    pycallgraph.make_dot_graph('callgraphs/cuba-bigcallgraph-NeuronGroup.__init__.no_magic_state_updater.png')

    P.v = -60 * mV + 10 * mV * rand(len(P))
    Pe = P.subgroup(Ne)
    Pi = P.subgroup(Ni)

    pycallgraph.start_trace(filter_func=ff)
    Ce = Connection(Pe, P, 'ge', weight=1.62 * mV, sparseness=p)
    pycallgraph.stop_trace()
    pycallgraph.make_dot_graph('callgraphs/cuba-bigcallgraph-Connection.__init__.png')

    Ci = Connection(Pi, P, 'gi', weight= -9 * mV, sparseness=p)

    pycallgraph.start_trace(filter_func=ff)
    M = SpikeMonitor(P)
    trace = StateMonitor(P, 'v', record=0)
    pycallgraph.stop_trace()
    pycallgraph.make_dot_graph('callgraphs/cuba-bigcallgraph-Monitors.__init__.png')

    pycallgraph.start_trace(filter_func=ff_prepare)
    run(.1 * ms)
    pycallgraph.stop_trace()
    pycallgraph.make_dot_graph('callgraphs/cuba-bigcallgraph-run-prepare.png')
def run_tests(test_labels, verbosity=1, interactive=True,
        extra_tests=[], nodatabase=False, xml_out=False, callgraph=False, html_only=False):
    """
    Test runner which displays a code coverage report at the end of the
    run.
    """
    cov = coverage.coverage()
    cov.erase()
    cov.use_cache(0)

    test_labels = test_labels or getattr(settings, "TEST_APPS", None)
    cover_branch = getattr(settings, "COVERAGE_BRANCH_COVERAGE", False)
    cov = coverage.coverage(branch=cover_branch, cover_pylib=False)
    cov.use_cache(0)

    coverage_modules = []
    if test_labels:
        for label in test_labels:
            # Don't report coverage if you're only running a single
            # test case.
            if '.' not in label:
                app = get_app(label)
                coverage_modules.extend(get_all_coverage_modules(app))
    else:
        for app in get_apps():
            coverage_modules.extend(get_all_coverage_modules(app))

    morfs = filter(is_wanted_module, coverage_modules)

    if callgraph:
        try:
            import pycallgraph
            #_include = [i.__name__ for i in coverage_modules]
            _included = getattr(settings, "COVERAGE_INCLUDE_MODULES", [])
            _excluded = getattr(settings, "COVERAGE_EXCLUDE_MODULES", [])

            _included = [i.strip('*')+'*' for i in _included]
            _excluded = [i.strip('*')+'*' for i in _included]

            _filter_func = pycallgraph.GlobbingFilter(
                include=_included or ['*'],
                #include=['lotericas.*'],
                #exclude=[],
                #max_depth=options.max_depth,
            )

            pycallgraph_enabled = True
        except ImportError:
            pycallgraph_enabled = False
    else:
        pycallgraph_enabled = False

    cov.start()

    if pycallgraph_enabled:
        pycallgraph.start_trace(filter_func=_filter_func)

    if nodatabase:
        results = nodatabase_run_tests(test_labels, verbosity, interactive,
            extra_tests)
    else:
        tr = django_test_runner(verbosity, interactive)
        results = tr.run_tests(test_labels, extra_tests)
        #results = django_test_runner(test_labels, verbosity, interactive,
        #    extra_tests)

    if callgraph and pycallgraph_enabled:
        pycallgraph.stop_trace()

    cov.stop()

    if getattr(settings, "COVERAGE_HTML_REPORT", False) or \
            os.environ.get("COVERAGE_HTML_REPORT"):
        output_dir = getattr(settings, "COVERAGE_HTML_DIRECTORY", "covhtml")
        report_method = curry(cov.html_report, directory=output_dir)
        if callgraph and pycallgraph_enabled:
            callgraph_path = output_dir + '/' + 'callgraph.png'
            pycallgraph.make_dot_graph(callgraph_path)

        print >>sys.stdout
        print >>sys.stdout, "Coverage HTML reports were output to '%s'" %output_dir
        if callgraph:
            if pycallgraph_enabled:
                print >>sys.stdout, "Call graph was output to '%s'" %callgraph_path
            else:
                print >>sys.stdout, "Call graph was not generated: Install 'pycallgraph' module to do so"

    else:
        report_method = cov.report

    if coverage_modules:
        if xml_out:
            # using the same output directory as the --xml function uses for testing
            if not os.path.isdir(os.path.join("temp", "xml")):
                os.makedirs(os.path.join("temp", "xml"))
            output_filename = 'temp/xml/coverage_output.xml'
            cov.xml_report(morfs=coverage_modules, outfile=output_filename)

        if not html_only:
            cov.report(coverage_modules, show_missing=1)

    return results
Beispiel #16
0
M = SpikeMonitor(P)

net = MagicNetwork()

start = time.time()
net.prepare()
net.run(1 * ms)
print 'Preparation time:', time.time() - start

if do_callgraph:
    import pycallgraph

    def ff(call_stack, module_name, class_name, func_name, full_name):
        if not 'brian' in module_name: return False
        return True

    pycallgraph.start_trace(filter_func=ff)

start = time.time()
net.run(1 * second)
print 'Run time:', time.time() - start

if do_callgraph:
    pycallgraph.stop_trace()
    pycallgraph.make_dot_graph('callgraph.png')

if plot_output:
    raster_plot(M)
    show()
Beispiel #17
0
    def run(self, runWithEnviron=False):
        '''Main code runner for testing. To set a new test, update the self.callTest attribute in __init__(). 
        '''
        suffix = '.svg'
        fmt = suffix[1:]
        _MOD = "test.timeGraphs.py"

        if runWithEnviron:
            from music21 import environment
            environLocal = environment.Environment(_MOD)
            fp = environLocal.getTempFile(suffix)
        # manually get a temporary file
        else:
            import tempfile
            import os
            import sys
            if os.name in ['nt'] or sys.platform.startswith('win'):
                platform = 'win'
            else:
                platform = 'other'
            
            tempdir = os.path.join(tempfile.gettempdir(), 'music21')
            if platform != 'win':
                fd, fp = tempfile.mkstemp(dir=tempdir, suffix=suffix)
                if isinstance(fd, int):
                # on MacOS, fd returns an int, like 3, when this is called
                # in some context (specifically, programmatically in a 
                # TestExternal class. the fp is still valid and works
                # TODO: this did not work on MacOS 10.6.8 w/ py 2.7
                    pass
                else:
                    fd.close() 
            else:
                tf = tempfile.NamedTemporaryFile(dir=tempdir, suffix=suffix)
                fp = tf.name
                tf.close()

 
        if self.includeList is not None:
            gf = pycallgraph.GlobbingFilter(include=self.includeList, exclude=self.excludeList)
        else:
            gf = pycallgraph.GlobbingFilter(exclude=self.excludeList)
        # create instance; will call setup routines
        ct = self.callTest()

        # start timer
        print('%s starting test' % _MOD)
        t = Timer()
        t.start()

        pycallgraph.start_trace(filter_func = gf)
        ct.testFocus() # run routine

        pycallgraph.stop_trace()
        pycallgraph.make_dot_graph(fp, format=fmt, tool='/usr/local/bin/dot')
        print('elapsed time: %s' % t)
        # open the completed file
        print('file path: ' + fp)
        try:
            environLocal = environment.Environment(_MOD)
            environLocal.launch(format, fp)
        except NameError:
            pass
Beispiel #18
0
 def __call__(self, *args, **kwargs):
     pycallgraph.start_trace(reset=False)
     return_value = self.func(*args, **kwargs)
     pycallgraph.stop_trace()
     return return_value
Beispiel #19
0
def run_tests(test_labels,
              verbosity=1,
              interactive=True,
              extra_tests=[],
              nodatabase=False,
              xml_out=False,
              callgraph=False):
    """
    Test runner which displays a code coverage report at the end of the
    run.
    """
    cov = coverage.coverage()
    cov.erase()
    cov.use_cache(0)

    test_labels = test_labels or getattr(settings, "TEST_APPS", None)
    cover_branch = getattr(settings, "COVERAGE_BRANCH_COVERAGE", False)
    cov = coverage.coverage(branch=cover_branch, cover_pylib=False)
    cov.use_cache(0)

    coverage_modules = []
    if test_labels:
        for label in test_labels:
            # Don't report coverage if you're only running a single
            # test case.
            if '.' not in label:
                app = get_app(label)
                coverage_modules.extend(get_all_coverage_modules(app))
    else:
        for app in get_apps():
            coverage_modules.extend(get_all_coverage_modules(app))

    morfs = filter(is_wanted_module, coverage_modules)

    if callgraph:
        try:
            import pycallgraph
            #_include = [i.__name__ for i in coverage_modules]
            _included = getattr(settings, "COVERAGE_INCLUDE_MODULES", [])
            _excluded = getattr(settings, "COVERAGE_EXCLUDE_MODULES", [])

            _included = [i.strip('*') + '*' for i in _included]
            _excluded = [i.strip('*') + '*' for i in _included]

            _filter_func = pycallgraph.GlobbingFilter(
                include=_included or ['*'],
                #include=['lotericas.*'],
                #exclude=[],
                #max_depth=options.max_depth,
            )

            pycallgraph_enabled = True
        except ImportError:
            pycallgraph_enabled = False
    else:
        pycallgraph_enabled = False

    cov.start()

    if pycallgraph_enabled:
        pycallgraph.start_trace(filter_func=_filter_func)

    if nodatabase:
        results = nodatabase_run_tests(test_labels, verbosity, interactive,
                                       extra_tests)
    else:
        results = django_test_runner(test_labels, verbosity, interactive,
                                     extra_tests)

    if callgraph and pycallgraph_enabled:
        pycallgraph.stop_trace()

    cov.stop()

    report_methd = cov.report
    if getattr(settings, "COVERAGE_HTML_REPORT", False) or \
            os.environ.get("COVERAGE_HTML_REPORT"):
        output_dir = getattr(settings, "COVERAGE_HTML_DIRECTORY", "covhtml")
        report_method = curry(cov.html_report, directory=output_dir)
        if callgraph and pycallgraph_enabled:
            callgraph_path = output_dir + '/' + 'callgraph.png'
            pycallgraph.make_dot_graph(callgraph_path)

        print >> sys.stdout
        print >> sys.stdout, "Coverage HTML reports were output to '%s'" % output_dir
        if callgraph:
            if pycallgraph_enabled:
                print >> sys.stdout, "Call graph was output to '%s'" % callgraph_path
            else:
                print >> sys.stdout, "Call graph was not generated: Install 'pycallgraph' module to do so"

    else:
        report_method = cov.report

    if coverage_modules:
        if xml_out:
            # using the same output directory as the --xml function uses for testing
            if not os.path.isdir(os.path.join("temp", "xml")):
                os.makedirs(os.path.join("temp", "xml"))
            output_filename = 'temp/xml/coverage_output.xml'
            cov.xml_report(morfs=coverage_modules, outfile=output_filename)

        cov.report(coverage_modules, show_missing=1)

    return results
Beispiel #20
0
 def stop (self): # {{{
     pycallgraph.stop_trace()
     self.started = False
Beispiel #21
0
                #structure='dense',
                )

M = SpikeMonitor(P)

net = MagicNetwork()

start = time.time()
net.prepare()
net.run(1*ms)
print 'Preparation time:', time.time()-start

if do_callgraph:
    import pycallgraph
    def ff(call_stack, module_name, class_name, func_name, full_name):
        if not 'brian' in module_name: return False
        return True
    pycallgraph.start_trace(filter_func=ff)

start = time.time()
net.run(1 * second)
print 'Run time:', time.time()-start

if do_callgraph:
    pycallgraph.stop_trace()
    pycallgraph.make_dot_graph('callgraph.png')

if plot_output:
    raster_plot(M)
    show()
Beispiel #22
0
def pygraphit():
    import pycallgraph
    pycallgraph.start_trace()
    pycallgraph.stop_trace()
    pycallgraph.make_graph('/tmp/bleh.png')
Beispiel #23
0
    def run(self, runWithEnviron=True):
        '''
        Main code runner for testing. To set a new test, update the self.callTest attribute in __init__(). 
        
        Note that the default of runWithEnviron imports music21.environment.  That might
        skew results
        '''
        suffix = '.svg'
        outputFormat = suffix[1:]
        _MOD = "test.timeGraphs.py"

        if runWithEnviron:
            from music21 import environment
            environLocal = environment.Environment(_MOD)
            fp = environLocal.getTempFile(suffix)
        # manually get a temporary file
        else:
            import tempfile
            import os
            import sys
            if os.name in ['nt'] or sys.platform.startswith('win'):
                platform = 'win'
            else:
                platform = 'other'

            tempdir = os.path.join(tempfile.gettempdir(), 'music21')
            if platform != 'win':
                fd, fp = tempfile.mkstemp(dir=tempdir, suffix=suffix)
                if isinstance(fd, int):
                    # on MacOS, fd returns an int, like 3, when this is called
                    # in some context (specifically, programmatically in a
                    # TestExternal class. the fp is still valid and works
                    # TODO: this did not work on MacOS 10.6.8 w/ py 2.7
                    pass
                else:
                    fd.close()
            else:
                tf = tempfile.NamedTemporaryFile(dir=tempdir, suffix=suffix)
                fp = tf.name
                tf.close()

        if self.includeList is not None:
            gf = pycallgraph.GlobbingFilter(include=self.includeList,
                                            exclude=self.excludeList)
        else:
            gf = pycallgraph.GlobbingFilter(exclude=self.excludeList)
        # create instance; will call setup routines
        ct = self.callTest()

        # start timer
        print('%s starting test' % _MOD)
        t = Timer()
        t.start()

        pycallgraph.start_trace(filter_func=gf)
        ct.testFocus()  # run routine

        pycallgraph.stop_trace()
        pycallgraph.make_dot_graph(fp,
                                   format=outputFormat,
                                   tool='/usr/local/bin/dot')
        print('elapsed time: %s' % t)
        # open the completed file
        print('file path: ' + fp)
        try:
            environLocal = environment.Environment(_MOD)
            environLocal.launch(outputFormat, fp)
        except NameError:
            pass
Beispiel #24
0
 def stop(self):
     pycallgraph.stop_trace()