discrete1.ml package

Submodules

discrete1.ml.predict module

discrete1.ml.tools module

Utilities for DJINN model training and inference.

This module provides utilities for data preprocessing, model training, and inference with DJINN (Deep Jointly-Informed Neural Networks) models. It includes functions for cleaning training data, normalizing inputs/outputs, computing error metrics, and managing model lifecycle.

Notes

The utilities support both fission and scatter rate prediction models, with helpers for data splitting, normalization, and error analysis.

discrete1.ml.tools.clean_data_fission(path, labels, splits=None)[source]

Prepare training data for fission rate prediction.

Uses precomputed flux as inputs (X) to compute fission reaction rates as targets (y), and adds a material enrichment label as an extra feature (G+1). Optionally splits and saves data by material group.

Parameters:
  • path (str) – Directory containing files produced by djinn1d.collections(). Expected filenames include flux_fission_model.npy, fission_cross_sections.npy and medium_map.npy.

  • labels (array-like of float) – Material labels (e.g., enrichments) aligned with medium_map.

  • splits (list of (str, list of float), optional) – Optional split specification such as [["hdpe", [15.04]], ["uh3", [0.0, 0.15]]]. When provided, separate datasets are written per split name containing rows whose label matches one of the listed values.

Returns:

Saves processed arrays to path. If splits is None, a single file fission_training_data.npy is written. Otherwise, files of the form fission_<name>_training_data.npy are written.

Return type:

None

discrete1.ml.tools.clean_data_scatter(path, labels, splits=None)[source]

Prepare training data for scatter rate prediction.

Reads one or more flux arrays and computes scattering reaction rates as targets (y), adding a material enrichment label as an extra feature (G+1). Optionally splits and saves data by material group.

Parameters:
  • path (str) – Directory containing files produced by djinn1d.collections(). Expected filenames include scatter_cross_sections.npy, medium_map.npy and one or more files matching flux_scatter_model*.npy.

  • labels (array-like of float) – Material labels (e.g., enrichments) aligned with medium_map.

  • splits (list of (str, list of float), optional) – Optional split specification such as [["hdpe", [15.04]], ["uh3", [0.0, 0.15]]]. When provided, separate datasets are written per split name containing rows whose label matches one of the listed values.

Returns:

Saves processed arrays to path. If splits is None, a single file scatter_training_data.npy is written. Otherwise, files of the form scatter_<name>_training_data.npy are written.

Return type:

None

discrete1.ml.tools.combine_flux_reaction(flux, xs_matrix, medium_map, labels)[source]

Build labeled input/target pairs from flux and cross sections.

Combines group-wise flux with material-specific cross sections to compute reaction rates and attaches a per-cell label. The output packs inputs (flux) and targets (reaction rates) into a single array for convenient downstream saving/splitting.

Parameters:
  • flux (numpy.ndarray, shape (iterations, cells_x, groups)) – Multigroup flux values per iteration and spatial cell.

  • xs_matrix (numpy.ndarray, shape (n_materials, groups, groups)) – Material-dependent cross-section matrices. For a material index m, xs_matrix[m] is multiplied by the flux vector to produce reaction rates.

  • medium_map (numpy.ndarray, shape (cells_x,)) – Integer mapping from spatial cell index to material index used to look up rows in xs_matrix.

  • labels (array-like of float, length ``cells_x``) – Per-cell labels (e.g., enrichments) to store in column 0.

Returns:

Stacked data where the first axis selects input vs target: - index 0: [label, flux_g1, ..., flux_gG] - index 1: [label, rate_g1, ..., rate_gG]

Return type:

numpy.ndarray, shape (2, iterations*cells_x, groups+1)

Notes

Rows with all-zero flux and rate are removed.

discrete1.ml.tools.explained_variance_score(y_true, y_pred)[source]

Calculate explained variance regression score.

Parameters:
  • y_true (numpy.ndarray) – Ground truth values.

  • y_pred (numpy.ndarray) – Predicted values.

Returns:

Explained variance scores, handling NaN/inf cases.

Return type:

numpy.ndarray

discrete1.ml.tools.mean_absolute_error(y_true, y_pred)[source]

Calculate Mean Absolute Error (MAE) between predictions and truth.

Parameters:
  • y_true (numpy.ndarray) – Ground truth values.

  • y_pred (numpy.ndarray) – Predicted values.

Returns:

MAE values averaged over last axis.

Return type:

numpy.ndarray

discrete1.ml.tools.mean_squared_error(y_true, y_pred)[source]

Calculate Mean Squared Error (MSE) between predictions and truth.

Parameters:
  • y_true (numpy.ndarray) – Ground truth values.

  • y_pred (numpy.ndarray) – Predicted values.

Returns:

MSE values averaged over last axis.

Return type:

numpy.ndarray

discrete1.ml.tools.min_max_normalization(data, verbose=False)[source]

Normalize data to [0,1] range using min-max scaling.

Scales each feature to the [0,1] interval by subtracting the minimum and dividing by the range. Handles NaN and infinite values safely.

Parameters:
  • data (numpy.ndarray) – Input data array to normalize.

  • verbose (bool, optional) – If True, return normalization bounds with result.

Returns:

Normalized data array if verbose=False. Tuple of (normalized data, max values, min values) if verbose=True.

Return type:

numpy.ndarray or tuple

discrete1.ml.tools.r2_score(y_true, y_pred)[source]

Calculate R2 score.

Parameters:
  • y_true (numpy.ndarray) – Ground truth values.

  • y_pred (numpy.ndarray) – Predicted values.

Returns:

R2 scores, handling NaN/inf cases.

Return type:

numpy.ndarray

discrete1.ml.tools.root_mean_squared_error(y_true, y_pred)[source]

Calculate Root Mean Squared Error (RMSE).

Parameters:
  • y_true (numpy.ndarray) – Ground truth values.

  • y_pred (numpy.ndarray) – Predicted values.

Returns:

RMSE values.

Return type:

numpy.ndarray

discrete1.ml.tools.root_normalization(data, root)[source]

Apply root normalization to input data.

Parameters:
  • data (numpy.ndarray) – Input data array.

  • root (float) – Root power to apply.

Returns:

Root-normalized data.

Return type:

numpy.ndarray

discrete1.ml.tools.update_cross_sections(xs_matrix, model_idx)[source]

Update cross sections for DJINN model compatibility.

Sums cross sections over specified axes while preserving array shape for materials that are handled by DJINN models.

Parameters:
  • xs_matrix (numpy.ndarray) – Cross section matrix to update.

  • model_idx (sequence) – Material indices handled by DJINN models.

Returns:

Updated cross section matrix with same shape as input.

Return type:

numpy.ndarray

discrete1.ml.train module

discrete1.ml.tune module

Module contents

Machine learning utilities for discrete1.

This optional subpackage provides tools that accelerate the 1D neutron transport solver via data-driven models. It includes helpers for data preparation, training and inference wrappers for DJINN-based models, and common error metrics.

Notes

This subpackage relies on optional dependencies. Install them with:

pip install discrete1[ml]

Dependencies include scikit-learn for data utilities, TensorFlow/Keras for autoencoders used in reduced-order models, and djinn for DJINN models. Only import these when needed to keep the base installation light.