gonum / plot

A repository for plotting and visualizing data
BSD 3-Clause "New" or "Revised" License
2.74k stars 203 forks source link

plotter: HeatMap panics if h.Min = -inf #434

Open btracey opened 6 years ago

btracey commented 6 years ago

While calling plot.Save

gonum.org/v1/plot/plotter.(*HeatMap).Plot(0xc42006a360, 0x13d8220, 0xc42052c420, 0x403e70933333332f, 0x4037d0c666666662, 0x4071f23ecccccccd, 0x4071f3fb9999999a, 0xc42042e000)
    /Users/brendan/Documents/mygo/src/gonum.org/v1/plot/plotter/heat.go:171 +0x1268
gonum.org/v1/plot.(*Plot).Draw(0xc42042e000, 0x13d8120, 0xc420088d80, 0x0, 0x0, 0x4072000000000000, 0x4072000000000000)
    /Users/brendan/Documents/mygo/src/gonum.org/v1/plot/plot.go:165 +0x84c
gonum.org/v1/plot.(*Plot).WriterTo(0xc42042e000, 0x4072000000000000, 0x4072000000000000, 0x1396f61, 0x3, 0x1, 0x1, 0xc42000e790, 0xc42042e398)
    /Users/brendan/Documents/mygo/src/gonum.org/v1/plot/plot.go:445 +0x136
gonum.org/v1/plot.(*Plot).Save(0xc42042e000, 0x4072000000000000, 0x4072000000000000, 0x1396f5b, 0x9, 0x0, 0x0)
    /Users/brendan/Documents/mygo/src/gonum.org/v1/plot/plot.go:471 +0x130

The problem is that the calculation on heat.go171 is fmt.Println(int((v-h.Min)*ps + 0.5)). When h.Min = -inf, then ps = 0, which means the calculation is NaN, and the interpreted integer is a very negative integer which panics on the bound.

I'm not sure what the fix is, Scatter et. al. have errors returned in creation, maybe this should be the case as well? Or maybe the non-inf min should be calculated to allow default special casing.

kortschak commented 6 years ago

We should not allow ±Inf or NaN bounds on creation. They make even less sense here than they do in the floats span functions. What were the parameters used in creating the heat map?

btracey commented 6 years ago

In this case, just the default. I had a -inf value in my grid and didn't realize it. This problem, though, isn't manifested until attempting to save the plot. I would suggest that NewHeatMap return (heat, err) giving an error for things like the (effective) data bounds are inf or NaN

kortschak commented 6 years ago

SGTM