Skip to content

Commit f6ee7cc

Browse files
committed
Refactor Distribution tests
1 parent 75275ee commit f6ee7cc

5 files changed

Lines changed: 258 additions & 317 deletions

File tree

tests/Common/FSharpTestBase/TestCharts/Chart2DTestCharts.fs

Lines changed: 150 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
module Chart2DTestCharts
22

3+
open System
34
open Plotly.NET
5+
open Plotly.NET.TraceObjects
6+
7+
open TestUtils.DataGeneration
48

59
module Point =
610

@@ -78,7 +82,6 @@ module Spline =
7882
let y = [2.; 1.5; 5.; 1.5; 3.; 2.5; 2.5; 1.5; 3.5; 1.]
7983
Chart.Spline(x = x, y = y, Name="spline", UseDefaults = false)
8084

81-
8285
module Bubble =
8386

8487
let ``Simple bubble chart`` =
@@ -132,7 +135,6 @@ module StackedArea =
132135
]
133136
|> Chart.combine
134137

135-
136138
module Funnel = ()
137139

138140
module StackedFunnel = ()
@@ -175,15 +177,100 @@ module Column =
175177

176178
module StackedColumn = ()
177179

178-
module Histogram = ()
180+
module Histogram =
181+
182+
let ``Simple histogram`` =
183+
let rnd = System.Random(5)
184+
let x = [for i=0 to 500 do yield rnd.NextDouble() ]
185+
Chart.Histogram(X = x, UseDefaults = false)
186+
|> Chart.withSize(500, 500)
187+
179188

180-
module Histogram2D = ()
189+
module Histogram2D =
190+
191+
let ``Histogram2D of 2D normal distribution`` =
192+
let rnd = System.Random(5)
193+
let n = 2000
194+
let a = -1.
195+
let b = 1.2
196+
let step i = a + ((b - a) / float (n - 1)) * float i
197+
198+
//---------------------- generate data distributed in x and y direction ----------------------
199+
let x = Array.init n (fun i -> ((step i)**3.) + (0.3 * (normal (rnd) 0. 2.) ))
200+
let y = Array.init n (fun i -> ((step i)**6.) + (0.3 * (normal (rnd) 0. 2.) ))
201+
Chart.Histogram2D (x = x, y = y, UseDefaults = false)
202+
203+
module BoxPlot =
204+
205+
let ``Simple boxplot with boxpoints`` =
206+
let y = [2.; 1.5; 5.; 1.5; 3.; 2.5; 2.5; 1.5; 3.5; 1.]
207+
let x = ["bin1";"bin2";"bin1";"bin2";"bin1";"bin2";"bin1";"bin1";"bin2";"bin1"]
208+
Chart.BoxPlot(X = x, Y = y,Jitter=0.1,BoxPoints=StyleParam.BoxPoints.All, UseDefaults = false)
209+
210+
let ``Simple horizontal boxplot with boxpoints`` =
211+
let y = [2.; 1.5; 5.; 1.5; 3.; 2.5; 2.5; 1.5; 3.5; 1.]
212+
let x = ["bin1";"bin2";"bin1";"bin2";"bin1";"bin2";"bin1";"bin1";"bin2";"bin1"]
213+
Chart.BoxPlot(X = y,Y = x,Jitter=0.1,BoxPoints=StyleParam.BoxPoints.All,Orientation=StyleParam.Orientation.Horizontal, UseDefaults = false)
214+
215+
let ``Combined chart with 2 horizontal boxplots`` =
216+
let y = [2.; 1.5; 5.; 1.5; 3.; 2.5; 2.5; 1.5; 3.5; 1.]
217+
let y' = [2.; 1.5; 5.; 1.5; 2.; 2.5; 2.1; 2.5; 1.5; 1.;2.; 1.5; 5.; 1.5; 3.; 2.5; 2.5; 1.5; 3.5; 1.]
218+
[
219+
Chart.BoxPlot(data = y , orientation = StyleParam.Orientation.Horizontal, Name="bin1",Jitter=0.1,BoxPoints=StyleParam.BoxPoints.All, UseDefaults = false);
220+
Chart.BoxPlot(data = y', orientation = StyleParam.Orientation.Horizontal, Name="bin2",Jitter=0.1,BoxPoints=StyleParam.BoxPoints.All, UseDefaults = false);
221+
]
222+
|> Chart.combine
223+
224+
module Violin =
225+
226+
let ``Simple violin plot with jitterpoints`` =
227+
let y = [2.; 1.5; 5.; 1.5; 3.; 2.5; 2.5; 1.5; 3.5; 1.]
228+
let x = ["bin1";"bin2";"bin1";"bin2";"bin1";"bin2";"bin1";"bin1";"bin2";"bin1"]
229+
Chart.Violin (
230+
X = x, Y = y,
231+
Points=StyleParam.JitterPoints.All,
232+
UseDefaults = false
233+
)
234+
235+
let ``Simple horizontal violin plot with jitterpoints`` =
236+
let y = [2.; 1.5; 5.; 1.5; 3.; 2.5; 2.5; 1.5; 3.5; 1.]
237+
let x = ["bin1";"bin2";"bin1";"bin2";"bin1";"bin2";"bin1";"bin1";"bin2";"bin1"]
238+
Chart.Violin(
239+
X = y, Y = x,
240+
Jitter=0.1,
241+
Points=StyleParam.JitterPoints.All,
242+
Orientation=StyleParam.Orientation.Horizontal,
243+
MeanLine=MeanLine.init(Visible=true),
244+
UseDefaults = false
245+
)
246+
247+
let ``Combined chart with 2 horizontal violin plots`` =
248+
let y = [2.; 1.5; 5.; 1.5; 3.; 2.5; 2.5; 1.5; 3.5; 1.]
249+
let y' = [2.; 1.5; 5.; 1.5; 2.; 2.5; 2.1; 2.5; 1.5; 1.;2.; 1.5; 5.; 1.5; 3.; 2.5; 2.5; 1.5; 3.5; 1.]
250+
[
251+
Chart.Violin (data = y , orientation = StyleParam.Orientation.Horizontal, Name="bin1",Jitter=0.1,Points=StyleParam.BoxPoints.All, UseDefaults = false)
252+
Chart.Violin (data = y', orientation = StyleParam.Orientation.Horizontal, Name="bin2",Jitter=0.1,Points=StyleParam.BoxPoints.All, UseDefaults = false)
253+
]
254+
|> Chart.combine
181255

182-
module BoxPlot = ()
256+
module Histogram2DContour =
183257

184-
module Violin = ()
258+
let ``Histogram2DContour of 2D normal distribution`` =
185259

186-
module Histogram2DContour = ()
260+
let rnd = System.Random(5)
261+
let n = 2000
262+
let a = -1.
263+
let b = 1.2
264+
let step i = a + ((b - a) / float (n - 1)) * float i
265+
266+
//---------------------- generate data distributed in x and y direction ----------------------
267+
let x = Array.init n (fun i -> ((step i)**3.) + (0.3 * (normal (rnd) 0. 2.) ))
268+
let y = Array.init n (fun i -> ((step i)**6.) + (0.3 * (normal (rnd) 0. 2.) ))
269+
[
270+
Chart.Histogram2DContour (x = x, y = y,ContourLine=Line.init(Width=0.), UseDefaults = false)
271+
Chart.Point(x = x,y = y,Opacity=0.3, UseDefaults = false)
272+
]
273+
|> Chart.combine
187274

188275
module Heatmap =
189276

@@ -290,12 +377,65 @@ module Image =
290377
)
291378
|> Chart.withTitle "This is Plotly.NET:"
292379

293-
module Contour = ()
380+
module Contour =
381+
382+
let ``Contour plot with peak and sink`` =
383+
384+
// Create example data
385+
let size = 100
386+
let x = linspace(-2. * Math.PI, 2. * Math.PI, size)
387+
let y = linspace(-2. * Math.PI, 2. * Math.PI, size)
388+
389+
let f x y = - (5. * x / (x**2. + y**2. + 1.) )
390+
391+
let z =
392+
Array.init size (fun i ->
393+
Array.init size (fun j ->
394+
f x.[j] y.[i]
395+
)
396+
)
397+
398+
Chart.Contour(zData = z, UseDefaults = false)
399+
|> Chart.withSize(600.,600.)
294400

295401
module OHLC = ()
296402

297403
module Candlestick = ()
298404

299-
module Splom = ()
405+
module Splom =
406+
407+
let ``Simple scatterplot matrix`` =
408+
let data =
409+
[
410+
"A",[|1.;4.;3.4;0.7;|]
411+
"B",[|3.;1.5;1.7;2.3;|]
412+
"C",[|2.;4.;3.1;5.|]
413+
"D",[|4.;2.;2.;4.;|]
414+
]
415+
Chart.Splom(keyValues = data, MarkerColor=Color.fromString "blue", UseDefaults = false)
416+
417+
418+
module PointDensity =
300419

301-
module PointDensity = ()
420+
421+
let ``Simple PointDensity chart`` =
422+
let rnd = new System.Random(5)
423+
let x = [for i in 0 .. 100 -> rnd.NextDouble()]
424+
let y = [for i in 0 .. 100 -> rnd.NextDouble()]
425+
Chart.PointDensity(x = x,y = y,UseDefaults = false)
426+
427+
let ``Styled PointDensity chart`` =
428+
let rnd = new System.Random(5)
429+
let x = [for i in 0 .. 100 -> rnd.NextDouble()]
430+
let y = [for i in 0 .. 100 -> rnd.NextDouble()]
431+
Chart.PointDensity(
432+
x = x,
433+
y = y,
434+
PointMarkerColor = Color.fromKeyword Purple,
435+
PointMarkerSymbol = StyleParam.MarkerSymbol.X,
436+
PointMarkerSize = 4,
437+
ColorScale = StyleParam.Colorscale.Viridis,
438+
ColorBar = ColorBar.init(Title = Title.init("Density")),
439+
ShowContourLabels = true,
440+
UseDefaults = false
441+
)

tests/Common/FSharpTestBase/TestUtils.fs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ module DataGeneration =
2626

2727
x,y,z
2828

29+
let normal (rnd:System.Random) mu tau =
30+
let mutable v1 = 2.0 * rnd.NextDouble() - 1.0
31+
let mutable v2 = 2.0 * rnd.NextDouble() - 1.0
32+
let mutable r = v1 * v1 + v2 * v2
33+
while (r >= 1.0 || r = 0.0) do
34+
v1 <- 2.0 * rnd.NextDouble() - 1.0
35+
v2 <- 2.0 * rnd.NextDouble() - 1.0
36+
r <- v1 * v1 + v2 * v2
37+
let fac = sqrt(-2.0*(log r)/r)
38+
(tau * v1 * fac + mu)
39+
2940
module HtmlCodegen =
3041

3142
let getLogoPNG() =

tests/CoreTests/CoreTests/CoreTests.fsproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
<Compile Include="Traces\TraceID.fs" />
3030
<Compile Include="HtmlCodegen\SimpleTests.fs" />
3131
<Compile Include="HtmlCodegen\ChartLayout.fs" />
32-
<Compile Include="HtmlCodegen\DistributionCharts.fs" />
3332
<Compile Include="HtmlCodegen\GeoMapCharts.fs" />
3433
<Compile Include="HtmlCodegen\MapboxMapCharts.fs" />
3534
<Compile Include="HtmlCodegen\FinanceCharts.fs" />

0 commit comments

Comments
 (0)