Skip to content

Commit

Permalink
Don't short circuit calculators when one of the metric calculations fail
Browse files Browse the repository at this point in the history
  • Loading branch information
nnansters committed Feb 7, 2024
1 parent 89deda2 commit 224b2a0
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 15 deletions.
22 changes: 18 additions & 4 deletions nannyml/drift/univariate/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,27 @@ def _calculate(self, data: pd.DataFrame, *args, **kwargs) -> Result:

for column_name in self.continuous_column_names:
for method in self._column_to_models_mapping[column_name]:
for k, v in _calculate_for_column(chunk.data, column_name, method).items():
row[f'{column_name}_{method.column_name}_{k}'] = v
try:
for k, v in _calculate_for_column(chunk.data, column_name, method).items():
row[f'{column_name}_{method.column_name}_{k}'] = v
except Exception as exc:
self._logger.error(
f"an unexpected exception occurred during calculation of method '{method.display_name}': "
f"{exc}"
)
continue

for column_name in self.categorical_column_names:
for method in self._column_to_models_mapping[column_name]:
for k, v in _calculate_for_column(chunk.data, column_name, method).items():
row[f'{column_name}_{method.column_name}_{k}'] = v
try:
for k, v in _calculate_for_column(chunk.data, column_name, method).items():
row[f'{column_name}_{method.column_name}_{k}'] = v
except Exception as exc:
self._logger.error(
f"an unexpected exception occurred during calculation of method '{method.display_name}': "
f"{exc}"
)
continue

rows.append(row)

Expand Down
20 changes: 15 additions & 5 deletions nannyml/performance_calculation/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,13 @@ def _fit(self, reference_data: pd.DataFrame, *args, **kwargs) -> PerformanceCalc
# data validation is performed during the _fit for each metric

for metric in self.metrics:
metric.fit(reference_data=reference_data, chunker=self.chunker)

try:
metric.fit(reference_data=reference_data, chunker=self.chunker)
except Exception as exc:
self._logger.error(
f"an unexpected error occurred when calculating metric '{metric.display_name}': {exc}"
)
continue
self.previous_reference_data = reference_data

self.result = self._calculate(reference_data)
Expand Down Expand Up @@ -349,9 +354,14 @@ def _calculate(self, data: pd.DataFrame, *args, **kwargs) -> Result:
def _calculate_metrics_for_chunk(self, chunk: Chunk) -> Dict:
chunk_records: Dict[str, Any] = {}
for metric in self.metrics:
chunk_record = metric.get_chunk_record(chunk.data)
chunk_records.update(chunk_record)

try:
chunk_record = metric.get_chunk_record(chunk.data)
chunk_records.update(chunk_record)
except Exception as exc:
self._logger.error(
f"an unexpected error occurred while calculating metric {metric.display_name}: {exc}"
)
continue
return chunk_records


Expand Down
12 changes: 9 additions & 3 deletions nannyml/performance_estimation/confidence_based/cbpe.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,15 @@ def _estimate(self, data: pd.DataFrame, *args, **kwargs) -> Result:
def _estimate_chunk(self, chunk: Chunk) -> Dict:
chunk_records: Dict[str, Any] = {}
for metric in self.metrics:
chunk_record = metric.get_chunk_record(chunk.data)
# add the chunk record to the chunk_records dict
chunk_records.update(chunk_record)
try:
chunk_record = metric.get_chunk_record(chunk.data)
# add the chunk record to the chunk_records dict
chunk_records.update(chunk_record)
except Exception as exc:
self._logger.error(
f"an unexpected error occurred while calculating metric {metric.display_name}: {exc}"
)
continue
return chunk_records

def _fit_binary(self, reference_data: pd.DataFrame) -> CBPE:
Expand Down
18 changes: 15 additions & 3 deletions nannyml/performance_estimation/direct_loss_estimation/dle.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,13 @@ def _fit(self, reference_data: pd.DataFrame, *args, **kwargs) -> Self:

for metric in self.metrics:
metric.categorical_column_names = categorical_feature_columns
metric.fit(reference_data)
try:
metric.fit(reference_data)
except Exception as exc:
self._logger.error(
f"an unexpected error occurred while calculating metric {metric.display_name}: {exc}"
)
continue

self.result = self._estimate(reference_data)
self.result.data[('chunk', 'period')] = 'reference'
Expand Down Expand Up @@ -374,8 +380,14 @@ def _estimate(self, data: pd.DataFrame, *args, **kwargs) -> Result:
def _estimate_chunk(self, chunk: Chunk) -> Dict:
estimates: Dict[str, Any] = {}
for metric in self.metrics:
estimated_metric = metric.estimate(chunk.data)
sampling_error = metric.sampling_error(chunk.data)
try:
estimated_metric = metric.estimate(chunk.data)
sampling_error = metric.sampling_error(chunk.data)
except Exception as exc:
self._logger.error(
f"an unexpected error occurred while calculating metric {metric.display_name}: {exc}"
)
continue

upper_confidence_boundary = estimated_metric + 3 * sampling_error
if metric.upper_threshold_value_limit is not None:
Expand Down

0 comments on commit 224b2a0

Please sign in to comment.