Skip to content

Commit

Permalink
import numpy as np
Browse files Browse the repository at this point in the history
  • Loading branch information
nschloe committed Feb 10, 2021
1 parent 461658a commit 00b840d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 40 deletions.
1 change: 1 addition & 0 deletions Makefile
Expand Up @@ -22,6 +22,7 @@ clean:
format:
isort .
black .
blacken-docs README.md

lint:
black --check .
Expand Down
18 changes: 9 additions & 9 deletions README.md
Expand Up @@ -18,25 +18,25 @@ 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)],
xlabel="len(a)",
# 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
Expand All @@ -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")
```
Expand Down
14 changes: 7 additions & 7 deletions 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)],
Expand Down
28 changes: 14 additions & 14 deletions perfplot/main.py
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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"

Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -268,15 +268,15 @@ 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
)
)
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)
Expand All @@ -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:
Expand Down
20 changes: 10 additions & 10 deletions test/perfplot_test.py
@@ -1,23 +1,23 @@
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,
xlabel="len(a)",
)

perfplot.show(
setup=numpy.random.rand,
setup=np.random.rand,
kernels=kernels,
labels=["c_"],
n_range=r,
Expand All @@ -27,7 +27,7 @@ def test():
)

out = perfplot.bench(
setup=numpy.random.rand,
setup=np.random.rand,
kernels=kernels,
labels=["c_"],
n_range=r,
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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)",
Expand All @@ -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,
Expand Down

0 comments on commit 00b840d

Please sign in to comment.