|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +from astropy.nddata.nduncertainty import ( |
| 4 | + IncompatibleUncertaintiesException, |
| 5 | + NDUncertainty, |
| 6 | + VarianceUncertainty, |
| 7 | + _VariancePropagationMixin, |
| 8 | +) |
| 9 | + |
| 10 | +__all__ = ["PoissonUncertainty"] |
| 11 | + |
| 12 | + |
| 13 | +class PoissonUncertainty(_VariancePropagationMixin, NDUncertainty): |
| 14 | + """Poissonian uncertainty assuming first order error propagation. |
| 15 | +
|
| 16 | + This class implements uncertainty propagation for ``addition``, |
| 17 | + ``subtraction``, ``multiplication`` and ``division`` with other instances |
| 18 | + of `Poisson`. The class can handle if the uncertainty has a |
| 19 | + unit that differs from (but is convertible to) the parents `NDData` unit. |
| 20 | + The unit of the resulting uncertainty will have the same unit as the |
| 21 | + resulting data. Also support for correlation is possible but requires the |
| 22 | + correlation as input. It cannot handle correlation determination itself. |
| 23 | +
|
| 24 | + Parameters |
| 25 | + ---------- |
| 26 | + args, kwargs : |
| 27 | + see `NDUncertainty` |
| 28 | +
|
| 29 | + Examples |
| 30 | + -------- |
| 31 | + `Poisson` should always be associated with an `NDData`-like |
| 32 | + instance, either by creating it during initialization:: |
| 33 | +
|
| 34 | + >>> from sunkit_spex.spectrum.uncertainty import PoissonUncertainty |
| 35 | + >>> ndd = NDData([1,2,3], unit='m', |
| 36 | + ... uncertainty=PoissonUncertainty([0.1, 0.1, 0.1])) |
| 37 | + >>> ndd.uncertainty # doctest: +FLOAT_CMP |
| 38 | + PoissonUncertainty([0.1, 0.1, 0.1]) |
| 39 | +
|
| 40 | + or by setting it manually on the `NDData` instance:: |
| 41 | +
|
| 42 | + >>> ndd.uncertainty = PoissonUncertainty([0.2], unit='m', copy=True) |
| 43 | + >>> ndd.uncertainty # doctest: +FLOAT_CMP |
| 44 | + PoissonUncertainty([0.2]) |
| 45 | +
|
| 46 | + the uncertainty ``array`` can also be set directly:: |
| 47 | +
|
| 48 | + >>> ndd.uncertainty.array = 2 |
| 49 | + >>> ndd.uncertainty |
| 50 | + PoissonUncertainty(2) |
| 51 | +
|
| 52 | + .. note:: |
| 53 | + The unit will not be displayed. |
| 54 | + """ |
| 55 | + |
| 56 | + @property |
| 57 | + def supports_correlated(self): |
| 58 | + """`True` : `StdDevUncertainty` allows to propagate correlated \ |
| 59 | + uncertainties. |
| 60 | +
|
| 61 | + ``correlation`` must be given, this class does not implement computing |
| 62 | + it by itself. |
| 63 | + """ |
| 64 | + return True |
| 65 | + |
| 66 | + @property |
| 67 | + def uncertainty_type(self): |
| 68 | + """``"poisson"`` : `PoissonUncertainty` implements Poisson uncertainty.""" |
| 69 | + return "poisson" |
| 70 | + |
| 71 | + def _convert_uncertainty(self, other_uncert): |
| 72 | + if isinstance(other_uncert, PoissonUncertainty): |
| 73 | + return other_uncert |
| 74 | + else: |
| 75 | + raise IncompatibleUncertaintiesException |
| 76 | + |
| 77 | + def _propagate_add(self, other_uncert, result_data, correlation): |
| 78 | + return super()._propagate_add_sub( |
| 79 | + other_uncert, |
| 80 | + result_data, |
| 81 | + correlation, |
| 82 | + subtract=False, |
| 83 | + to_variance=np.square, |
| 84 | + from_variance=np.sqrt, |
| 85 | + ) |
| 86 | + |
| 87 | + def _propagate_subtract(self, other_uncert, result_data, correlation): |
| 88 | + return super()._propagate_add_sub( |
| 89 | + other_uncert, |
| 90 | + result_data, |
| 91 | + correlation, |
| 92 | + subtract=True, |
| 93 | + to_variance=np.square, |
| 94 | + from_variance=np.sqrt, |
| 95 | + ) |
| 96 | + |
| 97 | + def _propagate_multiply(self, other_uncert, result_data, correlation): |
| 98 | + return super()._propagate_multiply_divide( |
| 99 | + other_uncert, |
| 100 | + result_data, |
| 101 | + correlation, |
| 102 | + divide=False, |
| 103 | + to_variance=np.square, |
| 104 | + from_variance=np.sqrt, |
| 105 | + ) |
| 106 | + |
| 107 | + def _propagate_divide(self, other_uncert, result_data, correlation): |
| 108 | + return super()._propagate_multiply_divide( |
| 109 | + other_uncert, |
| 110 | + result_data, |
| 111 | + correlation, |
| 112 | + divide=True, |
| 113 | + to_variance=np.square, |
| 114 | + from_variance=np.sqrt, |
| 115 | + ) |
| 116 | + |
| 117 | + def _propagate_collapse(self, numpy_operation, axis): |
| 118 | + # defer to _VariancePropagationMixin |
| 119 | + return super()._propagate_collapse(numpy_operation, axis=axis) |
| 120 | + |
| 121 | + def _data_unit_to_uncertainty_unit(self, value): |
| 122 | + return value |
| 123 | + |
| 124 | + def _convert_to_variance(self): |
| 125 | + new_array = None if self.array is None else self.array**2 |
| 126 | + new_unit = None if self.unit is None else self.unit**2 |
| 127 | + return VarianceUncertainty(new_array, unit=new_unit) |
| 128 | + |
| 129 | + @classmethod |
| 130 | + def _convert_from_variance(cls, var_uncert): |
| 131 | + new_array = None if var_uncert.array is None else var_uncert.array ** (1 / 2) |
| 132 | + new_unit = None if var_uncert.unit is None else var_uncert.unit ** (1 / 2) |
| 133 | + return cls(new_array, unit=new_unit) |
0 commit comments