class TestSetWidth(unittest.TestCase): ds = None def setUp(self): if self.ds is None: self.ds = fake_random_ds(64) self.slc = SlicePlot(self.ds, 0, "density") def _assert_05cm(self): assert_array_equal([self.slc.xlim, self.slc.ylim, self.slc.width], [(YTQuantity(0.25, 'cm'), YTQuantity(0.75, 'cm')), (YTQuantity(0.25, 'cm'), YTQuantity(0.75, 'cm')), (YTQuantity(0.5, 'cm'), YTQuantity(0.5, 'cm'))]) def _assert_05_075cm(self): assert_array_equal([self.slc.xlim, self.slc.ylim, self.slc.width], [(YTQuantity(0.25, 'cm'), YTQuantity(0.75, 'cm')), (YTQuantity(0.125, 'cm'), YTQuantity(0.875, 'cm')), (YTQuantity(0.5, 'cm'), YTQuantity(0.75, 'cm'))]) def test_set_width_one(self): assert_equal([self.slc.xlim, self.slc.ylim, self.slc.width], [(0.0, 1.0), (0.0, 1.0), (1.0, 1.0)]) assert_true(self.slc._axes_unit_names is None) def test_set_width_nonequal(self): self.slc.set_width((0.5, 0.8)) assert_rel_equal([self.slc.xlim, self.slc.ylim, self.slc.width], [(0.25, 0.75), (0.1, 0.9), (0.5, 0.8)], 15) assert_true(self.slc._axes_unit_names is None) def test_twoargs_eq(self): self.slc.set_width(0.5, 'cm') self._assert_05cm() assert_true(self.slc._axes_unit_names == ('cm', 'cm')) def test_tuple_eq(self): self.slc.set_width((0.5, 'cm')) self._assert_05cm() assert_true(self.slc._axes_unit_names == ('cm', 'cm')) def test_tuple_of_tuples_neq(self): self.slc.set_width(((0.5, 'cm'), (0.75, 'cm'))) self._assert_05_075cm() assert_true(self.slc._axes_unit_names == ('cm', 'cm')) def test_tuple_of_tuples_neq(self): self.slc.set_width(((0.5, 'cm'), (0.0075, 'm'))) self._assert_05_075cm() assert_true(self.slc._axes_unit_names == ('cm', 'm'))
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 test_plot_2d(): # Cartesian ds = fake_random_ds((32, 32, 1), fields=('temperature', ), units=('K', )) slc = SlicePlot(ds, "z", ["temperature"], width=(0.2, "unitary"), center=[0.4, 0.3, 0.5]) slc2 = plot_2d(ds, "temperature", width=(0.2, "unitary"), center=[0.4, 0.3]) slc3 = plot_2d(ds, "temperature", width=(0.2, "unitary"), center=ds.arr([0.4, 0.3], "cm")) assert_array_equal(slc.frb['temperature'], slc2.frb['temperature']) assert_array_equal(slc.frb['temperature'], slc3.frb['temperature']) # Cylindrical ds = data_dir_load(WD) slc = SlicePlot(ds, "theta", ["density"], width=(30000.0, "km")) slc2 = plot_2d(ds, "density", width=(30000.0, "km")) assert_array_equal(slc.frb['density'], slc2.frb['density'])
def test_set_background_color(): ds = fake_random_ds(32) plot = SlicePlot(ds, 2, "density") for field in ["density", ("gas", "density")]: plot.set_background_color(field, "red") plot._setup_plots() ax = plot.plots[field].axes assert_equal(ax.get_facecolor(), (1.0, 0.0, 0.0, 1.0))
def test_marker_callback(): with _cleanup_fname() as prefix: ax = "z" vector = [1.0, 1.0, 1.0] ds = fake_amr_ds(fields=("density",), units=("g/cm**3",)) p = ProjectionPlot(ds, ax, ("gas", "density")) p.annotate_marker([0.5, 0.5, 0.5]) assert_fname(p.save(prefix)[0]) p = SlicePlot(ds, ax, ("gas", "density")) p.annotate_marker([0.5, 0.5, 0.5]) assert_fname(p.save(prefix)[0]) p = OffAxisSlicePlot(ds, vector, ("gas", "density")) p.annotate_marker([0.5, 0.5, 0.5]) assert_fname(p.save(prefix)[0]) # Now we'll check a few additional minor things p = SlicePlot(ds, "x", ("gas", "density")) coord = ds.arr([0.75, 0.75, 0.75], "unitary") coord.convert_to_units("kpc") p.annotate_marker(coord, coord_system="data") p.annotate_marker([0.5, 0.5], coord_system="axis", marker="*") p.annotate_marker([[0.5, 0.6], [0.5, 0.6], [0.5, 0.6]], coord_system="data") p.annotate_marker( [[0.5, 0.6, 0.8], [0.5, 0.6, 0.8], [0.5, 0.6, 0.8]], coord_system="data" ) p.annotate_marker([[0.5, 0.6, 0.8], [0.5, 0.6, 0.8]], coord_system="axis") p.annotate_marker([[0.5, 0.6, 0.8], [0.5, 0.6, 0.8]], coord_system="figure") p.annotate_marker([[0.5, 0.6, 0.8], [0.5, 0.6, 0.8]], coord_system="plot") p.save(prefix) check_axis_manipulation(p, prefix) with _cleanup_fname() as prefix: ds = fake_amr_ds(fields=("density",), units=("g/cm**3",), geometry="spherical") p = ProjectionPlot(ds, "r", ("gas", "density")) p.annotate_marker([0.5, 0.5, 0.5]) assert_raises(YTDataTypeUnsupported, p.save, prefix) p = ProjectionPlot(ds, "r", ("gas", "density")) p.annotate_marker([0.5, 0.5], coord_system="axis") assert_fname(p.save(prefix)[0])
def test_ray_callback(): with _cleanup_fname() as prefix: ax = "z" vector = [1.0, 1.0, 1.0] ds = fake_amr_ds(fields=("density", ), units=("g/cm**3", )) ray = ds.ray((0.1, 0.2, 0.3), (0.6, 0.8, 0.5)) oray = ds.ortho_ray(0, (0.3, 0.4)) p = ProjectionPlot(ds, ax, "density") p.annotate_ray(oray) p.annotate_ray(ray) assert_fname(p.save(prefix)[0]) p = SlicePlot(ds, ax, "density") p.annotate_ray(oray) p.annotate_ray(ray) assert_fname(p.save(prefix)[0]) p = OffAxisSlicePlot(ds, vector, "density") p.annotate_ray(oray) p.annotate_ray(ray) assert_fname(p.save(prefix)[0]) # Now we'll check a few additional minor things p = SlicePlot(ds, "x", "density") p.annotate_ray(oray) p.annotate_ray(ray, plot_args={"color": "red"}) p.save(prefix) with _cleanup_fname() as prefix: ds = fake_amr_ds(fields=("density", ), units=("g/cm**3", ), geometry="spherical") ray = ds.ray((0.1, 0.2, 0.3), (0.6, 0.8, 0.5)) oray = ds.ortho_ray(0, (0.3, 0.4)) p = ProjectionPlot(ds, "r", "density") p.annotate_ray(oray) assert_raises(YTDataTypeUnsupported, p.save, prefix) p = ProjectionPlot(ds, "r", "density") p.annotate_ray(ray) assert_raises(YTDataTypeUnsupported, p.save, prefix)
def test_ray_callback(): with _cleanup_fname() as prefix: ax = 'z' vector = [1.0, 1.0, 1.0] ds = fake_amr_ds(fields=("density", )) ray = ds.ray((0.1, 0.2, 0.3), (1.6, 1.8, 1.5)) oray = ds.ortho_ray(0, (0.3, 0.4)) p = ProjectionPlot(ds, ax, "density") p.annotate_ray(oray) p.annotate_ray(ray) yield assert_fname, p.save(prefix)[0] p = SlicePlot(ds, ax, "density") p.annotate_ray(oray) p.annotate_ray(ray) yield assert_fname, p.save(prefix)[0] p = OffAxisSlicePlot(ds, vector, "density") p.annotate_ray(oray) p.annotate_ray(ray) yield assert_fname, p.save(prefix)[0] # Now we'll check a few additional minor things p = SlicePlot(ds, "x", "density") p.annotate_ray(oray) p.annotate_ray(ray, plot_args={'color': 'red'}) p.save(prefix)
def test_method_signature(): ds = fake_amr_ds( fields=[("gas", "density"), ("gas", "velocity_x"), ("gas", "velocity_y")], units=["g/cm**3", "m/s", "m/s"], ) p = SlicePlot(ds, "z", ("gas", "density")) sig = inspect.signature(p.annotate_velocity) # checking the first few arguments rather than the whole signature # we just want to validate that method wrapping works assert list(sig.parameters.keys())[:4] == [ "factor", "scale", "scale_units", "normalize", ]
def test_scale_callback(): with _cleanup_fname() as prefix: ax = "z" vector = [1.0, 1.0, 1.0] ds = fake_amr_ds(fields=("density",), units=("g/cm**3",)) p = ProjectionPlot(ds, ax, ("gas", "density")) p.annotate_scale() assert_fname(p.save(prefix)[0]) p = ProjectionPlot(ds, ax, ("gas", "density"), width=(0.5, 1.0)) p.annotate_scale() assert_fname(p.save(prefix)[0]) p = ProjectionPlot(ds, ax, ("gas", "density"), width=(1.0, 1.5)) p.annotate_scale() assert_fname(p.save(prefix)[0]) p = SlicePlot(ds, ax, ("gas", "density")) p.annotate_scale() assert_fname(p.save(prefix)[0]) p = OffAxisSlicePlot(ds, vector, ("gas", "density")) p.annotate_scale() assert_fname(p.save(prefix)[0]) # Now we'll check a few additional minor things p = SlicePlot(ds, "x", ("gas", "density")) p.annotate_scale(corner="upper_right", coeff=10.0, unit="kpc") assert_fname(p.save(prefix)[0]) p = SlicePlot(ds, "x", ("gas", "density")) p.annotate_scale(text_args={"size": 24}) assert_fname(p.save(prefix)[0]) p = SlicePlot(ds, "x", ("gas", "density")) p.annotate_scale(text_args={"font": 24}) assert_raises(YTPlotCallbackError) with _cleanup_fname() as prefix: ds = fake_amr_ds(fields=("density",), units=("g/cm**3",), geometry="spherical") p = ProjectionPlot(ds, "r", ("gas", "density")) assert_raises(YTDataTypeUnsupported, p.annotate_scale) assert_raises(YTDataTypeUnsupported, p.annotate_scale, coord_system="axis")
def test_set_background_color(): ds = fake_random_ds(32) plot = SlicePlot(ds, 2, "density") for field in ["density", ("gas", "density")]: plot.set_background_color(field, "red") plot._setup_plots() ax = plot.plots[field].axes if LooseVersion(matplotlib.__version__) < LooseVersion("2.0.0"): assert_equal(ax.get_axis_bgcolor(), "red") else: assert_equal(ax.get_facecolor(), (1.0, 0.0, 0.0, 1.0))
def setUp(self): if self.ds is None: self.ds = fake_random_ds(64) self.slc = SlicePlot(self.ds, 0, "density")
class TestHideAxesColorbar(unittest.TestCase): ds = None 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 tearDown(self): os.chdir(self.curdir) shutil.rmtree(self.tmpdir) def test_hide_show_axes(self): self.slc.hide_axes() self.slc.save() self.slc.show_axes() self.slc.save() def test_hide_show_colorbar(self): self.slc.hide_colorbar() self.slc.save() self.slc.show_colorbar() self.slc.save() def test_hide_axes_colorbar(self): self.slc.hide_colorbar() self.slc.hide_axes() self.slc.save()