def get_state_frequencies(state_dynamics, method='spectrum_fourier'): """ Returns the spectrum of the state occurence for each subject. Parameters ---------- state_dynamics : n_states x n_subjects x n_timepoints array The state dynamics output from fit_states function. method : a string, check nitime.spectral.SpectralAnalyzer for allowed methods. Returns ------- results : n_subjects list of tuple, first element is the array of frequencies, second element is the array n_states x frequencies of the spectrum. """ results = [] for s in state_dynamics: ts = TimeSeries(s, sampling_interval=1.) S = SpectralAnalyzer(ts) try: result = getattr(S, method) except AttributeError as _: result = S.spectrum_fourier results.append(result) return results
def plot_spectrum(Timeseries, tr): from nitime.timeseries import TimeSeries from nitime.analysis import SpectralAnalyzer import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt import os import numpy as np figure= [] for i, timeseries in enumerate(Timeseries): #T = io.time_series_from_file(in_file,TR=tr) title = os.path.abspath('spectra') timeseries = np.asarray(timeseries[1:]) timeseries = timeseries-np.mean(timeseries)*np.ones(timeseries.shape) T = TimeSeries(timeseries,sampling_interval=tr) S_original = SpectralAnalyzer(T) # Initialize a figure to put the results into: fig01 = plt.figure(figsize = (8,3)) ax01 = fig01.add_subplot(1, 1, 1) ax01.plot(S_original.psd[0], S_original.psd[1], label='Welch PSD') ax01.plot(S_original.spectrum_fourier[0], S_original.spectrum_fourier[1], label='FFT') ax01.plot(S_original.periodogram[0], S_original.periodogram[1], label='Periodogram') ax01.plot(S_original.spectrum_multi_taper[0], S_original.spectrum_multi_taper[1], label='Multi-taper') ax01.set_xlabel('Frequency (Hz)') ax01.set_ylabel('Power') ax01.legend() Figure = title+'%02d.png'%i plt.savefig(Figure, bbox_inches='tight') figure.append(Figure) plt.close() return figure
# plt.plot(data2d[87440,:]) # In[29]: TR = 2 # In[30]: T = TimeSeries(data2d, sampling_interval=TR) # In[31]: S_original = SpectralAnalyzer(T) # In[32]: fig01 = plt.figure(2) ax01 = fig01.add_subplot(1, 1, 1) # ax01.plot(S_original.psd[0], # S_original.psd[1][9], # label='Welch PSD') ax01.plot(S_original.spectrum_fourier[0], np.abs(S_original.spectrum_fourier[1][9]), label='FFT') ax01.plot(S_original.periodogram[0],
for n_idx, roi in enumerate(roi_names): data[n_idx] = data_rec[roi] # Initialize TimeSeries object: T = TimeSeries(data, sampling_interval=TR) T.metadata['roi'] = roi_names """ We will start, by examining the spectrum of the original data, before filtering. We do this by initializing a SpectralAnalyzer for the original data: """ S_original = SpectralAnalyzer(T) # Initialize a figure to put the results into: fig01 = plt.figure() ax01 = fig01.add_subplot(1, 1, 1) """ The spectral analyzer has several different methods of spectral analysis, however the all have a common API. This means that for all of them the output is a tuple. The first item in the tuple are the central frequencies of the frequency bins in the spectrum and the second item in the tuple are the magnitude of the spectrum in that frequency bin. For the purpose of this example, we will only plot the data from the 10th ROI (by indexing into the spectra). We compare all the methods of spectral estimation by plotting them