Skip to content

Commit

Permalink
Use setdefault to handle default kwargs more cleanly (#360)
Browse files Browse the repository at this point in the history
## Description

<!-- Provide a brief description of the PR's purpose here. -->

Using setdefault is shorter than checking "if X not in kwargs..."

## TODO

<!-- Notable points that this PR has either accomplished or will
accomplish. -->

## Questions

<!-- Any concerns or points of confusion? -->

## Status

- [x] I have read the guidelines in

[CONTRIBUTING.md](https://github.com/icaros-usc/pyribs/blob/master/CONTRIBUTING.md)
- [x] I have formatted my code using `yapf`
- [x] I have tested my code by running `pytest`
- [x] I have linted my code with `pylint`
- [N/A] I have added a one-line description of my change to the
changelog in
      `HISTORY.md`
- [x] This PR is ready to go
  • Loading branch information
btjanaka authored Sep 7, 2023
1 parent a064a18 commit 847cf6e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
18 changes: 9 additions & 9 deletions ribs/archives/_cvt_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,16 @@ def __init__(self,
# particularly if they want higher quality clusters.
self._k_means_kwargs = ({} if k_means_kwargs is None else
k_means_kwargs.copy())
if "n_init" not in self._k_means_kwargs:
self._k_means_kwargs.setdefault(
# Only run one iter to be fast.
self._k_means_kwargs["n_init"] = 1
if "init" not in self._k_means_kwargs:
# The default, "k-means++", takes very long to init.
self._k_means_kwargs["init"] = "random"
if "algorithm" not in self._k_means_kwargs:
self._k_means_kwargs["algorithm"] = "lloyd"
if "random_state" not in self._k_means_kwargs:
self._k_means_kwargs["random_state"] = seed
"n_init",
1)
self._k_means_kwargs.setdefault(
# The default "k-means++" takes very long to init.
"init",
"random")
self._k_means_kwargs.setdefault("algorithm", "lloyd")
self._k_means_kwargs.setdefault("random_state", seed)

self._use_kd_tree = use_kd_tree
self._centroid_kd_tree = None
Expand Down
8 changes: 3 additions & 5 deletions ribs/visualize/_parallel_axes_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,9 @@ def parallel_axes_plot(archive,
mappable = ScalarMappable(cmap=cmap)
mappable.set_clim(vmin, vmax)

# Default colorbar settings.
# Add default colorbar settings.
cbar_kwargs = {} if cbar_kwargs is None else cbar_kwargs.copy()
if "orientation" not in cbar_kwargs:
cbar_kwargs["orientation"] = "horizontal"
if "pad" not in cbar_kwargs:
cbar_kwargs["pad"] = 0.1
cbar_kwargs.setdefault("orientation", "horizontal")
cbar_kwargs.setdefault("pad", 0.1)

set_cbar(mappable, host_ax, cbar, cbar_kwargs)

0 comments on commit 847cf6e

Please sign in to comment.