calculateEpisodesOfLowAndHighVariation method

void calculateEpisodesOfLowAndHighVariation()

Implementation

void calculateEpisodesOfLowAndHighVariation() {
  /*if(bpmList.length/correctionCount <2){
          //todo: remove
      }*/

  try {
    int minutes =
        (cleanBaselineEpoch.length / NO_OF_SAMPLES_PER_MINUTE).truncate();
    if (minutes == 0) return;
    List<int?> minuteRanges = List.filled(minutes, null, growable: false);
    for (int interval = 0; interval < minutes; interval++) {
      int max = 0;
      int min = 0;
      int start = interval * NO_OF_SAMPLES_PER_MINUTE;
      int limit = ((1 + interval) * NO_OF_SAMPLES_PER_MINUTE).truncate();
      for (int i = start; i < limit; i++) {
        if (cleanMillisecondsEpochBpm[i]! < cleanBaselineEpochBpm[i]! - 30 ||
            cleanMillisecondsEpochBpm[i]! > 210) continue;

        int diff = cleanMillisecondsEpochBpm[i]! - cleanBaselineEpochBpm[i]!;
        if (diff > 0) {
          if (max < diff) max = diff;
        } else {
          if (min > diff) min = diff;
        }
      }

      minuteRanges[interval] = (max).abs() + (min).abs();
    }

    int errorCount = 0;
    for (int i = 0; i < minutes; i++) {
      if (minuteRanges[i] == 0) errorCount++;
      longTermVariation += minuteRanges[i]!;
    }
    longTermVariation =
        (longTermVariation / (minutes - errorCount)).truncate();

    double meanMinuteRange = 0,
        sum = 0,
        meanLowEpisodeBpm = 0,
        meanHighEpisodeBpm = 0;
    int highEpisodes = 0, lowEpisodes = 0, howManyLow = 0, howManyHigh = 0;
    int meanBpm = 0;

    for (int i = 0; i < minutes - 6; i++) {
      for (int j = i; j < i + 6; j++) {
        sum += minuteRanges[j]!;
      }

      meanMinuteRange = sum / 6;
      sum = 0;

      for (int j = i; j < i + 6; j++) {
        if ((meanMinuteRange * 6 - minuteRanges[j]!) / 5 <= 30) {
          howManyLow++;
        }

        if ((meanMinuteRange * 6 - minuteRanges[j]!) / 5 >= 32) {
          howManyHigh++;
        }
      }

      if (howManyLow >= 5) {
        lowEpisodes++;
        meanLowEpisodeBpm += meanMinuteRange;
      }

      if (howManyHigh >= 5) {
        // ignoring correlation and distribution of original traces for now
        highEpisodes++;
        meanHighEpisodeBpm += meanMinuteRange;
        if (gestAge <= 25) {
          // Do nothing
        } else if (meanHighEpisodeBpm / highEpisodes <
            highFHREpisodePercentiles[gestAge - 26][1]) {
          // checking 3rd percentile criteria

          highEpisodes--;
          meanHighEpisodeBpm -= meanMinuteRange;
        }
      }

      howManyLow = howManyHigh = 0;
    }
    int lengthOfHighFHREpisodes = highEpisodes;
    int lengthOfLowFHREpisodes = lowEpisodes;
  } catch (ex) {
    print(ex.toString());
  }
}