Skip to content
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

NF: Add keyword_only decorator to enforce keyword-only arguments #888

Merged
merged 11 commits into from
Jul 10, 2024
Merged
45 changes: 25 additions & 20 deletions fury/actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,12 @@ def shallow_copy(self):
lut = lookup_colormap
if lookup_colormap is None:
# Create a black/white lookup table.
lut = colormap_lookup_table((r1, r2), (0, 0), (0, 0), (0, 1))
lut = colormap_lookup_table(
scale_range=(r1, r2),
hue_range=(0, 0),
saturation_range=(0, 0),
value_range=(0, 1),
)

plane_colors = ImageMapToColors()
plane_colors.SetOutputFormatToRGB()
Expand Down Expand Up @@ -687,7 +692,7 @@ def streamtube(

"""
# Poly data with lines and colors
poly_data, color_is_scalar = lines_to_vtk_polydata(lines, colors)
poly_data, color_is_scalar = lines_to_vtk_polydata(lines, colors=colors)
next_input = poly_data

# set primitives count
Expand Down Expand Up @@ -838,7 +843,7 @@ def line(

"""
# Poly data with lines and colors
poly_data, color_is_scalar = lines_to_vtk_polydata(lines, colors)
poly_data, color_is_scalar = lines_to_vtk_polydata(lines, colors=colors)
next_input = poly_data

# set primitives count
Expand Down Expand Up @@ -1032,7 +1037,7 @@ def odf_slicer(

if sphere is None:
# Use a default sphere with 100 vertices
vertices, faces = fp.prim_sphere("repulsion100")
vertices, faces = fp.prim_sphere(name="repulsion100")
else:
vertices = sphere.vertices
faces = fix_winding_order(vertices, sphere.faces, clockwise=True)
Expand Down Expand Up @@ -1699,7 +1704,7 @@ def dot(points, colors=None, opacity=None, dot_size=5):
vtk_faces.InsertNextCell(1)
vtk_faces.InsertCellPoint(idd)

color_tuple = color_check(len(points), colors)
color_tuple = color_check(len(points), colors=colors)
color_array, global_opacity = color_tuple

# Create a polydata object
Expand Down Expand Up @@ -1863,7 +1868,7 @@ def sphere(
big_verts, big_faces, big_colors, _ = res
prim_count = len(centers)
sphere_actor = get_actor_from_primitive(
big_verts, big_faces, big_colors, prim_count=prim_count
big_verts, big_faces, colors=big_colors, prim_count=prim_count
)
sphere_actor.GetProperty().SetOpacity(opacity)
return sphere_actor
Expand Down Expand Up @@ -1948,7 +1953,7 @@ def cylinder(
big_verts, big_faces, big_colors, _ = res
prim_count = len(centers)
cylinder_actor = get_actor_from_primitive(
big_verts, big_faces, big_colors, prim_count=prim_count
big_verts, big_faces, colors=big_colors, prim_count=prim_count
)

else:
Expand Down Expand Up @@ -2095,7 +2100,7 @@ def square(centers, directions=(1, 0, 0), colors=(1, 0, 0), scales=1):
big_verts, big_faces, big_colors, _ = res
prim_count = len(centers)
sq_actor = get_actor_from_primitive(
big_verts, big_faces, big_colors, prim_count=prim_count
big_verts, big_faces, colors=big_colors, prim_count=prim_count
)
sq_actor.GetProperty().BackfaceCullingOff()
return sq_actor
Expand Down Expand Up @@ -2180,7 +2185,7 @@ def box(centers, directions=(1, 0, 0), colors=(1, 0, 0), scales=(1, 2, 3)):
big_verts, big_faces, big_colors, _ = res
prim_count = len(centers)
box_actor = get_actor_from_primitive(
big_verts, big_faces, big_colors, prim_count=prim_count
big_verts, big_faces, colors=big_colors, prim_count=prim_count
)
return box_actor

Expand Down Expand Up @@ -2288,7 +2293,7 @@ def arrow(
big_vertices, big_faces, big_colors, _ = res
prim_count = len(centers)
arrow_actor = get_actor_from_primitive(
big_vertices, big_faces, big_colors, prim_count=prim_count
big_vertices, big_faces, colors=big_colors, prim_count=prim_count
)
return arrow_actor

Expand Down Expand Up @@ -2389,7 +2394,7 @@ def cone(
big_verts, big_faces, big_colors, _ = res
prim_count = len(centers)
cone_actor = get_actor_from_primitive(
big_verts, big_faces, big_colors, prim_count=prim_count
big_verts, big_faces, colors=big_colors, prim_count=prim_count
)

return cone_actor
Expand Down Expand Up @@ -2438,7 +2443,7 @@ def triangularprism(centers, directions=(1, 0, 0), colors=(1, 0, 0), scales=1):
big_verts, big_faces, big_colors, _ = res
prim_count = len(centers)
tprism_actor = get_actor_from_primitive(
big_verts, big_faces, big_colors, prim_count=prim_count
big_verts, big_faces, colors=big_colors, prim_count=prim_count
)
return tprism_actor

Expand Down Expand Up @@ -2486,7 +2491,7 @@ def rhombicuboctahedron(centers, directions=(1, 0, 0), colors=(1, 0, 0), scales=
big_verts, big_faces, big_colors, _ = res
prim_count = len(centers)
rcoh_actor = get_actor_from_primitive(
big_verts, big_faces, big_colors, prim_count=prim_count
big_verts, big_faces, colors=big_colors, prim_count=prim_count
)
return rcoh_actor

Expand Down Expand Up @@ -2536,7 +2541,7 @@ def pentagonalprism(centers, directions=(1, 0, 0), colors=(1, 0, 0), scales=1):
big_verts, big_faces, big_colors, _ = res
prim_count = len(centers)
pent_actor = get_actor_from_primitive(
big_verts, big_faces, big_colors, prim_count=prim_count
big_verts, big_faces, colors=big_colors, prim_count=prim_count
)
return pent_actor

Expand Down Expand Up @@ -2585,7 +2590,7 @@ def octagonalprism(centers, directions=(1, 0, 0), colors=(1, 0, 0), scales=1):
big_verts, big_faces, big_colors, _ = res
prim_count = len(centers)
oct_actor = get_actor_from_primitive(
big_verts, big_faces, big_colors, prim_count=prim_count
big_verts, big_faces, colors=big_colors, prim_count=prim_count
)
return oct_actor

Expand Down Expand Up @@ -2634,7 +2639,7 @@ def frustum(centers, directions=(1, 0, 0), colors=(0, 1, 0), scales=1):
big_verts, big_faces, big_colors, _ = res
prim_count = len(centers)
frustum_actor = get_actor_from_primitive(
big_verts, big_faces, big_colors, prim_count=prim_count
big_verts, big_faces, colors=big_colors, prim_count=prim_count
)
return frustum_actor

Expand Down Expand Up @@ -2705,7 +2710,7 @@ def have_2_dimensions(arr):
big_verts, big_faces, big_colors, _ = res
prim_count = len(centers)
spq_actor = get_actor_from_primitive(
big_verts, big_faces, big_colors, prim_count=prim_count
big_verts, big_faces, colors=big_colors, prim_count=prim_count
)
return spq_actor

Expand Down Expand Up @@ -2766,7 +2771,7 @@ def billboard(

prim_count = len(centers)
bb_actor = get_actor_from_primitive(
big_verts, big_faces, big_colors, prim_count=prim_count
big_verts, big_faces, colors=big_colors, prim_count=prim_count
)
bb_actor.GetMapper().SetVBOShiftScaleMethod(False)
bb_actor.GetProperty().BackfaceCullingOff()
Expand Down Expand Up @@ -3597,7 +3602,7 @@ def sdf(centers, directions=(1, 0, 0), colors=(1, 0, 0), primitives="torus", sca
rep_verts, rep_faces, rep_colors, rep_centers = repeated
prim_count = len(centers)
box_actor = get_actor_from_primitive(
rep_verts, rep_faces, rep_colors, prim_count=prim_count
rep_verts, rep_faces, colors=rep_colors, prim_count=prim_count
)
box_actor.GetMapper().SetVBOShiftScaleMethod(False)

Expand Down Expand Up @@ -3707,7 +3712,7 @@ def markers(
big_verts, big_faces, big_colors, big_centers = res
prim_count = len(centers)
sq_actor = get_actor_from_primitive(
big_verts, big_faces, big_colors, prim_count=prim_count
big_verts, big_faces, colors=big_colors, prim_count=prim_count
)
sq_actor.GetMapper().SetVBOShiftScaleMethod(False)
sq_actor.GetProperty().BackfaceCullingOff()
Expand Down
4 changes: 2 additions & 2 deletions fury/actors/odf_slicer.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,14 @@ def _generate_color_for_vertices(self, sf):
if self.colormap is None:
raise IOError("if global_cm=True, colormap must be defined.")
else:
all_colors = create_colormap(sf.ravel(), self.colormap) * 255
all_colors = create_colormap(sf.ravel(), name=self.colormap) * 255
elif self.colormap is not None:
if isinstance(self.colormap, str):
# Map ODFs values [min, max] to [0, 1] for each ODF
range_sf = sf.max(axis=-1) - sf.min(axis=-1)
rescaled = sf - sf.min(axis=-1, keepdims=True)
rescaled[range_sf > 0] /= range_sf[range_sf > 0][..., None]
all_colors = create_colormap(rescaled.ravel(), self.colormap) * 255
all_colors = create_colormap(rescaled.ravel(), name=self.colormap) * 255
else:
all_colors = np.tile(
np.array(self.colormap).reshape(1, 3),
Expand Down
28 changes: 19 additions & 9 deletions fury/colormap.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from scipy import linalg

from fury.data import DATA_DIR
from fury.decorators import warn_on_args_to_kwargs
from fury.lib import LookupTable

# Allow import, but disable doctests if we don't have matplotlib
Expand All @@ -14,7 +15,9 @@
cm, have_matplotlib, _ = optional_package("matplotlib.cm")


@warn_on_args_to_kwargs()
def colormap_lookup_table(
*,
scale_range=(0, 1),
hue_range=(0.8, 0),
saturation_range=(1, 1),
Expand Down Expand Up @@ -242,7 +245,8 @@ def orient2rgb(v):
return orient


def line_colors(streamlines, cmap="rgb_standard"):
@warn_on_args_to_kwargs()
def line_colors(streamlines, *, cmap="rgb_standard"):
"""Create colors for streamlines to be used in actor.line.

Parameters
Expand Down Expand Up @@ -308,7 +312,8 @@ def simple_cmap(v):
return simple_cmap


def create_colormap(v, name="plasma", auto=True):
@warn_on_args_to_kwargs()
def create_colormap(v, *, name="plasma", auto=True):
"""Create colors from a specific colormap and return it
as an array of shape (N,3) where every row gives the corresponding
r,g,b value. The colormaps we use are similar with those of matplotlib.
Expand Down Expand Up @@ -511,7 +516,8 @@ def _lab2rgb(lab):
return _xyz2rgb(tmp)


def distinguishable_colormap(bg=(0, 0, 0), exclude=None, nb_colors=None):
@warn_on_args_to_kwargs()
def distinguishable_colormap(*, bg=(0, 0, 0), exclude=None, nb_colors=None):
"""Generate colors that are maximally perceptually distinct.

This function generates a set of colors which are distinguishable
Expand Down Expand Up @@ -903,7 +909,8 @@ def get_xyz_coords(illuminant, observer):
) from err


def xyz2lab(xyz, illuminant="D65", observer="2"):
@warn_on_args_to_kwargs()
def xyz2lab(xyz, *, illuminant="D65", observer="2"):
"""XYZ to CIE-LAB color space conversion.

Parameters
Expand Down Expand Up @@ -950,7 +957,8 @@ def xyz2lab(xyz, illuminant="D65", observer="2"):
return np.concatenate([x[..., np.newaxis] for x in [L, a, b]], axis=-1)


def lab2xyz(lab, illuminant="D65", observer="2"):
@warn_on_args_to_kwargs()
def lab2xyz(lab, *, illuminant="D65", observer="2"):
"""CIE-LAB to XYZcolor space conversion.

Parameters
Expand Down Expand Up @@ -1001,7 +1009,8 @@ def lab2xyz(lab, illuminant="D65", observer="2"):
return out


def rgb2lab(rgb, illuminant="D65", observer="2"):
@warn_on_args_to_kwargs()
def rgb2lab(rgb, *, illuminant="D65", observer="2"):
"""Conversion from the sRGB color space (IEC 61966-2-1:1999)
to the CIE Lab colorspace under the given illuminant and observer.

Expand All @@ -1028,10 +1037,11 @@ def rgb2lab(rgb, illuminant="D65", observer="2"):
This implementation might have been modified.

"""
return xyz2lab(rgb2xyz(rgb), illuminant, observer)
return xyz2lab(rgb2xyz(rgb), illuminant=illuminant, observer=observer)


def lab2rgb(lab, illuminant="D65", observer="2"):
@warn_on_args_to_kwargs()
def lab2rgb(lab, *, illuminant="D65", observer="2"):
"""Lab to RGB color space conversion.

Parameters
Expand All @@ -1057,4 +1067,4 @@ def lab2rgb(lab, illuminant="D65", observer="2"):
This implementation might have been modified.

"""
return xyz2rgb(lab2xyz(lab, illuminant, observer))
return xyz2rgb(lab2xyz(lab, illuminant=illuminant, observer=observer))
10 changes: 8 additions & 2 deletions fury/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

import numpy as np

from fury.decorators import warn_on_args_to_kwargs
from fury.io import load_image


@warn_on_args_to_kwargs()
def matplotlib_figure_to_numpy(
fig, dpi=100, fname=None, flip_up_down=True, transparent=False
fig, *, dpi=100, fname=None, flip_up_down=True, transparent=False
):
"""Convert a Matplotlib figure to a 3D numpy array with RGBA channels.
Expand Down Expand Up @@ -54,7 +56,11 @@ def matplotlib_figure_to_numpy(
arr = load_image(fname)
else:
fig.savefig(
fname, dpi=dpi, transparent=transparent, bbox_inches="tight", pad_inches=0
fname,
dpi=dpi,
transparent=transparent,
bbox_inches="tight",
pad_inches=0,
)
arr = load_image(fname)

Expand Down
Loading
Loading