Skip to content

Instantly share code, notes, and snippets.

@TahaHachana
Created May 13, 2015 17:30
#nowarn "211"
#I "."
#I "../Deedle/lib/net40"
#I "../Deedle.1.0.7/lib/net40"
#I "../Deedle.RPlugin/lib/net40"
#I "../Deedle.RPlugin.1.0.7/lib/net40"
#I "../FSharp.Charting/lib/net40"
#I "../FSharp.Charting.0.90.10/lib/net40"
#I "../FSharp.Data/lib/net40"
#I "../FSharp.Data.2.2.0/lib/net40"
#I "../Foogle.Charts/lib/net40"
#I "../Foogle.Charts.0.0.5/lib/net40"
#I "../MathNet.Numerics/lib/net40"
#I "../MathNet.Numerics.3.6.0/lib/net40"
#I "../MathNet.Numerics.FSharp/lib/net40"
#I "../MathNet.Numerics.FSharp.3.6.0/lib/net40"
#I "../RProvider/lib/net40"
#I "../RProvider.1.1.8/lib/net40"
#I "../R.NET.Community/lib/net40"
#I "../R.NET.Community.1.5.16/lib/net40"
#I "../R.NET.Community.FSharp/lib/net40"
#I "../R.NET.Community.FSharp.0.1.9/lib/net40"
#I "../XPlot.Plotly/lib/net45"
#I "../XPlot.Plotly.1.0.1.0/lib/net45"
#I "../XPlot.GoogleCharts/lib/net45"
#I "../XPlot.GoogleCharts.1.1.7.0/lib/net45"
#I "../XPlot.GoogleCharts.Deedle/lib/net45"
#I "../XPlot.GoogleCharts.Deedle.0.6.2.0/lib/net45"
#I "../Google.DataTable.Net.Wrapper/lib"
#I "../Google.DataTable.Net.Wrapper.3.1.2.0/lib"
#I "../Newtonsoft.Json/lib/net40"
#I "../Newtonsoft.Json.6.0.8/lib/net40"
#r "Deedle.dll"
#r "Deedle.RProvider.Plugin.dll"
#r "System.Windows.Forms.DataVisualization.dll"
#r "FSharp.Charting.dll"
#r "FSharp.Data.dll"
#r "Foogle.Charts.dll"
#r "MathNet.Numerics.dll"
#r "MathNet.Numerics.FSharp.dll"
#r "RProvider.Runtime.dll"
#r "RProvider.dll"
#r "RDotNet.dll"
#r "RDotNet.NativeLibrary.dll"
#r "RDotNet.FSharp.dll"
#r "XPlot.Plotly.dll"
#r "XPlot.GoogleCharts.dll"
#r "XPlot.GoogleCharts.Deedle.dll"
#r "Google.DataTable.Net.Wrapper.dll"
#r "Newtonsoft.Json.dll"
#r "../Owin.1.0/lib/net40/Owin.dll"
#r "../Microsoft.Owin.3.0.1/lib/net45/Microsoft.Owin.dll"
#r "../Microsoft.Owin.Cors.3.0.1/lib/net45/Microsoft.Owin.Cors.dll"
#r "../Microsoft.AspNet.Cors.5.2.3/lib/net45/System.Web.Cors.dll"
#r "../Microsoft.AspNet.SignalR.Core.2.2.0/lib/net45/Microsoft.AspNet.SignalR.Core.dll"
#r "../Microsoft.Owin.Diagnostics.3.0.1/lib/net45/Microsoft.Owin.Diagnostics.dll"
#r "../Microsoft.Owin.Host.HttpListener.3.0.1/lib/net45/Microsoft.Owin.Host.HttpListener.dll"
#r "../Microsoft.Owin.Hosting.3.0.1/lib/net45/Microsoft.Owin.Hosting.dll"
#r "../Microsoft.Owin.Security.2.1.0/lib/net45/Microsoft.Owin.Security.dll"
#r "../Microsoft.Owin.FileSystems.3.0.1/lib/net45/Microsoft.Owin.FileSystems.dll"
#r "../Microsoft.Owin.StaticFiles.3.0.1/lib/net45/Microsoft.Owin.StaticFiles.dll"
#r "Microsoft.CSharp.dll"
namespace FsLab
#if NO_FSI_ADDPRINTER
#else
module FsiAutoShow =
open FSharp.Charting
open RProvider
fsi.AddPrinter(fun (printer:Deedle.Internal.IFsiFormattable) ->
"\n" + (printer.Format()))
fsi.AddPrinter(fun (ch:FSharp.Charting.ChartTypes.GenericChart) ->
ch.ShowChart() |> ignore; "(Chart)")
fsi.AddPrinter(fun (synexpr:RDotNet.SymbolicExpression) ->
synexpr.Print())
open System
open System.Net
open Owin
open Microsoft.AspNet.SignalR
open Microsoft.Owin.Hosting
open System.IO
open Microsoft.Owin.FileSystems
open Microsoft.Owin.StaticFiles
// let random = Random()
//
// let freePort() =
// let properties = NetworkInformation.IPGlobalProperties.GetIPGlobalProperties()
//
// let tcpEndPoints =
// properties.GetActiveTcpListeners()
// |> Array.map (fun x -> x.Port)
//
// let rec port() =
// let rnd = random.Next(1000, 50000)
// let isActive = Array.exists (fun x -> x = rnd) tcpEndPoints
// match isActive with
// | false -> string rnd
// | true -> port()
//
// port()
let address = "http://localhost:8084/" //"http://localhost:" + freePort()
let guid = Guid.NewGuid().ToString()
let root = Path.GetTempPath()
let htmlFile = Path.GetTempPath() + guid + ".html"
let fileSystem = new PhysicalFileSystem(root)
let options =
new FileServerOptions(
EnableDirectoryBrowsing = true,
FileSystem = fileSystem
)
type Startup() =
member __.Configuration(app:IAppBuilder) =
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll) |> ignore
app.MapSignalR() |> ignore
app.UseFileServer(options) |> ignore
type ReloadHub() =
inherit Hub()
let reloadHub = GlobalHost.ConnectionManager.GetHubContext<ReloadHub>()
let template =
"""<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Google Chart</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/signalr/jquery.signalr-2.0.2.min.js"></script>
<script src="http://localhost:8084/signalr/hubs" type="text/javascript"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script>
var connection = $.hubConnection();
var contosoChatHubProxy = connection.createHubProxy('reloadHub');
contosoChatHubProxy.on('reload', function(name, message) {
console.log(name + ' ' + message);
location.reload();
});
connection.start();
</script>
<script type="text/javascript">
google.load("visualization", "1.1", { packages: ["corechart", "annotationchart", "calendar", "gauge", "geochart", "map", "sankey", "table", "timeline", "treemap"] })
</script>
</head>
<body>
{CHART}
</body>
</html>"""
// from http://fssnip.net/2U
open System
open System.Runtime.CompilerServices
open Microsoft.CSharp.RuntimeBinder
let (?) (inst:obj) name (arg:'T) : 'R =
let convertSite =
CallSite<Func<CallSite, Object, 'R>>.Create
(Binder.Convert(CSharpBinderFlags.None, typeof<'R>, null))
let callSite =
CallSite<Func<CallSite, Object, 'T, Object>>.Create
(Binder.InvokeMember
( CSharpBinderFlags.None, name, null, null,
[| CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null);
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) |]))
convertSite.Target.Invoke
(convertSite, callSite.Target.Invoke(callSite, inst, arg))
open System.IO
open Foogle
open Foogle.SimpleHttp
open System.Diagnostics
let server = ref None
let tempDir = Path.GetTempFileName()
let pid = System.Diagnostics.Process.GetCurrentProcess().Id
let counter = ref 1
do File.Delete(tempDir)
do Directory.CreateDirectory(tempDir) |> ignore
let displayHtml html =
File.WriteAllText(htmlFile, html)
match server.Value with
| None ->
let app = WebApp.Start<Startup>(address)
Process.Start(address + guid + ".html") |> ignore
server := Some app
| _ -> reloadHub.Clients.All?reload() |> ignore
fsi.AddPrinter(fun (chart:FoogleChart) ->
chart
|> Foogle.Formatting.Google.CreateGoogleChart
|> Foogle.Formatting.Google.GoogleChartHtml
|> displayHtml
"(Foogle Chart)" )
fsi.AddPrinter(fun (chart:XPlot.GoogleCharts.GoogleChart) ->
let ch = chart |> XPlot.GoogleCharts.Chart.WithSize (800, 600)
let html = template.Replace("{CHART}", ch.InlineHtml)
displayHtml html
"(Google Chart)")
fsi.AddPrinter(fun (chart:XPlot.Plotly.Figure) ->
let name =
match chart.Layout with
| Some ly -> ly.title
| None -> sprintf "XPlot Generated Chart %d" counter.Value
chart.Width <- 800
chart.Height <- 600
"""<!DOCTYPE html>
<html>
<head><title>Plotly Chart</title></head>
<body>""" + chart.GetInlineHtml(name) + "</body></html>" |> displayHtml
"(Plotly Chart)" )
#endif
namespace FSharp.Charting
open FSharp.Charting
open Deedle
[<AutoOpen>]
module FsLabExtensions =
type FSharp.Charting.Chart with
static member Line(data:Series<'K, 'V>, ?Name, ?Title, ?Labels, ?Color, ?XTitle, ?YTitle) =
Chart.Line(Series.observations data, ?Name=Name, ?Title=Title, ?Labels=Labels, ?Color=Color, ?XTitle=XTitle, ?YTitle=YTitle)
static member Column(data:Series<'K, 'V>, ?Name, ?Title, ?Labels, ?Color, ?XTitle, ?YTitle) =
Chart.Column(Series.observations data, ?Name=Name, ?Title=Title, ?Labels=Labels, ?Color=Color, ?XTitle=XTitle, ?YTitle=YTitle)
static member Pie(data:Series<'K, 'V>, ?Name, ?Title, ?Labels, ?Color, ?XTitle, ?YTitle) =
Chart.Pie(Series.observations data, ?Name=Name, ?Title=Title, ?Labels=Labels, ?Color=Color, ?XTitle=XTitle, ?YTitle=YTitle)
static member Area(data:Series<'K, 'V>, ?Name, ?Title, ?Labels, ?Color, ?XTitle, ?YTitle) =
Chart.Area(Series.observations data, ?Name=Name, ?Title=Title, ?Labels=Labels, ?Color=Color, ?XTitle=XTitle, ?YTitle=YTitle)
static member Bar(data:Series<'K, 'V>, ?Name, ?Title, ?Labels, ?Color, ?XTitle, ?YTitle) =
Chart.Bar(Series.observations data, ?Name=Name, ?Title=Title, ?Labels=Labels, ?Color=Color, ?XTitle=XTitle, ?YTitle=YTitle)
namespace Foogle
open Deedle
[<AutoOpen>]
module FoogleExtensions =
type Foogle.Chart with
static member PieChart(frame:Frame<_, _>, column, ?Label, ?PieHole) =
Foogle.Chart.PieChart
( frame.GetColumn<float>(column) |> Series.observations,
?Label=Label, ?PieHole=PieHole)
static member GeoChart(frame:Frame<_, _>, column, ?Label, ?Region, ?DisplayMode) =
Foogle.Chart.GeoChart
( frame.GetColumn<float>(column) |> Series.observations,
?Label=Label, ?Region=Region, ?DisplayMode=DisplayMode)
namespace MathNet.Numerics.LinearAlgebra
open MathNet.Numerics.LinearAlgebra
open Deedle
module Matrix =
let inline toFrame matrix = matrix |> Matrix.toArray2 |> Frame.ofArray2D
module DenseMatrix =
let inline ofFrame frame = frame |> Frame.toArray2D |> DenseMatrix.ofArray2
module SparseMatrix =
let inline ofFrame frame = frame |> Frame.toArray2D |> SparseMatrix.ofArray2
module Vector =
let inline toSeries vector = vector |> Vector.toSeq |> Series.ofValues
module DenseVector =
let inline ofSeries series = series |> Series.values |> Seq.map (float) |> DenseVector.ofSeq
module SparseVector =
let inline ofSeries series = series |> Series.values |> Seq.map (float) |> SparseVector.ofSeq
namespace Deedle
open Deedle
open MathNet.Numerics.LinearAlgebra
module Frame =
let inline ofMatrix matrix = matrix |> Matrix.toArray2 |> Frame.ofArray2D
let inline toMatrix frame = frame |> Frame.toArray2D |> DenseMatrix.ofArray2
let ofCsvRows (data:FSharp.Data.Runtime.CsvFile<'T>) =
match data.Headers with
| None -> Frame.ofRecords data.Rows
| Some names -> Frame.ofRecords data.Rows |> Frame.indexColsWith names
module Series =
let inline ofVector vector = vector |> Vector.toSeq |> Series.ofValues
let inline toVector series = series |> Series.values |> Seq.map (float) |> DenseVector.ofSeq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment