Skip to content

Commit c9ae7d2

Browse files
committed
Sort timestamps before xlim filtering, replace pylab with matplotlib.pyplot, and add test (Closes #387)
1 parent 5794f88 commit c9ae7d2

3 files changed

Lines changed: 54 additions & 19 deletions

File tree

‎.github/workflows/lint_python.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444
--show-source --statistics
4545
- run: isort --check-only --profile black . || true
4646
- run: pip install --editable .
47-
- run: pip install numpy pylab-sdk
47+
- run: pip install numpy matplotlib
4848
- run: mkdir --parents --verbose .mypy_cache
4949
- run: mypy --ignore-missing-imports --install-types --non-interactive . || true
5050
- run: shopt -s globstar && pyupgrade --py36-plus **/*.py || true
@@ -76,5 +76,5 @@ jobs:
7676
- run: pip install --upgrade pip wheel
7777
- run: pip install pytest ipython
7878
- run: pip install --editable .
79-
- run: pip install numpy pylab-sdk
79+
- run: pip install numpy matplotlib
8080
- run: make test

‎mprof.py‎

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,8 @@ def add_brackets(xloc, yloc, xshift=0, color="r", label=None, options=None):
306306
value to subtract to xloc.
307307
"""
308308
try:
309-
import pylab as pl
309+
import matplotlib.pyplot as pl
310+
import numpy as np
310311
except ImportError as e:
311312
print("matplotlib is needed for plotting.")
312313
print(e)
@@ -315,16 +316,17 @@ def add_brackets(xloc, yloc, xshift=0, color="r", label=None, options=None):
315316
vsize = (pl.ylim()[1] - pl.ylim()[0]) / height_ratio
316317
hsize = (pl.xlim()[1] - pl.xlim()[0]) / (3. * height_ratio)
317318

318-
bracket_x = pl.asarray([hsize, 0, 0, hsize])
319-
bracket_y = pl.asarray([vsize, vsize, -vsize, -vsize])
319+
bracket_x = np.asarray([hsize, 0, 0, hsize])
320+
bracket_y = np.asarray([vsize, vsize, -vsize, -vsize])
320321

321322
# Matplotlib workaround: labels starting with _ aren't displayed
322323
if label[0] == '_':
323324
label = ' ' + label
324-
if options.xlim is None or options.xlim[0] <= (xloc[0] - xshift) <= options.xlim[1]:
325+
xlim = getattr(options, 'xlim', None) if options is not None else None
326+
if xlim is None or xlim[0] <= (xloc[0] - xshift) <= xlim[1]:
325327
pl.plot(bracket_x + xloc[0] - xshift, bracket_y + yloc[0],
326328
"-" + color, linewidth=2, label=label)
327-
if options.xlim is None or options.xlim[0] <= (xloc[1] - xshift) <= options.xlim[1]:
329+
if xlim is None or xlim[0] <= (xloc[1] - xshift) <= xlim[1]:
328330
pl.plot(-bracket_x + xloc[1] - xshift, bracket_y + yloc[1],
329331
"-" + color, linewidth=2)
330332

@@ -401,12 +403,12 @@ def read_mprofile_file(filename):
401403

402404
def plot_file(filename, index=0, timestamps=True, children=True, options=None):
403405
try:
404-
import pylab as pl
406+
import matplotlib.pyplot as pl
405407
except ImportError as e:
406408
print("matplotlib is needed for plotting.")
407409
print(e)
408410
sys.exit(1)
409-
import numpy as np # pylab requires numpy anyway
411+
import numpy as np # matplotlib requires numpy anyway
410412
mprofile = read_mprofile_file(filename)
411413

412414
if len(mprofile['timestamp']) == 0:
@@ -431,18 +433,19 @@ def plot_file(filename, index=0, timestamps=True, children=True, options=None):
431433

432434
mem = np.asarray(mem)
433435
t = np.asarray(t)
434-
global_start = t[0]
435-
t = t - global_start
436-
437-
if options.xlim is not None:
438-
cond = (t >= options.xlim[0]) & (t <= options.xlim[1])
439-
mem, t = mem[cond], t[cond]
440-
441436
ind = t.argsort()
442437
mem = mem[ind]
443438
t = t[ind]
444439

445440
# Plot curves
441+
global_start = float(t[0])
442+
t = t - global_start
443+
444+
if options is not None and getattr(options, 'xlim', None) is not None:
445+
# Filter points to the requested xlim window so the y-axis and peak
446+
# memory marker rescale to the visible range (PR #367).
447+
cond = (t >= options.xlim[0]) & (t <= options.xlim[1])
448+
mem, t = mem[cond], t[cond]
446449
max_mem = mem.max()
447450
max_mem_ind = mem.argmax()
448451

@@ -538,12 +541,12 @@ def plot_file(filename, index=0, timestamps=True, children=True, options=None):
538541

539542
def flame_plotter(filename, index=0, timestamps=True, children=True, options=None):
540543
try:
541-
import pylab as pl
544+
import matplotlib.pyplot as pl
542545
except ImportError as e:
543546
print("matplotlib is needed for plotting.")
544547
print(e)
545548
sys.exit(1)
546-
import numpy as np # pylab requires numpy anyway
549+
import numpy as np # matplotlib requires numpy anyway
547550
mprofile = read_mprofile_file(filename)
548551

549552
if len(mprofile['timestamp']) == 0:
@@ -798,7 +801,7 @@ def xlim_type(value):
798801
import matplotlib
799802
matplotlib.use(args.backend)
800803

801-
import pylab as pl
804+
import matplotlib.pyplot as pl
802805
except ImportError as e:
803806
print("matplotlib is needed for plotting.")
804807
print(e)

‎test/test_mprof.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,38 @@ def test_non_run_action_accepts_negative_index(self):
6969
if os.path.exists(tmp_prof):
7070
os.remove(tmp_prof)
7171

72+
def test_plot_file_xlim_window_rescales_ylim(self):
73+
# Verify that plotting with --window (options.xlim) filters the plotted
74+
# data to the requested time window so the y-axis and peak memory marker
75+
# reflect the windowed data rather than out-of-window spikes (PR #367).
76+
import matplotlib
77+
matplotlib.use('Agg')
78+
import matplotlib.pyplot as pl
79+
80+
content = (
81+
"CMDLINE python test.py\n"
82+
"MEM 10.0 100.0\n"
83+
"MEM 20.0 101.0\n"
84+
"MEM 500.0 102.0\n" # Large spike at t=2.0s outside the [0, 1.5] window
85+
)
86+
with tempfile.NamedTemporaryFile('w', suffix='.dat') as tmp_dat:
87+
tmp_dat.write(content)
88+
tmp_dat.flush()
89+
90+
class Options:
91+
xlim = [0.0, 1.5]
92+
slope = False
93+
94+
fig = pl.figure()
95+
try:
96+
mprof.plot_file(tmp_dat.name, options=Options())
97+
top_ylim = pl.ylim()[1]
98+
# Since the 500.0 MiB spike at t=2.0s is outside xlim=[0.0, 1.5],
99+
# the y-axis upper bound should be scaled to ~20.0 MiB, not 500.0 MiB.
100+
self.assertLess(top_ylim, 50.0)
101+
finally:
102+
pl.close(fig)
103+
72104

73105
if __name__ == "__main__":
74106
unittest.main()

0 commit comments

Comments
 (0)