Skip to content

Commit

Permalink
Autoformat with black
Browse files Browse the repository at this point in the history
  • Loading branch information
brainless-bot[bot] committed Jan 25, 2024
1 parent 73d83a7 commit dbccf1c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 20 deletions.
15 changes: 12 additions & 3 deletions panoptica/metrics/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
from panoptica.metrics.assd import _average_surface_distance, _average_symmetric_surface_distance
from panoptica.metrics.cldice import _compute_centerline_dice, _compute_centerline_dice_coefficient
from panoptica.metrics.dice import _compute_dice_coefficient, _compute_instance_volumetric_dice
from panoptica.metrics.assd import (
_average_surface_distance,
_average_symmetric_surface_distance,
)
from panoptica.metrics.cldice import (
_compute_centerline_dice,
_compute_centerline_dice_coefficient,
)
from panoptica.metrics.dice import (
_compute_dice_coefficient,
_compute_instance_volumetric_dice,
)
from panoptica.metrics.iou import _compute_instance_iou, _compute_iou
from panoptica.metrics.metrics import (
Evaluation_List_Metric,
Expand Down
48 changes: 37 additions & 11 deletions panoptica/metrics/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ def __call__(
reference_arr = reference_arr.copy() == ref_instance_idx
if isinstance(pred_instance_idx, int):
pred_instance_idx = [pred_instance_idx]
prediction_arr = np.isin(prediction_arr.copy(), pred_instance_idx) # type:ignore
prediction_arr = np.isin(
prediction_arr.copy(), pred_instance_idx
) # type:ignore
return self._metric_function(reference_arr, prediction_arr, *args, **kwargs)

def __eq__(self, __value: object) -> bool:
Expand All @@ -61,8 +63,12 @@ def __hash__(self) -> int:
def increasing(self):
return not self.decreasing

def score_beats_threshold(self, matching_score: float, matching_threshold: float) -> bool:
return (self.increasing and matching_score >= matching_threshold) or (self.decreasing and matching_score <= matching_threshold)
def score_beats_threshold(
self, matching_score: float, matching_threshold: float
) -> bool:
return (self.increasing and matching_score >= matching_threshold) or (
self.decreasing and matching_score <= matching_threshold
)


class DirectValueMeta(EnumMeta):
Expand Down Expand Up @@ -117,7 +123,9 @@ def __call__(
**kwargs,
)

def score_beats_threshold(self, matching_score: float, matching_threshold: float) -> bool:
def score_beats_threshold(
self, matching_score: float, matching_threshold: float
) -> bool:
"""Calculates whether a score beats a specified threshold
Args:
Expand All @@ -127,7 +135,9 @@ def score_beats_threshold(self, matching_score: float, matching_threshold: float
Returns:
bool: True if the matching_score beats the threshold, False otherwise.
"""
return (self.increasing and matching_score >= matching_threshold) or (self.decreasing and matching_score <= matching_threshold)
return (self.increasing and matching_score >= matching_threshold) or (
self.decreasing and matching_score <= matching_threshold
)

@property
def name(self):
Expand Down Expand Up @@ -221,16 +231,22 @@ def __call__(self, result_obj: "PanopticaResult") -> Any:
# ERROR
if self._error:
if self._error_obj is None:
self._error_obj = MetricCouldNotBeComputedException(f"Metric {self.id} requested, but could not be computed")
self._error_obj = MetricCouldNotBeComputedException(
f"Metric {self.id} requested, but could not be computed"
)
raise self._error_obj
# Already calculated?
if self._was_calculated:
return self._value

# Calculate it
try:
assert not self._was_calculated, f"Metric {self.id} was called to compute, but is set to have been already calculated"
assert self._calc_func is not None, f"Metric {self.id} was called to compute, but has no calculation function set"
assert (
not self._was_calculated
), f"Metric {self.id} was called to compute, but is set to have been already calculated"
assert (
self._calc_func is not None
), f"Metric {self.id} was called to compute, but has no calculation function set"
value = self._calc_func(result_obj)
except MetricCouldNotBeComputedException as e:
value = e
Expand Down Expand Up @@ -273,17 +289,27 @@ def __init__(
else:
self.AVG = None if self.ALL is None else np.average(self.ALL)
self.SUM = None if self.ALL is None else np.sum(self.ALL)
self.STD = None if self.ALL is None else empty_list_std if len(self.ALL) == 0 else np.std(self.ALL)
self.STD = (
None
if self.ALL is None
else empty_list_std
if len(self.ALL) == 0
else np.std(self.ALL)
)

def __getitem__(self, mode: MetricMode | str):
if self.error:
raise MetricCouldNotBeComputedException(f"Metric {self.id} has not been calculated, add it to your eval_metrics")
raise MetricCouldNotBeComputedException(
f"Metric {self.id} has not been calculated, add it to your eval_metrics"
)
if isinstance(mode, MetricMode):
mode = mode.name
if hasattr(self, mode):
return getattr(self, mode)
else:
raise MetricCouldNotBeComputedException(f"List_Metric {self.id} does not contain {mode} member")
raise MetricCouldNotBeComputedException(
f"List_Metric {self.id} does not contain {mode} member"
)


if __name__ == "__main__":
Expand Down
28 changes: 22 additions & 6 deletions panoptica/panoptic_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@ def __init__(
num_pred_instances=self.num_pred_instances,
num_ref_instances=self.num_ref_instances,
)
self._list_metrics[k] = Evaluation_List_Metric(k, empty_list_std, v, is_edge_case, edge_case_result)
self._list_metrics[k] = Evaluation_List_Metric(
k, empty_list_std, v, is_edge_case, edge_case_result
)

def _add_metric(
self,
Expand All @@ -239,7 +241,11 @@ def _add_metric(
was_calculated
), "Tried to add a metric without a calc_function but that hasn't been calculated yet, how did you think this could works?"
eval_metric = Evaluation_Metric(
name_id, metric_type=metric_type, calc_func=calc_func, long_name=long_name, was_calculated=was_calculated
name_id,
metric_type=metric_type,
calc_func=calc_func,
long_name=long_name,
was_calculated=was_calculated,
)
self._evaluation_metrics[name_id] = eval_metric
return default_value
Expand Down Expand Up @@ -284,13 +290,19 @@ def __str__(self) -> str:
return text

def to_dict(self) -> dict:
return {k: getattr(self, v.id) for k, v in self._evaluation_metrics.items() if (v._error == False and v._was_calculated)}
return {
k: getattr(self, v.id)
for k, v in self._evaluation_metrics.items()
if (v._error == False and v._was_calculated)
}

def get_list_metric(self, metric: Metric, mode: MetricMode):
if metric in self._list_metrics:
return self._list_metrics[metric][mode]
else:
raise MetricCouldNotBeComputedException(f"{metric} could not be found, have you set it in eval_metrics during evaluation?")
raise MetricCouldNotBeComputedException(
f"{metric} could not be found, have you set it in eval_metrics during evaluation?"
)

def _calc_metric(self, metric_name: str, supress_error: bool = False):
if metric_name in self._evaluation_metrics:
Expand All @@ -306,7 +318,9 @@ def _calc_metric(self, metric_name: str, supress_error: bool = False):
self._evaluation_metrics[metric_name]._was_calculated = True
return value
else:
raise MetricCouldNotBeComputedException(f"could not find metric with name {metric_name}")
raise MetricCouldNotBeComputedException(
f"could not find metric with name {metric_name}"
)

def __getattribute__(self, __name: str) -> Any:
attr = None
Expand All @@ -319,7 +333,9 @@ def __getattribute__(self, __name: str) -> Any:
raise e
if attr is None:
if self._evaluation_metrics[__name]._error:
raise MetricCouldNotBeComputedException(f"Requested metric {__name} that could not be computed")
raise MetricCouldNotBeComputedException(
f"Requested metric {__name} that could not be computed"
)
elif not self._evaluation_metrics[__name]._was_calculated:
value = self._calc_metric(__name)
setattr(self, __name, value)
Expand Down

0 comments on commit dbccf1c

Please sign in to comment.