HyperSpy API is changing in version 2.0, see the release notes!

Setting the noise properties#

Some data operations require the data variance. Those methods use the metadata.Signal.Noise_properties.variance attribute if it exists. You can set this attribute as in the following example where we set the variance to be 10:

>>> s.metadata.Signal.set_item("Noise_properties.variance", 10) 

You can also use the functions set_noise_variance() and get_noise_variance() for convenience:

>>> s.set_noise_variance(10) 
>>> s.get_noise_variance() 
10

For heteroscedastic noise the variance attribute must be a BaseSignal. Poissonian noise is a common case of heteroscedastic noise where the variance is equal to the expected value. The estimate_poissonian_noise_variance() method can help setting the variance of data with semi-Poissonian noise. With the default arguments, this method simply sets the variance attribute to the given expected_value. However, more generally (although the noise is not strictly Poissonian), the variance may be proportional to the expected value. Moreover, when the noise is a mixture of white (Gaussian) and Poissonian noise, the variance is described by the following linear model:

\[\mathrm{Var}[X] = (a * \mathrm{E}[X] + b) * c\]

Where a is the gain_factor, b is the gain_offset (the Gaussian noise variance) and c the correlation_factor. The correlation factor accounts for correlation of adjacent signal elements that can be modelled as a convolution with a Gaussian point spread function. estimate_poissonian_noise_variance() can be used to set the noise properties when the variance can be described by this linear model, for example:

>>> s = hs.signals.Signal1D(np.ones(100))
>>> s.add_poissonian_noise()
>>> s.metadata
  ├── General
  │   └── title =
  └── Signal
      └── signal_type =

>>> s.estimate_poissonian_noise_variance()
>>> s.metadata
  ├── General
  │   └── title =
  └── Signal
      ├── Noise_properties
      │   ├── Variance_linear_model
      │   │   ├── correlation_factor = 1
      │   │   ├── gain_factor = 1
      │   │   └── gain_offset = 0
      │   └── variance = <BaseSignal, title: Variance of , dimensions: (|100)>
      └── signal_type =