binny.utils.io module#

I/O utilities.

binny.utils.io.load_binning_recipe(path: str) list[dict[str, Any]]#

Load a mixed-binning recipe from a YAML file.

Expected YAML structure:

name: "y10_mixed_scheme"
n_bins: 5
segments:
  - method: "eq"            # alias for equidistant
    n_bins: 3
    params:
      x_min: 0.0
      x_max: 1.0
  - method: "equal_number"
    n_bins: 2

params is optional; any keys in params are passed through to mixed_edges. Arrays like x, weights, z, chi are NOT stored in YAML; they are provided at runtime to mixed_edges.

binny.utils.io.load_nz(filename: str | Path, *, x_col: int = 0, nz_col: int = 1, key: str | None = None, delimiter: str | None = None) tuple[ndarray, ndarray]#

Load a 1D redshift grid z and corresponding n(z) from file.

Supported formats:
  • .npy : either shape (N, 2) array (z, n(z)) or separate dict-like with fields

  • .npzif key is provided, expects an array with shape (N, 2) under that key;

    otherwise tries the first array it finds with shape (N, 2)

  • .txt/.dat/.csvplain text with at least two columns; x_col and nz_col

    control which columns to use.

The function does NOT normalise n(z); normalisation is handled later by build_photoz_bins / build_specz_bins via the normalize_input flag.

Parameters:
  • filename – Path to the file on disk.

  • x_col – Column index for the redshift axis in text-based files (default: 0).

  • nz_col – Column index for n(z) in text-based files (default: 1).

  • key – For .npz files, optional key selecting the stored array. If None, the first array with shape (N, 2) is used.

  • delimiter – Optional delimiter passed to np.loadtxt for text/csv files. If None, numpy will try to guess.

Returns:

Two 1D arrays, z and nz, with the same length, sorted in

ascending z.

Raises:

ValueError – If the file format is unsupported or content cannot be interpreted as a (z, n(z)) pair.

binny.utils.io.load_yaml(source: str | Path, *, package: str | None = None) dict[str, Any]#

Loads YAML from disk or from a packaged resource.

Parameters:
  • source – If package is None: treated as a filesystem path. If package is provided: treated as a filename inside that package.

  • package – Package name for packaged YAML, e.g. "binny.surveys.configs". If None, load from disk.

Returns:

Parsed YAML as a top-level mapping.

Raises:
  • FileNotFoundError – If the file does not exist.

  • ValueError – If the YAML root is not a mapping (dict-like).