silx icon indicating copy to clipboard operation
silx copied to clipboard

Warning on statshandler

Open vallsv opened this issue 3 years ago • 1 comments
trafficstars

No idea if it was already fixed, but here is a warning from python 3.7, and silx 1.0.0 (maybe beta)

2022-01-27 19:03:04,091 eh3 3065 py.warnings WARNING : 
/.../bliss_dev/lib/python3.7/site-packages/silx/gui/plot/stats/statshandler.py:78:
FutureWarning: Format strings passed to MaskedConstant are ignored,
but in future may error or produce different behavior
  return self.formatter.format(val)

vallsv avatar Mar 10 '22 13:03 vallsv

From the information you provide this could come from something like a MaskedArray provided to StatsHandler.format. On 'recent' numpy version __format__ function is redefined and raise this kind of error.

The thing is that we are calling this function to format the results which are expected to be a scalar or a tuple (or a list in fact) If the result turn out to be a masked array then we should be able to handle it by adding something like:

      if isinstance(val, numpy.ma.masked_array):
          val = val.compressed()

But here I wonder why the result is a masked array. That I don't think it should happen.

@vallsv Do you have more input ? Like a way to reproduce it or do you know if this log comes from the default Stats classes from silx or from a home made one ?

payno avatar Apr 05 '22 11:04 payno

I managed to reproduce, this is the StatCOM that causes trouble when there is no data to compute it. In this case, the whole array is masked and the numpy.sum returns a MaskedConstant... as well as all computations further down...

To reproduce:

from silx import sx
w = sx.plot((1, 2, 3))                                                                                              
w.getStatsAction().trigger()                                                                                        
w.getStatsWidget().setStatsOnVisibleData(True)                                                                      
w.getXAxis().setLimits(-2, -1) # Move data outside of visible area                                                          
silx/gui/plot/stats/statshandler.py:78: FutureWarning: Format strings passed to MaskedConstant are ignored, but in future may error or produce different behavior
  return self.formatter.format(val)

t20100 avatar Sep 05 '22 13:09 t20100

Also for any Stats initialised with a function (e.g., numpy.mean), when data is outside the plot, the function is given a fully masked array, leading to the same warning.

t20100 avatar Sep 05 '22 13:09 t20100