exponential_growth module

Define exponential growth model as well as fitting function.

Note

Other models that I tried:

\[ \begin{align}\begin{aligned}N(t) = N_0 (1 + K \cos{(\omega_0 t + \phi_0)}) \mathrm{e}^{\alpha t}\\N(t) = N_0 (1 + K \cos{(\omega_0 t / T_{MP} + \phi_0)}) \mathrm{e}^{\alpha t}\end{aligned}\end{align} \]

I dropped them as with too much unkowns, any model can fit anything.

class ExpGrowthParameters[source]

Bases: TypedDict

Define parameters for exp growth model.

n_0: float

Number of electrons at t=t_0.

alpha: float

Exponential growth factor in \(\mathrm{ns^{-1}}\).

t_0: float

Starting time of exponential growth in \(\mathrm{ns}\).

model: Callable[[ndarray, float, float, float], ndarray]

Exponential growth function.

exp_growth(time, n_0, alpha, t_0=0.0, **kwargs)[source]

Exponential growth factor function.

\[N(t) = N_0 \mathrm{e}^{\alpha (t-t_0)}\]
Parameters:
  • time (np.ndarray) – Time in \(\mathrm{ns}\).

  • n_0 (float) – Number of electrons at the start of the exponential growth.

  • alpha (float) – Exponential growth factor in \(\mathrm{ns^{-1}}\).

  • t_0 (float, optional) – Time at which the exponential growth starts, in \(\mathrm{ns}\). The default is 0.0.

Returns:

population – Modelled population evolution. It is filled with NaN for times before t_0.

Return type:

np.ndarray

exp_growth_log(time, n_0, alpha, t_0=0.0, **kwargs)[source]

Exponential growth factor function, in log form.

\[\log{N(t)} = \log{N_0} + \alpha (t-t_0)\]

In general, better results for the fit process than the classic exp_growth().

Parameters:
  • time (np.ndarray) – Time in \(\mathrm{ns}\).

  • n_0 (float) – Number of electrons at the start of the exponential growth.

  • alpha (float) – Exponential growth factor in \(\mathrm{ns^{-1}}\).

  • t_0 (float, optional) – Time at which the exponential growth starts, in \(\mathrm{ns}\). The default is 0.0.

Returns:

log_population – Log of modelled population evolution. It is filled with NaN for times before t_0.

Return type:

np.ndarray

fit_alpha(time, population, fitting_range, period, running_mean=True, log_fit=True, minimum_final_number_of_electrons=0, bounds=([1e-10, -10.0], [inf, 10.0]), initial_values=[0.0, 0.0], **kwargs)[source]

Perform the exponential growth fitting.

Parameters:
  • time (np.ndarray) – Time in \(\mathrm{ns}\).

  • population (np.ndarray) – Evolution of electron population with time.

  • fitting_range (float) – Time over which the exp growth is searched. Longer is better, but you do not want to start the fit before the exp growth starts.

  • running_mean (bool, optional) – To tell if you want to average the number of particles over one period. Highly recommended. The default is True.

  • log_fit (bool, optional) – To perform the fit on exp_growth_log() rather than exp_growth(). The default is True, as it generally shows better convergence.

  • minimum_final_number_of_electrons (int, optional) – Under this final number of electrons, we do no bother finding the exp growth factor and return a NaN.

  • bounds (tuple[list[float], list[float]], optional) – Upper bound and lower bound for the two variables: initial number of electrons, exp growth factor.

  • initial_values (list[float | None], optional) – Initial values for the two variables: initial number of electrons, exp growth factor.

  • kwargs – Other keyword arguments passed to the curve_fit function.

  • period (float)

Returns:

exp_growth_parameters – Holds the fit parameters.

Return type:

ExpGrowthParameters

_to_fit(time, population, fitting_range, log_fit=True)[source]

Determine the x, f(x) arrays as well as f for the fit.

Parameters:
  • time (np.ndarray) – Full array of times.

  • population (np.ndarray) – Corresponding population evolution,

  • fitting_range (float) – Time over which the exp growth is searched. Longer is better, but you do not want to start the fit before the exp growth starts.

  • log_fit (bool, optional) – To perform the fit on exp_growth_log() rather than exp_growth(). The default is True, as it generally shows better convergence.

Return type:

tuple[Callable, ndarray, ndarray]

Returns:

  • fit_func (Callable) – The f we want to retrieve parameters.

  • fit_time (np.ndarray) – The x values for the fit.

  • fit_pop (np.ndarray) – The f(x) we will try to retrieve.

_indexes_for_fit(time, population, fitting_range, minimum_number_of_points=5)[source]

Determine the indexes on which the fit should be performed.

The fit is performed over fitting_range, ending at the last non-zero population count (first zero-population count is excluded to avoid messing with log(population)).

Parameters:
Return type:

range

_n_points_in_a_period(time, period, min_points_per_period=5)[source]

Get the number of data points in a single RF period.

Print a warning if the number of points is lower than min_points_per_period.

Parameters:
Return type:

int

_design_space(log_fit, fit_pop, bounds=([1e-10, -10.0], [inf, 10.0]), initial_values=[0.0, 0.0])[source]

Set initial value and bounds.

As for now, only set the initial value of n_0 to the population count at the start of the fit.

Note

n_0 is the actual population count, not the log of the pop count.

Parameters:
Return type:

tuple[tuple[list[float], list[float]], list[float]]

_smoothen(population, width)[source]

Smooth data (running mean).

Used to compensate the periodic population oscillations (period: RF period).

See also: https://stackoverflow.com/a/43200476/12188681

Todo

Better unit testing for this function.

Parameters:
Return type:

ndarray