def test_etc(): np.random.seed(seed=0x4d3d3d3) ds = data_dir_load(ETC) A = 3000. exp_time = 1.0e5 redshift = 0.1 apec_model = TableApecModel(APEC, 0.1, 20.0, 2000) tbabs_model = TableAbsorbModel(TBABS, 0.1) sphere = ds.sphere("max", (0.5, "mpc")) thermal_model = ThermalPhotonModel(apec_model, Zmet=0.3) photons = PhotonList.from_scratch(sphere, redshift, A, exp_time, thermal_model) events = photons.project_photons([0.0,0.0,1.0], responses=[ARF,RMF], absorb_model=tbabs_model) def photons_test(): return photons.photons def events_test(): return events.events for test in [GenericArrayTest(ds, photons_test), GenericArrayTest(ds, events_test)]: test_etc.__name__ = test.description yield test
def test_gasoline(): cosmology_parameters = dict(current_redshift = 0.0, omega_lambda = 0.728, omega_matter = 0.272, hubble_constant = 0.702) kwargs = dict(cosmology_parameters = cosmology_parameters, unit_base = {'length': (1.0/60.0, "Mpccm/h")}, n_ref = 64) ds = data_dir_load(gasoline, TipsyDataset, (), kwargs) yield assert_equal, str(ds), "agora_1e11.00400" dso = [ None, ("sphere", ("c", (0.3, 'unitary')))] dd = ds.all_data() yield assert_equal, dd["Coordinates"].shape, (10550576, 3) tot = sum(dd[ptype,"Coordinates"].shape[0] for ptype in ds.particle_types if ptype != "all") yield assert_equal, tot, 10550576 for dobj_name in dso: for field in _fields: for axis in [0, 1, 2]: for weight_field in [None]: yield PixelizedProjectionValuesTest( ds, axis, field, weight_field, dobj_name) yield FieldValuesTest(ds, field, dobj_name) dobj = create_obj(ds, dobj_name) s1 = dobj["ones"].sum() s2 = sum(mask.sum() for block, mask in dobj.blocks) yield assert_equal, s1, s2
def test_zp(): ds = data_dir_load(zp) yield assert_equal, str(ds), "plt32.2d.hdf5" for test in small_patch_amr(zp, _zp_fields, input_center="c", input_weight="rhs"): test_zp.__name__ = test.description yield test
def test_cantor_5(): np.random.seed(0x4d3d3d3) ds = data_dir_load(c5) yield assert_equal, str(ds), "c5" dso = [ None, ("sphere", ("c", (0.1, 'unitary'))), ("sphere", ("c", (0.2, 'unitary')))] dd = ds.all_data() yield assert_almost_equal, ds.index.get_smallest_dx(), 0.00411522633744843, 10 yield assert_equal, dd["x"].shape[0], 63*63*63 yield assert_almost_equal, \ dd["cell_volume"].in_units("code_length**3").sum(dtype="float64").d, \ 1.0, 10 for offset_1 in [1e-9, 1e-4, 0.1]: for offset_2 in [1e-9, 1e-4, 0.1]: DLE = ds.domain_left_edge DRE = ds.domain_right_edge ray = ds.ray(DLE + offset_1 * DLE.uq, DRE - offset_2 * DRE.uq) yield assert_almost_equal, ray["dts"].sum(dtype="float64"), 1.0, 8 for i, p1 in enumerate(np.random.random((5, 3))): for j, p2 in enumerate(np.random.random((5, 3))): ray = ds.ray(p1, p2) yield assert_almost_equal, ray["dts"].sum(dtype="float64"), 1.0, 8 for field in _fields: for dobj_name in dso: yield FieldValuesTest(c5, field, dobj_name)
def test_galaxy0030(): ds = data_dir_load(g30) yield check_color_conservation(ds) yield assert_equal, str(ds), "galaxy0030" for test in big_patch_amr(g30, _fields): test_galaxy0030.__name__ = test.description yield test
def test_nuclei_density_fields(): ds = data_dir_load(ecp) ad = ds.all_data() yield assert_array_equal, ad["H_nuclei_density"], \ (ad["H_number_density"] + ad["H_p1_number_density"]) yield assert_array_equal, ad["He_nuclei_density"], \ (ad["He_number_density"] + ad["He_p1_number_density"] + ad["He_p2_number_density"])
def setup(self): cosmology_parameters = dict(current_redshift = 0.0, omega_lambda = 0.728, omega_matter = 0.272, hubble_constant = 0.702) kwargs = dict(cosmology_parameters = cosmology_parameters, unit_base = {'length': (1.0/60.0, "Mpccm/h")}, n_ref = 64) self.ds = data_dir_load(self.dsname, yt.TipsyDataset, (), kwargs)
def test_d9p(): ds = data_dir_load(d9p) yield assert_equal, str(ds), "10MpcBox_HartGal_csf_a0.500.d" for test in big_patch_amr(d9p, _fields): test_d9p.__name__ = test.description yield test dso = [None, ("sphere", ("max", (0.1, 'unitary')))] for field in _fields: for axis in [0, 1, 2]: for dobj_name in dso: for weight_field in [None, "density"]: yield PixelizedProjectionValuesTest( d9p, axis, field, weight_field, dobj_name) ad = ds.all_data() # 'Ana' variable values output from the ART Fortran 'ANA' analysis code AnaNStars = 6255 yield assert_equal, ad[('stars','particle_type')].size, AnaNStars yield assert_equal, ad[('specie4', 'particle_type')].size, AnaNStars AnaNDM = 2833405 yield assert_equal, ad[('darkmatter','particle_type')].size, AnaNDM yield assert_equal, ad[('specie0', 'particle_type')].size + \ ad[('specie1', 'particle_type')].size + \ ad[('specie2', 'particle_type')].size + \ ad[('specie3', 'particle_type')].size, AnaNDM AnaBoxSize = yt.units.yt_array.YTQuantity(7.1442196564,'Mpc') AnaVolume = yt.units.yt_array.YTQuantity(364.640074656,'Mpc**3') Volume = 1 for i in ds.domain_width.in_units('Mpc'): yield assert_almost_equal, i, AnaBoxSize Volume *= i yield assert_almost_equal, Volume, AnaVolume AnaNCells = 4087490 yield assert_equal, len(ad[('index','cell_volume')]), AnaNCells AnaTotDMMass = yt.units.yt_array.YTQuantity(1.01191786811e+14,'Msun') yield assert_almost_equal, ad[('darkmatter','particle_mass')].sum()\ .in_units('Msun'), AnaTotDMMass AnaTotStarMass = yt.units.yt_array.YTQuantity(1776251.,'Msun') yield assert_almost_equal, ad[('stars','particle_mass')].sum()\ .in_units('Msun'), AnaTotStarMass AnaTotStarMassInitial = yt.units.yt_array.YTQuantity(2422854.,'Msun') yield assert_almost_equal, ad[('stars','particle_mass_initial')].sum()\ .in_units('Msun'), AnaTotStarMass AnaTotGasMass = yt.units.yt_array.YTQuantity(1.781994e+13,'Msun') yield assert_almost_equal, ad[('gas','cell_mass')].sum()\ .in_units('Msun'), AnaTotGasMass AnaTotTemp = yt.units.yt_array.YTQuantity(1.5019e11, 'K') #just leaves yield assert_equal, ad[('gas','temperature')].sum(), AnaTotTemp
def test_M7_offaxis(): ds = data_dir_load(M7) szprj = SZProjection(ds, freqs) szprj.off_axis(np.array([0.1,-0.2,0.4]), nx=100) def offaxis_array_func(): return szprj.data def offaxis_image_func(filename_prefix): szprj.write_png(filename_prefix) for test in [GenericArrayTest(ds, offaxis_array_func), GenericImageTest(ds, offaxis_image_func, 3)]: test_M7_offaxis.__name__ = test.description yield test
def test_attributes_wt(): plot_field = 'density' decimals = 3 ds = data_dir_load(WT) ax = 'z' for attr_name in ATTR_ARGS.keys(): for args in ATTR_ARGS[attr_name]: yield PlotWindowAttributeTest(ds, plot_field, ax, attr_name, args, decimals) for n, r in CALLBACK_TESTS: yield PlotWindowAttributeTest(ds, plot_field, ax, attr_name, args, decimals, callback_id=n, callback_runners=r)
def test_attributes(): """Test plot member functions that aren't callbacks""" plot_field = 'density' decimals = 3 ds = data_dir_load(M7) for ax in 'xyz': for attr_name in ATTR_ARGS.keys(): for args in ATTR_ARGS[attr_name]: test = PlotWindowAttributeTest(ds, plot_field, ax, attr_name, args, decimals) test_attributes.__name__ = test.description yield test for n, r in CALLBACK_TESTS: yield PlotWindowAttributeTest(ds, plot_field, ax, attr_name, args, decimals, callback_id=n, callback_runners=r)
def test_output_00080(): ds = data_dir_load(output_00080) yield assert_equal, str(ds), "info_00080" dso = [ None, ("sphere", ("max", (0.1, 'unitary')))] for dobj_name in dso: for field in _fields: for axis in [0, 1, 2]: for weight_field in [None, "density"]: yield PixelizedProjectionValuesTest( output_00080, axis, field, weight_field, dobj_name) yield FieldValuesTest(output_00080, field, dobj_name) dobj = create_obj(ds, dobj_name) s1 = dobj["ones"].sum() s2 = sum(mask.sum() for block, mask in dobj.blocks) yield assert_equal, s1, s2
def test_sizmbhloz(): ds = data_dir_load(sizmbhloz) ds.max_range = 1024*1024 yield assert_equal, str(ds), "sizmbhloz-clref04SNth-rs9_a0.9011.art" dso = [ None, ("sphere", ("max", (0.1, 'unitary')))] for dobj_name in dso: for field in _fields: for axis in [0, 1, 2]: for weight_field in [None, "density"]: yield PixelizedProjectionValuesTest( sizmbhloz, axis, field, weight_field, dobj_name) yield FieldValuesTest(sizmbhloz, field, dobj_name) dobj = create_obj(ds, dobj_name) s1 = dobj["ones"].sum() s2 = sum(mask.sum() for block, mask in dobj.blocks) yield assert_equal, s1, s2
def test_snapshot_033(): ds = data_dir_load(os33) yield assert_equal, str(ds), "snap_033" dso = [ None, ("sphere", ("c", (0.1, 'unitary')))] dd = ds.all_data() yield assert_equal, dd["particle_position"].shape[0], 2*(128*128*128) yield assert_equal, dd["particle_position"].shape[1], 3 tot = sum(dd[ptype,"particle_position"].shape[0] for ptype in ds.particle_types if ptype != "all") yield assert_equal, tot, (2*128*128*128) for dobj_name in dso: for field in _fields: for axis in [0, 1, 2]: for weight_field in [None, "density"]: yield PixelizedProjectionValuesTest( os33, axis, field, weight_field, dobj_name) yield FieldValuesTest(os33, field, dobj_name) dobj = create_obj(ds, dobj_name) s1 = dobj["ones"].sum() s2 = sum(mask.sum() for block, mask in dobj.blocks) yield assert_equal, s1, s2
def test_light_cone(): prng = RandomState(0x4d3d3d3) ds = data_dir_load(etc) A = 2000. exp_time = 1.0e5 fov = (0.5, "deg") lc = XrayLightCone('%s/32Mpc_32.enzo' % etc[:-14], 'Enzo', 0., 0.1, seed=24) source_model = ThermalSourceModel("apec", 0.1, 10.0, 1000, prng=prng) events = lc.generate_events(A, exp_time, fov, source_model, (30.0, 45.0), absorb_model="wabs", nH=0.02, sigma_pos=0.5, prng=prng) return_events = return_data(events.events) test = GenericArrayTest(ds, return_events, args=["events"]) test_light_cone.__name__ = test.description yield test
def test_A2052(): ds = data_dir_load(A2052, cls=SkyDataFITSDataset) assert_equal(str(ds), "A2052_merged_0.3-2_match-core_tmap_bgecorr.fits") for test in small_patch_amr(ds, _fields_A2052, input_center="c", input_weight="ones"): test_A2052.__name__ = test.description yield test
def test_grs(): ds = data_dir_load(grs, cls=SpectralCubeFITSDataset, kwargs={"nan_mask":0.0}) assert_equal(str(ds), "grs-50-cube.fits") for test in small_patch_amr(ds, _fields_grs, input_center="c", input_weight="ones"): test_grs.__name__ = test.description yield test
def test_EagleDataset(): ds = data_dir_load(s28) assert isinstance(ds, EagleDataset) psc = ParticleSelectionComparison(ds) psc.run_defaults()
def test_stripping(): ds = data_dir_load(stripping, kwargs={"units_override": uo_stripping}) yield assert_equal, str(ds), "rps.0062" for test in small_patch_amr(ds, _fields_stripping): test_stripping.__name__ = test.description yield test
def test_star(): ds = data_dir_load(star) yield assert_equal, str(ds), "plrd01000" for test in small_patch_amr(star, _fields): test_star.__name__ = test.description yield test
def test_MoabHex8Dataset(): assert isinstance(data_dir_load(c5), MoabHex8Dataset)
def test_NyxDataset(): assert isinstance(data_dir_load(LyA), NyxDataset)
def test_RT_particles(): ds = data_dir_load(RT_particles) assert_equal(str(ds), "plt00050") for test in small_patch_amr(ds, _castro_fields): test_RT_particles.__name__ = test.description yield test
def test_SkyDataFITSDataset(): assert isinstance(data_dir_load(A2052), SkyDataFITSDataset)
def test_EventsFITSDataset(): assert isinstance(data_dir_load(acis), EventsFITSDataset)
def test_SpectralCubeFITSDataset(): assert isinstance(data_dir_load(grs), SpectralCubeFITSDataset)
def test_FITSDataset(): assert isinstance(data_dir_load(vf), FITSDataset)
def test_sloshing(): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) prng = RandomState(0x4d3d3d3) ds = data_dir_load(gslr) A = 2000. exp_time = 1.0e4 redshift = 0.1 sphere = ds.sphere("c", (0.1, "Mpc")) sphere.set_field_parameter("X_H", 0.75) thermal_model = ThermalSourceModel("apec", 0.1, 11.0, 10000, Zmet=0.3, thermal_broad=False, prng=prng) photons1 = PhotonList.from_data_source(sphere, redshift, A, exp_time, thermal_model) return_photons = return_data(photons1.photons) nphots = 0 for i in range(4): phots = PhotonList.from_data_source(sphere, redshift, A, 0.25*exp_time, thermal_model) phots.write_h5_file("split_photons_%d.h5" % i) nphots += len(phots.photons["energy"]) merge_files(["split_photons_%d.h5" % i for i in range(4)], "merged_photons.h5", add_exposure_times=True, overwrite=True) merged_photons = PhotonList.from_file("merged_photons.h5") assert len(merged_photons.photons["energy"]) == nphots assert merged_photons.parameters["fid_exp_time"] == exp_time events1 = photons1.project_photons([1.0,-0.5,0.2], [30., 45.], absorb_model="tbabs", nH=0.1, prng=prng) return_events = return_data(events1.events) events1.write_spectrum("test_events_spec.fits", 0.2, 10.0, 2000) f = pyfits.open("test_events_spec.fits") return_spec = return_data(f["SPECTRUM"].data["COUNTS"]) f.close() events1.write_fits_image("test_events_img.fits", (20.0, "arcmin"), 1024) f = pyfits.open("test_events_img.fits") return_img = return_data(f[0].data) f.close() tests = [GenericArrayTest(ds, return_photons, args=["photons"]), GenericArrayTest(ds, return_events, args=["events"]), GenericArrayTest(ds, return_spec, args=["spec"]), GenericArrayTest(ds, return_img, args=["img"])] for test in tests: test_sloshing.__name__ = test.description yield test photons1.write_h5_file("test_photons.h5") events1.write_h5_file("test_events.h5") events1.write_fits_file("test_events.fits", 20.0, 1024) photons2 = PhotonList.from_file("test_photons.h5") events2 = EventList.from_h5_file("test_events.h5") events3 = EventList.from_fits_file("test_events.fits") for k in photons1.keys(): if k == "energy": arr1 = uconcatenate(photons1[k]) arr2 = uconcatenate(photons2[k]) else: arr1 = photons1[k] arr2 = photons2[k] assert_array_equal(arr1, arr2) for k in events2.keys(): assert_array_equal(events1[k], events2[k]) assert_allclose(events2[k], events3[k], rtol=1.0e-6) nevents = 0 for i in range(4): events = photons1.project_photons([1.0, -0.5, 0.2], [30., 45.], absorb_model="tbabs", nH=0.1, prng=prng) events.write_h5_file("split_events_%d.h5" % i) nevents += len(events["xsky"]) merge_files(["split_events_%d.h5" % i for i in range(4)], "merged_events.h5", add_exposure_times=True, overwrite=True) merged_events = EventList.from_h5_file("merged_events.h5") assert len(merged_events["xsky"]) == nevents assert merged_events.parameters["exp_time"] == 4.0*exp_time os.chdir(curdir) shutil.rmtree(tmpdir)
def test_RockstarDataset(): assert isinstance(data_dir_load(r1), RockstarDataset)
def test_CastroDataset(): assert isinstance(data_dir_load(RT_particles), CastroDataset)
def test_xray_binaries(): tmpdir = tempfile.mkdtemp() curdir = os.getcwd() os.chdir(tmpdir) prng = 25 ds = data_dir_load(galaxy) def _age(field, data): z_s = 1.0 / data["PartType4", "StellarFormationTime"] - 1.0 age = data.ds.cosmology.t_from_z(0.0) - data.ds.cosmology.t_from_z(z_s) age.convert_to_units("Gyr") return age ds.add_field(("PartType4", "particle_age"), function=_age, units="Gyr", particle_type=True) sp = ds.sphere("max", (0.25, "Mpc")) scale_length = (1.0, "kpc") age_field = ("PartType4", "particle_age") new_ds = make_xrb_particles(sp, age_field, scale_length, prng=prng) dd = new_ds.all_data() area = ds.quan(25000.0, "cm**2") exp_time = ds.quan(300.0, "ks") emin = 0.1 emax = 10.0 redshift = 0.01 photons_xrb = make_xrb_photons(new_ds, redshift, area, exp_time, emin, emax, center=sp.center, cosmology=ds.cosmology, prng=prng) D_L = ds.cosmology.luminosity_distance(0.0, redshift) kappa = convert_bands(alpha_lmxb, emin_hmxb, emax_hmxb, emin_lmxb, emax_lmxb) E = uconcatenate(photons_xrb["energy"]) lmxb_idxs = dd["particle_spectral_index"] < 2.0 hmxb_idxs = np.logical_not(lmxb_idxs) flux = dd["particle_luminosity"][lmxb_idxs].sum()*kappa/bc_lmxb flux += dd["particle_luminosity"][hmxb_idxs].sum()/bc_hmxb flux /= 4.0*np.pi*D_L*D_L idxs = E*(1.0+redshift) > 2.0 E_mean = E[idxs].mean().to("erg") n1 = flux*exp_time*area/E_mean n2 = idxs.sum() dn = np.sqrt(n2) assert np.abs(n1-n2) < 1.645*dn os.chdir(curdir) shutil.rmtree(tmpdir)
def test_WarpXDataset(): assert isinstance(data_dir_load(plasma), WarpXDataset)
def test_radtube(): ds = data_dir_load(rt) assert_equal(str(ds), "plt00500") for test in small_patch_amr(ds, _orion_fields): test_radtube.__name__ = test.description yield test
def test_star(): ds = data_dir_load(star) assert_equal(str(ds), "plrd01000") for test in small_patch_amr(ds, _orion_fields): test_star.__name__ = test.description yield test
def test_PlutoDataset(): assert isinstance(data_dir_load(kho), PlutoDataset)
def test_velocity_field(): ds = data_dir_load(vf, cls=FITSDataset) yield assert_equal, str(ds), "velocity_field_20.fits" for test in small_patch_amr(vf, _fields_vels, input_center="c", input_weight="ones"): test_velocity_field.__name__ = test.description yield test
def test_namelist_reading_should_not_fail(): for ds_name in (ramses_empty_record, output_00080): # Test that the reading does not fail for malformed namelist.txt files ds = data_dir_load(ds_name) ds.index # should work
def test_cloud(): ds = data_dir_load(cloud) yield assert_equal, str(ds), "Cloud.0050" for test in small_patch_amr(cloud, _fields_cloud): test_cloud.__name__ = test.description yield test
def test_fields_g1(): ds = data_dir_load(g1) assert_equal(str(ds), os.path.basename(g1)) for field in _fields: yield FieldValuesTest(g1, field, particle_type=True)
def test_stripping(): ds = data_dir_load(stripping, kwargs={"units_override":uo_stripping}) yield assert_equal, str(ds), "rps.0062" for test in small_patch_amr(stripping, _fields_stripping): test_stripping.__name__ = test.description yield test
def test_kh2d(): ds = data_dir_load(kh2d) assert_equal(str(ds), 'DD0011') for test in small_patch_amr(ds, ds.field_list): test_toro1d.__name__ = test.description yield test
def test_velocity_field(): ds = data_dir_load(vf, cls=FITSDataset) assert_equal(str(ds), "velocity_field_20.fits") for test in small_patch_amr(ds, _fields_vels, input_center="c", input_weight="ones"): test_velocity_field.__name__ = test.description yield test
def test_FITSDataset(): assert isinstance(data_dir_load(grs), FITSDataset)
def test_simulated_halo_mass_function(): ds = data_dir_load(enzotiny) for finder in ["fof", "hop"]: yield SimulatedHaloMassFunctionTest(ds, finder)
def test_blast(): ds = data_dir_load(blast) yield assert_equal, str(ds), "Blast.0100" for test in small_patch_amr(blast, _fields_blast): test_blast.__name__ = test.description yield test
def test_analytic_halo_mass_function(): ds = data_dir_load(enzotiny) for fit in range(1, 6): yield AnalyticHaloMassFunctionTest(ds, fit)
def test_AthenaDataset(): assert isinstance(data_dir_load(cloud), AthenaDataset)
def test_ecp(): ds = data_dir_load(ecp) # Now we test our species fields yield check_color_conservation(ds)
def test_fields_r1(): ds = data_dir_load(r1) yield assert_equal, str(ds), os.path.basename(r1) for field in _fields: yield FieldValuesTest(r1, field, particle_type=True)
def test_EnzoDataset(): assert isinstance(data_dir_load(enzotiny), EnzoDataset)
def test_cloud(): ds = data_dir_load(cloud) yield assert_equal, str(ds), "Cloud.0050" for test in small_patch_amr(ds, _fields_cloud): test_cloud.__name__ = test.description yield test
def test_tb(): ds = data_dir_load(tb) assert_equal(str(ds), "data.0005.3d.hdf5") for test in small_patch_amr(ds, _fields): test_tb.__name__ = test.description yield test
def test_GadgetDataset(): kwargs = dict(bounding_box=[[-3,3], [-3,3], [-3,3]]) assert isinstance(data_dir_load(isothermal, kwargs=kwargs), GadgetHDF5Dataset)
def test_kho(): ds = data_dir_load(kho) assert_equal(str(ds), "data.0004.hdf5") for test in small_patch_amr(ds, _fields): test_kho.__name__ = test.description yield test
def test_radtube(): ds = data_dir_load(rt) yield assert_equal, str(ds), "plt00500" for test in small_patch_amr(rt, _fields): test_radtube.__name__ = test.description yield test
def test_ChomboDataset(): assert isinstance(data_dir_load(zp), ChomboDataset)
def test_OrionDataset(): assert isinstance(data_dir_load(rt), OrionDataset)
def test_Orion2Dataset(): assert isinstance(data_dir_load(gc), Orion2Dataset)
def test_moving7(): ds = data_dir_load(m7) assert_equal(str(ds), "moving7_0010") for test in small_patch_amr(m7, _fields): test_moving7.__name__ = test.description yield test
def test_blast(): ds = data_dir_load(blast) yield assert_equal, str(ds), "Blast.0100" for test in small_patch_amr(ds, _fields_blast): test_blast.__name__ = test.description yield test