fsprojects / IfSharp

F# for Jupyter Notebooks
Other
440 stars 71 forks source link

Dynamic Chart with Angara #191

Closed caxelrud closed 5 years ago

caxelrud commented 5 years ago

Description

I am trying to use dynamic charts with Jupyter/Angara (like data streaming). I am able to partially do it with the following code but, at every interval, the full chart disappears and then appears again (not good):

AsyncSeq.intervalMs 5000 |> AsyncSeq.take 20 |> AsyncSeq.map (fun _ -> let r1=rand.Next(minValue=1,maxValue=10) let x = [| for i in 0..99 -> float(i) /(double r1) |] let y = x |> Array.map (fun x -> sin x) let z = x |> Array.map (fun x -> cos x) [ Angara.Charting.Plot.line(x, y) ] |> Angara.Charting.Chart.ofList)

Path to the solution:

I watched the video from Dmitry Grechka, where the charts have a nice dynamic behavior, at (https://www.youtube.com/watch?time_continue=1&v=DZjZ4tYjoG8)

But, I don't have the source code (let me know if you have it). I tried with the following code with out success (it generating dynamically data but not the charts). It may be related to the fact that I am missing the file Angara.Charting.AsyncSeq.fsx.

`#load "Paket.fsx"

load "AsyncDisplay.Paket.fsx"

load "Angara.Charting.Paket.fsx"

load "Paket.Generated.Refs.fsx"

load "Angara.Charting.fsx"

//#load "Angara.Charting.AsyncSeq.fsx" open Angara.Charting open FSharp.Control open Angara.Statistics let x=[|for i in 0..300 do yield float(i)/10.0 |] let mu=15.0 let sigma=3.0 let dist=Normal(mu,sigma) let truth_p=Array.map (fun x -> exp(Angara.Statistics.logpdf dist x)) x let prng = MT19937(123u) let samples=Array.init 3 (fun -> Angara.Statistics.draw prng dist) let generator state= async{ do! Async.Sleep 100 let counter,samples=state if counter > 300 then return None else let newsamples=Array.init 2 (fun ->Angara.Statistics.draw prng dist) let samples2=Array.append samples new_samples let est_p_x,est_p_y=Angara.Statistics.kde2 50 0.0 30.0 samples2 let result= [ Angara.Charting.Plot.line(x,truth_p, displayName="True dist",thickness=2.0) Angara.Charting.Plot.line(est_p_x,est_p_y,displayName="Estimated dist",stroke="red",thickness=2.0) ] let state=counter+1,samples2 return Some (result,state) } AsyncSeq.unfoldAsync generator (0,samples)`

It produces: [{Kind = "line"; DisplayName = "True dist"; Titles = map []; Properties = map [("fill68", StringValue "#1F497D"); ("fill95", StringValue "#1F497D"); ("stroke", StringValue "#1F497D"); ("thickness", RealValue 2.0); ("treatAs", StringValue "0"); ("x", RealArray [|0.0; 0.1; 0.2; 0.3; 0.4; 0.5; 0.6; 0.7; 0.8; 0.9; 1.0; 1.1; 1.2; 1.3; 1.4; 1.5; 1.6; 1.7; 1.8; 1.9; 2.0; 2.1; 2.2; 2.3; 2.4; 2.5; 2.6; 2.7; 2.8; 2.9; 3.0; 3.1; 3.2; 3.3; 3.4; 3.5; 3.6; 3.7; 3.8; 3.9; 4.0; 4.1; 4.2; 4.3; 4.4; 4.5; 4.6; 4.7; 4.8; 4.9; 5.0; 5.1; 5.2; 5.3; 5.4; 5.5; 5.6; 5.7; 5.8; 5.9; 6.0; 6.1; 6.2; 6.3; 6.4; 6.5; 6.6; 6.7; 6.8; 6.9; 7.0; 7.1; 7.2; 7.3; 7.4; 7.5; 7.6; 7.7; 7.8; 7.9; 8.0; 8.1; 8.2; 8.3; 8.4; 8.5; 8.6; 8.7; 8.8; 8.9; 9.0; 9.1; 9.2; 9.3; 9.4; 9.5; 9.6; 9.7; 9.8; 9.9; ...|]); ("y", RealArray [|4.955731716e-07; 5.851253666e-07; 6.90092831e-07; 8.129869153e-07; 9.567028033e-07; 1.124573869e-06; 1.32043303e-06; 1.548681898e-06; 1.814368574e-06; 2.12327506e-06; 2.48201529e-06; 2.898144913e-06; 3.380284022e-06; 3.938254142e-06; 4.583230874e-06; 5.327913702e-06; 6.186714563e-06; 7.175966912e-06; 8.314157097e-06; 9.622179971e-06; 1.11236208e-05; 1.284506558e-05; 1.481644206e-05; 1.707139372e-05; 1.964768925e-05; 2.258766996e-05; 2.593873766e-05; 2.975388573e-05; 3.409227596e-05; 3.90198639e-05; 4.461007525e-05; 5.094453612e-05; 5.811385947e-05; 6.621849046e-05; 7.53696128e-05; 8.569011835e-05; 9.731564193e-05; 0.0001103956628; 0.0001250946746; 0.0001415934235; 0.0001600902172; 0.0001808023021; 0.00020396731; 0.0002298447729; 0.0002587177021; 0.0002908942317; 0.0003267093204; 0.0003665265084; 0.0004107397228; 0.0004597751262; 0.0005140929988; 0.0005741896464; 0.0006405993231; 0.0007138961573; 0.0007946960672; 0.0008836586515; 0.0009814890401; 0.001088939685; 0.001206812076; 0.001335958353; 0.001477282804; 0.001631743217; 0.00180035206; 0.001984177473; 0.00218434403; 0.002402033255; 0.002638483861; 0.002894991674; 0.003172909218; 0.003473644938; 0.003798662008; 0.004149476721; 0.004527656411; 0.004934816892; 0.005372619371; 0.005842766831; 0.006346999839; 0.00688709177; 0.007464843432; 0.008082077061; 0.008740629698; 0.009442345914; 0.01018906991; 0.01098263697; 0.01182486428; 0.01271754117; 0.01366241868; 0.01466119866; 0.01571552224; 0.01682695786; 0.01799698884; 0.01922700052; 0.02051826712; 0.02187193826; 0.02328902536; 0.02477038785; 0.02631671943; 0.02792853432; 0.0296061537; 0.03134969246; ...|])];}; {Kind = "line"; DisplayName = "Estimated dist"; Titles = map []; Properties = map [("fill68", StringValue "#1F497D"); ("fill95", StringValue "#1F497D"); ("stroke", StringValue "red"); ("thickness", RealValue 2.0); ("treatAs", StringValue "0"); ("x", RealArray [|0.0; 0.4761904762; 0.9523809524; 1.428571429; 1.904761905; 2.380952381; 2.857142857; 3.333333333; 3.80952381; 4.285714286; 4.761904762; 5.238095238; 5.714285714; 6.19047619; 6.666666667; 7.142857143; 7.619047619; 8.095238095; 8.571428571; 9.047619048; 9.523809524; 10.0; 10.47619048; 10.95238095; 11.42857143; 11.9047619; 12.38095238; 12.85714286; 13.33333333; 13.80952381; 14.28571429; 14.76190476; 15.23809524; 15.71428571; 16.19047619; 16.66666667; 17.14285714; 17.61904762; 18.0952381; 18.57142857; 19.04761905; 19.52380952; 20.0; 20.47619048; 20.95238095; 21.42857143; 21.9047619; 22.38095238; 22.85714286; 23.33333333; 23.80952381; 24.28571429; 24.76190476; 25.23809524; 25.71428571; 26.19047619; 26.66666667; 27.14285714; 27.61904762; 28.0952381; 28.57142857; 29.04761905; 29.52380952; 30.0|]); ("y", RealArray [|6.189030769e-15; 7.023394814e-15; 6.414951939e-13; 1.287472894e-11; 2.180240588e-10; 2.955591316e-09; 3.223205689e-08; 2.830738446e-07; 2.005455802e-06; 1.148460333e-05; 5.330163232e-05; 0.0002011831701; 0.0006205506326; 0.001575435636; 0.003328162262; 0.005950334418; 0.009238384543; 0.01291899587; 0.01701060904; 0.02195061181; 0.02828216978; 0.03612832928; 0.04495385623; 0.05396025762; 0.06281284907; 0.07188216461; 0.08167471051; 0.0921283121; 0.1024963982; 0.1116929248; 0.1186443322; 0.1225993166; 0.1234372644; 0.1217398858; 0.1184466641; 0.1141977033; 0.1087434309; 0.1010779978; 0.09050258519; 0.07759353584; 0.06392075138; 0.05094445324; 0.03944751847; 0.02981247558; 0.02230495224; 0.01690663554; 0.0131573473; 0.01041578652; 0.008243648368; 0.006479674059; 0.005059758636; 0.00388770869; 0.002858123748; 0.001931427589; 0.001152181953; 0.0005869912629; 0.000249382507; 8.696305588e-05; 2.464012123e-05; 5.636962465e-06; 1.037149643e-06; 1.531021913e-07; 1.811323135e-08; 1.843404006e-09|])];}]

cgravill commented 5 years ago

Hi, the code for @dgrechka demo is over on the pull request #190. There was an issue using it on Mono which Dmitry since resolved. You can clone the code from that pull request and run from there. I'm actually just looking at merging in the change to master now.

You're exactly right that missing Angara.Charting.AsyncSeq.fsx is what's causing the issue. At the moment we need to do a bit of library specific work to make the updates smooth. You can see more discussion on #104

cgravill commented 5 years ago

This is now merged to master and there are some examples at the end of the feature notebook - https://github.com/fsprojects/IfSharp/blob/master/FSharp_Jupyter_Notebooks.ipynb