File size: 13,702 Bytes
e7eaeed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
from collections import defaultdict
from typing import Any, Dict, List, Optional, Tuple

import pandas as pd
from pytorch_ie import Document, DocumentMetric


class ScoreDistribution(DocumentMetric):
    """Computes the distribution of prediction scores for annotations in a layer. The scores are
    separated into true positives (TP) and false positives (FP) based on the gold annotations.

    Args:
        layer: The name of the annotation layer to analyze.
        per_label: If True, the scores are separated per label. Default is False.
        label_field: The field name of the label to use for separating the scores per label. Default is "label".
        equal_sample_size_binning: If True, the scores are binned into equal sample sizes. If False,
            the scores are binned into equal width. The former is useful when the distribution of scores is skewed.
            Default is True.
        show_plot: If True, a plot of the score distribution is shown. Default is False.
        plotting_backend: The plotting backend to use. Default is "plotly".
        plotting_caption_mapping: A mapping to rename any caption entries for plotting, i.e., the layer name,
            labels, or TP/FP. Default is None.
        plotting_colors: A dictionary mapping from gold scores to colors for plotting. Default is None.
    """

    def __init__(
        self,
        layer: str,
        label_field: str = "label",
        per_label: bool = False,
        show_plot: bool = False,
        equal_sample_size_binning: bool = True,
        plotting_backend: str = "plotly",
        plotting_caption_mapping: Optional[Dict[str, str]] = None,
        plotting_colors: Optional[Dict[str, str]] = None,
        plotly_use_create_distplot: bool = True,
        plotly_barmode: Optional[str] = None,
        plotly_marginal: Optional[str] = "violin",
        plotly_font_size: int = 18,
        plotly_font_family: Optional[str] = None,
        plotly_background_color: Optional[str] = None,
    ):
        super().__init__()
        self.layer = layer
        self.label_field = label_field
        self.per_label = per_label
        self.equal_sample_size_binning = equal_sample_size_binning
        self.plotting_backend = plotting_backend
        self.show_plot = show_plot
        self.plotting_caption_mapping = plotting_caption_mapping or {}
        self.plotting_colors = plotting_colors
        self.plotly_use_create_distplot = plotly_use_create_distplot
        self.plotly_barmode = plotly_barmode
        self.plotly_marginal = plotly_marginal
        self.plotly_font_size = plotly_font_size
        self.plotly_font_family = plotly_font_family
        self.plotly_background_color = plotly_background_color
        self.scores: Dict[str, Dict[str, List[float]]] = defaultdict(lambda: defaultdict(list))

    def reset(self):
        self.scores = defaultdict(lambda: defaultdict(list))

    def _update(self, document: Document):

        gold_annotations = set(document[self.layer])
        for ann in document[self.layer].predictions:
            if self.per_label:
                label = getattr(ann, self.label_field)
            else:
                label = "ALL"
            if ann in gold_annotations:
                self.scores[label]["TP"].append(ann.score)
            else:
                self.scores[label]["FP"].append(ann.score)

    def _combine_scores(
        self,
        scores_tp: List[float],
        score_fp: List[float],
        col_name_pred: str = "prediction",
        col_name_gold: str = "gold",
    ) -> pd.DataFrame:
        scores_tp_df = pd.DataFrame(scores_tp, columns=[col_name_pred])
        scores_tp_df[col_name_gold] = 1.0
        scores_fp_df = pd.DataFrame(score_fp, columns=[col_name_pred])
        scores_fp_df[col_name_gold] = 0.0
        scores_df = pd.concat([scores_tp_df, scores_fp_df])
        return scores_df

    def _get_calibration_data_and_metrics(
        self, scores: pd.DataFrame, q: int = 20
    ) -> Tuple[pd.DataFrame, pd.Series]:
        from sklearn.metrics import brier_score_loss

        if self.equal_sample_size_binning:
            # Create bins with equal number of samples.
            scores["bin"] = pd.qcut(scores["prediction"], q=q, labels=False)
        else:
            # Create bins with equal width.
            scores["bin"] = pd.cut(
                scores["prediction"],
                bins=q,
                include_lowest=True,
                right=True,
                labels=False,
            )

        calibration_data = (
            scores.groupby("bin")
            .apply(
                lambda x: pd.Series(
                    {
                        "avg_score": x["prediction"].mean(),
                        "fraction_positive": x["gold"].mean(),
                        "count": len(x),
                    }
                )
            )
            .reset_index()
        )

        total_count = scores.shape[0]
        calibration_data["bin_weight"] = calibration_data["count"] / total_count

        # Calculate the absolute differences and squared differences.
        calibration_data["abs_diff"] = abs(
            calibration_data["avg_score"] - calibration_data["fraction_positive"]
        )
        calibration_data["squared_diff"] = (
            calibration_data["avg_score"] - calibration_data["fraction_positive"]
        ) ** 2

        # Compute Expected Calibration Error (ECE): weighted average of absolute differences.
        ece = (calibration_data["abs_diff"] * calibration_data["bin_weight"]).sum()

        # Compute Maximum Calibration Error (MCE): maximum absolute difference.
        mce = calibration_data["abs_diff"].max()

        # Compute Mean Squared Error (MSE): weighted average of squared differences.
        mse = (calibration_data["squared_diff"] * calibration_data["bin_weight"]).sum()

        # Compute the Brier Score on the raw predictions.
        brier = brier_score_loss(scores["gold"], scores["prediction"])

        values = {
            "ece": ece,
            "mce": mce,
            "mse": mse,
            "brier": brier,
        }
        return calibration_data, pd.Series(values)

    def calculate_calibration_metrics(self, scores_combined: pd.DataFrame) -> pd.DataFrame:

        calibration_data_dict = {}
        calibration_metrics_dict = {}
        for label, current_scores in scores_combined.groupby("label"):
            calibration_data, calibration_metrics = self._get_calibration_data_and_metrics(
                current_scores, q=20
            )
            calibration_data_dict[label] = calibration_data
            calibration_metrics_dict[label] = calibration_metrics
        all_calibration_data = pd.concat(
            calibration_data_dict, names=["label", "idx"]
        ).reset_index(level=0)
        all_calibration_metrics = pd.concat(calibration_metrics_dict, axis=1).T

        if self.show_plot:
            self.plot_calibration_data(calibration_data=all_calibration_data)

        return all_calibration_metrics

    def calculate_correlation(self, scores: pd.DataFrame) -> pd.Series:
        result_dict = {}
        for label, current_scores in scores.groupby("label"):
            result_dict[label] = current_scores.drop("label", axis=1).corr()["prediction"]["gold"]

        return pd.Series(result_dict, name="correlation")

    @property
    def mapped_layer(self):
        return self.plotting_caption_mapping.get(self.layer, self.layer)

    def plot_score_distribution(self, scores: pd.DataFrame):
        if self.plotting_backend == "plotly":
            for label in scores["label"].unique():
                description = f"Distribution of Predicted Scores for {self.mapped_layer}"
                if self.per_label:
                    label_mapped = self.plotting_caption_mapping.get(label, label)
                    description += f" ({label_mapped})"
                if self.plotly_use_create_distplot:
                    import plotly.figure_factory as ff

                    current_scores = scores[scores["label"] == label]
                    # group by gold score
                    scores_dict = (
                        current_scores.groupby("gold")["prediction"].apply(list).to_dict()
                    )
                    group_labels, hist_data = zip(*scores_dict.items())
                    group_labels_renamed = [
                        self.plotting_caption_mapping.get(label, label) for label in group_labels
                    ]
                    if self.plotting_colors is not None:
                        colors = [
                            self.plotting_colors[group_label] for group_label in group_labels
                        ]
                    else:
                        colors = None
                    fig = ff.create_distplot(
                        hist_data,
                        group_labels=group_labels_renamed,
                        show_hist=True,
                        colors=colors,
                        bin_size=0.025,
                    )
                else:
                    import plotly.express as px

                    fig = px.histogram(
                        scores,
                        x="prediction",
                        color="gold",
                        marginal=self.plotly_marginal,  # "violin",  # or box, violin, rug
                        hover_data=scores.columns,
                        color_discrete_map=self.plotting_colors,
                        nbins=50,
                    )

                fig.update_layout(
                    height=600,
                    width=800,
                    title_text=description,
                    title_x=0.5,
                    font=dict(size=self.plotly_font_size),
                    legend=dict(yanchor="top", y=0.99, xanchor="left", x=0.01),
                )
                if self.plotly_barmode is not None:
                    fig.update_layout(barmode=self.plotly_barmode)
                if self.plotly_font_family is not None:
                    fig.update_layout(font_family=self.plotly_font_family)
                if self.plotly_background_color is not None:
                    fig.update_layout(
                        plot_bgcolor=self.plotly_background_color,
                        paper_bgcolor=self.plotly_background_color,
                    )

                fig.show()
        else:
            raise NotImplementedError(f"Plotting backend {self.plotting_backend} not implemented")

    def plot_calibration_data(self, calibration_data: pd.DataFrame):
        import plotly.express as px
        import plotly.graph_objects as go

        color = "label" if self.per_label else None
        x_col = "avg_score"
        y_col = "fraction_positive"
        fig = px.scatter(
            calibration_data,
            x=x_col,
            y=y_col,
            color=color,
            trendline="ols",
            labels=self.plotting_caption_mapping,
        )
        if not self.per_label:
            fig["data"][1]["name"] = "prediction vs. gold"

        # show legend only for trendlines
        for idx, trace_data in enumerate(fig["data"]):
            if idx % 2 == 0:
                trace_data["showlegend"] = False
            else:
                trace_data["showlegend"] = True

        # add the optimal line
        minimum = calibration_data[x_col].min()
        maximum = calibration_data[x_col].max()
        fig.add_trace(
            go.Scatter(
                x=[minimum, maximum],
                y=[minimum, maximum],
                mode="lines",
                name="optimal",
                line=dict(color="black", dash="dash"),
            )
        )
        fig.update_layout(
            height=600,
            width=800,
            title_text=f"Mean Binned Scores for {self.mapped_layer}",
            title_x=0.5,
            font=dict(size=self.plotly_font_size),
        )
        fig.update_layout(
            legend=dict(
                yanchor="top",
                y=0.99,
                xanchor="left",
                x=0.01,
                title="OLS trendline" + ("s" if self.per_label else ""),
            ),
        )
        if self.plotly_background_color is not None:
            fig.update_layout(
                plot_bgcolor=self.plotly_background_color,
                paper_bgcolor=self.plotly_background_color,
            )

        if self.plotly_font_family is not None:
            fig.update_layout(font_family=self.plotly_font_family)

        fig.show()

    def _compute(self) -> Dict[str, Dict[str, Any]]:
        scores_combined = pd.concat(
            {
                label: self._combine_scores(scores["TP"], scores["FP"])
                for label, scores in self.scores.items()
            },
            names=["label", "idx"],
        ).reset_index(level=0)

        result_df = scores_combined.groupby("label")["prediction"].agg(["mean", "std", "count"])
        if self.show_plot:
            self.plot_score_distribution(scores=scores_combined)

        calibration_metrics = self.calculate_calibration_metrics(scores_combined)
        calibration_metrics["correlation"] = self.calculate_correlation(scores_combined)

        result_df = pd.concat(
            {"prediction": result_df, "prediction vs. gold": calibration_metrics}, axis=1
        )

        if not self.per_label:
            result = result_df.xs("ALL")
        else:
            result = result_df.T.stack().unstack()

        result_dict = {
            main_key: result.xs(main_key).T.to_dict()
            for main_key in result.index.get_level_values(0).unique()
        }

        return result_dict