def plot_sepp_bg_surface(sepp_obj, domain=None, **kwargs): k = sepp_obj.bg_kde # if x_range is None: # x_range = [min(sepp_obj.data.toarray(1)), max(sepp_obj.data.toarray(1))] # y_range = [min(sepp_obj.data.toarray(2)), max(sepp_obj.data.toarray(2))] # loc = DataArray.from_meshgrid(*np.meshgrid( # np.linspace(x_range[0], x_range[1], npt), # np.linspace(y_range[0], y_range[1], npt), # )) # z = k.partial_marginal_pdf(loc) # if ax is None: # fig = plt.figure() # ax = fig.add_subplot(111) # ax.contourf(loc.toarray(0), loc.toarray(1), z, 50) # ax.set_aspect('equal') # if domain is not None: func = lambda x, y: k.partial_marginal_pdf(DataArray.from_meshgrid(x, y)) if domain is None: xmin, xmax = min(sepp_obj.data.toarray(1)), max( sepp_obj.data.toarray(1)) ymin, ymax = min(sepp_obj.data.toarray(2)), max( sepp_obj.data.toarray(2)) domain = shapely_rectangle_from_vertices(xmin, ymin, xmax, ymax) plot_surface_function_on_polygon(domain, func=func, **kwargs) plt.axis('equal')
def test_pdf_values(self): for i in range(self.min_nd, 4): x = DataArray.from_meshgrid( *np.meshgrid( *[np.linspace(-5, 5, 50)] * i ) ) y = self.kernels[i].pdf(x) ye = self.expected_pdf(x) self.assertTrue(np.all(np.abs(y - ye) < self.tol))
def test_cutoff(self): x = DataArray(np.linspace(-5, 5, 100)) self.assertTrue(np.all(self.kernels[1].pdf(x)[x.toarray(0) < 0] == 0)) x = DataArray.from_meshgrid( *np.meshgrid( np.linspace(-5, 5, 50), np.linspace(-5, 5, 50) ) ) res = self.kernels[2].pdf(x) self.assertTrue(np.all(res[x.toarray(0) < 0] == 0)) self.assertTrue(np.all(res[x.toarray(0) >= 0] > 0.))
def plot_xy_kde(k, x_range, y_range, npt_1d=50, **kwargs): x, y = np.meshgrid(np.linspace(x_range[0], x_range[1], npt_1d), np.linspace(y_range[0], y_range[1], npt_1d)) loc = DataArray.from_meshgrid(x, y) z = k.pdf(loc, normed=False) fig = plt.figure() ax = fig.add_subplot(111) n_contours = kwargs.pop('n_contours', 40) cax = ax.contourf(x, y, z, n_contours, cmap='binary') if 'clim' in kwargs: clim = kwargs.pop('clim') cax.set_clim(clim) ax.set_xlabel('X (m)') ax.set_ylabel('Y (m)') if kwargs.pop('colorbar', True): fig.colorbar(cax) return fig
l_target = k.pdf(res_all.getrows(this_idx_target), normed=False) l_sources.append(l_source) l_targets.append(l_target) kst[i, j] = n / (nv * S * T) * np.sum(1 / (omega * l_source * l_target)) # plots plt.figure() plt.contourf(uu, vv, kst - np.pi * uu**2 * vv, 50) plt.colorbar() xy = DataArray.from_meshgrid(*np.meshgrid( np.linspace(res_all.toarray(1).min(), res_all.toarray(1).max(), 50), np.linspace(res_all.toarray(2).min(), res_all.toarray(2).max(), 50))) zz = k.partial_marginal_pdf(xy, normed=False) plt.figure() plt.contourf(xy.toarray(0), xy.toarray(1), zz, 50) plt.colorbar() t = np.linspace(res_all.toarray(0).min(), res_all.toarray(0).max(), 500) plt.figure() plt.plot(t, k.marginal_pdf(t, dim=0, normed=False)) # simulation ONLY if b_sim: true_int = lambda xyz: 250 / ( (1 - np.exp(-1)) *
def weighted_bg_rel_range(x, y): xy = DataArray.from_meshgrid(x, y) fxy = np.array( [a.partial_marginal_pdf(xy) for a in res['weighted_backgrounds']]) fxy_mean = fxy.mean(axis=0) return (fxy.ptp(axis=0) / fxy_mean).reshape(xy.original_shape)
def weighted_bg_mean_density(x, y): xy = DataArray.from_meshgrid(x, y) fxy = np.array( [a.partial_marginal_pdf(xy) for a in res['weighted_backgrounds']]) return fxy.mean(axis=0).reshape(xy.original_shape)
def radial_spatial_triggering_plots(ppobj, simobj=None, xmax=None, ymax=None, cbar=True, fmax=None, vmax=None): npt = 500 ci = 0.99 fig_kwargs = { 'figsize': (10, 5), 'dpi': 100, 'facecolor': 'w', } assert fmax is None or vmax is None, "Can specify EITHER vmax OR fmax" xmax = xmax or ppobj.trigger_kde.marginal_icdf(ci, dim=1) ymax = ymax or xmax xy = DataArray.from_meshgrid(*np.meshgrid(np.linspace(-xmax, xmax, npt), np.linspace(-ymax, ymax, npt))) zxy1 = ppobj.trigger_kde.partial_marginal_pdf(xy, normed=False) / ppobj.ndata # zxy1 = ppobj.trigger_kde.partial_marginal_pdf(xy, normed=True) if fmax is not None: vmax = sorted(zxy1.flat)[int(zxy1.size * fmax)] elif vmax is not None: pass else: vmax = zxy1.max() if simobj: sx = simobj.trigger_params['sigma'][0] sy = simobj.trigger_params['sigma'][1] th = simobj.trigger_params['intensity'] # zxy2 = th / (np.sqrt(2 * np.pi) * sx * sy) * np.exp( # -(xy.toarray(0) ** 2) / (2 * sx**2) - xy.toarray(1) ** 2 / (2 * sy ** 2) # ) zxy2 = 1 / (2 * np.pi * sx * sy) * np.exp(-(xy.toarray(0)**2) / (2 * sx**2) - xy.toarray(1)**2 / (2 * sy**2)) vmax = max(zxy2.max(), vmax) vmax *= 1.02 fig = plt.figure(**fig_kwargs) if simobj: ax1 = fig.add_subplot(121) else: ax1 = fig.add_subplot(111) cont1 = ax1.contourf(xy.toarray(0), xy.toarray(1), zxy1, 50, cmap=cm.coolwarm, vmin=0, vmax=vmax) ax1.set_xlim([-xmax, xmax]) ax1.set_ylim([-ymax, ymax]) ax1.set_xlabel('X (metres)') ax1.set_ylabel('Y (metres)') ax1.set_aspect('equal') # cax1 = plt.colorbar(cont, ax=ax1) hbar = None if simobj: ax2 = fig.add_subplot(122) cont2 = ax2.contourf(xy.toarray(0), xy.toarray(1), zxy2, 50, cmap=cm.coolwarm, vmin=0, vmax=vmax) ax2.yaxis.set_ticklabels([]) ax2.set_xlabel('X (metres)') ax2.set_xlim([-xmax, xmax]) ax2.set_ylim([-ymax, ymax]) ax2.set_aspect('equal') if cbar: hbar = fig.colorbar(cont2, ax=[ax1, ax2]) else: if cbar: hbar = fig.colorbar(cont1, ax=ax1) if cbar: return hbar