def _estimate_gain(ns, cs, weighted=False, higher_than=None, plot_results=False, binning=0, pol_order=1): if binning > 0: factor = 2 ** binning remainder = np.mod(ns.shape[1], factor) if remainder != 0: ns = ns[:, remainder:] cs = cs[:, remainder:] new_shape = (ns.shape[0], ns.shape[1] / factor) ns = rebin(ns, new_shape) cs = rebin(cs, new_shape) noise = ns - cs variance = np.var(noise, 0) average = np.mean(cs, 0).squeeze() # Select only the values higher_than for the calculation if higher_than is not None: sorting_index_array = np.argsort(average) average_sorted = average[sorting_index_array] average_higher_than = average_sorted > higher_than variance_sorted = variance.squeeze()[sorting_index_array] variance2fit = variance_sorted[average_higher_than] average2fit = average_sorted[average_higher_than] else: variance2fit = variance average2fit = average fit = np.polyfit(average2fit, variance2fit, pol_order) if weighted is True: from hyperspy._signals.spectrum import Spectrum from hyperspy.model import Model from hyperspy.components import Line s = Spectrum(variance2fit) s.axes_manager.signal_axes[0].axis = average2fit m = Model(s) l = Line() l.a.value = fit[1] l.b.value = fit[0] m.append(l) m.fit(weights=True) fit[0] = l.b.value fit[1] = l.a.value if plot_results is True: plt.figure() plt.scatter(average.squeeze(), variance.squeeze()) plt.xlabel('Counts') plt.ylabel('Variance') plt.plot(average2fit, np.polyval(fit, average2fit), color='red') results = {'fit': fit, 'variance': variance.squeeze(), 'counts': average.squeeze()} return results
def test_3d_signal(self): # This code should run smoothly, any exceptions should trigger failure s = self.m_3d.as_signal() model = Model(s) p = hs.components.Polynomial(order=2) model.append(p) p.estimate_parameters(s, 0, 100, only_current=False) np.testing.assert_allclose(p.coefficients.map['values'], np.tile([0.5, 2, 3], (2, 5, 1)))
def _estimate_gain(ns, cs, weighted=False, higher_than=None, plot_results=False, binning=0, pol_order=1): if binning > 0: factor = 2**binning remainder = np.mod(ns.shape[1], factor) if remainder != 0: ns = ns[:, remainder:] cs = cs[:, remainder:] new_shape = (ns.shape[0], ns.shape[1] / factor) ns = rebin(ns, new_shape) cs = rebin(cs, new_shape) noise = ns - cs variance = np.var(noise, 0) average = np.mean(cs, 0).squeeze() # Select only the values higher_than for the calculation if higher_than is not None: sorting_index_array = np.argsort(average) average_sorted = average[sorting_index_array] average_higher_than = average_sorted > higher_than variance_sorted = variance.squeeze()[sorting_index_array] variance2fit = variance_sorted[average_higher_than] average2fit = average_sorted[average_higher_than] else: variance2fit = variance average2fit = average fit = np.polyfit(average2fit, variance2fit, pol_order) if weighted is True: from hyperspy._signals.spectrum import Spectrum from hyperspy.model import Model from hyperspy.components import Line s = Spectrum(variance2fit) s.axes_manager.signal_axes[0].axis = average2fit m = Model(s) l = Line() l.a.value = fit[1] l.b.value = fit[0] m.append(l) m.fit(weights=True) fit[0] = l.b.value fit[1] = l.a.value if plot_results is True: plt.figure() plt.scatter(average.squeeze(), variance.squeeze()) plt.xlabel('Counts') plt.ylabel('Variance') plt.plot(average2fit, np.polyval(fit, average2fit), color='red') results = { 'fit': fit, 'variance': variance.squeeze(), 'counts': average.squeeze() } return results