Skip to content

Add 'window' support to mprof to plot a particular time range #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 11, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions mprof
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import copy
import time
import math

from optparse import OptionParser
from optparse import OptionParser, OptionValueError

import memory_profiler as mp

Expand Down Expand Up @@ -236,7 +236,7 @@ def run_action():
include_children=options.include_children, stream=f)


def add_brackets(xloc, yloc, xshift=0, color="r", label=None):
def add_brackets(xloc, yloc, xshift=0, color="r", label=None, options=None):
"""Add two brackets on the memory line plot.

This function uses the current figure.
Expand Down Expand Up @@ -265,10 +265,12 @@ def add_brackets(xloc, yloc, xshift=0, color="r", label=None):
# Matplotlib workaround: labels starting with _ aren't displayed
if label[0] == '_':
label = ' ' + label
pl.plot(bracket_x + xloc[0] - xshift, bracket_y + yloc[0],
"-" + color, linewidth=2, label=label)
pl.plot(-bracket_x + xloc[1] - xshift, bracket_y + yloc[1],
"-" + color, linewidth=2 )
if options.xlim is None or options.xlim[0] <= (xloc[0] - xshift) <= options.xlim[1]:
pl.plot(bracket_x + xloc[0] - xshift, bracket_y + yloc[0],
"-" + color, linewidth=2, label=label)
if options.xlim is None or options.xlim[0] <= (xloc[1] - xshift) <= options.xlim[1]:
pl.plot(-bracket_x + xloc[1] - xshift, bracket_y + yloc[1],
"-" + color, linewidth=2 )

# TODO: use matplotlib.patches.Polygon to draw a colored background for
# each function.
Expand Down Expand Up @@ -330,7 +332,7 @@ def read_mprofile_file(filename):



def plot_file(filename, index=0, timestamps=True):
def plot_file(filename, index=0, timestamps=True, options=None):
try:
import pylab as pl
except ImportError:
Expand Down Expand Up @@ -392,7 +394,7 @@ def plot_file(filename, index=0, timestamps=True):
add_brackets(execution[:2], execution[2:], xshift=global_start,
color= all_colors[func_num % len(all_colors)],
label=f.split(".")[-1]
+ " %.3fs" % (execution[1] - execution[0]))
+ " %.3fs" % (execution[1] - execution[0]), options=options)
func_num += 1

if timestamps:
Expand All @@ -405,6 +407,15 @@ def plot_file(filename, index=0, timestamps=True):


def plot_action():
def get_comma_separated_args(option, opt, value, parser):
try:
newvalue = [float(x) for x in value.split(',')]
except:
raise OptionValueError("'%s' option must contain two numbers separated with a comma" % value)
if len(newvalue) != 2:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on my system I get an error because it doesn't find OptionValueError:
NameError: name 'OptionValueError' is not defined

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using Python 3.4

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the report, didn't realise Python wouldn't pick up missing imports until the line of code was run. Fixed with a modification to Line 12.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great. Now it just lacks support for open-ended windows (i.e. .1,:). Thanks!

raise OptionValueError("'%s' option must contain two numbers separated with a comma" % value)
setattr(parser.values, option.dest, newvalue)

try:
import pylab as pl
except ImportError:
Expand All @@ -421,6 +432,10 @@ def plot_action():
help="Do not display function timestamps on plot.")
parser.add_option("--output", "-o",
help="Save plot to file instead of displaying it.")
parser.add_option("--window", "-w", dest="xlim",
type="str", action="callback",
callback=get_comma_separated_args,
help="Plot a time-subset of the data. E.g. to plot between 0 and 20.5 seconds: --window 0,20.5")
(options, args) = parser.parse_args()

profiles = glob.glob("mprofile_??????????????.dat")
Expand Down Expand Up @@ -453,12 +468,15 @@ def plot_action():

fig = pl.figure(figsize=(14, 6), dpi=90)
ax = fig.add_axes([0.1, 0.1, 0.6, 0.75])
if options.xlim is not None:
pl.xlim(options.xlim[0], options.xlim[1])

if len(filenames) > 1 or options.no_timestamps:
timestamps = False
else:
timestamps = True
for n, filename in enumerate(filenames):
mprofile = plot_file(filename, index=n, timestamps=timestamps)
mprofile = plot_file(filename, index=n, timestamps=timestamps, options=options)
pl.xlabel("time (in seconds)")
pl.ylabel("memory used (in MiB)")

Expand Down