Tuesday, June 14, 2011

F# Active Parttern to break down data



// break down into tuple
open System.Drawing
let (|RGB|) (col:System.Drawing.Color) = (col.R, col.G, col.B)
let (|HSB|) (col:System.Drawing.Color) = (col.GetHue(), col.GetSaturation(),col.GetBrightness())

//parsing
open System
let (|Int|_|)(x:string) =
let mutable y =0.0
if System.Double.TryParse(x, &y) then Some(x)
else None

let PrintRGB x =
match x with
| RGB (r,g,b) -> printfn"%d %d %d" r g b

let Parse x =
match x with
| Int t -> printfn "%A" t
| _ -> printfn "not integer"


Sunday, June 12, 2011

F# Discriminated Union as Option Type



type Options<'a> =
| Some of 'a
| None

let op1 = Some(10.0)
let op2 = None
let op3 = Options.Some("adf")

let ShowOption x =
match x with
| None ->printfn "%A" "Nothing"
| Some t ->printfn "%A" t

ShowOption op1
ShowOption op2
ShowOption op3

Saturday, June 11, 2011

F# Finding Prime Number



let IsNotMultipleOf n x =
x=n || x%n<>0

let rec RemoveAllMultiples listn listx =
match listn with
| head::tail -> RemoveAllMultiples tail (List.filter (IsNotMultipleOf head) listx)
| [] -> listx

let GetPrimeTo n =
let r= (int) ( sqrt(float n))
RemoveAllMultiples [2..r] [1..100]

printfn "%A" (GetPrimeTo 100)

Wednesday, June 8, 2011

F# Async Workflow


#light
open System.IO
open System.Net
open System

let AyncHttp(url:string) =
async {
let req=WebRequest.Create(url)
let! rsp=req.AsyncGetResponse()
use s= rsp.GetResponseStream()
use r= new System.IO.StreamReader(s)
r.ReadToEnd()
}

//let SyncHttp(url:string) =
// let req=WebRequest.Create(url)
// let rsp=req.GetResponse()
// use s= rsp.GetResponseStream()
// use r= new System.IO.StreamReader(s)
// r.ReadToEnd()



let dt=System.DateTime.Now

//SyncHttp "http://maps.google.com"
//SyncHttp "http://maps.live.com"
//SyncHttp("http://www.nhl.com");
//SyncHttp("http://www.nfl.com");
//SyncHttp("http://www.china.com");
//SyncHttp("http://www.sina.com");
//SyncHttp("http://www.nba.com");
//printf "%A" (DateTime.Now-dt).Milliseconds

Async.Parallel [
AyncHttp("http://maps.google.com");
AyncHttp("http://maps.live.com");
AyncHttp("http://www.nhl.com");
AyncHttp("http://www.nfl.com");
AyncHttp("http://www.china.com");
AyncHttp("http://www.sina.com");
AyncHttp("http://www.nba.com");
]
|> Async.RunSynchronously

printf "%A" (DateTime.Now-dt).Milliseconds

Thursday, May 26, 2011

TreeMap and Zoom Control onto TreeGraph (WPF Toolkit and Extension)


WPF ToolKit and CodePlex WPF Extension

<Window x:Class="GMO.Monitor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
xmlns:dv="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
xmlns:tc="clr-namespace:TreeContainer;assembly=TreeContainer"
xmlns:wpfec="clr-namespace:WPFExtensions.Controls;assembly=WPFExtensions"
x:Name="M"
>
<Grid >
<!--<WrapPanel>-->
<DockPanel>
<dv:TreeMap Name="tmp1" Height="Auto" Width="200" >
<dv:TreeMap.Interpolators>
<dv:SolidColorBrushInterpolator
TargetName="bdr"
TargetProperty="Background"
DataRangeBinding="{Binding Path=totalValue}"
From="Green"
To="White"/>
</dv:TreeMap.Interpolators>
<dv:TreeMap.ItemDefinition>
<dv:TreeMapItemDefinition
ItemsSource="{Binding}"
ValueBinding="{Binding Path=totalValue}"
>
<DataTemplate>
<Border BorderThickness="1" BorderBrush="Black" Name="bdr" MouseDown="bdr_MouseDown" ToolTip="{Binding Path=ToolTip}" >
<TextBlock Text="{Binding Path=shortname}" VerticalAlignment="Center" Margin="2,2,0,0" FontSize="10" />
</Border>
</DataTemplate>
</dv:TreeMapItemDefinition>
</dv:TreeMap.ItemDefinition>
</dv:TreeMap>
<wpfec:ZoomControl >

<Grid Width="1000" Height="1000" Background="Beige">
<Canvas>
<tc:TreeContainer tc:TreeContainer.Root="O" HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="tc1" HorizontalBuffer="38" VerticalBuffer="48" HorizontalBufferSubtree="40" VerticalJustification="top">

</tc:TreeContainer>
</Canvas>
</Grid>
</wpfec:ZoomControl>
</DockPanel>
<!--</WrapPanel>-->
</Grid>
</Window>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.DataVisualization.Charting;
using System.Windows.Controls.DataVisualization;
using GMO.Monitor.Model;
using TreeContainer;
using System.Collections.ObjectModel;

namespace GMO.Monitor
{
///
/// Interaction logic for MainWindow.xaml
///

public partial class MainWindow : Window
{
ApexDevEntities ctx = new ApexDevEntities();
public MainWindow()
{
InitializeComponent();
var q = from t in ctx.GetDashboard() select new { t.shortname, t.totalValue, t.groupid, ToolTip = t.shortname + "," + t.groupid.ToString() };
tmp1.ItemsSource = q;
}

protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
//vb.Width = this.ActualWidth - tmp1.ActualWidth; ;
//vb.Height = this.ActualHeight - tmp1.ActualHeight;
base.OnRenderSizeChanged(sizeInfo);
}

private void bdr_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount <= 1) return;

Border bdr = sender as Border;
string tip = bdr.ToolTip.ToString();
int GrpId = Convert.ToInt32(tip.Split(',')[1]);
TextBlock tb = bdr.Child as TextBlock;
tc1.Clear();
TreeNode root= tc1.AddRoot(tb.Text);
var q = from t in ctx.CustomGroups where t.RelatedGroupId == GrpId select t;
foreach (var q1 in q)
{
TextBlock tb1 = new TextBlock() { Text = q1.CustomGroupName};
TreeNode tn= tc1.AddNode(tb1, root);
tn.Tag = q.FirstOrDefault();
// tn.MouseDoubleClick += new MouseButtonEventHandler(tn_MouseDoubleClick);
// TreeNode tn = sender as TreeNode;
DateTime dt = new DateTime(2011, 2, 1);
string subCGstr = "a,0,b,1,c,2";
var q2 = from t in ctx.GetCustomGroupExposures(dt, q1.CustomGroupId, true, subCGstr) select t;
foreach (var q3 in q2)
{
TextBlock tb3 = new TextBlock() { Text = q3.Bet + "\r\n " + q3.Exposure.ToString() + "\r\n " + q3.PercentExposure.ToString() };
TreeNode tn1 = tc1.AddNode(tb3, tn);
}
}
}

void tn_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
tc1.Clear();
TreeNode tn = sender as TreeNode;
CustomGroup cg = tn.Tag as CustomGroup;
TextBlock tb= new TextBlock() { Text= cg.CustomGroupName};
TreeNode root = tc1.AddRoot(tb);
DateTime dt = new DateTime(2011,2,1);
var q = from t in ctx.GetCustomGroupExposures(dt, cg.CustomGroupId, true, "") select t;
foreach (var q1 in q)
{
TextBlock tb1 = new TextBlock() { Text = q1.Bet+" "+q1.Exposure.ToString() +" "+q1.PercentExposure.ToString() };
TreeNode tn1 = tc1.AddNode(tb1, root);
}
}
}

public class Data1
{
public string shortname { get; set; }
public double totalValue { get; set; }
}
}

Saturday, April 23, 2011

Generating C# code from Shader File (WIP)

Shazzam tools generate C# code

Sunday, April 17, 2011

Data Visualization



1. Quiet Status on Dashboard should have non-alerting color

2. Single Number should be communicated with alerting color, Icon graphic ( tiny, "SparkCon ???")

3. Large data set are good for Line Chart Period Analysis. Small data set mush rely on Bar Chart.

4. Period comparison should not use line chart overlay. Comparison= Delta of %

5. How to Breakdown a variable:
Human are good in visualizing Height no area plot, unit plot, Pie chart ( need to calculate area, counting, pizza size w/o number). Sorted Bar chart are better.

6.Breakdown over a period of time:
Bar chart side by side comparison with different color will show difference and trend
Stack line or bar are not clear.

7. How to visuaize Category:
Map, Shapes, Color, Lines

8. How to visualize Quantity:
Size, Count, Speed, Color Intensity, Height Comparison.

9. Typical usage: Cluster Point Chart, Bar with Median, Mean, Sorting, Markup Chain Workflow Graph. Highlighting GridCell, Projectin, Targeting

10. Other techniques: Click-through, Grouping, Multi-factor, Summary and Rating, Confidence Level to filter out outliers