<commit_before>/*
 * Copyright 2006-2021 The MZmine Development Team
 *
 * This file is part of MZmine.
 *
 * MZmine is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * MZmine is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with MZmine; if not,
 * write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

package io.github.mzmine.util;

import com.google.common.collect.Range;
import io.github.mzmine.datamodel.features.FeatureList;
import io.github.mzmine.datamodel.features.FeatureList.FeatureListAppliedMethod;
import io.github.mzmine.datamodel.features.FeatureListRow;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class FeatureListUtils {

  /**
   * Copies the FeatureListAppliedMethods from <b>source</b> to <b>target</b>
   *
   * @param source The source feature list.
   * @param target the target feature list.
   */
  public static void copyPeakListAppliedMethods(FeatureList source, FeatureList target) {
    for (FeatureListAppliedMethod proc : source.getAppliedMethods()) {
      target.addDescriptionOfAppliedTask(proc);
    }
  }

  /**
   * @param rows       The rows to search.
   * @param rtRange    The rt range.
   * @param mzRange    The m/z range.
   * @param sortedByMz If the list is sorted by m/z (ascending)
   * @param <T>
   * @return List of matching rows.
   */
  @NotNull
  public static <T extends FeatureListRow> List<T> getRows(@NotNull final List<T> rows,
      @NotNull final Range<Float> rtRange, @NotNull final Range<Double> mzRange,
      boolean sortedByMz) {
    final List<T> validRows = new ArrayList<>();

    if (sortedByMz) {
      final double lower = mzRange.lowerEndpoint();
      final double upper = mzRange.upperEndpoint();

      for (T row : rows) {
        final double mz = row.getAverageMZ();
        if (mz < lower) {
          continue;
        } else if (mz > upper) {
          break;
        }
        if (rtRange.contains(row.getAverageRT())) {
          validRows.add(row);
        }
      }
    } else {
      for (T row : rows) {
        if (mzRange.contains(row.getAverageMZ()) && rtRange.contains(row.getAverageRT())) {
          validRows.add(row);
        }
      }
    }

    return validRows;
  }

  /**
   * @param rows          The rows to search.
   * @param rtRange       The rt range.
   * @param mzRange       The m/z range.
   * @param mobilityRange The mobilityRange.
   * @param sortedByMz    If the list is sorted by m/z (ascending)
   * @param <T>
   * @return List of matching rows.
   */
  @NotNull
  public static <T extends FeatureListRow> List<T> getRows(@NotNull final List<T> rows,
      @NotNull final Range<Float> rtRange, @NotNull final Range<Double> mzRange,
      @NotNull final Range<Float> mobilityRange, boolean sortedByMz) {
    final List<T> validRows = new ArrayList<>();

    if (sortedByMz) {
      final double lower = mzRange.lowerEndpoint();
      final double upper = mzRange.upperEndpoint();

      for (T row : rows) {
        final double mz = row.getAverageMZ();
        if (mz < lower) {
          continue;
        } else if (mz > upper) {
          break;
        }
        if (rtRange.contains(row.getAverageRT()) && (row.getAverageMobility() == null
            || mobilityRange.contains(row.getAverageMobility()))) {
          validRows.add(row);
        }
      }
    } else {
      for (T row : rows) {
        if (mzRange.contains(row.getAverageMZ()) && rtRange.contains(row.getAverageRT())) {
          validRows.add(row);
        }
      }
    }

    return validRows;
  }

  public static <T extends FeatureListRow> T getBestRow(@NotNull final List<T> rows,
      @Nullable Range<Double> mzRange, @Nullable Range<Float> rtRange,
      @Nullable Range<Float> mobilityRange) {
    return rows.stream()
        .max(Comparator.comparingDouble(r -> getScore(r, mzRange, rtRange, mobilityRange)))
        .orElse(null);
  }

  public static double getScore(FeatureListRow row, @Nullable Range<Double> mzRange,
      @Nullable Range<Float> rtRange, @Nullable Range<Float> mobilityRange) {

    // don't score range.all, will distort the scoring.
    mzRange = mzRange == null || mzRange.equals(Range.all()) ? null : mzRange;
    rtRange = rtRange == null || rtRange.equals(Range.all()) ? null : rtRange;
    mobilityRange =
        mobilityRange == null || mobilityRange.equals(Range.all()) ? null : mobilityRange;

    int scorers = 0;

    double score = 0f;
    final Double exactMass = mzRange != null ? RangeUtils.rangeCenter(mzRange) : null;
    // values are "matched" if the given value exists in this class and falls within the tolerance.
    if (mzRange != null && exactMass != null && !(row.getAverageMZ() == null || !mzRange.contains(
        row.getAverageMZ()))) {
      score +=
          1 - ((Math.abs(row.getAverageMZ() - exactMass)) / (RangeUtils.rangeLength(mzRange) / 2));
      scorers++;
    }

    final Float rt = rtRange != null ? RangeUtils.rangeCenter(rtRange) : null;
    if (rtRange != null && rt != null && !(row.getAverageRT() == null || !mobilityRange.contains(
        row.getAverageRT()))) {
      score += 1 - ((Math.abs(row.getAverageRT() - rt)) / (RangeUtils.rangeLength(rtRange) / 2));
      scorers++;
    }

    final Float mobility = mobilityRange != null ? RangeUtils.rangeCenter(mobilityRange) : null;
    if (mobilityRange != null && mobility != null && !(row.getAverageMobility() == null
        || !mobilityRange.contains(row.getAverageMobility()))) {
      score += 1 - ((Math.abs(row.getAverageMobility() - mobility)) / (
          RangeUtils.rangeLength(mobilityRange) / 2));
      scorers++;
    }

    if (scorers == 0) {
      return 0f;
    }

    return score / scorers;
  }

}
<commit_msg>fix wrong call<commit_after>/*
 * Copyright 2006-2021 The MZmine Development Team
 *
 * This file is part of MZmine.
 *
 * MZmine is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * MZmine is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with MZmine; if not,
 * write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

package io.github.mzmine.util;

import com.google.common.collect.Range;
import io.github.mzmine.datamodel.features.FeatureList;
import io.github.mzmine.datamodel.features.FeatureList.FeatureListAppliedMethod;
import io.github.mzmine.datamodel.features.FeatureListRow;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class FeatureListUtils {

  /**
   * Copies the FeatureListAppliedMethods from <b>source</b> to <b>target</b>
   *
   * @param source The source feature list.
   * @param target the target feature list.
   */
  public static void copyPeakListAppliedMethods(FeatureList source, FeatureList target) {
    for (FeatureListAppliedMethod proc : source.getAppliedMethods()) {
      target.addDescriptionOfAppliedTask(proc);
    }
  }

  /**
   * @param rows       The rows to search.
   * @param rtRange    The rt range.
   * @param mzRange    The m/z range.
   * @param sortedByMz If the list is sorted by m/z (ascending)
   * @param <T>
   * @return List of matching rows.
   */
  @NotNull
  public static <T extends FeatureListRow> List<T> getRows(@NotNull final List<T> rows,
      @NotNull final Range<Float> rtRange, @NotNull final Range<Double> mzRange,
      boolean sortedByMz) {
    final List<T> validRows = new ArrayList<>();

    if (sortedByMz) {
      final double lower = mzRange.lowerEndpoint();
      final double upper = mzRange.upperEndpoint();

      for (T row : rows) {
        final double mz = row.getAverageMZ();
        if (mz < lower) {
          continue;
        } else if (mz > upper) {
          break;
        }
        if (rtRange.contains(row.getAverageRT())) {
          validRows.add(row);
        }
      }
    } else {
      for (T row : rows) {
        if (mzRange.contains(row.getAverageMZ()) && rtRange.contains(row.getAverageRT())) {
          validRows.add(row);
        }
      }
    }

    return validRows;
  }

  /**
   * @param rows          The rows to search.
   * @param rtRange       The rt range.
   * @param mzRange       The m/z range.
   * @param mobilityRange The mobilityRange.
   * @param sortedByMz    If the list is sorted by m/z (ascending)
   * @param <T>
   * @return List of matching rows.
   */
  @NotNull
  public static <T extends FeatureListRow> List<T> getRows(@NotNull final List<T> rows,
      @NotNull final Range<Float> rtRange, @NotNull final Range<Double> mzRange,
      @NotNull final Range<Float> mobilityRange, boolean sortedByMz) {
    final List<T> validRows = new ArrayList<>();

    if (sortedByMz) {
      final double lower = mzRange.lowerEndpoint();
      final double upper = mzRange.upperEndpoint();

      for (T row : rows) {
        final double mz = row.getAverageMZ();
        if (mz < lower) {
          continue;
        } else if (mz > upper) {
          break;
        }
        if (rtRange.contains(row.getAverageRT()) && (row.getAverageMobility() == null
            || mobilityRange.contains(row.getAverageMobility()))) {
          validRows.add(row);
        }
      }
    } else {
      for (T row : rows) {
        if (mzRange.contains(row.getAverageMZ()) && rtRange.contains(row.getAverageRT())) {
          validRows.add(row);
        }
      }
    }

    return validRows;
  }

  public static <T extends FeatureListRow> T getBestRow(@NotNull final List<T> rows,
      @Nullable Range<Double> mzRange, @Nullable Range<Float> rtRange,
      @Nullable Range<Float> mobilityRange) {
    return rows.stream()
        .max(Comparator.comparingDouble(r -> getScore(r, mzRange, rtRange, mobilityRange)))
        .orElse(null);
  }

  public static double getScore(FeatureListRow row, @Nullable Range<Double> mzRange,
      @Nullable Range<Float> rtRange, @Nullable Range<Float> mobilityRange) {

    // don't score range.all, will distort the scoring.
    mzRange = mzRange == null || mzRange.equals(Range.all()) ? null : mzRange;
    rtRange = rtRange == null || rtRange.equals(Range.all()) ? null : rtRange;
    mobilityRange =
        mobilityRange == null || mobilityRange.equals(Range.all()) ? null : mobilityRange;

    int scorers = 0;

    double score = 0f;
    final Double exactMass = mzRange != null ? RangeUtils.rangeCenter(mzRange) : null;
    // values are "matched" if the given value exists in this class and falls within the tolerance.
    if (mzRange != null && exactMass != null && !(row.getAverageMZ() == null || !mzRange.contains(
        row.getAverageMZ()))) {
      score +=
          1 - ((Math.abs(row.getAverageMZ() - exactMass)) / (RangeUtils.rangeLength(mzRange) / 2));
      scorers++;
    }

    final Float rt = rtRange != null ? RangeUtils.rangeCenter(rtRange) : null;
    if (rtRange != null && rt != null && !(row.getAverageRT() == null || !rtRange.contains(
        row.getAverageRT()))) {
      score += 1 - ((Math.abs(row.getAverageRT() - rt)) / (RangeUtils.rangeLength(rtRange) / 2));
      scorers++;
    }

    final Float mobility = mobilityRange != null ? RangeUtils.rangeCenter(mobilityRange) : null;
    if (mobilityRange != null && mobility != null && !(row.getAverageMobility() == null
        || !mobilityRange.contains(row.getAverageMobility()))) {
      score += 1 - ((Math.abs(row.getAverageMobility() - mobility)) / (
          RangeUtils.rangeLength(mobilityRange) / 2));
      scorers++;
    }

    if (scorers == 0) {
      return 0f;
    }

    return score / scorers;
  }

}

{'fraction_non_alphanumeric': 0.11776875944935147, 'fraction_numerical': 0.004774409166865601, 'mean_word_length': 2.8635106055948354, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4895544', 'n_tokens_mistral': 3914, 'n_tokens_neox': 3763, 'n_words': 1106}