Skip to content

Commit

Permalink
analysis: Improve errors
Browse files Browse the repository at this point in the history
Improve the errors in the ema_workbench/analysis, by:
- Making them more explicit
- Including what the variable value or type not expected currently is
- Including what the variable value or type not expected should be
- Stating the explicit type of warning or error (DeprecationWarning, ValueError, etc.)
- Converting them to F-strings (for better in-code readability)
- Formatting warnings and errors with a Capital letter, while leaving log-statements lowercase.

This will make debugging easier, allowing more time for model development and experimentation.
  • Loading branch information
EwoutH committed Nov 16, 2023
1 parent 1831bd8 commit 9eb6cb4
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion ema_workbench/analysis/b_and_w_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def set_ax_collections_to_bw(ax, style, colormap):
try:
converter_func = _collection_converter[collection_type]
except KeyError:
raise EMAError(f"converter for {collection_type} not implemented")
raise EMAError(f"Converter for collection type {collection_type} not implemented")
else:
converter_func(collection, ax, style, colormap)

Expand Down
6 changes: 3 additions & 3 deletions ema_workbench/analysis/cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def setup_cart(results, classify, incl_unc=None, mass_min=0.05):
y = classify(outcomes)
mode = sdutil.RuleInductionType.BINARY
else:
raise TypeError("unknown type for classify")
raise TypeError(f"Unknown type for classify: {type(classify)}")

return CART(x, y, mass_min, mode=mode)

Expand Down Expand Up @@ -329,7 +329,7 @@ def show_tree(self, mplfig=True, format="png"):
# but just in case, we raise an error if assumption of len==1 does
# not hold
if len(graphs) > 1:
raise EMAError("trying to visualize more than one tree")
raise EMAError(f"Expected a single tree for visualization, but found {len(graphs)} trees.")

graph = graphs[0]

Expand All @@ -343,6 +343,6 @@ def show_tree(self, mplfig=True, format="png"):
elif format == "svg":
img = graph.create_svg()
else:
raise TypeError("""format must be in {'png', 'svg'}""")
raise TypeError(f"format must be 'png' or 'svg' (instead of {format}).")

return img
2 changes: 1 addition & 1 deletion ema_workbench/analysis/feature_scoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def _prepare_outcomes(outcomes, classify):
y = classify(outcomes)
categorical = True
else:
raise TypeError("unknown type for classify")
raise TypeError(f"Unknown type for classify: {type(classify)}")

return y, categorical

Expand Down
4 changes: 2 additions & 2 deletions ema_workbench/analysis/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ def multiple_densities(

# making of grid
if not points_in_time:
raise EMAError("no points in time specified")
raise EMAError("No points in time specified, should be a list of length 1-6")
if len(points_in_time) == 1:
ax_env = plt.subplot2grid((2, 3), (0, 0), colspan=3)
ax1 = plt.subplot2grid((2, 3), (1, 1), sharey=ax_env)
Expand Down Expand Up @@ -788,7 +788,7 @@ def multiple_densities(
ax6 = plt.subplot2grid((2, 6), (1, 5), sharex=ax1, sharey=ax_env)
kde_axes = [ax1, ax2, ax3, ax4, ax5, ax6]
else:
raise EMAError("too many points in time provided")
raise EMAError(f"Too many points in time specified: {len(points_in_time)}, max is 6")

axes_dict["main plot"] = ax_env
for n, entry in enumerate(kde_axes):
Expand Down
14 changes: 9 additions & 5 deletions ema_workbench/analysis/plotting_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ def group_density(ax_d, density, outcomes, outcome_to_plot, group_labels, log=Fa
elif density == Density.BOXENPLOT:
plot_boxenplot(ax_d, values, log, group_labels=group_labels)
else:
raise EMAError(f"unknown density type: {density}")
raise EMAError(f"Unknown density plot type: {density}")

ax_d.set_xlabel("")
ax_d.set_ylabel("")
Expand Down Expand Up @@ -316,7 +316,7 @@ def simple_density(density, value, ax_d, ax, log):
elif density == Density.BOXENPLOT:
plot_boxenplot(ax_d, [value[:, -1]], log)
else:
raise EMAError("unknown density plot type")
raise EMAError(f"Unknown density plot type: {density}")

ax_d.get_yaxis().set_view_interval(
ax.get_yaxis().get_view_interval()[0], ax.get_yaxis().get_view_interval()[1]
Expand Down Expand Up @@ -671,8 +671,12 @@ def prepare_pairs_data(
filter_scalar : bool, optional
"""
if isinstance(outcomes_to_show, str):
raise EMAError("for pair wise plotting, more than one outcome needs to be provided")
if outcomes_to_show is not None:
if not isinstance(outcomes_to_show, list):
raise TypeError(f"For pair-wise plotting multiple outcomes need to be provided.\n"
f"outcomes_to_show must be a list of strings or None, instead of a {type(outcomes_to_show)}")
elif len(outcomes_to_show) < 2:
raise ValueError(f"Only {len(outcomes_to_show)} outcome provided, at least two are needed for pair-wise plotting.")

experiments, outcomes, outcomes_to_show, time, grouping_labels = prepare_data(
experiments, None, outcomes, outcomes_to_show, group_by, grouping_specifiers, filter_scalar
Expand Down Expand Up @@ -755,7 +759,7 @@ def prepare_data(
if not grouping_specifiers:
# no grouping specifier, so infer from the data
if group_by == "index":
raise EMAError("no grouping specifiers provided while " "trying to group on index")
raise EMAError("No grouping specifiers provided while trying to group on index")
else:
column_to_group_by = experiments[group_by]
if column_to_group_by.dtype in (object, "category"):
Expand Down
10 changes: 5 additions & 5 deletions ema_workbench/analysis/prim.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def pca_preprocess(experiments, y, subsets=None, exclude=set()):
if not x.select_dtypes(exclude=np.number).empty:
raise RuntimeError("X includes non numeric columns")
if not set(np.unique(y)) == {0, 1}:
raise RuntimeError("y should only contain 0s and 1s")
raise RuntimeError(f"y should only contain 0s and 1s, currently y contains {set(np.unique(y))}.")

# if no subsets are provided all uncertainties with non dtype object
# are in the same subset, the name of this is r, for rotation
Expand Down Expand Up @@ -416,7 +416,7 @@ def inspect(self, i=None, style="table", **kwargs):
i = [i]

if not all(isinstance(x, int) for x in i):
raise TypeError("i must be an integer or list of integers")
raise TypeError(f"i must be an integer or list of integers, not {type(i)}")

return [self._inspect(entry, style=style, **kwargs) for entry in i]

Expand Down Expand Up @@ -450,7 +450,7 @@ def _inspect(self, i=None, style="table", **kwargs):
elif style == "data":
return self._inspect_data(i, uncs, qp_values)
else:
raise ValueError("style must be one of graph, table or data")
raise ValueError(f"style must be one of 'graph', 'table' or 'data', not {style}.")

def _inspect_data(self, i, uncs, qp_values):
"""Helper method for inspecting boxes,
Expand Down Expand Up @@ -1044,9 +1044,9 @@ def __init__(
self._update_yi_remaining = self._update_functions[update_function]

if len(self.y.shape) > 1:
raise PrimException("y is not a 1-d array")
raise PrimException(f"y is not a 1-d array, but has shape {self.y.shape}")
if self.y.shape[0] != len(self.x):
raise PrimException("len(y) != len(x)")
raise PrimException(f"len(y) != len(x): {self.y.shape[0]} != {len(self.x)}")

# store the remainder of the parameters
self.paste_alpha = paste_alpha
Expand Down

0 comments on commit 9eb6cb4

Please sign in to comment.