Repository navigation
Expand file tree
/
Copy pathanalysis.py
More file actions
159 lines (118 loc) · 5.1 KB
/
Copy pathanalysis.py
File metadata and controls
159 lines (118 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"""Analysis functions for FOOOF results."""
import numpy as np
###################################################################################################
###################################################################################################
def get_band_peak_fm(fm, band, ret_one=True, attribute='peak_params'):
"""Extract peaks from a band of interest from a FOOOFGroup object.
Parameters
----------
fm : FOOOF
FOOOF object to extract peak data from.
band : tuple of (float, float)
Defines the band of interest, as (lower_frequency_bound, upper_frequency_bound).
ret_one : bool, optional, default: True
Whether to return single peak (if True) or all peaks within the range found (if False).
If True, returns the highest power peak within the search range.
attribute : {'peak_params', 'gaussian_params'}
Which attribute of peak data to extract data from.
Returns
-------
1d or 2d array
Peak data. Each row is a peak, as [CF, Amp, BW]
"""
return get_band_peak(getattr(fm, attribute + '_'), band, ret_one)
def get_band_peaks_fg(fg, band, attribute='peak_params'):
"""Extract peaks from a band of interest from a FOOOF object.
Parameters
----------
fg : FOOOFGroup
FOOOFGroup object to extract peak data from.
band : tuple of (float, float)
Defines the band of interest, as (lower_frequency_bound, upper_frequency_bound).
attribute : {'peak_params', 'gaussian_params'}
Which attribute of peak data to extract data from.
Returns
-------
2d array
Peak data. Each row is a peak, as [CF, Amp, BW].
"""
return get_band_peaks_group(fg.get_all_data(attribute), band, len(fg))
def get_band_peaks_group(peak_params, band, n_fits):
"""Extracts peaks within a given band of interest.
Parameters
----------
peak_params : 2d array
Peak parameters, for a group fit, from FOOOF, with shape of [n_peaks, 4].
band : tuple of (float, float)
Defines the band of interest, as (lower_frequency_bound, upper_frequency_bound).
n_fits : int
The number of model fits in the FOOOFGroup data.
Returns
-------
band_peaks : 2d array
Peak data. Each row is a peak, as [CF, PW, BW].
Notes
-----
This function conserves the shape of the array, returning a [n_fits, 3] array.
* Each row reflects a FOOOF model fit, in order, filled with NaN if no peak was present.
* To do so, this function necessarily extracts and returns one peak per model fit.
If, instead, you with to extract all peaks within a band, per model fit, you can do:
>>> peaks = np.empty((0, 3))
>>> for f_res in fg:
>>> peaks = np.vstack((peaks, get_band_peak(f_res.peak_params, band, ret_one=False)))
"""
band_peaks = np.zeros(shape=[n_fits, 3])
# Extacts an array per FOOOF fit, and extracts band peaks from it
for ind in range(n_fits):
band_peaks[ind, :] = get_band_peak(peak_params[tuple([peak_params[:, -1] == ind])][:, 0:3],
band=band, ret_one=True)
return band_peaks
def get_band_peak(peak_params, band, ret_one=True):
"""Extracts peaks within a given band of interest.
Parameters
----------
peak_params : 2d array
Peak parameters, from FOOOF, with shape of [n_peaks, 3].
band : tuple of (float, float)
Defines the band of interest, as (lower_frequency_bound, upper_frequency_bound).
ret_one : bool, optional, default: True
Whether to return single peak (if True) or all peaks within the range found (if False).
If True, returns the highest peak within the search range.
Returns
-------
band_peaks : 1d or 2d array
Peak data. Each row is a peak, as [CF, PW, BW]
"""
# Return nan array if empty input
if peak_params.size == 0:
return np.array([np.nan, np.nan, np.nan])
# Find indices of peaks in the specified range, and check the number found
peak_inds = (peak_params[:, 0] >= band[0]) & (peak_params[:, 0] <= band[1])
n_peaks = sum(peak_inds)
# If there are no peaks within the specified range
# Note: this also catches and returns if the original input was empty
if n_peaks == 0:
return np.array([np.nan, np.nan, np.nan])
band_peaks = peak_params[peak_inds, :]
# If results > 1 and ret_one, then we return the highest peak
# Call a sub-function to select highest power peak in band
if n_peaks > 1 and ret_one:
band_peaks = get_highest_peak(band_peaks)
# If results == 1, return single peak
return np.squeeze(band_peaks)
def get_highest_peak(band_peaks):
"""Searches for the highest peak.
Parameters
----------
band_peaks : 2d array
Peak parameters, from FOOOF, with shape of [n_peaks, 3].
Returns
-------
1d array
Peak data. Each row is a peak, as [CF, PW, BW].
"""
# Catch & return NaN if empty
if len(band_peaks) == 0:
return np.array([np.nan, np.nan, np.nan])
high_ind = np.argmax(band_peaks[:, 1])
return band_peaks[high_ind, :]