Пример #1
0
    def execute(self):
        print '[INFO] Saving events data to dictionary...'
        
        energy_calculator = self.get_store().get(self._energy_key)
        tod_data = self.get_store().get(self._tod_key)
        cuts = self.get_store().get(self._cosig_key)
        peaks = cuts['peaks']
        cs = cuts['coincident_signals']
        
        #Initialize and fill empty dictionary
        events = []
        
        for peak in peaks:
            all_pixels = pixels_affected_in_event(cs, peak)
            start = peak[0]
            end = peak[1]
            duration = peak[2]
            number_of_pixels = peak[3]
            ref_index = int((start + end)/2)
            energy_per_detector = []
            for pid in all_pixels:
                """
                Uncomment this if you are analyzing energy per event, also, 
                change the return statement of energy calculator to return the sum of 4 det
                """
                
                """
                pix_energy = energy_calculator(pid,start,end)
                energy_per_detector.append(pix_energy)
                """
                e1,e2,e3,e4 = energy_calculator(pid,start,end)
                energy_dict ={str(pid): [e1,e2,e3,e4]}
                energy_per_detector.append(energy_dict)
            """Uncomment this if you are analyzing energy per detector"""
            #energy = np.sum(energy_per_detector)

            id = "%d.%d" % (self.get_id(), start)
            event = {
                'id': id,
                'start': start,
                'end': end,
                'duration': duration,
                'ctime': tod_data.ctime[ref_index],
                'peak': [int(start),int(end),int(duration),int(number_of_pixels)],
                'alt': tod_data.alt[ref_index],
                'az': tod_data.az[ref_index],
                'number_of_pixels': float(number_of_pixels),
                'pixels_affected': all_pixels,
                'energy': energy_per_detector,
            }
            events.append(event)
        
        self.get_store().set(self._output_key,events)
Пример #2
0
    def execute(self, store):
        print '[INFO] Plotting all glitches affecting detector ...'
        taus = store.get(self._time_constants)
        for tc in taus:
            if tc['det_uid'] == self._detuid:
                tau = tc['tau']

        tod_data = store.get(self._tod_key)  # retrieve tod_data
        cuts = store.get(self._cosig_key)  # retrieve tod_data
        array_name = self.get_array()
        peaks = cuts['peaks']
        self._pr = PixelReader()

        def cs_cuts():
            cuts = store.get(self._cosig_key)
            return cuts['coincident_signals']

        timeseries = store.get(self._timeseries_key)

        def plotter(pid, tau, start_time, end_time):

            x = timeseries(pid, start_time, end_time)[0]
            y1 = timeseries(pid, start_time, end_time)[1]
            y2 = timeseries(pid, start_time, end_time)[2]
            y3 = timeseries(pid, start_time, end_time)[3]
            y4 = timeseries(pid, start_time, end_time)[4]

            plt.title('Pixel affected from ' + str(start_time) + '-' +
                      str(end_time) + ', Pixel ' + str(pid))
            plt.xlabel('TOD track:' + str(self._tag) + ' Tau:' + str(tau))
            plt.plot(x, y1, '.-', label='90 GHz')
            plt.plot(x, y2, '.-', label='90 GHz')
            plt.plot(x, y3, '.-', label='150 GHz')
            plt.plot(x, y4, '.-', label='150 GHz')

            plt.legend()
            plt.show()

        cs = cuts['coincident_signals']

        for peak in peaks:

            stime = peak[0]
            etime = peak[1]
            pixels = pixels_affected_in_event(cs, peak)
            for pixel in pixels:
                if pixel == self._detuid:
                    plotter(pixel, tau, stime, etime)
Пример #3
0
 def execute(self):
     # Retrieve cut data
     cuts = self.get_store().get(self._input_key)
     cosig = cuts['coincident_signals']
     peaks = cuts['peaks']
     events = []
     for peak in peaks:
         event_id = "%d.%d" % (self.get_id(), peak[0])
         event = {
             'id': event_id,
             'start': peak[0],  # start index
             'end': peak[1],  # end index
             'duration': peak[2],
             'number_of_pixels': peak[3],
             'pixels_affected': pixels_affected_in_event(cosig, peak),
             'tag': self._tag
         }
         events.append(event)
     print '[INFO] Events generated: %d' % len(events)
     self.get_store().set(self._output_key, events)
    def execute(self):
        print '[INFO] Saving events data to dictionary...'
        
        energy_calculator = self.get_store().get(self._energy_key)
        tod_data = self.get_store().get(self._tod_key)
        cuts = self.get_store().get(self._cosig_key)
        peaks = cuts['peaks']
        cs = cuts['coincident_signals']
        
        #Initialize and fill empty dictionary
        events = []
        
        for peak in peaks:
            all_pixels = pixels_affected_in_event(cs, peak)
            start = peak[0]
            end = peak[1]
            duration = peak[2]
            number_of_pixels = peak[3]
            ref_index = int((start + end)/2)
            energy_per_detector = []
            for pid in all_pixels:
                pix_energy = energy_calculator(pid,start,end)
                energy_per_detector.append(pix_energy)
            energy = np.sum(energy_per_detector)

            id = "%d.%d" % (self.get_id(), start)
            event = {
                'id': id,
                'start': start,
                'end': end,
                'duration': duration,
                'ctime': tod_data.ctime[ref_index],
                'alt': tod_data.alt[ref_index],
                'az': tod_data.az[ref_index],
                'number_of_pixels': float(number_of_pixels),
                'pixels_affected': all_pixels,
                'energy': energy,
            }
            events.append(event)
        
        self.get_store().set(self._output_key,events)
Пример #5
0
    def execute(self, store):
        print '[INFO] Checking for correlation ...'
        self._pr = PixelReader()
        #self._pr = PixelReader(season = '2017', array=self.get_context().get_array())
        tod_data = store.get(self._tod_key)  # retrieve tod_data
        cuts = store.get(self._cosig_key)  # retrieve tod_data
        peaks = cuts['peaks']
        timeseries = store.get(self._timeseries_key)
        cs = cuts['coincident_signals']

        def avg_signal(pixels, start_time, end_time):

            for pid in pixels:
                x, y1, y2, y3, y4 = timeseries(pid, start_time, end_time)
                avg_y1, avg_y2, avg_y3, avg_y4 = np.zeros(len(y1)), np.zeros(
                    len(y2)), np.zeros(len(y3)), np.zeros(len(y4))
                avg_x = x
                avg_y1 += y1
                avg_y2 += y2
                avg_y3 += y3
                avg_y4 += y4

            x = avg_x
            y1 = avg_y1 / len(avg_y1)
            y2 = avg_y2 / len(avg_y2)
            y3 = avg_y3 / len(avg_y3)
            y4 = avg_y4 / len(avg_y4)
            return x, y1, y2, y3, y4

        def correlation(x1, x2, y1, y2):

            ts1 = y1
            ts2 = y2
            l1 = len(ts1)
            l2 = len(ts2)
            if l1 < l2:
                n = l1
                return max([
                    np.corrcoef(ts1, ts2[i:n + i])[0][1]
                    for i in range(0, l2 - l1)
                ])
            elif l2 < l1:
                n = l2
                return max([
                    np.corrcoef(ts1[i:n + i], ts2)[0][1]
                    for i in range(0, l1 - l2)
                ])
            else:
                return np.corrcoef(ts1, ts2)[0][1]

        avg_x1, avg_y1 = self._template[0], self._template[1]

        possible_events = []
        highlylikely_events = []
        lower_threshold = 0.6
        upper_threshold = self._coeff

        for peak in peaks:
            all_pixels = pixels_affected_in_event(cs, peak)
            avg_x2, avg_y2_1, avg_y2_2, avg_y2_3, avg_y2_4 = avg_signal(
                all_pixels, peak[0], peak[1])
            coeff1 = correlation(avg_x1, avg_x2, avg_y1, avg_y2_1)
            coeff2 = correlation(avg_x1, avg_x2, avg_y1, avg_y2_2)
            coeff3 = correlation(avg_x1, avg_x2, avg_y1, avg_y2_3)
            coeff4 = correlation(avg_x1, avg_x2, avg_y1, avg_y2_4)

            if (lower_threshold <= coeff1) & (lower_threshold <= coeff2) & (
                    lower_threshold <= coeff3
            ) & (lower_threshold <= coeff4) & (coeff1 < upper_threshold) & (
                    coeff2 < upper_threshold) & (coeff3 < upper_threshold) & (
                        coeff4 < upper_threshold):
                possible_events.append(peak)

            elif (coeff1 >= upper_threshold) & (coeff2 >= upper_threshold) & (
                    coeff3 >= upper_threshold) & (coeff4 >= upper_threshold):
                highlylikely_events.append(peak)

        print highlylikely_events
        print '[INFO] Events passed: %d / %d' % (len(highlylikely_events),
                                                 len(peaks))
        cuts['peaks'] = highlylikely_events
        store.set(self._output_key, cuts)
Пример #6
0
    def execute(self, store):
        print '[INFO] Loading Glitch Data ...'
        tod_data = store.get(self._tod_key)  # retrieve tod_data
        cuts = store.get(self._cosig_key)  # retrieve tod_data
        array_name = self.get_array()
        peaks = cuts['peaks']
        #print('[INFO] All glitches, unfiltered...')
        #print('[INFO] peaks: ', peaks)
        #self._pr = PixelReader(season= '2017', array=self.get_context().get_array()) #for covered
        self._pr = PixelReader()  #for uncovered
        #self._pr = PixelReader(season='2017',array = str(array_name))
        #self._pr = PixelReader(season='2017', array=self.get_context().get_array())

        plot = raw_input("Do you want to plot an event? Enter y/n: ")
        if plot == "y":
            tod_data = store.get(self._tod_key)  # retrieve tod_data
            cuts = store.get(self._cosig_key)  # retrieve tod_data
            peaks = cuts['peaks']

            def cs_cuts():
                cuts = store.get(self._cosig_key)
                return cuts['coincident_signals']

            timeseries = store.get(self._timeseries_key)
            """
            PLOTTING FUNCTION
            Plot all pixels affected given an array of pixel ids
            and a starting time and ending time
            
            """

            def plotter(pixels, start_time, end_time):

                for pid in pixels:

                    x = timeseries(pid, start_time, end_time)[0]
                    y1 = timeseries(pid, start_time, end_time)[1]
                    y2 = timeseries(pid, start_time, end_time)[2]
                    y3 = timeseries(pid, start_time, end_time)[3]
                    y4 = timeseries(pid, start_time, end_time)[4]

                    plt.title('Pixel affected from ' + str(start_time) + '-' +
                              str(end_time) + ', Pixel ' + str(pid))
                    plt.xlabel('TOD track:' + str(self._tag))
                    plt.plot(x, y1, '.-', label='90 GHz')
                    plt.plot(x, y2, '.-', label='90 GHz')
                    plt.plot(x, y3, '.-', label='150 GHz')
                    plt.plot(x, y4, '.-', label='150 GHz')

                    plt.legend()
                    plt.show()

            """
            SPECIFIC EVENT
            To plot specific event, this interface will ask you to supply the event list, make sure you 
            manually convert the last string to a float or integer
            """
            cs = cuts['coincident_signals']
            e = raw_input(
                'Please copy the event list to plot 4 freq channels:')
            event = json.loads(e)
            stime = event[0]
            etime = event[1]
            pixels = pixels_affected_in_event(cs, event)
            plotter(pixels, stime, etime)

            self._pr.plot(pixels)
            plt.show()

            y_n = ' '

            while y_n != 'n':
                y_n = raw_input(
                    "Would you like to plot another event? Enter y/n...")
                if y_n == 'y':
                    e = raw_input(
                        'Please copy the event list to plot 4 freq channels:')
                    event = json.loads(e)
                    stime = event[0]
                    etime = event[1]
                    pixels = pixels_affected_in_event(cs, event)
                    print '[INFO] Plotting Glitch...'
                    plotter(pixels, stime, etime)
                    self._pr.plot(pixels)
                    plt.show()

        else:
            print 'No plot will be displayed!'
Пример #7
0
    def execute(self):
        cuts = self.get_store().get(self._cosig_key)
        peaks = cuts['peaks']
        cosig = cuts['coincident_signals']
        tod_data = self.get_store().get(self._tod_key)

        def energyseries(pixel, s_time, e_time, buffer=0):

            start_time = s_time - buffer
            end_time = e_time + buffer

            a1, a2 = self._pr.get_f1(pixel)
            b1, b2 = self._pr.get_f2(pixel)
            d1, d2 = tod_data.data[a1], tod_data.data[a2]
            d3, d4 = tod_data.data[b1], tod_data.data[b2]

            d1 -= np.mean(d1[start_time:end_time])
            d2 -= np.mean(d2[start_time:end_time])
            d3 -= np.mean(d3[start_time:end_time])
            d4 -= np.mean(d4[start_time:end_time])

            time = tod_data.ctime - tod_data.ctime[0]
            time = time[start_time:end_time]

            d_1 = d1[start_time:end_time]
            d_2 = d2[start_time:end_time]
            d_3 = d3[start_time:end_time]
            d_4 = d4[start_time:end_time]

            return time, d_1, d_2, d_3, d_4

        def total_energy(pid, start_time, end_time):

            pix_all_amps = []

            pix_all_amps.append(
                energyseries(pid, start_time, end_time, buffer=0)[1])
            pix_all_amps.append(
                energyseries(pid, start_time, end_time, buffer=0)[2])
            pix_all_amps.append(
                energyseries(pid, start_time, end_time, buffer=0)[3])
            pix_all_amps.append(
                energyseries(pid, start_time, end_time, buffer=0)[4])

            Det_pWatts_90_a = []
            Det_pWatts_90_b = []
            Det_pWatts_150_a = []
            Det_pWatts_150_b = []

            Det_pJoules_90_a = []
            Det_pJoules_90_b = []
            Det_pJoules_150_a = []
            Det_pJoules_150_b = []

            for i in range(0, len(pix_all_amps), 4):
                ampid_1 = pix_all_amps[i]
                array_min_1 = np.amin(ampid_1)
                new_pix_amps_1 = ampid_1 - array_min_1
                pWatts_1 = np.sum(new_pix_amps_1) * 10**(12) / (400.)
                Det_pWatts_90_a.append(pWatts_1)
                Det_pJoules_90_a.append(pWatts_1 * (end_time - start_time))

                ampid_2 = pix_all_amps[i + 1]
                array_min_2 = np.amin(ampid_2)
                new_pix_amps_2 = ampid_2 - array_min_2
                pWatts_2 = np.sum(new_pix_amps_2) * 10**(12) / (400.)
                Det_pWatts_90_b.append(pWatts_2)
                Det_pJoules_90_b.append(pWatts_2 * (end_time - start_time))

                ampid_3 = pix_all_amps[i + 2]
                array_min_3 = np.amin(ampid_3)
                new_pix_amps_3 = ampid_3 - array_min_3
                pWatts_3 = np.sum(new_pix_amps_3) * 10**(12) / (400.)
                Det_pWatts_150_a.append(pWatts_3)
                Det_pJoules_150_a.append(pWatts_3 * (end_time - start_time))

                ampid_4 = pix_all_amps[i + 3]
                array_min_4 = np.amin(ampid_4)
                new_pix_amps_4 = ampid_4 - array_min_4
                pWatts_4 = np.sum(new_pix_amps_4) * 10**(12) / (400.)
                Det_pWatts_150_b.append(pWatts_4)

                Tot_pW_90a = np.sum(Det_pWatts_90_a)
                Tot_pW_90b = np.sum(Det_pWatts_90_b)
                Tot_pW_150a = np.sum(Det_pWatts_150_a)
                Tot_pW_150b = np.sum(Det_pWatts_150_b)

                Tot_pJ_90a = np.sum(Det_pJoules_90_a)
                Tot_pJ_90b = np.sum(Det_pJoules_90_b)
                Tot_pJ_150a = np.sum(Det_pJoules_150_a)
                Tot_pJ_150b = np.sum(Det_pJoules_150_b)

                values = [Tot_pJ_90a, Tot_pJ_90b, Tot_pJ_150a, Tot_pJ_150b]
                val_sum = np.sum(values)
                min_value = np.amin(values)
                max_value = np.amax(values)

                return val_sum  #,values

        event_list = []
        for event in peaks:
            pixels = pixels_affected_in_event(cosig, event)
            s_time = event[0]
            e_time = event[1]
            event_total_energy = 0
            for pixel in pixels:
                event_total_energy += total_energy(
                    pixel, s_time, e_time
                )  # * 6.241509 *10**6 # used if you need to convert from pJ to eV
            self._hist.fill(event_total_energy)
            event_list.append(event_total_energy)

        e_min = np.min(event_list)
        e_max = np.max(event_list)

        print "Min energy of event:", e_min, 'pJoules. Max energy of event:', e_max, 'pJoules'
Пример #8
0
    def execute(self):
        print '[INFO] Checking for correlation ...'
        tod_data = self.get_store().get(self._tod_key)  # retrieve tod_data
        cuts = self.get_store().get(self._cosig_key)  # retrieve tod_data
        peaks = cuts['peaks']

        def timeseries(pixel_id, s_time, e_time, buffer=10):

            start_time = s_time - buffer
            end_time = e_time + buffer

            a1, a2 = self._pr.get_f1(pixel_id)
            b1, b2 = self._pr.get_f2(pixel_id)
            d1, d2 = tod_data.data[a1], tod_data.data[a2]
            d3, d4 = tod_data.data[b1], tod_data.data[b2]

            # try to remove the mean from start_time to end_time
            d1 -= np.mean(d1[start_time:end_time])
            d2 -= np.mean(d2[start_time:end_time])
            d3 -= np.mean(d3[start_time:end_time])
            d4 -= np.mean(d4[start_time:end_time])

            time = tod_data.ctime - tod_data.ctime[0]
            time = time[start_time:end_time]

            d_1 = d1[start_time:end_time]
            d_2 = d2[start_time:end_time]
            d_3 = d3[start_time:end_time]
            d_4 = d4[start_time:end_time]

            return time, d_1, d_2, d_3, d_4

        def avg_signal(pixels, start_time, end_time):

            for pid in pixels:

                # x = timeseries(pid,start_time,end_time)[0]
                # y = timeseries(pid,start_time,end_time)[1]
                x, y1, y2, y3, y4 = timeseries(pid, start_time, end_time)

                avg_y1, avg_y2, avg_y3, avg_y4 = np.zeros(len(y1)), np.zeros(
                    len(y2)), np.zeros(len(y3)), np.zeros(len(y4))

                avg_x = x
                avg_y1 += y1
                avg_y2 += y2
                avg_y3 += y3
                avg_y4 += y4

            x = avg_x
            y1 = avg_y1 / len(avg_y1)
            y2 = avg_y2 / len(avg_y2)
            y3 = avg_y3 / len(avg_y3)
            y4 = avg_y4 / len(avg_y4)
            return x, y1, y2, y3, y4

        def correlation(x1, x2, y1, y2):
            """
            f1 = interp1d(x1,y1)
            f2 = interp1d(x2,y2)

            points = 100
            # points = 2*max(len(x1), len(x2))  # double precision

            x1new = np.linspace(min(x1), max(x1), points)
            x2new = np.linspace(min(x2), max(x2), points)

            y1new = f1(x1new)
            y2new = f2(x2new)
            """
            """
            NUMPY CORRELATION ROUTINE
            """
            #m_coeff = np.corrcoef(y1new,y2new)[0][1]
            """
            a = y1new
            b = y2new
            a = (a - np.mean(a)) / (np.std(a) * len(a))
            b = (b - np.mean(b)) / (np.std(b))
            c = np.correlate(a, b, 'full')
            m_coeff = np.max(abs(c))

            return m_coeff
            """

            ts1 = y1
            ts2 = y2
            l1 = len(ts1)
            l2 = len(ts2)
            if l1 < l2:
                n = l1
                return max([
                    np.corrcoef(ts1, ts2[i:n + i])[0][1]
                    for i in range(0, l2 - l1)
                ])
            elif l2 < l1:
                n = l2
                return max([
                    np.corrcoef(ts1[i:n + i], ts2)[0][1]
                    for i in range(0, l1 - l2)
                ])
            else:  # l1 == l2
                return np.corrcoef(ts1, ts2)[0][1]
            """
            plt.subplot(211)
            plt.plot( x1new,y1new,'g--')
            plt.title('Two Signals to Check for Correlation')
            plt.subplot(212)
            plt.plot(x2new,y2new,'r--')
            plt.xlabel('Cor. Matrix Coeff: ' + str(m_coeff))
            plt.show()
            """

        """
        CHECK CORRELATION BETWEEN SIGNALS FROM TWO EVENTS
        Find avgerage signal from an peak, copy events from peaks data below
        To check correlation, call correlation function with peak data
        """

        cs = cuts['coincident_signals']
        """
        FOR TWO SPECIFIC EVENTS
        """
        """
        event1 = [133034,133273,239,8]
        stime1 = event1[0]
        etime1 = event1[1]
        pixels1 = pixels_affected_in_event(cs, event1)
        avg_x1, avg_y1 = avg_signal(pixels1, stime1, etime1)
        np.savetxt('newslow_template.txt',(avg_x1,avg_y1))

#        event2 = [205344, 205375, 31, 35]
        event2 = [9300,9303,3,2]
        stime2 = event2[0]
        etime2 = event2[1]
        pixels2 = pixels_affected_in_event(cs, event2)
        avg_x2, avg_y2 = avg_signal(pixels2, stime2, etime2)
        
        correlation(avg_x1,avg_x2, avg_y1, avg_y2)
        """
        """
        TEMPLATE FRB or CR  AS EVENT 1
        change name of .txt file to frb_template or cr_template 
        to check correlation for either signal 
        """
        avg_x1, avg_y1 = self._template[0], self._template[1]
        """
        ALL EVENTS
        To compare all events in track to template, 
        initiate this loop
        """

        # Save outputs to a dictionary, here we initialize an empty dictionary
        events = []
        all_coeffs = []
        lower_threshold = 0.6
        upper_threshold = self._coeff

        for peak in peaks:
            all_pixels = pixels_affected_in_event(cs, peak)
            avg_x2, avg_y2_1, avg_y2_2, avg_y2_3, avg_y2_4 = avg_signal(
                all_pixels, peak[0], peak[1])
            coeff1 = correlation(avg_x1, avg_x2, avg_y1, avg_y2_1)
            coeff2 = correlation(avg_x1, avg_x2, avg_y1, avg_y2_2)
            coeff3 = correlation(avg_x1, avg_x2, avg_y1, avg_y2_3)
            coeff4 = correlation(avg_x1, avg_x2, avg_y1, avg_y2_4)
            all_coeffs.append(coeff1)
            if (lower_threshold <= coeff1) & (lower_threshold <= coeff2) & (
                    lower_threshold <= coeff3
            ) & (lower_threshold <= coeff4) & (coeff1 < upper_threshold) & (
                    coeff2 < upper_threshold) & (coeff3 < upper_threshold) & (
                        coeff4 < upper_threshold):
                print '[INFO] Possible %s' % self._tag, peak, 'Coeff = ', coeff1, coeff2, coeff3, coeff4
                #all_coeffs.append(coeff)
            elif (coeff1 >= upper_threshold) & (coeff2 >= upper_threshold) & (
                    coeff3 >= upper_threshold) & (coeff4 >= upper_threshold):
                print '[INFO] Highly Likely %s' % self._tag, peak, 'Coeff = ', coeff1, coeff2, coeff3, coeff4
                #all_coeffs.append(coeff)
                start = peak[0]
                end = peak[1]
                duration = peak[2]
                number_of_pixels = peak[3]
                ref_index = int((start + end) / 2)  # use as reference point
                id = "%d.%d" % (self.get_id(), start)
                event = {
                    'id': id,
                    'start': start,  # start index
                    'end': end,  # end index
                    'duration': duration,
                    'ctime': tod_data.ctime[ref_index],  # ref time
                    'alt': tod_data.alt[ref_index],  # ref alt
                    'az': tod_data.az[ref_index],  # ref az
                    'number_of_pixels': number_of_pixels,
                    'pixels_affected': all_pixels,
                    'coefficients': [coeff1, coeff2, coeff3, coeff4],
                    'tag': self._tag
                }
                events.append(event)

        print '[INFO] Events passed: %d / %d' % (len(events), len(peaks))
        self.get_store().set(self._output_key, events)
        self.get_store().set(self._all_coeff_output_key, all_coeffs)
Пример #9
0
    def execute(self):
        print '[INFO] Checking for correlation ...'
        tod_data = self.get_store().get(self._tod_key)  # retrieve tod_data
        cuts = self.get_store().get(self._cosig_key)  # retrieve tod_data
        peaks = cuts['peaks']

        def timeseries(pixel_id, s_time, e_time, buffer=10):

            start_time = s_time - buffer
            end_time = e_time + buffer

            a1, a2 = self._pr.get_f1(pixel_id)
            b1, b2 = self._pr.get_f2(pixel_id)
            d1, d2 = tod_data.data[a1], tod_data.data[a2]
            d3, d4 = tod_data.data[b1], tod_data.data[b2]

            # try to remove the mean from start_time to end_time
            d1 -= np.mean(d1[start_time:end_time])
            d2 -= np.mean(d2[start_time:end_time])
            d3 -= np.mean(d3[start_time:end_time])
            d4 -= np.mean(d4[start_time:end_time])

            time = tod_data.ctime - tod_data.ctime[0]
            time = time[start_time:end_time]

            d_1 = d1[start_time:end_time]
            d_2 = d2[start_time:end_time]
            d_3 = d3[start_time:end_time]
            d_4 = d4[start_time:end_time]
            
            return time, d_1

        """                                                                     
        TEMPLATE FRB or CR  AS EVENT 1                                          
        change name of .txt file to frb_template or cr_template                 
        to check correlation for either signal                                  
        """
        avg_x1, avg_y1 = self._template[0], self._template[1]
        temp_time = len(avg_x1)

        def avg_signal(pixels, start_time, end_time):

            for pid in pixels:

                x = timeseries(pid,start_time,end_time)[0]
                y = timeseries(pid,start_time,end_time)[1]

                if len(x) < temp_time:
                    buff = int(temp_time - len(x))/2
                    x, y = timeseries(pid,start_time,end_time,buff)
                    avg_y = np.zeros(len(y))
                    avg_x = x 
                    avg_y += y 
                    x1 = avg_x1
                    y1 = avg_y1
                else:
                    buff = int(len(x)-temp_time)/2
                    pad = [0]*buff
                    x, y = timeseries(pid,start_time,end_time)
                    avg_y = np.zeros(len(y))
                    avg_x = x
                    avg_y += y
                    x1,y1 =[],[]
                    x1.extend(pad)
                    x1.extend(avg_x1)
                    x1.extend(pad)
                    y1.extend(pad)
                    y1.extend(avg_y1)
                    y1.extend(pad)


            x2 = avg_x
            y2 = avg_y/len(avg_y)
            
            return x1,y1,x2,y2

       
        def correlation(x1,x2,y1,y2):
            
            """
            #NORMALIZE THE SIGNAL BEFORE CORRELATING
            min_y1,max_y1= np.min(y1), np.max(y1)
            min_y2,max_y2 = np.min(y2), np.max(y2)
            
            norm_y1 = (y1 - min_y1)/(max_y1 - min_y1)
            norm_y2 = (y2 - min_y2)/(max_y2 - min_y2)
            """
            
            f1 = interp1d(x1,norm_y1)
            f2 = interp1d(x2,norm_y2)

            points = 100
            # points = 2*max(len(x1), len(x2))  # double precision

            x1new = np.linspace(min(x1), max(x1), points)
            x2new = np.linspace(min(x2), max(x2), points)

            y1new = f1(x1new)
            y2new = f2(x2new)
            
            py1 = pd.DataFrame(y1new)
            py2 = pd.DataFrame(y2new)
            cor = pd.rolling_cor(py1,py2,5,center=True)
            coeff = np.array(cor)
            coeff = coeff[np.logical_not(np.isnan(coeff))]
            coeff = abs(coeff)
            
            max_coeff = max(coeff)

            return max_coeff

            """
            plt.subplot(211)
            plt.plot( x1new,y1new,'g--')
            plt.title('Two Signals to Check for Correlation')
            plt.subplot(212)
            plt.plot(x2new,y2new,'r--')
            plt.xlabel('Cor. Matrix Coeff: ' + str(m_coeff))
            plt.show()
            """

        """
        CHECK CORRELATION BETWEEN SIGNALS FROM TWO EVENTS
        Find avgerage signal from an peak, copy events from peaks data below
        To check correlation, call correlation function with peak data
        """

        cs = cuts['coincident_signals']

        """
        FOR TWO SPECIFIC EVENTS
        """
        """
        event1 = [101980, 101985, 5, 2]
        stime1 = event1[0]
        etime1 = event1[1]
        pixels1 = pixels_affected_in_event(cs, event1)
        avg_x1, avg_y1 = avg_signal(pixels1, stime1, etime1)
        np.savetxt('frb_template.txt',(avg_x1,avg_y1))

        
#        event2 = [205344, 205375, 31, 35]
        event2 = [9300,9303,3,2]
        stime2 = event2[0]
        etime2 = event2[1]
        pixels2 = pixels_affected_in_event(cs, event2)
        avg_x2, avg_y2 = avg_signal(pixels2, stime2, etime2)
        
        correlation(avg_x1,avg_x2, avg_y1, avg_y2)
        """

        """
        ALL EVENTS
        To compare all events in track to template, 
        initiate this loop
        """

        # Save outputs to a dictionary, here we initialize an empty dictionary
        events = []
        all_coeffs = []
        lower_threshold = 0.6
        upper_threshold = self._coeff

        for peak in peaks:
            all_pixels = pixels_affected_in_event(cs, peak)
            avg_x1,avg_y1,avg_x2, avg_y2 = avg_signal(all_pixels, peak[0], peak[1])
            coeff = correlation(avg_x1, avg_x2, avg_y1, avg_y2)

            if lower_threshold <= coeff < upper_threshold:
                print '[INFO] Possible %s' % self._tag, peak, 'Coeff = ', coeff
                all_coeffs.append(coeff)
            elif coeff >= upper_threshold:
                all_coeffs.append(coeff)
                print '[INFO] Highly Likely %s' % self._tag, peak, 'Coeff = ', coeff
                start = peak[0]
                end = peak[1]
                duration = peak[2]
                number_of_pixels = peak[3]
                ref_index = int((start + end)/2)  # use as reference point
                id = "%d.%d" % (self.get_id(), start)
                event = {
                    'id': id,
                    'start': start,  # start index
                    'end': end,  # end index
                    'duration': duration,
                    'ctime': tod_data.ctime[ref_index],  # ref time
                    'alt': tod_data.alt[ref_index],  # ref alt
                    'az': tod_data.az[ref_index],  # ref az
                    'number_of_pixels': number_of_pixels,
                    'pixels_affected': all_pixels,
                    'coefficient': coeff,
                    'tag': self._tag
                }
                events.append(event)

        print '[INFO] Events passed: %d / %d' % (len(events), len(peaks))
        self.get_store().set(self._output_key, events)
        self.get_store().set(self._all_coeff_output_key,all_coeffs)
Пример #10
0
    def execute(self):
        print '[INFO] Loading Glitch Data ...'
        tod_data = self.get_store().get(self._tod_key)  # retrieve tod_data
        cuts = self.get_store().get(self._cosig_key)  # retrieve tod_data

        # print('[INFO] pixels affected: ',pixels)
        peaks = cuts['peaks']
        #print('[INFO] peaks: ', peaks)

        def cs_cuts():
            cuts = self.get_store().get(self._cosig_key) 
            return cuts['coincident_signals']

        def timeseries(pixel_id, s_time, e_time, buffer=10):

            start_time = s_time - buffer
            end_time = e_time + buffer

            a1, a2 = self._pr.get_f1(pixel_id)
            b1, b2 = self._pr.get_f2(pixel_id)
            d1, d2 = tod_data.data[a1], tod_data.data[a2]
            d3, d4 = tod_data.data[b1], tod_data.data[b2]

            # try to remove the mean from start_time to end_time
            d1 -= np.mean(d1[start_time:end_time])
            d2 -= np.mean(d2[start_time:end_time])
            d3 -= np.mean(d3[start_time:end_time])
            d4 -= np.mean(d4[start_time:end_time])
            
            time = tod_data.ctime - tod_data.ctime[0]
            time = time[start_time:end_time]
            
            d_1 = d1[start_time:end_time]
            d_2 = d2[start_time:end_time]
            d_3 = d3[start_time:end_time]
            d_4 = d4[start_time:end_time]

            """
            UNCOMMENT TO PLOT FOUR CORRESPONDING PIXELS WITH HI-LO FREQ
            plt.plot(time,d_1, '.-', label=str(a1) + ' 90 GHz')
            plt.plot(time, d_2, '.-', label=str(a2) + ' 90 GHz')
            plt.plot(time, d_3, '.-', label=str(b1) + ' 150 GHz')
            plt.plot(time, d_4, '.-', label=str(b2) + ' 150 GHz')
            plt.legend(title='Detector UID')
            plt.show()
            """

            return time, d_1, d_2, d_3, d_4


        """
        PLOTTING FUNCTION
        Plot all pixels affected given an array of pixel ids
        and a starting time and ending time
      
        """
        def plotter(pixels,start_time,end_time):
                        
            for pid in pixels:
               
                x = timeseries(pid,start_time,end_time)[0]
                y1 = timeseries(pid,start_time,end_time)[1]
                y2 = timeseries(pid,start_time,end_time)[2]
                y3 = timeseries(pid,start_time,end_time)[3]
                y4 = timeseries(pid,start_time,end_time)[4]


                plt.title('Pixel affected from ' +str(start_time)+ '-' + str(end_time)+ ', Pixel ' + str(pid))
                plt.xlabel('TOD track:' + str(self._tag))  # CHANGE TOD TRACK NAME
                plt.plot(x,y1,'.-',label='90 GHz')
                plt.plot(x,y2,'.-',label='90 GHz')
                plt.plot(x,y3,'.-',label='150 GHz')
                plt.plot(x,y4,'.-',label='150 GHz')
                
                plt.legend()
                plt.show()





            
        """
        ALL EVENTS
        From peaks, find cs, then use cs to find all pixels affected
        then plot all pixels affected in all events in peak one by one
        """
        
        cs = cuts['coincident_signals']
        """
        for event in peaks:
            all_pixels = pixels_affected_in_event(cs,event)
            plotter(all_pixels, event[0], event[1])
        """
        
        """
        SPECIFIC EVENT
        To plot specific event, copy event from peaks below 
        """
      
        e = raw_input('Please copy the event list to plot 4 freq channels:')
        event = json.loads(e)
        stime = event[0]
        etime = event[1]
        pixels = pixels_affected_in_event(cs, event)
        print 'Pixels Affected:', pixels
        plotter(pixels, stime, etime)
        
        self._pr.plot(pixels)
        plt.show()
        
        y_n = ' '

        while y_n != 'n':
            y_n = raw_input ("Would you like to plot another event? Enter y/n...")
            if y_n == 'y':
                e= raw_input('Please copy the event list to plot 4 freq channels:')
                event = json.loads(e)
                stime = event[0]
                etime = event[1]
                pixels = pixels_affected_in_event(cs, event)
                print '[INFO] Plotting Glitch...'
                plotter(pixels, stime, etime)
                self._pr.plot(pixels)
                plt.show()
            
            else:
                print 'No plot will be displayed!'      


        """