def test_modified_unit_division(): ds1 = fake_random_ds(64) ds2 = fake_random_ds(64) # this mocks comoving coordinates without going through the trouble # of setting up a fake cosmological dataset ds1.unit_registry.modify('m', 50) a = ds1.quan(3, 'm') b = ds2.quan(3, 'm') ret = a/b yield assert_true, ret == 0.5 yield assert_true, ret.units.is_dimensionless yield assert_true, ret.units.base_value == 1.0
def test_cutting_plane_selector(): # generate fake data with a number of non-cubical grids ds = fake_random_ds(64, nprocs=51) assert all(ds.periodicity) # test cutting plane against orthogonal plane for i, d in enumerate("xyz"): norm = np.zeros(3) norm[i] = 1.0 for coord in np.arange(0, 1.0, 0.1): center = np.zeros(3) center[i] = coord data = ds.slice(i, coord) data.get_data() data2 = ds.cutting(norm, center) data2.get_data() assert data.shape[0] == data2.shape[0] cells1 = np.lexsort((data["x"], data["y"], data["z"])) cells2 = np.lexsort((data2["x"], data2["y"], data2["z"])) for d2 in "xyz": yield assert_equal, data[d2][cells1], data2[d2][cells2]
def test_slice(): fns = [] grid_eps = np.finfo(np.float64).eps for nprocs in [8, 1]: # We want to test both 1 proc and 8 procs, to make sure that # parallelism isn't broken ds = fake_random_ds(64, nprocs=nprocs) dims = ds.domain_dimensions xn, yn, zn = ds.domain_dimensions dx = ds.arr(1.0 / (ds.domain_dimensions * 2), 'code_length') xi, yi, zi = ds.domain_left_edge + dx xf, yf, zf = ds.domain_right_edge - dx coords = np.mgrid[xi:xf:xn * 1j, yi:yf:yn * 1j, zi:zf:zn * 1j] uc = [np.unique(c) for c in coords] slc_pos = 0.5 # Some simple slice tests with single grids for ax, an in enumerate("xyz"): xax = ds.coordinates.x_axis[ax] yax = ds.coordinates.y_axis[ax] for wf in ["density", None]: slc = ds.slice(ax, slc_pos) shifted_slc = ds.slice(ax, slc_pos + grid_eps) yield assert_equal, slc["ones"].sum(), slc["ones"].size yield assert_equal, slc["ones"].min(), 1.0 yield assert_equal, slc["ones"].max(), 1.0 yield assert_equal, np.unique(slc["px"]), uc[xax] yield assert_equal, np.unique(slc["py"]), uc[yax] yield assert_equal, np.unique(slc["pdx"]), 0.5 / dims[xax] yield assert_equal, np.unique(slc["pdy"]), 0.5 / dims[yax] pw = slc.to_pw(fields='density') for p in pw.plots.values(): tmpfd, tmpname = tempfile.mkstemp(suffix='.png') os.close(tmpfd) p.save(name=tmpname) fns.append(tmpname) for width in [(1.0, 'unitary'), 1.0, ds.quan(0.5, 'code_length')]: frb = slc.to_frb((1.0, 'unitary'), 64) shifted_frb = shifted_slc.to_frb((1.0, 'unitary'), 64) for slc_field in ['ones', 'density']: fi = ds._get_field_info(slc_field) yield assert_equal, frb[slc_field].info['data_source'], \ slc.__str__() yield assert_equal, frb[slc_field].info['axis'], \ ax yield assert_equal, frb[slc_field].info['field'], \ slc_field yield assert_equal, frb[slc_field].units, \ Unit(fi.units) yield assert_equal, frb[slc_field].info['xlim'], \ frb.bounds[:2] yield assert_equal, frb[slc_field].info['ylim'], \ frb.bounds[2:] yield assert_equal, frb[slc_field].info['center'], \ slc.center yield assert_equal, frb[slc_field].info['coord'], \ slc_pos yield assert_equal, frb[slc_field], \ shifted_frb[slc_field] yield assert_equal, wf, None teardown_func(fns)
def setUp(self): if self.ds is None: self.ds = fake_random_ds(64) self.slc = SlicePlot(self.ds, 0, "density") self.tmpdir = tempfile.mkdtemp() self.curdir = os.getcwd() os.chdir(self.tmpdir)
def setUpClass(cls): fields = ('density', 'temperature', 'velocity_x', 'velocity_y', 'velocity_z') units = ('g/cm**3', 'K', 'cm/s', 'cm/s', 'cm/s') test_ds = fake_random_ds(64, fields=fields, units=units) regions = [test_ds.region([0.5]*3, [0.4]*3, [0.6]*3), test_ds.all_data()] profiles = [] phases = [] pr_fields = [('density', 'temperature'), ('density', 'velocity_x'), ('temperature', 'cell_mass'), ('density', 'radius'), ('velocity_magnitude', 'cell_mass')] ph_fields = [('density', 'temperature', 'cell_mass'), ('density', 'velocity_x', 'cell_mass'), ('radius', 'temperature', 'velocity_magnitude')] for reg in regions: for x_field, y_field in pr_fields: profiles.append(ProfilePlot(reg, x_field, y_field)) profiles.append(ProfilePlot(reg, x_field, y_field, fractional=True, accumulation=True)) p1d = create_profile(reg, x_field, y_field) profiles.append(ProfilePlot.from_profiles(p1d)) for x_field, y_field, z_field in ph_fields: # set n_bins to [16, 16] since matplotlib's postscript # renderer is slow when it has to write a lot of polygons phases.append(PhasePlot(reg, x_field, y_field, z_field, x_bins=16, y_bins=16)) phases.append(PhasePlot(reg, x_field, y_field, z_field, fractional=True, accumulation=True, x_bins=16, y_bins=16)) p2d = create_profile(reg, [x_field, y_field], z_field, n_bins=[16, 16]) phases.append(PhasePlot.from_profile(p2d)) cls.profiles = profiles cls.phases = phases cls.ds = test_ds
def test_fix_length(): """ Test fixing the length of an array. Used in spheres and other data objects """ ds = fake_random_ds(64, nprocs=1, length_unit=10) length = ds.quan(1.0, 'code_length') new_length = fix_length(length, ds=ds) yield assert_equal, YTQuantity(10, 'cm'), new_length
def test_write_projection(): """Tests functionality of off_axis_projection and write_projection.""" # Perform I/O in safe place instead of yt main dir tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) # args for off_axis_projection test_ds = fake_random_ds(64) c = [0.5, 0.5, 0.5] norm = [0.5, 0.5, 0.5] W = [0.5,0.5,1.0] N = 64 field = "density" oap_args = [test_ds, c, norm, W, N, field] # kwargs for off_axis_projection oap_kwargs = {} oap_kwargs['weight'] = (None, 'cell_mass') oap_kwargs['no_ghost'] = (True, False) oap_kwargs['interpolated'] = (True, False) oap_kwargs['north_vector'] = ((1,0,0), (0,0.5,1.0)) oap_kwargs_list = expand_keywords(oap_kwargs) # args for write_projection fn = "test.png" # kwargs for write_projection wp_kwargs = {} wp_kwargs['colorbar'] = (True, False) wp_kwargs['colorbar_label'] = ('test') wp_kwargs['title'] = ('test') wp_kwargs['limits'] = (None, (1e3, 1e5)) wp_kwargs['take_log'] = (True, False) wp_kwargs['figsize'] = ((8,6), [1,1]) wp_kwargs['dpi'] = (100, 50) wp_kwargs['cmap_name'] = ('algae', 'jet') wp_kwargs_list = expand_keywords(wp_kwargs) # test all off_axis_projection kwargs and write_projection kwargs # make sure they are able to be projected, then remove and try next # iteration for oap_kwargs in oap_kwargs_list: image = off_axis_projection(*oap_args, **oap_kwargs) for wp_kwargs in wp_kwargs_list: write_projection(image, fn, **wp_kwargs) yield assert_equal, os.path.exists(fn), True os.remove(fn) os.chdir(curdir) # clean up shutil.rmtree(tmpdir)
def test_slice_selector(): # generate fake data with a number of non-cubical grids ds = fake_random_ds(64, nprocs=51) assert all(ds.periodicity) for i, d in enumerate("xyz"): for coord in np.arange(0.0, 1.0, 0.1): data = ds.slice(i, coord) data.get_data() v = data[d].to_ndarray() yield assert_equal, data.shape[0], 64 ** 2 yield assert_equal, data["ones"].shape[0], 64 ** 2 yield assert_array_less, np.abs(v - coord), 1.0 / 128.0 + 1e-6
def test_export_frb(): test_ds = fake_random_ds(128) slc = test_ds.slice(0,0.5) frb = slc.to_frb((0.5,"unitary"), 64) frb_ds = frb.export_dataset(fields=["density"], nprocs=8) dd_frb = frb_ds.all_data() yield assert_equal, frb_ds.domain_left_edge.v, np.array([0.25,0.25,0.0]) yield assert_equal, frb_ds.domain_right_edge.v, np.array([0.75,0.75,1.0]) yield assert_equal, frb_ds.domain_width.v, np.array([0.5,0.5,1.0]) yield assert_equal, frb_ds.domain_dimensions, np.array([64,64,1], dtype="int64") yield assert_allclose_units, frb["density"].sum(), \ dd_frb.quantities.total_quantity("density") yield assert_equal, frb_ds.index.num_grids, 8
def test_is_code_unit(): ds = fake_random_ds(64, nprocs=1) u1 = Unit('code_mass', registry=ds.unit_registry) u2 = Unit('code_mass/code_length', registry=ds.unit_registry) u3 = Unit('code_velocity*code_mass**2', registry=ds.unit_registry) u4 = Unit('code_time*code_mass**0.5', registry=ds.unit_registry) u5 = Unit('code_mass*g', registry=ds.unit_registry) u6 = Unit('g/cm**3') yield assert_true, u1.is_code_unit yield assert_true, u2.is_code_unit yield assert_true, u3.is_code_unit yield assert_true, u4.is_code_unit yield assert_true, not u5.is_code_unit yield assert_true, not u6.is_code_unit
def setUp(self): if use_tmpdir: self.curdir = os.getcwd() # Perform I/O in safe place instead of yt main dir self.tmpdir = tempfile.mkdtemp() os.chdir(self.tmpdir) else: self.curdir, self.tmpdir = None, None self.ds = fake_random_ds(64) self.c = self.ds.domain_center self.L = np.array([0.5, 0.5, 0.5]) self.W = 1.5*self.ds.domain_width self.N = 64 self.field = ("gas", "density")
def test_h5_io(): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) ds = fake_random_ds(64, nprocs=1, length_unit=10) warr = ds.arr(np.random.random((256, 256)), 'code_length') warr.write_hdf5('test.h5') iarr = YTArray.from_hdf5('test.h5') yield assert_equal, warr, iarr yield assert_equal, warr.units.registry['code_length'], iarr.units.registry['code_length'] os.chdir(curdir) shutil.rmtree(tmpdir)
def test_point_selector(): # generate fake amr data ds = fake_random_ds(64, nprocs=51) assert all(ds.periodicity) dd = ds.all_data() positions = np.array([dd[ax] for ax in "xyz"]) delta = 0.5 * np.array([dd["d" + ax] for ax in "xyz"]) # ensure cell centers and corners always return one and # only one point object for p in positions: data = ds.point(p) assert_equal(data["ones"].shape[0], 1) for p in positions - delta: data = ds.point(p) assert_equal(data["ones"].shape[0], 1) for p in positions + delta: data = ds.point(p) assert_equal(data["ones"].shape[0], 1)
def test_ellipsoid_selector(): # generate fake data with a number of non-cubical grids ds = fake_random_ds(64, nprocs=51) assert all(ds.periodicity) ellipsoids = [[0.0, 0.0, 0.0], [0.5, 0.5, 0.5], [1.0, 1.0, 1.0], [0.25, 0.75, 0.25]] # spherical ellipsoid tests ratios = 3 * [0.25] for center in ellipsoids: data = ds.ellipsoid(center, ratios[0], ratios[1], ratios[2], np.array([1.0, 0.0, 0.0]), 0.0) data.get_data() dd = ds.all_data() dd.set_field_parameter("center", ds.arr(center, "code_length")) n_outside = (dd["radius"] >= ratios[0]).sum() assert_equal(data["radius"].size + n_outside, dd["radius"].size) positions = np.array([data[ax] for ax in "xyz"]) centers = np.tile(data.center, data.shape[0]).reshape(data.shape[0], 3).transpose() dist = periodic_dist(positions, centers, ds.domain_right_edge - ds.domain_left_edge, ds.periodicity) # WARNING: this value has not been externally verified yield assert_array_less, dist, ratios[0] # aligned ellipsoid tests ratios = [0.25, 0.1, 0.1] for center in ellipsoids: data = ds.ellipsoid(center, ratios[0], ratios[1], ratios[2], np.array([1.0, 0.0, 0.0]), 0.0) # hack to compute elliptic distance dist2 = np.zeros(data["ones"].shape[0]) for i, ax in enumerate("xyz"): positions = np.zeros((3, data["ones"].shape[0])) positions[i, :] = data[ax] centers = np.zeros((3, data["ones"].shape[0])) centers[i, :] = center[i] dist2 += ( periodic_dist(positions, centers, ds.domain_right_edge - ds.domain_left_edge, ds.periodicity) / ratios[i] ) ** 2 # WARNING: this value has not been externally verified yield assert_array_less, dist2, 1.0
def test_write_gdf(): """Main test suite for write_gdf""" tmpdir = tempfile.mkdtemp() tmpfile = os.path.join(tmpdir, 'test_gdf.h5') try: test_ds = fake_random_ds(64) write_to_gdf(test_ds, tmpfile, data_author=TEST_AUTHOR, data_comment=TEST_COMMENT) del test_ds assert isinstance(load(tmpfile), GDFDataset) h5f = h5.File(tmpfile, 'r') gdf = h5f['gridded_data_format'].attrs assert_equal(gdf['data_author'], TEST_AUTHOR) assert_equal(gdf['data_comment'], TEST_COMMENT) h5f.close() finally: shutil.rmtree(tmpdir)
def test_ytarray_pickle(): ds = fake_random_ds(64, nprocs=1) test_data = [ds.quan(12.0, 'code_length'), ds.arr([1, 2, 3], 'code_length')] for data in test_data: tempf = tempfile.NamedTemporaryFile(delete=False) pickle.dump(data, tempf) tempf.close() with open(tempf.name, "rb") as fname: loaded_data = pickle.load(fname) os.unlink(tempf.name) yield assert_array_equal, data, loaded_data yield assert_equal, data.units, loaded_data.units yield assert_array_equal, array(data.in_cgs()), \ array(loaded_data.in_cgs()) yield assert_equal, float(data.units.base_value), \ float(loaded_data.units.base_value)
def test_sphere_selector(): # generate fake data with a number of non-cubical grids ds = fake_random_ds(64, nprocs=51) assert all(ds.periodicity) # aligned tests spheres = [[0.0, 0.0, 0.0], [0.5, 0.5, 0.5], [1.0, 1.0, 1.0], [0.25, 0.75, 0.25]] for center in spheres: data = ds.sphere(center, 0.25) # WARNING: this value has not be externally verified dd = ds.all_data() dd.set_field_parameter("center", ds.arr(center, "code_length")) n_outside = (dd["radius"] >= 0.25).sum() assert_equal(data["radius"].size + n_outside, dd["radius"].size) positions = np.array([data[ax] for ax in "xyz"]) centers = np.tile(data.center, data["x"].shape[0]).reshape(data["x"].shape[0], 3).transpose() dist = periodic_dist(positions, centers, ds.domain_right_edge - ds.domain_left_edge, ds.periodicity) # WARNING: this value has not been externally verified yield assert_array_less, dist, 0.25
def test_cutting_plane(): fns = [] for nprocs in [8, 1]: # We want to test both 1 proc and 8 procs, to make sure that # parallelism isn't broken ds = fake_random_ds(64, nprocs=nprocs) center = [0.5, 0.5, 0.5] normal = [1, 1, 1] cut = ds.cutting(normal, center) yield assert_equal, cut["ones"].sum(), cut["ones"].size yield assert_equal, cut["ones"].min(), 1.0 yield assert_equal, cut["ones"].max(), 1.0 pw = cut.to_pw(fields='density') for p in pw.plots.values(): tmpfd, tmpname = tempfile.mkstemp(suffix='.png') os.close(tmpfd) p.save(name=tmpname) fns.append(tmpname) for width in [(1.0, 'unitary'), 1.0, ds.quan(0.5, 'code_length')]: frb = cut.to_frb(width, 64) for cut_field in ['ones', 'density']: fi = ds._get_field_info("unknown", cut_field) yield assert_equal, frb[cut_field].info['data_source'], \ cut.__str__() yield assert_equal, frb[cut_field].info['axis'], \ 4 yield assert_equal, frb[cut_field].info['field'], \ cut_field yield assert_equal, frb[cut_field].units, \ Unit(fi.units) yield assert_equal, frb[cut_field].info['xlim'], \ frb.bounds[:2] yield assert_equal, frb[cut_field].info['ylim'], \ frb.bounds[2:] yield assert_equal, frb[cut_field].info['length_to_cm'], \ ds.length_unit.in_cgs() yield assert_equal, frb[cut_field].info['center'], \ cut.center teardown_func(fns)
def test_registry_association(): ds = fake_random_ds(64, nprocs=1, length_unit=10) a = ds.quan(3, 'cm') b = YTQuantity(4, 'm') c = ds.quan(6, '') d = 5 yield assert_equal, id(a.units.registry), id(ds.unit_registry) def binary_op_registry_comparison(op): e = op(a, b) f = op(b, a) g = op(c, d) h = op(d, c) assert_equal(id(e.units.registry), id(ds.unit_registry)) assert_equal(id(f.units.registry), id(b.units.registry)) assert_equal(id(g.units.registry), id(h.units.registry)) assert_equal(id(g.units.registry), id(ds.unit_registry)) def unary_op_registry_comparison(op): c = op(a) d = op(b) assert_equal(id(c.units.registry), id(ds.unit_registry)) assert_equal(id(d.units.registry), id(b.units.registry)) binary_ops = [operator.add, operator.sub, operator.mul, operator.truediv] if hasattr(operator, "div"): binary_ops.append(operator.div) for op in binary_ops: yield binary_op_registry_comparison, op for op in [operator.abs, operator.neg, operator.pos]: yield unary_op_registry_comparison, op
def setUpClass(cls): test_ds = fake_random_ds(64) normal = [1, 1, 1] ds_region = test_ds.region([0.5] * 3, [0.4] * 3, [0.6] * 3) projections = [] projections_ds = [] projections_c = [] projections_wf = [] projections_w = {} projections_m = [] for dim in range(3): projections.append(ProjectionPlot(test_ds, dim, "density")) projections_ds.append(ProjectionPlot(test_ds, dim, "density", data_source=ds_region)) for center in CENTER_SPECS: projections_c.append(ProjectionPlot(test_ds, dim, "density", center=center)) for width in WIDTH_SPECS: projections_w[width] = ProjectionPlot(test_ds, dim, 'density', width=width) for wf in WEIGHT_FIELDS: projections_wf.append(ProjectionPlot(test_ds, dim, "density", weight_field=wf)) for m in PROJECTION_METHODS: projections_m.append(ProjectionPlot(test_ds, dim, "density", method=m)) cls.slices = [SlicePlot(test_ds, dim, "density") for dim in range(3)] cls.projections = projections cls.projections_ds = projections_ds cls.projections_c = projections_c cls.projections_wf = projections_wf cls.projections_w = projections_w cls.projections_m = projections_m cls.offaxis_slice = OffAxisSlicePlot(test_ds, normal, "density") cls.offaxis_proj = OffAxisProjectionPlot(test_ds, normal, "density")
def test_profiles(): ds = fake_random_ds(64, nprocs=8, fields=_fields, units=_units) nv = ds.domain_dimensions.prod() dd = ds.all_data() (rmi, rma), (tmi, tma), (dmi, dma) = dd.quantities["Extrema"]( ["density", "temperature", "dinosaurs"]) rt, tt, dt = dd.quantities["TotalQuantity"]( ["density", "temperature", "dinosaurs"]) e1, e2 = 0.9, 1.1 for nb in [8, 16, 32, 64]: for input_units in ['mks', 'cgs']: for ex in [rmi, rma, tmi, tma, dmi, dma]: getattr(ex, 'convert_to_%s' % input_units)() # We log all the fields or don't log 'em all. No need to do them # individually. for lf in [True, False]: direct_profile = Profile1D(dd, "density", nb, rmi * e1, rma * e2, lf, weight_field=None) direct_profile.add_fields(["ones", "temperature"]) indirect_profile_s = create_profile( dd, "density", ["ones", "temperature"], n_bins=nb, extrema={'density': (rmi * e1, rma * e2)}, logs={'density': lf}, weight_field=None) indirect_profile_t = create_profile( dd, ("gas", "density"), [("index", "ones"), ("gas", "temperature")], n_bins=nb, extrema={'density': (rmi * e1, rma * e2)}, logs={'density': lf}, weight_field=None) for p1d in [ direct_profile, indirect_profile_s, indirect_profile_t ]: yield assert_equal, p1d["index", "ones"].sum(), nv yield assert_rel_equal, tt, \ p1d["gas", "temperature"].sum(), 7 p2d = Profile2D(dd, "density", nb, rmi * e1, rma * e2, lf, "temperature", nb, tmi * e1, tma * e2, lf, weight_field=None) p2d.add_fields(["ones", "temperature"]) yield assert_equal, p2d["ones"].sum(), nv yield assert_rel_equal, tt, p2d["temperature"].sum(), 7 p3d = Profile3D(dd, "density", nb, rmi * e1, rma * e2, lf, "temperature", nb, tmi * e1, tma * e2, lf, "dinosaurs", nb, dmi * e1, dma * e2, lf, weight_field=None) p3d.add_fields(["ones", "temperature"]) yield assert_equal, p3d["ones"].sum(), nv yield assert_rel_equal, tt, p3d["temperature"].sum(), 7 p1d = Profile1D(dd, "x", nb, 0.0, 1.0, False, weight_field=None) p1d.add_fields("ones") av = nv / nb yield assert_equal, p1d["ones"], np.ones(nb) * av # We re-bin ones with a weight now p1d = Profile1D(dd, "x", nb, 0.0, 1.0, False, weight_field="temperature") p1d.add_fields(["ones"]) yield assert_equal, p1d["ones"], np.ones(nb) # Verify we can access "ones" after adding a new field # See issue 988 p1d.add_fields(["density"]) yield assert_equal, p1d["ones"], np.ones(nb) p2d = Profile2D(dd, "x", nb, 0.0, 1.0, False, "y", nb, 0.0, 1.0, False, weight_field=None) p2d.add_fields("ones") av = nv / nb**2 yield assert_equal, p2d["ones"], np.ones((nb, nb)) * av # We re-bin ones with a weight now p2d = Profile2D(dd, "x", nb, 0.0, 1.0, False, "y", nb, 0.0, 1.0, False, weight_field="temperature") p2d.add_fields(["ones"]) yield assert_equal, p2d["ones"], np.ones((nb, nb)) p3d = Profile3D(dd, "x", nb, 0.0, 1.0, False, "y", nb, 0.0, 1.0, False, "z", nb, 0.0, 1.0, False, weight_field=None) p3d.add_fields("ones") av = nv / nb**3 yield assert_equal, p3d["ones"], np.ones((nb, nb, nb)) * av # We re-bin ones with a weight now p3d = Profile3D(dd, "x", nb, 0.0, 1.0, False, "y", nb, 0.0, 1.0, False, "z", nb, 0.0, 1.0, False, weight_field="temperature") p3d.add_fields(["ones"]) yield assert_equal, p3d["ones"], np.ones((nb, nb, nb))
import yt from yt.testing import fake_random_ds, assert_equal def _test(field, data): return data[('stream', 'velocity_x')] ds = fake_random_ds() ds.add_field(('stream, density'), function=_test, units='cm/s', force_override=True) assert_equal(ds.all_data()[('stream', 'density')], ds.all_data()[('stream', 'velocity_x')])
def test_mixed_particle_mesh_profiles(): ds = fake_random_ds(32, particles=10) ad = ds.all_data() assert_raises( YTIllDefinedProfile, ProfilePlot, ad, ("index", "radius"), ("all", "particle_mass"), ) assert_raises( YTIllDefinedProfile, ProfilePlot, ad, "radius", [("all", "particle_mass"), ("all", "particle_ones")], ) assert_raises( YTIllDefinedProfile, ProfilePlot, ad, ("index", "radius"), [("all", "particle_mass"), ("index", "ones")], ) assert_raises( YTIllDefinedProfile, ProfilePlot, ad, ("all", "particle_radius"), ("all", "particle_mass"), ("gas", "cell_mass"), ) assert_raises( YTIllDefinedProfile, ProfilePlot, ad, ("index", "radius"), ("gas", "cell_mass"), ("all", "particle_ones"), ) assert_raises( YTIllDefinedProfile, PhasePlot, ad, ("index", "radius"), ("all", "particle_mass"), ("gas", "velocity_x"), ) assert_raises( YTIllDefinedProfile, PhasePlot, ad, ("all", "particle_radius"), ("all", "particle_mass"), ("gas", "cell_mass"), ) assert_raises( YTIllDefinedProfile, PhasePlot, ad, ("index", "radius"), ("gas", "cell_mass"), ("all", "particle_ones"), ) assert_raises( YTIllDefinedProfile, PhasePlot, ad, ("all", "particle_radius"), ("all", "particle_mass"), ("all", "particle_ones"), )
def test_slice_over_outer_boundary(): ds = fake_random_ds(64, nprocs=8, fields=["density"], negative=[False]) slc = ds.slice(2, 1.0) slc["density"] yield assert_equal, slc["density"].size, 0
def test_slice_over_edges(): ds = fake_random_ds(64, nprocs=8, fields=["density"], negative=[False]) slc = ds.slice(0, 0.0) slc["density"] slc = ds.slice(1, 0.5) slc["density"]
def test_particle_profiles(): for nproc in [1, 2, 4, 8]: ds = fake_random_ds(32, nprocs=nproc, particles=32**3) dd = ds.all_data() p1d = Profile1D(dd, "particle_position_x", 128, 0.0, 1.0, False, weight_field=None) p1d.add_fields(["particle_ones"]) assert_equal(p1d["particle_ones"].sum(), 32**3) p1d = create_profile( dd, ["particle_position_x"], ["particle_ones"], weight_field=None, n_bins=128, extrema=extrema_s, logs=logs_s, ) assert_equal(p1d["particle_ones"].sum(), 32**3) p1d = create_profile( dd, [("all", "particle_position_x")], [("all", "particle_ones")], weight_field=None, n_bins=128, extrema=extrema_t, logs=logs_t, ) assert_equal(p1d["particle_ones"].sum(), 32**3) p2d = Profile2D( dd, "particle_position_x", 128, 0.0, 1.0, False, "particle_position_y", 128, 0.0, 1.0, False, weight_field=None, ) p2d.add_fields(["particle_ones"]) assert_equal(p2d["particle_ones"].sum(), 32**3) p3d = Profile3D( dd, "particle_position_x", 128, 0.0, 1.0, False, "particle_position_y", 128, 0.0, 1.0, False, "particle_position_z", 128, 0.0, 1.0, False, weight_field=None, ) p3d.add_fields(["particle_ones"]) assert_equal(p3d["particle_ones"].sum(), 32**3)
def test_periodicity(): # First test the simple case were we find the distance between two points a = [0.1, 0.1, 0.1] b = [0.9, 0.9, 0.9] period = 1.0 dist = periodic_dist(a, b, period) assert_almost_equal(dist, 0.34641016151377535) dist = periodic_dist(a, b, period, (True, False, False)) assert_almost_equal(dist, 1.1489125293076059) dist = periodic_dist(a, b, period, (False, True, False)) assert_almost_equal(dist, 1.1489125293076059) dist = periodic_dist(a, b, period, (False, False, True)) assert_almost_equal(dist, 1.1489125293076059) dist = periodic_dist(a, b, period, (True, True, False)) assert_almost_equal(dist, 0.84852813742385713) dist = periodic_dist(a, b, period, (True, False, True)) assert_almost_equal(dist, 0.84852813742385713) dist = periodic_dist(a, b, period, (False, True, True)) assert_almost_equal(dist, 0.84852813742385713) dist = euclidean_dist(a, b) assert_almost_equal(dist, 1.3856406460551021) # Now test the more complicated cases where we're calculating radii based # on data objects ds = fake_random_ds(64) # First we test flattened data data = ds.all_data() positions = np.array([data[ax] for ax in "xyz"]) c = [0.1, 0.1, 0.1] n_tup = tuple([1 for i in range(positions.ndim - 1)]) center = np.tile( np.reshape(np.array(c), (positions.shape[0],) + n_tup), (1,) + positions.shape[1:], ) dist = periodic_dist(positions, center, period, ds.periodicity) assert_almost_equal(dist.min(), 0.00270632938683) assert_almost_equal(dist.max(), 0.863319074398) dist = euclidean_dist(positions, center) assert_almost_equal(dist.min(), 0.00270632938683) assert_almost_equal(dist.max(), 1.54531407988) # Then grid-like data data = ds.index.grids[0] positions = np.array([data[ax] for ax in "xyz"]) c = [0.1, 0.1, 0.1] n_tup = tuple([1 for i in range(positions.ndim - 1)]) center = np.tile( np.reshape(np.array(c), (positions.shape[0],) + n_tup), (1,) + positions.shape[1:], ) dist = periodic_dist(positions, center, period, ds.periodicity) assert_almost_equal(dist.min(), 0.00270632938683) assert_almost_equal(dist.max(), 0.863319074398) dist = euclidean_dist(positions, center) assert_almost_equal(dist.min(), 0.00270632938683) assert_almost_equal(dist.max(), 1.54531407988)
def test_projection_plot_m(self): test_ds = fake_random_ds(16) for method in PROJECTION_METHODS: proj = ProjectionPlot(test_ds, 0, "density", method=method) proj.save()
def test_particle_counts(): ds = fake_random_ds(16, particles=100) assert ds.particle_type_counts == {"io": 100} pds = fake_particle_ds(npart=128) assert pds.particle_type_counts == {"io": 128}
def test_projection_plot_c(self): test_ds = fake_random_ds(16) for center in CENTER_SPECS: proj = ProjectionPlot(test_ds, 0, "density", center=center) proj.save()
def test_projection_plot_wf(self): test_ds = fake_random_ds(16) for wf in WEIGHT_FIELDS: proj = ProjectionPlot(test_ds, 0, "density", weight_field=wf) proj.save()
def test_projection_plot_ds(self): test_ds = fake_random_ds(16) reg = test_ds.region([0.5] * 3, [0.4] * 3, [0.6] * 3) for dim in range(3): proj = ProjectionPlot(test_ds, dim, "density", data_source=reg) proj.save()
def test_projection_plot(self): test_ds = fake_random_ds(16) for dim in range(3): proj = ProjectionPlot(test_ds, dim, "density") for fname in TEST_FLNMS: assert_fname(proj.save(fname)[0])
def test_repr_html(self): test_ds = fake_random_ds(16) slc = SlicePlot(test_ds, 0, "density") slc._repr_html_()
def test_slice_plot(self): test_ds = fake_random_ds(16) for dim in range(3): slc = SlicePlot(test_ds, dim, "density") for fname in TEST_FLNMS: assert_fname(slc.save(fname)[0])
def test_offaxis_projection_plot(self): test_ds = fake_random_ds(16) prj = OffAxisProjectionPlot(test_ds, [1, 1, 1], "density") for fname in TEST_FLNMS: assert_fname(prj.save(fname)[0])
from yt.utilities.cosmology import \ Cosmology from yt.frontends.stream.fields import \ StreamFieldInfo from yt.frontends.api import _frontends from yt.fields.derived_field import NullFunc import yt.frontends as frontends_module from yt.units.yt_array import Unit from yt.units import dimensions fields, units = [], [] for fname, (code_units, aliases, dn) in StreamFieldInfo.known_other_fields: fields.append(("gas", fname)) units.append(code_units) base_ds = fake_random_ds(4, fields=fields, units=units) base_ds.index base_ds.cosmological_simulation = 1 base_ds.cosmology = Cosmology() from yt.config import ytcfg ytcfg["yt", "__withintesting"] = "True" np.seterr(all='ignore') def _strip_ftype(field): if not isinstance(field, tuple): return field elif field[0] == "all": return field return field[1]
def test_setup_origin(): origin_inputs = ( "domain", "left-window", "center-domain", "lower-right-window", ("window", ), ("right", "domain"), ("lower", "window"), ("lower", "right", "window"), (0.5, 0.5, "domain"), ((50, "cm"), (50, "cm"), "domain"), ) w = (10, "cm") ds = fake_random_ds(32, length_unit=100.0) generated_limits = [] # lower limit -> llim # upper limit -> ulim # xllim xulim yllim yulim correct_limits = [ 45.0, 55.0, 45.0, 55.0, 0.0, 10.0, 0.0, 10.0, -5.0, 5.0, -5.0, 5.0, -10.0, 0, 0, 10.0, 0.0, 10.0, 0.0, 10.0, -55.0, -45.0, -55.0, -45.0, -5.0, 5.0, 0.0, 10.0, -10.0, 0, 0, 10.0, -5.0, 5.0, -5.0, 5.0, -5.0, 5.0, -5.0, 5.0, ] for o in origin_inputs: slc = SlicePlot(ds, 2, "density", width=w, origin=o) ax = slc.plots["density"].axes xlims = ax.get_xlim() ylims = ax.get_ylim() lims = [xlims[0], xlims[1], ylims[0], ylims[1]] for l in lims: generated_limits.append(l) assert_array_almost_equal(correct_limits, generated_limits)
def test_fits_image(): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) fields = ("density", "temperature") units = ( 'g/cm**3', 'K', ) ds = fake_random_ds(64, fields=fields, units=units, nprocs=16, length_unit=100.0) prj = ds.proj("density", 2) prj_frb = prj.to_frb((0.5, "unitary"), 128) fid1 = FITSImageData(prj_frb, fields=["density", "temperature"], units="cm") fits_prj = FITSProjection(ds, "z", ["density", "temperature"], image_res=128, width=(0.5, "unitary")) assert_equal(fid1["density"].data, fits_prj["density"].data) assert_equal(fid1["temperature"].data, fits_prj["temperature"].data) fid1.writeto("fid1.fits", clobber=True) new_fid1 = FITSImageData.from_file("fid1.fits") assert_equal(fid1["density"].data, new_fid1["density"].data) assert_equal(fid1["temperature"].data, new_fid1["temperature"].data) ds2 = load("fid1.fits") ds2.index assert ("fits", "density") in ds2.field_list assert ("fits", "temperature") in ds2.field_list dw_cm = ds2.domain_width.in_units("cm") assert dw_cm[0].v == 50. assert dw_cm[1].v == 50. slc = ds.slice(2, 0.5) slc_frb = slc.to_frb((0.5, "unitary"), 128) fid2 = FITSImageData(slc_frb, fields=["density", "temperature"], units="cm") fits_slc = FITSSlice(ds, "z", ["density", "temperature"], image_res=128, width=(0.5, "unitary")) assert_equal(fid2["density"].data, fits_slc["density"].data) assert_equal(fid2["temperature"].data, fits_slc["temperature"].data) dens_img = fid2.pop("density") temp_img = fid2.pop("temperature") # This already has some assertions in it, so we don't need to do anything # with it other than just make one FITSImageData.from_images([dens_img, temp_img]) cut = ds.cutting([0.1, 0.2, -0.9], [0.5, 0.42, 0.6]) cut_frb = cut.to_frb((0.5, "unitary"), 128) fid3 = FITSImageData(cut_frb, fields=["density", "temperature"], units="cm") fits_cut = FITSOffAxisSlice(ds, [0.1, 0.2, -0.9], ["density", "temperature"], image_res=128, center=[0.5, 0.42, 0.6], width=(0.5, "unitary")) assert_equal(fid3["density"].data, fits_cut["density"].data) assert_equal(fid3["temperature"].data, fits_cut["temperature"].data) fid3.create_sky_wcs([30., 45.], (1.0, "arcsec/kpc")) fid3.writeto("fid3.fits", clobber=True) new_fid3 = FITSImageData.from_file("fid3.fits") assert_same_wcs(fid3.wcs, new_fid3.wcs) assert new_fid3.wcs.wcs.cunit[0] == "deg" assert new_fid3.wcs.wcs.cunit[1] == "deg" assert new_fid3.wcs.wcs.ctype[0] == "RA---TAN" assert new_fid3.wcs.wcs.ctype[1] == "DEC--TAN" buf = off_axis_projection(ds, ds.domain_center, [0.1, 0.2, -0.9], 0.5, 128, "density").swapaxes(0, 1) fid4 = FITSImageData(buf, fields="density", width=100.0) fits_oap = FITSOffAxisProjection(ds, [0.1, 0.2, -0.9], "density", width=(0.5, "unitary"), image_res=128, depth=(0.5, "unitary")) assert_equal(fid4["density"].data, fits_oap["density"].data) fid4.create_sky_wcs([30., 45.], (1.0, "arcsec/kpc"), replace_old_wcs=False) assert fid4.wcs.wcs.cunit[0] == "cm" assert fid4.wcs.wcs.cunit[1] == "cm" assert fid4.wcs.wcs.ctype[0] == "linear" assert fid4.wcs.wcs.ctype[1] == "linear" assert fid4.wcsa.wcs.cunit[0] == "deg" assert fid4.wcsa.wcs.cunit[1] == "deg" assert fid4.wcsa.wcs.ctype[0] == "RA---TAN" assert fid4.wcsa.wcs.ctype[1] == "DEC--TAN" cvg = ds.covering_grid(ds.index.max_level, [0.25, 0.25, 0.25], [32, 32, 32], fields=["density", "temperature"]) fid5 = FITSImageData(cvg, fields=["density", "temperature"]) assert fid5.dimensionality == 3 fid5.update_header("density", "time", 0.1) fid5.update_header("all", "units", "cgs") assert fid5["density"].header["time"] == 0.1 assert fid5["temperature"].header["units"] == "cgs" assert fid5["density"].header["units"] == "cgs" os.chdir(curdir) shutil.rmtree(tmpdir)
def test_sigma_clip(self): ds = fake_random_ds(32) sc = yt.create_scene(ds) sc.save("clip_2.png", sigma_clip=2)
def test_smoothed_covering_grid_2d_dataset(): ds = fake_random_ds([32, 32, 1], nprocs=4) ds.force_periodicity() scg = ds.smoothed_covering_grid(1, [0.0, 0.0, 0.0], [32, 32, 1]) assert_equal(scg[("gas", "density")].shape, [32, 32, 1])
def test_domain_sphere(): # Now we test that we can get different radial velocities based on field # parameters. # Get the first sphere ds = fake_random_ds( 16, fields=("density", "velocity_x", "velocity_y", "velocity_z"), units=("g/cm**3", "cm/s", "cm/s", "cm/s"), ) sp0 = ds.sphere(ds.domain_center, 0.25) # Compute the bulk velocity from the cells in this sphere bulk_vel = sp0.quantities.bulk_velocity() # Get the second sphere sp1 = ds.sphere(ds.domain_center, 0.25) # Set the bulk velocity field parameter sp1.set_field_parameter("bulk_velocity", bulk_vel) assert_equal(np.any(sp0["radial_velocity"] == sp1["radial_velocity"]), False) # Radial profile without correction # Note we set n_bins = 8 here. rp0 = create_profile( sp0, "radius", "radial_velocity", units={"radius": "kpc"}, logs={"radius": False}, n_bins=8, ) # Radial profile with correction for bulk velocity rp1 = create_profile( sp1, "radius", "radial_velocity", units={"radius": "kpc"}, logs={"radius": False}, n_bins=8, ) assert_equal(rp0.x_bins, rp1.x_bins) assert_equal(rp0.used, rp1.used) assert_equal(rp0.used.sum() > rp0.used.size / 2.0, True) assert_equal( np.any(rp0["radial_velocity"][rp0.used] == rp1["radial_velocity"][rp1.used]), False, ) ref_sp = ds.sphere("c", 0.25) for f in _fields_to_compare: ref_sp[f].sort() for center in periodicity_cases(ds): sp = ds.sphere(center, 0.25) for f in _fields_to_compare: sp[f].sort() assert_equal(sp[f], ref_sp[f])
def test_fits_image(): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) fields = ("density", "temperature") units = ('g/cm**3', 'K',) ds = fake_random_ds(64, fields=fields, units=units, nprocs=16, length_unit=100.0) prj = ds.proj("density", 2) prj_frb = prj.to_frb((0.5, "unitary"), 128) fid1 = FITSImageData(prj_frb, fields=["density","temperature"], units="cm") fits_prj = FITSProjection(ds, "z", ["density","temperature"], image_res=128, width=(0.5,"unitary")) yield assert_equal, fid1.get_data("density"), fits_prj.get_data("density") yield assert_equal, fid1.get_data("temperature"), fits_prj.get_data("temperature") fid1.writeto("fid1.fits", clobber=True) new_fid1 = FITSImageData.from_file("fid1.fits") yield assert_equal, fid1.get_data("density"), new_fid1.get_data("density") yield assert_equal, fid1.get_data("temperature"), new_fid1.get_data("temperature") ds2 = load("fid1.fits") ds2.index assert ("fits","density") in ds2.field_list assert ("fits","temperature") in ds2.field_list dw_cm = ds2.domain_width.in_units("cm") assert dw_cm[0].v == 50. assert dw_cm[1].v == 50. slc = ds.slice(2, 0.5) slc_frb = slc.to_frb((0.5, "unitary"), 128) fid2 = FITSImageData(slc_frb, fields=["density","temperature"], units="cm") fits_slc = FITSSlice(ds, "z", ["density","temperature"], image_res=128, width=(0.5,"unitary")) yield assert_equal, fid2.get_data("density"), fits_slc.get_data("density") yield assert_equal, fid2.get_data("temperature"), fits_slc.get_data("temperature") dens_img = fid2.pop("density") temp_img = fid2.pop("temperature") # This already has some assertions in it, so we don't need to do anything # with it other can just make one fid_comb = FITSImageData.from_images([dens_img, temp_img]) cut = ds.cutting([0.1, 0.2, -0.9], [0.5, 0.42, 0.6]) cut_frb = cut.to_frb((0.5, "unitary"), 128) fid3 = FITSImageData(cut_frb, fields=["density","temperature"], units="cm") fits_cut = FITSOffAxisSlice(ds, [0.1, 0.2, -0.9], ["density","temperature"], image_res=128, center=[0.5, 0.42, 0.6], width=(0.5,"unitary")) yield assert_equal, fid3.get_data("density"), fits_cut.get_data("density") yield assert_equal, fid3.get_data("temperature"), fits_cut.get_data("temperature") fid3.create_sky_wcs([30.,45.], (1.0,"arcsec/kpc")) fid3.writeto("fid3.fits", clobber=True) new_fid3 = FITSImageData.from_file("fid3.fits") assert_same_wcs(fid3.wcs, new_fid3.wcs) assert new_fid3.wcs.wcs.cunit[0] == "deg" assert new_fid3.wcs.wcs.cunit[1] == "deg" assert new_fid3.wcs.wcs.ctype[0] == "RA---TAN" assert new_fid3.wcs.wcs.ctype[1] == "DEC--TAN" buf = off_axis_projection(ds, ds.domain_center, [0.1, 0.2, -0.9], 0.5, 128, "density").swapaxes(0, 1) fid4 = FITSImageData(buf, fields="density", width=100.0) fits_oap = FITSOffAxisProjection(ds, [0.1, 0.2, -0.9], "density", width=(0.5,"unitary"), image_res=128, depth_res=128, depth=(0.5,"unitary")) yield assert_equal, fid4.get_data("density"), fits_oap.get_data("density") cvg = ds.covering_grid(ds.index.max_level, [0.25,0.25,0.25], [32, 32, 32], fields=["density","temperature"]) fid5 = FITSImageData(cvg, fields=["density","temperature"]) assert fid5.dimensionality == 3 fid5.update_header("density", "time", 0.1) fid5.update_header("all", "units", "cgs") assert fid5["density"].header["time"] == 0.1 assert fid5["temperature"].header["units"] == "cgs" assert fid5["density"].header["units"] == "cgs" os.chdir(curdir) shutil.rmtree(tmpdir)
def test_mean_sum_integrate(): for nprocs in [-1, 1, 2, 16]: if nprocs == -1: ds = fake_amr_ds(fields=("density",), units=("g/cm**3",), particles=20) else: ds = fake_random_ds( 32, nprocs=nprocs, fields=("density",), units=("g/cm**3",), particles=20 ) ad = ds.all_data() # Sums q = ad.sum(("gas", "density")) q1 = ad.quantities.total_quantity(("gas", "density")) assert_equal(q, q1) q = ad.sum(("all", "particle_ones")) q1 = ad.quantities.total_quantity(("all", "particle_ones")) assert_equal(q, q1) # Weighted Averages w = ad.mean(("gas", "density")) w1 = ad.quantities.weighted_average_quantity( ("gas", "density"), ("index", "ones") ) assert_equal(w, w1) w = ad.mean(("gas", "density"), weight=("gas", "density")) w1 = ad.quantities.weighted_average_quantity( ("gas", "density"), ("gas", "density") ) assert_equal(w, w1) w = ad.mean(("all", "particle_mass")) w1 = ad.quantities.weighted_average_quantity( ("all", "particle_mass"), ("all", "particle_ones") ) assert_equal(w, w1) w = ad.mean(("all", "particle_mass"), weight=("all", "particle_mass")) w1 = ad.quantities.weighted_average_quantity( ("all", "particle_mass"), ("all", "particle_mass") ) assert_equal(w, w1) # Projections p = ad.sum(("gas", "density"), axis=0) p1 = ds.proj(("gas", "density"), 0, data_source=ad, method="sum") assert_equal(p[("gas", "density")], p1[("gas", "density")]) # Check by axis-name p = ad.sum(("gas", "density"), axis="x") assert_equal(p[("gas", "density")], p1[("gas", "density")]) # Now we check proper projections p = ad.integrate(("gas", "density"), axis=0) p1 = ds.proj(("gas", "density"), 0, data_source=ad) assert_equal(p[("gas", "density")], p1[("gas", "density")]) # Check by axis-name p = ad.integrate(("gas", "density"), axis="x") assert_equal(p[("gas", "density")], p1[("gas", "density")])
from pyxsim import \ TableApecModel, XSpecThermalModel from yt.utilities.answer_testing.framework import \ GenericArrayTest from yt.testing import requires_module, fake_random_ds from numpy.testing import assert_allclose def setup(): from yt.config import ytcfg ytcfg["yt", "__withintesting"] = "True" ds = fake_random_ds(64) @requires_module("astropy") def test_apec(): amod = TableApecModel(0.1, 10.0, 10000, thermal_broad=True) amod.prepare_spectrum(0.2) acspec, amspec = amod.get_spectrum(6.0) spec = acspec + 0.3 * amspec spec2 = amod.return_spectrum(6.0, 0.3, 0.2, 1.0e-14) yield assert_allclose, spec.v, spec2.v def spec_test(): return spec.v
def test_projection(): fns = [] for nprocs in [8, 1]: # We want to test both 1 proc and 8 procs, to make sure that # parallelism isn't broken fields = ("density", "temperature", "velocity_x", "velocity_y", "velocity_z") units = ('g/cm**3', 'K', 'cm/s', 'cm/s', 'cm/s') ds = fake_random_ds(64, fields=fields, units=units, nprocs=nprocs, length_unit=LENGTH_UNIT) dims = ds.domain_dimensions xn, yn, zn = ds.domain_dimensions xi, yi, zi = ds.domain_left_edge.to_ndarray() + \ 1.0 / (ds.domain_dimensions * 2) xf, yf, zf = ds.domain_right_edge.to_ndarray() - \ 1.0 / (ds.domain_dimensions * 2) dd = ds.all_data() rho_tot = dd.quantities["TotalQuantity"]("density") coords = np.mgrid[xi:xf:xn*1j, yi:yf:yn*1j, zi:zf:zn*1j] uc = [np.unique(c) for c in coords] # test if projections inherit the field parameters of their data sources dd.set_field_parameter("bulk_velocity", np.array([0,1,2])) proj = ds.proj("density", 0, data_source=dd) yield assert_equal, dd.field_parameters["bulk_velocity"], \ proj.field_parameters["bulk_velocity"] # Some simple projection tests with single grids for ax, an in enumerate("xyz"): xax = ds.coordinates.x_axis[ax] yax = ds.coordinates.y_axis[ax] for wf in ['density', ("gas", "density"), None]: proj = ds.proj(["ones", "density"], ax, weight_field=wf) if wf is None: yield assert_equal, proj["ones"].sum(), LENGTH_UNIT*proj["ones"].size yield assert_equal, proj["ones"].min(), LENGTH_UNIT yield assert_equal, proj["ones"].max(), LENGTH_UNIT else: yield assert_equal, proj["ones"].sum(), proj["ones"].size yield assert_equal, proj["ones"].min(), 1.0 yield assert_equal, proj["ones"].max(), 1.0 yield assert_equal, np.unique(proj["px"]), uc[xax] yield assert_equal, np.unique(proj["py"]), uc[yax] yield assert_equal, np.unique(proj["pdx"]), 1.0/(dims[xax]*2.0) yield assert_equal, np.unique(proj["pdy"]), 1.0/(dims[yax]*2.0) plots = [proj.to_pw(fields='density'), proj.to_pw()] for pw in plots: for p in pw.plots.values(): tmpfd, tmpname = tempfile.mkstemp(suffix='.png') os.close(tmpfd) p.save(name=tmpname) fns.append(tmpname) frb = proj.to_frb((1.0, 'unitary'), 64) for proj_field in ['ones', 'density', 'temperature']: fi = ds._get_field_info(proj_field) yield assert_equal, frb[proj_field].info['data_source'], \ proj.__str__() yield assert_equal, frb[proj_field].info['axis'], \ ax yield assert_equal, frb[proj_field].info['field'], \ proj_field field_unit = Unit(fi.units) if wf is not None: yield assert_equal, frb[proj_field].units, \ Unit(field_unit, registry=ds.unit_registry) else: if frb[proj_field].units.is_code_unit: proj_unit = "code_length" else: proj_unit = "cm" if field_unit != '' and field_unit != Unit(): proj_unit = \ "({0}) * {1}".format(field_unit, proj_unit) yield assert_equal, frb[proj_field].units, \ Unit(proj_unit, registry=ds.unit_registry) yield assert_equal, frb[proj_field].info['xlim'], \ frb.bounds[:2] yield assert_equal, frb[proj_field].info['ylim'], \ frb.bounds[2:] yield assert_equal, frb[proj_field].info['center'], \ proj.center if wf is None: yield assert_equal, \ frb[proj_field].info['weight_field'], wf else: yield assert_equal, \ frb[proj_field].info['weight_field'], \ proj.data_source._determine_fields(wf)[0] # wf == None yield assert_equal, wf, None v1 = proj["density"].sum() v2 = (LENGTH_UNIT * dd["density"] * dd["d%s" % an]).sum() yield assert_rel_equal, v1, v2, 10 teardown_func(fns)
def test_simple_vr(self): ds = fake_random_ds(32) im, sc = yt.volume_render(ds, fname='test.png', sigma_clip=4.0) return im, sc
def setUp(self): if self.ds is None: self.ds = fake_random_ds(64) self.slc = SlicePlot(self.ds, 0, "density")
def test_smoothed_covering_grid_2d_dataset(): ds = fake_random_ds([32, 32, 1], nprocs=4) ds.periodicity = (True, True, True) scg = ds.smoothed_covering_grid(1, [0.0, 0.0, 0.0], [32, 32, 1]) assert_equal(scg["density"].shape, [32, 32, 1])
def test_covering_grid(): # We decompose in different ways for level in [0, 1, 2]: for nprocs in [1, 2, 4, 8]: ds = fake_random_ds(16, nprocs=nprocs) axis_name = ds.coordinates.axis_name dn = ds.refine_by ** level cg = ds.covering_grid(level, [0.0, 0.0, 0.0], dn * ds.domain_dimensions) # Test coordinate generation assert_equal(np.unique(cg[f"d{axis_name[0]}"]).size, 1) xmi = cg[axis_name[0]].min() xma = cg[axis_name[0]].max() dx = cg[f"d{axis_name[0]}"].flat[0:1] edges = ds.arr([[0, 1], [0, 1], [0, 1]], "code_length") assert_equal(xmi, edges[0, 0] + dx / 2.0) assert_equal(xmi, cg[axis_name[0]][0, 0, 0]) assert_equal(xmi, cg[axis_name[0]][0, 1, 1]) assert_equal(xma, edges[0, 1] - dx / 2.0) assert_equal(xma, cg[axis_name[0]][-1, 0, 0]) assert_equal(xma, cg[axis_name[0]][-1, 1, 1]) assert_equal(np.unique(cg[f"d{axis_name[1]}"]).size, 1) ymi = cg[axis_name[1]].min() yma = cg[axis_name[1]].max() dy = cg[f"d{axis_name[1]}"][0] assert_equal(ymi, edges[1, 0] + dy / 2.0) assert_equal(ymi, cg[axis_name[1]][0, 0, 0]) assert_equal(ymi, cg[axis_name[1]][1, 0, 1]) assert_equal(yma, edges[1, 1] - dy / 2.0) assert_equal(yma, cg[axis_name[1]][0, -1, 0]) assert_equal(yma, cg[axis_name[1]][1, -1, 1]) assert_equal(np.unique(cg[f"d{axis_name[2]}"]).size, 1) zmi = cg[axis_name[2]].min() zma = cg[axis_name[2]].max() dz = cg[f"d{axis_name[2]}"][0] assert_equal(zmi, edges[2, 0] + dz / 2.0) assert_equal(zmi, cg[axis_name[2]][0, 0, 0]) assert_equal(zmi, cg[axis_name[2]][1, 1, 0]) assert_equal(zma, edges[2, 1] - dz / 2.0) assert_equal(zma, cg[axis_name[2]][0, 0, -1]) assert_equal(zma, cg[axis_name[2]][1, 1, -1]) # Now we test other attributes assert_equal(cg["ones"].max(), 1.0) assert_equal(cg["ones"].min(), 1.0) assert_equal(cg["grid_level"], level) assert_equal(cg["cell_volume"].sum(), ds.domain_width.prod()) for g in ds.index.grids: di = g.get_global_startindex() dd = g.ActiveDimensions for i in range(dn): f = cg["density"][ dn * di[0] + i : dn * (di[0] + dd[0]) + i : dn, dn * di[1] + i : dn * (di[1] + dd[1]) + i : dn, dn * di[2] + i : dn * (di[2] + dd[2]) + i : dn, ] assert_equal(f, g["density"]) # More tests for cylindrical geometry for fn in [cyl_2d, cyl_3d]: ds = load(fn) ad = ds.all_data() upper_ad = ad.cut_region(["obj['z'] > 0"]) sp = ds.sphere((0, 0, 0), 0.5 * ds.domain_width[0], data_source=upper_ad) sp.quantities.total_mass()
def test_profiles(): ds = fake_random_ds(64, nprocs=8, fields=_fields, units=_units) nv = ds.domain_dimensions.prod() dd = ds.all_data() rt, tt, dt = dd.quantities["TotalQuantity"]( ["density", "temperature", "dinosaurs"]) e1, e2 = 0.9, 1.1 for nb in [8, 16, 32, 64]: for input_units in ["mks", "cgs"]: (rmi, rma), (tmi, tma), (dmi, dma) = [ getattr(ex, f"in_{input_units}")() for ex in dd.quantities["Extrema"] (["density", "temperature", "dinosaurs"]) ] # We log all the fields or don't log 'em all. No need to do them # individually. for lf in [True, False]: direct_profile = Profile1D(dd, "density", nb, rmi * e1, rma * e2, lf, weight_field=None) direct_profile.add_fields(["ones", "temperature"]) indirect_profile_s = create_profile( dd, "density", ["ones", "temperature"], n_bins=nb, extrema={"density": (rmi * e1, rma * e2)}, logs={"density": lf}, weight_field=None, ) indirect_profile_t = create_profile( dd, ("gas", "density"), [("index", "ones"), ("gas", "temperature")], n_bins=nb, extrema={"density": (rmi * e1, rma * e2)}, logs={"density": lf}, weight_field=None, ) for p1d in [ direct_profile, indirect_profile_s, indirect_profile_t ]: assert_equal(p1d["index", "ones"].sum(), nv) assert_rel_equal(tt, p1d["gas", "temperature"].sum(), 7) p2d = Profile2D( dd, "density", nb, rmi * e1, rma * e2, lf, "temperature", nb, tmi * e1, tma * e2, lf, weight_field=None, ) p2d.add_fields(["ones", "temperature"]) assert_equal(p2d["ones"].sum(), nv) assert_rel_equal(tt, p2d["temperature"].sum(), 7) p3d = Profile3D( dd, "density", nb, rmi * e1, rma * e2, lf, "temperature", nb, tmi * e1, tma * e2, lf, "dinosaurs", nb, dmi * e1, dma * e2, lf, weight_field=None, ) p3d.add_fields(["ones", "temperature"]) assert_equal(p3d["ones"].sum(), nv) assert_rel_equal(tt, p3d["temperature"].sum(), 7) p1d = Profile1D(dd, "x", nb, 0.0, 1.0, False, weight_field=None) p1d.add_fields("ones") av = nv / nb assert_equal(p1d["ones"], np.ones(nb) * av) # We re-bin ones with a weight now p1d = Profile1D(dd, "x", nb, 0.0, 1.0, False, weight_field="temperature") p1d.add_fields(["ones"]) assert_equal(p1d["ones"], np.ones(nb)) # Verify we can access "ones" after adding a new field # See issue 988 p1d.add_fields(["density"]) assert_equal(p1d["ones"], np.ones(nb)) p2d = Profile2D(dd, "x", nb, 0.0, 1.0, False, "y", nb, 0.0, 1.0, False, weight_field=None) p2d.add_fields("ones") av = nv / nb**2 assert_equal(p2d["ones"], np.ones((nb, nb)) * av) # We re-bin ones with a weight now p2d = Profile2D( dd, "x", nb, 0.0, 1.0, False, "y", nb, 0.0, 1.0, False, weight_field="temperature", ) p2d.add_fields(["ones"]) assert_equal(p2d["ones"], np.ones((nb, nb))) p3d = Profile3D( dd, "x", nb, 0.0, 1.0, False, "y", nb, 0.0, 1.0, False, "z", nb, 0.0, 1.0, False, weight_field=None, ) p3d.add_fields("ones") av = nv / nb**3 assert_equal(p3d["ones"], np.ones((nb, nb, nb)) * av) # We re-bin ones with a weight now p3d = Profile3D( dd, "x", nb, 0.0, 1.0, False, "y", nb, 0.0, 1.0, False, "z", nb, 0.0, 1.0, False, weight_field="temperature", ) p3d.add_fields(["ones"]) assert_equal(p3d["ones"], np.ones((nb, nb, nb))) p2d = create_profile( dd, ("gas", "density"), ("gas", "temperature"), weight_field=("gas", "cell_mass"), extrema={"density": (None, rma * e2)}, ) assert_equal(p2d.x_bins[0], rmi - np.spacing(rmi)) assert_equal(p2d.x_bins[-1], rma * e2) assert str(ds.field_info["gas", "cell_mass"].units) == str(p2d.weight.units) p2d = create_profile( dd, ("gas", "density"), ("gas", "temperature"), weight_field=("gas", "cell_mass"), extrema={"density": (rmi * e2, None)}, ) assert_equal(p2d.x_bins[0], rmi * e2) assert_equal(p2d.x_bins[-1], rma + np.spacing(rma))
def test_offaxis_slice_plot(self): test_ds = fake_random_ds(16) slc = OffAxisSlicePlot(test_ds, [1, 1, 1], "density") for fname in TEST_FLNMS: assert_fname(slc.save(fname)[0])
def test_checksum(): assert fake_random_ds(16).checksum == "notafile" assert data_dir_load(g30).checksum == "6169536e4b9f737ce3d3ad440df44c58"