def _serialize_plots( plots: Tuple[slicer.SliceKeyType, Dict[Text, Any]], post_export_metrics: List[types.AddMetricsCallbackType]) -> bytes: """Converts the given slice plots into serialized proto PlotsForSlice.. Args: plots: The slice plots. post_export_metrics: A list of metric callbacks. This should be the same list as the one passed to tfma.Evaluate(). Returns: The serialized proto PlotsForSlice. """ result = metrics_for_slice_pb2.PlotsForSlice() slice_key, slice_plots = plots if metric_keys.ERROR_METRIC in slice_plots: tf.compat.v1.logging.warning( 'Error for slice: %s with error message: %s ', slice_key, slice_plots[metric_keys.ERROR_METRIC]) metrics = metrics_for_slice_pb2.PlotsForSlice() metrics.slice_key.CopyFrom(slicer.serialize_slice_key(slice_key)) metrics.plots[metric_keys.ERROR_METRIC].debug_message = slice_plots[ metric_keys.ERROR_METRIC] return metrics.SerializeToString() # Convert the slice key. result.slice_key.CopyFrom(slicer.serialize_slice_key(slice_key)) # Convert the slice plots. _convert_slice_plots(slice_plots, post_export_metrics, result.plots) # pytype: disable=wrong-arg-types return result.SerializeToString()
def _serialize_metrics(metrics: Tuple[slicer.SliceKeyType, Dict[Text, Any]], post_export_metrics: List[types.AddMetricsCallbackType] ) -> bytes: """Converts the given slice metrics into serialized proto MetricsForSlice. Args: metrics: The slice metrics. post_export_metrics: A list of metric callbacks. This should be the same list as the one passed to tfma.Evaluate(). Returns: The serialized proto MetricsForSlice. Raises: TypeError: If the type of the feature value in slice key cannot be recognized. """ result = metrics_for_slice_pb2.MetricsForSlice() slice_key, slice_metrics = metrics if metric_keys.ERROR_METRIC in slice_metrics: tf.logging.warning('Error for slice: %s with error message: %s ', slice_key, slice_metrics[metric_keys.ERROR_METRIC]) metrics = metrics_for_slice_pb2.MetricsForSlice() metrics.slice_key.CopyFrom(slicer.serialize_slice_key(slice_key)) metrics.metrics[metric_keys.ERROR_METRIC].debug_message = slice_metrics[ metric_keys.ERROR_METRIC] return metrics.SerializeToString() # Convert the slice key. result.slice_key.CopyFrom(slicer.serialize_slice_key(slice_key)) # Convert the slice metrics. convert_slice_metrics(slice_metrics, post_export_metrics, result) return result.SerializeToString()
def _serialize_metrics( metrics, post_export_metrics): """Converts the given slice metrics into serialized proto MetricsForSlice. Args: metrics: The slice metrics. post_export_metrics: A list of metric callbacks. This should be the same list as the one passed to tfma.Evaluate(). Returns: The serialized proto MetricsForSlice. Raises: TypeError: If the type of the feature value in slice key cannot be recognized. """ result = metrics_for_slice_pb2.MetricsForSlice() slice_key, slice_metrics = metrics # Convert the slice key. result.slice_key.CopyFrom(slicer.serialize_slice_key(slice_key)) # Convert the slice metrics. _convert_slice_metrics(slice_metrics, post_export_metrics, result) return result.SerializeToString()
def testSerializePlots_emptyPlot(self): slice_key = _make_slice_key('fruit', 'apple') tfma_plots = {metric_keys.ERROR_METRIC: 'error_message'} calibration_plot = ( post_export_metrics.calibration_plot_and_prediction_histogram()) actual_plot = metrics_and_plots_serialization._serialize_plots( (slice_key, tfma_plots), [calibration_plot]) expected_plot = metrics_for_slice_pb2.PlotsForSlice() expected_plot.slice_key.CopyFrom(slicer.serialize_slice_key(slice_key)) expected_plot.plots[ metric_keys.ERROR_METRIC].debug_message = 'error_message' self.assertProtoEquals( expected_plot, metrics_for_slice_pb2.PlotsForSlice.FromString(actual_plot))
def testSerializeMetrics_emptyMetrics(self): slice_key = _make_slice_key('age', 5, 'language', 'english', 'price', 0.3) slice_metrics = {metric_keys.ERROR_METRIC: 'error_message'} actual_metrics = metrics_and_plots_serialization._serialize_metrics( (slice_key, slice_metrics), [post_export_metrics.auc(), post_export_metrics.auc(curve='PR')]) expected_metrics = metrics_for_slice_pb2.MetricsForSlice() expected_metrics.slice_key.CopyFrom( slicer.serialize_slice_key(slice_key)) expected_metrics.metrics[ metric_keys.ERROR_METRIC].debug_message = 'error_message' self.assertProtoEquals( expected_metrics, metrics_for_slice_pb2.MetricsForSlice.FromString(actual_metrics))
def _serialize_plots(plots, post_export_metrics): """Converts the given slice plots into serialized proto PlotsForSlice.. Args: plots: The slice plots. post_export_metrics: A list of metric callbacks. This should be the same list as the one passed to tfma.Evaluate(). Returns: The serialized proto PlotsForSlice. """ result = metrics_for_slice_pb2.PlotsForSlice() slice_key, slice_plots = plots # Convert the slice key. result.slice_key.CopyFrom(slicer.serialize_slice_key(slice_key)) # Convert the slice plots. _convert_slice_plots(slice_plots, post_export_metrics, result.plot_data) # pytype: disable=wrong-arg-types return result.SerializeToString()