コード例 #1
0
ファイル: analyze_pixel.py プロジェクト: lindat18/ffta
def analyze_pixel(ibw_file, param_file):
    '''
    Analyzes a single pixel
    
    Parameters
    ----------
    ibw_file : str
        path to \*.ibw file
    param_file : str
        path to parameters.cfg file
        
    Returns
    -------
    pixel : Pixel
        The pixel object read and analyzed
    '''
    signal_array = signal(ibw_file)
    n_pixels, params = configuration(param_file)
    pixel = Pixel(signal_array, params=params)

    pixel.analyze()
    pixel.plot()
    plt.xlabel('Time Step')
    plt.ylabel('Freq Shift (Hz)')

    print('tFP is', pixel.tfp, 's')

    return pixel.tfp
コード例 #2
0
    def test(self, pixel_ind=[0, 0]):
        """
        Test the Pixel analysis of a single pixel

        :param pixel_ind: Index of the pixel in the dataset that the process needs to be tested on.
            If a list it is read as [row, column]
        :type pixel_ind: uint or list
            

        :returns: List [inst_freq, tfp, shift]
            WHERE
            array inst_freq is the instantaneous frequency array for that pixel
            float tfp is the time to first peak
            float shift the frequency shift at time t=tfp (i.e. maximum frequency shift)

        """
        # First read the HDF5 dataset to get the deflection for this pixel

        if type(pixel_ind) is not list:
            col = int(pixel_ind % self.parm_dict['num_rows'])
            row = int(np.floor(pixel_ind % self.parm_dict['num_rows']))
            pixel_ind = [row, col]

        # as an array, not an ffta.Pixel
        defl = get_utils.get_pixel(self.h5_main, pixel_ind, array_form=True)

        pix = Pixel(defl, self.parm_dict, **self.pixel_params)

        tfp, shift, inst_freq = pix.analyze()
        pix.plot()

        return self._map_function(defl, self.parm_dict, self.pixel_params,
                                  self.impulse)
コード例 #3
0
    def _map_function(defl, *args, **kwargs):
        """
        
        :param defl:
        :type defl:
        
        :param args:
        :type args:
        
        :param kwargs:
        :type kwargs:
        
        :returns: List [inst_freq, amplitude, phase, tfp, shift, pwr_diss]
            WHERE
            [type] inst_freq is...
            [type] amplitude is...
            [type] phase is...
            [type] tfp is...
            [type] shift is...
            [type] pwr_diss is...
        """
        parm_dict = args[0]
        pixel_params = args[1]
        impulse = args[2]

        pix = Pixel(defl, parm_dict, **pixel_params)

        if parm_dict['if_only']:
            inst_freq, _, _ = pix.generate_inst_freq()
            tfp = 0
            shift = 0
            amplitude = 0
            phase = 0
            pwr_diss = 0
        elif parm_dict['deconvolve']:
            iterations = parm_dict['conv_iterations']
            impulse = impulse[
                parm_dict['impulse_window'][0]:parm_dict['impulse_window'][1]]
            inst_freq, amplitude, phase = pix.generate_inst_freq()
            conv = restoration.richardson_lucy(inst_freq,
                                               impulse,
                                               clip=False,
                                               num_iter=iterations)
            pix.inst_freq = conv
            pix.find_tfp()
            tfp = pix.tfp
            shift = pix.shift
            pix.calculate_power_dissipation()
            pwr_diss = pix.power_dissipated
        else:
            tfp, shift, inst_freq = pix.analyze()
            pix.calculate_power_dissipation()
            amplitude = pix.amplitude
            phase = pix.phase
            pwr_diss = pix.power_dissipated

        return [inst_freq, amplitude, phase, tfp, shift, pwr_diss]
コード例 #4
0
    def _map_function(defl, *args, **kwargs):

        parm_dict = args[0]
        pixel_params = args[1]

        pix = Pixel(defl, parm_dict, **pixel_params)

        if parm_dict['if_only']:
            inst_freq, _, _ = pix.generate_inst_freq()
            tfp = 0
            shift = 0
            amplitude = 0
            phase = 0
            pwr_diss = 0
        else:
            tfp, shift, inst_freq = pix.analyze()
            pix.calculate_power_dissipation()
            amplitude = pix.amplitude
            phase = pix.phase
            pwr_diss = pix.power_dissipated

        return [inst_freq, amplitude, phase, tfp, shift, pwr_diss]
コード例 #5
0
    def test_deconv(self, window, pixel_ind=[0, 0], iterations=10):
        """
        Tests the deconvolution by bracketing the impulse around window
        A reasonable window size might be 100 us pre-trigger to 500 us post-trigger
        
        :param window: List of format [left_index, right_index] for impulse
        :type window: list
        
        :param pixel_ind: Index of the pixel in the dataset that the process needs to be tested on.
            If a list it is read as [row, column]
        :type pixel_ind: uint or list
            
        
        :param iterations: Number of Richardson-Lucy deconvolution iterations
        :type iterations: int

        """
        if len(window) != 2 or not isinstance(window, list):
            raise ValueError('window must be specified[left, right]')

        if isinstance(window[0], float):  # passed a time index
            window = np.array(window) / self.parm_dict['sampling_rate']
            window = int(window)

        if type(pixel_ind) is not list:
            col = int(pixel_ind % self.parm_dict['num_rows'])
            row = int(np.floor(pixel_ind % self.parm_dict['num_rows']))
            pixel_ind = [row, col]

        # as an array, not an ffta.Pixel
        defl = get_utils.get_pixel(self.h5_main, pixel_ind, array_form=True)

        pixraw = Pixel(defl, self.parm_dict, **self.pixel_params)
        tfp, shift, inst_freq = pixraw.analyze()

        impulse = self.impulse[window[0]:window[1]]
        self.parm_dict['impulse_window'] = window
        self.parm_dict['conv_iterations'] = iterations

        pixconv = restoration.richardson_lucy(inst_freq,
                                              impulse,
                                              clip=False,
                                              num_iter=iterations)

        pixrl = pixraw
        tfp_raw = pixraw.tfp
        fit_raw = pixraw.best_fit
        if_raw = pixraw.inst_freq

        pixrl.inst_freq = pixconv
        pixrl.find_tfp()
        tfp_conv = pixrl.tfp
        fit_conv = pixrl.best_fit
        if_conv = pixrl.inst_freq

        print(tfp_raw, tfp_conv)

        # Plot the results of the original+fit compared to deconvolution+fit
        ridx = int(self.parm_dict['roi'] * self.parm_dict['sampling_rate'])
        fidx = int(pixraw.tidx)
        ifx = np.array([fidx - 1100, fidx + ridx
                        ]) * 1e3 / self.parm_dict['sampling_rate']
        windx = np.array(window) * 1e3 / self.parm_dict['sampling_rate']
        yl0 = [
            if_raw[fidx:(fidx + ridx)].min(), if_raw[fidx:(fidx + ridx)].max()
        ]
        yl1 = [
            if_conv[fidx:(fidx + ridx)].min(),
            if_conv[fidx:(fidx + ridx)].max()
        ]
        yl2 = [
            self.impulse[fidx:(fidx + ridx)].min(),
            self.impulse[fidx:(fidx + ridx)].max()
        ]

        fig, ax = plt.subplots(nrows=2,
                               ncols=2,
                               facecolor='white',
                               figsize=(12, 8))
        tx = np.arange(0, pixraw.total_time, 1 / pixraw.sampling_rate) * 1e3

        ax[0][0].plot(tx, if_raw, 'b', label='Pre-Conv')
        ax[0][0].plot(tx[fidx:(fidx + ridx)],
                      fit_raw,
                      'r--',
                      label='Pre-Conv, fit')
        ax[0][0].set_title('Raw')

        ax[1][0].plot(tx, if_conv, 'b', label='Conv')
        ax[1][0].plot(tx[fidx:(fidx + ridx)],
                      fit_conv,
                      'r--',
                      label='Conv, fit')
        ax[1][0].set_title('Deconvolved')

        ax[0][1].plot(tx, self.impulse, 'k')
        ax[0][1].axvspan(windx[0], windx[1], alpha=0.5, color='red')
        ax[0][1].set_title('Impulse response')

        ax[1][1].plot(tx[fidx:(fidx + ridx)],
                      fit_raw,
                      'r--',
                      label='Pre-Conv, fit')
        ax[1][1].plot(tx[fidx:(fidx + ridx)],
                      fit_conv,
                      'k--',
                      label='Conv, fit')
        ax[1][1].set_title('Comparing fits')
        ax[1][1].legend()

        ax[0][0].set_xlim(ifx)
        ax[0][0].set_ylim(yl0)
        ax[1][0].set_xlim(ifx)
        ax[1][0].set_ylim(yl1)
        ax[0][1].set_xlim(ifx)
        ax[0][1].set_ylim(yl2)
        ax[1][1].set_xlim(ifx)
        ax[1][0].set_xlabel('Time (ms)')
        ax[1][1].set_xlabel('Time (ms)')

        plt.tight_layout()

        return pixrl