Skip to content

Commit

Permalink
Merge pull request #178 from BrainLesion/157
Browse files Browse the repository at this point in the history
added some features while doing #157
  • Loading branch information
Hendrik-code authored Jan 31, 2025
2 parents b194ff1 + eb284e0 commit f44db2b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
8 changes: 7 additions & 1 deletion panoptica/panoptica_aggregator.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ def __init__(
self.__panoptica_evaluator = panoptica_evaluator
self.__class_group_names = panoptica_evaluator.segmentation_class_groups_names
self.__evaluation_metrics = panoptica_evaluator.resulting_metric_keys
self.__log_times = log_times

if log_times:
if log_times and COMPUTATION_TIME_KEY not in self.__evaluation_metrics:
self.__evaluation_metrics.append(COMPUTATION_TIME_KEY)

if isinstance(output_file, str):
Expand Down Expand Up @@ -176,6 +177,7 @@ def evaluate(
result_all=True,
verbose=False,
log_times=False,
save_group_times=self.__log_times,
)

# Add to file
Expand Down Expand Up @@ -208,6 +210,10 @@ def _save_one_subject(self, subject_name, result_grouped):
def panoptica_evaluator(self):
return self.__panoptica_evaluator

@property
def evaluation_metrics(self):
return self.__evaluation_metrics


def _read_first_row(file: str | Path):
"""Reads the first row of a TSV file.
Expand Down
4 changes: 2 additions & 2 deletions panoptica/panoptica_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def _evaluate_group(
save_group_times: bool = False,
) -> PanopticaResult:
assert isinstance(label_group, LabelGroup)
if self.__save_group_times:
if self.__save_group_times or save_group_times:
start_time = perf_counter()

prediction_arr_grouped = label_group(processing_pair.prediction_arr)
Expand Down Expand Up @@ -225,7 +225,7 @@ def _evaluate_group(
verbose=True if verbose is None else verbose,
verbose_calc=self.__verbose if verbose is None else verbose,
)
if save_group_times:
if self.__save_group_times or save_group_times:
duration = perf_counter() - start_time
result.computation_time = duration
return result
Expand Down
41 changes: 37 additions & 4 deletions panoptica/panoptica_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@
class ValueSummary:
def __init__(self, value_list: list[float]) -> None:
self.__value_list = value_list
self.__avg = float(np.average(value_list))
self.__std = float(np.std(value_list))
self.__min = min(value_list)
self.__max = max(value_list)
if len(value_list) == 0:
self.__avg = np.nan
self.__std = np.nan
self.__min = np.nan
self.__max = np.nan
else:
self.__avg = float(np.average(value_list))
self.__std = float(np.std(value_list))
self.__min = min(value_list)
self.__max = max(value_list)

@property
def values(self) -> list[float]:
Expand All @@ -40,6 +46,9 @@ def min(self) -> float:
def max(self) -> float:
return self.__max

def __repr__(self):
return str(self)

def __str__(self):
return f"[{round(self.min, 3)}, {round(self.max, 3)}], avg = {round(self.avg, 3)} +- {round(self.std, 3)}"

Expand Down Expand Up @@ -181,6 +190,30 @@ def get_one_subject(self, subjectname: str):
for g in self.__groupnames
}

def get_one_metric(self, metricname: str):
"""Gets the dictionary mapping the group to the metrics specified
Args:
metricname (str): _description_
Returns:
_type_: _description_
"""
self._assertmetric(metricname)
return {g: self.get(g, metricname) for g in self.__groupnames}

def get_one_group(self, groupname: str):
"""Gets the dictionary mapping metric to values for ONE group
Args:
groupname (str): _description_
Returns:
_type_: _description_
"""
self._assertgroup(groupname)
return {m: self.get(groupname, m) for m in self.__metricnames}

def get_across_groups(self, metric) -> list[float]:
"""Given metric, gives list of all values (even across groups!) Treat with care!
Expand Down

0 comments on commit f44db2b

Please sign in to comment.