Skip to content

Commit

Permalink
Skip NaN values in Lines plot, allowing for partial lines (#52)
Browse files Browse the repository at this point in the history
* Skip NaN values in `Lines` plot, allowing for partial lines
* Add a test with NaN values
  • Loading branch information
mlange-42 committed May 6, 2024
1 parent e019e91 commit c062687
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [[v0.8.1]](https://github.com/mlange-42/arche-pixel/compare/v0.8.0...v0.8.1)

### Features

* Skip NaN values in `Lines` plot, allowing for partial lines (#52)

## [[v0.8.0]](https://github.com/mlange-42/arche-pixel/compare/v0.7.0...v0.8.0)

### Breaking changes
Expand Down
4 changes: 4 additions & 0 deletions plot/lines.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package plot
import (
"fmt"
"image/color"
"math"

pixel "github.com/gopxl/pixel/v2"
"github.com/gopxl/pixel/v2/backends/opengl"
Expand Down Expand Up @@ -136,6 +137,9 @@ func (l *Lines) updateData(w *ecs.World) {
if xi >= 0 {
x = row[xi]
}
if math.IsNaN(row[idx]) {
continue
}
l.series[i] = append(l.series[i], plotter.XY{X: x, Y: row[idx]})
}
}
Expand Down
42 changes: 42 additions & 0 deletions plot/lines_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package plot_test

import (
"math"
"math/rand"
"testing"

Expand Down Expand Up @@ -92,6 +93,22 @@ func TestLines_PanicY(t *testing.T) {
assert.Panics(t, m.Run)
}

func TestLinesNaN(t *testing.T) {
m := model.New()
m.AddUISystem((&window.Window{}).
With(&plot.Lines{
Observer: &TableObserverNaN{},
X: "X",
Y: []string{"A"},
}))

m.AddSystem(&system.FixedTermination{
Steps: 100,
})

m.Run()
}

// TableObserver to generate random time series.
type TableObserver struct {
data [][]float64
Expand All @@ -115,3 +132,28 @@ func (o *TableObserver) Values(w *ecs.World) [][]float64 {
}
return o.data
}

// TableObserver to generate test time series containing NaN.
type TableObserverNaN struct {
data [][]float64
}

func (o *TableObserverNaN) Initialize(w *ecs.World) {
rows := 25
o.data = make([][]float64, rows)

for i := 0; i < rows; i++ {
v := 1.0
if i < 5 || i > rows-5 {
v = math.NaN()
}
o.data[i] = []float64{float64(i), v}
}
}
func (o *TableObserverNaN) Update(w *ecs.World) {}
func (o *TableObserverNaN) Header() []string {
return []string{"X", "A"}
}
func (o *TableObserverNaN) Values(w *ecs.World) [][]float64 {
return o.data
}

0 comments on commit c062687

Please sign in to comment.