<commit_before>import { createSelector } from 'reselect';
import { addNode, infer } from 'bayesjs';

export const getNetwork = state => state.network;
export const getNodes = state => state.nodes;
export const getBeliefs = state => state.network.beliefs;

export const getStateToSave = createSelector(
  getNetwork,
  getNodes,
  (network, nodes) => ({
    network: {
      ...network,
      selectedNodes: [],
      beliefs: {},
    },
    nodes,
  })
);

export const getSelectedNode = createSelector(
  getNetwork,
  getNodes,
  (network, nodes) => {
    if (network.selectedNodes.length !== 1) {
      return null;
    }

    return nodes.find(x => x.id === network.selectedNodes[0]);
  },
);

export const getInferenceResults = createSelector(
  getNodes,
  getBeliefs,
  (nodes, beliefs) => {
    let network = {};

    const remainingNodes = [...nodes];

    while (remainingNodes.length > 0) {
      const nodesToAdd = [];

      for (let i = 0; i < remainingNodes.length; i++) {
        if (remainingNodes[i].parents.every(p => network.hasOwnProperty(p))) {
          nodesToAdd.push(remainingNodes.splice(i, 1)[0]);
        }
      }

      nodesToAdd.forEach(nodeToAdd => {
        network = addNode(network, nodeToAdd);
      });
    }

    const results = {};

    nodes.forEach(node => {
      results[node.id] = {};

      node.states.forEach(state => {
        results[node.id][state] = infer(network, { [node.id]: state }, beliefs);
      });
    });

    return results;
  }
);<commit_msg>Only pass beliefs when needed<commit_after>import { createSelector } from 'reselect';
import { addNode, infer } from 'bayesjs';

export const getNetwork = state => state.network;
export const getNodes = state => state.nodes;
export const getBeliefs = state => state.network.beliefs;

export const getStateToSave = createSelector(
  getNetwork,
  getNodes,
  (network, nodes) => ({
    network: {
      ...network,
      selectedNodes: [],
      beliefs: {},
    },
    nodes,
  })
);

export const getSelectedNode = createSelector(
  getNetwork,
  getNodes,
  (network, nodes) => {
    if (network.selectedNodes.length !== 1) {
      return null;
    }

    return nodes.find(x => x.id === network.selectedNodes[0]);
  },
);

export const getInferenceResults = createSelector(
  getNodes,
  getBeliefs,
  (nodes, beliefs) => {
    let network = {};

    const remainingNodes = [...nodes];

    while (remainingNodes.length > 0) {
      const nodesToAdd = [];

      for (let i = 0; i < remainingNodes.length; i++) {
        if (remainingNodes[i].parents.every(p => network.hasOwnProperty(p))) {
          nodesToAdd.push(remainingNodes.splice(i, 1)[0]);
        }
      }

      nodesToAdd.forEach(nodeToAdd => {
        network = addNode(network, nodeToAdd);
      });
    }

    const results = {};

    nodes.forEach(node => {
      results[node.id] = {};

      node.states.forEach(state => {
        results[node.id][state] = infer(
          network,
          { [node.id]: state },
          Object.keys(beliefs).length === 0 ? undefined : beliefs,
        );
      });
    });

    return results;
  }
);
{'fraction_non_alphanumeric': 0.14940686117345303, 'fraction_numerical': 0.004168002564924655, 'mean_word_length': 2.6491228070175437, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6575849', 'n_tokens_mistral': 978, 'n_tokens_neox': 958, 'n_words': 208}