Exemple #1
0
def test_mag_factor():
    ds1 = load(sloshing,
               units_override=uo_sloshing,
               magnetic_normalization="gaussian")
    assert ds1.magnetic_unit == np.sqrt(4.0 * np.pi * ds1.mass_unit /
                                        (ds1.time_unit**2 * ds1.length_unit))
    sp1 = ds1.sphere("c", (100.0, "kpc"))
    pB1a = (sp1["athena", "cell_centered_B_x"]**2 +
            sp1["athena", "cell_centered_B_y"]**2 +
            sp1["athena", "cell_centered_B_z"]**2) / (8.0 * np.pi)
    pB1b = (sp1["gas", "magnetic_field_x"]**2 + sp1["gas", "magnetic_field_y"]
            **2 + sp1["gas", "magnetic_field_z"]**2) / (8.0 * np.pi)
    pB1a.convert_to_units("dyn/cm**2")
    pB1b.convert_to_units("dyn/cm**2")
    assert_allclose_units(pB1a, pB1b)
    assert_allclose_units(pB1a, sp1["magnetic_pressure"])
    ds2 = load(sloshing,
               units_override=uo_sloshing,
               magnetic_normalization="lorentz_heaviside")
    assert ds2.magnetic_unit == np.sqrt(ds2.mass_unit /
                                        (ds2.time_unit**2 * ds2.length_unit))
    sp2 = ds2.sphere("c", (100.0, "kpc"))
    pB2a = (sp2["athena", "cell_centered_B_x"]**2 +
            sp2["athena", "cell_centered_B_y"]**2 +
            sp2["athena", "cell_centered_B_z"]**2) / 2.0
    pB2b = (sp2["gas", "magnetic_field_x"]**2 + sp2["gas", "magnetic_field_y"]
            **2 + sp2["gas", "magnetic_field_z"]**2) / 2.0
    pB2a.convert_to_units("dyn/cm**2")
    pB2b.convert_to_units("dyn/cm**2")
    assert_allclose_units(pB2a, pB2b)
    assert_allclose_units(pB2a, sp2["magnetic_pressure"])
    assert_allclose_units(pB1a, pB2a)
Exemple #2
0
def test_rockstar():
    from mpi4py import MPI

    tmpdir = tempfile.mkdtemp()
    curdir = os.getcwd()
    os.chdir(tmpdir)

    filename = os.path.join(os.path.dirname(__file__),
                            "run_rockstar.py")
    comm = MPI.COMM_SELF.Spawn(sys.executable,
                               args=[filename],
                               maxprocs=3)
    comm.Disconnect()

    h1 = "rockstar_halos/halos_0.0.bin"
    d1 = load(h1)
    d1.parameters['format_revision'] = 2
    for field in _fields:
        yield FieldValuesTest(d1, field, particle_type=True, decimals=1)
    h2 = "rockstar_halos/halos_1.0.bin"
    d2 = load(h2)
    d2.parameters['format_revision'] = 2
    for field in _fields:
        yield FieldValuesTest(d2, field, particle_type=True, decimals=1)

    os.chdir(curdir)
    shutil.rmtree(tmpdir)
Exemple #3
0
def test_nprocs():
    ds1 = load(sloshing, units_override=uo_sloshing)
    sp1 = ds1.sphere("c", (100.0, "kpc"))
    prj1 = ds1.proj(("gas", "density"), 0)
    ds2 = load(sloshing, units_override=uo_sloshing, nprocs=8)
    sp2 = ds2.sphere("c", (100.0, "kpc"))
    prj2 = ds1.proj(("gas", "density"), 0)

    assert_equal(
        sp1.quantities.extrema(("gas", "pressure")),
        sp2.quantities.extrema(("gas", "pressure")),
    )
    assert_allclose_units(
        sp1.quantities.total_quantity(("gas", "pressure")),
        sp2.quantities.total_quantity(("gas", "pressure")),
    )
    for ax in "xyz":
        assert_equal(
            sp1.quantities.extrema(("gas", f"velocity_{ax}")),
            sp2.quantities.extrema(("gas", f"velocity_{ax}")),
        )
    assert_allclose_units(
        sp1.quantities.bulk_velocity(), sp2.quantities.bulk_velocity()
    )
    assert_equal(prj1[("gas", "density")], prj2[("gas", "density")])
Exemple #4
0
def test_line_integral_convolution_callback():
    with _cleanup_fname() as prefix:
        ds = fake_amr_ds(
            fields=("density", "velocity_x", "velocity_y", "velocity_z"),
            units=("g/cm**3", "cm/s", "cm/s", "cm/s"),
        )
        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.0,
            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", "magnetic_field_strength")
        slc.annotate_line_integral_convolution("magnetic_field_r",
                                               "magnetic_field_z")
        assert_fname(slc.save(prefix)[0])

    with _cleanup_fname() as prefix:
        ds = load(cyl_3d)
        slc = SlicePlot(ds, "r", "magnetic_field_strength")
        slc.annotate_line_integral_convolution("magnetic_field_theta",
                                               "magnetic_field_z")
        assert_fname(slc.save(prefix)[0])
        slc = SlicePlot(ds, "z", "magnetic_field_strength")
        slc.annotate_line_integral_convolution("magnetic_field_cartesian_x",
                                               "magnetic_field_cartesian_y")
        assert_fname(slc.save(prefix)[0])
        slc = SlicePlot(ds, "theta", "magnetic_field_strength")
        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"),
            units=("g/cm**3", "cm/s", "cm/s", "cm/s"),
            geometry="spherical",
        )
        p = SlicePlot(ds, "r", "density")
        p.annotate_line_integral_convolution("velocity_theta", "velocity_phi")
        assert_raises(YTDataTypeUnsupported, p.save, prefix)
Exemple #5
0
def test_nonspatial_data():
    tmpdir = make_tempdir()
    curdir = os.getcwd()
    os.chdir(tmpdir)
    ds = data_dir_load(enzotiny)
    region = ds.box([0.25] * 3, [0.75] * 3)
    sphere = ds.sphere(ds.domain_center, (10, "Mpc"))
    my_data = {}
    my_data["region_density"] = region[("gas", "density")]
    my_data["sphere_density"] = sphere[("gas", "density")]
    fn = "test_data.h5"
    save_as_dataset(ds, fn, my_data)
    full_fn = os.path.join(tmpdir, fn)
    array_ds = load(full_fn)
    compare_unit_attributes(ds, array_ds)
    assert isinstance(array_ds, YTNonspatialDataset)
    yield YTDataFieldTest(full_fn, "region_density", geometric=False)
    yield YTDataFieldTest(full_fn, "sphere_density", geometric=False)

    my_data = {"density": YTArray(np.linspace(1.0, 20.0, 10), "g/cm**3")}
    fake_ds = {"current_time": YTQuantity(10, "Myr")}
    fn = "random_data.h5"
    save_as_dataset(fake_ds, fn, my_data)
    full_fn = os.path.join(tmpdir, fn)
    new_ds = load(full_fn)
    assert isinstance(new_ds, YTNonspatialDataset)
    yield YTDataFieldTest(full_fn, ("data", "density"), geometric=False)
    os.chdir(curdir)
    if tmpdir != ".":
        shutil.rmtree(tmpdir)
Exemple #6
0
def test_nprocs():
    ds1 = load(sloshing, units_override=uo_sloshing)
    sp1 = ds1.sphere("c", (100.0, "kpc"))
    prj1 = ds1.proj("density", 0)
    ds2 = load(sloshing, units_override=uo_sloshing, nprocs=8)
    sp2 = ds2.sphere("c", (100.0, "kpc"))
    prj2 = ds1.proj("density", 0)

    ds3 = load(sloshing, parameters=uo_sloshing)
    assert_equal(ds3.length_unit, 1.0 * u.Mpc)
    assert_equal(ds3.time_unit, 1.0 * u.Myr)
    assert_equal(ds3.mass_unit, 1e14 * u.Msun)

    assert_equal(sp1.quantities.extrema("pressure"),
                 sp2.quantities.extrema("pressure"))
    assert_allclose_units(
        sp1.quantities.total_quantity("pressure"),
        sp2.quantities.total_quantity("pressure"),
    )
    for ax in "xyz":
        assert_equal(
            sp1.quantities.extrema(f"velocity_{ax}"),
            sp2.quantities.extrema(f"velocity_{ax}"),
        )
    assert_allclose_units(sp1.quantities.bulk_velocity(),
                          sp2.quantities.bulk_velocity())
    assert_equal(prj1["density"], prj2["density"])
Exemple #7
0
def test_plot_data():
    tmpdir = tempfile.mkdtemp()
    curdir = os.getcwd()
    os.chdir(tmpdir)
    ds = fake_random_ds(16)

    plot = SlicePlot(ds, "z", ("gas", "density"))
    fn = plot.data_source.save_as_dataset("slice.h5")
    ds_slice = load(fn)
    p = SlicePlot(ds_slice, "z", ("gas", "density"))
    fn = p.save()
    assert_fname(fn[0])

    plot = ProjectionPlot(ds, "z", ("gas", "density"))
    fn = plot.data_source.save_as_dataset("proj.h5")
    ds_proj = load(fn)
    p = ProjectionPlot(ds_proj, "z", ("gas", "density"))
    fn = p.save()
    assert_fname(fn[0])

    plot = SlicePlot(ds, [1, 1, 1], ("gas", "density"))
    fn = plot.data_source.save_as_dataset("oas.h5")
    ds_oas = load(fn)
    p = SlicePlot(ds_oas, [1, 1, 1], ("gas", "density"))
    fn = p.save()
    assert_fname(fn[0])

    os.chdir(curdir)
    if tmpdir != ".":
        shutil.rmtree(tmpdir)
Exemple #8
0
def test_velocity_callback():
    with _cleanup_fname() as prefix:
        ds = fake_amr_ds(
            fields=("density", "velocity_x", "velocity_y", "velocity_z"),
            units=("g/cm**3", "cm/s", "cm/s", "cm/s"),
        )
        for ax in "xyz":
            p = ProjectionPlot(ds,
                               ax, ("gas", "density"),
                               weight_field=("gas", "density"))
            p.annotate_velocity()
            assert_fname(p.save(prefix)[0])
            p = SlicePlot(ds, ax, ("gas", "density"))
            p.annotate_velocity()
            assert_fname(p.save(prefix)[0])
        # Test for OffAxis Slice
        p = SlicePlot(ds, [1, 1, 0], ("gas", "density"),
                      north_vector=[0, 0, 1])
        p.annotate_velocity(factor=40, normalize=True)
        assert_fname(p.save(prefix)[0])
        # Now we'll check a few additional minor things
        p = SlicePlot(ds, "x", ("gas", "density"))
        p.annotate_velocity(factor=8,
                            scale=0.5,
                            scale_units="inches",
                            normalize=True)
        assert_fname(p.save(prefix)[0])

    with _cleanup_fname() as prefix:
        ds = fake_hexahedral_ds(fields=[f"velocity_{ax}" for ax in "xyz"])
        sl = SlicePlot(ds, 1, ("connect1", "test"))
        sl.annotate_velocity()
        assert_fname(sl.save(prefix)[0])

    with _cleanup_fname() as prefix:
        ds = load(cyl_2d)
        slc = SlicePlot(ds, "theta", ("gas", "velocity_magnitude"))
        slc.annotate_velocity()
        assert_fname(slc.save(prefix)[0])

    with _cleanup_fname() as prefix:
        ds = load(cyl_3d)
        for ax in ["r", "z", "theta"]:
            slc = SlicePlot(ds, ax, ("gas", "velocity_magnitude"))
            slc.annotate_velocity()
            assert_fname(slc.save(prefix)[0])
            slc = ProjectionPlot(ds, ax, ("gas", "velocity_magnitude"))
            slc.annotate_velocity()
            assert_fname(slc.save(prefix)[0])

    with _cleanup_fname() as prefix:
        ds = fake_amr_ds(
            fields=("density", "velocity_r", "velocity_theta", "velocity_phi"),
            units=("g/cm**3", "cm/s", "cm/s", "cm/s"),
            geometry="spherical",
        )
        p = ProjectionPlot(ds, "r", ("gas", "density"))
        p.annotate_velocity(factor=40, normalize=True)
        assert_raises(YTDataTypeUnsupported, p.save, prefix)
Exemple #9
0
def test_profile_data():
    tmpdir = make_tempdir()
    curdir = os.getcwd()
    os.chdir(tmpdir)
    ds = data_dir_load(enzotiny)
    ad = ds.all_data()
    profile_1d = create_profile(ad,
                                "density",
                                "temperature",
                                weight_field="cell_mass")
    fn = profile_1d.save_as_dataset()
    full_fn = os.path.join(tmpdir, fn)
    prof_1d_ds = load(full_fn)
    compare_unit_attributes(ds, prof_1d_ds)
    assert isinstance(prof_1d_ds, YTProfileDataset)

    for field in profile_1d.standard_deviation:
        assert_array_equal(
            profile_1d.standard_deviation[field],
            prof_1d_ds.profile.standard_deviation["data", field[1]],
        )

    p1 = ProfilePlot(prof_1d_ds.data,
                     "density",
                     "temperature",
                     weight_field="cell_mass")
    p1.save()

    yield YTDataFieldTest(full_fn, "temperature", geometric=False)
    yield YTDataFieldTest(full_fn, "x", geometric=False)
    yield YTDataFieldTest(full_fn, "density", geometric=False)
    profile_2d = create_profile(
        ad,
        ["density", "temperature"],
        "cell_mass",
        weight_field=None,
        n_bins=(128, 128),
    )
    fn = profile_2d.save_as_dataset()
    full_fn = os.path.join(tmpdir, fn)
    prof_2d_ds = load(full_fn)
    compare_unit_attributes(ds, prof_2d_ds)
    assert isinstance(prof_2d_ds, YTProfileDataset)

    p2 = PhasePlot(prof_2d_ds.data,
                   "density",
                   "temperature",
                   "cell_mass",
                   weight_field=None)
    p2.save()

    yield YTDataFieldTest(full_fn, "density", geometric=False)
    yield YTDataFieldTest(full_fn, "x", geometric=False)
    yield YTDataFieldTest(full_fn, "temperature", geometric=False)
    yield YTDataFieldTest(full_fn, "y", geometric=False)
    yield YTDataFieldTest(full_fn, "cell_mass", geometric=False)
    os.chdir(curdir)
    if tmpdir != ".":
        shutil.rmtree(tmpdir)
Exemple #10
0
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 = SlicePlot(ds, "theta", "magnetic_field_strength")
        slc.annotate_magnetic_field()
        assert_fname(slc.save(prefix)[0])

    with _cleanup_fname() as prefix:
        ds = load(cyl_3d)
        for ax in ["r", "z", "theta"]:
            slc = SlicePlot(ds, ax, "magnetic_field_strength")
            slc.annotate_magnetic_field()
            assert_fname(slc.save(prefix)[0])
            slc = ProjectionPlot(ds, ax, "magnetic_field_strength")
            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)
Exemple #11
0
def _fix_ds(arg, *args, **kwargs):
    if os.path.isdir(f"{arg}") and os.path.exists(f"{arg}/{arg}"):
        ds = load(f"{arg}/{arg}", *args, **kwargs)
    elif os.path.isdir(f"{arg}.dir") and os.path.exists(f"{arg}.dir/{arg}"):
        ds = load(f"{arg}.dir/{arg}", *args, **kwargs)
    elif arg.endswith(".index"):
        ds = load(arg[:-10], *args, **kwargs)
    else:
        ds = load(arg, *args, **kwargs)
    return ds
Exemple #12
0
    def _load(self, output_fn, **kwargs):
        from yt.loaders import load

        if self._dataset_cls is not None:
            return self._dataset_cls(output_fn, **kwargs)
        elif self._mixed_dataset_types:
            return load(output_fn, **kwargs)
        ds = load(output_fn, **kwargs)
        self._dataset_cls = ds.__class__
        return ds
Exemple #13
0
def test_magnetic_units():
    ds1 = load(plasma)
    assert_allclose(ds1.magnetic_unit.value, 1.0)
    assert str(ds1.magnetic_unit.units) == "T"
    mag_unit1 = ds1.magnetic_unit.to("code_magnetic")
    assert_allclose(mag_unit1.value, 1.0)
    assert str(mag_unit1.units) == "code_magnetic"
    ds2 = load(plasma, unit_system="cgs")
    assert_allclose(ds2.magnetic_unit.value, 1.0e4)
    assert str(ds2.magnetic_unit.units) == "G"
    mag_unit2 = ds2.magnetic_unit.to("code_magnetic")
    assert_allclose(mag_unit2.value, 1.0)
    assert str(mag_unit2.units) == "code_magnetic"
Exemple #14
0
def test_load_ambiguous_data(tmp_path):
    with pytest.raises(YTAmbiguousDataType):
        load(tmp_path)

    file = tmp_path / "fake_datafile0011.dump"
    file.touch()

    pattern = str(tmp_path / "fake_datafile00??.dump")

    # loading a DatasetSeries should not crash until an item is retrieved
    ts = load(pattern)
    with pytest.raises(YTAmbiguousDataType):
        ts[0]
Exemple #15
0
def test_magnetic_units():
    ds1 = load(sloshing)
    assert_allclose(ds1.magnetic_unit.value, np.sqrt(4.0 * np.pi))
    assert str(ds1.magnetic_unit.units) == "G"
    mag_unit1 = ds1.magnetic_unit.to("code_magnetic")
    assert_allclose(mag_unit1.value, 1.0)
    assert str(mag_unit1.units) == "code_magnetic"
    ds2 = load(sloshing, unit_system="mks")
    assert_allclose(ds2.magnetic_unit.value, np.sqrt(4.0 * np.pi) * 1.0e-4)
    assert str(ds2.magnetic_unit.units) == "T"
    mag_unit2 = ds2.magnetic_unit.to("code_magnetic")
    assert_allclose(mag_unit2.value, 1.0)
    assert str(mag_unit2.units) == "code_magnetic"
Exemple #16
0
    def test_halo_finders(self):
        from mpi4py import MPI

        filename = os.path.join(os.path.dirname(__file__), "run_halo_finder_ts.py")
        for method in methods:
            comm = MPI.COMM_SELF.Spawn(
                sys.executable,
                args=[filename, method, self.tmpdir],
                maxprocs=methods[method],
            )
            comm.Disconnect()

            if method == "rockstar":
                hcfns = [f"halos_{i}.0.bin" for i in range(2)]
            else:
                hcfns = [
                    os.path.join(f"DD{i:04d}", f"DD{i:04d}.0.h5") for i in [20, 46]
                ]

            for hcfn in hcfns:
                fn = os.path.join(self.tmpdir, "halo_catalogs", method, hcfn)

                ds = load(fn)
                if method == "rockstar":
                    ds.parameters["format_revision"] = 2
                    ds_type = RockstarDataset
                else:
                    ds_type = YTHaloCatalogDataset
                assert isinstance(ds, ds_type)
Exemple #17
0
def test_region_chunked_read():
    # see #2104
    ds = load("IsolatedGalaxy/galaxy0030/galaxy0030")

    sp = ds.sphere((0.5, 0.5, 0.5), (2, "kpc"))
    dense_sp = sp.cut_region(['obj["H_p0_number_density"]>= 1e-2'])
    dense_sp.quantities.angular_momentum_vector()
def test_halo_analysis_finders():
    from mpi4py import MPI

    curdir = os.getcwd()
    filename = os.path.join(os.path.dirname(__file__), "run_halo_finder.py")
    for method in methods:
        tmpdir = tempfile.mkdtemp()
        os.chdir(tmpdir)
        comm = MPI.COMM_SELF.Spawn(sys.executable,
                                   args=[filename, method, tmpdir],
                                   maxprocs=methods[method])
        comm.Disconnect()

        fn = os.path.join(tmpdir, "halo_catalogs", method, "%s.0.h5" % method)
        ds = load(fn)
        if method == "rockstar":
            ds.parameters['format_revision'] = 2
        assert isinstance(ds, YTHaloCatalogDataset)
        for field in _fields:
            yield FieldValuesTest(ds,
                                  field,
                                  particle_type=True,
                                  decimals=decimals[method])

        os.chdir(curdir)
        shutil.rmtree(tmpdir)
Exemple #19
0
def test_set_units():
    ds = load(tgal)
    sp = ds.sphere("max", (1.0, "Mpc"))
    pp = ParticlePhasePlot(sp, ("Gas", "density"), ("Gas", "temperature"),
                           ("Gas", "particle_mass"))
    # make sure we can set the units using the tuple without erroring out
    pp.set_unit(("Gas", "particle_mass"), "Msun")
    def run(self):
        curdir = os.getcwd()
        tmpdir = tempfile.mkdtemp()
        os.chdir(tmpdir)

        dds = data_dir_load(self.data_ds_fn)
        hds = data_dir_load(self.halos_ds_fn)
        hc = HaloCatalog(data_ds=dds,
                         halos_ds=hds,
                         output_dir=os.path.join(tmpdir, str(dds)))
        hc.add_callback("sphere")
        hc.add_quantity("nstars")
        hc.create()

        fn = os.path.join(tmpdir, str(dds), "%s.0.h5" % str(dds))
        ds = load(fn)
        ad = ds.all_data()
        mi, ma = ad.quantities.extrema("nstars")
        mean = ad.quantities.weighted_average_quantity("nstars",
                                                       "particle_ones")

        os.chdir(curdir)
        shutil.rmtree(tmpdir)

        return np.array([mean, mi, ma])
    def _check_for_outputs(self, potential_outputs):
        r"""
        Check a list of files to see if they are valid datasets.
        """

        only_on_root(mylog.info, "Checking %d potential outputs.",
                     len(potential_outputs))

        my_outputs = {}
        for my_storage, output in parallel_objects(potential_outputs,
                                                   storage=my_outputs):
            try:
                ds = load(output)
            except (FileNotFoundError, YTUnidentifiedDataType):
                mylog.error("Failed to load %s", output)
                continue
            my_storage.result = {
                "filename": output,
                "time": ds.current_time.in_units("s"),
            }
            if ds.cosmological_simulation:
                my_storage.result["redshift"] = ds.current_redshift

        my_outputs = [
            my_output for my_output in my_outputs.values()
            if my_output is not None
        ]
        return my_outputs
Exemple #22
0
def test_groupBased_out():
    dss = load(groupBased)
    particle_types = ("all", "io", "nbody")
    field_list = list(product(particle_types, particle_fields))
    field_list += [
        ("openPMD", "J_x"),
        ("openPMD", "J_y"),
        ("openPMD", "J_z"),
        ("openPMD", "e-chargeDensity"),
    ]
    domain_dimensions = [32, 64, 64] * np.ones_like(dss[0].domain_dimensions)
    domain_width = [0.0002752, 0.0005504, 0.0005504] * np.ones_like(
        dss[0].domain_left_edge)

    assert_equal(len(dss), 101)
    for i in range(0, len(dss),
                   20):  # Test only every 20th ds out of the series
        ds = dss[i]
        assert_equal(str(ds), "simData.h5")
        assert_equal(ds.dimensionality, 3)
        assert_equal(ds.particle_types_raw, ("io", ))
        assert_array_equal(ds.field_list, field_list)
        assert_array_equal(ds.domain_dimensions, domain_dimensions)
        assert ds.current_time >= np.zeros_like(ds.current_time)
        assert ds.current_time <= 1.6499999999999998e-12 * np.ones_like(
            ds.current_time)
        assert_almost_equal(ds.domain_right_edge - ds.domain_left_edge,
                            domain_width)
Exemple #23
0
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 = load(cyl_2d)
        slc = SlicePlot(ds, "theta", "density")
        slc.annotate_cell_edges()
        assert_fname(slc.save(prefix)[0])

    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)
Exemple #24
0
    def _check_for_outputs(self, potential_outputs):
        r"""
        Check a list of files to see if they are valid datasets.
        """

        only_on_root(mylog.info, "Checking %d potential outputs.",
                     len(potential_outputs))

        my_outputs = {}
        llevel = mylog.level
        # suppress logging as we load every dataset, unless set to debug
        if llevel > 10 and llevel < 40:
            mylog.setLevel(40)
        for my_storage, output in parallel_objects(potential_outputs,
                                                   storage=my_outputs):
            if os.path.exists(output):
                try:
                    ds = load(output)
                    if ds is not None:
                        my_storage.result = {
                            "filename": output,
                            "time": ds.current_time.in_units("s")
                        }
                        if ds.cosmological_simulation:
                            my_storage.result["redshift"] = ds.current_redshift
                except YTOutputNotIdentified:
                    mylog.error("Failed to load %s", output)
        mylog.setLevel(llevel)
        my_outputs = [my_output for my_output in my_outputs.values() \
                      if my_output is not None]
        return my_outputs
Exemple #25
0
    def test_absorption_spectrum_non_cosmo_sph(self):
        """
        This test generates an absorption spectrum from a simple light ray on a
        particle dataset
        """

        ds = load(GIZMO_PLUS_SINGLE)
        lr = LightRay(ds)
        ray_start = ds.domain_left_edge
        ray_end = ds.domain_right_edge
        lr.make_light_ray(start_position=ray_start, end_position=ray_end,
                          fields=[('gas', 'temperature'),
                                  ('gas', 'H_p0_number_density')],
                          data_filename='lightray.h5')

        sp = AbsorptionSpectrum(1200.0, 1300.0, 10001)

        my_label = 'HI Lya'
        field = ('gas', 'H_p0_number_density')
        wavelength = 1215.6700  # Angstromss
        f_value = 4.164E-01
        gamma = 6.265e+08
        mass = 1.00794

        sp.add_line(my_label, field, wavelength, f_value,
                    gamma, mass, label_threshold=1.e10)

        filename = "spectrum.h5"
        wavelength, flux = sp.make_spectrum('lightray.h5',
                                            output_file=filename,
                                            output_absorbers_file='lines.txt',
                                            use_peculiar_velocity=True)
        return filename
Exemple #26
0
def test_grid_datacontainer_data():
    tmpdir = make_tempdir()
    curdir = os.getcwd()
    os.chdir(tmpdir)
    ds = data_dir_load(enzotiny)

    cg = ds.covering_grid(level=0, left_edge=[0.25] * 3, dims=[16] * 3)
    fn = cg.save_as_dataset(fields=[
        ("gas", "density"),
        ("all", "particle_mass"),
        ("all", "particle_position"),
    ])
    full_fn = os.path.join(tmpdir, fn)
    cg_ds = load(full_fn)
    compare_unit_attributes(ds, cg_ds)
    assert isinstance(cg_ds, YTGridDataset)
    assert (
        cg["all",
           "particle_position"].shape == cg_ds.r["all",
                                                 "particle_position"].shape)
    yield YTDataFieldTest(full_fn, ("grid", "density"))
    yield YTDataFieldTest(full_fn, ("all", "particle_mass"))

    ag = ds.arbitrary_grid(left_edge=[0.25] * 3,
                           right_edge=[0.75] * 3,
                           dims=[16] * 3)
    fn = ag.save_as_dataset(fields=[("gas", "density"), ("all",
                                                         "particle_mass")])
    full_fn = os.path.join(tmpdir, fn)
    ag_ds = load(full_fn)
    compare_unit_attributes(ds, ag_ds)
    assert isinstance(ag_ds, YTGridDataset)
    yield YTDataFieldTest(full_fn, ("grid", "density"))
    yield YTDataFieldTest(full_fn, ("all", "particle_mass"))

    my_proj = ds.proj(("gas", "density"), "x", weight_field=("gas", "density"))
    frb = my_proj.to_frb(1.0, (800, 800))
    fn = frb.save_as_dataset(fields=[("gas", "density")])
    frb_ds = load(fn)
    assert_array_equal(frb[("gas", "density")],
                       frb_ds.data[("gas", "density")])
    compare_unit_attributes(ds, frb_ds)
    assert isinstance(frb_ds, YTGridDataset)
    yield YTDataFieldTest(full_fn, ("grid", "density"), geometric=False)
    os.chdir(curdir)
    if tmpdir != ".":
        shutil.rmtree(tmpdir)
Exemple #27
0
def can_run_ds(ds_fn, file_check=False):
    r"""
    Validates whether or not a given input can be loaded and used as a
    Dataset object.
    """
    if isinstance(ds_fn, Dataset):
        return True
    path = ytcfg.get("yt", "test_data_dir")
    if not os.path.isdir(path):
        return False
    if file_check:
        return os.path.isfile(os.path.join(path, ds_fn))
    try:
        load(ds_fn)
        return True
    except FileNotFoundError:
        return False
Exemple #28
0
def test_load_ambiguous_data_with_hint(hint, expected_type, tmp_path):
    ds = load(tmp_path, hint=hint)
    assert type(ds).__name__ == expected_type

    file1 = tmp_path / "fake_datafile0011.dump"
    file2 = tmp_path / "fake_datafile0022.dump"
    file1.touch()
    file2.touch()

    pattern = str(tmp_path / "fake_datafile00??.dump")

    ts = load(pattern, hint=hint)
    ds = ts[0]
    assert type(ds).__name__ == expected_type

    ds = ts[1]
    assert type(ds).__name__ == expected_type
Exemple #29
0
def test_default_to_cartesian():
    data = {"density": np.random.random(128)}
    ds_attrs = {"current_time": YTQuantity(10, "Myr")}
    with TemporaryDirectory() as tmpdir:
        tmpf = os.path.join(tmpdir, "savefile.h5")
        fn = save_as_dataset(ds_attrs, tmpf, data)
        ds2 = load(fn)
    assert ds2.geometry == "cartesian"
Exemple #30
0
def can_run_ds(ds_fn, file_check=False):
    result_storage = AnswerTestingTest.result_storage
    if isinstance(ds_fn, Dataset):
        return result_storage is not None
    path = ytcfg.get("yt", "test_data_dir")
    if not os.path.isdir(path):
        return False
    if file_check:
        return os.path.isfile(os.path.join(path, ds_fn)) and result_storage is not None
    try:
        load(ds_fn)
    except FileNotFoundError:
        if ytcfg.get("yt", "internals", "strict_requires"):
            if result_storage is not None:
                result_storage["tainted"] = True
            raise
        return False
    return result_storage is not None