def view_lightcurve(self, start=-10,
                        stop=20.0,
                        dt=1.0,
                        use_binner=False,
                        with_dead_time=True):
        # type: (float, float, float, bool) -> None
        """
        :param start:
        :param stop:
        :param dt:
        :param use_binner:

        """

        # git a set of bins containing the intervals

        bins = self._binned_spectrum_set.time_intervals.containing_interval(
            start, stop
        )  # type: TimeIntervalSet

        cnts = []
        width = []
        width_dead = []

        log.debug(f"viewing light curve with dead time: {with_dead_time}")

        for time_bin in bins:

            cnts.append(self.counts_over_interval(
                time_bin.start_time, time_bin.stop_time))

            # use the actual exposure

            

            width_dead.append(self.exposure_over_interval(
                    time_bin.start_time, time_bin.stop_time))

            # just use the "defined edges"

            

            width.append(time_bin.duration)

        # now we want to get the estimated background from the polynomial fit

        if self.poly_fit_exists:

            bkg = []
            for j, time_bin in enumerate(bins):
                tmpbkg = 0.0
                for poly in self.polynomials:

                    tmpbkg += poly.integral(time_bin.start_time, time_bin.stop_time)
                    
                
                bkg.append(tmpbkg/width[j])
                
            
            
        else:

            
            bkg = None

        # pass all this to the light curve plotter

        if self.time_intervals is not None:

            selection = self.time_intervals.bin_stack

        else:

            selection = None

        if self.poly_intervals is not None:

            bkg_selection = self.poly_intervals.bin_stack

        else:

            bkg_selection = None

        # plot the light curve

        fig = binned_light_curve_plot(
            time_bins=bins.bin_stack,
            cnts=np.array(cnts),
            width=np.array(width_dead),
            bkg=bkg,
            selection=selection,
            bkg_selections=bkg_selection,
        )

        return fig
Ejemplo n.º 2
0
    def view_lightcurve(self, start=-10, stop=20., dt=1., use_binner=False):
        # type: (float, float, float, bool) -> None
        """
        :param start:
        :param stop:
        :param dt:
        :param use_binner:

        """

        if use_binner:

            # we will use the binner object to bin the
            # light curve and ignore the normal linear binning

            bins = self.bins.time_edges

            # perhaps we want to look a little before or after the binner
            if start < bins[0]:
                pre_bins = np.arange(start, bins[0], dt).tolist()[:-1]

                pre_bins.extend(bins)

                bins = pre_bins

            if stop > bins[-1]:
                post_bins = np.arange(bins[-1], stop, dt)

                bins.extend(post_bins[1:])

        else:

            # otherwise, just use regular linear binning

            bins = np.arange(start, stop + dt, dt)

        cnts, bins = np.histogram(self.arrival_times, bins=bins)
        time_bins = np.array([[bins[i], bins[i + 1]]
                              for i in range(len(bins) - 1)])

        #width = np.diff(bins)
        width = []

        # now we want to get the estimated background from the polynomial fit

        if self.poly_fit_exists:

            # we will store the bkg rate for each time bin

            bkg = []

            for j, tb in enumerate(time_bins):

                # zero out the bkg
                tmpbkg = 0.

                # we will use the exposure for the width

                this_width = self.exposure_over_interval(tb[0], tb[1])

                # sum up the counts over this interval

                for poly in self.polynomials:

                    tmpbkg += poly.integral(tb[0], tb[1])

                # capture the exposure

                width.append(this_width)

                # capture the bkg *rate*

                bkg.append(tmpbkg / this_width)

        else:

            bkg = None

            for j, tb in enumerate(time_bins):

                this_width = self.exposure_over_interval(tb[0], tb[1])

                width.append(this_width)

        width = np.array(width)

        # pass all this to the light curve plotter

        if self.time_intervals is not None:

            selection = self.time_intervals.bin_stack

        else:

            selection = None

        if self.poly_intervals is not None:

            bkg_selection = self.poly_intervals.bin_stack

        else:

            bkg_selection = None

        return binned_light_curve_plot(
            time_bins=time_bins,
            cnts=cnts,
            width=width,
            bkg=bkg,
            selection=selection,
            bkg_selections=bkg_selection,
        )
Ejemplo n.º 3
0
    def view_lightcurve(self, start=-10, stop=20., dt=1., use_binner=False):
        # type: (float, float, float, bool) -> None

        """
        :param start:
        :param stop:
        :param dt:
        :param use_binner:

        """

        # git a set of bins containing the intervals

        bins = self._binned_spectrum_set.time_intervals.containing_interval( start, stop) # type: TimeIntervalSet

        cnts = []
        width = []

        for bin in bins:

            cnts.append(self.counts_over_interval(bin.start_time, bin.stop_time) )
            width.append(bin.duration)


        # now we want to get the estimated background from the polynomial fit

        if self.poly_fit_exists:

            bkg = []
            for j, tb in enumerate(bins):
                tmpbkg = 0.
                for poly in self.polynomials:
                    tmpbkg += poly.integral(tb.start_time, tb.stop_time)

                bkg.append(tmpbkg / width[j])

        else:

            bkg = None

        # pass all this to the light curve plotter

        if self.time_intervals is not None:

            selection = self.time_intervals.bin_stack

        else:

            selection = None

        if self.poly_intervals is not None:

            bkg_selection = self.poly_intervals.bin_stack

        else:

            bkg_selection = None


        # plot the light curve

        fig = binned_light_curve_plot(time_bins=bins.bin_stack,
                                cnts=np.array(cnts),
                                width=np.array(width),
                                bkg=bkg,
                                selection=selection,
                                bkg_selections=bkg_selection)



        return fig
Ejemplo n.º 4
0
    def view_lightcurve(self,
                        start: float = -10,
                        stop: float = 20.0,
                        dt: float = 1.0,
                        use_binner: bool = False,
                        use_echans_start: int = 0,
                        use_echans_stop: int = -1,
                        with_dead_time=True) -> plt.Figure:
        # type: (float, float, float, bool) -> None
        """
        :param start:
        :param stop:
        :param dt:
        :param use_binner:

        """

        # validate echan mask input
        if not isinstance(use_echans_start, int):
            log.error(f"The use_echans_start variable must be a integer."
                      f" Input is {use_echans_start}.")
            raise AssertionError()

        if not np.abs(use_echans_start) < self.n_channels:
            log.error(
                f"The use_echans_start variable must be"
                f"between {(-1)*(self.n_channels-1)} and {self.n_channels-1}."
                f" Input is {use_echans_start}.")
            raise AssertionError()

        if not isinstance(use_echans_stop, int):
            log.error(f"The use_echans_stop variable must be a integer."
                      f" Input is {use_echans_stop}.")
            raise AssertionError()

        if not np.abs(use_echans_stop) < self.n_channels:
            log.error(
                f"The use_echans_stop variable must be"
                f"between {(-1)*(self.n_channels-1)} and {self.n_channels-1}."
                f" Input is {use_echans_start}.")
            raise AssertionError()

        if use_echans_start < 0:
            use_echans_start = self.n_channels + use_echans_start

        if use_echans_stop < 0:
            use_echans_stop = self.n_channels + use_echans_stop

        if not use_echans_stop >= use_echans_start:
            log.error(f"The use_echans_stop variable must be larger"
                      f" or equal than the use_echans_start variable"
                      f" Input is use_echans_start: {use_echans_start}"
                      f" > use_echans_stop: {use_echans_stop}")
            raise AssertionError()

        # git a set of bins containing the intervals

        bins = self._binned_spectrum_set.time_intervals.containing_interval(
            start, stop)  # type: TimeIntervalSet

        cnts = []
        width = []
        width_dead = []

        log.debug(f"viewing light curve with dead time: {with_dead_time}")

        for time_bin in bins:

            cnts.append(
                np.sum(
                    self.count_per_channel_over_interval(
                        time_bin.start_time,
                        time_bin.stop_time)[use_echans_start:use_echans_stop +
                                            1]))

            # use the actual exposure

            width_dead.append(
                self.exposure_over_interval(time_bin.start_time,
                                            time_bin.stop_time))

            # just use the "defined edges"

            width.append(time_bin.duration)

        # now we want to get the estimated background from the polynomial fit

        if self.poly_fit_exists:

            bkg = []
            for j, time_bin in enumerate(bins):
                tmpbkg = 0.0
                for poly in self.polynomials[use_echans_start:use_echans_stop +
                                             1]:

                    tmpbkg += poly.integral(time_bin.start_time,
                                            time_bin.stop_time)

                bkg.append(tmpbkg / width[j])

        else:

            bkg = None

        # pass all this to the light curve plotter

        if self.time_intervals is not None:

            selection = self.time_intervals.bin_stack

        else:

            selection = None

        if self.poly_intervals is not None:

            bkg_selection = self.poly_intervals.bin_stack

        else:

            bkg_selection = None

        # plot the light curve

        fig = binned_light_curve_plot(
            time_bins=bins.bin_stack,
            cnts=np.array(cnts),
            width=np.array(width_dead),
            bkg=bkg,
            selection=selection,
            bkg_selections=bkg_selection,
        )

        return fig
Ejemplo n.º 5
0
    def view_lightcurve(self,
                        start: float = -10,
                        stop: float = 20.0,
                        dt: float = 1.0,
                        use_binner: bool = False,
                        use_echans_start: int = 0,
                        use_echans_stop: int = -1) -> plt.Figure:
        # type: (float, float, float, bool) -> None
        """
        :param start:
        :param stop:
        :param dt:
        :param use_binner:

        """

        # validate echan mask input
        if not isinstance(use_echans_start, int):
            log.error(f"The use_echans_start variable must be a integer."
                      f" Input is {use_echans_start}.")
            raise AssertionError()

        if not (use_echans_start >
                (-1) * (self.n_channels) - 1 and use_echans_start <
                (self.n_channels)):
            log.error(
                f"The use_echans_start variable must be"
                f"between {(-1)*(self.n_channels)} and {self.n_channels-1}."
                f" Input is {use_echans_start}.")
            raise AssertionError()

        if not isinstance(use_echans_stop, int):
            log.error(f"The use_echans_stop variable must be a integer."
                      f" Input is {use_echans_stop}.")
            raise AssertionError()

        if not (use_echans_stop >
                (-1) * (self.n_channels) - 1 and use_echans_stop <
                (self.n_channels)):
            log.error(
                f"The use_echans_stop variable must be"
                f"between {(-1)*(self.n_channels)} and {self.n_channels-1}."
                f" Input is {use_echans_stop}.")
            raise AssertionError()

        if use_echans_start < 0:
            use_echans_start = self.n_channels + use_echans_start

        if use_echans_stop < 0:
            use_echans_stop = self.n_channels + use_echans_stop

        if not use_echans_stop >= use_echans_start:
            log.error(f"The use_echans_stop variable must be larger"
                      f" or equal than the use_echans_start variable"
                      f" Input is use_echans_start: {use_echans_start}"
                      f" > use_echans_stop: {use_echans_stop}")
            raise AssertionError()

        # get echan bins
        echan_bins = np.arange(use_echans_start, use_echans_stop + 2, 1) - 0.5

        if use_binner:

            # we will use the binner object to bin the
            # light curve and ignore the normal linear binning

            bins = self.bins.time_edges

            # perhaps we want to look a little before or after the binner
            if start < bins[0]:
                pre_bins = np.arange(start, bins[0], dt).tolist()[:-1]

                pre_bins.extend(bins)

                bins = pre_bins

            if stop > bins[-1]:
                post_bins = np.arange(bins[-1], stop, dt)

                bins.extend(post_bins[1:])

        else:

            # otherwise, just use regular linear binning

            bins = np.arange(start, stop + dt, dt)

        cnts, bins, _ = np.histogram2d(self.arrival_times,
                                       self.measurement,
                                       bins=(bins, echan_bins))
        cnts = np.sum(cnts, axis=1)

        time_bins = np.array([[bins[i], bins[i + 1]]
                              for i in range(len(bins) - 1)])

        # now we want to get the estimated background from the polynomial fit

        if self.poly_fit_exists:

            # we will store the bkg rate for each time bin

            bkg = []

            for j, tb in enumerate(time_bins):

                # zero out the bkg
                tmpbkg = 0.0

                # sum up the counts over this interval

                for poly in self.polynomials[use_echans_start:use_echans_stop +
                                             1]:

                    tmpbkg += poly.integral(tb[0], tb[1])

                # capture the bkg *rate*

                # Divide the background counts by the time intervall
                # We do not use the dead time corrected exposure here
                # because the integration is done over the full time bin
                # and not the dead time corrected exposure
                bkg.append(old_div(tmpbkg, tb[1] - tb[0]))

        else:

            bkg = None

        width = []
        for j, tb in enumerate(time_bins):
            # capture the exposure
            this_width = self.exposure_over_interval(tb[0], tb[1])

            width.append(this_width)

        width = np.array(width)

        # pass all this to the light curve plotter

        if self.time_intervals is not None:

            selection = self.time_intervals.bin_stack

        else:

            selection = None

        if self.poly_intervals is not None:

            bkg_selection = self.poly_intervals.bin_stack

        else:

            bkg_selection = None

        return binned_light_curve_plot(
            time_bins=time_bins,
            cnts=cnts,
            width=width,
            bkg=bkg,
            selection=selection,
            bkg_selections=bkg_selection,
        )