Exemplo n.º 1
0
def create_matrix_from_file(coh_file, channels):
    """
    Creates coherence matrix from data that's in a file.
    Used typically as helper function for plotting

    Parameters:
    -----------
    coh_file : str
        File containing coherence data
    channels : list (str)
        channels to plot

    Returns: 
    --------
    coh_matrix : Spectrogram object
        coherence matrix in form of spectrogram object
        returns automatically in terms of coherence SNR:
        coherence * N.
        (not actually a spectrogram, though)
    frequencies : numpy array
        numpy array of frequencies associated with coherence matrix
    labels : list (str)
        labels for coherence matrix
    N : int
        Number of time segment used to create coherence spectra
    """
    labels = []
    counter = 0
    if not os.path.exists(coh_file):
	return None, None, None, None
    f = h5py.File(coh_file, 'r')
    # get number of averages
    N = f['info'].value
    channels = f['psd2s'].keys()
    First = 1
    s = 0
    for channel in channels:
        data = Spectrum.from_hdf5(f['coherences'][channel])
        s_temp = data.size
        if s_temp > s:
            s = s_temp
    for channel in channels:
        if First:
            # initialize matrix!
            darm_psd = Spectrum.from_hdf5(f['psd1'][f['psd1'].keys()[0]])
            First = 0
            s = max(s, darm_psd.size)
            coh_matrix = np.zeros((s, len(channels)))
        data = Spectrum.from_hdf5(f['coherences'][channel])
        labels.append(channel[3:-3].replace('_', '-'))
        coh_matrix[:data.size, counter] = data
        counter += 1
    coh_matrix = Spectrogram(coh_matrix)
    frequencies = (np.arange(s)+1) * (darm_psd.frequencies.value[1] - darm_psd.frequencies.value[0])
    return coh_matrix, frequencies, labels, N