def test_approx_cli(args): settings = make_test_approx_parser().parse_args(args=args) from . import geometry, particles from pwkit import cgs from pwkit.astutil import D2R from pwkit.ndshow_gtk3 import cycle, view particles = particles.ParticleDistribution.load(settings.particles_path) distrib = geometry.GriddedDistribution(particles, cgs.rjup) soln = distrib.test_approx( settings.mlat * D2R, settings.mlon * D2R, settings.L, ) def patchlog(a): pos = (a > 0) mn = a[pos].min() a[~pos] = 0.01 * mn return np.log10(a) cycle( [patchlog(soln.data), patchlog(soln.mdata)], descs = ['Data', 'Model'], ) view(soln.resids, title='Residuals')
def main(): import omega as om from pwkit.ndshow_gtk3 import cycle, view ###xg, yg, grid1 = calculate_dynat(n_pseudo_particles=2048, n_steps=30000) ###xg, yg, grid1 = calculate_fixed(n_pseudo_particles=16384, delta_t=0.00001) ###xg, yg, grid1 = calculate_debug(n_pseudo_particles=4096, n_steps=10000, delta_t=0.00001) ###_, _, grid2 = calculate(delta_t=0.00005) y_bins = 40 y_edges = np.linspace(YMIN, YMAX, y_bins + 1) y_centers = 0.5 * (y_edges[1:] + y_edges[:-1]) yg = y_centers.reshape((-1, 1)) x_bins = 40 x_edges = np.linspace(XMIN, XMAX, x_bins + 1) x_centers = 0.5 * (x_edges[1:] + x_edges[:-1]) xg = x_centers.reshape((1, -1)) rho = np.sqrt(np.log(xg / x0)**2 + np.log(yg / y0)**2) / u0 exact = ((xg * y0 / (x0 * yg))**(1. / (2 * u0 * D0)) * k0(rho * np.sqrt(1 + D0**2 * u0**2) / (np.sqrt(2) * D0)) / (2 * np.pi * D0 * u0**2 * np.sqrt(xg * yg * x0 * y0))) ###grid1 *= np.percentile(exact, 95) / np.percentile(grid1, 95) ###cycle([exact[::-1], grid1[::-1]], yflip=True) view(exact[::-1], yflip=True) p = om.RectPlot() p.addXY(xg, exact[10], 'exact') ###p.addXY(xg, grid1[10], 'mine') p.show()
def view_vk(self, data, log=False, abs=False, **kwargs): from pwkit.ndshow_gtk3 import view buf = self.vk_to_rect(data) if abs: buf = np.abs(buf) if log: buf = np.log10(buf) view(buf, **kwargs)
def view_hdf5_cli(args): """XXX: huge code redundancy with "view cube". Whatever.""" import h5py ap = argparse.ArgumentParser(prog='vernon view hdf5', ) ap.add_argument('-s', dest='stretch', type=str, nargs=1, default=['default'], choices='default sqrt neg'.split(), help='What kind of stretch to use on the data.') ap.add_argument( '-p', dest='outer_plane_number', metavar='P', type=int, help='Isolate the outermost P\'th plane of the cube before viewing.') ap.add_argument('-T', dest='transpose', action='store_true', help='Transpose the array before viewing.') ap.add_argument( '-f', dest='y_flip', action='store_true', help='Render the cube so that the first row is at the bottom.') ap.add_argument('FILE', metavar='HDF5-PATH', help='The HDF5 file to load') ap.add_argument('ITEMS', nargs='+', metavar='ITEM-NAMES', help='The name of the item within the file to view') settings = ap.parse_args(args=args) stretch_spec = settings.stretch[0] if stretch_spec == 'default': stretch = lambda data: data elif stretch_spec == 'sqrt': def stretch(data): neg = (data < 0) data[neg] *= -1 data = np.sqrt(data) data[neg] *= -1 return data elif stretch_spec == 'neg': stretch = lambda data: (data < 0).astype(np.int) else: cli.die('unknown stretch specification %r', stretch_spec) if settings.y_flip: y_slice = slice(None, None, -1) else: y_slice = slice(None, None) def describe(a): print('Final shape:', repr(a.shape)) print('Min/max/med: %.16e %.16e %.16e' % (np.nanmin(a), np.nanmax(a), np.nanmedian(a))) print('# positive / # negative / # nonfinite: %d %d %d' % ((a > 0).sum(), (a < 0).sum(), (~np.isfinite(a)).sum())) return a # convenience arrays = [] with h5py.File(settings.FILE, 'r') as ds: for item in settings.ITEMS: a = ds[item][...] if settings.outer_plane_number is not None: a = a[settings.outer_plane_number] arrays.append(a) if len(arrays) > 2: a = np.stack(arrays) else: a = arrays[0] if settings.transpose: a = a.T if a.ndim == 2: stretched = stretch(describe(a)) view(stretched[y_slice], yflip=settings.y_flip) elif a.ndim == 3: stretched = stretch(describe(a)) cycle(stretched[:, y_slice], yflip=settings.y_flip) elif a.ndim == 4: print('Shape:', a.shape) for i in range(a.shape[0]): stretched = stretch(describe(a[i])) cycle(stretched[:, y_slice], yflip=settings.y_flip) else: cli.die('cannot handle %d-dimensional arrays', a.ndim)