Skip to content

Commit

Permalink
refactor: dropped DEFAULT from settings, consistent docs
Browse files Browse the repository at this point in the history
  • Loading branch information
martibosch committed Mar 28, 2024
1 parent 60f0d14 commit 5c00c95
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 131 deletions.
69 changes: 32 additions & 37 deletions detectree/classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,52 +42,51 @@ def __init__(
The list of scale parameters (sigmas) to build the Gaussian filter bank that
will be used to compute the pixel-level features. The provided argument will
be passed to the initialization method of the `PixelFeaturesBuilder` class.
If no value is provided, the default value set in
`settings.GAUSS_DEFAULT_SIGMAS` will be taken.
If no value is provided, the value set in `settings.GAUSS_SIGMAS` will be
taken.
num_orientations : int, optional
The number of equally-distributed orientations to build the Gaussian filter
bank that will be used to compute the pixel-level features. The provided
argument will be passed to the initialization method of the
`PixelFeaturesBuilder` class. If no value is provided, the default value set
in `settings.GAUSS_DEFAULT_NUM_ORIENTATIONS` will be taken.
`PixelFeaturesBuilder` class. If no value is provided, the value set in
`settings.GAUSS_NUM_ORIENTATIONS` is used.
neighborhood : array-like, optional
The base neighborhood structure that will be used to compute the entropy
features. Theprovided argument will be passed to the initialization method
of the `PixelFeaturesBuilder` class. If no value is provided, a square with
a side size of `2 * min_neighborhood_range + 1` will be used.
a side size of `2 * min_neighborhood_range + 1` is used.
min_neighborhood_range : int, optional
The range (i.e., the square radius) of the smallest neigbhorhood window that
will be used to compute the entropy features. The provided argument will be
passed to the initialization method of the `PixelFeaturesBuilder` class. If
no value is provided, the default value set in
`settings.ENTROPY_DEFAULT_MIN_NEIGHBORHOOD_RANGE` will be taken.
no value is provided, the value set in
`settings.ENTROPY_MIN_NEIGHBORHOOD_RANGE` is used.
num_neighborhoods : int, optional
The number of neigbhorhood windows (whose size follows a geometric
progression starting at `min_neighborhood_range`) that will be used to
compute the entropy features. The provided argument will be passed to the
initialization method of the `PixelFeaturesBuilder` class. If no value is
provided, the default value set in
`settings.ENTROPY_DEFAULT_NUM_NEIGHBORHOODS` will be taken.
provided, the value set in `settings.ENTROPY_NUM_NEIGHBORHOODS` is used.
tree_val : int, optional
The value that designates tree pixels in the response images. The provided
argument will be passed to the initialization method of the
`PixelResponseBuilder` class. If no value is provided, the default value set
in `settings.RESPONSE_DEFAULT_TREE_VAL` will be taken.
`PixelResponseBuilder` class. If no value is provided, the value set in
`settings.RESPONSE_TREE_VAL` is used.
nontree_val : int, optional
The value that designates non-tree pixels in the response images. The
provided argument will be passed to the initialization method of the
`PixelResponseBuilder` class. If no value is provided, the default value set
in `settings.RESPONSE_DEFAULT_NONTREE_VAL` will be taken.
`PixelResponseBuilder` class. If no value is provided, the value set in
`settings.RESPONSE_NONTREE_VAL` is used.
classifier_class : class, optional
The class of the classifier to be trained. It can be any scikit-learn
compatible estimator that implements the `fit`, `predict` and
`predict_proba` methods and that can be saved to and loaded from memory
using skops. If no value is provided, the default value set in
`settings.CLF_DEFAULT_CLASS` will be taken.
using skops. If no value is provided, the value set in `settings.CLF_CLASS`
is used.
classifier_kws : key-value pairings, optional
Keyword arguments that will be passed to the initialization of
`classifier_class`. If no value is provided, the default value set in
`settings.CLF_DEFAULT_KWS` will be taken.
`classifier_class`. If no value is provided, the value set in
`settings.CLF_KWS` is used.
"""
self.pixel_features_builder_kws = dict(
sigmas=sigmas,
Expand All @@ -100,10 +99,10 @@ def __init__(
tree_val=tree_val, nontree_val=nontree_val
)
if classifier_class is None:
classifier_class = settings.CLF_DEFAULT_CLASS
classifier_class = settings.CLF_CLASS
self.classifier_class = classifier_class
if classifier_kws == {}:
classifier_kws = settings.CLF_DEFAULT_KWS
classifier_kws = settings.CLF_KWS
self.classifier_kws = classifier_kws

def train_classifier(
Expand Down Expand Up @@ -145,9 +144,8 @@ def train_classifier(
`img_filepaths` is provided.
img_filename_pattern : str representing a file-name pattern, optional
Filename pattern to be matched in order to obtain the list of images. If no
value is provided, the default value set in
`settings.IMG_DEFAULT_FILENAME_PATTERN` will be taken. Ignored if `split_df`
or `img_filepaths` is provided.
value is provided, the value set in `settings.IMG_FILENAME_PATTERN` is used.
Ignored if `split_df` or `img_filepaths` is provided.
method : {'cluster-I', 'cluster-II'}, optional
Method used in the train/test split.
img_cluster : int, optional
Expand All @@ -167,7 +165,7 @@ def train_classifier(
if img_filepaths is None:
# TODO: this is copied from `build_features` - ideally, we should DRY it
if img_filename_pattern is None:
img_filename_pattern = settings.IMG_DEFAULT_FILENAME_PATTERN
img_filename_pattern = settings.IMG_FILENAME_PATTERN
if img_dir is None:
raise ValueError(
"Either `split_df`, `img_filepaths` or `img_dir` must "
Expand Down Expand Up @@ -272,42 +270,39 @@ def __init__(
----------
tree_val : int, optional
Label used to denote tree pixels in the predicted images. If no value is
provided, the default value set in `settings.CLF_DEFAULT_TREE_VAL` will be
taken.
provided, the value set in `settings.CLF_TREE_VAL` is used.
nontree_val : int, optional
Label used to denote non-tree pixels in the predicted images. If no value is
provided, the default value set in `settings.CLF_DEFAULT_NONTREE_VAL` will
be taken.
provided, the value set in `settings.CLF_NONTREE_VAL` is used.
refine : bool, optional
Whether the pixel-level classification should be refined by optimizing the
consistence between neighboring pixels. If no value is provided, the default
value set in `settings.CLF_DEFAULT_REFINE` will be taken.
consistence between neighboring pixels. If no value is provided, the value
set in `settings.CLF_REFINE` is used.
refine_beta : int, optional
Parameter of the refinement procedure that controls the smoothness of the
labelling. Larger values lead to smoother shapes. If no value is provided,
the default value set in `settings.CLF_DEFAULT_REFINE_BETA` will be taken.
the value set in `settings.CLF_REFINE_BETA` is used.
refine_int_rescale : int, optional
Parameter of the refinement procedure that controls the precision of the
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 default value set in
`settings.CLF_DEFAULT_REFINE_INT_RESCALE` will be taken.
is provided, the value set in `settings.CLF_REFINE_INT_RESCALE` is used.
pixel_features_builder_kws : dict, optional
Keyword arguments that will be passed to `detectree.PixelFeaturesBuilder`,
which customize how the pixel features are built.
"""
super().__init__()

if tree_val is None:
tree_val = settings.CLF_DEFAULT_TREE_VAL
tree_val = settings.CLF_TREE_VAL
if nontree_val is None:
nontree_val = settings.CLF_DEFAULT_NONTREE_VAL
nontree_val = settings.CLF_NONTREE_VAL
if refine is None:
refine = settings.CLF_DEFAULT_REFINE
refine = settings.CLF_REFINE
if refine_beta is None:
refine_beta = settings.CLF_DEFAULT_REFINE_BETA
refine_beta = settings.CLF_REFINE_BETA
if refine_int_rescale is None:
refine_int_rescale = settings.CLF_DEFAULT_REFINE_INT_RESCALE
refine_int_rescale = settings.CLF_REFINE_INT_RESCALE

self.tree_val = tree_val
self.nontree_val = nontree_val
Expand Down
6 changes: 3 additions & 3 deletions detectree/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ def classify_img(

c.classify_img(
img_filepath,
io.load(clf_filepath, trusted=settings.SKOPS_DEFAULT_TRUSTED),
io.load(clf_filepath, trusted=settings.SKOPS_TRUSTED),
output_filepath,
)
logger.info("Dumped predicted image to %s", output_filepath)
Expand Down Expand Up @@ -388,7 +388,7 @@ def classify_imgs(

if clf_filepath is not None:
clf_dict = None
clf = io.load(clf_filepath, settings.SKOPS_DEFAULT_TRUSTED)
clf = io.load(clf_filepath, settings.SKOPS_TRUSTED)
logger.info(
"Classifying images from %s with classifier of %s",
split_filepath,
Expand All @@ -401,7 +401,7 @@ def classify_imgs(
for img_cluster in split_df["img_cluster"].unique():
clf_dict[img_cluster] = io.load(
path.join(clf_dir, f"{img_cluster}.skops"),
settings.SKOPS_DEFAULT_TRUSTED,
settings.SKOPS_TRUSTED,
)

pixel_features_builder_kws = _dict_from_kws(pixel_features_builder_kws)
Expand Down
2 changes: 1 addition & 1 deletion detectree/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def get_gabor_filter_bank(frequencies, num_orientations):
Returns
-------
kernels : list-like
List of kernel 2-D arrays that correspond to the filter bank
List of kernel 2-D arrays that correspond to the filter bank.
"""
kernels = []

Expand Down
12 changes: 9 additions & 3 deletions detectree/lidar.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,19 @@ def __init__(
Threshold of lidar points classified as tree by pixel at which
point the pixel is considered a tree. As a rule of thumb, the value can be
set to result of dividing the point density of the lidar (e.g., pts/m^2) by
the pixel area (e.g., m^2).
the pixel area (e.g., m^2). If no value is provided, the value set in
`settings.LIDAR_TREE_THRESHOLD` is used.
output_dtype : str or numpy dtype, optional
The desired data type of the output raster canopy masks.
The desired data type of the output raster canopy masks. If no value is
provided, the value set in `settings.LIDAR_OUTPUT_DTYPE` is used.
output_tree_val : int, optional
The value that designates tree pixels in the output raster canopy masks.
The value that designates tree pixels in the output raster canopy masks. If
no value is provided, the value set in `settings.LIDAR_OUTPUT_TREE_VAL` is
used.
output_nodata : int, optional
The value that designates non-tree pixels in the output raster canopy masks.
If no value is provided, the value set in `settings.LIDAR_OUTPUT_NODATA` is
used.
"""
if tree_threshold is None:
tree_threshold = settings.LIDAR_TREE_THRESHOLD
Expand Down
38 changes: 18 additions & 20 deletions detectree/pixel_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,41 +63,40 @@ def __init__(
The list of scale parameters (sigmas) to build the Gaussian filter bank that
will be used to compute the pixel-level features. The provided argument will
be passed to the initialization method of the `PixelFeaturesBuilder`
class. If no value is provided, the default value set in
`settings.GAUSS_DEFAULT_SIGMAS` will be taken.
class. If no value is provided, the value set in `settings.GAUSS_SIGMAS` is
used.
num_orientations : int, optional
The number of equally-distributed orientations to build the Gaussian filter
bank that will be used to compute the pixel-level features. The provided
argument will be passed to the initialization method of the
`PixelFeaturesBuilder` class. If no value is provided, the default value set
in `settings.GAUSS_DEFAULT_NUM_ORIENTATIONS` will be taken.
`PixelFeaturesBuilder` class. If no value is provided, the value set in
`settings.GAUSS_NUM_ORIENTATIONS` is used.
neighborhood : array-like, optional
The base neighborhood structure that will be used to compute the entropy
features. The provided argument will be passed to the initialization method
of the `PixelFeaturesBuilder` class. If no value is provided, a square with
a side size of `2 * min_neighborhood_range + 1` will be used.
a side size of `2 * min_neighborhood_range + 1` is used.
min_neighborhood_range : int, optional
The range (i.e., the square radius) of the smallest neigbhorhood window that
will be used to compute the entropy features. The provided argument will be
passed to the initialization method of the `PixelFeaturesBuilder` class. If
no value is provided, the default value set in
`settings.ENTROPY_DEFAULT_MIN_NEIGHBORHOOD_RANGE` will be taken.
no value is provided, the value set in
`settings.ENTROPY_MIN_NEIGHBORHOOD_RANGE` is used.
num_neighborhoods : int, optional
The number of neigbhorhood windows (whose size follows a geometric
progression starting at `min_neighborhood_range`) that will be used to
compute the entropy features. The provided argument will be passed to the
initialization method of the `PixelFeaturesBuilder` class. If no value is
provided, the default value set in
`settings.ENTROPY_DEFAULT_NUM_NEIGHBORHOODS` will be taken.
provided, the value set in `settings.ENTROPY_NUM_NEIGHBORHOODS` is used.
"""
# preprocess technical keyword arguments
# texture features
if sigmas is None:
sigmas = settings.GAUSS_DEFAULT_SIGMAS
sigmas = settings.GAUSS_SIGMAS
self.sigmas = sigmas

if num_orientations is None:
num_orientations = settings.GAUSS_DEFAULT_NUM_ORIENTATIONS
num_orientations = settings.GAUSS_NUM_ORIENTATIONS
self.num_orientations = num_orientations

# entropy features
Expand All @@ -117,11 +116,11 @@ def __init__(
# num_neighborhoods = len(neighborhoods)
if neighborhood is None:
if min_neighborhood_range is None:
min_neighborhood_range = settings.ENTROPY_DEFAULT_MIN_NEIGHBORHOOD_RANGE
min_neighborhood_range = settings.ENTROPY_MIN_NEIGHBORHOOD_RANGE
neighborhood = morphology.square(2 * min_neighborhood_range + 1)
self.neighborhood = neighborhood
if num_neighborhoods is None:
num_neighborhoods = settings.ENTROPY_DEFAULT_NUM_NEIGHBORHOODS
num_neighborhoods = settings.ENTROPY_NUM_NEIGHBORHOODS
self.scales = np.geomspace(
1, 2 ** (num_neighborhoods - 1), num_neighborhoods
).astype(int)
Expand All @@ -148,7 +147,7 @@ def build_features_from_arr(self, img_rgb):
Returns
-------
responses : numpy ndarray
Array with the pixel responses
Array with the pixel responses.
"""
# the third component `_` is actually the number of channels in RGB, which is
# already defined in the constant `NUM_RGB_CHANNELS`
Expand Down Expand Up @@ -220,7 +219,7 @@ def build_features_from_filepath(self, img_filepath):
Returns
-------
responses : numpy ndarray
Array with the pixel responses
Array with the pixel responses.
"""
img_rgb = utils.img_rgb_from_filepath(img_filepath)
return self.build_features_from_arr(img_rgb)
Expand Down Expand Up @@ -252,9 +251,8 @@ def build_features(
`img_filepaths` is provided.
img_filename_pattern : str representing a file-name pattern, optional
Filename pattern to be matched in order to obtain the list of images. If no
value is provided, the default value set in
`settings.IMG_DEFAULT_FILENAME_PATTERN` will be taken. Ignored if `split_df`
or `img_filepaths` is provided.
value is provided, the value set in `settings.IMG_FILENAME_PATTERN` is used.
Ignored if `split_df` or `img_filepaths` is provided.
method : {'cluster-I', 'cluster-II'}, optional
Method used in the train/test split
img_cluster : int, optional
Expand All @@ -263,7 +261,7 @@ def build_features(
Returns
-------
X : numpy ndarray
Array with the pixel features
Array with the pixel features.
"""
# TODO: accept `neighborhoods` kwarg
if split_df is not None:
Expand All @@ -286,7 +284,7 @@ def build_features(
else:
if img_filepaths is None:
if img_filename_pattern is None:
img_filename_pattern = settings.IMG_DEFAULT_FILENAME_PATTERN
img_filename_pattern = settings.IMG_FILENAME_PATTERN
if img_dir is None:
raise ValueError(
"Either `split_df`, `img_filepaths` or `img_dir` must "
Expand Down
Loading

0 comments on commit 5c00c95

Please sign in to comment.