Skip to content

Commit

Permalink
refactor: kws->kwargs
Browse files Browse the repository at this point in the history
  • Loading branch information
martibosch committed Mar 28, 2024
1 parent 853d9ad commit 0c9da36
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 48 deletions.
30 changes: 15 additions & 15 deletions detectree/classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __init__(
tree_val=None,
nontree_val=None,
classifier_class=None,
**classifier_kws,
**classifier_kwargs,
):
"""
Initialize the classifier.
Expand Down Expand Up @@ -83,27 +83,27 @@ def __init__(
`predict_proba` methods and that can be saved to and loaded from memory
using skops. If no value is provided, the value set in `settings.CLF_CLASS`
is used.
classifier_kws : key-value pairings, optional
classifier_kwargs : key-value pairings, optional
Keyword arguments that will be passed to the initialization of
`classifier_class`. If no value is provided, the value set in
`settings.CLF_KWS` is used.
`settings.CLF_KWARGS` is used.
"""
self.pixel_features_builder_kws = dict(
self.pixel_features_builder_kwargs = dict(
sigmas=sigmas,
num_orientations=num_orientations,
neighborhood=neighborhood,
min_neighborhood_range=min_neighborhood_range,
num_neighborhoods=num_neighborhoods,
)
self.pixel_response_builder_kws = dict(
self.pixel_response_builder_kwargs = dict(
tree_val=tree_val, nontree_val=nontree_val
)
if classifier_class is None:
classifier_class = settings.CLF_CLASS
self.classifier_class = classifier_class
if classifier_kws == {}:
classifier_kws = settings.CLF_KWS
self.classifier_kws = classifier_kws
if classifier_kwargs == {}:
classifier_kwargs = settings.CLF_KWARGS
self.classifier_kwargs = classifier_kwargs

def train_classifier(
self,
Expand Down Expand Up @@ -179,7 +179,7 @@ def train_classifier(
]

X = pixel_features.PixelFeaturesBuilder(
**self.pixel_features_builder_kws
**self.pixel_features_builder_kwargs
).build_features(
split_df=split_df,
img_filepaths=img_filepaths,
Expand All @@ -190,7 +190,7 @@ def train_classifier(
)

y = pixel_response.PixelResponseBuilder(
**self.pixel_response_builder_kws
**self.pixel_response_builder_kwargs
).build_response(
split_df=split_df,
response_img_dir=response_img_dir,
Expand All @@ -200,7 +200,7 @@ def train_classifier(
img_cluster=img_cluster,
)

clf = self.classifier_class(**self.classifier_kws)
clf = self.classifier_class(**self.classifier_kwargs)
clf.fit(X, y)

return clf
Expand Down Expand Up @@ -258,7 +258,7 @@ def __init__(
refine=None,
refine_beta=None,
refine_int_rescale=None,
**pixel_features_builder_kws,
**pixel_features_builder_kwargs,
):
"""
Initialize the classifier instance.
Expand Down Expand Up @@ -287,7 +287,7 @@ def __init__(
transformation of float to integer edge weights, required for the employed
graph cuts algorithm. Larger values lead to greater precision. If no value
is provided, the value set in `settings.CLF_REFINE_INT_RESCALE` is used.
pixel_features_builder_kws : dict, optional
pixel_features_builder_kwargs : dict, optional
Keyword arguments that will be passed to `detectree.PixelFeaturesBuilder`,
which customize how the pixel features are built.
"""
Expand All @@ -310,7 +310,7 @@ def __init__(
self.refine_beta = refine_beta
self.refine_int_rescale = refine_int_rescale

self.pixel_features_builder_kws = pixel_features_builder_kws
self.pixel_features_builder_kwargs = pixel_features_builder_kwargs

def classify_img(self, img_filepath, clf, *, output_filepath=None):
"""
Expand Down Expand Up @@ -342,7 +342,7 @@ def classify_img(self, img_filepath, clf, *, output_filepath=None):
img_shape = src.shape

X = pixel_features.PixelFeaturesBuilder(
**self.pixel_features_builder_kws
**self.pixel_features_builder_kwargs
).build_features_from_filepath(img_filepath)

if not self.refine:
Expand Down
56 changes: 28 additions & 28 deletions detectree/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ def parser_process(value, state):
return retval


def _dict_from_kws(kws):
def _dict_from_kwargs(kwargs):
# Multiple key:value pair arguments in click, see https://bit.ly/32BaES3
if kws is not None:
kws = dict(kw.split(":") for kw in kws)
if kwargs is not None:
kwargs = dict(kwarg.split(":") for kwarg in kwargs)
else:
kws = {}
kwargs = {}

return kws
return kwargs


def _init_classifier_trainer(
Expand All @@ -72,11 +72,11 @@ def _init_classifier_trainer(
num_neighborhoods,
tree_val,
nontree_val,
classifier_kws,
classifier_kwargs,
):
# pixel_features_builder_kws = _dict_from_kws(pixel_features_builder_kws)
# pixel_response_builder_kws = _dict_from_kws(pixel_response_builder_kws)
classifier_kws = _dict_from_kws(classifier_kws)
classifier_kwargs = _dict_from_kwargs(classifier_kwargs)

return dtr.ClassifierTrainer(
sigmas=sigmas,
Expand All @@ -85,7 +85,7 @@ def _init_classifier_trainer(
num_neighborhoods=num_neighborhoods,
tree_val=tree_val,
nontree_val=nontree_val,
**classifier_kws,
**classifier_kwargs,
)


Expand Down Expand Up @@ -155,16 +155,16 @@ def train_test_split(
else:
logger.info("Loaded %d images", num_imgs)

tts_kws = {}
tts_kwargs = {}
if method is not None:
tts_kws["method"] = method
tts_kwargs["method"] = method
if num_components is not None:
tts_kws["num_components"] = num_components
tts_kwargs["num_components"] = num_components
if num_img_clusters is not None:
tts_kws["num_img_clusters"] = num_img_clusters
tts_kwargs["num_img_clusters"] = num_img_clusters
if train_prop is not None:
tts_kws["train_prop"] = train_prop
df, evr = ts.train_test_split(return_evr=True, **tts_kws)
tts_kwargs["train_prop"] = train_prop
df, evr = ts.train_test_split(return_evr=True, **tts_kwargs)
logger.info("Variance ratio explained by PCA: %f", evr)

if output_filepath is None:
Expand All @@ -190,7 +190,7 @@ def train_test_split(
@click.option("--num-neighborhoods", type=int)
@click.option("--tree-val", type=int)
@click.option("--nontree-val", type=int)
@click.option("--classifier-kws", cls=_OptionEatAll)
@click.option("--classifier-kwargs", cls=_OptionEatAll)
@click.option("--output-filepath", type=click.Path())
def train_classifier(
ctx,
Expand All @@ -208,7 +208,7 @@ def train_classifier(
num_neighborhoods,
tree_val,
nontree_val,
classifier_kws,
classifier_kwargs,
output_filepath,
):
"""Train a tree/non-tree pixel classifier."""
Expand All @@ -227,7 +227,7 @@ def train_classifier(
num_neighborhoods=num_neighborhoods,
tree_val=tree_val,
nontree_val=nontree_val,
classifier_kws=classifier_kws,
classifier_kwargs=classifier_kwargs,
)
clf = ct.train_classifier(
split_df=split_df,
Expand Down Expand Up @@ -256,7 +256,7 @@ def train_classifier(
@click.option("--num-neighborhoods", type=int)
@click.option("--tree-val", type=int)
@click.option("--nontree-val", type=int)
@click.option("--classifier-kws", cls=_OptionEatAll)
@click.option("--classifier-kwargs", cls=_OptionEatAll)
@click.option("--output-dir", type=click.Path(exists=True))
def train_classifiers(
ctx,
Expand All @@ -268,7 +268,7 @@ def train_classifiers(
num_neighborhoods,
tree_val,
nontree_val,
classifier_kws,
classifier_kwargs,
output_dir,
):
"""Train tree/non-tree pixel classifier(s) for a given train/test split."""
Expand All @@ -287,7 +287,7 @@ def train_classifiers(
num_neighborhoods=num_neighborhoods,
tree_val=tree_val,
nontree_val=nontree_val,
classifier_kws=classifier_kws,
classifier_kwargs=classifier_kwargs,
)
clfs_dict = ct.train_classifiers(split_df, response_img_dir)

Expand All @@ -311,7 +311,7 @@ def train_classifiers(
@click.option("--refine", is_flag=True)
@click.option("--refine-beta", type=int)
@click.option("--refine-int-rescale", type=int)
@click.option("--pixel-features-builder-kws", cls=_OptionEatAll)
@click.option("--pixel-features-builder-kwargs", cls=_OptionEatAll)
@click.option("--output-filepath", type=click.Path())
def classify_img(
ctx,
Expand All @@ -322,22 +322,22 @@ def classify_img(
refine,
refine_beta,
refine_int_rescale,
pixel_features_builder_kws,
pixel_features_builder_kwargs,
output_filepath,
):
"""Use a trained classifier to predict tree pixels in an image."""
logger = ctx.obj["LOGGER"]

logger.info("Classifying %s with classifier of %s", img_filepath, clf_filepath)

pixel_features_builder_kws = _dict_from_kws(pixel_features_builder_kws)
pixel_features_builder_kwargs = _dict_from_kwargs(pixel_features_builder_kwargs)
c = dtr.Classifier(
tree_val=tree_val,
nontree_val=nontree_val,
refine=refine,
refine_beta=refine_beta,
refine_int_rescale=refine_int_rescale,
**pixel_features_builder_kws,
**pixel_features_builder_kwargs,
)

if output_filepath is None:
Expand All @@ -364,7 +364,7 @@ def classify_img(
@click.option("--refine", is_flag=True)
@click.option("--refine-beta", type=int)
@click.option("--refine-int-rescale", type=int)
@click.option("--pixel-features-builder-kws", cls=_OptionEatAll)
@click.option("--pixel-features-builder-kwargs", cls=_OptionEatAll)
@click.option("--output-dir", type=click.Path(exists=True))
def classify_imgs(
ctx,
Expand All @@ -378,7 +378,7 @@ def classify_imgs(
refine,
refine_beta,
refine_int_rescale,
pixel_features_builder_kws,
pixel_features_builder_kwargs,
output_dir,
):
"""Use trained classifier(s) to predict tree pixels in multiple images."""
Expand All @@ -404,15 +404,15 @@ def classify_imgs(
settings.SKOPS_TRUSTED,
)

pixel_features_builder_kws = _dict_from_kws(pixel_features_builder_kws)
pixel_features_builder_kwargs = _dict_from_kwargs(pixel_features_builder_kwargs)

c = dtr.Classifier(
tree_val=tree_val,
nontree_val=nontree_val,
refine=refine,
refine_beta=refine_beta,
refine_int_rescale=refine_int_rescale,
**pixel_features_builder_kws,
**pixel_features_builder_kwargs,
)

if output_dir is None:
Expand Down
6 changes: 3 additions & 3 deletions detectree/lidar.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def to_canopy_mask(
output_filepath=None,
postprocess_func=None,
postprocess_func_args=None,
postprocess_func_kws=None,
postprocess_func_kwargs=None,
):
"""
Transform a LiDAR file into a canopy mask.
Expand All @@ -144,7 +144,7 @@ def to_canopy_mask(
ndarray.
postprocess_func_args : list-like, optional
Arguments to be passed to `postprocess_func`.
postprocess_func_kws : dict, optional
postprocess_func_kwargs : dict, optional
Keyword arguments to be passed to `postprocess_func`.
Returns
Expand All @@ -168,7 +168,7 @@ def to_canopy_mask(
canopy_arr = lidar_arr >= self.tree_threshold
if postprocess_func is not None:
canopy_arr = postprocess_func(
canopy_arr, *postprocess_func_args, **postprocess_func_kws
canopy_arr, *postprocess_func_args, **postprocess_func_kwargs
)
canopy_arr = np.where(
canopy_arr, self.output_tree_val, self.output_nodata
Expand Down
2 changes: 1 addition & 1 deletion detectree/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

# classifier
CLF_CLASS = lgb.LGBMClassifier
CLF_KWS = {"n_estimators": 200}
CLF_KWARGS = {"n_estimators": 200}
CLF_TREE_VAL = 255
CLF_NONTREE_VAL = 0
CLF_REFINE = True
Expand Down
2 changes: 1 addition & 1 deletion tests/test_detectree.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ def test_lidar_to_canopy(self):
self.ref_img_filepath,
postprocess_func=ndi.binary_dilation,
postprocess_func_args=[ndi.generate_binary_structure(2, 2)],
postprocess_func_kws={"border_value": 0},
postprocess_func_kwargs={"border_value": 0},
)
# test that `to_canopy_mask` with `output_filepath` returns a ndarray and dumps
# it
Expand Down

0 comments on commit 0c9da36

Please sign in to comment.