Exemple #1
0
    def _create_plot_pr(self, estimate=False):
        """
        Create and prepare invertor instance from
        a plottable data set.

        :param path: path of the file to read in

        """
        # Sanity check
        if self.current_plottable is None:
            msg = "Please load a valid data set before proceeding."
            wx.PostEvent(self.parent, StatusEvent(status=msg))
            return None

        # Get the data from the chosen data set and perform inversion
        pr = Invertor()
        pr.d_max = self.max_length
        pr.alpha = self.alpha
        pr.q_min = self.q_min
        pr.q_max = self.q_max
        pr.x = self.current_plottable.x
        pr.y = self.current_plottable.y
        pr.est_bck = self.est_bck
        pr.slit_height = self.slit_height
        pr.slit_width = self.slit_width
        pr.background = self.bck_val

        # Keep track of the plot window title to ensure that
        # we can overlay the plots
        pr.info["plot_group_id"] = self.current_plottable.group_id

        # Fill in errors if none were provided
        err = self.current_plottable.dy
        all_zeros = True
        if err is None:
            err = np.zeros(len(pr.y))
        else:
            for i in range(len(err)):
                if err[i] > 0:
                    all_zeros = False

        if all_zeros:
            scale = None
            min_err = 0.0
            for i in range(len(pr.y)):
                # Scale the error so that we can fit over several decades of Q
                if scale is None:
                    scale = 0.05 * math.sqrt(pr.y[i])
                    min_err = 0.01 * pr.y[i]
                err[i] = scale * math.sqrt(math.fabs(pr.y[i])) + min_err
            message = "The loaded file had no error bars, "
            message += "statistical errors are assumed."
            wx.PostEvent(self.parent, StatusEvent(status=message))

        pr.err = err

        return pr
Exemple #2
0
    def _create_plot_pr(self, estimate=False):
        """
        Create and prepare invertor instance from
        a plottable data set.

        :param path: path of the file to read in

        """
        # Sanity check
        if self.current_plottable is None:
            msg = "Please load a valid data set before proceeding."
            wx.PostEvent(self.parent, StatusEvent(status=msg))
            return None

        # Get the data from the chosen data set and perform inversion
        pr = Invertor()
        pr.d_max = self.max_length
        pr.alpha = self.alpha
        pr.q_min = self.q_min
        pr.q_max = self.q_max
        pr.x = self.current_plottable.x
        pr.y = self.current_plottable.y
        pr.est_bck = self.est_bck
        pr.slit_height = self.slit_height
        pr.slit_width = self.slit_width
        pr.background = self.bck_val

        # Keep track of the plot window title to ensure that
        # we can overlay the plots
        pr.info["plot_group_id"] = self.current_plottable.group_id

        # Fill in errors if none were provided
        err = self.current_plottable.dy
        all_zeros = True
        if err is None:
            err = np.zeros(len(pr.y))
        else:
            for i in range(len(err)):
                if err[i] > 0:
                    all_zeros = False

        if all_zeros:
            scale = None
            min_err = 0.0
            for i in range(len(pr.y)):
                # Scale the error so that we can fit over several decades of Q
                if scale is None:
                    scale = 0.05 * math.sqrt(pr.y[i])
                    min_err = 0.01 * pr.y[i]
                err[i] = scale * math.sqrt(math.fabs(pr.y[i])) + min_err
            message = "The loaded file had no error bars, "
            message += "statistical errors are assumed."
            wx.PostEvent(self.parent, StatusEvent(status=message))

        pr.err = err

        return pr
Exemple #3
0
    def _fit_pr(self, evt):
        """
        """
        # Generate P(r) for sphere
        radius = 60.0
        d_max = 2 * radius

        r = pylab.arange(0.01, d_max, d_max / 51.0)
        M = len(r)
        y = np.zeros(M)
        pr_err = np.zeros(M)

        total = 0.0
        for j in range(M):
            value = self.pr_theory(r[j], radius)
            total += value
            y[j] = value
            pr_err[j] = math.sqrt(y[j])

        y = y / total * d_max / len(r)

        # Perform fit
        pr = Invertor()
        pr.d_max = d_max
        pr.alpha = 0
        pr.x = r
        pr.y = y
        pr.err = pr_err
        out, cov = pr.pr_fit()
        for i in range(len(out)):
            print("%g +- %g" % (out[i], math.sqrt(cov[i][i])))

        # Show input P(r)
        title = "Pr"
        new_plot = Data1D(pr.x, pr.y, dy=pr.err)
        new_plot.name = "P_{obs}(r)"
        new_plot.xaxis("\\rm{r}", 'A')
        new_plot.yaxis("\\rm{P(r)} ", "cm^{-3}")
        new_plot.group_id = "P_{obs}(r)"
        new_plot.id = "P_{obs}(r)"
        new_plot.title = title
        self.parent.update_theory(data_id=self.data_id, theory=new_plot)
        wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title=title))

        # Show P(r) fit
        self.show_pr(out, pr)

        # Show I(q) fit
        q = pylab.arange(0.001, 0.1, 0.01 / 51.0)
        self.show_iq(out, pr, q)
Exemple #4
0
    def _fit_pr(self, evt):
        """
        """
        # Generate P(r) for sphere
        radius = 60.0
        d_max = 2 * radius

        r = pylab.arange(0.01, d_max, d_max / 51.0)
        M = len(r)
        y = np.zeros(M)
        pr_err = np.zeros(M)

        total = 0.0
        for j in range(M):
            value = self.pr_theory(r[j], radius)
            total += value
            y[j] = value
            pr_err[j] = math.sqrt(y[j])

        y = y / total * d_max / len(r)

        # Perform fit
        pr = Invertor()
        pr.d_max = d_max
        pr.alpha = 0
        pr.x = r
        pr.y = y
        pr.err = pr_err
        out, cov = pr.pr_fit()
        for i in range(len(out)):
            print("%g +- %g" % (out[i], math.sqrt(cov[i][i])))

        # Show input P(r)
        title = "Pr"
        new_plot = Data1D(pr.x, pr.y, dy=pr.err)
        new_plot.name = "P_{obs}(r)"
        new_plot.xaxis("\\rm{r}", 'A')
        new_plot.yaxis("\\rm{P(r)} ", "cm^{-3}")
        new_plot.group_id = "P_{obs}(r)"
        new_plot.id = "P_{obs}(r)"
        new_plot.title = title
        self.parent.update_theory(data_id=self.data_id, theory=new_plot)
        wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot, title=title))

        # Show P(r) fit
        self.show_pr(out, pr)

        # Show I(q) fit
        q = pylab.arange(0.001, 0.1, 0.01 / 51.0)
        self.show_iq(out, pr, q)
Exemple #5
0
                if len(toks) > 2:
                    err = float(toks[2])
                else:
                    if scale is None:
                        scale = 0.05 * math.sqrt(test_y)
                        #scale = 0.05/math.sqrt(y)
                        min_err = 0.01 * y
                    err = scale * math.sqrt(test_y) + min_err
                    #err = 0

                data_x = np.append(data_x, test_x)
                data_y = np.append(data_y, test_y)
                data_err = np.append(data_err, err)
            except:
                logger.error(sys.exc_value)

    return data_x, data_y, data_err


if __name__ == "__main__":
    invert = Invertor()
    x, y, erro = load("test/Cyl_A_D102.txt")
    invert.d_max = 102.0
    invert.nfunc = 10
    invert.x = x
    invert.y = y
    invert.err = erro
    # Testing estimator
    est = NTermEstimator(invert)
    print(est.num_terms())
Exemple #6
0
    def _create_file_pr(self, data):
        """
        Create and prepare invertor instance from
        a file data set.

        :param path: path of the file to read in

        """
        # Reset the status bar so that we don't get mixed up
        # with old messages.
        #TODO: refactor this into a proper status handling
        wx.PostEvent(self.parent, StatusEvent(status=''))
        try:

            class FileData(object):
                x = None
                y = None
                err = None
                path = None

                def __init__(self, path):
                    self.path = path

            self._current_file_data = FileData(data.path)
            self._current_file_data.x = data.x
            self._current_file_data.y = data.y
            self._current_file_data.err = data.dy
            x, y, err = data.x, data.y, data.dy
        except:
            load_error(sys.exc_value)
            return None

        # If the file contains no data, just return
        if x is None or len(x) == 0:
            load_error("The loaded file contains no data")
            return None

        # If we have not errors, add statistical errors
        if y is not None:
            if err is None or np.all(err) == 0:
                err = np.zeros(len(y))
                scale = None
                min_err = 0.0
                for i in range(len(y)):
                    # Scale the error so that we can fit over several decades of Q
                    if scale is None:
                        scale = 0.05 * math.sqrt(y[i])
                        min_err = 0.01 * y[i]
                    err[i] = scale * math.sqrt(math.fabs(y[i])) + min_err
                message = "The loaded file had no error bars, "
                message += "statistical errors are assumed."
                wx.PostEvent(self.parent, StatusEvent(status=message))

        try:
            # Get the data from the chosen data set and perform inversion
            pr = Invertor()
            pr.d_max = self.max_length
            pr.alpha = self.alpha
            pr.q_min = self.q_min
            pr.q_max = self.q_max
            pr.x = x
            pr.y = y
            pr.err = err
            pr.est_bck = self.est_bck
            pr.slit_height = self.slit_height
            pr.slit_width = self.slit_width
            return pr
        except:
            load_error(sys.exc_value)
        return None
Exemple #7
0
                if len(toks) > 2:
                    err = float(toks[2])
                else:
                    if scale is None:
                        scale = 0.05 * math.sqrt(test_y)
                        #scale = 0.05/math.sqrt(y)
                        min_err = 0.01 * y
                    err = scale * math.sqrt(test_y) + min_err
                    #err = 0

                data_x = np.append(data_x, test_x)
                data_y = np.append(data_y, test_y)
                data_err = np.append(data_err, err)
            except:
                logger.error(sys.exc_value)

    return data_x, data_y, data_err


if __name__ == "__main__":
    invert = Invertor()
    x, y, erro = load("test/Cyl_A_D102.txt")
    invert.d_max = 102.0
    invert.nfunc = 10
    invert.x = x
    invert.y = y
    invert.err = erro
    # Testing estimator
    est = NTermEstimator(invert)
    print(est.num_terms())
Exemple #8
0
    def _create_file_pr(self, data):
        """
        Create and prepare invertor instance from
        a file data set.

        :param path: path of the file to read in

        """
        # Reset the status bar so that we don't get mixed up
        # with old messages.
        #TODO: refactor this into a proper status handling
        wx.PostEvent(self.parent, StatusEvent(status=''))
        try:
            class FileData(object):
                x = None
                y = None
                err = None
                path = None
                def __init__(self, path):
                    self.path = path

            self._current_file_data = FileData(data.path)
            self._current_file_data.x = data.x
            self._current_file_data.y = data.y
            self._current_file_data.err = data.dy
            x, y, err = data.x, data.y, data.dy
        except:
            load_error(sys.exc_value)
            return None

        # If the file contains no data, just return
        if x is None or len(x) == 0:
            load_error("The loaded file contains no data")
            return None

        # If we have not errors, add statistical errors
        if y is not None:
            if err is None or np.all(err) == 0:
                err = np.zeros(len(y))
                scale = None
                min_err = 0.0
                for i in range(len(y)):
                    # Scale the error so that we can fit over several decades of Q
                    if scale is None:
                        scale = 0.05 * math.sqrt(y[i])
                        min_err = 0.01 * y[i]
                    err[i] = scale * math.sqrt(math.fabs(y[i])) + min_err
                message = "The loaded file had no error bars, "
                message += "statistical errors are assumed."
                wx.PostEvent(self.parent, StatusEvent(status=message))

        try:
            # Get the data from the chosen data set and perform inversion
            pr = Invertor()
            pr.d_max = self.max_length
            pr.alpha = self.alpha
            pr.q_min = self.q_min
            pr.q_max = self.q_max
            pr.x = x
            pr.y = y
            pr.err = err
            pr.est_bck = self.est_bck
            pr.slit_height = self.slit_height
            pr.slit_width = self.slit_width
            return pr
        except:
            load_error(sys.exc_value)
        return None