protected function filter_out_prediction_samples_and_ranges(array &$sampleids, array &$ranges,
            \core_analytics\local\time_splitting\base $timesplitting) {

        if (count($ranges) > 1) {
            throw new \coding_exception('$ranges argument should only contain one range');
        }

        $rangeindex = key($ranges);
        $predictedrange = $this->get_predict_samples_record($timesplitting, $rangeindex);

        if (!$predictedrange) {
            // Nothing to filter out.
            return null;
        }

        $predictedrange->sampleids = json_decode($predictedrange->sampleids, true);
        $missingsamples = array_diff_key($sampleids, $predictedrange->sampleids);
        if (count($missingsamples) === 0) {
            // All samples already calculated.
            unset($ranges[$rangeindex]);
            return null;
        }

        // Replace the list of samples by the one excluding samples that already got predictions at this range.
        $sampleids = $missingsamples;

        return $predictedrange;
    }