rExtractForest function

String rExtractForest(
  1. String log,
  2. WidgetRef ref
)

Extract from the R log lines of output from the random forest.

Implementation

String rExtractForest(
  String log,
  WidgetRef ref,
) {
  AlgorithmType forestAlgorithm =
      ref.read(algorithmForestProvider.notifier).state;

  String extract = _basicTemplate(log, ref);

  extract = extract.replaceAll('Call:\n', '');

  if (forestAlgorithm == AlgorithmType.traditional) {
    // Nicely format the call to randomForest.

    extract = extract.replaceAllMapped(
      RegExp(
        r'\n (randomForest\(.*?)\)',
        multiLine: true,
        dotAll: false,
      ),
      (match) {
        // The first group is then the whole randomForest(...) call.

        String txt = match.group(1) ?? '';

        txt = txt.replaceAll('\n', '');
        txt = txt.replaceAll(RegExp(r',\s*m'), ', m');

        txt = txt.replaceAllMapped(
          RegExp(r'(\w+)\s*=\s*([^,]+),'),
          (match) {
            return '\n    ${match.group(1)}=${match.group(2)},';
          },
        );

        txt = txt.replaceAll(' = ', '=');

        return '\n$txt\n)\n';
      },
    );

    extract = extract.replaceAll(
      '\nConfusion matrix:\n',
      '\n\nConfusion matrix:\n\n',
    );

    extract = extract.replaceAll('\nArea under', '\n\nArea under');
  } else if (forestAlgorithm == AlgorithmType.conditional) {
    // Nicely format the call to model_conditionalForest.

    extract = extract.replaceAllMapped(
      RegExp(
        r'\n (model_conditionalForest\(.*?)\)',
        multiLine: true,
        dotAll: false,
      ),
      (match) {
        String txt = match.group(1) ?? '';

        txt = txt.replaceAll('\n', '');
        txt = txt.replaceAll(RegExp(r',\s*m'), ', m');

        txt = txt.replaceAllMapped(
          RegExp(r'(\w+)\s*=\s*([^,]+),'),
          (match) {
            return '\n    ${match.group(1)}=${match.group(2)},';
          },
        );

        txt = txt.replaceAll(' = ', '=');

        return '\n$txt\n)\n';
      },
    );
  }

  return extract;
}