-
Notifications
You must be signed in to change notification settings - Fork 0
/
axis1d.go
169 lines (137 loc) · 3.07 KB
/
axis1d.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2015 The go-hep Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package hbook
import (
"bytes"
"encoding/gob"
"fmt"
"io"
"math"
"github.com/go-hep/rio"
"github.com/gonuts/binary"
)
type EvenBinAxis struct {
nbins int // number of bins for this axis
low float64 // low-edge of this axis
high float64 // high-edge of this axis
size float64 // bin size
}
func NewEvenBinAxis(nbins int, low, high float64) *EvenBinAxis {
axis := &EvenBinAxis{
nbins: nbins,
low: low,
high: high,
size: (high - low) / float64(nbins),
}
return axis
}
func (axis *EvenBinAxis) Kind() AxisKind {
return FixedBinning
}
func (axis *EvenBinAxis) LowerEdge() float64 {
return axis.low
}
func (axis *EvenBinAxis) UpperEdge() float64 {
return axis.high
}
func (axis *EvenBinAxis) Bins() int {
return axis.nbins
}
func (axis *EvenBinAxis) BinLowerEdge(idx int) float64 {
if idx >= 0 && idx <= axis.nbins {
return axis.low + float64(idx)*axis.size
}
if idx == UnderflowBin {
return axis.low
}
panic(fmt.Errorf("hist: out of bound index (%d)", idx))
}
func (axis *EvenBinAxis) BinUpperEdge(idx int) float64 {
if idx >= 0 && idx < axis.nbins {
return axis.low + float64(idx+1)*axis.size
}
if idx == OverflowBin {
return axis.high
}
panic(fmt.Errorf("hist: out of bound index (%d)", idx))
}
func (axis *EvenBinAxis) BinWidth(idx int) float64 {
return axis.size
}
func (axis *EvenBinAxis) CoordToIndex(coord float64) int {
switch {
case coord < axis.low:
return UnderflowBin
case coord >= axis.high:
return OverflowBin
default:
return int(math.Floor((coord - axis.low) / float64(axis.size)))
}
panic("unreachable")
}
func (axis *EvenBinAxis) MarshalBinary() ([]byte, error) {
buf := new(bytes.Buffer)
err := axis.RioMarshal(buf)
return buf.Bytes(), err
}
func (axis *EvenBinAxis) UnmarshalBinary(data []byte) error {
buf := bytes.NewReader(data)
err := axis.RioUnmarshal(buf)
return err
}
func (axis *EvenBinAxis) RioVersion() rio.Version {
return 0
}
func (axis *EvenBinAxis) RioMarshal(w io.Writer) error {
var err error
enc := binary.NewEncoder(w)
err = enc.Encode(axis.nbins)
if err != nil {
return err
}
err = enc.Encode(axis.low)
if err != nil {
return err
}
err = enc.Encode(axis.high)
if err != nil {
return err
}
err = enc.Encode(axis.size)
if err != nil {
return err
}
return err
}
func (axis *EvenBinAxis) RioUnmarshal(r io.Reader) error {
var err error
dec := binary.NewDecoder(r)
err = dec.Decode(&axis.nbins)
if err != nil {
return err
}
err = dec.Decode(&axis.low)
if err != nil {
return err
}
err = dec.Decode(&axis.high)
if err != nil {
return err
}
err = dec.Decode(&axis.size)
if err != nil {
return err
}
return err
}
// check EvenBinAxis satisfies Axis interface
var _ Axis = (*EvenBinAxis)(nil)
// serialization interfaces
var _ rio.Marshaler = (*EvenBinAxis)(nil)
var _ rio.Unmarshaler = (*EvenBinAxis)(nil)
var _ rio.Streamer = (*EvenBinAxis)(nil)
func init() {
gob.Register((*EvenBinAxis)(nil))
}
// EOF