From d8baf3c754f46a65f44fe7cf61a4a32bebaaea0b Mon Sep 17 00:00:00 2001 From: Philip Eisenlohr Date: Sat, 18 Nov 2023 20:00:55 -0500 Subject: [PATCH 1/3] reset field to average of bounds avoiding casting masked NaNs --- python/damask/_colormap.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/python/damask/_colormap.py b/python/damask/_colormap.py index ccb8d3d70..5af72acd8 100644 --- a/python/damask/_colormap.py +++ b/python/damask/_colormap.py @@ -296,7 +296,7 @@ class Colormap(mpl.colors.ListedColormap): RGBA image of shaded data. """ - mask = np.logical_not(np.isnan(field) if gap is None else \ + mask = np.logical_not(np.isnan(field) if gap is None else np.logical_or (np.isnan(field), field == gap)) # mask NaN (and gap if present) l,r = (field[mask].min(),field[mask].max()) if bounds is None else \ @@ -305,9 +305,11 @@ class Colormap(mpl.colors.ListedColormap): if abs(delta := r-l) * 1e8 <= (avg := 0.5*abs(r+l)): # delta is similar to numerical noise l,r = (l-0.5*avg*np.sign(delta),r+0.5*avg*np.sign(delta)) # extend range to have actual data centered within + field[np.isnan(field)] = (l+r)/2 + return Image.fromarray( (np.dstack(( - self.colors[(np.round(np.clip((field-l)/delta,0.0,1.0)*(self.N-1))).astype(np.uint16),:3], + self.colors[np.round(np.clip((field-l)/(r-l),0.0,1.0)*(self.N-1)).astype(np.uint16),:3], mask.astype(float) ) )*255 From 39dacbc9727d154e367ce8aab3e1344b6d377794 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Mon, 20 Nov 2023 11:49:59 +0100 Subject: [PATCH 2/3] easier to understand --- python/damask/_colormap.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/damask/_colormap.py b/python/damask/_colormap.py index 5af72acd8..ad7aa286b 100644 --- a/python/damask/_colormap.py +++ b/python/damask/_colormap.py @@ -297,7 +297,7 @@ class Colormap(mpl.colors.ListedColormap): """ mask = np.logical_not(np.isnan(field) if gap is None else - np.logical_or (np.isnan(field), field == gap)) # mask NaN (and gap if present) + np.logical_or(np.isnan(field), field == gap)) # mask NaN (and gap if present) l,r = (field[mask].min(),field[mask].max()) if bounds is None else \ (bounds[0],bounds[1]) From c0a97a93fe4a2606a45eff926f412ceb3137762e Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Mon, 20 Nov 2023 12:27:23 +0100 Subject: [PATCH 3/3] do not overwrite input, use specialized function --- python/damask/_colormap.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/damask/_colormap.py b/python/damask/_colormap.py index ad7aa286b..f4834da78 100644 --- a/python/damask/_colormap.py +++ b/python/damask/_colormap.py @@ -305,11 +305,11 @@ class Colormap(mpl.colors.ListedColormap): if abs(delta := r-l) * 1e8 <= (avg := 0.5*abs(r+l)): # delta is similar to numerical noise l,r = (l-0.5*avg*np.sign(delta),r+0.5*avg*np.sign(delta)) # extend range to have actual data centered within - field[np.isnan(field)] = (l+r)/2 + field_ = np.nan_to_num(field, nan=(l+r)/2, posinf=r, neginf=l) return Image.fromarray( (np.dstack(( - self.colors[np.round(np.clip((field-l)/(r-l),0.0,1.0)*(self.N-1)).astype(np.uint16),:3], + self.colors[np.round(np.clip((field_-l)/(r-l),0.0,1.0)*(self.N-1)).astype(np.uint16),:3], mask.astype(float) ) )*255