Skip to content

Commit

Permalink
ENH: Add more precision to exporting to VTK format. Require format.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwboth committed Aug 19, 2024
1 parent e133b93 commit 1b20eb0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
11 changes: 10 additions & 1 deletion src/darsia/image/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1637,7 +1637,16 @@ def to_vtk(self, path: Union[str, Path], name: Optional[str] = None) -> None:
name = self.name
if name is None:
name = "data"
darsia.plotting.to_vtk(path, [(name, self)])
darsia.plotting.to_vtk(
path,
[
(
name,
self,
darsia.Format.SCALAR if self.scalar else darsia.Format.TENSOR,
)
],
)

# ! ---- Auxiliary routines

Expand Down
22 changes: 11 additions & 11 deletions src/darsia/measure/wasserstein.py
Original file line number Diff line number Diff line change
Expand Up @@ -1837,17 +1837,17 @@ def wasserstein_distance_to_vtk(
"""
data = [
(key, info[key])
for key in [
"src",
"dst",
"mass_diff",
"flux",
"weighted_flux",
"pressure",
"transport_density",
"weight",
"weight_inv",
(key, info[key], format)
for key, format in [
("src", darsia.Format.SCALAR),
("dst", darsia.Format.SCALAR),
("mass_diff", darsia.Format.SCALAR),
("flux", darsia.Format.VECTOR),
("weighted_flux", darsia.Format.VECTOR),
("pressure", darsia.Format.SCALAR),
("transport_density", darsia.Format.SCALAR),
("weight", darsia.Format.TENSOR),
("weight_inv", darsia.Format.TENSOR),
]
]
darsia.plotting.to_vtk(path, data)
26 changes: 18 additions & 8 deletions src/darsia/utils/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ def plot_2d_wasserstein_distance(

def to_vtk(
path: Union[str, Path],
data: list[tuple[Union[darsia.Image, np.ndarray], str]],
data: list[tuple[Union[darsia.Image, np.ndarray, darsia.Format], str]],
) -> None:
"""Write data to a VTK file.
Args:
path (Union[str, Path]): path to the VTK file
data (list[tuple[Union[darsia.Image, np.ndarray], str]]): data to write, includes
the data and the name of the data. Require at least one data point to be an
image.
data (list[tuple[Union[darsia.Image, np.ndarray, darsia.Format], str]]): data to
write, includes the data and the name of the data. Require at least one data
point to be an image.
NOTE: Requires pyevtk to be installed.
Expand All @@ -134,7 +134,7 @@ def to_vtk(
# Check whether the data contains at least one image, and pick the first one
image = None
for d in data:
name, img = d
name, img, format = d
if isinstance(img, darsia.Image):
image = img
break
Expand Down Expand Up @@ -179,7 +179,7 @@ def conditional_flip(x, cond):
# Convert cell data to right format
cellData = {}
for d in data:
name, img = d
name, img, format = d
if isinstance(img, darsia.Image):
img = img.img.copy()
assert isinstance(img, np.ndarray), "Data must be of type np.ndarray."
Expand Down Expand Up @@ -219,9 +219,19 @@ def conditional_flip(x, cond):
if image.space_dim == 1:
img = tuple(img)
elif image.space_dim == 2:
img = (-img[1], img[0], img[2])
if format == darsia.Format.VECTOR:
img = (-img[1], img[0], img[2])
elif format == darsia.Format.TENSOR:
img = (img[1], img[0], img[2])
else:
raise ValueError("Format not supported.")
elif image.space_dim == 3:
img = (-img[1], img[2], img[0])
if format == darsia.Format.VECTOR:
img = (-img[1], img[2], img[0])
elif format == darsia.Format.TENSOR:
img = (img[1], img[2], img[0])
else:
raise ValueError("Format not supported.")
cellData[name] = img

# Make directory if necessary
Expand Down

0 comments on commit 1b20eb0

Please sign in to comment.