def test_white_noise_filter(): ds = fake_amr_ds(fields=("density",)) p = ds.proj("density", "z") frb = p.to_frb((1, 'unitary'), 64) frb.apply_white_noise() frb.apply_white_noise(1e-3) frb["density"]
def test_contour_callback(): with _cleanup_fname() as prefix: ds = fake_amr_ds(fields = ("density", "temperature")) for ax in 'xyz': p = ProjectionPlot(ds, ax, "density") p.annotate_contour("temperature") yield assert_fname, p.save(prefix)[0] p = ProjectionPlot(ds, ax, "density", weight_field="density") p.annotate_contour("temperature") yield assert_fname, p.save(prefix)[0] p = SlicePlot(ds, ax, "density") p.annotate_contour("temperature") # BREAKS WITH ndarray yield assert_fname, p.save(prefix)[0] # Now we'll check a few additional minor things p = SlicePlot(ds, "x", "density") p.annotate_contour("temperature", ncont=10, factor=8, take_log=False, clim=(0.4, 0.6), plot_args={'lw':2.0}, label=True, text_args={'text-size':'x-large'}) p.save(prefix) p = SlicePlot(ds, "x", "density") s2 = ds.slice(0, 0.2) p.annotate_contour("temperature", ncont=10, factor=8, take_log=False, clim=(0.4, 0.6), plot_args={'lw':2.0}, label=True, text_args={'text-size':'x-large'}, data_source=s2) p.save(prefix)
def test_line_integral_convolution_callback(): with _cleanup_fname() as prefix: ds = fake_amr_ds(fields=("density", "velocity_x", "velocity_y", "velocity_z")) for ax in 'xyz': p = ProjectionPlot(ds, ax, "density") p.annotate_line_integral_convolution("velocity_x", "velocity_y") assert_fname(p.save(prefix)[0]) p = ProjectionPlot(ds, ax, "density", weight_field="density") p.annotate_line_integral_convolution("velocity_x", "velocity_y") assert_fname(p.save(prefix)[0]) p = SlicePlot(ds, ax, "density") p.annotate_line_integral_convolution("velocity_x", "velocity_y") assert_fname(p.save(prefix)[0]) # Now we'll check a few additional minor things p = SlicePlot(ds, "x", "density") p.annotate_line_integral_convolution("velocity_x", "velocity_y", kernellen=100., lim=(0.4, 0.7), cmap=ytcfg.get( "yt", "default_colormap"), alpha=0.9, const_alpha=True) p.save(prefix) with _cleanup_fname() as prefix: ds = load(cyl_2d) slc = SlicePlot(ds, "theta", "density") slc.annotate_line_integral_convolution("magnetic_field_r", "magnetic_field_z") assert_fname(slc.save(prefix)[0]) with _cleanup_fname() as prefix: ds = fake_amr_ds(fields=("density", "velocity_r", "velocity_theta", "velocity_phi"), geometry="spherical") p = SlicePlot(ds, "r", "density") p.annotate_line_integral_convolution("velocity_theta", "velocity_phi") assert_raises(YTDataTypeUnsupported, p.save, prefix)
def test_marker_callback(): with _cleanup_fname() as prefix: ax = 'z' vector = [1.0, 1.0, 1.0] ds = fake_amr_ds(fields=("density", )) p = ProjectionPlot(ds, ax, "density") p.annotate_marker([0.5, 0.5, 0.5]) assert_fname(p.save(prefix)[0]) p = SlicePlot(ds, ax, "density") p.annotate_marker([0.5, 0.5, 0.5]) assert_fname(p.save(prefix)[0]) p = OffAxisSlicePlot(ds, vector, "density") p.annotate_marker([0.5, 0.5, 0.5]) assert_fname(p.save(prefix)[0]) # Now we'll check a few additional minor things p = SlicePlot(ds, "x", "density") coord = ds.arr([0.75, 0.75, 0.75], 'unitary') coord.convert_to_units('kpc') p.annotate_marker(coord, coord_system='data') p.annotate_marker([0.5, 0.5], coord_system='axis', marker='*') p.annotate_marker([[0.5, 0.6], [0.5, 0.6], [0.5, 0.6]], coord_system='data') p.annotate_marker([[0.5, 0.6, 0.8], [0.5, 0.6, 0.8], [0.5, 0.6, 0.8]], coord_system='data') p.annotate_marker([[0.5, 0.6, 0.8], [0.5, 0.6, 0.8]], coord_system='axis') p.annotate_marker([[0.5, 0.6, 0.8], [0.5, 0.6, 0.8]], coord_system='figure') p.annotate_marker([[0.5, 0.6, 0.8], [0.5, 0.6, 0.8]], coord_system='plot') p.save(prefix) with _cleanup_fname() as prefix: ds = fake_amr_ds(fields=("density", ), geometry="spherical") p = ProjectionPlot(ds, "r", "density") p.annotate_marker([0.5, 0.5, 0.5]) assert_raises(YTDataTypeUnsupported, p.save, prefix) p = ProjectionPlot(ds, "r", "density") p.annotate_marker([0.5, 0.5], coord_system="axis") assert_fname(p.save(prefix)[0])
def test_contour_callback(): with _cleanup_fname() as prefix: ds = fake_amr_ds(fields = ("density", "temperature")) for ax in 'xyz': p = ProjectionPlot(ds, ax, "density") p.annotate_contour("temperature") yield assert_fname, p.save(prefix)[0] p = ProjectionPlot(ds, ax, "density", weight_field="density") p.annotate_contour("temperature") yield assert_fname, p.save(prefix)[0] p = SlicePlot(ds, ax, "density") p.annotate_contour("temperature") # BREAKS WITH ndarray yield assert_fname, p.save(prefix)[0] # Now we'll check a few additional minor things p = SlicePlot(ds, "x", "density") p.annotate_contour("temperature", ncont=10, factor=8, take_log=False, clim=(0.4, 0.6), plot_args={'lw':2.0}, label=True, text_args={'text-size':'x-large'}) p.save(prefix) p = SlicePlot(ds, "x", "density") s2 = ds.slice(0, 0.2) p.annotate_contour("temperature", ncont=10, factor=8, take_log=False, clim=(0.4, 0.6), plot_args={'lw':2.0}, label=True, text_args={'text-size':'x-large'}, data_source=s2) p.save(prefix) with _cleanup_fname() as prefix: ds = fake_amr_ds(fields = ("density", "temperature"), geometry="spherical") p = SlicePlot(ds, "r", "density") p.annotate_contour("temperature", ncont=10, factor=8, take_log=False, clim=(0.4, 0.6), plot_args={'lw':2.0}, label=True, text_args={'text-size':'x-large'}) yield assert_raises, YTDataTypeUnsupported, p.save, prefix
def test_magnetic_callback(): with _cleanup_fname() as prefix: ds = fake_amr_ds(fields=("density", "magnetic_field_x", "magnetic_field_y", "magnetic_field_z")) for ax in 'xyz': p = ProjectionPlot(ds, ax, "density", weight_field="density") p.annotate_magnetic_field() assert_fname(p.save(prefix)[0]) p = SlicePlot(ds, ax, "density") p.annotate_magnetic_field() assert_fname(p.save(prefix)[0]) # Test for OffAxis Slice p = SlicePlot(ds, [1, 1, 0], 'density', north_vector=[0, 0, 1]) p.annotate_magnetic_field(factor=40, normalize=True) assert_fname(p.save(prefix)[0]) # Now we'll check a few additional minor things p = SlicePlot(ds, "x", "density") p.annotate_magnetic_field(factor=8, scale=0.5, scale_units="inches", normalize=True) assert_fname(p.save(prefix)[0]) with _cleanup_fname() as prefix: ds = load(cyl_2d) slc = ProjectionPlot(ds, "theta", "density") slc.annotate_magnetic_field() assert_fname(slc.save(prefix)[0]) with _cleanup_fname() as prefix: ds = fake_amr_ds(fields=("density", "magnetic_field_r", "magnetic_field_theta", "magnetic_field_phi"), geometry="spherical") p = ProjectionPlot(ds, "r", "density") p.annotate_magnetic_field(factor=8, scale=0.5, scale_units="inches", normalize=True) assert_raises(YTDataTypeUnsupported, p.save, prefix)
def test_scale_callback(): with _cleanup_fname() as prefix: ax = 'z' vector = [1.0, 1.0, 1.0] ds = fake_amr_ds(fields=("density", )) p = ProjectionPlot(ds, ax, "density") p.annotate_scale() assert_fname(p.save(prefix)[0]) p = ProjectionPlot(ds, ax, "density", width=(0.5, 1.0)) p.annotate_scale() assert_fname(p.save(prefix)[0]) p = ProjectionPlot(ds, ax, "density", width=(1.0, 1.5)) p.annotate_scale() assert_fname(p.save(prefix)[0]) p = SlicePlot(ds, ax, "density") p.annotate_scale() assert_fname(p.save(prefix)[0]) p = OffAxisSlicePlot(ds, vector, "density") p.annotate_scale() assert_fname(p.save(prefix)[0]) # Now we'll check a few additional minor things p = SlicePlot(ds, "x", "density") p.annotate_scale(corner='upper_right', coeff=10., unit='kpc') assert_fname(p.save(prefix)[0]) p = SlicePlot(ds, "x", "density") p.annotate_scale(text_args={"size": 24}) assert_fname(p.save(prefix)[0]) p = SlicePlot(ds, "x", "density") p.annotate_scale(text_args={"font": 24}) assert_raises(YTPlotCallbackError) with _cleanup_fname() as prefix: ds = fake_amr_ds(fields=("density", ), geometry="spherical") p = ProjectionPlot(ds, "r", "density") p.annotate_scale() assert_raises(YTDataTypeUnsupported, p.save, prefix) p = ProjectionPlot(ds, "r", "density") p.annotate_scale(coord_system="axis") assert_raises(YTDataTypeUnsupported, p.save, prefix)
def test_particles_callback(): with _cleanup_fname() as prefix: ax = 'z' ds = fake_amr_ds(fields=("density",), particles=1) p = ProjectionPlot(ds, ax, "density") p.annotate_particles((10, "Mpc")) assert_fname(p.save(prefix)[0]) p = SlicePlot(ds, ax, "density") p.annotate_particles((10, "Mpc")) assert_fname(p.save(prefix)[0]) # Now we'll check a few additional minor things p = SlicePlot(ds, "x", "density") p.annotate_particles((10, "Mpc"), p_size=1.0, col="k", marker="o", stride=1, ptype="all", minimum_mass=None, alpha=1.0) p.save(prefix) with _cleanup_fname() as prefix: ds = fake_amr_ds(fields=("density",), geometry="spherical") p = ProjectionPlot(ds, "r", "density") p.annotate_particles((10, "Mpc")) assert_raises(YTDataTypeUnsupported, p.save, prefix)
def test_geo_slices_amr(): ds = fake_amr_ds(geometry="geographic") for transform in transform_list: if transform == 'UTM': # requires additional argument so we skip continue if transform == 'OSNI': # avoid crashes, see https://github.com/SciTools/cartopy/issues/1177 continue for field in ds.field_list: prefix = "%s_%s_%s" % (field[0], field[1], transform) yield compare(ds, field, 'altitude', test_prefix=prefix, test_name="geo_slices_amr", projection=transform)
def test_min_max(): for nprocs in [-1, 1, 2, 16]: fields = ["density", "temperature"] units = ["g/cm**3", "K"] if nprocs == -1: ds = fake_amr_ds(fields=fields, units=units, particles=20) else: ds = fake_random_ds(32, nprocs=nprocs, fields=fields, units=units, particles=20) ad = ds.all_data() q = ad.min("density").v assert_equal(q, ad["density"].min()) q = ad.max("density").v assert_equal(q, ad["density"].max()) q = ad.min("particle_mass").v assert_equal(q, ad["particle_mass"].min()) q = ad.max("particle_mass").v assert_equal(q, ad["particle_mass"].max()) ptp = ad.ptp("density").v assert_equal(ptp, ad["density"].max() - ad["density"].min()) ptp = ad.ptp("particle_mass").v assert_equal(ptp, ad["particle_mass"].max() - ad["particle_mass"].min()) p = ad.max("density", axis=1) p1 = ds.proj("density", 1, data_source=ad, method="mip") assert_equal(p["density"], p1["density"]) p = ad.max("density", axis="y") p1 = ds.proj("density", 1, data_source=ad, method="mip") assert_equal(p["density"], p1["density"]) # Test that we can get multiple in a single pass qrho, qtemp = ad.max(["density", "temperature"]) assert_equal(qrho, ad["density"].max()) assert_equal(qtemp, ad["temperature"].max()) qrho, qtemp = ad.min(["density", "temperature"]) assert_equal(qrho, ad["density"].min()) assert_equal(qtemp, ad["temperature"].min())
def test_marker_callback(): with _cleanup_fname() as prefix: ax = "z" vector = [1.0, 1.0, 1.0] ds = fake_amr_ds(fields=("density",), units=("g/cm**3",)) p = ProjectionPlot(ds, ax, ("gas", "density")) p.annotate_marker([0.5, 0.5, 0.5]) assert_fname(p.save(prefix)[0]) p = SlicePlot(ds, ax, ("gas", "density")) p.annotate_marker([0.5, 0.5, 0.5]) assert_fname(p.save(prefix)[0]) p = OffAxisSlicePlot(ds, vector, ("gas", "density")) p.annotate_marker([0.5, 0.5, 0.5]) assert_fname(p.save(prefix)[0]) # Now we'll check a few additional minor things p = SlicePlot(ds, "x", ("gas", "density")) coord = ds.arr([0.75, 0.75, 0.75], "unitary") coord.convert_to_units("kpc") p.annotate_marker(coord, coord_system="data") p.annotate_marker([0.5, 0.5], coord_system="axis", marker="*") p.annotate_marker([[0.5, 0.6], [0.5, 0.6], [0.5, 0.6]], coord_system="data") p.annotate_marker( [[0.5, 0.6, 0.8], [0.5, 0.6, 0.8], [0.5, 0.6, 0.8]], coord_system="data" ) p.annotate_marker([[0.5, 0.6, 0.8], [0.5, 0.6, 0.8]], coord_system="axis") p.annotate_marker([[0.5, 0.6, 0.8], [0.5, 0.6, 0.8]], coord_system="figure") p.annotate_marker([[0.5, 0.6, 0.8], [0.5, 0.6, 0.8]], coord_system="plot") p.save(prefix) check_axis_manipulation(p, prefix) with _cleanup_fname() as prefix: ds = fake_amr_ds(fields=("density",), units=("g/cm**3",), geometry="spherical") p = ProjectionPlot(ds, "r", ("gas", "density")) p.annotate_marker([0.5, 0.5, 0.5]) assert_raises(YTDataTypeUnsupported, p.save, prefix) p = ProjectionPlot(ds, "r", ("gas", "density")) p.annotate_marker([0.5, 0.5], coord_system="axis") assert_fname(p.save(prefix)[0])
def test_quiver_callback(): with _cleanup_fname() as prefix: ds = fake_amr_ds(fields = ("density", "velocity_x", "velocity_y", "velocity_z")) for ax in 'xyz': p = ProjectionPlot(ds, ax, "density") p.annotate_quiver("velocity_x", "velocity_y") assert_fname(p.save(prefix)[0]) p = ProjectionPlot(ds, ax, "density", weight_field="density") p.annotate_quiver("velocity_x", "velocity_y") assert_fname(p.save(prefix)[0]) p = SlicePlot(ds, ax, "density") p.annotate_quiver("velocity_x", "velocity_y") assert_fname(p.save(prefix)[0]) # Now we'll check a few additional minor things p = SlicePlot(ds, "x", "density") p.annotate_quiver("velocity_x", "velocity_y", factor=8, scale=0.5, scale_units="inches", normalize = True, bv_x = 0.5 * u.cm / u.s, bv_y = 0.5 * u.cm / u.s) assert_fname(p.save(prefix)[0]) with _cleanup_fname() as prefix: ds = load(cyl_2d) slc = SlicePlot(ds, "theta", "density") slc.annotate_quiver("velocity_x", "velocity_y") assert_fname(slc.save(prefix)[0]) with _cleanup_fname() as prefix: ds = fake_amr_ds(fields = ("density", "velocity_x", "velocity_theta", "velocity_phi"), geometry="spherical") p = ProjectionPlot(ds, "r", "density") p.annotate_quiver("velocity_theta", "velocity_phi", factor=8, scale=0.5, scale_units="inches", normalize = True, bv_x = 0.5 * u.cm / u.s, bv_y = 0.5 * u.cm / u.s) assert_raises(YTDataTypeUnsupported, p.save, prefix)
def test_geo_slices_amr(): ds = fake_amr_ds(geometry="geographic") for transform in transform_list: if transform == 'UTM': # requires additional argument so we skip continue for field in ds.field_list: prefix = "%s_%s_%s" % (field[0], field[1], transform) yield compare(ds, field, 'altitude', test_prefix=prefix, test_name="geo_slices_amr", projection=transform)
def test_cell_edges_callback(): with _cleanup_fname() as prefix: ds = fake_amr_ds(fields=("density", )) for ax in 'xyz': p = ProjectionPlot(ds, ax, "density") p.annotate_cell_edges() assert_fname(p.save(prefix)[0]) p = ProjectionPlot(ds, ax, "density", weight_field="density") p.annotate_cell_edges() assert_fname(p.save(prefix)[0]) p = SlicePlot(ds, ax, "density") p.annotate_cell_edges() assert_fname(p.save(prefix)[0]) # Now we'll check a few additional minor things p = SlicePlot(ds, "x", "density") p.annotate_cell_edges(alpha=0.7, line_width=0.9, color=(0.0, 1.0, 1.0)) p.save(prefix) with _cleanup_fname() as prefix: ds = fake_amr_ds(fields=("density", ), geometry="spherical") p = SlicePlot(ds, "r", "density") p.annotate_cell_edges() assert_raises(YTDataTypeUnsupported, p.save, prefix)
def test_to_frb(self): # Test cylindrical geometry fields = ["density", "cell_mass"] ds = fake_amr_ds(fields=fields, geometry="cylindrical", particles=16**3) dd = ds.all_data() proj = ds.proj("density", weight_field="cell_mass", axis=1, data_source=dd) frb = proj.to_frb((1.0, "unitary"), 64) assert_equal(frb.radius, (1.0, "unitary")) assert_equal(frb.buff_size, 64)
def test_mean_sum_integrate(): for nprocs in [-1, 1, 2, 16]: if nprocs == -1: ds = fake_amr_ds(fields=("density",)) else: ds = fake_random_ds(32, nprocs=nprocs, fields=("density",)) ad = ds.all_data() # Sums q = ad.sum('density') q1 = ad.quantities.total_quantity('density') yield assert_equal, q, q1 # Weighted Averages w = ad.mean("density") w1 = ad.quantities.weighted_average_quantity('density', 'ones') yield assert_equal, w, w1 w = ad.mean("density", weight="density") w1 = ad.quantities.weighted_average_quantity('density', 'density') yield assert_equal, w, w1 # Projections p = ad.sum('density', axis=0) p1 = ds.proj('density', 0, data_source=ad, method="sum") yield assert_equal, p['density'], p1['density'] # Check by axis-name p = ad.sum('density', axis='x') yield assert_equal, p['density'], p1['density'] # Now we check proper projections p = ad.integrate("density", axis=0) p1 = ds.proj("density", 0, data_source=ad) yield assert_equal, p['density'], p1['density'] # Check by axis-name p = ad.integrate('density', axis='x') yield assert_equal, p['density'], p1['density']
def test_nameonly_field_with_all_aliases_candidates(): # see https://github.com/yt-project/yt/issues/3839 ds = fake_amr_ds(fields=["density"], units=["g/cm**3"]) # here we rely on implementations details from fake_amr_ds, # so we verify that it provides the appropriate conditions # for the actual test. candidates = [f for f in ds.derived_field_list if f[1] == "density"] assert len(candidates) == 2 fi = ds.field_info assert fi[candidates[0]].is_alias_to(fi[candidates[1]]) # this is the actual test (check that no error or warning is raised) ds.all_data()["density"]
def test_grids_callback(): with _cleanup_fname() as prefix: ds = fake_amr_ds(fields=("density", )) for ax in 'xyz': p = ProjectionPlot(ds, ax, "density") p.annotate_grids() assert_fname(p.save(prefix)[0]) p = ProjectionPlot(ds, ax, "density", weight_field="density") p.annotate_grids() assert_fname(p.save(prefix)[0]) p = SlicePlot(ds, ax, "density") p.annotate_grids() assert_fname(p.save(prefix)[0]) # Now we'll check a few additional minor things p = SlicePlot(ds, "x", "density") p.annotate_grids(alpha=0.7, min_pix=10, min_pix_ids=30, draw_ids=True, periodic=False, min_level=2, max_level=3, cmap="gist_stern") p.save(prefix) with _cleanup_fname() as prefix: ds = fake_amr_ds(fields=("density", ), geometry="spherical") p = SlicePlot(ds, "r", "density") p.annotate_grids(alpha=0.7, min_pix=10, min_pix_ids=30, draw_ids=True, periodic=False, min_level=2, max_level=3, cmap="gist_stern") assert_raises(YTDataTypeUnsupported, p.save, prefix)
def test_ray_callback(): with _cleanup_fname() as prefix: ax = "z" vector = [1.0, 1.0, 1.0] ds = fake_amr_ds(fields=("density", ), units=("g/cm**3", )) ray = ds.ray((0.1, 0.2, 0.3), (0.6, 0.8, 0.5)) oray = ds.ortho_ray(0, (0.3, 0.4)) p = ProjectionPlot(ds, ax, "density") p.annotate_ray(oray) p.annotate_ray(ray) assert_fname(p.save(prefix)[0]) p = SlicePlot(ds, ax, "density") p.annotate_ray(oray) p.annotate_ray(ray) assert_fname(p.save(prefix)[0]) p = OffAxisSlicePlot(ds, vector, "density") p.annotate_ray(oray) p.annotate_ray(ray) assert_fname(p.save(prefix)[0]) # Now we'll check a few additional minor things p = SlicePlot(ds, "x", "density") p.annotate_ray(oray) p.annotate_ray(ray, plot_args={"color": "red"}) p.save(prefix) with _cleanup_fname() as prefix: ds = fake_amr_ds(fields=("density", ), units=("g/cm**3", ), geometry="spherical") ray = ds.ray((0.1, 0.2, 0.3), (0.6, 0.8, 0.5)) oray = ds.ortho_ray(0, (0.3, 0.4)) p = ProjectionPlot(ds, "r", "density") p.annotate_ray(oray) assert_raises(YTDataTypeUnsupported, p.save, prefix) p = ProjectionPlot(ds, "r", "density") p.annotate_ray(ray) assert_raises(YTDataTypeUnsupported, p.save, prefix)
def test_boolean_ray_slice_no_overlap(): r"""Test to make sure that boolean objects (ray, slice, no overlap) behave the way we expect. Test non-overlapping ray and slice. This also checks that the original regions don't change as part of constructing the booleans. """ ds = fake_amr_ds() sl = ds.r[:, :, 0.25] ra = ds.ray([0] * 3, [0, 1, 0]) # Store the original indices i1 = sl["index", "morton_index"] i1.sort() i2 = ra["index", "morton_index"] i2.sort() ii = np.concatenate((i1, i2)) ii.sort() # Make some booleans bo1 = sl & ra bo2 = sl - ra bo3 = sl | ra bo4 = ds.union([sl, ra]) bo5 = ds.intersection([sl, ra]) # This makes sure the original containers didn't change. new_i1 = sl["index", "morton_index"] new_i1.sort() new_i2 = ra["index", "morton_index"] new_i2.sort() assert_array_equal(new_i1, i1) assert_array_equal(new_i2, i2) # Now make sure the indices also behave as we expect. empty = np.array([]) assert_array_equal(bo1["index", "morton_index"], empty) assert_array_equal(bo5["index", "morton_index"], empty) b2 = bo2["index", "morton_index"] b2.sort() assert_array_equal(b2, i1) b3 = bo3["index", "morton_index"] b3.sort() assert_array_equal(b3, ii) b4 = bo4["index", "morton_index"] b4.sort() b5 = bo5["index", "morton_index"] b5.sort() assert_array_equal(b3, b4) bo6 = sl ^ ra b6 = bo6["index", "morton_index"] b6.sort() assert_array_equal(b6, np.setxor1d(i1, i2))
def test_boolean_ellipsoids_no_overlap(): r"""Test to make sure that boolean objects (ellipsoids, no overlap) behave the way we expect. Test non-overlapping ellipsoids. This also checks that the original ellipsoids don't change as part of constructing the booleans. """ ds = fake_amr_ds() ell1 = ds.ellipsoid([0.25] * 3, 0.05, 0.05, 0.05, np.array([0.1] * 3), 0.1) ell2 = ds.ellipsoid([0.75] * 3, 0.05, 0.05, 0.05, np.array([0.1] * 3), 0.1) # Store the original indices i1 = ell1["index", "morton_index"] i1.sort() i2 = ell2["index", "morton_index"] i2.sort() ii = np.concatenate((i1, i2)) ii.sort() # Make some booleans bo1 = ell1 & ell2 bo2 = ell1 - ell2 bo3 = ell1 | ell2 bo4 = ds.union([ell1, ell2]) bo5 = ds.intersection([ell1, ell2]) # This makes sure the original containers didn't change. new_i1 = ell1["index", "morton_index"] new_i1.sort() new_i2 = ell2["index", "morton_index"] new_i2.sort() assert_array_equal(new_i1, i1) assert_array_equal(new_i2, i2) # Now make sure the indices also behave as we expect. empty = np.array([]) assert_array_equal(bo1["index", "morton_index"], empty) assert_array_equal(bo5["index", "morton_index"], empty) b2 = bo2["index", "morton_index"] b2.sort() assert_array_equal(b2, i1) b3 = bo3["index", "morton_index"] b3.sort() assert_array_equal(b3, ii) b4 = bo4["index", "morton_index"] b4.sort() b5 = bo5["index", "morton_index"] b5.sort() assert_array_equal(b3, b4) bo6 = ell1 ^ ell2 b6 = bo6["index", "morton_index"] b6.sort() assert_array_equal(b6, np.setxor1d(i1, i2))
def test_boolean_regions_no_overlap(): r"""Test to make sure that boolean objects (regions, no overlap) behave the way we expect. Test non-overlapping regions. This also checks that the original regions don't change as part of constructing the booleans. """ ds = fake_amr_ds() re1 = ds.region([0.25] * 3, [0.2] * 3, [0.3] * 3) re2 = ds.region([0.65] * 3, [0.6] * 3, [0.7] * 3) # Store the original indices i1 = re1["index", "morton_index"] i1.sort() i2 = re2["index", "morton_index"] i2.sort() ii = np.concatenate((i1, i2)) ii.sort() # Make some booleans bo1 = re1 & re2 bo2 = re1 - re2 bo3 = re1 | re2 bo4 = ds.union([re1, re2]) bo5 = ds.intersection([re1, re2]) # This makes sure the original containers didn't change. new_i1 = re1["index", "morton_index"] new_i1.sort() new_i2 = re2["index", "morton_index"] new_i2.sort() assert_array_equal(new_i1, i1) assert_array_equal(new_i2, i2) # Now make sure the indices also behave as we expect. empty = np.array([]) assert_array_equal(bo1["index", "morton_index"], empty) assert_array_equal(bo5["index", "morton_index"], empty) b2 = bo2["index", "morton_index"] b2.sort() assert_array_equal(b2, i1) b3 = bo3["index", "morton_index"] b3.sort() assert_array_equal(b3, ii) b4 = bo4["index", "morton_index"] b4.sort() b5 = bo5["index", "morton_index"] b5.sort() assert_array_equal(b3, b4) bo6 = re1 ^ re2 b6 = bo6["index", "morton_index"] b6.sort() assert_array_equal(b6, np.setxor1d(i1, i2))
def test_add_ion_mass_fields_to_amr_ds(): """ Test to add various ion fields """ ds = fake_amr_ds(fields=("density", "velocity_x", "velocity_y", "velocity_z", "temperature", "metallicity")) ad = ds.all_data() add_ion_mass_field('O', 6, ds) field = ('gas', 'O_p5_mass') assert field in ds.derived_field_list assert isinstance(ad[field], np.ndarray) dirpath = tempfile.mkdtemp() SlicePlot(ds, 'x', field).save(dirpath) shutil.rmtree(dirpath)
def test_noise_plots(): ds = fake_amr_ds(geometry="spherical") add_noise_fields(ds) def create_image(filename_prefix): fields = ["noise%d" % i for i in range(4)] for normal in ("phi", "theta"): p = SlicePlot(ds, normal, fields) p.save(f"{filename_prefix}_{normal}") test = GenericImageTest(ds, create_image, 12) test.prefix = "test_noise_plot_lin" test_noise_plots.__name__ = test.description yield test
def test_method_signature(): ds = fake_amr_ds( fields=[("gas", "density"), ("gas", "velocity_x"), ("gas", "velocity_y")], units=["g/cm**3", "m/s", "m/s"], ) p = SlicePlot(ds, "z", ("gas", "density")) sig = inspect.signature(p.annotate_velocity) # checking the first few arguments rather than the whole signature # we just want to validate that method wrapping works assert list(sig.parameters.keys())[:4] == [ "factor", "scale", "scale_units", "normalize", ]
def test_preserve_geometric_properties(): for geom in ("cartesian", "cylindrical", "spherical"): ds1 = fake_amr_ds(fields=[("gas", "density")], geometry=geom) ad = ds1.all_data() with TemporaryDirectory() as tmpdir: tmpf = os.path.join(tmpdir, "savefile.h5") fn = ad.save_as_dataset(tmpf, fields=["density"]) ds2 = load(fn) assert isinstance(ds2, YTDataContainerDataset) dfl = ds2.derived_field_list assert ds1.geometry == ds2.geometry == geom expected = set(ds1.coordinates.axis_order) actual = {fname for ftype, fname in dfl} assert expected.difference(actual) == set()
def test_boolean_rays_no_overlap(): r"""Test to make sure that boolean objects (rays, no overlap) behave the way we expect. Test non-overlapping rays. """ ds = fake_amr_ds() ra1 = ds.ray([0, 0, 0], [0, 0, 1]) ra2 = ds.ray([1, 0, 0], [1, 0, 1]) # Store the original indices i1 = ra1["index", "morton_index"] i1.sort() i2 = ra2["index", "morton_index"] i2.sort() ii = np.concatenate((i1, i2)) ii.sort() # Make some booleans bo1 = ra1 & ra2 bo2 = ra1 - ra2 bo3 = ra1 | ra2 bo4 = ds.union([ra1, ra2]) bo5 = ds.intersection([ra1, ra2]) # This makes sure the original containers didn't change. new_i1 = ra1["index", "morton_index"] new_i1.sort() new_i2 = ra2["index", "morton_index"] new_i2.sort() assert_array_equal(new_i1, i1) assert_array_equal(new_i2, i2) # Now make sure the indices also behave as we expect. empty = np.array([]) assert_array_equal(bo1["index", "morton_index"], empty) assert_array_equal(bo5["index", "morton_index"], empty) b2 = bo2["index", "morton_index"] b2.sort() assert_array_equal(b2, i1) b3 = bo3["index", "morton_index"] b3.sort() assert_array_equal(b3, ii) b4 = bo4["index", "morton_index"] b4.sort() b5 = bo5["index", "morton_index"] b5.sort() assert_array_equal(b3, b4) bo6 = ra1 ^ ra2 b6 = bo6["index", "morton_index"] b6.sort() assert_array_equal(b6, np.setxor1d(i1, i2))
def test_magnetic_callback(): with _cleanup_fname() as prefix: ds = fake_amr_ds(fields = ("density", "magnetic_field_x", "magnetic_field_y", "magnetic_field_z")) for ax in 'xyz': p = ProjectionPlot(ds, ax, "density", weight_field="density") p.annotate_magnetic_field() yield assert_fname, p.save(prefix)[0] p = SlicePlot(ds, ax, "density") p.annotate_magnetic_field() yield assert_fname, p.save(prefix)[0] # Now we'll check a few additional minor things p = SlicePlot(ds, "x", "density") p.annotate_magnetic_field(factor=8, scale=0.5, scale_units="inches", normalize = True) p.save(prefix)
def test_ortho_ray_from_r(): ds = fake_amr_ds(fields=["density"]) ray1 = ds.r[:, 0.3, 0.2] ray2 = ds.ortho_ray("x", [0.3, 0.2]) assert_equal(ray1["density"], ray2["density"]) # the y-coord is funny so test it too ray3 = ds.r[0.3, :, 0.2] ray4 = ds.ortho_ray("y", [0.2, 0.3]) assert_equal(ray3["density"], ray4["density"]) # Test ray which doesn't cover the whole domain box = ds.box([0.25, 0.0, 0.0], [0.75, 1.0, 1.0]) ray5 = ds.r[0.25:0.75, 0.3, 0.2] ray6 = ds.ortho_ray("x", [0.3, 0.2], data_source=box) assert_equal(ray5["density"], ray6["density"])
def test_extract_isocontours(self): # Test isocontour properties for AMRGridData ds = fake_amr_ds(fields=["density", "cell_mass"], particles=16**3) dd = ds.all_data() q = dd.quantities["WeightedAverageQuantity"] rho = q("density", weight="cell_mass") dd.extract_isocontours("density", rho, "triangles.obj", True) dd.calculate_isocontour_flux("density", rho, "x", "y", "z", "dx") # Test error in case of ParticleData ds = fake_particle_ds() dd = ds.all_data() q = dd.quantities["WeightedAverageQuantity"] rho = q("particle_velocity_x", weight="particle_mass") with assert_raises(NotImplementedError): dd.extract_isocontours("density", rho, sample_values="x")
def test_geographic_coordinates(): # We're going to load up a simple AMR grid and check its volume # calculations and path length calculations. # Note that we are setting it up to have an altitude of 1000 maximum, which # means our volume will be that of a shell 1000 wide, starting at r of # whatever our surface_height is set to. ds = fake_amr_ds(geometry="geographic") ds.surface_height = ds.quan(5000.0, "code_length") axes = ["latitude", "longitude", "altitude"] for i, axis in enumerate(axes): dd = ds.all_data() fi = ("index", axis) fd = ("index", f"d{axis}") ma = np.argmax(dd[fi]) assert_equal(dd[fi][ma] + dd[fd][ma] / 2.0, ds.domain_right_edge[i].d) mi = np.argmin(dd[fi]) assert_equal(dd[fi][mi] - dd[fd][mi] / 2.0, ds.domain_left_edge[i].d) assert_equal(dd[fd].max(), (ds.domain_width / ds.domain_dimensions)[i].d) inner_r = ds.surface_height outer_r = ds.surface_height + ds.domain_width[2] assert_equal(dd["index", "dtheta"], dd["index", "dlatitude"] * np.pi / 180.0) assert_equal(dd["index", "dphi"], dd["index", "dlongitude"] * np.pi / 180.0) # Note our terrible agreement here. assert_rel_equal( dd["cell_volume"].sum(dtype="float64"), (4.0 / 3.0) * np.pi * (outer_r**3 - inner_r**3), 10, ) assert_equal(dd["index", "path_element_altitude"], dd["index", "daltitude"]) assert_equal(dd["index", "path_element_altitude"], dd["index", "dr"]) # Note that latitude corresponds to theta, longitude to phi assert_equal( dd["index", "path_element_latitude"], dd["index", "r"] * dd["index", "dlatitude"] * np.pi / 180.0, ) assert_equal( dd["index", "path_element_longitude"], (dd["index", "r"] * dd["index", "dlongitude"] * np.pi / 180.0 * np.sin( (dd["index", "latitude"] + 90.0) * np.pi / 180.0)), ) # We also want to check that our radius is correct assert_equal(dd["index", "r"], dd["index", "altitude"] + ds.surface_height)
def test_boolean_spheres_no_overlap(): r"""Test to make sure that boolean objects (spheres, no overlap) behave the way we expect. Test non-overlapping spheres. This also checks that the original spheres don't change as part of constructing the booleans. """ ds = fake_amr_ds() sp1 = ds.sphere([0.25, 0.25, 0.25], 0.15) sp2 = ds.sphere([0.75, 0.75, 0.75], 0.15) # Store the original indices i1 = sp1["index", "morton_index"] i1.sort() i2 = sp2["index", "morton_index"] i2.sort() ii = np.concatenate((i1, i2)) ii.sort() # Make some booleans bo1 = sp1 & sp2 bo2 = sp1 - sp2 bo3 = sp1 | sp2 # also works with + bo4 = ds.union([sp1, sp2]) bo5 = ds.intersection([sp1, sp2]) # This makes sure the original containers didn't change. new_i1 = sp1["index", "morton_index"] new_i1.sort() new_i2 = sp2["index", "morton_index"] new_i2.sort() assert_array_equal(new_i1, i1) assert_array_equal(new_i2, i2) # Now make sure the indices also behave as we expect. empty = np.array([]) assert_array_equal(bo1["index", "morton_index"], empty) assert_array_equal(bo5["index", "morton_index"], empty) b2 = bo2["index", "morton_index"] b2.sort() assert_array_equal(b2, i1) b3 = bo3["index", "morton_index"] b3.sort() assert_array_equal(b3, ii) b4 = bo4["index", "morton_index"] b4.sort() assert_array_equal(b4, ii) bo6 = sp1 ^ sp2 b6 = bo6["index", "morton_index"] b6.sort() assert_array_equal(b6, np.setxor1d(i1, i2))
def test_internal_geographic_coordinates(): # We're going to load up a simple AMR grid and check its volume # calculations and path length calculations. # Note that we are setting it up to have depth of 1000 maximum, which # means our volume will be that of a shell 1000 wide, starting at r of # outer_radius - 1000. ds = fake_amr_ds(geometry="internal_geographic") ds.outer_radius = ds.quan(5000, "code_length") axes = ["latitude", "longitude", "depth"] for i, axis in enumerate(axes): dd = ds.all_data() fi = ("index", axis) fd = ("index", "d%s" % axis) ma = np.argmax(dd[fi]) yield assert_equal, dd[fi][ ma] + dd[fd][ma] / 2.0, ds.domain_right_edge[i].d mi = np.argmin(dd[fi]) yield assert_equal, dd[fi][mi] - dd[fd][mi] / 2.0, ds.domain_left_edge[ i].d yield assert_equal, dd[fd].max(), (ds.domain_width / ds.domain_dimensions)[i].d inner_r = ds.outer_radius - ds.domain_right_edge[2] outer_r = ds.outer_radius yield assert_equal, dd["index","dtheta"], \ dd["index","dlatitude"]*np.pi/180.0 yield assert_equal, dd["index","dphi"], \ dd["index","dlongitude"]*np.pi/180.0 # Note our terrible agreement here. yield assert_rel_equal, dd["cell_volume"].sum(dtype="float64"), \ (4.0/3.0) * np.pi * (outer_r**3 - inner_r**3), \ 3 yield assert_equal, dd["index", "path_element_depth"], \ dd["index", "ddepth"] yield assert_equal, dd["index", "path_element_depth"], \ dd["index", "dr"] # Note that latitude corresponds to theta, longitude to phi yield assert_equal, dd["index", "path_element_latitude"], \ dd["index", "r"] * \ dd["index", "dlatitude"] * np.pi/180.0 yield assert_equal, dd["index", "path_element_longitude"], \ dd["index", "r"] * \ dd["index", "dlongitude"] * np.pi/180.0 * \ np.sin((dd["index", "latitude"] + 90.0) * np.pi/180.0) # We also want to check that our radius is correct yield assert_equal, dd["index","r"], \ -1.0*dd["index","depth"] + ds.outer_radius
def test_scale_callback(): with _cleanup_fname() as prefix: ax = 'z' vector = [1.0,1.0,1.0] ds = fake_amr_ds(fields = ("density",)) p = ProjectionPlot(ds, ax, "density") p.annotate_scale() yield assert_fname, p.save(prefix)[0] p = SlicePlot(ds, ax, "density") p.annotate_scale() yield assert_fname, p.save(prefix)[0] p = OffAxisSlicePlot(ds, vector, "density") p.annotate_scale() yield assert_fname, p.save(prefix)[0] # Now we'll check a few additional minor things p = SlicePlot(ds, "x", "density") p.annotate_scale(corner='upper_right', coeff=10., unit='kpc') p.save(prefix)
def test_sphere_callback(): with _cleanup_fname() as prefix: ax = 'z' vector = [1.0,1.0,1.0] ds = fake_amr_ds(fields = ("density",)) p = ProjectionPlot(ds, ax, "density") p.annotate_sphere([0.5,0.5,0.5], 0.1) yield assert_fname, p.save(prefix)[0] p = SlicePlot(ds, ax, "density") p.annotate_sphere([0.5,0.5,0.5], 0.1) yield assert_fname, p.save(prefix)[0] p = OffAxisSlicePlot(ds, vector, "density") p.annotate_sphere([0.5,0.5,0.5], 0.1) yield assert_fname, p.save(prefix)[0] # Now we'll check a few additional minor things p = SlicePlot(ds, "x", "density") p.annotate_sphere([0.5,0.5], 0.1, coord_system='axis', text='blah') p.save(prefix)
def test_text_callback(): with _cleanup_fname() as prefix: ax = 'z' vector = [1.0,1.0,1.0] ds = fake_amr_ds(fields = ("density",)) p = ProjectionPlot(ds, ax, "density") p.annotate_text([0.5,0.5,0.5], 'dinosaurs!') yield assert_fname, p.save(prefix)[0] p = SlicePlot(ds, ax, "density") p.annotate_text([0.5,0.5,0.5], 'dinosaurs!') yield assert_fname, p.save(prefix)[0] p = OffAxisSlicePlot(ds, vector, "density") p.annotate_text([0.5,0.5,0.5], 'dinosaurs!') yield assert_fname, p.save(prefix)[0] # Now we'll check a few additional minor things p = SlicePlot(ds, "x", "density") p.annotate_text([0.5,0.5], 'dinosaurs!', coord_system='axis', text_args={'color':'red'}) p.save(prefix)
def test_grids_callback(): with _cleanup_fname() as prefix: ds = fake_amr_ds(fields = ("density",)) for ax in 'xyz': p = ProjectionPlot(ds, ax, "density") p.annotate_grids() yield assert_fname, p.save(prefix)[0] p = ProjectionPlot(ds, ax, "density", weight_field="density") p.annotate_grids() yield assert_fname, p.save(prefix)[0] p = SlicePlot(ds, ax, "density") p.annotate_grids() yield assert_fname, p.save(prefix)[0] # Now we'll check a few additional minor things p = SlicePlot(ds, "x", "density") p.annotate_grids(alpha=0.7, min_pix=10, min_pix_ids=30, draw_ids=True, periodic=False, min_level=2, max_level=3, cmap="gist_stern") p.save(prefix)
def test_timestamp_callback(): with _cleanup_fname() as prefix: ax = 'z' vector = [1.0,1.0,1.0] ds = fake_amr_ds(fields = ("density",)) p = ProjectionPlot(ds, ax, "density") p.annotate_timestamp() yield assert_fname, p.save(prefix)[0] p = SlicePlot(ds, ax, "density") p.annotate_timestamp() yield assert_fname, p.save(prefix)[0] p = OffAxisSlicePlot(ds, vector, "density") p.annotate_timestamp() yield assert_fname, p.save(prefix)[0] # Now we'll check a few additional minor things p = SlicePlot(ds, "x", "density") p.annotate_timestamp(corner='lower_right', redshift=True, draw_inset_box=True) p.save(prefix)
def test_quiver_callback(): with _cleanup_fname() as prefix: ds = fake_amr_ds(fields = ("density", "velocity_x", "velocity_y", "velocity_z")) for ax in 'xyz': p = ProjectionPlot(ds, ax, "density") p.annotate_quiver("velocity_x", "velocity_y") yield assert_fname, p.save(prefix)[0] p = ProjectionPlot(ds, ax, "density", weight_field="density") p.annotate_quiver("velocity_x", "velocity_y") yield assert_fname, p.save(prefix)[0] p = SlicePlot(ds, ax, "density") p.annotate_quiver("velocity_x", "velocity_y") yield assert_fname, p.save(prefix)[0] # Now we'll check a few additional minor things p = SlicePlot(ds, "x", "density") p.annotate_quiver("velocity_x", "velocity_y", factor=8, scale=0.5, scale_units="inches", normalize = True, bv_x = 0.5 * u.cm / u.s, bv_y = 0.5 * u.cm / u.s) p.save(prefix)
def test_ray_callback(): with _cleanup_fname() as prefix: ax = 'z' vector = [1.0,1.0,1.0] ds = fake_amr_ds(fields = ("density",)) ray = ds.ray((0.1, 0.2, 0.3), (1.6, 1.8, 1.5)) oray = ds.ortho_ray(0, (0.3, 0.4)) p = ProjectionPlot(ds, ax, "density") p.annotate_ray(oray) p.annotate_ray(ray) yield assert_fname, p.save(prefix)[0] p = SlicePlot(ds, ax, "density") p.annotate_ray(oray) p.annotate_ray(ray) yield assert_fname, p.save(prefix)[0] p = OffAxisSlicePlot(ds, vector, "density") p.annotate_ray(oray) p.annotate_ray(ray) yield assert_fname, p.save(prefix)[0] # Now we'll check a few additional minor things p = SlicePlot(ds, "x", "density") p.annotate_ray(oray) p.annotate_ray(ray, plot_args={'color':'red'}) p.save(prefix)
def test_gauss_beam_filter(): ds = fake_amr_ds(fields=("density",)) p = ds.proj("density", "z") frb = p.to_frb((1, 'unitary'), 64) frb.apply_gauss_beam(nbeam=15, sigma=1.0) frb["density"]