From 22c7e34f86766ae71da639573d53c7de8f5a9d01 Mon Sep 17 00:00:00 2001 From: biboyd Date: Mon, 15 Apr 2024 18:49:30 -0400 Subject: [PATCH 1/4] add path to reduce images without considering opacity --- yt/utilities/amr_kdtree/amr_kdtools.py | 45 ++++++++++--------- yt/utilities/amr_kdtree/amr_kdtree.py | 4 +- .../volume_rendering/render_source.py | 4 +- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/yt/utilities/amr_kdtree/amr_kdtools.py b/yt/utilities/amr_kdtree/amr_kdtools.py index f42e3a36f7f..fb933291f4f 100644 --- a/yt/utilities/amr_kdtree/amr_kdtools.py +++ b/yt/utilities/amr_kdtree/amr_kdtools.py @@ -3,34 +3,39 @@ from yt.funcs import mylog -def receive_and_reduce(comm, incoming_rank, image, add_to_front): +def receive_and_reduce(comm, incoming_rank, image, add_to_front, use_opacity): mylog.debug("Receiving image from %04i", incoming_rank) # mylog.debug( '%04i receiving image from %04i'%(self.comm.rank,back.owner)) arr2 = comm.recv_array(incoming_rank, incoming_rank).reshape( (image.shape[0], image.shape[1], image.shape[2]) ) - if add_to_front: - front = arr2 - back = image + if use_opacity: + if add_to_front: + front = arr2 + back = image + else: + front = image + back = arr2 + + if image.shape[2] == 3: + # Assume Projection Camera, Add + np.add(image, front, image) + return image + + ta = 1.0 - front[:, :, 3] + np.maximum(ta, 0.0, ta) + # This now does the following calculation, but in a memory + # conservative fashion + # image[:,:,i ] = front[:,:,i] + ta*back[:,:,i] + image = back.copy() + for i in range(4): + np.multiply(image[:, :, i], ta, image[:, :, i]) + np.add(image, front, image) + else: - front = image - back = arr2 + np.add(image, arr2, image) - if image.shape[2] == 3: - # Assume Projection Camera, Add - np.add(image, front, image) - return image - - ta = 1.0 - front[:, :, 3] - np.maximum(ta, 0.0, ta) - # This now does the following calculation, but in a memory - # conservative fashion - # image[:,:,i ] = front[:,:,i] + ta*back[:,:,i] - image = back.copy() - for i in range(4): - np.multiply(image[:, :, i], ta, image[:, :, i]) - np.add(image, front, image) return image diff --git a/yt/utilities/amr_kdtree/amr_kdtree.py b/yt/utilities/amr_kdtree/amr_kdtree.py index b8378c66715..527e93e3ae0 100644 --- a/yt/utilities/amr_kdtree/amr_kdtree.py +++ b/yt/utilities/amr_kdtree/amr_kdtree.py @@ -294,7 +294,7 @@ def get_reduce_owners(self): owners[temp.node_id] = owners[temp.left.node_id] return owners - def reduce_tree_images(self, image, viewpoint): + def reduce_tree_images(self, image, viewpoint, use_opacity): if self.comm.size <= 1: return image myrank = self.comm.rank @@ -307,7 +307,7 @@ def reduce_tree_images(self, image, viewpoint): split_pos = node.parent.get_split_pos() add_to_front = viewpoint[split_dim] >= split_pos image = receive_and_reduce( - self.comm, owners[node.parent.right.node_id], image, add_to_front + self.comm, owners[node.parent.right.node_id], image, add_to_front, use_opacity, ) if node.parent.node_id == 1: break diff --git a/yt/visualization/volume_rendering/render_source.py b/yt/visualization/volume_rendering/render_source.py index 65d6cd2e557..42ce43c067c 100644 --- a/yt/visualization/volume_rendering/render_source.py +++ b/yt/visualization/volume_rendering/render_source.py @@ -607,7 +607,9 @@ def render(self, camera, zbuffer=None): def finalize_image(self, camera, image): if self._volume is not None: - image = self.volume.reduce_tree_images(image, camera.lens.viewpoint) + image = self.volume.reduce_tree_images( + image, camera.lens.viewpoint, self.transfer_function.grey_opacity + ) return super().finalize_image(camera, image) From 0de8d6329ab640917a532f2dbfbf6a3663eb946a Mon Sep 17 00:00:00 2001 From: biboyd Date: Wed, 17 Apr 2024 12:01:36 -0400 Subject: [PATCH 2/4] make use_opacity a kwarg --- yt/utilities/amr_kdtree/amr_kdtree.py | 8 ++++++-- yt/visualization/volume_rendering/render_source.py | 6 ++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/yt/utilities/amr_kdtree/amr_kdtree.py b/yt/utilities/amr_kdtree/amr_kdtree.py index 527e93e3ae0..30be6e3c868 100644 --- a/yt/utilities/amr_kdtree/amr_kdtree.py +++ b/yt/utilities/amr_kdtree/amr_kdtree.py @@ -294,7 +294,7 @@ def get_reduce_owners(self): owners[temp.node_id] = owners[temp.left.node_id] return owners - def reduce_tree_images(self, image, viewpoint, use_opacity): + def reduce_tree_images(self, image, viewpoint, use_opacity=True): if self.comm.size <= 1: return image myrank = self.comm.rank @@ -307,7 +307,11 @@ def reduce_tree_images(self, image, viewpoint, use_opacity): split_pos = node.parent.get_split_pos() add_to_front = viewpoint[split_dim] >= split_pos image = receive_and_reduce( - self.comm, owners[node.parent.right.node_id], image, add_to_front, use_opacity, + self.comm, + owners[node.parent.right.node_id], + image, + add_to_front, + use_opacity, ) if node.parent.node_id == 1: break diff --git a/yt/visualization/volume_rendering/render_source.py b/yt/visualization/volume_rendering/render_source.py index 42ce43c067c..c7d0beed2e4 100644 --- a/yt/visualization/volume_rendering/render_source.py +++ b/yt/visualization/volume_rendering/render_source.py @@ -608,8 +608,10 @@ def render(self, camera, zbuffer=None): def finalize_image(self, camera, image): if self._volume is not None: image = self.volume.reduce_tree_images( - image, camera.lens.viewpoint, self.transfer_function.grey_opacity - ) + image, + camera.lens.viewpoint, + use_opacity=self.transfer_function.grey_opacity, + ) return super().finalize_image(camera, image) From 10f3ef40d72c34184f0688732a43bcb1bfd70a1a Mon Sep 17 00:00:00 2001 From: Brendan Boyd <42776109+biboyd@users.noreply.github.com> Date: Wed, 17 Apr 2024 14:53:52 -0400 Subject: [PATCH 3/4] add more/force keyword arguments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Clément Robert --- yt/utilities/amr_kdtree/amr_kdtools.py | 2 +- yt/utilities/amr_kdtree/amr_kdtree.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/yt/utilities/amr_kdtree/amr_kdtools.py b/yt/utilities/amr_kdtree/amr_kdtools.py index fb933291f4f..990010c51aa 100644 --- a/yt/utilities/amr_kdtree/amr_kdtools.py +++ b/yt/utilities/amr_kdtree/amr_kdtools.py @@ -3,7 +3,7 @@ from yt.funcs import mylog -def receive_and_reduce(comm, incoming_rank, image, add_to_front, use_opacity): +def receive_and_reduce(comm, incoming_rank, image, add_to_front, *, use_opacity=True): mylog.debug("Receiving image from %04i", incoming_rank) # mylog.debug( '%04i receiving image from %04i'%(self.comm.rank,back.owner)) arr2 = comm.recv_array(incoming_rank, incoming_rank).reshape( diff --git a/yt/utilities/amr_kdtree/amr_kdtree.py b/yt/utilities/amr_kdtree/amr_kdtree.py index 30be6e3c868..861f4320c14 100644 --- a/yt/utilities/amr_kdtree/amr_kdtree.py +++ b/yt/utilities/amr_kdtree/amr_kdtree.py @@ -294,7 +294,7 @@ def get_reduce_owners(self): owners[temp.node_id] = owners[temp.left.node_id] return owners - def reduce_tree_images(self, image, viewpoint, use_opacity=True): + def reduce_tree_images(self, image, viewpoint, *, use_opacity=True): if self.comm.size <= 1: return image myrank = self.comm.rank @@ -311,7 +311,7 @@ def reduce_tree_images(self, image, viewpoint, use_opacity=True): owners[node.parent.right.node_id], image, add_to_front, - use_opacity, + use_opacity=use_opacity, ) if node.parent.node_id == 1: break From a104859c9d86800af28451d46c2ea5e3c6dd8d8a Mon Sep 17 00:00:00 2001 From: biboyd Date: Wed, 17 Apr 2024 15:01:29 -0400 Subject: [PATCH 4/4] return image early --- yt/utilities/amr_kdtree/amr_kdtools.py | 46 +++++++++++++------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/yt/utilities/amr_kdtree/amr_kdtools.py b/yt/utilities/amr_kdtree/amr_kdtools.py index 990010c51aa..01d7327a267 100644 --- a/yt/utilities/amr_kdtree/amr_kdtools.py +++ b/yt/utilities/amr_kdtree/amr_kdtools.py @@ -10,31 +10,31 @@ def receive_and_reduce(comm, incoming_rank, image, add_to_front, *, use_opacity= (image.shape[0], image.shape[1], image.shape[2]) ) - if use_opacity: - if add_to_front: - front = arr2 - back = image - else: - front = image - back = arr2 - - if image.shape[2] == 3: - # Assume Projection Camera, Add - np.add(image, front, image) - return image - - ta = 1.0 - front[:, :, 3] - np.maximum(ta, 0.0, ta) - # This now does the following calculation, but in a memory - # conservative fashion - # image[:,:,i ] = front[:,:,i] + ta*back[:,:,i] - image = back.copy() - for i in range(4): - np.multiply(image[:, :, i], ta, image[:, :, i]) - np.add(image, front, image) + if not use_opacity: + np.add(image, arr2, image) + return image + if add_to_front: + front = arr2 + back = image else: - np.add(image, arr2, image) + front = image + back = arr2 + + if image.shape[2] == 3: + # Assume Projection Camera, Add + np.add(image, front, image) + return image + + ta = 1.0 - front[:, :, 3] + np.maximum(ta, 0.0, ta) + # This now does the following calculation, but in a memory + # conservative fashion + # image[:,:,i ] = front[:,:,i] + ta*back[:,:,i] + image = back.copy() + for i in range(4): + np.multiply(image[:, :, i], ta, image[:, :, i]) + np.add(image, front, image) return image