def test_multi_mesh(): coordsMulti = np.array([[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]], dtype=np.float64) connect1 = np.array([ [0, 1, 3], ], dtype=np.int64) connect2 = np.array([ [1, 2, 3], ], dtype=np.int64) data1 = {} data2 = {} data1["connect1", "test"] = np.array([ [0.0, 1.0, 3.0], ], dtype=np.float64) data2["connect2", "test"] = np.array([ [1.0, 2.0, 3.0], ], dtype=np.float64) connectList = [connect1, connect2] dataList = [data1, data2] ds = load_unstructured_mesh(connectList, coordsMulti, dataList) sl = SlicePlot(ds, "z", ("connect1", "test")) assert sl.data_source.field_data["connect1", "test"].shape == (1, 3) sl = SlicePlot(ds, "z", ("connect2", "test")) assert sl.data_source.field_data["connect2", "test"].shape == (1, 3) sl = SlicePlot(ds, "z", ("all", "test")) assert sl.data_source.field_data["all", "test"].shape == (2, 3) sl.annotate_mesh_lines()
def test_stream_hexahedral(): np.random.seed(0x4D3D3D3) Nx, Ny, Nz = 32, 18, 24 # Note what we're doing here -- we are creating a randomly spaced mesh, but # because of how the accumulate operation works, we also reset the leftmost # cell boundary to 0.0. cell_x = np.random.random(Nx + 1) cell_x /= cell_x.sum() cell_x = np.add.accumulate(cell_x) cell_x[0] = 0.0 cell_y = np.random.random(Ny + 1) cell_y /= cell_y.sum() cell_y = np.add.accumulate(cell_y) cell_y[0] = 0.0 cell_z = np.random.random(Nz + 1) cell_z /= cell_z.sum() cell_z = np.add.accumulate(cell_z) cell_z[0] = 0.0 coords, conn = hexahedral_connectivity(cell_x, cell_y, cell_z) data = {"random_field": np.random.random((Nx, Ny, Nz))} bbox = np.array([[0.0, 1.0], [0.0, 1.0], [0.0, 1.0]]) ds = load_hexahedral_mesh(data, conn, coords, bbox=bbox) dd = ds.all_data() # raise RuntimeError assert_almost_equal(float(dd[("gas", "cell_volume")].sum(dtype="float64")), 1.0) assert_equal(dd[("index", "ones")].size, Nx * Ny * Nz) # Now we try it with a standard mesh cell_x = np.linspace(0.0, 1.0, Nx + 1) cell_y = np.linspace(0.0, 1.0, Ny + 1) cell_z = np.linspace(0.0, 1.0, Nz + 1) coords, conn = hexahedral_connectivity(cell_x, cell_y, cell_z) data = {"random_field": np.random.random((Nx, Ny, Nz))} bbox = np.array([[0.0, 1.0], [0.0, 1.0], [0.0, 1.0]]) ds = load_hexahedral_mesh(data, conn, coords, bbox=bbox) dd = ds.all_data() assert_almost_equal(float(dd[("gas", "cell_volume")].sum(dtype="float64")), 1.0) assert_equal(dd[("index", "ones")].size, Nx * Ny * Nz) assert_almost_equal(dd[("index", "dx")].to_ndarray(), 1.0 / Nx) assert_almost_equal(dd[("index", "dy")].to_ndarray(), 1.0 / Ny) assert_almost_equal(dd[("index", "dz")].to_ndarray(), 1.0 / Nz) s = SlicePlot(ds, "x", "random_field") s._setup_plots() s.frb[("stream", "random_field")]
def create_image(filename_prefix): fields = ['noise%d' % i for i in range(4)] p = SlicePlot(ds, 'z', fields) p.save('%s_log' % filename_prefix) p.set_log('all', False) p.save('%s_lin' % filename_prefix)
def create_image(filename_prefix): fields = ["noise%d" % i for i in range(4)] p = SlicePlot(ds, "z", fields) p.save(f"{filename_prefix}_log") p.set_log("all", False) p.save(f"{filename_prefix}_lin")
def test_compare_arbitrary_grid_slice(): ds = fake_sph_orientation_ds() c = np.array([0.0, 0.0, 0.0]) width = 1.5 buff_size = 51 field = ("gas", "density") # buffer from arbitrary grid ag = ds.arbitrary_grid(c - width / 2, c + width / 2, [buff_size] * 3) buff_ag = ag[field][:, :, int(np.floor(buff_size / 2))].d.T # buffer from slice p = SlicePlot(ds, "z", field, center=c, width=width) p.set_buff_size(51) buff_slc = p.frb.data[field].d assert_equal(buff_slc, buff_ag)
def test_multi_mesh(): coordsMulti = np.array([[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]], dtype=np.float64) connect1 = np.array([ [0, 1, 3], ], dtype=np.int64) connect2 = np.array([ [1, 2, 3], ], dtype=np.int64) data1 = {} data2 = {} data1['connect1', 'test'] = np.array([ [0.0, 1.0, 3.0], ], dtype=np.float64) data2['connect2', 'test'] = np.array([ [1.0, 2.0, 3.0], ], dtype=np.float64) connectList = [connect1, connect2] dataList = [data1, data2] ds = load_unstructured_mesh(connectList, coordsMulti, dataList) sl = SlicePlot(ds, 'z', ('connect1', 'test')) sl = SlicePlot(ds, 'z', ('connect2', 'test')) sl = SlicePlot(ds, 'z', ('all', 'test')) sl.annotate_mesh_lines()
def test_add_ion_mass_fields_to_amr_ds(): """ Test to add various ion fields """ ds = fake_amr_ds(fields=("density", "velocity_x", "velocity_y", "velocity_z", "temperature", "metallicity")) ad = ds.all_data() add_ion_mass_field('O', 6, ds) field = ('gas', 'O_p5_mass') assert field in ds.derived_field_list assert isinstance(ad[field], np.ndarray) dirpath = tempfile.mkdtemp() SlicePlot(ds, 'x', field).save(dirpath) shutil.rmtree(dirpath)
def test_add_ion_fields_to_gizmo(): """ Test to add various ion fields to gizmo dataset and slice on them """ ds = load(FIRE_SIM) add_ion_fields(ds, ['H', 'O VI'], ftype='PartType0') ad = ds.all_data() fields = ['H_ion_fraction', 'O_p5_mass'] # Assure that a sampling of fields are added and can be sliced dirpath = tempfile.mkdtemp() for field in fields: field = ('gas', field) assert field in ds.derived_field_list assert isinstance(ad[field], np.ndarray) SlicePlot(ds, 'x', field).save(dirpath) shutil.rmtree(dirpath)
def test_add_ion_fields_to_enzo(): """ Test to add various ion fields to Enzo dataset and slice on them """ ds = load(ISO_GALAXY) add_ion_fields(ds, ['H', 'O VI'], ftype='gas') ad = ds.all_data() fields = ['H_p0_number_density', 'O_p5_density'] # Assure that a sampling of fields are added and can be sliced dirpath = tempfile.mkdtemp() for field in fields: field = ('gas', field) assert field in ds.derived_field_list assert isinstance(ad[field], np.ndarray) SlicePlot(ds, 'x', field).save(dirpath) shutil.rmtree(dirpath)
def test_add_ion_number_density_field_to_grid_ds(): """ Test to add various ion fields """ ds = fake_random_ds(8, fields=("density", "velocity_x", "velocity_y", "velocity_z", "temperature", "metallicity"), units=('g/cm**3', 'cm/s', 'cm/s', 'cm/s', 'K', '')) ad = ds.all_data() add_ion_mass_field('O', 6, ds) field = ('gas', 'O_p5_number_density') assert field in ds.derived_field_list assert isinstance(ad[field], np.ndarray) dirpath = tempfile.mkdtemp() SlicePlot(ds, 'x', field).save(dirpath) shutil.rmtree(dirpath)
def test_gather_slice(): ds = fake_sph_grid_ds() ds.num_neighbors = 5 field = ("gas", "density") c = np.array([1.5, 1.5, 0.5]) width = 3.0 p = SlicePlot(ds, "z", field, center=c, width=width) p.set_buff_size(3) buff_scatter = p.frb.data[field].d ds.sph_smoothing_style = "gather" p = SlicePlot(ds, "z", field, center=c, width=width) p.set_buff_size(3) buff_gather = p.frb.data[field].d assert_equal(buff_scatter, buff_gather)
def test_multi_field(): coords = np.array([[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]], dtype=np.float64) connect = np.array([[0, 1, 3], [1, 2, 3]], dtype=np.int64) data = {} data["connect1", "test"] = np.array([[0.0, 1.0, 3.0], [1.0, 2.0, 3.0]], dtype=np.float64) data["connect1", "testAgain"] = np.array([[0.0, 1.0, 3.0], [1.0, 2.0, 3.0]], dtype=np.float64) ds = load_unstructured_mesh(connect, coords, data) sl = SlicePlot(ds, "z", "test") sl.annotate_mesh_lines() sl = SlicePlot(ds, "z", "testAgain") sl.annotate_mesh_lines()
def test_add_all_ion_fields_to_amr_ds(): """ Test to add various ion fields """ ds = fake_amr_ds(fields=("density", "velocity_x", "velocity_y", "velocity_z", "temperature", "metallicity")) ftype = 'gas' ad = ds.all_data() ions = ['H', 'O', 'N V'] add_ion_fields(ds, ions, ftype=ftype) fields = ['H_ion_fraction', 'H_p0_number_density', 'O_p5_mass', 'N_p4_density'] # Assure that a sampling of fields are added and can be sliced dirpath = tempfile.mkdtemp() for field in fields: field = (ftype, field) assert field in ds.derived_field_list assert isinstance(ad[field], np.ndarray) SlicePlot(ds, 'x', field).save(dirpath) shutil.rmtree(dirpath)
def test_add_all_ion_fields_to_grid_ds_from_file(): """ Test to add various ion fields """ ds = fake_random_ds(8, fields=("density", "velocity_x", "velocity_y", "velocity_z", "temperature", "metallicity"), units= ('g/cm**3', 'cm/s', 'cm/s', 'cm/s', 'K', '')) ftype = 'gas' ad = ds.all_data() add_ion_fields(ds, 'all', ftype=ftype, line_database='lines.txt') fields = ['H_ion_fraction', 'H_p0_number_density', 'O_p5_mass', 'N_p4_density'] # Assure that a sampling of fields are added and can be sliced dirpath = tempfile.mkdtemp() for field in fields: field = (ftype, field) assert field in ds.derived_field_list assert isinstance(ad[field], np.ndarray) SlicePlot(ds, 'x', field).save(dirpath) shutil.rmtree(dirpath)
def __init__(self, ds, image_name="ytSliceImage", axis='z', variable='density', center=[0.0, 0.0, 0.0], width=10.0, units='kpc', color_map='hot', show_annotations=True): from yt import SlicePlot p = SlicePlot(ds, axis, variable, center=center, width=(width, units)) p.hide_axes() p.hide_colorbar() p.set_cmap(variable, color_map) p._setup_plots() plot = p.plots[variable] plot.canvas.draw() buff = plot.canvas.tostring_argb() ncols, nrows = plot.canvas.get_width_height() vv = np.fromstring(buff, dtype=np.uint8).reshape((nrows, ncols, 4), order="C") if show_annotations: p = SlicePlot(ds, axis, variable, center=center, width=(width, units)) p.set_cmap(variable, color_map) p._setup_plots() plot = p.plots[variable] plot.canvas.draw() buff2 = plot.canvas.tostring_argb() ncols2, nrows2 = plot.canvas.get_width_height() vv2 = np.fromstring(buff2, dtype=np.uint8).reshape( (nrows2, ncols2, 4), order="C") ncols = vv.shape[1] nrows = vv.shape[0] # delete if already there for im in bpy.data.images: if im.name == image_name: delete_unused_images(image_name) delete_unused_textures(image_name) delete_unused_materials(image_name) # for annotations for im in bpy.data.images: if im.name == image_name + '_ann': delete_unused_images(image_name + '_ann') delete_unused_textures(image_name + '_ann') delete_unused_materials(image_name + '_ann') # switching a from back to front, and flipping image # if also with annotation do a nice one in the domain pixels_tmp = np.array(vv) for i in range(0, nrows): #pixels_tmp[i,:,0] = vv[i,:,0] #pixels_tmp[i,:,1] = vv[i,:,1] #pixels_tmp[i,:,2] = vv[i,:,2] #pixels_tmp[i,:,3] = vv[i,:,3] pixels_tmp[i, :, 3] = vv[nrows - 1 - i, :, 0] pixels_tmp[i, :, 0] = vv[nrows - 1 - i, :, 1] pixels_tmp[i, :, 1] = vv[nrows - 1 - i, :, 2] pixels_tmp[i, :, 2] = vv[nrows - 1 - i, :, 3] if show_annotations: pixels_tmp2 = np.array(vv2) if show_annotations: for i in range(0, nrows2): pixels_tmp2[i, :, 3] = vv2[nrows2 - 1 - i, :, 0] pixels_tmp2[i, :, 0] = vv2[nrows2 - 1 - i, :, 1] pixels_tmp2[i, :, 1] = vv2[nrows2 - 1 - i, :, 2] pixels_tmp2[i, :, 2] = vv2[nrows2 - 1 - i, :, 3] # blank image image = bpy.data.images.new(image_name, width=ncols, height=nrows) if show_annotations: image2 = bpy.data.images.new(image_name + '_ann', width=ncols2, height=nrows2) pixels = pixels_tmp.ravel() / 255. #pixels = vv.ravel()/255. if show_annotations: pixels2 = pixels_tmp2.ravel() / 255. image.pixels = pixels if show_annotations: image2.pixels = pixels2 # activate the image in the uv editor for area in bpy.context.screen.areas: if area.type == 'IMAGE_EDITOR': if show_annotations: area.spaces.active.image = image2 else: area.spaces.active.image = image elif area.type == 'VIEW_3D': # make sure material/render view is on for space in area.spaces: if space.type == 'VIEW_3D': if (space.viewport_shade != 'RENDERED') and ( space.viewport_shade != 'MATERIAL'): space.viewport_shade = 'RENDERED' # material is too slow # also, attach to the projection in the blender 3d window # now, set color figmat = makeMaterial(image_name, (0, 0, 0), (1, 1, 1), 1.0, 0.0) # no emissivity or transperency for now setMaterial(bpy.data.objects[image_name], figmat) # also, make shadeless bpy.data.materials[image_name].use_shadeless = True # Create image texture from image cTex = bpy.data.textures.new(image_name, type='IMAGE') cTex.image = image # Add texture slot for color texture mat = bpy.data.materials[image_name] mtex = mat.texture_slots.add() mtex.texture = cTex #mtex.texture_coords = 'UV' mtex.texture_coords = 'OBJECT' # this seems to work better for figures, but certainly needs to be tested mtex.use_map_color_diffuse = True mtex.use_map_color_emission = True mtex.emission_color_factor = 0.5 mtex.use_map_density = True mtex.mapping = 'FLAT' # map to object mtex.object = bpy.data.objects[image_name]
def create_image(filename_prefix): fields = ["noise%d" % i for i in range(4)] for normal in ("phi", "theta"): p = SlicePlot(ds, normal, fields) p.save(f"{filename_prefix}_{normal}")
axis = 'x' variable = 'density' center = [0., 0., 0.] width = 200.0 units = 'kpc' color_map = 'algae' show_annotations = True image_name = 'testslice44' #my_sphere = ds.sphere(center, (width, units)) from yt import SlicePlot #p = SlicePlot(my_sphere, axis, variable, center = center, width = (width, units)) p = SlicePlot(ds, axis, variable, center=center, width=(width, units)) p.hide_axes() p.hide_colorbar() plot = p.plots[variable] p.set_cmap(variable, "hot") p.save('~/Desktop/testslc.png') #fig = plot.figure #f = BytesIO() #vv = yt.write_image(np.log10(p.frb[variable][:,:-1].d),f, cmap_name = color_map) plot.canvas.draw() buff = plot.canvas.tostring_argb() ncols, nrows = plot.canvas.get_width_height() vv = np.fromstring(buff, dtype=np.uint8).reshape((nrows, ncols, 4), order="C") if show_annotations: p = SlicePlot(ds, axis, variable, center=center, width=(width, units))