-
-
Notifications
You must be signed in to change notification settings - Fork 7
Add CartesianPlotter
for visualizing active region magnetic fields in a local Cartesian box
#195
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
Conversation
Thank you for this, I will try to review it in next two weeks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me - I left a few suggestions
sunkit_pyvista/plotter.py
Outdated
self.grid = VectorGrid(vectors.astype(np.float64), *args, **kwargs) | ||
self.xcoords = self.grid.xcoords.astype(np.float64) | ||
self.ycoords = self.grid.ycoords.astype(np.float64) | ||
self.zcoords = self.grid.zcoords.astype(np.float64) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the casting to float64
needed here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.grid = VectorGrid(vectors.astype(np.float64), *args, **kwargs)
If the vectors
of VectorGrid
is not of type float64
(e.g., float32
), it throws the following error:
Code
from streamtracer import StreamTracer, VectorGrid
grid = VectorGrid(vectors.astype(np.float32), grid_spacing=(1, 1, 1))
tracer = StreamTracer(10000, 0.1)
tracer.trace(seeds, grid)
Error
.../streamtracer/streamline.py:329, in StreamTracer.trace(self, seeds, grid, direction)
...
--> 329 xs_f, ns_f, ROT_f = trace_streamlines(
...
TypeError: argument 'values': 'ndarray' object cannot be converted to 'PyArray<T, D>'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.xcoords = self.grid.xcoords.astype(np.float64)
self.ycoords = self.grid.ycoords.astype(np.float64)
self.zcoords = self.grid.zcoords.astype(np.float64)
For these three lines, I don't remember exactly, but I think I added them to avoid the following warning from pyvista.StructuredGrid
:
Code
import pyvista as pv
from streamtracer import VectorGrid
grid = VectorGrid(vectors.astype(np.float64), grid_spacing=(1, 1, 1))
x, y, z = np.meshgrid(grid.xcoords, grid.ycoords, grid.zcoords, indexing="ij")
mesh = pv.StructuredGrid(x, y, z)
Warning
UserWarning: Points is not a float type. This can cause issues when transforming or applying filters. Casting to `np.float32`. Disable this by passing `force_float=False`.
warnings.warn(
However, since PyVista internally casts to np.float32
, casting the coordinates to np.float64
is unnecessary. Perhaps casting to np.float32
, or just removing the casting and letting the warning be shown, could both be reasonable options. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like showing warnings but maybe its worth not casting and letting the warning show so users can do the casting themselves?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. Showing the warning may help users better understand what's going on.
kwargs : dict | ||
Keyword arguments for `pyvista.Plotter.add_mesh`. | ||
""" | ||
tracer = StreamTracer(max_steps, step_size) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
VTK / pyvista has it's own streamline functionality which is probably a more natural fit for this package. Are you using streamtracer for some specific reason?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At first, I used PyVista's streamline function: https://github.com/mgjeon/magnetic_field_line/blob/main/magplot/base.py#L159
My goal was to trace field lines starting from the bottom, so I used the following code:
seed = pv.Plane(center=(self.grid.center[0], self.grid.center[1], 0), direction=(0,0,1),
i_size=i_size, j_size=j_size,
i_resolution=i_resolution, j_resolution=j_resolution)
strl = self.grid.streamlines_from_source(seed,
vectors='vector',
max_time=180,
initial_step_length=0.1,
integration_direction='both')
because I don't know how to provide seed points directly to PyVista's streamline API.
So I used StreamTracer
instead, which accepts seed points directly via streamtracer.StreamTracer.trace(seeds, grid, direction=0)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would be interested in the difference but if Streamtracer works, is it an issue if we added it as a dependency?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think people who use magnetic field line tracing may already use sunkit-magex, which has streamtracer as a dependency. So, adding it as a dependency is unlikely to be a major issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the dependency is a big issue, it's just the cost of bouncing around between VTK and back. I'm happy to merge this like this, maybe we could open an issue though?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
Hi @mgjeon, sorry for the late reply. Is it possible to add both examples as unit tests into the PR? we can create a figure test for them as well, so we can compare. Or even as an example? What are your thoughts? |
I can add both to the sunkit-pyvista/examples folder as an example. But I’m not familiar with writing unit tests. Which code should I refer to? |
Yeah, we can just start with the examples and then expand from there. For the unit tests, I wonder if we can create something like this: https://github.com/sunpy/sunkit-pyvista/blob/main/sunkit_pyvista/tests/test_plotting.py#L31 |
@nabobalis Sorry for the late reply. I added a figure test and example only for the analytical field case, since the numerical field (~1.5 GB) is too large and the usage is the same for both. |
Co-authored-by: David Stansby <[email protected]>
…hinx config to include streamtracer docs
Thank you! I rebased the PR with current origin and ran the pre commit. It looks great to me. |
I will fix the figure issues in a follow up PR |
Closes #194 by adding a new plotter
CartesianPlotter
for active region magnetic fields.Description
CartesianPlotter
, enabling visualization of active region magnetic fields in a local Cartesian coordinate system.RobertJaro/NF2
intosunkit-magex
(see this issue).Minimal Example
1. Analytical Magnetic Field
(Adapted from https://github.com/antyeates1983/flhtools)
Define magnetic field array and seed points
Plot
2. Numerical Magnetic Field
Download https://hinode.isee.nagoya-u.ac.jp/nlfff_database/v12/11890/20131106/11890_20131106_003600.nc
Define magnetic field array and seed points
Plot
Feedback
You can test
CartesianPlotter
in this Colab notebook.I'm not sure if this API design is appropriate. Also, I want to extend AR visualization to the spherical coordinate system, but I'm uncertain how to approach it. Please feel free to share your thoughts and suggestions.