public static AnomalyResult fromRawTRCFResult()

in src/main/java/org/opensearch/ad/model/AnomalyResult.java [325:449]


    public static AnomalyResult fromRawTRCFResult(
        String detectorId,
        long intervalMillis,
        String taskId,
        Double rcfScore,
        Double grade,
        Double confidence,
        List<FeatureData> featureData,
        Instant dataStartTime,
        Instant dataEndTime,
        Instant executionStartTime,
        Instant executionEndTime,
        String error,
        Entity entity,
        User user,
        Integer schemaVersion,
        String modelId,
        double[] relevantAttribution,
        Integer relativeIndex,
        double[] pastValues,
        double[][] expectedValuesList,
        double[] likelihoodOfValues,
        Double threshold
    ) {
        List<DataByFeatureId> convertedRelevantAttribution = null;
        List<DataByFeatureId> convertedPastValuesList = null;
        List<ExpectedValueList> convertedExpectedValues = null;

        if (grade > 0) {
            int featureSize = featureData.size();
            if (relevantAttribution != null) {
                if (relevantAttribution.length == featureSize) {
                    convertedRelevantAttribution = new ArrayList<>(featureSize);
                    for (int j = 0; j < featureSize; j++) {
                        convertedRelevantAttribution.add(new DataByFeatureId(featureData.get(j).getFeatureId(), relevantAttribution[j]));
                    }
                } else {
                    LOG
                        .error(
                            new ParameterizedMessage(
                                "Attribution array size does not match.  Expected [{}] but got [{}]",
                                featureSize,
                                relevantAttribution.length
                            )
                        );
                }
            }

            if (pastValues != null) {
                if (pastValues.length == featureSize) {
                    convertedPastValuesList = new ArrayList<>(featureSize);
                    for (int j = 0; j < featureSize; j++) {
                        convertedPastValuesList.add(new DataByFeatureId(featureData.get(j).getFeatureId(), pastValues[j]));
                    }
                } else {
                    LOG
                        .error(
                            new ParameterizedMessage(
                                "Past value array size does not match.  Expected [{}] but got [{}]",
                                featureSize,
                                pastValues.length
                            )
                        );
                }
            }

            if (expectedValuesList != null && expectedValuesList.length > 0) {
                int numberOfExpectedLists = expectedValuesList.length;
                int numberOfExpectedVals = expectedValuesList[0].length;
                if (numberOfExpectedVals == featureSize && likelihoodOfValues.length == numberOfExpectedLists) {
                    convertedExpectedValues = new ArrayList<>(numberOfExpectedLists);
                    for (int j = 0; j < numberOfExpectedLists; j++) {
                        List<DataByFeatureId> valueList = new ArrayList<>(featureSize);
                        for (int k = 0; k < featureSize; k++) {
                            valueList.add(new DataByFeatureId(featureData.get(k).getFeatureId(), expectedValuesList[j][k]));
                        }
                        convertedExpectedValues.add(new ExpectedValueList(likelihoodOfValues[j], valueList));
                    }
                } else if (numberOfExpectedVals != featureSize) {
                    LOG
                        .error(
                            new ParameterizedMessage(
                                "expected value array mismatch.  Expected [{}] actual [{}].",
                                featureSize,
                                numberOfExpectedVals
                            )
                        );
                } else {
                    LOG
                        .error(
                            new ParameterizedMessage(
                                "likelihood and expected array mismatch: Likelihood [{}] expected value [{}].",
                                likelihoodOfValues.length,
                                numberOfExpectedLists
                            )
                        );
                }
            }
        }

        return new AnomalyResult(
            detectorId,
            taskId,
            rcfScore,
            grade,
            confidence,
            featureData,
            dataStartTime,
            dataEndTime,
            executionStartTime,
            executionEndTime,
            error,
            entity,
            user,
            schemaVersion,
            modelId,
            (relativeIndex == null || dataStartTime == null)
                ? null
                : Instant.ofEpochMilli(dataStartTime.toEpochMilli() + relativeIndex * intervalMillis),
            convertedRelevantAttribution,
            convertedPastValuesList,
            convertedExpectedValues,
            threshold
        );
    }