def plot_fps(fp_arr, dim=3, decomp=isomap_factory, cmap='hsv', ax=None, fwid=5, **kwargs): p = decomp(dim) arr_iter = u.make_array_ind_iterator(fp_arr.shape[:-1]) fp_arr_flat = np.stack(list(fp_arr[ind] for ind in arr_iter if not np.any(np.isnan(fp_arr[ind]))), axis=0) p.fit(fp_arr_flat) cm = plt.get_cmap(cmap) cols = cm(np.linspace(0, 1, fp_arr.shape[0])) if ax is None: f = plt.figure(figsize=(fwid, fwid)) if dim == 3: ax = f.add_subplot(1, 1, 1, projection='3d') else: ax = f.add_subplot(1, 1, 1) for i, fp_col in enumerate(fp_arr): for ind in u.make_array_ind_iterator(fp_col.shape[:-2]): if not np.any(np.isnan(fp_col[ind])): trs_fp = p.transform(fp_col[ind]) ax.plot(*trs_fp.T, 'o', color=cols[i]) ax.plot(*trs_fp.T, color=cols[i]) return ax
def explore_mse_tradeoff_parallel(n_units, total_dims, overlaps, total_pwrs, n_regions=(1, 2), n_jobs=-1, **kwargs): arr_shape = (len(n_units), len(total_dims), len(overlaps), len(total_pwrs)) totals = {nr:np.zeros(arr_shape) for nr in n_regions} distorts = {nr:np.zeros(arr_shape) for nr in n_regions} ae_rates = {nr:np.zeros_like(distorts[nr]) for nr in n_regions} mis_prob = {nr:np.zeros(arr_shape, dtype=object) for nr in n_regions} mis_err = {nr:np.zeros(arr_shape, dtype=object) for nr in n_regions} def _mse_tradeoff_helper(ind): i, j, k, l = ind out = mse_tradeoff(n_units[i], total_dims[j], n_regions=n_regions, overlap=overlaps[k], total_pwr=total_pwrs[l], **kwargs) total, ds, aers, mp, me = out return ind, total, ds, aers, mp, me args = (n_units, total_dims, overlaps, total_pwrs) ind_iter = u.make_array_ind_iterator(arr_shape) par = jl.Parallel(n_jobs=n_jobs) out = par(jl.delayed(_mse_tradeoff_helper)(ind) for ind in ind_iter) for ind, total, ds, aers, mp, me in out: for k, d_k in ds.items(): totals[k][ind] = total[k] distorts[k][ind] = np.sum(d_k) # np.product(d_k)/np.sum(d_k) ae_rates[k][ind] = np.sum(aers[k]) mis_prob[k][ind] = mp[k] mis_err[k][ind] = me[k] return args, totals, distorts, ae_rates, mis_prob, mis_err
def _compute_assignment_distortion(n_feats, overlaps, num_regions): overlaps = np.array(overlaps) left_out = np.zeros((len(n_feats), len(overlaps))) for (i, j) in u.make_array_ind_iterator(left_out.shape): nf = n_feats[i] overlap = overlaps[j] left_out[i, j] = nf - max(_split_integer(nf + overlap, num_regions)) left_out[left_out <= 0] = 0 ws = left_out/6 # ws[ws <= 0] = 0 ws = np.expand_dims(ws, 0) ws = np.expand_dims(ws, -1) return ws
def explore_fi_tradeoff(n_units, total_dims, overlaps, total_pwrs, n_regions=(1, 2), **kwargs): arr_shape = (len(n_units), len(total_dims), len(overlaps), len(total_pwrs)) distorts = {nr:np.zeros(arr_shape) for nr in n_regions} ae_rates = {nr:np.zeros_like(distorts[nr]) for nr in n_regions} for ind in u.make_array_ind_iterator(arr_shape): i, j, k, l = ind ds, aers = fi_tradeoff(n_units[i], total_dims[j], n_regions=n_regions, overlap=overlaps[k], total_pwr=total_pwrs[l], **kwargs) for k, d_k in ds.items(): distorts[k][ind] = np.mean(d_k) ae_rates[k][ind] = np.mean(aers[k]) return distorts, ae_rates
def plot_mu_hists(models, mu_key, mu2=None, axs=None, fwid=3): pshape = list(models.values())[0].posterior[mu_key].shape[2:] if axs is None: figsize = (fwid * pshape[1], fwid * pshape[0]) f, axs = plt.subplots(*pshape, figsize=figsize) for k, m in models.items(): mus = m.posterior[mu_key] for (i, j) in u.make_array_ind_iterator(pshape): mu_plot = mus[..., i, j].to_numpy().flatten() if mu2 is not None: mu2_plot = m.posterior[mu2][..., i, j].to_numpy().flatten() mu_plot = mu_plot - mu2_plot axs[i, j].hist(mu_plot, label=k, histtype='step') gpl.add_vlines(0, axs[i, j]) axs[i, j].legend(frameon=False) return axs
def post_estimated_mse(est_arr, conf_dict, pwr=None, n_units=None, dim=None): new_arr = np.zeros(est_arr.shape + est_arr[0, 0, 0, 0].shape) mse_fi_arr = np.zeros(est_arr.shape) for ind in u.make_array_ind_iterator(est_arr.shape): new_arr[ind] = est_arr[ind] mse_fi_arr[ind] = 1/conf_dict[ind[:2]][0][0, 0] if pwr is not None and n_units is not None: pwr_i = pwr[ind[0]] n_i = n_units[ind[1]] w_use = conf_dict[ind[:2]][1] resp_use = conf_dict[ind[:2]][2] prob, mag = rfm.compute_threshold_err_prob(pwr_i, n_i, dim, w_use, resp_scale=resp_use) mse_fi_arr[ind] = (1 - prob)*mse_fi_arr[ind] + prob*mag return new_arr, mse_fi_arr
def compute_transition(total_dims, total_pwrs, total_units, nrs=(1, 2, 3), overlap=1): l_mse = np.zeros((len(nrs), len(total_dims), len(total_pwrs), len(total_units))) nl_mse = np.zeros_like(l_mse) tot_noae = np.zeros_like(l_mse) ae_rate = np.zeros_like(l_mse) tot_nothr = np.zeros_like(l_mse) for (nr_i, td_i, tp_i, tu_i) in u.make_array_ind_iterator(l_mse.shape): nr, td, tp, tu = (nrs[nr_i], total_dims[td_i], total_pwrs[tp_i], total_units[tu_i]) out = mse_tradeoff(tu, td, n_regions=(nr,), overlap=overlap, total_pwr=tp) tot_te, ld, ae_prob, t_prob, t_mag = out l_mse[nr_i, td_i, tp_i, tu_i] = np.sum(ld[nr]) nl_mse[nr_i, td_i, tp_i, tu_i] = np.sum(t_prob[nr]*t_mag[nr]) tot = np.sum(ld[nr]*(1 - t_prob[nr]) + t_prob[nr]*t_mag[nr]) tot_noae[nr_i, td_i, tp_i, tu_i] = tot ae_rate[nr_i, td_i, tp_i, tu_i] = np.sum(ae_prob[nr]) tot_nothr[nr_i, td_i, tp_i, tu_i] = tot_te[nr] return tot_nothr, tot_noae, l_mse, nl_mse, ae_rate
def explore_fi_tradeoff_parallel(n_units, total_dims, overlaps, total_pwrs, n_regions=(1, 2), n_jobs=-1, **kwargs): arr_shape = (len(n_units), len(total_dims), len(overlaps), len(total_pwrs)) distorts = {nr:np.zeros(arr_shape) for nr in n_regions} ae_rates = {nr:np.zeros_like(distorts[nr]) for nr in n_regions} def _fi_tradeoff_helper(ind): i, j, k, l = ind ds, aers = fi_tradeoff(n_units[i], total_dims[j], n_regions=n_regions, overlap=overlaps[k], total_pwr=total_pwrs[l], **kwargs) return ind, ds, aers args = (n_units, total_dims, overlaps, total_pwrs) ind_iter = u.make_array_ind_iterator(arr_shape) par = jl.Parallel(n_jobs=n_jobs) out = par(jl.delayed(_fi_tradeoff_helper)(ind) for ind in ind_iter) for ind, ds, aers in out: for k, d_k in ds.items(): distorts[k][ind] = np.mean(d_k) # np.product(d_k)/np.sum(d_k) ae_rates[k][ind] = np.mean(aers[k]) return args, distorts, ae_rates