Exemple #1
0
def channel_plot(ax, chan_min, chan_max, counts, **kwargs):
    chans = np.array(zip(chan_min, chan_max))
    width = chan_max - chan_min

    step_plot(chans, counts / width, ax, **kwargs)
    ax.set_xscale('log')
    ax.set_yscale('log')

    return ax
Exemple #2
0
def channel_plot(ax, chan_min, chan_max, counts, **kwargs):
    chans = np.vstack([chan_min, chan_max]).T
    width = chan_max - chan_min

    step_plot(chans, old_div(counts, width), ax, **kwargs)
    ax.set_xscale("log")
    ax.set_yscale("log")

    return ax
def channel_plot(ax, chan_min, chan_max, counts, **kwargs):
    chans = np.array(zip(chan_min, chan_max))
    width = chan_max - chan_min

    step_plot(chans, counts / width, ax, **kwargs)
    ax.set_xscale('log')
    ax.set_yscale('log')

    return ax
Exemple #4
0
    def display(self,fill=False,fill_min=0.,x_label='x',y_label='y',**kwargs):

        fig, ax =  plt.subplots()

        step_plot(xbins=self.bin_stack,
                  y=self._contents,
                  ax=ax,
                  fill=fill,
                  fill_min=fill_min,
                  **kwargs)

        ax.set_xlabel(x_label)
        ax.set_ylabel(y_label)

        return fig
Exemple #5
0
    def add_model_step(self, xmin, xmax, xwidth, y, label, color):
        """
        Add a model but use discontinuous steps for the plotting.

        :param xmin: the low end boundaries
        :param xmax: the high end boundaries
        :param xwidth: the width of the bins
        :param y: the height of the bins
        :param label: the label of the model
        :param color: the color of the model
        :return: None
        """
        step_plot(np.asarray(zip(xmin, xmax)),
                  y / xwidth,
                  self._data_axis,
                  alpha=.8,
                  label=label,
                  color=color)
    def add_model_step(self, xmin, xmax, xwidth, y, label, **kwargs):
        """
        Add a model but use discontinuous steps for the plotting.

        :param xmin: the low end boundaries
        :param xmax: the high end boundaries
        :param xwidth: the width of the bins
        :param y: the height of the bins
        :param label: the label of the model
        :param **kwargs: any kwargs passed to plot
        :return: None
        """

        step_plot(np.asarray(list(zip(xmin, xmax))),
                  old_div(y, xwidth),
                  self._data_axis,
                  label=label,
                  **kwargs)
Exemple #7
0
    def display(self,
                ax=None,
                show_data=True,
                show_model=True,
                show_total=False,
                model_kwargs={},
                data_kwargs={},
                edges=True,
                min_rate=None):
        """

        Display the data, model, or both.

        :param ax:
        :param show_data:
        :param show_model:
        :param show_total:
        :param model_kwargs:
        :param data_kwargs:
        :return:
        """

        tmp = ((self._observed_counts / self._exposure) -
               self._background_counts / self._background_exposure)

        scattering_edges = np.array(self._observation.edges)

        sa_min, sa_max = scattering_edges[:-1], scattering_edges[1:]

        tmp_db = ((self._observed_counts / self._exposure) -
                  self._background_counts / self._background_exposure) / (
                      sa_max - sa_min)

        old_rebinner = self._rebinner

        if min_rate is not None:

            rebinner = Rebinner(tmp_db, min_rate, mask=None)

            self._apply_rebinner(rebinner)

            net_rate = rebinner.rebin(tmp)
        else:

            net_rate = tmp

        sa_min, sa_max = self.scattering_boundaries

        if show_total:
            show_model = False
            show_data = False

        if ax is None:

            fig, ax = plt.subplots()

        else:

            fig = ax.get_figure()

        xs = self.scattering_boundaries

        if show_total:

            total_rate = self._current_observed_counts / self._exposure / self.bin_widths

            bkg_rate = self._current_background_counts / \
                self._background_exposure / self.bin_widths

            total_errors = np.sqrt(total_rate)

            if self._background.is_poisson:

                bkg_errors = np.sqrt(bkg_rate)

            else:

                bkg_errors = self._current_background_count_errors / self.bin_widths

            ax.hlines(total_rate,
                      sa_min,
                      sa_max,
                      color='#7D0505',
                      **data_kwargs)
            ax.vlines(np.mean([xs], axis=1),
                      total_rate - total_errors,
                      total_rate + total_errors,
                      color='#7D0505',
                      **data_kwargs)

            ax.hlines(bkg_rate, sa_min, sa_max, color='#0D5BAE', **data_kwargs)
            ax.vlines(np.mean([xs], axis=1),
                      bkg_rate - bkg_errors,
                      bkg_rate + bkg_errors,
                      color='#0D5BAE',
                      **data_kwargs)

        if show_data:

            if self._background.is_poisson:

                errors = np.sqrt((self._current_observed_counts /
                                  self._exposure) +
                                 (self._current_background_counts /
                                  self._background_exposure))

            else:

                errors = np.sqrt((self._current_observed_counts /
                                  self._exposure) +
                                 (self._current_background_count_errors /
                                  self._background_exposure)**2)

            ax.hlines(net_rate / self.bin_widths, sa_min, sa_max,
                      **data_kwargs)
            ax.vlines(np.mean([xs],
                              axis=1), (net_rate - errors) / self.bin_widths,
                      (net_rate + errors) / self.bin_widths, **data_kwargs)

        if show_model:

            if edges:

                step_plot(ax=ax,
                          xbins=np.vstack([sa_min, sa_max]).T,
                          y=self._get_model_counts() / self._exposure /
                          self.bin_widths,
                          **model_kwargs)

            else:

                y = self._get_model_counts() / self._exposure / self.bin_widths
                ax.hlines(y, sa_min, sa_max, **model_kwargs)

        ax.set_xlabel('Scattering Angle')
        ax.set_ylabel('Net Rate (cnt/s/bin)')

        if old_rebinner is not None:

            # There was a rebinner, use it. Note that the rebinner applies the mask by itself

            self._apply_rebinner(old_rebinner)

        else:

            self.remove_rebinning()

        return fig
Exemple #8
0
def binned_light_curve_plot(time_bins,
                            cnts,
                            width,
                            bkg=None,
                            selection=None,
                            bkg_selections=None):
    """

    :param time_bins: stacked array of time intervals
    :param cnts: counts per bin
    :param bkg: background of the light curve
    :param width: with of the bins
    :param selection: bin selection
    :param bkg_selections:
    :param instrument:

    :return:
    """
    fig, ax = plt.subplots()

    top = max(old_div(cnts, width)) * 1.2
    min_cnts = min(old_div(cnts[cnts > 0], width[cnts > 0])) * 0.95
    bottom = min_cnts
    mean_time = np.mean(time_bins, axis=1)

    all_masks = []

    # round
    np.round(time_bins, decimals=4, out=time_bins)

    light_curve_color = threeML_config["lightcurve"]["lightcurve color"]
    selection_color = threeML_config["lightcurve"]["selection color"]
    background_color = threeML_config["lightcurve"]["background color"]
    background_selection_color = threeML_config["lightcurve"][
        "background selection color"]

    # first plot the full lightcurve

    step_plot(
        time_bins,
        old_div(cnts, width),
        ax,
        color=light_curve_color,
        label="Light Curve",
    )

    if selection is not None:

        # now plot the temporal selections

        np.round(selection, decimals=4, out=selection)

        for tmin, tmax in selection:
            tmp_mask = np.logical_and(time_bins[:, 0] >= tmin,
                                      time_bins[:, 1] <= tmax)

            all_masks.append(tmp_mask)

        if len(all_masks) > 1:

            for mask in all_masks[1:]:
                step_plot(
                    time_bins[mask],
                    old_div(cnts[mask], width[mask]),
                    ax,
                    color=selection_color,
                    fill=True,
                    fill_min=min_cnts,
                )

        step_plot(
            time_bins[all_masks[0]],
            old_div(cnts[all_masks[0]], width[all_masks[0]]),
            ax,
            color=selection_color,
            fill=True,
            fill_min=min_cnts,
            label="Selection",
        )

    # now plot the background selections

    if bkg_selections is not None:

        np.round(bkg_selections, decimals=4, out=bkg_selections)

        all_masks = []
        for tmin, tmax in bkg_selections:
            tmp_mask = np.logical_and(time_bins[:, 0] >= tmin,
                                      time_bins[:, 1] <= tmax)

            all_masks.append(tmp_mask)

        if len(all_masks) > 1:

            for mask in all_masks[1:]:
                step_plot(
                    time_bins[mask],
                    old_div(cnts[mask], width[mask]),
                    ax,
                    color=background_selection_color,
                    fill=True,
                    alpha=0.4,
                    fill_min=min_cnts,
                )

        step_plot(
            time_bins[all_masks[0]],
            old_div(cnts[all_masks[0]], width[all_masks[0]]),
            ax,
            color=background_selection_color,
            fill=True,
            fill_min=min_cnts,
            alpha=0.4,
            label="Bkg. Selections",
            zorder=-30,
        )

    if bkg is not None:
        # now plot the estimated background

        ax.plot(mean_time, bkg, background_color, lw=2.0, label="Background")

    # ax.fill_between(selection, bottom, top, color="#fc8d62", alpha=.4)

    ax.set_xlabel("Time (s)")
    ax.set_ylabel("Rate (cnts/s)")
    ax.set_ylim(bottom, top)
    ax.set_xlim(time_bins.min(), time_bins.max())
    ax.legend()

    return fig
Exemple #9
0
def binned_light_curve_plot(time_bins, cnts, width, bkg=None, selection=None, bkg_selections=None):
    """

    :param time_bins: stacked array of time intervals
    :param cnts: counts per bin
    :param bkg: background of the light curve
    :param width: with of the bins
    :param selection: bin selection
    :param bkg_selections:
    :param instrument:

    :return:
    """
    fig, ax = plt.subplots()

    top = max(cnts / width) * 1.2
    min_cnts = min(cnts[cnts > 0] / width[cnts > 0]) * 0.95
    bottom = min_cnts
    mean_time = map(np.mean, time_bins)

    all_masks = []

    # round
    np.round(time_bins, decimals=4, out=time_bins)


    light_curve_color = threeML_config['lightcurve']['lightcurve color']
    selection_color = threeML_config['lightcurve']['selection color']
    background_color = threeML_config['lightcurve']['background color']
    background_selection_color = threeML_config['lightcurve']['background selection color']

    # first plot the full lightcurve

    step_plot(time_bins, cnts / width, ax,
              color=light_curve_color, label="Light Curve")

    if selection is not None:

        # now plot the temporal selections

        np.round(selection, decimals=4, out=selection)

        for tmin, tmax in selection:
            tmp_mask = np.logical_and(time_bins[:, 0] >= tmin, time_bins[:, 1] <= tmax)

            all_masks.append(tmp_mask)

        if len(all_masks) > 1:

            for mask in all_masks[1:]:
                step_plot(time_bins[mask], cnts[mask] / width[mask], ax,
                          color=selection_color,
                          fill=True,
                          fill_min=min_cnts)

        step_plot(time_bins[all_masks[0]], cnts[all_masks[0]] / width[all_masks[0]], ax,
                  color=selection_color,
                  fill=True,
                  fill_min=min_cnts, label="Selection")

    # now plot the background selections

    if bkg_selections is not None:

        np.round(bkg_selections, decimals=4, out=bkg_selections)

        all_masks = []
        for tmin, tmax in bkg_selections:
            tmp_mask = np.logical_and(time_bins[:, 0] >= tmin, time_bins[:, 1] <= tmax)

            all_masks.append(tmp_mask)

        if len(all_masks) > 1:

            for mask in all_masks[1:]:
                step_plot(time_bins[mask], cnts[mask] / width[mask], ax,
                          color=background_selection_color,
                          fill=True,
                          alpha=.4,
                          fill_min=min_cnts)

        step_plot(time_bins[all_masks[0]], cnts[all_masks[0]] / width[all_masks[0]], ax,
                  color=background_selection_color,
                  fill=True,
                  fill_min=min_cnts,
                  alpha=.4,
                  label="Bkg. Selections",
                  zorder=-30)

    if bkg is not None:
        # now plot the estimated background

        ax.plot(mean_time, bkg, background_color, lw=2., label="Background")


    # ax.fill_between(selection, bottom, top, color="#fc8d62", alpha=.4)

    ax.set_xlabel("Time (s)")
    ax.set_ylabel("Rate (cnts/s)")
    ax.set_ylim(bottom, top)
    ax.set_xlim(time_bins.min(), time_bins.max())
    ax.legend()

    return fig