Showing posts with label SQL server. Show all posts
Showing posts with label SQL server. Show all posts

Wednesday, May 7, 2014

Simple plots in Microsoft Sho


MS Sho is *really* kool. 

Been looking for a while for something like this. With all the fuss being made about data and stats these days it seemed odd to me that the MS camp didn't put out some r/ python-esque data analysis tools. Turns out I just wasn't looking in the right places. "Sho: the .NET Playground for Data" is a nifty, .net-friendly data analysis environment from MS research. It has a kool, R-feeling, console that makes for speedy data exploration.


From the start menu


Sho console
So first of all, data. Getting it into Sho is super easy. 

Generating data: Nifty random functions to generate data:

>>> x = rand(20)
>>> x
[ 0.9505  0.7515  0.7579  0.6926  0.7209  0.2979  0.3866  0.4590  ...4 cols...  0.5072  0.6721  0.9819  0.2756  0.0660  0.1953  0.9479  0.1866]

Reading data from a delimited file: 
Use 'dlmreadJaggedArray' with a specified delimiter



Reading data from SQL server (of course!): 


Easiest thing ever - connecting to a local, default instance, using windows authentication, 



Ok so now some simple plots:

>>> x = rand(20)
>>> y = rand(20)
>>> plot(x)





>>> plot(x,'.')




>>> plot(x,y,'.')




>>> bar(x)




>>> hist(x)


Notice the awesome "number of bins" slider!!



so these commands pull up a new window with the plots, however!! you can use some 'show' commands to view the plots in the console itself (except for the histogram :/ )





So that's some Sho basics, but what's kool is that you can do all this from other .net languages!!


Here's how you make a histogram from c#:

            DoubleArray y1 = ArrayRandom.RandomDoubleArray(20);

            //histogram control
            ShoHistControl hist1 = new ShoHistControl(y1, 6);
            hist1.Width = 620;
            hist1.Height = 460;
            hist1.Location = new Point(1, 1);

            //form to display histogram control
            Form f1 = new Form();
            f1.Text = "Histogram 1";
            f1.Width = 640;
            f1.Height = 480;
            f1.MaximizeBox = false;
            
            f1.Controls.Add(hist1);

            f1.ShowDialog();


produces this:






basic scatter/ line/ bar plots are easier, you don't need to create a form to contain it, so this is all you need:

            int[] x = { 1, 2, 2, 8, 2, 3, 2, 1 };
            int[] y = { 1, 2, 3, 4, 5, 6, 7, 8 };

            IFigure fig1 = ShoPlotHelper.Figure();


            fig1.Plot(x, y, ".");

to produce this!:






So long story short, MS Sho is super fun and I for one am going to continue finding reasons to use it. One of the biggest advantages in my opinion is now I have a simple option for making quick, stand-alone dashboard applications which are easy to distribute to clients, something which was trickier to do with R.

Anyway folks, hope this is helpful to someone, any questions/ comments/ corrections are surely welcome.

oh P.S!! the guys at MS research did a great job of documenting all this, you can find out everything you need to know about Sho at their documentation page


update:

Plot from powershell!:

add-type -Path "C:\Program Files (x86)\Sho 2.1\bin\ShoArray.dll"
add-type -Path "C:\Program Files (x86)\Sho 2.1\bin\ShoViz.dll"

$r = 2.7
$x = 0.7
$x1 = 0

#number of iterations
$i=100

$i1 = $i

$points = [ShoNS.Array.FloatArray]::Zeros($i)
$xAxis = @()

while($i -gt 0){
    $x1 = $r * ($x - ($x * $x) )
    $points[$i1-$i] = $x1
    $xAxis += @($i1-$i)
    $x = [System.Decimal]::Parse($x1)
    $i --
}

$vis = [ShoNS.Visualization.ShoPlotHelper]::Figure()

$vis.Plot($xAxis, $points)


Friday, December 23, 2011

Put it *where*?


disclaimer: this is kind of pointless. read at your own
risk

--------------------------------------------------------------------------------------

So let’s put a database in an alternate data stream. for some reason. Furthermore let’s make it all sneaky and make it look like some random unsuspicious system file, like thumbs.db :

CREATE DATABASE [hiddendb] ON PRIMARY


( NAME = N'hiddendb', FILENAME = N'D:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\thumbs.db:mdf' , SIZE = 3072KB , FILEGROWTH = 1024KB )

LOG ON


( NAME = N'hiddendb_log', FILENAME = N'D:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\thumbs.db:ldf' , SIZE = 1024KB , FILEGROWTH = 10%)

GO

Holy crap, that actually worked somehow…




Can we put data in it though?

use hiddendb


go

select *

into dbo.randomrows

from spt.dbo.exported, spt.conversion.modeloff --some other tables

(343917 row(s) affected)


yup, seems so. (column names have been hidden to protect innocent).

But wait…


My thumbs.db file still only shows up as 0 KB! it totally isn't though, you can see it's real size in sys.database_files

So anyway that gets me thinking….said database is in an alternate data stream. SQL server uses alternate data streams when running checkdb. How would that work with this fancy db?

Msg 1823, Level 16, State 2, Line 1

A database snapshot cannot be created because it failed to start.

Msg 5123, Level 16, State 1, Line 1

CREATE FILE encountered operating system error 123(The filename, directory name, or volume label syntax is incorrect.) while attempting to open or create the physical file 'D:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\thumbs.db:mdf:MSSQL_DBCC26'.

Msg 7928, Level 16, State 1, Line 1

The database snapshot for online checks could not be created. Either the reason is given in a previous error or one of the underlying volumes does not support sparse files or alternate streams. Attempting to get exclusive access to run checks offline.

DBCC results for 'hiddendb'.

so that was fun! hope you find this interesting!