-
Notifications
You must be signed in to change notification settings - Fork 0
/
Histogram.nim
126 lines (118 loc) · 4.47 KB
/
Histogram.nim
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
import std/[math, random, sugar]
import imgui
import implot
import utils
#------------------
# demo_Histogram()
#------------------
proc demo_Histogram*() =
const
mu: double = 5
sigma: double = 2
var
dist {.global.}: seq[cdouble]
hist_flags{.global.} = Density.int32
bins {.global.}: ImPlotBin = cast[ImPlotBin](50)
once:
discard initRand()
dist = collect(for i in 0..<10000: gauss(mu, sigma).cdouble)
igSetNextItemWidth(200)
if igRadioButton("Sqrt", bins == Sqrt): bins = Sqrt
igSameLine()
if igRadioButton("Sturges", bins == Sturges): bins = Sturges
igSameLine()
if igRadioButton("Rice", bins == Rice): bins = Rice
igSameLine()
if igRadioButton("Scott", bins == Scott): bins = Scott
#igSameLine()
if igRadioButton("N Bins", bins.int32 >= 0): bins = cast[ImPlotBin](50)
if bins.int32 >= 0:
igSameLine()
igSetNextItemWidth(200)
igSliderInt("##Bins", cast[ptr int32](addr bins), 1, 100)
igCheckboxFlags("Horizontal", addr hist_flags, ImPlotHistogramFlags.Horizontal.int32)
igSameLine()
igCheckboxFlags("Density", addr hist_flags, Density.int32)
igSameLine()
igCheckboxFlags("Cumulative", addr hist_flags, Cumulative.int32)
var frange{.global.} = false
igCheckbox("Range", addr frange)
var
rmin{.global.}: cfloat = -3
rmax{.global.}: cfloat = 13
range1{.global.} = [rmin, rmax]
if frange:
igSameLine()
igSetNextItemWidth(200)
igDragFloat2("##Range", range1, 0.1f, -3, 13)
rmin = range1[0]
rmax = range1[1]
igSameLine()
igCheckboxFlags("Exclude Outliers", addr hist_flags, NoOutliers.int32)
#static NormalDistribution<10000> dist(mu, sigma)
var
x{.global.}: array[100, double]
y{.global.}: array[100, double]
if 0 != (hist_flags and Density.int32):
for i in 0..<100:
x[i] = -3 + 16 * i.double / 99.0
y[i] = exp( - (x[i] - mu) * (x[i] - mu) / (2 * sigma * sigma)) / (sigma * sqrt(2 * 3.141592653589793238))
if 0 != (hist_flags and Cumulative.int32):
for i in 1..<100:
y[i] += y[i-1]
for i in 0..<100:
y[i] /= y[99]
if ipBeginPlot("##Histograms"):
defer: ipEndPlot()
ipSetupAxes(nil, nil, AutoFit, AutoFit)
ipSetNextFillStyle(IMPLOT_AUTO_COL,0.5f)
#ipSetNextFillStyle(ImVec4(x: 0, y: 0, z: 0, w: -1), 0.5f)
ipPlotHistogram("Empirical", dist.ptz, 10000.int, bins.int,
1.0.cdouble, (if frange: ImPlotRange(min: rmin, max: rmax) else: ImPlotRange()),
hist_flags.ImPlotHistogramFlags)
if (0 != (hist_flags and Density.int32)) and not (0 != (hist_flags and NoOutliers.int32)):
if 0 != (hist_flags and ImPlotHistogramFlags.Horizontal.int32):
ipPlotLine("Theoretical", y.ptz, x.ptz, 100)
else:
ipPlotLine("Theoretical", x.ptz, y.ptz, 100)
#------------------
# demo_Histogram2D()
#------------------
proc demo_Histogram2D*() =
var
count{.global.} = 50000.int32
xybins{.global.} = [100.int32,100]
hist_flags{.global.} = 0.int32
dist1 {.global.}: seq[cdouble]
dist2 {.global.}: seq[cdouble]
once:
discard initRand()
dist1 = collect(for i in 0..<100000: gauss(1, 2).cdouble)
dist2 = collect(for i in 0..<100000: gauss(1, 1).cdouble)
igSliderInt("Count",addr count,100,100000)
igSliderInt2("Bins", xybins,1,500)
igSameLine()
igCheckboxFlags("Density", addr hist_flags, Density.int32)
#static NormalDistribution<100000> dist1(1, 2)
#static NormalDistribution<100000> dist2(1, 1)
var max_count = 0.cdouble
var flags = AutoFit or ImPlotAxisFlags.Foreground
ipPushColormap("Hot")
var regn = ImVec2()
igGetContentRegionAvailNonUDT(addr regn)
if ipBeginPlot("##Hist2D",ImVec2(x: regn.x - 100 - igGetStyle().itemSpacing.x,y: 0)):
ipSetupAxes(nullptr, nullptr, flags.ImPlotAxisFlags , flags.ImPlotAxisFlags )
ipSetupAxesLimits(-6,6,-6,6)
max_count = ipPlotHistogram2D("Hist2D"
,dist1.ptz
,dist2.ptz
,count.int
,xybins[0].int
,xybins[1].int
,ImPlotRect(x: ImPlotRange(min: -6,max: 6) ,y: ImPlotRange(min: -6,max: 6))
,hist_flags.ImPlotHistogramFlags)
ipEndPlot()
igSameLine()
ipColormapScale((if 0 != (hist_flags and Density.int32) : "Density" else: "Count").cstring
,0,max_count,ImVec2(x: 100,y: 0))
ipPopColormap()