Skip to content

Commit

Permalink
Merge branch 'avoid-nan-cast-warning' into 'development'
Browse files Browse the repository at this point in the history
Silence Runtime warning when casting (masking) NaN values to color

See merge request damask/DAMASK!847
  • Loading branch information
eisenlohr committed Nov 20, 2023
2 parents 9a12bf5 + c0a97a9 commit 873ca78
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions python/damask/_colormap.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,18 +296,20 @@ def shade(self,
RGBA image of shaded data.
"""
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)
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 \
(bounds[0],bounds[1])

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.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)/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
Expand Down

0 comments on commit 873ca78

Please sign in to comment.