Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Fix off-axis rendering when center is not [0.5, 0.5, 0.5], fix periodicity #5059

Merged
merged 2 commits into from
Dec 2, 2024

Conversation

cphyc
Copy link
Member

@cphyc cphyc commented Nov 20, 2024

PR Summary

The off-axis renderer introduced in #4741 has an issue when the center and the center in rotated frame do not coincide (which happens when it isn't 0.5, 0.5, 0.5 afaik).

In addition, it wraps around the boundaries of the width of the plotting window rather than the simulation domain (if required).

This PR fixes both.

PR Checklist

  • Adds a test for any bugs fixed. Adds tests for new features.

Bug reproduction:

import numpy as np
import yt

ds = yt.load_sample("output_00080")
ad = ds.all_data()

center = ds.arr(ad.argmax("density"))

sp = ds.sphere(center, (200, "kpc"))

n = np.array([1, 1, 1.])
n /= np.linalg.norm(n)
p = yt.ProjectionPlot(ds, n, "density", center=center, data_source=sp, width=(400, "kpc"), weight_field=("gas", "density"))
p.set_zlim(("gas", "density"), 1e-4, 5)
p.save("frames/")
Before With this PR
info_00080_OffAxisProjection_density_density info_00080_OffAxisProjection_density_density

@cphyc cphyc changed the title Fix off-axis rendering when center is not [0.5, 0.5, 0.5] [BUG] Fix off-axis rendering when center is not [0.5, 0.5, 0.5], fix periodicity Nov 20, 2024
@cphyc
Copy link
Member Author

cphyc commented Nov 20, 2024

@yt-fido test this please

@cphyc cphyc marked this pull request as ready for review November 26, 2024 13:33
chrishavlin
chrishavlin previously approved these changes Nov 26, 2024
Copy link
Contributor

@chrishavlin chrishavlin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM now assuming tests are green again.

@chrishavlin chrishavlin added this to the 4.4.1 milestone Nov 26, 2024
@chrishavlin
Copy link
Contributor

well hmmm....

FAILED yt/visualization/tests/test_offaxisprojection.py::test_off_axis_octree - AssertionError: assert unyt_quantity(0.38448323, '(dimensionless)') < 0.2

Maybe that check I made you add is too restrictive?

@chrishavlin
Copy link
Contributor

So part of the trickiness is that the test is highly variable between runs: I just ran the code from test_off_axis_octree locally, recording the maximum of the absolute value in the difference and over 10 runs, the max abs fractional difference was [0.17469636745540013, 0.22734252938752475, 0.5588387477439833, 0.06742315690744759, 0.19014792348598422, 0.32429411325319835, 0.24292832837751724, 0.20697617456665565, 0.3351829388729374, 0.09990611014962943]. Setting a seed would help make it deterministic... but the large variation might be indicative of some other issue? Or maybe it's expected and you should just revert the comparison of the absolute value in difference?

I did check what the diffs look like, the large differences are always at cell edges. e.g., for one case:

image

for which the standard axis aligned ProjectionPlot with annotated cells looks like:

Screenshot from 2024-11-26 10-39-06

@cphyc
Copy link
Member Author

cphyc commented Nov 27, 2024

OK - I've actually uncovered a funny one.

import numpy as np
import yt
import matplotlib.pyplot as plt
ds = yt.load("output_00080")
p1 = yt.ProjectionPlot(
    ds,
    "x",
    ("gas", "density"),
    center=[0.5] * 3,
    width=.8,
    #weight_field=("gas", "density"),
)
p2 = yt.OffAxisProjectionPlot(
    ds,
    [1, 0, 0],
    ("gas", "density"),
    center=[0.5] * 3,
    width=.8,
    #weight_field=("gas", "density"),
)

# Note: due to our implementation, the off-axis projection will have a
# slightly blurred cell edges so we can't do an exact comparison
v1, v2 = p1.frb["gas", "density"], p2.frb["gas", "density"]

fig = plt.figure()
plt.imshow(v1, origin="lower", norm=plt.matplotlib.colors.LogNorm())
plt.colorbar()
fig.savefig("/tmp/1.png")

fig = plt.figure()
plt.imshow(v2, origin="lower", norm=plt.matplotlib.colors.LogNorm())
plt.colorbar()
fig.savefig("/tmp/2.png")

fig = plt.figure()
plt.imshow(v1 / v2, origin="lower")
plt.colorbar()
fig.savefig("/tmp/3.png")
print(np.median(v1 / v2), np.mean(v1 / v2), np.std(v1 / v2))

This returns:

Projection Off-axis projection Difference
1 2 3

The funny thing is that the ratio between the on-axis and the off-axis projection is equal (within some uncertainties) to plot_width * 0.5675 (above, plot_width = 0.8). This is the case for a RAMSES dataset (here), but the same can be obtained using fake_random_ds instead, yielding the same value.

As much as I see how I could have missed a plot_width factor, what the heck is 0.5675?

@chrishavlin
Copy link
Contributor

chrishavlin commented Nov 27, 2024

ya, I'm thoroughly confused now. that is a weird number to be off by... and looking at projections of index fields like ('index', 'ones') or ('index', 'y'), the cell edges pop out very clearly and the projected values are all larger than they should be by a pretty big fraction, even in cell interiors (example notebook).

@cphyc
Copy link
Member Author

cphyc commented Nov 28, 2024

@chrishavlin I think fixing this bug above is definitely important, but may be part of another PR. Would you be fine with the following plan?

  1. Roll back the test on the absolute value of the difference, since this is hard to get stable,
  2. Create an issue with milestone 4.4.1 describing the mismatch when using a projection with no weight,
  3. Have this PR merged as is?

Regarding point 1, it think it is possible to correct the cell-edges effects, but I don't have the time to think carefully about it. If you are adamant this should be done, I'm happy to open another issue to keep track of it.

@chrishavlin
Copy link
Contributor

@cphyc sounds like a good plan to me!

cphyc added 2 commits December 2, 2024 17:24
Fix units for older versions of unyt

Move comment about periodic boundary close to statement
@cphyc
Copy link
Member Author

cphyc commented Dec 2, 2024

@chrishavlin, I have opened the issue and rebased this PR.

@chrishavlin chrishavlin enabled auto-merge December 2, 2024 16:27
@chrishavlin chrishavlin merged commit 3f9765b into yt-project:main Dec 2, 2024
13 checks passed
meeseeksmachine pushed a commit to meeseeksmachine/yt that referenced this pull request Dec 2, 2024
@cphyc cphyc deleted the fix-off-axis branch December 3, 2024 10:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants