From 00b840d72e8e07f0064273fee423ebb9a212acfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nico=20Schl=C3=B6mer?= Date: Wed, 10 Feb 2021 08:31:48 +0100 Subject: [PATCH] import numpy as np --- Makefile | 1 + README.md | 18 +++++++++--------- example/concat.py | 14 +++++++------- perfplot/main.py | 28 ++++++++++++++-------------- test/perfplot_test.py | 20 ++++++++++---------- 5 files changed, 41 insertions(+), 40 deletions(-) diff --git a/Makefile b/Makefile index 65eaee7..0401cba 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,7 @@ clean: format: isort . black . + blacken-docs README.md lint: black --check . diff --git a/README.md b/README.md index 14cd412..4663252 100644 --- a/README.md +++ b/README.md @@ -18,17 +18,17 @@ results. For example, to compare different NumPy array concatenation methods, the script ```python -import numpy +import numpy as np import perfplot perfplot.show( - setup=lambda n: numpy.random.rand(n), # or setup=numpy.random.rand + setup=lambda n: np.random.rand(n), # or setup=np.random.rand kernels=[ - lambda a: numpy.c_[a, a], - lambda a: numpy.stack([a, a]).T, - lambda a: numpy.vstack([a, a]).T, - lambda a: numpy.column_stack([a, a]), - lambda a: numpy.concatenate([a[:, None], a[:, None]], axis=1), + lambda a: np.c_[a, a], + lambda a: np.stack([a, a]).T, + lambda a: np.vstack([a, a]).T, + lambda a: np.column_stack([a, a]), + lambda a: np.concatenate([a[:, None], a[:, None]], axis=1), ], labels=["c_", "stack", "vstack", "column_stack", "concat"], n_range=[2 ** k for k in range(25)], @@ -36,7 +36,7 @@ perfplot.show( # More optional arguments with their default values: # logx="auto", # set to True or False to force scaling # logy="auto", - # equality_check=numpy.allclose, # set to None to disable "correctness" assertion + # equality_check=np.allclose, # set to None to disable "correctness" assertion # show_progress=True, # target_time_per_measurement=1.0, # time_unit="s", # set to one of ("auto", "s", "ms", "us", or "ns") to force plot units @@ -59,7 +59,7 @@ for example: ```python out = perfplot.bench( # same arguments as above (except the plot-related ones, like time_unit or log*) - ) +) out.show() out.save("perf.png", transparent=True, bbox_inches="tight") ``` diff --git a/example/concat.py b/example/concat.py index 04e2b36..413aaad 100644 --- a/example/concat.py +++ b/example/concat.py @@ -1,15 +1,15 @@ -import numpy +import numpy as np import perfplot perfplot.show( - setup=numpy.random.rand, + setup=np.random.rand, kernels=[ - lambda a: numpy.c_[a, a], - lambda a: numpy.stack([a, a]).T, - lambda a: numpy.vstack([a, a]).T, - lambda a: numpy.column_stack([a, a]), - lambda a: numpy.concatenate([a[:, None], a[:, None]], axis=1), + lambda a: np.c_[a, a], + lambda a: np.stack([a, a]).T, + lambda a: np.vstack([a, a]).T, + lambda a: np.column_stack([a, a]), + lambda a: np.concatenate([a[:, None], a[:, None]], axis=1), ], labels=["c_", "stack", "vstack", "column_stack", "concat"], n_range=[2 ** k for k in range(15)], diff --git a/perfplot/main.py b/perfplot/main.py index f1b14c6..4abb295 100644 --- a/perfplot/main.py +++ b/perfplot/main.py @@ -6,7 +6,7 @@ import dufte import matplotlib import matplotlib.pyplot as plt -import numpy +import numpy as np from rich.console import Console from rich.progress import Progress from rich.table import Table @@ -60,7 +60,7 @@ def __init__( labels, xlabel, ): - self.n_range = numpy.asarray(n_range) + self.n_range = np.asarray(n_range) self.timings = timings self.flop = flop self.labels = labels @@ -75,14 +75,14 @@ def plot( # noqa: C901 ): if logx == "auto": # Check if the x values are approximately equally spaced in log - if numpy.any(self.n_range <= 0): + if np.any(self.n_range <= 0): logx = False else: - log_n_range = numpy.log(self.n_range) - diff = log_n_range - numpy.linspace( + log_n_range = np.log(self.n_range) + diff = log_n_range - np.linspace( log_n_range[0], log_n_range[-1], len(log_n_range) ) - logx = numpy.all(numpy.abs(diff) < 1.0e-5) + logx = np.all(np.abs(diff) < 1.0e-5) if logy == "auto": if relative_to is not None: @@ -106,7 +106,7 @@ def plot( # noqa: C901 # Set time unit of plots. # Allowed values: ("s", "ms", "us", "ns", "auto") if time_unit == "auto": - time_unit = _auto_time_unit(numpy.min(self.timings)) + time_unit = _auto_time_unit(np.min(self.timings)) else: assert time_unit in si_time, "Provided `time_unit` is not valid" @@ -169,7 +169,7 @@ def bench( labels=None, xlabel=None, target_time_per_measurement=1.0, - equality_check=numpy.allclose, + equality_check=np.allclose, show_progress=True, ): if labels is None: @@ -187,13 +187,13 @@ def bench( # Estimate the timer resolution by measuring a no-op. number = 100 noop_time = timeit.repeat(repeat=10, number=number, timer=timer) - resolution = numpy.min(noop_time) / number / si_time["ns"] + resolution = np.min(noop_time) / number / si_time["ns"] # round up to nearest integer resolution = -int(-resolution // 1) # typically around 10 (ns) - timings = numpy.empty((len(kernels), len(n_range)), dtype=numpy.uint64) + timings = np.empty((len(kernels), len(n_range)), dtype=np.uint64) - flop = None if flops is None else numpy.array([flops(n) for n in n_range]) + flop = None if flops is None else np.array([flops(n) for n in n_range]) with Progress() as progress: try: @@ -268,7 +268,7 @@ def _b(data, kernel, repeat, timer, is_ns_timer, resolution): tm = None while min_timing <= required_timing: - tm = numpy.array( + tm = np.array( timeit.repeat( stmt=lambda: kernel(data), repeat=repeat, number=number, timer=timer ) @@ -276,7 +276,7 @@ def _b(data, kernel, repeat, timer, is_ns_timer, resolution): if not is_ns_timer: tm /= si_time["ns"] tm = tm.astype(int) - min_timing = numpy.min(tm) + min_timing = np.min(tm) # plt.title("number={} repeat={}".format(number, repeat)) # plt.semilogy(tm) # # plt.hist(tm) @@ -303,7 +303,7 @@ def _b(data, kernel, repeat, timer, is_ns_timer, resolution): assert tm is not None # Only return the minimum time; everthing else just measures how slow the system can # go. - return numpy.min(tm), numpy.sum(tm) + return np.min(tm), np.sum(tm) # For backward compatibility: diff --git a/test/perfplot_test.py b/test/perfplot_test.py index 511c031..c0c0ef7 100644 --- a/test/perfplot_test.py +++ b/test/perfplot_test.py @@ -1,15 +1,15 @@ -import numpy +import numpy as np import pytest import perfplot -kernels = [lambda a: numpy.c_[a, a]] +kernels = [lambda a: np.c_[a, a]] r = [2 ** k for k in range(4)] def test(): perfplot.show( - setup=numpy.random.rand, + setup=np.random.rand, kernels=kernels, labels=["c_"], n_range=r, @@ -17,7 +17,7 @@ def test(): ) perfplot.show( - setup=numpy.random.rand, + setup=np.random.rand, kernels=kernels, labels=["c_"], n_range=r, @@ -27,7 +27,7 @@ def test(): ) out = perfplot.bench( - setup=numpy.random.rand, + setup=np.random.rand, kernels=kernels, labels=["c_"], n_range=r, @@ -36,7 +36,7 @@ def test(): print(out) perfplot.show( - setup=numpy.random.rand, + setup=np.random.rand, kernels=kernels, labels=["c_"], n_range=r, @@ -47,7 +47,7 @@ def test(): def test_no_labels(): - perfplot.plot(setup=numpy.random.rand, kernels=kernels, n_range=r, xlabel="len(a)") + perfplot.plot(setup=np.random.rand, kernels=kernels, n_range=r, xlabel="len(a)") # (expected_unit, time in nanoseconds, expected_timing, time_unit) format @@ -79,7 +79,7 @@ def test_automatic_scale(exp_unit, time_ns, time_unit): data = PerfplotData( n_range=[1], # Converting timings to ns - timings=numpy.full((1, 1), time_ns, dtype=numpy.uint64), + timings=np.full((1, 1), time_ns, dtype=np.uint64), labels=["."], # Suppress no handle error # TODO fix this xlabel="", flop=None, @@ -94,7 +94,7 @@ def test_automatic_scale(exp_unit, time_ns, time_unit): def test_save(): perfplot.save( "out.png", - setup=numpy.random.rand, + setup=np.random.rand, kernels=kernels, n_range=r, xlabel="len(a)", @@ -104,7 +104,7 @@ def test_save(): def test_flops(): perfplot.show( - setup=numpy.random.rand, + setup=np.random.rand, kernels=kernels, labels=["c_"], n_range=r,