Exemplo n.º 1
0
    def test_my_moment(self):
        x = utils.linspace_step(0, 100, 1)
        y = utils.gauss1D(
            x, 42.321, 13.5
        )  # relation between dx in the gauss1D and in the moment is np.sqrt(4*np.log(2))

        x0, dx = utils.my_moment(x, y)
        assert x0 == pytest.approx(42.321)
        assert dx * np.sqrt(4 * np.log(2)) == pytest.approx(13.5)
Exemplo n.º 2
0
    def do_measurement(self, xmin, xmax, xaxis, data1D, mtype, msubtype):
        try:
            boundaries = utils.find_index(xaxis, [xmin, xmax])
            sub_xaxis = xaxis[boundaries[0][0]:boundaries[1][0]]
            sub_data = data1D[boundaries[0][0]:boundaries[1][0]]
            mtypes = Measurement_type.names()
            if msubtype in self.subitems:
                msub_ind = self.subitems.index(msubtype)

            measurement_results = dict(status=None,
                                       value=0,
                                       xaxis=np.array([]),
                                       datafit=np.array([]))

            if mtype == 'Cursor_Integration':  # "Cursor Intensity Integration":
                if msubtype == "sum":
                    result_measurement = np.sum(sub_data)
                elif msubtype == "mean":
                    result_measurement = np.mean(sub_data)
                elif msubtype == "std":
                    result_measurement = np.std(sub_data)
                else:
                    result_measurement = 0

            elif mtype == 'Max':  # "Max":
                result_measurement = np.max(sub_data)

            elif mtype == 'Min':  # "Min":
                result_measurement = np.min(sub_data)

            elif mtype == 'Gaussian_Fit':  # "Gaussian Fit":
                measurement_results['xaxis'] = sub_xaxis
                offset = np.min(sub_data)
                amp = np.max(sub_data) - np.min(sub_data)
                m = utils.my_moment(sub_xaxis, sub_data)
                p0 = [amp, m[1], m[0], offset]
                popt, pcov = curve_fit(self.eval_func,
                                       sub_xaxis,
                                       sub_data,
                                       p0=p0)
                measurement_results['datafit'] = self.eval_func(
                    sub_xaxis, *popt)
                result_measurement = popt[msub_ind]

            elif mtype == 'Lorentzian_Fit':  # "Lorentzian Fit":
                measurement_results['xaxis'] = sub_xaxis
                offset = np.min(sub_data)
                amp = np.max(sub_data) - np.min(sub_data)
                m = utils.my_moment(sub_xaxis, sub_data)
                p0 = [amp, m[1], m[0], offset]
                popt, pcov = curve_fit(self.eval_func,
                                       sub_xaxis,
                                       sub_data,
                                       p0=p0)
                measurement_results['datafit'] = self.eval_func(
                    sub_xaxis, *popt)
                if msub_ind == 4:  # amplitude
                    result_measurement = popt[0] * 2 / (np.pi * popt[1]
                                                        )  # 2*alpha/(pi*gamma)
                else:
                    result_measurement = popt[msub_ind]

            elif mtype == 'Exponential_Decay_Fit':  # "Exponential Decay Fit":
                ind_x0 = utils.find_index(sub_data, np.max(sub_data))[0][0]
                x0 = sub_xaxis[ind_x0]
                sub_xaxis = sub_xaxis[ind_x0:]
                sub_data = sub_data[ind_x0:]
                offset = min([sub_data[0], sub_data[-1]])
                measurement_results['xaxis'] = sub_xaxis
                N0 = np.max(sub_data) - offset
                t37 = sub_xaxis[utils.find_index(sub_data - offset,
                                                 0.37 * N0)[0][0]] - x0
                #polynome = np.polyfit(sub_xaxis, -np.log((sub_data - 0.99 * offset) / N0), 1)
                p0 = [N0, t37, x0, offset]
                popt, pcov = curve_fit(self.eval_func,
                                       sub_xaxis,
                                       sub_data,
                                       p0=p0)
                measurement_results['datafit'] = self.eval_func(
                    sub_xaxis, *popt)
                result_measurement = popt[msub_ind]

            elif mtype == 'Sinus':  #
                offset = np.mean(sub_data)
                A = (np.max(sub_data) - np.min(sub_data)) / 2
                phi = self.fourierfilt.phase
                dx = 1 / self.fourierfilt.frequency
                measurement_results['xaxis'] = sub_xaxis
                p0 = [A, dx, phi, offset]
                popt, pcov = curve_fit(self.eval_func,
                                       sub_xaxis,
                                       sub_data,
                                       p0=p0)
                measurement_results['datafit'] = self.eval_func(
                    sub_xaxis, *popt)
                result_measurement = popt[msub_ind]

            # elif mtype=="Custom Formula":
            #    #offset=np.min(sub_data)
            #    #amp=np.max(sub_data)-np.min(sub_data)
            #    #m=utils.my_moment(sub_xaxis,sub_data)
            #    #p0=[amp,m[1],m[0],offset]
            #    popt, pcov = curve_fit(self.custom_func, sub_xaxis, sub_data,p0=[140,750,50,15])
            #    self.curve_fitting_sig.emit([sub_xaxis,self.gaussian_func(sub_xaxis,*popt)])
            #    result_measurement=popt[msub_ind]
            else:
                result_measurement = 0

            measurement_results['value'] = result_measurement

            return measurement_results
        except Exception as e:
            result_measurement = 0
            measurement_results['status'] = str(e)
            return measurement_results
Exemplo n.º 3
0
    def update_measurement(self, xmin, xmax, xaxis, data1D, msub_ind):
        try:
            mtype = self.name
            names = self.names()
            boundaries = find_index(xaxis, [xmin, xmax])
            sub_xaxis = xaxis[boundaries[0][0]:boundaries[1][0]]
            sub_data = data1D[boundaries[0][0]:boundaries[1][0]]
            result_measurement = dict(Status=None, datafit=None, xaxis=None)

            if mtype == names[0]:  # Cursor integration:
                if msub_ind == 0:  # sum
                    result_measurement['value'] = np.sum(sub_data)
                elif msub_ind == 1:  # mean
                    result_measurement['value'] = np.mean(sub_data)
                elif msub_ind == 2:  # std
                    result_measurement['value'] = np.std(sub_data)

            elif mtype == names[1]:  # "Max":
                result_measurement['value'] = np.max(sub_data)

            elif mtype == names[2]:  # "Min":
                result_measurement['value'] = np.min(sub_data)

            elif mtype == names[3]:  # "Gaussian Fit":
                offset = np.min(sub_data)
                amp = np.max(sub_data) - np.min(sub_data)
                m = my_moment(sub_xaxis, sub_data)
                p0 = [amp, m[1], m[0], offset]
                popt, pcov = curve_fit(self.gaussian_func,
                                       sub_xaxis,
                                       sub_data,
                                       p0=p0)
                result_measurement['xaxis'] = sub_xaxis
                result_measurement['datafit'] = self.gaussian_func(
                    sub_xaxis, *popt)
                result_measurement['value'] = popt[msub_ind]

            elif mtype == names[4]:  # "Lorentzian Fit":
                offset = np.min(sub_data)
                gamma = 1
                amp = np.max(sub_data) - np.min(sub_data)
                m = my_moment(sub_xaxis, sub_data)
                p0 = [gamma, amp, m[1], m[0], offset]
                popt, pcov = curve_fit(self.laurentzian_func,
                                       sub_xaxis,
                                       sub_data,
                                       p0=p0)
                result_measurement['xaxis'] = sub_xaxis
                result_measurement['datafit'] = self.laurentzian_func(
                    sub_xaxis, *popt)

                if msub_ind == 4:  # amplitude
                    result_measurement['value'] = popt[0] * 2 / (
                        np.pi * popt[1])  # 2*alpha/(pi*gamma)
                else:
                    result_measurement['value'] = popt[msub_ind]
            elif mtype == names[5]:  # "Exponential Decay Fit":
                offset = min([sub_data[0], sub_data[-1]])
                N0 = np.max(sub_data) - offset
                polynome = np.polyfit(sub_xaxis, -np.log(
                    (sub_data - 0.99 * offset) / N0), 1)
                p0 = [N0, polynome[0], offset]
                popt, pcov = curve_fit(self.decaying_func,
                                       sub_xaxis,
                                       sub_data,
                                       p0=p0)
                self.curve_fitting_sig.emit(
                    [True, sub_xaxis,
                     self.decaying_func(sub_xaxis, *popt)])
                result_measurement['xaxis'] = sub_xaxis
                result_measurement['datafit'] = self.decaying_func(
                    sub_xaxis, *popt)
                result_measurement['value'] = popt[msub_ind]

            return (result_measurement)
        except Exception as e:
            result_measurement['Status'] = str(e)
            return result_measurement