{"record":{"id":"625c556097133590","repo":"hankcs/HanLP","slug":"mask-not-supported-in-spearmancorrelation-for-now","errorCode":null,"errorMessage":"mask not supported in SpearmanCorrelation for now.","messagePattern":"mask not supported in SpearmanCorrelation for now\\.","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"hanlp/metrics/spearman_correlation.py","lineNumber":69,"sourceCode":"        self.total_predictions = torch.zeros(0)\n        self.total_gold_labels = torch.zeros(0)\n\n    def __call__(\n            self,\n            predictions: torch.Tensor,\n            gold_labels: torch.Tensor,\n            mask=None\n    ):\n        \"\"\"\n        # Parameters\n\n        predictions : `torch.Tensor`, required.\n            A tensor of predictions of shape (batch_size, ...).\n        gold_labels : `torch.Tensor`, required.\n            A tensor of the same shape as `predictions`.\n        \"\"\"\n        if mask is not None:\n            raise NotImplemented('mask not supported in SpearmanCorrelation for now.')\n        # Flatten predictions, gold_labels, and mask. We calculate the Spearman correlation between\n        # the vectors, since each element in the predictions and gold_labels tensor is assumed\n        # to be a separate observation.\n        predictions = predictions.reshape(-1)\n        gold_labels = gold_labels.reshape(-1)\n\n        self.total_predictions = self.total_predictions.to(predictions.device)\n        self.total_gold_labels = self.total_gold_labels.to(gold_labels.device)\n        self.total_predictions = torch.cat((self.total_predictions, predictions), 0)\n        self.total_gold_labels = torch.cat((self.total_gold_labels, gold_labels), 0)\n\n    def reset(self):\n        self.total_predictions = torch.zeros(0)\n        self.total_gold_labels = torch.zeros(0)\n\n    def __str__(self) -> str:\n        return f'spearman: {self.score * 100:.2f}'\n","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/hankcs/HanLP/blob/ddb1299bddff079e447af52ec12549c50636bfa8/hanlp/metrics/spearman_correlation.py#L51-L87","documentation":"SpearmanCorrelation.__call__ rejects a non-None mask argument because mask-aware computation was never implemented for this metric. It raises NotImplemented (itself a bug: NotImplemented is a constant, not an exception, so it technically raises a TypeError at raise-time).","triggerScenarios":"Passing mask=... to a SpearmanCorrelation Metric object, e.g. when a training loop uniformly forwards masks to all metrics including this one.","commonSituations":"Frameworks/allennlp-style training loops that always pass a mask; users copying usage from MaskedAverageAccuracy-style metrics; upgrading code where the metric signature gained a mask parameter.","solutions":["Drop the mask argument when calling SpearmanCorrelation; pre-filter predictions/gold_labels instead.","If masking is genuinely needed, compute it yourself with scipy.stats.spearmanr on the masked elements.","Note for maintainers: the check should raise NotImplementedError, not NotImplemented, otherwise you get a confusing TypeError."],"exampleFix":"# before\nmetric(predictions, gold_labels, mask=mask)\n# after\nmetric(predictions.reshape(-1)[mask.bool()], gold_labels.reshape(-1)[mask.bool()])","handlingStrategy":"type-guard","validationCode":"if mask is not None:\n    predictions = predictions.reshape(-1)[mask.reshape(-1).bool()]\n    gold_labels = gold_labels.reshape(-1)[mask.reshape(-1).bool()]\nmetric(predictions, gold_labels)","typeGuard":"def call_metric(metric, pred, gold, mask=None):\n    import inspect\n    if mask is not None and 'SpearmanCorrelation' in type(metric).__name__:\n        pred, gold = pred.reshape(-1)[mask.reshape(-1).bool()], gold.reshape(-1)[mask.reshape(-1).bool()]\n        mask = None\n    return metric(pred, gold, mask=mask)","tryCatchPattern":"try:\n    metric(pred, gold)\nexcept TypeError:\n    metric(pred.reshape(-1), gold.reshape(-1))  # mask path unsupported","preventionTips":["Never forward mask blindly to every metric; filter by metric capability.","Read the metric's __call__ signature before wiring a shared training loop."],"tags":["nlp","metric","api-misuse","bug"],"backgroundTag":"unsupported-argument","analyzedSha":"ddb1299bddff079e447af52ec12549c50636bfa8","analyzedAt":"2026-08-27T03:36:54.287Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}