From fcce09270e46913bb6c18f6871d28d288bf65e5c Mon Sep 17 00:00:00 2001 From: ehavazli Date: Thu, 7 Mar 2024 11:27:17 -0800 Subject: [PATCH 1/5] improve memory usage by avoiding creating separate mask arrays --- src/mintpy/objects/conncomp.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/mintpy/objects/conncomp.py b/src/mintpy/objects/conncomp.py index 58a243516..0273f2847 100644 --- a/src/mintpy/objects/conncomp.py +++ b/src/mintpy/objects/conncomp.py @@ -357,19 +357,20 @@ def unwrap_conn_comp(self, unw, radius=50, ramp_type=None, print_msg=False): def plot_bridge(self, ax, cmap='jet', radius=50): # label background ax.imshow(self.labelImg, cmap=cmap, interpolation='nearest') - # bridges + + # plot bridges for bridge in self.bridges: ax.plot([bridge['x0'], bridge['x1']], [bridge['y0'], bridge['y1']], 'w-', lw=1) + # endpoint window if radius > 0: aoi_mask0, aoi_mask1 = self.get_bridge_endpoint_aoi_mask(bridge, radius=radius) - label_mask0 = self.labelImg == bridge['label0'] - label_mask1 = self.labelImg == bridge['label1'] - mask0 = np.ma.masked_where(~(aoi_mask0*label_mask0), np.zeros(self.labelImg.shape)) - mask1 = np.ma.masked_where(~(aoi_mask1*label_mask1), np.zeros(self.labelImg.shape)) - ax.imshow(mask0, cmap='gray', alpha=0.3, vmin=0, vmax=1) - ax.imshow(mask1, cmap='gray', alpha=0.3, vmin=0, vmax=1) + + # Overlay bridge regions directly using plot function + ax.plot(np.nonzero(aoi_mask0)[1], np.nonzero(aoi_mask0)[0], 'gray', alpha=0.3) + ax.plot(np.nonzero(aoi_mask1)[1], np.nonzero(aoi_mask1)[0], 'gray', alpha=0.3) + # reference pixel ax.plot(self.refX, self.refY, 'ks', ms=2) return ax From a4d23effe6836207e2654a2776eb9d625031be74 Mon Sep 17 00:00:00 2001 From: ehavazli Date: Tue, 19 Mar 2024 16:39:09 -0700 Subject: [PATCH 2/5] mask out unused pixels --- src/mintpy/objects/conncomp.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/mintpy/objects/conncomp.py b/src/mintpy/objects/conncomp.py index 0273f2847..239b5df2e 100644 --- a/src/mintpy/objects/conncomp.py +++ b/src/mintpy/objects/conncomp.py @@ -366,10 +366,11 @@ def plot_bridge(self, ax, cmap='jet', radius=50): # endpoint window if radius > 0: aoi_mask0, aoi_mask1 = self.get_bridge_endpoint_aoi_mask(bridge, radius=radius) - + label_mask0 = self.labelImg == bridge['label0'] + label_mask1 = self.labelImg == bridge['label1'] # Overlay bridge regions directly using plot function - ax.plot(np.nonzero(aoi_mask0)[1], np.nonzero(aoi_mask0)[0], 'gray', alpha=0.3) - ax.plot(np.nonzero(aoi_mask1)[1], np.nonzero(aoi_mask1)[0], 'gray', alpha=0.3) + ax.plot(np.nonzero(aoi_mask0*label_mask0)[1], np.nonzero(aoi_mask0*label_mask0)[0], 'gray', alpha=0.3) + ax.plot(np.nonzero(aoi_mask1*label_mask1)[1], np.nonzero(aoi_mask1*label_mask1)[0], 'gray', alpha=0.3) # reference pixel ax.plot(self.refX, self.refY, 'ks', ms=2) From b97ca06ca1564bcc9ba82484121e0def07e3a5f1 Mon Sep 17 00:00:00 2001 From: Zhang Yunjun Date: Wed, 20 Mar 2024 10:27:15 +0800 Subject: [PATCH 3/5] more comments --- src/mintpy/objects/conncomp.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/mintpy/objects/conncomp.py b/src/mintpy/objects/conncomp.py index 239b5df2e..4d039898f 100644 --- a/src/mintpy/objects/conncomp.py +++ b/src/mintpy/objects/conncomp.py @@ -355,11 +355,11 @@ def unwrap_conn_comp(self, unw, radius=50, ramp_type=None, print_msg=False): def plot_bridge(self, ax, cmap='jet', radius=50): - # label background + # background label ax.imshow(self.labelImg, cmap=cmap, interpolation='nearest') - # plot bridges for bridge in self.bridges: + # bridges ax.plot([bridge['x0'], bridge['x1']], [bridge['y0'], bridge['y1']], 'w-', lw=1) @@ -368,7 +368,9 @@ def plot_bridge(self, ax, cmap='jet', radius=50): aoi_mask0, aoi_mask1 = self.get_bridge_endpoint_aoi_mask(bridge, radius=radius) label_mask0 = self.labelImg == bridge['label0'] label_mask1 = self.labelImg == bridge['label1'] - # Overlay bridge regions directly using plot function + # Note by Emre Mar 2024: overlay bridge regions directly using plot() function, + # instead of using np.ma.masked_where() with imshow(), + # to save memory while calling this func in a loop (https://github.com/insarlab/MintPy/pull/1155) ax.plot(np.nonzero(aoi_mask0*label_mask0)[1], np.nonzero(aoi_mask0*label_mask0)[0], 'gray', alpha=0.3) ax.plot(np.nonzero(aoi_mask1*label_mask1)[1], np.nonzero(aoi_mask1*label_mask1)[0], 'gray', alpha=0.3) From 4cc04a555fed577de9d7f19193f3b872eb59503b Mon Sep 17 00:00:00 2001 From: Zhang Yunjun Date: Wed, 20 Mar 2024 10:28:21 +0800 Subject: [PATCH 4/5] Update conncomp.py --- src/mintpy/objects/conncomp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mintpy/objects/conncomp.py b/src/mintpy/objects/conncomp.py index 4d039898f..414001357 100644 --- a/src/mintpy/objects/conncomp.py +++ b/src/mintpy/objects/conncomp.py @@ -368,7 +368,7 @@ def plot_bridge(self, ax, cmap='jet', radius=50): aoi_mask0, aoi_mask1 = self.get_bridge_endpoint_aoi_mask(bridge, radius=radius) label_mask0 = self.labelImg == bridge['label0'] label_mask1 = self.labelImg == bridge['label1'] - # Note by Emre Mar 2024: overlay bridge regions directly using plot() function, + # Note by Emre, Mar 2024: overlay bridge regions directly using plot() function, # instead of using np.ma.masked_where() with imshow(), # to save memory while calling this func in a loop (https://github.com/insarlab/MintPy/pull/1155) ax.plot(np.nonzero(aoi_mask0*label_mask0)[1], np.nonzero(aoi_mask0*label_mask0)[0], 'gray', alpha=0.3) From 254f2b5e7ed70994ac3d45ff0b00418293fb2c60 Mon Sep 17 00:00:00 2001 From: Zhang Yunjun Date: Wed, 20 Mar 2024 10:32:21 +0800 Subject: [PATCH 5/5] Update conncomp.py --- src/mintpy/objects/conncomp.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mintpy/objects/conncomp.py b/src/mintpy/objects/conncomp.py index 414001357..a00aacb81 100644 --- a/src/mintpy/objects/conncomp.py +++ b/src/mintpy/objects/conncomp.py @@ -369,8 +369,8 @@ def plot_bridge(self, ax, cmap='jet', radius=50): label_mask0 = self.labelImg == bridge['label0'] label_mask1 = self.labelImg == bridge['label1'] # Note by Emre, Mar 2024: overlay bridge regions directly using plot() function, - # instead of using np.ma.masked_where() with imshow(), - # to save memory while calling this func in a loop (https://github.com/insarlab/MintPy/pull/1155) + # to save memory while calling this func in a loop by avoiding creating separate + # mask arrays (https://github.com/insarlab/MintPy/pull/1155) ax.plot(np.nonzero(aoi_mask0*label_mask0)[1], np.nonzero(aoi_mask0*label_mask0)[0], 'gray', alpha=0.3) ax.plot(np.nonzero(aoi_mask1*label_mask1)[1], np.nonzero(aoi_mask1*label_mask1)[0], 'gray', alpha=0.3)