Module input

Structure and loading of input files.

class multifit.input.Config(data: DataConfig, model: ModelConfig, fit: FitConfig)

Bases: object

Class holding the full multifit configuration.

See attribute classes for more detail.

data

Configuration for reading the data.

Type:

DataConfig

model

Configuration of the model.

Type:

ModelConfig

fit

Configuration of the fit.

Type:

FitConfig

classmethod from_yaml(file: TextIO | Path | str, error_if_incomplete=True)

Initialize configuration from yaml configuration file.

Parameters:
  • file (TextIO or path or str) – Configuration file to read.

  • error_if_incomplete (bool, default True) – If true, raise an error when the configuration file is incomplete. If false, issue a warning.

class multifit.input.DataConfig(directory, files, bin_width: float, column: int = 0, delimiter: str = None)

Bases: object

Class for configuration of the input data.

Data should be in the form of histograms with equal width bins.

directory

Directory from which to read the data files.

Type:

path

files

List of data files relative to directory

Type:

list of path

bin_width

Bin width of the data histograms.

Type:

float

column

Column of the data file containing the histogram content.

Type:

int

delimiter

Column delimiter for data stored in a text file.

Type:

str

class multifit.input.ModelConfig(module, function: str, number_of_peaks: int, spectrum_params: list[str], peak_params: list[str], kwargs: dict)

Bases: object

Class for configuration of the model

The model consists of a cumulative density function describing a single peak structure. The function may be repeated to model several peak structures. Each function parameter may be shared by all peaks in a spectrum or all instances of a peak across spectra.

module

Path to the python module where the model function is defined.

Type:

path

function

Name of the function defining the model.

Type:

str

number_of_peaks

Number of peak structures to model for each spectrum.

Type:

int

spectrum_params

Name of parameters to be shared by all peaks in a spectrum.

Type:

list of str

peak_params

Name of parameters to be shared by all instances of the same peak across spectra.

Type:

list of str

kwargs

Additional keyword arguments required by the function.

Type:

dict

class multifit.input.FitConfig(range: tuple | list[tuple], parameter_ranges, initial_values: dict[float])

Bases: object

Class for configuration of the fit.

range

Range of data to fit. A single tuple is read as a range. A list of tuples is read as a union of ranges.

Type:

tuple or list of tuple

parameter_ranges

Allowed range for each fit parameter.

Type:

dict of tuple

initial_values

Initial values for each fit parameter.

Type:

dict[float]

multifit.input.fetch_data(config: DataConfig, cls, sorting_key=0, npy=True) → list[Spectrum | CoincidenceMatrix]

Read data files.

WARNING: This function will be depracated in the future.

Parameters:
  • config (DataConfig) – Data configuration object.

  • cls ({Spectrum, CoincidenceMatrix}) – Class to read the data into.

  • sorting_key – key to use when sorting the data object list. May be a constant or a callable taking the file name as a positional argument.

  • npy (bool) – If true, try to read the data from a .npy file. If such a file doesn’t exist, generate it. (default True)

Returns:

Sorted list of data objects read from each file.

Return type:

list of cls

multifit.input.import_cdf(path: Path, function: str) → callable

Import a function from an arbitrary module.

Parameters:
  • path (path) – Path to the python module containing the function.

  • function (str) – Name of the function to import.

Returns:

The imported function.

Return type:

callable

multifit: simultaneous fitting to multiple data sets.

Copyright (C) 2026 Johannes Sørby Heines

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.

class multifit.model.Model(cdf, *, spectrum_params, peak_params, number_of_peaks)

Bases: object

Manage independent and shared parameters.

cdf

Model describing a peak structure

Type:

callable

number_of_peaks

Number pf peak structures to model

Type:

int

independent_params

parameters which are not shared

Type:

set

unique_params

parameters which are shared accross all peaks and spectra.

Type:

set

spectrum_params

parameters which are shared between peaks within, but not accross, spectra.

Type:

set

peak_params

parameters which are shared accross spectra, but are different for each peak number.

Type:

set

from_config(ModelConfig: config) → Self

Build a class instance from a ModelConfig instance.

make_cdf(peak_number, spectrum_number) → callable

Define the cdf with the signature needed by iminuit.

sum_cdfs(spectrum_number) → callable

Combine all cdfs in a spectrum to one function.

match_param(string, peak_numbers, spectrum_numbers) → list[str]

List parameters matching the arguments.

expand_and_list(string) → list:

Expand a parameter name as a list [name, peak, spectrum].

classmethod from_config(config: ModelConfig) → Self

Build a class instance from a ModelConfig instance.

make_cdf(*, peak_number: int, spectrum_number: int) → callable

Return the cdf with the proper signature.

Define a function to be used by iminuit.cost.ExtendedBinnedNLL, with a signature ensuring the correct sharing of parameters.

Parameters:
  • peak_number (int) – number identifying a specific peak. Peaks with the same peak_number will share peak_params.

  • spectrum_number (int) – number identifying a specific spectrum. Peaks with the same spectrum number will share spectrum_params.

Returns:

Cumulative distribution function with parameter names specifying peak and spectrum numbers.

Return type:

callable

sum_cdfs(spectrum_number) → callable

Sum the cdfs for all peaks in a spectrum to a single cdf.

match_param(string, peak_numbers, spectrum_numbers) → list[str]

List parameters matching string given peak and spectrum numbers.

Parameters:
  • string – On the form <name> or <name>_<peak>_<spectrum>, where <peak> and <spectrum> can be numbers or wildcard ‘*’. The input <name> is equivalent to <name>_*_*.

  • peak_numbers – numbers of the peaks against which string is matched.

  • spectrum_numbers – numbers of the spectra against which string is matched.

Returns:

A list of valid parameter names to the function returned by make_cdf for each combination of peak and spectrum number.

Return type:

list[str]

expand_and_list(string) → list

Expand a parameter name as a list [name, peak, spectrum].

Parameters:

string (str) – On the form <name> or <name>_<peak>_<spectrum>, where <peak> and <spectrum> can be numbers or wildcard ‘*’. The input <name> is equivalent to <name>_*_*

Returns:

A list corresponding to the input string, as [

name: str, peak: int | slice(None), spectrum: int | slice(None) ]

Return type:

list

multifit: simultaneous fitting to multiple data sets.

Copyright (C) 2026 Johannes Sørby Heines

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.

class multifit.data.Data(counts: ArrayLike, bin_edges: ArrayLike, key: str)

Bases: object

Base class for input data.

classmethod equal_bins(*, counts: ArrayLike, bin_width: float, minimum: float = 0, key: str) → Self

Construct an instance of cls with equal width bins.

class multifit.data.Spectrum(*, counts: ArrayLike, bin_edges: ArrayLike, key: str)

Bases: Data

truncate(lower_bin_edge, upper_bin_edge, inplace=False)

Select a range of the spectrum.

Keyword Arguments:
  • lower_bin_edge – first bin edge to include.

  • upper_bin_edge – last bin edge to include.

  • inplace (bool) – if True, modify the current Spectrum and return None, if False create and return a new Spectrum.

class multifit.data.CoincidenceMatrix(*, counts: ArrayLike, bin_edges: ArrayLike, key: str)

Bases: Data

gate(lower: float, upper: float) → Spectrum

Return a Spectrum by gating on the coincidence matrix.

Parameters:
  • lower – lower value of the gate

  • upper – upper value of the gate

  • axis – axis on which to apply the gate.

multifit: simultaneous fitting to multiple data sets.

Copyright (C) 2026 Johannes Sørby Heines

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.