Skip to content

Commit

Permalink
apply ruff to experiments, examples, benchmarks, scripts as well
Browse files Browse the repository at this point in the history
  • Loading branch information
grlee77 committed Oct 24, 2023
1 parent 9919b82 commit 10c3a44
Show file tree
Hide file tree
Showing 24 changed files with 85 additions and 83 deletions.
16 changes: 13 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,30 @@ conda install isort black ruff
pip install isort black ruff
```

These tools are used to auto-format the Python code in the repository. Additionally, there is a CI check in place to enforce that the committed code follows our standards. You can use the tools to automatically format your python code by running:
These tools are used to auto-format the Python code in the repository. Additionally, there is a CI check in place to enforce that the committed code follows our standards. To run only for the python/cucim folder, change to that folder and run

```bash
isort .
black .
ruff .
```

To also check formatting in top-level folders like `benchmarks`, `examples` and `experiments`, these tools can also be run from the top level of the repository as follows:

```bash
isort --settings-path="python/cucim/pyproject.toml" .
ruff python/cucim
black --config python/cucim/pyproject.toml .
ruff .
```

In addition to these tools, [codespell]() can be used to help diagnose and interactively fix spelling errors in both Python and C++ code. It can also be run from the top level of the repository in interactive mode using:
In addition to these tools, [codespell](https://github.com/codespell-project/codespell) can be used to help diagnose and interactively fix spelling errors in both Python and C++ code. It can also be run from the top level of the repository in interactive mode using:

```bash
codespell --toml python/cucim/pyproject.toml . -i 3 -w
```

If codespell is finding false positives in newly added code, the `ignore-words-list` entry of the `tool.codespell` section in `pyproject.toml` can be updated as needed.

### Get libcucim Dependencies

Compiler requirements:
Expand Down
3 changes: 1 addition & 2 deletions benchmarks/skimage/_image_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def __init__(
fixed_kwargs={},
var_kwargs={},
index_str=None, # extra string to append to dataframe index
# set_args_kwargs={}, # for passing additional arguments to custom set_args method
module_cpu=scipy.ndimage,
module_gpu=cupyx.scipy.ndimage,
function_is_generator=False,
Expand Down Expand Up @@ -181,7 +180,7 @@ def run_benchmark(self, duration=3, verbose=True):
df.at[index, "dtype"] = np.dtype(dtype).name
df.at[index, "ndim"] = len(self.shape)

if self.run_cpu == True:
if self.run_cpu is True:
perf = repeat(
self.func_cpu, self.args_cpu, kw_cpu, **rep_kwargs_cpu
)
Expand Down
16 changes: 7 additions & 9 deletions benchmarks/skimage/bench_convolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
"""
import cupy as cp
import cupyx.scipy.ndimage as ndi
import pytest
from cupyx.profiler import benchmark

from cucim.skimage._vendored.ndimage import (
from cucim.skimage._vendored.ndimage import ( # noqa: F401
convolve1d,
correlate1d,
gaussian_filter,
Expand Down Expand Up @@ -134,7 +133,6 @@ def _compare_implementations_other(
if output_dtype is None:
output_dtype = image.dtype
output1 = cp.empty(image.shape, dtype=output_dtype)
output2 = cp.empty(image.shape, dtype=output_dtype)
kwargs.update(dict(output=output1))
perf1 = benchmark(
function_ref,
Expand Down Expand Up @@ -177,10 +175,10 @@ def _compare_implementations_other(

print("\n\n")
print(
"function | shape | dtype | mode | kernel size | preallocated | axis | dur (ms), CuPy | dur (ms), cuCIM | acceleration "
"function | shape | dtype | mode | kernel size | preallocated | axis | dur (ms), CuPy | dur (ms), cuCIM | acceleration " # noqa: E501
)
print(
"---------|-------|-------|------|-------------|--------------|------|----------------|-----------------|--------------"
"---------|-------|-------|------|-------------|--------------|------|----------------|-----------------|--------------" # noqa: E501
)
for function in [convolve1d]:
for shape in [(512, 512), (3840, 2160), (64, 64, 64), (256, 256, 256)]:
Expand All @@ -203,15 +201,15 @@ def _compare_implementations_other(
t_elem = perf1.gpu_times * 1000.0
t_shared = perf2.gpu_times * 1000.0
print(
f"{function.__name__} | {shape} | {cp.dtype(dtype).name} | {mode} | {kernel_size=} | prealloc={output_preallocated} | {axis=} | {t_elem.mean():0.3f} +/- {t_elem.std():0.3f} | {t_shared.mean():0.3f} +/- {t_shared.std():0.3f} | {t_elem.mean() / t_shared.mean():0.3f}"
f"{function.__name__} | {shape} | {cp.dtype(dtype).name} | {mode} | {kernel_size=} | prealloc={output_preallocated} | {axis=} | {t_elem.mean():0.3f} +/- {t_elem.std():0.3f} | {t_shared.mean():0.3f} +/- {t_shared.std():0.3f} | {t_elem.mean() / t_shared.mean():0.3f}" # noqa: E501
)


print(
"function | kwargs | shape | dtype | mode | preallocated | dur (ms), CuPy | dur (ms), cuCIM | acceleration "
"function | kwargs | shape | dtype | mode | preallocated | dur (ms), CuPy | dur (ms), cuCIM | acceleration " # noqa: E501
)
print(
"---------|--------|-------|-------|------|--------------|----------------|-----------------|--------------"
"---------|--------|-------|-------|------|--------------|----------------|-----------------|--------------" # noqa: E501
)
for function, func_kwargs in [
# (gaussian_filter1d, dict(sigma=1.0, axis=0)),
Expand Down Expand Up @@ -245,5 +243,5 @@ def _compare_implementations_other(
t_elem = perf1.gpu_times * 1000.0
t_shared = perf2.gpu_times * 1000.0
print(
f"{function.__name__} | {func_kwargs} | {shape} | {cp.dtype(dtype).name} | {mode} | {output_preallocated} | {t_elem.mean():0.3f} +/- {t_elem.std():0.3f} | {t_shared.mean():0.3f} +/- {t_shared.std():0.3f} | {t_elem.mean() / t_shared.mean():0.3f}"
f"{function.__name__} | {func_kwargs} | {shape} | {cp.dtype(dtype).name} | {mode} | {output_preallocated} | {t_elem.mean():0.3f} +/- {t_elem.std():0.3f} | {t_shared.mean():0.3f} +/- {t_shared.std():0.3f} | {t_elem.mean() / t_shared.mean():0.3f}" # noqa: E501
)
4 changes: 1 addition & 3 deletions benchmarks/skimage/cucim_color_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ def main(args):

all_colorspaces = False

ndim = len(shape)

for function_name in func_name_choices:
if function_name != args.func_name:
continue
Expand Down Expand Up @@ -224,7 +222,7 @@ def main(args):
all_results.to_csv(fbase + ".csv")
all_results.to_pickle(pfile)
try:
import tabular
import tabular # noqa: F401

with open(fbase + ".md", "wt") as f:
f.write(all_results.to_markdown())
Expand Down
5 changes: 2 additions & 3 deletions benchmarks/skimage/cucim_exposure_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ def main(args):
if function_name != args.func_name:
continue

ndim = len(shape)

if function_name == "match_histograms":
channel_axis = -1 if shape[-1] in [3, 4] else None

Expand All @@ -106,7 +104,8 @@ def main(args):
continue

if function_name == "equalize_adapthist":
# TODO: fix equalize_adapthist for size (3840, 2160) and kernel_size = [16, 16]
# TODO: fix equalize_adapthist for size (3840, 2160)
# and kernel_size = [16, 16]
size_factors = [4, 8, 16]
kernel_sizes = []
for size_factor in size_factors:
Expand Down
9 changes: 4 additions & 5 deletions benchmarks/skimage/cucim_feature_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ def set_args(self, dtype):
locs = np.unravel_index(offsets, img.shape)
for loc in zip(*locs):
loc = tuple(
min(l, s - es) for l, s, es in zip(loc, img.shape, e.shape)
min(p, s - es) for p, s, es in zip(loc, img.shape, e.shape)
)
sl = tuple(slice(l, l + es) for l, es in zip(loc, e.shape))
sl = tuple(slice(p, p + es) for p, es in zip(loc, e.shape))
img[sl] = e
else:
raise NotImplementedError("unsupported ndim")
Expand Down Expand Up @@ -82,7 +82,8 @@ def main(args):
True,
),
("canny", dict(sigma=1.8), dict(), False, False),
# reduced default rings, histograms, orientations to fit daisy at (3840, 2160) into GPU memory
# reduced default rings, histograms, orientations to fit daisy at
# (3840, 2160) into GPU memory
(
"daisy",
dict(step=4, radius=15, rings=2, histograms=5, orientations=4),
Expand Down Expand Up @@ -152,8 +153,6 @@ def main(args):
else:
continue

# if function_name in ["corner_peaks", "peak_local_max"] and np.prod(shape) > 1000000:
# skip any large sizes that take too long
ndim = len(shape)
run_cpu = not args.no_cpu
if not allow_nd:
Expand Down
3 changes: 2 additions & 1 deletion benchmarks/skimage/cucim_filters_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ def main(args):
# lpi_filter.py
# TODO: benchmark wiener
# ridges.py
# TODO: had to set meijering, etc allow_nd to False just due to insufficient GPU memory
# TODO: Had to set meijering, etc allow_nd to False just due to
# insufficient GPU memory
(
"meijering",
dict(sigmas=range(1, 10, 2), alpha=None),
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/skimage/cucim_measure_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def main(args):

for contiguous_labels in [True, False]:
if contiguous_labels:
index_str = f"contiguous"
index_str = "contiguous"
else:
index_str = None
B = Tester(
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/skimage/cucim_morphology_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def main(args):
all_results.to_csv(fbase + ".csv")
all_results.to_pickle(pfile)
try:
import tabular
import tabular # noqa: F401

with open(fbase + ".md", "wt") as f:
f.write(all_results.to_markdown())
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/skimage/cucim_registration_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def main(args):
all_results.to_csv(fbase + ".csv")
all_results.to_pickle(pfile)
try:
import tabular
import tabular # noqa: F401

with open(fbase + ".md", "wt") as f:
f.write(all_results.to_markdown())
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/skimage/cucim_restoration_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def main(args):
all_results.to_csv(fbase + ".csv")
all_results.to_pickle(pfile)
try:
import tabular
import tabular # noqa: F401

with open(fbase + ".md", "wt") as f:
f.write(all_results.to_markdown())
Expand Down
6 changes: 4 additions & 2 deletions benchmarks/skimage/cucim_segmentation_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from _image_bench import ImageBench

import cucim.skimage
from cucim.skimage import data, exposure, measure, segmentation
from cucim.skimage import data, measure


class LabelBench(ImageBench):
Expand Down Expand Up @@ -262,6 +262,7 @@ def main(args):
var_kwargs=var_kwargs,
module_cpu=skimage.segmentation,
module_gpu=cucim.skimage.segmentation,
run_cpu=run_cpu,
)
results = B.run_benchmark(duration=args.duration)
all_results = pd.concat([all_results, results["full"]])
Expand All @@ -285,6 +286,7 @@ def main(args):
var_kwargs=var_kwargs,
module_cpu=skimage.segmentation,
module_gpu=cucim.skimage.segmentation,
run_cpu=run_cpu,
)
results = B.run_benchmark(duration=args.duration)
all_results = pd.concat([all_results, results["full"]])
Expand All @@ -293,7 +295,7 @@ def main(args):
all_results.to_csv(fbase + ".csv")
all_results.to_pickle(pfile)
try:
import tabular
import tabular # noqa: F401

with open(fbase + ".md", "wt") as f:
f.write(all_results.to_markdown())
Expand Down
3 changes: 2 additions & 1 deletion benchmarks/skimage/cucim_transform_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def main(args):
module_cpu=skimage.transform,
module_gpu=cucim.skimage.transform,
function_is_generator=function_is_generator,
run_cpu=run_cpu,
)
results = B.run_benchmark(duration=args.duration)
all_results = pd.concat([all_results, results["full"]])
Expand All @@ -162,7 +163,7 @@ def main(args):
all_results.to_csv(fbase + ".csv")
all_results.to_pickle(pfile)
try:
import tabular
import tabular # noqa: F401

with open(fbase + ".md", "wt") as f:
f.write(all_results.to_markdown())
Expand Down
1 change: 0 additions & 1 deletion benchmarks/skimage/cupyx_scipy_ndimage_morphology_bench.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import math
import os
import pickle

Expand Down
2 changes: 1 addition & 1 deletion examples/python/gds_whole_slide/benchmark_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,4 @@
duration (kvikio-pread (n_buffer=16) (gds_enabled=True)) = 0.9027577819824221
duration (kvikio-pread (n_buffer=64) (gds_enabled=True)) = 0.7827104492187501
duration (kvikio-pread (n_buffer=256) (gds_enabled=True)) = 0.756464599609375
"""
""" # noqa: E501
4 changes: 2 additions & 2 deletions examples/python/gds_whole_slide/benchmark_round_trip.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def round_trip(
"tiled" if apply_kernel_tilewise else "global"
)
gds_description = "with GDS" if gds_enabled else "without GDS"
label = f"{computation=}, {kernel_description}, chunk_shape={zarr_chunk_shape}, {gds_description}"
label = f"{computation=}, {kernel_description}, chunk_shape={zarr_chunk_shape}, {gds_description}" # noqa: E501
print(f"Duration ({label}): {t.mean()} s +/- {t.std()} s")

times.append(t.mean())
Expand Down Expand Up @@ -242,4 +242,4 @@ def round_trip(
Duration (computation='sobel', tiled, chunk_shape=(4096, 4096, 3), without GDS): 5.430656982421875 s +/- 0.01277270507812478 s
Duration (computation='sobel', tiled, chunk_shape=(4096, 4096, 3), with GDS): 3.1940713500976563 s +/- 0.19852391299904837 s
"""
""" # noqa: E501
2 changes: 1 addition & 1 deletion examples/python/gds_whole_slide/benchmark_zarr_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
write_time_means[i, j, k, m] = t.mean()
write_time_stds[i, j, k, m] = t.std()
print(
f"Duration ({cp.dtype(dtype).name} write, {chunk_shape=}, {backend=}, {gds_enabled=}): "
f"Duration ({cp.dtype(dtype).name} write, {chunk_shape=}, {backend=}, {gds_enabled=}): " # noqa: E501
f"{t.mean()} s +/- {t.std()} s"
)
out_name = "write_times.npz"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
from lz4_nvcomp import LZ4NVCOMP
from tifffile import TiffFile

from cucim.core.operations.color import image_to_absorbance

data_dir = os.environ.get("WHOLE_SLIDE_DATA_DIR", os.path.dirname("__file__"))
fname = os.path.join(data_dir, "resize.tiff")
if not os.path.exists(fname):
Expand Down Expand Up @@ -55,7 +53,7 @@

# benchmark writing these CuPy outputs to Zarr with various chunk sizes
# Note: nvcomp only supports integer and unsigned dtypes.
# https://github.com/rapidsai/kvikio/blob/b0c6cedf43d1bc240c3ef1b38ebb9d89574a08ee/python/kvikio/nvcomp.py#L12-L21 # noqa
# https://github.com/rapidsai/kvikio/blob/b0c6cedf43d1bc240c3ef1b38ebb9d89574a08ee/python/kvikio/nvcomp.py#L12-L21 # noqa: E501

dtypes = ["uint16"]
chunk_shapes = [
Expand Down Expand Up @@ -107,7 +105,7 @@
write_time_means[i, j, k, m] = t.mean()
write_time_stds[i, j, k, m] = t.std()
print(
f"Duration ({cp.dtype(dtype).name} write, {chunk_shape=}, {compressor=}, {gds_enabled=}): "
f"Duration ({cp.dtype(dtype).name} write, {chunk_shape=}, {compressor=}, {gds_enabled=}): " # noqa: E501
f"{t.mean()} s +/- {t.std()} s"
)

Expand Down Expand Up @@ -171,4 +169,4 @@
Duration (uint16 write, chunk_shape=(4096, 4096, 3), compressor=LZ4NVCOMP, gds_enabled=False): 0.8166022583007813 s +/- 0.19712617258152443 s
Duration (uint16 write, chunk_shape=(4096, 4096, 3), compressor=LZ4NVCOMP, gds_enabled=True): 0.7342889607747396 s +/- 0.025796103217999546 s
"""
""" # noqa: E501
Loading

0 comments on commit 10c3a44

Please sign in to comment.