Example #1
0
def test_export_astropy():
    from yt.units.yt_array import YTArray

    ds = fake_random_ds(64)
    ad = ds.all_data()
    prof = ad.profile(
        ("index", "radius"),
        [("gas", "density"), ("gas", "velocity_x")],
        weight_field=("index", "ones"),
        n_bins=32,
    )
    # export to AstroPy table
    at1 = prof.to_astropy_table()
    assert "radius" in at1.colnames
    assert "density" in at1.colnames
    assert "velocity_x" in at1.colnames
    assert_equal(prof.x.d, at1["radius"].value)
    assert_equal(prof[("gas", "density")].d, at1["density"].value)
    assert_equal(prof[("gas", "velocity_x")].d, at1["velocity_x"].value)
    assert prof.x.units == YTArray.from_astropy(at1["radius"]).units
    assert prof[("gas", "density")].units == YTArray.from_astropy(
        at1["density"]).units
    assert (prof[("gas", "velocity_x")].units == YTArray.from_astropy(
        at1["velocity_x"]).units)
    assert np.all(at1.mask["density"] == prof.used)
    at2 = prof.to_astropy_table(fields=("gas", "density"), only_used=True)
    assert "radius" in at2.colnames
    assert "velocity_x" not in at2.colnames
    assert_equal(prof.x.d[prof.used], at2["radius"].value)
    assert_equal(prof[("gas", "density")].d[prof.used], at2["density"].value)
Example #2
0
def test_export_astropy():
    from yt.units.yt_array import YTArray
    ds = fake_random_ds(64)
    ad = ds.all_data()
    prof = ad.profile('radius', [('gas', 'density'), ('gas', 'velocity_x')],
                      weight_field=('index', 'ones'),
                      n_bins=32)
    # export to AstroPy table
    at1 = prof.to_astropy_table()
    assert 'radius' in at1.colnames
    assert 'density' in at1.colnames
    assert 'velocity_x' in at1.colnames
    assert_equal(prof.x.d, at1["radius"].value)
    assert_equal(prof["density"].d, at1["density"].value)
    assert_equal(prof["velocity_x"].d, at1["velocity_x"].value)
    assert prof.x.units == YTArray.from_astropy(at1["radius"]).units
    assert prof["density"].units == YTArray.from_astropy(at1["density"]).units
    assert prof["velocity_x"].units == YTArray.from_astropy(
        at1["velocity_x"]).units
    assert np.all(at1.mask['density'] == prof.used)
    at2 = prof.to_astropy_table(fields="density", only_used=True)
    assert 'radius' in at2.colnames
    assert 'velocity_x' not in at2.colnames
    assert_equal(prof.x.d[prof.used], at2["radius"].value)
    assert_equal(prof["density"].d[prof.used], at2["density"].value)
Example #3
0
 def test_to_astropy_table(self):
     from yt.units.yt_array import YTArray
     fields = ["density", "velocity_z"]
     ds = fake_random_ds(6)
     dd = ds.all_data()
     at1 = dd.to_astropy_table(fields)
     assert_array_equal(dd[fields[0]].d, at1[fields[0]].value)
     assert_array_equal(dd[fields[1]].d, at1[fields[1]].value)
     assert dd[fields[0]].units == YTArray.from_astropy(at1[fields[0]]).units
     assert dd[fields[1]].units == YTArray.from_astropy(at1[fields[1]]).units
Example #4
0
def make_point_sources(area, exp_time, positions, sky_center,
                       spectra, prng=None):
    r"""
    Create a new :class:`~pyxsim.event_list.EventList` which contains
    point sources.

    Parameters
    ----------
    area : float, (value, unit) tuple, :class:`~yt.units.yt_array.YTQuantity`, or :class:`~astropy.units.Quantity`
        The collecting area to determine the number of events. If units are
        not specified, it is assumed to be in cm^2.
    exp_time : float, (value, unit) tuple, :class:`~yt.units.yt_array.YTQuantity`, or :class:`~astropy.units.Quantity`
        The exposure time to determine the number of events. If units are
        not specified, it is assumed to be in seconds.
    positions : array of source positions, shape 2xN
        The positions of the point sources in RA, Dec, where N is the
        number of point sources. Coordinates should be in degrees.
    sky_center : array-like
        Center RA, Dec of the events in degrees.
    spectra : list (size N) of :class:`~soxs.spectra.Spectrum` objects
        The spectra for the point sources, where N is the number 
        of point sources. Assumed to be in the observer frame.
    prng : integer or :class:`~numpy.random.RandomState` object 
        A pseudo-random number generator. Typically will only be specified
        if you have a reason to generate the same set of random numbers, such as for a
        test. Default is to use the :mod:`numpy.random` module.
    """
    prng = parse_prng(prng)

    spectra = ensure_list(spectra)
    positions = ensure_list(positions)

    area = parse_value(area, "cm**2")
    exp_time = parse_value(exp_time, "s")

    t_exp = exp_time.value/comm.size

    x = []
    y = []
    e = []

    for pos, spectrum in zip(positions, spectra):
        eobs = spectrum.generate_energies(t_exp, area.value, prng=prng)
        ne = eobs.size
        x.append(YTArray([pos[0]] * ne, "degree"))
        y.append(YTArray([pos[1]] * ne, "degree"))
        e.append(YTArray.from_astropy(eobs))

    parameters = {"sky_center": YTArray(sky_center, "degree"),
                  "exp_time": exp_time,
                  "area": area}

    events = {}
    events["eobs"] = uconcatenate(e)
    events["xsky"] = uconcatenate(x)
    events["ysky"] = uconcatenate(y)

    return EventList(events, parameters)
Example #5
0
def make_point_sources(area, exp_time, positions, sky_center,
                       spectra, prng=None):
    r"""
    Create a new :class:`~pyxsim.event_list.EventList` which contains
    point sources.

    Parameters
    ----------
    area : float, (value, unit) tuple, :class:`~yt.units.yt_array.YTQuantity`, or :class:`~astropy.units.Quantity`
        The collecting area to determine the number of events. If units are
        not specified, it is assumed to be in cm^2.
    exp_time : float, (value, unit) tuple, :class:`~yt.units.yt_array.YTQuantity`, or :class:`~astropy.units.Quantity`
        The exposure time to determine the number of events. If units are
        not specified, it is assumed to be in seconds.
    positions : array of source positions, shape 2xN
        The positions of the point sources in RA, Dec, where N is the
        number of point sources. Coordinates should be in degrees.
    sky_center : array-like
        Center RA, Dec of the events in degrees.
    spectra : list (size N) of :class:`~soxs.spectra.Spectrum` objects
        The spectra for the point sources, where N is the number 
        of point sources. Assumed to be in the observer frame.
    prng : integer or :class:`~numpy.random.RandomState` object 
        A pseudo-random number generator. Typically will only be specified
        if you have a reason to generate the same set of random numbers, such as for a
        test. Default is to use the :mod:`numpy.random` module.
    """
    prng = parse_prng(prng)

    spectra = ensure_list(spectra)
    positions = ensure_list(positions)

    area = parse_value(area, "cm**2")
    exp_time = parse_value(exp_time, "s")

    t_exp = exp_time.value/comm.size

    x = []
    y = []
    e = []

    for pos, spectrum in zip(positions, spectra):
        eobs = spectrum.generate_energies(t_exp, area.value, prng=prng)
        ne = eobs.size
        x.append(YTArray([pos[0]] * ne, "deg"))
        y.append(YTArray([pos[1]] * ne, "deg"))
        e.append(YTArray.from_astropy(eobs))

    parameters = {"sky_center": YTArray(sky_center, "degree"),
                  "exp_time": exp_time,
                  "area": area}

    events = {}
    events["xsky"] = uconcatenate(x)
    events["ysky"] = uconcatenate(y)
    events["eobs"] = uconcatenate(e)

    return EventList(events, parameters)
def test_astropy():
    from yt.utilities.on_demand_imports import _astropy

    ap_arr = np.arange(10) * _astropy.units.km / _astropy.units.hr
    yt_arr = YTArray(np.arange(10), "km/hr")
    yt_arr2 = YTArray.from_astropy(ap_arr)

    ap_quan = 10. * _astropy.units.Msun**0.5 / (_astropy.units.kpc**3)
    yt_quan = YTQuantity(10., "sqrt(Msun)/kpc**3")
    yt_quan2 = YTQuantity.from_astropy(ap_quan)

    assert_array_equal(ap_arr, yt_arr.to_astropy())
    assert_array_equal(yt_arr, YTArray.from_astropy(ap_arr))
    assert_array_equal(yt_arr, yt_arr2)

    assert_equal(ap_quan, yt_quan.to_astropy())
    assert_equal(yt_quan, YTQuantity.from_astropy(ap_quan))
    assert_equal(yt_quan, yt_quan2)

    assert_array_equal(yt_arr, YTArray.from_astropy(yt_arr.to_astropy()))
    assert_equal(yt_quan, YTQuantity.from_astropy(yt_quan.to_astropy()))
def test_astropy():
    from yt.utilities.on_demand_imports import _astropy

    ap_arr = np.arange(10)*_astropy.units.km/_astropy.units.hr
    yt_arr = YTArray(np.arange(10), "km/hr")
    yt_arr2 = YTArray.from_astropy(ap_arr)

    ap_quan = 10.*_astropy.units.Msun**0.5/(_astropy.units.kpc**3)
    yt_quan = YTQuantity(10., "sqrt(Msun)/kpc**3")
    yt_quan2 = YTQuantity.from_astropy(ap_quan)

    yield assert_array_equal, ap_arr, yt_arr.to_astropy()
    yield assert_array_equal, yt_arr, YTArray.from_astropy(ap_arr)
    yield assert_array_equal, yt_arr, yt_arr2

    yield assert_equal, ap_quan, yt_quan.to_astropy()
    yield assert_equal, yt_quan, YTQuantity.from_astropy(ap_quan)
    yield assert_equal, yt_quan, yt_quan2

    yield assert_array_equal, yt_arr, YTArray.from_astropy(yt_arr.to_astropy())
    yield assert_equal, yt_quan, YTQuantity.from_astropy(yt_quan.to_astropy())