Average forward solutions. Parameters ---------- fwds : list of Forward Forward solutions to average. Each entry (dict) should be a forward solution. weights : array | None Weights to apply to each forward solution in averaging. If None, forward solut
(fwds, weights=None, verbose=None)
| 2092 | |
| 2093 | @verbose |
| 2094 | def average_forward_solutions(fwds, weights=None, verbose=None): |
| 2095 | """Average forward solutions. |
| 2096 | |
| 2097 | Parameters |
| 2098 | ---------- |
| 2099 | fwds : list of Forward |
| 2100 | Forward solutions to average. Each entry (dict) should be a |
| 2101 | forward solution. |
| 2102 | weights : array | None |
| 2103 | Weights to apply to each forward solution in averaging. If None, |
| 2104 | forward solutions will be equally weighted. Weights must be |
| 2105 | non-negative, and will be adjusted to sum to one. |
| 2106 | %(verbose)s |
| 2107 | |
| 2108 | Returns |
| 2109 | ------- |
| 2110 | fwd : Forward |
| 2111 | The averaged forward solution. |
| 2112 | """ |
| 2113 | # check for fwds being a list |
| 2114 | _validate_type(fwds, list, "fwds") |
| 2115 | if not len(fwds) > 0: |
| 2116 | raise ValueError("fwds must not be empty") |
| 2117 | |
| 2118 | # check weights |
| 2119 | if weights is None: |
| 2120 | weights = np.ones(len(fwds)) |
| 2121 | weights = np.asanyarray(weights) # in case it's a list, convert it |
| 2122 | if not np.all(weights >= 0): |
| 2123 | raise ValueError("weights must be non-negative") |
| 2124 | if not len(weights) == len(fwds): |
| 2125 | raise ValueError("weights must be None or the same length as fwds") |
| 2126 | w_sum = np.sum(weights) |
| 2127 | if not w_sum > 0: |
| 2128 | raise ValueError("weights cannot all be zero") |
| 2129 | weights /= w_sum |
| 2130 | |
| 2131 | # check our forward solutions |
| 2132 | for fwd in fwds: |
| 2133 | # check to make sure it's a forward solution |
| 2134 | _validate_type(fwd, dict, "each entry in fwds", "dict") |
| 2135 | # check to make sure the dict is actually a fwd |
| 2136 | check_keys = [ |
| 2137 | "info", |
| 2138 | "sol_grad", |
| 2139 | "nchan", |
| 2140 | "src", |
| 2141 | "source_nn", |
| 2142 | "sol", |
| 2143 | "source_rr", |
| 2144 | "source_ori", |
| 2145 | "surf_ori", |
| 2146 | "coord_frame", |
| 2147 | "mri_head_t", |
| 2148 | "nsource", |
| 2149 | ] |
| 2150 | if not all(key in fwd for key in check_keys): |
| 2151 | raise KeyError( |