Get stack trace.
summarizeis set to
False(default), returns the call stack trace. The stack trace is a list of tuples each of which represents one of two types of operations, push(“ENTER”) or pop(“EXIT”) to/from the call stack. The table blow shows data sored in the tuple elements.
“ENTER” or “EXIT”
Time (Seconds elapsed from the epoch)
String to represent Cells object
A tuple of arguments to the Cells object
summarizeis set to
True, returns a summary of the trace. The summary is a
dict, whose keys are the representation strings of the called Cells, and whose values are dicts containing the following statistics of the Cells.
Number of calls to the Cells
Total time in seconds elapsed in the Cells
Time (from the epoch) of the first entry to the Cells
Time (from the epoch) of the last exit from the Cells
The call stack trace must be activated by
get_stacktrace(), otherwise a runtime error is raised. When setting
True, make sure that
start_stacktrace()was called with its parameter
maxlenbeing set to
get_stacktrace()may raise an Error because of incomplete trace records.
A list of tuples each of which is a record of stack history, or a dict containing the summary information.
The sample code below creates and executes a sample model, and stores a trace summary of the execution as Pandas DataFrame:
import time import pandas as pd import modelx as mx m = mx.new_model() m.time = time @mx.defcells def foo(x): time.sleep(0.1) # Waits 0.1 second return foo(x-1) + 1 if x > 0 else bar() @mx.defcells def bar(): time.sleep(0.2) # Waits 0.2 second return 0 mx.start_stacktrace(maxlen=None) foo(5) df = pd.DataFrame.from_dict( mx.get_stacktrace(summarize=True), orient="index") mx.stop_stacktrace()
The DataFrame shows how many times each formula was called, how much time each formula took, time at which the execution enters into each formula for the first time, and time at which the execution leaves each formula for the last.
Changed in version 0.11.0: summarize parameter is added.
New in version 0.0.25.